refactor: Refactor OpenGL backend to use OpenGLEnums
Use centralized OpenGLEnums.h for enum conversion: - Remove local ToGL* functions from OpenGLCommandList - Replace with ToOpenGL() and ToOpenGLClearBuffer() from OpenGLEnums - Simplify OpenGLTexture, OpenGLBuffer, OpenGLSampler, etc.
This commit is contained in:
@@ -5,27 +5,13 @@
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLFramebuffer.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLRenderPass.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLDescriptorSet.h"
|
||||
#include "XCEngine/RHI/OpenGL/OpenGLEnums.h"
|
||||
#include <glad/glad.h>
|
||||
|
||||
namespace XCEngine {
|
||||
namespace RHI {
|
||||
|
||||
static unsigned int ToGLPrimitiveType(PrimitiveType type) {
|
||||
switch (type) {
|
||||
case PrimitiveType::Points: return GL_POINTS;
|
||||
case PrimitiveType::Lines: return GL_LINES;
|
||||
case PrimitiveType::LineStrip: return GL_LINE_STRIP;
|
||||
case PrimitiveType::Triangles: return GL_TRIANGLES;
|
||||
case PrimitiveType::TriangleStrip: return GL_TRIANGLE_STRIP;
|
||||
case PrimitiveType::TriangleFan: return GL_TRIANGLE_FAN;
|
||||
case PrimitiveType::LineListAdj: return GL_LINES_ADJACENCY;
|
||||
case PrimitiveType::TriangleListAdj: return GL_TRIANGLES_ADJACENCY;
|
||||
case PrimitiveType::Patch: return GL_PATCHES;
|
||||
default: return GL_TRIANGLES;
|
||||
}
|
||||
}
|
||||
|
||||
OpenGLCommandList::OpenGLCommandList()
|
||||
OpenGLCommandList::OpenGLCommandList()
|
||||
: m_primitiveType(GL_TRIANGLES)
|
||||
, m_currentVAO(0)
|
||||
, m_currentProgram(0)
|
||||
@@ -37,11 +23,7 @@ OpenGLCommandList::~OpenGLCommandList() {
|
||||
|
||||
void OpenGLCommandList::Clear(float r, float g, float b, float a, unsigned int buffers) {
|
||||
glClearColor(r, g, b, a);
|
||||
unsigned int glBuffers = 0;
|
||||
if (buffers & 1) glBuffers |= GL_COLOR_BUFFER_BIT;
|
||||
if (buffers & 2) glBuffers |= GL_DEPTH_BUFFER_BIT;
|
||||
if (buffers & 4) glBuffers |= GL_STENCIL_BUFFER_BIT;
|
||||
glClear(glBuffers);
|
||||
glClear(ToOpenGLClearBuffer(buffers));
|
||||
}
|
||||
|
||||
void OpenGLCommandList::ClearColor(float r, float g, float b, float a) {
|
||||
@@ -206,35 +188,35 @@ void OpenGLCommandList::SetPolygonOffset(float factor, float units) {
|
||||
}
|
||||
|
||||
void OpenGLCommandList::SetPrimitiveType(PrimitiveType type) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
}
|
||||
|
||||
void OpenGLCommandList::Draw(PrimitiveType type, unsigned int vertexCount, unsigned int startVertex) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glDrawArrays(m_primitiveType, startVertex, vertexCount);
|
||||
}
|
||||
|
||||
void OpenGLCommandList::DrawInstanced(PrimitiveType type, unsigned int vertexCount, unsigned int instanceCount, unsigned int startVertex, unsigned int startInstance) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glDrawArraysInstanced(m_primitiveType, startVertex, vertexCount, instanceCount);
|
||||
(void)startInstance;
|
||||
}
|
||||
|
||||
void OpenGLCommandList::DrawIndexed(PrimitiveType type, unsigned int indexCount, unsigned int startIndex, int baseVertex) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glDrawElements(m_primitiveType, indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(unsigned int)));
|
||||
(void)baseVertex;
|
||||
}
|
||||
|
||||
void OpenGLCommandList::DrawIndexedInstanced(PrimitiveType type, unsigned int indexCount, unsigned int instanceCount, unsigned int startIndex, int baseVertex, unsigned int startInstance) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glDrawElementsInstanced(m_primitiveType, indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(unsigned int)), instanceCount);
|
||||
(void)baseVertex;
|
||||
(void)startInstance;
|
||||
}
|
||||
|
||||
void OpenGLCommandList::DrawIndirect(PrimitiveType type, unsigned int buffer, size_t offset, unsigned int drawCount, unsigned int stride) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer);
|
||||
glDrawArraysIndirect(m_primitiveType, (void*)offset);
|
||||
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
|
||||
@@ -243,7 +225,7 @@ void OpenGLCommandList::DrawIndirect(PrimitiveType type, unsigned int buffer, si
|
||||
}
|
||||
|
||||
void OpenGLCommandList::DrawIndexedIndirect(PrimitiveType type, unsigned int buffer, size_t offset, unsigned int drawCount, unsigned int stride) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, buffer);
|
||||
glDrawElementsIndirect(m_primitiveType, GL_UNSIGNED_INT, (void*)offset);
|
||||
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
|
||||
@@ -252,12 +234,12 @@ void OpenGLCommandList::DrawIndexedIndirect(PrimitiveType type, unsigned int buf
|
||||
}
|
||||
|
||||
void OpenGLCommandList::MultiDrawArrays(PrimitiveType type, const int* first, const int* count, unsigned int drawCount) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glMultiDrawArrays(m_primitiveType, first, count, drawCount);
|
||||
}
|
||||
|
||||
void OpenGLCommandList::MultiDrawElements(PrimitiveType type, const int* count, unsigned int type_, const void* const* indices, unsigned int drawCount) {
|
||||
m_primitiveType = ToGLPrimitiveType(type);
|
||||
m_primitiveType = ToOpenGL(type);
|
||||
glMultiDrawElements(m_primitiveType, count, type_, indices, drawCount);
|
||||
}
|
||||
|
||||
@@ -483,25 +465,25 @@ void OpenGLCommandList::BeginRenderPass(RHIRenderPass* renderPass, RHIFramebuffe
|
||||
(void)renderArea;
|
||||
(void)clearValueCount;
|
||||
(void)clearValues;
|
||||
|
||||
|
||||
if (!framebuffer) return;
|
||||
|
||||
|
||||
OpenGLFramebuffer* glFramebuffer = static_cast<OpenGLFramebuffer*>(framebuffer);
|
||||
glFramebuffer->Bind();
|
||||
|
||||
|
||||
if (!renderPass) return;
|
||||
|
||||
|
||||
OpenGLRenderPass* glRenderPass = static_cast<OpenGLRenderPass*>(renderPass);
|
||||
if (glRenderPass) {
|
||||
const AttachmentDesc* colorAttachments = glRenderPass->GetColorAttachments();
|
||||
uint32_t colorCount = glRenderPass->GetColorAttachmentCount();
|
||||
|
||||
|
||||
for (uint32_t i = 0; i < colorCount && i < clearValueCount; ++i) {
|
||||
if (colorAttachments[i].loadOp == LoadAction::Clear) {
|
||||
glClearBufferfv(GL_COLOR, i, &clearValues[i].color.r);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const AttachmentDesc* dsAttachment = glRenderPass->GetDepthStencilAttachment();
|
||||
if (dsAttachment && clearValueCount > colorCount) {
|
||||
if (dsAttachment->loadOp == LoadAction::Clear) {
|
||||
@@ -566,7 +548,7 @@ void OpenGLCommandList::SetRenderTargets(uint32_t count, RHIResourceView** rende
|
||||
|
||||
std::vector<GLenum> drawBuffers(count);
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
|
||||
drawBuffers[i] = ToOpenGLColorAttachment(i);
|
||||
}
|
||||
glDrawBuffers(count, drawBuffers.data());
|
||||
|
||||
@@ -627,7 +609,7 @@ void OpenGLCommandList::SetGraphicsDescriptorSets(
|
||||
RHIPipelineLayout* pipelineLayout) {
|
||||
(void)firstSet;
|
||||
(void)pipelineLayout;
|
||||
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
if (descriptorSets[i] != nullptr) {
|
||||
OpenGLDescriptorSet* glSet = static_cast<OpenGLDescriptorSet*>(descriptorSets[i]);
|
||||
@@ -643,7 +625,7 @@ void OpenGLCommandList::SetComputeDescriptorSets(
|
||||
RHIPipelineLayout* pipelineLayout) {
|
||||
(void)firstSet;
|
||||
(void)pipelineLayout;
|
||||
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
if (descriptorSets[i] != nullptr) {
|
||||
OpenGLDescriptorSet* glSet = static_cast<OpenGLDescriptorSet*>(descriptorSets[i]);
|
||||
@@ -657,7 +639,7 @@ void OpenGLCommandList::SetVertexBuffers(uint32_t startSlot, uint32_t count, RHI
|
||||
if (!buffers[i]) continue;
|
||||
OpenGLResourceView* view = static_cast<OpenGLResourceView*>(buffers[i]);
|
||||
if (!view->IsValid()) continue;
|
||||
|
||||
|
||||
GLuint glBuffer = view->GetBuffer();
|
||||
glBindBuffer(GL_ARRAY_BUFFER, glBuffer);
|
||||
glEnableVertexAttribArray(startSlot + i);
|
||||
@@ -700,86 +682,8 @@ void OpenGLCommandList::DrawIndexed(uint32_t indexCount, uint32_t instanceCount,
|
||||
reinterpret_cast<const void*>(startIndex * sizeof(GLuint)), static_cast<GLsizei>(instanceCount));
|
||||
}
|
||||
|
||||
static unsigned int ToGLPrimitiveTopology(PrimitiveTopology topology) {
|
||||
switch (topology) {
|
||||
case PrimitiveTopology::PointList: return GL_POINTS;
|
||||
case PrimitiveTopology::LineList: return GL_LINES;
|
||||
case PrimitiveTopology::LineStrip: return GL_LINE_STRIP;
|
||||
case PrimitiveTopology::TriangleList: return GL_TRIANGLES;
|
||||
case PrimitiveTopology::TriangleStrip: return GL_TRIANGLE_STRIP;
|
||||
case PrimitiveTopology::LineListAdj: return GL_LINES_ADJACENCY;
|
||||
case PrimitiveTopology::LineStripAdj: return GL_LINE_STRIP_ADJACENCY;
|
||||
case PrimitiveTopology::TriangleListAdj: return GL_TRIANGLES_ADJACENCY;
|
||||
case PrimitiveTopology::TriangleStripAdj: return GL_TRIANGLE_STRIP_ADJACENCY;
|
||||
case PrimitiveTopology::PatchList: return GL_PATCHES;
|
||||
default: return GL_TRIANGLES;
|
||||
}
|
||||
}
|
||||
|
||||
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 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::IncrSat: return GL_INCR;
|
||||
case StencilOp::DecrSat: return GL_DECR;
|
||||
case StencilOp::Invert: return GL_INVERT;
|
||||
case StencilOp::Incr: return GL_INCR_WRAP;
|
||||
case StencilOp::Decr: return GL_DECR_WRAP;
|
||||
default: return GL_KEEP;
|
||||
}
|
||||
}
|
||||
|
||||
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::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::DstColor: return GL_DST_COLOR;
|
||||
case BlendFactor::InvDstColor: return GL_ONE_MINUS_DST_COLOR;
|
||||
case BlendFactor::SrcAlphaSat: return GL_SRC_ALPHA_SATURATE;
|
||||
case BlendFactor::BlendFactor: return GL_CONSTANT_COLOR;
|
||||
case BlendFactor::InvBlendFactor: return GL_ONE_MINUS_CONSTANT_COLOR;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGLCommandList::SetPrimitiveTopology(PrimitiveTopology topology) {
|
||||
m_primitiveType = ToGLPrimitiveTopology(topology);
|
||||
m_primitiveType = ToOpenGL(topology);
|
||||
}
|
||||
|
||||
void OpenGLCommandList::SetStencilRef(uint8_t ref) {
|
||||
|
||||
Reference in New Issue
Block a user