VirtualBox

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

Last change on this file since 38146 was 38146, checked in by vboxsync, 13 years ago

wddm/wine: enable single context

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

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