VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine_new/wined3d/context.c@ 48999

Last change on this file since 48999 was 48999, checked in by vboxsync, 11 years ago

wddm: ensure all windows are initially hidden

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 102.5 KB
Line 
1/*
2 * Context and render target management in wined3d
3 *
4 * Copyright 2007-2011, 2013 Stefan Dösinger for CodeWeavers
5 * Copyright 2009-2011 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 "wine/port.h"
33
34#include <stdio.h>
35#ifdef HAVE_FLOAT_H
36# include <float.h>
37#endif
38
39#include "wined3d_private.h"
40
41WINE_DEFAULT_DEBUG_CHANNEL(d3d);
42WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
43WINE_DECLARE_DEBUG_CHANNEL(d3d_synchronous);
44
45static DWORD wined3d_context_tls_idx;
46
47#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
48# define vboxGetCurrentContext() VBoxTlsRefGetCurrent(struct wined3d_context, wined3d_context_tls_idx)
49# define vboxSetCurrentContext(_ctx) VBoxTlsRefSetCurrent(struct wined3d_context, wined3d_context_tls_idx, (_ctx))
50#endif
51
52/* FBO helper functions */
53
54/* Context activation is done by the caller. */
55static void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint fbo)
56{
57 const struct wined3d_gl_info *gl_info = context->gl_info;
58
59 switch (target)
60 {
61 case GL_READ_FRAMEBUFFER:
62 if (context->fbo_read_binding == fbo) return;
63 context->fbo_read_binding = fbo;
64 break;
65
66 case GL_DRAW_FRAMEBUFFER:
67 if (context->fbo_draw_binding == fbo) return;
68 context->fbo_draw_binding = fbo;
69 break;
70
71 case GL_FRAMEBUFFER:
72 if (context->fbo_read_binding == fbo
73 && context->fbo_draw_binding == fbo) return;
74 context->fbo_read_binding = fbo;
75 context->fbo_draw_binding = fbo;
76 break;
77
78 default:
79 FIXME("Unhandled target %#x.\n", target);
80 break;
81 }
82
83 gl_info->fbo_ops.glBindFramebuffer(target, fbo);
84 checkGLcall("glBindFramebuffer()");
85}
86
87/* Context activation is done by the caller. */
88static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info, GLenum target)
89{
90 unsigned int i;
91
92 for (i = 0; i < gl_info->limits.buffers; ++i)
93 {
94 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
95 checkGLcall("glFramebufferTexture2D()");
96 }
97 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
98 checkGLcall("glFramebufferTexture2D()");
99
100 gl_info->fbo_ops.glFramebufferTexture2D(target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
101 checkGLcall("glFramebufferTexture2D()");
102}
103
104/* Context activation is done by the caller. */
105static void context_destroy_fbo(struct wined3d_context *context, GLuint fbo)
106{
107 const struct wined3d_gl_info *gl_info = context->gl_info;
108
109 context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
110 context_clean_fbo_attachments(gl_info, GL_FRAMEBUFFER);
111 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
112
113 gl_info->fbo_ops.glDeleteFramebuffers(1, &fbo);
114 checkGLcall("glDeleteFramebuffers()");
115}
116
117static void context_attach_depth_stencil_rb(const struct wined3d_gl_info *gl_info,
118 GLenum fbo_target, DWORD format_flags, GLuint rb)
119{
120 if (format_flags & WINED3DFMT_FLAG_DEPTH)
121 {
122 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rb);
123 checkGLcall("glFramebufferRenderbuffer()");
124 }
125
126 if (format_flags & WINED3DFMT_FLAG_STENCIL)
127 {
128 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rb);
129 checkGLcall("glFramebufferRenderbuffer()");
130 }
131}
132
133/* Context activation is done by the caller. */
134static void context_attach_depth_stencil_fbo(struct wined3d_context *context,
135 GLenum fbo_target, struct wined3d_surface *depth_stencil, DWORD location)
136{
137 const struct wined3d_gl_info *gl_info = context->gl_info;
138
139 TRACE("Attach depth stencil %p\n", depth_stencil);
140
141 if (depth_stencil)
142 {
143 DWORD format_flags = depth_stencil->resource.format->flags;
144
145 if (depth_stencil->current_renderbuffer)
146 {
147 context_attach_depth_stencil_rb(gl_info, fbo_target,
148 format_flags, depth_stencil->current_renderbuffer->id);
149 }
150 else
151 {
152 switch (location)
153 {
154 case SFLAG_INTEXTURE:
155 case SFLAG_INSRGBTEX:
156 surface_prepare_texture(depth_stencil, context, FALSE);
157
158 if (format_flags & WINED3DFMT_FLAG_DEPTH)
159 {
160 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT,
161 depth_stencil->texture_target, depth_stencil->texture_name,
162 depth_stencil->texture_level);
163 checkGLcall("glFramebufferTexture2D()");
164 }
165
166 if (format_flags & WINED3DFMT_FLAG_STENCIL)
167 {
168 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT,
169 depth_stencil->texture_target, depth_stencil->texture_name,
170 depth_stencil->texture_level);
171 checkGLcall("glFramebufferTexture2D()");
172 }
173 break;
174
175 case SFLAG_INRB_MULTISAMPLE:
176 surface_prepare_rb(depth_stencil, gl_info, TRUE);
177 context_attach_depth_stencil_rb(gl_info, fbo_target,
178 format_flags, depth_stencil->rb_multisample);
179 break;
180
181 case SFLAG_INRB_RESOLVED:
182 surface_prepare_rb(depth_stencil, gl_info, FALSE);
183 context_attach_depth_stencil_rb(gl_info, fbo_target,
184 format_flags, depth_stencil->rb_resolved);
185 break;
186
187 default:
188 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location), location);
189 break;
190 }
191 }
192
193 if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
194 {
195 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
196 checkGLcall("glFramebufferTexture2D()");
197 }
198
199 if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
200 {
201 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
202 checkGLcall("glFramebufferTexture2D()");
203 }
204 }
205 else
206 {
207 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
208 checkGLcall("glFramebufferTexture2D()");
209
210 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
211 checkGLcall("glFramebufferTexture2D()");
212 }
213}
214
215/* Context activation is done by the caller. */
216static void context_attach_surface_fbo(struct wined3d_context *context,
217 GLenum fbo_target, DWORD idx, struct wined3d_surface *surface, DWORD location)
218{
219 const struct wined3d_gl_info *gl_info = context->gl_info;
220
221 TRACE("Attach surface %p to %u\n", surface, idx);
222
223 if (surface && surface->resource.format->id != WINED3DFMT_NULL)
224 {
225 BOOL srgb;
226
227 switch (location)
228 {
229 case SFLAG_INTEXTURE:
230 case SFLAG_INSRGBTEX:
231 srgb = location == SFLAG_INSRGBTEX;
232 surface_prepare_texture(surface, context, srgb);
233 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
234 surface->texture_target, surface_get_texture_name(surface, gl_info, srgb),
235 surface->texture_level);
236 checkGLcall("glFramebufferTexture2D()");
237 break;
238
239 case SFLAG_INRB_MULTISAMPLE:
240 surface_prepare_rb(surface, gl_info, TRUE);
241 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
242 GL_RENDERBUFFER, surface->rb_multisample);
243 checkGLcall("glFramebufferRenderbuffer()");
244 break;
245
246 case SFLAG_INRB_RESOLVED:
247 surface_prepare_rb(surface, gl_info, FALSE);
248 gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_COLOR_ATTACHMENT0 + idx,
249 GL_RENDERBUFFER, surface->rb_resolved);
250 checkGLcall("glFramebufferRenderbuffer()");
251 break;
252
253 default:
254 ERR("Unsupported location %s (%#x).\n", debug_surflocation(location), location);
255 break;
256 }
257 }
258 else
259 {
260 gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, GL_TEXTURE_2D, 0, 0);
261 checkGLcall("glFramebufferTexture2D()");
262 }
263}
264
265/* Context activation is done by the caller. */
266void context_check_fbo_status(const struct wined3d_context *context, GLenum target)
267{
268 const struct wined3d_gl_info *gl_info = context->gl_info;
269 GLenum status;
270
271 if (!FIXME_ON(d3d)) return;
272
273 status = gl_info->fbo_ops.glCheckFramebufferStatus(target);
274 if (status == GL_FRAMEBUFFER_COMPLETE)
275 {
276 TRACE("FBO complete\n");
277 }
278 else
279 {
280 const struct wined3d_surface *attachment;
281 unsigned int i;
282
283 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
284
285 if (!context->current_fbo)
286 {
287 ERR("FBO 0 is incomplete, driver bug?\n");
288 return;
289 }
290
291 FIXME("\tLocation %s (%#x).\n", debug_surflocation(context->current_fbo->location),
292 context->current_fbo->location);
293
294 /* Dump the FBO attachments */
295 for (i = 0; i < gl_info->limits.buffers; ++i)
296 {
297 attachment = context->current_fbo->render_targets[i];
298 if (attachment)
299 {
300 FIXME("\tColor attachment %d: (%p) %s %ux%u %u samples.\n",
301 i, attachment, debug_d3dformat(attachment->resource.format->id),
302 attachment->pow2Width, attachment->pow2Height, attachment->resource.multisample_type);
303 }
304 }
305 attachment = context->current_fbo->depth_stencil;
306 if (attachment)
307 {
308 FIXME("\tDepth attachment: (%p) %s %ux%u %u samples.\n",
309 attachment, debug_d3dformat(attachment->resource.format->id),
310 attachment->pow2Width, attachment->pow2Height, attachment->resource.multisample_type);
311 }
312 }
313}
314
315static inline DWORD context_generate_rt_mask(GLenum buffer)
316{
317 /* Should take care of all the GL_FRONT/GL_BACK/GL_AUXi/GL_NONE... cases */
318 return buffer ? (1 << 31) | buffer : 0;
319}
320
321static inline DWORD context_generate_rt_mask_from_surface(const struct wined3d_surface *target)
322{
323 return (1 << 31) | surface_get_gl_buffer(target);
324}
325
326static struct fbo_entry *context_create_fbo_entry(const struct wined3d_context *context,
327 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
328{
329 const struct wined3d_gl_info *gl_info = context->gl_info;
330 struct fbo_entry *entry;
331
332 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
333 entry->render_targets = HeapAlloc(GetProcessHeap(), 0, gl_info->limits.buffers * sizeof(*entry->render_targets));
334 memcpy(entry->render_targets, render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
335 entry->depth_stencil = depth_stencil;
336 entry->location = location;
337 entry->rt_mask = context_generate_rt_mask(GL_COLOR_ATTACHMENT0);
338 entry->attached = FALSE;
339 gl_info->fbo_ops.glGenFramebuffers(1, &entry->id);
340 checkGLcall("glGenFramebuffers()");
341 TRACE("Created FBO %u.\n", entry->id);
342
343 return entry;
344}
345
346/* Context activation is done by the caller. */
347static void context_reuse_fbo_entry(struct wined3d_context *context, GLenum target,
348 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil,
349 DWORD location, struct fbo_entry *entry)
350{
351 const struct wined3d_gl_info *gl_info = context->gl_info;
352
353 context_bind_fbo(context, target, entry->id);
354 context_clean_fbo_attachments(gl_info, target);
355
356 memcpy(entry->render_targets, render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
357 entry->depth_stencil = depth_stencil;
358 entry->location = location;
359 entry->attached = FALSE;
360}
361
362/* Context activation is done by the caller. */
363static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
364{
365 if (entry->id)
366 {
367 TRACE("Destroy FBO %u.\n", entry->id);
368 context_destroy_fbo(context, entry->id);
369 }
370 --context->fbo_entry_count;
371 list_remove(&entry->entry);
372 HeapFree(GetProcessHeap(), 0, entry->render_targets);
373 HeapFree(GetProcessHeap(), 0, entry);
374}
375
376
377/* Context activation is done by the caller. */
378static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context, GLenum target,
379 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
380{
381 const struct wined3d_gl_info *gl_info = context->gl_info;
382 struct fbo_entry *entry;
383
384 if (depth_stencil && render_targets && render_targets[0])
385 {
386 if (depth_stencil->resource.width < render_targets[0]->resource.width ||
387 depth_stencil->resource.height < render_targets[0]->resource.height)
388 {
389 WARN("Depth stencil is smaller than the primary color buffer, disabling\n");
390 depth_stencil = NULL;
391 }
392 }
393
394 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
395 {
396 if (!memcmp(entry->render_targets,
397 render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets))
398 && entry->depth_stencil == depth_stencil && entry->location == location)
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, render_targets, depth_stencil, location);
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, target, render_targets, depth_stencil, location, entry);
416 list_remove(&entry->entry);
417 list_add_head(&context->fbo_list, &entry->entry);
418 }
419
420 return entry;
421}
422
423/* Context activation is done by the caller. */
424static void context_apply_fbo_entry(struct wined3d_context *context, GLenum target, struct fbo_entry *entry)
425{
426 const struct wined3d_gl_info *gl_info = context->gl_info;
427 unsigned int i;
428 GLuint read_binding, draw_binding;
429
430 if (entry->attached)
431 {
432 context_bind_fbo(context, target, entry->id);
433 return;
434 }
435
436 read_binding = context->fbo_read_binding;
437 draw_binding = context->fbo_draw_binding;
438 context_bind_fbo(context, GL_FRAMEBUFFER, entry->id);
439
440 /* Apply render targets */
441 for (i = 0; i < gl_info->limits.buffers; ++i)
442 {
443 context_attach_surface_fbo(context, target, i, entry->render_targets[i], entry->location);
444 }
445
446 /* Apply depth targets */
447 if (entry->depth_stencil)
448 surface_set_compatible_renderbuffer(entry->depth_stencil, entry->render_targets[0]);
449 context_attach_depth_stencil_fbo(context, target, entry->depth_stencil, entry->location);
450
451 /* Set valid read and draw buffer bindings to satisfy pedantic pre-ES2_compatibility
452 * GL contexts requirements. */
453 glReadBuffer(GL_NONE);
454 context_set_draw_buffer(context, GL_NONE);
455 if (target != GL_FRAMEBUFFER)
456 {
457 if (target == GL_READ_FRAMEBUFFER)
458 context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, draw_binding);
459 else
460 context_bind_fbo(context, GL_READ_FRAMEBUFFER, read_binding);
461 }
462
463 entry->attached = TRUE;
464}
465
466/* Context activation is done by the caller. */
467static void context_apply_fbo_state(struct wined3d_context *context, GLenum target,
468 struct wined3d_surface **render_targets, struct wined3d_surface *depth_stencil, DWORD location)
469{
470 struct fbo_entry *entry, *entry2;
471
472 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
473 {
474 context_destroy_fbo_entry(context, entry);
475 }
476
477 if (context->rebind_fbo)
478 {
479 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
480 context->rebind_fbo = FALSE;
481 }
482
483 if (location == SFLAG_INDRAWABLE)
484 {
485 context->current_fbo = NULL;
486 context_bind_fbo(context, target, 0);
487 }
488 else
489 {
490 context->current_fbo = context_find_fbo_entry(context, target, render_targets, depth_stencil, location);
491 context_apply_fbo_entry(context, target, context->current_fbo);
492 }
493}
494
495/* Context activation is done by the caller. */
496void context_apply_fbo_state_blit(struct wined3d_context *context, GLenum target,
497 struct wined3d_surface *render_target, struct wined3d_surface *depth_stencil, DWORD location)
498{
499 UINT clear_size = (context->gl_info->limits.buffers - 1) * sizeof(*context->blit_targets);
500
501 context->blit_targets[0] = render_target;
502 if (clear_size)
503 memset(&context->blit_targets[1], 0, clear_size);
504 context_apply_fbo_state(context, target, context->blit_targets, depth_stencil, location);
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 GL_EXTCALL(glGenQueriesARB(1, &query->id));
521 checkGLcall("glGenQueriesARB");
522
523 TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
524 }
525 else
526 {
527 WARN("Occlusion queries not supported, not allocating query id.\n");
528 query->id = 0;
529 }
530 }
531
532 query->context = context;
533 list_add_head(&context->occlusion_queries, &query->entry);
534}
535
536void context_free_occlusion_query(struct wined3d_occlusion_query *query)
537{
538 struct wined3d_context *context = query->context;
539
540 list_remove(&query->entry);
541 query->context = NULL;
542
543 if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
544 {
545 UINT new_size = context->free_occlusion_query_size << 1;
546 GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
547 new_size * sizeof(*context->free_occlusion_queries));
548
549 if (!new_data)
550 {
551 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
552 return;
553 }
554
555 context->free_occlusion_query_size = new_size;
556 context->free_occlusion_queries = new_data;
557 }
558
559 context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
560}
561
562/* Context activation is done by the caller. */
563void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
564{
565 const struct wined3d_gl_info *gl_info = context->gl_info;
566
567 if (context->free_event_query_count)
568 {
569 query->object = context->free_event_queries[--context->free_event_query_count];
570 }
571 else
572 {
573 if (gl_info->supported[ARB_SYNC])
574 {
575 /* Using ARB_sync, not much to do here. */
576 query->object.sync = NULL;
577 TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
578 }
579 else if (gl_info->supported[APPLE_FENCE])
580 {
581 GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
582 checkGLcall("glGenFencesAPPLE");
583
584 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
585 }
586 else if(gl_info->supported[NV_FENCE])
587 {
588 GL_EXTCALL(glGenFencesNV(1, &query->object.id));
589 checkGLcall("glGenFencesNV");
590
591 TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
592 }
593 else
594 {
595 WARN("Event queries not supported, not allocating query id.\n");
596 query->object.id = 0;
597 }
598 }
599
600 query->context = context;
601 list_add_head(&context->event_queries, &query->entry);
602}
603
604void context_free_event_query(struct wined3d_event_query *query)
605{
606 struct wined3d_context *context = query->context;
607
608 list_remove(&query->entry);
609 query->context = NULL;
610
611 if (context->free_event_query_count >= context->free_event_query_size - 1)
612 {
613 UINT new_size = context->free_event_query_size << 1;
614 union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
615 new_size * sizeof(*context->free_event_queries));
616
617 if (!new_data)
618 {
619 ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
620 return;
621 }
622
623 context->free_event_query_size = new_size;
624 context->free_event_queries = new_data;
625 }
626
627 context->free_event_queries[context->free_event_query_count++] = query->object;
628}
629
630typedef void (context_fbo_entry_func_t)(struct wined3d_context *context, struct fbo_entry *entry);
631
632static void context_enum_surface_fbo_entries(const struct wined3d_device *device,
633 const struct wined3d_surface *surface, context_fbo_entry_func_t *callback)
634{
635 UINT i;
636
637 for (i = 0; i < device->context_count; ++i)
638 {
639 struct wined3d_context *context = device->contexts[i];
640 const struct wined3d_gl_info *gl_info = context->gl_info;
641 struct fbo_entry *entry, *entry2;
642
643 if (context->current_rt == surface) context->current_rt = NULL;
644
645 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
646 {
647 UINT j;
648
649 if (entry->depth_stencil == surface)
650 {
651 callback(context, entry);
652 continue;
653 }
654
655 for (j = 0; j < gl_info->limits.buffers; ++j)
656 {
657 if (entry->render_targets[j] == surface)
658 {
659 callback(context, entry);
660 break;
661 }
662 }
663 }
664 }
665}
666
667static void context_queue_fbo_entry_destruction(struct wined3d_context *context, struct fbo_entry *entry)
668{
669 list_remove(&entry->entry);
670 list_add_head(&context->fbo_destroy_list, &entry->entry);
671}
672
673void context_resource_released(const struct wined3d_device *device,
674 struct wined3d_resource *resource, enum wined3d_resource_type type)
675{
676 if (!device->d3d_initialized) return;
677
678 switch (type)
679 {
680 case WINED3D_RTYPE_SURFACE:
681 context_enum_surface_fbo_entries(device, surface_from_resource(resource),
682 context_queue_fbo_entry_destruction);
683 break;
684
685 default:
686 break;
687 }
688}
689
690static void context_detach_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
691{
692 entry->attached = FALSE;
693}
694
695void context_resource_unloaded(const struct wined3d_device *device,
696 struct wined3d_resource *resource, enum wined3d_resource_type type)
697{
698 switch (type)
699 {
700 case WINED3D_RTYPE_SURFACE:
701 context_enum_surface_fbo_entries(device, surface_from_resource(resource),
702 context_detach_fbo_entry);
703 break;
704
705 default:
706 break;
707 }
708}
709
710void context_surface_update(struct wined3d_context *context, const struct wined3d_surface *surface)
711{
712 const struct wined3d_gl_info *gl_info = context->gl_info;
713 struct fbo_entry *entry = context->current_fbo;
714 unsigned int i;
715
716 if (!entry || context->rebind_fbo) return;
717
718 for (i = 0; i < gl_info->limits.buffers; ++i)
719 {
720 if (surface == entry->render_targets[i])
721 {
722 TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface, i);
723 context->rebind_fbo = TRUE;
724 return;
725 }
726 }
727
728 if (surface == entry->depth_stencil)
729 {
730 TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface);
731 context->rebind_fbo = TRUE;
732 }
733}
734
735#ifndef VBOX
736static BOOL context_set_pixel_format(const struct wined3d_gl_info *gl_info, HDC dc, int format)
737{
738 int current = GetPixelFormat(dc);
739
740 if (current == format) return TRUE;
741
742 if (!current)
743 {
744 if (!SetPixelFormat(dc, format, NULL))
745 {
746 /* This may also happen if the dc belongs to a destroyed window. */
747 WARN("Failed to set pixel format %d on device context %p, last error %#x.\n",
748 format, dc, GetLastError());
749 return FALSE;
750 }
751 return TRUE;
752 }
753
754 /* By default WGL doesn't allow pixel format adjustments but we need it
755 * here. For this reason there's a Wine specific wglSetPixelFormat()
756 * which allows us to set the pixel format multiple times. Only use it
757 * when really needed. */
758 if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
759 {
760 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format)))
761 {
762 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
763 format, dc);
764 return FALSE;
765 }
766 return TRUE;
767 }
768
769 /* OpenGL doesn't allow pixel format adjustments. Print an error and
770 * continue using the old format. There's a big chance that the old
771 * format works although with a performance hit and perhaps rendering
772 * errors. */
773 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
774 format, dc, current);
775 return TRUE;
776}
777#endif
778
779static BOOL context_set_gl_context(struct wined3d_context *ctx)
780{
781 struct wined3d_swapchain *swapchain = ctx->swapchain;
782#ifndef VBOX
783 BOOL backup = FALSE;
784
785 if (!context_set_pixel_format(ctx->gl_info, ctx->hdc, ctx->pixel_format))
786 {
787 WARN("Failed to set pixel format %d on device context %p.\n",
788 ctx->pixel_format, ctx->hdc);
789 backup = TRUE;
790 }
791
792 if (backup || !wglMakeCurrent(ctx->hdc, ctx->glCtx))
793#else
794 if (!wglMakeCurrent(ctx->swapchain->hDC, ctx->glCtx))
795#endif
796 {
797#ifndef VBOX_WITH_WDDM
798 HDC dc;
799#endif
800
801#ifndef VBOX
802 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
803 ctx->glCtx, ctx->hdc, GetLastError());
804#else
805 WARN("Failed to make GL context %p current on device context %p, last error %#x.\n",
806 ctx->glCtx, ctx->swapchain->hDC, GetLastError());
807#endif
808 ctx->valid = 0;
809 WARN("Trying fallback to the backup window.\n");
810#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
811 /* FIXME: If the context is destroyed it's no longer associated with
812 * a swapchain, so we can't use the swapchain to get a backup dc. To
813 * make this work windowless contexts would need to be handled by the
814 * device. */
815 if (ctx->destroyed)
816 {
817 FIXME("Unable to get backup dc for destroyed context %p.\n", ctx);
818 context_set_current(NULL);
819 return FALSE;
820 }
821#endif
822#ifndef VBOX_WITH_WDDM
823 if (!(dc = swapchain_get_backup_dc(swapchain)))
824 {
825 context_set_current(NULL);
826 return FALSE;
827 }
828#ifndef VBOX
829 if (!context_set_pixel_format(ctx->gl_info, dc, ctx->pixel_format))
830 {
831 ERR("Failed to set pixel format %d on device context %p.\n",
832 ctx->pixel_format, dc);
833 context_set_current(NULL);
834 return FALSE;
835 }
836#endif
837 if (!wglMakeCurrent(dc, ctx->glCtx))
838 {
839 ERR("Fallback to backup window (dc %p) failed too, last error %#x.\n",
840 dc, GetLastError());
841 context_set_current(NULL);
842 return FALSE;
843 }
844#else
845 return FALSE;
846#endif
847 }
848 return TRUE;
849}
850
851static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx, int pf)
852{
853#ifndef VBOX
854 if (!context_set_pixel_format(gl_info, dc, pf))
855 {
856 ERR("Failed to restore pixel format %d on device context %p.\n", pf, dc);
857 context_set_current(NULL);
858 return;
859 }
860#endif
861 if (!wglMakeCurrent(dc, gl_ctx))
862 {
863 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
864 gl_ctx, dc, GetLastError());
865 context_set_current(NULL);
866 }
867#ifdef VBOX
868 else
869 {
870 /* success branch */
871 /* sync back our tls with gl settings */
872 const struct wined3d_context *current_context = context_get_current();
873 if (current_context && current_context->glCtx != gl_ctx)
874 {
875 UINT i = 0;
876 struct wined3d_device *device = current_context->swapchain->device;
877 for (; i < device->context_count; ++i)
878 {
879 struct wined3d_context *ctx = device->contexts[i];
880 if (ctx->glCtx == gl_ctx)
881 {
882 context_set_current(ctx);
883 break;
884 }
885 }
886
887 if (i == device->context_count)
888 {
889 context_set_current(NULL);
890 }
891 }
892 }
893#endif
894}
895
896#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
897static BOOL swapchain_validate(const struct wined3d_swapchain *swapchain)
898{
899 if (!swapchain->hDC)
900 {
901 ERR("NULL hDC");
902 return FALSE;
903 }
904
905#if defined(DEBUG) || !defined(VBOX_WITH_WDDM)
906 {
907 HWND hWnd = WindowFromDC(swapchain->hDC);
908 if (hWnd != swapchain->win_handle)
909 {
910# if defined(VBOX_WITH_WDDM)
911 ERR("Unexpected swapchain for dc %p window expected %p, but was %p.\n", swapchain->hDC, swapchain->win_handle, hWnd);
912# else
913 ERR("Swapchain for dc %p window expected %p, but was %p.\n", swapchain->hDC, swapchain->win_handle, hWnd);
914 return FALSE;
915# endif
916 }
917 }
918#endif
919 return TRUE;
920}
921
922static struct wined3d_swapchain* swapchain_find_valid(const struct wined3d_device *device)
923{
924 int i;
925 struct wined3d_context * context = device->contexts[0];
926 struct wined3d_swapchain* swapchain = NULL;
927 if (context)
928 {
929 swapchain = context->swapchain;
930 if (swapchain && swapchain_validate(swapchain))
931 {
932 return swapchain;
933 }
934 }
935 for (i = device->swapchain_count - 1; i >= 0 ; --i)
936 {
937 Assert(device->swapchains[i]);
938 if (swapchain != device->swapchains[i] /* the current context swapchain is known to be invalid */
939 && swapchain_validate(device->swapchains[i]))
940 {
941 return device->swapchains[i];
942 }
943 }
944
945 return NULL;
946}
947
948static struct wined3d_swapchain* swapchain_find_valid_or_any(const struct wined3d_device *device)
949{
950 struct wined3d_swapchain* swapchain = swapchain_find_valid(device);
951 if (swapchain)
952 return swapchain;
953
954 ERR("no valid swapchain found!");
955 swapchain = device->swapchains[0];
956 Assert(swapchain);
957 return swapchain;
958}
959
960struct wined3d_context *context_find_create(struct wined3d_device *device,
961 struct wined3d_swapchain *swapchain,
962 const struct wined3d_format *ds_format)
963{
964 struct wined3d_context *context;
965 struct wined3d_surface *target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
966 if (!device->context_count)
967 {
968 Assert(!device->swapchain_count);
969 context = context_create(swapchain, target, ds_format
970#ifdef VBOX_WITH_WDDM
971 , device->pHgsmi
972#endif
973 );
974 if(!context)
975 {
976 ERR("Failed to create the context.\n");
977 }
978 else
979 {
980 Assert(context->valid);
981 }
982 }
983 else
984 {
985 context = context_acquire(device, target);
986 if(!context)
987 {
988 ERR("Failed to acquire the context.\n");
989 }
990 else
991 {
992 Assert(context->valid);
993 }
994 }
995
996 return context;
997}
998
999#endif
1000
1001static void context_update_window(struct wined3d_context *context
1002#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1003 , struct wined3d_swapchain *swapchain
1004#endif
1005 )
1006{
1007#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1008 TRACE("Updating context %p swapchain from %p to %p.\n",
1009 context, context->swapchain, swapchain);
1010
1011 if (context->swapchain == swapchain)
1012 return;
1013
1014 if (!swapchain_validate(swapchain))
1015 {
1016 ERR("invalid swapchain %p\n", swapchain);
1017 goto err;
1018 }
1019 context->swapchain = swapchain;
1020
1021 context->valid = 1;
1022
1023 context_set_gl_context(context);
1024#else
1025 if (context->win_handle == context->swapchain->win_handle)
1026 return;
1027
1028 TRACE("Updating context %p window from %p to %p.\n",
1029 context, context->win_handle, context->swapchain->win_handle);
1030
1031 if (context->valid)
1032 {
1033 /* You'd figure ReleaseDC() would fail if the DC doesn't match the
1034 * window. However, that's not what actually happens, and there are
1035 * user32 tests that confirm ReleaseDC() with the wrong window is
1036 * supposed to succeed. So explicitly check that the DC belongs to
1037 * the window, since we want to avoid releasing a DC that belongs to
1038 * some other window if the original window was already destroyed. */
1039 if (WindowFromDC(context->hdc) != context->win_handle)
1040 {
1041 WARN("DC %p does not belong to window %p.\n",
1042 context->hdc, context->win_handle);
1043 }
1044#ifndef VBOX
1045 else if (!ReleaseDC(context->win_handle, context->hdc))
1046#else
1047 else if (!VBoxExtReleaseDC(context->win_handle, context->hdc))
1048#endif
1049 {
1050 ERR("Failed to release device context %p, last error %#x.\n",
1051 context->hdc, GetLastError());
1052 }
1053 }
1054 else context->valid = 1;
1055
1056 context->win_handle = context->swapchain->win_handle;
1057
1058#ifndef VBOX
1059 if (!(context->hdc = GetDC(context->win_handle)))
1060#else
1061 if (!(context->hdc = VBoxExtGetDC(context->win_handle)))
1062#endif
1063 {
1064 ERR("Failed to get a device context for window %p.\n", context->win_handle);
1065 goto err;
1066 }
1067#ifndef VBOX
1068 if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
1069 {
1070 ERR("Failed to set pixel format %d on device context %p.\n",
1071 context->pixel_format, context->hdc);
1072 goto err;
1073 }
1074#endif
1075 context_set_gl_context(context);
1076#endif
1077 return;
1078
1079err:
1080 context->valid = 0;
1081}
1082
1083#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1084static void context_adjust_any_valid(struct wined3d_context *context)
1085{
1086 struct wined3d_swapchain* swapchain = context->swapchain;
1087
1088 if (context->valid && swapchain && swapchain_validate(swapchain))
1089 return;
1090
1091 swapchain = swapchain_find_valid(context->device);
1092 if (!swapchain)
1093 {
1094 ERR("no valid swapchain found");
1095 Assert(!context->valid);
1096 return;
1097 }
1098
1099 context_update_window(context, swapchain);
1100}
1101#endif
1102
1103/* Do not call while under the GL lock. */
1104static void context_destroy_gl_resources(struct wined3d_context *context)
1105{
1106 const struct wined3d_gl_info *gl_info = context->gl_info;
1107 struct wined3d_occlusion_query *occlusion_query;
1108 struct wined3d_event_query *event_query;
1109 struct fbo_entry *entry, *entry2;
1110 HGLRC restore_ctx;
1111 HDC restore_dc;
1112 unsigned int i;
1113 int restore_pf;
1114
1115 restore_ctx = wglGetCurrentContext();
1116 restore_dc = wglGetCurrentDC();
1117 restore_pf = GetPixelFormat(restore_dc);
1118
1119#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1120 if (context->valid && restore_ctx != context->glCtx)
1121 context_set_gl_context(context);
1122#else
1123 if (restore_ctx != context->glCtx)
1124 context_adjust_any_valid(context);
1125#endif
1126 else
1127 restore_ctx = NULL;
1128
1129
1130 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
1131 {
1132 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
1133 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
1134 occlusion_query->context = NULL;
1135 }
1136
1137 LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
1138 {
1139 if (context->valid)
1140 {
1141 if (gl_info->supported[ARB_SYNC])
1142 {
1143 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
1144 }
1145 else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
1146 else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
1147 }
1148 event_query->context = NULL;
1149 }
1150
1151 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
1152 {
1153 if (!context->valid) entry->id = 0;
1154 context_destroy_fbo_entry(context, entry);
1155 }
1156
1157 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1158 {
1159 if (!context->valid) entry->id = 0;
1160 context_destroy_fbo_entry(context, entry);
1161 }
1162
1163 if (context->valid)
1164 {
1165 if (context->dummy_arbfp_prog)
1166 {
1167 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
1168 }
1169
1170 if (gl_info->supported[ARB_OCCLUSION_QUERY])
1171 GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
1172
1173 if (gl_info->supported[ARB_SYNC])
1174 {
1175 for (i = 0; i < context->free_event_query_count; ++i)
1176 {
1177 GL_EXTCALL(glDeleteSync(context->free_event_queries[i].sync));
1178 }
1179 }
1180 else if (gl_info->supported[APPLE_FENCE])
1181 {
1182 for (i = 0; i < context->free_event_query_count; ++i)
1183 {
1184 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
1185 }
1186 }
1187 else if (gl_info->supported[NV_FENCE])
1188 {
1189 for (i = 0; i < context->free_event_query_count; ++i)
1190 {
1191 GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
1192 }
1193 }
1194
1195 checkGLcall("context cleanup");
1196 }
1197
1198 HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
1199 HeapFree(GetProcessHeap(), 0, context->free_event_queries);
1200
1201 if (restore_ctx)
1202 {
1203 context_restore_gl_context(gl_info, restore_dc, restore_ctx, restore_pf);
1204 }
1205 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1206 {
1207 ERR("Failed to disable GL context.\n");
1208 }
1209
1210#ifndef VBOX_WITH_WDDM
1211# ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1212# ifndef VBOX
1213 ReleaseDC(context->win_handle, context->hdc);
1214# else
1215 VBoxExtReleaseDC(context->win_handle, context->hdc);
1216# endif
1217# endif
1218#endif
1219
1220 if (!wglDeleteContext(context->glCtx))
1221 {
1222 DWORD err = GetLastError();
1223 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
1224 }
1225}
1226
1227DWORD context_get_tls_idx(void)
1228{
1229 return wined3d_context_tls_idx;
1230}
1231
1232void context_set_tls_idx(DWORD idx)
1233{
1234 wined3d_context_tls_idx = idx;
1235}
1236
1237#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1238static struct wined3d_context *context_get_current_ex(DWORD adjustTid)
1239{
1240 struct wined3d_context *ctx = vboxGetCurrentContext();
1241 if (ctx && !VBoxTlsRefIsFunctional(ctx))
1242 {
1243 /* this is a destroyed context left in the tls of the current thread */
1244 /* 1. this releases the context and clears the tls */
1245 vboxSetCurrentContext(NULL);
1246 /* return there is no context current */
1247 return NULL;
1248 }
1249 if (!adjustTid)
1250 return ctx;
1251 if (!ctx || ctx->tid == adjustTid)
1252 return ctx;
1253 /* the context may have cleared swapchain (if swapchain was destroyed), ensure it has some valid swapchain set */
1254 if (context_set_current(ctx))
1255 {
1256 Assert(ctx->tid == adjustTid);
1257 return ctx;
1258 }
1259 ERR("context_set_current failed\n");
1260 return NULL;
1261}
1262#endif
1263
1264struct wined3d_context *context_get_current(void)
1265{
1266#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1267 return TlsGetValue(wined3d_context_tls_idx);
1268#else
1269 DWORD tid = GetCurrentThreadId();
1270 return context_get_current_ex(tid);
1271#endif
1272}
1273
1274/* Do not call while under the GL lock. */
1275BOOL context_set_current(struct wined3d_context *ctx)
1276{
1277#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1278 struct wined3d_context *old = context_get_current_ex(0);
1279 DWORD tid = GetCurrentThreadId();
1280#else
1281 struct wined3d_context *old = context_get_current();
1282#endif
1283
1284 if (old == ctx)
1285 {
1286#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1287 if (ctx && ctx->tid != tid)
1288 {
1289 old = NULL;
1290 }
1291 else
1292#endif
1293 {
1294 TRACE("Already using D3D context %p.\n", ctx);
1295 return TRUE;
1296 }
1297 }
1298
1299 if (old)
1300 {
1301#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1302 old->tid = 0;
1303 old->current = 0;
1304#else
1305 if (old->destroyed)
1306 {
1307 TRACE("Switching away from destroyed context %p.\n", old);
1308 context_destroy_gl_resources(old);
1309 HeapFree(GetProcessHeap(), 0, (void *)old->gl_info);
1310 HeapFree(GetProcessHeap(), 0, old);
1311 }
1312 else
1313 {
1314 old->current = 0;
1315 }
1316#endif
1317 }
1318
1319 if (ctx)
1320 {
1321 if (!ctx->valid)
1322 {
1323 ERR("Trying to make invalid context %p current\n", ctx);
1324 return FALSE;
1325 }
1326
1327#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1328 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1329#else
1330 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->swapchain->hDC);
1331#endif
1332 if (!context_set_gl_context(ctx))
1333 return FALSE;
1334 ctx->current = 1;
1335 }
1336 else if(wglGetCurrentContext())
1337 {
1338 TRACE("Clearing current D3D context.\n");
1339 if (!wglMakeCurrent(NULL, NULL))
1340 {
1341 DWORD err = GetLastError();
1342 ERR("Failed to clear current GL context, last error %#x.\n", err);
1343#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1344 vboxSetCurrentContext(NULL);
1345#else
1346 TlsSetValue(wined3d_context_tls_idx, NULL);
1347#endif
1348 return FALSE;
1349 }
1350 }
1351#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1352 vboxSetCurrentContext(ctx);
1353 if (ctx)
1354 ctx->tid = tid;
1355 return TRUE;
1356#else
1357 return TlsSetValue(wined3d_context_tls_idx, ctx);
1358#endif
1359}
1360
1361#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1362void context_clear_on_thread_detach()
1363{
1364 /* In theory, we should do context_set_current(NULL) here,
1365 * but since it may result in calling a context dtor, it should be done under wined3d lock.
1366 * We can not acquire a wined3d lock here since this routine is called in a DllMain context
1367 * and this would result in a lock order violation, which may result in a deadlock.
1368 * In other words, wined3d may internally call Win32 API functions which result in
1369 * a DLL lock acquisition while holding wined3d lock.
1370 * So lock order should always be "wined3d lock" -> "dll lock".
1371 *
1372 * This is why we do the following:
1373 * */
1374
1375 /* 1. get the current context w/o adjusting its thread id, etc. */
1376 struct wined3d_context *old = context_get_current_ex(0);
1377 if (!old)
1378 return;
1379
1380// /* there is a currently assigned context,
1381// * 2. now increase its ref count to ensure its dtor routine is not called while making set_current(NULL).
1382// * This is needed since dtor can only be run with a wined3d lock held */
1383// VBoxTlsRefAddRef(old);
1384
1385 /* context_tls_dtor now does only memfree, so just call it right away */
1386
1387 /* 3. now we can call context_set_current(NULL) */
1388 context_set_current(NULL);
1389
1390// /* 4. to avoid possible deadlocks we make an asynchronous call to a worker thread to make
1391// * wined3d lock - context release - wined3d unlock from there. */
1392// VBoxExtReleaseContextAsync(old);
1393}
1394#endif
1395
1396void context_release(struct wined3d_context *context)
1397{
1398 TRACE("Releasing context %p, level %u.\n", context, context->level);
1399
1400 if (WARN_ON(d3d))
1401 {
1402 if (!context->level)
1403 WARN("Context %p is not active.\n", context);
1404 else if (context != context_get_current())
1405 WARN("Context %p is not the current context.\n", context);
1406 }
1407
1408 if (!--context->level && context->restore_ctx)
1409 {
1410 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1411 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx, context->restore_pf);
1412 context->restore_ctx = NULL;
1413 context->restore_dc = NULL;
1414 }
1415}
1416
1417static void context_enter(struct wined3d_context *context)
1418{
1419 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1420
1421 if (!context->level++)
1422 {
1423 const struct wined3d_context *current_context = context_get_current();
1424 HGLRC current_gl = wglGetCurrentContext();
1425
1426 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1427 {
1428 TRACE("Another GL context (%p on device context %p) is already current.\n",
1429 current_gl, wglGetCurrentDC());
1430 context->restore_ctx = current_gl;
1431 context->restore_dc = wglGetCurrentDC();
1432 context->restore_pf = GetPixelFormat(context->restore_dc);
1433 }
1434 }
1435}
1436
1437void context_invalidate_state(struct wined3d_context *context, DWORD state)
1438{
1439 DWORD rep = context->state_table[state].representative;
1440 DWORD idx;
1441 BYTE shift;
1442
1443 if (isStateDirty(context, rep)) return;
1444
1445 context->dirtyArray[context->numDirtyEntries++] = rep;
1446 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1447 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1448 context->isStateDirty[idx] |= (1 << shift);
1449}
1450
1451/* This function takes care of wined3d pixel format selection. */
1452static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1453 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1454 BOOL auxBuffers, BOOL findCompatible)
1455{
1456 int iPixelFormat=0;
1457 BYTE redBits, greenBits, blueBits, alphaBits, colorBits;
1458 BYTE depthBits=0, stencilBits=0;
1459 unsigned int current_value;
1460 unsigned int cfg_count = device->adapter->cfg_count;
1461 unsigned int i;
1462
1463 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1464 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1465 auxBuffers, findCompatible);
1466
1467 if (!getColorBits(color_format, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
1468 {
1469 ERR("Unable to get color bits for format %s (%#x)!\n",
1470 debug_d3dformat(color_format->id), color_format->id);
1471 return 0;
1472 }
1473
1474 getDepthStencilBits(ds_format, &depthBits, &stencilBits);
1475
1476 current_value = 0;
1477 for (i = 0; i < cfg_count; ++i)
1478 {
1479 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1480 unsigned int value;
1481
1482 /* For now only accept RGBA formats. Perhaps some day we will
1483 * allow floating point formats for pbuffers. */
1484 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1485 continue;
1486 /* In window mode we need a window drawable format and double buffering. */
1487 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1488 continue;
1489 if (cfg->redSize < redBits)
1490 continue;
1491 if (cfg->greenSize < greenBits)
1492 continue;
1493 if (cfg->blueSize < blueBits)
1494 continue;
1495 if (cfg->alphaSize < alphaBits)
1496 continue;
1497 if (cfg->depthSize < depthBits)
1498 continue;
1499 if (stencilBits && cfg->stencilSize != stencilBits)
1500 continue;
1501 /* Check multisampling support. */
1502 if (cfg->numSamples)
1503 continue;
1504
1505 value = 1;
1506 /* We try to locate a format which matches our requirements exactly. In case of
1507 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1508 if (cfg->depthSize == depthBits)
1509 value += 1;
1510 if (cfg->stencilSize == stencilBits)
1511 value += 2;
1512 if (cfg->alphaSize == alphaBits)
1513 value += 4;
1514 /* We like to have aux buffers in backbuffer mode */
1515 if (auxBuffers && cfg->auxBuffers)
1516 value += 8;
1517 if (cfg->redSize == redBits
1518 && cfg->greenSize == greenBits
1519 && cfg->blueSize == blueBits)
1520 value += 16;
1521
1522 if (value > current_value)
1523 {
1524 iPixelFormat = cfg->iPixelFormat;
1525 current_value = value;
1526 }
1527 }
1528
1529 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1530 if(!iPixelFormat && !findCompatible) {
1531 ERR("Can't find a suitable iPixelFormat\n");
1532 return FALSE;
1533 } else if(!iPixelFormat) {
1534 PIXELFORMATDESCRIPTOR pfd;
1535
1536 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1537 /* PixelFormat selection */
1538 ZeroMemory(&pfd, sizeof(pfd));
1539 pfd.nSize = sizeof(pfd);
1540 pfd.nVersion = 1;
1541 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1542 pfd.iPixelType = PFD_TYPE_RGBA;
1543 pfd.cAlphaBits = alphaBits;
1544 pfd.cColorBits = colorBits;
1545 pfd.cDepthBits = depthBits;
1546 pfd.cStencilBits = stencilBits;
1547 pfd.iLayerType = PFD_MAIN_PLANE;
1548
1549 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1550 if(!iPixelFormat) {
1551 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1552 ERR("Can't find a suitable iPixelFormat\n");
1553 return FALSE;
1554 }
1555 }
1556
1557 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1558 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1559 return iPixelFormat;
1560}
1561
1562/* Context activation is done by the caller. */
1563static void bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1564{
1565 const struct wined3d_gl_info *gl_info = context->gl_info;
1566 unsigned int i, count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1567
1568 for (i = 0; i < count; ++i)
1569 {
1570 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1571 checkGLcall("glActiveTextureARB");
1572
1573 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1574 checkGLcall("glBindTexture");
1575
1576 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1577 {
1578 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1579 checkGLcall("glBindTexture");
1580 }
1581
1582 if (gl_info->supported[EXT_TEXTURE3D])
1583 {
1584 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1585 checkGLcall("glBindTexture");
1586 }
1587
1588 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1589 {
1590 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1591 checkGLcall("glBindTexture");
1592 }
1593 }
1594}
1595
1596#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1597static DECLCALLBACK(void) context_tls_dtor(void* pvCtx)
1598{
1599 struct wined3d_context * context = (struct wined3d_context *)pvCtx;
1600 HeapFree(GetProcessHeap(), 0, context);
1601}
1602#endif
1603
1604BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1605{
1606 return gl_info->supported[ARB_DEBUG_OUTPUT]
1607 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1608}
1609
1610static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1611 GLenum severity, GLsizei length, const char *message, void *ctx)
1612{
1613 switch (type)
1614 {
1615 case GL_DEBUG_TYPE_ERROR_ARB:
1616 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1617 break;
1618
1619 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1620 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1621 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1622 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1623 break;
1624
1625 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1626 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1627 break;
1628
1629 default:
1630 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1631 break;
1632 }
1633}
1634
1635/* Do not call while under the GL lock. */
1636struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1637 struct wined3d_surface *target, const struct wined3d_format *ds_format
1638#ifdef VBOX_WITH_WDDM
1639 , struct VBOXUHGSMI *pHgsmi
1640#endif
1641 )
1642{
1643 struct wined3d_device *device = swapchain->device;
1644 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1645 const struct wined3d_format *color_format;
1646 struct wined3d_context *ret;
1647 BOOL auxBuffers = FALSE;
1648 HGLRC ctx, share_ctx;
1649 int pixel_format;
1650 unsigned int s;
1651 int swap_interval;
1652 DWORD state;
1653 HDC hdc;
1654
1655 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1656
1657#ifdef VBOX_WITH_WDDM
1658 if (!pHgsmi)
1659 {
1660 ERR("HGSMI should be specified!");
1661 return NULL;
1662 }
1663#endif
1664
1665 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1666 if (!ret)
1667 return NULL;
1668
1669 ret->blit_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1670 gl_info->limits.buffers * sizeof(*ret->blit_targets));
1671 if (!ret->blit_targets)
1672 goto out;
1673
1674 ret->draw_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1675 gl_info->limits.buffers * sizeof(*ret->draw_buffers));
1676 if (!ret->draw_buffers)
1677 goto out;
1678
1679 ret->free_occlusion_query_size = 4;
1680 ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
1681 ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
1682 if (!ret->free_occlusion_queries)
1683 goto out;
1684
1685 list_init(&ret->occlusion_queries);
1686
1687 ret->free_event_query_size = 4;
1688 ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
1689 ret->free_event_query_size * sizeof(*ret->free_event_queries));
1690 if (!ret->free_event_queries)
1691 goto out;
1692
1693 list_init(&ret->event_queries);
1694 list_init(&ret->fbo_list);
1695 list_init(&ret->fbo_destroy_list);
1696
1697#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1698 VBoxTlsRefInit(ret, context_tls_dtor);
1699#endif
1700
1701#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1702 ret->device = device;
1703 if (!swapchain->hDC)
1704 {
1705 ERR("Swapchain hDC is null");
1706 goto out;
1707 }
1708 hdc = swapchain->hDC;
1709#else
1710 if (!(hdc = GetDC(swapchain->win_handle)))
1711 {
1712 WARN("Failed to retireve device context, trying swapchain backup.\n");
1713 if (!(hdc = swapchain_get_backup_dc(swapchain)))
1714 {
1715 ERR("Failed to retrieve a device context.\n");
1716 goto out;
1717 }
1718 }
1719#endif
1720
1721 color_format = target->resource.format;
1722
1723 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1724 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1725 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1726 {
1727 auxBuffers = TRUE;
1728
1729 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1730 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM);
1731 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1732 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1733 }
1734
1735 /* DirectDraw supports 8bit paletted render targets and these are used by
1736 * old games like StarCraft and C&C. Most modern hardware doesn't support
1737 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1738 * conversion (ab)uses the alpha component for storing the palette index.
1739 * For this reason we require a format with 8bit alpha, so request
1740 * A8R8G8B8. */
1741 if (color_format->id == WINED3DFMT_P8_UINT)
1742 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1743
1744 /* Try to find a pixel format which matches our requirements. */
1745 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, FALSE);
1746
1747 /* Try to locate a compatible format if we weren't able to find anything. */
1748 if (!pixel_format)
1749 {
1750 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1751 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, TRUE);
1752 }
1753
1754 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1755 if (!pixel_format)
1756 {
1757 ERR("Can't find a suitable pixel format.\n");
1758 goto out;
1759 }
1760
1761 context_enter(ret);
1762#ifndef VBOX
1763 if (!context_set_pixel_format(gl_info, hdc, pixel_format))
1764 {
1765 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1766 context_release(ret);
1767 goto out;
1768 }
1769
1770 share_ctx = device->context_count ? device->contexts[0]->glCtx : NULL;
1771 if (gl_info->p_wglCreateContextAttribsARB)
1772 {
1773 unsigned int ctx_attrib_idx = 0;
1774 GLint ctx_attribs[3];
1775
1776 if (context_debug_output_enabled(gl_info))
1777 {
1778 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1779 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_DEBUG_BIT_ARB;
1780 }
1781 ctx_attribs[ctx_attrib_idx] = 0;
1782
1783 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1784 {
1785 ERR("Failed to create a WGL context.\n");
1786 context_release(ret);
1787 goto out;
1788 }
1789 }
1790 else
1791 {
1792 if (!(ctx = wglCreateContext(hdc)))
1793#else
1794 ctx = pVBoxCreateContext(hdc
1795#ifdef VBOX_WITH_WDDM
1796 , pHgsmi
1797#else
1798 , NULL
1799#endif
1800 );
1801 if (!ctx)
1802#endif
1803 {
1804 ERR("Failed to create a WGL context.\n");
1805 context_release(ret);
1806 goto out;
1807 }
1808
1809#ifdef VBOX_WITH_WDDM
1810 pVBoxCtxChromiumParameteriCR(ctx, GL_HOST_WND_CREATED_HIDDEN_CR, GL_TRUE);
1811#endif
1812
1813#ifndef VBOX
1814 if (share_ctx && !wglShareLists(share_ctx, ctx))
1815 {
1816 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
1817 context_release(ret);
1818 if (!wglDeleteContext(ctx))
1819 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1820 goto out;
1821 }
1822 }
1823#endif
1824
1825 if (!device_context_add(device, ret))
1826 {
1827 ERR("Failed to add the newly created context to the context list\n");
1828 context_release(ret);
1829 if (!wglDeleteContext(ctx))
1830 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1831 goto out;
1832 }
1833
1834 ret->gl_info = gl_info;
1835 ret->d3d_info = &device->adapter->d3d_info;
1836 ret->state_table = device->StateTable;
1837
1838 /* Mark all states dirty to force a proper initialization of the states
1839 * on the first use of the context. */
1840 for (state = 0; state <= STATE_HIGHEST; ++state)
1841 {
1842 if (ret->state_table[state].representative)
1843 context_invalidate_state(ret, state);
1844 }
1845
1846 ret->swapchain = swapchain;
1847 ret->current_rt = target;
1848#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1849 ret->tid = GetCurrentThreadId();
1850#else
1851 ret->tid = 0;
1852#endif
1853
1854 ret->render_offscreen = surface_is_offscreen(target);
1855 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
1856 ret->valid = 1;
1857
1858 ret->glCtx = ctx;
1859#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1860 ret->win_handle = swapchain->win_handle;
1861 ret->hdc = hdc;
1862#endif
1863 ret->pixel_format = pixel_format;
1864
1865 /* Set up the context defaults */
1866 if (!context_set_current(ret))
1867 {
1868 ERR("Cannot activate context to set up defaults.\n");
1869 device_context_remove(device, ret);
1870 context_release(ret);
1871 if (!wglDeleteContext(ctx))
1872 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1873 goto out;
1874 }
1875
1876 if (context_debug_output_enabled(gl_info))
1877 {
1878 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback, ret));
1879 if (TRACE_ON(d3d_synchronous))
1880 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
1881 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
1882 if (ERR_ON(d3d))
1883 {
1884 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR_ARB,
1885 GL_DONT_CARE, 0, NULL, GL_TRUE));
1886 }
1887 if (FIXME_ON(d3d))
1888 {
1889 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB,
1890 GL_DONT_CARE, 0, NULL, GL_TRUE));
1891 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB,
1892 GL_DONT_CARE, 0, NULL, GL_TRUE));
1893 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY_ARB,
1894 GL_DONT_CARE, 0, NULL, GL_TRUE));
1895 }
1896 if (WARN_ON(d3d_perf))
1897 {
1898 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE_ARB,
1899 GL_DONT_CARE, 0, NULL, GL_TRUE));
1900 }
1901 }
1902
1903 switch (swapchain->desc.swap_interval)
1904 {
1905 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1906 swap_interval = 0;
1907 break;
1908 case WINED3DPRESENT_INTERVAL_DEFAULT:
1909 case WINED3DPRESENT_INTERVAL_ONE:
1910 swap_interval = 1;
1911 break;
1912 case WINED3DPRESENT_INTERVAL_TWO:
1913 swap_interval = 2;
1914 break;
1915 case WINED3DPRESENT_INTERVAL_THREE:
1916 swap_interval = 3;
1917 break;
1918 case WINED3DPRESENT_INTERVAL_FOUR:
1919 swap_interval = 4;
1920 break;
1921 default:
1922 FIXME("Unknown swap interval %#x.\n", swapchain->desc.swap_interval);
1923 swap_interval = 1;
1924 }
1925
1926 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1927 {
1928 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1929 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1930 swap_interval, ret, GetLastError());
1931 }
1932
1933#ifdef VBOX
1934 /* for WDDM case this is used for shared resource handling
1935 *
1936 * for XPDM this is needed to at least support texture sharing between device contexts.
1937 * this is a kinda hack, but it is needed since our ogl driver currently does not support ShareLists */
1938 pglChromiumParameteriCR(GL_SHARE_CONTEXT_RESOURCES_CR, GL_TRUE);
1939# if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1940 pglChromiumParameteriCR(GL_FLUSH_ON_THREAD_SWITCH_CR, GL_TRUE);
1941# endif
1942# if defined(VBOX_WITH_WDDM)
1943 pglChromiumParameteriCR(GL_HOST_WND_CREATED_HIDDEN_CR, GL_TRUE);
1944# endif
1945#endif
1946
1947 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
1948
1949 TRACE("Setting up the screen\n");
1950
1951 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1952 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1953
1954 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1955 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1956
1957 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1958 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1959
1960 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
1961 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1962 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
1963 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1964
1965 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1966 {
1967 /* Most textures will use client storage if supported. Exceptions are
1968 * non-native power of 2 textures and textures in DIB sections. */
1969 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1970 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1971 }
1972 if (gl_info->supported[ARB_VERTEX_BLEND])
1973 {
1974 /* Direct3D always uses n-1 weights for n world matrices and uses
1975 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1976 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1977 * enabled as well. */
1978 gl_info->gl_ops.gl.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB);
1979 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1980 }
1981 if (gl_info->supported[NV_TEXTURE_SHADER2])
1982 {
1983 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1984 * the previous texture where to source the offset from is always unit - 1.
1985 */
1986 for (s = 1; s < gl_info->limits.textures; ++s)
1987 {
1988 context_active_texture(ret, gl_info, s);
1989 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
1990 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
1991 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1992 }
1993 }
1994 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
1995 {
1996 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1997 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1998 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1999 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
2000 * is ever assigned.
2001 *
2002 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
2003 * program and the dummy program is destroyed when the context is destroyed.
2004 */
2005 const char *dummy_program =
2006 "!!ARBfp1.0\n"
2007 "MOV result.color, fragment.color.primary;\n"
2008 "END\n";
2009 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
2010 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
2011 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
2012 }
2013
2014 if (gl_info->supported[ARB_POINT_SPRITE])
2015 {
2016 for (s = 0; s < gl_info->limits.textures; ++s)
2017 {
2018 context_active_texture(ret, gl_info, s);
2019 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2020 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2021 }
2022 }
2023
2024 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2025 {
2026 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2027 }
2028 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2029 {
2030 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2031 }
2032 ret->select_shader = 1;
2033
2034 /* If this happens to be the first context for the device, dummy textures
2035 * are not created yet. In that case, they will be created (and bound) by
2036 * create_dummy_textures right after this context is initialized. */
2037 if (device->dummy_texture_2d[0])
2038 bind_dummy_textures(device, ret);
2039
2040 TRACE("Created context %p.\n", ret);
2041
2042 return ret;
2043
2044out:
2045 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
2046 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
2047 HeapFree(GetProcessHeap(), 0, ret->draw_buffers);
2048 HeapFree(GetProcessHeap(), 0, ret->blit_targets);
2049 HeapFree(GetProcessHeap(), 0, ret);
2050 return NULL;
2051}
2052
2053/* Do not call while under the GL lock. */
2054void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
2055{
2056#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2057 BOOL destroy;
2058#endif
2059
2060 TRACE("Destroying ctx %p\n", context);
2061
2062#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2063 if (context->tid == GetCurrentThreadId() || !context->current)
2064 {
2065 context_destroy_gl_resources(context);
2066 TlsSetValue(wined3d_context_tls_idx, NULL);
2067 destroy = TRUE;
2068 }
2069 else
2070 {
2071 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2072 in wined3d_adapter may go away in the meantime */
2073 struct wined3d_gl_info *gl_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info));
2074 *gl_info = *context->gl_info;
2075 context->gl_info = gl_info;
2076 context->destroyed = 1;
2077 destroy = FALSE;
2078 }
2079#else
2080 context_destroy_gl_resources(context);
2081#endif
2082
2083 HeapFree(GetProcessHeap(), 0, context->draw_buffers);
2084 HeapFree(GetProcessHeap(), 0, context->blit_targets);
2085 device_context_remove(device, context);
2086#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2087 if (destroy) HeapFree(GetProcessHeap(), 0, context);
2088#else
2089 context->swapchain = NULL;
2090
2091 VBoxTlsRefMarkDestroy(context);
2092 VBoxTlsRefRelease(context);
2093#endif
2094}
2095
2096/* Context activation is done by the caller. */
2097static void set_blit_dimension(const struct wined3d_gl_info *gl_info, UINT width, UINT height)
2098{
2099 const GLdouble projection[] =
2100 {
2101 2.0 / width, 0.0, 0.0, 0.0,
2102 0.0, 2.0 / height, 0.0, 0.0,
2103 0.0, 0.0, 2.0, 0.0,
2104 -1.0, -1.0, -1.0, 1.0,
2105 };
2106
2107 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2108 checkGLcall("glMatrixMode(GL_PROJECTION)");
2109 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
2110 checkGLcall("glLoadMatrixd");
2111 gl_info->gl_ops.gl.p_glViewport(0, 0, width, height);
2112 checkGLcall("glViewport");
2113}
2114
2115static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
2116{
2117 const struct wined3d_surface *rt = context->current_rt;
2118
2119 if (rt->swapchain && rt->swapchain->front_buffer == rt)
2120 {
2121 RECT window_size;
2122#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
2123 ERR("not tested!");
2124 GetClientRect(rt->swapchain->win_handle, &window_size);
2125#else
2126 GetClientRect(context->win_handle, &window_size);
2127#endif
2128#ifdef DEBUG_misha
2129 Assert(window_size.right - window_size.left == rt->resource.width);
2130 Assert(window_size.bottom - window_size.top == rt->resource.height);
2131#endif
2132 size->cx = window_size.right - window_size.left;
2133 size->cy = window_size.bottom - window_size.top;
2134
2135 return;
2136 }
2137 size->cx = rt->resource.width;
2138 size->cy = rt->resource.height;
2139}
2140
2141/*****************************************************************************
2142 * SetupForBlit
2143 *
2144 * Sets up a context for DirectDraw blitting.
2145 * All texture units are disabled, texture unit 0 is set as current unit
2146 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2147 * color writing enabled for all channels
2148 * register combiners disabled, shaders disabled
2149 * world matrix is set to identity, texture matrix 0 too
2150 * projection matrix is setup for drawing screen coordinates
2151 *
2152 * Params:
2153 * This: Device to activate the context for
2154 * context: Context to setup
2155 *
2156 *****************************************************************************/
2157/* Context activation is done by the caller. */
2158static void SetupForBlit(const struct wined3d_device *device, struct wined3d_context *context)
2159{
2160 int i;
2161 const struct wined3d_gl_info *gl_info = context->gl_info;
2162 DWORD sampler;
2163 SIZE rt_size;
2164
2165 TRACE("Setting up context %p for blitting\n", context);
2166
2167 context_get_rt_size(context, &rt_size);
2168
2169 if (context->last_was_blit)
2170 {
2171 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
2172 {
2173 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2174 context->blit_w = rt_size.cx;
2175 context->blit_h = rt_size.cy;
2176 /* No need to dirtify here, the states are still dirtified because
2177 * they weren't applied since the last SetupForBlit() call. */
2178 }
2179 TRACE("Context is already set up for blitting, nothing to do\n");
2180 return;
2181 }
2182 context->last_was_blit = TRUE;
2183
2184 /* Disable all textures. The caller can then bind a texture it wants to blit
2185 * from
2186 *
2187 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2188 * function texture unit. No need to care for higher samplers
2189 */
2190 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
2191 {
2192 sampler = device->rev_tex_unit_map[i];
2193 context_active_texture(context, gl_info, i);
2194
2195 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2196 {
2197 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2198 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2199 }
2200 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2201 checkGLcall("glDisable GL_TEXTURE_3D");
2202 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2203 {
2204 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2205 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2206 }
2207 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2208 checkGLcall("glDisable GL_TEXTURE_2D");
2209
2210 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2211 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2212
2213 if (sampler != WINED3D_UNMAPPED_STAGE)
2214 {
2215 if (sampler < MAX_TEXTURES)
2216 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2217 context_invalidate_state(context, STATE_SAMPLER(sampler));
2218 }
2219 }
2220 context_active_texture(context, gl_info, 0);
2221
2222 sampler = device->rev_tex_unit_map[0];
2223
2224 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2225 {
2226 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2227 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2228 }
2229 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2230 checkGLcall("glDisable GL_TEXTURE_3D");
2231 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2232 {
2233 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2234 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2235 }
2236 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2237 checkGLcall("glDisable GL_TEXTURE_2D");
2238
2239 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2240
2241 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
2242 checkGLcall("glMatrixMode(GL_TEXTURE)");
2243 gl_info->gl_ops.gl.p_glLoadIdentity();
2244 checkGLcall("glLoadIdentity()");
2245
2246 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
2247 {
2248 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
2249 GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
2250 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2251 }
2252
2253 if (sampler != WINED3D_UNMAPPED_STAGE)
2254 {
2255 if (sampler < MAX_TEXTURES)
2256 {
2257 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2258 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2259 }
2260 context_invalidate_state(context, STATE_SAMPLER(sampler));
2261 }
2262
2263 /* Other misc states */
2264 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2265 checkGLcall("glDisable(GL_ALPHA_TEST)");
2266 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2267 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2268 checkGLcall("glDisable GL_LIGHTING");
2269 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
2270 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2271 checkGLcall("glDisable GL_DEPTH_TEST");
2272 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
2273 glDisableWINE(GL_FOG);
2274 checkGLcall("glDisable GL_FOG");
2275 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
2276 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2277 checkGLcall("glDisable GL_BLEND");
2278 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2279 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2280 checkGLcall("glDisable GL_CULL_FACE");
2281 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
2282 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2283 checkGLcall("glDisable GL_STENCIL_TEST");
2284 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
2285 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2286 checkGLcall("glDisable GL_SCISSOR_TEST");
2287 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2288 if (gl_info->supported[ARB_POINT_SPRITE])
2289 {
2290 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2291 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2292 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2293 }
2294 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
2295 checkGLcall("glColorMask");
2296 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
2297 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
2298 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
2299 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
2300 if (gl_info->supported[EXT_SECONDARY_COLOR])
2301 {
2302 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
2303 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
2304 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2305 }
2306
2307 /* Setup transforms */
2308 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2309 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2310 gl_info->gl_ops.gl.p_glLoadIdentity();
2311 checkGLcall("glLoadIdentity()");
2312 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2313
2314 context->last_was_rhw = TRUE;
2315 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2316
2317 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
2318 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
2319 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
2320 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
2321 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
2322 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
2323 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2324
2325 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2326
2327 /* Disable shaders */
2328 device->shader_backend->shader_disable(device->shader_priv, context);
2329 context->select_shader = 1;
2330 context->load_constants = 1;
2331
2332 context->blit_w = rt_size.cx;
2333 context->blit_h = rt_size.cy;
2334 context_invalidate_state(context, STATE_VIEWPORT);
2335 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2336}
2337
2338static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2339{
2340 return rt_mask & (1 << 31);
2341}
2342
2343static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2344{
2345 return rt_mask & ~(1 << 31);
2346}
2347
2348/* Context activation is done by the caller. */
2349static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
2350{
2351 const struct wined3d_gl_info *gl_info = context->gl_info;
2352
2353 if (!rt_mask)
2354 {
2355 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2356 checkGLcall("glDrawBuffer()");
2357 }
2358 else if (is_rt_mask_onscreen(rt_mask))
2359 {
2360 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2361 checkGLcall("glDrawBuffer()");
2362 }
2363 else
2364 {
2365 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2366 {
2367 unsigned int i = 0;
2368
2369 while (rt_mask)
2370 {
2371 if (rt_mask & 1)
2372 context->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2373 else
2374 context->draw_buffers[i] = GL_NONE;
2375
2376 rt_mask >>= 1;
2377 ++i;
2378 }
2379
2380 if (gl_info->supported[ARB_DRAW_BUFFERS])
2381 {
2382 GL_EXTCALL(glDrawBuffersARB(i, context->draw_buffers));
2383 checkGLcall("glDrawBuffers()");
2384 }
2385 else
2386 {
2387 gl_info->gl_ops.gl.p_glDrawBuffer(context->draw_buffers[0]);
2388 checkGLcall("glDrawBuffer()");
2389 }
2390 }
2391 else
2392 {
2393 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2394 }
2395 }
2396}
2397
2398/* Context activation is done by the caller. */
2399void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2400{
2401 const struct wined3d_gl_info *gl_info = context->gl_info;
2402 DWORD *current_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2403 DWORD new_mask = context_generate_rt_mask(buffer);
2404
2405 if (new_mask == *current_mask)
2406 return;
2407
2408 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2409 checkGLcall("glDrawBuffer()");
2410
2411 *current_mask = new_mask;
2412}
2413
2414/* Context activation is done by the caller. */
2415void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
2416{
2417 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0 + unit));
2418 checkGLcall("glActiveTextureARB");
2419 context->active_texture = unit;
2420}
2421
2422void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
2423{
2424 const struct wined3d_gl_info *gl_info = context->gl_info;
2425 DWORD unit = context->active_texture;
2426 DWORD old_texture_type = context->texture_type[unit];
2427
2428 if (name)
2429 {
2430 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2431 checkGLcall("glBindTexture");
2432 }
2433 else
2434 {
2435 target = GL_NONE;
2436 }
2437
2438 if (old_texture_type != target)
2439 {
2440 const struct wined3d_device *device = context->swapchain->device;
2441
2442 switch (old_texture_type)
2443 {
2444 case GL_NONE:
2445 /* nothing to do */
2446 break;
2447 case GL_TEXTURE_2D:
2448 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[unit]);
2449 checkGLcall("glBindTexture");
2450 break;
2451 case GL_TEXTURE_RECTANGLE_ARB:
2452 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[unit]);
2453 checkGLcall("glBindTexture");
2454 break;
2455 case GL_TEXTURE_CUBE_MAP:
2456 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[unit]);
2457 checkGLcall("glBindTexture");
2458 break;
2459 case GL_TEXTURE_3D:
2460 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[unit]);
2461 checkGLcall("glBindTexture");
2462 break;
2463 default:
2464 ERR("Unexpected texture target %#x\n", old_texture_type);
2465 }
2466
2467 context->texture_type[unit] = target;
2468 }
2469}
2470
2471static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2472{
2473 if (context->render_offscreen == offscreen) return;
2474
2475 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2476 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2477 context_invalidate_state(context, STATE_VIEWPORT);
2478 context_invalidate_state(context, STATE_SCISSORRECT);
2479 context_invalidate_state(context, STATE_FRONTFACE);
2480 context->render_offscreen = offscreen;
2481}
2482
2483static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2484 const struct wined3d_format *required)
2485{
2486 BYTE existing_depth, existing_stencil, required_depth, required_stencil;
2487
2488 if (existing == required) return TRUE;
2489 if ((existing->flags & WINED3DFMT_FLAG_FLOAT) != (required->flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2490
2491 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2492 getDepthStencilBits(required, &required_depth, &required_stencil);
2493
2494 if(existing_depth < required_depth) return FALSE;
2495 /* If stencil bits are used the exact amount is required - otherwise wrapping
2496 * won't work correctly */
2497 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2498 return TRUE;
2499}
2500
2501/* The caller provides a context */
2502static void context_validate_onscreen_formats(struct wined3d_context *context,
2503 const struct wined3d_surface *depth_stencil)
2504{
2505 /* Onscreen surfaces are always in a swapchain */
2506 struct wined3d_swapchain *swapchain = context->current_rt->swapchain;
2507
2508 if (context->render_offscreen || !depth_stencil) return;
2509 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format)) return;
2510
2511 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2512 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2513 * format. */
2514 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2515
2516 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2517 surface_load_location(context->current_rt, SFLAG_INTEXTURE, NULL);
2518 swapchain->render_to_fbo = TRUE;
2519 swapchain_update_draw_bindings(swapchain);
2520 context_set_render_offscreen(context, TRUE);
2521}
2522
2523static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_device *device, const struct wined3d_surface *rt)
2524{
2525 if (!rt || rt->resource.format->id == WINED3DFMT_NULL)
2526 return 0;
2527 else if (rt->swapchain)
2528 return context_generate_rt_mask_from_surface(rt);
2529 else
2530 return context_generate_rt_mask(device->offscreenBuffer);
2531}
2532
2533/* Context activation is done by the caller. */
2534void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2535{
2536 struct wined3d_surface *rt = context->current_rt;
2537 DWORD rt_mask, *cur_mask;
2538
2539 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2540 {
2541 context_validate_onscreen_formats(context, NULL);
2542
2543 if (context->render_offscreen)
2544 {
2545 surface_internal_preload(rt, SRGB_RGB);
2546
2547 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, rt, NULL, rt->draw_binding);
2548 if (rt->resource.format->id != WINED3DFMT_NULL)
2549 rt_mask = 1;
2550 else
2551 rt_mask = 0;
2552 }
2553 else
2554 {
2555 context->current_fbo = NULL;
2556 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
2557 rt_mask = context_generate_rt_mask_from_surface(rt);
2558 }
2559 }
2560 else
2561 {
2562 rt_mask = context_generate_rt_mask_no_fbo(device, rt);
2563 }
2564
2565 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2566
2567 if (rt_mask != *cur_mask)
2568 {
2569 context_apply_draw_buffers(context, rt_mask);
2570 *cur_mask = rt_mask;
2571 }
2572
2573 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2574 {
2575 context_check_fbo_status(context, GL_FRAMEBUFFER);
2576 }
2577
2578 SetupForBlit(device, context);
2579 context_invalidate_state(context, STATE_FRAMEBUFFER);
2580}
2581
2582static BOOL context_validate_rt_config(UINT rt_count,
2583 struct wined3d_surface * const *rts, const struct wined3d_surface *ds)
2584{
2585 unsigned int i;
2586
2587 if (ds) return TRUE;
2588
2589 for (i = 0; i < rt_count; ++i)
2590 {
2591 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2592 return TRUE;
2593 }
2594
2595 WARN("Invalid render target config, need at least one attachment.\n");
2596 return FALSE;
2597}
2598
2599/* Context activation is done by the caller. */
2600BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_device *device,
2601 UINT rt_count, const struct wined3d_fb_state *fb)
2602{
2603 const struct wined3d_gl_info *gl_info = context->gl_info;
2604 DWORD rt_mask = 0, *cur_mask;
2605 UINT i;
2606 struct wined3d_surface **rts = fb->render_targets;
2607
2608 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != &device->fb
2609 || rt_count != context->gl_info->limits.buffers)
2610 {
2611 if (!context_validate_rt_config(rt_count, rts, fb->depth_stencil))
2612 return FALSE;
2613
2614 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2615 {
2616 context_validate_onscreen_formats(context, fb->depth_stencil);
2617
2618 if (!rt_count || surface_is_offscreen(rts[0]))
2619 {
2620 for (i = 0; i < rt_count; ++i)
2621 {
2622 context->blit_targets[i] = rts[i];
2623 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2624 rt_mask |= (1 << i);
2625 }
2626 while (i < context->gl_info->limits.buffers)
2627 {
2628 context->blit_targets[i] = NULL;
2629 ++i;
2630 }
2631 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets, fb->depth_stencil,
2632 rt_count ? rts[0]->draw_binding : SFLAG_INTEXTURE);
2633 }
2634 else
2635 {
2636 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2637 rt_mask = context_generate_rt_mask_from_surface(rts[0]);
2638 }
2639
2640 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2641 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2642 * state management allows this */
2643 context_invalidate_state(context, STATE_FRAMEBUFFER);
2644 }
2645 else
2646 {
2647 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2648 }
2649 }
2650 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2651 && (!rt_count || surface_is_offscreen(rts[0])))
2652 {
2653 for (i = 0; i < rt_count; ++i)
2654 {
2655 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL) rt_mask |= (1 << i);
2656 }
2657 }
2658 else
2659 {
2660 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2661 }
2662
2663 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2664
2665 if (rt_mask != *cur_mask)
2666 {
2667 context_apply_draw_buffers(context, rt_mask);
2668 *cur_mask = rt_mask;
2669 context_invalidate_state(context, STATE_FRAMEBUFFER);
2670 }
2671
2672 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2673 {
2674 context_check_fbo_status(context, GL_FRAMEBUFFER);
2675 }
2676
2677 if (context->last_was_blit)
2678 context->last_was_blit = FALSE;
2679
2680 /* Blending and clearing should be orthogonal, but tests on the nvidia
2681 * driver show that disabling blending when clearing improves the clearing
2682 * performance incredibly. */
2683 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2684 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
2685 checkGLcall("glEnable GL_SCISSOR_TEST");
2686
2687 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2688 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2689 context_invalidate_state(context, STATE_SCISSORRECT);
2690
2691 return TRUE;
2692}
2693
2694static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_device *device)
2695{
2696 const struct wined3d_state *state = &device->stateBlock->state;
2697 struct wined3d_surface **rts = state->fb->render_targets;
2698 struct wined3d_shader *ps = state->pixel_shader;
2699 DWORD rt_mask, rt_mask_bits;
2700 unsigned int i;
2701
2702 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return context_generate_rt_mask_no_fbo(device, rts[0]);
2703 else if (!context->render_offscreen) return context_generate_rt_mask_from_surface(rts[0]);
2704
2705 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
2706 rt_mask &= context->d3d_info->valid_rt_mask;
2707 rt_mask_bits = rt_mask;
2708 i = 0;
2709 while (rt_mask_bits)
2710 {
2711 rt_mask_bits &= ~(1 << i);
2712 if (!rts[i] || rts[i]->resource.format->id == WINED3DFMT_NULL)
2713 rt_mask &= ~(1 << i);
2714
2715 i++;
2716 }
2717
2718 return rt_mask;
2719}
2720
2721/* Context activation is done by the caller. */
2722void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2723{
2724 const struct wined3d_device *device = context->swapchain->device;
2725 const struct wined3d_fb_state *fb = state->fb;
2726 DWORD rt_mask = find_draw_buffers_mask(context, device);
2727 DWORD *cur_mask;
2728
2729 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2730 {
2731 if (!context->render_offscreen)
2732 {
2733 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2734 }
2735 else
2736 {
2737 context_apply_fbo_state(context, GL_FRAMEBUFFER, fb->render_targets, fb->depth_stencil,
2738 fb->render_targets[0]->draw_binding);
2739 }
2740 }
2741
2742 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2743 if (rt_mask != *cur_mask)
2744 {
2745 context_apply_draw_buffers(context, rt_mask);
2746 *cur_mask = rt_mask;
2747 }
2748}
2749
2750/* Context activation is done by the caller. */
2751void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2752{
2753 const struct wined3d_device *device = context->swapchain->device;
2754 DWORD rt_mask, *cur_mask;
2755
2756 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
2757
2758 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2759 rt_mask = find_draw_buffers_mask(context, device);
2760 if (rt_mask != *cur_mask)
2761 {
2762 context_apply_draw_buffers(context, rt_mask);
2763 *cur_mask = rt_mask;
2764 }
2765}
2766
2767/* Context activation is done by the caller. */
2768BOOL context_apply_draw_state(struct wined3d_context *context, struct wined3d_device *device)
2769{
2770 const struct wined3d_state *state = &device->stateBlock->state;
2771 const struct StateEntry *state_table = context->state_table;
2772 const struct wined3d_fb_state *fb = state->fb;
2773 unsigned int i;
2774
2775 if (!context_validate_rt_config(context->gl_info->limits.buffers,
2776 fb->render_targets, fb->depth_stencil))
2777 return FALSE;
2778
2779 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
2780 {
2781 context_validate_onscreen_formats(context, fb->depth_stencil);
2782 }
2783
2784 /* Preload resources before FBO setup. Texture preload in particular may
2785 * result in changes to the current FBO, due to using e.g. FBO blits for
2786 * updating a resource location. */
2787 device_update_tex_unit_map(device);
2788 device_preload_textures(device);
2789 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC))
2790 device_update_stream_info(device, context->gl_info);
2791 if (state->index_buffer)
2792 {
2793 if (device->stream_info.all_vbo)
2794 wined3d_buffer_preload(state->index_buffer);
2795 else
2796 buffer_get_sysmem(state->index_buffer, context->gl_info);
2797 }
2798
2799 for (i = 0; i < context->numDirtyEntries; ++i)
2800 {
2801 DWORD rep = context->dirtyArray[i];
2802 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2803 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2804 context->isStateDirty[idx] &= ~(1 << shift);
2805 state_table[rep].apply(context, state, rep);
2806 }
2807
2808 if (context->select_shader)
2809 {
2810 device->shader_backend->shader_select(device->shader_priv, context, state);
2811 context->select_shader = 0;
2812 }
2813
2814 if (context->load_constants)
2815 {
2816 device->shader_backend->shader_load_constants(device->shader_priv,
2817 context, state);
2818 context->load_constants = 0;
2819 }
2820
2821 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2822 {
2823 context_check_fbo_status(context, GL_FRAMEBUFFER);
2824 }
2825
2826 context->numDirtyEntries = 0; /* This makes the whole list clean */
2827 context->last_was_blit = FALSE;
2828
2829 return TRUE;
2830}
2831
2832static void context_setup_target(struct wined3d_context *context, struct wined3d_surface *target)
2833{
2834 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2835
2836 render_offscreen = surface_is_offscreen(target);
2837 if (context->current_rt == target && render_offscreen == old_render_offscreen) return;
2838
2839 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2840 * the alpha blend state changes with different render target formats. */
2841 if (!context->current_rt)
2842 {
2843 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2844 }
2845 else
2846 {
2847 const struct wined3d_format *old = context->current_rt->resource.format;
2848 const struct wined3d_format *new = target->resource.format;
2849
2850 if (old->id != new->id)
2851 {
2852 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2853 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
2854 || !(new->flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2855 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2856
2857 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2858 if ((old->flags & WINED3DFMT_FLAG_SRGB_WRITE) != (new->flags & WINED3DFMT_FLAG_SRGB_WRITE))
2859 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
2860 }
2861
2862 /* When switching away from an offscreen render target, and we're not
2863 * using FBOs, we have to read the drawable into the texture. This is
2864 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2865 * are some things that need care though. PreLoad needs a GL context,
2866 * and FindContext is called before the context is activated. It also
2867 * has to be called with the old rendertarget active, otherwise a
2868 * wrong drawable is read. */
2869 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2870 && old_render_offscreen && context->current_rt != target)
2871 {
2872 /* Read the back buffer of the old drawable into the destination texture. */
2873 if (context->current_rt->texture_name_srgb)
2874 surface_internal_preload(context->current_rt, SRGB_SRGB);
2875 surface_internal_preload(context->current_rt, SRGB_RGB);
2876 surface_modify_location(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2877 }
2878 }
2879
2880 context->current_rt = target;
2881 context_set_render_offscreen(context, render_offscreen);
2882}
2883
2884/* Do not call while under the GL lock. */
2885struct wined3d_context *context_acquire(const struct wined3d_device *device, struct wined3d_surface *target)
2886{
2887 struct wined3d_context *current_context = context_get_current();
2888 struct wined3d_context *context;
2889#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
2890 struct wined3d_swapchain *swapchain = NULL;
2891#endif
2892
2893 TRACE("device %p, target %p.\n", device, target);
2894
2895#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2896 if (current_context && current_context->destroyed)
2897 current_context = NULL;
2898#endif
2899
2900 if (!target)
2901 {
2902 if (current_context
2903 && current_context->current_rt
2904#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2905 && current_context->swapchain->device == device
2906#else
2907 && current_context->device == device
2908#endif
2909 )
2910 {
2911 target = current_context->current_rt;
2912 }
2913 else
2914 {
2915#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2916 struct wined3d_swapchain *swapchain = device->swapchains[0];
2917#else
2918 swapchain = swapchain_find_valid_or_any(device);
2919 if (!swapchain)
2920 {
2921 ERR("no valid swapchain found!");
2922 swapchain = device->swapchains[0];
2923 Assert(swapchain);
2924 }
2925#endif
2926 if (swapchain->back_buffers)
2927 target = swapchain->back_buffers[0];
2928 else
2929 target = swapchain->front_buffer;
2930 }
2931 }
2932
2933#ifdef VBOX_WITH_WINE_DBG
2934 Assert(target);
2935#endif
2936
2937 if (current_context && current_context->current_rt == target)
2938 {
2939 context = current_context;
2940 }
2941 else if (target->swapchain)
2942 {
2943 TRACE("Rendering onscreen.\n");
2944
2945 context = swapchain_get_context(target->swapchain);
2946#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
2947 swapchain = target->swapchain;
2948 Assert(swapchain);
2949#endif
2950 }
2951 else
2952 {
2953 TRACE("Rendering offscreen.\n");
2954
2955#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2956 /* Stay with the current context if possible. Otherwise use the
2957 * context for the primary swapchain. */
2958 if (current_context && current_context->swapchain->device == device)
2959 context = current_context;
2960 else
2961#endif
2962 context = swapchain_get_context(device->swapchains[0]);
2963 }
2964
2965#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2966 context_update_window(context);
2967#else
2968 if (!swapchain)
2969 swapchain = swapchain_find_valid_or_any(device);
2970 context_update_window(context, swapchain);
2971#endif
2972 context_setup_target(context, target);
2973 context_enter(context);
2974 if (!context->valid) return context;
2975
2976 if (context != current_context)
2977 {
2978 if (!context_set_current(context))
2979 ERR("Failed to activate the new context.\n");
2980 }
2981 else if (context->restore_ctx)
2982 {
2983 context_set_gl_context(context);
2984 }
2985
2986 return context;
2987}
Note: See TracBrowser for help on using the repository browser.

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