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
;
210 TRACE("(%p): Creating a %s context for render target %p\n", This
, win
? "onscreen" : "offscreen", target
);
216 TRACE("Creating a pBuffer drawable for the new context\n");
218 cfgs
= pbuffer_find_fbconfigs(This
, target
, display
);
220 ERR("Cannot find a frame buffer configuration for the pbuffer\n");
224 attribs
[nAttribs
++] = GLX_PBUFFER_WIDTH
;
225 attribs
[nAttribs
++] = target
->currentDesc
.Width
;
226 attribs
[nAttribs
++] = GLX_PBUFFER_HEIGHT
;
227 attribs
[nAttribs
++] = target
->currentDesc
.Height
;
228 attribs
[nAttribs
++] = None
;
230 visinfo
= glXGetVisualFromFBConfig(display
, cfgs
[0]);
232 ERR("Cannot find a visual for the pbuffer\n");
236 drawable
= glXCreatePbuffer(display
, cfgs
[0], attribs
);
239 ERR("Cannot create a pbuffer\n");
245 /* Create an onscreen target */
246 XVisualInfo
template;
249 template.visualid
= (VisualID
)GetPropA(GetDesktopWindow(), "__wine_x11_visual_id");
250 /* TODO: change this to find a similar visual, but one with a stencil/zbuffer buffer that matches the request
251 (or the best possible if none is requested) */
252 TRACE("Found x visual ID : %ld\n", template.visualid
);
253 visinfo
= XGetVisualInfo(display
, VisualIDMask
, &template, &num
);
255 if (NULL
== visinfo
) {
256 ERR("cannot really get XVisual\n");
260 /* Write out some debug info about the visual/s */
261 TRACE("Using x visual ID : %ld\n", template.visualid
);
262 TRACE(" visual info: %p\n", visinfo
);
263 TRACE(" num items : %d\n", num
);
264 for (n
= 0;n
< num
; n
++) {
265 TRACE("=====item=====: %d\n", n
+ 1);
266 TRACE(" visualid : %ld\n", visinfo
[n
].visualid
);
267 TRACE(" screen : %d\n", visinfo
[n
].screen
);
268 TRACE(" depth : %u\n", visinfo
[n
].depth
);
269 TRACE(" class : %d\n", visinfo
[n
].class);
270 TRACE(" red_mask : %ld\n", visinfo
[n
].red_mask
);
271 TRACE(" green_mask : %ld\n", visinfo
[n
].green_mask
);
272 TRACE(" blue_mask : %ld\n", visinfo
[n
].blue_mask
);
273 TRACE(" colormap_size : %d\n", visinfo
[n
].colormap_size
);
274 TRACE(" bits_per_rgb : %d\n", visinfo
[n
].bits_per_rgb
);
275 /* log some extra glx info */
276 glXGetConfig(display
, visinfo
, GLX_AUX_BUFFERS
, &value
);
277 TRACE(" gl_aux_buffers : %d\n", value
);
278 glXGetConfig(display
, visinfo
, GLX_BUFFER_SIZE
,&value
);
279 TRACE(" gl_buffer_size : %d\n", value
);
280 glXGetConfig(display
, visinfo
, GLX_RED_SIZE
, &value
);
281 TRACE(" gl_red_size : %d\n", value
);
282 glXGetConfig(display
, visinfo
, GLX_GREEN_SIZE
, &value
);
283 TRACE(" gl_green_size : %d\n", value
);
284 glXGetConfig(display
, visinfo
, GLX_BLUE_SIZE
, &value
);
285 TRACE(" gl_blue_size : %d\n", value
);
286 glXGetConfig(display
, visinfo
, GLX_ALPHA_SIZE
, &value
);
287 TRACE(" gl_alpha_size : %d\n", value
);
288 glXGetConfig(display
, visinfo
, GLX_DEPTH_SIZE
,&value
);
289 TRACE(" gl_depth_size : %d\n", value
);
290 glXGetConfig(display
, visinfo
, GLX_STENCIL_SIZE
, &value
);
291 TRACE(" gl_stencil_size : %d\n", value
);
293 /* Now choose a similar visual ID*/
297 ctx
= glXCreateContext(display
, visinfo
,
298 This
->numContexts
? This
->contexts
[0]->glCtx
: NULL
,
301 ERR("Failed to create a glX context\n");
302 if(drawable
!= win
) glXDestroyPbuffer(display
, drawable
);
305 ret
= AddContextToArray(This
, display
, ctx
, drawable
);
307 ERR("Failed to add the newly created context to the context list\n");
308 glXDestroyContext(display
, ctx
);
309 if(drawable
!= win
) glXDestroyPbuffer(display
, drawable
);
312 ret
->surface
= (IWineD3DSurface
*) target
;
313 ret
->isPBuffer
= win
== 0;
315 TRACE("Successfully created new context %p\n", ret
);
317 /* Set up the context defaults */
318 oldCtx
= glXGetCurrentContext();
319 oldDrawable
= glXGetCurrentDrawable();
320 if(glXMakeCurrent(display
, drawable
, ctx
) == FALSE
) {
321 ERR("Cannot activate context to set up defaults\n");
325 TRACE("Setting up the screen\n");
326 /* Clear the screen */
327 glClearColor(1.0, 0.0, 0.0, 0.0);
328 checkGLcall("glClearColor");
331 glClearStencil(0xffff);
333 checkGLcall("glClear");
335 glColor3f(1.0, 1.0, 1.0);
336 checkGLcall("glColor3f");
338 glEnable(GL_LIGHTING
);
339 checkGLcall("glEnable");
341 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
342 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
344 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
345 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
347 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
348 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
350 glPixelStorei(GL_PACK_ALIGNMENT
, This
->surface_alignment
);
351 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
352 glPixelStorei(GL_UNPACK_ALIGNMENT
, This
->surface_alignment
);
353 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
355 if(GL_SUPPORT(APPLE_CLIENT_STORAGE
)) {
356 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
357 * and textures in DIB sections(due to the memory protection).
359 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
360 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
362 if(GL_SUPPORT(ARB_VERTEX_BLEND
)) {
363 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
364 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
365 * GL_VERTEX_BLEND_ARB isn't enabled too
367 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
368 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
370 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");
386 if(oldDrawable
&& oldCtx
) {
387 glXMakeCurrent(display
, oldDrawable
, oldCtx
);
391 if(visinfo
) XFree(visinfo
);
392 if(cfgs
) XFree(cfgs
);
396 /*****************************************************************************
397 * RemoveContextFromArray
399 * Removes a context from the context manager. The opengl context is not
400 * destroyed or unset. context is not a valid pointer after that call.
402 * Similar to the former call this isn't a performance critical function. A
403 * helper function for DestroyContext.
406 * This: Device to activate the context for
407 * context: Context to remove
409 *****************************************************************************/
410 static void RemoveContextFromArray(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
412 WineD3DContext
**oldArray
= This
->contexts
;
414 TRACE("Removing ctx %p\n", context
);
418 if(This
->numContexts
) {
419 This
->contexts
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->contexts
) * This
->numContexts
);
420 if(!This
->contexts
) {
421 ERR("Cannot allocate a new context array, PANIC!!!\n");
424 for(s
= 0; s
< This
->numContexts
; s
++) {
425 if(oldArray
[s
] == context
) continue;
426 This
->contexts
[t
] = oldArray
[s
];
430 This
->contexts
= NULL
;
433 HeapFree(GetProcessHeap(), 0, context
);
434 HeapFree(GetProcessHeap(), 0, oldArray
);
437 /*****************************************************************************
440 * Destroys a wineD3DContext
443 * This: Device to activate the context for
444 * context: Context to destroy
446 *****************************************************************************/
447 void DestroyContext(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
) {
449 /* check that we are the current context first */
450 TRACE("Destroying ctx %p\n", context
);
451 if(glXGetCurrentContext() == context
->glCtx
){
452 glXMakeCurrent(context
->display
, None
, NULL
);
455 glXDestroyContext(context
->display
, context
->glCtx
);
456 if(context
->isPBuffer
) {
457 glXDestroyPbuffer(context
->display
, context
->drawable
);
459 RemoveContextFromArray(This
, context
);
462 /*****************************************************************************
465 * Sets up a context for DirectDraw blitting.
466 * All texture units are disabled, except unit 0
467 * Texture unit 0 is activted where GL_TEXTURE_2D is activated
468 * fog, lighting, blending, alpha test, z test, scissor test, culling diabled
469 * color writing enabled for all channels
470 * register combiners disabled, shaders disabled
471 * world matris is set to identity, texture matrix 0 too
472 * projection matrix is setup for drawing screen coordinates
475 * This: Device to activate the context for
476 * context: Context to setup
477 * width: render target width
478 * height: render target height
480 *****************************************************************************/
481 static inline void SetupForBlit(IWineD3DDeviceImpl
*This
, WineD3DContext
*context
, UINT width
, UINT height
) {
484 TRACE("Setting up context %p for blitting\n", context
);
485 if(context
->last_was_blit
) {
486 TRACE("Context is already set up for blitting, nothing to do\n");
489 context
->last_was_blit
= TRUE
;
491 /* TODO: Use a display list */
493 /* Disable shaders */
494 This
->shader_backend
->shader_cleanup((IWineD3DDevice
*) This
);
495 Context_MarkStateDirty(context
, STATE_VSHADER
);
496 Context_MarkStateDirty(context
, STATE_PIXELSHADER
);
498 /* Disable all textures. The caller can then bind a texture it wants to blit
501 if(GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
502 glDisable(GL_REGISTER_COMBINERS_NV
);
503 checkGLcall("glDisable(GL_REGISTER_COMBINERS_NV)");
505 if (GL_SUPPORT(ARB_MULTITEXTURE
)) {
506 /* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
507 * function texture unit. No need to care for higher samplers
509 for(i
= GL_LIMITS(textures
) - 1; i
> 0 ; i
--) {
510 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
511 checkGLcall("glActiveTextureARB");
513 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
514 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
515 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
517 glDisable(GL_TEXTURE_3D
);
518 checkGLcall("glDisable GL_TEXTURE_3D");
519 glDisable(GL_TEXTURE_2D
);
520 checkGLcall("glDisable GL_TEXTURE_2D");
522 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
523 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
525 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(i
, WINED3DTSS_COLOROP
));
526 Context_MarkStateDirty(context
, STATE_SAMPLER(i
));
528 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
529 checkGLcall("glActiveTextureARB");
531 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP
)) {
532 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
533 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
535 glDisable(GL_TEXTURE_3D
);
536 checkGLcall("glDisable GL_TEXTURE_3D");
537 glEnable(GL_TEXTURE_2D
);
538 checkGLcall("glEnable GL_TEXTURE_2D");
540 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
542 glMatrixMode(GL_TEXTURE
);
543 checkGLcall("glMatrixMode(GL_TEXTURE)");
545 checkGLcall("glLoadIdentity()");
546 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
));
548 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS
)) {
549 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
550 GL_TEXTURE_LOD_BIAS_EXT
,
552 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
554 Context_MarkStateDirty(context
, STATE_SAMPLER(0));
555 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(0, WINED3DTSS_COLOROP
));
557 /* Other misc states */
558 glDisable(GL_ALPHA_TEST
);
559 checkGLcall("glDisable(GL_ALPHA_TEST)");
560 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
));
561 glDisable(GL_LIGHTING
);
562 checkGLcall("glDisable GL_LIGHTING");
563 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
));
564 glDisable(GL_DEPTH_TEST
);
565 checkGLcall("glDisable GL_DEPTH_TEST");
566 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
));
568 checkGLcall("glDisable GL_FOG");
569 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
));
571 checkGLcall("glDisable GL_BLEND");
572 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
));
573 glDisable(GL_CULL_FACE
);
574 checkGLcall("glDisable GL_CULL_FACE");
575 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
));
576 glDisable(GL_STENCIL_TEST
);
577 checkGLcall("glDisable GL_STENCIL_TEST");
578 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
));
579 if(GL_SUPPORT(ARB_POINT_SPRITE
)) {
580 glDisable(GL_POINT_SPRITE_ARB
);
581 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
582 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
));
584 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
585 checkGLcall("glColorMask");
586 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
));
588 /* Setup transforms */
589 glMatrixMode(GL_MODELVIEW
);
590 checkGLcall("glMatrixMode(GL_MODELVIEW)");
592 checkGLcall("glLoadIdentity()");
593 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)));
595 glMatrixMode(GL_PROJECTION
);
596 checkGLcall("glMatrixMode(GL_PROJECTION)");
598 checkGLcall("glLoadIdentity()");
599 glOrtho(0, width
, height
, 0, 0.0, -1.0);
600 checkGLcall("glOrtho");
601 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
));
603 context
->last_was_rhw
= TRUE
;
604 Context_MarkStateDirty(context
, STATE_VDECL
); /* because of last_was_rhw = TRUE */
606 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
607 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
608 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
609 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
610 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
611 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
612 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
));
614 glViewport(0, 0, width
, height
);
615 checkGLcall("glViewport");
616 Context_MarkStateDirty(context
, STATE_VIEWPORT
);
618 if(GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
619 glDisable(GL_TEXTURE_SHADER_NV
);
620 checkGLcall("glDisable(GL_TEXTURE_SHADER_NV)");
624 /*****************************************************************************
627 * Finds a rendering context and drawable matching the device and render
628 * target for the current thread, activates them and puts them into the
632 * This: Device to activate the context for
633 * target: Requested render target
634 * usage: Prepares the context for blitting, drawing or other actions
636 *****************************************************************************/
637 void ActivateContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
, ContextUsage usage
) {
638 DWORD tid
= This
->createParms
.BehaviorFlags
& WINED3DCREATE_MULTITHREADED
? GetCurrentThreadId() : 0;
640 DWORD dirtyState
, idx
;
642 WineD3DContext
*context
= This
->activeContext
;
643 BOOL oldRenderOffscreen
= This
->render_offscreen
;
645 TRACE("(%p): Selecting context for render target %p, thread %d\n", This
, target
, tid
);
647 if(This
->lastActiveRenderTarget
!= target
) {
648 IWineD3DSwapChain
*swapchain
= NULL
;
650 BOOL readTexture
= wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
&& This
->render_offscreen
;
652 hr
= IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **) &swapchain
);
653 if(hr
== WINED3D_OK
&& swapchain
) {
654 TRACE("Rendering onscreen\n");
655 context
= ((IWineD3DSwapChainImpl
*) swapchain
)->context
[0];
656 This
->render_offscreen
= FALSE
;
657 /* The context != This->activeContext will catch a NOP context change. This can occur
658 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
659 * rendering. No context change is needed in that case
662 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
) {
663 if(((IWineD3DSwapChainImpl
*) swapchain
)->backBuffer
) {
664 glDrawBuffer(GL_BACK
);
665 checkGLcall("glDrawBuffer(GL_BACK)");
667 glDrawBuffer(GL_FRONT
);
668 checkGLcall("glDrawBuffer(GL_FRONT)");
671 IWineD3DSwapChain_Release(swapchain
);
673 if(oldRenderOffscreen
) {
674 Context_MarkStateDirty(context
, WINED3DRS_CULLMODE
);
675 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
);
676 Context_MarkStateDirty(context
, STATE_VDECL
);
677 Context_MarkStateDirty(context
, STATE_VIEWPORT
);
680 TRACE("Rendering offscreen\n");
681 This
->render_offscreen
= TRUE
;
683 switch(wined3d_settings
.offscreen_rendering_mode
) {
685 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
686 if(This
->activeContext
) {
687 context
= This
->activeContext
;
689 /* This may happen if the app jumps streight into offscreen rendering
690 * Start using the context of the primary swapchain
692 context
= ((IWineD3DSwapChainImpl
*) This
->swapchains
[0])->context
[0];
698 IWineD3DSurfaceImpl
*targetimpl
= (IWineD3DSurfaceImpl
*) target
;
699 if(This
->pbufferContext
== NULL
||
700 This
->pbufferWidth
< targetimpl
->currentDesc
.Width
||
701 This
->pbufferHeight
< targetimpl
->currentDesc
.Height
) {
702 if(This
->pbufferContext
) {
703 DestroyContext(This
, This
->pbufferContext
);
706 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
707 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
709 This
->pbufferContext
= CreateContext(This
, targetimpl
,
710 ((IWineD3DSwapChainImpl
*) This
->swapchains
[0])->context
[0]->display
,
712 This
->pbufferWidth
= targetimpl
->currentDesc
.Width
;
713 This
->pbufferHeight
= targetimpl
->currentDesc
.Height
;
716 if(This
->pbufferContext
) {
717 context
= This
->pbufferContext
;
720 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
721 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
726 /* Stay with the currently active context for back buffer rendering */
727 if(This
->activeContext
) {
728 context
= This
->activeContext
;
730 /* This may happen if the app jumps streight into offscreen rendering
731 * Start using the context of the primary swapchain
733 context
= ((IWineD3DSwapChainImpl
*) This
->swapchains
[0])->context
[0];
735 glDrawBuffer(This
->offscreenBuffer
);
736 checkGLcall("glDrawBuffer(This->offscreenBuffer)");
740 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) {
741 /* Make sure we have a OpenGL texture name so the PreLoad() used to read the buffer
742 * back when we are done won't mark us dirty.
744 IWineD3DSurface_PreLoad(target
);
747 if(!oldRenderOffscreen
) {
748 Context_MarkStateDirty(context
, WINED3DRS_CULLMODE
);
749 Context_MarkStateDirty(context
, WINED3DTS_PROJECTION
);
750 Context_MarkStateDirty(context
, STATE_VDECL
);
751 Context_MarkStateDirty(context
, STATE_VIEWPORT
);
755 BOOL oldInDraw
= This
->isInDraw
;
757 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
758 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
759 * when using offscreen rendering with multithreading
761 This
->isInDraw
= TRUE
;
763 /* Do that before switching the context:
764 * Read the back buffer of the old drawable into the destination texture
766 IWineD3DSurface_PreLoad(This
->lastActiveRenderTarget
);
768 /* Assume that the drawable will be modified by some other things now */
769 ((IWineD3DSurfaceImpl
*) This
->lastActiveRenderTarget
)->Flags
&= ~SFLAG_INDRAWABLE
;
771 This
->isInDraw
= oldInDraw
;
773 This
->lastActiveRenderTarget
= target
;
774 if(oldRenderOffscreen
!= This
->render_offscreen
&& This
->depth_copy_state
!= WINED3D_DCS_NO_COPY
) {
775 This
->depth_copy_state
= WINED3D_DCS_COPY
;
778 /* Stick to the old context */
779 context
= This
->activeContext
;
782 /* Activate the opengl context */
783 if(context
!= This
->activeContext
) {
785 TRACE("Switching gl ctx to %p, drawable=%ld, ctx=%p\n", context
, context
->drawable
, context
->glCtx
);
786 ret
= glXMakeCurrent(context
->display
, context
->drawable
, context
->glCtx
);
788 ERR("Failed to activate the new context\n");
790 This
->activeContext
= context
;
794 case CTXUSAGE_RESOURCELOAD
:
795 /* This does not require any special states to be set up */
798 case CTXUSAGE_DRAWPRIM
:
799 /* This needs all dirty states applied */
800 if(context
->last_was_blit
&& GL_SUPPORT(NV_TEXTURE_SHADER2
)) {
801 glEnable(GL_TEXTURE_SHADER_NV
);
802 checkGLcall("glEnable(GL_TEXTURE_SHADER_NV)");
804 if (GL_SUPPORT(NV_REGISTER_COMBINERS
)) {
805 IWineD3DDeviceImpl_FindTexUnitMap(This
);
807 for(i
=0; i
< context
->numDirtyEntries
; i
++) {
808 dirtyState
= context
->dirtyArray
[i
];
809 idx
= dirtyState
>> 5;
810 shift
= dirtyState
& 0x1f;
811 context
->isStateDirty
[idx
] &= ~(1 << shift
);
812 StateTable
[dirtyState
].apply(dirtyState
, This
->stateBlock
, context
);
814 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
815 context
->last_was_blit
= FALSE
;
819 SetupForBlit(This
, context
,
820 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Width
,
821 ((IWineD3DSurfaceImpl
*)target
)->currentDesc
.Height
);
825 FIXME("Unexpected context usage requested\n");