VirtualBox

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

最後變更 在這個檔案從69350是 63025,由 vboxsync 提交於 9 年 前

GA/NT/Graphics: warnings

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

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