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 ret
->fbo_color_attachments
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IWineD3DSurface
*) * GL_LIMITS(buffers
));
489 if (!ret
->fbo_color_attachments
)
491 ERR("Out of memory!\n");
495 /* Set up the context defaults */
496 oldCtx
= pwglGetCurrentContext();
497 oldDrawable
= pwglGetCurrentDC();
498 if(oldCtx
&& oldDrawable
) {
499 /* See comment in ActivateContext context switching */
500 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
502 if(pwglMakeCurrent(hdc
, ctx
) == FALSE
) {
503 ERR("Cannot activate context to set up defaults\n");
509 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
511 TRACE("Setting up the screen\n");
512 /* Clear the screen */
513 glClearColor(1.0, 0.0, 0.0, 0.0);
514 checkGLcall("glClearColor");
517 glClearStencil(0xffff);
519 checkGLcall("glClear");
521 glColor3f(1.0, 1.0, 1.0);
522 checkGLcall("glColor3f");
524 glEnable(GL_LIGHTING
);
525 checkGLcall("glEnable");
527 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
528 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
530 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
531 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
533 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
534 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
536 glPixelStorei(GL_PACK_ALIGNMENT
, This
->surface_alignment
);
537 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
538 glPixelStorei(GL_UNPACK_ALIGNMENT
, This
->surface_alignment
);
539 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
541 if(GL_SUPPORT(APPLE_CLIENT_STORAGE
)) {
542 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
543 * and textures in DIB sections(due to the memory protection).
545 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
546 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
548 if(GL_SUPPORT(ARB_VERTEX_BLEND
)) {
549 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
550 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
551 * GL_VERTEX_BLEND_ARB isn't enabled too
553 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
554 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
556 if(GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
557 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
558 * the previous texture where to source the offset from is always unit - 1.
560 for(s
= 1; s
< GL_LIMITS(textures
); s
++) {
561 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
562 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
563 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
567 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
568 for(s
= 0; s
< GL_LIMITS(textures
); s
++) {
569 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
570 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
571 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
576 /* Never keep GL_FRAGMENT_SHADER_ATI enabled on a context that we switch away from,
577 * but enable it for the first context we create, and reenable it on the old context
579 if(oldDrawable
&& oldCtx
) {
580 pwglMakeCurrent(oldDrawable
, oldCtx
);
584 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
589 HeapFree(GetProcessHeap(), 0, ret
->fbo_color_attachments
);
593 /*****************************************************************************
594 * RemoveContextFromArray
596 * Removes a context from the context manager. The opengl context is not
597 * destroyed or unset. context is not a valid pointer after that call.
599 * Similar to the former call this isn't a performance critical function. A
600 * helper function for DestroyContext.
603 * This: Device to activate the context for
604 * context: Context to remove
606 *****************************************************************************/
607 static void RemoveContextFromArray(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
609 WineD3DContext
**oldArray
= This
->contexts
;
611 TRACE("Removing ctx %p\n", context
);
615 if(This
->numContexts
) {
616 This
->contexts
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->contexts
) * This
->numContexts
);
617 if(!This
->contexts
) {
618 ERR("Cannot allocate a new context array, PANIC!!!\n");
621 /* Note that we decreased numContexts a few lines up, so use '<=' instead of '<' */
622 for(s
= 0; s
<= This
->numContexts
; s
++) {
623 if(oldArray
[s
] == context
) continue;
624 This
->contexts
[t
] = oldArray
[s
];
628 This
->contexts
= NULL
;
631 HeapFree(GetProcessHeap(), 0, context
);
632 HeapFree(GetProcessHeap(), 0, oldArray
);
635 /*****************************************************************************
638 * Destroys a wineD3DContext
641 * This: Device to activate the context for
642 * context: Context to destroy
644 *****************************************************************************/
645 void DestroyContext(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
647 /* check that we are the current context first */
648 TRACE("Destroying ctx %p\n", context
);
649 if(pwglGetCurrentContext() == context
->glCtx
){
650 pwglMakeCurrent(NULL
, NULL
);
655 /* FIXME: We probably need an active context to do this... */
657 GL_EXTCALL(glDeleteFramebuffersEXT(1, &context
->fbo
));
659 if (context
->src_fbo
) {
660 GL_EXTCALL(glDeleteFramebuffersEXT(1, &context
->src_fbo
));
662 if (context
->dst_fbo
) {
663 GL_EXTCALL(glDeleteFramebuffersEXT(1, &context
->dst_fbo
));
666 HeapFree(GetProcessHeap(), 0, context
->fbo_color_attachments
);
667 context
->fbo_color_attachments
= NULL
;
669 if(context
->isPBuffer
) {
670 GL_EXTCALL(wglReleasePbufferDCARB(context
->pbuffer
, context
->hdc
));
671 GL_EXTCALL(wglDestroyPbufferARB(context
->pbuffer
));
672 } else ReleaseDC(context
->win_handle
, context
->hdc
);
673 pwglDeleteContext(context
->glCtx
);
675 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
676 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
677 RemoveContextFromArray(This
, context
);
680 static inline void set_blit_dimension(UINT width
, UINT height
) {
681 glMatrixMode(GL_PROJECTION
);
682 checkGLcall("glMatrixMode(GL_PROJECTION)");
684 checkGLcall("glLoadIdentity()");
685 glOrtho(0, width
, height
, 0, 0.0, -1.0);
686 checkGLcall("glOrtho");
687 glViewport(0, 0, width
, height
);
688 checkGLcall("glViewport");
691 /*****************************************************************************
694 * Sets up a context for DirectDraw blitting.
695 * All texture units are disabled, texture unit 0 is set as current unit
696 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
697 * color writing enabled for all channels
698 * register combiners disabled, shaders disabled
699 * world matrix is set to identity, texture matrix 0 too
700 * projection matrix is setup for drawing screen coordinates
703 * This: Device to activate the context for
704 * context: Context to setup
705 * width: render target width
706 * height: render target height
708 *****************************************************************************/
709 static inline void SetupForBlit(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
, UINT width
, UINT height
) {
711 const struct StateEntry
*StateTable
= This
->StateTable
;
713 TRACE("Setting up context %p for blitting\n", context
);
714 if(context
->last_was_blit
) {
715 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
716 set_blit_dimension(width
, height
);
717 context
->blit_w
= width
; context
->blit_h
= height
;
718 /* No need to dirtify here, the states are still dirtified because they weren't
719 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
723 TRACE("Context is already set up for blitting, nothing to do\n");
726 context
->last_was_blit
= TRUE
;
728 /* TODO: Use a display list */
730 /* Disable shaders */
731 This
->shader_backend
->shader_cleanup((IWineD3DDevice
*) This
);
732 Context_MarkStateDirty(context
, STATE_VSHADER
, StateTable
);
733 Context_MarkStateDirty(context
, STATE_PIXELSHADER
, StateTable
);
735 /* Disable all textures. The caller can then bind a texture it wants to blit
738 if(GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
739 glDisable(GL_REGISTER_COMBINERS_NV
);
740 checkGLcall("glDisable(GL_REGISTER_COMBINERS_NV)");
742 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
743 /* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
744 * function texture unit. No need to care for higher samplers
746 for(i
= GL_LIMITS(textures
) - 1; i
> 0 ; i
--) {
747 sampler
= This
->rev_tex_unit_map
[i
];
748 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
749 checkGLcall("glActiveTextureARB");
751 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
752 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
753 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
755 glDisable(GL_TEXTURE_3D
);
756 checkGLcall("glDisable GL_TEXTURE_3D");
757 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
758 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
759 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
761 glDisable(GL_TEXTURE_2D
);
762 checkGLcall("glDisable GL_TEXTURE_2D");
764 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
765 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
768 if (sampler
< MAX_TEXTURES
) {
769 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
771 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
774 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
775 checkGLcall("glActiveTextureARB");
778 sampler
= This
->rev_tex_unit_map
[0];
780 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
781 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
782 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
784 glDisable(GL_TEXTURE_3D
);
785 checkGLcall("glDisable GL_TEXTURE_3D");
786 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
787 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
788 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
790 glDisable(GL_TEXTURE_2D
);
791 checkGLcall("glDisable GL_TEXTURE_2D");
793 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
795 glMatrixMode(GL_TEXTURE
);
796 checkGLcall("glMatrixMode(GL_TEXTURE)");
798 checkGLcall("glLoadIdentity()");
800 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS
)) {
801 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
802 GL_TEXTURE_LOD_BIAS_EXT
,
804 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
808 if (sampler
< MAX_TEXTURES
) {
809 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
810 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
812 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
815 /* Other misc states */
816 glDisable(GL_ALPHA_TEST
);
817 checkGLcall("glDisable(GL_ALPHA_TEST)");
818 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
819 glDisable(GL_LIGHTING
);
820 checkGLcall("glDisable GL_LIGHTING");
821 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
822 glDisable(GL_DEPTH_TEST
);
823 checkGLcall("glDisable GL_DEPTH_TEST");
824 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
826 checkGLcall("glDisable GL_FOG");
827 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
829 checkGLcall("glDisable GL_BLEND");
830 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
831 glDisable(GL_CULL_FACE
);
832 checkGLcall("glDisable GL_CULL_FACE");
833 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
834 glDisable(GL_STENCIL_TEST
);
835 checkGLcall("glDisable GL_STENCIL_TEST");
836 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
837 glDisable(GL_SCISSOR_TEST
);
838 checkGLcall("glDisable GL_SCISSOR_TEST");
839 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
840 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
841 glDisable(GL_POINT_SPRITE_ARB
);
842 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
843 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
845 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
846 checkGLcall("glColorMask");
847 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
848 if (GL_SUPPORT(EXT_SECONDARY_COLOR
)) {
849 glDisable(GL_COLOR_SUM_EXT
);
850 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
851 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
853 if (GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
854 GL_EXTCALL(glFinalCombinerInputNV(GL_VARIABLE_B_NV
, GL_SPARE0_NV
, GL_UNSIGNED_IDENTITY_NV
, GL_RGB
));
855 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
856 checkGLcall("glFinalCombinerInputNV");
859 /* Setup transforms */
860 glMatrixMode(GL_MODELVIEW
);
861 checkGLcall("glMatrixMode(GL_MODELVIEW)");
863 checkGLcall("glLoadIdentity()");
864 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
866 context
->last_was_rhw
= TRUE
;
867 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
869 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
870 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
871 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
872 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
873 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
874 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
875 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
877 set_blit_dimension(width
, height
);
878 context
->blit_w
= width
; context
->blit_h
= height
;
879 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
880 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
883 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
886 /*****************************************************************************
887 * findThreadContextForSwapChain
889 * Searches a swapchain for all contexts and picks one for the thread tid.
890 * If none can be found the swapchain is requested to create a new context
892 *****************************************************************************/
893 static WineD3DContext
*findThreadContextForSwapChain(IWineD3DSwapChain
*swapchain
, DWORD tid
) {
896 for(i
= 0; i
< ((IWineD3DSwapChainImpl
*) swapchain
)->num_contexts
; i
++) {
897 if(((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
]->tid
== tid
) {
898 return ((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
];
903 /* Create a new context for the thread */
904 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain
);
907 /*****************************************************************************
910 * Finds a context for the current render target and thread
913 * target: Render target to find the context for
914 * tid: Thread to activate the context for
916 * Returns: The needed context
918 *****************************************************************************/
919 static inline WineD3DContext
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, DWORD tid
) {
920 IWineD3DSwapChain
*swapchain
= NULL
;
922 BOOL readTexture
= wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
&& This
->render_offscreen
;
923 WineD3DContext
*context
= This
->activeContext
;
924 BOOL oldRenderOffscreen
= This
->render_offscreen
;
925 const WINED3DFORMAT oldFmt
= ((IWineD3DSurfaceImpl
*) This
->lastActiveRenderTarget
)->resource
.format
;
926 const WINED3DFORMAT newFmt
= ((IWineD3DSurfaceImpl
*) target
)->resource
.format
;
927 const struct StateEntry
*StateTable
= This
->StateTable
;
929 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
930 * the alpha blend state changes with different render target formats
932 if(oldFmt
!= newFmt
) {
933 const GlPixelFormatDesc
*glDesc
;
934 const StaticPixelFormatDesc
*old
= getFormatDescEntry(oldFmt
, NULL
, NULL
);
935 const StaticPixelFormatDesc
*new = getFormatDescEntry(newFmt
, &GLINFO_LOCATION
, &glDesc
);
937 /* Disable blending when the alphaMask has changed and when a format doesn't support blending */
938 if((old
->alphaMask
&& !new->alphaMask
) || (!old
->alphaMask
&& new->alphaMask
) || !(glDesc
->Flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
)) {
939 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
943 hr
= IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
944 if(hr
== WINED3D_OK
&& swapchain
) {
945 TRACE("Rendering onscreen\n");
947 context
= findThreadContextForSwapChain(swapchain
, tid
);
949 This
->render_offscreen
= FALSE
;
950 /* The context != This->activeContext will catch a NOP context change. This can occur
951 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
952 * rendering. No context change is needed in that case
955 if(wined3d_settings
.offscreen_rendering_mode
== ORM_PBUFFER
) {
956 if(This
->pbufferContext
&& tid
== This
->pbufferContext
->tid
) {
957 This
->pbufferContext
->tid
= 0;
960 IWineD3DSwapChain_Release(swapchain
);
962 if(oldRenderOffscreen
) {
963 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
, StateTable
);
964 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
965 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
966 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
967 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
971 TRACE("Rendering offscreen\n");
972 This
->render_offscreen
= TRUE
;
974 switch(wined3d_settings
.offscreen_rendering_mode
) {
976 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
977 if(This
->activeContext
&& tid
== This
->lastThread
) {
978 context
= This
->activeContext
;
980 /* This may happen if the app jumps straight into offscreen rendering
981 * Start using the context of the primary swapchain. tid == 0 is no problem
982 * for findThreadContextForSwapChain.
984 * Can also happen on thread switches - in that case findThreadContextForSwapChain
985 * is perfect to call.
987 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
993 IWineD3DSurfaceImpl
*targetimpl
= (IWineD3DSurfaceImpl
*) target
;
994 if(This
->pbufferContext
== NULL
||
995 This
->pbufferWidth
< targetimpl
->currentDesc
.Width
||
996 This
->pbufferHeight
< targetimpl
->currentDesc
.Height
) {
997 if(This
->pbufferContext
) {
998 DestroyContext(This
, This
->pbufferContext
);
1001 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
1002 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
1004 This
->pbufferContext
= CreateContext(This
, targetimpl
,
1005 ((IWineD3DSwapChainImpl
*) This
->swapchains
[0])->context
[0]->win_handle
,
1006 TRUE
/* pbuffer */, &((IWineD3DSwapChainImpl
*)This
->swapchains
[0])->presentParms
);
1007 This
->pbufferWidth
= targetimpl
->currentDesc
.Width
;
1008 This
->pbufferHeight
= targetimpl
->currentDesc
.Height
;
1011 if(This
->pbufferContext
) {
1012 if(This
->pbufferContext
->tid
!= 0 && This
->pbufferContext
->tid
!= tid
) {
1013 FIXME("The PBuffr context is only supported for one thread for now!\n");
1015 This
->pbufferContext
->tid
= tid
;
1016 context
= This
->pbufferContext
;
1019 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
1020 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
1024 case ORM_BACKBUFFER
:
1025 /* Stay with the currently active context for back buffer rendering */
1026 if(This
->activeContext
&& tid
== This
->lastThread
) {
1027 context
= This
->activeContext
;
1029 /* This may happen if the app jumps straight into offscreen rendering
1030 * Start using the context of the primary swapchain. tid == 0 is no problem
1031 * for findThreadContextForSwapChain.
1033 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1034 * is perfect to call.
1036 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
1041 if(!oldRenderOffscreen
) {
1042 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
, StateTable
);
1043 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
1044 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1045 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1046 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
1050 /* When switching away from an offscreen render target, and we're not using FBOs,
1051 * we have to read the drawable into the texture. This is done via PreLoad(and
1052 * SFLAG_INDRAWABLE set on the surface). There are some things that need care though.
1053 * PreLoad needs a GL context, and FindContext is called before the context is activated.
1054 * It also has to be called with the old rendertarget active, otherwise a wrong drawable
1055 * is read. This leads to these possible situations:
1057 * 0) lastActiveRenderTarget == target && oldTid == newTid:
1058 * Nothing to do, we don't even reach this code in this case...
1060 * 1) lastActiveRenderTarget != target && oldTid == newTid:
1061 * The currently active context is OK for readback. Call PreLoad, and it
1064 * 2) lastActiveRenderTarget == target && oldTid != newTid:
1065 * Nothing to do - the drawable is unchanged
1067 * 3) lastActiveRenderTarget != target && oldTid != newTid:
1068 * This is tricky. We have to get a context with the old drawable from somewhere
1069 * before we can switch to the new context. In this case, PreLoad calls
1070 * ActivateContext(lastActiveRenderTarget) from the new(current) thread. This
1071 * is case (2) then. The old drawable is activated for the new thread, and the
1072 * readback can be done. The recursed ActivateContext does *not* call PreLoad again.
1073 * After that, the outer ActivateContext(which calls PreLoad) can activate the new
1074 * target for the new thread
1076 if (readTexture
&& This
->lastActiveRenderTarget
!= target
) {
1077 BOOL oldInDraw
= This
->isInDraw
;
1079 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
1080 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
1081 * when using offscreen rendering with multithreading
1083 This
->isInDraw
= TRUE
;
1085 /* Do that before switching the context:
1086 * Read the back buffer of the old drawable into the destination texture
1088 IWineD3DSurface_PreLoad(This
->lastActiveRenderTarget
);
1090 /* Assume that the drawable will be modified by some other things now */
1091 IWineD3DSurface_ModifyLocation(This
->lastActiveRenderTarget
, SFLAG_INDRAWABLE
, FALSE
);
1093 This
->isInDraw
= oldInDraw
;
1099 static void apply_draw_buffer(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, BOOL blit
)
1102 IWineD3DSwapChain
*swapchain
;
1104 hr
= IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **)&swapchain
);
1107 IWineD3DSwapChain_Release((IUnknown
*)swapchain
);
1108 glDrawBuffer(surface_get_gl_buffer(target
, swapchain
));
1109 checkGLcall("glDrawBuffers()");
1113 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1117 if (GL_SUPPORT(ARB_DRAW_BUFFERS
))
1119 GL_EXTCALL(glDrawBuffersARB(GL_LIMITS(buffers
), This
->draw_buffers
));
1120 checkGLcall("glDrawBuffers()");
1124 glDrawBuffer(This
->draw_buffers
[0]);
1125 checkGLcall("glDrawBuffer()");
1128 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT
);
1129 checkGLcall("glDrawBuffer()");
1134 glDrawBuffer(This
->offscreenBuffer
);
1135 checkGLcall("glDrawBuffer()");
1140 /*****************************************************************************
1143 * Finds a rendering context and drawable matching the device and render
1144 * target for the current thread, activates them and puts them into the
1148 * This: Device to activate the context for
1149 * target: Requested render target
1150 * usage: Prepares the context for blitting, drawing or other actions
1152 *****************************************************************************/
1153 void ActivateContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, ContextUsage usage
) {
1154 DWORD tid
= GetCurrentThreadId();
1156 DWORD dirtyState
, idx
;
1158 WineD3DContext
*context
;
1159 const struct StateEntry
*StateTable
= This
->StateTable
;
1161 TRACE("(%p): Selecting context for render target %p, thread %d\n", This
, target
, tid
);
1162 if(This
->lastActiveRenderTarget
!= target
|| tid
!= This
->lastThread
) {
1163 context
= FindContext(This
, target
, tid
);
1164 context
->draw_buffer_dirty
= TRUE
;
1165 This
->lastActiveRenderTarget
= target
;
1166 This
->lastThread
= tid
;
1168 /* Stick to the old context */
1169 context
= This
->activeContext
;
1172 /* Activate the opengl context */
1173 if(last_device
!= This
|| context
!= This
->activeContext
) {
1176 /* Prevent an unneeded context switch as those are expensive */
1177 if(context
->glCtx
&& (context
->glCtx
== pwglGetCurrentContext())) {
1178 TRACE("Already using gl context %p\n", context
->glCtx
);
1181 TRACE("Switching gl ctx to %p, hdc=%p ctx=%p\n", context
, context
->hdc
, context
->glCtx
);
1183 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
1184 ret
= pwglMakeCurrent(context
->hdc
, context
->glCtx
);
1186 ERR("Failed to activate the new context\n");
1187 } else if(!context
->last_was_blit
) {
1188 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
1191 if(This
->activeContext
->vshader_const_dirty
) {
1192 memset(This
->activeContext
->vshader_const_dirty
, 1,
1193 sizeof(*This
->activeContext
->vshader_const_dirty
) * GL_LIMITS(vshader_constantsF
));
1195 if(This
->activeContext
->pshader_const_dirty
) {
1196 memset(This
->activeContext
->pshader_const_dirty
, 1,
1197 sizeof(*This
->activeContext
->pshader_const_dirty
) * GL_LIMITS(pshader_constantsF
));
1199 This
->activeContext
= context
;
1203 /* We only need ENTER_GL for the gl calls made below and for the helper functions which make GL calls */
1207 case CTXUSAGE_CLEAR
:
1208 case CTXUSAGE_DRAWPRIM
:
1209 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
1210 apply_fbo_state((IWineD3DDevice
*)This
);
1212 if (context
->draw_buffer_dirty
) {
1213 apply_draw_buffer(This
, target
, FALSE
);
1214 context
->draw_buffer_dirty
= FALSE
;
1219 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
1220 if (This
->render_offscreen
) {
1221 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
1222 bind_fbo((IWineD3DDevice
*)This
, GL_FRAMEBUFFER_EXT
, &context
->dst_fbo
);
1223 attach_surface_fbo(This
, GL_FRAMEBUFFER_EXT
, 0, target
);
1224 GL_EXTCALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT
, GL_DEPTH_ATTACHMENT_EXT
, GL_RENDERBUFFER_EXT
, 0));
1225 checkGLcall("glFramebufferRenderbufferEXT");
1227 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0));
1228 checkGLcall("glFramebufferRenderbufferEXT");
1230 context
->draw_buffer_dirty
= TRUE
;
1232 if (context
->draw_buffer_dirty
) {
1233 apply_draw_buffer(This
, target
, TRUE
);
1234 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) {
1235 context
->draw_buffer_dirty
= FALSE
;
1245 case CTXUSAGE_RESOURCELOAD
:
1246 /* This does not require any special states to be set up */
1249 case CTXUSAGE_CLEAR
:
1250 if(context
->last_was_blit
) {
1251 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
1254 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
1255 * blending when clearing improves the clearing performance incredibly.
1257 glDisable(GL_BLEND
);
1258 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1260 glEnable(GL_SCISSOR_TEST
);
1261 checkGLcall("glEnable GL_SCISSOR_TEST");
1262 context
->last_was_blit
= FALSE
;
1263 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1264 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1267 case CTXUSAGE_DRAWPRIM
:
1268 /* This needs all dirty states applied */
1269 if(context
->last_was_blit
) {
1270 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, TRUE
);
1273 IWineD3DDeviceImpl_FindTexUnitMap(This
);
1275 for(i
=0; i
< context
->numDirtyEntries
; i
++) {
1276 dirtyState
= context
->dirtyArray
[i
];
1277 idx
= dirtyState
>> 5;
1278 shift
= dirtyState
& 0x1f;
1279 context
->isStateDirty
[idx
] &= ~(1 << shift
);
1280 StateTable
[dirtyState
].apply(dirtyState
, This
->stateBlock
, context
);
1282 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
1283 context
->last_was_blit
= FALSE
;
1287 SetupForBlit(This
, context
,
1288 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Width
,
1289 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Height
);
1293 FIXME("Unexpected context usage requested\n");