VirtualBox

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

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

wddm/3d: fix libwine crashes on winsat run

  • Property svn:eol-style set to native
File size: 87.4 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 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
24 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
25 * a choice of LGPL license versions is made available with the language indicating
26 * that LGPLv2 or any later version may be used, or where a choice of which version
27 * of the LGPL is applied is otherwise unspecified.
28 */
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# ifdef DEBUG_misha
760 Assert(0);
761# endif
762 TRACE("Updating context %p window from %p to %p.\n",
763 context, context->win_handle, swapchain->win_handle);
764#else
765 TRACE("Updating context %p window from %p to %p.\n",
766 context, context->win_handle, context->swapchain->win_handle);
767#endif
768
769 if (context->valid)
770 {
771 if (!ReleaseDC(context->win_handle, context->hdc))
772 {
773 ERR("Failed to release device context %p, last error %#x.\n",
774 context->hdc, GetLastError());
775 }
776 }
777 else context->valid = 1;
778
779#ifdef VBOX_WITH_WDDM
780 context->win_handle = swapchain->win_handle;
781 context->currentSwapchain = swapchain;
782#else
783 context->win_handle = context->swapchain->win_handle;
784#endif
785 if (!(context->hdc = GetDC(context->win_handle)))
786 {
787 ERR("Failed to get a device context for window %p.\n", context->win_handle);
788 goto err;
789 }
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#if defined(VBOX_WITH_WDDM) && defined(DEBUG_misha)
1296 Assert(0);
1297#endif
1298
1299 TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
1300
1301 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
1302 if (!ret)
1303 {
1304 ERR("Failed to allocate context memory.\n");
1305 return NULL;
1306 }
1307
1308 if (!(hdc = GetDC(swapchain->win_handle)))
1309 {
1310 ERR("Failed to retrieve a device context.\n");
1311 goto out;
1312 }
1313
1314 color_format_desc = target->resource.format_desc;
1315
1316 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1317 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1318 if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
1319 {
1320 auxBuffers = TRUE;
1321
1322 if (color_format_desc->format == WINED3DFMT_B4G4R4X4_UNORM)
1323 color_format_desc = getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM, gl_info);
1324 else if (color_format_desc->format == WINED3DFMT_B8G8R8X8_UNORM)
1325 color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
1326 }
1327
1328 /* DirectDraw supports 8bit paletted render targets and these are used by
1329 * old games like Starcraft and C&C. Most modern hardware doesn't support
1330 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1331 * conversion (ab)uses the alpha component for storing the palette index.
1332 * For this reason we require a format with 8bit alpha, so request
1333 * A8R8G8B8. */
1334 if (color_format_desc->format == WINED3DFMT_P8_UINT)
1335 color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
1336
1337 /* Retrieve the depth stencil format from the present parameters.
1338 * The choice of the proper format can give a nice performance boost
1339 * in case of GPU limited programs. */
1340 if (swapchain->presentParms.EnableAutoDepthStencil)
1341 {
1342 TRACE("Auto depth stencil enabled, using format %s.\n",
1343 debug_d3dformat(swapchain->presentParms.AutoDepthStencilFormat));
1344 ds_format_desc = getFormatDescEntry(swapchain->presentParms.AutoDepthStencilFormat, gl_info);
1345 }
1346
1347 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1348 if (swapchain->presentParms.MultiSampleType && (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD))
1349 {
1350 if (!gl_info->supported[ARB_MULTISAMPLE])
1351 WARN("The application is requesting multisampling without support.\n");
1352 else
1353 {
1354 TRACE("Requesting multisample type %#x.\n", swapchain->presentParms.MultiSampleType);
1355 numSamples = swapchain->presentParms.MultiSampleType;
1356 }
1357 }
1358
1359 /* Try to find a pixel format which matches our requirements. */
1360 pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
1361 auxBuffers, numSamples, FALSE /* findCompatible */);
1362
1363 /* Try to locate a compatible format if we weren't able to find anything. */
1364 if (!pixel_format)
1365 {
1366 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1367 pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
1368 auxBuffers, 0 /* numSamples */, TRUE /* findCompatible */);
1369 }
1370
1371 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1372 if (!pixel_format)
1373 {
1374 ERR("Can't find a suitable pixel format.\n");
1375 goto out;
1376 }
1377
1378 DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd);
1379 if (!context_set_pixel_format(gl_info, hdc, pixel_format))
1380 {
1381 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
1382 goto out;
1383 }
1384
1385 ctx = pwglCreateContext(hdc);
1386 if (device->numContexts)
1387 {
1388 if (!pwglShareLists(device->contexts[0]->glCtx, ctx))
1389 {
1390 DWORD err = GetLastError();
1391 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1392 device->contexts[0]->glCtx, ctx, err);
1393 }
1394 }
1395
1396 if(!ctx) {
1397 ERR("Failed to create a WGL context\n");
1398 goto out;
1399 }
1400
1401 if (!device_context_add(device, ret))
1402 {
1403 ERR("Failed to add the newly created context to the context list\n");
1404 if (!pwglDeleteContext(ctx))
1405 {
1406 DWORD err = GetLastError();
1407 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, err);
1408 }
1409 goto out;
1410 }
1411
1412 ret->gl_info = gl_info;
1413
1414 /* Mark all states dirty to force a proper initialization of the states
1415 * on the first use of the context. */
1416 for (state = 0; state <= STATE_HIGHEST; ++state)
1417 {
1418 if (device->StateTable[state].representative)
1419 Context_MarkStateDirty(ret, state, device->StateTable);
1420 }
1421
1422#ifdef VBOX_WITH_WDDM
1423 ret->device = device;
1424 ret->currentSwapchain = swapchain;
1425#else
1426 ret->swapchain = swapchain;
1427#endif
1428 ret->current_rt = (IWineD3DSurface *)target;
1429 ret->tid = GetCurrentThreadId();
1430
1431 ret->render_offscreen = surface_is_offscreen((IWineD3DSurface *) target);
1432 ret->draw_buffer_dirty = TRUE;
1433 ret->valid = 1;
1434
1435 ret->glCtx = ctx;
1436 ret->win_handle = swapchain->win_handle;
1437 ret->hdc = hdc;
1438 ret->pixel_format = pixel_format;
1439
1440 if (device->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *)device))
1441 {
1442 /* Create the dirty constants array and initialize them to dirty */
1443 ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
1444 sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
1445 ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
1446 sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
1447 memset(ret->vshader_const_dirty, 1,
1448 sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
1449 memset(ret->pshader_const_dirty, 1,
1450 sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
1451 }
1452
1453 ret->free_occlusion_query_size = 4;
1454 ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
1455 ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
1456 if (!ret->free_occlusion_queries) goto out;
1457
1458 list_init(&ret->occlusion_queries);
1459
1460 ret->free_event_query_size = 4;
1461 ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
1462 ret->free_event_query_size * sizeof(*ret->free_event_queries));
1463 if (!ret->free_event_queries) goto out;
1464
1465 list_init(&ret->event_queries);
1466
1467 TRACE("Successfully created new context %p\n", ret);
1468
1469 list_init(&ret->fbo_list);
1470 list_init(&ret->fbo_destroy_list);
1471
1472 context_enter(ret);
1473
1474 /* Set up the context defaults */
1475 if (!context_set_current(ret))
1476 {
1477 ERR("Cannot activate context to set up defaults\n");
1478 context_release(ret);
1479 goto out;
1480 }
1481
1482 ENTER_GL();
1483
1484 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
1485
1486 TRACE("Setting up the screen\n");
1487 /* Clear the screen */
1488 glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
1489 checkGLcall("glClearColor");
1490 glClearIndex(0);
1491 glClearDepth(1);
1492 glClearStencil(0xffff);
1493
1494 checkGLcall("glClear");
1495
1496 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
1497 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1498
1499 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
1500 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1501
1502 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
1503 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1504
1505 glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
1506 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1507 glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
1508 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1509
1510 if (gl_info->supported[APPLE_CLIENT_STORAGE])
1511 {
1512 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1513 * and textures in DIB sections(due to the memory protection).
1514 */
1515 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1516 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1517 }
1518 if (gl_info->supported[ARB_VERTEX_BLEND])
1519 {
1520 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1521 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1522 * GL_VERTEX_BLEND_ARB isn't enabled too
1523 */
1524 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
1525 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1526 }
1527 if (gl_info->supported[NV_TEXTURE_SHADER2])
1528 {
1529 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1530 * the previous texture where to source the offset from is always unit - 1.
1531 */
1532 for (s = 1; s < gl_info->limits.textures; ++s)
1533 {
1534 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
1535 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
1536 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1537 }
1538 }
1539 if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
1540 {
1541 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1542 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1543 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1544 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1545 * is ever assigned.
1546 *
1547 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1548 * program and the dummy program is destroyed when the context is destroyed.
1549 */
1550 const char *dummy_program =
1551 "!!ARBfp1.0\n"
1552 "MOV result.color, fragment.color.primary;\n"
1553 "END\n";
1554 GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
1555 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
1556 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
1557 }
1558
1559 for (s = 0; s < gl_info->limits.point_sprite_units; ++s)
1560 {
1561 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
1562 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
1563 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1564 }
1565
1566 if (gl_info->supported[ARB_PROVOKING_VERTEX])
1567 {
1568 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
1569 }
1570 else if (gl_info->supported[EXT_PROVOKING_VERTEX])
1571 {
1572 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
1573 }
1574
1575#ifdef VBOX_WITH_WDDM
1576 GL_EXTCALL(glChromiumParameteriCR(GL_SHARE_CONTEXT_RESOURCES_CR, GL_TRUE));
1577#endif
1578
1579 LEAVE_GL();
1580
1581 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
1582
1583 TRACE("Created context %p.\n", ret);
1584
1585 return ret;
1586
1587out:
1588 HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
1589 HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
1590 HeapFree(GetProcessHeap(), 0, ret->pshader_const_dirty);
1591 HeapFree(GetProcessHeap(), 0, ret->vshader_const_dirty);
1592 HeapFree(GetProcessHeap(), 0, ret);
1593 return NULL;
1594}
1595
1596#ifdef VBOX_WITH_WDDM
1597static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
1598static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage);
1599
1600BOOL context_acquire_context(struct wined3d_context * context, IWineD3DSurface *target, enum ContextUsage usage, BOOL bReValidate)
1601{
1602 IWineD3DDeviceImpl *device = context->device;
1603 struct wined3d_context *current_context = context_get_current();
1604 if (bReValidate)
1605 {
1606 IWineD3DSwapChain *swapchain = NULL;
1607 if (target && SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
1608 context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
1609 IWineD3DSwapChain_Release(swapchain);
1610 }
1611 else {
1612 /* tmp work-around */
1613 context_validate(current_context,
1614 NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
1615 );
1616 }
1617 }
1618 context_setup_target(device, context, target);
1619 context_enter(context);
1620 Assert(context->valid);
1621 if (!context->valid) return FALSE;
1622
1623 if (context != current_context)
1624 {
1625 if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
1626 else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
1627
1628 if (context->vshader_const_dirty)
1629 {
1630 memset(context->vshader_const_dirty, 1,
1631 sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
1632 device->highest_dirty_vs_const = device->d3d_vshader_constantF;
1633 }
1634 if (context->pshader_const_dirty)
1635 {
1636 memset(context->pshader_const_dirty, 1,
1637 sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
1638 device->highest_dirty_ps_const = device->d3d_pshader_constantF;
1639 }
1640 }
1641 else if (context->restore_ctx)
1642 {
1643 if (!pwglMakeCurrent(context->hdc, context->glCtx))
1644 {
1645 DWORD err = GetLastError();
1646 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
1647 context->hdc, context->glCtx, err);
1648 }
1649 }
1650
1651 context_apply_state(context, device, usage);
1652
1653 return TRUE;
1654}
1655
1656struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
1657 const struct wined3d_format_desc *ds_format_desc)
1658{
1659 UINT i;
1660 DWORD tid = GetCurrentThreadId();
1661 struct wined3d_context *context = NULL;
1662
1663#ifdef DEBUG_misha
1664 Assert(0);
1665#endif
1666
1667 for(i = 0 ; i < device->numContexts ; i ++)
1668 {
1669 if(device->contexts[i]->tid == tid) {
1670 context = device->contexts[i];
1671 break;
1672 }
1673 }
1674
1675 if (!context)
1676 {
1677 Assert(!device->NumberOfSwapChains);
1678 context = context_create(swapchain, target, ds_format_desc);
1679 }
1680 else
1681 {
1682 if(!context_acquire_context(context, (IWineD3DSurface*)target, CTXUSAGE_RESOURCELOAD, TRUE))
1683 {
1684 ERR("Failed to acquire the context.\n");
1685 Assert(0);
1686 Assert(!context->valid);
1687 context = NULL;
1688 }
1689 else
1690 {
1691 Assert(context->valid);
1692 }
1693 }
1694
1695 return context;
1696}
1697#endif
1698
1699/*****************************************************************************
1700 * context_destroy
1701 *
1702 * Destroys a wined3d context
1703 *
1704 * Params:
1705 * This: Device to activate the context for
1706 * context: Context to destroy
1707 *
1708 *****************************************************************************/
1709void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context)
1710{
1711 BOOL destroy;
1712
1713 TRACE("Destroying ctx %p\n", context);
1714
1715 if (context->tid == GetCurrentThreadId() || !context->current)
1716 {
1717 context_destroy_gl_resources(context);
1718 TlsSetValue(wined3d_context_tls_idx, NULL);
1719 destroy = TRUE;
1720 }
1721 else
1722 {
1723 context->destroyed = 1;
1724 destroy = FALSE;
1725 }
1726
1727 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
1728 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
1729 device_context_remove(This, context);
1730 if (destroy) HeapFree(GetProcessHeap(), 0, context);
1731}
1732
1733/* GL locking is done by the caller */
1734static inline void set_blit_dimension(UINT width, UINT height) {
1735 glMatrixMode(GL_PROJECTION);
1736 checkGLcall("glMatrixMode(GL_PROJECTION)");
1737 glLoadIdentity();
1738 checkGLcall("glLoadIdentity()");
1739 glOrtho(0, width, height, 0, 0.0, -1.0);
1740 checkGLcall("glOrtho");
1741 glViewport(0, 0, width, height);
1742 checkGLcall("glViewport");
1743}
1744
1745/*****************************************************************************
1746 * SetupForBlit
1747 *
1748 * Sets up a context for DirectDraw blitting.
1749 * All texture units are disabled, texture unit 0 is set as current unit
1750 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1751 * color writing enabled for all channels
1752 * register combiners disabled, shaders disabled
1753 * world matrix is set to identity, texture matrix 0 too
1754 * projection matrix is setup for drawing screen coordinates
1755 *
1756 * Params:
1757 * This: Device to activate the context for
1758 * context: Context to setup
1759 *
1760 *****************************************************************************/
1761/* Context activation is done by the caller. */
1762static void SetupForBlit(IWineD3DDeviceImpl *This, struct wined3d_context *context)
1763{
1764 int i;
1765 const struct StateEntry *StateTable = This->StateTable;
1766 const struct wined3d_gl_info *gl_info = context->gl_info;
1767 UINT width = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Width;
1768 UINT height = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Height;
1769 DWORD sampler;
1770
1771 TRACE("Setting up context %p for blitting\n", context);
1772 if(context->last_was_blit) {
1773 if(context->blit_w != width || context->blit_h != height) {
1774 ENTER_GL();
1775 set_blit_dimension(width, height);
1776 LEAVE_GL();
1777 context->blit_w = width; context->blit_h = height;
1778 /* No need to dirtify here, the states are still dirtified because they weren't
1779 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1780 * be set
1781 */
1782 }
1783 TRACE("Context is already set up for blitting, nothing to do\n");
1784 return;
1785 }
1786 context->last_was_blit = TRUE;
1787
1788 /* TODO: Use a display list */
1789
1790 /* Disable shaders */
1791 ENTER_GL();
1792 This->shader_backend->shader_select(context, FALSE, FALSE);
1793 LEAVE_GL();
1794
1795 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
1796 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
1797
1798 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1799 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1800 * which can safely be called from here, we only lock once instead locking/unlocking
1801 * after each GL call.
1802 */
1803 ENTER_GL();
1804
1805 /* Disable all textures. The caller can then bind a texture it wants to blit
1806 * from
1807 *
1808 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1809 * function texture unit. No need to care for higher samplers
1810 */
1811 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
1812 {
1813 sampler = This->rev_tex_unit_map[i];
1814 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1815 checkGLcall("glActiveTextureARB");
1816
1817 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1818 {
1819 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1820 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1821 }
1822 glDisable(GL_TEXTURE_3D);
1823 checkGLcall("glDisable GL_TEXTURE_3D");
1824 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1825 {
1826 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1827 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1828 }
1829 glDisable(GL_TEXTURE_2D);
1830 checkGLcall("glDisable GL_TEXTURE_2D");
1831
1832 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1833 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1834
1835 if (sampler != WINED3D_UNMAPPED_STAGE)
1836 {
1837 if (sampler < MAX_TEXTURES) {
1838 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1839 }
1840 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1841 }
1842 }
1843 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
1844 checkGLcall("glActiveTextureARB");
1845
1846 sampler = This->rev_tex_unit_map[0];
1847
1848 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1849 {
1850 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1851 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1852 }
1853 glDisable(GL_TEXTURE_3D);
1854 checkGLcall("glDisable GL_TEXTURE_3D");
1855 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1856 {
1857 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1858 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1859 }
1860 glDisable(GL_TEXTURE_2D);
1861 checkGLcall("glDisable GL_TEXTURE_2D");
1862
1863 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1864
1865 glMatrixMode(GL_TEXTURE);
1866 checkGLcall("glMatrixMode(GL_TEXTURE)");
1867 glLoadIdentity();
1868 checkGLcall("glLoadIdentity()");
1869
1870 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1871 {
1872 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1873 GL_TEXTURE_LOD_BIAS_EXT,
1874 0.0f);
1875 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1876 }
1877
1878 if (sampler != WINED3D_UNMAPPED_STAGE)
1879 {
1880 if (sampler < MAX_TEXTURES) {
1881 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
1882 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1883 }
1884 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1885 }
1886
1887 /* Other misc states */
1888 glDisable(GL_ALPHA_TEST);
1889 checkGLcall("glDisable(GL_ALPHA_TEST)");
1890 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
1891 glDisable(GL_LIGHTING);
1892 checkGLcall("glDisable GL_LIGHTING");
1893 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
1894 glDisable(GL_DEPTH_TEST);
1895 checkGLcall("glDisable GL_DEPTH_TEST");
1896 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
1897 glDisableWINE(GL_FOG);
1898 checkGLcall("glDisable GL_FOG");
1899 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
1900 glDisable(GL_BLEND);
1901 checkGLcall("glDisable GL_BLEND");
1902 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1903 glDisable(GL_CULL_FACE);
1904 checkGLcall("glDisable GL_CULL_FACE");
1905 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
1906 glDisable(GL_STENCIL_TEST);
1907 checkGLcall("glDisable GL_STENCIL_TEST");
1908 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
1909 glDisable(GL_SCISSOR_TEST);
1910 checkGLcall("glDisable GL_SCISSOR_TEST");
1911 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1912 if (gl_info->supported[ARB_POINT_SPRITE])
1913 {
1914 glDisable(GL_POINT_SPRITE_ARB);
1915 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1916 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
1917 }
1918 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1919 checkGLcall("glColorMask");
1920 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE), StateTable);
1921 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1), StateTable);
1922 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2), StateTable);
1923 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3), StateTable);
1924 if (gl_info->supported[EXT_SECONDARY_COLOR])
1925 {
1926 glDisable(GL_COLOR_SUM_EXT);
1927 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
1928 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1929 }
1930
1931 /* Setup transforms */
1932 glMatrixMode(GL_MODELVIEW);
1933 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1934 glLoadIdentity();
1935 checkGLcall("glLoadIdentity()");
1936 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
1937
1938 context->last_was_rhw = TRUE;
1939 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
1940
1941 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1942 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1943 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1944 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1945 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1946 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1947 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1948
1949 set_blit_dimension(width, height);
1950
1951 LEAVE_GL();
1952
1953 context->blit_w = width; context->blit_h = height;
1954 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1955 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
1956
1957
1958 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1959}
1960
1961/*****************************************************************************
1962 * findThreadContextForSwapChain
1963 *
1964 * Searches a swapchain for all contexts and picks one for the thread tid.
1965 * If none can be found the swapchain is requested to create a new context
1966 *
1967 *****************************************************************************/
1968static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid)
1969{
1970 unsigned int i;
1971#ifdef VBOX_WITH_WDDM
1972 IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
1973 for (i = 0; i < device->numContexts; ++i)
1974 {
1975 if (device->contexts[i]->tid == tid)
1976 return device->contexts[i];
1977 }
1978#else
1979 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
1980 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
1981 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
1982 }
1983 }
1984#endif
1985
1986 /* Create a new context for the thread */
1987 return swapchain_create_context_for_thread(swapchain);
1988}
1989
1990/*****************************************************************************
1991 * FindContext
1992 *
1993 * Finds a context for the current render target and thread
1994 *
1995 * Parameters:
1996 * target: Render target to find the context for
1997 * tid: Thread to activate the context for
1998 *
1999 * Returns: The needed context
2000 *
2001 *****************************************************************************/
2002static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
2003{
2004 IWineD3DSwapChain *swapchain = NULL;
2005 struct wined3d_context *current_context = context_get_current();
2006 DWORD tid = GetCurrentThreadId();
2007 struct wined3d_context *context;
2008
2009 if (current_context && current_context->destroyed) current_context = NULL;
2010
2011 if (!target)
2012 {
2013 if (current_context
2014 && current_context->current_rt
2015#ifdef VBOX_WITH_WDDM
2016 && current_context->device == This
2017#else
2018 && current_context->swapchain->device == This
2019#endif
2020 )
2021 {
2022 target = current_context->current_rt;
2023 }
2024 else
2025 {
2026#ifdef VBOX_WITH_WDDM
2027 /* tmp work-around */
2028 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
2029#else
2030 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[0];
2031#endif
2032 if (swapchain->backBuffer) target = swapchain->backBuffer[0];
2033 else target = swapchain->frontBuffer;
2034 }
2035 }
2036
2037 if (current_context && current_context->current_rt == target)
2038 {
2039#ifdef VBOX_WITH_WDDM
2040 IWineD3DSwapChain *swapchain = NULL;
2041 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
2042 context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
2043 IWineD3DSwapChain_Release(swapchain);
2044 }
2045 else {
2046 /* tmp work-around */
2047 context_validate(current_context,
2048 NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
2049 );
2050 }
2051#else
2052 context_validate(current_context);
2053#endif
2054 return current_context;
2055 }
2056
2057 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
2058 TRACE("Rendering onscreen\n");
2059
2060 context = findThreadContextForSwapChain(swapchain, tid);
2061#ifdef VBOX_WITH_WDDM
2062 context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
2063#endif
2064 IWineD3DSwapChain_Release(swapchain);
2065 }
2066 else
2067 {
2068 TRACE("Rendering offscreen\n");
2069
2070 /* Stay with the currently active context. */
2071 if (current_context
2072#ifdef VBOX_WITH_WDDM
2073 && current_context->device == This
2074#else
2075 && current_context->swapchain->device == This
2076#endif
2077 )
2078 {
2079 context = current_context;
2080 }
2081 else
2082 {
2083 /* This may happen if the app jumps straight into offscreen rendering
2084 * Start using the context of the primary swapchain. tid == 0 is no problem
2085 * for findThreadContextForSwapChain.
2086 *
2087 * Can also happen on thread switches - in that case findThreadContextForSwapChain
2088 * is perfect to call. */
2089#ifdef VBOX_WITH_WDDM /* tmp work-around */
2090 context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1], tid);
2091#else
2092 context = findThreadContextForSwapChain(This->swapchains[0], tid);
2093#endif
2094 }
2095#ifdef VBOX_WITH_WDDM
2096 context_validate(context,
2097 NULL //(IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
2098 );
2099#endif
2100 }
2101
2102#ifndef VBOX_WITH_WDDM
2103 context_validate(context);
2104#endif
2105
2106 return context;
2107}
2108
2109/* Context activation is done by the caller. */
2110static void context_apply_draw_buffer(struct wined3d_context *context, BOOL blit)
2111{
2112 const struct wined3d_gl_info *gl_info = context->gl_info;
2113 IWineD3DSurface *rt = context->current_rt;
2114 IWineD3DDeviceImpl *device;
2115
2116 device = ((IWineD3DSurfaceImpl *)rt)->resource.device;
2117 if (!surface_is_offscreen(rt))
2118 {
2119 ENTER_GL();
2120 glDrawBuffer(surface_get_gl_buffer(rt));
2121 checkGLcall("glDrawBuffers()");
2122 LEAVE_GL();
2123 }
2124 else
2125 {
2126 ENTER_GL();
2127 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2128 {
2129 if (!blit)
2130 {
2131 if (gl_info->supported[ARB_DRAW_BUFFERS])
2132 {
2133 GL_EXTCALL(glDrawBuffersARB(gl_info->limits.buffers, device->draw_buffers));
2134 checkGLcall("glDrawBuffers()");
2135 }
2136 else
2137 {
2138 glDrawBuffer(device->draw_buffers[0]);
2139 checkGLcall("glDrawBuffer()");
2140 }
2141 } else {
2142 glDrawBuffer(GL_COLOR_ATTACHMENT0);
2143 checkGLcall("glDrawBuffer()");
2144 }
2145 }
2146 else
2147 {
2148 glDrawBuffer(device->offscreenBuffer);
2149 checkGLcall("glDrawBuffer()");
2150 }
2151 LEAVE_GL();
2152 }
2153}
2154
2155/* GL locking is done by the caller. */
2156void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2157{
2158 glDrawBuffer(buffer);
2159 checkGLcall("glDrawBuffer()");
2160 context->draw_buffer_dirty = TRUE;
2161}
2162
2163static inline void context_set_render_offscreen(struct wined3d_context *context, const struct StateEntry *StateTable,
2164 BOOL offscreen)
2165{
2166 if (context->render_offscreen == offscreen) return;
2167
2168 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
2169 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
2170 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
2171 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
2172 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
2173 context->render_offscreen = offscreen;
2174}
2175
2176static BOOL match_depth_stencil_format(const struct wined3d_format_desc *existing,
2177 const struct wined3d_format_desc *required)
2178{
2179 short existing_depth, existing_stencil, required_depth, required_stencil;
2180
2181 if(existing == required) return TRUE;
2182 if((existing->Flags & WINED3DFMT_FLAG_FLOAT) != (required->Flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2183
2184 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2185 getDepthStencilBits(required, &required_depth, &required_stencil);
2186
2187 if(existing_depth < required_depth) return FALSE;
2188 /* If stencil bits are used the exact amount is required - otherwise wrapping
2189 * won't work correctly */
2190 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2191 return TRUE;
2192}
2193/* The caller provides a context */
2194static void context_validate_onscreen_formats(IWineD3DDeviceImpl *device, struct wined3d_context *context)
2195{
2196 /* Onscreen surfaces are always in a swapchain */
2197 IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *) device->stencilBufferTarget;
2198 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) ((IWineD3DSurfaceImpl *)context->current_rt)->container;
2199
2200 if (!depth_stencil) return;
2201 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format_desc)) return;
2202
2203 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2204 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2205 * format. */
2206 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2207
2208 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2209 IWineD3DSurface_LoadLocation(context->current_rt, SFLAG_INTEXTURE, NULL);
2210 swapchain->render_to_fbo = TRUE;
2211 context_set_render_offscreen(context, device->StateTable, TRUE);
2212}
2213
2214/* Context activation is done by the caller. */
2215static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage)
2216{
2217 const struct StateEntry *state_table = device->StateTable;
2218 unsigned int i;
2219
2220 switch (usage) {
2221 case CTXUSAGE_CLEAR:
2222 case CTXUSAGE_DRAWPRIM:
2223 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2224 if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
2225 ENTER_GL();
2226 context_apply_fbo_state(context);
2227 LEAVE_GL();
2228 }
2229 if (context->draw_buffer_dirty) {
2230 context_apply_draw_buffer(context, FALSE);
2231 context->draw_buffer_dirty = FALSE;
2232 }
2233 break;
2234
2235 case CTXUSAGE_BLIT:
2236 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2237 if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
2238 if (context->render_offscreen)
2239 {
2240 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
2241 surface_internal_preload(context->current_rt, SRGB_RGB);
2242
2243 ENTER_GL();
2244 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
2245 context_attach_surface_fbo(context, GL_FRAMEBUFFER, 0, (IWineD3DSurfaceImpl *)context->current_rt);
2246 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, NULL, FALSE);
2247 LEAVE_GL();
2248 } else {
2249 ENTER_GL();
2250 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
2251 LEAVE_GL();
2252 }
2253 context->draw_buffer_dirty = TRUE;
2254 }
2255 if (context->draw_buffer_dirty) {
2256 context_apply_draw_buffer(context, TRUE);
2257 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
2258 context->draw_buffer_dirty = FALSE;
2259 }
2260 }
2261 break;
2262
2263 default:
2264 break;
2265 }
2266
2267 switch(usage) {
2268 case CTXUSAGE_RESOURCELOAD:
2269 /* This does not require any special states to be set up */
2270 break;
2271
2272 case CTXUSAGE_CLEAR:
2273 if(context->last_was_blit) {
2274 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
2275 }
2276
2277 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
2278 * blending when clearing improves the clearing performance incredibly.
2279 */
2280 ENTER_GL();
2281 glDisable(GL_BLEND);
2282 LEAVE_GL();
2283 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), state_table);
2284
2285 ENTER_GL();
2286 glEnable(GL_SCISSOR_TEST);
2287 checkGLcall("glEnable GL_SCISSOR_TEST");
2288 LEAVE_GL();
2289 context->last_was_blit = FALSE;
2290 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), state_table);
2291 Context_MarkStateDirty(context, STATE_SCISSORRECT, state_table);
2292 break;
2293
2294 case CTXUSAGE_DRAWPRIM:
2295 /* This needs all dirty states applied */
2296 if(context->last_was_blit) {
2297 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
2298 }
2299
2300 IWineD3DDeviceImpl_FindTexUnitMap(device);
2301 device_preload_textures(device);
2302 if (isStateDirty(context, STATE_VDECL))
2303 device_update_stream_info(device, context->gl_info);
2304
2305 ENTER_GL();
2306 for (i = 0; i < context->numDirtyEntries; ++i)
2307 {
2308 DWORD rep = context->dirtyArray[i];
2309 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2310 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2311 context->isStateDirty[idx] &= ~(1 << shift);
2312 state_table[rep].apply(rep, device->stateBlock, context);
2313 }
2314 LEAVE_GL();
2315 context->numDirtyEntries = 0; /* This makes the whole list clean */
2316 context->last_was_blit = FALSE;
2317 break;
2318
2319 case CTXUSAGE_BLIT:
2320 SetupForBlit(device, context);
2321 break;
2322
2323 default:
2324 FIXME("Unexpected context usage requested\n");
2325 }
2326}
2327
2328static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target)
2329{
2330 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2331 const struct StateEntry *StateTable = device->StateTable;
2332
2333 if (!target) return;
2334 else if (context->current_rt == target) return;
2335 render_offscreen = surface_is_offscreen(target);
2336
2337 context_set_render_offscreen(context, StateTable, render_offscreen);
2338
2339 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2340 * the alpha blend state changes with different render target formats. */
2341 if (!context->current_rt)
2342 {
2343 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
2344 }
2345 else
2346 {
2347 const struct wined3d_format_desc *old = ((IWineD3DSurfaceImpl *)context->current_rt)->resource.format_desc;
2348 const struct wined3d_format_desc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
2349
2350 if (old->format != new->format)
2351 {
2352 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2353 if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
2354 || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2355 {
2356 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
2357 }
2358 }
2359
2360 /* When switching away from an offscreen render target, and we're not
2361 * using FBOs, we have to read the drawable into the texture. This is
2362 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2363 * are some things that need care though. PreLoad needs a GL context,
2364 * and FindContext is called before the context is activated. It also
2365 * has to be called with the old rendertarget active, otherwise a
2366 * wrong drawable is read. */
2367 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2368 && old_render_offscreen && context->current_rt != target)
2369 {
2370 BOOL oldInDraw = device->isInDraw;
2371
2372 /* surface_internal_preload() requires a context to load the
2373 * texture, so it will call context_acquire(). Set isInDraw to true
2374 * to signal surface_internal_preload() that it has a context. */
2375
2376 /* FIXME: This is just broken. There's no guarantee whatsoever
2377 * that the currently active context, if any, is appropriate for
2378 * reading back the render target. We should probably call
2379 * context_set_current(context) here and then rely on
2380 * context_acquire() doing the right thing. */
2381 device->isInDraw = TRUE;
2382
2383 /* Read the back buffer of the old drawable into the destination texture. */
2384 if (((IWineD3DSurfaceImpl *)context->current_rt)->texture_name_srgb)
2385 {
2386 surface_internal_preload(context->current_rt, SRGB_BOTH);
2387 }
2388 else
2389 {
2390 surface_internal_preload(context->current_rt, SRGB_RGB);
2391 }
2392
2393 IWineD3DSurface_ModifyLocation(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2394
2395 device->isInDraw = oldInDraw;
2396 }
2397 }
2398
2399 context->draw_buffer_dirty = TRUE;
2400 context->current_rt = target;
2401}
2402
2403/*****************************************************************************
2404 * context_acquire
2405 *
2406 * Finds a rendering context and drawable matching the device and render
2407 * target for the current thread, activates them and puts them into the
2408 * requested state.
2409 *
2410 * Params:
2411 * This: Device to activate the context for
2412 * target: Requested render target
2413 * usage: Prepares the context for blitting, drawing or other actions
2414 *
2415 *****************************************************************************/
2416struct wined3d_context *context_acquire(IWineD3DDeviceImpl *device, IWineD3DSurface *target, enum ContextUsage usage)
2417{
2418 struct wined3d_context *current_context = context_get_current();
2419 struct wined3d_context *context;
2420
2421 TRACE("device %p, target %p, usage %#x.\n", device, target, usage);
2422
2423 context = FindContext(device, target);
2424 context_setup_target(device, context, target);
2425 context_enter(context);
2426 if (!context->valid) return context;
2427
2428 if (context != current_context)
2429 {
2430 if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
2431 else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
2432
2433 if (context->vshader_const_dirty)
2434 {
2435 memset(context->vshader_const_dirty, 1,
2436 sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
2437 device->highest_dirty_vs_const = device->d3d_vshader_constantF;
2438 }
2439 if (context->pshader_const_dirty)
2440 {
2441 memset(context->pshader_const_dirty, 1,
2442 sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
2443 device->highest_dirty_ps_const = device->d3d_pshader_constantF;
2444 }
2445 }
2446 else if (context->restore_ctx)
2447 {
2448 if (!pwglMakeCurrent(context->hdc, context->glCtx))
2449 {
2450 DWORD err = GetLastError();
2451 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2452 context->hdc, context->glCtx, err);
2453 }
2454 }
2455
2456 context_apply_state(context, device, usage);
2457
2458 return context;
2459}
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