RHI: 增强RHIResourceView抽象接口,添加GetViewType/GetDimension/GetFormat虚函数

- 在RHIResourceView基类添加3个纯虚函数:GetViewType()、GetDimension()、GetFormat()
- D3D12ResourceView和OpenGLResourceView子类实现这些虚函数
- 在各InitializeAs*方法中正确存储format和dimension信息
- 消除调用者必须向下转型才能获取视图类型的需求
This commit is contained in:
2026-03-24 20:01:54 +08:00
parent 0f5d018c1a
commit cab290b17d
5 changed files with 38 additions and 4 deletions

View File

@@ -24,7 +24,9 @@ public:
bool IsValid() const override;
void SetViewType(ResourceViewType type) { m_viewType = type; }
ResourceViewType GetViewType() const { return m_viewType; }
ResourceViewType GetViewType() const override { return m_viewType; }
ResourceViewDimension GetDimension() const override { return m_dimension; }
Format GetFormat() const override { return m_format; }
void InitializeAsRenderTarget(ID3D12Device* device, ID3D12Resource* resource,
const D3D12_RENDER_TARGET_VIEW_DESC* desc,
@@ -55,6 +57,8 @@ public:
private:
ResourceViewType m_viewType;
Format m_format;
ResourceViewDimension m_dimension;
D3D12_CPU_DESCRIPTOR_HANDLE m_handle;
ID3D12Resource* m_resource;
D3D12DescriptorHeap* m_heap;

View File

@@ -46,7 +46,9 @@ public:
const ResourceViewDesc& desc,
OpenGLUniformBufferManager* manager);
ResourceViewType GetViewType() const { return m_viewType; }
ResourceViewType GetViewType() const override { return m_viewType; }
ResourceViewDimension GetDimension() const override { return m_dimension; }
Format GetFormat() const override { return m_format; }
unsigned int GetFramebuffer() const;
int32_t GetTextureUnit() const { return m_textureUnit; }
int32_t GetBindingPoint() const { return m_bindingPoint; }
@@ -55,6 +57,8 @@ public:
private:
ResourceViewType m_viewType;
Format m_format;
ResourceViewDimension m_dimension;
unsigned int m_framebufferID;
int32_t m_textureUnit;
int32_t m_bindingPoint;

View File

@@ -15,6 +15,10 @@ public:
virtual void* GetNativeHandle() = 0;
virtual bool IsValid() const = 0;
virtual ResourceViewType GetViewType() const = 0;
virtual ResourceViewDimension GetDimension() const = 0;
virtual Format GetFormat() const = 0;
};
} // namespace RHI

View File

@@ -6,6 +6,8 @@ namespace RHI {
D3D12ResourceView::D3D12ResourceView()
: m_viewType(ResourceViewType::ShaderResource)
, m_format(Format::Unknown)
, m_dimension(ResourceViewDimension::Unknown)
, m_handle({0})
, m_resource(nullptr)
, m_heap(nullptr)
@@ -35,6 +37,8 @@ void D3D12ResourceView::InitializeAsRenderTarget(ID3D12Device* device, ID3D12Res
const D3D12_RENDER_TARGET_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::RenderTarget;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = ResourceViewDimension::Texture2D;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -48,6 +52,8 @@ void D3D12ResourceView::InitializeAsDepthStencil(ID3D12Device* device, ID3D12Res
const D3D12_DEPTH_STENCIL_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::DepthStencil;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = ResourceViewDimension::Texture2D;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -61,6 +67,8 @@ void D3D12ResourceView::InitializeAsShaderResource(ID3D12Device* device, ID3D12R
const D3D12_SHADER_RESOURCE_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::ShaderResource;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = desc ? static_cast<ResourceViewDimension>(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -74,6 +82,8 @@ void D3D12ResourceView::InitializeAsUnorderedAccess(ID3D12Device* device, ID3D12
const D3D12_UNORDERED_ACCESS_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::UnorderedAccess;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = desc ? static_cast<ResourceViewDimension>(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -87,6 +97,8 @@ void D3D12ResourceView::InitializeAsConstantBuffer(ID3D12Device* device, ID3D12R
const D3D12_CONSTANT_BUFFER_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::ConstantBuffer;
m_format = Format::Unknown;
m_dimension = ResourceViewDimension::Buffer;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;

View File

@@ -12,6 +12,8 @@ namespace RHI {
OpenGLResourceView::OpenGLResourceView()
: m_viewType(ResourceViewType::RenderTarget)
, m_format(Format::Unknown)
, m_dimension(ResourceViewDimension::Unknown)
, m_framebufferID(0)
, m_textureUnit(-1)
, m_bindingPoint(-1)
@@ -81,12 +83,13 @@ bool OpenGLResourceView::InitializeAsRenderTarget(
OpenGLTexture* texture,
const ResourceViewDesc& desc,
OpenGLFramebuffer* framebuffer) {
(void)desc;
if (!texture || !framebuffer) {
return false;
}
m_viewType = ResourceViewType::RenderTarget;
m_format = static_cast<Format>(desc.format);
m_dimension = desc.dimension;
m_texture = texture;
m_framebuffer = framebuffer;
m_framebufferID = framebuffer->GetFramebuffer();
@@ -97,12 +100,13 @@ bool OpenGLResourceView::InitializeAsDepthStencil(
OpenGLTexture* texture,
const ResourceViewDesc& desc,
OpenGLFramebuffer* framebuffer) {
(void)desc;
if (!texture || !framebuffer) {
return false;
}
m_viewType = ResourceViewType::DepthStencil;
m_format = static_cast<Format>(desc.format);
m_dimension = desc.dimension;
m_texture = texture;
m_framebuffer = framebuffer;
m_framebufferID = framebuffer->GetFramebuffer();
@@ -123,6 +127,8 @@ bool OpenGLResourceView::InitializeAsShaderResource(
}
m_viewType = ResourceViewType::ShaderResource;
m_format = static_cast<Format>(desc.format);
m_dimension = desc.dimension;
m_texture = texture;
m_textureUnit = unit;
m_textureUnitAllocator = allocator;
@@ -144,6 +150,8 @@ bool OpenGLResourceView::InitializeAsUnorderedAccess(
}
m_viewType = ResourceViewType::UnorderedAccess;
m_format = static_cast<Format>(desc.format);
m_dimension = desc.dimension;
m_texture = texture;
m_textureUnit = unit;
m_textureUnitAllocator = allocator;
@@ -169,6 +177,8 @@ bool OpenGLResourceView::InitializeAsConstantBuffer(
}
m_viewType = ResourceViewType::ConstantBuffer;
m_format = Format::Unknown;
m_dimension = ResourceViewDimension::Buffer;
m_buffer = buffer;
m_bindingPoint = bindingPoint;
m_uniformBufferManager = manager;