Replace vertex/index buffer creation with D3D12Buffer wrapper

This commit is contained in:
2026-03-15 19:58:22 +08:00
parent 38e23e45c4
commit 13818fe641
2 changed files with 8 additions and 75 deletions

View File

@@ -97,9 +97,6 @@ D3D12_RESOURCE_BARRIER InitResourceBarrier(
ID3D12Resource* inResource, D3D12_RESOURCE_STATES inPrevState,
D3D12_RESOURCE_STATES inNextState);
ID3D12Resource* CreateBufferObject(ID3D12GraphicsCommandList* inCommandList,
void* inData, int inDataLen, D3D12_RESOURCE_STATES inFinalResourceState);
//=================================================================================
// 工具函数
//=================================================================================
@@ -121,7 +118,7 @@ struct StaticMeshComponentVertexData {
};
struct SubMesh {
ID3D12Resource* mIBO;
XCEngine::RHI::D3D12Buffer mIBO;
D3D12_INDEX_BUFFER_VIEW mIBView;
int mIndexCount;
};
@@ -132,7 +129,7 @@ struct SubMesh {
//=================================================================================
class StaticMeshComponent {
public:
ID3D12Resource* mVBO;
XCEngine::RHI::D3D12Buffer mVBO;
D3D12_VERTEX_BUFFER_VIEW mVBOView;
StaticMeshComponentVertexData* mVertexData;
int mVertexCount;
@@ -176,10 +173,10 @@ public:
mVertexCount = temp;
mVertexData = new StaticMeshComponentVertexData[mVertexCount];
fread(mVertexData, 1, sizeof(StaticMeshComponentVertexData) * mVertexCount, pFile);
mVBO = CreateBufferObject(inCommandList, mVertexData,
mVBO.InitializeWithData(gDevice.GetDevice(), inCommandList, mVertexData,
sizeof(StaticMeshComponentVertexData) * mVertexCount,
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
mVBOView.BufferLocation = mVBO->GetGPUVirtualAddress();
mVBOView.BufferLocation = mVBO.GetGPUVirtualAddress();
mVBOView.SizeInBytes = sizeof(StaticMeshComponentVertexData) * mVertexCount;
mVBOView.StrideInBytes = sizeof(StaticMeshComponentVertexData);
@@ -195,11 +192,11 @@ public:
submesh->mIndexCount = temp;
unsigned int* indexes = new unsigned int[temp];
fread(indexes, 1, sizeof(unsigned int) * temp, pFile);
submesh->mIBO = CreateBufferObject(inCommandList, indexes,
submesh->mIBO.InitializeWithData(gDevice.GetDevice(), inCommandList, indexes,
sizeof(unsigned int) * temp,
D3D12_RESOURCE_STATE_INDEX_BUFFER);
submesh->mIBView.BufferLocation = submesh->mIBO->GetGPUVirtualAddress();
submesh->mIBView.BufferLocation = submesh->mIBO.GetGPUVirtualAddress();
submesh->mIBView.SizeInBytes = sizeof(unsigned int) * temp;
submesh->mIBView.Format = DXGI_FORMAT_R32_UINT;
mSubMeshes.insert(std::pair<std::string, SubMesh*>(name, submesh));
@@ -321,70 +318,6 @@ void UpdateConstantBuffer(XCEngine::RHI::D3D12Buffer& buffer, void* inData, int
buffer.GetResource()->Unmap(0, nullptr);
}
//=================================================================================
// GPU Buffer创建 (顶点/索引缓冲)
// DEFAULT堆 → 通过Upload堆中转数据 → 状态转换
//=================================================================================
ID3D12Resource* CreateBufferObject(ID3D12GraphicsCommandList* inCommandList,
void* inData, int inDataLen, D3D12_RESOURCE_STATES inFinalResourceState) {
D3D12_HEAP_PROPERTIES d3dHeapProperties = {};
d3dHeapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_DESC d3d12ResourceDesc = {};
d3d12ResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
d3d12ResourceDesc.Alignment = 0;
d3d12ResourceDesc.Width = inDataLen;
d3d12ResourceDesc.Height = 1;
d3d12ResourceDesc.DepthOrArraySize = 1;
d3d12ResourceDesc.MipLevels = 1;
d3d12ResourceDesc.Format = DXGI_FORMAT_UNKNOWN;
d3d12ResourceDesc.SampleDesc.Count = 1;
d3d12ResourceDesc.SampleDesc.Quality = 0;
d3d12ResourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
d3d12ResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
ID3D12Resource* bufferObject = nullptr;
gDevice.GetDevice()->CreateCommittedResource(
&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(&bufferObject)
);
d3d12ResourceDesc = bufferObject->GetDesc();
UINT64 memorySizeUsed = 0;
UINT64 rowSizeInBytes = 0;
UINT rowUsed = 0;
D3D12_PLACED_SUBRESOURCE_FOOTPRINT subresourceFootprint;
gDevice.GetDevice()->GetCopyableFootprints(&d3d12ResourceDesc, 0, 1, 0,
&subresourceFootprint, &rowUsed, &rowSizeInBytes, &memorySizeUsed);
ID3D12Resource* tempBufferObject = nullptr;
d3dHeapProperties = {};
d3dHeapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
gDevice.GetDevice()->CreateCommittedResource(
&d3dHeapProperties,
D3D12_HEAP_FLAG_NONE,
&d3d12ResourceDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&tempBufferObject)
);
BYTE* pData;
tempBufferObject->Map(0, nullptr, reinterpret_cast<void**>(&pData));
BYTE* pDstTempBuffer = reinterpret_cast<BYTE*>(pData + subresourceFootprint.Offset);
const BYTE* pSrcData = reinterpret_cast<BYTE*>(inData);
for (UINT i = 0; i < rowUsed; i++) {
memcpy(pDstTempBuffer + subresourceFootprint.Footprint.RowPitch * i, pSrcData + rowSizeInBytes * i, rowSizeInBytes);
}
tempBufferObject->Unmap(0, nullptr);
inCommandList->CopyBufferRegion(bufferObject, 0, tempBufferObject, 0, subresourceFootprint.Footprint.Width);
D3D12_RESOURCE_BARRIER barrier = InitResourceBarrier(bufferObject, D3D12_RESOURCE_STATE_COPY_DEST, inFinalResourceState);
inCommandList->ResourceBarrier(1, &barrier);
return bufferObject;
}
//=================================================================================
//=================================================================================
// 渲染管线状态对象 (PSO)