DX11 code maintenance, part 4:

Move blend state management from EmuGfxState to Renderer.


git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6905 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
NeoBrainX 2011-01-24 10:42:43 +00:00
parent cd5a2e4cc5
commit e0c6092721
10 changed files with 143 additions and 171 deletions

View File

@ -66,7 +66,7 @@ public:
virtual void SetSamplerState(int stage,int texindex) = 0;
virtual void SetInterlacingMode() = 0;
virtual void ApplyState() = 0;
virtual void ApplyState(bool bUseDstAlpha) = 0;
virtual void RestoreState() = 0;
// Real internal resolution:

View File

@ -26,20 +26,6 @@ StateManager* stateman;
EmuGfxState::EmuGfxState() : vertexshader(NULL), vsbytecode(NULL), pixelshader(NULL), psbytecode(NULL), apply_called(false)
{
m_useDstAlpha = false;
memset(&blenddesc, 0, sizeof(blenddesc));
blenddesc.AlphaToCoverageEnable = FALSE;
blenddesc.IndependentBlendEnable = FALSE;
blenddesc.RenderTarget[0].BlendEnable = FALSE;
blenddesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
blenddesc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
blenddesc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
blenddesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blenddesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blenddesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
blenddesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
pscbuf = NULL;
vscbuf = NULL;
vshaderchanged = false;
@ -152,13 +138,6 @@ void EmuGfxState::ApplyState()
}
D3D::context->PSSetConstantBuffers(0, 1, &pscbuf);
ID3D11BlendState* blstate;
hr = device->CreateBlendState(&blenddesc, &blstate);
if (FAILED(hr)) PanicAlert("Failed to create blend state at %s %d\n", __FILE__, __LINE__);
stateman->PushBlendState(blstate);
SetDebugObjectName((ID3D11DeviceChild*)blstate, "a blend state of EmuGfxState");
SAFE_RELEASE(blstate);
context->PSSetShader(pixelshader, NULL, 0);
context->VSSetShader(vertexshader, NULL, 0);
@ -169,114 +148,10 @@ void EmuGfxState::Reset()
{
if (apply_called)
{
stateman->PopBlendState();
apply_called = false;
}
}
void EmuGfxState::SetAlphaBlendEnable(bool enable)
{
blenddesc.RenderTarget[0].BlendEnable = enable;
}
void EmuGfxState::SetRenderTargetWriteMask(UINT8 mask)
{
blenddesc.RenderTarget[0].RenderTargetWriteMask = mask;
}
void EmuGfxState::SetSrcBlend(D3D11_BLEND val)
{
// TODO: Check whether e.g. the dest color check is needed here
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
{
// 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
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
{
// 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)
{
blenddesc.RenderTarget[0].BlendOp = val;
if (m_useDstAlpha)
{
blenddesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
}
else
{
blenddesc.RenderTarget[0].BlendOpAlpha = val;
}
}
void EmuGfxState::SetDstAlpha(bool enable)
{
m_useDstAlpha = enable;
SetSrcBlend(blenddesc.RenderTarget[0].SrcBlend);
SetDestBlend(blenddesc.RenderTarget[0].DestBlend);
SetBlendOp(blenddesc.RenderTarget[0].BlendOp);
}
template<typename T> AutoState<T>::AutoState(const T* object) : state(object)
{
((IUnknown*)state)->AddRef();

View File

@ -40,15 +40,6 @@ public:
void ApplyState(); // apply current state
void Reset();
// blend state
void SetAlphaBlendEnable(bool enable);
void SetRenderTargetWriteMask(UINT8 mask);
void SetSrcBlend(D3D11_BLEND val);
void SetDestBlend(D3D11_BLEND val);
void SetBlendOp(D3D11_BLEND_OP val);
void SetDstAlpha(bool enable);
float psconstants[C_PENVCONST_END*4];
float vsconstants[C_VENVCONST_END*4];
@ -69,10 +60,6 @@ private:
D3D11_INPUT_ELEMENT_DESC inp_elems[32];
int num_inp_elems;
D3D11_BLEND_DESC blenddesc;
bool m_useDstAlpha;
bool apply_called;
};

View File

@ -68,6 +68,7 @@ ID3D11RasterizerState* resetraststate = NULL;
struct
{
D3D11_SAMPLER_DESC sampdc[8];
D3D11_BLEND_DESC blenddc;
D3D11_DEPTH_STENCIL_DESC depthdc;
D3D11_RASTERIZER_DESC rastdc;
} gx_state;
@ -347,6 +348,18 @@ Renderer::Renderer()
SetupDeviceObjects();
// Setup GX pipeline state
memset(&gx_state.blenddc, 0, sizeof(gx_state.blenddc));
gx_state.blenddc.AlphaToCoverageEnable = FALSE;
gx_state.blenddc.IndependentBlendEnable = FALSE;
gx_state.blenddc.RenderTarget[0].BlendEnable = FALSE;
gx_state.blenddc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
gx_state.blenddc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
gx_state.blenddc.RenderTarget[0].DestBlend = D3D11_BLEND_ZERO;
gx_state.blenddc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
gx_state.blenddc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
memset(&gx_state.depthdc, 0, sizeof(gx_state.depthdc));
gx_state.depthdc.DepthEnable = TRUE;
gx_state.depthdc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
@ -508,7 +521,7 @@ void Renderer::SetColorMask()
color_mask = D3D11_COLOR_WRITE_ENABLE_ALPHA;
if (bpmem.blendmode.colorupdate)
color_mask |= D3D11_COLOR_WRITE_ENABLE_RED | D3D11_COLOR_WRITE_ENABLE_GREEN | D3D11_COLOR_WRITE_ENABLE_BLUE;
D3D::gfxstate->SetRenderTargetWriteMask(color_mask);
gx_state.blenddc.RenderTarget[0].RenderTargetWriteMask = color_mask;
}
// This function allows the CPU to directly access the EFB.
@ -789,6 +802,56 @@ void Renderer::ReinterpretPixelData(unsigned int convtype)
// TODO
}
void SetSrcBlend(D3D11_BLEND 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)
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_COLOR)
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
else if (val == D3D11_BLEND_DEST_COLOR)
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_DEST_ALPHA;
else if (val == D3D11_BLEND_INV_DEST_COLOR)
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
else
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = val;
gx_state.blenddc.RenderTarget[0].SrcBlend = val;
}
void SetDestBlend(D3D11_BLEND 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)
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_SRC_ALPHA;
else if (val == D3D11_BLEND_INV_SRC_COLOR)
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
else if (val == D3D11_BLEND_DEST_COLOR)
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
else if (val == D3D11_BLEND_INV_DEST_COLOR)
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA;
else
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = val;
gx_state.blenddc.RenderTarget[0].DestBlend = val;
}
void SetBlendOp(D3D11_BLEND_OP val)
{
gx_state.blenddc.RenderTarget[0].BlendOp = val;
gx_state.blenddc.RenderTarget[0].BlendOpAlpha = val;
}
void Renderer::SetBlendMode(bool forceUpdate)
{
if (bpmem.blendmode.logicopenable && !forceUpdate)
@ -796,19 +859,19 @@ void Renderer::SetBlendMode(bool forceUpdate)
if (bpmem.blendmode.subtract) // enable blending src 1 dst 1
{
D3D::gfxstate->SetAlphaBlendEnable(true);
D3D::gfxstate->SetBlendOp(D3D11_BLEND_OP_REV_SUBTRACT);
D3D::gfxstate->SetSrcBlend(d3dSrcFactors[1]);
D3D::gfxstate->SetDestBlend(d3dDestFactors[1]);
gx_state.blenddc.RenderTarget[0].BlendEnable = true;
SetBlendOp(D3D11_BLEND_OP_REV_SUBTRACT);
SetSrcBlend(d3dSrcFactors[1]);
SetDestBlend(d3dDestFactors[1]);
}
else
{
D3D::gfxstate->SetAlphaBlendEnable(bpmem.blendmode.blendenable && (!( bpmem.blendmode.srcfactor == 1 && bpmem.blendmode.dstfactor == 0)));
gx_state.blenddc.RenderTarget[0].BlendEnable = bpmem.blendmode.blendenable && (!( bpmem.blendmode.srcfactor == 1 && bpmem.blendmode.dstfactor == 0));
if (bpmem.blendmode.blendenable && (!( bpmem.blendmode.srcfactor == 1 && bpmem.blendmode.dstfactor == 0)))
{
D3D::gfxstate->SetBlendOp(D3D11_BLEND_OP_ADD);
D3D::gfxstate->SetSrcBlend(d3dSrcFactors[bpmem.blendmode.srcfactor]);
D3D::gfxstate->SetDestBlend(d3dDestFactors[bpmem.blendmode.dstfactor]);
SetBlendOp(D3D11_BLEND_OP_ADD);
SetSrcBlend(d3dSrcFactors[bpmem.blendmode.srcfactor]);
SetDestBlend(d3dDestFactors[bpmem.blendmode.dstfactor]);
}
}
}
@ -1100,10 +1163,36 @@ void Renderer::RestoreAPIState()
reset_called = false;
}
void Renderer::ApplyState()
void Renderer::ApplyState(bool bUseDstAlpha)
{
HRESULT hr;
if (bUseDstAlpha)
{
// Colors should blend against SRC1_ALPHA
if (gx_state.blenddc.RenderTarget[0].SrcBlend == D3D11_BLEND_SRC_ALPHA)
gx_state.blenddc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC1_ALPHA;
else if (gx_state.blenddc.RenderTarget[0].SrcBlend == D3D11_BLEND_INV_SRC_ALPHA)
gx_state.blenddc.RenderTarget[0].SrcBlend = D3D11_BLEND_INV_SRC1_ALPHA;
// Colors should blend against SRC1_ALPHA
if (gx_state.blenddc.RenderTarget[0].DestBlend == D3D11_BLEND_SRC_ALPHA)
gx_state.blenddc.RenderTarget[0].DestBlend = D3D11_BLEND_SRC1_ALPHA;
else if (gx_state.blenddc.RenderTarget[0].DestBlend == D3D11_BLEND_INV_SRC_ALPHA)
gx_state.blenddc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC1_ALPHA;
gx_state.blenddc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
gx_state.blenddc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
gx_state.blenddc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
}
ID3D11BlendState* blstate;
hr = D3D::device->CreateBlendState(&gx_state.blenddc, &blstate);
if (FAILED(hr)) PanicAlert("Failed to create blend state at %s %d\n", __FILE__, __LINE__);
D3D::stateman->PushBlendState(blstate);
D3D::SetDebugObjectName((ID3D11DeviceChild*)blstate, "a blend state of EmuGfxState");
SAFE_RELEASE(blstate);
ID3D11DepthStencilState* depth_state;
hr = D3D::device->CreateDepthStencilState(&gx_state.depthdc, &depth_state);
if (SUCCEEDED(hr)) D3D::SetDebugObjectName((ID3D11DeviceChild*)depth_state, "depth-stencil state used to emulate the GX pipeline");
@ -1137,6 +1226,13 @@ void Renderer::ApplyState()
SAFE_RELEASE(samplerstate[stage]);
D3D::stateman->Apply();
if (bUseDstAlpha)
{
// restore actual state
SetBlendMode(false);
SetLogicOpMode();
}
}
void Renderer::RestoreState()
@ -1144,6 +1240,7 @@ void Renderer::RestoreState()
ID3D11ShaderResourceView* shader_resources[8] = { NULL };
D3D::context->PSSetShaderResources(0, 8, shader_resources);
D3D::stateman->PopBlendState();
D3D::stateman->PopDepthState();
D3D::stateman->PopRasterizerState();
}
@ -1175,10 +1272,10 @@ void Renderer::SetLogicOpMode()
if (bpmem.blendmode.logicopenable && bpmem.blendmode.logicmode != 3)
{
s_blendMode = 0;
D3D::gfxstate->SetAlphaBlendEnable(true);
D3D::gfxstate->SetBlendOp(d3dLogicOps[bpmem.blendmode.logicmode]);
D3D::gfxstate->SetSrcBlend(d3dLogicOpSrcFactors[bpmem.blendmode.logicmode]);
D3D::gfxstate->SetDestBlend(d3dLogicOpDestFactors[bpmem.blendmode.logicmode]);
gx_state.blenddc.RenderTarget[0].BlendEnable = true;
SetBlendOp(d3dLogicOps[bpmem.blendmode.logicmode]);
SetSrcBlend(d3dLogicOpSrcFactors[bpmem.blendmode.logicmode]);
SetDestBlend(d3dLogicOpDestFactors[bpmem.blendmode.logicmode]);
}
else
{

View File

@ -25,7 +25,7 @@ public:
void SetInterlacingMode();
// TODO: Fix confusing names (see ResetAPIState and RestoreAPIState)
void ApplyState();
void ApplyState(bool bUseDstAlpha);
void RestoreState();
void RenderText(const char* pstr, int left, int top, u32 color);

View File

@ -212,8 +212,6 @@ void VertexManager::vFlush()
bool useDstAlpha = bpmem.dstalpha.enable && bpmem.blendmode.alphaupdate &&
bpmem.zcontrol.pixel_format == PIXELFMT_RGBA6_Z24;
D3D::gfxstate->SetDstAlpha(useDstAlpha);
if (!PixelShaderCache::SetShader(
useDstAlpha ? DSTALPHA_DUAL_SOURCE_BLEND : DSTALPHA_NONE,
g_nativeVertexFmt->m_components))
@ -231,14 +229,15 @@ void VertexManager::vFlush()
g_nativeVertexFmt->SetupVertexPointers();
D3D::gfxstate->ApplyState();
g_renderer->ApplyState();
g_renderer->ApplyState(useDstAlpha);
LoadBuffers();
Draw(stride);
g_renderer->RestoreState();
D3D::gfxstate->Reset();
GFX_DEBUGGER_PAUSE_AT(NEXT_FLUSH, true);
g_renderer->RestoreState();
D3D::gfxstate->Reset();
shader_fail:
ResetBuffer();
}

