diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 85f62efc..243268b3 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -127,6 +127,34 @@ add_library(XCEngine STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/D3D12/D3D12QueryHeap.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 ${CMAKE_CURRENT_SOURCE_DIR}/src/RHI/RHIFactory.cpp diff --git a/engine/include/XCEngine/Containers/HashMap.h b/engine/include/XCEngine/Containers/HashMap.h index 5345718a..4bc74b76 100644 --- a/engine/include/XCEngine/Containers/HashMap.h +++ b/engine/include/XCEngine/Containers/HashMap.h @@ -42,6 +42,24 @@ public: size_t Size() const { return m_size; } bool Empty() const { return m_size == 0; } + using Iterator = typename Array::Iterator; + using ConstIterator = typename Array::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; } private: diff --git a/engine/include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h b/engine/include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h index 342dc9f9..1a9c59a0 100644 --- a/engine/include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h +++ b/engine/include/XCEngine/RHI/OpenGL/OpenGLPipelineState.h @@ -43,7 +43,7 @@ enum class PolygonMode { Fill }; -struct DepthStencilState { +struct OpenGLDepthStencilState { bool depthTestEnable = true; bool depthWriteEnable = true; ComparisonFunc depthFunc = ComparisonFunc::Less; @@ -57,7 +57,7 @@ struct DepthStencilState { StencilOp stencilDepthPassOp = StencilOp::Keep; }; -struct BlendState { +struct OpenGLBlendState { bool blendEnable = false; BlendFactor srcBlend = BlendFactor::SrcAlpha; BlendFactor dstBlend = BlendFactor::InvSrcAlpha; @@ -69,7 +69,7 @@ struct BlendState { float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; }; -struct RasterizerState { +struct OpenGLRasterizerState { bool cullFaceEnable = true; CullFace cullFace = CullFace::Back; FrontFace frontFace = FrontFace::CounterClockwise; @@ -117,9 +117,9 @@ public: void* GetNativeHandle() override; PipelineType GetType() const override { return PipelineType::Graphics; } - void SetDepthStencilState(const DepthStencilState& state); - void SetBlendState(const BlendState& state); - void SetRasterizerState(const RasterizerState& state); + void SetDepthStencilState(const OpenGLDepthStencilState& state); + void SetBlendState(const OpenGLBlendState& state); + void SetRasterizerState(const OpenGLRasterizerState& state); void SetViewport(const ViewportState& state); void SetScissor(const ScissorState& state); void SetLogicalOperation(const LogicalOperation& state); @@ -137,15 +137,14 @@ public: void AttachShader(unsigned int program); void DetachShader(); - const DepthStencilState& GetDepthStencilState() const { return m_depthStencilState; } - const BlendState& GetBlendState() const { return m_blendState; } - const RasterizerState& GetRasterizerState() const { return m_rasterizerState; } - const ViewportState& GetViewportState() const { return m_viewportState; } + const OpenGLDepthStencilState& GetDepthStencilState() const { return m_depthStencilState; } + const OpenGLBlendState& GetBlendState() const { return m_blendState; } + const OpenGLRasterizerState& GetRasterizerState() const { return m_rasterizerState; } private: - DepthStencilState m_depthStencilState; - BlendState m_blendState; - RasterizerState m_rasterizerState; + OpenGLDepthStencilState m_depthStencilState; + OpenGLBlendState m_blendState; + OpenGLRasterizerState m_rasterizerState; ViewportState m_viewportState; ScissorState m_scissorState; LogicalOperation m_logicalOperation; diff --git a/engine/src/RHI/OpenGL/OpenGLDevice.cpp b/engine/src/RHI/OpenGL/OpenGLDevice.cpp index f00980b7..ac0b2bdd 100644 --- a/engine/src/RHI/OpenGL/OpenGLDevice.cpp +++ b/engine/src/RHI/OpenGL/OpenGLDevice.cpp @@ -168,11 +168,11 @@ RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) { auto* buffer = new OpenGLBuffer(); OpenGLBufferType bufferType = OpenGLBufferType::Vertex; - switch (desc.usage) { - case ResourceUsage::IndexBuffer: + switch (desc.bufferType) { + case 1: // Index buffer bufferType = OpenGLBufferType::Index; break; - case ResourceUsage::ConstantBuffer: + case 2: // Constant buffer bufferType = OpenGLBufferType::Uniform; break; default: @@ -180,7 +180,7 @@ RHIBuffer* OpenGLDevice::CreateBuffer(const BufferDesc& desc) { break; } - buffer->Initialize(bufferType, desc.size, nullptr, desc.cpuAccess == CPUAccess::Write); + buffer->Initialize(bufferType, desc.size, nullptr, false); return buffer; } @@ -188,14 +188,14 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) { auto* texture = new OpenGLTexture(); OpenGLTextureType type = OpenGLTextureType::Texture2D; - switch (desc.type) { - case TextureType::Texture1D: + switch (desc.textureType) { + case 0: type = OpenGLTextureType::Texture1D; break; - case TextureType::Texture3D: + case 2: type = OpenGLTextureType::Texture3D; break; - case TextureType::TextureCube: + case 3: type = OpenGLTextureType::TextureCube; break; default: @@ -228,10 +228,11 @@ RHICommandQueue* OpenGLDevice::CreateCommandQueue(const CommandQueueDesc& desc) RHIShader* OpenGLDevice::CompileShader(const ShaderCompileDesc& desc) { auto* shader = new OpenGLShader(); - if (desc.sourceData && desc.sourceSize > 0) { - shader->Compile(static_cast(desc.sourceData), desc.sourceSize); - } else if (desc.filePath) { - shader->CompileFromFile(desc.filePath); + std::wstring filePath = desc.fileName; + std::string entryPoint(desc.entryPoint.begin(), desc.entryPoint.end()); + std::string profile(desc.profile.begin(), desc.profile.end()); + if (!filePath.empty()) { + shader->CompileFromFile(filePath.c_str(), entryPoint.c_str(), profile.c_str()); } return shader; } diff --git a/engine/src/RHI/OpenGL/OpenGLPipelineState.cpp b/engine/src/RHI/OpenGL/OpenGLPipelineState.cpp index 2ab92432..9c63d21f 100644 --- a/engine/src/RHI/OpenGL/OpenGLPipelineState.cpp +++ b/engine/src/RHI/OpenGL/OpenGLPipelineState.cpp @@ -100,15 +100,15 @@ OpenGLPipelineState::OpenGLPipelineState() OpenGLPipelineState::~OpenGLPipelineState() { } -void OpenGLPipelineState::SetDepthStencilState(const DepthStencilState& state) { +void OpenGLPipelineState::SetDepthStencilState(const OpenGLDepthStencilState& state) { m_depthStencilState = state; } -void OpenGLPipelineState::SetBlendState(const BlendState& state) { +void OpenGLPipelineState::SetBlendState(const OpenGLBlendState& state) { m_blendState = state; } -void OpenGLPipelineState::SetRasterizerState(const RasterizerState& state) { +void OpenGLPipelineState::SetRasterizerState(const OpenGLRasterizerState& state) { m_rasterizerState = state; } diff --git a/engine/src/RHI/OpenGL/OpenGLShader.cpp b/engine/src/RHI/OpenGL/OpenGLShader.cpp index 436710f0..93d78a0e 100644 --- a/engine/src/RHI/OpenGL/OpenGLShader.cpp +++ b/engine/src/RHI/OpenGL/OpenGLShader.cpp @@ -202,6 +202,19 @@ bool OpenGLShader::Compile(const char* source, ShaderType type) { 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(sourceData), ShaderType::Fragment); +} + void OpenGLShader::Shutdown() { if (m_program) { glDeleteProgram(m_program); diff --git a/engine/src/Resources/ResourceManager.cpp b/engine/src/Resources/ResourceManager.cpp index d0dd6fa6..96eb80df 100644 --- a/engine/src/Resources/ResourceManager.cpp +++ b/engine/src/Resources/ResourceManager.cpp @@ -189,7 +189,7 @@ void ResourceManager::ReloadResource(ResourceGUID guid) { Containers::Array ResourceManager::GetResourcePaths() const { Containers::Array paths; for (const auto& pair : m_guidToPath) { - paths.Add(pair.value); + paths.PushBack(pair.second); } return paths; } diff --git a/tests/RHI/OpenGL/test_pipeline_state.cpp b/tests/RHI/OpenGL/test_pipeline_state.cpp index f0e246d5..384462d8 100644 --- a/tests/RHI/OpenGL/test_pipeline_state.cpp +++ b/tests/RHI/OpenGL/test_pipeline_state.cpp @@ -6,7 +6,7 @@ using namespace XCEngine::RHI; TEST_F(OpenGLTestFixture, PipelineState_SetDepthStencilState) { OpenGLPipelineState pipeline; - DepthStencilState state; + OpenGLDepthStencilState state; state.depthTestEnable = true; state.depthWriteEnable = true; state.depthFunc = ComparisonFunc::Less; @@ -26,7 +26,7 @@ TEST_F(OpenGLTestFixture, PipelineState_SetDepthStencilState) { TEST_F(OpenGLTestFixture, PipelineState_SetBlendState) { OpenGLPipelineState pipeline; - BlendState state; + OpenGLBlendState state; state.blendEnable = true; state.srcBlend = BlendFactor::SrcAlpha; state.dstBlend = BlendFactor::InvSrcAlpha;