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
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 /*****************************************************************************
40 * Context_MarkStateDirty
42 * Marks a state in a context dirty. Only one context, opposed to
43 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
47 * context: Context to mark the state dirty in
48 * state: State to mark dirty
49 * StateTable: Pointer to the state table in use(for state grouping)
51 *****************************************************************************/
52 static void Context_MarkStateDirty(WineD3DContext
*context
, DWORD state
, const struct StateEntry
*StateTable
) {
53 DWORD rep
= StateTable
[state
].representative
;
57 if(!rep
|| isStateDirty(context
, rep
)) return;
59 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
62 context
->isStateDirty
[idx
] |= (1 << shift
);
65 /*****************************************************************************
68 * Adds a context to the context array. Helper function for CreateContext
70 * This method is not called in performance-critical code paths, only when a
71 * new render target or swapchain is created. Thus performance is not an issue
75 * This: Device to add the context for
77 * glCtx: WGL context to add
78 * pbuffer: optional pbuffer used with this context
80 *****************************************************************************/
81 static WineD3DContext
*AddContextToArray(IWineD3DDeviceImpl
*This
, HWND win_handle
, HDC hdc
, HGLRC glCtx
, HPBUFFERARB pbuffer
) {
82 WineD3DContext
**oldArray
= This
->contexts
;
85 This
->contexts
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->contexts
) * (This
->numContexts
+ 1));
86 if(This
->contexts
== NULL
) {
87 ERR("Unable to grow the context array\n");
88 This
->contexts
= oldArray
;
92 memcpy(This
->contexts
, oldArray
, sizeof(*This
->contexts
) * This
->numContexts
);
95 This
->contexts
[This
->numContexts
] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WineD3DContext
));
96 if(This
->contexts
[This
->numContexts
] == NULL
) {
97 ERR("Unable to allocate a new context\n");
98 HeapFree(GetProcessHeap(), 0, This
->contexts
);
99 This
->contexts
= oldArray
;
103 This
->contexts
[This
->numContexts
]->hdc
= hdc
;
104 This
->contexts
[This
->numContexts
]->glCtx
= glCtx
;
105 This
->contexts
[This
->numContexts
]->pbuffer
= pbuffer
;
106 This
->contexts
[This
->numContexts
]->win_handle
= win_handle
;
107 HeapFree(GetProcessHeap(), 0, oldArray
);
109 /* Mark all states dirty to force a proper initialization of the states on the first use of the context
111 for(state
= 0; state
<= STATE_HIGHEST
; state
++) {
112 Context_MarkStateDirty(This
->contexts
[This
->numContexts
], state
, This
->StateTable
);
116 TRACE("Created context %p\n", This
->contexts
[This
->numContexts
- 1]);
117 return This
->contexts
[This
->numContexts
- 1];
120 /* This function takes care of WineD3D pixel format selection. */
121 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl
*This
, HDC hdc
, WINED3DFORMAT ColorFormat
, WINED3DFORMAT DepthStencilFormat
, BOOL auxBuffers
, int numSamples
, BOOL pbuffer
, BOOL findCompatible
)
123 int iPixelFormat
=0, matchtry
;
124 short redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
125 short depthBits
=0, stencilBits
=0;
132 /* First, try without alpha match buffers. MacOS supports aux buffers only
133 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
134 * Then try without aux buffers - this is the most common cause for not
135 * finding a pixel format. Also some drivers(the open source ones)
136 * only offer 32 bit ARB pixel formats. First try without an exact alpha
137 * match, then try without an exact alpha and color match.
139 { FALSE
, TRUE
, TRUE
},
140 { TRUE
, TRUE
, TRUE
},
141 { FALSE
, FALSE
, TRUE
},
142 { FALSE
, FALSE
, FALSE
},
143 { TRUE
, FALSE
, TRUE
},
144 { TRUE
, FALSE
, FALSE
},
148 int nCfgs
= This
->adapter
->nCfgs
;
149 WineD3D_PixelFormat
*cfgs
= This
->adapter
->cfgs
;
151 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, pbuffer=%d, findCompatible=%d\n",
152 debug_d3dformat(ColorFormat
), debug_d3dformat(DepthStencilFormat
), auxBuffers
, numSamples
, pbuffer
, findCompatible
);
154 if(!getColorBits(ColorFormat
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
)) {
155 ERR("Unable to get color bits for format %s (%#x)!\n", debug_d3dformat(ColorFormat
), ColorFormat
);
159 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
160 * You are able to add a depth + stencil surface at a later stage when you need it.
161 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
162 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
163 * context, need torecreate shaders, textures and other resources.
165 * The context manager already takes care of the state problem and for the other tasks code from Reset
166 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
167 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
168 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
169 * issue needs to be fixed. */
170 if(DepthStencilFormat
!= WINED3DFMT_D24S8
)
171 FIXME("Add OpenGL context recreation support to SetDepthStencilSurface\n");
173 DepthStencilFormat
= WINED3DFMT_D24S8
;
175 if(DepthStencilFormat
) {
176 getDepthStencilBits(DepthStencilFormat
, &depthBits
, &stencilBits
);
179 for(matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(matches
[0])); matchtry
++) {
180 for(i
=0; i
<nCfgs
; i
++) {
181 BOOL exactDepthMatch
= TRUE
;
182 cfgs
= &This
->adapter
->cfgs
[i
];
184 /* For now only accept RGBA formats. Perhaps some day we will
185 * allow floating point formats for pbuffers. */
186 if(cfgs
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
189 /* In window mode (!pbuffer) we need a window drawable format and double buffering. */
190 if(!pbuffer
&& !(cfgs
->windowDrawable
&& cfgs
->doubleBuffer
))
193 /* We like to have aux buffers in backbuffer mode */
194 if(auxBuffers
&& !cfgs
->auxBuffers
&& matches
[matchtry
].require_aux
)
197 /* In pbuffer-mode we need a pbuffer-capable format but we don't want double buffering */
198 if(pbuffer
&& (!cfgs
->pbufferDrawable
|| cfgs
->doubleBuffer
))
201 if(matches
[matchtry
].exact_color
) {
202 if(cfgs
->redSize
!= redBits
)
204 if(cfgs
->greenSize
!= greenBits
)
206 if(cfgs
->blueSize
!= blueBits
)
209 if(cfgs
->redSize
< redBits
)
211 if(cfgs
->greenSize
< greenBits
)
213 if(cfgs
->blueSize
< blueBits
)
216 if(matches
[matchtry
].exact_alpha
) {
217 if(cfgs
->alphaSize
!= alphaBits
)
220 if(cfgs
->alphaSize
< alphaBits
)
224 /* We try to locate a format which matches our requirements exactly. In case of
225 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
226 if(cfgs
->depthSize
< depthBits
)
228 else if(cfgs
->depthSize
> depthBits
)
229 exactDepthMatch
= FALSE
;
231 /* In all cases make sure the number of stencil bits matches our requirements
232 * even when we don't need stencil because it could affect performance EXCEPT
233 * on cards which don't offer depth formats without stencil like the i915 drivers
235 if(stencilBits
!= cfgs
->stencilSize
&& !(This
->adapter
->brokenStencil
&& stencilBits
<= cfgs
->stencilSize
))
238 /* Check multisampling support */
239 if(cfgs
->numSamples
!= numSamples
)
242 /* When we have passed all the checks then we have found a format which matches our
243 * requirements. Note that we only check for a limit number of capabilities right now,
244 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
245 * can still differ in things like multisampling, stereo, SRGB and other flags.
248 /* Exit the loop as we have found a format :) */
249 if(exactDepthMatch
) {
250 iPixelFormat
= cfgs
->iPixelFormat
;
252 } else if(!iPixelFormat
) {
253 /* In the end we might end up with a format which doesn't exactly match our depth
254 * requirements. Accept the first format we found because formats with higher iPixelFormat
255 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
256 iPixelFormat
= cfgs
->iPixelFormat
;
261 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
262 if(!iPixelFormat
&& !findCompatible
) {
263 ERR("Can't find a suitable iPixelFormat\n");
265 } else if(!iPixelFormat
) {
266 PIXELFORMATDESCRIPTOR pfd
;
268 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
269 /* PixelFormat selection */
270 ZeroMemory(&pfd
, sizeof(pfd
));
271 pfd
.nSize
= sizeof(pfd
);
273 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
274 pfd
.iPixelType
= PFD_TYPE_RGBA
;
275 pfd
.cAlphaBits
= alphaBits
;
276 pfd
.cColorBits
= colorBits
;
277 pfd
.cDepthBits
= depthBits
;
278 pfd
.cStencilBits
= stencilBits
;
279 pfd
.iLayerType
= PFD_MAIN_PLANE
;
281 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
283 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
284 ERR("Can't find a suitable iPixelFormat\n");
289 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n", iPixelFormat
, debug_d3dformat(ColorFormat
), debug_d3dformat(DepthStencilFormat
));
293 /*****************************************************************************
296 * Creates a new context for a window, or a pbuffer context.
299 * This: Device to activate the context for
300 * target: Surface this context will render to
301 * win_handle: handle to the window which we are drawing to
302 * create_pbuffer: tells whether to create a pbuffer or not
303 * pPresentParameters: contains the pixelformats to use for onscreen rendering
305 *****************************************************************************/
306 WineD3DContext
*CreateContext(IWineD3DDeviceImpl
*This
, IWineD3DSurfaceImpl
*target
, HWND win_handle
, BOOL create_pbuffer
, const WINED3DPRESENT_PARAMETERS
*pPresentParms
) {
307 HDC oldDrawable
, hdc
;
308 HPBUFFERARB pbuffer
= NULL
;
309 HGLRC ctx
= NULL
, oldCtx
;
310 WineD3DContext
*ret
= NULL
;
313 TRACE("(%p): Creating a %s context for render target %p\n", This
, create_pbuffer
? "offscreen" : "onscreen", target
);
316 HDC hdc_parent
= GetDC(win_handle
);
317 int iPixelFormat
= 0;
319 IWineD3DSurface
*StencilSurface
= This
->stencilBufferTarget
;
320 WINED3DFORMAT StencilBufferFormat
= (NULL
!= StencilSurface
) ? ((IWineD3DSurfaceImpl
*) StencilSurface
)->resource
.format
: 0;
322 /* Try to find a pixel format with pbuffer support. */
323 iPixelFormat
= WineD3D_ChoosePixelFormat(This
, hdc_parent
, target
->resource
.format
, StencilBufferFormat
, FALSE
/* auxBuffers */, 0 /* numSamples */, TRUE
/* PBUFFER */, FALSE
/* findCompatible */);
325 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
327 /* For some reason we weren't able to find a format, try to find something instead of crashing.
328 * A reason for failure could have been wglChoosePixelFormatARB strictness. */
329 iPixelFormat
= WineD3D_ChoosePixelFormat(This
, hdc_parent
, target
->resource
.format
, StencilBufferFormat
, FALSE
/* auxBuffer */, 0 /* numSamples */, TRUE
/* PBUFFER */, TRUE
/* findCompatible */);
332 /* This shouldn't happen as ChoosePixelFormat always returns something */
334 ERR("Unable to locate a pixel format for a pbuffer\n");
335 ReleaseDC(win_handle
, hdc_parent
);
339 TRACE("Creating a pBuffer drawable for the new context\n");
340 pbuffer
= GL_EXTCALL(wglCreatePbufferARB(hdc_parent
, iPixelFormat
, target
->currentDesc
.Width
, target
->currentDesc
.Height
, 0));
342 ERR("Cannot create a pbuffer\n");
343 ReleaseDC(win_handle
, hdc_parent
);
347 /* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
348 hdc
= GL_EXTCALL(wglGetPbufferDCARB(pbuffer
));
350 ERR("Cannot get a HDC for pbuffer (%p)\n", pbuffer
);
351 GL_EXTCALL(wglDestroyPbufferARB(pbuffer
));
352 ReleaseDC(win_handle
, hdc_parent
);
355 ReleaseDC(win_handle
, hdc_parent
);
357 PIXELFORMATDESCRIPTOR pfd
;
360 WINED3DFORMAT ColorFormat
= target
->resource
.format
;
361 WINED3DFORMAT DepthStencilFormat
= 0;
362 BOOL auxBuffers
= FALSE
;
365 hdc
= GetDC(win_handle
);
367 ERR("Cannot retrieve a device context!\n");
371 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
372 if(wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
) {
375 if(target
->resource
.format
== WINED3DFMT_X4R4G4B4
)
376 ColorFormat
= WINED3DFMT_A4R4G4B4
;
377 else if(target
->resource
.format
== WINED3DFMT_X8R8G8B8
)
378 ColorFormat
= WINED3DFMT_A8R8G8B8
;
381 /* DirectDraw supports 8bit paletted render targets and these are used by old games like Starcraft and C&C.
382 * Most modern hardware doesn't support 8bit natively so we perform some form of 8bit -> 32bit conversion.
383 * The conversion (ab)uses the alpha component for storing the palette index. For this reason we require
384 * a format with 8bit alpha, so request A8R8G8B8. */
385 if(ColorFormat
== WINED3DFMT_P8
)
386 ColorFormat
= WINED3DFMT_A8R8G8B8
;
388 /* Retrieve the depth stencil format from the present parameters.
389 * The choice of the proper format can give a nice performance boost
390 * in case of GPU limited programs. */
391 if(pPresentParms
->EnableAutoDepthStencil
) {
392 TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms
->AutoDepthStencilFormat
));
393 DepthStencilFormat
= pPresentParms
->AutoDepthStencilFormat
;
396 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD */
397 if(pPresentParms
->MultiSampleType
&& (pPresentParms
->SwapEffect
== WINED3DSWAPEFFECT_DISCARD
)) {
398 if(!GL_SUPPORT(ARB_MULTISAMPLE
))
399 ERR("The program is requesting multisampling without support!\n");
401 ERR("Requesting MultiSampleType=%d\n", pPresentParms
->MultiSampleType
);
402 numSamples
= pPresentParms
->MultiSampleType
;
406 /* Try to find a pixel format which matches our requirements */
407 iPixelFormat
= WineD3D_ChoosePixelFormat(This
, hdc
, ColorFormat
, DepthStencilFormat
, auxBuffers
, numSamples
, FALSE
/* PBUFFER */, FALSE
/* findCompatible */);
409 /* Try to locate a compatible format if we weren't able to find anything */
411 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
412 iPixelFormat
= WineD3D_ChoosePixelFormat(This
, hdc
, ColorFormat
, DepthStencilFormat
, auxBuffers
, 0 /* numSamples */, FALSE
/* PBUFFER */, TRUE
/* findCompatible */ );
415 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
417 ERR("Can't find a suitable iPixelFormat\n");
421 DescribePixelFormat(hdc
, iPixelFormat
, sizeof(pfd
), &pfd
);
422 res
= SetPixelFormat(hdc
, iPixelFormat
, NULL
);
424 int oldPixelFormat
= GetPixelFormat(hdc
);
426 /* By default WGL doesn't allow pixel format adjustments but we need it here.
427 * For this reason there is a WINE-specific wglSetPixelFormat which allows you to
428 * set the pixel format multiple times. Only use it when it is really needed. */
430 if(oldPixelFormat
== iPixelFormat
) {
431 /* We don't have to do anything as the formats are the same :) */
432 } else if(oldPixelFormat
&& GL_SUPPORT(WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
)) {
433 res
= GL_EXTCALL(wglSetPixelFormatWINE(hdc
, iPixelFormat
, NULL
));
436 ERR("wglSetPixelFormatWINE failed on HDC=%p for iPixelFormat=%d\n", hdc
, iPixelFormat
);
439 } else if(oldPixelFormat
) {
440 /* OpenGL doesn't allow pixel format adjustments. Print an error and continue using the old format.
441 * There's a big chance that the old format works although with a performance hit and perhaps rendering errors. */
442 ERR("HDC=%p is already set to iPixelFormat=%d and OpenGL doesn't allow changes!\n", hdc
, oldPixelFormat
);
444 ERR("SetPixelFormat failed on HDC=%p for iPixelFormat=%d\n", hdc
, iPixelFormat
);
450 ctx
= pwglCreateContext(hdc
);
451 if(This
->numContexts
) pwglShareLists(This
->contexts
[0]->glCtx
, ctx
);
454 ERR("Failed to create a WGL context\n");
456 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer
, hdc
));
457 GL_EXTCALL(wglDestroyPbufferARB(pbuffer
));
461 ret
= AddContextToArray(This
, win_handle
, hdc
, ctx
, pbuffer
);
463 ERR("Failed to add the newly created context to the context list\n");
464 pwglDeleteContext(ctx
);
466 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer
, hdc
));
467 GL_EXTCALL(wglDestroyPbufferARB(pbuffer
));
471 ret
->surface
= (IWineD3DSurface
*) target
;
472 ret
->isPBuffer
= create_pbuffer
;
473 ret
->tid
= GetCurrentThreadId();
474 if(This
->shader_backend
->shader_dirtifyable_constants((IWineD3DDevice
*) This
)) {
475 /* Create the dirty constants array and initialize them to dirty */
476 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
477 sizeof(*ret
->vshader_const_dirty
) * GL_LIMITS(vshader_constantsF
));
478 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
479 sizeof(*ret
->pshader_const_dirty
) * GL_LIMITS(pshader_constantsF
));
480 memset(ret
->vshader_const_dirty
, 1,
481 sizeof(*ret
->vshader_const_dirty
) * GL_LIMITS(vshader_constantsF
));
482 memset(ret
->pshader_const_dirty
, 1,
483 sizeof(*ret
->pshader_const_dirty
) * GL_LIMITS(pshader_constantsF
));
486 TRACE("Successfully created new context %p\n", ret
);
488 /* Set up the context defaults */
489 oldCtx
= pwglGetCurrentContext();
490 oldDrawable
= pwglGetCurrentDC();
491 if(oldCtx
&& oldDrawable
) {
492 /* See comment in ActivateContext context switching */
493 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
495 if(pwglMakeCurrent(hdc
, ctx
) == FALSE
) {
496 ERR("Cannot activate context to set up defaults\n");
502 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
504 TRACE("Setting up the screen\n");
505 /* Clear the screen */
506 glClearColor(1.0, 0.0, 0.0, 0.0);
507 checkGLcall("glClearColor");
510 glClearStencil(0xffff);
512 checkGLcall("glClear");
514 glColor3f(1.0, 1.0, 1.0);
515 checkGLcall("glColor3f");
517 glEnable(GL_LIGHTING
);
518 checkGLcall("glEnable");
520 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
521 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
523 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
524 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
526 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
527 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
529 glPixelStorei(GL_PACK_ALIGNMENT
, This
->surface_alignment
);
530 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
531 glPixelStorei(GL_UNPACK_ALIGNMENT
, This
->surface_alignment
);
532 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
534 if(GL_SUPPORT(APPLE_CLIENT_STORAGE
)) {
535 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
536 * and textures in DIB sections(due to the memory protection).
538 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
539 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
541 if(GL_SUPPORT(ARB_VERTEX_BLEND
)) {
542 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
543 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
544 * GL_VERTEX_BLEND_ARB isn't enabled too
546 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
547 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
549 if(GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
550 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
551 * the previous texture where to source the offset from is always unit - 1.
553 for(s
= 1; s
< GL_LIMITS(textures
); s
++) {
554 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
555 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
556 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
560 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
561 for(s
= 0; s
< GL_LIMITS(textures
); s
++) {
562 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
563 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
564 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
569 /* Never keep GL_FRAGMENT_SHADER_ATI enabled on a context that we switch away from,
570 * but enable it for the first context we create, and reenable it on the old context
572 if(oldDrawable
&& oldCtx
) {
573 pwglMakeCurrent(oldDrawable
, oldCtx
);
577 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
583 /*****************************************************************************
584 * RemoveContextFromArray
586 * Removes a context from the context manager. The opengl context is not
587 * destroyed or unset. context is not a valid pointer after that call.
589 * Similar to the former call this isn't a performance critical function. A
590 * helper function for DestroyContext.
593 * This: Device to activate the context for
594 * context: Context to remove
596 *****************************************************************************/
597 static void RemoveContextFromArray(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
599 WineD3DContext
**oldArray
= This
->contexts
;
601 TRACE("Removing ctx %p\n", context
);
605 if(This
->numContexts
) {
606 This
->contexts
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->contexts
) * This
->numContexts
);
607 if(!This
->contexts
) {
608 ERR("Cannot allocate a new context array, PANIC!!!\n");
611 /* Note that we decreased numContexts a few lines up, so use '<=' instead of '<' */
612 for(s
= 0; s
<= This
->numContexts
; s
++) {
613 if(oldArray
[s
] == context
) continue;
614 This
->contexts
[t
] = oldArray
[s
];
618 This
->contexts
= NULL
;
621 HeapFree(GetProcessHeap(), 0, context
);
622 HeapFree(GetProcessHeap(), 0, oldArray
);
625 /*****************************************************************************
628 * Destroys a wineD3DContext
631 * This: Device to activate the context for
632 * context: Context to destroy
634 *****************************************************************************/
635 void DestroyContext(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
637 /* check that we are the current context first */
638 TRACE("Destroying ctx %p\n", context
);
639 if(pwglGetCurrentContext() == context
->glCtx
){
640 pwglMakeCurrent(NULL
, NULL
);
645 if(context
->isPBuffer
) {
646 GL_EXTCALL(wglReleasePbufferDCARB(context
->pbuffer
, context
->hdc
));
647 GL_EXTCALL(wglDestroyPbufferARB(context
->pbuffer
));
648 } else ReleaseDC(context
->win_handle
, context
->hdc
);
649 pwglDeleteContext(context
->glCtx
);
651 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
652 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
653 RemoveContextFromArray(This
, context
);
656 static inline void set_blit_dimension(UINT width
, UINT height
) {
657 glMatrixMode(GL_PROJECTION
);
658 checkGLcall("glMatrixMode(GL_PROJECTION)");
660 checkGLcall("glLoadIdentity()");
661 glOrtho(0, width
, height
, 0, 0.0, -1.0);
662 checkGLcall("glOrtho");
663 glViewport(0, 0, width
, height
);
664 checkGLcall("glViewport");
667 /*****************************************************************************
670 * Sets up a context for DirectDraw blitting.
671 * All texture units are disabled, texture unit 0 is set as current unit
672 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
673 * color writing enabled for all channels
674 * register combiners disabled, shaders disabled
675 * world matrix is set to identity, texture matrix 0 too
676 * projection matrix is setup for drawing screen coordinates
679 * This: Device to activate the context for
680 * context: Context to setup
681 * width: render target width
682 * height: render target height
684 *****************************************************************************/
685 static inline void SetupForBlit(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
, UINT width
, UINT height
) {
687 const struct StateEntry
*StateTable
= This
->StateTable
;
689 TRACE("Setting up context %p for blitting\n", context
);
690 if(context
->last_was_blit
) {
691 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
692 set_blit_dimension(width
, height
);
693 context
->blit_w
= width
; context
->blit_h
= height
;
694 /* No need to dirtify here, the states are still dirtified because they weren't
695 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
699 TRACE("Context is already set up for blitting, nothing to do\n");
702 context
->last_was_blit
= TRUE
;
704 /* TODO: Use a display list */
706 /* Disable shaders */
707 This
->shader_backend
->shader_cleanup((IWineD3DDevice
*) This
);
708 Context_MarkStateDirty(context
, STATE_VSHADER
, StateTable
);
709 Context_MarkStateDirty(context
, STATE_PIXELSHADER
, StateTable
);
711 /* Disable all textures. The caller can then bind a texture it wants to blit
714 if(GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
715 glDisable(GL_REGISTER_COMBINERS_NV
);
716 checkGLcall("glDisable(GL_REGISTER_COMBINERS_NV)");
718 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
719 /* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
720 * function texture unit. No need to care for higher samplers
722 for(i
= GL_LIMITS(textures
) - 1; i
> 0 ; i
--) {
723 sampler
= This
->rev_tex_unit_map
[i
];
724 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
725 checkGLcall("glActiveTextureARB");
727 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
728 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
729 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
731 glDisable(GL_TEXTURE_3D
);
732 checkGLcall("glDisable GL_TEXTURE_3D");
733 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
734 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
735 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
737 glDisable(GL_TEXTURE_2D
);
738 checkGLcall("glDisable GL_TEXTURE_2D");
740 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
741 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
744 if (sampler
< MAX_TEXTURES
) {
745 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
747 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
750 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
751 checkGLcall("glActiveTextureARB");
754 sampler
= This
->rev_tex_unit_map
[0];
756 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
757 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
758 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
760 glDisable(GL_TEXTURE_3D
);
761 checkGLcall("glDisable GL_TEXTURE_3D");
762 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
763 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
764 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
766 glDisable(GL_TEXTURE_2D
);
767 checkGLcall("glDisable GL_TEXTURE_2D");
769 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
771 glMatrixMode(GL_TEXTURE
);
772 checkGLcall("glMatrixMode(GL_TEXTURE)");
774 checkGLcall("glLoadIdentity()");
776 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS
)) {
777 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
778 GL_TEXTURE_LOD_BIAS_EXT
,
780 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
784 if (sampler
< MAX_TEXTURES
) {
785 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
786 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
788 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
791 /* Other misc states */
792 glDisable(GL_ALPHA_TEST
);
793 checkGLcall("glDisable(GL_ALPHA_TEST)");
794 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
795 glDisable(GL_LIGHTING
);
796 checkGLcall("glDisable GL_LIGHTING");
797 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
798 glDisable(GL_DEPTH_TEST
);
799 checkGLcall("glDisable GL_DEPTH_TEST");
800 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
802 checkGLcall("glDisable GL_FOG");
803 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
805 checkGLcall("glDisable GL_BLEND");
806 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
807 glDisable(GL_CULL_FACE
);
808 checkGLcall("glDisable GL_CULL_FACE");
809 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
810 glDisable(GL_STENCIL_TEST
);
811 checkGLcall("glDisable GL_STENCIL_TEST");
812 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
813 glDisable(GL_SCISSOR_TEST
);
814 checkGLcall("glDisable GL_SCISSOR_TEST");
815 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
816 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
817 glDisable(GL_POINT_SPRITE_ARB
);
818 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
819 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
821 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
822 checkGLcall("glColorMask");
823 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
824 if (GL_SUPPORT(EXT_SECONDARY_COLOR
)) {
825 glDisable(GL_COLOR_SUM_EXT
);
826 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
827 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
829 if (GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
830 GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV
, GL_SPARE0_NV
, GL_UNSIGNED_IDENTITY_NV
, GL_RGB
));
831 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
832 checkGLcall("glFinalCombinerInputNV");
835 /* Setup transforms */
836 glMatrixMode(GL_MODELVIEW
);
837 checkGLcall("glMatrixMode(GL_MODELVIEW)");
839 checkGLcall("glLoadIdentity()");
840 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
842 context
->last_was_rhw
= TRUE
;
843 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
845 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
846 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
847 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
848 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
849 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
850 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
851 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
853 set_blit_dimension(width
, height
);
854 context
->blit_w
= width
; context
->blit_h
= height
;
855 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
856 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
859 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
862 /*****************************************************************************
863 * findThreadContextForSwapChain
865 * Searches a swapchain for all contexts and picks one for the thread tid.
866 * If none can be found the swapchain is requested to create a new context
868 *****************************************************************************/
869 static WineD3DContext
*findThreadContextForSwapChain(IWineD3DSwapChain
*swapchain
, DWORD tid
) {
872 for(i
= 0; i
< ((IWineD3DSwapChainImpl
*) swapchain
)->num_contexts
; i
++) {
873 if(((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
]->tid
== tid
) {
874 return ((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
];
879 /* Create a new context for the thread */
880 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain
);
883 /*****************************************************************************
886 * Finds a context for the current render target and thread
889 * target: Render target to find the context for
890 * tid: Thread to activate the context for
892 * Returns: The needed context
894 *****************************************************************************/
895 static inline WineD3DContext
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, DWORD tid
) {
896 IWineD3DSwapChain
*swapchain
= NULL
;
898 BOOL readTexture
= wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
&& This
->render_offscreen
;
899 WineD3DContext
*context
= This
->activeContext
;
900 BOOL oldRenderOffscreen
= This
->render_offscreen
;
901 const WINED3DFORMAT oldFmt
= ((IWineD3DSurfaceImpl
*) This
->lastActiveRenderTarget
)->resource
.format
;
902 const WINED3DFORMAT newFmt
= ((IWineD3DSurfaceImpl
*) target
)->resource
.format
;
903 const struct StateEntry
*StateTable
= This
->StateTable
;
905 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
906 * the alpha blend state changes with different render target formats
908 if(oldFmt
!= newFmt
) {
909 const GlPixelFormatDesc
*glDesc
;
910 const StaticPixelFormatDesc
*old
= getFormatDescEntry(oldFmt
, NULL
, NULL
);
911 const StaticPixelFormatDesc
*new = getFormatDescEntry(newFmt
, &GLINFO_LOCATION
, &glDesc
);
913 /* Disable blending when the alphaMask has changed and when a format doesn't support blending */
914 if((old
->alphaMask
&& !new->alphaMask
) || (!old
->alphaMask
&& new->alphaMask
) || !(glDesc
->Flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
)) {
915 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
919 hr
= IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
920 if(hr
== WINED3D_OK
&& swapchain
) {
921 TRACE("Rendering onscreen\n");
923 context
= findThreadContextForSwapChain(swapchain
, tid
);
925 This
->render_offscreen
= FALSE
;
926 /* The context != This->activeContext will catch a NOP context change. This can occur
927 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
928 * rendering. No context change is needed in that case
931 if(wined3d_settings
.offscreen_rendering_mode
== ORM_PBUFFER
) {
932 if(This
->pbufferContext
&& tid
== This
->pbufferContext
->tid
) {
933 This
->pbufferContext
->tid
= 0;
936 IWineD3DSwapChain_Release(swapchain
);
938 if(oldRenderOffscreen
) {
939 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
, StateTable
);
940 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
941 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
942 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
943 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
947 TRACE("Rendering offscreen\n");
948 This
->render_offscreen
= TRUE
;
950 switch(wined3d_settings
.offscreen_rendering_mode
) {
952 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
953 if(This
->activeContext
&& tid
== This
->lastThread
) {
954 context
= This
->activeContext
;
956 /* This may happen if the app jumps straight into offscreen rendering
957 * Start using the context of the primary swapchain. tid == 0 is no problem
958 * for findThreadContextForSwapChain.
960 * Can also happen on thread switches - in that case findThreadContextForSwapChain
961 * is perfect to call.
963 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
969 IWineD3DSurfaceImpl
*targetimpl
= (IWineD3DSurfaceImpl
*) target
;
970 if(This
->pbufferContext
== NULL
||
971 This
->pbufferWidth
< targetimpl
->currentDesc
.Width
||
972 This
->pbufferHeight
< targetimpl
->currentDesc
.Height
) {
973 if(This
->pbufferContext
) {
974 DestroyContext(This
, This
->pbufferContext
);
977 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
978 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
980 This
->pbufferContext
= CreateContext(This
, targetimpl
,
981 ((IWineD3DSwapChainImpl
*) This
->swapchains
[0])->context
[0]->win_handle
,
982 TRUE
/* pbuffer */, &((IWineD3DSwapChainImpl
*)This
->swapchains
[0])->presentParms
);
983 This
->pbufferWidth
= targetimpl
->currentDesc
.Width
;
984 This
->pbufferHeight
= targetimpl
->currentDesc
.Height
;
987 if(This
->pbufferContext
) {
988 if(This
->pbufferContext
->tid
!= 0 && This
->pbufferContext
->tid
!= tid
) {
989 FIXME("The PBuffr context is only supported for one thread for now!\n");
991 This
->pbufferContext
->tid
= tid
;
992 context
= This
->pbufferContext
;
995 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
996 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
1000 case ORM_BACKBUFFER
:
1001 /* Stay with the currently active context for back buffer rendering */
1002 if(This
->activeContext
&& tid
== This
->lastThread
) {
1003 context
= This
->activeContext
;
1005 /* This may happen if the app jumps straight into offscreen rendering
1006 * Start using the context of the primary swapchain. tid == 0 is no problem
1007 * for findThreadContextForSwapChain.
1009 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1010 * is perfect to call.
1012 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
1017 if(!oldRenderOffscreen
) {
1018 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
, StateTable
);
1019 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
1020 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1021 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1022 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
1026 /* When switching away from an offscreen render target, and we're not using FBOs,
1027 * we have to read the drawable into the texture. This is done via PreLoad(and
1028 * SFLAG_INDRAWABLE set on the surface). There are some things that need care though.
1029 * PreLoad needs a GL context, and FindContext is called before the context is activated.
1030 * It also has to be called with the old rendertarget active, otherwise a wrong drawable
1031 * is read. This leads to these possible situations:
1033 * 0) lastActiveRenderTarget == target && oldTid == newTid:
1034 * Nothing to do, we don't even reach this code in this case...
1036 * 1) lastActiveRenderTarget != target && oldTid == newTid:
1037 * The currently active context is OK for readback. Call PreLoad, and it
1040 * 2) lastActiveRenderTarget == target && oldTid != newTid:
1041 * Nothing to do - the drawable is unchanged
1043 * 3) lastActiveRenderTarget != target && oldTid != newTid:
1044 * This is tricky. We have to get a context with the old drawable from somewhere
1045 * before we can switch to the new context. In this case, PreLoad calls
1046 * ActivateContext(lastActiveRenderTarget) from the new(current) thread. This
1047 * is case (2) then. The old drawable is activated for the new thread, and the
1048 * readback can be done. The recursed ActivateContext does *not* call PreLoad again.
1049 * After that, the outer ActivateContext(which calls PreLoad) can activate the new
1050 * target for the new thread
1052 if (readTexture
&& This
->lastActiveRenderTarget
!= target
) {
1053 BOOL oldInDraw
= This
->isInDraw
;
1055 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
1056 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
1057 * when using offscreen rendering with multithreading
1059 This
->isInDraw
= TRUE
;
1061 /* Do that before switching the context:
1062 * Read the back buffer of the old drawable into the destination texture
1064 IWineD3DSurface_PreLoad(This
->lastActiveRenderTarget
);
1066 /* Assume that the drawable will be modified by some other things now */
1067 IWineD3DSurface_ModifyLocation(This
->lastActiveRenderTarget
, SFLAG_INDRAWABLE
, FALSE
);
1069 This
->isInDraw
= oldInDraw
;
1075 static void apply_draw_buffer(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, BOOL blit
)
1078 IWineD3DSwapChain
*swapchain
;
1080 hr
= IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **)&swapchain
);
1083 IWineD3DSwapChain_Release((IUnknown
*)swapchain
);
1084 glDrawBuffer(surface_get_gl_buffer(target
, swapchain
));
1085 checkGLcall("glDrawBuffers()");
1089 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1093 if (GL_SUPPORT(ARB_DRAW_BUFFERS
))
1095 GL_EXTCALL(glDrawBuffersARB(GL_LIMITS(buffers
), This
->draw_buffers
));
1096 checkGLcall("glDrawBuffers()");
1100 glDrawBuffer(This
->draw_buffers
[0]);
1101 checkGLcall("glDrawBuffer()");
1104 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
1105 checkGLcall("glDrawBuffer()");
1110 glDrawBuffer(This
->offscreenBuffer
);
1111 checkGLcall("glDrawBuffer()");
1116 /*****************************************************************************
1119 * Finds a rendering context and drawable matching the device and render
1120 * target for the current thread, activates them and puts them into the
1124 * This: Device to activate the context for
1125 * target: Requested render target
1126 * usage: Prepares the context for blitting, drawing or other actions
1128 *****************************************************************************/
1129 void ActivateContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, ContextUsage usage
) {
1130 DWORD tid
= GetCurrentThreadId();
1132 DWORD dirtyState
, idx
;
1134 WineD3DContext
*context
;
1135 const struct StateEntry
*StateTable
= This
->StateTable
;
1137 TRACE("(%p): Selecting context for render target %p, thread %d\n", This
, target
, tid
);
1138 if(This
->lastActiveRenderTarget
!= target
|| tid
!= This
->lastThread
) {
1139 context
= FindContext(This
, target
, tid
);
1140 context
->draw_buffer_dirty
= TRUE
;
1141 This
->lastActiveRenderTarget
= target
;
1142 This
->lastThread
= tid
;
1144 /* Stick to the old context */
1145 context
= This
->activeContext
;
1148 /* Activate the opengl context */
1149 if(last_device
!= This
|| context
!= This
->activeContext
) {
1152 /* Prevent an unneeded context switch as those are expensive */
1153 if(context
->glCtx
&& (context
->glCtx
== pwglGetCurrentContext())) {
1154 TRACE("Already using gl context %p\n", context
->glCtx
);
1157 TRACE("Switching gl ctx to %p, hdc=%p ctx=%p\n", context
, context
->hdc
, context
->glCtx
);
1159 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
1160 ret
= pwglMakeCurrent(context
->hdc
, context
->glCtx
);
1162 ERR("Failed to activate the new context\n");
1163 } else if(!context
->last_was_blit
) {
1164 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
1167 if(This
->activeContext
->vshader_const_dirty
) {
1168 memset(This
->activeContext
->vshader_const_dirty
, 1,
1169 sizeof(*This
->activeContext
->vshader_const_dirty
) * GL_LIMITS(vshader_constantsF
));
1171 if(This
->activeContext
->pshader_const_dirty
) {
1172 memset(This
->activeContext
->pshader_const_dirty
, 1,
1173 sizeof(*This
->activeContext
->pshader_const_dirty
) * GL_LIMITS(pshader_constantsF
));
1175 This
->activeContext
= context
;
1179 /* We only need ENTER_GL for the gl calls made below and for the helper functions which make GL calls */
1183 case CTXUSAGE_CLEAR
:
1184 case CTXUSAGE_DRAWPRIM
:
1185 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
1186 apply_fbo_state((IWineD3DDevice
*)This
);
1188 if (context
->draw_buffer_dirty
) {
1189 apply_draw_buffer(This
, target
, FALSE
);
1190 context
->draw_buffer_dirty
= FALSE
;
1195 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
1196 if (This
->render_offscreen
) {
1197 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
1198 bind_fbo((IWineD3DDevice
*)This
, GL_FRAMEBUFFER_EXT
, &This
->dst_fbo
);
1199 attach_surface_fbo(This
, GL_FRAMEBUFFER_EXT
, 0, target
);
1200 GL_EXTCALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
, GL_DEPTH_ATTACHMENT_EXT
, GL_RENDERBUFFER_EXT
, 0));
1201 checkGLcall("glFramebufferRenderbufferEXT");
1203 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0));
1204 checkGLcall("glFramebufferRenderbufferEXT");
1206 context
->draw_buffer_dirty
= TRUE
;
1208 if (context
->draw_buffer_dirty
) {
1209 apply_draw_buffer(This
, target
, TRUE
);
1210 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) {
1211 context
->draw_buffer_dirty
= FALSE
;
1221 case CTXUSAGE_RESOURCELOAD
:
1222 /* This does not require any special states to be set up */
1225 case CTXUSAGE_CLEAR
:
1226 if(context
->last_was_blit
) {
1227 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
1230 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
1231 * blending when clearing improves the clearing performance incredibly.
1233 glDisable(GL_BLEND
);
1234 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1236 glEnable(GL_SCISSOR_TEST
);
1237 checkGLcall("glEnable GL_SCISSOR_TEST");
1238 context
->last_was_blit
= FALSE
;
1239 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1240 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1243 case CTXUSAGE_DRAWPRIM
:
1244 /* This needs all dirty states applied */
1245 if(context
->last_was_blit
) {
1246 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
1249 IWineD3DDeviceImpl_FindTexUnitMap(This
);
1251 for(i
=0; i
< context
->numDirtyEntries
; i
++) {
1252 dirtyState
= context
->dirtyArray
[i
];
1253 idx
= dirtyState
>> 5;
1254 shift
= dirtyState
& 0x1f;
1255 context
->isStateDirty
[idx
] &= ~(1 << shift
);
1256 StateTable
[dirtyState
].apply(dirtyState
, This
->stateBlock
, context
);
1258 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
1259 context
->last_was_blit
= FALSE
;
1263 SetupForBlit(This
, context
,
1264 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Width
,
1265 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Height
);
1269 FIXME("Unexpected context usage requested\n");