VirtualBox

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

Last change on this file since 35718 was 34290, checked in by vboxsync, 14 years ago

wddm/3d: aero multimon fixes

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