View File

@ -1261,6 +1261,25 @@ void Renderer::Swap(u32 xfbAddr, FieldType field, u32 fbWidth, u32 fbHeight,cons
XFBWrited = false;
}
void Renderer::ApplyState(bool bUseDstAlpha)
{
if (bUseDstAlpha)
{
D3D::ChangeRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA);
D3D::ChangeRenderState(D3DRS_ALPHABLENDENABLE, false);
}
}
void Renderer::RestoreState()
{
D3D::RefreshRenderState(D3DRS_COLORWRITEENABLE);
D3D::RefreshRenderState(D3DRS_ALPHABLENDENABLE);
// TODO: Enable this code. Caused glitches for me however (neobrain)
// for (unsigned int i = 0; i < 8; ++i)
// D3D::dev->SetTexture(i, NULL);
}
// ALWAYS call RestoreAPIState for each ResetAPIState call you're doing
void Renderer::ResetAPIState()
{

View File

@ -24,9 +24,8 @@ public:
void SetSamplerState(int stage,int texindex);
void SetInterlacingMode();
// No need to implement these in D3D9
void ApplyState() {}
void RestoreState() {}
void ApplyState(bool bUseDstAlpha);
void RestoreState();
void RenderText(const char* pstr, int left, int top, u32 color);

View File

@ -182,13 +182,9 @@ void VertexManager::vFlush()
goto shader_fail;
}
// update alpha only
D3D::ChangeRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA);
D3D::ChangeRenderState(D3DRS_ALPHABLENDENABLE, false);
g_renderer->ApplyState(true);
Draw(stride);
D3D::RefreshRenderState(D3DRS_COLORWRITEENABLE);
D3D::RefreshRenderState(D3DRS_ALPHABLENDENABLE);
g_renderer->RestoreState();
}
GFX_DEBUGGER_PAUSE_AT(NEXT_FLUSH, true);

View File

@ -24,8 +24,8 @@ public:
void SetSamplerState(int stage,int texindex);
void SetInterlacingMode();
// No need to implement these in OGL
void ApplyState() {}
// TODO: Implement and use these
void ApplyState(bool bUseDstAlpha) {}
void RestoreState() {}
void RenderText(const char* pstr, int left, int top, u32 color);