gdiplus: Implemented GdipGetPenEndCap.
[wine.git] / dlls / wined3d / context.c
blob382f96d6348f642583cd7d0c4fd6f37af206ca48
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, int numSamples, 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 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, pbuffer=%d, findCompatible=%d\n",
125 debug_d3dformat(ColorFormat), debug_d3dformat(DepthStencilFormat), auxBuffers, numSamples, pbuffer, findCompatible);
127 if(!getColorBits(ColorFormat, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits)) {
128 ERR("Unable to get color bits for format %s (%#x)!\n", debug_d3dformat(ColorFormat), ColorFormat);
129 return 0;
132 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
133 * You are able to add a depth + stencil surface at a later stage when you need it.
134 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
135 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
136 * context, need torecreate shaders, textures and other resources.
138 * The context manager already takes care of the state problem and for the other tasks code from Reset
139 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
140 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
141 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
142 * issue needs to be fixed. */
143 if(DepthStencilFormat != WINED3DFMT_D24S8)
144 FIXME("Add OpenGL context recreation support to SetDepthStencilSurface\n");
146 DepthStencilFormat = WINED3DFMT_D24S8;
148 if(DepthStencilFormat) {
149 getDepthStencilBits(DepthStencilFormat, &depthBits, &stencilBits);
152 /* Find a pixel format which EXACTLY matches our requirements (except for depth) */
153 for(i=0; i<nCfgs; i++) {
154 BOOL exactDepthMatch = TRUE;
155 cfgs = &This->adapter->cfgs[i];
157 /* For now only accept RGBA formats. Perhaps some day we will
158 * allow floating point formats for pbuffers. */
159 if(cfgs->iPixelType != WGL_TYPE_RGBA_ARB)
160 continue;
162 /* In window mode (!pbuffer) we need a window drawable format and double buffering. */
163 if(!pbuffer && !(cfgs->windowDrawable && cfgs->doubleBuffer))
164 continue;
166 /* We like to have aux buffers in backbuffer mode */
167 if(auxBuffers && !cfgs->auxBuffers)
168 continue;
170 /* In pbuffer-mode we need a pbuffer-capable format but we don't want double buffering */
171 if(pbuffer && (!cfgs->pbufferDrawable || cfgs->doubleBuffer))
172 continue;
174 if(cfgs->redSize != redBits)
175 continue;
176 if(cfgs->greenSize != greenBits)
177 continue;
178 if(cfgs->blueSize != blueBits)
179 continue;
180 if(cfgs->alphaSize != alphaBits)
181 continue;
183 /* We try to locate a format which matches our requirements exactly. In case of
184 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
185 if(cfgs->depthSize < depthBits)
186 continue;
187 else if(cfgs->depthSize > depthBits)
188 exactDepthMatch = FALSE;
190 /* In all cases make sure the number of stencil bits matches our requirements
191 * even when we don't need stencil because it could affect performance EXCEPT
192 * on cards which don't offer depth formats without stencil like the i915 drivers
193 * on Linux. */
194 if(stencilBits != cfgs->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfgs->stencilSize))
195 continue;
197 /* Check multisampling support */
198 if(cfgs->numSamples != numSamples)
199 continue;
201 /* When we have passed all the checks then we have found a format which matches our
202 * requirements. Note that we only check for a limit number of capabilities right now,
203 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
204 * can still differ in things like multisampling, stereo, SRGB and other flags.
207 /* Exit the loop as we have found a format :) */
208 if(exactDepthMatch) {
209 iPixelFormat = cfgs->iPixelFormat;
210 break;
211 } else if(!iPixelFormat) {
212 /* In the end we might end up with a format which doesn't exactly match our depth
213 * requirements. Accept the first format we found because formats with higher iPixelFormat
214 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
215 iPixelFormat = cfgs->iPixelFormat;
219 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
220 if(!iPixelFormat && !findCompatible) {
221 ERR("Can't find a suitable iPixelFormat\n");
222 return FALSE;
223 } else if(!iPixelFormat) {
224 PIXELFORMATDESCRIPTOR pfd;
226 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
227 /* PixelFormat selection */
228 ZeroMemory(&pfd, sizeof(pfd));
229 pfd.nSize = sizeof(pfd);
230 pfd.nVersion = 1;
231 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
232 pfd.iPixelType = PFD_TYPE_RGBA;
233 pfd.cAlphaBits = alphaBits;
234 pfd.cColorBits = colorBits;
235 pfd.cDepthBits = depthBits;
236 pfd.cStencilBits = stencilBits;
237 pfd.iLayerType = PFD_MAIN_PLANE;
239 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
240 if(!iPixelFormat) {
241 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
242 ERR("Can't find a suitable iPixelFormat\n");
243 return FALSE;
247 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n", iPixelFormat, debug_d3dformat(ColorFormat), debug_d3dformat(DepthStencilFormat));
248 return iPixelFormat;
251 /*****************************************************************************
252 * CreateContext
254 * Creates a new context for a window, or a pbuffer context.
256 * * Params:
257 * This: Device to activate the context for
258 * target: Surface this context will render to
259 * win_handle: handle to the window which we are drawing to
260 * create_pbuffer: tells whether to create a pbuffer or not
261 * pPresentParameters: contains the pixelformats to use for onscreen rendering
263 *****************************************************************************/
264 WineD3DContext *CreateContext(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *target, HWND win_handle, BOOL create_pbuffer, const WINED3DPRESENT_PARAMETERS *pPresentParms) {
265 HDC oldDrawable, hdc;
266 HPBUFFERARB pbuffer = NULL;
267 HGLRC ctx = NULL, oldCtx;
268 WineD3DContext *ret = NULL;
269 int s;
271 TRACE("(%p): Creating a %s context for render target %p\n", This, create_pbuffer ? "offscreen" : "onscreen", target);
273 if(create_pbuffer) {
274 HDC hdc_parent = GetDC(win_handle);
275 int iPixelFormat = 0;
277 IWineD3DSurface *StencilSurface = This->stencilBufferTarget;
278 WINED3DFORMAT StencilBufferFormat = (NULL != StencilSurface) ? ((IWineD3DSurfaceImpl *) StencilSurface)->resource.format : 0;
280 /* Try to find a pixel format with pbuffer support. */
281 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc_parent, target->resource.format, StencilBufferFormat, FALSE /* auxBuffers */, 0 /* numSamples */, TRUE /* PBUFFER */, FALSE /* findCompatible */);
282 if(!iPixelFormat) {
283 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
285 /* For some reason we weren't able to find a format, try to find something instead of crashing.
286 * A reason for failure could have been wglChoosePixelFormatARB strictness. */
287 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc_parent, target->resource.format, StencilBufferFormat, FALSE /* auxBuffer */, 0 /* numSamples */, TRUE /* PBUFFER */, TRUE /* findCompatible */);
290 /* This shouldn't happen as ChoosePixelFormat always returns something */
291 if(!iPixelFormat) {
292 ERR("Unable to locate a pixel format for a pbuffer\n");
293 ReleaseDC(win_handle, hdc_parent);
294 goto out;
297 TRACE("Creating a pBuffer drawable for the new context\n");
298 pbuffer = GL_EXTCALL(wglCreatePbufferARB(hdc_parent, iPixelFormat, target->currentDesc.Width, target->currentDesc.Height, 0));
299 if(!pbuffer) {
300 ERR("Cannot create a pbuffer\n");
301 ReleaseDC(win_handle, hdc_parent);
302 goto out;
305 /* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
306 hdc = GL_EXTCALL(wglGetPbufferDCARB(pbuffer));
307 if(!hdc) {
308 ERR("Cannot get a HDC for pbuffer (%p)\n", pbuffer);
309 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
310 ReleaseDC(win_handle, hdc_parent);
311 goto out;
313 ReleaseDC(win_handle, hdc_parent);
314 } else {
315 PIXELFORMATDESCRIPTOR pfd;
316 int iPixelFormat;
317 int res;
318 WINED3DFORMAT ColorFormat = target->resource.format;
319 WINED3DFORMAT DepthStencilFormat = 0;
320 BOOL auxBuffers = FALSE;
321 int numSamples = 0;
323 hdc = GetDC(win_handle);
324 if(hdc == NULL) {
325 ERR("Cannot retrieve a device context!\n");
326 goto out;
329 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
330 if(wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER) {
331 auxBuffers = TRUE;
333 if(target->resource.format == WINED3DFMT_X4R4G4B4)
334 ColorFormat = WINED3DFMT_A4R4G4B4;
335 else if(target->resource.format == WINED3DFMT_X8R8G8B8)
336 ColorFormat = WINED3DFMT_A8R8G8B8;
339 /* DirectDraw supports 8bit paletted render targets and these are used by old games like Starcraft and C&C.
340 * Most modern hardware doesn't support 8bit natively so we perform some form of 8bit -> 32bit conversion.
341 * The conversion (ab)uses the alpha component for storing the palette index. For this reason we require
342 * a format with 8bit alpha, so request A8R8G8B8. */
343 if(ColorFormat == WINED3DFMT_P8)
344 ColorFormat = WINED3DFMT_A8R8G8B8;
346 /* Retrieve the depth stencil format from the present parameters.
347 * The choice of the proper format can give a nice performance boost
348 * in case of GPU limited programs. */
349 if(pPresentParms->EnableAutoDepthStencil) {
350 TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms->AutoDepthStencilFormat));
351 DepthStencilFormat = pPresentParms->AutoDepthStencilFormat;
354 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD */
355 if(pPresentParms->MultiSampleType && (pPresentParms->SwapEffect == WINED3DSWAPEFFECT_DISCARD)) {
356 if(!GL_SUPPORT(ARB_MULTISAMPLE))
357 ERR("The program is requesting multisampling without support!\n");
358 else {
359 ERR("Requesting MultiSampleType=%d\n", pPresentParms->MultiSampleType);
360 numSamples = pPresentParms->MultiSampleType;
364 /* Try to find a pixel format which matches our requirements */
365 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, ColorFormat, DepthStencilFormat, auxBuffers, numSamples, FALSE /* PBUFFER */, FALSE /* findCompatible */);
367 /* Try to locate a compatible format if we weren't able to find anything */
368 if(!iPixelFormat) {
369 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
370 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, ColorFormat, DepthStencilFormat, auxBuffers, 0 /* numSamples */, FALSE /* PBUFFER */, TRUE /* findCompatible */ );
373 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
374 if(!iPixelFormat) {
375 ERR("Can't find a suitable iPixelFormat\n");
376 return FALSE;
379 DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd);
380 res = SetPixelFormat(hdc, iPixelFormat, NULL);
381 if(!res) {
382 int oldPixelFormat = GetPixelFormat(hdc);
384 /* By default WGL doesn't allow pixel format adjustments but we need it here.
385 * For this reason there is a WINE-specific wglSetPixelFormat which allows you to
386 * set the pixel format multiple times. Only use it when it is really needed. */
388 if(oldPixelFormat == iPixelFormat) {
389 /* We don't have to do anything as the formats are the same :) */
390 } else if(oldPixelFormat && GL_SUPPORT(WGL_WINE_PIXEL_FORMAT_PASSTHROUGH)) {
391 res = GL_EXTCALL(wglSetPixelFormatWINE(hdc, iPixelFormat, NULL));
393 if(!res) {
394 ERR("wglSetPixelFormatWINE failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
395 return FALSE;
397 } else if(oldPixelFormat) {
398 /* OpenGL doesn't allow pixel format adjustments. Print an error and continue using the old format.
399 * There's a big chance that the old format works although with a performance hit and perhaps rendering errors. */
400 ERR("HDC=%p is already set to iPixelFormat=%d and OpenGL doesn't allow changes!\n", hdc, oldPixelFormat);
401 } else {
402 ERR("SetPixelFormat failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
403 return FALSE;
408 ctx = pwglCreateContext(hdc);
409 if(This->numContexts) pwglShareLists(This->contexts[0]->glCtx, ctx);
411 if(!ctx) {
412 ERR("Failed to create a WGL context\n");
413 if(create_pbuffer) {
414 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
415 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
417 goto out;
419 ret = AddContextToArray(This, win_handle, hdc, ctx, pbuffer);
420 if(!ret) {
421 ERR("Failed to add the newly created context to the context list\n");
422 pwglDeleteContext(ctx);
423 if(create_pbuffer) {
424 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
425 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
427 goto out;
429 ret->surface = (IWineD3DSurface *) target;
430 ret->isPBuffer = create_pbuffer;
431 ret->tid = GetCurrentThreadId();
432 if(This->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *) This)) {
433 /* Create the dirty constants array and initialize them to dirty */
434 ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
435 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
436 ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
437 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
438 memset(ret->vshader_const_dirty, 1,
439 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
440 memset(ret->pshader_const_dirty, 1,
441 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
444 TRACE("Successfully created new context %p\n", ret);
446 /* Set up the context defaults */
447 oldCtx = pwglGetCurrentContext();
448 oldDrawable = pwglGetCurrentDC();
449 if(oldCtx && oldDrawable) {
450 /* See comment in ActivateContext context switching */
451 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, FALSE);
453 if(pwglMakeCurrent(hdc, ctx) == FALSE) {
454 ERR("Cannot activate context to set up defaults\n");
455 goto out;
458 ENTER_GL();
460 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
462 TRACE("Setting up the screen\n");
463 /* Clear the screen */
464 glClearColor(1.0, 0.0, 0.0, 0.0);
465 checkGLcall("glClearColor");
466 glClearIndex(0);
467 glClearDepth(1);
468 glClearStencil(0xffff);
470 checkGLcall("glClear");
472 glColor3f(1.0, 1.0, 1.0);
473 checkGLcall("glColor3f");
475 glEnable(GL_LIGHTING);
476 checkGLcall("glEnable");
478 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
479 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
481 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
482 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
484 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
485 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
487 glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);
488 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
489 glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);
490 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
492 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
493 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
494 * and textures in DIB sections(due to the memory protection).
496 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
497 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
499 if(GL_SUPPORT(ARB_VERTEX_BLEND)) {
500 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
501 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
502 * GL_VERTEX_BLEND_ARB isn't enabled too
504 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
505 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
507 if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
508 glEnable(GL_TEXTURE_SHADER_NV);
509 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
511 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
512 * the previous texture where to source the offset from is always unit - 1.
514 for(s = 1; s < GL_LIMITS(textures); s++) {
515 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
516 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
517 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
521 if(GL_SUPPORT(ARB_POINT_SPRITE)) {
522 for(s = 0; s < GL_LIMITS(textures); s++) {
523 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
524 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
525 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
528 LEAVE_GL();
530 /* Never keep GL_FRAGMENT_SHADER_ATI enabled on a context that we switch away from,
531 * but enable it for the first context we create, and reenable it on the old context
533 if(oldDrawable && oldCtx) {
534 pwglMakeCurrent(oldDrawable, oldCtx);
536 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
538 out:
539 return ret;
542 /*****************************************************************************
543 * RemoveContextFromArray
545 * Removes a context from the context manager. The opengl context is not
546 * destroyed or unset. context is not a valid pointer after that call.
548 * Similar to the former call this isn't a performance critical function. A
549 * helper function for DestroyContext.
551 * Params:
552 * This: Device to activate the context for
553 * context: Context to remove
555 *****************************************************************************/
556 static void RemoveContextFromArray(IWineD3DDeviceImpl *This, WineD3DContext *context) {
557 UINT t, s;
558 WineD3DContext **oldArray = This->contexts;
560 TRACE("Removing ctx %p\n", context);
562 This->numContexts--;
564 if(This->numContexts) {
565 This->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->contexts) * This->numContexts);
566 if(!This->contexts) {
567 ERR("Cannot allocate a new context array, PANIC!!!\n");
569 t = 0;
570 /* Note that we decreased numContexts a few lines up, so use '<=' instead of '<' */
571 for(s = 0; s <= This->numContexts; s++) {
572 if(oldArray[s] == context) continue;
573 This->contexts[t] = oldArray[s];
574 t++;
576 } else {
577 This->contexts = NULL;
580 HeapFree(GetProcessHeap(), 0, context);
581 HeapFree(GetProcessHeap(), 0, oldArray);
584 /*****************************************************************************
585 * DestroyContext
587 * Destroys a wineD3DContext
589 * Params:
590 * This: Device to activate the context for
591 * context: Context to destroy
593 *****************************************************************************/
594 void DestroyContext(IWineD3DDeviceImpl *This, WineD3DContext *context) {
596 /* check that we are the current context first */
597 TRACE("Destroying ctx %p\n", context);
598 if(pwglGetCurrentContext() == context->glCtx){
599 pwglMakeCurrent(NULL, NULL);
602 if(context->isPBuffer) {
603 GL_EXTCALL(wglReleasePbufferDCARB(context->pbuffer, context->hdc));
604 GL_EXTCALL(wglDestroyPbufferARB(context->pbuffer));
605 } else ReleaseDC(context->win_handle, context->hdc);
606 pwglDeleteContext(context->glCtx);
608 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
609 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
610 RemoveContextFromArray(This, context);
613 /*****************************************************************************
614 * SetupForBlit
616 * Sets up a context for DirectDraw blitting.
617 * All texture units are disabled, texture unit 0 is set as current unit
618 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
619 * color writing enabled for all channels
620 * register combiners disabled, shaders disabled
621 * world matrix is set to identity, texture matrix 0 too
622 * projection matrix is setup for drawing screen coordinates
624 * Params:
625 * This: Device to activate the context for
626 * context: Context to setup
627 * width: render target width
628 * height: render target height
630 *****************************************************************************/
631 static inline void SetupForBlit(IWineD3DDeviceImpl *This, WineD3DContext *context, UINT width, UINT height) {
632 int i;
633 const struct StateEntry *StateTable = This->shader_backend->StateTable;
635 TRACE("Setting up context %p for blitting\n", context);
636 if(context->last_was_blit) {
637 TRACE("Context is already set up for blitting, nothing to do\n");
638 return;
640 context->last_was_blit = TRUE;
642 /* TODO: Use a display list */
644 /* Disable shaders */
645 This->shader_backend->shader_cleanup((IWineD3DDevice *) This);
646 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
647 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
649 /* Disable all textures. The caller can then bind a texture it wants to blit
650 * from
652 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
653 glDisable(GL_REGISTER_COMBINERS_NV);
654 checkGLcall("glDisable(GL_REGISTER_COMBINERS_NV)");
656 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
657 /* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
658 * function texture unit. No need to care for higher samplers
660 for(i = GL_LIMITS(textures) - 1; i > 0 ; i--) {
661 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
662 checkGLcall("glActiveTextureARB");
664 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
665 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
666 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
668 glDisable(GL_TEXTURE_3D);
669 checkGLcall("glDisable GL_TEXTURE_3D");
670 glDisable(GL_TEXTURE_2D);
671 checkGLcall("glDisable GL_TEXTURE_2D");
673 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
674 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
676 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(i, WINED3DTSS_COLOROP), StateTable);
677 Context_MarkStateDirty(context, STATE_SAMPLER(i), StateTable);
679 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
680 checkGLcall("glActiveTextureARB");
682 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
683 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
684 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
686 glDisable(GL_TEXTURE_3D);
687 checkGLcall("glDisable GL_TEXTURE_3D");
688 glDisable(GL_TEXTURE_2D);
689 checkGLcall("glDisable GL_TEXTURE_2D");
691 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
693 glMatrixMode(GL_TEXTURE);
694 checkGLcall("glMatrixMode(GL_TEXTURE)");
695 glLoadIdentity();
696 checkGLcall("glLoadIdentity()");
697 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0), StateTable);
699 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS)) {
700 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
701 GL_TEXTURE_LOD_BIAS_EXT,
702 0.0);
703 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
705 Context_MarkStateDirty(context, STATE_SAMPLER(0), StateTable);
706 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP), StateTable);
708 /* Other misc states */
709 glDisable(GL_ALPHA_TEST);
710 checkGLcall("glDisable(GL_ALPHA_TEST)");
711 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
712 glDisable(GL_LIGHTING);
713 checkGLcall("glDisable GL_LIGHTING");
714 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
715 glDisable(GL_DEPTH_TEST);
716 checkGLcall("glDisable GL_DEPTH_TEST");
717 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
718 glDisable(GL_FOG);
719 checkGLcall("glDisable GL_FOG");
720 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
721 glDisable(GL_BLEND);
722 checkGLcall("glDisable GL_BLEND");
723 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
724 glDisable(GL_CULL_FACE);
725 checkGLcall("glDisable GL_CULL_FACE");
726 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
727 glDisable(GL_STENCIL_TEST);
728 checkGLcall("glDisable GL_STENCIL_TEST");
729 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
730 glDisable(GL_SCISSOR_TEST);
731 checkGLcall("glDisable GL_SCISSOR_TEST");
732 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
733 if(GL_SUPPORT(ARB_POINT_SPRITE)) {
734 glDisable(GL_POINT_SPRITE_ARB);
735 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
736 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
738 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
739 checkGLcall("glColorMask");
740 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
741 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
742 glDisable(GL_COLOR_SUM_EXT);
743 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
744 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
746 if (GL_SUPPORT(NV_REGISTER_COMBINERS)) {
747 GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV, GL_SPARE0_NV, GL_UNSIGNED_IDENTITY_NV, GL_RGB));
748 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
749 checkGLcall("glFinalCombinerInputNV");
752 /* Setup transforms */
753 glMatrixMode(GL_MODELVIEW);
754 checkGLcall("glMatrixMode(GL_MODELVIEW)");
755 glLoadIdentity();
756 checkGLcall("glLoadIdentity()");
757 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
759 glMatrixMode(GL_PROJECTION);
760 checkGLcall("glMatrixMode(GL_PROJECTION)");
761 glLoadIdentity();
762 checkGLcall("glLoadIdentity()");
763 glOrtho(0, width, height, 0, 0.0, -1.0);
764 checkGLcall("glOrtho");
765 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
767 context->last_was_rhw = TRUE;
768 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
770 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
771 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
772 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
773 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
774 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
775 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
776 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
778 glViewport(0, 0, width, height);
779 checkGLcall("glViewport");
780 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
782 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, FALSE);
785 /*****************************************************************************
786 * findThreadContextForSwapChain
788 * Searches a swapchain for all contexts and picks one for the thread tid.
789 * If none can be found the swapchain is requested to create a new context
791 *****************************************************************************/
792 static WineD3DContext *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid) {
793 int i;
795 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
796 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
797 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
802 /* Create a new context for the thread */
803 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain);
806 /*****************************************************************************
807 * FindContext
809 * Finds a context for the current render target and thread
811 * Parameters:
812 * target: Render target to find the context for
813 * tid: Thread to activate the context for
815 * Returns: The needed context
817 *****************************************************************************/
818 static inline WineD3DContext *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, DWORD tid, GLint *buffer) {
819 IWineD3DSwapChain *swapchain = NULL;
820 HRESULT hr;
821 BOOL readTexture = wined3d_settings.offscreen_rendering_mode != ORM_FBO && This->render_offscreen;
822 WineD3DContext *context = This->activeContext;
823 BOOL oldRenderOffscreen = This->render_offscreen;
824 const WINED3DFORMAT oldFmt = ((IWineD3DSurfaceImpl *) This->lastActiveRenderTarget)->resource.format;
825 const WINED3DFORMAT newFmt = ((IWineD3DSurfaceImpl *) target)->resource.format;
826 const struct StateEntry *StateTable = This->shader_backend->StateTable;
828 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
829 * the alpha blend state changes with different render target formats
831 if(oldFmt != newFmt) {
832 const GlPixelFormatDesc *glDesc;
833 const StaticPixelFormatDesc *old = getFormatDescEntry(oldFmt, NULL, NULL);
834 const StaticPixelFormatDesc *new = getFormatDescEntry(newFmt, &GLINFO_LOCATION, &glDesc);
836 /* Disable blending when the alphaMask has changed and when a format doesn't support blending */
837 if((old->alphaMask && !new->alphaMask) || (!old->alphaMask && new->alphaMask) || !(glDesc->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING)) {
838 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
842 hr = IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **) &swapchain);
843 if(hr == WINED3D_OK && swapchain) {
844 TRACE("Rendering onscreen\n");
846 context = findThreadContextForSwapChain(swapchain, tid);
848 This->render_offscreen = FALSE;
849 /* The context != This->activeContext will catch a NOP context change. This can occur
850 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
851 * rendering. No context change is needed in that case
854 if(((IWineD3DSwapChainImpl *) swapchain)->frontBuffer == target) {
855 *buffer = GL_FRONT;
856 } else {
857 *buffer = GL_BACK;
859 if(wined3d_settings.offscreen_rendering_mode == ORM_PBUFFER) {
860 if(This->pbufferContext && tid == This->pbufferContext->tid) {
861 This->pbufferContext->tid = 0;
864 IWineD3DSwapChain_Release(swapchain);
866 if(oldRenderOffscreen) {
867 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
868 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
869 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
870 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
871 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
874 } else {
875 TRACE("Rendering offscreen\n");
876 This->render_offscreen = TRUE;
877 *buffer = This->offscreenBuffer;
879 switch(wined3d_settings.offscreen_rendering_mode) {
880 case ORM_FBO:
881 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
882 if(This->activeContext && tid == This->lastThread) {
883 context = This->activeContext;
884 } else {
885 /* This may happen if the app jumps straight into offscreen rendering
886 * Start using the context of the primary swapchain. tid == 0 is no problem
887 * for findThreadContextForSwapChain.
889 * Can also happen on thread switches - in that case findThreadContextForSwapChain
890 * is perfect to call.
892 context = findThreadContextForSwapChain(This->swapchains[0], tid);
894 break;
896 case ORM_PBUFFER:
898 IWineD3DSurfaceImpl *targetimpl = (IWineD3DSurfaceImpl *) target;
899 if(This->pbufferContext == NULL ||
900 This->pbufferWidth < targetimpl->currentDesc.Width ||
901 This->pbufferHeight < targetimpl->currentDesc.Height) {
902 if(This->pbufferContext) {
903 DestroyContext(This, This->pbufferContext);
906 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
907 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
909 This->pbufferContext = CreateContext(This, targetimpl,
910 ((IWineD3DSwapChainImpl *) This->swapchains[0])->context[0]->win_handle,
911 TRUE /* pbuffer */, &((IWineD3DSwapChainImpl *)This->swapchains[0])->presentParms);
912 This->pbufferWidth = targetimpl->currentDesc.Width;
913 This->pbufferHeight = targetimpl->currentDesc.Height;
916 if(This->pbufferContext) {
917 if(This->pbufferContext->tid != 0 && This->pbufferContext->tid != tid) {
918 FIXME("The PBuffr context is only supported for one thread for now!\n");
920 This->pbufferContext->tid = tid;
921 context = This->pbufferContext;
922 break;
923 } else {
924 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
925 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
929 case ORM_BACKBUFFER:
930 /* Stay with the currently active context for back buffer rendering */
931 if(This->activeContext && tid == This->lastThread) {
932 context = This->activeContext;
933 } else {
934 /* This may happen if the app jumps straight into offscreen rendering
935 * Start using the context of the primary swapchain. tid == 0 is no problem
936 * for findThreadContextForSwapChain.
938 * Can also happen on thread switches - in that case findThreadContextForSwapChain
939 * is perfect to call.
941 context = findThreadContextForSwapChain(This->swapchains[0], tid);
943 break;
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);
955 /* When switching away from an offscreen render target, and we're not using FBOs,
956 * we have to read the drawable into the texture. This is done via PreLoad(and
957 * SFLAG_INDRAWABLE set on the surface). There are some things that need care though.
958 * PreLoad needs a GL context, and FindContext is called before the context is activated.
959 * It also has to be called with the old rendertarget active, otherwise a wrong drawable
960 * is read. This leads to these possible situations:
962 * 0) lastActiveRenderTarget == target && oldTid == newTid:
963 * Nothing to do, we don't even reach this code in this case...
965 * 1) lastActiveRenderTarget != target && oldTid == newTid:
966 * The currently active context is OK for readback. Call PreLoad, and it
967 * performs the read
969 * 2) lastActiveRenderTarget == target && oldTid != newTid:
970 * Nothing to do - the drawable is unchanged
972 * 3) lastActiveRenderTarget != target && oldTid != newTid:
973 * This is tricky. We have to get a context with the old drawable from somewhere
974 * before we can switch to the new context. In this case, PreLoad calls
975 * ActivateContext(lastActiveRenderTarget) from the new(current) thread. This
976 * is case (2) then. The old drawable is activated for the new thread, and the
977 * readback can be done. The recursed ActivateContext does *not* call PreLoad again.
978 * After that, the outer ActivateContext(which calls PreLoad) can activate the new
979 * target for the new thread
981 if (readTexture && This->lastActiveRenderTarget != target) {
982 BOOL oldInDraw = This->isInDraw;
984 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
985 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
986 * when using offscreen rendering with multithreading
988 This->isInDraw = TRUE;
990 /* Do that before switching the context:
991 * Read the back buffer of the old drawable into the destination texture
993 IWineD3DSurface_PreLoad(This->lastActiveRenderTarget);
995 /* Assume that the drawable will be modified by some other things now */
996 IWineD3DSurface_ModifyLocation(This->lastActiveRenderTarget, SFLAG_INDRAWABLE, FALSE);
998 This->isInDraw = oldInDraw;
1001 if(oldRenderOffscreen != This->render_offscreen && This->depth_copy_state != WINED3D_DCS_NO_COPY) {
1002 This->depth_copy_state = WINED3D_DCS_COPY;
1004 return context;
1007 /*****************************************************************************
1008 * ActivateContext
1010 * Finds a rendering context and drawable matching the device and render
1011 * target for the current thread, activates them and puts them into the
1012 * requested state.
1014 * Params:
1015 * This: Device to activate the context for
1016 * target: Requested render target
1017 * usage: Prepares the context for blitting, drawing or other actions
1019 *****************************************************************************/
1020 void ActivateContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, ContextUsage usage) {
1021 DWORD tid = GetCurrentThreadId();
1022 int i;
1023 DWORD dirtyState, idx;
1024 BYTE shift;
1025 WineD3DContext *context;
1026 GLint drawBuffer=0;
1027 const struct StateEntry *StateTable = This->shader_backend->StateTable;
1029 TRACE("(%p): Selecting context for render target %p, thread %d\n", This, target, tid);
1030 if(This->lastActiveRenderTarget != target || tid != This->lastThread) {
1031 context = FindContext(This, target, tid, &drawBuffer);
1032 This->lastActiveRenderTarget = target;
1033 This->lastThread = tid;
1034 } else {
1035 /* Stick to the old context */
1036 context = This->activeContext;
1039 /* Activate the opengl context */
1040 if(context != This->activeContext) {
1041 BOOL ret;
1043 /* Prevent an unneeded context switch as those are expensive */
1044 if(context->glCtx && (context->glCtx == pwglGetCurrentContext())) {
1045 TRACE("Already using gl context %p\n", context->glCtx);
1047 else {
1048 TRACE("Switching gl ctx to %p, hdc=%p ctx=%p\n", context, context->hdc, context->glCtx);
1050 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, FALSE);
1051 ret = pwglMakeCurrent(context->hdc, context->glCtx);
1052 if(ret == FALSE) {
1053 ERR("Failed to activate the new context\n");
1054 } else if(!context->last_was_blit) {
1055 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
1058 if(This->activeContext->vshader_const_dirty) {
1059 memset(This->activeContext->vshader_const_dirty, 1,
1060 sizeof(*This->activeContext->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
1062 if(This->activeContext->pshader_const_dirty) {
1063 memset(This->activeContext->pshader_const_dirty, 1,
1064 sizeof(*This->activeContext->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
1066 This->activeContext = context;
1069 /* We only need ENTER_GL for the gl calls made below and for the helper functions which make GL calls */
1070 ENTER_GL();
1071 /* Select the right draw buffer. It is selected in FindContext. */
1072 if(drawBuffer && context->last_draw_buffer != drawBuffer) {
1073 TRACE("Drawing to buffer: %#x\n", drawBuffer);
1074 context->last_draw_buffer = drawBuffer;
1076 glDrawBuffer(drawBuffer);
1077 checkGLcall("glDrawBuffer");
1080 switch(usage) {
1081 case CTXUSAGE_RESOURCELOAD:
1082 /* This does not require any special states to be set up */
1083 break;
1085 case CTXUSAGE_CLEAR:
1086 if(context->last_was_blit) {
1087 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
1090 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
1091 * blending when clearing improves the clearing performance incredibly.
1093 glDisable(GL_BLEND);
1094 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1096 glEnable(GL_SCISSOR_TEST);
1097 checkGLcall("glEnable GL_SCISSOR_TEST");
1098 context->last_was_blit = FALSE;
1099 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1100 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1101 break;
1103 case CTXUSAGE_DRAWPRIM:
1104 /* This needs all dirty states applied */
1105 if(context->last_was_blit) {
1106 This->shader_backend->shader_fragment_enable((IWineD3DDevice *) This, TRUE);
1109 IWineD3DDeviceImpl_FindTexUnitMap(This);
1111 for(i=0; i < context->numDirtyEntries; i++) {
1112 dirtyState = context->dirtyArray[i];
1113 idx = dirtyState >> 5;
1114 shift = dirtyState & 0x1f;
1115 context->isStateDirty[idx] &= ~(1 << shift);
1116 StateTable[dirtyState].apply(dirtyState, This->stateBlock, context);
1118 context->numDirtyEntries = 0; /* This makes the whole list clean */
1119 context->last_was_blit = FALSE;
1120 break;
1122 case CTXUSAGE_BLIT:
1123 SetupForBlit(This, context,
1124 ((IWineD3DSurfaceImpl *)target)->currentDesc.Width,
1125 ((IWineD3DSurfaceImpl *)target)->currentDesc.Height);
1126 break;
1128 default:
1129 FIXME("Unexpected context usage requested\n");
1131 LEAVE_GL();