📄 context.c
字号:
/*
* Context and render target management in wined3d
*
* Copyright 2007 Stefan D鰏inger for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* 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
*/
#include "config.h"
#include <stdio.h>
#ifdef HAVE_FLOAT_H
# include <float.h>
#endif
#include "wined3d_private.h"
WINE_DEFAULT_DEBUG_CHANNEL(d3d);
#define GLINFO_LOCATION This->adapter->gl_info
/*****************************************************************************
* 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
*
*****************************************************************************/
static void Context_MarkStateDirty(WineD3DContext *context, DWORD state) {
DWORD rep = StateTable[state].representative;
DWORD idx;
BYTE shift;
if(!rep || isStateDirty(context, rep)) return;
context->dirtyArray[context->numDirtyEntries++] = rep;
idx = rep >> 5;
shift = rep & 0x1f;
context->isStateDirty[idx] |= (1 << shift);
}
/*****************************************************************************
* 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
*
*****************************************************************************/
static WineD3DContext *AddContextToArray(IWineD3DDeviceImpl *This, HWND win_handle, HDC hdc, HGLRC glCtx, HPBUFFERARB pbuffer) {
WineD3DContext **oldArray = This->contexts;
DWORD state;
This->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->contexts) * (This->numContexts + 1));
if(This->contexts == NULL) {
ERR("Unable to grow the context array\n");
This->contexts = oldArray;
return NULL;
}
if(oldArray) {
memcpy(This->contexts, oldArray, sizeof(*This->contexts) * This->numContexts);
}
This->contexts[This->numContexts] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WineD3DContext));
if(This->contexts[This->numContexts] == NULL) {
ERR("Unable to allocate a new context\n");
HeapFree(GetProcessHeap(), 0, This->contexts);
This->contexts = oldArray;
return NULL;
}
This->contexts[This->numContexts]->hdc = hdc;
This->contexts[This->numContexts]->glCtx = glCtx;
This->contexts[This->numContexts]->pbuffer = pbuffer;
This->contexts[This->numContexts]->win_handle = win_handle;
HeapFree(GetProcessHeap(), 0, oldArray);
/* Mark all states dirty to force a proper initialization of the states on the first use of the context
*/
for(state = 0; state <= STATE_HIGHEST; state++) {
Context_MarkStateDirty(This->contexts[This->numContexts], state);
}
This->numContexts++;
TRACE("Created context %p\n", This->contexts[This->numContexts - 1]);
return This->contexts[This->numContexts - 1];
}
/*****************************************************************************
* 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
*
*****************************************************************************/
WineD3DContext *CreateContext(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *target, HWND win_handle, BOOL create_pbuffer, const WINED3DPRESENT_PARAMETERS *pPresentParms) {
HDC oldDrawable, hdc;
HPBUFFERARB pbuffer = NULL;
HGLRC ctx = NULL, oldCtx;
WineD3DContext *ret = NULL;
int s;
TRACE("(%p): Creating a %s context for render target %p\n", This, create_pbuffer ? "offscreen" : "onscreen", target);
if(create_pbuffer) {
HDC hdc_parent = GetDC(win_handle);
int iPixelFormat = 0;
short red, green, blue, alphaBits, colorBits;
short depthBits, stencilBits;
IWineD3DSurface *StencilSurface = This->stencilBufferTarget;
WINED3DFORMAT StencilBufferFormat = (NULL != StencilSurface) ? ((IWineD3DSurfaceImpl *) StencilSurface)->resource.format : 0;
int attribs[256];
int nAttribs = 0;
unsigned int nFormats;
#define PUSH1(att) attribs[nAttribs++] = (att);
#define PUSH2(att,value) attribs[nAttribs++] = (att); attribs[nAttribs++] = (value);
/* Retrieve the specifications for the pixelformat from the backbuffer / stencilbuffer */
getColorBits(target->resource.format, &red, &green, &blue, &alphaBits, &colorBits);
getDepthStencilBits(StencilBufferFormat, &depthBits, &stencilBits);
PUSH2(WGL_DRAW_TO_PBUFFER_ARB, 1); /* We need pbuffer support; doublebuffering isn't needed */
PUSH2(WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB); /* Make sure we don't get a float or color index format */
PUSH2(WGL_COLOR_BITS_ARB, colorBits);
PUSH2(WGL_ALPHA_BITS_ARB, alphaBits);
PUSH2(WGL_DEPTH_BITS_ARB, depthBits);
PUSH2(WGL_STENCIL_BITS_ARB, stencilBits);
PUSH1(0); /* end the list */
#undef PUSH1
#undef PUSH2
/* Try to find a pixelformat that matches exactly. If that fails let ChoosePixelFormat try to find a close match */
if(!GL_EXTCALL(wglChoosePixelFormatARB(hdc_parent, (const int*)&attribs, NULL, 1, &iPixelFormat, &nFormats)))
{
PIXELFORMATDESCRIPTOR pfd;
TRACE("Falling back to ChoosePixelFormat as wglChoosePixelFormatARB failed\n");
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER_DONTCARE | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = colorBits;
pfd.cDepthBits = depthBits;
pfd.cStencilBits = stencilBits;
pfd.iLayerType = PFD_MAIN_PLANE;
iPixelFormat = ChoosePixelFormat(hdc_parent, &pfd);
if(!iPixelFormat) {
/* If this happens something is very wrong as ChoosePixelFormat barely fails */
ERR("Can't find a suitable iPixelFormat for the pbuffer\n");
}
}
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");
ReleaseDC(win_handle, hdc_parent);
goto out;
}
/* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
hdc = GL_EXTCALL(wglGetPbufferDCARB(pbuffer));
if(!hdc) {
ERR("Cannot get a HDC for pbuffer (%p)\n", pbuffer);
GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
ReleaseDC(win_handle, hdc_parent);
goto out;
}
ReleaseDC(win_handle, hdc_parent);
} else {
PIXELFORMATDESCRIPTOR pfd;
int iPixelFormat;
short red, green, blue, alpha;
short colorBits;
short depthBits, stencilBits;
int res;
hdc = GetDC(win_handle);
if(hdc == NULL) {
ERR("Cannot retrieve a device context!\n");
goto out;
}
/* PixelFormat selection */
/* TODO: fill cColorBits/cDepthBits with target->resource.format */
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 0;
pfd.cStencilBits = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
/* Try to match the colorBits of the d3d format */
if(getColorBits(target->resource.format, &red, &green, &blue, &alpha, &colorBits))
pfd.cColorBits = colorBits;
/* 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. */
if(pPresentParms->EnableAutoDepthStencil) {
TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms->AutoDepthStencilFormat));
if(getDepthStencilBits(pPresentParms->AutoDepthStencilFormat, &depthBits, &stencilBits)) {
pfd.cDepthBits = depthBits;
pfd.cStencilBits = stencilBits;
}
}
iPixelFormat = ChoosePixelFormat(hdc, &pfd);
if(!iPixelFormat) {
/* If this happens something is very wrong as ChoosePixelFormat barely fails */
ERR("Can't find a suitable iPixelFormat\n");
}
DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd);
res = SetPixelFormat(hdc, iPixelFormat, NULL);
if(!res) {
int oldPixelFormat = GetPixelFormat(hdc);
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 {
ERR("SetPixelFormat failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
return FALSE;
}
}
}
ctx = pwglCreateContext(hdc);
if(This->numContexts) pwglShareLists(This->contexts[0]->glCtx, ctx);
if(!ctx) {
ERR("Failed to create a WGL context\n");
if(create_pbuffer) {
GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
}
goto out;
}
ret = AddContextToArray(This, win_handle, hdc, ctx, pbuffer);
if(!ret) {
ERR("Failed to add the newly created context to the context list\n");
pwglDeleteContext(ctx);
if(create_pbuffer) {
GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
}
goto out;
}
ret->surface = (IWineD3DSurface *) target;
ret->isPBuffer = create_pbuffer;
ret->tid = GetCurrentThreadId();
TRACE("Successfully created new context %p\n", ret);
/* Set up the context defaults */
oldCtx = pwglGetCurrentContext();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -