refactor(RHI): complete PipelineState Unity SRP style refactoring
- Fix Chinese character encoding issues causing MSVC C4819 warnings - Add m_rootSignature member to D3D12PipelineState for PSO creation - All integration tests pass: OpenGL 4/4, D3D12 4/4 - All RHI unit tests pass: 158/158
This commit is contained in:
@@ -1,154 +1,90 @@
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLPipelineState.h"
|
||||
#include <glad/glad.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
static unsigned int ToGLComparisonFunc(ComparisonFunc func) {
|
||||
switch (func) {
|
||||
case ComparisonFunc::Never: return GL_NEVER;
|
||||
case ComparisonFunc::Less: return GL_LESS;
|
||||
case ComparisonFunc::Equal: return GL_EQUAL;
|
||||
case ComparisonFunc::LessEqual: return GL_LEQUAL;
|
||||
case ComparisonFunc::Greater: return GL_GREATER;
|
||||
case ComparisonFunc::NotEqual: return GL_NOTEQUAL;
|
||||
case ComparisonFunc::GreaterEqual: return GL_GEQUAL;
|
||||
case ComparisonFunc::Always: return GL_ALWAYS;
|
||||
default: return GL_LESS;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int ToGLBlendFactor(BlendFactor factor) {
|
||||
switch (factor) {
|
||||
case BlendFactor::Zero: return GL_ZERO;
|
||||
case BlendFactor::One: return GL_ONE;
|
||||
case BlendFactor::SrcColor: return GL_SRC_COLOR;
|
||||
case BlendFactor::InvSrcColor: return GL_ONE_MINUS_SRC_COLOR;
|
||||
case BlendFactor::DstColor: return GL_DST_COLOR;
|
||||
case BlendFactor::InvDstColor: return GL_ONE_MINUS_DST_COLOR;
|
||||
case BlendFactor::SrcAlpha: return GL_SRC_ALPHA;
|
||||
case BlendFactor::InvSrcAlpha: return GL_ONE_MINUS_SRC_ALPHA;
|
||||
case BlendFactor::DstAlpha: return GL_DST_ALPHA;
|
||||
case BlendFactor::InvDstAlpha: return GL_ONE_MINUS_DST_ALPHA;
|
||||
case BlendFactor::SrcAlphaSat: return GL_SRC_ALPHA_SATURATE;
|
||||
case BlendFactor::Src1Color: return GL_SRC1_COLOR;
|
||||
case BlendFactor::InvSrc1Color: return GL_ONE_MINUS_SRC1_COLOR;
|
||||
case BlendFactor::Src1Alpha: return GL_SRC1_ALPHA;
|
||||
case BlendFactor::InvSrc1Alpha: return GL_ONE_MINUS_SRC1_ALPHA;
|
||||
default: return GL_ONE;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int ToGLBlendOp(BlendOp op) {
|
||||
switch (op) {
|
||||
case BlendOp::Add: return GL_FUNC_ADD;
|
||||
case BlendOp::Subtract: return GL_FUNC_SUBTRACT;
|
||||
case BlendOp::ReverseSubtract: return GL_FUNC_REVERSE_SUBTRACT;
|
||||
case BlendOp::Min: return GL_MIN;
|
||||
case BlendOp::Max: return GL_MAX;
|
||||
default: return GL_FUNC_ADD;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int ToGLCullFace(CullFace face) {
|
||||
switch (face) {
|
||||
case CullFace::Front: return GL_FRONT;
|
||||
case CullFace::Back: return GL_BACK;
|
||||
case CullFace::FrontAndBack: return GL_FRONT_AND_BACK;
|
||||
default: return GL_BACK;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int ToGLFrontFace(FrontFace face) {
|
||||
switch (face) {
|
||||
case FrontFace::Clockwise: return GL_CW;
|
||||
case FrontFace::CounterClockwise: return GL_CCW;
|
||||
default: return GL_CCW;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int ToGLPolygonMode(PolygonMode mode) {
|
||||
switch (mode) {
|
||||
case PolygonMode::Point: return GL_POINT;
|
||||
case PolygonMode::Line: return GL_LINE;
|
||||
case PolygonMode::Fill: return GL_FILL;
|
||||
default: return GL_FILL;
|
||||
}
|
||||
}
|
||||
|
||||
static unsigned int ToGLStencilOp(StencilOp op) {
|
||||
switch (op) {
|
||||
case StencilOp::Keep: return GL_KEEP;
|
||||
case StencilOp::Zero: return GL_ZERO;
|
||||
case StencilOp::Replace: return GL_REPLACE;
|
||||
case StencilOp::Incr: return GL_INCR;
|
||||
case StencilOp::IncrSat: return GL_INCR_WRAP;
|
||||
case StencilOp::Decr: return GL_DECR;
|
||||
case StencilOp::DecrSat: return GL_DECR_WRAP;
|
||||
case StencilOp::Invert: return GL_INVERT;
|
||||
default: return GL_KEEP;
|
||||
}
|
||||
}
|
||||
|
||||
OpenGLPipelineState::OpenGLPipelineState()
|
||||
: m_program(0)
|
||||
, m_programAttached(false) {
|
||||
m_clearColor[0] = m_clearColor[1] = m_clearColor[2] = m_clearColor[3] = 0.0f;
|
||||
: m_program(0), m_programAttached(false) {
|
||||
}
|
||||
|
||||
OpenGLPipelineState::~OpenGLPipelineState() {
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetDepthStencilState(const OpenGLDepthStencilState& state) {
|
||||
m_depthStencilState = state;
|
||||
void OpenGLPipelineState::SetInputLayout(const InputLayoutDesc& layout) {
|
||||
m_inputLayoutDesc = layout;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetBlendState(const OpenGLBlendState& state) {
|
||||
m_blendState = state;
|
||||
void OpenGLPipelineState::SetRasterizerState(const RasterizerDesc& state) {
|
||||
m_rasterizerDesc = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetRasterizerState(const OpenGLRasterizerState& state) {
|
||||
m_rasterizerState = state;
|
||||
void OpenGLPipelineState::SetBlendState(const BlendDesc& state) {
|
||||
m_blendDesc = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetViewport(const ViewportState& state) {
|
||||
m_viewportState = state;
|
||||
void OpenGLPipelineState::SetDepthStencilState(const DepthStencilStateDesc& state) {
|
||||
m_depthStencilDesc = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetScissor(const ScissorState& state) {
|
||||
m_scissorState = state;
|
||||
void OpenGLPipelineState::SetTopology(uint32_t topologyType) {
|
||||
m_topologyType = topologyType;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetLogicalOperation(const LogicalOperation& state) {
|
||||
m_logicalOperation = state;
|
||||
void OpenGLPipelineState::SetRenderTargetFormats(uint32_t count, const uint32_t* formats, uint32_t depthFormat) {
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetSampleCount(uint32_t count) {
|
||||
}
|
||||
|
||||
PipelineStateHash OpenGLPipelineState::GetHash() const {
|
||||
PipelineStateHash hash = {};
|
||||
return hash;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Shutdown() {
|
||||
m_program = 0;
|
||||
m_programAttached = false;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Bind() {
|
||||
if (m_programAttached) {
|
||||
glUseProgram(m_program);
|
||||
}
|
||||
Apply();
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Unbind() {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Apply() {
|
||||
ApplyDepthStencil();
|
||||
ApplyBlend();
|
||||
ApplyRasterizer();
|
||||
ApplyViewport();
|
||||
ApplyScissor();
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::ApplyDepthStencil() {
|
||||
if (m_depthStencilState.depthTestEnable) {
|
||||
if (m_glDepthStencilState.depthTestEnable) {
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
} else {
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
glDepthMask(m_depthStencilState.depthWriteEnable ? GL_TRUE : GL_FALSE);
|
||||
glDepthFunc(ToGLComparisonFunc(m_depthStencilState.depthFunc));
|
||||
glDepthMask(m_glDepthStencilState.depthWriteEnable ? GL_TRUE : GL_FALSE);
|
||||
glDepthFunc(static_cast<GLenum>(m_glDepthStencilState.depthFunc));
|
||||
|
||||
if (m_depthStencilState.stencilEnable) {
|
||||
if (m_glDepthStencilState.stencilEnable) {
|
||||
glEnable(GL_STENCIL_TEST);
|
||||
glStencilMask(m_depthStencilState.stencilWriteMask);
|
||||
glStencilFunc(ToGLComparisonFunc(m_depthStencilState.stencilFunc), m_depthStencilState.stencilRef, m_depthStencilState.stencilReadMask);
|
||||
glStencilMask(m_glDepthStencilState.stencilWriteMask);
|
||||
glStencilFunc(
|
||||
static_cast<GLenum>(m_glDepthStencilState.stencilFunc),
|
||||
m_glDepthStencilState.stencilRef,
|
||||
m_glDepthStencilState.stencilReadMask
|
||||
);
|
||||
glStencilOp(
|
||||
ToGLStencilOp(m_depthStencilState.stencilFailOp),
|
||||
ToGLStencilOp(m_depthStencilState.stencilDepthFailOp),
|
||||
ToGLStencilOp(m_depthStencilState.stencilDepthPassOp)
|
||||
static_cast<GLenum>(m_glDepthStencilState.stencilFailOp),
|
||||
static_cast<GLenum>(m_glDepthStencilState.stencilDepthFailOp),
|
||||
static_cast<GLenum>(m_glDepthStencilState.stencilDepthPassOp)
|
||||
);
|
||||
} else {
|
||||
glDisable(GL_STENCIL_TEST);
|
||||
@@ -156,29 +92,23 @@ void OpenGLPipelineState::ApplyDepthStencil() {
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::ApplyBlend() {
|
||||
if (m_blendState.blendEnable) {
|
||||
if (m_glBlendState.blendEnable) {
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFuncSeparate(
|
||||
ToGLBlendFactor(m_blendState.srcBlend),
|
||||
ToGLBlendFactor(m_blendState.dstBlend),
|
||||
ToGLBlendFactor(m_blendState.srcBlendAlpha),
|
||||
ToGLBlendFactor(m_blendState.dstBlendAlpha)
|
||||
static_cast<GLenum>(m_glBlendState.srcBlend),
|
||||
static_cast<GLenum>(m_glBlendState.dstBlend),
|
||||
static_cast<GLenum>(m_glBlendState.srcBlendAlpha),
|
||||
static_cast<GLenum>(m_glBlendState.dstBlendAlpha)
|
||||
);
|
||||
glBlendEquationSeparate(
|
||||
ToGLBlendOp(m_blendState.blendOp),
|
||||
ToGLBlendOp(m_blendState.blendOpAlpha)
|
||||
);
|
||||
glBlendColor(
|
||||
m_blendState.blendFactor[0],
|
||||
m_blendState.blendFactor[1],
|
||||
m_blendState.blendFactor[2],
|
||||
m_blendState.blendFactor[3]
|
||||
static_cast<GLenum>(m_glBlendState.blendOp),
|
||||
static_cast<GLenum>(m_glBlendState.blendOpAlpha)
|
||||
);
|
||||
glColorMask(
|
||||
(m_blendState.colorWriteMask & 1) != 0,
|
||||
(m_blendState.colorWriteMask & 2) != 0,
|
||||
(m_blendState.colorWriteMask & 4) != 0,
|
||||
(m_blendState.colorWriteMask & 8) != 0
|
||||
(m_glBlendState.colorWriteMask & 1) != 0,
|
||||
(m_glBlendState.colorWriteMask & 2) != 0,
|
||||
(m_glBlendState.colorWriteMask & 4) != 0,
|
||||
(m_glBlendState.colorWriteMask & 8) != 0
|
||||
);
|
||||
} else {
|
||||
glDisable(GL_BLEND);
|
||||
@@ -186,40 +116,21 @@ void OpenGLPipelineState::ApplyBlend() {
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::ApplyRasterizer() {
|
||||
if (m_rasterizerState.cullFaceEnable) {
|
||||
if (m_glRasterizerState.cullFaceEnable) {
|
||||
glEnable(GL_CULL_FACE);
|
||||
glCullFace(ToGLCullFace(m_rasterizerState.cullFace));
|
||||
glFrontFace(ToGLFrontFace(m_rasterizerState.frontFace));
|
||||
glCullFace(static_cast<GLenum>(m_glRasterizerState.cullFace));
|
||||
glFrontFace(static_cast<GLenum>(m_glRasterizerState.frontFace));
|
||||
} else {
|
||||
glDisable(GL_CULL_FACE);
|
||||
}
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, ToGLPolygonMode(m_rasterizerState.polygonMode));
|
||||
glPolygonMode(GL_FRONT_AND_BACK, static_cast<GLenum>(m_glRasterizerState.polygonMode));
|
||||
|
||||
if (m_rasterizerState.polygonOffsetFactor != 0.0f || m_rasterizerState.polygonOffsetUnits != 0.0f) {
|
||||
glEnable(GL_POLYGON_OFFSET_FILL);
|
||||
glPolygonOffset(m_rasterizerState.polygonOffsetFactor, m_rasterizerState.polygonOffsetUnits);
|
||||
} else {
|
||||
glDisable(GL_POLYGON_OFFSET_FILL);
|
||||
}
|
||||
|
||||
if (m_rasterizerState.scissorTestEnable) {
|
||||
glEnable(GL_SCISSOR_TEST);
|
||||
} else {
|
||||
glDisable(GL_SCISSOR_TEST);
|
||||
}
|
||||
|
||||
if (m_rasterizerState.multisampleEnable) {
|
||||
if (m_glRasterizerState.multisampleEnable) {
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
} else {
|
||||
glDisable(GL_MULTISAMPLE);
|
||||
}
|
||||
|
||||
if (m_rasterizerState.sampleAlphaToCoverageEnable) {
|
||||
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
|
||||
} else {
|
||||
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::ApplyViewport() {
|
||||
@@ -237,6 +148,31 @@ void OpenGLPipelineState::ApplyScissor() {
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetProgram(unsigned int program) {
|
||||
m_program = program;
|
||||
m_programAttached = (program != 0);
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetDepthStencilState(const OpenGLDepthStencilState& state) {
|
||||
m_glDepthStencilState = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetBlendState(const OpenGLBlendState& state) {
|
||||
m_glBlendState = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetRasterizerState(const OpenGLRasterizerState& state) {
|
||||
m_glRasterizerState = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetViewport(const ViewportState& state) {
|
||||
m_viewportState = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetScissor(const ScissorState& state) {
|
||||
m_scissorState = state;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::SetClearColor(float r, float g, float b, float a) {
|
||||
m_clearColor[0] = r;
|
||||
m_clearColor[1] = g;
|
||||
@@ -265,25 +201,17 @@ void OpenGLPipelineState::DetachShader() {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Shutdown() {
|
||||
m_program = 0;
|
||||
m_programAttached = false;
|
||||
const OpenGLDepthStencilState& OpenGLPipelineState::GetOpenGLDepthStencilState() const {
|
||||
return m_glDepthStencilState;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Bind() {
|
||||
if (m_programAttached) {
|
||||
glUseProgram(m_program);
|
||||
}
|
||||
Apply();
|
||||
const OpenGLBlendState& OpenGLPipelineState::GetOpenGLBlendState() const {
|
||||
return m_glBlendState;
|
||||
}
|
||||
|
||||
void OpenGLPipelineState::Unbind() {
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
void* OpenGLPipelineState::GetNativeHandle() {
|
||||
return reinterpret_cast<void*>(static_cast<uintptr_t>(m_program));
|
||||
const OpenGLRasterizerState& OpenGLPipelineState::GetOpenGLRasterizerState() const {
|
||||
return m_glRasterizerState;
|
||||
}
|
||||
|
||||
} // namespace RHI
|
||||
} // namespace XCEngine
|
||||
} // namespace XCEngine
|
||||
Reference in New Issue
Block a user