wined3d: Let WineD3D_ChoosePixelFormat operate on the pixel format database we store...
[wine/multimedia.git] / dlls / wined3d / context.c
blob901a36cd2d6c2b8bcaf807e7ffcaae846dfa71e0
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 /*****************************************************************************
33 * Context_MarkStateDirty
35 * Marks a state in a context dirty. Only one context, opposed to
36 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
37 * contexts
39 * Params:
40 * context: Context to mark the state dirty in
41 * state: State to mark dirty
42 * StateTable: Pointer to the state table in use(for state grouping)
44 *****************************************************************************/
45 static void Context_MarkStateDirty(WineD3DContext *context, DWORD state, const struct StateEntry *StateTable) {
46 DWORD rep = StateTable[state].representative;
47 DWORD idx;
48 BYTE shift;
50 if(!rep || isStateDirty(context, rep)) return;
52 context->dirtyArray[context->numDirtyEntries++] = rep;
53 idx = rep >> 5;
54 shift = rep & 0x1f;
55 context->isStateDirty[idx] |= (1 << shift);
58 /*****************************************************************************
59 * AddContextToArray
61 * Adds a context to the context array. Helper function for CreateContext
63 * This method is not called in performance-critical code paths, only when a
64 * new render target or swapchain is created. Thus performance is not an issue
65 * here.
67 * Params:
68 * This: Device to add the context for
69 * hdc: device context
70 * glCtx: WGL context to add
71 * pbuffer: optional pbuffer used with this context
73 *****************************************************************************/
74 static WineD3DContext *AddContextToArray(IWineD3DDeviceImpl *This, HWND win_handle, HDC hdc, HGLRC glCtx, HPBUFFERARB pbuffer) {
75 WineD3DContext **oldArray = This->contexts;
76 DWORD state;
78 This->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->contexts) * (This->numContexts + 1));
79 if(This->contexts == NULL) {
80 ERR("Unable to grow the context array\n");
81 This->contexts = oldArray;
82 return NULL;
84 if(oldArray) {
85 memcpy(This->contexts, oldArray, sizeof(*This->contexts) * This->numContexts);
88 This->contexts[This->numContexts] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WineD3DContext));
89 if(This->contexts[This->numContexts] == NULL) {
90 ERR("Unable to allocate a new context\n");
91 HeapFree(GetProcessHeap(), 0, This->contexts);
92 This->contexts = oldArray;
93 return NULL;
96 This->contexts[This->numContexts]->hdc = hdc;
97 This->contexts[This->numContexts]->glCtx = glCtx;
98 This->contexts[This->numContexts]->pbuffer = pbuffer;
99 This->contexts[This->numContexts]->win_handle = win_handle;
100 HeapFree(GetProcessHeap(), 0, oldArray);
102 /* Mark all states dirty to force a proper initialization of the states on the first use of the context
104 for(state = 0; state <= STATE_HIGHEST; state++) {
105 Context_MarkStateDirty(This->contexts[This->numContexts], state, This->shader_backend->StateTable);
108 This->numContexts++;
109 TRACE("Created context %p\n", This->contexts[This->numContexts - 1]);
110 return This->contexts[This->numContexts - 1];
113 /* This function takes care of WineD3D pixel format selection. */
114 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl *This, HDC hdc, WINED3DFORMAT ColorFormat, WINED3DFORMAT DepthStencilFormat, BOOL auxBuffers, BOOL pbuffer, BOOL findCompatible)
116 int iPixelFormat=0;
117 short redBits, greenBits, blueBits, alphaBits, colorBits;
118 short depthBits=0, stencilBits=0;
120 int i = 0;
121 int nCfgs = This->adapter->nCfgs;
122 WineD3D_PixelFormat *cfgs = This->adapter->cfgs;
124 if(!getColorBits(ColorFormat, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits)) {
125 ERR("Unable to get color bits for format %s (%#x)!\n", debug_d3dformat(ColorFormat), ColorFormat);
126 return 0;
129 if(DepthStencilFormat) {
130 getDepthStencilBits(DepthStencilFormat, &depthBits, &stencilBits);
133 /* Find a pixel format which EXACTLY matches our requirements (except for depth) */
134 for(i=0; i<nCfgs; i++) {
135 BOOL exactDepthMatch = TRUE;
137 /* For now only accept RGBA formats. Perhaps some day we will
138 * allow floating point formats for pbuffers. */
139 if(cfgs->iPixelType != WGL_TYPE_RGBA_ARB)
140 continue;
142 /* In all cases except when we are in pbuffer-mode we need a window drawable format with double buffering. */
143 if(!pbuffer && !cfgs->windowDrawable && !cfgs->doubleBuffer)
144 continue;
146 /* We like to have aux buffers in backbuffer mode */
147 if(auxBuffers && !cfgs->auxBuffers)
148 continue;
150 /* In pbuffer-mode we need a pbuffer-capable format */
151 if(pbuffer && !cfgs->pbufferDrawable)
152 continue;
154 if(cfgs->redSize != redBits)
155 continue;
156 if(cfgs->greenSize != greenBits)
157 continue;
158 if(cfgs->blueSize != blueBits)
159 continue;
160 if(cfgs->alphaSize != alphaBits)
161 continue;
163 /* We try to locate a format which matches our requirements exactly. In case of
164 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
165 if(cfgs->depthSize < depthBits)
166 continue;
167 else if(cfgs->depthSize > depthBits)
168 exactDepthMatch = FALSE;
170 /* In all cases make sure the number of stencil bits matches our requirements
171 * even when we don't need stencil because it could affect performance */
172 if(!(cfgs->stencilSize == stencilBits))
173 continue;
175 /* When we have passed all the checks then we have found a format which matches our
176 * requirements. Note that we only check for a limit number of capabilities right now,
177 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
178 * can still differ in things like multisampling, stereo, SRGB and other flags.
181 /* Exit the loop as we have found a format :) */
182 if(exactDepthMatch) {
183 iPixelFormat = cfgs->iPixelFormat;
184 break;
185 } else if(!iPixelFormat) {
186 /* In the end we might end up with a format which doesn't exactly match our depth
187 * requirements. Accept the first format we found because formats with higher iPixelFormat
188 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
189 iPixelFormat = cfgs->iPixelFormat;
193 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
194 if(!iPixelFormat && !findCompatible) {
195 ERR("Can't find a suitable iPixelFormat\n");
196 return FALSE;
197 } else {
198 PIXELFORMATDESCRIPTOR pfd;
200 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
201 /* PixelFormat selection */
202 ZeroMemory(&pfd, sizeof(pfd));
203 pfd.nSize = sizeof(pfd);
204 pfd.nVersion = 1;
205 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
206 pfd.iPixelType = PFD_TYPE_RGBA;
207 pfd.cAlphaBits = alphaBits;
208 pfd.cColorBits = colorBits;
209 pfd.cDepthBits = depthBits;
210 pfd.cStencilBits = stencilBits;
211 pfd.iLayerType = PFD_MAIN_PLANE;
213 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
214 if(!iPixelFormat) {
215 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
216 ERR("Can't find a suitable iPixelFormat\n");
217 return FALSE;
221 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n", cfgs->iPixelFormat, debug_d3dformat(ColorFormat), debug_d3dformat(DepthStencilFormat));
222 return iPixelFormat;
225 /*****************************************************************************
226 * CreateContext
228 * Creates a new context for a window, or a pbuffer context.
230 * * Params:
231 * This: Device to activate the context for
232 * target: Surface this context will render to
233 * win_handle: handle to the window which we are drawing to
234 * create_pbuffer: tells whether to create a pbuffer or not
235 * pPresentParameters: contains the pixelformats to use for onscreen rendering
237 *****************************************************************************/
238 WineD3DContext *CreateContext(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *target, HWND win_handle, BOOL create_pbuffer, const WINED3DPRESENT_PARAMETERS *pPresentParms) {
239 HDC oldDrawable, hdc;
240 HPBUFFERARB pbuffer = NULL;
241 HGLRC ctx = NULL, oldCtx;
242 WineD3DContext *ret = NULL;
243 int s;
245 TRACE("(%p): Creating a %s context for render target %p\n", This, create_pbuffer ? "offscreen" : "onscreen", target);
247 #define PUSH1(att) attribs[nAttribs++] = (att);
248 #define PUSH2(att,value) attribs[nAttribs++] = (att); attribs[nAttribs++] = (value);
249 if(create_pbuffer) {
250 HDC hdc_parent = GetDC(win_handle);
251 int iPixelFormat = 0;
252 short redBits, greenBits, blueBits, alphaBits, colorBits;
253 short depthBits, stencilBits;
255 IWineD3DSurface *StencilSurface = This->stencilBufferTarget;
256 WINED3DFORMAT StencilBufferFormat = (NULL != StencilSurface) ? ((IWineD3DSurfaceImpl *) StencilSurface)->resource.format : 0;
258 int attribs[256];
259 int nAttribs = 0;
260 unsigned int nFormats;
262 /* Retrieve the specifications for the pixelformat from the backbuffer / stencilbuffer */
263 getColorBits(target->resource.format, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits);
264 getDepthStencilBits(StencilBufferFormat, &depthBits, &stencilBits);
265 PUSH2(WGL_DRAW_TO_PBUFFER_ARB, 1); /* We need pbuffer support; doublebuffering isn't needed */
266 PUSH2(WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB); /* Make sure we don't get a float or color index format */
267 PUSH2(WGL_COLOR_BITS_ARB, colorBits);
268 PUSH2(WGL_RED_BITS_ARB, redBits);
269 PUSH2(WGL_GREEN_BITS_ARB, greenBits);
270 PUSH2(WGL_BLUE_BITS_ARB, blueBits);
271 PUSH2(WGL_ALPHA_BITS_ARB, alphaBits);
272 PUSH2(WGL_DEPTH_BITS_ARB, depthBits);
273 PUSH2(WGL_STENCIL_BITS_ARB, stencilBits);
274 PUSH1(0); /* end the list */
276 /* Try to find a pixelformat that matches exactly. If that fails let ChoosePixelFormat try to find a close match */
277 if(!GL_EXTCALL(wglChoosePixelFormatARB(hdc_parent, (const int*)&attribs, NULL, 1, &iPixelFormat, &nFormats)))
279 PIXELFORMATDESCRIPTOR pfd;
281 TRACE("Falling back to ChoosePixelFormat as wglChoosePixelFormatARB failed\n");
283 ZeroMemory(&pfd, sizeof(pfd));
284 pfd.nSize = sizeof(pfd);
285 pfd.nVersion = 1;
286 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER_DONTCARE | PFD_DRAW_TO_WINDOW;
287 pfd.iPixelType = PFD_TYPE_RGBA;
288 pfd.cColorBits = colorBits;
289 pfd.cDepthBits = depthBits;
290 pfd.cStencilBits = stencilBits;
291 pfd.iLayerType = PFD_MAIN_PLANE;
293 iPixelFormat = ChoosePixelFormat(hdc_parent, &pfd);
294 if(!iPixelFormat) {
295 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
296 ERR("Can't find a suitable iPixelFormat for the pbuffer\n");
300 TRACE("Creating a pBuffer drawable for the new context\n");
301 pbuffer = GL_EXTCALL(wglCreatePbufferARB(hdc_parent, iPixelFormat, target->currentDesc.Width, target->currentDesc.Height, 0));
302 if(!pbuffer) {
303 ERR("Cannot create a pbuffer\n");
304 ReleaseDC(win_handle, hdc_parent);
305 goto out;
308 /* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
309 hdc = GL_EXTCALL(wglGetPbufferDCARB(pbuffer));
310 if(!hdc) {
311 ERR("Cannot get a HDC for pbuffer (%p)\n", pbuffer);
312 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
313 ReleaseDC(win_handle, hdc_parent);
314 goto out;
316 ReleaseDC(win_handle, hdc_parent);
317 } else {
318 PIXELFORMATDESCRIPTOR pfd;
319 int iPixelFormat;
320 int res;
321 WINED3DFORMAT ColorFormat = target->resource.format;
322 WINED3DFORMAT DepthStencilFormat = 0;
323 BOOL auxBuffers = FALSE;
325 hdc = GetDC(win_handle);
326 if(hdc == NULL) {
327 ERR("Cannot retrieve a device context!\n");
328 goto out;
331 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
332 if(wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER) {
333 auxBuffers = TRUE;
335 if(target->resource.format == WINED3DFMT_X4R4G4B4)
336 ColorFormat = WINED3DFMT_A4R4G4B4;
337 else if(target->resource.format == WINED3DFMT_X8R8G8B8)
338 ColorFormat = WINED3DFMT_A8R8G8B8;
341 /* DirectDraw supports 8bit paletted render targets and these are used by old games like Starcraft and C&C.
342 * Most modern hardware doesn't support 8bit natively so we perform some form of 8bit -> 32bit conversion.
343 * The conversion (ab)uses the alpha component for storing the palette index. For this reason we require
344 * a format with 8bit alpha, so request A8R8G8B8. */
345 if(ColorFormat == WINED3DFMT_P8)
346 ColorFormat = WINED3DFMT_A8R8G8B8;
348 /* Retrieve the depth stencil format from the present parameters.
349 * The choice of the proper format can give a nice performance boost
350 * in case of GPU limited programs. */
351 if(pPresentParms->EnableAutoDepthStencil) {
352 TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms->AutoDepthStencilFormat));
353 DepthStencilFormat = pPresentParms->AutoDepthStencilFormat;
356 /* Try to find a pixel format which matches our requirements */
357 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, ColorFormat, DepthStencilFormat, auxBuffers, FALSE /* PBUFFER */, FALSE /* findCompatible */);
359 /* Try to locate a compatible format if we weren't able to find anything */
360 if(!iPixelFormat) {
361 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
362 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, ColorFormat, DepthStencilFormat, auxBuffers, FALSE /* PBUFFER */, TRUE /* findCompatible */ );
365 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
366 if(!iPixelFormat) {
367 ERR("Can't find a suitable iPixelFormat\n");
368 return FALSE;
371 DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd);
372 res = SetPixelFormat(hdc, iPixelFormat, NULL);
373 if(!res) {
374 int oldPixelFormat = GetPixelFormat(hdc);
376 /* By default WGL doesn't allow pixel format adjustments but we need it here.
377 * For this reason there is a WINE-specific wglSetPixelFormat which allows you to
378 * set the pixel format multiple times. Only use it when it is really needed. */
380 if(oldPixelFormat == iPixelFormat) {
381 /* We don't have to do anything as the formats are the same :) */
382 } else if(oldPixelFormat && GL_SUPPORT(WGL_WINE_PIXEL_FORMAT_PASSTHROUGH)) {
383 res = GL_EXTCALL(wglSetPixelFormatWINE(hdc, iPixelFormat, NULL));
385 if(!res) {
386 ERR("wglSetPixelFormatWINE failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
387 return FALSE;
389 } else if(oldPixelFormat) {
390 /* OpenGL doesn't allow pixel format adjustments. Print an error and continue using the old format.
391 * There's a big chance that the old format works although with a performance hit and perhaps rendering errors. */
392 ERR("HDC=%p is already set to iPixelFormat=%d and OpenGL doesn't allow changes!\n", hdc, oldPixelFormat);
393 } else {
394 ERR("SetPixelFormat failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
395 return FALSE;
399 #undef PUSH1
400 #undef PUSH2
402 ctx = pwglCreateContext(hdc);
403 if(This->numContexts) pwglShareLists(This->contexts[0]->glCtx, ctx);
405 if(!ctx) {
406 ERR("Failed to create a WGL context\n");
407 if(create_pbuffer) {
408 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
409 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
411 goto out;
413 ret = AddContextToArray(This, win_handle, hdc, ctx, pbuffer);
414 if(!ret) {
415 ERR("Failed to add the newly created context to the context list\n");
416 pwglDeleteContext(ctx);
417 if(create_pbuffer) {
418 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
419 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
421 goto out;
423 ret->surface = (IWineD3DSurface *) target;
424 ret->isPBuffer = create_pbuffer;
425 ret->tid = GetCurrentThreadId();
426 if(This->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *) This)) {
427 /* Create the dirty constants array and initialize them to dirty */
428 ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
429 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
430 ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
431 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
432 memset(ret->vshader_const_dirty, 1,
433 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
434 memset(ret->pshader_const_dirty, 1,
435 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
438 TRACE("Successfully created new context %p\n", ret);
440 /* Set up the context defaults */
441 oldCtx = pwglGetCurrentContext();
442 oldDrawable = pwglGetCurrentDC();
443 if(oldCtx && oldDrawable) {
444 /* See comment in ActivateContext context switching */
445 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, FALSE);
447 if(pwglMakeCurrent(hdc, ctx) == FALSE) {
448 ERR("Cannot activate context to set up defaults\n");
449 goto out;
452 ENTER_GL();
454 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
456 TRACE("Setting up the screen\n");
457 /* Clear the screen */
458 glClearColor(1.0, 0.0, 0.0, 0.0);
459 checkGLcall("glClearColor");
460 glClearIndex(0);
461 glClearDepth(1);
462 glClearStencil(0xffff);
464 checkGLcall("glClear");
466 glColor3f(1.0, 1.0, 1.0);
467 checkGLcall("glColor3f");
469 glEnable(GL_LIGHTING);
470 checkGLcall("glEnable");
472 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
473 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
475 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
476 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
478 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
479 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
481 glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);
482 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
483 glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);
484 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
486 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
487 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
488 * and textures in DIB sections(due to the memory protection).
490 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
491 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
493 if(GL_SUPPORT(ARB_VERTEX_BLEND)) {
494 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
495 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
496 * GL_VERTEX_BLEND_ARB isn't enabled too
498 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
499 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
501 if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
502 glEnable(GL_TEXTURE_SHADER_NV);
503 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
505 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
506 * the previous texture where to source the offset from is always unit - 1.
508 for(s = 1; s < GL_LIMITS(textures); s++) {
509 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
510 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
511 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
515 if(GL_SUPPORT(ARB_POINT_SPRITE)) {
516 for(s = 0; s < GL_LIMITS(textures); s++) {
517 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
518 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
519 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
522 LEAVE_GL();
524 /* Never keep GL_FRAGMENT_SHADER_ATI enabled on a context that we switch away from,
525 * but enable it for the first context we create, and reenable it on the old context
527 if(oldDrawable && oldCtx) {
528 pwglMakeCurrent(oldDrawable, oldCtx);
530 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
532 out:
533 return ret;
536 /*****************************************************************************
537 * RemoveContextFromArray
539 * Removes a context from the context manager. The opengl context is not
540 * destroyed or unset. context is not a valid pointer after that call.
542 * Similar to the former call this isn't a performance critical function. A
543 * helper function for DestroyContext.
545 * Params:
546 * This: Device to activate the context for
547 * context: Context to remove
549 *****************************************************************************/
550 static void RemoveContextFromArray(IWineD3DDeviceImpl *This, WineD3DContext *context) {
551 UINT t, s;
552 WineD3DContext **oldArray = This->contexts;
554 TRACE("Removing ctx %p\n", context);
556 This->numContexts--;
558 if(This->numContexts) {
559 This->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->contexts) * This->numContexts);
560 if(!This->contexts) {
561 ERR("Cannot allocate a new context array, PANIC!!!\n");
563 t = 0;
564 for(s = 0; s < This->numContexts; s++) {
565 if(oldArray[s] == context) continue;
566 This->contexts[t] = oldArray[s];
567 t++;
569 } else {
570 This->contexts = NULL;
573 HeapFree(GetProcessHeap(), 0, context);
574 HeapFree(GetProcessHeap(), 0, oldArray);
577 /*****************************************************************************
578 * DestroyContext
580 * Destroys a wineD3DContext
582 * Params:
583 * This: Device to activate the context for
584 * context: Context to destroy
586 *****************************************************************************/
587 void DestroyContext(IWineD3DDeviceImpl *This, WineD3DContext *context) {
589 /* check that we are the current context first */
590 TRACE("Destroying ctx %p\n", context);
591 if(pwglGetCurrentContext() == context->glCtx){
592 pwglMakeCurrent(NULL, NULL);
595 if(context->isPBuffer) {
596 GL_EXTCALL(wglReleasePbufferDCARB(context->pbuffer, context->hdc));
597 GL_EXTCALL(wglDestroyPbufferARB(context->pbuffer));
598 } else ReleaseDC(context->win_handle, context->hdc);
599 pwglDeleteContext(context->glCtx);
601 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
602 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
603 RemoveContextFromArray(This, context);
606 /*****************************************************************************
607 * SetupForBlit
609 * Sets up a context for DirectDraw blitting.
610 * All texture units are disabled, texture unit 0 is set as current unit
611 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
612 * color writing enabled for all channels
613 * register combiners disabled, shaders disabled
614 * world matrix is set to identity, texture matrix 0 too
615 * projection matrix is setup for drawing screen coordinates
617 * Params:
618 * This: Device to activate the context for
619 * context: Context to setup
620 * width: render target width
621 * height: render target height
623 *****************************************************************************/
624 static inline void SetupForBlit(IWineD3DDeviceImpl *This, WineD3DContext *context, UINT width, UINT height) {
625 int i;
626 const struct StateEntry *StateTable = This->shader_backend->StateTable;
628 TRACE("Setting up context %p for blitting\n", context);
629 if(context->last_was_blit) {
630 TRACE("Context is already set up for blitting, nothing to do\n");
631 return;
633 context->last_was_blit = TRUE;
635 /* TODO: Use a display list */
637 /* Disable shaders */
638 This->shader_backend->shader_cleanup((IWineD3DDevice *) This);
639 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
640 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
642 /* Disable all textures. The caller can then bind a texture it wants to blit
643 * from
645 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
646 glDisable(GL_REGISTER_COMBINERS_NV);
647 checkGLcall("glDisable(GL_REGISTER_COMBINERS_NV)");
649 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
650 /* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
651 * function texture unit. No need to care for higher samplers
653 for(i = GL_LIMITS(textures) - 1; i > 0 ; i--) {
654 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
655 checkGLcall("glActiveTextureARB");
657 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
658 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
659 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
661 glDisable(GL_TEXTURE_3D);
662 checkGLcall("glDisable GL_TEXTURE_3D");
663 glDisable(GL_TEXTURE_2D);
664 checkGLcall("glDisable GL_TEXTURE_2D");
666 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
667 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
669 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP), StateTable);
670 Context_MarkStateDirty(context, STATE_SAMPLER(i), StateTable);
672 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
673 checkGLcall("glActiveTextureARB");
675 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
676 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
677 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
679 glDisable(GL_TEXTURE_3D);
680 checkGLcall("glDisable GL_TEXTURE_3D");
681 glDisable(GL_TEXTURE_2D);
682 checkGLcall("glDisable GL_TEXTURE_2D");
684 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
686 glMatrixMode(GL_TEXTURE);
687 checkGLcall("glMatrixMode(GL_TEXTURE)");
688 glLoadIdentity();
689 checkGLcall("glLoadIdentity()");
690 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0), StateTable);
692 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS)) {
693 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
694 GL_TEXTURE_LOD_BIAS_EXT,
695 0.0);
696 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
698 Context_MarkStateDirty(context, STATE_SAMPLER(0), StateTable);
699 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), StateTable);
701 /* Other misc states */
702 glDisable(GL_ALPHA_TEST);
703 checkGLcall("glDisable(GL_ALPHA_TEST)");
704 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
705 glDisable(GL_LIGHTING);
706 checkGLcall("glDisable GL_LIGHTING");
707 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
708 glDisable(GL_DEPTH_TEST);
709 checkGLcall("glDisable GL_DEPTH_TEST");
710 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
711 glDisable(GL_FOG);
712 checkGLcall("glDisable GL_FOG");
713 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
714 glDisable(GL_BLEND);
715 checkGLcall("glDisable GL_BLEND");
716 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
717 glDisable(GL_CULL_FACE);
718 checkGLcall("glDisable GL_CULL_FACE");
719 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
720 glDisable(GL_STENCIL_TEST);
721 checkGLcall("glDisable GL_STENCIL_TEST");
722 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
723 glDisable(GL_SCISSOR_TEST);
724 checkGLcall("glDisable GL_SCISSOR_TEST");
725 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
726 if(GL_SUPPORT(ARB_POINT_SPRITE)) {
727 glDisable(GL_POINT_SPRITE_ARB);
728 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
729 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
731 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
732 checkGLcall("glColorMask");
733 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
734 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
735 glDisable(GL_COLOR_SUM_EXT);
736 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
737 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
739 if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
740 GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
741 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
742 checkGLcall("glFinalCombinerInputNV");
745 /* Setup transforms */
746 glMatrixMode(GL_MODELVIEW);
747 checkGLcall("glMatrixMode(GL_MODELVIEW)");
748 glLoadIdentity();
749 checkGLcall("glLoadIdentity()");
750 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
752 glMatrixMode(GL_PROJECTION);
753 checkGLcall("glMatrixMode(GL_PROJECTION)");
754 glLoadIdentity();
755 checkGLcall("glLoadIdentity()");
756 glOrtho(0, width, height, 0, 0.0, -1.0);
757 checkGLcall("glOrtho");
758 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
760 context->last_was_rhw = TRUE;
761 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
763 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
764 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
765 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
766 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
767 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
768 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
769 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
771 glViewport(0, 0, width, height);
772 checkGLcall("glViewport");
773 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
775 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, FALSE);
778 /*****************************************************************************
779 * findThreadContextForSwapChain
781 * Searches a swapchain for all contexts and picks one for the thread tid.
782 * If none can be found the swapchain is requested to create a new context
784 *****************************************************************************/
785 static WineD3DContext *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid) {
786 int i;
788 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
789 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
790 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
795 /* Create a new context for the thread */
796 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain);
799 /*****************************************************************************
800 * FindContext
802 * Finds a context for the current render target and thread
804 * Parameters:
805 * target: Render target to find the context for
806 * tid: Thread to activate the context for
808 * Returns: The needed context
810 *****************************************************************************/
811 static inline WineD3DContext *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, DWORD tid, GLint *buffer) {
812 IWineD3DSwapChain *swapchain = NULL;
813 HRESULT hr;
814 BOOL readTexture = wined3d_settings.offscreen_rendering_mode != ORM_FBO && This->render_offscreen;
815 WineD3DContext *context = This->activeContext;
816 BOOL oldRenderOffscreen = This->render_offscreen;
817 const WINED3DFORMAT oldFmt = ((IWineD3DSurfaceImpl *) This->lastActiveRenderTarget)->resource.format;
818 const WINED3DFORMAT newFmt = ((IWineD3DSurfaceImpl *) target)->resource.format;
819 const struct StateEntry *StateTable = This->shader_backend->StateTable;
821 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
822 * the alpha blend state changes with different render target formats
824 if(oldFmt != newFmt) {
825 const GlPixelFormatDesc *glDesc;
826 const StaticPixelFormatDesc *old = getFormatDescEntry(oldFmt, NULL, NULL);
827 const StaticPixelFormatDesc *new = getFormatDescEntry(newFmt, &GLINFO_LOCATION, &glDesc);
829 /* Disable blending when the alphaMask has changed and when a format doesn't support blending */
830 if((old->alphaMask && !new->alphaMask) || (!old->alphaMask && new->alphaMask) || !(glDesc->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING)) {
831 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
835 hr = IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **) &swapchain);
836 if(hr == WINED3D_OK && swapchain) {
837 TRACE("Rendering onscreen\n");
839 context = findThreadContextForSwapChain(swapchain, tid);
841 This->render_offscreen = FALSE;
842 /* The context != This->activeContext will catch a NOP context change. This can occur
843 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
844 * rendering. No context change is needed in that case
847 if(((IWineD3DSwapChainImpl *) swapchain)->frontBuffer == target) {
848 *buffer = GL_FRONT;
849 } else {
850 *buffer = GL_BACK;
852 if(wined3d_settings.offscreen_rendering_mode == ORM_PBUFFER) {
853 if(This->pbufferContext && tid == This->pbufferContext->tid) {
854 This->pbufferContext->tid = 0;
857 IWineD3DSwapChain_Release(swapchain);
859 if(oldRenderOffscreen) {
860 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
861 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
862 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
863 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
864 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
867 } else {
868 TRACE("Rendering offscreen\n");
869 This->render_offscreen = TRUE;
870 *buffer = This->offscreenBuffer;
872 switch(wined3d_settings.offscreen_rendering_mode) {
873 case ORM_FBO:
874 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
875 if(This->activeContext && tid == This->lastThread) {
876 context = This->activeContext;
877 } else {
878 /* This may happen if the app jumps straight into offscreen rendering
879 * Start using the context of the primary swapchain. tid == 0 is no problem
880 * for findThreadContextForSwapChain.
882 * Can also happen on thread switches - in that case findThreadContextForSwapChain
883 * is perfect to call.
885 context = findThreadContextForSwapChain(This->swapchains[0], tid);
887 break;
889 case ORM_PBUFFER:
891 IWineD3DSurfaceImpl *targetimpl = (IWineD3DSurfaceImpl *) target;
892 if(This->pbufferContext == NULL ||
893 This->pbufferWidth < targetimpl->currentDesc.Width ||
894 This->pbufferHeight < targetimpl->currentDesc.Height) {
895 if(This->pbufferContext) {
896 DestroyContext(This, This->pbufferContext);
899 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
900 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
902 This->pbufferContext = CreateContext(This, targetimpl,
903 ((IWineD3DSwapChainImpl *) This->swapchains[0])->context[0]->win_handle,
904 TRUE /* pbuffer */, &((IWineD3DSwapChainImpl *)This->swapchains[0])->presentParms);
905 This->pbufferWidth = targetimpl->currentDesc.Width;
906 This->pbufferHeight = targetimpl->currentDesc.Height;
909 if(This->pbufferContext) {
910 if(This->pbufferContext->tid != 0 && This->pbufferContext->tid != tid) {
911 FIXME("The PBuffr context is only supported for one thread for now!\n");
913 This->pbufferContext->tid = tid;
914 context = This->pbufferContext;
915 break;
916 } else {
917 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
918 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
922 case ORM_BACKBUFFER:
923 /* Stay with the currently active context for back buffer rendering */
924 if(This->activeContext && tid == This->lastThread) {
925 context = This->activeContext;
926 } else {
927 /* This may happen if the app jumps straight into offscreen rendering
928 * Start using the context of the primary swapchain. tid == 0 is no problem
929 * for findThreadContextForSwapChain.
931 * Can also happen on thread switches - in that case findThreadContextForSwapChain
932 * is perfect to call.
934 context = findThreadContextForSwapChain(This->swapchains[0], tid);
936 break;
939 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
940 /* Make sure we have a OpenGL texture name so the PreLoad() used to read the buffer
941 * back when we are done won't mark us dirty.
943 IWineD3DSurface_PreLoad(target);
946 if(!oldRenderOffscreen) {
947 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
948 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
949 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
950 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
951 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
954 if (readTexture) {
955 BOOL oldInDraw = This->isInDraw;
957 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
958 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
959 * when using offscreen rendering with multithreading
961 This->isInDraw = TRUE;
963 /* Do that before switching the context:
964 * Read the back buffer of the old drawable into the destination texture
966 IWineD3DSurface_PreLoad(This->lastActiveRenderTarget);
968 /* Assume that the drawable will be modified by some other things now */
969 IWineD3DSurface_ModifyLocation(This->lastActiveRenderTarget, SFLAG_INDRAWABLE, FALSE);
971 This->isInDraw = oldInDraw;
974 if(oldRenderOffscreen != This->render_offscreen && This->depth_copy_state != WINED3D_DCS_NO_COPY) {
975 This->depth_copy_state = WINED3D_DCS_COPY;
977 return context;
980 /*****************************************************************************
981 * ActivateContext
983 * Finds a rendering context and drawable matching the device and render
984 * target for the current thread, activates them and puts them into the
985 * requested state.
987 * Params:
988 * This: Device to activate the context for
989 * target: Requested render target
990 * usage: Prepares the context for blitting, drawing or other actions
992 *****************************************************************************/
993 void ActivateContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, ContextUsage usage) {
994 DWORD tid = GetCurrentThreadId();
995 int i;
996 DWORD dirtyState, idx;
997 BYTE shift;
998 WineD3DContext *context;
999 GLint drawBuffer=0;
1000 const struct StateEntry *StateTable = This->shader_backend->StateTable;
1002 TRACE("(%p): Selecting context for render target %p, thread %d\n", This, target, tid);
1003 if(This->lastActiveRenderTarget != target || tid != This->lastThread) {
1004 context = FindContext(This, target, tid, &drawBuffer);
1005 This->lastActiveRenderTarget = target;
1006 This->lastThread = tid;
1007 } else {
1008 /* Stick to the old context */
1009 context = This->activeContext;
1012 /* Activate the opengl context */
1013 if(context != This->activeContext) {
1014 BOOL ret;
1016 /* Prevent an unneeded context switch as those are expensive */
1017 if(context->glCtx && (context->glCtx == pwglGetCurrentContext())) {
1018 TRACE("Already using gl context %p\n", context->glCtx);
1020 else {
1021 TRACE("Switching gl ctx to %p, hdc=%p ctx=%p\n", context, context->hdc, context->glCtx);
1023 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, FALSE);
1024 ret = pwglMakeCurrent(context->hdc, context->glCtx);
1025 if(ret == FALSE) {
1026 ERR("Failed to activate the new context\n");
1027 } else if(!context->last_was_blit) {
1028 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
1031 if(This->activeContext->vshader_const_dirty) {
1032 memset(This->activeContext->vshader_const_dirty, 1,
1033 sizeof(*This->activeContext->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
1035 if(This->activeContext->pshader_const_dirty) {
1036 memset(This->activeContext->pshader_const_dirty, 1,
1037 sizeof(*This->activeContext->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
1039 This->activeContext = context;
1042 /* We only need ENTER_GL for the gl calls made below and for the helper functions which make GL calls */
1043 ENTER_GL();
1044 /* Select the right draw buffer. It is selected in FindContext. */
1045 if(drawBuffer && context->last_draw_buffer != drawBuffer) {
1046 TRACE("Drawing to buffer: %#x\n", drawBuffer);
1047 context->last_draw_buffer = drawBuffer;
1049 glDrawBuffer(drawBuffer);
1050 checkGLcall("glDrawBuffer");
1053 switch(usage) {
1054 case CTXUSAGE_RESOURCELOAD:
1055 /* This does not require any special states to be set up */
1056 break;
1058 case CTXUSAGE_CLEAR:
1059 if(context->last_was_blit) {
1060 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
1063 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
1064 * blending when clearing improves the clearing performance incredibly.
1066 glDisable(GL_BLEND);
1067 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1069 glEnable(GL_SCISSOR_TEST);
1070 checkGLcall("glEnable GL_SCISSOR_TEST");
1071 context->last_was_blit = FALSE;
1072 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1073 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1074 break;
1076 case CTXUSAGE_DRAWPRIM:
1077 /* This needs all dirty states applied */
1078 if(context->last_was_blit) {
1079 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
1082 IWineD3DDeviceImpl_FindTexUnitMap(This);
1084 for(i=0; i < context->numDirtyEntries; i++) {
1085 dirtyState = context->dirtyArray[i];
1086 idx = dirtyState >> 5;
1087 shift = dirtyState & 0x1f;
1088 context->isStateDirty[idx] &= ~(1 << shift);
1089 StateTable[dirtyState].apply(dirtyState, This->stateBlock, context);
1091 context->numDirtyEntries = 0; /* This makes the whole list clean */
1092 context->last_was_blit = FALSE;
1093 break;
1095 case CTXUSAGE_BLIT:
1096 SetupForBlit(This, context,
1097 ((IWineD3DSurfaceImpl *)target)->currentDesc.Width,
1098 ((IWineD3DSurfaceImpl *)target)->currentDesc.Height);
1099 break;
1101 default:
1102 FIXME("Unexpected context usage requested\n");
1104 LEAVE_GL();