VirtualBox

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

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

header fixes

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