From 41cea4d1a2384bf979f9cd216a5d6db5fe78070f Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Wed, 25 Mar 2026 13:03:02 +0800 Subject: [PATCH] RHI: Implement CreateRenderPass/CreateFramebuffer in D3D12 and OpenGL backends --- engine/src/RHI/D3D12/D3D12Device.cpp | 45 +++++++++++++++++++++++ engine/src/RHI/D3D12/D3D12Framebuffer.cpp | 4 +- engine/src/RHI/D3D12/D3D12RenderPass.cpp | 2 +- engine/src/RHI/OpenGL/OpenGLDevice.cpp | 27 ++++++++++++++ 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/engine/src/RHI/D3D12/D3D12Device.cpp b/engine/src/RHI/D3D12/D3D12Device.cpp index 6de21baa..dd581c17 100644 --- a/engine/src/RHI/D3D12/D3D12Device.cpp +++ b/engine/src/RHI/D3D12/D3D12Device.cpp @@ -15,6 +15,8 @@ #include "XCEngine/RHI/D3D12/D3D12SwapChain.h" #include "XCEngine/RHI/D3D12/D3D12Shader.h" #include "XCEngine/RHI/D3D12/D3D12ResourceView.h" +#include "XCEngine/RHI/D3D12/D3D12RenderPass.h" +#include "XCEngine/RHI/D3D12/D3D12Framebuffer.h" #include #ifdef _DEBUG @@ -283,6 +285,13 @@ RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc) { d3d12Desc.SampleDesc.Count = desc.sampleCount > 0 ? desc.sampleCount : 1; d3d12Desc.SampleDesc.Quality = desc.sampleQuality; d3d12Desc.Flags = static_cast(desc.flags); + + Format format = static_cast(desc.format); + if (format == Format::D24_UNorm_S8_UInt || format == Format::D32_Float || + format == Format::D16_UNorm) { + d3d12Desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + } + d3d12Desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; if (texture->Initialize(m_device.Get(), d3d12Desc)) { return texture; @@ -344,6 +353,32 @@ RHIDescriptorPool* D3D12Device::CreateDescriptorPool(const DescriptorPoolDesc& d return nullptr; } +RHIRenderPass* D3D12Device::CreateRenderPass( + uint32_t colorAttachmentCount, + const AttachmentDesc* colorAttachments, + const AttachmentDesc* depthStencilAttachment) { + auto* renderPass = new D3D12RenderPass(); + if (!renderPass->Initialize(colorAttachmentCount, colorAttachments, depthStencilAttachment)) { + delete renderPass; + return nullptr; + } + return renderPass; +} + +RHIFramebuffer* D3D12Device::CreateFramebuffer( + class RHIRenderPass* renderPass, + uint32_t width, uint32_t height, + uint32_t colorAttachmentCount, + RHIResourceView** colorAttachments, + RHIResourceView* depthStencilAttachment) { + auto* framebuffer = new D3D12Framebuffer(); + if (!framebuffer->Initialize(renderPass, width, height, colorAttachmentCount, colorAttachments, depthStencilAttachment)) { + delete framebuffer; + return nullptr; + } + return framebuffer; +} + RHIDescriptorSet* D3D12Device::CreateDescriptorSet(RHIDescriptorPool* pool, const DescriptorSetLayoutDesc& layout) { if (pool == nullptr) { return nullptr; @@ -433,6 +468,11 @@ RHIResourceView* D3D12Device::CreateRenderTargetView(RHITexture* texture, const auto* heapPool = CreateDescriptorHeap(heapDesc); auto* heap = static_cast(heapPool); + if (heap == nullptr) { + delete view; + return nullptr; + } + view->InitializeAsRenderTarget(m_device.Get(), resource, &rtvDesc, heap, 0); return view; } @@ -453,6 +493,11 @@ RHIResourceView* D3D12Device::CreateDepthStencilView(RHITexture* texture, const auto* heapPool = CreateDescriptorHeap(heapDesc); auto* heap = static_cast(heapPool); + if (heap == nullptr) { + delete view; + return nullptr; + } + view->InitializeAsDepthStencil(m_device.Get(), resource, &dsvDesc, heap, 0); return view; } diff --git a/engine/src/RHI/D3D12/D3D12Framebuffer.cpp b/engine/src/RHI/D3D12/D3D12Framebuffer.cpp index 44247bb4..a8f6825e 100644 --- a/engine/src/RHI/D3D12/D3D12Framebuffer.cpp +++ b/engine/src/RHI/D3D12/D3D12Framebuffer.cpp @@ -20,10 +20,10 @@ void D3D12Framebuffer::Shutdown() { m_height = 0; } -bool D3D12Framebuffer::Initialize(D3D12RenderPass* renderPass, uint32_t width, uint32_t height, +bool D3D12Framebuffer::Initialize(class RHIRenderPass* renderPass, uint32_t width, uint32_t height, uint32_t colorAttachmentCount, RHIResourceView** colorAttachments, RHIResourceView* depthStencilAttachment) { - m_renderPass = renderPass; + m_renderPass = static_cast(renderPass); m_width = width; m_height = height; diff --git a/engine/src/RHI/D3D12/D3D12RenderPass.cpp b/engine/src/RHI/D3D12/D3D12RenderPass.cpp index 03e63a0c..bfad784f 100644 --- a/engine/src/RHI/D3D12/D3D12RenderPass.cpp +++ b/engine/src/RHI/D3D12/D3D12RenderPass.cpp @@ -62,7 +62,7 @@ D3D12_RENDER_PASS_ENDING_ACCESS_TYPE D3D12RenderPass::GetEndingAccessType(uint32 case StoreAction::Resolve: return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_RESOLVE; case StoreAction::StoreAndResolve: - return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_STORE_AND_RESOLVE; + return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_RESOLVE; case StoreAction::Undefined: default: return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_NO_ACCESS; diff --git a/engine/src/RHI/OpenGL/OpenGLDevice.cpp b/engine/src/RHI/OpenGL/OpenGLDevice.cpp index 5da2c77c..2216cc07 100644 --- a/engine/src/RHI/OpenGL/OpenGLDevice.cpp +++ b/engine/src/RHI/OpenGL/OpenGLDevice.cpp @@ -17,6 +17,7 @@ #include "XCEngine/RHI/OpenGL/OpenGLTextureUnitAllocator.h" #include "XCEngine/RHI/OpenGL/OpenGLUniformBufferManager.h" #include "XCEngine/RHI/OpenGL/OpenGLFramebuffer.h" +#include "XCEngine/RHI/OpenGL/OpenGLRenderPass.h" #include "XCEngine/RHI/OpenGL/OpenGLResourceView.h" #include "XCEngine/RHI/OpenGL/OpenGLDescriptorPool.h" #include "XCEngine/RHI/OpenGL/OpenGLDescriptorSet.h" @@ -418,6 +419,32 @@ RHISampler* OpenGLDevice::CreateSampler(const SamplerDesc& desc) { return sampler; } +RHIRenderPass* OpenGLDevice::CreateRenderPass( + uint32_t colorAttachmentCount, + const AttachmentDesc* colorAttachments, + const AttachmentDesc* depthStencilAttachment) { + auto* renderPass = new OpenGLRenderPass(); + if (!renderPass->Initialize(colorAttachmentCount, colorAttachments, depthStencilAttachment)) { + delete renderPass; + return nullptr; + } + return renderPass; +} + +RHIFramebuffer* OpenGLDevice::CreateFramebuffer( + class RHIRenderPass* renderPass, + uint32_t width, uint32_t height, + uint32_t colorAttachmentCount, + RHIResourceView** colorAttachments, + RHIResourceView* depthStencilAttachment) { + auto* framebuffer = new OpenGLFramebuffer(); + if (!framebuffer->Initialize(renderPass, width, height, colorAttachmentCount, colorAttachments, depthStencilAttachment)) { + delete framebuffer; + return nullptr; + } + return framebuffer; +} + RHIDescriptorPool* OpenGLDevice::CreateDescriptorPool(const DescriptorPoolDesc& desc) { auto* pool = new OpenGLDescriptorPool(); if (pool->Initialize(desc)) {