d3d12: Use separate vertex constant buffer for good

This commit is contained in:
vlj 2015-05-24 19:10:18 +02:00 committed by Vincent Lejeune
parent 6d61e36f5d
commit 12fc6e6145
2 changed files with 38 additions and 24 deletions

View file

@ -25,7 +25,6 @@ static void check(HRESULT hr)
void D3D12GSRender::ResourceStorage::Reset()
{
m_currentVertexBuffersHeapOffset = 0;
m_constantsBufferSize = 0;
m_constantsBufferIndex = 0;
m_currentScaleOffsetBufferIndex = 0;
m_constantsBuffersHeapFreeSpace = 0;
@ -80,15 +79,6 @@ void D3D12GSRender::ResourceStorage::Init(ID3D12Device *device)
IID_PPV_ARGS(&m_indexBuffer)
));
check(device->CreateCommittedResource(
&heapProp,
D3D12_HEAP_FLAG_NONE,
&resDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&m_constantsVertexBuffer)
));
D3D12_DESCRIPTOR_HEAP_DESC descriptorHeapDesc = {};
descriptorHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
descriptorHeapDesc.NumDescriptors = 1000; // For safety
@ -129,7 +119,6 @@ void D3D12GSRender::ResourceStorage::Release()
m_backbufferAsRendertarget->Release();
m_constantsBufferDescriptorsHeap->Release();
m_scaleOffsetDescriptorHeap->Release();
m_constantsVertexBuffer->Release();
m_constantsBuffersHeap->Release();
m_vertexBuffersHeap->Release();
m_backBuffer->Release();
@ -322,6 +311,8 @@ D3D12GSRender::D3D12GSRender()
m_currentResourceStorageIndex = m_swapChain->GetCurrentBackBufferIndex();
compileF32toU8CS();
vertexConstantShadowCopy = new float[512 * 4];
}
D3D12GSRender::~D3D12GSRender()
@ -335,6 +326,7 @@ D3D12GSRender::~D3D12GSRender()
m_rootSignature->Release();
m_swapChain->Release();
m_device->Release();
delete[] vertexConstantShadowCopy;
}
D3D12GSRender::ResourceStorage &D3D12GSRender::getCurrentResourceStorage()
@ -645,27 +637,49 @@ void D3D12GSRender::setScaleOffset()
void D3D12GSRender::FillVertexShaderConstantsBuffer()
{
void *constantsBufferMap;
check(getCurrentResourceStorage().m_constantsVertexBuffer->Map(0, nullptr, &constantsBufferMap));
for (const RSXTransformConstant& c : m_transform_constants)
{
size_t offset = c.id * 4 * sizeof(float);
float vector[] = { c.x, c.y, c.z, c.w };
memcpy((char*)constantsBufferMap + offset, vector, 4 * sizeof(float));
size_t bufferSizeCandidate = offset + 4 * sizeof(float);
getCurrentResourceStorage().m_constantsBufferSize = bufferSizeCandidate > getCurrentResourceStorage().m_constantsBufferSize ? bufferSizeCandidate : getCurrentResourceStorage().m_constantsBufferSize;
memcpy((char*)vertexConstantShadowCopy + offset, vector, 4 * sizeof(float));
}
getCurrentResourceStorage().m_constantsVertexBuffer->Unmap(0, nullptr);
// make it multiple of 256 bytes
getCurrentResourceStorage().m_constantsBufferSize = (getCurrentResourceStorage().m_constantsBufferSize + 255) & ~255;
size_t constantBuffersHeapOffset = getCurrentResourceStorage().m_constantsBuffersHeapFreeSpace;
// 65536 alignment
constantBuffersHeapOffset = (constantBuffersHeapOffset + 65536 - 1) & ~65535;
D3D12_RESOURCE_DESC resDesc = {};
resDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
resDesc.Width = 512 * 4 * sizeof(float);
resDesc.Height = 1;
resDesc.DepthOrArraySize = 1;
resDesc.SampleDesc.Count = 1;
resDesc.MipLevels = 1;
resDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
ID3D12Resource *constantsBuffer;
check(m_device->CreatePlacedResource(
getCurrentResourceStorage().m_constantsBuffersHeap,
constantBuffersHeapOffset,
&resDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(&constantsBuffer)
));
void *constantsBufferMap;
check(constantsBuffer->Map(0, nullptr, &constantsBufferMap));
memcpy(constantsBufferMap, vertexConstantShadowCopy, 512 * 4 * sizeof(float));
constantsBuffer->Unmap(0, nullptr);
D3D12_CONSTANT_BUFFER_VIEW_DESC constantBufferViewDesc = {};
constantBufferViewDesc.BufferLocation = getCurrentResourceStorage().m_constantsVertexBuffer->GetGPUVirtualAddress();
constantBufferViewDesc.SizeInBytes = (UINT)getCurrentResourceStorage().m_constantsBufferSize;
constantBufferViewDesc.BufferLocation = constantsBuffer->GetGPUVirtualAddress();
constantBufferViewDesc.SizeInBytes = 512 * 4 * sizeof(float);
D3D12_CPU_DESCRIPTOR_HANDLE Handle = getCurrentResourceStorage().m_constantsBufferDescriptorsHeap->GetCPUDescriptorHandleForHeapStart();
Handle.ptr += getCurrentResourceStorage().m_constantsBufferIndex * m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
m_device->CreateConstantBufferView(&constantBufferViewDesc, Handle);
getCurrentResourceStorage().m_constantsBuffersHeapFreeSpace = constantBuffersHeapOffset + 512 * 4 * sizeof(float);
getCurrentResourceStorage().m_inflightResources.push_back(constantsBuffer);
}
static

View file

@ -65,11 +65,10 @@ private:
ID3D12Resource *m_indexBuffer;
// Constants storage
ID3D12Resource *m_constantsVertexBuffer;
ID3D12Heap *m_constantsBuffersHeap;
size_t m_constantsBuffersHeapFreeSpace;
ID3D12DescriptorHeap *m_constantsBufferDescriptorsHeap;
size_t m_constantsBufferSize, m_constantsBufferIndex;
size_t m_constantsBufferIndex;
ID3D12DescriptorHeap *m_scaleOffsetDescriptorHeap;
size_t m_currentScaleOffsetBufferIndex;
@ -113,6 +112,7 @@ public:
GSFrameBase2 *m_frame;
u32 m_draw_frames;
u32 m_skip_frames;
float *vertexConstantShadowCopy;
D3D12GSRender();
virtual ~D3D12GSRender();