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

@@ -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