feat(RHI): 实现 RHICommandList 抽象基类

This commit is contained in:
2026-03-17 18:01:55 +08:00
parent 4638539f17
commit 55865a0252
6 changed files with 454 additions and 34 deletions

View File

@@ -760,6 +760,84 @@ public:
- 解决GetNativeHandle() 返回 void*
### 5.12 命令列表RHICommandList抽象设计
#### 5.12.1 设计理念对应
| 差异点 | 设计理念 | 处理方案 |
|--------|---------|---------|
| 资源状态转换 | 特性降级 | D3D12 实现转换OpenGL 空实现 |
| 绑定方式 | 求同存异 | 统一接口,后端各自实现 |
| 渲染目标 | 求同存异 | 统一接口,后端各自实现 |
#### 5.12.2 现有实现对比
| 功能 | D3D12CommandList | OpenGLCommandList | 处理方案 |
|------|-------------------|-------------------|----------|
| 资源转换 | TransitionBarrier | glBarrier | 统一 TransitionBarrier |
| PSO 设置 | SetPipelineState | UseShader | 统一 SetPipelineState |
| 视口/裁剪 | SetViewport/ScissorRect | SetViewport/Scissor | 统一 |
| 顶点缓冲 | SetVertexBuffer | BindVertexArray | 统一 |
| 索引缓冲 | SetIndexBuffer | BindVertexArray | 统一 |
| 绘制 | Draw/DrawIndexed | Draw/DrawIndexed | 统一 |
| 渲染目标 | SetRenderTargets | glBindFramebuffer | 统一 |
| 清除 | ClearRenderTargetView | Clear | 统一 |
| 复制 | CopyResource | CopyImageSubData | 统一 |
| 计算 | Dispatch | DispatchCompute | 统一 |
#### 5.12.3 抽象接口定义
```cpp
class RHICommandList {
public:
virtual ~RHICommandList() = default;
virtual void Shutdown() = 0;
virtual void Reset() = 0;
virtual void Close() = 0;
virtual void TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) = 0;
virtual void SetPipelineState(void* pso) = 0;
virtual void SetViewport(const Viewport& viewport) = 0;
virtual void SetScissorRect(const Rect& rect) = 0;
virtual void SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil = nullptr) = 0;
virtual void SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) = 0;
virtual void SetIndexBuffer(void* buffer, uint64_t offset, Format format) = 0;
virtual void Draw(uint32_t vertexCount, uint32_t instanceCount = 1, uint32_t startVertex = 0, uint32_t startInstance = 0) = 0;
virtual void DrawIndexed(uint32_t indexCount, uint32_t instanceCount = 1, uint32_t startIndex = 0, int32_t baseVertex = 0, uint32_t startInstance = 0) = 0;
virtual void Clear(float r, float g, float b, float a, uint32_t buffers) = 0;
virtual void ClearRenderTarget(void* renderTarget, const float color[4]) = 0;
virtual void ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) = 0;
virtual void CopyResource(void* dst, void* src) = 0;
virtual void Dispatch(uint32_t x, uint32_t y, uint32_t z) = 0;
};
```
#### 5.12.4 差异处理策略
1. **资源状态转换(特性降级)**
- D3D12显式 TransitionBarrier 管理资源状态
- OpenGL无资源状态概念
- 解决D3D12 实现转换OpenGL 空实现
2. **绑定方式(求同存异)**
- D3D12通过 DescriptorHeap 和 RootSignature
- OpenGL通过 glBind* 和 VAO
- 解决:统一 Set* 接口,后端各自实现
3. **渲染目标(求同存异)**
- D3D12SetRenderTargets 设置渲染目标视图
- OpenGLglBindFramebuffer 绑定帧缓冲
- 解决:统一 SetRenderTargets 接口
## 6. 后端实现示例
### 6.1 D3D12 设备实现D3D12Device.h
```cpp

View File

@@ -5,6 +5,7 @@
#include <vector>
#include <unordered_map>
#include "../RHICommandList.h"
#include "../RHIEnums.h"
#include "../RHITypes.h"
#include "D3D12Enum.h"
@@ -14,36 +15,41 @@ using Microsoft::WRL::ComPtr;
namespace XCEngine {
namespace RHI {
class D3D12CommandList {
class D3D12CommandList : public RHICommandList {
public:
D3D12CommandList();
~D3D12CommandList();
~D3D12CommandList() override;
bool Initialize(ID3D12Device* device, CommandQueueType type = CommandQueueType::Direct, ID3D12CommandAllocator* allocator = nullptr);
void Shutdown();
void Shutdown() override;
void Reset(ID3D12CommandAllocator* allocator);
void Close();
void Reset() override;
void Close() override;
ID3D12GraphicsCommandList* GetCommandList() const { return m_commandList.Get(); }
void TransitionBarrier(ID3D12Resource* resource, ResourceStates stateBefore, ResourceStates stateAfter, uint32_t subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES);
void UAVBarrier(ID3D12Resource* resource = nullptr);
void AliasBarrier(ID3D12Resource* beforeResource = nullptr, ID3D12Resource* afterResource = nullptr);
void TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) override;
void TransitionBarrierInternal(ID3D12Resource* resource, ResourceStates stateBefore, ResourceStates stateAfter, uint32_t subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES);
void UAVBarrier(void* resource = nullptr);
void AliasBarrier(void* beforeResource = nullptr, void* afterResource = nullptr);
void SetPipelineState(ID3D12PipelineState* pso);
void SetPipelineState(void* pso) override;
void SetRootSignature(ID3D12RootSignature* signature);
void SetViewport(const Viewport& viewport);
void SetViewports(uint32_t count, const Viewport* viewports);
void SetScissorRect(const Rect& rect);
void SetScissorRects(uint32_t count, const Rect* rects);
void SetViewport(const Viewport& viewport) override;
void SetViewports(uint32_t count, const Viewport* viewports) override;
void SetScissorRect(const Rect& rect) override;
void SetScissorRects(uint32_t count, const Rect* rects) override;
void SetPrimitiveTopology(PrimitiveTopology topology);
void SetRenderTargets(uint32_t count, ID3D12Resource** renderTargets, ID3D12Resource* depthStencil = nullptr);
void SetRenderTargets(uint32_t count, const D3D12_CPU_DESCRIPTOR_HANDLE* renderTargetHandles, const D3D12_CPU_DESCRIPTOR_HANDLE* depthStencilHandle = nullptr);
void SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil = nullptr) override;
void SetRenderTargetsInternal(uint32_t count, ID3D12Resource** renderTargets, ID3D12Resource* depthStencil = nullptr);
void SetRenderTargetsHandle(uint32_t count, const D3D12_CPU_DESCRIPTOR_HANDLE* renderTargetHandles, const D3D12_CPU_DESCRIPTOR_HANDLE* depthStencilHandle = nullptr);
void SetVertexBuffer(uint32_t slot, ID3D12Resource* buffer, uint64_t offset, uint32_t stride);
void SetVertexBuffers(uint32_t startSlot, uint32_t count, const D3D12_VERTEX_BUFFER_VIEW* views);
void SetIndexBuffer(ID3D12Resource* buffer, uint64_t offset, Format indexFormat);
void SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) override;
void SetVertexBufferInternal(uint32_t slot, ID3D12Resource* buffer, uint64_t offset, uint32_t stride);
void SetVertexBuffers(uint32_t startSlot, uint32_t count, const uint64_t* buffers, const uint64_t* offsets, const uint32_t* strides) override;
void SetVertexBuffersInternal(uint32_t startSlot, uint32_t count, const D3D12_VERTEX_BUFFER_VIEW* views);
void SetIndexBuffer(void* buffer, uint64_t offset, Format format) override;
void SetIndexBufferInternal(ID3D12Resource* buffer, uint64_t offset, Format indexFormat);
void SetDescriptorHeap(ID3D12DescriptorHeap* heap);
void SetDescriptorHeaps(uint32_t count, ID3D12DescriptorHeap** heaps);
@@ -59,18 +65,22 @@ public:
void SetBlendFactor(const float blendFactor[4]);
void SetDepthBias(float depthBias, float slopeScaledDepthBias, float depthBiasClamp);
void Draw(uint32_t vertexCount, uint32_t instanceCount = 1, uint32_t startVertex = 0, uint32_t startInstance = 0);
void DrawIndexed(uint32_t indexCount, uint32_t instanceCount = 1, uint32_t startIndex = 0, int32_t baseVertex = 0, uint32_t startInstance = 0);
void DrawInstancedIndirect(ID3D12Resource* argBuffer, uint64_t alignedByteOffset);
void DrawIndexedInstancedIndirect(ID3D12Resource* argBuffer, uint64_t alignedByteOffset);
void Draw(uint32_t vertexCount, uint32_t instanceCount = 1, uint32_t startVertex = 0, uint32_t startInstance = 0) override;
void DrawIndexed(uint32_t indexCount, uint32_t instanceCount = 1, uint32_t startIndex = 0, int32_t baseVertex = 0, uint32_t startInstance = 0) override;
void DrawInstancedIndirect(void* argBuffer, uint64_t alignedByteOffset);
void DrawIndexedInstancedIndirect(void* argBuffer, uint64_t alignedByteOffset);
void Clear(float r, float g, float b, float a, uint32_t buffers) override;
void ClearRenderTarget(void* renderTarget, const float color[4]) override;
void ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) override;
void ClearRenderTargetView(ID3D12Resource* renderTarget, const float color[4], uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearRenderTargetView(D3D12_CPU_DESCRIPTOR_HANDLE renderTargetHandle, const float color[4], uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearDepthStencilView(ID3D12Resource* depthStencil, uint32_t clearFlags, float depth = 1.0f, uint8_t stencil = 0, uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearDepthStencilView(D3D12_CPU_DESCRIPTOR_HANDLE depthStencilHandle, uint32_t clearFlags, float depth = 1.0f, uint8_t stencil = 0, uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void ClearUnorderedAccessView(D3D12_GPU_DESCRIPTOR_HANDLE viewHandle, D3D12_CPU_DESCRIPTOR_HANDLE resourceHandle, ID3D12Resource* unorderedAccess, const float values[4], uint32_t rectCount = 0, const D3D12_RECT* rects = nullptr);
void CopyResource(ID3D12Resource* dst, ID3D12Resource* src);
void CopyResource(void* dst, void* src) override;
void CopyResourceInternal(ID3D12Resource* dst, ID3D12Resource* src);
void CopyBuffer(ID3D12Resource* dst, uint64_t dstOffset, ID3D12Resource* src, uint64_t srcOffset, uint64_t size);
void CopyTexture(ID3D12Resource* dst, const D3D12_TEXTURE_COPY_LOCATION& dstLocation, ID3D12Resource* src, const D3D12_TEXTURE_COPY_LOCATION& srcLocation);
@@ -78,8 +88,8 @@ public:
void EndQuery(ID3D12QueryHeap* queryHeap, QueryType type, uint32_t index);
void ResolveQueryData(ID3D12QueryHeap* queryHeap, QueryType type, uint32_t startIndex, uint32_t count, ID3D12Resource* resultBuffer, uint64_t resultOffset);
void Dispatch(uint32_t threadGroupCountX, uint32_t threadGroupCountY, uint32_t threadGroupCountZ);
void DispatchIndirect(ID3D12Resource* argBuffer, uint64_t alignedByteOffset);
void Dispatch(uint32_t x, uint32_t y, uint32_t z) override;
void DispatchIndirect(void* argBuffer, uint64_t alignedByteOffset);
void ExecuteBundle(ID3D12GraphicsCommandList* bundle);

View File

@@ -4,6 +4,8 @@
#include <vector>
#include <cstdint>
#include "../RHICommandList.h"
namespace XCEngine {
namespace RHI {
@@ -34,22 +36,26 @@ inline ClearFlag operator|(ClearFlag a, ClearFlag b) {
return static_cast<ClearFlag>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
}
class OpenGLCommandList {
class OpenGLCommandList : public RHICommandList {
public:
OpenGLCommandList();
~OpenGLCommandList();
~OpenGLCommandList() override;
void Clear(float r, float g, float b, float a, unsigned int buffers);
void Shutdown() override;
void Reset() override;
void Close() override;
void Clear(float r, float g, float b, float a, unsigned int buffers) override;
void ClearColor(float r, float g, float b, float a);
void ClearDepth(float depth);
void ClearStencil(int stencil);
void ClearDepthStencil(float depth, int stencil);
void SetPipelineState(void* pipelineState);
void SetVertexBuffer(unsigned int buffer, size_t offset, size_t stride);
void SetVertexBuffers(unsigned int startSlot, unsigned int count, const unsigned int* buffers, const size_t* offsets, const size_t* strides);
void SetIndexBuffer(unsigned int buffer, unsigned int type);
void SetIndexBuffer(unsigned int buffer, unsigned int type, size_t offset);
void SetPipelineState(void* pipelineState) override;
void SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) override;
void SetVertexBuffers(uint32_t startSlot, uint32_t count, const uint64_t* buffers, const uint64_t* offsets, const uint32_t* strides) override;
void SetIndexBuffer(void* buffer, uint64_t offset, Format format) override;
void BindVertexArray(unsigned int vao);
void BindVertexArray(unsigned int vao, unsigned int indexBuffer, unsigned int indexType);
@@ -92,7 +98,7 @@ public:
void MultiDrawArrays(PrimitiveType type, const int* first, const int* count, unsigned int drawCount);
void MultiDrawElements(PrimitiveType type, const int* count, unsigned int type_, const void* const* indices, unsigned int drawCount);
void Dispatch(unsigned int x, unsigned int y, unsigned int z);
void Dispatch(uint32_t x, uint32_t y, uint32_t z) override;
void DispatchIndirect(unsigned int buffer, size_t offset);
void DispatchCompute(unsigned int x, unsigned int y, unsigned int z, unsigned int groupX, unsigned int groupY, unsigned int groupZ);
@@ -137,6 +143,7 @@ public:
void BlitFramebuffer(int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, unsigned int mask, unsigned int filter);
void CopyImageSubData(unsigned int srcName, unsigned int srcTarget, int srcLevel, int srcX, int srcY, int srcZ, unsigned int dstName, unsigned int dstTarget, int dstLevel, int dstX, int dstY, int dstZ, int width, int height, int depth);
void CopyResource(void* dst, void* src) override;
void InvalidateFramebuffer(unsigned int target, unsigned int count, const unsigned int* attachments);
void InvalidateSubFramebuffer(unsigned int target, unsigned int count, const unsigned int* attachments, int x, int y, int width, int height);
@@ -144,6 +151,17 @@ public:
void PushDebugGroup(unsigned int source, unsigned int id, int length, const char* message);
void PopDebugGroup();
void TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) override;
void SetViewport(const Viewport& viewport) override;
void SetViewports(uint32_t count, const Viewport* viewports) override;
void SetScissorRect(const Rect& rect) override;
void SetScissorRects(uint32_t count, const Rect* rects) override;
void SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil = nullptr) override;
void ClearRenderTarget(void* renderTarget, const float color[4]) override;
void ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) override;
void Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t startVertex, uint32_t startInstance) override;
void DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t startIndex, int32_t baseVertex, uint32_t startInstance) override;
private:
unsigned int m_primitiveType;
unsigned int m_currentVAO;

View File

@@ -0,0 +1,43 @@
#pragma once
#include "RHITypes.h"
namespace XCEngine {
namespace RHI {
class RHICommandList {
public:
virtual ~RHICommandList() = default;
virtual void Shutdown() = 0;
virtual void Reset() = 0;
virtual void Close() = 0;
virtual void TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) = 0;
virtual void SetPipelineState(void* pso) = 0;
virtual void SetViewport(const Viewport& viewport) = 0;
virtual void SetViewports(uint32_t count, const Viewport* viewports) = 0;
virtual void SetScissorRect(const Rect& rect) = 0;
virtual void SetScissorRects(uint32_t count, const Rect* rects) = 0;
virtual void SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil = nullptr) = 0;
virtual void SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) = 0;
virtual void SetVertexBuffers(uint32_t startSlot, uint32_t count, const uint64_t* buffers, const uint64_t* offsets, const uint32_t* strides) = 0;
virtual void SetIndexBuffer(void* buffer, uint64_t offset, Format format) = 0;
virtual void Draw(uint32_t vertexCount, uint32_t instanceCount = 1, uint32_t startVertex = 0, uint32_t startInstance = 0) = 0;
virtual void DrawIndexed(uint32_t indexCount, uint32_t instanceCount = 1, uint32_t startIndex = 0, int32_t baseVertex = 0, uint32_t startInstance = 0) = 0;
virtual void Clear(float r, float g, float b, float a, uint32_t buffers) = 0;
virtual void ClearRenderTarget(void* renderTarget, const float color[4]) = 0;
virtual void ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) = 0;
virtual void CopyResource(void* dst, void* src) = 0;
virtual void Dispatch(uint32_t x, uint32_t y, uint32_t z) = 0;
};
} // namespace RHI
} // namespace XCEngine

View File

@@ -343,5 +343,191 @@ void D3D12CommandList::TrackResource(ID3D12Resource* resource) {
}
}
void D3D12CommandList::Reset() {
m_currentTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
m_currentPipelineState = nullptr;
m_currentRootSignature = nullptr;
m_currentDescriptorHeap = nullptr;
}
void D3D12CommandList::TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) {
TransitionBarrierInternal(static_cast<ID3D12Resource*>(resource), stateBefore, stateAfter);
}
void D3D12CommandList::UAVBarrier(void* resource) {
UAVBarrierInternal(static_cast<ID3D12Resource*>(resource));
}
void D3D12CommandList::AliasBarrier(void* beforeResource, void* afterResource) {
AliasBarrierInternal(static_cast<ID3D12Resource*>(beforeResource), static_cast<ID3D12Resource*>(afterResource));
}
void D3D12CommandList::SetPipelineState(void* pso) {
SetPipelineStateInternal(static_cast<ID3D12PipelineState*>(pso));
}
void D3D12CommandList::SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil) {
std::vector<ID3D12Resource*> resources(count);
for (uint32_t i = 0; i < count; ++i) {
resources[i] = static_cast<ID3D12Resource*>(renderTargets[i]);
}
SetRenderTargetsInternal(count, resources.data(), static_cast<ID3D12Resource*>(depthStencil));
}
void D3D12CommandList::SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) {
SetVertexBufferInternal(slot, static_cast<ID3D12Resource*>(buffer), offset, stride);
}
void D3D12CommandList::SetVertexBuffers(uint32_t startSlot, uint32_t count, const uint64_t* buffers, const uint64_t* offsets, const uint32_t* strides) {
std::vector<D3D12_VERTEX_BUFFER_VIEW> views(count);
for (uint32_t i = 0; i < count; ++i) {
views[i].BufferLocation = buffers[i];
views[i].StrideInBytes = strides[i];
views[i].SizeInBytes = 0;
}
SetVertexBuffersInternal(startSlot, count, views.data());
}
void D3D12CommandList::SetIndexBuffer(void* buffer, uint64_t offset, Format format) {
SetIndexBufferInternal(static_cast<ID3D12Resource*>(buffer), offset, format);
}
void D3D12CommandList::Clear(float r, float g, float b, float a, uint32_t buffers) {
float color[4] = { r, g, b, a };
ClearRenderTarget(nullptr, color);
}
void D3D12CommandList::ClearRenderTarget(void* renderTarget, const float color[4]) {
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = {};
if (renderTarget) {
}
m_commandList->ClearRenderTargetView(rtvHandle, color, 0, nullptr);
}
void D3D12CommandList::ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) {
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = {};
if (depthStencil) {
}
m_commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, depth, stencil, 0, nullptr);
}
void D3D12CommandList::CopyResource(void* dst, void* src) {
CopyResourceInternal(static_cast<ID3D12Resource*>(dst), static_cast<ID3D12Resource*>(src));
}
void D3D12CommandList::Dispatch(uint32_t x, uint32_t y, uint32_t z) {
m_commandList->Dispatch(x, y, z);
}
void D3D12CommandList::DispatchIndirect(void* argBuffer, uint64_t alignedByteOffset) {
DispatchIndirectInternal(static_cast<ID3D12Resource*>(argBuffer), alignedByteOffset);
}
void D3D12CommandList::DrawInstancedIndirect(void* argBuffer, uint64_t alignedByteOffset) {
DrawInstancedIndirectInternal(static_cast<ID3D12Resource*>(argBuffer), alignedByteOffset);
}
void D3D12CommandList::DrawIndexedInstancedIndirect(void* argBuffer, uint64_t alignedByteOffset) {
DrawIndexedInstancedIndirectInternal(static_cast<ID3D12Resource*>(argBuffer), alignedByteOffset);
}
void D3D12CommandList::TransitionBarrierInternal(ID3D12Resource* resource, ResourceStates stateBefore, ResourceStates stateAfter, uint32_t subresource) {
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = resource;
barrier.Transition.StateBefore = ToD3D12(stateBefore);
barrier.Transition.StateAfter = ToD3D12(stateAfter);
barrier.Transition.Subresource = subresource;
m_commandList->ResourceBarrier(1, &barrier);
m_resourceStateMap[resource] = stateAfter;
}
void D3D12CommandList::UAVBarrierInternal(ID3D12Resource* resource) {
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.UAV.pResource = resource;
m_commandList->ResourceBarrier(1, &barrier);
}
void D3D12CommandList::AliasBarrierInternal(ID3D12Resource* beforeResource, ID3D12Resource* afterResource) {
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_ALIASING;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Aliasing.pResourceBefore = beforeResource;
barrier.Aliasing.pResourceAfter = afterResource;
m_commandList->ResourceBarrier(1, &barrier);
}
void D3D12CommandList::SetPipelineStateInternal(ID3D12PipelineState* pso) {
m_commandList->SetPipelineState(pso);
m_currentPipelineState = pso;
}
void D3D12CommandList::SetRenderTargetsInternal(uint32_t count, ID3D12Resource** renderTargets, ID3D12Resource* depthStencil) {
std::vector<D3D12_CPU_DESCRIPTOR_HANDLE> rtvHandles(count);
for (uint32_t i = 0; i < count; ++i) {
rtvHandles[i].ptr = 0;
}
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = {};
dsvHandle.ptr = 0;
m_commandList->OMSetRenderTargets(count, count > 0 ? rtvHandles.data() : nullptr, FALSE, depthStencil ? &dsvHandle : nullptr);
}
void D3D12CommandList::SetVertexBufferInternal(uint32_t slot, ID3D12Resource* buffer, uint64_t offset, uint32_t stride) {
D3D12_VERTEX_BUFFER_VIEW view = {};
view.BufferLocation = buffer->GetGPUVirtualAddress() + offset;
view.SizeInBytes = static_cast<UINT>(buffer->GetDesc().Width) - static_cast<UINT>(offset);
view.StrideInBytes = stride;
m_commandList->IASetVertexBuffers(slot, 1, &view);
}
void D3D12CommandList::SetVertexBuffersInternal(uint32_t startSlot, uint32_t count, const D3D12_VERTEX_BUFFER_VIEW* views) {
m_commandList->IASetVertexBuffers(startSlot, count, views);
}
void D3D12CommandList::SetIndexBufferInternal(ID3D12Resource* buffer, uint64_t offset, Format indexFormat) {
D3D12_INDEX_BUFFER_VIEW view = {};
view.BufferLocation = buffer->GetGPUVirtualAddress() + offset;
view.SizeInBytes = static_cast<UINT>(buffer->GetDesc().Width) - static_cast<UINT>(offset);
view.Format = ToD3D12(indexFormat);
m_commandList->IASetIndexBuffer(&view);
}
void D3D12CommandList::ClearRenderTargetView(ID3D12Resource* renderTarget, const float color[4], uint32_t rectCount, const D3D12_RECT* rects) {
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = {};
m_commandList->ClearRenderTargetView(rtvHandle, color, rectCount, rects);
}
void D3D12CommandList::ClearDepthStencilView(ID3D12Resource* depthStencil, uint32_t clearFlags, float depth, uint8_t stencil, uint32_t rectCount, const D3D12_RECT* rects) {
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = {};
m_commandList->ClearDepthStencilView(dsvHandle, static_cast<D3D12_CLEAR_FLAGS>(clearFlags), depth, stencil, rectCount, rects);
}
void D3D12CommandList::CopyResourceInternal(ID3D12Resource* dst, ID3D12Resource* src) {
m_commandList->CopyResource(dst, src);
}
void D3D12CommandList::DispatchIndirectInternal(ID3D12Resource* argBuffer, uint64_t alignedByteOffset) {
m_commandList->ExecuteIndirect(nullptr, 1, argBuffer, alignedByteOffset, nullptr, 0);
}
void D3D12CommandList::DrawInstancedIndirectInternal(ID3D12Resource* argBuffer, uint64_t alignedByteOffset) {
m_commandList->ExecuteIndirect(nullptr, 1, argBuffer, alignedByteOffset, nullptr, 0);
}
void D3D12CommandList::DrawIndexedInstancedIndirectInternal(ID3D12Resource* argBuffer, uint64_t alignedByteOffset) {
m_commandList->ExecuteIndirect(nullptr, 1, argBuffer, alignedByteOffset, nullptr, 0);
}
} // namespace RHI
} // namespace XCEngine

View File

@@ -425,5 +425,90 @@ void OpenGLCommandList::PopDebugGroup() {
glPopDebugGroup();
}
void OpenGLCommandList::Shutdown() {
}
void OpenGLCommandList::Reset() {
}
void OpenGLCommandList::Close() {
}
void OpenGLCommandList::TransitionBarrier(void* resource, ResourceStates stateBefore, ResourceStates stateAfter) {
}
void OpenGLCommandList::SetViewport(const Viewport& viewport) {
glViewport(static_cast<int>(viewport.topLeftX), static_cast<int>(viewport.topLeftY),
static_cast<int>(viewport.width), static_cast<int>(viewport.height));
}
void OpenGLCommandList::SetViewports(uint32_t count, const Viewport* viewports) {
std::vector<float> viewportsGL(count * 6);
for (uint32_t i = 0; i < count; ++i) {
viewportsGL[i * 6 + 0] = viewports[i].topLeftX;
viewportsGL[i * 6 + 1] = viewports[i].topLeftY;
viewportsGL[i * 6 + 2] = viewports[i].width;
viewportsGL[i * 6 + 3] = viewports[i].height;
viewportsGL[i * 6 + 4] = viewports[i].minDepth;
viewportsGL[i * 6 + 5] = viewports[i].maxDepth;
}
glViewportArrayv(0, count, viewportsGL.data());
}
void OpenGLCommandList::SetScissorRect(const Rect& rect) {
glScissor(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
}
void OpenGLCommandList::SetScissorRects(uint32_t count, const Rect* rects) {
std::vector<int> scissorGL(count * 4);
for (uint32_t i = 0; i < count; ++i) {
scissorGL[i * 4 + 0] = rects[i].left;
scissorGL[i * 4 + 1] = rects[i].top;
scissorGL[i * 4 + 2] = rects[i].right - rects[i].left;
scissorGL[i * 4 + 3] = rects[i].bottom - rects[i].top;
}
glScissorArrayv(0, count, scissorGL.data());
}
void OpenGLCommandList::SetRenderTargets(uint32_t count, void** renderTargets, void* depthStencil) {
}
void OpenGLCommandList::ClearRenderTarget(void* renderTarget, const float color[4]) {
glClearColor(color[0], color[1], color[2], color[3]);
glClear(GL_COLOR_BUFFER_BIT);
}
void OpenGLCommandList::ClearDepthStencil(void* depthStencil, float depth, uint8_t stencil) {
glClearDepth(depth);
glClear(GL_DEPTH_BUFFER_BIT);
}
void OpenGLCommandList::SetPipelineState(void* pipelineState) {
if (pipelineState) {
UseShader(reinterpret_cast<uintptr_t>(pipelineState));
}
}
void OpenGLCommandList::SetVertexBuffer(uint32_t slot, void* buffer, uint64_t offset, uint32_t stride) {
}
void OpenGLCommandList::SetVertexBuffers(uint32_t startSlot, uint32_t count, const uint64_t* buffers, const uint64_t* offsets, const uint32_t* strides) {
}
void OpenGLCommandList::SetIndexBuffer(void* buffer, uint64_t offset, Format format) {
}
void OpenGLCommandList::CopyResource(void* dst, void* src) {
}
void OpenGLCommandList::Draw(uint32_t vertexCount, uint32_t instanceCount, uint32_t startVertex, uint32_t startInstance) {
glDrawArraysInstanced(GL_TRIANGLES, static_cast<GLint>(startVertex), static_cast<GLsizei>(vertexCount), static_cast<GLsizei>(instanceCount));
}
void OpenGLCommandList::DrawIndexed(uint32_t indexCount, uint32_t instanceCount, uint32_t startIndex, int32_t baseVertex, uint32_t startInstance) {
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(indexCount), GL_UNSIGNED_INT,
reinterpret_cast<const void*>(startIndex * sizeof(GLuint)), static_cast<GLsizei>(instanceCount));
}
} // namespace RHI
} // namespace XCEngine