Remove helper functions: CreateConstantBufferObject, UpdateConstantBuffer

This commit is contained in:
2026-03-15 20:05:25 +08:00
parent a557ed75bf
commit b9285f37b1

View File

@@ -300,24 +300,6 @@ ID3D12RootSignature* InitRootSignature() {
}
//=================================================================================
// 常量缓冲 (Constant Buffer) 创建与更新
// UPLOAD堆: CPU可写, GPU可读
//=================================================================================
bool CreateConstantBufferObject(XCEngine::RHI::D3D12Buffer& buffer, int inDataLen) {
return buffer.Initialize(gDevice.GetDevice(), inDataLen, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
}
//=================================================================================
// 更新常量缓冲数据
//=================================================================================
void UpdateConstantBuffer(XCEngine::RHI::D3D12Buffer& buffer, void* inData, int inDataLen) {
D3D12_RANGE d3d12Range = { 0 };
unsigned char* pBuffer = nullptr;
buffer.GetResource()->Map(0, &d3d12Range, (void**)&pBuffer);
memcpy(pBuffer, inData, inDataLen);
buffer.GetResource()->Unmap(0, nullptr);
}
//=================================================================================
//=================================================================================
// 渲染管线状态对象 (PSO)
@@ -618,7 +600,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
gPixelShader.CompileFromFile(L"Res/Shader/gs.hlsl", "MainPS", "ps_5_1");
ID3D12PipelineState* pso = CreatePSO(rootSignature, gVertexShader.GetBytecode(), gPixelShader.GetBytecode(), gGeometryShader.GetBytecode());
CreateConstantBufferObject(gConstantBuffer, 65536);
gConstantBuffer.Initialize(gDevice.GetDevice(), 65536, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
(45.0f * 3.141592f) / 180.0f, 1280.0f / 720.0f, 0.1f, 1000.0f);
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixIdentity();
@@ -639,9 +621,15 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
DirectX::XMStoreFloat4x4(&tempMatrix, modelMatrix);
memcpy(matrices + 48, &tempMatrix, sizeof(float) * 16);
}
UpdateConstantBuffer(gConstantBuffer, matrices, sizeof(float) * 64);
{
D3D12_RANGE d3d12Range = { 0 };
unsigned char* pBuffer = nullptr;
gConstantBuffer.GetResource()->Map(0, &d3d12Range, (void**)&pBuffer);
memcpy(pBuffer, matrices, sizeof(float) * 64);
gConstantBuffer.GetResource()->Unmap(0, nullptr);
}
CreateConstantBufferObject(gMaterialBuffer, 65536);
gMaterialBuffer.Initialize(gDevice.GetDevice(), 65536, D3D12_RESOURCE_STATE_GENERIC_READ, D3D12_HEAP_TYPE_UPLOAD);
struct MaterialData {
float r;
};
@@ -649,7 +637,13 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
for (int i = 0; i < 3000; i++) {
materialDatas[i].r = srandom() * 0.1f + 0.1f;
}
UpdateConstantBuffer(gMaterialBuffer, materialDatas, sizeof(MaterialData) * 3000);
{
D3D12_RANGE d3d12Range = { 0 };
unsigned char* pBuffer = nullptr;
gMaterialBuffer.GetResource()->Map(0, &d3d12Range, (void**)&pBuffer);
memcpy(pBuffer, materialDatas, sizeof(MaterialData) * 3000);
gMaterialBuffer.GetResource()->Unmap(0, nullptr);
}
int imageWidth, imageHeight, imageChannel;
stbi_uc* pixels = stbi_load("Res/Image/earth_d.jpg", &imageWidth, &imageHeight, &imageChannel, 4);