VirtualBox

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

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

crOpenGL: bugfixes and work-arounds for wine issues

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 101.7 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
845 ctx->valid = 1;
846#else
847 return FALSE;
848#endif
849 }
850 return TRUE;
851}
852
853static void context_restore_gl_context(const struct wined3d_gl_info *gl_info, HDC dc, HGLRC gl_ctx, int pf)
854{
855#ifndef VBOX
856 if (!context_set_pixel_format(gl_info, dc, pf))
857 {
858 ERR("Failed to restore pixel format %d on device context %p.\n", pf, dc);
859 context_set_current(NULL);
860 return;
861 }
862#endif
863 if (!wglMakeCurrent(dc, gl_ctx))
864 {
865 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
866 gl_ctx, dc, GetLastError());
867 context_set_current(NULL);
868 }
869#ifdef VBOX
870 else
871 {
872 /* success branch */
873 /* sync back our tls with gl settings */
874 const struct wined3d_context *current_context = context_get_current();
875 if (current_context && current_context->glCtx != gl_ctx)
876 {
877 UINT i = 0;
878 struct wined3d_device *device = current_context->swapchain->device;
879 for (; i < device->context_count; ++i)
880 {
881 struct wined3d_context *ctx = device->contexts[i];
882 if (ctx->glCtx == gl_ctx)
883 {
884 context_set_current(ctx);
885 break;
886 }
887 }
888
889 if (i == device->context_count)
890 {
891 context_set_current(NULL);
892 }
893 }
894 }
895#endif
896}
897
898#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
899static BOOL swapchain_validate(const struct wined3d_swapchain *swapchain)
900{
901 if (!swapchain->hDC)
902 {
903 ERR("NULL hDC");
904 return FALSE;
905 }
906
907#if defined(DEBUG) || !defined(VBOX_WITH_WDDM)
908 {
909 HWND hWnd = WindowFromDC(swapchain->hDC);
910 if (hWnd != swapchain->win_handle)
911 {
912# if defined(VBOX_WITH_WDDM)
913 ERR("Unexpected swapchain for dc %p window expected %p, but was %p.\n", swapchain->hDC, swapchain->win_handle, hWnd);
914# else
915 ERR("Swapchain for dc %p window expected %p, but was %p.\n", swapchain->hDC, swapchain->win_handle, hWnd);
916 return FALSE;
917# endif
918 }
919 }
920#endif
921 return TRUE;
922}
923
924static struct wined3d_swapchain* swapchain_find_valid(const struct wined3d_device *device)
925{
926 int i;
927 struct wined3d_context * context = device->contexts[0];
928 struct wined3d_swapchain* swapchain = NULL;
929 if (context)
930 {
931 swapchain = context->swapchain;
932 if (swapchain && swapchain_validate(swapchain))
933 {
934 return swapchain;
935 }
936 }
937 for (i = device->swapchain_count - 1; i >= 0 ; --i)
938 {
939 Assert(device->swapchains[i]);
940 if (swapchain != device->swapchains[i] /* the current context swapchain is known to be invalid */
941 && swapchain_validate(device->swapchains[i]))
942 {
943 return device->swapchains[i];
944 }
945 }
946
947 return NULL;
948}
949
950static struct wined3d_swapchain* swapchain_find_valid_or_any(const struct wined3d_device *device)
951{
952 struct wined3d_swapchain* swapchain = swapchain_find_valid(device);
953 if (swapchain)
954 return swapchain;
955
956 ERR("no valid swapchain found!");
957 swapchain = device->swapchains[0];
958 Assert(swapchain);
959 return swapchain;
960}
961
962struct wined3d_context *context_find_create(struct wined3d_device *device,
963 struct wined3d_swapchain *swapchain,
964 const struct wined3d_format *ds_format)
965{
966 struct wined3d_context *context;
967 struct wined3d_surface *target = swapchain->back_buffers ? swapchain->back_buffers[0] : swapchain->front_buffer;
968 if (!device->context_count)
969 {
970 Assert(!device->swapchain_count);
971 context = context_create(swapchain, target, ds_format
972#ifdef VBOX_WITH_WDDM
973 , device->pHgsmi
974#endif
975 );
976 if(!context)
977 {
978 ERR("Failed to create the context.\n");
979 }
980 else
981 {
982 Assert(context->valid);
983 }
984 }
985 else
986 {
987 context = context_acquire(device, target);
988 if(!context)
989 {
990 ERR("Failed to acquire the context.\n");
991 }
992 else
993 {
994 Assert(context->valid);
995 }
996 }
997
998 return context;
999}
1000
1001#endif
1002
1003static void context_update_window(struct wined3d_context *context
1004#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1005 , struct wined3d_swapchain *swapchain
1006#endif
1007 )
1008{
1009#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1010 TRACE("Updating context %p swapchain from %p to %p.\n",
1011 context, context->swapchain, swapchain);
1012
1013 if (context->swapchain == swapchain)
1014 return;
1015
1016 if (!swapchain_validate(swapchain))
1017 {
1018 ERR("invalid swapchain %p\n", swapchain);
1019 goto err;
1020 }
1021 context->swapchain = swapchain;
1022
1023 context->valid = 1;
1024
1025 context_set_gl_context(context);
1026#else
1027 if (context->win_handle == context->swapchain->win_handle)
1028 return;
1029
1030 TRACE("Updating context %p window from %p to %p.\n",
1031 context, context->win_handle, context->swapchain->win_handle);
1032
1033 if (context->valid)
1034 wined3d_release_dc(context->win_handle, context->hdc);
1035 else
1036 context->valid = 1;
1037
1038 context->win_handle = context->swapchain->win_handle;
1039
1040#ifndef VBOX
1041 if (!(context->hdc = GetDC(context->win_handle)))
1042#else
1043 if (!(context->hdc = VBoxExtGetDC(context->win_handle)))
1044#endif
1045 {
1046 ERR("Failed to get a device context for window %p.\n", context->win_handle);
1047 goto err;
1048 }
1049#ifndef VBOX
1050 if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
1051 {
1052 ERR("Failed to set pixel format %d on device context %p.\n",
1053 context->pixel_format, context->hdc);
1054 goto err;
1055 }
1056#endif
1057 context_set_gl_context(context);
1058#endif
1059 return;
1060
1061err:
1062 context->valid = 0;
1063}
1064
1065#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1066static void context_adjust_any_valid(struct wined3d_context *context)
1067{
1068 struct wined3d_swapchain* swapchain = context->swapchain;
1069
1070 if (context->valid && swapchain && swapchain_validate(swapchain))
1071 return;
1072
1073 swapchain = swapchain_find_valid(context->device);
1074 if (!swapchain)
1075 {
1076 ERR("no valid swapchain found");
1077 Assert(!context->valid);
1078 return;
1079 }
1080
1081 context_update_window(context, swapchain);
1082}
1083#endif
1084
1085/* Do not call while under the GL lock. */
1086static void context_destroy_gl_resources(struct wined3d_context *context)
1087{
1088 const struct wined3d_gl_info *gl_info = context->gl_info;
1089 struct wined3d_occlusion_query *occlusion_query;
1090 struct wined3d_event_query *event_query;
1091 struct fbo_entry *entry, *entry2;
1092 HGLRC restore_ctx;
1093 HDC restore_dc;
1094 unsigned int i;
1095 int restore_pf;
1096
1097 restore_ctx = wglGetCurrentContext();
1098 restore_dc = wglGetCurrentDC();
1099 restore_pf = GetPixelFormat(restore_dc);
1100
1101#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1102 if (context->valid && restore_ctx != context->glCtx)
1103 context_set_gl_context(context);
1104#else
1105 if (restore_ctx != context->glCtx)
1106 context_adjust_any_valid(context);
1107#endif
1108 else
1109 restore_ctx = NULL;
1110
1111
1112 LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
1113 {
1114 if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
1115 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
1116 occlusion_query->context = NULL;
1117 }
1118
1119 LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
1120 {
1121 if (context->valid)
1122 {
1123 if (gl_info->supported[ARB_SYNC])
1124 {
1125 if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
1126 }
1127 else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
1128 else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
1129 }
1130 event_query->context = NULL;
1131 }
1132
1133 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
1134 {
1135 if (!context->valid) entry->id = 0;
1136 context_destroy_fbo_entry(context, entry);
1137 }
1138
1139 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
1140 {
1141 if (!context->valid) entry->id = 0;
1142 context_destroy_fbo_entry(context, entry);
1143 }
1144
1145 if (context->valid)
1146 {
1147 if (context->dummy_arbfp_prog)
1148 {
1149 GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
1150 }
1151
1152 if (gl_info->supported[ARB_OCCLUSION_QUERY])
1153 GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
1154
1155 if (gl_info->supported[ARB_SYNC])
1156 {
1157 for (i = 0; i < context->free_event_query_count; ++i)
1158 {
1159 GL_EXTCALL(glDeleteSync(context->free_event_queries[i].sync));
1160 }
1161 }
1162 else if (gl_info->supported[APPLE_FENCE])
1163 {
1164 for (i = 0; i < context->free_event_query_count; ++i)
1165 {
1166 GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
1167 }
1168 }
1169 else if (gl_info->supported[NV_FENCE])
1170 {
1171 for (i = 0; i < context->free_event_query_count; ++i)
1172 {
1173 GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
1174 }
1175 }
1176
1177 checkGLcall("context cleanup");
1178 }
1179
1180 HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
1181 HeapFree(GetProcessHeap(), 0, context->free_event_queries);
1182
1183 if (restore_ctx)
1184 {
1185 context_restore_gl_context(gl_info, restore_dc, restore_ctx, restore_pf);
1186 }
1187 else if (wglGetCurrentContext() && !wglMakeCurrent(NULL, NULL))
1188 {
1189 ERR("Failed to disable GL context.\n");
1190 }
1191
1192#ifndef VBOX_WITH_WDDM
1193# ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1194# ifndef VBOX
1195 wined3d_release_dc(context->win_handle, context->hdc);
1196# else
1197 VBoxExtReleaseDC(context->win_handle, context->hdc);
1198# endif
1199# endif
1200#endif
1201
1202 if (!wglDeleteContext(context->glCtx))
1203 {
1204 DWORD err = GetLastError();
1205 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
1206 }
1207}
1208
1209DWORD context_get_tls_idx(void)
1210{
1211 return wined3d_context_tls_idx;
1212}
1213
1214void context_set_tls_idx(DWORD idx)
1215{
1216 wined3d_context_tls_idx = idx;
1217}
1218
1219#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1220static struct wined3d_context *context_get_current_ex(DWORD adjustTid)
1221{
1222 struct wined3d_context *ctx = vboxGetCurrentContext();
1223 if (ctx && !VBoxTlsRefIsFunctional(ctx))
1224 {
1225 /* this is a destroyed context left in the tls of the current thread */
1226 /* 1. this releases the context and clears the tls */
1227 vboxSetCurrentContext(NULL);
1228 /* return there is no context current */
1229 return NULL;
1230 }
1231 if (!adjustTid)
1232 return ctx;
1233 if (!ctx || ctx->tid == adjustTid)
1234 return ctx;
1235 /* the context may have cleared swapchain (if swapchain was destroyed), ensure it has some valid swapchain set */
1236 if (context_set_current(ctx))
1237 {
1238 Assert(ctx->tid == adjustTid);
1239 return ctx;
1240 }
1241 ERR("context_set_current failed\n");
1242 return NULL;
1243}
1244#endif
1245
1246struct wined3d_context *context_get_current(void)
1247{
1248#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1249 return TlsGetValue(wined3d_context_tls_idx);
1250#else
1251 DWORD tid = GetCurrentThreadId();
1252 return context_get_current_ex(tid);
1253#endif
1254}
1255
1256/* Do not call while under the GL lock. */
1257BOOL context_set_current(struct wined3d_context *ctx)
1258{
1259#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1260 struct wined3d_context *old = context_get_current_ex(0);
1261 DWORD tid = GetCurrentThreadId();
1262#else
1263 struct wined3d_context *old = context_get_current();
1264#endif
1265
1266 if (old == ctx)
1267 {
1268#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1269 if (ctx && ctx->tid != tid)
1270 {
1271 old = NULL;
1272 }
1273 else
1274#endif
1275 {
1276 TRACE("Already using D3D context %p.\n", ctx);
1277 return TRUE;
1278 }
1279 }
1280
1281 if (old)
1282 {
1283#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1284 old->tid = 0;
1285 old->current = 0;
1286#else
1287 if (old->destroyed)
1288 {
1289 TRACE("Switching away from destroyed context %p.\n", old);
1290 context_destroy_gl_resources(old);
1291 HeapFree(GetProcessHeap(), 0, (void *)old->gl_info);
1292 HeapFree(GetProcessHeap(), 0, old);
1293 }
1294 else
1295 {
1296 old->current = 0;
1297 }
1298#endif
1299 }
1300
1301 if (ctx)
1302 {
1303 if (!ctx->valid)
1304 {
1305 ERR("Trying to make invalid context %p current\n", ctx);
1306 return FALSE;
1307 }
1308
1309#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
1310 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
1311#else
1312 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->swapchain->hDC);
1313#endif
1314 if (!context_set_gl_context(ctx))
1315 return FALSE;
1316 ctx->current = 1;
1317 }
1318 else if(wglGetCurrentContext())
1319 {
1320 TRACE("Clearing current D3D context.\n");
1321 if (!wglMakeCurrent(NULL, NULL))
1322 {
1323 DWORD err = GetLastError();
1324 ERR("Failed to clear current GL context, last error %#x.\n", err);
1325#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1326 vboxSetCurrentContext(NULL);
1327#else
1328 TlsSetValue(wined3d_context_tls_idx, NULL);
1329#endif
1330 return FALSE;
1331 }
1332 }
1333#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1334 vboxSetCurrentContext(ctx);
1335 if (ctx)
1336 ctx->tid = tid;
1337 return TRUE;
1338#else
1339 return TlsSetValue(wined3d_context_tls_idx, ctx);
1340#endif
1341}
1342
1343#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1344void context_clear_on_thread_detach()
1345{
1346 /* In theory, we should do context_set_current(NULL) here,
1347 * but since it may result in calling a context dtor, it should be done under wined3d lock.
1348 * We can not acquire a wined3d lock here since this routine is called in a DllMain context
1349 * and this would result in a lock order violation, which may result in a deadlock.
1350 * In other words, wined3d may internally call Win32 API functions which result in
1351 * a DLL lock acquisition while holding wined3d lock.
1352 * So lock order should always be "wined3d lock" -> "dll lock".
1353 *
1354 * This is why we do the following:
1355 * */
1356
1357 /* 1. get the current context w/o adjusting its thread id, etc. */
1358 struct wined3d_context *old = context_get_current_ex(0);
1359 if (!old)
1360 return;
1361
1362// /* there is a currently assigned context,
1363// * 2. now increase its ref count to ensure its dtor routine is not called while making set_current(NULL).
1364// * This is needed since dtor can only be run with a wined3d lock held */
1365// VBoxTlsRefAddRef(old);
1366
1367 /* context_tls_dtor now does only memfree, so just call it right away */
1368
1369 /* 3. now we can call context_set_current(NULL) */
1370 context_set_current(NULL);
1371
1372// /* 4. to avoid possible deadlocks we make an asynchronous call to a worker thread to make
1373// * wined3d lock - context release - wined3d unlock from there. */
1374// VBoxExtReleaseContextAsync(old);
1375}
1376#endif
1377
1378void context_release(struct wined3d_context *context)
1379{
1380 TRACE("Releasing context %p, level %u.\n", context, context->level);
1381
1382 if (WARN_ON(d3d))
1383 {
1384 if (!context->level)
1385 WARN("Context %p is not active.\n", context);
1386 else if (context != context_get_current())
1387 WARN("Context %p is not the current context.\n", context);
1388 }
1389
1390 if (!--context->level && context->restore_ctx)
1391 {
1392 TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
1393 context_restore_gl_context(context->gl_info, context->restore_dc, context->restore_ctx, context->restore_pf);
1394 context->restore_ctx = NULL;
1395 context->restore_dc = NULL;
1396 }
1397}
1398
1399static void context_enter(struct wined3d_context *context)
1400{
1401 TRACE("Entering context %p, level %u.\n", context, context->level + 1);
1402
1403 if (!context->level++)
1404 {
1405 const struct wined3d_context *current_context = context_get_current();
1406 HGLRC current_gl = wglGetCurrentContext();
1407
1408 if (current_gl && (!current_context || current_context->glCtx != current_gl))
1409 {
1410 TRACE("Another GL context (%p on device context %p) is already current.\n",
1411 current_gl, wglGetCurrentDC());
1412 context->restore_ctx = current_gl;
1413 context->restore_dc = wglGetCurrentDC();
1414 context->restore_pf = GetPixelFormat(context->restore_dc);
1415 }
1416 }
1417}
1418
1419void context_invalidate_state(struct wined3d_context *context, DWORD state)
1420{
1421 DWORD rep = context->state_table[state].representative;
1422 DWORD idx;
1423 BYTE shift;
1424
1425 if (isStateDirty(context, rep)) return;
1426
1427 context->dirtyArray[context->numDirtyEntries++] = rep;
1428 idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
1429 shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
1430 context->isStateDirty[idx] |= (1 << shift);
1431}
1432
1433/* This function takes care of wined3d pixel format selection. */
1434static int context_choose_pixel_format(const struct wined3d_device *device, HDC hdc,
1435 const struct wined3d_format *color_format, const struct wined3d_format *ds_format,
1436 BOOL auxBuffers, BOOL findCompatible)
1437{
1438 int iPixelFormat=0;
1439 BYTE redBits, greenBits, blueBits, alphaBits, colorBits;
1440 BYTE depthBits=0, stencilBits=0;
1441 unsigned int current_value;
1442 unsigned int cfg_count = device->adapter->cfg_count;
1443 unsigned int i;
1444
1445 TRACE("device %p, dc %p, color_format %s, ds_format %s, aux_buffers %#x, find_compatible %#x.\n",
1446 device, hdc, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id),
1447 auxBuffers, findCompatible);
1448
1449 if (!getColorBits(color_format, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
1450 {
1451 ERR("Unable to get color bits for format %s (%#x)!\n",
1452 debug_d3dformat(color_format->id), color_format->id);
1453 return 0;
1454 }
1455
1456 getDepthStencilBits(ds_format, &depthBits, &stencilBits);
1457
1458 current_value = 0;
1459 for (i = 0; i < cfg_count; ++i)
1460 {
1461 const struct wined3d_pixel_format *cfg = &device->adapter->cfgs[i];
1462 unsigned int value;
1463
1464 /* For now only accept RGBA formats. Perhaps some day we will
1465 * allow floating point formats for pbuffers. */
1466 if (cfg->iPixelType != WGL_TYPE_RGBA_ARB)
1467 continue;
1468 /* In window mode we need a window drawable format and double buffering. */
1469 if (!(cfg->windowDrawable && cfg->doubleBuffer))
1470 continue;
1471 if (cfg->redSize < redBits)
1472 continue;
1473 if (cfg->greenSize < greenBits)
1474 continue;
1475 if (cfg->blueSize < blueBits)
1476 continue;
1477 if (cfg->alphaSize < alphaBits)
1478 continue;
1479 if (cfg->depthSize < depthBits)
1480 continue;
1481 if (stencilBits && cfg->stencilSize != stencilBits)
1482 continue;
1483 /* Check multisampling support. */
1484 if (cfg->numSamples)
1485 continue;
1486
1487 value = 1;
1488 /* We try to locate a format which matches our requirements exactly. In case of
1489 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1490 if (cfg->depthSize == depthBits)
1491 value += 1;
1492 if (cfg->stencilSize == stencilBits)
1493 value += 2;
1494 if (cfg->alphaSize == alphaBits)
1495 value += 4;
1496 /* We like to have aux buffers in backbuffer mode */
1497 if (auxBuffers && cfg->auxBuffers)
1498 value += 8;
1499 if (cfg->redSize == redBits
1500 && cfg->greenSize == greenBits
1501 && cfg->blueSize == blueBits)
1502 value += 16;
1503
1504 if (value > current_value)
1505 {
1506 iPixelFormat = cfg->iPixelFormat;
1507 current_value = value;
1508 }
1509 }
1510
1511 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1512 if(!iPixelFormat && !findCompatible) {
1513 ERR("Can't find a suitable iPixelFormat\n");
1514 return FALSE;
1515 } else if(!iPixelFormat) {
1516 PIXELFORMATDESCRIPTOR pfd;
1517
1518 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1519 /* PixelFormat selection */
1520 ZeroMemory(&pfd, sizeof(pfd));
1521 pfd.nSize = sizeof(pfd);
1522 pfd.nVersion = 1;
1523 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
1524 pfd.iPixelType = PFD_TYPE_RGBA;
1525 pfd.cAlphaBits = alphaBits;
1526 pfd.cColorBits = colorBits;
1527 pfd.cDepthBits = depthBits;
1528 pfd.cStencilBits = stencilBits;
1529 pfd.iLayerType = PFD_MAIN_PLANE;
1530
1531 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
1532 if(!iPixelFormat) {
1533 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1534 ERR("Can't find a suitable iPixelFormat\n");
1535 return FALSE;
1536 }
1537 }
1538
1539 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1540 iPixelFormat, debug_d3dformat(color_format->id), debug_d3dformat(ds_format->id));
1541 return iPixelFormat;
1542}
1543
1544/* Context activation is done by the caller. */
1545static void bind_dummy_textures(const struct wined3d_device *device, const struct wined3d_context *context)
1546{
1547 const struct wined3d_gl_info *gl_info = context->gl_info;
1548 unsigned int i, count = min(MAX_COMBINED_SAMPLERS, gl_info->limits.combined_samplers);
1549
1550 for (i = 0; i < count; ++i)
1551 {
1552 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1553 checkGLcall("glActiveTextureARB");
1554
1555 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[i]);
1556 checkGLcall("glBindTexture");
1557
1558 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1559 {
1560 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[i]);
1561 checkGLcall("glBindTexture");
1562 }
1563
1564 if (gl_info->supported[EXT_TEXTURE3D])
1565 {
1566 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[i]);
1567 checkGLcall("glBindTexture");
1568 }
1569
1570 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1571 {
1572 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[i]);
1573 checkGLcall("glBindTexture");
1574 }
1575 }
1576}
1577
1578#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1579static DECLCALLBACK(void) context_tls_dtor(void* pvCtx)
1580{
1581 struct wined3d_context * context = (struct wined3d_context *)pvCtx;
1582 HeapFree(GetProcessHeap(), 0, context);
1583}
1584#endif
1585
1586BOOL context_debug_output_enabled(const struct wined3d_gl_info *gl_info)
1587{
1588 return gl_info->supported[ARB_DEBUG_OUTPUT]
1589 && (ERR_ON(d3d) || FIXME_ON(d3d) || WARN_ON(d3d_perf));
1590}
1591
1592static void WINE_GLAPI wined3d_debug_callback(GLenum source, GLenum type, GLuint id,
1593 GLenum severity, GLsizei length, const char *message, void *ctx)
1594{
1595 switch (type)
1596 {
1597 case GL_DEBUG_TYPE_ERROR_ARB:
1598 ERR("%p: %s.\n", ctx, debugstr_an(message, length));
1599 break;
1600
1601 case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB:
1602 case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB:
1603 case GL_DEBUG_TYPE_PORTABILITY_ARB:
1604 FIXME("%p: %s.\n", ctx, debugstr_an(message, length));
1605 break;
1606
1607 case GL_DEBUG_TYPE_PERFORMANCE_ARB:
1608 WARN_(d3d_perf)("%p: %s.\n", ctx, debugstr_an(message, length));
1609 break;
1610
1611 default:
1612 FIXME("ctx %p, type %#x: %s.\n", ctx, type, debugstr_an(message, length));
1613 break;
1614 }
1615}
1616
1617/* Do not call while under the GL lock. */
1618struct wined3d_context *context_create(struct wined3d_swapchain *swapchain,
1619 struct wined3d_surface *target, const struct wined3d_format *ds_format
1620#ifdef VBOX_WITH_WDDM
1621 , struct VBOXUHGSMI *pHgsmi
1622#endif
1623 )
1624{
1625 struct wined3d_device *device = swapchain->device;
1626 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1627 const struct wined3d_format *color_format;
1628 struct wined3d_context *ret;
1629 BOOL auxBuffers = FALSE;
1630 HGLRC ctx, share_ctx;
1631 int pixel_format;
1632 unsigned int s;
1633 int swap_interval;
1634 DWORD state;
1635 HDC hdc;
1636
1637 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1638
1639#ifdef VBOX_WITH_WDDM
1640 if (!pHgsmi)
1641 {
1642 ERR("HGSMI should be specified!");
1643 return NULL;
1644 }
1645#endif
1646
1647 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1648 if (!ret)
1649 return NULL;
1650
1651 ret->blit_targets = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1652 gl_info->limits.buffers * sizeof(*ret->blit_targets));
1653 if (!ret->blit_targets)
1654 goto out;
1655
1656 ret->draw_buffers = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1657 gl_info->limits.buffers * sizeof(*ret->draw_buffers));
1658 if (!ret->draw_buffers)
1659 goto out;
1660
1661 ret->free_occlusion_query_size = 4;
1662 ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
1663 ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
1664 if (!ret->free_occlusion_queries)
1665 goto out;
1666
1667 list_init(&ret->occlusion_queries);
1668
1669 ret->free_event_query_size = 4;
1670 ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
1671 ret->free_event_query_size * sizeof(*ret->free_event_queries));
1672 if (!ret->free_event_queries)
1673 goto out;
1674
1675 list_init(&ret->event_queries);
1676 list_init(&ret->fbo_list);
1677 list_init(&ret->fbo_destroy_list);
1678
1679#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1680 VBoxTlsRefInit(ret, context_tls_dtor);
1681#endif
1682
1683#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
1684 ret->device = device;
1685 if (!swapchain->hDC)
1686 {
1687 ERR("Swapchain hDC is null");
1688 goto out;
1689 }
1690 hdc = swapchain->hDC;
1691#else
1692 if (!(hdc = GetDC(swapchain->win_handle)))
1693 {
1694 WARN("Failed to retireve device context, trying swapchain backup.\n");
1695 if (!(hdc = swapchain_get_backup_dc(swapchain)))
1696 {
1697 ERR("Failed to retrieve a device context.\n");
1698 goto out;
1699 }
1700 }
1701#endif
1702
1703 color_format = target->resource.format;
1704
1705 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1706 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1707 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1708 {
1709 auxBuffers = TRUE;
1710
1711 if (color_format->id == WINED3DFMT_B4G4R4X4_UNORM)
1712 color_format = wined3d_get_format(gl_info, WINED3DFMT_B4G4R4A4_UNORM);
1713 else if (color_format->id == WINED3DFMT_B8G8R8X8_UNORM)
1714 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1715 }
1716
1717 /* DirectDraw supports 8bit paletted render targets and these are used by
1718 * old games like StarCraft and C&C. Most modern hardware doesn't support
1719 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1720 * conversion (ab)uses the alpha component for storing the palette index.
1721 * For this reason we require a format with 8bit alpha, so request
1722 * A8R8G8B8. */
1723 if (color_format->id == WINED3DFMT_P8_UINT)
1724 color_format = wined3d_get_format(gl_info, WINED3DFMT_B8G8R8A8_UNORM);
1725
1726 /* Try to find a pixel format which matches our requirements. */
1727 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, FALSE);
1728
1729 /* Try to locate a compatible format if we weren't able to find anything. */
1730 if (!pixel_format)
1731 {
1732 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1733 pixel_format = context_choose_pixel_format(device, hdc, color_format, ds_format, auxBuffers, TRUE);
1734 }
1735
1736 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1737 if (!pixel_format)
1738 {
1739 ERR("Can't find a suitable pixel format.\n");
1740 goto out;
1741 }
1742
1743 context_enter(ret);
1744#ifndef VBOX
1745 if (!context_set_pixel_format(gl_info, hdc, pixel_format))
1746 {
1747 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1748 context_release(ret);
1749 goto out;
1750 }
1751
1752 share_ctx = device->context_count ? device->contexts[0]->glCtx : NULL;
1753 if (gl_info->p_wglCreateContextAttribsARB)
1754 {
1755 unsigned int ctx_attrib_idx = 0;
1756 GLint ctx_attribs[3];
1757
1758 if (context_debug_output_enabled(gl_info))
1759 {
1760 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_FLAGS_ARB;
1761 ctx_attribs[ctx_attrib_idx++] = WGL_CONTEXT_DEBUG_BIT_ARB;
1762 }
1763 ctx_attribs[ctx_attrib_idx] = 0;
1764
1765 if (!(ctx = gl_info->p_wglCreateContextAttribsARB(hdc, share_ctx, ctx_attribs)))
1766 {
1767 ERR("Failed to create a WGL context.\n");
1768 context_release(ret);
1769 goto out;
1770 }
1771 }
1772 else
1773 {
1774 if (!(ctx = wglCreateContext(hdc)))
1775#else
1776 ctx = pVBoxCreateContext(hdc
1777#ifdef VBOX_WITH_WDDM
1778 , pHgsmi
1779#else
1780 , NULL
1781#endif
1782 );
1783 if (!ctx)
1784#endif
1785 {
1786 ERR("Failed to create a WGL context.\n");
1787 context_release(ret);
1788 goto out;
1789 }
1790
1791#ifdef VBOX_WITH_WDDM
1792 pVBoxCtxChromiumParameteriCR(ctx, GL_HOST_WND_CREATED_HIDDEN_CR, GL_TRUE);
1793#endif
1794
1795#ifndef VBOX
1796 if (share_ctx && !wglShareLists(share_ctx, ctx))
1797 {
1798 ERR("wglShareLists(%p, %p) failed, last error %#x.\n", share_ctx, ctx, GetLastError());
1799 context_release(ret);
1800 if (!wglDeleteContext(ctx))
1801 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1802 goto out;
1803 }
1804 }
1805#endif
1806
1807 if (!device_context_add(device, ret))
1808 {
1809 ERR("Failed to add the newly created context to the context list\n");
1810 context_release(ret);
1811 if (!wglDeleteContext(ctx))
1812 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1813 goto out;
1814 }
1815
1816 ret->gl_info = gl_info;
1817 ret->d3d_info = &device->adapter->d3d_info;
1818 ret->state_table = device->StateTable;
1819
1820 /* Mark all states dirty to force a proper initialization of the states
1821 * on the first use of the context. */
1822 for (state = 0; state <= STATE_HIGHEST; ++state)
1823 {
1824 if (ret->state_table[state].representative)
1825 context_invalidate_state(ret, state);
1826 }
1827
1828 ret->swapchain = swapchain;
1829 ret->current_rt = target;
1830#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1831 ret->tid = GetCurrentThreadId();
1832#else
1833 ret->tid = 0;
1834#endif
1835
1836 ret->render_offscreen = surface_is_offscreen(target);
1837 ret->draw_buffers_mask = context_generate_rt_mask(GL_BACK);
1838 ret->valid = 1;
1839
1840 ret->glCtx = ctx;
1841#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1842 ret->win_handle = swapchain->win_handle;
1843 ret->hdc = hdc;
1844#endif
1845 ret->pixel_format = pixel_format;
1846
1847 /* Set up the context defaults */
1848 if (!context_set_current(ret))
1849 {
1850 ERR("Cannot activate context to set up defaults.\n");
1851 device_context_remove(device, ret);
1852 context_release(ret);
1853 if (!wglDeleteContext(ctx))
1854 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, GetLastError());
1855 goto out;
1856 }
1857
1858 if (context_debug_output_enabled(gl_info))
1859 {
1860 GL_EXTCALL(glDebugMessageCallbackARB(wined3d_debug_callback, ret));
1861 if (TRACE_ON(d3d_synchronous))
1862 gl_info->gl_ops.gl.p_glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
1863 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE));
1864 if (ERR_ON(d3d))
1865 {
1866 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_ERROR_ARB,
1867 GL_DONT_CARE, 0, NULL, GL_TRUE));
1868 }
1869 if (FIXME_ON(d3d))
1870 {
1871 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_ARB,
1872 GL_DONT_CARE, 0, NULL, GL_TRUE));
1873 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_ARB,
1874 GL_DONT_CARE, 0, NULL, GL_TRUE));
1875 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PORTABILITY_ARB,
1876 GL_DONT_CARE, 0, NULL, GL_TRUE));
1877 }
1878 if (WARN_ON(d3d_perf))
1879 {
1880 GL_EXTCALL(glDebugMessageControlARB(GL_DONT_CARE, GL_DEBUG_TYPE_PERFORMANCE_ARB,
1881 GL_DONT_CARE, 0, NULL, GL_TRUE));
1882 }
1883 }
1884
1885 switch (swapchain->desc.swap_interval)
1886 {
1887 case WINED3DPRESENT_INTERVAL_IMMEDIATE:
1888 swap_interval = 0;
1889 break;
1890 case WINED3DPRESENT_INTERVAL_DEFAULT:
1891 case WINED3DPRESENT_INTERVAL_ONE:
1892 swap_interval = 1;
1893 break;
1894 case WINED3DPRESENT_INTERVAL_TWO:
1895 swap_interval = 2;
1896 break;
1897 case WINED3DPRESENT_INTERVAL_THREE:
1898 swap_interval = 3;
1899 break;
1900 case WINED3DPRESENT_INTERVAL_FOUR:
1901 swap_interval = 4;
1902 break;
1903 default:
1904 FIXME("Unknown swap interval %#x.\n", swapchain->desc.swap_interval);
1905 swap_interval = 1;
1906 }
1907
1908 if (gl_info->supported[WGL_EXT_SWAP_CONTROL])
1909 {
1910 if (!GL_EXTCALL(wglSwapIntervalEXT(swap_interval)))
1911 ERR("wglSwapIntervalEXT failed to set swap interval %d for context %p, last error %#x\n",
1912 swap_interval, ret, GetLastError());
1913 }
1914
1915#ifdef VBOX
1916 /* for WDDM case this is used for shared resource handling
1917 *
1918 * for XPDM this is needed to at least support texture sharing between device contexts.
1919 * this is a kinda hack, but it is needed since our ogl driver currently does not support ShareLists */
1920 pglChromiumParameteriCR(GL_SHARE_CONTEXT_RESOURCES_CR, GL_TRUE);
1921 pglChromiumParameteriCR(GL_CHECK_ZERO_VERT_ARRT, GL_TRUE);
1922# if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
1923 pglChromiumParameteriCR(GL_FLUSH_ON_THREAD_SWITCH_CR, GL_TRUE);
1924# endif
1925# if defined(VBOX_WITH_WDDM)
1926 pglChromiumParameteriCR(GL_HOST_WND_CREATED_HIDDEN_CR, GL_TRUE);
1927# endif
1928#endif
1929
1930 gl_info->gl_ops.gl.p_glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
1931
1932 TRACE("Setting up the screen\n");
1933
1934 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1935 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1936
1937 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1938 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1939
1940 gl_info->gl_ops.gl.p_glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1941 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1942
1943 gl_info->gl_ops.gl.p_glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
1944 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1945 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
1946 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1947
1948 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1949 {
1950 /* Most textures will use client storage if supported. Exceptions are
1951 * non-native power of 2 textures and textures in DIB sections. */
1952 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1953 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1954 }
1955 if (gl_info->supported[ARB_VERTEX_BLEND])
1956 {
1957 /* Direct3D always uses n-1 weights for n world matrices and uses
1958 * 1 - sum for the last one this is equal to GL_WEIGHT_SUM_UNITY_ARB.
1959 * Enabling it doesn't do anything unless GL_VERTEX_BLEND_ARB isn't
1960 * enabled as well. */
1961 gl_info->gl_ops.gl.p_glEnable(GL_WEIGHT_SUM_UNITY_ARB);
1962 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1963 }
1964 if (gl_info->supported[NV_TEXTURE_SHADER2])
1965 {
1966 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1967 * the previous texture where to source the offset from is always unit - 1.
1968 */
1969 for (s = 1; s < gl_info->limits.textures; ++s)
1970 {
1971 context_active_texture(ret, gl_info, s);
1972 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_SHADER_NV,
1973 GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
1974 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1975 }
1976 }
1977 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
1978 {
1979 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1980 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1981 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1982 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1983 * is ever assigned.
1984 *
1985 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1986 * program and the dummy program is destroyed when the context is destroyed.
1987 */
1988 const char *dummy_program =
1989 "!!ARBfp1.0\n"
1990 "MOV result.color, fragment.color.primary;\n"
1991 "END\n";
1992 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
1993 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
1994 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
1995 }
1996
1997 if (gl_info->supported[ARB_POINT_SPRITE])
1998 {
1999 for (s = 0; s < gl_info->limits.textures; ++s)
2000 {
2001 context_active_texture(ret, gl_info, s);
2002 gl_info->gl_ops.gl.p_glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
2003 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
2004 }
2005 }
2006
2007 if (gl_info->supported[ARB_PROVOKING_VERTEX])
2008 {
2009 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
2010 }
2011 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
2012 {
2013 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
2014 }
2015 ret->select_shader = 1;
2016
2017 /* If this happens to be the first context for the device, dummy textures
2018 * are not created yet. In that case, they will be created (and bound) by
2019 * create_dummy_textures right after this context is initialized. */
2020 if (device->dummy_texture_2d[0])
2021 bind_dummy_textures(device, ret);
2022
2023 TRACE("Created context %p.\n", ret);
2024
2025 return ret;
2026
2027out:
2028 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
2029 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
2030 HeapFree(GetProcessHeap(), 0, ret->draw_buffers);
2031 HeapFree(GetProcessHeap(), 0, ret->blit_targets);
2032 HeapFree(GetProcessHeap(), 0, ret);
2033 return NULL;
2034}
2035
2036/* Do not call while under the GL lock. */
2037void context_destroy(struct wined3d_device *device, struct wined3d_context *context)
2038{
2039#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2040 BOOL destroy;
2041#endif
2042
2043 TRACE("Destroying ctx %p\n", context);
2044
2045#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2046 if (context->tid == GetCurrentThreadId() || !context->current)
2047 {
2048 context_destroy_gl_resources(context);
2049 TlsSetValue(wined3d_context_tls_idx, NULL);
2050 destroy = TRUE;
2051 }
2052 else
2053 {
2054 /* Make a copy of gl_info for context_destroy_gl_resources use, the one
2055 in wined3d_adapter may go away in the meantime */
2056 struct wined3d_gl_info *gl_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_info));
2057 *gl_info = *context->gl_info;
2058 context->gl_info = gl_info;
2059 context->destroyed = 1;
2060 destroy = FALSE;
2061 }
2062#else
2063 context_destroy_gl_resources(context);
2064#endif
2065
2066 HeapFree(GetProcessHeap(), 0, context->draw_buffers);
2067 HeapFree(GetProcessHeap(), 0, context->blit_targets);
2068 device_context_remove(device, context);
2069#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2070 if (destroy) HeapFree(GetProcessHeap(), 0, context);
2071#else
2072 context->swapchain = NULL;
2073
2074 VBoxTlsRefMarkDestroy(context);
2075 VBoxTlsRefRelease(context);
2076#endif
2077}
2078
2079/* Context activation is done by the caller. */
2080static void set_blit_dimension(const struct wined3d_gl_info *gl_info, UINT width, UINT height)
2081{
2082 const GLdouble projection[] =
2083 {
2084 2.0 / width, 0.0, 0.0, 0.0,
2085 0.0, 2.0 / height, 0.0, 0.0,
2086 0.0, 0.0, 2.0, 0.0,
2087 -1.0, -1.0, -1.0, 1.0,
2088 };
2089
2090 gl_info->gl_ops.gl.p_glMatrixMode(GL_PROJECTION);
2091 checkGLcall("glMatrixMode(GL_PROJECTION)");
2092 gl_info->gl_ops.gl.p_glLoadMatrixd(projection);
2093 checkGLcall("glLoadMatrixd");
2094 gl_info->gl_ops.gl.p_glViewport(0, 0, width, height);
2095 checkGLcall("glViewport");
2096}
2097
2098static void context_get_rt_size(const struct wined3d_context *context, SIZE *size)
2099{
2100 const struct wined3d_surface *rt = context->current_rt;
2101
2102 if (rt->swapchain && rt->swapchain->front_buffer == rt)
2103 {
2104 RECT window_size;
2105#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
2106 ERR("not tested!");
2107 GetClientRect(rt->swapchain->win_handle, &window_size);
2108#else
2109 GetClientRect(context->win_handle, &window_size);
2110#endif
2111#ifdef DEBUG_misha
2112 Assert(window_size.right - window_size.left == rt->resource.width);
2113 Assert(window_size.bottom - window_size.top == rt->resource.height);
2114#endif
2115 size->cx = window_size.right - window_size.left;
2116 size->cy = window_size.bottom - window_size.top;
2117
2118 return;
2119 }
2120 size->cx = rt->resource.width;
2121 size->cy = rt->resource.height;
2122}
2123
2124/*****************************************************************************
2125 * SetupForBlit
2126 *
2127 * Sets up a context for DirectDraw blitting.
2128 * All texture units are disabled, texture unit 0 is set as current unit
2129 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
2130 * color writing enabled for all channels
2131 * register combiners disabled, shaders disabled
2132 * world matrix is set to identity, texture matrix 0 too
2133 * projection matrix is setup for drawing screen coordinates
2134 *
2135 * Params:
2136 * This: Device to activate the context for
2137 * context: Context to setup
2138 *
2139 *****************************************************************************/
2140/* Context activation is done by the caller. */
2141static void SetupForBlit(const struct wined3d_device *device, struct wined3d_context *context)
2142{
2143 int i;
2144 const struct wined3d_gl_info *gl_info = context->gl_info;
2145 DWORD sampler;
2146 SIZE rt_size;
2147
2148 TRACE("Setting up context %p for blitting\n", context);
2149
2150 context_get_rt_size(context, &rt_size);
2151
2152 if (context->last_was_blit)
2153 {
2154 if (context->blit_w != rt_size.cx || context->blit_h != rt_size.cy)
2155 {
2156 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2157 context->blit_w = rt_size.cx;
2158 context->blit_h = rt_size.cy;
2159 /* No need to dirtify here, the states are still dirtified because
2160 * they weren't applied since the last SetupForBlit() call. */
2161 }
2162 TRACE("Context is already set up for blitting, nothing to do\n");
2163 return;
2164 }
2165 context->last_was_blit = TRUE;
2166
2167 /* Disable all textures. The caller can then bind a texture it wants to blit
2168 * from
2169 *
2170 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
2171 * function texture unit. No need to care for higher samplers
2172 */
2173 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
2174 {
2175 sampler = device->rev_tex_unit_map[i];
2176 context_active_texture(context, gl_info, i);
2177
2178 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2179 {
2180 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2181 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2182 }
2183 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2184 checkGLcall("glDisable GL_TEXTURE_3D");
2185 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2186 {
2187 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2188 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2189 }
2190 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2191 checkGLcall("glDisable GL_TEXTURE_2D");
2192
2193 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2194 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
2195
2196 if (sampler != WINED3D_UNMAPPED_STAGE)
2197 {
2198 if (sampler < MAX_TEXTURES)
2199 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2200 context_invalidate_state(context, STATE_SAMPLER(sampler));
2201 }
2202 }
2203 context_active_texture(context, gl_info, 0);
2204
2205 sampler = device->rev_tex_unit_map[0];
2206
2207 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
2208 {
2209 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_CUBE_MAP_ARB);
2210 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
2211 }
2212 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_3D);
2213 checkGLcall("glDisable GL_TEXTURE_3D");
2214 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
2215 {
2216 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_RECTANGLE_ARB);
2217 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
2218 }
2219 gl_info->gl_ops.gl.p_glDisable(GL_TEXTURE_2D);
2220 checkGLcall("glDisable GL_TEXTURE_2D");
2221
2222 gl_info->gl_ops.gl.p_glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2223
2224 gl_info->gl_ops.gl.p_glMatrixMode(GL_TEXTURE);
2225 checkGLcall("glMatrixMode(GL_TEXTURE)");
2226 gl_info->gl_ops.gl.p_glLoadIdentity();
2227 checkGLcall("glLoadIdentity()");
2228
2229 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
2230 {
2231 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
2232 GL_TEXTURE_LOD_BIAS_EXT, 0.0f);
2233 checkGLcall("glTexEnvf GL_TEXTURE_LOD_BIAS_EXT ...");
2234 }
2235
2236 if (sampler != WINED3D_UNMAPPED_STAGE)
2237 {
2238 if (sampler < MAX_TEXTURES)
2239 {
2240 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_TEXTURE0 + sampler));
2241 context_invalidate_state(context, STATE_TEXTURESTAGE(sampler, WINED3D_TSS_COLOR_OP));
2242 }
2243 context_invalidate_state(context, STATE_SAMPLER(sampler));
2244 }
2245
2246 /* Other misc states */
2247 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
2248 checkGLcall("glDisable(GL_ALPHA_TEST)");
2249 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHATESTENABLE));
2250 gl_info->gl_ops.gl.p_glDisable(GL_LIGHTING);
2251 checkGLcall("glDisable GL_LIGHTING");
2252 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_LIGHTING));
2253 gl_info->gl_ops.gl.p_glDisable(GL_DEPTH_TEST);
2254 checkGLcall("glDisable GL_DEPTH_TEST");
2255 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ZENABLE));
2256 glDisableWINE(GL_FOG);
2257 checkGLcall("glDisable GL_FOG");
2258 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_FOGENABLE));
2259 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2260 checkGLcall("glDisable GL_BLEND");
2261 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2262 gl_info->gl_ops.gl.p_glDisable(GL_CULL_FACE);
2263 checkGLcall("glDisable GL_CULL_FACE");
2264 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CULLMODE));
2265 gl_info->gl_ops.gl.p_glDisable(GL_STENCIL_TEST);
2266 checkGLcall("glDisable GL_STENCIL_TEST");
2267 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_STENCILENABLE));
2268 gl_info->gl_ops.gl.p_glDisable(GL_SCISSOR_TEST);
2269 checkGLcall("glDisable GL_SCISSOR_TEST");
2270 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2271 if (gl_info->supported[ARB_POINT_SPRITE])
2272 {
2273 gl_info->gl_ops.gl.p_glDisable(GL_POINT_SPRITE_ARB);
2274 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
2275 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE));
2276 }
2277 gl_info->gl_ops.gl.p_glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
2278 checkGLcall("glColorMask");
2279 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE));
2280 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE1));
2281 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE2));
2282 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_COLORWRITEENABLE3));
2283 if (gl_info->supported[EXT_SECONDARY_COLOR])
2284 {
2285 gl_info->gl_ops.gl.p_glDisable(GL_COLOR_SUM_EXT);
2286 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SPECULARENABLE));
2287 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
2288 }
2289
2290 /* Setup transforms */
2291 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
2292 checkGLcall("glMatrixMode(GL_MODELVIEW)");
2293 gl_info->gl_ops.gl.p_glLoadIdentity();
2294 checkGLcall("glLoadIdentity()");
2295 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)));
2296
2297 context->last_was_rhw = TRUE;
2298 context_invalidate_state(context, STATE_VDECL); /* because of last_was_rhw = TRUE */
2299
2300 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
2301 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
2302 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
2303 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
2304 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
2305 gl_info->gl_ops.gl.p_glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
2306 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_CLIPPING));
2307
2308 set_blit_dimension(gl_info, rt_size.cx, rt_size.cy);
2309
2310 /* Disable shaders */
2311 device->shader_backend->shader_disable(device->shader_priv, context);
2312 context->select_shader = 1;
2313 context->load_constants = 1;
2314
2315 context->blit_w = rt_size.cx;
2316 context->blit_h = rt_size.cy;
2317 context_invalidate_state(context, STATE_VIEWPORT);
2318 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2319}
2320
2321static inline BOOL is_rt_mask_onscreen(DWORD rt_mask)
2322{
2323 return rt_mask & (1 << 31);
2324}
2325
2326static inline GLenum draw_buffer_from_rt_mask(DWORD rt_mask)
2327{
2328 return rt_mask & ~(1 << 31);
2329}
2330
2331/* Context activation is done by the caller. */
2332static void context_apply_draw_buffers(struct wined3d_context *context, DWORD rt_mask)
2333{
2334 const struct wined3d_gl_info *gl_info = context->gl_info;
2335
2336 if (!rt_mask)
2337 {
2338 gl_info->gl_ops.gl.p_glDrawBuffer(GL_NONE);
2339 checkGLcall("glDrawBuffer()");
2340 }
2341 else if (is_rt_mask_onscreen(rt_mask))
2342 {
2343 gl_info->gl_ops.gl.p_glDrawBuffer(draw_buffer_from_rt_mask(rt_mask));
2344 checkGLcall("glDrawBuffer()");
2345 }
2346 else
2347 {
2348 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2349 {
2350 unsigned int i = 0;
2351
2352 while (rt_mask)
2353 {
2354 if (rt_mask & 1)
2355 context->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
2356 else
2357 context->draw_buffers[i] = GL_NONE;
2358
2359 rt_mask >>= 1;
2360 ++i;
2361 }
2362
2363 if (gl_info->supported[ARB_DRAW_BUFFERS])
2364 {
2365 GL_EXTCALL(glDrawBuffersARB(i, context->draw_buffers));
2366 checkGLcall("glDrawBuffers()");
2367 }
2368 else
2369 {
2370 gl_info->gl_ops.gl.p_glDrawBuffer(context->draw_buffers[0]);
2371 checkGLcall("glDrawBuffer()");
2372 }
2373 }
2374 else
2375 {
2376 ERR("Unexpected draw buffers mask with backbuffer ORM.\n");
2377 }
2378 }
2379}
2380
2381/* Context activation is done by the caller. */
2382void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2383{
2384 const struct wined3d_gl_info *gl_info = context->gl_info;
2385 DWORD *current_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2386 DWORD new_mask = context_generate_rt_mask(buffer);
2387
2388 if (new_mask == *current_mask)
2389 return;
2390
2391 gl_info->gl_ops.gl.p_glDrawBuffer(buffer);
2392 checkGLcall("glDrawBuffer()");
2393
2394 *current_mask = new_mask;
2395}
2396
2397/* Context activation is done by the caller. */
2398void context_active_texture(struct wined3d_context *context, const struct wined3d_gl_info *gl_info, unsigned int unit)
2399{
2400 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0 + unit));
2401 checkGLcall("glActiveTextureARB");
2402 context->active_texture = unit;
2403}
2404
2405void context_bind_texture(struct wined3d_context *context, GLenum target, GLuint name)
2406{
2407 const struct wined3d_gl_info *gl_info = context->gl_info;
2408 DWORD unit = context->active_texture;
2409 DWORD old_texture_type = context->texture_type[unit];
2410
2411 if (name)
2412 {
2413 gl_info->gl_ops.gl.p_glBindTexture(target, name);
2414 checkGLcall("glBindTexture");
2415 }
2416 else
2417 {
2418 target = GL_NONE;
2419 }
2420
2421 if (old_texture_type != target)
2422 {
2423 const struct wined3d_device *device = context->swapchain->device;
2424
2425 switch (old_texture_type)
2426 {
2427 case GL_NONE:
2428 /* nothing to do */
2429 break;
2430 case GL_TEXTURE_2D:
2431 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_2D, device->dummy_texture_2d[unit]);
2432 checkGLcall("glBindTexture");
2433 break;
2434 case GL_TEXTURE_RECTANGLE_ARB:
2435 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_RECTANGLE_ARB, device->dummy_texture_rect[unit]);
2436 checkGLcall("glBindTexture");
2437 break;
2438 case GL_TEXTURE_CUBE_MAP:
2439 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_CUBE_MAP, device->dummy_texture_cube[unit]);
2440 checkGLcall("glBindTexture");
2441 break;
2442 case GL_TEXTURE_3D:
2443 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_3D, device->dummy_texture_3d[unit]);
2444 checkGLcall("glBindTexture");
2445 break;
2446 default:
2447 ERR("Unexpected texture target %#x\n", old_texture_type);
2448 }
2449
2450 context->texture_type[unit] = target;
2451 }
2452}
2453
2454static void context_set_render_offscreen(struct wined3d_context *context, BOOL offscreen)
2455{
2456 if (context->render_offscreen == offscreen) return;
2457
2458 context_invalidate_state(context, STATE_POINTSPRITECOORDORIGIN);
2459 context_invalidate_state(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
2460 context_invalidate_state(context, STATE_VIEWPORT);
2461 context_invalidate_state(context, STATE_SCISSORRECT);
2462 context_invalidate_state(context, STATE_FRONTFACE);
2463 context->render_offscreen = offscreen;
2464}
2465
2466static BOOL match_depth_stencil_format(const struct wined3d_format *existing,
2467 const struct wined3d_format *required)
2468{
2469 BYTE existing_depth, existing_stencil, required_depth, required_stencil;
2470
2471 if (existing == required) return TRUE;
2472 if ((existing->flags & WINED3DFMT_FLAG_FLOAT) != (required->flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2473
2474 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2475 getDepthStencilBits(required, &required_depth, &required_stencil);
2476
2477 if(existing_depth < required_depth) return FALSE;
2478 /* If stencil bits are used the exact amount is required - otherwise wrapping
2479 * won't work correctly */
2480 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2481 return TRUE;
2482}
2483
2484/* The caller provides a context */
2485static void context_validate_onscreen_formats(struct wined3d_context *context,
2486 const struct wined3d_surface *depth_stencil)
2487{
2488 /* Onscreen surfaces are always in a swapchain */
2489 struct wined3d_swapchain *swapchain = context->current_rt->swapchain;
2490
2491 if (context->render_offscreen || !depth_stencil) return;
2492 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format)) return;
2493
2494 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2495 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2496 * format. */
2497 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2498
2499 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2500 surface_load_location(context->current_rt, SFLAG_INTEXTURE, NULL);
2501 swapchain->render_to_fbo = TRUE;
2502 swapchain_update_draw_bindings(swapchain);
2503 context_set_render_offscreen(context, TRUE);
2504}
2505
2506static DWORD context_generate_rt_mask_no_fbo(const struct wined3d_device *device, const struct wined3d_surface *rt)
2507{
2508 if (!rt || rt->resource.format->id == WINED3DFMT_NULL)
2509 return 0;
2510 else if (rt->swapchain)
2511 return context_generate_rt_mask_from_surface(rt);
2512 else
2513 return context_generate_rt_mask(device->offscreenBuffer);
2514}
2515
2516/* Context activation is done by the caller. */
2517void context_apply_blit_state(struct wined3d_context *context, const struct wined3d_device *device)
2518{
2519 struct wined3d_surface *rt = context->current_rt;
2520 DWORD rt_mask, *cur_mask;
2521
2522 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2523 {
2524 context_validate_onscreen_formats(context, NULL);
2525
2526 if (context->render_offscreen)
2527 {
2528 surface_internal_preload(rt, SRGB_RGB);
2529
2530 context_apply_fbo_state_blit(context, GL_FRAMEBUFFER, rt, NULL, rt->draw_binding);
2531 if (rt->resource.format->id != WINED3DFMT_NULL)
2532 rt_mask = 1;
2533 else
2534 rt_mask = 0;
2535 }
2536 else
2537 {
2538 context->current_fbo = NULL;
2539 context_bind_fbo(context, GL_FRAMEBUFFER, 0);
2540 rt_mask = context_generate_rt_mask_from_surface(rt);
2541 }
2542 }
2543 else
2544 {
2545 rt_mask = context_generate_rt_mask_no_fbo(device, rt);
2546 }
2547
2548 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2549
2550 if (rt_mask != *cur_mask)
2551 {
2552 context_apply_draw_buffers(context, rt_mask);
2553 *cur_mask = rt_mask;
2554 }
2555
2556 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2557 {
2558 context_check_fbo_status(context, GL_FRAMEBUFFER);
2559 }
2560
2561 SetupForBlit(device, context);
2562 context_invalidate_state(context, STATE_FRAMEBUFFER);
2563}
2564
2565static BOOL context_validate_rt_config(UINT rt_count,
2566 struct wined3d_surface * const *rts, const struct wined3d_surface *ds)
2567{
2568 unsigned int i;
2569
2570 if (ds) return TRUE;
2571
2572 for (i = 0; i < rt_count; ++i)
2573 {
2574 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2575 return TRUE;
2576 }
2577
2578 WARN("Invalid render target config, need at least one attachment.\n");
2579 return FALSE;
2580}
2581
2582/* Context activation is done by the caller. */
2583BOOL context_apply_clear_state(struct wined3d_context *context, const struct wined3d_device *device,
2584 UINT rt_count, const struct wined3d_fb_state *fb)
2585{
2586 const struct wined3d_gl_info *gl_info = context->gl_info;
2587 DWORD rt_mask = 0, *cur_mask;
2588 UINT i;
2589 struct wined3d_surface **rts = fb->render_targets;
2590
2591 if (isStateDirty(context, STATE_FRAMEBUFFER) || fb != &device->fb
2592 || rt_count != context->gl_info->limits.buffers)
2593 {
2594 if (!context_validate_rt_config(rt_count, rts, fb->depth_stencil))
2595 return FALSE;
2596
2597 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2598 {
2599 context_validate_onscreen_formats(context, fb->depth_stencil);
2600
2601 if (!rt_count || surface_is_offscreen(rts[0]))
2602 {
2603 for (i = 0; i < rt_count; ++i)
2604 {
2605 context->blit_targets[i] = rts[i];
2606 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL)
2607 rt_mask |= (1 << i);
2608 }
2609 while (i < context->gl_info->limits.buffers)
2610 {
2611 context->blit_targets[i] = NULL;
2612 ++i;
2613 }
2614 context_apply_fbo_state(context, GL_FRAMEBUFFER, context->blit_targets, fb->depth_stencil,
2615 rt_count ? rts[0]->draw_binding : SFLAG_INTEXTURE);
2616 }
2617 else
2618 {
2619 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2620 rt_mask = context_generate_rt_mask_from_surface(rts[0]);
2621 }
2622
2623 /* If the framebuffer is not the device's fb the device's fb has to be reapplied
2624 * next draw. Otherwise we could mark the framebuffer state clean here, once the
2625 * state management allows this */
2626 context_invalidate_state(context, STATE_FRAMEBUFFER);
2627 }
2628 else
2629 {
2630 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2631 }
2632 }
2633 else if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
2634 && (!rt_count || surface_is_offscreen(rts[0])))
2635 {
2636 for (i = 0; i < rt_count; ++i)
2637 {
2638 if (rts[i] && rts[i]->resource.format->id != WINED3DFMT_NULL) rt_mask |= (1 << i);
2639 }
2640 }
2641 else
2642 {
2643 rt_mask = context_generate_rt_mask_no_fbo(device, rt_count ? rts[0] : NULL);
2644 }
2645
2646 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2647
2648 if (rt_mask != *cur_mask)
2649 {
2650 context_apply_draw_buffers(context, rt_mask);
2651 *cur_mask = rt_mask;
2652 context_invalidate_state(context, STATE_FRAMEBUFFER);
2653 }
2654
2655 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2656 {
2657 context_check_fbo_status(context, GL_FRAMEBUFFER);
2658 }
2659
2660 if (context->last_was_blit)
2661 context->last_was_blit = FALSE;
2662
2663 /* Blending and clearing should be orthogonal, but tests on the nvidia
2664 * driver show that disabling blending when clearing improves the clearing
2665 * performance incredibly. */
2666 gl_info->gl_ops.gl.p_glDisable(GL_BLEND);
2667 gl_info->gl_ops.gl.p_glEnable(GL_SCISSOR_TEST);
2668 checkGLcall("glEnable GL_SCISSOR_TEST");
2669
2670 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2671 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SCISSORTESTENABLE));
2672 context_invalidate_state(context, STATE_SCISSORRECT);
2673
2674 return TRUE;
2675}
2676
2677static DWORD find_draw_buffers_mask(const struct wined3d_context *context, const struct wined3d_device *device)
2678{
2679 const struct wined3d_state *state = &device->stateBlock->state;
2680 struct wined3d_surface **rts = state->fb->render_targets;
2681 struct wined3d_shader *ps = state->pixel_shader;
2682 DWORD rt_mask, rt_mask_bits;
2683 unsigned int i;
2684
2685 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return context_generate_rt_mask_no_fbo(device, rts[0]);
2686 else if (!context->render_offscreen) return context_generate_rt_mask_from_surface(rts[0]);
2687
2688 rt_mask = ps ? ps->reg_maps.rt_mask : 1;
2689 rt_mask &= context->d3d_info->valid_rt_mask;
2690 rt_mask_bits = rt_mask;
2691 i = 0;
2692 while (rt_mask_bits)
2693 {
2694 rt_mask_bits &= ~(1 << i);
2695 if (!rts[i] || rts[i]->resource.format->id == WINED3DFMT_NULL)
2696 rt_mask &= ~(1 << i);
2697
2698 i++;
2699 }
2700
2701 return rt_mask;
2702}
2703
2704/* Context activation is done by the caller. */
2705void context_state_fb(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2706{
2707 const struct wined3d_device *device = context->swapchain->device;
2708 const struct wined3d_fb_state *fb = state->fb;
2709 DWORD rt_mask = find_draw_buffers_mask(context, device);
2710 DWORD *cur_mask;
2711
2712 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2713 {
2714 if (!context->render_offscreen)
2715 {
2716 context_apply_fbo_state(context, GL_FRAMEBUFFER, NULL, NULL, SFLAG_INDRAWABLE);
2717 }
2718 else
2719 {
2720 context_apply_fbo_state(context, GL_FRAMEBUFFER, fb->render_targets, fb->depth_stencil,
2721 fb->render_targets[0]->draw_binding);
2722 }
2723 }
2724
2725 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2726 if (rt_mask != *cur_mask)
2727 {
2728 context_apply_draw_buffers(context, rt_mask);
2729 *cur_mask = rt_mask;
2730 }
2731}
2732
2733/* Context activation is done by the caller. */
2734void context_state_drawbuf(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
2735{
2736 const struct wined3d_device *device = context->swapchain->device;
2737 DWORD rt_mask, *cur_mask;
2738
2739 if (isStateDirty(context, STATE_FRAMEBUFFER)) return;
2740
2741 cur_mask = context->current_fbo ? &context->current_fbo->rt_mask : &context->draw_buffers_mask;
2742 rt_mask = find_draw_buffers_mask(context, device);
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. */
2751BOOL context_apply_draw_state(struct wined3d_context *context, struct wined3d_device *device)
2752{
2753 const struct wined3d_state *state = &device->stateBlock->state;
2754 const struct StateEntry *state_table = context->state_table;
2755 const struct wined3d_fb_state *fb = state->fb;
2756 unsigned int i;
2757
2758 if (!context_validate_rt_config(context->gl_info->limits.buffers,
2759 fb->render_targets, fb->depth_stencil))
2760 return FALSE;
2761
2762 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && isStateDirty(context, STATE_FRAMEBUFFER))
2763 {
2764 context_validate_onscreen_formats(context, fb->depth_stencil);
2765 }
2766
2767 /* Preload resources before FBO setup. Texture preload in particular may
2768 * result in changes to the current FBO, due to using e.g. FBO blits for
2769 * updating a resource location. */
2770 device_update_tex_unit_map(device);
2771 device_preload_textures(device);
2772 if (isStateDirty(context, STATE_VDECL) || isStateDirty(context, STATE_STREAMSRC))
2773 device_update_stream_info(device, context->gl_info);
2774 if (state->index_buffer)
2775 {
2776 if (device->stream_info.all_vbo)
2777 wined3d_buffer_preload(state->index_buffer);
2778 else
2779 buffer_get_sysmem(state->index_buffer, context->gl_info);
2780 }
2781
2782 for (i = 0; i < context->numDirtyEntries; ++i)
2783 {
2784 DWORD rep = context->dirtyArray[i];
2785 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2786 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2787 context->isStateDirty[idx] &= ~(1 << shift);
2788 state_table[rep].apply(context, state, rep);
2789 }
2790
2791 if (context->select_shader)
2792 {
2793 device->shader_backend->shader_select(device->shader_priv, context, state);
2794 context->select_shader = 0;
2795 }
2796
2797 if (context->load_constants)
2798 {
2799 device->shader_backend->shader_load_constants(device->shader_priv,
2800 context, state);
2801 context->load_constants = 0;
2802 }
2803
2804 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2805 {
2806 context_check_fbo_status(context, GL_FRAMEBUFFER);
2807 }
2808
2809 context->numDirtyEntries = 0; /* This makes the whole list clean */
2810 context->last_was_blit = FALSE;
2811
2812 return TRUE;
2813}
2814
2815static void context_setup_target(struct wined3d_context *context, struct wined3d_surface *target)
2816{
2817 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2818
2819 render_offscreen = surface_is_offscreen(target);
2820 if (context->current_rt == target && render_offscreen == old_render_offscreen) return;
2821
2822 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2823 * the alpha blend state changes with different render target formats. */
2824 if (!context->current_rt)
2825 {
2826 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2827 }
2828 else
2829 {
2830 const struct wined3d_format *old = context->current_rt->resource.format;
2831 const struct wined3d_format *new = target->resource.format;
2832
2833 if (old->id != new->id)
2834 {
2835 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2836 if ((old->alpha_size && !new->alpha_size) || (!old->alpha_size && new->alpha_size)
2837 || !(new->flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2838 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_ALPHABLENDENABLE));
2839
2840 /* Update sRGB writing when switching between formats that do/do not support sRGB writing */
2841 if ((old->flags & WINED3DFMT_FLAG_SRGB_WRITE) != (new->flags & WINED3DFMT_FLAG_SRGB_WRITE))
2842 context_invalidate_state(context, STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE));
2843 }
2844
2845 /* When switching away from an offscreen render target, and we're not
2846 * using FBOs, we have to read the drawable into the texture. This is
2847 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2848 * are some things that need care though. PreLoad needs a GL context,
2849 * and FindContext is called before the context is activated. It also
2850 * has to be called with the old rendertarget active, otherwise a
2851 * wrong drawable is read. */
2852 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2853 && old_render_offscreen && context->current_rt != target)
2854 {
2855 /* Read the back buffer of the old drawable into the destination texture. */
2856 if (context->current_rt->texture_name_srgb)
2857 surface_internal_preload(context->current_rt, SRGB_SRGB);
2858 surface_internal_preload(context->current_rt, SRGB_RGB);
2859 surface_modify_location(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2860 }
2861 }
2862
2863 context->current_rt = target;
2864 context_set_render_offscreen(context, render_offscreen);
2865}
2866
2867/* Do not call while under the GL lock. */
2868struct wined3d_context *context_acquire(const struct wined3d_device *device, struct wined3d_surface *target)
2869{
2870 struct wined3d_context *current_context = context_get_current();
2871 struct wined3d_context *context;
2872#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
2873 struct wined3d_swapchain *swapchain = NULL;
2874#endif
2875
2876 TRACE("device %p, target %p.\n", device, target);
2877
2878#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
2879 if (current_context && current_context->destroyed)
2880 current_context = NULL;
2881#endif
2882
2883 if (!target)
2884 {
2885 if (current_context
2886 && current_context->current_rt
2887#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2888 && current_context->swapchain->device == device
2889#else
2890 && current_context->device == device
2891#endif
2892 )
2893 {
2894 target = current_context->current_rt;
2895 }
2896 else
2897 {
2898#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2899 struct wined3d_swapchain *swapchain = device->swapchains[0];
2900#else
2901 swapchain = swapchain_find_valid_or_any(device);
2902 if (!swapchain)
2903 {
2904 ERR("no valid swapchain found!");
2905 swapchain = device->swapchains[0];
2906 Assert(swapchain);
2907 }
2908#endif
2909 if (swapchain->back_buffers)
2910 target = swapchain->back_buffers[0];
2911 else
2912 target = swapchain->front_buffer;
2913 }
2914 }
2915
2916#ifdef VBOX_WITH_WINE_DBG
2917 Assert(target);
2918#endif
2919
2920 if (current_context && current_context->current_rt == target)
2921 {
2922 context = current_context;
2923 }
2924 else if (target->swapchain)
2925 {
2926 TRACE("Rendering onscreen.\n");
2927
2928 context = swapchain_get_context(target->swapchain);
2929#ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
2930 swapchain = target->swapchain;
2931 Assert(swapchain);
2932#endif
2933 }
2934 else
2935 {
2936 TRACE("Rendering offscreen.\n");
2937
2938#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2939 /* Stay with the current context if possible. Otherwise use the
2940 * context for the primary swapchain. */
2941 if (current_context && current_context->swapchain->device == device)
2942 context = current_context;
2943 else
2944#endif
2945 context = swapchain_get_context(device->swapchains[0]);
2946 }
2947
2948#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
2949 context_update_window(context);
2950#else
2951 if (!swapchain)
2952 swapchain = swapchain_find_valid_or_any(device);
2953 context_update_window(context, swapchain);
2954#endif
2955 context_setup_target(context, target);
2956 context_enter(context);
2957 if (!context->valid) return context;
2958
2959 if (context != current_context)
2960 {
2961 if (!context_set_current(context))
2962 ERR("Failed to activate the new context.\n");
2963 }
2964 else if (context->restore_ctx)
2965 {
2966 context_set_gl_context(context);
2967 }
2968
2969 return context;
2970}
Note: See TracBrowser for help on using the repository browser.

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