Replace constant buffer with D3D12Buffer wrapper
This commit is contained in:
@@ -15,7 +15,7 @@ public:
|
||||
D3D12Buffer();
|
||||
~D3D12Buffer();
|
||||
|
||||
bool Initialize(ID3D12Device* device, uint64_t size, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON);
|
||||
bool Initialize(ID3D12Device* device, uint64_t size, D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON, D3D12_HEAP_TYPE heapType = D3D12_HEAP_TYPE_DEFAULT);
|
||||
bool InitializeFromExisting(ID3D12Resource* resource);
|
||||
void Shutdown();
|
||||
|
||||
|
||||
@@ -10,9 +10,9 @@ D3D12Buffer::~D3D12Buffer() {
|
||||
Shutdown();
|
||||
}
|
||||
|
||||
bool D3D12Buffer::Initialize(ID3D12Device* device, uint64_t size, D3D12_RESOURCE_STATES initialState) {
|
||||
bool D3D12Buffer::Initialize(ID3D12Device* device, uint64_t size, D3D12_RESOURCE_STATES initialState, D3D12_HEAP_TYPE heapType) {
|
||||
D3D12_HEAP_PROPERTIES heapProperties = {};
|
||||
heapProperties.Type = D3D12_HEAP_TYPE_DEFAULT;
|
||||
heapProperties.Type = heapType;
|
||||
heapProperties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
|
||||
heapProperties.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
|
||||
heapProperties.CreationNodeMask = 0;
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "XCEngine/RHI/D3D12/D3D12SwapChain.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12RootSignature.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12PipelineState.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Buffer.h"
|
||||
#include "XCEngine/RHI/D3D12/D3D12Screenshot.h"
|
||||
#include "XCEngine/Debug/Logger.h"
|
||||
#include "XCEngine/Debug/ConsoleLogSink.h"
|
||||
@@ -73,6 +74,10 @@ XCEngine::RHI::D3D12Shader gVertexShader;
|
||||
XCEngine::RHI::D3D12Shader gGeometryShader;
|
||||
XCEngine::RHI::D3D12Shader gPixelShader;
|
||||
|
||||
// Buffer objects
|
||||
XCEngine::RHI::D3D12Buffer gConstantBuffer; // matrices
|
||||
XCEngine::RHI::D3D12Buffer gMaterialBuffer; // material data
|
||||
|
||||
// 同步对象
|
||||
XCEngine::RHI::D3D12Fence gFence;
|
||||
UINT64 gFenceValue = 0;
|
||||
@@ -293,43 +298,19 @@ ID3D12RootSignature* InitRootSignature() {
|
||||
// 常量缓冲 (Constant Buffer) 创建与更新
|
||||
// UPLOAD堆: CPU可写, GPU可读
|
||||
//=================================================================================
|
||||
ID3D12Resource* CreateConstantBufferObject(int inDataLen) {
|
||||
D3D12_HEAP_PROPERTIES d3dHeapProperties = {};
|
||||
d3dHeapProperties.Type = D3D12_HEAP_TYPE_UPLOAD;
|
||||
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_GENERIC_READ,
|
||||
nullptr,
|
||||
IID_PPV_ARGS(&bufferObject)
|
||||
);
|
||||
return bufferObject;
|
||||
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(ID3D12Resource* inCB, void* inData, int inDataLen) {
|
||||
void UpdateConstantBuffer(XCEngine::RHI::D3D12Buffer& buffer, void* inData, int inDataLen) {
|
||||
D3D12_RANGE d3d12Range = { 0 };
|
||||
unsigned char* pBuffer = nullptr;
|
||||
inCB->Map(0, &d3d12Range, (void**)&pBuffer);
|
||||
buffer.GetResource()->Map(0, &d3d12Range, (void**)&pBuffer);
|
||||
memcpy(pBuffer, inData, inDataLen);
|
||||
inCB->Unmap(0, nullptr);
|
||||
buffer.GetResource()->Unmap(0, nullptr);
|
||||
}
|
||||
|
||||
//=================================================================================
|
||||
@@ -833,7 +814,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());
|
||||
|
||||
ID3D12Resource* cb = CreateConstantBufferObject(65536);
|
||||
CreateConstantBufferObject(gConstantBuffer, 65536);
|
||||
DirectX::XMMATRIX projectionMatrix = DirectX::XMMatrixPerspectiveFovLH(
|
||||
(45.0f * 3.141592f) / 180.0f, 1280.0f / 720.0f, 0.1f, 1000.0f);
|
||||
DirectX::XMMATRIX viewMatrix = DirectX::XMMatrixIdentity();
|
||||
@@ -854,9 +835,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
DirectX::XMStoreFloat4x4(&tempMatrix, modelMatrix);
|
||||
memcpy(matrices + 48, &tempMatrix, sizeof(float) * 16);
|
||||
}
|
||||
UpdateConstantBuffer(cb, matrices, sizeof(float) * 64);
|
||||
UpdateConstantBuffer(gConstantBuffer, matrices, sizeof(float) * 64);
|
||||
|
||||
ID3D12Resource* sb = CreateConstantBufferObject(65536);
|
||||
CreateConstantBufferObject(gMaterialBuffer, 65536);
|
||||
struct MaterialData {
|
||||
float r;
|
||||
};
|
||||
@@ -864,7 +845,7 @@ 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(sb, materialDatas, sizeof(MaterialData) * 3000);
|
||||
UpdateConstantBuffer(gMaterialBuffer, materialDatas, sizeof(MaterialData) * 3000);
|
||||
|
||||
int imageWidth, imageHeight, imageChannel;
|
||||
stbi_uc* pixels = stbi_load("Res/Image/earth_d.jpg", &imageWidth, &imageHeight, &imageChannel, 4);
|
||||
@@ -923,10 +904,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
|
||||
gCommandList.SetPipelineState(pso);
|
||||
gCommandList.SetRootSignature(rootSignature);
|
||||
gCommandList.SetDescriptorHeaps(_countof(descriptorHeaps), descriptorHeaps);
|
||||
gCommandList.SetGraphicsRootConstantBufferView(0, cb->GetGPUVirtualAddress());
|
||||
gCommandList.SetGraphicsRootConstantBufferView(0, gConstantBuffer.GetGPUVirtualAddress());
|
||||
gCommandList.SetGraphicsRoot32BitConstants(1, 4, color, 0);
|
||||
gCommandList.SetGraphicsRootDescriptorTable(2, srvHeap.GetGPUDescriptorHandleForHeapStart());
|
||||
gCommandList.SetGraphicsRootShaderResourceView(3, sb->GetGPUVirtualAddress());
|
||||
gCommandList.SetGraphicsRootShaderResourceView(3, gMaterialBuffer.GetGPUVirtualAddress());
|
||||
gCommandList.SetPrimitiveTopology(XCEngine::RHI::PrimitiveTopology::TriangleList);
|
||||
staticMeshComponent.Render(gCommandList.GetCommandList());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user