push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / wined3d / context.c
blob02215c8af833e0bba1ade01bdcded437681c0e4a
1 /*
2 * Context and render target management in wined3d
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include <stdio.h>
23 #ifdef HAVE_FLOAT_H
24 # include <float.h>
25 #endif
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
30 #define GLINFO_LOCATION This->adapter->gl_info
32 /* The last used device.
34 * If the application creates multiple devices and switches between them, ActivateContext has to
35 * change the opengl context. This flag allows to keep track which device is active
37 static IWineD3DDeviceImpl *last_device;
39 /* FBO helper functions */
41 /* GL locking is done by the caller */
42 void context_bind_fbo(IWineD3DDevice *iface, GLenum target, GLuint *fbo)
44 const IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
46 if (!*fbo)
48 GL_EXTCALL(glGenFramebuffersEXT(1, fbo));
49 checkGLcall("glGenFramebuffersEXT()");
50 TRACE("Created FBO %d\n", *fbo);
53 GL_EXTCALL(glBindFramebufferEXT(target, *fbo));
54 checkGLcall("glBindFramebuffer()");
57 /* GL locking is done by the caller */
58 static void context_clean_fbo_attachments(IWineD3DDeviceImpl *This)
60 unsigned int i;
62 for (i = 0; i < GL_LIMITS(buffers); ++i)
64 GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, 0, 0));
65 checkGLcall("glFramebufferTexture2D()");
67 GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
68 checkGLcall("glFramebufferTexture2D()");
71 /* GL locking is done by the caller */
72 static void context_destroy_fbo(IWineD3DDeviceImpl *This, const GLuint *fbo)
74 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo));
75 checkGLcall("glBindFramebuffer()");
77 context_clean_fbo_attachments(This);
79 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
80 checkGLcall("glBindFramebuffer()");
81 GL_EXTCALL(glDeleteFramebuffersEXT(1, fbo));
82 checkGLcall("glDeleteFramebuffers()");
85 /* GL locking is done by the caller */
86 static void context_apply_attachment_filter_states(IWineD3DDevice *iface, IWineD3DSurface *surface, BOOL force_preload)
88 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
89 const IWineD3DSurfaceImpl *surface_impl = (IWineD3DSurfaceImpl *)surface;
90 IWineD3DBaseTextureImpl *texture_impl;
91 BOOL update_minfilter = FALSE;
92 BOOL update_magfilter = FALSE;
94 /* Update base texture states array */
95 if (SUCCEEDED(IWineD3DSurface_GetContainer(surface, &IID_IWineD3DBaseTexture, (void **)&texture_impl)))
97 if (texture_impl->baseTexture.states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_POINT
98 || texture_impl->baseTexture.states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_NONE)
100 texture_impl->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
101 texture_impl->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
102 update_minfilter = TRUE;
105 if (texture_impl->baseTexture.states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_POINT)
107 texture_impl->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
108 update_magfilter = TRUE;
111 if (texture_impl->baseTexture.bindCount)
113 WARN("Render targets should not be bound to a sampler\n");
114 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_SAMPLER(texture_impl->baseTexture.sampler));
117 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture_impl);
120 if (update_minfilter || update_magfilter || force_preload)
122 GLenum target, bind_target;
123 GLint old_binding;
125 target = surface_impl->glDescription.target;
126 if (target == GL_TEXTURE_2D)
128 bind_target = GL_TEXTURE_2D;
129 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
130 } else if (target == GL_TEXTURE_RECTANGLE_ARB) {
131 bind_target = GL_TEXTURE_RECTANGLE_ARB;
132 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
133 } else {
134 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
135 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB, &old_binding);
138 surface_internal_preload(surface, SRGB_RGB);
140 glBindTexture(bind_target, surface_impl->glDescription.textureName);
141 if (update_minfilter) glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
142 if (update_magfilter) glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
143 glBindTexture(bind_target, old_binding);
146 checkGLcall("apply_attachment_filter_states()");
149 /* TODO: Handle stencil attachments */
150 /* GL locking is done by the caller */
151 void context_attach_depth_stencil_fbo(IWineD3DDeviceImpl *This, GLenum fbo_target, IWineD3DSurface *depth_stencil, BOOL use_render_buffer)
153 IWineD3DSurfaceImpl *depth_stencil_impl = (IWineD3DSurfaceImpl *)depth_stencil;
155 TRACE("Attach depth stencil %p\n", depth_stencil);
157 if (depth_stencil)
159 if (use_render_buffer && depth_stencil_impl->current_renderbuffer)
161 GL_EXTCALL(glFramebufferRenderbufferEXT(fbo_target, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_stencil_impl->current_renderbuffer->id));
162 checkGLcall("glFramebufferRenderbufferEXT()");
163 } else {
164 context_apply_attachment_filter_states((IWineD3DDevice *)This, depth_stencil, TRUE);
166 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_DEPTH_ATTACHMENT_EXT, depth_stencil_impl->glDescription.target,
167 depth_stencil_impl->glDescription.textureName, depth_stencil_impl->glDescription.level));
168 checkGLcall("glFramebufferTexture2DEXT()");
170 } else {
171 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
172 checkGLcall("glFramebufferTexture2DEXT()");
176 /* GL locking is done by the caller */
177 void context_attach_surface_fbo(IWineD3DDeviceImpl *This, GLenum fbo_target, DWORD idx, IWineD3DSurface *surface)
179 const IWineD3DSurfaceImpl *surface_impl = (IWineD3DSurfaceImpl *)surface;
181 TRACE("Attach surface %p to %u\n", surface, idx);
183 if (surface)
185 context_apply_attachment_filter_states((IWineD3DDevice *)This, surface, TRUE);
187 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_COLOR_ATTACHMENT0_EXT + idx, surface_impl->glDescription.target,
188 surface_impl->glDescription.textureName, surface_impl->glDescription.level));
189 checkGLcall("glFramebufferTexture2DEXT()");
190 } else {
191 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_COLOR_ATTACHMENT0_EXT + idx, GL_TEXTURE_2D, 0, 0));
192 checkGLcall("glFramebufferTexture2DEXT()");
196 /* GL locking is done by the caller */
197 static void context_check_fbo_status(IWineD3DDevice *iface)
199 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
200 GLenum status;
202 status = GL_EXTCALL(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
203 if (status == GL_FRAMEBUFFER_COMPLETE_EXT)
205 TRACE("FBO complete\n");
206 } else {
207 IWineD3DSurfaceImpl *attachment;
208 unsigned int i;
209 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
211 /* Dump the FBO attachments */
212 for (i = 0; i < GL_LIMITS(buffers); ++i)
214 attachment = (IWineD3DSurfaceImpl *)This->activeContext->current_fbo->render_targets[i];
215 if (attachment)
217 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
218 i, attachment, debug_d3dformat(attachment->resource.format_desc->format),
219 attachment->pow2Width, attachment->pow2Height);
222 attachment = (IWineD3DSurfaceImpl *)This->activeContext->current_fbo->depth_stencil;
223 if (attachment)
225 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
226 attachment, debug_d3dformat(attachment->resource.format_desc->format),
227 attachment->pow2Width, attachment->pow2Height);
232 static struct fbo_entry *context_create_fbo_entry(IWineD3DDevice *iface)
234 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
235 struct fbo_entry *entry;
237 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
238 entry->render_targets = HeapAlloc(GetProcessHeap(), 0, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
239 memcpy(entry->render_targets, This->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
240 entry->depth_stencil = This->stencilBufferTarget;
241 entry->attached = FALSE;
242 entry->id = 0;
244 return entry;
247 /* GL locking is done by the caller */
248 static void context_reuse_fbo_entry(IWineD3DDevice *iface, struct fbo_entry *entry)
250 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
252 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, entry->id));
253 checkGLcall("glBindFramebuffer()");
254 context_clean_fbo_attachments(This);
256 memcpy(entry->render_targets, This->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
257 entry->depth_stencil = This->stencilBufferTarget;
258 entry->attached = FALSE;
261 /* GL locking is done by the caller */
262 static void context_destroy_fbo_entry(IWineD3DDeviceImpl *This, struct fbo_entry *entry)
264 if (entry->id)
266 TRACE("Destroy FBO %d\n", entry->id);
267 context_destroy_fbo(This, &entry->id);
269 list_remove(&entry->entry);
270 HeapFree(GetProcessHeap(), 0, entry->render_targets);
271 HeapFree(GetProcessHeap(), 0, entry);
275 /* GL locking is done by the caller */
276 static struct fbo_entry *context_find_fbo_entry(IWineD3DDevice *iface, WineD3DContext *context)
278 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
279 struct fbo_entry *entry;
281 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
283 if (!memcmp(entry->render_targets, This->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets))
284 && entry->depth_stencil == This->stencilBufferTarget)
286 list_remove(&entry->entry);
287 list_add_head(&context->fbo_list, &entry->entry);
288 return entry;
292 if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
294 entry = context_create_fbo_entry(iface);
295 list_add_head(&context->fbo_list, &entry->entry);
296 ++context->fbo_entry_count;
298 else
300 entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
301 context_reuse_fbo_entry(iface, entry);
302 list_remove(&entry->entry);
303 list_add_head(&context->fbo_list, &entry->entry);
306 return entry;
309 /* GL locking is done by the caller */
310 static void context_apply_fbo_entry(IWineD3DDevice *iface, struct fbo_entry *entry)
312 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
313 unsigned int i;
315 context_bind_fbo(iface, GL_FRAMEBUFFER_EXT, &entry->id);
317 if (!entry->attached)
319 /* Apply render targets */
320 for (i = 0; i < GL_LIMITS(buffers); ++i)
322 IWineD3DSurface *render_target = This->render_targets[i];
323 context_attach_surface_fbo(This, GL_FRAMEBUFFER_EXT, i, render_target);
326 /* Apply depth targets */
327 if (This->stencilBufferTarget) {
328 unsigned int w = ((IWineD3DSurfaceImpl *)This->render_targets[0])->pow2Width;
329 unsigned int h = ((IWineD3DSurfaceImpl *)This->render_targets[0])->pow2Height;
331 surface_set_compatible_renderbuffer(This->stencilBufferTarget, w, h);
333 context_attach_depth_stencil_fbo(This, GL_FRAMEBUFFER_EXT, This->stencilBufferTarget, TRUE);
335 entry->attached = TRUE;
336 } else {
337 for (i = 0; i < GL_LIMITS(buffers); ++i)
339 if (This->render_targets[i])
340 context_apply_attachment_filter_states(iface, This->render_targets[i], FALSE);
342 if (This->stencilBufferTarget)
343 context_apply_attachment_filter_states(iface, This->stencilBufferTarget, FALSE);
346 for (i = 0; i < GL_LIMITS(buffers); ++i)
348 if (This->render_targets[i])
349 This->draw_buffers[i] = GL_COLOR_ATTACHMENT0_EXT + i;
350 else
351 This->draw_buffers[i] = GL_NONE;
355 /* GL locking is done by the caller */
356 static void context_apply_fbo_state(IWineD3DDevice *iface)
358 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
359 WineD3DContext *context = This->activeContext;
361 if (This->render_offscreen)
363 context->current_fbo = context_find_fbo_entry(iface, context);
364 context_apply_fbo_entry(iface, context->current_fbo);
365 } else {
366 context->current_fbo = NULL;
367 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
370 context_check_fbo_status(iface);
373 void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type)
375 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
376 UINT i;
378 switch(type)
380 case WINED3DRTYPE_SURFACE:
382 for (i = 0; i < This->numContexts; ++i)
384 struct fbo_entry *entry, *entry2;
386 ENTER_GL();
388 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->contexts[i]->fbo_list, struct fbo_entry, entry)
390 BOOL destroyed = FALSE;
391 UINT j;
393 for (j = 0; !destroyed && j < GL_LIMITS(buffers); ++j)
395 if (entry->render_targets[j] == (IWineD3DSurface *)resource)
397 context_destroy_fbo_entry(This, entry);
398 destroyed = TRUE;
402 if (!destroyed && entry->depth_stencil == (IWineD3DSurface *)resource)
403 context_destroy_fbo_entry(This, entry);
406 LEAVE_GL();
409 break;
412 default:
413 break;
417 /*****************************************************************************
418 * Context_MarkStateDirty
420 * Marks a state in a context dirty. Only one context, opposed to
421 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
422 * contexts
424 * Params:
425 * context: Context to mark the state dirty in
426 * state: State to mark dirty
427 * StateTable: Pointer to the state table in use(for state grouping)
429 *****************************************************************************/
430 static void Context_MarkStateDirty(WineD3DContext *context, DWORD state, const struct StateEntry *StateTable) {
431 DWORD rep = StateTable[state].representative;
432 DWORD idx;
433 BYTE shift;
435 if(!rep || isStateDirty(context, rep)) return;
437 context->dirtyArray[context->numDirtyEntries++] = rep;
438 idx = rep >> 5;
439 shift = rep & 0x1f;
440 context->isStateDirty[idx] |= (1 << shift);
443 /*****************************************************************************
444 * AddContextToArray
446 * Adds a context to the context array. Helper function for CreateContext
448 * This method is not called in performance-critical code paths, only when a
449 * new render target or swapchain is created. Thus performance is not an issue
450 * here.
452 * Params:
453 * This: Device to add the context for
454 * hdc: device context
455 * glCtx: WGL context to add
456 * pbuffer: optional pbuffer used with this context
458 *****************************************************************************/
459 static WineD3DContext *AddContextToArray(IWineD3DDeviceImpl *This, HWND win_handle, HDC hdc, HGLRC glCtx, HPBUFFERARB pbuffer) {
460 WineD3DContext **oldArray = This->contexts;
461 DWORD state;
463 This->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->contexts) * (This->numContexts + 1));
464 if(This->contexts == NULL) {
465 ERR("Unable to grow the context array\n");
466 This->contexts = oldArray;
467 return NULL;
469 if(oldArray) {
470 memcpy(This->contexts, oldArray, sizeof(*This->contexts) * This->numContexts);
473 This->contexts[This->numContexts] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WineD3DContext));
474 if(This->contexts[This->numContexts] == NULL) {
475 ERR("Unable to allocate a new context\n");
476 HeapFree(GetProcessHeap(), 0, This->contexts);
477 This->contexts = oldArray;
478 return NULL;
481 This->contexts[This->numContexts]->hdc = hdc;
482 This->contexts[This->numContexts]->glCtx = glCtx;
483 This->contexts[This->numContexts]->pbuffer = pbuffer;
484 This->contexts[This->numContexts]->win_handle = win_handle;
485 HeapFree(GetProcessHeap(), 0, oldArray);
487 /* Mark all states dirty to force a proper initialization of the states on the first use of the context
489 for(state = 0; state <= STATE_HIGHEST; state++) {
490 Context_MarkStateDirty(This->contexts[This->numContexts], state, This->StateTable);
493 This->numContexts++;
494 TRACE("Created context %p\n", This->contexts[This->numContexts - 1]);
495 return This->contexts[This->numContexts - 1];
498 /* This function takes care of WineD3D pixel format selection. */
499 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl *This, HDC hdc,
500 const struct GlPixelFormatDesc *color_format_desc, const struct GlPixelFormatDesc *ds_format_desc,
501 BOOL auxBuffers, int numSamples, BOOL pbuffer, BOOL findCompatible)
503 int iPixelFormat=0;
504 unsigned int matchtry;
505 short redBits, greenBits, blueBits, alphaBits, colorBits;
506 short depthBits=0, stencilBits=0;
508 struct match_type {
509 BOOL require_aux;
510 BOOL exact_alpha;
511 BOOL exact_color;
512 } matches[] = {
513 /* First, try without alpha match buffers. MacOS supports aux buffers only
514 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
515 * Then try without aux buffers - this is the most common cause for not
516 * finding a pixel format. Also some drivers(the open source ones)
517 * only offer 32 bit ARB pixel formats. First try without an exact alpha
518 * match, then try without an exact alpha and color match.
520 { TRUE, TRUE, TRUE },
521 { TRUE, FALSE, TRUE },
522 { FALSE, TRUE, TRUE },
523 { FALSE, FALSE, TRUE },
524 { TRUE, FALSE, FALSE },
525 { FALSE, FALSE, FALSE },
528 int i = 0;
529 int nCfgs = This->adapter->nCfgs;
531 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, pbuffer=%d, findCompatible=%d\n",
532 debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format),
533 auxBuffers, numSamples, pbuffer, findCompatible);
535 if (!getColorBits(color_format_desc, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
537 ERR("Unable to get color bits for format %s (%#x)!\n",
538 debug_d3dformat(color_format_desc->format), color_format_desc->format);
539 return 0;
542 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
543 * You are able to add a depth + stencil surface at a later stage when you need it.
544 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
545 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
546 * context, need torecreate shaders, textures and other resources.
548 * The context manager already takes care of the state problem and for the other tasks code from Reset
549 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
550 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
551 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
552 * issue needs to be fixed. */
553 if (ds_format_desc->format != WINED3DFMT_D24S8)
555 FIXME("Add OpenGL context recreation support to SetDepthStencilSurface\n");
556 ds_format_desc = getFormatDescEntry(WINED3DFMT_D24S8, &This->adapter->gl_info);
559 getDepthStencilBits(ds_format_desc, &depthBits, &stencilBits);
561 for(matchtry = 0; matchtry < (sizeof(matches) / sizeof(matches[0])) && !iPixelFormat; matchtry++) {
562 for(i=0; i<nCfgs; i++) {
563 BOOL exactDepthMatch = TRUE;
564 WineD3D_PixelFormat *cfg = &This->adapter->cfgs[i];
566 /* For now only accept RGBA formats. Perhaps some day we will
567 * allow floating point formats for pbuffers. */
568 if(cfg->iPixelType != WGL_TYPE_RGBA_ARB)
569 continue;
571 /* In window mode (!pbuffer) we need a window drawable format and double buffering. */
572 if(!pbuffer && !(cfg->windowDrawable && cfg->doubleBuffer))
573 continue;
575 /* We like to have aux buffers in backbuffer mode */
576 if(auxBuffers && !cfg->auxBuffers && matches[matchtry].require_aux)
577 continue;
579 /* In pbuffer-mode we need a pbuffer-capable format but we don't want double buffering */
580 if(pbuffer && (!cfg->pbufferDrawable || cfg->doubleBuffer))
581 continue;
583 if(matches[matchtry].exact_color) {
584 if(cfg->redSize != redBits)
585 continue;
586 if(cfg->greenSize != greenBits)
587 continue;
588 if(cfg->blueSize != blueBits)
589 continue;
590 } else {
591 if(cfg->redSize < redBits)
592 continue;
593 if(cfg->greenSize < greenBits)
594 continue;
595 if(cfg->blueSize < blueBits)
596 continue;
598 if(matches[matchtry].exact_alpha) {
599 if(cfg->alphaSize != alphaBits)
600 continue;
601 } else {
602 if(cfg->alphaSize < alphaBits)
603 continue;
606 /* We try to locate a format which matches our requirements exactly. In case of
607 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
608 if(cfg->depthSize < depthBits)
609 continue;
610 else if(cfg->depthSize > depthBits)
611 exactDepthMatch = FALSE;
613 /* In all cases make sure the number of stencil bits matches our requirements
614 * even when we don't need stencil because it could affect performance EXCEPT
615 * on cards which don't offer depth formats without stencil like the i915 drivers
616 * on Linux. */
617 if(stencilBits != cfg->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfg->stencilSize))
618 continue;
620 /* Check multisampling support */
621 if(cfg->numSamples != numSamples)
622 continue;
624 /* When we have passed all the checks then we have found a format which matches our
625 * requirements. Note that we only check for a limit number of capabilities right now,
626 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
627 * can still differ in things like multisampling, stereo, SRGB and other flags.
630 /* Exit the loop as we have found a format :) */
631 if(exactDepthMatch) {
632 iPixelFormat = cfg->iPixelFormat;
633 break;
634 } else if(!iPixelFormat) {
635 /* In the end we might end up with a format which doesn't exactly match our depth
636 * requirements. Accept the first format we found because formats with higher iPixelFormat
637 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
638 iPixelFormat = cfg->iPixelFormat;
643 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
644 if(!iPixelFormat && !findCompatible) {
645 ERR("Can't find a suitable iPixelFormat\n");
646 return FALSE;
647 } else if(!iPixelFormat) {
648 PIXELFORMATDESCRIPTOR pfd;
650 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
651 /* PixelFormat selection */
652 ZeroMemory(&pfd, sizeof(pfd));
653 pfd.nSize = sizeof(pfd);
654 pfd.nVersion = 1;
655 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
656 pfd.iPixelType = PFD_TYPE_RGBA;
657 pfd.cAlphaBits = alphaBits;
658 pfd.cColorBits = colorBits;
659 pfd.cDepthBits = depthBits;
660 pfd.cStencilBits = stencilBits;
661 pfd.iLayerType = PFD_MAIN_PLANE;
663 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
664 if(!iPixelFormat) {
665 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
666 ERR("Can't find a suitable iPixelFormat\n");
667 return FALSE;
671 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
672 iPixelFormat, debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format));
673 return iPixelFormat;
676 /*****************************************************************************
677 * CreateContext
679 * Creates a new context for a window, or a pbuffer context.
681 * * Params:
682 * This: Device to activate the context for
683 * target: Surface this context will render to
684 * win_handle: handle to the window which we are drawing to
685 * create_pbuffer: tells whether to create a pbuffer or not
686 * pPresentParameters: contains the pixelformats to use for onscreen rendering
688 *****************************************************************************/
689 WineD3DContext *CreateContext(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *target, HWND win_handle, BOOL create_pbuffer, const WINED3DPRESENT_PARAMETERS *pPresentParms) {
690 HDC oldDrawable, hdc;
691 HPBUFFERARB pbuffer = NULL;
692 HGLRC ctx = NULL, oldCtx;
693 WineD3DContext *ret = NULL;
694 unsigned int s;
696 TRACE("(%p): Creating a %s context for render target %p\n", This, create_pbuffer ? "offscreen" : "onscreen", target);
698 if(create_pbuffer) {
699 HDC hdc_parent = GetDC(win_handle);
700 int iPixelFormat = 0;
702 IWineD3DSurface *StencilSurface = This->stencilBufferTarget;
703 const struct GlPixelFormatDesc *ds_format_desc = StencilSurface
704 ? ((IWineD3DSurfaceImpl *)StencilSurface)->resource.format_desc
705 : getFormatDescEntry(WINED3DFMT_UNKNOWN, &This->adapter->gl_info);
707 /* Try to find a pixel format with pbuffer support. */
708 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc_parent, target->resource.format_desc,
709 ds_format_desc, FALSE /* auxBuffers */, 0 /* numSamples */, TRUE /* PBUFFER */,
710 FALSE /* findCompatible */);
711 if(!iPixelFormat) {
712 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
714 /* For some reason we weren't able to find a format, try to find something instead of crashing.
715 * A reason for failure could have been wglChoosePixelFormatARB strictness. */
716 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc_parent, target->resource.format_desc,
717 ds_format_desc, FALSE /* auxBuffer */, 0 /* numSamples */, TRUE /* PBUFFER */,
718 TRUE /* findCompatible */);
721 /* This shouldn't happen as ChoosePixelFormat always returns something */
722 if(!iPixelFormat) {
723 ERR("Unable to locate a pixel format for a pbuffer\n");
724 ReleaseDC(win_handle, hdc_parent);
725 goto out;
728 TRACE("Creating a pBuffer drawable for the new context\n");
729 pbuffer = GL_EXTCALL(wglCreatePbufferARB(hdc_parent, iPixelFormat, target->currentDesc.Width, target->currentDesc.Height, 0));
730 if(!pbuffer) {
731 ERR("Cannot create a pbuffer\n");
732 ReleaseDC(win_handle, hdc_parent);
733 goto out;
736 /* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
737 hdc = GL_EXTCALL(wglGetPbufferDCARB(pbuffer));
738 if(!hdc) {
739 ERR("Cannot get a HDC for pbuffer (%p)\n", pbuffer);
740 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
741 ReleaseDC(win_handle, hdc_parent);
742 goto out;
744 ReleaseDC(win_handle, hdc_parent);
745 } else {
746 PIXELFORMATDESCRIPTOR pfd;
747 int iPixelFormat;
748 int res;
749 const struct GlPixelFormatDesc *color_format_desc = target->resource.format_desc;
750 const struct GlPixelFormatDesc *ds_format_desc = getFormatDescEntry(WINED3DFMT_UNKNOWN,
751 &This->adapter->gl_info);
752 BOOL auxBuffers = FALSE;
753 int numSamples = 0;
755 hdc = GetDC(win_handle);
756 if(hdc == NULL) {
757 ERR("Cannot retrieve a device context!\n");
758 goto out;
761 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
762 if(wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER) {
763 auxBuffers = TRUE;
765 if (color_format_desc->format == WINED3DFMT_X4R4G4B4)
766 color_format_desc = getFormatDescEntry(WINED3DFMT_A4R4G4B4, &This->adapter->gl_info);
767 else if (color_format_desc->format == WINED3DFMT_X8R8G8B8)
768 color_format_desc = getFormatDescEntry(WINED3DFMT_A8R8G8B8, &This->adapter->gl_info);
771 /* DirectDraw supports 8bit paletted render targets and these are used by old games like Starcraft and C&C.
772 * Most modern hardware doesn't support 8bit natively so we perform some form of 8bit -> 32bit conversion.
773 * The conversion (ab)uses the alpha component for storing the palette index. For this reason we require
774 * a format with 8bit alpha, so request A8R8G8B8. */
775 if (color_format_desc->format == WINED3DFMT_P8)
776 color_format_desc = getFormatDescEntry(WINED3DFMT_A8R8G8B8, &This->adapter->gl_info);
778 /* Retrieve the depth stencil format from the present parameters.
779 * The choice of the proper format can give a nice performance boost
780 * in case of GPU limited programs. */
781 if(pPresentParms->EnableAutoDepthStencil) {
782 TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms->AutoDepthStencilFormat));
783 ds_format_desc = getFormatDescEntry(pPresentParms->AutoDepthStencilFormat, &This->adapter->gl_info);
786 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD */
787 if(pPresentParms->MultiSampleType && (pPresentParms->SwapEffect == WINED3DSWAPEFFECT_DISCARD)) {
788 if(!GL_SUPPORT(ARB_MULTISAMPLE))
789 ERR("The program is requesting multisampling without support!\n");
790 else {
791 ERR("Requesting MultiSampleType=%d\n", pPresentParms->MultiSampleType);
792 numSamples = pPresentParms->MultiSampleType;
796 /* Try to find a pixel format which matches our requirements */
797 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, color_format_desc, ds_format_desc,
798 auxBuffers, numSamples, FALSE /* PBUFFER */, FALSE /* findCompatible */);
800 /* Try to locate a compatible format if we weren't able to find anything */
801 if(!iPixelFormat) {
802 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
803 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, color_format_desc, ds_format_desc,
804 auxBuffers, 0 /* numSamples */, FALSE /* PBUFFER */, TRUE /* findCompatible */ );
807 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
808 if(!iPixelFormat) {
809 ERR("Can't find a suitable iPixelFormat\n");
810 return FALSE;
813 DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd);
814 res = SetPixelFormat(hdc, iPixelFormat, NULL);
815 if(!res) {
816 int oldPixelFormat = GetPixelFormat(hdc);
818 /* By default WGL doesn't allow pixel format adjustments but we need it here.
819 * For this reason there is a WINE-specific wglSetPixelFormat which allows you to
820 * set the pixel format multiple times. Only use it when it is really needed. */
822 if(oldPixelFormat == iPixelFormat) {
823 /* We don't have to do anything as the formats are the same :) */
824 } else if(oldPixelFormat && GL_SUPPORT(WGL_WINE_PIXEL_FORMAT_PASSTHROUGH)) {
825 res = GL_EXTCALL(wglSetPixelFormatWINE(hdc, iPixelFormat, NULL));
827 if(!res) {
828 ERR("wglSetPixelFormatWINE failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
829 return FALSE;
831 } else if(oldPixelFormat) {
832 /* OpenGL doesn't allow pixel format adjustments. Print an error and continue using the old format.
833 * There's a big chance that the old format works although with a performance hit and perhaps rendering errors. */
834 ERR("HDC=%p is already set to iPixelFormat=%d and OpenGL doesn't allow changes!\n", hdc, oldPixelFormat);
835 } else {
836 ERR("SetPixelFormat failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
837 return FALSE;
842 ctx = pwglCreateContext(hdc);
843 if(This->numContexts) pwglShareLists(This->contexts[0]->glCtx, ctx);
845 if(!ctx) {
846 ERR("Failed to create a WGL context\n");
847 if(create_pbuffer) {
848 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
849 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
851 goto out;
853 ret = AddContextToArray(This, win_handle, hdc, ctx, pbuffer);
854 if(!ret) {
855 ERR("Failed to add the newly created context to the context list\n");
856 pwglDeleteContext(ctx);
857 if(create_pbuffer) {
858 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
859 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
861 goto out;
863 ret->surface = (IWineD3DSurface *) target;
864 ret->isPBuffer = create_pbuffer;
865 ret->tid = GetCurrentThreadId();
866 if(This->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *) This)) {
867 /* Create the dirty constants array and initialize them to dirty */
868 ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
869 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
870 ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
871 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
872 memset(ret->vshader_const_dirty, 1,
873 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
874 memset(ret->pshader_const_dirty, 1,
875 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
878 TRACE("Successfully created new context %p\n", ret);
880 list_init(&ret->fbo_list);
882 /* Set up the context defaults */
883 oldCtx = pwglGetCurrentContext();
884 oldDrawable = pwglGetCurrentDC();
885 if(oldCtx && oldDrawable) {
886 /* See comment in ActivateContext context switching */
887 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
889 if(pwglMakeCurrent(hdc, ctx) == FALSE) {
890 ERR("Cannot activate context to set up defaults\n");
891 goto out;
894 ENTER_GL();
896 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
898 TRACE("Setting up the screen\n");
899 /* Clear the screen */
900 glClearColor(1.0, 0.0, 0.0, 0.0);
901 checkGLcall("glClearColor");
902 glClearIndex(0);
903 glClearDepth(1);
904 glClearStencil(0xffff);
906 checkGLcall("glClear");
908 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
909 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
911 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
912 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
914 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
915 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
917 glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);
918 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
919 glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);
920 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
922 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
923 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
924 * and textures in DIB sections(due to the memory protection).
926 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
927 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
929 if(GL_SUPPORT(ARB_VERTEX_BLEND)) {
930 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
931 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
932 * GL_VERTEX_BLEND_ARB isn't enabled too
934 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
935 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
937 if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
938 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
939 * the previous texture where to source the offset from is always unit - 1.
941 for(s = 1; s < GL_LIMITS(textures); s++) {
942 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
943 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
944 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
948 for(s = 0; s < GL_LIMITS(point_sprite_units); s++) {
949 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
950 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
951 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
953 LEAVE_GL();
955 /* Never keep GL_FRAGMENT_SHADER_ATI enabled on a context that we switch away from,
956 * but enable it for the first context we create, and reenable it on the old context
958 if(oldDrawable && oldCtx) {
959 pwglMakeCurrent(oldDrawable, oldCtx);
960 } else {
961 last_device = This;
963 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
965 return ret;
967 out:
968 return NULL;
971 /*****************************************************************************
972 * RemoveContextFromArray
974 * Removes a context from the context manager. The opengl context is not
975 * destroyed or unset. context is not a valid pointer after that call.
977 * Similar to the former call this isn't a performance critical function. A
978 * helper function for DestroyContext.
980 * Params:
981 * This: Device to activate the context for
982 * context: Context to remove
984 *****************************************************************************/
985 static void RemoveContextFromArray(IWineD3DDeviceImpl *This, WineD3DContext *context) {
986 WineD3DContext **new_array;
987 BOOL found = FALSE;
988 UINT i;
990 TRACE("Removing ctx %p\n", context);
992 for (i = 0; i < This->numContexts; ++i)
994 if (This->contexts[i] == context)
996 HeapFree(GetProcessHeap(), 0, context);
997 found = TRUE;
998 break;
1002 if (!found)
1004 ERR("Context %p doesn't exist in context array\n", context);
1005 return;
1008 while (i < This->numContexts - 1)
1010 This->contexts[i] = This->contexts[i + 1];
1011 ++i;
1014 --This->numContexts;
1015 if (!This->numContexts)
1017 HeapFree(GetProcessHeap(), 0, This->contexts);
1018 This->contexts = NULL;
1019 return;
1022 new_array = HeapReAlloc(GetProcessHeap(), 0, This->contexts, This->numContexts * sizeof(*This->contexts));
1023 if (!new_array)
1025 ERR("Failed to shrink context array. Oh well.\n");
1026 return;
1029 This->contexts = new_array;
1032 /*****************************************************************************
1033 * DestroyContext
1035 * Destroys a wineD3DContext
1037 * Params:
1038 * This: Device to activate the context for
1039 * context: Context to destroy
1041 *****************************************************************************/
1042 void DestroyContext(IWineD3DDeviceImpl *This, WineD3DContext *context) {
1043 struct fbo_entry *entry, *entry2;
1045 TRACE("Destroying ctx %p\n", context);
1047 /* The correct GL context needs to be active to cleanup the GL resources below */
1048 if(pwglGetCurrentContext() != context->glCtx){
1049 pwglMakeCurrent(context->hdc, context->glCtx);
1050 last_device = NULL;
1053 ENTER_GL();
1055 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry) {
1056 context_destroy_fbo_entry(This, entry);
1058 if (context->src_fbo) {
1059 TRACE("Destroy src FBO %d\n", context->src_fbo);
1060 context_destroy_fbo(This, &context->src_fbo);
1062 if (context->dst_fbo) {
1063 TRACE("Destroy dst FBO %d\n", context->dst_fbo);
1064 context_destroy_fbo(This, &context->dst_fbo);
1067 LEAVE_GL();
1069 if (This->activeContext == context)
1071 This->activeContext = NULL;
1072 TRACE("Destroying the active context.\n");
1075 /* Cleanup the GL context */
1076 pwglMakeCurrent(NULL, NULL);
1077 if(context->isPBuffer) {
1078 GL_EXTCALL(wglReleasePbufferDCARB(context->pbuffer, context->hdc));
1079 GL_EXTCALL(wglDestroyPbufferARB(context->pbuffer));
1080 } else ReleaseDC(context->win_handle, context->hdc);
1081 pwglDeleteContext(context->glCtx);
1083 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
1084 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
1085 RemoveContextFromArray(This, context);
1088 /* GL locking is done by the caller */
1089 static inline void set_blit_dimension(UINT width, UINT height) {
1090 glMatrixMode(GL_PROJECTION);
1091 checkGLcall("glMatrixMode(GL_PROJECTION)");
1092 glLoadIdentity();
1093 checkGLcall("glLoadIdentity()");
1094 glOrtho(0, width, height, 0, 0.0, -1.0);
1095 checkGLcall("glOrtho");
1096 glViewport(0, 0, width, height);
1097 checkGLcall("glViewport");
1100 /*****************************************************************************
1101 * SetupForBlit
1103 * Sets up a context for DirectDraw blitting.
1104 * All texture units are disabled, texture unit 0 is set as current unit
1105 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1106 * color writing enabled for all channels
1107 * register combiners disabled, shaders disabled
1108 * world matrix is set to identity, texture matrix 0 too
1109 * projection matrix is setup for drawing screen coordinates
1111 * Params:
1112 * This: Device to activate the context for
1113 * context: Context to setup
1114 * width: render target width
1115 * height: render target height
1117 *****************************************************************************/
1118 static inline void SetupForBlit(IWineD3DDeviceImpl *This, WineD3DContext *context, UINT width, UINT height) {
1119 int i, sampler;
1120 const struct StateEntry *StateTable = This->StateTable;
1122 TRACE("Setting up context %p for blitting\n", context);
1123 if(context->last_was_blit) {
1124 if(context->blit_w != width || context->blit_h != height) {
1125 ENTER_GL();
1126 set_blit_dimension(width, height);
1127 LEAVE_GL();
1128 context->blit_w = width; context->blit_h = height;
1129 /* No need to dirtify here, the states are still dirtified because they weren't
1130 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1131 * be set
1134 TRACE("Context is already set up for blitting, nothing to do\n");
1135 return;
1137 context->last_was_blit = TRUE;
1139 /* TODO: Use a display list */
1141 /* Disable shaders */
1142 ENTER_GL();
1143 This->shader_backend->shader_select((IWineD3DDevice *)This, FALSE, FALSE);
1144 LEAVE_GL();
1146 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
1147 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
1149 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1150 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1151 * which can safely be called from here, we only lock once instead locking/unlocking
1152 * after each GL call.
1154 ENTER_GL();
1156 /* Disable all textures. The caller can then bind a texture it wants to blit
1157 * from
1159 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1160 * function texture unit. No need to care for higher samplers
1162 for(i = GL_LIMITS(textures) - 1; i > 0 ; i--) {
1163 sampler = This->rev_tex_unit_map[i];
1164 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1165 checkGLcall("glActiveTextureARB");
1167 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
1168 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1169 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1171 glDisable(GL_TEXTURE_3D);
1172 checkGLcall("glDisable GL_TEXTURE_3D");
1173 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
1174 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1175 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1177 glDisable(GL_TEXTURE_2D);
1178 checkGLcall("glDisable GL_TEXTURE_2D");
1180 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1181 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1183 if (sampler != -1) {
1184 if (sampler < MAX_TEXTURES) {
1185 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1187 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1190 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
1191 checkGLcall("glActiveTextureARB");
1193 sampler = This->rev_tex_unit_map[0];
1195 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
1196 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1197 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1199 glDisable(GL_TEXTURE_3D);
1200 checkGLcall("glDisable GL_TEXTURE_3D");
1201 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
1202 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1203 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1205 glDisable(GL_TEXTURE_2D);
1206 checkGLcall("glDisable GL_TEXTURE_2D");
1208 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1210 glMatrixMode(GL_TEXTURE);
1211 checkGLcall("glMatrixMode(GL_TEXTURE)");
1212 glLoadIdentity();
1213 checkGLcall("glLoadIdentity()");
1215 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS)) {
1216 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1217 GL_TEXTURE_LOD_BIAS_EXT,
1218 0.0);
1219 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1222 if (sampler != -1) {
1223 if (sampler < MAX_TEXTURES) {
1224 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
1225 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1227 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1230 /* Other misc states */
1231 glDisable(GL_ALPHA_TEST);
1232 checkGLcall("glDisable(GL_ALPHA_TEST)");
1233 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
1234 glDisable(GL_LIGHTING);
1235 checkGLcall("glDisable GL_LIGHTING");
1236 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
1237 glDisable(GL_DEPTH_TEST);
1238 checkGLcall("glDisable GL_DEPTH_TEST");
1239 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
1240 glDisableWINE(GL_FOG);
1241 checkGLcall("glDisable GL_FOG");
1242 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
1243 glDisable(GL_BLEND);
1244 checkGLcall("glDisable GL_BLEND");
1245 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1246 glDisable(GL_CULL_FACE);
1247 checkGLcall("glDisable GL_CULL_FACE");
1248 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
1249 glDisable(GL_STENCIL_TEST);
1250 checkGLcall("glDisable GL_STENCIL_TEST");
1251 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
1252 glDisable(GL_SCISSOR_TEST);
1253 checkGLcall("glDisable GL_SCISSOR_TEST");
1254 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1255 if(GL_SUPPORT(ARB_POINT_SPRITE)) {
1256 glDisable(GL_POINT_SPRITE_ARB);
1257 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1258 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
1260 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1261 checkGLcall("glColorMask");
1262 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1263 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
1264 glDisable(GL_COLOR_SUM_EXT);
1265 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
1266 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1269 /* Setup transforms */
1270 glMatrixMode(GL_MODELVIEW);
1271 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1272 glLoadIdentity();
1273 checkGLcall("glLoadIdentity()");
1274 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
1276 context->last_was_rhw = TRUE;
1277 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
1279 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1280 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1281 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1282 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1283 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1284 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1285 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1287 set_blit_dimension(width, height);
1289 LEAVE_GL();
1291 context->blit_w = width; context->blit_h = height;
1292 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1293 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
1296 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1299 /*****************************************************************************
1300 * findThreadContextForSwapChain
1302 * Searches a swapchain for all contexts and picks one for the thread tid.
1303 * If none can be found the swapchain is requested to create a new context
1305 *****************************************************************************/
1306 static WineD3DContext *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid) {
1307 unsigned int i;
1309 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
1310 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
1311 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
1316 /* Create a new context for the thread */
1317 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain);
1320 /*****************************************************************************
1321 * FindContext
1323 * Finds a context for the current render target and thread
1325 * Parameters:
1326 * target: Render target to find the context for
1327 * tid: Thread to activate the context for
1329 * Returns: The needed context
1331 *****************************************************************************/
1332 static inline WineD3DContext *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, DWORD tid) {
1333 IWineD3DSwapChain *swapchain = NULL;
1334 BOOL readTexture = wined3d_settings.offscreen_rendering_mode != ORM_FBO && This->render_offscreen;
1335 WineD3DContext *context = This->activeContext;
1336 BOOL oldRenderOffscreen = This->render_offscreen;
1337 const struct GlPixelFormatDesc *old = ((IWineD3DSurfaceImpl *)This->lastActiveRenderTarget)->resource.format_desc;
1338 const struct GlPixelFormatDesc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
1339 const struct StateEntry *StateTable = This->StateTable;
1341 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
1342 * the alpha blend state changes with different render target formats
1344 if (old->format != new->format)
1346 /* Disable blending when the alpha mask has changed and when a format doesn't support blending */
1347 if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
1348 || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
1350 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1354 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
1355 TRACE("Rendering onscreen\n");
1357 context = findThreadContextForSwapChain(swapchain, tid);
1359 This->render_offscreen = FALSE;
1360 /* The context != This->activeContext will catch a NOP context change. This can occur
1361 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
1362 * rendering. No context change is needed in that case
1365 if(wined3d_settings.offscreen_rendering_mode == ORM_PBUFFER) {
1366 if(This->pbufferContext && tid == This->pbufferContext->tid) {
1367 This->pbufferContext->tid = 0;
1370 IWineD3DSwapChain_Release(swapchain);
1372 if(oldRenderOffscreen) {
1373 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
1374 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
1375 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1376 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1377 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
1380 } else {
1381 TRACE("Rendering offscreen\n");
1382 This->render_offscreen = TRUE;
1384 switch(wined3d_settings.offscreen_rendering_mode) {
1385 case ORM_FBO:
1386 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
1387 if(This->activeContext && tid == This->lastThread) {
1388 context = This->activeContext;
1389 } else {
1390 /* This may happen if the app jumps straight into offscreen rendering
1391 * Start using the context of the primary swapchain. tid == 0 is no problem
1392 * for findThreadContextForSwapChain.
1394 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1395 * is perfect to call.
1397 context = findThreadContextForSwapChain(This->swapchains[0], tid);
1399 break;
1401 case ORM_PBUFFER:
1403 IWineD3DSurfaceImpl *targetimpl = (IWineD3DSurfaceImpl *) target;
1404 if(This->pbufferContext == NULL ||
1405 This->pbufferWidth < targetimpl->currentDesc.Width ||
1406 This->pbufferHeight < targetimpl->currentDesc.Height) {
1407 if(This->pbufferContext) {
1408 DestroyContext(This, This->pbufferContext);
1411 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
1412 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
1414 This->pbufferContext = CreateContext(This, targetimpl,
1415 ((IWineD3DSwapChainImpl *) This->swapchains[0])->context[0]->win_handle,
1416 TRUE /* pbuffer */, &((IWineD3DSwapChainImpl *)This->swapchains[0])->presentParms);
1417 This->pbufferWidth = targetimpl->currentDesc.Width;
1418 This->pbufferHeight = targetimpl->currentDesc.Height;
1421 if(This->pbufferContext) {
1422 if(This->pbufferContext->tid != 0 && This->pbufferContext->tid != tid) {
1423 FIXME("The PBuffr context is only supported for one thread for now!\n");
1425 This->pbufferContext->tid = tid;
1426 context = This->pbufferContext;
1427 break;
1428 } else {
1429 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
1430 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
1434 case ORM_BACKBUFFER:
1435 /* Stay with the currently active context for back buffer rendering */
1436 if(This->activeContext && tid == This->lastThread) {
1437 context = This->activeContext;
1438 } else {
1439 /* This may happen if the app jumps straight into offscreen rendering
1440 * Start using the context of the primary swapchain. tid == 0 is no problem
1441 * for findThreadContextForSwapChain.
1443 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1444 * is perfect to call.
1446 context = findThreadContextForSwapChain(This->swapchains[0], tid);
1448 break;
1451 if(!oldRenderOffscreen) {
1452 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
1453 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
1454 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1455 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1456 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
1460 /* When switching away from an offscreen render target, and we're not using FBOs,
1461 * we have to read the drawable into the texture. This is done via PreLoad(and
1462 * SFLAG_INDRAWABLE set on the surface). There are some things that need care though.
1463 * PreLoad needs a GL context, and FindContext is called before the context is activated.
1464 * It also has to be called with the old rendertarget active, otherwise a wrong drawable
1465 * is read. This leads to these possible situations:
1467 * 0) lastActiveRenderTarget == target && oldTid == newTid:
1468 * Nothing to do, we don't even reach this code in this case...
1470 * 1) lastActiveRenderTarget != target && oldTid == newTid:
1471 * The currently active context is OK for readback. Call PreLoad, and it
1472 * performs the read
1474 * 2) lastActiveRenderTarget == target && oldTid != newTid:
1475 * Nothing to do - the drawable is unchanged
1477 * 3) lastActiveRenderTarget != target && oldTid != newTid:
1478 * This is tricky. We have to get a context with the old drawable from somewhere
1479 * before we can switch to the new context. In this case, PreLoad calls
1480 * ActivateContext(lastActiveRenderTarget) from the new(current) thread. This
1481 * is case (2) then. The old drawable is activated for the new thread, and the
1482 * readback can be done. The recursed ActivateContext does *not* call PreLoad again.
1483 * After that, the outer ActivateContext(which calls PreLoad) can activate the new
1484 * target for the new thread
1486 if (readTexture && This->lastActiveRenderTarget != target) {
1487 BOOL oldInDraw = This->isInDraw;
1489 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
1490 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
1491 * when using offscreen rendering with multithreading
1493 This->isInDraw = TRUE;
1495 /* Do that before switching the context:
1496 * Read the back buffer of the old drawable into the destination texture
1498 if(((IWineD3DSurfaceImpl *)This->lastActiveRenderTarget)->glDescription.srgbTextureName) {
1499 surface_internal_preload(This->lastActiveRenderTarget, SRGB_BOTH);
1500 } else {
1501 surface_internal_preload(This->lastActiveRenderTarget, SRGB_RGB);
1504 /* Assume that the drawable will be modified by some other things now */
1505 IWineD3DSurface_ModifyLocation(This->lastActiveRenderTarget, SFLAG_INDRAWABLE, FALSE);
1507 This->isInDraw = oldInDraw;
1510 return context;
1513 static void apply_draw_buffer(IWineD3DDeviceImpl *This, IWineD3DSurface *target, BOOL blit)
1515 IWineD3DSwapChain *swapchain;
1517 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain)))
1519 IWineD3DSwapChain_Release((IUnknown *)swapchain);
1520 ENTER_GL();
1521 glDrawBuffer(surface_get_gl_buffer(target, swapchain));
1522 checkGLcall("glDrawBuffers()");
1523 LEAVE_GL();
1525 else
1527 ENTER_GL();
1528 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
1530 if (!blit)
1532 if (GL_SUPPORT(ARB_DRAW_BUFFERS))
1534 GL_EXTCALL(glDrawBuffersARB(GL_LIMITS(buffers), This->draw_buffers));
1535 checkGLcall("glDrawBuffers()");
1537 else
1539 glDrawBuffer(This->draw_buffers[0]);
1540 checkGLcall("glDrawBuffer()");
1542 } else {
1543 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
1544 checkGLcall("glDrawBuffer()");
1547 else
1549 glDrawBuffer(This->offscreenBuffer);
1550 checkGLcall("glDrawBuffer()");
1552 LEAVE_GL();
1556 /*****************************************************************************
1557 * ActivateContext
1559 * Finds a rendering context and drawable matching the device and render
1560 * target for the current thread, activates them and puts them into the
1561 * requested state.
1563 * Params:
1564 * This: Device to activate the context for
1565 * target: Requested render target
1566 * usage: Prepares the context for blitting, drawing or other actions
1568 *****************************************************************************/
1569 void ActivateContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, ContextUsage usage) {
1570 DWORD tid = GetCurrentThreadId();
1571 DWORD i, dirtyState, idx;
1572 BYTE shift;
1573 WineD3DContext *context;
1574 const struct StateEntry *StateTable = This->StateTable;
1576 TRACE("(%p): Selecting context for render target %p, thread %d\n", This, target, tid);
1577 if(This->lastActiveRenderTarget != target || tid != This->lastThread) {
1578 context = FindContext(This, target, tid);
1579 context->draw_buffer_dirty = TRUE;
1580 This->lastActiveRenderTarget = target;
1581 This->lastThread = tid;
1582 } else {
1583 /* Stick to the old context */
1584 context = This->activeContext;
1587 /* Activate the opengl context */
1588 if(last_device != This || context != This->activeContext) {
1589 BOOL ret;
1591 /* Prevent an unneeded context switch as those are expensive */
1592 if(context->glCtx && (context->glCtx == pwglGetCurrentContext())) {
1593 TRACE("Already using gl context %p\n", context->glCtx);
1595 else {
1596 TRACE("Switching gl ctx to %p, hdc=%p ctx=%p\n", context, context->hdc, context->glCtx);
1598 ret = pwglMakeCurrent(context->hdc, context->glCtx);
1599 if(ret == FALSE) {
1600 ERR("Failed to activate the new context\n");
1601 } else if(!context->last_was_blit) {
1602 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
1603 } else {
1604 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1607 if(This->activeContext->vshader_const_dirty) {
1608 memset(This->activeContext->vshader_const_dirty, 1,
1609 sizeof(*This->activeContext->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
1611 if(This->activeContext->pshader_const_dirty) {
1612 memset(This->activeContext->pshader_const_dirty, 1,
1613 sizeof(*This->activeContext->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
1615 This->activeContext = context;
1616 last_device = This;
1619 switch (usage) {
1620 case CTXUSAGE_CLEAR:
1621 case CTXUSAGE_DRAWPRIM:
1622 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1623 ENTER_GL();
1624 context_apply_fbo_state((IWineD3DDevice *)This);
1625 LEAVE_GL();
1627 if (context->draw_buffer_dirty) {
1628 apply_draw_buffer(This, target, FALSE);
1629 context->draw_buffer_dirty = FALSE;
1631 break;
1633 case CTXUSAGE_BLIT:
1634 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1635 if (This->render_offscreen) {
1636 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
1637 ENTER_GL();
1638 context_bind_fbo((IWineD3DDevice *)This, GL_FRAMEBUFFER_EXT, &context->dst_fbo);
1639 context_attach_surface_fbo(This, GL_FRAMEBUFFER_EXT, 0, target);
1640 GL_EXTCALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0));
1641 checkGLcall("glFramebufferRenderbufferEXT");
1642 LEAVE_GL();
1643 } else {
1644 ENTER_GL();
1645 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
1646 checkGLcall("glFramebufferRenderbufferEXT");
1647 LEAVE_GL();
1649 context->draw_buffer_dirty = TRUE;
1651 if (context->draw_buffer_dirty) {
1652 apply_draw_buffer(This, target, TRUE);
1653 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
1654 context->draw_buffer_dirty = FALSE;
1657 break;
1659 default:
1660 break;
1663 switch(usage) {
1664 case CTXUSAGE_RESOURCELOAD:
1665 /* This does not require any special states to be set up */
1666 break;
1668 case CTXUSAGE_CLEAR:
1669 if(context->last_was_blit) {
1670 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
1673 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
1674 * blending when clearing improves the clearing performance incredibly.
1676 ENTER_GL();
1677 glDisable(GL_BLEND);
1678 LEAVE_GL();
1679 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1681 ENTER_GL();
1682 glEnable(GL_SCISSOR_TEST);
1683 checkGLcall("glEnable GL_SCISSOR_TEST");
1684 LEAVE_GL();
1685 context->last_was_blit = FALSE;
1686 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1687 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1688 break;
1690 case CTXUSAGE_DRAWPRIM:
1691 /* This needs all dirty states applied */
1692 if(context->last_was_blit) {
1693 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
1696 IWineD3DDeviceImpl_FindTexUnitMap(This);
1698 ENTER_GL();
1699 for(i=0; i < context->numDirtyEntries; i++) {
1700 dirtyState = context->dirtyArray[i];
1701 idx = dirtyState >> 5;
1702 shift = dirtyState & 0x1f;
1703 context->isStateDirty[idx] &= ~(1 << shift);
1704 StateTable[dirtyState].apply(dirtyState, This->stateBlock, context);
1706 LEAVE_GL();
1707 context->numDirtyEntries = 0; /* This makes the whole list clean */
1708 context->last_was_blit = FALSE;
1709 break;
1711 case CTXUSAGE_BLIT:
1712 SetupForBlit(This, context,
1713 ((IWineD3DSurfaceImpl *)target)->currentDesc.Width,
1714 ((IWineD3DSurfaceImpl *)target)->currentDesc.Height);
1715 break;
1717 default:
1718 FIXME("Unexpected context usage requested\n");
1722 WineD3DContext *getActiveContext(void) {
1723 return last_device->activeContext;