VirtualBox

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

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

undo undsired change

  • 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 * 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 for(i = 0 ; i < device->numContexts ; i ++)
1664 {
1665 if(device->contexts[i]->tid == tid) {
1666 context = device->contexts[i];
1667 break;
1668 }
1669 }
1670
1671 if (!context)
1672 {
1673 Assert(!device->NumberOfSwapChains);
1674 context = context_create(swapchain, target, ds_format_desc);
1675 }
1676 else
1677 {
1678 if(!context_acquire_context(context, (IWineD3DSurface*)target, CTXUSAGE_RESOURCELOAD, TRUE))
1679 {
1680 ERR("Failed to acquire the context.\n");
1681 Assert(0);
1682 Assert(!context->valid);
1683 context = NULL;
1684 }
1685 else
1686 {
1687 Assert(context->valid);
1688 }
1689 }
1690
1691 return context;
1692}
1693#endif
1694
1695/*****************************************************************************
1696 * context_destroy
1697 *
1698 * Destroys a wined3d context
1699 *
1700 * Params:
1701 * This: Device to activate the context for
1702 * context: Context to destroy
1703 *
1704 *****************************************************************************/
1705void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context)
1706{
1707 BOOL destroy;
1708
1709 TRACE("Destroying ctx %p\n", context);
1710
1711 if (context->tid == GetCurrentThreadId() || !context->current)
1712 {
1713 context_destroy_gl_resources(context);
1714 TlsSetValue(wined3d_context_tls_idx, NULL);
1715 destroy = TRUE;
1716 }
1717 else
1718 {
1719 context->destroyed = 1;
1720 destroy = FALSE;
1721 }
1722
1723 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
1724 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
1725 device_context_remove(This, context);
1726 if (destroy) HeapFree(GetProcessHeap(), 0, context);
1727}
1728
1729/* GL locking is done by the caller */
1730static inline void set_blit_dimension(UINT width, UINT height) {
1731 glMatrixMode(GL_PROJECTION);
1732 checkGLcall("glMatrixMode(GL_PROJECTION)");
1733 glLoadIdentity();
1734 checkGLcall("glLoadIdentity()");
1735 glOrtho(0, width, height, 0, 0.0, -1.0);
1736 checkGLcall("glOrtho");
1737 glViewport(0, 0, width, height);
1738 checkGLcall("glViewport");
1739}
1740
1741/*****************************************************************************
1742 * SetupForBlit
1743 *
1744 * Sets up a context for DirectDraw blitting.
1745 * All texture units are disabled, texture unit 0 is set as current unit
1746 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1747 * color writing enabled for all channels
1748 * register combiners disabled, shaders disabled
1749 * world matrix is set to identity, texture matrix 0 too
1750 * projection matrix is setup for drawing screen coordinates
1751 *
1752 * Params:
1753 * This: Device to activate the context for
1754 * context: Context to setup
1755 *
1756 *****************************************************************************/
1757/* Context activation is done by the caller. */
1758static void SetupForBlit(IWineD3DDeviceImpl *This, struct wined3d_context *context)
1759{
1760 int i;
1761 const struct StateEntry *StateTable = This->StateTable;
1762 const struct wined3d_gl_info *gl_info = context->gl_info;
1763 UINT width = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Width;
1764 UINT height = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Height;
1765 DWORD sampler;
1766
1767 TRACE("Setting up context %p for blitting\n", context);
1768 if(context->last_was_blit) {
1769 if(context->blit_w != width || context->blit_h != height) {
1770 ENTER_GL();
1771 set_blit_dimension(width, height);
1772 LEAVE_GL();
1773 context->blit_w = width; context->blit_h = height;
1774 /* No need to dirtify here, the states are still dirtified because they weren't
1775 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1776 * be set
1777 */
1778 }
1779 TRACE("Context is already set up for blitting, nothing to do\n");
1780 return;
1781 }
1782 context->last_was_blit = TRUE;
1783
1784 /* TODO: Use a display list */
1785
1786 /* Disable shaders */
1787 ENTER_GL();
1788 This->shader_backend->shader_select(context, FALSE, FALSE);
1789 LEAVE_GL();
1790
1791 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
1792 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
1793
1794 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1795 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1796 * which can safely be called from here, we only lock once instead locking/unlocking
1797 * after each GL call.
1798 */
1799 ENTER_GL();
1800
1801 /* Disable all textures. The caller can then bind a texture it wants to blit
1802 * from
1803 *
1804 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1805 * function texture unit. No need to care for higher samplers
1806 */
1807 for (i = gl_info->limits.textures - 1; i > 0 ; --i)
1808 {
1809 sampler = This->rev_tex_unit_map[i];
1810 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1811 checkGLcall("glActiveTextureARB");
1812
1813 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1814 {
1815 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1816 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1817 }
1818 glDisable(GL_TEXTURE_3D);
1819 checkGLcall("glDisable GL_TEXTURE_3D");
1820 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1821 {
1822 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1823 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1824 }
1825 glDisable(GL_TEXTURE_2D);
1826 checkGLcall("glDisable GL_TEXTURE_2D");
1827
1828 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1829 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1830
1831 if (sampler != WINED3D_UNMAPPED_STAGE)
1832 {
1833 if (sampler < MAX_TEXTURES) {
1834 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1835 }
1836 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1837 }
1838 }
1839 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
1840 checkGLcall("glActiveTextureARB");
1841
1842 sampler = This->rev_tex_unit_map[0];
1843
1844 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
1845 {
1846 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1847 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1848 }
1849 glDisable(GL_TEXTURE_3D);
1850 checkGLcall("glDisable GL_TEXTURE_3D");
1851 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
1852 {
1853 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1854 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1855 }
1856 glDisable(GL_TEXTURE_2D);
1857 checkGLcall("glDisable GL_TEXTURE_2D");
1858
1859 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1860
1861 glMatrixMode(GL_TEXTURE);
1862 checkGLcall("glMatrixMode(GL_TEXTURE)");
1863 glLoadIdentity();
1864 checkGLcall("glLoadIdentity()");
1865
1866 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1867 {
1868 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1869 GL_TEXTURE_LOD_BIAS_EXT,
1870 0.0f);
1871 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1872 }
1873
1874 if (sampler != WINED3D_UNMAPPED_STAGE)
1875 {
1876 if (sampler < MAX_TEXTURES) {
1877 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
1878 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1879 }
1880 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1881 }
1882
1883 /* Other misc states */
1884 glDisable(GL_ALPHA_TEST);
1885 checkGLcall("glDisable(GL_ALPHA_TEST)");
1886 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
1887 glDisable(GL_LIGHTING);
1888 checkGLcall("glDisable GL_LIGHTING");
1889 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
1890 glDisable(GL_DEPTH_TEST);
1891 checkGLcall("glDisable GL_DEPTH_TEST");
1892 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
1893 glDisableWINE(GL_FOG);
1894 checkGLcall("glDisable GL_FOG");
1895 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
1896 glDisable(GL_BLEND);
1897 checkGLcall("glDisable GL_BLEND");
1898 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1899 glDisable(GL_CULL_FACE);
1900 checkGLcall("glDisable GL_CULL_FACE");
1901 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
1902 glDisable(GL_STENCIL_TEST);
1903 checkGLcall("glDisable GL_STENCIL_TEST");
1904 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
1905 glDisable(GL_SCISSOR_TEST);
1906 checkGLcall("glDisable GL_SCISSOR_TEST");
1907 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1908 if (gl_info->supported[ARB_POINT_SPRITE])
1909 {
1910 glDisable(GL_POINT_SPRITE_ARB);
1911 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1912 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
1913 }
1914 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1915 checkGLcall("glColorMask");
1916 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE), StateTable);
1917 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1), StateTable);
1918 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2), StateTable);
1919 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3), StateTable);
1920 if (gl_info->supported[EXT_SECONDARY_COLOR])
1921 {
1922 glDisable(GL_COLOR_SUM_EXT);
1923 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
1924 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1925 }
1926
1927 /* Setup transforms */
1928 glMatrixMode(GL_MODELVIEW);
1929 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1930 glLoadIdentity();
1931 checkGLcall("glLoadIdentity()");
1932 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
1933
1934 context->last_was_rhw = TRUE;
1935 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
1936
1937 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1938 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1939 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1940 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1941 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1942 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1943 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1944
1945 set_blit_dimension(width, height);
1946
1947 LEAVE_GL();
1948
1949 context->blit_w = width; context->blit_h = height;
1950 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1951 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
1952
1953
1954 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1955}
1956
1957/*****************************************************************************
1958 * findThreadContextForSwapChain
1959 *
1960 * Searches a swapchain for all contexts and picks one for the thread tid.
1961 * If none can be found the swapchain is requested to create a new context
1962 *
1963 *****************************************************************************/
1964static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid)
1965{
1966 unsigned int i;
1967#ifdef VBOX_WITH_WDDM
1968 IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
1969 for (i = 0; i < device->numContexts; ++i)
1970 {
1971 if (device->contexts[i]->tid == tid)
1972 return device->contexts[i];
1973 }
1974#else
1975 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
1976 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
1977 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
1978 }
1979 }
1980#endif
1981
1982 /* Create a new context for the thread */
1983 return swapchain_create_context_for_thread(swapchain);
1984}
1985
1986/*****************************************************************************
1987 * FindContext
1988 *
1989 * Finds a context for the current render target and thread
1990 *
1991 * Parameters:
1992 * target: Render target to find the context for
1993 * tid: Thread to activate the context for
1994 *
1995 * Returns: The needed context
1996 *
1997 *****************************************************************************/
1998static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
1999{
2000 IWineD3DSwapChain *swapchain = NULL;
2001 struct wined3d_context *current_context = context_get_current();
2002 DWORD tid = GetCurrentThreadId();
2003 struct wined3d_context *context;
2004
2005 if (current_context && current_context->destroyed) current_context = NULL;
2006
2007 if (!target)
2008 {
2009 if (current_context
2010 && current_context->current_rt
2011#ifdef VBOX_WITH_WDDM
2012 && current_context->device == This
2013#else
2014 && current_context->swapchain->device == This
2015#endif
2016 )
2017 {
2018 target = current_context->current_rt;
2019 }
2020 else
2021 {
2022#ifdef VBOX_WITH_WDDM
2023 /* tmp work-around */
2024 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
2025#else
2026 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[0];
2027#endif
2028 if (swapchain->backBuffer) target = swapchain->backBuffer[0];
2029 else target = swapchain->frontBuffer;
2030 }
2031 }
2032
2033 if (current_context && current_context->current_rt == target)
2034 {
2035#ifdef VBOX_WITH_WDDM
2036 IWineD3DSwapChain *swapchain = NULL;
2037 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
2038 context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
2039 IWineD3DSwapChain_Release(swapchain);
2040 }
2041 else {
2042 /* tmp work-around */
2043 context_validate(current_context,
2044 NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
2045 );
2046 }
2047#else
2048 context_validate(current_context);
2049#endif
2050 return current_context;
2051 }
2052
2053 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
2054 TRACE("Rendering onscreen\n");
2055
2056 context = findThreadContextForSwapChain(swapchain, tid);
2057#ifdef VBOX_WITH_WDDM
2058 context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
2059#endif
2060 IWineD3DSwapChain_Release(swapchain);
2061 }
2062 else
2063 {
2064 TRACE("Rendering offscreen\n");
2065
2066 /* Stay with the currently active context. */
2067 if (current_context
2068#ifdef VBOX_WITH_WDDM
2069 && current_context->device == This
2070#else
2071 && current_context->swapchain->device == This
2072#endif
2073 )
2074 {
2075 context = current_context;
2076 }
2077 else
2078 {
2079 /* This may happen if the app jumps straight into offscreen rendering
2080 * Start using the context of the primary swapchain. tid == 0 is no problem
2081 * for findThreadContextForSwapChain.
2082 *
2083 * Can also happen on thread switches - in that case findThreadContextForSwapChain
2084 * is perfect to call. */
2085#ifdef VBOX_WITH_WDDM /* tmp work-around */
2086 context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1], tid);
2087#else
2088 context = findThreadContextForSwapChain(This->swapchains[0], tid);
2089#endif
2090 }
2091#ifdef VBOX_WITH_WDDM
2092 context_validate(context,
2093 NULL //(IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
2094 );
2095#endif
2096 }
2097
2098#ifndef VBOX_WITH_WDDM
2099 context_validate(context);
2100#endif
2101
2102 return context;
2103}
2104
2105/* Context activation is done by the caller. */
2106static void context_apply_draw_buffer(struct wined3d_context *context, BOOL blit)
2107{
2108 const struct wined3d_gl_info *gl_info = context->gl_info;
2109 IWineD3DSurface *rt = context->current_rt;
2110 IWineD3DDeviceImpl *device;
2111
2112 device = ((IWineD3DSurfaceImpl *)rt)->resource.device;
2113 if (!surface_is_offscreen(rt))
2114 {
2115 ENTER_GL();
2116 glDrawBuffer(surface_get_gl_buffer(rt));
2117 checkGLcall("glDrawBuffers()");
2118 LEAVE_GL();
2119 }
2120 else
2121 {
2122 ENTER_GL();
2123 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
2124 {
2125 if (!blit)
2126 {
2127 if (gl_info->supported[ARB_DRAW_BUFFERS])
2128 {
2129 GL_EXTCALL(glDrawBuffersARB(gl_info->limits.buffers, device->draw_buffers));
2130 checkGLcall("glDrawBuffers()");
2131 }
2132 else
2133 {
2134 glDrawBuffer(device->draw_buffers[0]);
2135 checkGLcall("glDrawBuffer()");
2136 }
2137 } else {
2138 glDrawBuffer(GL_COLOR_ATTACHMENT0);
2139 checkGLcall("glDrawBuffer()");
2140 }
2141 }
2142 else
2143 {
2144 glDrawBuffer(device->offscreenBuffer);
2145 checkGLcall("glDrawBuffer()");
2146 }
2147 LEAVE_GL();
2148 }
2149}
2150
2151/* GL locking is done by the caller. */
2152void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
2153{
2154 glDrawBuffer(buffer);
2155 checkGLcall("glDrawBuffer()");
2156 context->draw_buffer_dirty = TRUE;
2157}
2158
2159static inline void context_set_render_offscreen(struct wined3d_context *context, const struct StateEntry *StateTable,
2160 BOOL offscreen)
2161{
2162 if (context->render_offscreen == offscreen) return;
2163
2164 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
2165 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
2166 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
2167 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
2168 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
2169 context->render_offscreen = offscreen;
2170}
2171
2172static BOOL match_depth_stencil_format(const struct wined3d_format_desc *existing,
2173 const struct wined3d_format_desc *required)
2174{
2175 short existing_depth, existing_stencil, required_depth, required_stencil;
2176
2177 if(existing == required) return TRUE;
2178 if((existing->Flags & WINED3DFMT_FLAG_FLOAT) != (required->Flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
2179
2180 getDepthStencilBits(existing, &existing_depth, &existing_stencil);
2181 getDepthStencilBits(required, &required_depth, &required_stencil);
2182
2183 if(existing_depth < required_depth) return FALSE;
2184 /* If stencil bits are used the exact amount is required - otherwise wrapping
2185 * won't work correctly */
2186 if(required_stencil && required_stencil != existing_stencil) return FALSE;
2187 return TRUE;
2188}
2189/* The caller provides a context */
2190static void context_validate_onscreen_formats(IWineD3DDeviceImpl *device, struct wined3d_context *context)
2191{
2192 /* Onscreen surfaces are always in a swapchain */
2193 IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *) device->stencilBufferTarget;
2194 IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) ((IWineD3DSurfaceImpl *)context->current_rt)->container;
2195
2196 if (!depth_stencil) return;
2197 if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format_desc)) return;
2198
2199 /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
2200 * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
2201 * format. */
2202 WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
2203
2204 /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
2205 IWineD3DSurface_LoadLocation(context->current_rt, SFLAG_INTEXTURE, NULL);
2206 swapchain->render_to_fbo = TRUE;
2207 context_set_render_offscreen(context, device->StateTable, TRUE);
2208}
2209
2210/* Context activation is done by the caller. */
2211static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage)
2212{
2213 const struct StateEntry *state_table = device->StateTable;
2214 unsigned int i;
2215
2216 switch (usage) {
2217 case CTXUSAGE_CLEAR:
2218 case CTXUSAGE_DRAWPRIM:
2219 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2220 if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
2221 ENTER_GL();
2222 context_apply_fbo_state(context);
2223 LEAVE_GL();
2224 }
2225 if (context->draw_buffer_dirty) {
2226 context_apply_draw_buffer(context, FALSE);
2227 context->draw_buffer_dirty = FALSE;
2228 }
2229 break;
2230
2231 case CTXUSAGE_BLIT:
2232 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2233 if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
2234 if (context->render_offscreen)
2235 {
2236 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
2237 surface_internal_preload(context->current_rt, SRGB_RGB);
2238
2239 ENTER_GL();
2240 context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
2241 context_attach_surface_fbo(context, GL_FRAMEBUFFER, 0, (IWineD3DSurfaceImpl *)context->current_rt);
2242 context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, NULL, FALSE);
2243 LEAVE_GL();
2244 } else {
2245 ENTER_GL();
2246 context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
2247 LEAVE_GL();
2248 }
2249 context->draw_buffer_dirty = TRUE;
2250 }
2251 if (context->draw_buffer_dirty) {
2252 context_apply_draw_buffer(context, TRUE);
2253 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
2254 context->draw_buffer_dirty = FALSE;
2255 }
2256 }
2257 break;
2258
2259 default:
2260 break;
2261 }
2262
2263 switch(usage) {
2264 case CTXUSAGE_RESOURCELOAD:
2265 /* This does not require any special states to be set up */
2266 break;
2267
2268 case CTXUSAGE_CLEAR:
2269 if(context->last_was_blit) {
2270 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
2271 }
2272
2273 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
2274 * blending when clearing improves the clearing performance incredibly.
2275 */
2276 ENTER_GL();
2277 glDisable(GL_BLEND);
2278 LEAVE_GL();
2279 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), state_table);
2280
2281 ENTER_GL();
2282 glEnable(GL_SCISSOR_TEST);
2283 checkGLcall("glEnable GL_SCISSOR_TEST");
2284 LEAVE_GL();
2285 context->last_was_blit = FALSE;
2286 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), state_table);
2287 Context_MarkStateDirty(context, STATE_SCISSORRECT, state_table);
2288 break;
2289
2290 case CTXUSAGE_DRAWPRIM:
2291 /* This needs all dirty states applied */
2292 if(context->last_was_blit) {
2293 device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
2294 }
2295
2296 IWineD3DDeviceImpl_FindTexUnitMap(device);
2297 device_preload_textures(device);
2298 if (isStateDirty(context, STATE_VDECL))
2299 device_update_stream_info(device, context->gl_info);
2300
2301 ENTER_GL();
2302 for (i = 0; i < context->numDirtyEntries; ++i)
2303 {
2304 DWORD rep = context->dirtyArray[i];
2305 DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
2306 BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
2307 context->isStateDirty[idx] &= ~(1 << shift);
2308 state_table[rep].apply(rep, device->stateBlock, context);
2309 }
2310 LEAVE_GL();
2311 context->numDirtyEntries = 0; /* This makes the whole list clean */
2312 context->last_was_blit = FALSE;
2313 break;
2314
2315 case CTXUSAGE_BLIT:
2316 SetupForBlit(device, context);
2317 break;
2318
2319 default:
2320 FIXME("Unexpected context usage requested\n");
2321 }
2322}
2323
2324static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target)
2325{
2326 BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
2327 const struct StateEntry *StateTable = device->StateTable;
2328
2329 if (!target) return;
2330 else if (context->current_rt == target) return;
2331 render_offscreen = surface_is_offscreen(target);
2332
2333 context_set_render_offscreen(context, StateTable, render_offscreen);
2334
2335 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
2336 * the alpha blend state changes with different render target formats. */
2337 if (!context->current_rt)
2338 {
2339 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
2340 }
2341 else
2342 {
2343 const struct wined3d_format_desc *old = ((IWineD3DSurfaceImpl *)context->current_rt)->resource.format_desc;
2344 const struct wined3d_format_desc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
2345
2346 if (old->format != new->format)
2347 {
2348 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
2349 if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
2350 || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
2351 {
2352 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
2353 }
2354 }
2355
2356 /* When switching away from an offscreen render target, and we're not
2357 * using FBOs, we have to read the drawable into the texture. This is
2358 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
2359 * are some things that need care though. PreLoad needs a GL context,
2360 * and FindContext is called before the context is activated. It also
2361 * has to be called with the old rendertarget active, otherwise a
2362 * wrong drawable is read. */
2363 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
2364 && old_render_offscreen && context->current_rt != target)
2365 {
2366 BOOL oldInDraw = device->isInDraw;
2367
2368 /* surface_internal_preload() requires a context to load the
2369 * texture, so it will call context_acquire(). Set isInDraw to true
2370 * to signal surface_internal_preload() that it has a context. */
2371
2372 /* FIXME: This is just broken. There's no guarantee whatsoever
2373 * that the currently active context, if any, is appropriate for
2374 * reading back the render target. We should probably call
2375 * context_set_current(context) here and then rely on
2376 * context_acquire() doing the right thing. */
2377 device->isInDraw = TRUE;
2378
2379 /* Read the back buffer of the old drawable into the destination texture. */
2380 if (((IWineD3DSurfaceImpl *)context->current_rt)->texture_name_srgb)
2381 {
2382 surface_internal_preload(context->current_rt, SRGB_BOTH);
2383 }
2384 else
2385 {
2386 surface_internal_preload(context->current_rt, SRGB_RGB);
2387 }
2388
2389 IWineD3DSurface_ModifyLocation(context->current_rt, SFLAG_INDRAWABLE, FALSE);
2390
2391 device->isInDraw = oldInDraw;
2392 }
2393 }
2394
2395 context->draw_buffer_dirty = TRUE;
2396 context->current_rt = target;
2397}
2398
2399/*****************************************************************************
2400 * context_acquire
2401 *
2402 * Finds a rendering context and drawable matching the device and render
2403 * target for the current thread, activates them and puts them into the
2404 * requested state.
2405 *
2406 * Params:
2407 * This: Device to activate the context for
2408 * target: Requested render target
2409 * usage: Prepares the context for blitting, drawing or other actions
2410 *
2411 *****************************************************************************/
2412struct wined3d_context *context_acquire(IWineD3DDeviceImpl *device, IWineD3DSurface *target, enum ContextUsage usage)
2413{
2414 struct wined3d_context *current_context = context_get_current();
2415 struct wined3d_context *context;
2416
2417 TRACE("device %p, target %p, usage %#x.\n", device, target, usage);
2418
2419 context = FindContext(device, target);
2420 context_setup_target(device, context, target);
2421 context_enter(context);
2422 if (!context->valid) return context;
2423
2424 if (context != current_context)
2425 {
2426 if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
2427 else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
2428
2429 if (context->vshader_const_dirty)
2430 {
2431 memset(context->vshader_const_dirty, 1,
2432 sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
2433 device->highest_dirty_vs_const = device->d3d_vshader_constantF;
2434 }
2435 if (context->pshader_const_dirty)
2436 {
2437 memset(context->pshader_const_dirty, 1,
2438 sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
2439 device->highest_dirty_ps_const = device->d3d_pshader_constantF;
2440 }
2441 }
2442 else if (context->restore_ctx)
2443 {
2444 if (!pwglMakeCurrent(context->hdc, context->glCtx))
2445 {
2446 DWORD err = GetLastError();
2447 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2448 context->hdc, context->glCtx, err);
2449 }
2450 }
2451
2452 context_apply_state(context, device, usage);
2453
2454 return context;
2455}
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