Renderer: Move depth state to VideoCommon and seperate from bpmem

This commit is contained in:
Stenzek 2017-04-30 15:54:45 +10:00
parent 4d36f0cc87
commit 2869c570f1
20 changed files with 80 additions and 104 deletions

View File

@ -456,10 +456,9 @@ ID3D11RasterizerState* StateCache::Get(RasterizerState state)
return res;
}
ID3D11DepthStencilState* StateCache::Get(ZMode state)
ID3D11DepthStencilState* StateCache::Get(DepthState state)
{
auto it = m_depth.find(state.hex);
if (it != m_depth.end())
return it->second;
@ -472,6 +471,7 @@ ID3D11DepthStencilState* StateCache::Get(ZMode state)
depthdc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
depthdc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
// Less/greater are swapped due to inverted depth.
const D3D11_COMPARISON_FUNC d3dCmpFuncs[8] = {
D3D11_COMPARISON_NEVER, D3D11_COMPARISON_GREATER, D3D11_COMPARISON_EQUAL,
D3D11_COMPARISON_GREATER_EQUAL, D3D11_COMPARISON_LESS, D3D11_COMPARISON_NOT_EQUAL,

View File

@ -49,7 +49,7 @@ public:
ID3D11SamplerState* Get(SamplerState state);
ID3D11BlendState* Get(BlendingState state);
ID3D11RasterizerState* Get(RasterizerState state);
ID3D11DepthStencilState* Get(ZMode state);
ID3D11DepthStencilState* Get(DepthState state);
// Release all cached states and clear hash tables.
void Clear();

View File

@ -60,7 +60,7 @@ struct GXPipelineState
{
std::array<SamplerState, 8> samplers;
BlendingState blend;
ZMode zmode;
DepthState zmode;
RasterizerState raster;
};
@ -916,9 +916,9 @@ void Renderer::SetGenerationMode()
s_gx_state.raster.cull_mode = d3d_cull_modes[bpmem.genMode.cullmode];
}
void Renderer::SetDepthMode()
void Renderer::SetDepthState(const DepthState& state)
{
s_gx_state.zmode.hex = bpmem.zmode.hex;
s_gx_state.zmode.hex = state.hex;
}
void Renderer::SetSamplerState(int stage, int texindex, bool custom_tex)

View File

@ -22,7 +22,7 @@ public:
void SetBlendingState(const BlendingState& state) override;
void SetScissorRect(const EFBRectangle& rc) override;
void SetGenerationMode() override;
void SetDepthMode() override;
void SetDepthState(const DepthState& state) override;
void SetSamplerState(int stage, int texindex, bool custom_tex) override;
void SetInterlacingMode() override;
void SetViewport() override;

View File

@ -1787,7 +1787,7 @@ void Renderer::RestoreAPIState()
}
SetGenerationMode();
BPFunctions::SetScissor();
SetDepthMode();
BPFunctions::SetDepthMode();
BPFunctions::SetBlendMode();
SetViewport();
@ -1813,16 +1813,16 @@ void Renderer::SetGenerationMode()
}
}
void Renderer::SetDepthMode()
void Renderer::SetDepthState(const DepthState& state)
{
const GLenum glCmpFuncs[8] = {GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL,
GL_GREATER, GL_NOTEQUAL, GL_GEQUAL, GL_ALWAYS};
if (bpmem.zmode.testenable)
if (state.testenable)
{
glEnable(GL_DEPTH_TEST);
glDepthMask(bpmem.zmode.updateenable ? GL_TRUE : GL_FALSE);
glDepthFunc(glCmpFuncs[bpmem.zmode.func]);
glDepthMask(state.updateenable ? GL_TRUE : GL_FALSE);
glDepthFunc(glCmpFuncs[state.func]);
}
else
{

View File

@ -80,7 +80,7 @@ public:
void SetBlendingState(const BlendingState& state) override;
void SetScissorRect(const EFBRectangle& rc) override;
void SetGenerationMode() override;
void SetDepthMode() override;
void SetDepthState(const DepthState& state) override;
void SetSamplerState(int stage, int texindex, bool custom_tex) override;
void SetInterlacingMode() override;
void SetViewport() override;

View File

@ -135,16 +135,6 @@ union RasterizationState
u32 bits;
};
// Depth state info
union DepthStencilState
{
BitField<0, 1, VkBool32> test_enable;
BitField<1, 1, VkBool32> write_enable;
BitField<2, 3, VkCompareOp> compare_op;
u32 bits;
};
// Sampler info
union SamplerState
{

View File

@ -1164,16 +1164,16 @@ void FramebufferManager::DrawPokeVertices(const EFBPokeVertex* vertices, size_t
pipeline_info.render_pass = m_efb_load_render_pass;
pipeline_info.rasterization_state.bits = Util::GetNoCullRasterizationState().bits;
pipeline_info.rasterization_state.samples = m_efb_samples;
pipeline_info.depth_stencil_state.bits = Util::GetNoDepthTestingDepthStencilState().bits;
pipeline_info.depth_state.hex = Util::GetNoDepthTestingDepthStencilState().hex;
pipeline_info.blend_state.hex = Util::GetNoBlendingBlendState().hex;
pipeline_info.blend_state.colorupdate = write_color;
pipeline_info.blend_state.alphaupdate = write_color;
pipeline_info.primitive_topology = m_poke_primitive_topology;
if (write_depth)
{
pipeline_info.depth_stencil_state.test_enable = VK_TRUE;
pipeline_info.depth_stencil_state.write_enable = VK_TRUE;
pipeline_info.depth_stencil_state.compare_op = VK_COMPARE_OP_ALWAYS;
pipeline_info.depth_state.testenable = true;
pipeline_info.depth_state.updateenable = true;
pipeline_info.depth_state.func = ZMode::ALWAYS;
}
VkPipeline pipeline = g_shader_cache->GetPipeline(pipeline_info);

View File

@ -459,10 +459,10 @@ void Renderer::ClearScreen(const EFBRectangle& rc, bool color_enable, bool alpha
blend_state.colorupdate = color_enable;
blend_state.alphaupdate = alpha_enable;
DepthStencilState depth_state = Util::GetNoDepthTestingDepthStencilState();
depth_state.test_enable = z_enable ? VK_TRUE : VK_FALSE;
depth_state.write_enable = z_enable ? VK_TRUE : VK_FALSE;
depth_state.compare_op = VK_COMPARE_OP_ALWAYS;
DepthState depth_state = Util::GetNoDepthTestingDepthStencilState();
depth_state.testenable = z_enable;
depth_state.updateenable = z_enable;
depth_state.func = ZMode::ALWAYS;
RasterizationState rs_state = Util::GetNoCullRasterizationState();
rs_state.per_sample_shading = g_ActiveConfig.bSSAA ? VK_TRUE : VK_FALSE;
@ -476,7 +476,7 @@ void Renderer::ClearScreen(const EFBRectangle& rc, bool color_enable, bool alpha
g_shader_cache->GetPassthroughGeometryShader(), m_clear_fragment_shader);
draw.SetRasterizationState(rs_state);
draw.SetDepthStencilState(depth_state);
draw.SetDepthState(depth_state);
draw.SetBlendState(blend_state);
draw.DrawColoredQuad(target_rc.left, target_rc.top, target_rc.GetWidth(), target_rc.GetHeight(),
@ -1303,45 +1303,9 @@ void Renderer::SetGenerationMode()
StateTracker::GetInstance()->SetRasterizationState(new_rs_state);
}
void Renderer::SetDepthMode()
void Renderer::SetDepthState(const DepthState& state)
{
DepthStencilState new_ds_state = {};
new_ds_state.test_enable = bpmem.zmode.testenable ? VK_TRUE : VK_FALSE;
new_ds_state.write_enable = bpmem.zmode.updateenable ? VK_TRUE : VK_FALSE;
// Inverted depth, hence these are swapped
switch (bpmem.zmode.func)
{
case ZMode::NEVER:
new_ds_state.compare_op = VK_COMPARE_OP_NEVER;
break;
case ZMode::LESS:
new_ds_state.compare_op = VK_COMPARE_OP_GREATER;
break;
case ZMode::EQUAL:
new_ds_state.compare_op = VK_COMPARE_OP_EQUAL;
break;
case ZMode::LEQUAL:
new_ds_state.compare_op = VK_COMPARE_OP_GREATER_OR_EQUAL;
break;
case ZMode::GREATER:
new_ds_state.compare_op = VK_COMPARE_OP_LESS;
break;
case ZMode::NEQUAL:
new_ds_state.compare_op = VK_COMPARE_OP_NOT_EQUAL;
break;
case ZMode::GEQUAL:
new_ds_state.compare_op = VK_COMPARE_OP_LESS_OR_EQUAL;
break;
case ZMode::ALWAYS:
new_ds_state.compare_op = VK_COMPARE_OP_ALWAYS;
break;
default:
new_ds_state.compare_op = VK_COMPARE_OP_ALWAYS;
break;
}
StateTracker::GetInstance()->SetDepthStencilState(new_ds_state);
StateTracker::GetInstance()->SetDepthState(state);
}
void Renderer::SetBlendingState(const BlendingState& state)

View File

@ -59,7 +59,7 @@ public:
void SetBlendingState(const BlendingState& state) override;
void SetScissorRect(const EFBRectangle& rc) override;
void SetGenerationMode() override;
void SetDepthMode() override;
void SetDepthState(const DepthState& state) override;
void SetSamplerState(int stage, int texindex, bool custom_tex) override;
void SetInterlacingMode() override;
void SetViewport() override;

View File

@ -122,16 +122,21 @@ GetVulkanMultisampleState(const RasterizationState& rs_state)
};
}
static VkPipelineDepthStencilStateCreateInfo
GetVulkanDepthStencilState(const DepthStencilState& state)
static VkPipelineDepthStencilStateCreateInfo GetVulkanDepthStencilState(const DepthState& state)
{
// Less/greater are swapped due to inverted depth.
static constexpr std::array<VkCompareOp, 8> funcs = {
{VK_COMPARE_OP_NEVER, VK_COMPARE_OP_GREATER, VK_COMPARE_OP_EQUAL,
VK_COMPARE_OP_GREATER_OR_EQUAL, VK_COMPARE_OP_LESS, VK_COMPARE_OP_NOT_EQUAL,
VK_COMPARE_OP_LESS_OR_EQUAL, VK_COMPARE_OP_ALWAYS}};
return {
VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO, // VkStructureType sType
nullptr, // const void* pNext
0, // VkPipelineDepthStencilStateCreateFlags flags
state.test_enable, // VkBool32 depthTestEnable
state.write_enable, // VkBool32 depthWriteEnable
state.compare_op, // VkCompareOp depthCompareOp
state.testenable, // VkBool32 depthTestEnable
state.updateenable, // VkBool32 depthWriteEnable
funcs[state.func], // VkCompareOp depthCompareOp
VK_FALSE, // VkBool32 depthBoundsTestEnable
VK_FALSE, // VkBool32 stencilTestEnable
{}, // VkStencilOpState front

View File

@ -49,7 +49,7 @@ struct PipelineInfo
VkRenderPass render_pass;
BlendingState blend_state;
RasterizationState rasterization_state;
DepthStencilState depth_stencil_state;
DepthState depth_state;
VkPrimitiveTopology primitive_topology;
};

View File

@ -58,9 +58,9 @@ bool StateTracker::Initialize()
m_pipeline_state.rasterization_state.cull_mode = VK_CULL_MODE_NONE;
m_pipeline_state.rasterization_state.per_sample_shading = VK_FALSE;
m_pipeline_state.rasterization_state.depth_clamp = VK_FALSE;
m_pipeline_state.depth_stencil_state.test_enable = VK_TRUE;
m_pipeline_state.depth_stencil_state.write_enable = VK_TRUE;
m_pipeline_state.depth_stencil_state.compare_op = VK_COMPARE_OP_LESS;
m_pipeline_state.depth_state.testenable = true;
m_pipeline_state.depth_state.updateenable = true;
m_pipeline_state.depth_state.func = ZMode::ALWAYS;
m_pipeline_state.blend_state.hex = 0;
m_pipeline_state.blend_state.blendenable = false;
m_pipeline_state.blend_state.srcfactor = BlendMode::ONE;
@ -167,7 +167,7 @@ void StateTracker::AppendToPipelineUIDCache(const PipelineInfo& info)
SerializedPipelineUID sinfo;
sinfo.blend_state_bits = info.blend_state.hex;
sinfo.rasterizer_state_bits = info.rasterization_state.bits;
sinfo.depth_stencil_state_bits = info.depth_stencil_state.bits;
sinfo.depth_state_bits = info.depth_state.hex;
sinfo.vertex_decl = m_pipeline_state.vertex_format->GetVertexDeclaration();
sinfo.vs_uid = m_vs_uid;
sinfo.gs_uid = m_gs_uid;
@ -212,7 +212,7 @@ bool StateTracker::PrecachePipelineUID(const SerializedPipelineUID& uid)
}
pinfo.render_pass = m_load_render_pass;
pinfo.rasterization_state.bits = uid.rasterizer_state_bits;
pinfo.depth_stencil_state.bits = uid.depth_stencil_state_bits;
pinfo.depth_state.hex = uid.depth_state_bits;
pinfo.blend_state.hex = uid.blend_state_bits;
pinfo.primitive_topology = uid.primitive_topology;
@ -316,12 +316,12 @@ void StateTracker::SetRasterizationState(const RasterizationState& state)
m_dirty_flags |= DIRTY_FLAG_PIPELINE;
}
void StateTracker::SetDepthStencilState(const DepthStencilState& state)
void StateTracker::SetDepthState(const DepthState& state)
{
if (m_pipeline_state.depth_stencil_state.bits == state.bits)
if (m_pipeline_state.depth_state.hex == state.hex)
return;
m_pipeline_state.depth_stencil_state.bits = state.bits;
m_pipeline_state.depth_state.hex = state.hex;
m_dirty_flags |= DIRTY_FLAG_PIPELINE;
}

View File

@ -39,10 +39,7 @@ public:
{
return m_pipeline_state.rasterization_state;
}
const DepthStencilState& GetDepthStencilState() const
{
return m_pipeline_state.depth_stencil_state;
}
const DepthState& GetDepthStencilState() const { return m_pipeline_state.depth_state; }
const BlendingState& GetBlendState() const { return m_pipeline_state.blend_state; }
void SetVertexBuffer(VkBuffer buffer, VkDeviceSize offset);
void SetIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType type);
@ -58,7 +55,7 @@ public:
void DisableBackFaceCulling();
void SetRasterizationState(const RasterizationState& state);
void SetDepthStencilState(const DepthStencilState& state);
void SetDepthState(const DepthState& state);
void SetBlendState(const BlendingState& state);
bool CheckForShaderChanges(u32 gx_primitive_type);
@ -130,7 +127,7 @@ private:
struct SerializedPipelineUID
{
u32 rasterizer_state_bits;
u32 depth_stencil_state_bits;
u32 depth_state_bits;
u32 blend_state_bits;
PortableVertexDeclaration vertex_decl;
VertexShaderUid vs_uid;

View File

@ -195,12 +195,12 @@ RasterizationState GetNoCullRasterizationState()
return state;
}
DepthStencilState GetNoDepthTestingDepthStencilState()
DepthState GetNoDepthTestingDepthStencilState()
{
DepthStencilState state = {};
state.test_enable = VK_FALSE;
state.write_enable = VK_FALSE;
state.compare_op = VK_COMPARE_OP_ALWAYS;
DepthState state = {};
state.testenable = false;
state.updateenable = false;
state.func = ZMode::ALWAYS;
return state;
}
@ -346,7 +346,7 @@ UtilityShaderDraw::UtilityShaderDraw(VkCommandBuffer command_buffer,
m_pipeline_info.gs = geometry_shader;
m_pipeline_info.ps = pixel_shader;
m_pipeline_info.rasterization_state.bits = Util::GetNoCullRasterizationState().bits;
m_pipeline_info.depth_stencil_state.bits = Util::GetNoDepthTestingDepthStencilState().bits;
m_pipeline_info.depth_state.hex = Util::GetNoDepthTestingDepthStencilState().hex;
m_pipeline_info.blend_state.hex = Util::GetNoBlendingBlendState().hex;
m_pipeline_info.primitive_topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
}
@ -450,9 +450,9 @@ void UtilityShaderDraw::SetRasterizationState(const RasterizationState& state)
m_pipeline_info.rasterization_state.bits = state.bits;
}
void UtilityShaderDraw::SetDepthStencilState(const DepthStencilState& state)
void UtilityShaderDraw::SetDepthState(const DepthState& state)
{
m_pipeline_info.depth_stencil_state.bits = state.bits;
m_pipeline_info.depth_state.hex = state.hex;
}
void UtilityShaderDraw::SetBlendState(const BlendingState& state)

