shaderapi.c revision 07557d07616212d7ba6e7ab3059e85cb14633775
/* $Id$ */
/** @file
* shaderlib -- interface to WINE's Direct3D shader functions
*/
/*
* Copyright (C) 2014-2015 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#define WINED3D_EXTERN
#include "wined3d_private.h"
#include "shaderlib.h"
#ifdef RT_OS_WINDOWS
# define OGLGETPROCADDRESS wglGetProcAddress
# include <dlfcn.h>
# define OGLGETPROCADDRESS(x) MyNSGLGetProcAddress((const char *)x)
void *MyNSGLGetProcAddress(const char *pszSymbol)
{
/* Another copy in DevVGA-SVGA3d-ogl.cpp. */
s_pvImage = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
}
#else
#endif
#define GL_EXT_FUNCS_GEN \
/* GL_ARB_shader_objects */ \
static struct wined3d_adapter g_adapter = {0};
static bool g_fInitializedLibrary = false;
#define SHADER_SET_CURRENT_CONTEXT(ctx) \
{
/* Dynamically load all GL core functions. */
#ifdef RT_OS_WINDOWS
#else
#endif
/* Dynamically load all GL extension functions. */
{ \
}
/* Fill in GL capabilities. */
LogRel(("shaderlib: GL Limits:\n"));
LogRel(("shaderlib: buffers=%-2u lights=%-2u textures=%-2u texture_stages=%u\n",
gl_info->limits.buffers, gl_info->limits.lights, gl_info->limits.textures, gl_info->limits.texture_stages));
LogRel(("shaderlib: fragment_samplers=%-2u vertex_samplers=%-2u combined_samplers=%-3u general_combiners=%u\n",
gl_info->limits.fragment_samplers, gl_info->limits.vertex_samplers, gl_info->limits.combined_samplers, gl_info->limits.general_combiners));
LogRel(("shaderlib: sampler_stages=%-2u clipplanes=%-2u texture_size=%-5u texture3d_size=%u\n",
gl_info->limits.sampler_stages, gl_info->limits.clipplanes, gl_info->limits.texture_size, gl_info->limits.texture3d_size));
LogRel(("shaderlib: pointsize_max=%d.%d pointsize_min=%d.%d point_sprite_units=%-2u blends=%u\n",
LogRel(("shaderlib: anisotropy=%-2u shininess=%d.%02d\n",
gl_info->limits.anisotropy, (int)gl_info->limits.shininess, (int)(gl_info->limits.shininess * 100) % 100));
LogRel(("shaderlib: glsl_varyings=%-3u glsl_vs_float_constants=%-4u glsl_ps_float_constants=%u\n",
gl_info->limits.glsl_varyings, gl_info->limits.glsl_vs_float_constants, gl_info->limits.glsl_ps_float_constants));
LogRel(("shaderlib: arb_vs_instructions=%-4u arb_vs_native_constants=%-4u qarb_vs_float_constants=%u\n",
gl_info->limits.arb_vs_instructions, gl_info->limits.arb_vs_native_constants, gl_info->limits.arb_vs_float_constants));
LogRel(("shaderlib: arb_vs_temps=%-2u arb_ps_float_constants=%-4u arb_ps_local_constants=%u\n",
gl_info->limits.arb_vs_temps, gl_info->limits.arb_ps_float_constants, gl_info->limits.arb_ps_local_constants));
LogRel(("shaderlib: arb_ps_instructions=%-4u arb_ps_temps=%-2u arb_ps_native_constants=%u\n",
gl_info->limits.arb_ps_instructions, gl_info->limits.arb_ps_temps, gl_info->limits.arb_ps_native_constants));
g_fInitializedLibrary = true;
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderDestroyLib(void)
{
return VINF_SUCCESS;
}
{
return context->pDeviceContext;
}
struct wined3d_context *context_get_current(void)
{
return g_pCurrentContext;
}
struct wined3d_context *context_acquire(IWineD3DDeviceImpl *This, IWineD3DSurface *target, enum ContextUsage usage)
{
return g_pCurrentContext;
}
{
struct wined3d_context *pContext;
pContext->render_offscreen = false;
{
struct shader_caps shader_caps;
/* Initialize the shader backend. */
hr = pContext->pDeviceContext->shader_backend->shader_alloc_private((IWineD3DDevice *)pContext->pDeviceContext);
pContext->pDeviceContext->stateBlock = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pContext->pDeviceContext->stateBlock));
pContext->pDeviceContext->stateBlock->vertexDecl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DVertexDeclarationImpl));
/* Initialize the texture unit mapping to a 1:1 mapping */
{
{
} else {
}
}
}
*ppShaderContext = (void *)pContext;
return VINF_SUCCESS;
}
{
if (pContext->pDeviceContext)
{
/* Fails during init only. */
pContext->pDeviceContext->shader_backend->shader_free_private((IWineD3DDevice *)pContext->pDeviceContext);
if (This)
{
if (This->vertexShaderConstantF)
if (This->pixelShaderConstantF)
if (This->contained_vs_consts_f)
if (This->contained_ps_consts_f)
if (This->vertexDecl)
}
}
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderCreateVertexShader(void *pShaderContext, const uint32_t *pShaderData, void **pShaderObj)
{
if (!object)
{
Log(("Failed to allocate shader memory.\n"));
return VERR_NO_MEMORY;
}
{
return VERR_INTERNAL_ERROR;
}
#ifdef VBOX_WINE_WITH_SHADER_CACHE
#endif
*pShaderObj = (void *)object;
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderCreatePixelShader(void *pShaderContext, const uint32_t *pShaderData, void **pShaderObj)
{
if (!object)
{
Log(("Failed to allocate shader memory.\n"));
return VERR_NO_MEMORY;
}
{
return VERR_INTERNAL_ERROR;
}
#ifdef VBOX_WINE_WITH_SHADER_CACHE
#endif
*pShaderObj = (void *)object;
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
/* Checked here to allow proper stateblock recording */
Log(("App is setting the old shader over, nothing to do\n"));
return VINF_SUCCESS;
}
g_pCurrentContext->fChangedVertexShader = true;
return VINF_SUCCESS;
}
{
/* Checked here to allow proper stateblock recording */
Log(("App is setting the old shader over, nothing to do\n"));
return VINF_SUCCESS;
}
g_pCurrentContext->fChangedPixelShader = true;
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderSetVertexShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
{
Log(("(ShaderSetVertexShaderConstantB %p, srcData %p, start %d, count %d)\n",
{
Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
return VERR_INVALID_PARAMETER;
}
for (i = 0; i < cnt; i++)
}
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderSetVertexShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
{
Log(("(ShaderSetVertexShaderConstantI %p, srcData %p, start %d, count %d)\n",
{
Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
return VERR_INVALID_PARAMETER;
}
memcpy(&This->updateStateBlock->vertexShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
}
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderSetVertexShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
{
Log(("(ShaderSetVertexShaderConstantF %p, srcData %p, start %d, count %d)\n",
if (srcData == NULL || start + count > This->d3d_vshader_constantF || start > This->d3d_vshader_constantF)
{
Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
return VERR_INVALID_PARAMETER;
}
memcpy(&This->updateStateBlock->vertexShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderSetPixelShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
{
Log(("(ShaderSetPixelShaderConstantB %p, srcData %p, start %d, count %d)\n",
{
Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
return VERR_INVALID_PARAMETER;
}
for (i = 0; i < cnt; i++)
}
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderSetPixelShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
{
Log(("(ShaderSetPixelShaderConstantI %p, srcData %p, start %d, count %d)\n",
{
Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
return VERR_INVALID_PARAMETER;
}
memcpy(&This->updateStateBlock->pixelShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
}
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderSetPixelShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
{
Log(("(ShaderSetPixelShaderConstantF %p, srcData %p, start %d, count %d)\n",
if (srcData == NULL || start + count > This->d3d_pshader_constantF || start > This->d3d_pshader_constantF)
{
Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
return VERR_INVALID_PARAMETER;
}
memcpy(&This->updateStateBlock->pixelShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
return VINF_SUCCESS;
}
{
#ifdef DEBUG
#endif
/* @todo missing state:
* - fog enable (stateblock->renderState[WINED3DRS_FOGENABLE])
* - fog mode (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
* - stateblock->vertexDecl->position_transformed
*/
pThis->shader_backend->shader_select(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
pThis->shader_backend->shader_load_constants(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
g_pCurrentContext->fChangedPixelShaderConstant = false;
return VINF_SUCCESS;
}
SHADERDECL(int) ShaderTransformProjection(unsigned cxViewPort, unsigned cyViewPort, float matrix[16])
{
#ifdef DEBUG
#endif
/* Assumes OpenGL context has been activated. */
/* The rule is that the window coordinate 0 does not correspond to the
beginning of the first pixel, but the center of the first pixel.
As a consequence if you want to correctly draw one line exactly from
the left to the right end of the viewport (with all matrices set to
be identity), the x coords of both ends of the line would be not
-1 and 1 respectively but (-1-1/viewport_widh) and (1-1/viewport_width)
instead.
1.0 / Width is used because the coord range goes from -1.0 to 1.0, then we
half a pixel.
The other fun is that d3d's output z range after the transformation is [0;1],
but opengl's is [-1;1]. Since the z buffer is in range [0;1] for both, gl
scales [-1;1] to [0;1]. This would mean that we end up in [0.5;1] and loose a lot
of Z buffer precision and the clear values do not match in the z test. Thus scale
[0;1] to [-1;1], so when gl undoes that we utilize the full z range
*/
/*
* Careful with the order of operations here, we're essentially working backwards:
* x = x + 1/w;
* y = (y - 1/h) * flip;
* z = z * 2 - 1;
*
* Becomes:
* glTranslatef(0.0, 0.0, -1.0);
* glScalef(1.0, 1.0, 2.0);
*
* glScalef(1.0, flip, 1.0);
* glTranslatef(1/w, -1/h, 0.0);
*
* This is equivalent to:
* glTranslatef(1/w, -flip/h, -1.0)
* glScalef(1.0, flip, 2.0);
*/
/* Translate by slightly less than a half pixel to force a top-left
* filling convention. We want the difference to be large enough that
* it doesn't get lost due to rounding inside the driver, but small
* enough to prevent it from interfering with any anti-aliasing. */
/* flip y coordinate origin too */
#ifdef DEBUG
lastError = glGetError(); \
AssertMsgReturn(lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, lastError), VERR_INTERNAL_ERROR);
#endif
return VINF_SUCCESS;
}