context.c revision 30438b06a7a54cb1e30907e480fc66561bd7ea4b
/*
* 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
*/
/*
* Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
* other than GPL or LGPL is available it will apply instead, Oracle 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;
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
# define vboxGetCurrentContext() VBoxTlsRefGetCurrent(struct wined3d_context, wined3d_context_tls_idx)
# define vboxSetCurrentContext(_ctx) VBoxTlsRefSetCurrent(struct wined3d_context, wined3d_context_tls_idx, (_ctx))
#endif
/* FBO helper functions */
/* GL locking is done by the caller */
{
GLuint f;
if (!fbo)
{
f = 0;
}
else
{
if (!*fbo)
{
checkGLcall("glGenFramebuffers()");
}
f = *fbo;
}
switch (target)
{
case GL_READ_FRAMEBUFFER:
if (context->fbo_read_binding == f) return;
context->fbo_read_binding = f;
break;
case GL_DRAW_FRAMEBUFFER:
if (context->fbo_draw_binding == f) return;
context->fbo_draw_binding = f;
break;
case GL_FRAMEBUFFER:
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_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
checkGLcall("glFramebufferTexture2D()");
}
checkGLcall("glFramebufferTexture2D()");
gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, 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 */
&IID_IWineD3DBaseTexture, (void **)&texture_impl)))
{
{
}
{
}
{
WARN("Render targets should not be bound to a sampler\n");
}
if (update_minfilter || update_magfilter)
{
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("glFramebufferRenderbuffer()");
}
{
checkGLcall("glFramebufferRenderbuffer()");
}
}
else
{
{
checkGLcall("glFramebufferTexture2D()");
}
{
checkGLcall("glFramebufferTexture2D()");
}
}
if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
{
checkGLcall("glFramebufferTexture2D()");
}
if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
{
checkGLcall("glFramebufferTexture2D()");
}
}
else
{
checkGLcall("glFramebufferTexture2D()");
checkGLcall("glFramebufferTexture2D()");
}
}
/* GL locking is done by the caller */
{
if (surface)
{
gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, surface->texture_target,
checkGLcall("glFramebufferTexture2D()");
}
else
{
gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, GL_TEXTURE_2D, 0, 0);
checkGLcall("glFramebufferTexture2D()");
}
}
/* GL locking is done by the caller */
{
if (status == GL_FRAMEBUFFER_COMPLETE)
{
TRACE("FBO complete\n");
} else {
unsigned int i;
if (!context->current_fbo)
{
ERR("FBO 0 is incomplete, driver bug?\n");
return;
}
/* 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_info->limits.buffers * sizeof(*entry->render_targets));
memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
return entry;
}
/* GL locking is done by the caller */
{
memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
}
/* GL locking is done by the caller */
{
{
}
}
/* GL locking is done by the caller */
{
{
{
return entry;
}
}
{
}
else
{
}
return entry;
}
/* GL locking is done by the caller */
{
unsigned int i;
{
/* Apply render targets */
{
}
/* Apply depth targets */
if (device->stencilBufferTarget)
{
}
context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, (IWineD3DSurfaceImpl *)device->stencilBufferTarget, TRUE);
}
else
{
{
if (device->render_targets[i])
}
if (device->stencilBufferTarget)
}
{
if (device->render_targets[i])
else
}
}
/* GL locking is done by the caller */
{
{
}
if (context->rebind_fbo)
{
}
if (context->render_offscreen)
{
} else {
}
#if defined(DEBUG) && !defined(DEBUG_misha)
#endif
}
/* 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
{
{
/* Using ARB_sync, not much to do here. */
}
{
ENTER_GL();
checkGLcall("glGenFencesAPPLE");
LEAVE_GL();
}
{
ENTER_GL();
checkGLcall("glGenFencesNV");
LEAVE_GL();
}
else
{
WARN("Event queries not supported, not allocating query id.\n");
}
}
}
{
{
union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
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)
{
{
UINT j;
{
continue;
}
{
{
break;
}
}
}
}
break;
}
default:
break;
}
}
{
unsigned int i;
{
{
return;
}
}
{
}
}
{
if (!current)
{
{
ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
return FALSE;
}
return TRUE;
}
/* By default WGL doesn't allow pixel format adjustments but we need it
* here. For this reason there's a Wine specific wglSetPixelFormat()
* which allows us to set the pixel format multiple times. Only use it
* when really needed. */
{
{
ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
return FALSE;
}
return TRUE;
}
/* 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("Unable to set pixel format %d on device context %p. Already using format %d.\n",
return TRUE;
}
#ifdef VBOX_WITH_WDDM
{
{
ERR("NULL hDC");
return FALSE;
}
#ifdef DEBUG
{
{
ERR("Unexpected swapchain for dc %p window expected %p, but was %p.\n", swapchain->hDC, swapchain->win_handle, hWnd);
}
}
#endif
return TRUE;
}
{
int i;
{
{
}
}
return NULL;
}
#endif
#ifdef VBOX_WITH_WDDM
#endif
)
{
#ifdef VBOX_WITH_WDDM
TRACE("Updating context %p swapchain from %p to %p.\n",
if (!swapchain_validate(swapchain))
{
goto err;
}
{
ERR("Failed to set pixel format %d on device context %p.\n",
goto err;
}
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
goto err;
}
#else
TRACE("Updating context %p window from %p to %p.\n",
{
{
ERR("Failed to release device context %p, last error %#x.\n",
}
}
{
goto err;
}
{
ERR("Failed to set pixel format %d on device context %p.\n",
goto err;
}
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
goto err;
}
#endif
return;
err:
}
#ifdef VBOX_WITH_WDDM
# ifdef DEBUG_misha
# endif
#endif
)
{
#ifdef VBOX_WITH_WDDM
if (!swapchain)
{
}
if (!swapchain)
{
# ifdef DEBUG_misha
if (fExpectedValid)
{
ERR("no current swapchain!\n");
}
# endif
return;
}
{
}
else
#else
{
WARN("DC %p belongs to window %p instead of %p., winEr(%d)\n",
}
#endif
{
#ifdef VBOX_WITH_WDDM
#endif
);
}
}
#ifdef VBOX_WITH_WDDM
{
# ifdef DEBUG_misha
, FALSE
# endif
);
return;
if (swapchain)
{
# ifdef DEBUG_misha
, TRUE
# endif
);
{
ERR("unexpected\n");
}
}
else
{
ERR("novalid swapchain found\n");
}
}
#endif
{
struct wined3d_occlusion_query *occlusion_query;
struct wined3d_event_query *event_query;
unsigned int i;
#ifdef VBOX_WITH_WDDM
if (context->valid && restore_ctx != context->glCtx) pwglMakeCurrent(context->currentSwapchain->hDC, context->glCtx);
else restore_ctx = NULL;
#else
else restore_ctx = NULL;
#endif
ENTER_GL();
LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
{
}
{
{
{
}
else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
}
}
{
}
{
}
{
{
}
{
}
if (context->dummy_arbfp_prog)
{
}
GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
{
}
{
for (i = 0; i < context->free_event_query_count; ++i)
{
}
}
{
for (i = 0; i < context->free_event_query_count; ++i)
{
}
}
checkGLcall("context cleanup");
}
LEAVE_GL();
if (restore_ctx)
{
{
ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
}
}
{
ERR("Failed to disable GL context.\n");
}
#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
# ifndef VBOX_WITH_WDDM
# else
# endif
#endif
{
}
}
DWORD context_get_tls_idx(void)
{
return wined3d_context_tls_idx;
}
{
}
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
{
{
/* this is a destroyed context left in the tls of the current thread */
/* 1. this releases the context and clears the tls */
/* return there is no context current */
return NULL;
}
if (!adjustTid)
return ctx;
return ctx;
if (context_set_current(ctx))
{
return ctx;
}
ERR("context_set_current failed\n");
return NULL;
}
#endif
struct wined3d_context *context_get_current(void)
{
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
return TlsGetValue(wined3d_context_tls_idx);
#else
return context_get_current_ex(tid);
#endif
}
{
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#else
#endif
{
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
{
}
else
#endif
{
return TRUE;
}
}
if (old)
{
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#else
{
}
else
{
}
#endif
}
if (ctx)
{
#ifdef VBOX_WITH_WDDM
TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->currentSwapchain->hDC);
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
return FALSE;
}
#else
TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
return FALSE;
}
#endif
}
else if(pwglGetCurrentContext())
{
TRACE("Clearing current D3D context.\n");
{
return FALSE;
}
}
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#else
if (ctx)
return TRUE;
#endif
}
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
{
/* In theory, we should do context_set_current(NULL) here,
* but since it may result in calling a context dtor, it should be done under wined3d lock.
* We can not acquire a wined3d lock here since this routine is called in a DllMain context
* and this would result in a lock order violation, which may result in a deadlock.
* In other words, wined3d may internally call Win32 API functions which result in
* a DLL lock acquisition while holding wined3d lock.
* So lock order should always be "wined3d lock" -> "dll lock".
*
* This is why we do the following:
* */
/* 1. get the current context w/o adjusting its thread id, etc. */
if (!old)
return;
/* there is a currently assigned context,
* 2. now increase its ref count to ensure its dtor routine is not called while making set_current(NULL).
* This is needed since dtor can only be run with a wined3d lock held */
/* 3. now we can call context_set_current(NULL) */
/* 4. to avoid possible deadlocks we make an asynchronous call to a worker thread to make
* wined3d lock - context release - wined3d unlock from there. */
}
#endif
{
{
else if (context != context_get_current())
}
{
TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
{
ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
}
else
{
/* success branch */
/* sync back our tls with gl settings */
{
#ifdef VBOX_WITH_WDDM
#else
#endif
UINT i = 0;
for (; i < device->numContexts; ++i)
{
{
break;
}
}
if (i == device->numContexts)
{
}
}
}
}
}
{
{
{
TRACE("Another GL context (%p on device context %p) is already current.\n",
}
}
}
/*****************************************************************************
* 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)
{
}
/* This function takes care of WineD3D pixel format selection. */
const struct wined3d_format_desc *color_format_desc, const struct wined3d_format_desc *ds_format_desc,
{
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, findCompatible=%d\n",
{
ERR("Unable to get color bits for format %s (%#x)!\n",
return 0;
}
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 we need a window drawable format and double buffering. */
continue;
/* We like to have aux buffers in backbuffer mode */
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) {
#ifdef DEBUG_misha
WARN("Can't find a suitable iPixelFormat\n");
#elif !defined(VBOX_WITH_WDDM)
ERR("Can't find a suitable iPixelFormat\n");
#endif
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;
}
{
#ifdef VBOX_WITH_WDDM
#else
#endif
}
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
{
}
#endif
/*****************************************************************************
* context_create
*
* Creates a new 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
* pPresentParameters: contains the pixelformats to use for onscreen rendering
*
*****************************************************************************/
struct wined3d_context *context_create(IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
const struct wined3d_format_desc *ds_format_desc)
{
const struct wined3d_format_desc *color_format_desc;
struct wined3d_context *ret;
int numSamples = 0;
int pixel_format;
unsigned int s;
if (!ret)
{
ERR("Failed to allocate context memory.\n");
return NULL;
}
#if defined(VBOX_WINE_WITH_SINGLE_CONTEXT) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#endif
if (!(hdc =
#else
#endif
)
)
{
ERR("Failed to retrieve a device context.\n");
goto out;
}
/* In case of ORM_BACKBUFFER, make sure to request an alpha component for
{
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("Auto depth stencil enabled, using format %s.\n",
}
/* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
if (swapchain->presentParms.MultiSampleType && (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD))
{
WARN("The application 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 (!pixel_format)
{
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 (!pixel_format)
{
ERR("Can't find a suitable pixel format.\n");
goto out;
}
{
goto out;
}
if (device->numContexts)
{
{
ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
}
}
if(!ctx) {
ERR("Failed to create a WGL context\n");
goto out;
}
{
ERR("Failed to add the newly created context to the context list\n");
if (!pwglDeleteContext(ctx))
{
}
goto out;
}
/* Mark all states dirty to force a proper initialization of the states
* on the first use of the context. */
{
}
#ifdef VBOX_WITH_WDDM
#else
#endif
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#endif
#ifndef VBOX_WITH_WDDM
#endif
{
/* 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, device->surface_alignment);");
checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
{
/* 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)");
}
{
/* 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)");
}
{
/* 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, ...");
}
}
{
/* 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));
}
{
checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
}
{
}
{
}
/* for WDDM case this is used for shared resource handling
*
* for XPDM this is needed to at least support texture sharing between device contexts.
* this is a kinda hack, but it is needed since our ogl driver currently does not support ShareLists */
#if defined(VBOX_WITH_WDDM) || defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#endif
LEAVE_GL();
return ret;
out:
return NULL;
}
#ifdef VBOX_WITH_WDDM
static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage);
BOOL context_acquire_context(struct wined3d_context * context, IWineD3DSurface *target, enum ContextUsage usage, BOOL bReValidate)
{
if (bReValidate)
{
if (target && SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
# ifdef DEBUG_misha
, TRUE
# endif
);
}
else {
}
}
// Assert(context->valid);
if (context != current_context)
{
if (context->vshader_const_dirty)
{
}
if (context->pshader_const_dirty)
{
}
}
else if (context->restore_ctx)
{
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
}
}
return TRUE;
}
struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
const struct wined3d_format_desc *ds_format_desc)
{
#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
UINT i;
#endif
#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
for(i = 0 ; i < device->numContexts ; i ++)
{
break;
}
}
#else
# error "Port Me!"
# endif
#endif
if (!context)
{
}
else
{
{
ERR("Failed to acquire the context.\n");
Assert(0);
}
else
{
}
}
return context;
}
#endif
/*****************************************************************************
* context_destroy
*
* Destroys a wined3d context
*
* Params:
* This: Device to activate the context for
* context: Context to destroy
*
*****************************************************************************/
{
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#endif
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
{
}
else
{
}
#else
#endif
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#else
# ifndef VBOX_WITH_WDDM
#else
# endif
#endif
}
/* 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
*
*****************************************************************************/
/* Context activation is done by the caller. */
{
int i;
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");
{
checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
}
checkGLcall("glDisable GL_TEXTURE_3D");
{
checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
}
checkGLcall("glDisable GL_TEXTURE_2D");
checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
if (sampler != WINED3D_UNMAPPED_STAGE)
{
if (sampler < MAX_TEXTURES) {
}
}
}
checkGLcall("glActiveTextureARB");
{
checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
}
checkGLcall("glDisable GL_TEXTURE_3D");
{
checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
}
checkGLcall("glDisable GL_TEXTURE_2D");
checkGLcall("glMatrixMode(GL_TEXTURE)");
checkGLcall("glLoadIdentity()");
{
0.0f);
checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
}
if (sampler != WINED3D_UNMAPPED_STAGE)
{
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");
{
checkGLcall("glDisable GL_POINT_SPRITE_ARB");
}
checkGLcall("glColorMask");
{
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
*
*****************************************************************************/
#if !defined(VBOX_WINE_WITH_SINGLE_CONTEXT) && !defined(VBOX_WINE_WITH_SINGLE_SWAPCHAIN_CONTEXT)
#endif
)
{
if (device->numContexts)
#else
unsigned int i;
# ifdef VBOX_WITH_WDDM
for (i = 0; i < device->numContexts; ++i)
{
# error "port me!"
# endif
}
# else
# else
# endif
}
}
# endif
#endif
/* Create a new context for the thread */
}
#ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
/*****************************************************************************
* 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
*
*****************************************************************************/
{
#endif
struct wined3d_context *context;
if (current_context
#else
#endif
) current_context = NULL;
if (!target)
{
if (current_context
)
{
}
else
{
#ifdef VBOX_WITH_WDDM
/* tmp work-around */
IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
#else
#endif
}
}
{
#ifdef VBOX_WITH_WDDM
}
else {
/* tmp work-around */
NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
);
}
#else
#endif
return current_context;
}
TRACE("Rendering onscreen\n");
, tid
#endif
);
#ifdef VBOX_WITH_WDDM
#endif
}
else
{
TRACE("Rendering offscreen\n");
/* Stay with the currently active 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. */
#ifdef VBOX_WITH_WDDM /* tmp work-around */
, tid
# endif
);
#else
, tid
# endif
);
#endif
}
#ifdef VBOX_WITH_WDDM
NULL //(IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
);
#endif
}
#ifndef VBOX_WITH_WDDM
#endif
return context;
}
#else
/*****************************************************************************
* 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
*
*****************************************************************************/
{
{
ERR("context is destroyed");
}
if (!target)
{
if (context
&& context->current_rt
)
{
}
else
{
if (!swapchain)
swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1]; /* just fallback to anything to avoid NPE */
}
}
{
# ifdef DEBUG_misha
, TRUE
# endif
);
}
else {
}
return context;
}
TRACE("Rendering onscreen\n");
# ifdef DEBUG_misha
, TRUE
# endif
);
}
else
{
if (context)
{
}
else
{
if (!swapchain)
swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1]; /* just fallback to anything to avoid NPE */
# ifdef DEBUG_misha
, TRUE
# endif
);
}
TRACE("Rendering offscreen\n");
}
return context;
}
#endif
/* Context activation is done by the caller. */
{
if (!surface_is_offscreen(rt))
{
ENTER_GL();
checkGLcall("glDrawBuffers()");
LEAVE_GL();
}
else
{
ENTER_GL();
{
if (!blit)
{
{
checkGLcall("glDrawBuffers()");
}
else
{
checkGLcall("glDrawBuffer()");
}
} else {
checkGLcall("glDrawBuffer()");
}
}
else
{
checkGLcall("glDrawBuffer()");
}
LEAVE_GL();
}
}
/* GL locking is done by the caller. */
{
checkGLcall("glDrawBuffer()");
}
static inline void context_set_render_offscreen(struct wined3d_context *context, const struct StateEntry *StateTable,
{
}
const struct wined3d_format_desc *required)
{
if((existing->Flags & WINED3DFMT_FLAG_FLOAT) != (required->Flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
/* If stencil bits are used the exact amount is required - otherwise wrapping
* won't work correctly */
return TRUE;
}
/* The caller provides a context */
static void context_validate_onscreen_formats(IWineD3DDeviceImpl *device, struct wined3d_context *context)
{
/* Onscreen surfaces are always in a swapchain */
IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) ((IWineD3DSurfaceImpl *)context->current_rt)->container;
if (!depth_stencil) return;
/* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
* or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
* format. */
WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
/* The currently active context is the necessary context to access the swapchain's onscreen buffers */
}
/* Context activation is done by the caller. */
static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage)
{
unsigned int i;
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");
}
}
static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target)
{
if (!target) return;
/* 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 wined3d_format_desc *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 context_acquire(). 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
* context_acquire() doing the right thing. */
/* Read the back buffer of the old drawable into the destination texture. */
{
}
else
{
}
}
}
}
/*****************************************************************************
* context_acquire
*
* 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 *context_acquire(IWineD3DDeviceImpl *device, IWineD3DSurface *target, enum ContextUsage usage)
{
struct wined3d_context *context;
{
ERR("context_acquire failed to get a valid context!");
return context;
}
if (context != current_context)
{
if (context->vshader_const_dirty)
{
}
if (context->pshader_const_dirty)
{
}
}
else if (context->restore_ctx)
{
#ifdef VBOX_WITH_WDDM
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
}
#else
{
ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
}
#endif
}
#ifdef DEBUG
#endif
#ifdef DEBUG
#endif
return context;
}