View File

@ -39,7 +39,7 @@ VkRect2D ClampRect2D(const VkRect2D& rect, u32 width, u32 height);
VkBlendFactor GetAlphaBlendFactor(VkBlendFactor factor);
RasterizationState GetNoCullRasterizationState();
DepthStencilState GetNoDepthTestingDepthStencilState();
DepthState GetNoDepthTestingDepthStencilState();
BlendingState GetNoBlendingBlendState();
// Combines viewport and scissor updates
@ -151,7 +151,7 @@ public:
void SetPSTexelBuffer(VkBufferView view);
void SetRasterizationState(const RasterizationState& state);
void SetDepthStencilState(const DepthStencilState& state);
void SetDepthState(const DepthState& state);
void SetBlendState(const BlendingState& state);
void BeginRenderPass(VkFramebuffer framebuffer, const VkRect2D& region,

View File

@ -68,12 +68,14 @@ void SetScissor()
void SetDepthMode()
{
g_renderer->SetDepthMode();
DepthState state = {};
state.Generate(bpmem);
g_renderer->SetDepthState(state);
}
void SetBlendMode()
{
BlendingState state;
BlendingState state = {};
state.Generate(bpmem);
g_renderer->SetBlendingState(state);
}

View File

@ -67,7 +67,7 @@ public:
virtual void SetBlendingState(const BlendingState& state) {}
virtual void SetScissorRect(const EFBRectangle& rc) {}
virtual void SetGenerationMode() {}
virtual void SetDepthMode() {}
virtual void SetDepthState(const DepthState& state) {}
virtual void SetSamplerState(int stage, int texindex, bool custom_tex) {}
virtual void SetInterlacingMode() {}
virtual void SetViewport() {}

View File

@ -4,6 +4,13 @@
#include "VideoCommon/RenderState.h"
void DepthState::Generate(const BPMemory& bp)
{
testenable = bp.zmode.testenable.Value();
updateenable = bp.zmode.updateenable.Value();
func = bp.zmode.func.Value();
}
// If the framebuffer format has no alpha channel, it is assumed to
// ONE on blending. As the backends may emulate this framebuffer
// configuration with an alpha channel, we just drop all references

View File

@ -9,6 +9,17 @@
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/BPStructs.h"
union DepthState
{
void Generate(const BPMemory& bp);
BitField<0, 1, u32> testenable;
BitField<1, 1, u32> updateenable;
BitField<2, 3, ZMode::CompareMode> func;
u32 hex;
};
union BlendingState
{
void Generate(const BPMemory& bp);