Replace vertex/index buffer creation with D3D12Buffer wrapper
This commit is contained in:
@@ -92,7 +92,7 @@ bool D3D12Buffer::InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommand
|
|||||||
device->GetCopyableFootprints(&bufferDesc, 0, 1, 0,
|
device->GetCopyableFootprints(&bufferDesc, 0, 1, 0,
|
||||||
&subresourceFootprint, &rowUsed, &rowSizeInBytes, &memorySizeUsed);
|
&subresourceFootprint, &rowUsed, &rowSizeInBytes, &memorySizeUsed);
|
||||||
|
|
||||||
ComPtr<ID3D12Resource> tempBufferObject;
|
ID3D12Resource* tempBufferObject = nullptr;
|
||||||
D3D12_HEAP_PROPERTIES d3dTempHeapProperties = {};
|
D3D12_HEAP_PROPERTIES d3dTempHeapProperties = {};
|
||||||
d3dTempHeapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
|
d3dTempHeapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||||
|
|
||||||
@@ -129,7 +129,7 @@ bool D3D12Buffer::InitializeWithData(ID3D12Device* device, ID3D12GraphicsCommand
|
|||||||
memcpy(pDstTempBuffer, pSrcData, size);
|
memcpy(pDstTempBuffer, pSrcData, size);
|
||||||
tempBufferObject->Unmap(0, nullptr);
|
tempBufferObject->Unmap(0, nullptr);
|
||||||
|
|
||||||
commandList->CopyBufferRegion(m_resource.Get(), 0, tempBufferObject.Get(), 0, subresourceFootprint.Footprint.Width);
|
commandList->CopyBufferRegion(m_resource.Get(), 0, tempBufferObject, 0, subresourceFootprint.Footprint.Width);
|
||||||
|
|
||||||
D3D12_RESOURCE_BARRIER barrier = {};
|
D3D12_RESOURCE_BARRIER barrier = {};
|
||||||
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||||
|
|||||||
@@ -97,9 +97,6 @@ D3D12_RESOURCE_BARRIER InitResourceBarrier(
|
|||||||
ID3D12Resource* inResource, D3D12_RESOURCE_STATES inPrevState,
|
ID3D12Resource* inResource, D3D12_RESOURCE_STATES inPrevState,
|
||||||
D3D12_RESOURCE_STATES inNextState);
|
D3D12_RESOURCE_STATES inNextState);
|
||||||
|
|
||||||
ID3D12Resource* CreateBufferObject(ID3D12GraphicsCommandList* inCommandList,
|
|
||||||
void* inData, int inDataLen, D3D12_RESOURCE_STATES inFinalResourceState);
|
|
||||||
|
|
||||||
//=================================================================================
|
//=================================================================================
|
||||||
// 工具函数
|
// 工具函数
|
||||||
//=================================================================================
|
//=================================================================================
|
||||||
@@ -121,7 +118,7 @@ struct StaticMeshComponentVertexData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SubMesh {
|
struct SubMesh {
|
||||||
ID3D12Resource* mIBO;
|
XCEngine::RHI::D3D12Buffer mIBO;
|
||||||
D3D12_INDEX_BUFFER_VIEW mIBView;
|
D3D12_INDEX_BUFFER_VIEW mIBView;
|
||||||
int mIndexCount;
|
int mIndexCount;
|
||||||
};
|
};
|
||||||
@@ -132,7 +129,7 @@ struct SubMesh {
|
|||||||
//=================================================================================
|
//=================================================================================
|
||||||
class StaticMeshComponent {
|
class StaticMeshComponent {
|
||||||
public:
|
public:
|
||||||
ID3D12Resource* mVBO;
|
XCEngine::RHI::D3D12Buffer mVBO;
|
||||||
D3D12_VERTEX_BUFFER_VIEW mVBOView;
|
D3D12_VERTEX_BUFFER_VIEW mVBOView;
|
||||||
StaticMeshComponentVertexData* mVertexData;
|
StaticMeshComponentVertexData* mVertexData;
|
||||||
int mVertexCount;
|
int mVertexCount;
|
||||||
@@ -176,10 +173,10 @@ public:
|
|||||||
mVertexCount = temp;
|
mVertexCount = temp;
|
||||||
mVertexData = new StaticMeshComponentVertexData[mVertexCount];
|
mVertexData = new StaticMeshComponentVertexData[mVertexCount];
|
||||||
fread(mVertexData, 1, sizeof(StaticMeshComponentVertexData) * mVertexCount, pFile);
|
fread(mVertexData, 1, sizeof(StaticMeshComponentVertexData) * mVertexCount, pFile);
|
||||||
mVBO = CreateBufferObject(inCommandList, mVertexData,
|
mVBO.InitializeWithData(gDevice.GetDevice(), inCommandList, mVertexData,
|
||||||
sizeof(StaticMeshComponentVertexData) * mVertexCount,
|
sizeof(StaticMeshComponentVertexData) * mVertexCount,
|
||||||
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
|
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
|
||||||
mVBOView.BufferLocation = mVBO->GetGPUVirtualAddress();
|
mVBOView.BufferLocation = mVBO.GetGPUVirtualAddress();
|
||||||
mVBOView.SizeInBytes = sizeof(StaticMeshComponentVertexData) * mVertexCount;
|
mVBOView.SizeInBytes = sizeof(StaticMeshComponentVertexData) * mVertexCount;
|
||||||
mVBOView.StrideInBytes = sizeof(StaticMeshComponentVertexData);
|
mVBOView.StrideInBytes = sizeof(StaticMeshComponentVertexData);
|
||||||
|
|
||||||
@@ -195,11 +192,11 @@ public:
|
|||||||
submesh->mIndexCount = temp;
|
submesh->mIndexCount = temp;
|
||||||
unsigned int* indexes = new unsigned int[temp];
|
unsigned int* indexes = new unsigned int[temp];
|
||||||
fread(indexes, 1, sizeof(unsigned int) * temp, pFile);
|
fread(indexes, 1, sizeof(unsigned int) * temp, pFile);
|
||||||
submesh->mIBO = CreateBufferObject(inCommandList, indexes,
|
submesh->mIBO.InitializeWithData(gDevice.GetDevice(), inCommandList, indexes,
|
||||||
sizeof(unsigned int) * temp,
|
sizeof(unsigned int) * temp,
|
||||||
D3D12_RESOURCE_STATE_INDEX_BUFFER);
|
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.SizeInBytes = sizeof(unsigned int) * temp;
|
||||||
submesh->mIBView.Format = DXGI_FORMAT_R32_UINT;
|
submesh->mIBView.Format = DXGI_FORMAT_R32_UINT;
|
||||||
mSubMeshes.insert(std::pair<std::string, SubMesh*>(name, submesh));
|
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);
|
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)
|
// 渲染管线状态对象 (PSO)
|
||||||
|
|||||||
Reference in New Issue
Block a user