RHI: Implement CreateRenderPass/CreateFramebuffer in D3D12 and OpenGL backends

This commit is contained in:
2026-03-25 13:03:02 +08:00
parent 2313124cc5
commit 41cea4d1a2
4 changed files with 75 additions and 3 deletions

View File

@@ -15,6 +15,8 @@
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h" #include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
#include "XCEngine/RHI/D3D12/D3D12Shader.h" #include "XCEngine/RHI/D3D12/D3D12Shader.h"
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h" #include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12RenderPass.h"
#include "XCEngine/RHI/D3D12/D3D12Framebuffer.h"
#include <stdio.h> #include <stdio.h>
#ifdef _DEBUG #ifdef _DEBUG
@@ -283,6 +285,13 @@ RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc) {
d3d12Desc.SampleDesc.Count = desc.sampleCount > 0 ? desc.sampleCount : 1; d3d12Desc.SampleDesc.Count = desc.sampleCount > 0 ? desc.sampleCount : 1;
d3d12Desc.SampleDesc.Quality = desc.sampleQuality; d3d12Desc.SampleDesc.Quality = desc.sampleQuality;
d3d12Desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(desc.flags); d3d12Desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(desc.flags);
Format format = static_cast<Format>(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; d3d12Desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
if (texture->Initialize(m_device.Get(), d3d12Desc)) { if (texture->Initialize(m_device.Get(), d3d12Desc)) {
return texture; return texture;
@@ -344,6 +353,32 @@ RHIDescriptorPool* D3D12Device::CreateDescriptorPool(const DescriptorPoolDesc& d
return nullptr; 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) { RHIDescriptorSet* D3D12Device::CreateDescriptorSet(RHIDescriptorPool* pool, const DescriptorSetLayoutDesc& layout) {
if (pool == nullptr) { if (pool == nullptr) {
return nullptr; return nullptr;
@@ -433,6 +468,11 @@ RHIResourceView* D3D12Device::CreateRenderTargetView(RHITexture* texture, const
auto* heapPool = CreateDescriptorHeap(heapDesc); auto* heapPool = CreateDescriptorHeap(heapDesc);
auto* heap = static_cast<D3D12DescriptorHeap*>(heapPool); auto* heap = static_cast<D3D12DescriptorHeap*>(heapPool);
if (heap == nullptr) {
delete view;
return nullptr;
}
view->InitializeAsRenderTarget(m_device.Get(), resource, &rtvDesc, heap, 0); view->InitializeAsRenderTarget(m_device.Get(), resource, &rtvDesc, heap, 0);
return view; return view;
} }
@@ -453,6 +493,11 @@ RHIResourceView* D3D12Device::CreateDepthStencilView(RHITexture* texture, const
auto* heapPool = CreateDescriptorHeap(heapDesc); auto* heapPool = CreateDescriptorHeap(heapDesc);
auto* heap = static_cast<D3D12DescriptorHeap*>(heapPool); auto* heap = static_cast<D3D12DescriptorHeap*>(heapPool);
if (heap == nullptr) {
delete view;
return nullptr;
}
view->InitializeAsDepthStencil(m_device.Get(), resource, &dsvDesc, heap, 0); view->InitializeAsDepthStencil(m_device.Get(), resource, &dsvDesc, heap, 0);
return view; return view;
} }

View File

@@ -20,10 +20,10 @@ void D3D12Framebuffer::Shutdown() {
m_height = 0; 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, uint32_t colorAttachmentCount, RHIResourceView** colorAttachments,
RHIResourceView* depthStencilAttachment) { RHIResourceView* depthStencilAttachment) {
m_renderPass = renderPass; m_renderPass = static_cast<D3D12RenderPass*>(renderPass);
m_width = width; m_width = width;
m_height = height; m_height = height;

View File

@@ -62,7 +62,7 @@ D3D12_RENDER_PASS_ENDING_ACCESS_TYPE D3D12RenderPass::GetEndingAccessType(uint32
case StoreAction::Resolve: case StoreAction::Resolve:
return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_RESOLVE; return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_RESOLVE;
case StoreAction::StoreAndResolve: 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: case StoreAction::Undefined:
default: default:
return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_NO_ACCESS; return D3D12_RENDER_PASS_ENDING_ACCESS_TYPE::D3D12_RENDER_PASS_ENDING_ACCESS_TYPE_NO_ACCESS;

View File

@@ -17,6 +17,7 @@
#include "XCEngine/RHI/OpenGL/OpenGLTextureUnitAllocator.h" #include "XCEngine/RHI/OpenGL/OpenGLTextureUnitAllocator.h"
#include "XCEngine/RHI/OpenGL/OpenGLUniformBufferManager.h" #include "XCEngine/RHI/OpenGL/OpenGLUniformBufferManager.h"
#include "XCEngine/RHI/OpenGL/OpenGLFramebuffer.h" #include "XCEngine/RHI/OpenGL/OpenGLFramebuffer.h"
#include "XCEngine/RHI/OpenGL/OpenGLRenderPass.h"
#include "XCEngine/RHI/OpenGL/OpenGLResourceView.h" #include "XCEngine/RHI/OpenGL/OpenGLResourceView.h"
#include "XCEngine/RHI/OpenGL/OpenGLDescriptorPool.h" #include "XCEngine/RHI/OpenGL/OpenGLDescriptorPool.h"
#include "XCEngine/RHI/OpenGL/OpenGLDescriptorSet.h" #include "XCEngine/RHI/OpenGL/OpenGLDescriptorSet.h"
@@ -418,6 +419,32 @@ RHISampler* OpenGLDevice::CreateSampler(const SamplerDesc& desc) {
return sampler; 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) { RHIDescriptorPool* OpenGLDevice::CreateDescriptorPool(const DescriptorPoolDesc& desc) {
auto* pool = new OpenGLDescriptorPool(); auto* pool = new OpenGLDescriptorPool();
if (pool->Initialize(desc)) { if (pool->Initialize(desc)) {