VirtualBox

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

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

wddm/3d: fix google earth rendering

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