fix(RHI): 添加 OpenGL 源文件到 CMakeLists 并修复编译错误

- 添加 OpenGL RHI 所有源文件到 engine/CMakeLists.txt
- 修复 OpenGLPipelineState 结构体重定义问题
- 修复 BufferDesc/TextureDesc/ShaderCompileDesc API 不匹配
- 添加 OpenGLShader 缺少的基类纯虚函数实现
- 修复 HashMap 迭代器支持和 ResourceManager API 调用
This commit is contained in:
2026-03-18 03:37:34 +08:00
parent 8344057886
commit 3196261e9b
8 changed files with 90 additions and 31 deletions

View File

@@ -127,6 +127,34 @@ add_library(XCEngine STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/D3D12/D3D12QueryHeap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/D3D12/D3D12QueryHeap.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/D3D12/D3D12UnorderedAccessView.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/D3D12/D3D12UnorderedAccessView.cpp
# OpenGL RHI
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLBuffer.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLTexture.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLSampler.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLShader.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLDevice.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLCommandList.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLCommandQueue.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLFence.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLSwapChain.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLRenderTargetView.h
${CMAKE_CURRENT_SOURCE_DIR}/include/XCEngine/RHI/OpenGL/OpenGLDepthStencilView.h
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLBuffer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLTexture.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLSampler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLShader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLVertexArray.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLDevice.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLCommandList.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLCommandQueue.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLFence.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLPipelineState.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLSwapChain.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLRenderTargetView.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/OpenGL/OpenGLDepthStencilView.cpp
# RHI Factory # RHI Factory
${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/RHIFactory.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/RHIFactory.cpp

View File

@@ -42,6 +42,24 @@ public:
size_t Size() const { return m_size; } size_t Size() const { return m_size; }
bool Empty() const { return m_size == 0; } bool Empty() const { return m_size == 0; }
using Iterator = typename Array<Pair>::Iterator;
using ConstIterator = typename Array<Pair>::ConstIterator;
Iterator begin() { return m_buckets[0].pairs.begin(); }
Iterator end() {
if (m_bucketCount > 0) {
return m_buckets[m_bucketCount - 1].pairs.end();
}
return Iterator(nullptr);
}
ConstIterator begin() const { return m_buckets[0].pairs.begin(); }
ConstIterator end() const {
if (m_bucketCount > 0) {
return m_buckets[m_bucketCount - 1].pairs.end();
}
return ConstIterator(nullptr);
}
void SetAllocator(Memory::IAllocator* allocator) { m_allocator = allocator; } void SetAllocator(Memory::IAllocator* allocator) { m_allocator = allocator; }
private: private:

View File

@@ -43,7 +43,7 @@ enum class PolygonMode {
Fill Fill
}; };
struct DepthStencilState { struct OpenGLDepthStencilState {
bool depthTestEnable = true; bool depthTestEnable = true;
bool depthWriteEnable = true; bool depthWriteEnable = true;
ComparisonFunc depthFunc = ComparisonFunc::Less; ComparisonFunc depthFunc = ComparisonFunc::Less;
@@ -57,7 +57,7 @@ struct DepthStencilState {
StencilOp stencilDepthPassOp = StencilOp::Keep; StencilOp stencilDepthPassOp = StencilOp::Keep;
}; };
struct BlendState { struct OpenGLBlendState {
bool blendEnable = false; bool blendEnable = false;
BlendFactor srcBlend = BlendFactor::SrcAlpha; BlendFactor srcBlend = BlendFactor::SrcAlpha;
BlendFactor dstBlend = BlendFactor::InvSrcAlpha; BlendFactor dstBlend = BlendFactor::InvSrcAlpha;
@@ -69,7 +69,7 @@ struct BlendState {
float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
}; };
struct RasterizerState { struct OpenGLRasterizerState {
bool cullFaceEnable = true; bool cullFaceEnable = true;
CullFace cullFace = CullFace::Back; CullFace cullFace = CullFace::Back;
FrontFace frontFace = FrontFace::CounterClockwise; FrontFace frontFace = FrontFace::CounterClockwise;
@@ -117,9 +117,9 @@ public:
void* GetNativeHandle() override; void* GetNativeHandle() override;
PipelineType GetType() const override { return PipelineType::Graphics; } PipelineType GetType() const override { return PipelineType::Graphics; }
void SetDepthStencilState(const DepthStencilState& state); void SetDepthStencilState(const OpenGLDepthStencilState& state);
void SetBlendState(const BlendState& state); void SetBlendState(const OpenGLBlendState& state);
void SetRasterizerState(const RasterizerState& state); void SetRasterizerState(const OpenGLRasterizerState& state);
void SetViewport(const ViewportState& state); void SetViewport(const ViewportState& state);
void SetScissor(const ScissorState& state); void SetScissor(const ScissorState& state);
void SetLogicalOperation(const LogicalOperation& state); void SetLogicalOperation(const LogicalOperation& state);
@@ -137,15 +137,14 @@ public:
void AttachShader(unsigned int program); void AttachShader(unsigned int program);
void DetachShader(); void DetachShader();
const DepthStencilState& GetDepthStencilState() const { return m_depthStencilState; } const OpenGLDepthStencilState& GetDepthStencilState() const { return m_depthStencilState; }
const BlendState& GetBlendState() const { return m_blendState; } const OpenGLBlendState& GetBlendState() const { return m_blendState; }
const RasterizerState& GetRasterizerState() const { return m_rasterizerState; } const OpenGLRasterizerState& GetRasterizerState() const { return m_rasterizerState; }
const ViewportState& GetViewportState() const { return m_viewportState; }
private: private:
DepthStencilState m_depthStencilState; OpenGLDepthStencilState m_depthStencilState;
BlendState m_blendState; OpenGLBlendState m_blendState;
RasterizerState m_rasterizerState; OpenGLRasterizerState m_rasterizerState;
ViewportState m_viewportState; ViewportState m_viewportState;
ScissorState m_scissorState; ScissorState m_scissorState;
LogicalOperation m_logicalOperation; LogicalOperation m_logicalOperation;

View File

@@ -168,11 +168,11 @@ RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) {
auto* buffer = new OpenGLBuffer(); auto* buffer = new OpenGLBuffer();
OpenGLBufferType bufferType = OpenGLBufferType::Vertex; OpenGLBufferType bufferType = OpenGLBufferType::Vertex;
switch (desc.usage) { switch (desc.bufferType) {
case ResourceUsage::IndexBuffer: case 1: // Index buffer
bufferType = OpenGLBufferType::Index; bufferType = OpenGLBufferType::Index;
break; break;
case ResourceUsage::ConstantBuffer: case 2: // Constant buffer
bufferType = OpenGLBufferType::Uniform; bufferType = OpenGLBufferType::Uniform;
break; break;
default: default:
@@ -180,7 +180,7 @@ RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) {
break; break;
} }
buffer->Initialize(bufferType, desc.size, nullptr, desc.cpuAccess == CPUAccess::Write); buffer->Initialize(bufferType, desc.size, nullptr, false);
return buffer; return buffer;
} }
@@ -188,14 +188,14 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
auto* texture = new OpenGLTexture(); auto* texture = new OpenGLTexture();
OpenGLTextureType type = OpenGLTextureType::Texture2D; OpenGLTextureType type = OpenGLTextureType::Texture2D;
switch (desc.type) { switch (desc.textureType) {
case TextureType::Texture1D: case 0:
type = OpenGLTextureType::Texture1D; type = OpenGLTextureType::Texture1D;
break; break;
case TextureType::Texture3D: case 2:
type = OpenGLTextureType::Texture3D; type = OpenGLTextureType::Texture3D;
break; break;
case TextureType::TextureCube: case 3:
type = OpenGLTextureType::TextureCube; type = OpenGLTextureType::TextureCube;
break; break;
default: default:
@@ -228,10 +228,11 @@ RHICommandQueue* OpenGLDevice::CreateCommandQueue(const CommandQueueDesc& desc)
RHIShader* OpenGLDevice::CompileShader(const ShaderCompileDesc& desc) { RHIShader* OpenGLDevice::CompileShader(const ShaderCompileDesc& desc) {
auto* shader = new OpenGLShader(); auto* shader = new OpenGLShader();
if (desc.sourceData && desc.sourceSize > 0) { std::wstring filePath = desc.fileName;
shader->Compile(static_cast<const char*>(desc.sourceData), desc.sourceSize); std::string entryPoint(desc.entryPoint.begin(), desc.entryPoint.end());
} else if (desc.filePath) { std::string profile(desc.profile.begin(), desc.profile.end());
shader->CompileFromFile(desc.filePath); if (!filePath.empty()) {
shader->CompileFromFile(filePath.c_str(), entryPoint.c_str(), profile.c_str());
} }
return shader; return shader;
} }

View File

@@ -100,15 +100,15 @@ OpenGLPipelineState::OpenGLPipelineState()
OpenGLPipelineState::~OpenGLPipelineState() { OpenGLPipelineState::~OpenGLPipelineState() {
} }
void OpenGLPipelineState::SetDepthStencilState(const DepthStencilState& state) { void OpenGLPipelineState::SetDepthStencilState(const OpenGLDepthStencilState& state) {
m_depthStencilState = state; m_depthStencilState = state;
} }
void OpenGLPipelineState::SetBlendState(const BlendState& state) { void OpenGLPipelineState::SetBlendState(const OpenGLBlendState& state) {
m_blendState = state; m_blendState = state;
} }
void OpenGLPipelineState::SetRasterizerState(const RasterizerState& state) { void OpenGLPipelineState::SetRasterizerState(const OpenGLRasterizerState& state) {
m_rasterizerState = state; m_rasterizerState = state;
} }

View File

@@ -202,6 +202,19 @@ bool OpenGLShader::Compile(const char* source, ShaderType type) {
return true; return true;
} }
bool OpenGLShader::CompileFromFile(const wchar_t* filePath, const char* entryPoint, const char* target) {
std::wstring ws(filePath);
std::string path(ws.begin(), ws.end());
return CompileFromFile(path.c_str(), nullptr);
}
bool OpenGLShader::Compile(const void* sourceData, size_t sourceSize, const char* entryPoint, const char* target) {
if (!sourceData || sourceSize == 0) {
return false;
}
return Compile(static_cast<const char*>(sourceData), ShaderType::Fragment);
}
void OpenGLShader::Shutdown() { void OpenGLShader::Shutdown() {
if (m_program) { if (m_program) {
glDeleteProgram(m_program); glDeleteProgram(m_program);

View File

@@ -189,7 +189,7 @@ void ResourceManager::ReloadResource(ResourceGUID guid) {
Containers::Array<Containers::String> ResourceManager::GetResourcePaths() const { Containers::Array<Containers::String> ResourceManager::GetResourcePaths() const {
Containers::Array<Containers::String> paths; Containers::Array<Containers::String> paths;
for (const auto& pair : m_guidToPath) { for (const auto& pair : m_guidToPath) {
paths.Add(pair.value); paths.PushBack(pair.second);
} }
return paths; return paths;
} }

View File

@@ -6,7 +6,7 @@ using namespace XCEngine::RHI;
TEST_F(OpenGLTestFixture, PipelineState_SetDepthStencilState) { TEST_F(OpenGLTestFixture, PipelineState_SetDepthStencilState) {
OpenGLPipelineState pipeline; OpenGLPipelineState pipeline;
DepthStencilState state; OpenGLDepthStencilState state;
state.depthTestEnable = true; state.depthTestEnable = true;
state.depthWriteEnable = true; state.depthWriteEnable = true;
state.depthFunc = ComparisonFunc::Less; state.depthFunc = ComparisonFunc::Less;
@@ -26,7 +26,7 @@ TEST_F(OpenGLTestFixture, PipelineState_SetDepthStencilState) {
TEST_F(OpenGLTestFixture, PipelineState_SetBlendState) { TEST_F(OpenGLTestFixture, PipelineState_SetBlendState) {
OpenGLPipelineState pipeline; OpenGLPipelineState pipeline;
BlendState state; OpenGLBlendState state;
state.blendEnable = true; state.blendEnable = true;
state.srcBlend = BlendFactor::SrcAlpha; state.srcBlend = BlendFactor::SrcAlpha;
state.dstBlend = BlendFactor::InvSrcAlpha; state.dstBlend = BlendFactor::InvSrcAlpha;