VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c@ 33684

Last change on this file since 33684 was 33684, checked in by vboxsync, 14 years ago

wddm/3d: fix hgcm fallback (for insufficient hgsmi resources)

  • Property svn:eol-style set to native
File size: 87.2 KB
Line 
1/*
2 * Context and render target management in wined3d
3 *
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
5 * Copyright 2009 Henri Verbeet for CodeWeavers
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22/*
23 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
24 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
25 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
26 * a choice of LGPL license versions is made available with the language indicating
27 * that LGPLv2 or any later version may be used, or where a choice of which version
28 * of the LGPL is applied is otherwise unspecified.
29 */
30
31#include "config.h"
32#include <stdio.h>
33#ifdef HAVE_FLOAT_H
34# include <float.h>
35#endif
36#include "wined3d_private.h"
37
38WINE_DEFAULT_DEBUG_CHANNEL(d3d);
39
40#define GLINFO_LOCATION (*gl_info)
41
42static DWORD wined3d_context_tls_idx;
43
44/* FBO helper functions */
45
46/* GL locking is done by the caller */
47void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint *fbo)
48{
49 const struct wined3d_gl_info *gl_info = context->gl_info;
50 GLuint f;
51
52 if (!fbo)
53 {
54 f = 0;
55 }
56 else
57 {
58 if (!*fbo)
59 {
60 gl_info->fbo_ops.glGenFramebuffers(1, fbo);
61 checkGLcall("glGenFramebuffers()");
62 TRACE("Created FBO %u.\n", *fbo);
63 }
64 f = *fbo;
65 }
66
67 switch (target)
68 {
69 case GL_READ_FRAMEBUFFER:
70 if (context->fbo_read_binding == f) return;
71 context->fbo_read_binding = f;
72 break;
73
74 case GL_DRAW_FRAMEBUFFER:
75 if (context->fbo_draw_binding == f) return;
76 context->fbo_draw_binding = f;
77 break;
78
79 case GL_FRAMEBUFFER:
80 if (context->fbo_read_binding == f
81 && context->fbo_draw_binding == f) return;
82 context->fbo_read_binding = f;
83 context->fbo_draw_binding = f;
84 break;
85
86 default:
87 FIXME("Unhandled target %#x.\n", target);
88 break;
89 }
90
91 gl_info->fbo_ops.glBindFramebuffer(target, f);
92 checkGLcall("glBindFramebuffer()");
93}
94
95/* GL locking is done by the caller */
96static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info)
97{
98 unsigned int i;
99
100 for (i = 0; i < gl_info->limits.buffers; ++i)
101 {
102 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
103 checkGLcall("glFramebufferTexture2D()");
104 }
105 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
106 checkGLcall("glFramebufferTexture2D()");
107
108 gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
109 checkGLcall("glFramebufferTexture2D()");
110}
111
112/* GL locking is done by the caller */
113static void context_destroy_fbo(struct wined3d_context *context, GLuint *fbo)
114{
115 const struct wined3d_gl_info *gl_info = context->gl_info;
116
117 context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
118 context_clean_fbo_attachments(gl_info);
119 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
120
121 gl_info->fbo_ops.glDeleteFramebuffers(1, fbo);
122 checkGLcall("glDeleteFramebuffers()");
123}
124
125/* GL locking is done by the caller */
126static void context_apply_attachment_filter_states(IWineD3DSurfaceImpl *surface)
127{
128 IWineD3DBaseTextureImpl *texture_impl;
129
130 /* Update base texture states array */
131 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)surface,
132 &IID_IWineD3DBaseTexture, (void **)&texture_impl)))
133 {
134 IWineD3DDeviceImpl *device = surface->resource.device;
135 BOOL update_minfilter = FALSE;
136 BOOL update_magfilter = FALSE;
137
138 if (texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_POINT
139 || texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_NONE)
140 {
141 texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
142 texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
143 update_minfilter = TRUE;
144 }
145
146 if (texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_POINT)
147 {
148 texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
149 update_magfilter = TRUE;
150 }
151
152 if (texture_impl->baseTexture.bindCount)
153 {
154 WARN("Render targets should not be bound to a sampler\n");
155 IWineD3DDeviceImpl_MarkStateDirty(device, STATE_SAMPLER(texture_impl->baseTexture.sampler));
156 }
157
158 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture_impl);
159
160 if (update_minfilter || update_magfilter)
161 {
162 GLenum target, bind_target;
163 GLint old_binding;
164
165 target = surface->texture_target;
166 if (target == GL_TEXTURE_2D)
167 {
168 bind_target = GL_TEXTURE_2D;
169 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
170 }
171 else if (target == GL_TEXTURE_RECTANGLE_ARB)
172 {
173 bind_target = GL_TEXTURE_RECTANGLE_ARB;
174 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
175 }
176 else
177 {
178 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
179 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB, &old_binding);
180 }
181
182 glBindTexture(bind_target, surface->texture_name);
183 if (update_minfilter) glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
184 if (update_magfilter) glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
185 glBindTexture(bind_target, old_binding);
186 }
187
188 checkGLcall("apply_attachment_filter_states()");
189 }
190}
191
192/* GL locking is done by the caller */
193void context_attach_depth_stencil_fbo(struct wined3d_context *context,
194 GLenum fbo_target, IWineD3DSurfaceImpl *depth_stencil, BOOL use_render_buffer)
195{
196 const struct wined3d_gl_info *gl_info = context->gl_info;
197
198 TRACE("Attach depth stencil %p\n", depth_stencil);
199
200 if (depth_stencil)
201 {
202 DWORD format_flags = depth_stencil->resource.format_desc->Flags;
203
204 if (use_render_buffer && depth_stencil->current_renderbuffer)
205 {
206 if (format_flags & WINED3DFMT_FLAG_DEPTH)
207 {
208 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT,
209 GL_RENDERBUFFER, depth_stencil->current_renderbuffer->id);
210 checkGLcall("glFramebufferRenderbuffer()");
211 }
212
213 if (format_flags & WINED3DFMT_FLAG_STENCIL)
214 {
215 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT,
216 GL_RENDERBUFFER, depth_stencil->current_renderbuffer->id);
217 checkGLcall("glFramebufferRenderbuffer()");
218 }
219 }
220 else
221 {
222 surface_prepare_texture(depth_stencil, gl_info, FALSE);
223 context_apply_attachment_filter_states(depth_stencil);
224
225 if (format_flags & WINED3DFMT_FLAG_DEPTH)
226 {
227 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT,
228 depth_stencil->texture_target, depth_stencil->texture_name,
229 depth_stencil->texture_level);
230 checkGLcall("glFramebufferTexture2D()");
231 }
232
233 if (format_flags & WINED3DFMT_FLAG_STENCIL)
234 {
235 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT,
236 depth_stencil->texture_target, depth_stencil->texture_name,
237 depth_stencil->texture_level);
238 checkGLcall("glFramebufferTexture2D()");
239 }
240 }
241
242 if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
243 {
244 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
245 checkGLcall("glFramebufferTexture2D()");
246 }
247
248 if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
249 {
250 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
251 checkGLcall("glFramebufferTexture2D()");
252 }
253 }
254 else
255 {
256 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
257 checkGLcall("glFramebufferTexture2D()");
258
259 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
260 checkGLcall("glFramebufferTexture2D()");
261 }
262}
263
264/* GL locking is done by the caller */
265void context_attach_surface_fbo(const struct wined3d_context *context,
266 GLenum fbo_target, DWORD idx, IWineD3DSurfaceImpl *surface)
267{
268 const struct wined3d_gl_info *gl_info = context->gl_info;
269
270 TRACE("Attach surface %p to %u\n", surface, idx);
271
272 if (surface)
273 {
274 surface_prepare_texture(surface, gl_info, FALSE);
275 context_apply_attachment_filter_states(surface);
276
277 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, surface->texture_target,
278 surface->texture_name, surface->texture_level);
279 checkGLcall("glFramebufferTexture2D()");
280 }
281 else
282 {
283 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, GL_TEXTURE_2D, 0, 0);
284 checkGLcall("glFramebufferTexture2D()");
285 }
286}
287
288/* GL locking is done by the caller */
289static void context_check_fbo_status(struct wined3d_context *context)
290{
291 const struct wined3d_gl_info *gl_info = context->gl_info;
292 GLenum status;
293
294 status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
295 if (status == GL_FRAMEBUFFER_COMPLETE)
296 {
297 TRACE("FBO complete\n");
298 } else {
299 IWineD3DSurfaceImpl *attachment;
300 unsigned int i;
301 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
302
303 if (!context->current_fbo)
304 {
305 ERR("FBO 0 is incomplete, driver bug?\n");
306 return;
307 }
308
309 /* Dump the FBO attachments */
310 for (i = 0; i < gl_info->limits.buffers; ++i)
311 {
312 attachment = context->current_fbo->render_targets[i];
313 if (attachment)
314 {
315 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
316 i, attachment, debug_d3dformat(attachment->resource.format_desc->format),
317 attachment->pow2Width, attachment->pow2Height);
318 }
319 }
320 attachment = context->current_fbo->depth_stencil;
321 if (attachment)
322 {
323 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
324 attachment, debug_d3dformat(attachment->resource.format_desc->format),
325 attachment->pow2Width, attachment->pow2Height);
326 }
327 }
328}
329
330static struct fbo_entry *context_create_fbo_entry(struct wined3d_context *context)
331{
332 const struct wined3d_gl_info *gl_info = context->gl_info;
333#ifdef VBOX_WITH_WDDM
334 IWineD3DDeviceImpl *device = context->device;
335#else
336 IWineD3DDeviceImpl *device = context->swapchain->device;
337#endif
338 struct fbo_entry *entry;
339
340 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
341 entry->render_targets = HeapAlloc(GetProcessHeap(), 0, gl_info->limits.buffers * sizeof(*entry->render_targets));
342 memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
343 entry->depth_stencil = (IWineD3DSurfaceImpl *)device->stencilBufferTarget;
344 entry->attached = FALSE;
345 entry->id = 0;
346
347 return entry;
348}
349
350/* GL locking is done by the caller */
351static void context_reuse_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
352{
353 const struct wined3d_gl_info *gl_info = context->gl_info;
354#ifdef VBOX_WITH_WDDM
355 IWineD3DDeviceImpl *device = context->device;
356#else
357 IWineD3DDeviceImpl *device = context->swapchain->device;
358#endif
359
360 context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
361 context_clean_fbo_attachments(gl_info);
362
363 memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
364 entry->depth_stencil = (IWineD3DSurfaceImpl *)device->stencilBufferTarget;
365 entry->attached = FALSE;
366}
367
368/* GL locking is done by the caller */
369static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
370{
371 if (entry->id)
372 {
373 TRACE("Destroy FBO %d\n", entry->id);
374 context_destroy_fbo(context, &entry->id);
375 }
376 --context->fbo_entry_count;
377 list_remove(&entry->entry);
378 HeapFree(GetProcessHeap(), 0, entry->render_targets);
379 HeapFree(GetProcessHeap(), 0, entry);
380}
381
382
383/* GL locking is done by the caller */
384static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context)
385{
386 const struct wined3d_gl_info *gl_info = context->gl_info;
387#ifdef VBOX_WITH_WDDM
388 IWineD3DDeviceImpl *device = context->device;
389#else
390 IWineD3DDeviceImpl *device = context->swapchain->device;
391#endif
392 struct fbo_entry *entry;
393
394 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
395 {
396 if (!memcmp(entry->render_targets,
397 device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets))
398 && entry->depth_stencil == (IWineD3DSurfaceImpl *)device->stencilBufferTarget)
399 {
400 list_remove(&entry->entry);
401 list_add_head(&context->fbo_list, &entry->entry);
402 return entry;
403 }
404 }
405
406 if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
407 {
408 entry = context_create_fbo_entry(context);
409 list_add_head(&context->fbo_list, &entry->entry);
410 ++context->fbo_entry_count;
411 }
412 else
413 {
414 entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
415 context_reuse_fbo_entry(context, entry);
416 list_remove(&entry->entry);
417 list_add_head(&context->fbo_list, &entry->entry);
418 }
419
420 return entry;
421}
422
423/* GL locking is done by the caller */
424static void context_apply_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
425{
426 const struct wined3d_gl_info *gl_info = context->gl_info;
427#ifdef VBOX_WITH_WDDM
428 IWineD3DDeviceImpl *device = context->device;
429#else
430 IWineD3DDeviceImpl *device = context->swapchain->device;
431#endif
432 unsigned int i;
433
434 context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
435
436 if (!entry->attached)
437 {
438 /* Apply render targets */
439 for (i = 0; i < gl_info->limits.buffers; ++i)
440 {
441 IWineD3DSurfaceImpl *render_target = (IWineD3DSurfaceImpl *)device->render_targets[i];
442 context_attach_surface_fbo(context, GL_FRAMEBUFFER, i, render_target);
443 }
444
445 /* Apply depth targets */
446 if (device->stencilBufferTarget)
447 {
448 unsigned int w = ((IWineD3DSurfaceImpl *)device->render_targets[0])->pow2Width;
449 unsigned int h = ((IWineD3DSurfaceImpl *)device->render_targets[0])->pow2Height;
450
451 surface_set_compatible_renderbuffer(device->stencilBufferTarget, w, h);
452 }
453 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, (IWineD3DSurfaceImpl *)device->stencilBufferTarget, TRUE);
454
455 entry->attached = TRUE;
456 }
457 else
458 {
459 for (i = 0; i < gl_info->limits.buffers; ++i)
460 {
461 if (device->render_targets[i])
462 context_apply_attachment_filter_states((IWineD3DSurfaceImpl *)device->render_targets[i]);
463 }
464 if (device->stencilBufferTarget)
465 context_apply_attachment_filter_states((IWineD3DSurfaceImpl *)device->stencilBufferTarget);
466 }
467
468 for (i = 0; i < gl_info->limits.buffers; ++i)
469 {
470 if (device->render_targets[i])
471 device->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
472 else
473 device->draw_buffers[i] = GL_NONE;
474 }
475}
476
477/* GL locking is done by the caller */
478static void context_apply_fbo_state(struct wined3d_context *context)
479{
480 struct fbo_entry *entry, *entry2;
481
482 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
483 {
484 context_destroy_fbo_entry(context, entry);
485 }
486
487 if (context->rebind_fbo)
488 {
489 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
490 context->rebind_fbo = FALSE;
491 }
492
493 if (context->render_offscreen)
494 {
495 context->current_fbo = context_find_fbo_entry(context);
496 context_apply_fbo_entry(context, context->current_fbo);
497 } else {
498 context->current_fbo = NULL;
499 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
500 }
501
502#if defined(DEBUG) && !defined(DEBUG_misha)
503 context_check_fbo_status(context);
504#endif
505}
506
507/* Context activation is done by the caller. */
508void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
509{
510 const struct wined3d_gl_info *gl_info = context->gl_info;
511
512 if (context->free_occlusion_query_count)
513 {
514 query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
515 }
516 else
517 {
518 if (gl_info->supported[ARB_OCCLUSION_QUERY])
519 {
520 ENTER_GL();
521 GL_EXTCALL(glGenQueriesARB(1, &query->id));
522 checkGLcall("glGenQueriesARB");
523 LEAVE_GL();
524
525 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
526 }
527 else
528 {
529 WARN("Occlusion queries not supported, not allocating query id.\n");
530 query->id = 0;
531 }
532 }
533
534 query->context = context;
535 list_add_head(&context->occlusion_queries, &query->entry);
536}
537
538void context_free_occlusion_query(struct wined3d_occlusion_query *query)
539{
540 struct wined3d_context *context = query->context;
541
542 list_remove(&query->entry);
543 query->context = NULL;
544
545 if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
546 {
547 UINT new_size = context->free_occlusion_query_size << 1;
548 GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
549 new_size * sizeof(*context->free_occlusion_queries));
550
551 if (!new_data)
552 {
553 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
554 return;
555 }
556
557 context->free_occlusion_query_size = new_size;
558 context->free_occlusion_queries = new_data;
559 }
560
561 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
562}
563
564/* Context activation is done by the caller. */
565void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
566{
567 const struct wined3d_gl_info *gl_info = context->gl_info;
568
569 if (context->free_event_query_count)
570 {
571 query->object = context->free_event_queries[--context->free_event_query_count];
572 }
573 else
574 {
575 if (gl_info->supported[ARB_SYNC])
576 {
577 /* Using ARB_sync, not much to do here. */
578 query->object.sync = NULL;
579 TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
580 }
581 else if (gl_info->supported[APPLE_FENCE])
582 {
583 ENTER_GL();
584 GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
585 checkGLcall("glGenFencesAPPLE");
586 LEAVE_GL();
587
588 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
589 }
590 else if(gl_info->supported[NV_FENCE])
591 {
592 ENTER_GL();
593 GL_EXTCALL(glGenFencesNV(1, &query->object.id));
594 checkGLcall("glGenFencesNV");
595 LEAVE_GL();
596
597 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
598 }
599 else
600 {
601 WARN("Event queries not supported, not allocating query id.\n");
602 query->object.id = 0;
603 }
604 }
605
606 query->context = context;
607 list_add_head(&context->event_queries, &query->entry);
608}
609
610void context_free_event_query(struct wined3d_event_query *query)
611{
612 struct wined3d_context *context = query->context;
613
614 list_remove(&query->entry);
615 query->context = NULL;
616
617 if (context->free_event_query_count >= context->free_event_query_size - 1)
618 {
619 UINT new_size = context->free_event_query_size << 1;
620 union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
621 new_size * sizeof(*context->free_event_queries));
622
623 if (!new_data)
624 {
625 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
626 return;
627 }
628
629 context->free_event_query_size = new_size;
630 context->free_event_queries = new_data;
631 }
632
633 context->free_event_queries[context->free_event_query_count++] = query->object;
634}
635
636void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type)
637{
638 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
639 UINT i;
640
641 if (!This->d3d_initialized) return;
642
643 switch(type)
644 {
645 case WINED3DRTYPE_SURFACE:
646 {
647 for (i = 0; i < This->numContexts; ++i)
648 {
649 struct wined3d_context *context = This->contexts[i];
650 const struct wined3d_gl_info *gl_info = context->gl_info;
651 struct fbo_entry *entry, *entry2;
652
653 if (context->current_rt == (IWineD3DSurface *)resource) context->current_rt = NULL;
654
655 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
656 {
657 UINT j;
658
659 if (entry->depth_stencil == (IWineD3DSurfaceImpl *)resource)
660 {
661 list_remove(&entry->entry);
662 list_add_head(&context->fbo_destroy_list, &entry->entry);
663 continue;
664 }
665
666 for (j = 0; j < gl_info->limits.buffers; ++j)
667 {
668 if (entry->render_targets[j] == (IWineD3DSurfaceImpl *)resource)
669 {
670 list_remove(&entry->entry);
671 list_add_head(&context->fbo_destroy_list, &entry->entry);
672 break;
673 }
674 }
675 }
676 }
677
678 break;
679 }
680
681 default:
682 break;
683 }
684}
685
686void context_surface_update(struct wined3d_context *context, IWineD3DSurfaceImpl *surface)
687{
688 const struct wined3d_gl_info *gl_info = context->gl_info;
689 struct fbo_entry *entry = context->current_fbo;
690 unsigned int i;
691
692 if (!entry || context->rebind_fbo) return;
693
694 for (i = 0; i < gl_info->limits.buffers; ++i)
695 {
696 if (surface == entry->render_targets[i])
697 {
698 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface, i);
699 context->rebind_fbo = TRUE;
700 return;
701 }
702 }
703
704 if (surface == entry->depth_stencil)
705 {
706 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface);
707 context->rebind_fbo = TRUE;
708 }
709}
710
711static BOOL context_set_pixel_format(const struct wined3d_gl_info *gl_info, HDC dc, int format)
712{
713 int current = GetPixelFormat(dc);
714
715 if (current == format) return TRUE;
716
717 if (!current)
718 {
719 if (!SetPixelFormat(dc, format, NULL))
720 {
721 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
722 format, dc, GetLastError());
723 return FALSE;
724 }
725 return TRUE;
726 }
727
728 /* By default WGL doesn't allow pixel format adjustments but we need it
729 * here. For this reason there's a Wine specific wglSetPixelFormat()
730 * which allows us to set the pixel format multiple times. Only use it
731 * when really needed. */
732 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
733 {
734 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format, NULL)))
735 {
736 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
737 format, dc);
738 return FALSE;
739 }
740 return TRUE;
741 }
742
743 /* OpenGL doesn't allow pixel format adjustments. Print an error and
744 * continue using the old format. There's a big chance that the old
745 * format works although with a performance hit and perhaps rendering
746 * errors. */
747 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
748 format, dc, current);
749 return TRUE;
750}
751
752static void context_update_window(struct wined3d_context *context
753#ifdef VBOX_WITH_WDDM
754 , IWineD3DSwapChainImpl *swapchain
755#endif
756 )
757{
758#ifdef VBOX_WITH_WDDM
759 TRACE("Updating context %p window from %p to %p.\n",
760 context, context->win_handle, swapchain->win_handle);
761#else
762 TRACE("Updating context %p window from %p to %p.\n",
763 context, context->win_handle, context->swapchain->win_handle);
764#endif
765
766 if (context->valid)
767 {
768 if (!ReleaseDC(context->win_handle, context->hdc))
769 {
770 ERR("Failed to release device context %p, last error %#x.\n",
771 context->hdc, GetLastError());
772 }
773 }
774 else context->valid = 1;
775
776#ifdef VBOX_WITH_WDDM
777 context->win_handle = swapchain->win_handle;
778 context->currentSwapchain = swapchain;
779#else
780 context->win_handle = context->swapchain->win_handle;
781#endif
782 if (!(context->hdc = GetDC(context->win_handle)))
783 {
784 ERR("Failed to get a device context for window %p.\n", context->win_handle);
785 goto err;
786 }
787
788 if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
789 {
790 ERR("Failed to set pixel format %d on device context %p.\n",
791 context->pixel_format, context->hdc);
792 goto err;
793 }
794
795 if (!pwglMakeCurrent(context->hdc, context->glCtx))
796 {
797 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
798 context->glCtx, context->hdc, GetLastError());
799 goto err;
800 }
801
802 return;
803
804err:
805 context->valid = 0;
806}
807
808static void context_validate(struct wined3d_context *context
809#ifdef VBOX_WITH_WDDM
810 , IWineD3DSwapChainImpl *swapchain
811#endif
812 )
813{
814 HWND wnd = WindowFromDC(context->hdc);
815
816 if (wnd != context->win_handle)
817 {
818 WARN("DC %p belongs to window %p instead of %p.\n",
819 context->hdc, wnd, context->win_handle);
820 context->valid = 0;
821 }
822
823 if (
824#ifdef VBOX_WITH_WDDM
825 swapchain && context->win_handle != swapchain->win_handle
826#else
827 context->win_handle != context->swapchain->win_handle
828#endif
829 )
830 {
831 context_update_window(context
832#ifdef VBOX_WITH_WDDM
833 , swapchain
834#endif
835 );
836 }
837}
838
839static void context_destroy_gl_resources(struct wined3d_context *context)
840{
841 const struct wined3d_gl_info *gl_info = context->gl_info;
842 struct wined3d_occlusion_query *occlusion_query;
843 struct wined3d_event_query *event_query;
844 struct fbo_entry *entry, *entry2;
845 HGLRC restore_ctx;
846 HDC restore_dc;
847 unsigned int i;
848
849 restore_ctx = pwglGetCurrentContext();
850 restore_dc = pwglGetCurrentDC();
851
852 context_validate(context
853#ifdef VBOX_WITH_WDDM/* tmp work-around */
854 , NULL //(IWineD3DSwapChainImpl*)context->device->swapchains[context->device->NumberOfSwapChains-1]
855#endif
856 );
857 if (context->valid && restore_ctx != context->glCtx) pwglMakeCurrent(context->hdc, context->glCtx);
858 else restore_ctx = NULL;
859
860 ENTER_GL();
861
862 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
863 {
864 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
865 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
866 occlusion_query->context = NULL;
867 }
868
869 LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
870 {
871 if (context->valid)
872 {
873 if (gl_info->supported[ARB_SYNC])
874 {
875 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
876 }
877 else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
878 else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
879 }
880 event_query->context = NULL;
881 }
882
883 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
884 {
885 if (!context->valid) entry->id = 0;
886 context_destroy_fbo_entry(context, entry);
887 }
888
889 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
890 {
891 if (!context->valid) entry->id = 0;
892 context_destroy_fbo_entry(context, entry);
893 }
894
895 if (context->valid)
896 {
897 if (context->src_fbo)
898 {
899 TRACE("Destroy src FBO %d\n", context->src_fbo);
900 context_destroy_fbo(context, &context->src_fbo);
901 }
902 if (context->dst_fbo)
903 {
904 TRACE("Destroy dst FBO %d\n", context->dst_fbo);
905 context_destroy_fbo(context, &context->dst_fbo);
906 }
907 if (context->dummy_arbfp_prog)
908 {
909 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
910 }
911
912 if (gl_info->supported[ARB_OCCLUSION_QUERY])
913 GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
914
915 if (gl_info->supported[ARB_SYNC])
916 {
917 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
918 }
919 else if (gl_info->supported[APPLE_FENCE])
920 {
921 for (i = 0; i < context->free_event_query_count; ++i)
922 {
923 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
924 }
925 }
926 else if (gl_info->supported[NV_FENCE])
927 {
928 for (i = 0; i < context->free_event_query_count; ++i)
929 {
930 GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
931 }
932 }
933
934 checkGLcall("context cleanup");
935 }
936
937 LEAVE_GL();
938
939 HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
940 HeapFree(GetProcessHeap(), 0, context->free_event_queries);
941
942 if (restore_ctx)
943 {
944 if (!pwglMakeCurrent(restore_dc, restore_ctx))
945 {
946 DWORD err = GetLastError();
947 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
948 restore_ctx, restore_dc, err);
949 }
950 }
951 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL, NULL))
952 {
953 ERR("Failed to disable GL context.\n");
954 }
955
956 ReleaseDC(context->win_handle, context->hdc);
957
958 if (!pwglDeleteContext(context->glCtx))
959 {
960 DWORD err = GetLastError();
961 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
962 }
963}
964
965DWORD context_get_tls_idx(void)
966{
967 return wined3d_context_tls_idx;
968}
969
970void context_set_tls_idx(DWORD idx)
971{
972 wined3d_context_tls_idx = idx;
973}
974
975struct wined3d_context *context_get_current(void)
976{
977 return TlsGetValue(wined3d_context_tls_idx);
978}
979
980BOOL context_set_current(struct wined3d_context *ctx)
981{
982 struct wined3d_context *old = context_get_current();
983
984 if (old == ctx)
985 {
986 TRACE("Already using D3D context %p.\n", ctx);
987 return TRUE;
988 }
989
990 if (old)
991 {
992 if (old->destroyed)
993 {
994 TRACE("Switching away from destroyed context %p.\n", old);
995 context_destroy_gl_resources(old);
996 HeapFree(GetProcessHeap(), 0, old);
997 }
998 else
999 {
1000 old->current = 0;
1001 }
1002 }
1003
1004 if (ctx)
1005 {
1006 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1007 if (!pwglMakeCurrent(ctx->hdc, ctx->glCtx))
1008 {
1009 DWORD err = GetLastError();
1010 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
1011 ctx->glCtx, ctx->hdc, err);
1012 TlsSetValue(wined3d_context_tls_idx, NULL);
1013 return FALSE;
1014 }
1015 ctx->current = 1;
1016 }
1017 else if(pwglGetCurrentContext())
1018 {
1019 TRACE("Clearing current D3D context.\n");
1020 if (!pwglMakeCurrent(NULL, NULL))
1021 {
1022 DWORD err = GetLastError();
1023 ERR("Failed to clear current GL context, last error %#x.\n", err);
1024 TlsSetValue(wined3d_context_tls_idx, NULL);
1025 return FALSE;
1026 }
1027 }
1028
1029 return TlsSetValue(wined3d_context_tls_idx, ctx);
1030}
1031
1032void context_release(struct wined3d_context *context)
1033{
1034 TRACE("Releasing context %p, level %u.\n", context, context->level);
1035
1036 if (WARN_ON(d3d))
1037 {
1038 if (!context->level)
1039 WARN("Context %p is not active.\n", context);
1040 else if (context != context_get_current())
1041 WARN("Context %p is not the current context.\n", context);
1042 }
1043
1044 if (!--context->level && context->restore_ctx)
1045 {
1046 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1047 if (!pwglMakeCurrent(context->restore_dc, context->restore_ctx))
1048 {
1049 DWORD err = GetLastError();
1050 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
1051 context->restore_ctx, context->restore_dc, err);
1052 }
1053 context->restore_ctx = NULL;
1054 context->restore_dc = NULL;
1055 }
1056}
1057
1058static void context_enter(struct wined3d_context *context)
1059{
1060 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1061
1062 if (!context->level++)
1063 {
1064 const struct wined3d_context *current_context = context_get_current();
1065 HGLRC current_gl = pwglGetCurrentContext();
1066
1067 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1068 {
1069 TRACE("Another GL context (%p on device context %p) is already current.\n",
1070 current_gl, pwglGetCurrentDC());
1071 context->restore_ctx = current_gl;
1072 context->restore_dc = pwglGetCurrentDC();
1073 }
1074 }
1075}
1076
1077/*****************************************************************************
1078 * Context_MarkStateDirty
1079 *
1080 * Marks a state in a context dirty. Only one context, opposed to
1081 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
1082 * contexts
1083 *
1084 * Params:
1085 * context: Context to mark the state dirty in
1086 * state: State to mark dirty
1087 * StateTable: Pointer to the state table in use(for state grouping)
1088 *
1089 *****************************************************************************/
1090static void Context_MarkStateDirty(struct wined3d_context *context, DWORD state, const struct StateEntry *StateTable)
1091{
1092 DWORD rep = StateTable[state].representative;
1093 DWORD idx;
1094 BYTE shift;
1095
1096 if (isStateDirty(context, rep)) return;
1097
1098 context->dirtyArray[context->numDirtyEntries++] = rep;
1099 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1100 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1101 context->isStateDirty[idx] |= (1 << shift);
1102}
1103
1104/* This function takes care of WineD3D pixel format selection. */
1105static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl *This, HDC hdc,
1106 const struct wined3d_format_desc *color_format_desc, const struct wined3d_format_desc *ds_format_desc,
1107 BOOL auxBuffers, int numSamples, BOOL findCompatible)
1108{
1109 int iPixelFormat=0;
1110 unsigned int matchtry;
1111 short redBits, greenBits, blueBits, alphaBits, colorBits;
1112 short depthBits=0, stencilBits=0;
1113
1114 struct match_type {
1115 BOOL require_aux;
1116 BOOL exact_alpha;
1117 BOOL exact_color;
1118 } matches[] = {
1119 /* First, try without alpha match buffers. MacOS supports aux buffers only
1120 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1121 * Then try without aux buffers - this is the most common cause for not
1122 * finding a pixel format. Also some drivers(the open source ones)
1123 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1124 * match, then try without an exact alpha and color match.
1125 */
1126 { TRUE, TRUE, TRUE },
1127 { TRUE, FALSE, TRUE },
1128 { FALSE, TRUE, TRUE },
1129 { FALSE, FALSE, TRUE },
1130 { TRUE, FALSE, FALSE },
1131 { FALSE, FALSE, FALSE },
1132 };
1133
1134 int i = 0;
1135 int nCfgs = This->adapter->nCfgs;
1136
1137 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1138 debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format),
1139 auxBuffers, numSamples, findCompatible);
1140
1141 if (!getColorBits(color_format_desc, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
1142 {
1143 ERR("Unable to get color bits for format %s (%#x)!\n",
1144 debug_d3dformat(color_format_desc->format), color_format_desc->format);
1145 return 0;
1146 }
1147
1148 getDepthStencilBits(ds_format_desc, &depthBits, &stencilBits);
1149
1150 for(matchtry = 0; matchtry < (sizeof(matches) / sizeof(matches[0])) && !iPixelFormat; matchtry++) {
1151 for(i=0; i<nCfgs; i++) {
1152 BOOL exactDepthMatch = TRUE;
1153 WineD3D_PixelFormat *cfg = &This->adapter->cfgs[i];
1154
1155 /* For now only accept RGBA formats. Perhaps some day we will
1156 * allow floating point formats for pbuffers. */
1157 if(cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1158 continue;
1159
1160 /* In window mode we need a window drawable format and double buffering. */
1161 if(!(cfg->windowDrawable && cfg->doubleBuffer))
1162 continue;
1163
1164 /* We like to have aux buffers in backbuffer mode */
1165 if(auxBuffers && !cfg->auxBuffers && matches[matchtry].require_aux)
1166 continue;
1167
1168 if(matches[matchtry].exact_color) {
1169 if(cfg->redSize != redBits)
1170 continue;
1171 if(cfg->greenSize != greenBits)
1172 continue;
1173 if(cfg->blueSize != blueBits)
1174 continue;
1175 } else {
1176 if(cfg->redSize < redBits)
1177 continue;
1178 if(cfg->greenSize < greenBits)
1179 continue;
1180 if(cfg->blueSize < blueBits)
1181 continue;
1182 }
1183 if(matches[matchtry].exact_alpha) {
1184 if(cfg->alphaSize != alphaBits)
1185 continue;
1186 } else {
1187 if(cfg->alphaSize < alphaBits)
1188 continue;
1189 }
1190
1191 /* We try to locate a format which matches our requirements exactly. In case of
1192 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1193 if(cfg->depthSize < depthBits)
1194 continue;
1195 else if(cfg->depthSize > depthBits)
1196 exactDepthMatch = FALSE;
1197
1198 /* In all cases make sure the number of stencil bits matches our requirements
1199 * even when we don't need stencil because it could affect performance EXCEPT
1200 * on cards which don't offer depth formats without stencil like the i915 drivers
1201 * on Linux. */
1202 if(stencilBits != cfg->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfg->stencilSize))
1203 continue;
1204
1205 /* Check multisampling support */
1206 if(cfg->numSamples != numSamples)
1207 continue;
1208
1209 /* When we have passed all the checks then we have found a format which matches our
1210 * requirements. Note that we only check for a limit number of capabilities right now,
1211 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1212 * can still differ in things like multisampling, stereo, SRGB and other flags.
1213 */
1214
1215 /* Exit the loop as we have found a format :) */
1216 if(exactDepthMatch) {
1217 iPixelFormat = cfg->iPixelFormat;
1218 break;
1219 } else if(!iPixelFormat) {
1220 /* In the end we might end up with a format which doesn't exactly match our depth
1221 * requirements. Accept the first format we found because formats with higher iPixelFormat
1222 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1223 iPixelFormat = cfg->iPixelFormat;
1224 }
1225 }
1226 }
1227
1228 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1229 if(!iPixelFormat && !findCompatible) {
1230 ERR("Can't find a suitable iPixelFormat\n");
1231 return FALSE;
1232 } else if(!iPixelFormat) {
1233 PIXELFORMATDESCRIPTOR pfd;
1234
1235 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1236 /* PixelFormat selection */
1237 ZeroMemory(&pfd, sizeof(pfd));
1238 pfd.nSize = sizeof(pfd);
1239 pfd.nVersion = 1;
1240 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1241 pfd.iPixelType = PFD_TYPE_RGBA;
1242 pfd.cAlphaBits = alphaBits;
1243 pfd.cColorBits = colorBits;
1244 pfd.cDepthBits = depthBits;
1245 pfd.cStencilBits = stencilBits;
1246 pfd.iLayerType = PFD_MAIN_PLANE;
1247
1248 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1249 if(!iPixelFormat) {
1250 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1251 ERR("Can't find a suitable iPixelFormat\n");
1252 return FALSE;
1253 }
1254 }
1255
1256 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1257 iPixelFormat, debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format));
1258 return iPixelFormat;
1259}
1260
1261
1262
1263
1264/*****************************************************************************
1265 * context_create
1266 *
1267 * Creates a new context.
1268 *
1269 * * Params:
1270 * This: Device to activate the context for
1271 * target: Surface this context will render to
1272 * win_handle: handle to the window which we are drawing to
1273 * pPresentParameters: contains the pixelformats to use for onscreen rendering
1274 *
1275 *****************************************************************************/
1276struct wined3d_context *context_create(IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
1277 const struct wined3d_format_desc *ds_format_desc)
1278{
1279 IWineD3DDeviceImpl *device = swapchain->device;
1280 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1281 const struct wined3d_format_desc *color_format_desc;
1282 struct wined3d_context *ret;
1283 PIXELFORMATDESCRIPTOR pfd;
1284 BOOL auxBuffers = FALSE;
1285 int numSamples = 0;
1286 int pixel_format;
1287 unsigned int s;
1288 DWORD state;
1289 HGLRC ctx;
1290 HDC hdc;
1291
1292 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1293
1294 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1295 if (!ret)
1296 {
1297 ERR("Failed to allocate context memory.\n");
1298 return NULL;
1299 }
1300
1301 if (!(hdc = GetDC(swapchain->win_handle)))
1302 {
1303 ERR("Failed to retrieve a device context.\n");
1304 goto out;
1305 }
1306
1307 color_format_desc = target->resource.format_desc;
1308
1309 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1310 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1311 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1312 {
1313 auxBuffers = TRUE;
1314
1315 if (color_format_desc->format == WINED3DFMT_B4G4R4X4_UNORM)
1316 color_format_desc = getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM, gl_info);
1317 else if (color_format_desc->format == WINED3DFMT_B8G8R8X8_UNORM)
1318 color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
1319 }
1320
1321 /* DirectDraw supports 8bit paletted render targets and these are used by
1322 * old games like Starcraft and C&C. Most modern hardware doesn't support
1323 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1324 * conversion (ab)uses the alpha component for storing the palette index.
1325 * For this reason we require a format with 8bit alpha, so request
1326 * A8R8G8B8. */
1327 if (color_format_desc->format == WINED3DFMT_P8_UINT)
1328 color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
1329
1330 /* Retrieve the depth stencil format from the present parameters.
1331 * The choice of the proper format can give a nice performance boost
1332 * in case of GPU limited programs. */
1333 if (swapchain->presentParms.EnableAutoDepthStencil)
1334 {
1335 TRACE("Auto depth stencil enabled, using format %s.\n",
1336 debug_d3dformat(swapchain->presentParms.AutoDepthStencilFormat));
1337 ds_format_desc = getFormatDescEntry(swapchain->presentParms.AutoDepthStencilFormat, gl_info);
1338 }
1339
1340 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1341 if (swapchain->presentParms.MultiSampleType && (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD))
1342 {
1343 if (!gl_info->supported[ARB_MULTISAMPLE])
1344 WARN("The application is requesting multisampling without support.\n");
1345 else
1346 {
1347 TRACE("Requesting multisample type %#x.\n", swapchain->presentParms.MultiSampleType);
1348 numSamples = swapchain->presentParms.MultiSampleType;
1349 }
1350 }
1351
1352 /* Try to find a pixel format which matches our requirements. */
1353 pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
1354 auxBuffers, numSamples, FALSE /* findCompatible */);
1355
1356 /* Try to locate a compatible format if we weren't able to find anything. */
1357 if (!pixel_format)
1358 {
1359 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1360 pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
1361 auxBuffers, 0 /* numSamples */, TRUE /* findCompatible */);
1362 }
1363
1364 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1365 if (!pixel_format)
1366 {
1367 ERR("Can't find a suitable pixel format.\n");
1368 goto out;
1369 }
1370
1371 DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd);
1372 if (!context_set_pixel_format(gl_info, hdc, pixel_format))
1373 {
1374 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1375 goto out;
1376 }
1377
1378 ctx = pwglCreateContext(hdc);
1379 if (device->numContexts)
1380 {
1381 if (!pwglShareLists(device->contexts[0]->glCtx, ctx))
1382 {
1383 DWORD err = GetLastError();
1384 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1385 device->contexts[0]->glCtx, ctx, err);
1386 }
1387 }
1388
1389 if(!ctx) {
1390 ERR("Failed to create a WGL context\n");
1391 goto out;
1392 }
1393
1394 if (!device_context_add(device, ret))
1395 {
1396 ERR("Failed to add the newly created context to the context list\n");
1397 if (!pwglDeleteContext(ctx))
1398 {
1399 DWORD err = GetLastError();
1400 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, err);
1401 }
1402 goto out;
1403 }
1404
1405 ret->gl_info = gl_info;
1406
1407 /* Mark all states dirty to force a proper initialization of the states
1408 * on the first use of the context. */
1409 for (state = 0; state <= STATE_HIGHEST; ++state)
1410 {
1411 if (device->StateTable[state].representative)
1412 Context_MarkStateDirty(ret, state, device->StateTable);
1413 }
1414
1415#ifdef VBOX_WITH_WDDM
1416 ret->device = device;
1417 ret->currentSwapchain = swapchain;
1418#else
1419 ret->swapchain = swapchain;
1420#endif
1421 ret->current_rt = (IWineD3DSurface *)target;
1422 ret->tid = GetCurrentThreadId();
1423
1424 ret->render_offscreen = surface_is_offscreen((IWineD3DSurface *) target);
1425 ret->draw_buffer_dirty = TRUE;
1426 ret->valid = 1;
1427
1428 ret->glCtx = ctx;
1429 ret->win_handle = swapchain->win_handle;
1430 ret->hdc = hdc;
1431 ret->pixel_format = pixel_format;
1432
1433 if (device->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *)device))
1434 {
1435 /* Create the dirty constants array and initialize them to dirty */
1436 ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
1437 sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
1438 ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
1439 sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
1440 memset(ret->vshader_const_dirty, 1,
1441 sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
1442 memset(ret->pshader_const_dirty, 1,
1443 sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
1444 }
1445
1446 ret->free_occlusion_query_size = 4;
1447 ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
1448 ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
1449 if (!ret->free_occlusion_queries) goto out;
1450
1451 list_init(&ret->occlusion_queries);
1452
1453 ret->free_event_query_size = 4;
1454 ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
1455 ret->free_event_query_size * sizeof(*ret->free_event_queries));
1456 if (!ret->free_event_queries) goto out;
1457
1458 list_init(&ret->event_queries);
1459
1460 TRACE("Successfully created new context %p\n", ret);
1461
1462 list_init(&ret->fbo_list);
1463 list_init(&ret->fbo_destroy_list);
1464
1465 context_enter(ret);
1466
1467 /* Set up the context defaults */
1468 if (!context_set_current(ret))
1469 {
1470 ERR("Cannot activate context to set up defaults\n");
1471 context_release(ret);
1472 goto out;
1473 }
1474
1475 ENTER_GL();
1476
1477 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
1478
1479 TRACE("Setting up the screen\n");
1480 /* Clear the screen */
1481 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
1482 checkGLcall("glClearColor");
1483 glClearIndex(0);
1484 glClearDepth(1);
1485 glClearStencil(0xffff);
1486
1487 checkGLcall("glClear");
1488
1489 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1490 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1491
1492 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1493 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1494
1495 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1496 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1497
1498 glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
1499 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1500 glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
1501 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1502
1503 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1504 {
1505 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1506 * and textures in DIB sections(due to the memory protection).
1507 */
1508 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1509 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1510 }
1511 if (gl_info->supported[ARB_VERTEX_BLEND])
1512 {
1513 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1514 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1515 * GL_VERTEX_BLEND_ARB isn't enabled too
1516 */
1517 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
1518 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1519 }
1520 if (gl_info->supported[NV_TEXTURE_SHADER2])
1521 {
1522 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1523 * the previous texture where to source the offset from is always unit - 1.
1524 */
1525 for (s = 1; s < gl_info->limits.textures; ++s)
1526 {
1527 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
1528 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
1529 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1530 }
1531 }
1532 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
1533 {
1534 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1535 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1536 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1537 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1538 * is ever assigned.
1539 *
1540 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1541 * program and the dummy program is destroyed when the context is destroyed.
1542 */
1543 const char *dummy_program =
1544 "!!ARBfp1.0\n"
1545 "MOV result.color, fragment.color.primary;\n"
1546 "END\n";
1547 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
1548 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
1549 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
1550 }
1551
1552 for (s = 0; s < gl_info->limits.point_sprite_units; ++s)
1553 {
1554 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
1555 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
1556 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1557 }
1558
1559 if (gl_info->supported[ARB_PROVOKING_VERTEX])
1560 {
1561 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
1562 }
1563 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
1564 {
1565 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
1566 }
1567
1568#ifdef VBOX_WITH_WDDM
1569 GL_EXTCALL(glChromiumParameteriCR(GL_SHARE_CONTEXT_RESOURCES_CR, GL_TRUE));
1570#endif
1571
1572 LEAVE_GL();
1573
1574 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
1575
1576 TRACE("Created context %p.\n", ret);
1577
1578 return ret;
1579
1580out:
1581 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
1582 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
1583 HeapFree(GetProcessHeap(), 0, ret->pshader_const_dirty);
1584 HeapFree(GetProcessHeap(), 0, ret->vshader_const_dirty);
1585 HeapFree(GetProcessHeap(), 0, ret);
1586 return NULL;
1587}
1588
1589#ifdef VBOX_WITH_WDDM
1590static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
1591static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage);
1592
1593BOOL context_acquire_context(struct wined3d_context * context, IWineD3DSurface *target, enum ContextUsage usage, BOOL bReValidate)
1594{
1595 IWineD3DDeviceImpl *device = context->device;
1596 struct wined3d_context *current_context = context_get_current();
1597 if (bReValidate)
1598 {
1599 IWineD3DSwapChain *swapchain = NULL;
1600 if (target && SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
1601 context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
1602 IWineD3DSwapChain_Release(swapchain);
1603 }
1604 else {
1605 /* tmp work-around */
1606 context_validate(current_context,
1607 NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
1608 );
1609 }
1610 }
1611 context_setup_target(device, context, target);
1612 context_enter(context);
1613// Assert(context->valid);
1614 if (!context->valid) return FALSE;
1615
1616 if (context != current_context)
1617 {
1618 if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
1619 else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
1620
1621 if (context->vshader_const_dirty)
1622 {
1623 memset(context->vshader_const_dirty, 1,
1624 sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
1625 device->highest_dirty_vs_const = device->d3d_vshader_constantF;
1626 }
1627 if (context->pshader_const_dirty)
1628 {
1629 memset(context->pshader_const_dirty, 1,
1630 sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
1631 device->highest_dirty_ps_const = device->d3d_pshader_constantF;
1632 }
1633 }
1634 else if (context->restore_ctx)
1635 {
1636 if (!pwglMakeCurrent(context->hdc, context->glCtx))
1637 {
1638 DWORD err = GetLastError();
1639 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
1640 context->hdc, context->glCtx, err);
1641 }
1642 }
1643
1644 context_apply_state(context, device, usage);
1645
1646 return TRUE;
1647}
1648
1649struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
1650 const struct wined3d_format_desc *ds_format_desc)
1651{
1652 UINT i;
1653 DWORD tid = GetCurrentThreadId();
1654 struct wined3d_context *context = NULL;
1655
1656 for(i = 0 ; i < device->numContexts ; i ++)
1657 {
1658 if(device->contexts[i]->tid == tid) {
1659 context = device->contexts[i];
1660 break;
1661 }
1662 }
1663
1664 if (!context)
1665 {
1666 Assert(!device->NumberOfSwapChains);
1667 context = context_create(swapchain, target, ds_format_desc);
1668 }
1669 else
1670 {
1671 if(!context_acquire_context(context, (IWineD3DSurface*)target, CTXUSAGE_RESOURCELOAD, TRUE))
1672 {
1673 ERR("Failed to acquire the context.\n");
1674 Assert(0);
1675 Assert(!context->valid);
1676 context = NULL;
1677 }
1678 else
1679 {
1680 Assert(context->valid);
1681 }
1682 }
1683
1684 return context;
1685}
1686#endif
1687
1688/*****************************************************************************
1689 * context_destroy
1690 *
1691 * Destroys a wined3d context
1692 *
1693 * Params:
1694 * This: Device to activate the context for
1695 * context: Context to destroy
1696 *
1697 *****************************************************************************/
1698void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context)
1699{
1700 BOOL destroy;
1701
1702 TRACE("Destroying ctx %p\n", context);
1703
1704 if (context->tid == GetCurrentThreadId() || !context->current)
1705 {
1706 context_destroy_gl_resources(context);
1707 TlsSetValue(wined3d_context_tls_idx, NULL);
1708 destroy = TRUE;
1709 }
1710 else
1711 {
1712 context->destroyed = 1;
1713 destroy = FALSE;
1714 }
1715
1716 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
1717 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
1718 device_context_remove(This, context);
1719 if (destroy) HeapFree(GetProcessHeap(), 0, context);
1720}
1721
1722/* GL locking is done by the caller */
1723static inline void set_blit_dimension(UINT width, UINT height) {
1724 glMatrixMode(GL_PROJECTION);
1725 checkGLcall("glMatrixMode(GL_PROJECTION)");
1726 glLoadIdentity();
1727 checkGLcall("glLoadIdentity()");
1728 glOrtho(0, width, height, 0, 0.0, -1.0);
1729 checkGLcall("glOrtho");
1730 glViewport(0, 0, width, height);
1731 checkGLcall("glViewport");
1732}
1733
1734/*****************************************************************************
1735 * SetupForBlit
1736 *
1737 * Sets up a context for DirectDraw blitting.
1738 * All texture units are disabled, texture unit 0 is set as current unit
1739 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1740 * color writing enabled for all channels
1741 * register combiners disabled, shaders disabled
1742 * world matrix is set to identity, texture matrix 0 too
1743 * projection matrix is setup for drawing screen coordinates
1744 *
1745 * Params:
1746 * This: Device to activate the context for
1747 * context: Context to setup
1748 *
1749 *****************************************************************************/
1750/* Context activation is done by the caller. */
1751static void SetupForBlit(IWineD3DDeviceImpl *This, struct wined3d_context *context)
1752{
1753 int i;
1754 const struct StateEntry *StateTable = This->StateTable;
1755 const struct wined3d_gl_info *gl_info = context->gl_info;
1756 UINT width = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Width;
1757 UINT height = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Height;
1758 DWORD sampler;
1759
1760 TRACE("Setting up context %p for blitting\n", context);
1761 if(context->last_was_blit) {
1762 if(context->blit_w != width || context->blit_h != height) {
1763 ENTER_GL();
1764 set_blit_dimension(width, height);
1765 LEAVE_GL();
1766 context->blit_w = width; context->blit_h = height;
1767 /* No need to dirtify here, the states are still dirtified because they weren't
1768 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1769 * be set
1770 */
1771 }
1772 TRACE("Context is already set up for blitting, nothing to do\n");
1773 return;
1774 }
1775 context->last_was_blit = TRUE;
1776
1777 /* TODO: Use a display list */
1778
1779 /* Disable shaders */
1780 ENTER_GL();
1781 This->shader_backend->shader_select(context, FALSE, FALSE);
1782 LEAVE_GL();
1783
1784 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
1785 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
1786
1787 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1788 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1789 * which can safely be called from here, we only lock once instead locking/unlocking
1790 * after each GL call.
1791 */
1792 ENTER_GL();
1793
1794 /* Disable all textures. The caller can then bind a texture it wants to blit
1795 * from
1796 *
1797 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1798 * function texture unit. No need to care for higher samplers
1799 */
1800 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
1801 {
1802 sampler = This->rev_tex_unit_map[i];
1803 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1804 checkGLcall("glActiveTextureARB");
1805
1806 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1807 {
1808 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1809 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1810 }
1811 glDisable(GL_TEXTURE_3D);
1812 checkGLcall("glDisable GL_TEXTURE_3D");
1813 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1814 {
1815 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1816 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1817 }
1818 glDisable(GL_TEXTURE_2D);
1819 checkGLcall("glDisable GL_TEXTURE_2D");
1820
1821 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1822 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1823
1824 if (sampler != WINED3D_UNMAPPED_STAGE)
1825 {
1826 if (sampler < MAX_TEXTURES) {
1827 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1828 }
1829 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1830 }
1831 }
1832 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
1833 checkGLcall("glActiveTextureARB");
1834
1835 sampler = This->rev_tex_unit_map[0];
1836
1837 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1838 {
1839 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1840 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1841 }
1842 glDisable(GL_TEXTURE_3D);
1843 checkGLcall("glDisable GL_TEXTURE_3D");
1844 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1845 {
1846 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1847 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1848 }
1849 glDisable(GL_TEXTURE_2D);
1850 checkGLcall("glDisable GL_TEXTURE_2D");
1851
1852 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1853
1854 glMatrixMode(GL_TEXTURE);
1855 checkGLcall("glMatrixMode(GL_TEXTURE)");
1856 glLoadIdentity();
1857 checkGLcall("glLoadIdentity()");
1858
1859 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1860 {
1861 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1862 GL_TEXTURE_LOD_BIAS_EXT,
1863 0.0f);
1864 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1865 }
1866
1867 if (sampler != WINED3D_UNMAPPED_STAGE)
1868 {
1869 if (sampler < MAX_TEXTURES) {
1870 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
1871 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1872 }
1873 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1874 }
1875
1876 /* Other misc states */
1877 glDisable(GL_ALPHA_TEST);
1878 checkGLcall("glDisable(GL_ALPHA_TEST)");
1879 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
1880 glDisable(GL_LIGHTING);
1881 checkGLcall("glDisable GL_LIGHTING");
1882 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
1883 glDisable(GL_DEPTH_TEST);
1884 checkGLcall("glDisable GL_DEPTH_TEST");
1885 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
1886 glDisableWINE(GL_FOG);
1887 checkGLcall("glDisable GL_FOG");
1888 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
1889 glDisable(GL_BLEND);
1890 checkGLcall("glDisable GL_BLEND");
1891 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1892 glDisable(GL_CULL_FACE);
1893 checkGLcall("glDisable GL_CULL_FACE");
1894 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
1895 glDisable(GL_STENCIL_TEST);
1896 checkGLcall("glDisable GL_STENCIL_TEST");
1897 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
1898 glDisable(GL_SCISSOR_TEST);
1899 checkGLcall("glDisable GL_SCISSOR_TEST");
1900 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1901 if (gl_info->supported[ARB_POINT_SPRITE])
1902 {
1903 glDisable(GL_POINT_SPRITE_ARB);
1904 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1905 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
1906 }
1907 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1908 checkGLcall("glColorMask");
1909 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE), StateTable);
1910 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1), StateTable);
1911 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2), StateTable);
1912 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3), StateTable);
1913 if (gl_info->supported[EXT_SECONDARY_COLOR])
1914 {
1915 glDisable(GL_COLOR_SUM_EXT);
1916 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
1917 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1918 }
1919
1920 /* Setup transforms */
1921 glMatrixMode(GL_MODELVIEW);
1922 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1923 glLoadIdentity();
1924 checkGLcall("glLoadIdentity()");
1925 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
1926
1927 context->last_was_rhw = TRUE;
1928 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
1929
1930 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1931 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1932 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1933 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1934 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1935 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1936 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1937
1938 set_blit_dimension(width, height);
1939
1940 LEAVE_GL();
1941
1942 context->blit_w = width; context->blit_h = height;
1943 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1944 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
1945
1946
1947 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1948}
1949
1950/*****************************************************************************
1951 * findThreadContextForSwapChain
1952 *
1953 * Searches a swapchain for all contexts and picks one for the thread tid.
1954 * If none can be found the swapchain is requested to create a new context
1955 *
1956 *****************************************************************************/
1957static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid)
1958{
1959 unsigned int i;
1960#ifdef VBOX_WITH_WDDM
1961 IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
1962 for (i = 0; i < device->numContexts; ++i)
1963 {
1964 if (device->contexts[i]->tid == tid)
1965 return device->contexts[i];
1966 }
1967#else
1968 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
1969 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
1970 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
1971 }
1972 }
1973#endif
1974
1975 /* Create a new context for the thread */
1976 return swapchain_create_context_for_thread(swapchain);
1977}
1978
1979/*****************************************************************************
1980 * FindContext
1981 *
1982 * Finds a context for the current render target and thread
1983 *
1984 * Parameters:
1985 * target: Render target to find the context for
1986 * tid: Thread to activate the context for
1987 *
1988 * Returns: The needed context
1989 *
1990 *****************************************************************************/
1991static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
1992{
1993 IWineD3DSwapChain *swapchain = NULL;
1994 struct wined3d_context *current_context = context_get_current();
1995 DWORD tid = GetCurrentThreadId();
1996 struct wined3d_context *context;
1997
1998 if (current_context && current_context->destroyed) current_context = NULL;
1999
2000 if (!target)
2001 {
2002 if (current_context
2003 && current_context->current_rt
2004#ifdef VBOX_WITH_WDDM
2005 && current_context->device == This
2006#else
2007 && current_context->swapchain->device == This
2008#endif
2009 )
2010 {
2011 target = current_context->current_rt;
2012 }
2013 else
2014 {
2015#ifdef VBOX_WITH_WDDM
2016 /* tmp work-around */
2017 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
2018#else
2019 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[0];
2020#endif
2021 if (swapchain->backBuffer) target = swapchain->backBuffer[0];
2022 else target = swapchain->frontBuffer;
2023 }
2024 }
2025
2026 if (current_context && current_context->current_rt == target)
2027 {
2028#ifdef VBOX_WITH_WDDM
2029 IWineD3DSwapChain *swapchain = NULL;
2030 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
2031 context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
2032 IWineD3DSwapChain_Release(swapchain);
2033 }
2034 else {
2035 /* tmp work-around */
2036 context_validate(current_context,
2037 NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
2038 );
2039 }
2040#else
2041 context_validate(current_context);
2042#endif
2043 return current_context;
2044 }
2045
2046 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
2047 TRACE("Rendering onscreen\n");
2048
2049 context = findThreadContextForSwapChain(swapchain, tid);
2050#ifdef VBOX_WITH_WDDM
2051 context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
2052#endif
2053 IWineD3DSwapChain_Release(swapchain);
2054 }
2055 else
2056 {
2057 TRACE("Rendering offscreen\n");
2058
2059 /* Stay with the currently active context. */
2060 if (current_context
2061#ifdef VBOX_WITH_WDDM
2062 && current_context->device == This
2063#else
2064 && current_context->swapchain->device == This
2065#endif
2066 )
2067 {
2068 context = current_context;
2069 }
2070 else
2071 {
2072 /* This may happen if the app jumps straight into offscreen rendering
2073 * Start using the context of the primary swapchain. tid == 0 is no problem
2074 * for findThreadContextForSwapChain.
2075 *
2076 * Can also happen on thread switches - in that case findThreadContextForSwapChain
2077 * is perfect to call. */
2078#ifdef VBOX_WITH_WDDM /* tmp work-around */
2079 context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1], tid);
2080#else
2081 context = findThreadContextForSwapChain(This->swapchains[0], tid);
2082#endif
2083 }
2084#ifdef VBOX_WITH_WDDM
2085 context_validate(context,
2086 NULL //(IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
2087 );
2088#endif
2089 }
2090
2091#ifndef VBOX_WITH_WDDM
2092 context_validate(context);
2093#endif
2094
2095 return context;
2096}
2097
2098/* Context activation is done by the caller. */
2099static void context_apply_draw_buffer(struct wined3d_context *context, BOOL blit)
2100{
2101 const struct wined3d_gl_info *gl_info = context->gl_info;
2102 IWineD3DSurface *rt = context->current_rt;
2103 IWineD3DDeviceImpl *device;
2104
2105 device = ((IWineD3DSurfaceImpl *)rt)->resource.device;
2106 if (!surface_is_offscreen(rt))
2107 {
2108 ENTER_GL();
2109 glDrawBuffer(surface_get_gl_buffer(rt));
2110 checkGLcall("glDrawBuffers()");
2111 LEAVE_GL();
2112 }
2113 else
2114 {
2115 ENTER_GL();
2116 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2117 {
2118 if (!blit)
2119 {
2120 if (gl_info->supported[ARB_DRAW_BUFFERS])
2121 {
2122 GL_EXTCALL(glDrawBuffersARB(gl_info->limits.buffers, device->draw_buffers));
2123 checkGLcall("glDrawBuffers()");
2124 }
2125 else
2126 {
2127 glDrawBuffer(device->draw_buffers[0]);
2128 checkGLcall("glDrawBuffer()");
2129 }
2130 } else {
2131 glDrawBuffer(GL_COLOR_ATTACHMENT0);
2132 checkGLcall("glDrawBuffer()");
2133 }
2134 }
2135 else
2136 {
2137 glDrawBuffer(device->offscreenBuffer);
2138 checkGLcall("glDrawBuffer()");
2139 }
2140 LEAVE_GL();
2141 }
2142}
2143
2144/* GL locking is done by the caller. */
2145void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2146{
2147 glDrawBuffer(buffer);
2148 checkGLcall("glDrawBuffer()");
2149 context->draw_buffer_dirty = TRUE;
2150}
2151
2152static inline void context_set_render_offscreen(struct wined3d_context *context, const struct StateEntry *StateTable,
2153 BOOL offscreen)
2154{
2155 if (context->render_offscreen == offscreen) return;
2156
2157 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
2158 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
2159 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
2160 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
2161 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
2162 context->render_offscreen = offscreen;
2163}
2164
2165static BOOL match_depth_stencil_format(const struct wined3d_format_desc *existing,
2166 const struct wined3d_format_desc *required)
2167{
2168 short existing_depth, existing_stencil, required_depth, required_stencil;
2169
2170 if(existing == required) return TRUE;
2171 if((existing->Flags & WINED3DFMT_FLAG_FLOAT) != (required->Flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2172
2173 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2174 getDepthStencilBits(required, &required_depth, &required_stencil);
2175
2176 if(existing_depth < required_depth) return FALSE;
2177 /* If stencil bits are used the exact amount is required - otherwise wrapping
2178 * won't work correctly */
2179 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2180 return TRUE;
2181}
2182/* The caller provides a context */
2183static void context_validate_onscreen_formats(IWineD3DDeviceImpl *device, struct wined3d_context *context)
2184{
2185 /* Onscreen surfaces are always in a swapchain */
2186 IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *) device->stencilBufferTarget;
2187 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) ((IWineD3DSurfaceImpl *)context->current_rt)->container;
2188
2189 if (!depth_stencil) return;
2190 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format_desc)) return;
2191
2192 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2193 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2194 * format. */
2195 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2196
2197 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2198 IWineD3DSurface_LoadLocation(context->current_rt, SFLAG_INTEXTURE, NULL);
2199 swapchain->render_to_fbo = TRUE;
2200 context_set_render_offscreen(context, device->StateTable, TRUE);
2201}
2202
2203/* Context activation is done by the caller. */
2204static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage)
2205{
2206 const struct StateEntry *state_table = device->StateTable;
2207 unsigned int i;
2208
2209 switch (usage) {
2210 case CTXUSAGE_CLEAR:
2211 case CTXUSAGE_DRAWPRIM:
2212 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2213 if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
2214 ENTER_GL();
2215 context_apply_fbo_state(context);
2216 LEAVE_GL();
2217 }
2218 if (context->draw_buffer_dirty) {
2219 context_apply_draw_buffer(context, FALSE);
2220 context->draw_buffer_dirty = FALSE;
2221 }
2222 break;
2223
2224 case CTXUSAGE_BLIT:
2225 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2226 if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
2227 if (context->render_offscreen)
2228 {
2229 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
2230 surface_internal_preload(context->current_rt, SRGB_RGB);
2231
2232 ENTER_GL();
2233 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
2234 context_attach_surface_fbo(context, GL_FRAMEBUFFER, 0, (IWineD3DSurfaceImpl *)context->current_rt);
2235 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, NULL, FALSE);
2236 LEAVE_GL();
2237 } else {
2238 ENTER_GL();
2239 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
2240 LEAVE_GL();
2241 }
2242 context->draw_buffer_dirty = TRUE;
2243 }
2244 if (context->draw_buffer_dirty) {
2245 context_apply_draw_buffer(context, TRUE);
2246 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
2247 context->draw_buffer_dirty = FALSE;
2248 }
2249 }
2250 break;
2251
2252 default:
2253 break;
2254 }
2255
2256 switch(usage) {
2257 case CTXUSAGE_RESOURCELOAD:
2258 /* This does not require any special states to be set up */
2259 break;
2260
2261 case CTXUSAGE_CLEAR:
2262 if(context->last_was_blit) {
2263 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
2264 }
2265
2266 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
2267 * blending when clearing improves the clearing performance incredibly.
2268 */
2269 ENTER_GL();
2270 glDisable(GL_BLEND);
2271 LEAVE_GL();
2272 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), state_table);
2273
2274 ENTER_GL();
2275 glEnable(GL_SCISSOR_TEST);
2276 checkGLcall("glEnable GL_SCISSOR_TEST");
2277 LEAVE_GL();
2278 context->last_was_blit = FALSE;
2279 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), state_table);
2280 Context_MarkStateDirty(context, STATE_SCISSORRECT, state_table);
2281 break;
2282
2283 case CTXUSAGE_DRAWPRIM:
2284 /* This needs all dirty states applied */
2285 if(context->last_was_blit) {
2286 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
2287 }
2288
2289 IWineD3DDeviceImpl_FindTexUnitMap(device);
2290 device_preload_textures(device);
2291 if (isStateDirty(context, STATE_VDECL))
2292 device_update_stream_info(device, context->gl_info);
2293
2294 ENTER_GL();
2295 for (i = 0; i < context->numDirtyEntries; ++i)
2296 {
2297 DWORD rep = context->dirtyArray[i];
2298 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2299 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2300 context->isStateDirty[idx] &= ~(1 << shift);
2301 state_table[rep].apply(rep, device->stateBlock, context);
2302 }
2303 LEAVE_GL();
2304 context->numDirtyEntries = 0; /* This makes the whole list clean */
2305 context->last_was_blit = FALSE;
2306 break;
2307
2308 case CTXUSAGE_BLIT:
2309 SetupForBlit(device, context);
2310 break;
2311
2312 default:
2313 FIXME("Unexpected context usage requested\n");
2314 }
2315}
2316
2317static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target)
2318{
2319 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2320 const struct StateEntry *StateTable = device->StateTable;
2321
2322 if (!target) return;
2323 else if (context->current_rt == target) return;
2324 render_offscreen = surface_is_offscreen(target);
2325
2326 context_set_render_offscreen(context, StateTable, render_offscreen);
2327
2328 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2329 * the alpha blend state changes with different render target formats. */
2330 if (!context->current_rt)
2331 {
2332 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
2333 }
2334 else
2335 {
2336 const struct wined3d_format_desc *old = ((IWineD3DSurfaceImpl *)context->current_rt)->resource.format_desc;
2337 const struct wined3d_format_desc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
2338
2339 if (old->format != new->format)
2340 {
2341 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2342 if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
2343 || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2344 {
2345 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
2346 }
2347 }
2348
2349 /* When switching away from an offscreen render target, and we're not
2350 * using FBOs, we have to read the drawable into the texture. This is
2351 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2352 * are some things that need care though. PreLoad needs a GL context,
2353 * and FindContext is called before the context is activated. It also
2354 * has to be called with the old rendertarget active, otherwise a
2355 * wrong drawable is read. */
2356 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2357 && old_render_offscreen && context->current_rt != target)
2358 {
2359 BOOL oldInDraw = device->isInDraw;
2360
2361 /* surface_internal_preload() requires a context to load the
2362 * texture, so it will call context_acquire(). Set isInDraw to true
2363 * to signal surface_internal_preload() that it has a context. */
2364
2365 /* FIXME: This is just broken. There's no guarantee whatsoever
2366 * that the currently active context, if any, is appropriate for
2367 * reading back the render target. We should probably call
2368 * context_set_current(context) here and then rely on
2369 * context_acquire() doing the right thing. */
2370 device->isInDraw = TRUE;
2371
2372 /* Read the back buffer of the old drawable into the destination texture. */
2373 if (((IWineD3DSurfaceImpl *)context->current_rt)->texture_name_srgb)
2374 {
2375 surface_internal_preload(context->current_rt, SRGB_BOTH);
2376 }
2377 else
2378 {
2379 surface_internal_preload(context->current_rt, SRGB_RGB);
2380 }
2381
2382 IWineD3DSurface_ModifyLocation(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2383
2384 device->isInDraw = oldInDraw;
2385 }
2386 }
2387
2388 context->draw_buffer_dirty = TRUE;
2389 context->current_rt = target;
2390}
2391
2392/*****************************************************************************
2393 * context_acquire
2394 *
2395 * Finds a rendering context and drawable matching the device and render
2396 * target for the current thread, activates them and puts them into the
2397 * requested state.
2398 *
2399 * Params:
2400 * This: Device to activate the context for
2401 * target: Requested render target
2402 * usage: Prepares the context for blitting, drawing or other actions
2403 *
2404 *****************************************************************************/
2405struct wined3d_context *context_acquire(IWineD3DDeviceImpl *device, IWineD3DSurface *target, enum ContextUsage usage)
2406{
2407 struct wined3d_context *current_context = context_get_current();
2408 struct wined3d_context *context;
2409
2410 TRACE("device %p, target %p, usage %#x.\n", device, target, usage);
2411
2412 context = FindContext(device, target);
2413 context_setup_target(device, context, target);
2414 context_enter(context);
2415 if (!context->valid) return context;
2416
2417 if (context != current_context)
2418 {
2419 if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
2420 else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
2421
2422 if (context->vshader_const_dirty)
2423 {
2424 memset(context->vshader_const_dirty, 1,
2425 sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
2426 device->highest_dirty_vs_const = device->d3d_vshader_constantF;
2427 }
2428 if (context->pshader_const_dirty)
2429 {
2430 memset(context->pshader_const_dirty, 1,
2431 sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
2432 device->highest_dirty_ps_const = device->d3d_pshader_constantF;
2433 }
2434 }
2435 else if (context->restore_ctx)
2436 {
2437 if (!pwglMakeCurrent(context->hdc, context->glCtx))
2438 {
2439 DWORD err = GetLastError();
2440 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2441 context->hdc, context->glCtx, err);
2442 }
2443 }
2444
2445 context_apply_state(context, device, usage);
2446
2447 return context;
2448}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette