Put infrastructure in place so that other plugins may support dual-source blending.

git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6296 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
Nolan Check 2010-10-21 05:22:18 +00:00
parent 0b7e90f0de
commit 95cfca08e2
14 changed files with 105 additions and 51 deletions

View File

@ -22,7 +22,7 @@ static const char ID[4] = {'D', 'C', 'A', 'C'};
// Update this to the current SVN revision every time you change shader generation code.
// We don't automatically get this from SVN_REV because that would mean regenerating the
// shader cache for every revision, graphics-related or not, which is simply annoying.
const int version = 6294;
const int version = 6296;
LinearDiskCache::LinearDiskCache()
: file_(NULL), num_entries_(0) {

View File

@ -33,7 +33,7 @@ PIXELSHADERUID last_pixel_shader_uid;
// a unique identifier, basically containing all the bits. Yup, it's a lot ....
// It would likely be a lot more efficient to build this incrementally as the attributes
// are set...
void GetPixelShaderId(PIXELSHADERUID *uid, u32 dstAlphaEnable)
void GetPixelShaderId(PIXELSHADERUID *uid, DSTALPHA_MODE dstAlphaMode)
{
u32 numstages = bpmem.genMode.numtevstages + 1;
u32 projtexcoords = 0;
@ -49,12 +49,11 @@ void GetPixelShaderId(PIXELSHADERUID *uid, u32 dstAlphaEnable)
uid->values[0] = (u32)bpmem.genMode.numtevstages |
((u32)bpmem.genMode.numindstages << 4) |
((u32)bpmem.genMode.numtexgens << 7) |
((u32)dstAlphaEnable << 11) |
((u32)((bpmem.alphaFunc.hex >> 16) & 0xff) << 12) |
(projtexcoords << 20) |
((u32)bpmem.ztex2.op << 28);
((u32)dstAlphaMode << 11) |
((u32)((bpmem.alphaFunc.hex >> 16) & 0xff) << 13) |
(projtexcoords << 21) |
((u32)bpmem.ztex2.op << 29);
uid->values[0] = (uid->values[0] & ~0x0ff00000) | (projtexcoords << 20);
// swap table
for (int i = 0; i < 8; i += 2)
((u8*)&uid->values[1])[i / 2] = (bpmem.tevksel[i].hex & 0xf) | ((bpmem.tevksel[i + 1].hex & 0xf) << 4);
@ -443,7 +442,7 @@ char *GeneratePixelLightShader(char *p, int index, const LitChannel& chan, const
const char *GeneratePixelShaderCode(bool dstAlphaEnable, API_TYPE ApiType,u32 components)
const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType,u32 components)
{
setlocale(LC_NUMERIC, "C"); // Reset locale for compilation
text[sizeof(text) - 1] = 0x7C; // canary
@ -519,9 +518,18 @@ const char *GeneratePixelShaderCode(bool dstAlphaEnable, API_TYPE ApiType,u32 co
WRITE(p, "void main(\n");
if(ApiType != API_D3D11)
WRITE(p, " out float4 ocol0 : COLOR0,%s\n in float4 rawpos : %s,\n",DepthTextureEnable ? "\n out float depth : DEPTH," : "", ApiType == API_OPENGL ? "WPOS" : "POSITION");
{
// TODO: Support DSTALPHA_DUAL_SOURCE_BLEND for non-D3D11 shaders
WRITE(p, " out float4 ocol0 : COLOR0,%s\n in float4 rawpos : %s,\n",
DepthTextureEnable ? "\n out float depth : DEPTH," : "",
ApiType == API_OPENGL ? "WPOS" : "POSITION");
}
else
WRITE(p, " out float4 ocol0 : SV_Target0,\n out float4 ocol1 : SV_Target1,%s\n in float4 rawpos : SV_Position,\n",DepthTextureEnable ? "\n out float depth : SV_Depth," : "");
{
WRITE(p, " out float4 ocol0 : SV_Target0,%s%s\n in float4 rawpos : SV_Position,\n",
dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : SV_Target1," : "",
DepthTextureEnable ? "\n out float depth : SV_Depth," : "");
}
WRITE(p, " in float4 colors_0 : COLOR0,\n");
WRITE(p, " in float4 colors_1 : COLOR1");
@ -544,8 +552,7 @@ const char *GeneratePixelShaderCode(bool dstAlphaEnable, API_TYPE ApiType,u32 co
char* pmainstart = p;
int Pretest = AlphaPreTest();
// TODO: Re-enable the early discard on D3D11
if (dstAlphaEnable && !DepthTextureEnable && Pretest >= 0 && ApiType != API_D3D11)
if (dstAlphaMode == DSTALPHA_ALPHA_PASS && !DepthTextureEnable && Pretest >= 0)
{
if (!Pretest)
{
@ -807,7 +814,7 @@ const char *GeneratePixelShaderCode(bool dstAlphaEnable, API_TYPE ApiType,u32 co
WRITE(p, "depth = zCoord;\n");
}
if (dstAlphaEnable && ApiType != API_D3D11)
if (dstAlphaMode == DSTALPHA_ALPHA_PASS)
WRITE(p, " ocol0 = float4(prev.rgb, "I_ALPHA"[0].a);\n");
else
{
@ -817,12 +824,11 @@ const char *GeneratePixelShaderCode(bool dstAlphaEnable, API_TYPE ApiType,u32 co
// On D3D11, use dual-source color blending to perform dst alpha in a
// single pass
if (ApiType == API_D3D11)
if (dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND)
{
// Colors will be blended against the alpha from ocol1...
WRITE(p, " ocol1 = ocol0;\n");
// ...and the alpha from ocol0 will be written to the framebuffer.
if (dstAlphaEnable)
WRITE(p, " ocol0.a = "I_ALPHA"[0].a;\n");
}
}

View File

@ -104,8 +104,16 @@ public:
}
};
const char *GeneratePixelShaderCode(bool dstAlphaEnable, API_TYPE ApiType,u32 components);
void GetPixelShaderId(PIXELSHADERUID *uid, u32 dstAlphaEnable);
// Different ways to achieve rendering with destination alpha
enum DSTALPHA_MODE
{
DSTALPHA_NONE, // Render normally, without destination alpha
DSTALPHA_ALPHA_PASS, // Render normally first, then render again for alpha
DSTALPHA_DUAL_SOURCE_BLEND // Use dual-source blending
};
const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType,u32 components);
void GetPixelShaderId(PIXELSHADERUID *uid, DSTALPHA_MODE dstAlphaMode);
extern PIXELSHADERUID last_pixel_shader_uid;

View File

@ -254,37 +254,73 @@ void EmuGfxState::SetRenderTargetWriteMask(UINT8 mask)
void EmuGfxState::SetSrcBlend(D3D11_BLEND val)
{
// TODO: Check whether e.g. the dest color check is needed here
blenddesc.RenderTarget[0].SrcBlend = val;
if (m_useDstAlpha)
{
// Colors should blend against SRC1_ALPHA
if (val == D3D11_BLEND_SRC_ALPHA)
val = D3D11_BLEND_SRC1_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_ALPHA)
val = D3D11_BLEND_INV_SRC1_ALPHA;
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
}
else
{
if (val == D3D11_BLEND_SRC_COLOR) blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_COLOR) blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
else if (val == D3D11_BLEND_DEST_COLOR) blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_DEST_ALPHA;
else if (val == D3D11_BLEND_INV_DEST_COLOR) blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
else blenddesc.RenderTarget[0].SrcBlendAlpha = val;
// Colors should blend against SRC_ALPHA
if (val == D3D11_BLEND_SRC1_ALPHA)
val = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC1_ALPHA)
val = D3D11_BLEND_INV_SRC_ALPHA;
if (val == D3D11_BLEND_SRC_COLOR)
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_COLOR)
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
else if (val == D3D11_BLEND_DEST_COLOR)
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_DEST_ALPHA;
else if (val == D3D11_BLEND_INV_DEST_COLOR)
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
else
blenddesc.RenderTarget[0].SrcBlendAlpha = val;
}
blenddesc.RenderTarget[0].SrcBlend = val;
}
void EmuGfxState::SetDestBlend(D3D11_BLEND val)
{
// TODO: Check whether e.g. the source color check is needed here
blenddesc.RenderTarget[0].DestBlend = val;
if (m_useDstAlpha)
{
// Colors should blend against SRC1_ALPHA
if (val == D3D11_BLEND_SRC_ALPHA)
val = D3D11_BLEND_SRC1_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_ALPHA)
val = D3D11_BLEND_INV_SRC1_ALPHA;
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
}
else
{
if (val == D3D11_BLEND_SRC_COLOR) blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_COLOR) blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
else if (val == D3D11_BLEND_DEST_COLOR) blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
else if (val == D3D11_BLEND_INV_DEST_COLOR) blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
else blenddesc.RenderTarget[0].DestBlendAlpha = val;
// Colors should blend against SRC_ALPHA
if (val == D3D11_BLEND_SRC1_ALPHA)
val = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC1_ALPHA)
val = D3D11_BLEND_INV_SRC_ALPHA;
if (val == D3D11_BLEND_SRC_COLOR)
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_COLOR)
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
else if (val == D3D11_BLEND_DEST_COLOR)
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
else if (val == D3D11_BLEND_INV_DEST_COLOR)
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
else
blenddesc.RenderTarget[0].DestBlendAlpha = val;
}
blenddesc.RenderTarget[0].DestBlend = val;
}
void EmuGfxState::SetBlendOp(D3D11_BLEND_OP val)

View File

@ -228,10 +228,10 @@ void PixelShaderCache::Shutdown()
g_ps_disk_cache.Close();
}
bool PixelShaderCache::SetShader(bool dstAlpha,u32 components)
bool PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components)
{
PIXELSHADERUID uid;
GetPixelShaderId(&uid, dstAlpha);
GetPixelShaderId(&uid, dstAlphaMode);
// Check if the shader is already set
if (uid == last_pixel_shader_uid && PixelShaders[uid].frameCount == frameCount)
@ -256,7 +256,7 @@ bool PixelShaderCache::SetShader(bool dstAlpha,u32 components)
}
// Need to compile a new shader
const char* code = GeneratePixelShaderCode(dstAlpha, API_D3D11,components);
const char* code = GeneratePixelShaderCode(dstAlphaMode, API_D3D11, components);
D3DBlob* pbytecode;
if (!D3D::CompilePixelShader(code, strlen(code), &pbytecode))

View File

@ -32,7 +32,7 @@ public:
static void Init();
static void Clear();
static void Shutdown();
static bool SetShader(bool dstAlpha,u32 components);
static bool SetShader(DSTALPHA_MODE dstAlphaMode, u32 components);
static bool InsertByteCode(const PIXELSHADERUID &uid, void* bytecode, unsigned int bytecodelen);
static ID3D11PixelShader* GetColorMatrixProgram();

View File

@ -90,8 +90,8 @@ static const D3D11_BLEND d3dSrcFactors[8] =
D3D11_BLEND_ONE,
D3D11_BLEND_DEST_COLOR,
D3D11_BLEND_INV_DEST_COLOR,
D3D11_BLEND_SRC1_ALPHA,
D3D11_BLEND_INV_SRC1_ALPHA, // Use dual-source color blending for dst alpha
D3D11_BLEND_SRC_ALPHA,
D3D11_BLEND_INV_SRC_ALPHA, // NOTE: Use SRC1_ALPHA if dst alpha is enabled!
D3D11_BLEND_DEST_ALPHA,
D3D11_BLEND_INV_DEST_ALPHA
};
@ -102,8 +102,8 @@ static const D3D11_BLEND d3dDestFactors[8] =
D3D11_BLEND_ONE,
D3D11_BLEND_SRC_COLOR,
D3D11_BLEND_INV_SRC_COLOR,
D3D11_BLEND_SRC1_ALPHA,
D3D11_BLEND_INV_SRC1_ALPHA, // Use dual-source color blending for dst alpha
D3D11_BLEND_SRC_ALPHA,
D3D11_BLEND_INV_SRC_ALPHA, // NOTE: Use SRC1_ALPHA if dst alpha is enabled!
D3D11_BLEND_DEST_ALPHA,
D3D11_BLEND_INV_DEST_ALPHA
};

View File

@ -223,7 +223,9 @@ void VertexManager::vFlush()
D3D::gfxstate->SetDstAlpha(useDstAlpha);
if (!PixelShaderCache::SetShader(useDstAlpha,g_nativeVertexFmt->m_components))
if (!PixelShaderCache::SetShader(
useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE,
g_nativeVertexFmt->m_components))
goto shader_fail;
if (!VertexShaderCache::SetShader(g_nativeVertexFmt->m_components))
goto shader_fail;

View File

@ -273,10 +273,10 @@ void PixelShaderCache::Shutdown()
unique_shaders.clear();
}
bool PixelShaderCache::SetShader(bool dstAlpha,u32 components)
bool PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components)
{
PIXELSHADERUID uid;
GetPixelShaderId(&uid, dstAlpha ? 1 : 0);
GetPixelShaderId(&uid, dstAlphaMode);
// Check if the shader is already set
if (uid == last_pixel_shader_uid && PixelShaders[uid].frameCount == frameCount)
@ -308,7 +308,7 @@ bool PixelShaderCache::SetShader(bool dstAlpha,u32 components)
}
// Need to compile a new shader
const char *code = GeneratePixelShaderCode(dstAlpha, API_D3D9,components);
const char *code = GeneratePixelShaderCode(dstAlphaMode, API_D3D9, components);
u32 code_hash = HashAdler32((const u8 *)code, strlen(code));
unique_shaders.insert(code_hash);

View File

@ -60,7 +60,7 @@ private:
public:
static void Init();
static void Shutdown();
static bool SetShader(bool dstAlpha, u32 componets);
static bool SetShader(DSTALPHA_MODE dstAlphaMode, u32 componets);
static bool InsertByteCode(const PIXELSHADERUID &uid, const u8 *bytecode, int bytecodelen, bool activate);
static LPDIRECT3DPIXELSHADER9 GetColorMatrixProgram(int SSAAMode);
static LPDIRECT3DPIXELSHADER9 GetColorCopyProgram(int SSAAMode);

View File

@ -155,7 +155,7 @@ void VertexManager::vFlush()
VertexShaderManager::SetConstants();
PixelShaderManager::SetConstants();
if (!PixelShaderCache::SetShader(false,g_nativeVertexFmt->m_components))
if (!PixelShaderCache::SetShader(DSTALPHA_NONE,g_nativeVertexFmt->m_components))
{
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
goto shader_fail;
@ -175,7 +175,7 @@ void VertexManager::vFlush()
if (bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate)
{
DWORD write = 0;
if (!PixelShaderCache::SetShader(true, g_nativeVertexFmt->m_components))
if (!PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS, g_nativeVertexFmt->m_components))
{
DEBUGGER_PAUSE_LOG_AT(NEXT_ERROR,true,{printf("Fail to set pixel shader\n");});
goto shader_fail;

View File

@ -184,11 +184,11 @@ void PixelShaderCache::Shutdown()
PixelShaders.clear();
}
FRAGMENTSHADER* PixelShaderCache::SetShader(bool dstAlpha,u32 components)
FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components)
{
DVSTARTPROFILE();
PIXELSHADERUID uid;
GetPixelShaderId(&uid, dstAlpha ? 1 : 0);
GetPixelShaderId(&uid, dstAlphaMode);
// Check if the shader is already set
if (uid == last_pixel_shader_uid && PixelShaders[uid].frameCount == frameCount)
@ -216,7 +216,7 @@ FRAGMENTSHADER* PixelShaderCache::SetShader(bool dstAlpha,u32 components)
PSCacheEntry& newentry = PixelShaders[uid];
newentry.frameCount = frameCount;
pShaderLast = &newentry.shader;
const char *code = GeneratePixelShaderCode(dstAlpha,API_OPENGL,components);
const char *code = GeneratePixelShaderCode(dstAlphaMode, API_OPENGL, components);
#if defined(_DEBUG) || defined(DEBUGFAST)
if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) {

View File

@ -71,7 +71,7 @@ public:
static void Init();
static void Shutdown();
static FRAGMENTSHADER* SetShader(bool dstAlphaEnable,u32 components);
static FRAGMENTSHADER* SetShader(DSTALPHA_MODE dstAlphaMode, u32 components);
static bool CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram);
static GLuint GetColorMatrixProgram();

View File

@ -185,7 +185,7 @@ void VertexManager::vFlush()
PixelShaderManager::SetConstants();
// finally bind
FRAGMENTSHADER* ps = PixelShaderCache::SetShader(false,g_nativeVertexFmt->m_components);
FRAGMENTSHADER* ps = PixelShaderCache::SetShader(DSTALPHA_NONE,g_nativeVertexFmt->m_components);
VERTEXSHADER* vs = VertexShaderCache::SetShader(g_nativeVertexFmt->m_components);
if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid); // Lego Star Wars crashes here.
if (vs) VertexShaderCache::SetCurrentShader(vs->glprogid);
@ -195,7 +195,9 @@ void VertexManager::vFlush()
// run through vertex groups again to set alpha
if (!g_ActiveConfig.bDstAlphaPass && bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate)
{
ps = PixelShaderCache::SetShader(true,g_nativeVertexFmt->m_components);
// TODO: If host supports GL_ARB_blend_func_extended, use
// DSTALPHA_DUAL_SOURCE_BLEND and set blend modes accordingly.
ps = PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS,g_nativeVertexFmt->m_components);
if (ps) PixelShaderCache::SetCurrentShader(ps->glprogid);
// only update alpha