context.c revision 114410893548b9522c46fdcbd8f63385eb8bfb68
/*
* Context and render target management in wined3d
*
* Copyright 2007-2008 Stefan Dösinger for CodeWeavers
* Copyright 2009 Henri Verbeet for CodeWeavers
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Sun elects to use only
* the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
* a choice of LGPL license versions is made available with the language indicating
* that LGPLv2 or any later version may be used, or where a choice of which version
* of the LGPL is applied is otherwise unspecified.
*/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_FLOAT_H
# include <float.h>
#endif
#include "wined3d_private.h"
#define GLINFO_LOCATION (*gl_info)
static DWORD wined3d_context_tls_idx;
/* FBO helper functions */
/* GL locking is done by the caller */
{
GLuint f;
if (!fbo)
{
f = 0;
}
else
{
if (!*fbo)
{
checkGLcall("glGenFramebuffersEXT()");
}
f = *fbo;
}
switch (target)
{
case GL_READ_FRAMEBUFFER_EXT:
if (context->fbo_read_binding == f) return;
context->fbo_read_binding = f;
break;
case GL_DRAW_FRAMEBUFFER_EXT:
if (context->fbo_draw_binding == f) return;
context->fbo_draw_binding = f;
break;
case GL_FRAMEBUFFER_EXT:
if (context->fbo_read_binding == f
&& context->fbo_draw_binding == f) return;
context->fbo_read_binding = f;
context->fbo_draw_binding = f;
break;
default:
break;
}
checkGLcall("glBindFramebuffer()");
}
/* GL locking is done by the caller */
{
unsigned int i;
{
GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, 0, 0));
checkGLcall("glFramebufferTexture2D()");
}
GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
checkGLcall("glFramebufferTexture2D()");
GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
checkGLcall("glFramebufferTexture2D()");
}
/* GL locking is done by the caller */
{
checkGLcall("glDeleteFramebuffers()");
}
/* GL locking is done by the caller */
{
/* Update base texture states array */
if (SUCCEEDED(IWineD3DSurface_GetContainer(surface, &IID_IWineD3DBaseTexture, (void **)&texture_impl)))
{
{
}
{
}
{
WARN("Render targets should not be bound to a sampler\n");
}
}
{
if (target == GL_TEXTURE_2D)
{
} else if (target == GL_TEXTURE_RECTANGLE_ARB) {
} else {
}
}
checkGLcall("apply_attachment_filter_states()");
}
/* GL locking is done by the caller */
{
if (depth_stencil)
{
{
{
checkGLcall("glFramebufferRenderbufferEXT()");
}
{
checkGLcall("glFramebufferRenderbufferEXT()");
}
}
else
{
{
checkGLcall("glFramebufferTexture2DEXT()");
}
{
checkGLcall("glFramebufferTexture2DEXT()");
}
}
if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
{
checkGLcall("glFramebufferTexture2DEXT()");
}
if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
{
checkGLcall("glFramebufferTexture2DEXT()");
}
}
else
{
checkGLcall("glFramebufferTexture2DEXT()");
checkGLcall("glFramebufferTexture2DEXT()");
}
}
/* GL locking is done by the caller */
{
if (surface)
{
GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_COLOR_ATTACHMENT0_EXT + idx, surface_impl->texture_target,
checkGLcall("glFramebufferTexture2DEXT()");
} else {
GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_COLOR_ATTACHMENT0_EXT + idx, GL_TEXTURE_2D, 0, 0));
checkGLcall("glFramebufferTexture2DEXT()");
}
}
/* GL locking is done by the caller */
{
if (status == GL_FRAMEBUFFER_COMPLETE_EXT)
{
TRACE("FBO complete\n");
} else {
unsigned int i;
if (context->current_fbo)
{
/* Dump the FBO attachments */
{
if (attachment)
{
FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
}
}
if (attachment)
{
FIXME("\tDepth attachment: (%p) %s %ux%u\n",
}
}
}
}
{
entry->render_targets = HeapAlloc(GetProcessHeap(), 0, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
memcpy(entry->render_targets, device->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
return entry;
}
/* GL locking is done by the caller */
{
memcpy(entry->render_targets, device->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
}
/* GL locking is done by the caller */
{
{
}
}
/* GL locking is done by the caller */
{
{
if (!memcmp(entry->render_targets, device->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets))
{
return entry;
}
}
{
}
else
{
}
return entry;
}
/* GL locking is done by the caller */
{
unsigned int i;
{
/* Apply render targets */
{
}
/* Apply depth targets */
if (device->stencilBufferTarget)
{
}
} else {
{
if (device->render_targets[i])
}
if (device->stencilBufferTarget)
}
{
if (device->render_targets[i])
else
}
}
/* GL locking is done by the caller */
{
if (context->render_offscreen)
{
} else {
}
}
/* Context activation is done by the caller. */
void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
{
{
}
else
{
{
ENTER_GL();
checkGLcall("glGenQueriesARB");
LEAVE_GL();
}
else
{
WARN("Occlusion queries not supported, not allocating query id.\n");
}
}
}
{
{
if (!new_data)
{
return;
}
}
}
/* Context activation is done by the caller. */
{
{
}
else
{
if (GL_SUPPORT(APPLE_FENCE))
{
ENTER_GL();
checkGLcall("glGenFencesAPPLE");
LEAVE_GL();
}
else if(GL_SUPPORT(NV_FENCE))
{
ENTER_GL();
checkGLcall("glGenFencesNV");
LEAVE_GL();
}
else
{
WARN("Event queries not supported, not allocating query id.\n");
}
}
}
{
{
if (!new_data)
{
return;
}
}
}
void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type)
{
UINT i;
if (!This->d3d_initialized) return;
switch(type)
{
case WINED3DRTYPE_SURFACE:
{
for (i = 0; i < This->numContexts; ++i)
{
ENTER_GL();
{
UINT j;
{
{
}
}
}
LEAVE_GL();
}
break;
}
default:
break;
}
}
{
struct wined3d_occlusion_query *occlusion_query;
struct wined3d_event_query *event_query;
ENTER_GL();
LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
{
if (has_glctx && GL_SUPPORT(ARB_OCCLUSION_QUERY)) GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
}
{
if (has_glctx)
{
}
}
}
if (has_glctx)
{
{
}
{
}
if (context->dummy_arbfp_prog)
{
}
GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
if (GL_SUPPORT(APPLE_FENCE))
else if (GL_SUPPORT(NV_FENCE))
checkGLcall("context cleanup");
}
LEAVE_GL();
{
ERR("Failed to disable GL context.\n");
}
{
}
else
{
}
{
}
}
DWORD context_get_tls_idx(void)
{
return wined3d_context_tls_idx;
}
{
}
struct wined3d_context *context_get_current(void)
{
return TlsGetValue(wined3d_context_tls_idx);
}
{
{
return TRUE;
}
if (old)
{
{
}
else
{
}
}
if (ctx)
{
TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
{
return FALSE;
}
}
else
{
TRACE("Clearing current D3D context.\n");
{
ERR("Failed to clear current GL context.\n");
return FALSE;
}
}
}
/*****************************************************************************
* Context_MarkStateDirty
*
* Marks a state in a context dirty. Only one context, opposed to
* IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
* contexts
*
* Params:
* context: Context to mark the state dirty in
* state: State to mark dirty
* StateTable: Pointer to the state table in use(for state grouping)
*
*****************************************************************************/
static void Context_MarkStateDirty(struct wined3d_context *context, DWORD state, const struct StateEntry *StateTable)
{
}
/*****************************************************************************
* AddContextToArray
*
* Adds a context to the context array. Helper function for CreateContext
*
* This method is not called in performance-critical code paths, only when a
* new render target or swapchain is created. Thus performance is not an issue
* here.
*
* Params:
* This: Device to add the context for
* hdc: device context
* glCtx: WGL context to add
* pbuffer: optional pbuffer used with this context
*
*****************************************************************************/
{
ERR("Unable to grow the context array\n");
return NULL;
}
if(oldArray) {
}
This->contexts[This->numContexts] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**This->contexts));
ERR("Unable to allocate a new context\n");
return NULL;
}
/* Mark all states dirty to force a proper initialization of the states on the first use of the context
*/
}
This->numContexts++;
}
/* This function takes care of WineD3D pixel format selection. */
{
int iPixelFormat=0;
unsigned int matchtry;
short depthBits=0, stencilBits=0;
struct match_type {
} matches[] = {
/* First, try without alpha match buffers. MacOS supports aux buffers only
* on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
* Then try without aux buffers - this is the most common cause for not
* finding a pixel format. Also some drivers(the open source ones)
* only offer 32 bit ARB pixel formats. First try without an exact alpha
* match, then try without an exact alpha and color match.
*/
};
int i = 0;
TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, pbuffer=%d, findCompatible=%d\n",
{
ERR("Unable to get color bits for format %s (%#x)!\n",
return 0;
}
/* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
* You are able to add a depth + stencil surface at a later stage when you need it.
* In order to support this properly in WineD3D we need the ability to recreate the opengl context and
* drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
* context, need torecreate shaders, textures and other resources.
*
* The context manager already takes care of the state problem and for the other tasks code from Reset
* can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
* Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
* time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
* issue needs to be fixed. */
{
FIXME("Add OpenGL context recreation support to SetDepthStencilSurface\n");
}
for(i=0; i<nCfgs; i++) {
/* For now only accept RGBA formats. Perhaps some day we will
* allow floating point formats for pbuffers. */
continue;
/* In window mode (!pbuffer) we need a window drawable format and double buffering. */
continue;
/* We like to have aux buffers in backbuffer mode */
continue;
/* In pbuffer-mode we need a pbuffer-capable format but we don't want double buffering */
continue;
continue;
continue;
continue;
} else {
continue;
continue;
continue;
}
continue;
} else {
continue;
}
/* We try to locate a format which matches our requirements exactly. In case of
* depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
continue;
/* In all cases make sure the number of stencil bits matches our requirements
* even when we don't need stencil because it could affect performance EXCEPT
* on cards which don't offer depth formats without stencil like the i915 drivers
* on Linux. */
if(stencilBits != cfg->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfg->stencilSize))
continue;
/* Check multisampling support */
continue;
/* When we have passed all the checks then we have found a format which matches our
* requirements. Note that we only check for a limit number of capabilities right now,
* so there can easily be a dozen of pixel formats which appear to be the 'same' but
* can still differ in things like multisampling, stereo, SRGB and other flags.
*/
/* Exit the loop as we have found a format :) */
if(exactDepthMatch) {
break;
} else if(!iPixelFormat) {
/* In the end we might end up with a format which doesn't exactly match our depth
* requirements. Accept the first format we found because formats with higher iPixelFormat
* values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
}
}
}
/* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
if(!iPixelFormat && !findCompatible) {
ERR("Can't find a suitable iPixelFormat\n");
return FALSE;
} else if(!iPixelFormat) {
TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
/* PixelFormat selection */
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
if(!iPixelFormat) {
/* If this happens something is very wrong as ChoosePixelFormat barely fails */
ERR("Can't find a suitable iPixelFormat\n");
return FALSE;
}
}
TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
return iPixelFormat;
}
/*****************************************************************************
* CreateContext
*
* Creates a new context for a window, or a pbuffer context.
*
* * Params:
* This: Device to activate the context for
* target: Surface this context will render to
* win_handle: handle to the window which we are drawing to
* create_pbuffer: tells whether to create a pbuffer or not
* pPresentParameters: contains the pixelformats to use for onscreen rendering
*
*****************************************************************************/
{
unsigned int s;
TRACE("(%p): Creating a %s context for render target %p\n", This, create_pbuffer ? "offscreen" : "onscreen", target);
if(create_pbuffer) {
int iPixelFormat = 0;
/* Try to find a pixel format with pbuffer support. */
FALSE /* findCompatible */);
if(!iPixelFormat) {
TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
/* For some reason we weren't able to find a format, try to find something instead of crashing.
* A reason for failure could have been wglChoosePixelFormatARB strictness. */
TRUE /* findCompatible */);
}
/* This shouldn't happen as ChoosePixelFormat always returns something */
if(!iPixelFormat) {
ERR("Unable to locate a pixel format for a pbuffer\n");
goto out;
}
TRACE("Creating a pBuffer drawable for the new context\n");
pbuffer = GL_EXTCALL(wglCreatePbufferARB(hdc_parent, iPixelFormat, target->currentDesc.Width, target->currentDesc.Height, 0));
if(!pbuffer) {
ERR("Cannot create a pbuffer\n");
goto out;
}
/* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
if(!hdc) {
goto out;
}
} else {
int iPixelFormat;
int res;
int numSamples = 0;
ERR("Cannot retrieve a device context!\n");
goto out;
}
/* In case of ORM_BACKBUFFER, make sure to request an alpha component for X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
auxBuffers = TRUE;
}
/* DirectDraw supports 8bit paletted render targets and these are used by old games like Starcraft and C&C.
* Most modern hardware doesn't support 8bit natively so we perform some form of 8bit -> 32bit conversion.
* The conversion (ab)uses the alpha component for storing the palette index. For this reason we require
* a format with 8bit alpha, so request A8R8G8B8. */
/* Retrieve the depth stencil format from the present parameters.
* The choice of the proper format can give a nice performance boost
* in case of GPU limited programs. */
TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms->AutoDepthStencilFormat));
ds_format_desc = getFormatDescEntry(pPresentParms->AutoDepthStencilFormat, &This->adapter->gl_info);
}
/* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD */
if(!GL_SUPPORT(ARB_MULTISAMPLE))
ERR("The program is requesting multisampling without support!\n");
else {
}
}
/* Try to find a pixel format which matches our requirements */
/* Try to locate a compatible format if we weren't able to find anything */
if(!iPixelFormat) {
TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
}
/* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
if(!iPixelFormat) {
ERR("Can't find a suitable iPixelFormat\n");
return NULL;
}
if(!res) {
/* By default WGL doesn't allow pixel format adjustments but we need it here.
* For this reason there is a WINE-specific wglSetPixelFormat which allows you to
* set the pixel format multiple times. Only use it when it is really needed. */
if(oldPixelFormat == iPixelFormat) {
/* We don't have to do anything as the formats are the same :) */
if(!res) {
return NULL;
}
} else if(oldPixelFormat) {
/* OpenGL doesn't allow pixel format adjustments. Print an error and continue using the old format.
* There's a big chance that the old format works although with a performance hit and perhaps rendering errors. */
ERR("HDC=%p is already set to iPixelFormat=%d and OpenGL doesn't allow changes!\n", hdc, oldPixelFormat);
} else {
return NULL;
}
}
}
if (This->numContexts)
{
{
ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
}
}
if(!ctx) {
ERR("Failed to create a WGL context\n");
if(create_pbuffer) {
}
goto out;
}
if(!ret) {
ERR("Failed to add the newly created context to the context list\n");
if (!pwglDeleteContext(ctx))
{
}
if(create_pbuffer) {
}
goto out;
}
/* Create the dirty constants array and initialize them to dirty */
}
/* Set up the context defaults */
if (!context_set_current(ret))
{
ERR("Cannot activate context to set up defaults\n");
goto out;
}
ENTER_GL();
TRACE("Setting up the screen\n");
/* Clear the screen */
checkGLcall("glClearColor");
glClearIndex(0);
glClearDepth(1);
glClearStencil(0xffff);
checkGLcall("glClear");
checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
/* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
* and textures in DIB sections(due to the memory protection).
*/
checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
}
if(GL_SUPPORT(ARB_VERTEX_BLEND)) {
/* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
* this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
* GL_VERTEX_BLEND_ARB isn't enabled too
*/
checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
}
if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
/* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
* the previous texture where to source the offset from is always unit - 1.
*/
checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
}
}
if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
/* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
* enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
* GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
* because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
* is ever assigned.
*
* So make sure a program is assigned to each context. The first real ARBFP use will set a different
* program and the dummy program is destroyed when the context is destroyed.
*/
const char *dummy_program =
"!!ARBfp1.0\n"
"MOV result.color, fragment.color.primary;\n"
"END\n";
GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
}
for(s = 0; s < GL_LIMITS(point_sprite_units); s++) {
checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
}
LEAVE_GL();
return ret;
out:
if (ret)
{
}
return NULL;
}
/*****************************************************************************
* RemoveContextFromArray
*
* Removes a context from the context manager. The opengl context is not
* destroyed or unset. context is not a valid pointer after that call.
*
* Similar to the former call this isn't a performance critical function. A
* helper function for DestroyContext.
*
* Params:
* This: Device to activate the context for
* context: Context to remove
*
*****************************************************************************/
{
struct wined3d_context **new_array;
UINT i;
for (i = 0; i < This->numContexts; ++i)
{
{
break;
}
}
if (!found)
{
return;
}
{
++i;
}
--This->numContexts;
if (!This->numContexts)
{
return;
}
new_array = HeapReAlloc(GetProcessHeap(), 0, This->contexts, This->numContexts * sizeof(*This->contexts));
if (!new_array)
{
ERR("Failed to shrink context array. Oh well.\n");
return;
}
}
/*****************************************************************************
* DestroyContext
*
* Destroys a wineD3DContext
*
* Params:
* This: Device to activate the context for
* context: Context to destroy
*
*****************************************************************************/
{
{
if (!context_set_current(NULL))
{
ERR("Failed to clear current D3D context.\n");
}
}
else
{
}
}
/* GL locking is done by the caller */
checkGLcall("glMatrixMode(GL_PROJECTION)");
checkGLcall("glLoadIdentity()");
checkGLcall("glOrtho");
checkGLcall("glViewport");
}
/*****************************************************************************
* SetupForBlit
*
* Sets up a context for DirectDraw blitting.
* All texture units are disabled, texture unit 0 is set as current unit
* fog, lighting, blending, alpha test, z test, scissor test, culling disabled
* color writing enabled for all channels
* register combiners disabled, shaders disabled
* world matrix is set to identity, texture matrix 0 too
* projection matrix is setup for drawing screen coordinates
*
* Params:
* This: Device to activate the context for
* context: Context to setup
* width: render target width
* height: render target height
*
*****************************************************************************/
/* Context activation is done by the caller. */
static inline void SetupForBlit(IWineD3DDeviceImpl *This, struct wined3d_context *context, UINT width, UINT height)
{
int i, sampler;
if(context->last_was_blit) {
ENTER_GL();
LEAVE_GL();
/* No need to dirtify here, the states are still dirtified because they weren't
* applied since the last SetupForBlit call. Otherwise last_was_blit would not
* be set
*/
}
TRACE("Context is already set up for blitting, nothing to do\n");
return;
}
/* TODO: Use a display list */
/* Disable shaders */
ENTER_GL();
LEAVE_GL();
/* Call ENTER_GL() once for all gl calls below. In theory we should not call
* helper functions in between gl calls. This function is full of Context_MarkStateDirty
* after each GL call.
*/
ENTER_GL();
/* Disable all textures. The caller can then bind a texture it wants to blit
* from
*
* The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
* function texture unit. No need to care for higher samplers
*/
checkGLcall("glActiveTextureARB");
if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
}
checkGLcall("glDisable GL_TEXTURE_3D");
if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
}
checkGLcall("glDisable GL_TEXTURE_2D");
checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
if (sampler != -1) {
if (sampler < MAX_TEXTURES) {
}
}
}
checkGLcall("glActiveTextureARB");
if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
}
checkGLcall("glDisable GL_TEXTURE_3D");
if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
}
checkGLcall("glDisable GL_TEXTURE_2D");
checkGLcall("glMatrixMode(GL_TEXTURE)");
checkGLcall("glLoadIdentity()");
if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS)) {
0.0f);
checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
}
if (sampler != -1) {
if (sampler < MAX_TEXTURES) {
}
}
/* Other misc states */
checkGLcall("glDisable(GL_ALPHA_TEST)");
checkGLcall("glDisable GL_LIGHTING");
checkGLcall("glDisable GL_DEPTH_TEST");
checkGLcall("glDisable GL_FOG");
checkGLcall("glDisable GL_BLEND");
checkGLcall("glDisable GL_CULL_FACE");
checkGLcall("glDisable GL_STENCIL_TEST");
checkGLcall("glDisable GL_SCISSOR_TEST");
if(GL_SUPPORT(ARB_POINT_SPRITE)) {
checkGLcall("glDisable GL_POINT_SPRITE_ARB");
}
checkGLcall("glColorMask");
if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
}
/* Setup transforms */
checkGLcall("glMatrixMode(GL_MODELVIEW)");
checkGLcall("glLoadIdentity()");
LEAVE_GL();
}
/*****************************************************************************
* findThreadContextForSwapChain
*
* Searches a swapchain for all contexts and picks one for the thread tid.
* If none can be found the swapchain is requested to create a new context
*
*****************************************************************************/
static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid)
{
unsigned int i;
}
}
/* Create a new context for the thread */
}
/*****************************************************************************
* FindContext
*
* Finds a context for the current render target and thread
*
* Parameters:
* target: Render target to find the context for
* tid: Thread to activate the context for
*
* Returns: The needed context
*
*****************************************************************************/
static inline struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, DWORD tid)
{
struct wined3d_context *context;
if (!target)
{
if (current_context
{
}
else
{
}
}
{
return current_context;
}
TRACE("Rendering onscreen\n");
/* The context != This->activeContext will catch a NOP context change. This can occur
* if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
* rendering. No context change is needed in that case
*/
}
}
}
else
{
TRACE("Rendering offscreen\n");
{
if (!This->pbufferContext
{
/* The display is irrelevant here, the window is 0. But
* CreateContext needs a valid X connection. Create the context
* on the same server as the primary swapchain. The primary
* swapchain is exists at this point. */
}
if (This->pbufferContext)
{
{
FIXME("The PBuffer context is only supported for one thread for now!\n");
}
}
else
{
ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering.\n");
goto retry;
}
}
else
{
/* Stay with the currently active context. */
if (current_context
{
}
else
{
/* This may happen if the app jumps straight into offscreen rendering
* Start using the context of the primary swapchain. tid == 0 is no problem
* for findThreadContextForSwapChain.
*
* Can also happen on thread switches - in that case findThreadContextForSwapChain
* is perfect to call. */
}
}
}
{
}
/* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
* the alpha blend state changes with different render target formats. */
if (!context->current_rt)
{
}
else
{
const struct GlPixelFormatDesc *old = ((IWineD3DSurfaceImpl *)context->current_rt)->resource.format_desc;
{
/* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
{
}
}
/* When switching away from an offscreen render target, and we're not
* using FBOs, we have to read the drawable into the texture. This is
* done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
* are some things that need care though. PreLoad needs a GL context,
* and FindContext is called before the context is activated. It also
* has to be called with the old rendertarget active, otherwise a
* wrong drawable is read. */
{
/* surface_internal_preload() requires a context to load the
* texture, so it will call ActivateContext. Set isInDraw to true
* to signal surface_internal_preload() that it has a context. */
/* FIXME: This is just broken. There's no guarantee whatsoever
* that the currently active context, if any, is appropriate for
* reading back the render target. We should probably call
* context_set_current(context) here and then rely on
* ActivateContext() doing the right thing. */
/* Read the back buffer of the old drawable into the destination texture. */
{
}
else
{
}
}
}
return context;
}
/* Context activation is done by the caller. */
{
{
ENTER_GL();
checkGLcall("glDrawBuffers()");
LEAVE_GL();
}
else
{
ENTER_GL();
{
if (!blit)
{
if (GL_SUPPORT(ARB_DRAW_BUFFERS))
{
checkGLcall("glDrawBuffers()");
}
else
{
checkGLcall("glDrawBuffer()");
}
} else {
checkGLcall("glDrawBuffer()");
}
}
else
{
checkGLcall("glDrawBuffer()");
}
LEAVE_GL();
}
}
/*****************************************************************************
* ActivateContext
*
* Finds a rendering context and drawable matching the device and render
* target for the current thread, activates them and puts them into the
* requested state.
*
* Params:
* This: Device to activate the context for
* target: Requested render target
* usage: Prepares the context for blitting, drawing or other actions
*
*****************************************************************************/
struct wined3d_context *ActivateContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, enum ContextUsage usage)
{
const struct wined3d_gl_info *gl_info;
struct wined3d_context *context;
/* Activate the opengl context */
if (context != current_context)
{
if (context->vshader_const_dirty)
{
}
if (context->pshader_const_dirty)
{
}
}
switch (usage) {
case CTXUSAGE_CLEAR:
case CTXUSAGE_DRAWPRIM:
ENTER_GL();
LEAVE_GL();
}
if (context->draw_buffer_dirty) {
}
break;
case CTXUSAGE_BLIT:
if (context->render_offscreen)
{
FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
ENTER_GL();
LEAVE_GL();
} else {
ENTER_GL();
LEAVE_GL();
}
}
if (context->draw_buffer_dirty) {
}
}
break;
default:
break;
}
switch(usage) {
case CTXUSAGE_RESOURCELOAD:
/* This does not require any special states to be set up */
break;
case CTXUSAGE_CLEAR:
if(context->last_was_blit) {
}
/* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
* blending when clearing improves the clearing performance incredibly.
*/
ENTER_GL();
LEAVE_GL();
ENTER_GL();
checkGLcall("glEnable GL_SCISSOR_TEST");
LEAVE_GL();
break;
case CTXUSAGE_DRAWPRIM:
/* This needs all dirty states applied */
if(context->last_was_blit) {
}
ENTER_GL();
for(i=0; i < context->numDirtyEntries; i++) {
}
LEAVE_GL();
break;
case CTXUSAGE_BLIT:
break;
default:
FIXME("Unexpected context usage requested\n");
}
return context;
}