2 * Context and render target management in wined3d
4 * Copyright 2007 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 /*****************************************************************************
33 * Context_MarkStateDirty
35 * Marks a state in a context dirty. Only one context, opposed to
36 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
40 * context: Context to mark the state dirty in
41 * state: State to mark dirty
43 *****************************************************************************/
44 static void Context_MarkStateDirty(WineD3DContext
*context
, DWORD state
) {
45 DWORD rep
= StateTable
[state
].representative
;
49 if(!rep
|| isStateDirty(context
, rep
)) return;
51 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
54 context
->isStateDirty
[idx
] |= (1 << shift
);
57 /*****************************************************************************
60 * Adds a context to the context array. Helper function for CreateContext
62 * This method is not called in performance-critical code paths, only when a
63 * new render target or swapchain is created. Thus performance is not an issue
67 * This: Device to add the context for
68 * display: X display this context uses
69 * glCtx: glX context to add
70 * drawable: drawable used with this context.
72 *****************************************************************************/
73 static WineD3DContext
*AddContextToArray(IWineD3DDeviceImpl
*This
, Display
*display
, GLXContext glCtx
, Drawable drawable
) {
74 WineD3DContext
**oldArray
= This
->contexts
;
77 This
->contexts
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->contexts
) * (This
->numContexts
+ 1));
78 if(This
->contexts
== NULL
) {
79 ERR("Unable to grow the context array\n");
80 This
->contexts
= oldArray
;
84 memcpy(This
->contexts
, oldArray
, sizeof(*This
->contexts
) * This
->numContexts
);
87 This
->contexts
[This
->numContexts
] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WineD3DContext
));
88 if(This
->contexts
[This
->numContexts
] == NULL
) {
89 ERR("Unable to allocate a new context\n");
90 HeapFree(GetProcessHeap(), 0, This
->contexts
);
91 This
->contexts
= oldArray
;
95 This
->contexts
[This
->numContexts
]->display
= display
;
96 This
->contexts
[This
->numContexts
]->glCtx
= glCtx
;
97 This
->contexts
[This
->numContexts
]->drawable
= drawable
;
98 HeapFree(GetProcessHeap(), 0, oldArray
);
100 /* Mark all states dirty to force a proper initialization of the states on the first use of the context
102 for(state
= 0; state
<= STATE_HIGHEST
; state
++) {
103 Context_MarkStateDirty(This
->contexts
[This
->numContexts
], state
);
107 TRACE("Created context %p\n", This
->contexts
[This
->numContexts
- 1]);
108 return This
->contexts
[This
->numContexts
- 1];
111 /* Returns an array of compatible FBconfig(s).
112 * The array must be freed with XFree. Requires ENTER_GL()
114 static GLXFBConfig
* pbuffer_find_fbconfigs(
115 IWineD3DDeviceImpl
* This
,
116 IWineD3DSurfaceImpl
* RenderSurface
,
119 GLXFBConfig
* cfgs
= NULL
;
124 IWineD3DSurface
*StencilSurface
= This
->stencilBufferTarget
;
125 WINED3DFORMAT BackBufferFormat
= RenderSurface
->resource
.format
;
126 WINED3DFORMAT StencilBufferFormat
= (NULL
!= StencilSurface
) ? ((IWineD3DSurfaceImpl
*) StencilSurface
)->resource
.format
: 0;
129 * if StencilSurface == NULL && zBufferTarget != NULL then switch the zbuffer off,
130 * it StencilSurface != NULL && zBufferTarget == NULL switch it on
133 #define PUSH1(att) attribs[nAttribs++] = (att);
134 #define PUSH2(att,value) attribs[nAttribs++] = (att); attribs[nAttribs++] = (value);
136 /* PUSH2(GLX_BIND_TO_TEXTURE_RGBA_ATI, True); examples of this are few and far between (but I've got a nice working one!)*/
138 PUSH2(GLX_DRAWABLE_TYPE
, GLX_PBUFFER_BIT
);
139 PUSH2(GLX_X_RENDERABLE
, TRUE
);
140 PUSH2(GLX_DOUBLEBUFFER
, TRUE
);
141 TRACE("calling makeglcfg\n");
142 D3DFmtMakeGlCfg(BackBufferFormat
, StencilBufferFormat
, attribs
, &nAttribs
, FALSE
/* alternate */);
144 TRACE("calling chooseFGConfig\n");
145 cfgs
= glXChooseFBConfig(display
,
146 DefaultScreen(display
),
149 /* OK we didn't find the exact config, so use any reasonable match */
150 /* TODO: fill in the 'requested' and 'current' depths, and make sure that's
152 static BOOL show_message
= TRUE
;
154 ERR("Failed to find exact match, finding alternative but you may "
155 "suffer performance issues, try changing xfree's depth to match the requested depth\n");
156 show_message
= FALSE
;
159 PUSH2(GLX_DRAWABLE_TYPE
, GLX_PBUFFER_BIT
| GLX_WINDOW_BIT
);
160 /* PUSH2(GLX_X_RENDERABLE, TRUE); */
161 PUSH2(GLX_RENDER_TYPE
, GLX_RGBA_BIT
);
162 PUSH2(GLX_DOUBLEBUFFER
, FALSE
);
163 TRACE("calling makeglcfg\n");
164 D3DFmtMakeGlCfg(BackBufferFormat
, StencilBufferFormat
, attribs
, &nAttribs
, TRUE
/* alternate */);
166 cfgs
= glXChooseFBConfig(display
,
167 DefaultScreen(display
),
172 ERR("Could not get a valid FBConfig for (%u,%s)/(%u,%s)\n",
173 BackBufferFormat
, debug_d3dformat(BackBufferFormat
),
174 StencilBufferFormat
, debug_d3dformat(StencilBufferFormat
));
178 for (i
= 0; i
< nCfgs
; ++i
) {
179 TRACE("for (%u,%s)/(%u,%s) found config[%d]@%p\n", BackBufferFormat
,
180 debug_d3dformat(BackBufferFormat
), StencilBufferFormat
,
181 debug_d3dformat(StencilBufferFormat
), i
, cfgs
[i
]);
191 /*****************************************************************************
194 * Creates a new context for a window, or a pbuffer context.
197 * This: Device to activate the context for
198 * target: Surface this context will render to
199 * display: X11 connection
200 * win: Target window. NULL for a pbuffer
202 *****************************************************************************/
203 WineD3DContext
*CreateContext(IWineD3DDeviceImpl
*This
, IWineD3DSurfaceImpl
*target
, Display
*display
, Window win
) {
204 Drawable drawable
= win
, oldDrawable
;
205 XVisualInfo
*visinfo
= NULL
;
206 GLXFBConfig
*cfgs
= NULL
;
207 GLXContext ctx
= NULL
, oldCtx
;
208 WineD3DContext
*ret
= NULL
;
211 TRACE("(%p): Creating a %s context for render target %p\n", This
, win
? "onscreen" : "offscreen", target
);
217 TRACE("Creating a pBuffer drawable for the new context\n");
219 cfgs
= pbuffer_find_fbconfigs(This
, target
, display
);
221 ERR("Cannot find a frame buffer configuration for the pbuffer\n");
225 attribs
[nAttribs
++] = GLX_PBUFFER_WIDTH
;
226 attribs
[nAttribs
++] = target
->currentDesc
.Width
;
227 attribs
[nAttribs
++] = GLX_PBUFFER_HEIGHT
;
228 attribs
[nAttribs
++] = target
->currentDesc
.Height
;
229 attribs
[nAttribs
++] = None
;
231 visinfo
= glXGetVisualFromFBConfig(display
, cfgs
[0]);
233 ERR("Cannot find a visual for the pbuffer\n");
237 drawable
= glXCreatePbuffer(display
, cfgs
[0], attribs
);
240 ERR("Cannot create a pbuffer\n");
246 /* Create an onscreen target */
247 XVisualInfo
template;
250 template.visualid
= (VisualID
)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
251 /* TODO: change this to find a similar visual, but one with a stencil/zbuffer buffer that matches the request
252 (or the best possible if none is requested) */
253 TRACE("Found x visual ID : %ld\n", template.visualid
);
254 visinfo
= XGetVisualInfo(display
, VisualIDMask
, &template, &num
);
256 if (NULL
== visinfo
) {
257 ERR("cannot really get XVisual\n");
261 /* Write out some debug info about the visual/s */
262 TRACE("Using x visual ID : %ld\n", template.visualid
);
263 TRACE(" visual info: %p\n", visinfo
);
264 TRACE(" num items : %d\n", num
);
265 for (n
= 0;n
< num
; n
++) {
266 TRACE("=====item=====: %d\n", n
+ 1);
267 TRACE(" visualid : %ld\n", visinfo
[n
].visualid
);
268 TRACE(" screen : %d\n", visinfo
[n
].screen
);
269 TRACE(" depth : %u\n", visinfo
[n
].depth
);
270 TRACE(" class : %d\n", visinfo
[n
].class);
271 TRACE(" red_mask : %ld\n", visinfo
[n
].red_mask
);
272 TRACE(" green_mask : %ld\n", visinfo
[n
].green_mask
);
273 TRACE(" blue_mask : %ld\n", visinfo
[n
].blue_mask
);
274 TRACE(" colormap_size : %d\n", visinfo
[n
].colormap_size
);
275 TRACE(" bits_per_rgb : %d\n", visinfo
[n
].bits_per_rgb
);
276 /* log some extra glx info */
277 glXGetConfig(display
, visinfo
, GLX_AUX_BUFFERS
, &value
);
278 TRACE(" gl_aux_buffers : %d\n", value
);
279 glXGetConfig(display
, visinfo
, GLX_BUFFER_SIZE
,&value
);
280 TRACE(" gl_buffer_size : %d\n", value
);
281 glXGetConfig(display
, visinfo
, GLX_RED_SIZE
, &value
);
282 TRACE(" gl_red_size : %d\n", value
);
283 glXGetConfig(display
, visinfo
, GLX_GREEN_SIZE
, &value
);
284 TRACE(" gl_green_size : %d\n", value
);
285 glXGetConfig(display
, visinfo
, GLX_BLUE_SIZE
, &value
);
286 TRACE(" gl_blue_size : %d\n", value
);
287 glXGetConfig(display
, visinfo
, GLX_ALPHA_SIZE
, &value
);
288 TRACE(" gl_alpha_size : %d\n", value
);
289 glXGetConfig(display
, visinfo
, GLX_DEPTH_SIZE
,&value
);
290 TRACE(" gl_depth_size : %d\n", value
);
291 glXGetConfig(display
, visinfo
, GLX_STENCIL_SIZE
, &value
);
292 TRACE(" gl_stencil_size : %d\n", value
);
294 /* Now choose a similar visual ID*/
298 ctx
= glXCreateContext(display
, visinfo
,
299 This
->numContexts
? This
->contexts
[0]->glCtx
: NULL
,
302 ERR("Failed to create a glX context\n");
303 if(drawable
!= win
) glXDestroyPbuffer(display
, drawable
);
306 ret
= AddContextToArray(This
, display
, ctx
, drawable
);
308 ERR("Failed to add the newly created context to the context list\n");
309 glXDestroyContext(display
, ctx
);
310 if(drawable
!= win
) glXDestroyPbuffer(display
, drawable
);
313 ret
->surface
= (IWineD3DSurface
*) target
;
314 ret
->isPBuffer
= win
== 0;
315 ret
->tid
= GetCurrentThreadId();
317 TRACE("Successfully created new context %p\n", ret
);
319 /* Set up the context defaults */
320 oldCtx
= glXGetCurrentContext();
321 oldDrawable
= glXGetCurrentDrawable();
322 if(glXMakeCurrent(display
, drawable
, ctx
) == FALSE
) {
323 ERR("Cannot activate context to set up defaults\n");
327 TRACE("Setting up the screen\n");
328 /* Clear the screen */
329 glClearColor(1.0, 0.0, 0.0, 0.0);
330 checkGLcall("glClearColor");
333 glClearStencil(0xffff);
335 checkGLcall("glClear");
337 glColor3f(1.0, 1.0, 1.0);
338 checkGLcall("glColor3f");
340 glEnable(GL_LIGHTING
);
341 checkGLcall("glEnable");
343 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
344 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
346 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
347 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
349 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
350 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
352 glPixelStorei(GL_PACK_ALIGNMENT
, This
->surface_alignment
);
353 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
354 glPixelStorei(GL_UNPACK_ALIGNMENT
, This
->surface_alignment
);
355 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
357 if(GL_SUPPORT(APPLE_CLIENT_STORAGE
)) {
358 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
359 * and textures in DIB sections(due to the memory protection).
361 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
362 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
364 if(GL_SUPPORT(ARB_VERTEX_BLEND
)) {
365 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
366 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
367 * GL_VERTEX_BLEND_ARB isn't enabled too
369 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
370 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
372 if(GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
373 glEnable(GL_TEXTURE_SHADER_NV
);
374 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
376 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
377 * the previous texture where to source the offset from is always unit - 1.
379 for(s
= 1; s
< GL_LIMITS(textures
); s
++) {
380 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
381 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
382 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
385 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
386 for(s
= 0; s
< GL_LIMITS(textures
); s
++) {
387 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
388 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
389 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
393 if(oldDrawable
&& oldCtx
) {
394 glXMakeCurrent(display
, oldDrawable
, oldCtx
);
398 if(visinfo
) XFree(visinfo
);
399 if(cfgs
) XFree(cfgs
);
403 /*****************************************************************************
404 * RemoveContextFromArray
406 * Removes a context from the context manager. The opengl context is not
407 * destroyed or unset. context is not a valid pointer after that call.
409 * Similar to the former call this isn't a performance critical function. A
410 * helper function for DestroyContext.
413 * This: Device to activate the context for
414 * context: Context to remove
416 *****************************************************************************/
417 static void RemoveContextFromArray(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
419 WineD3DContext
**oldArray
= This
->contexts
;
421 TRACE("Removing ctx %p\n", context
);
425 if(This
->numContexts
) {
426 This
->contexts
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->contexts
) * This
->numContexts
);
427 if(!This
->contexts
) {
428 ERR("Cannot allocate a new context array, PANIC!!!\n");
431 for(s
= 0; s
< This
->numContexts
; s
++) {
432 if(oldArray
[s
] == context
) continue;
433 This
->contexts
[t
] = oldArray
[s
];
437 This
->contexts
= NULL
;
440 HeapFree(GetProcessHeap(), 0, context
);
441 HeapFree(GetProcessHeap(), 0, oldArray
);
444 /*****************************************************************************
447 * Destroys a wineD3DContext
450 * This: Device to activate the context for
451 * context: Context to destroy
453 *****************************************************************************/
454 void DestroyContext(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
456 /* check that we are the current context first */
457 TRACE("Destroying ctx %p\n", context
);
458 if(glXGetCurrentContext() == context
->glCtx
){
459 glXMakeCurrent(context
->display
, None
, NULL
);
462 glXDestroyContext(context
->display
, context
->glCtx
);
463 if(context
->isPBuffer
) {
464 glXDestroyPbuffer(context
->display
, context
->drawable
);
466 RemoveContextFromArray(This
, context
);
469 /*****************************************************************************
472 * Sets up a context for DirectDraw blitting.
473 * All texture units are disabled, except unit 0
474 * Texture unit 0 is activted where GL_TEXTURE_2D is activated
475 * fog, lighting, blending, alpha test, z test, scissor test, culling diabled
476 * color writing enabled for all channels
477 * register combiners disabled, shaders disabled
478 * world matris is set to identity, texture matrix 0 too
479 * projection matrix is setup for drawing screen coordinates
482 * This: Device to activate the context for
483 * context: Context to setup
484 * width: render target width
485 * height: render target height
487 *****************************************************************************/
488 static inline void SetupForBlit(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
, UINT width
, UINT height
) {
491 TRACE("Setting up context %p for blitting\n", context
);
492 if(context
->last_was_blit
) {
493 TRACE("Context is already set up for blitting, nothing to do\n");
496 context
->last_was_blit
= TRUE
;
498 /* TODO: Use a display list */
500 /* Disable shaders */
501 This
->shader_backend
->shader_cleanup((IWineD3DDevice
*) This
);
502 Context_MarkStateDirty(context
, STATE_VSHADER
);
503 Context_MarkStateDirty(context
, STATE_PIXELSHADER
);
505 /* Disable all textures. The caller can then bind a texture it wants to blit
508 if(GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
509 glDisable(GL_REGISTER_COMBINERS_NV
);
510 checkGLcall("glDisable(GL_REGISTER_COMBINERS_NV)");
512 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
513 /* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
514 * function texture unit. No need to care for higher samplers
516 for(i
= GL_LIMITS(textures
) - 1; i
> 0 ; i
--) {
517 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
518 checkGLcall("glActiveTextureARB");
520 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
521 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
522 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
524 glDisable(GL_TEXTURE_3D
);
525 checkGLcall("glDisable GL_TEXTURE_3D");
526 glDisable(GL_TEXTURE_2D
);
527 checkGLcall("glDisable GL_TEXTURE_2D");
529 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
530 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
532 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(i
, WINED3DTSS_COLOROP
));
533 Context_MarkStateDirty(context
, STATE_SAMPLER(i
));
535 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
536 checkGLcall("glActiveTextureARB");
538 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
539 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
540 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
542 glDisable(GL_TEXTURE_3D
);
543 checkGLcall("glDisable GL_TEXTURE_3D");
544 glEnable(GL_TEXTURE_2D
);
545 checkGLcall("glEnable GL_TEXTURE_2D");
547 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
549 glMatrixMode(GL_TEXTURE
);
550 checkGLcall("glMatrixMode(GL_TEXTURE)");
552 checkGLcall("glLoadIdentity()");
553 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
));
555 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS
)) {
556 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
557 GL_TEXTURE_LOD_BIAS_EXT
,
559 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
561 Context_MarkStateDirty(context
, STATE_SAMPLER(0));
562 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP
));
564 /* Other misc states */
565 glDisable(GL_ALPHA_TEST
);
566 checkGLcall("glDisable(GL_ALPHA_TEST)");
567 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
));
568 glDisable(GL_LIGHTING
);
569 checkGLcall("glDisable GL_LIGHTING");
570 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
));
571 glDisable(GL_DEPTH_TEST
);
572 checkGLcall("glDisable GL_DEPTH_TEST");
573 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
));
575 checkGLcall("glDisable GL_FOG");
576 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
));
578 checkGLcall("glDisable GL_BLEND");
579 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
));
580 glDisable(GL_CULL_FACE
);
581 checkGLcall("glDisable GL_CULL_FACE");
582 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
));
583 glDisable(GL_STENCIL_TEST
);
584 checkGLcall("glDisable GL_STENCIL_TEST");
585 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
));
586 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
587 glDisable(GL_POINT_SPRITE_ARB
);
588 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
589 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
));
591 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
592 checkGLcall("glColorMask");
593 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
));
595 /* Setup transforms */
596 glMatrixMode(GL_MODELVIEW
);
597 checkGLcall("glMatrixMode(GL_MODELVIEW)");
599 checkGLcall("glLoadIdentity()");
600 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)));
602 glMatrixMode(GL_PROJECTION
);
603 checkGLcall("glMatrixMode(GL_PROJECTION)");
605 checkGLcall("glLoadIdentity()");
606 glOrtho(0, width
, height
, 0, 0.0, -1.0);
607 checkGLcall("glOrtho");
608 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
));
610 context
->last_was_rhw
= TRUE
;
611 Context_MarkStateDirty(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
613 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
614 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
615 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
616 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
617 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
618 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
619 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
));
621 glViewport(0, 0, width
, height
);
622 checkGLcall("glViewport");
623 Context_MarkStateDirty(context
, STATE_VIEWPORT
);
625 if(GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
626 glDisable(GL_TEXTURE_SHADER_NV
);
627 checkGLcall("glDisable(GL_TEXTURE_SHADER_NV)");
631 /*****************************************************************************
632 * findThreadContextForSwapChain
634 * Searches a swapchain for all contexts and picks one for the thread tid.
635 * If none can be found the swapchain is requested to create a new context
637 *****************************************************************************/
638 static WineD3DContext
*findThreadContextForSwapChain(IWineD3DSwapChain
*swapchain
, DWORD tid
) {
641 for(i
= 0; i
< ((IWineD3DSwapChainImpl
*) swapchain
)->num_contexts
; i
++) {
642 if(((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
]->tid
== tid
) {
643 return ((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
];
648 /* Create a new context for the thread */
649 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain
);
652 /*****************************************************************************
655 * Finds a context for the current render target and thread
658 * target: Render target to find the context for
659 * tid: Thread to activate the context for
661 * Returns: The needed context
663 *****************************************************************************/
664 static inline WineD3DContext
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, DWORD tid
) {
665 IWineD3DSwapChain
*swapchain
= NULL
;
667 BOOL readTexture
= wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
&& This
->render_offscreen
;
668 WineD3DContext
*context
= This
->activeContext
;
669 BOOL oldRenderOffscreen
= This
->render_offscreen
;
671 hr
= IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
672 if(hr
== WINED3D_OK
&& swapchain
) {
673 TRACE("Rendering onscreen\n");
675 context
= findThreadContextForSwapChain(swapchain
, tid
);
677 This
->render_offscreen
= FALSE
;
678 /* The context != This->activeContext will catch a NOP context change. This can occur
679 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
680 * rendering. No context change is needed in that case
683 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
) {
684 if(((IWineD3DSwapChainImpl
*) swapchain
)->backBuffer
) {
685 glDrawBuffer(GL_BACK
);
686 checkGLcall("glDrawBuffer(GL_BACK)");
688 glDrawBuffer(GL_FRONT
);
689 checkGLcall("glDrawBuffer(GL_FRONT)");
691 } else if(wined3d_settings
.offscreen_rendering_mode
== ORM_PBUFFER
) {
692 if(This
->pbufferContext
&& tid
== This
->pbufferContext
->tid
) {
693 This
->pbufferContext
->tid
= 0;
696 IWineD3DSwapChain_Release(swapchain
);
698 if(oldRenderOffscreen
) {
699 Context_MarkStateDirty(context
, WINED3DRS_CULLMODE
);
700 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
);
701 Context_MarkStateDirty(context
, STATE_VDECL
);
702 Context_MarkStateDirty(context
, STATE_VIEWPORT
);
703 Context_MarkStateDirty(context
, STATE_SCISSORRECT
);
707 TRACE("Rendering offscreen\n");
708 This
->render_offscreen
= TRUE
;
710 switch(wined3d_settings
.offscreen_rendering_mode
) {
712 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
713 if(This
->activeContext
&& tid
== This
->lastThread
) {
714 context
= This
->activeContext
;
716 /* This may happen if the app jumps streight into offscreen rendering
717 * Start using the context of the primary swapchain. tid == 0 is no problem
718 * for findThreadContextForSwapChain.
720 * Can also happen on thread switches - in that case findThreadContextForSwapChain
721 * is perfect to call.
723 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
729 IWineD3DSurfaceImpl
*targetimpl
= (IWineD3DSurfaceImpl
*) target
;
730 if(This
->pbufferContext
== NULL
||
731 This
->pbufferWidth
< targetimpl
->currentDesc
.Width
||
732 This
->pbufferHeight
< targetimpl
->currentDesc
.Height
) {
733 if(This
->pbufferContext
) {
734 DestroyContext(This
, This
->pbufferContext
);
737 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
738 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
740 This
->pbufferContext
= CreateContext(This
, targetimpl
,
741 ((IWineD3DSwapChainImpl
*) This
->swapchains
[0])->context
[0]->display
,
743 This
->pbufferWidth
= targetimpl
->currentDesc
.Width
;
744 This
->pbufferHeight
= targetimpl
->currentDesc
.Height
;
747 if(This
->pbufferContext
) {
748 if(This
->pbufferContext
->tid
!= 0 && This
->pbufferContext
->tid
!= tid
) {
749 FIXME("The PBuffr context is only supported for one thread for now!\n");
751 This
->pbufferContext
->tid
= tid
;
752 context
= This
->pbufferContext
;
755 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
756 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
761 /* Stay with the currently active context for back buffer rendering */
762 if(This
->activeContext
&& tid
== This
->lastThread
) {
763 context
= This
->activeContext
;
765 /* This may happen if the app jumps streight into offscreen rendering
766 * Start using the context of the primary swapchain. tid == 0 is no problem
767 * for findThreadContextForSwapChain.
769 * Can also happen on thread switches - in that case findThreadContextForSwapChain
770 * is perfect to call.
772 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
774 glDrawBuffer(This
->offscreenBuffer
);
775 checkGLcall("glDrawBuffer(This->offscreenBuffer)");
779 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) {
780 /* Make sure we have a OpenGL texture name so the PreLoad() used to read the buffer
781 * back when we are done won't mark us dirty.
783 IWineD3DSurface_PreLoad(target
);
786 if(!oldRenderOffscreen
) {
787 Context_MarkStateDirty(context
, WINED3DRS_CULLMODE
);
788 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
);
789 Context_MarkStateDirty(context
, STATE_VDECL
);
790 Context_MarkStateDirty(context
, STATE_VIEWPORT
);
791 Context_MarkStateDirty(context
, STATE_SCISSORRECT
);
795 BOOL oldInDraw
= This
->isInDraw
;
797 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
798 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
799 * when using offscreen rendering with multithreading
801 This
->isInDraw
= TRUE
;
803 /* Do that before switching the context:
804 * Read the back buffer of the old drawable into the destination texture
806 IWineD3DSurface_PreLoad(This
->lastActiveRenderTarget
);
808 /* Assume that the drawable will be modified by some other things now */
809 ((IWineD3DSurfaceImpl
*) This
->lastActiveRenderTarget
)->Flags
&= ~SFLAG_INDRAWABLE
;
811 This
->isInDraw
= oldInDraw
;
814 if(oldRenderOffscreen
!= This
->render_offscreen
&& This
->depth_copy_state
!= WINED3D_DCS_NO_COPY
) {
815 This
->depth_copy_state
= WINED3D_DCS_COPY
;
820 /*****************************************************************************
823 * Finds a rendering context and drawable matching the device and render
824 * target for the current thread, activates them and puts them into the
828 * This: Device to activate the context for
829 * target: Requested render target
830 * usage: Prepares the context for blitting, drawing or other actions
832 *****************************************************************************/
833 void ActivateContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, ContextUsage usage
) {
834 DWORD tid
= GetCurrentThreadId();
836 DWORD dirtyState
, idx
;
838 WineD3DContext
*context
;
840 TRACE("(%p): Selecting context for render target %p, thread %d\n", This
, target
, tid
);
843 if(This
->lastActiveRenderTarget
!= target
|| tid
!= This
->lastThread
) {
844 context
= FindContext(This
, target
, tid
);
845 This
->lastActiveRenderTarget
= target
;
846 This
->lastThread
= tid
;
848 /* Stick to the old context */
849 context
= This
->activeContext
;
852 /* Activate the opengl context */
853 if(context
!= This
->activeContext
) {
855 TRACE("Switching gl ctx to %p, drawable=%ld, ctx=%p\n", context
, context
->drawable
, context
->glCtx
);
856 ret
= glXMakeCurrent(context
->display
, context
->drawable
, context
->glCtx
);
858 ERR("Failed to activate the new context\n");
860 This
->activeContext
= context
;
864 case CTXUSAGE_RESOURCELOAD
:
865 /* This does not require any special states to be set up */
869 if(context
->last_was_blit
&& GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
870 glEnable(GL_TEXTURE_SHADER_NV
);
871 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
874 glEnable(GL_SCISSOR_TEST
);
875 checkGLcall("glEnable GL_SCISSOR_TEST");
876 context
->last_was_blit
= FALSE
;
877 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
));
878 Context_MarkStateDirty(context
, STATE_SCISSORRECT
);
881 case CTXUSAGE_DRAWPRIM
:
882 /* This needs all dirty states applied */
883 if(context
->last_was_blit
&& GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
884 glEnable(GL_TEXTURE_SHADER_NV
);
885 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
888 IWineD3DDeviceImpl_FindTexUnitMap(This
);
890 for(i
=0; i
< context
->numDirtyEntries
; i
++) {
891 dirtyState
= context
->dirtyArray
[i
];
892 idx
= dirtyState
>> 5;
893 shift
= dirtyState
& 0x1f;
894 context
->isStateDirty
[idx
] &= ~(1 << shift
);
895 StateTable
[dirtyState
].apply(dirtyState
, This
->stateBlock
, context
);
897 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
898 context
->last_was_blit
= FALSE
;
902 SetupForBlit(This
, context
,
903 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Width
,
904 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Height
);
908 FIXME("Unexpected context usage requested\n");