Add GPUDevice::CreateConstantBuffer for custom constants buffers usage

This commit is contained in:
Wojtek Figat
2022-11-26 21:17:05 +01:00
parent 39dc439cd8
commit 189575efec
20 changed files with 73 additions and 91 deletions

View File

@@ -1,5 +1,6 @@
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
#include "Engine/Profiler/ProfilerCPU.h"
#if GRAPHICS_API_DIRECTX11
#include "GPUContextDX11.h"
@@ -348,6 +349,7 @@ void GPUContextDX11::BindUA(int32 slot, GPUResourceView* view)
void GPUContextDX11::BindVB(const Span<GPUBuffer*>& vertexBuffers, const uint32* vertexBuffersOffsets)
{
PROFILE_CPU();
ASSERT(vertexBuffers.Length() >= 0 && vertexBuffers.Length() <= GPU_MAX_VB_BINDED);
bool vbEdited = false;
@@ -372,6 +374,7 @@ void GPUContextDX11::BindVB(const Span<GPUBuffer*>& vertexBuffers, const uint32*
void GPUContextDX11::BindIB(GPUBuffer* indexBuffer)
{
PROFILE_CPU();
const auto ibDX11 = static_cast<GPUBufferDX11*>(indexBuffer);
if (ibDX11 != _ibHandle)
{
@@ -452,6 +455,7 @@ void GPUContextDX11::ResolveMultisample(GPUTexture* sourceMultisampleTexture, GP
void GPUContextDX11::DrawInstanced(uint32 verticesCount, uint32 instanceCount, int32 startInstance, int32 startVertex)
{
PROFILE_CPU();
onDrawCall();
if (instanceCount > 1)
_context->DrawInstanced(verticesCount, instanceCount, startVertex, startInstance);
@@ -462,6 +466,7 @@ void GPUContextDX11::DrawInstanced(uint32 verticesCount, uint32 instanceCount, i
void GPUContextDX11::DrawIndexedInstanced(uint32 indicesCount, uint32 instanceCount, int32 startInstance, int32 startVertex, int32 startIndex)
{
PROFILE_CPU();
onDrawCall();
if (instanceCount > 1)
_context->DrawIndexedInstanced(indicesCount, instanceCount, startIndex, startVertex, startInstance);
@@ -472,6 +477,7 @@ void GPUContextDX11::DrawIndexedInstanced(uint32 indicesCount, uint32 instanceCo
void GPUContextDX11::DrawInstancedIndirect(GPUBuffer* bufferForArgs, uint32 offsetForArgs)
{
PROFILE_CPU();
ASSERT(bufferForArgs && bufferForArgs->GetFlags() & GPUBufferFlags::Argument);
const auto bufferForArgsDX11 = static_cast<GPUBufferDX11*>(bufferForArgs);
@@ -483,6 +489,7 @@ void GPUContextDX11::DrawInstancedIndirect(GPUBuffer* bufferForArgs, uint32 offs
void GPUContextDX11::DrawIndexedInstancedIndirect(GPUBuffer* bufferForArgs, uint32 offsetForArgs)
{
PROFILE_CPU();
ASSERT(bufferForArgs && bufferForArgs->GetFlags() & GPUBufferFlags::Argument);
const auto bufferForArgsDX11 = static_cast<GPUBufferDX11*>(bufferForArgs);
@@ -873,6 +880,7 @@ void GPUContextDX11::flushOM()
void GPUContextDX11::onDrawCall()
{
PROFILE_CPU();
flushCBs();
flushSRVs();
flushUAVs();

View File

@@ -701,4 +701,29 @@ GPUSwapChain* GPUDeviceDX11::CreateSwapChain(Window* window)
return New<GPUSwapChainDX11>(this, window);
}
GPUConstantBuffer* GPUDeviceDX11::CreateConstantBuffer(uint32 size, const StringView& name)
{
ID3D11Buffer* buffer = nullptr;
uint32 memorySize = 0;
if (size)
{
// Create buffer
D3D11_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = Math::AlignUp<uint32>(size, 16);
cbDesc.Usage = D3D11_USAGE_DEFAULT;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = 0;
cbDesc.MiscFlags = 0;
cbDesc.StructureByteStride = 0;
const HRESULT result = _device->CreateBuffer(&cbDesc, nullptr, &buffer);
if (FAILED(result))
{
LOG_DIRECTX_RESULT(result);
return nullptr;
}
memorySize = cbDesc.ByteWidth;
}
return New<GPUConstantBufferDX11>(this, size, memorySize, buffer, name);
}
#endif

View File

@@ -98,6 +98,7 @@ public:
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};
/// <summary>

View File

@@ -159,32 +159,6 @@ GPUShaderProgram* GPUShaderDX11::CreateGPUShaderProgram(ShaderStage type, const
return shader;
}
GPUConstantBuffer* GPUShaderDX11::CreateCB(const String& name, uint32 size, MemoryReadStream& stream)
{
ID3D11Buffer* buffer = nullptr;
uint32 memorySize = 0;
if (size)
{
// Create buffer
D3D11_BUFFER_DESC cbDesc;
cbDesc.ByteWidth = Math::AlignUp<uint32>(size, 16);
cbDesc.Usage = D3D11_USAGE_DEFAULT;
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbDesc.CPUAccessFlags = 0;
cbDesc.MiscFlags = 0;
cbDesc.StructureByteStride = 0;
const HRESULT result = _device->GetDevice()->CreateBuffer(&cbDesc, nullptr, &buffer);
if (FAILED(result))
{
LOG_DIRECTX_RESULT(result);
return nullptr;
}
memorySize = cbDesc.ByteWidth;
}
return new(_cbs) GPUConstantBufferDX11(_device, name, size, memorySize, buffer);
}
void GPUShaderDX11::OnReleaseGPU()
{
_cbs.Clear();

View File

@@ -18,16 +18,7 @@ private:
ID3D11Buffer* _resource;
public:
/// <summary>
/// Initializes a new instance of the <see cref="GPUConstantBufferDX11"/> class.
/// </summary>
/// <param name="device">The graphics device.</param>
/// <param name="name">The resource name.</param>
/// <param name="size">The buffer size (in bytes).</param>
/// <param name="memorySize">The buffer memory size (in bytes).</param>
/// <param name="buffer">The buffer.</param>
GPUConstantBufferDX11(GPUDeviceDX11* device, const String& name, uint32 size, uint32 memorySize, ID3D11Buffer* buffer) noexcept
GPUConstantBufferDX11(GPUDeviceDX11* device, uint32 size, uint32 memorySize, ID3D11Buffer* buffer, const StringView& name) noexcept
: GPUResourceDX11(device, name)
, _resource(buffer)
{
@@ -97,7 +88,6 @@ protected:
// [GPUShader]
GPUShaderProgram* CreateGPUShaderProgram(ShaderStage type, const GPUShaderProgramInitializer& initializer, byte* cacheBytes, uint32 cacheSize, MemoryReadStream& stream) override;
GPUConstantBuffer* CreateCB(const String& name, uint32 size, MemoryReadStream& stream) override;
void OnReleaseGPU() override;
public:

View File

@@ -14,7 +14,6 @@
#include "Engine/Engine/Engine.h"
#include "Engine/Engine/CommandLine.h"
#include "Engine/Graphics/RenderTask.h"
#include "Engine/Graphics/Async/GPUTasksExecutor.h"
#include "Engine/GraphicsDevice/DirectX/RenderToolsDX.h"
#include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Core/Log.h"
@@ -795,6 +794,11 @@ GPUSwapChain* GPUDeviceDX12::CreateSwapChain(Window* window)
return New<GPUSwapChainDX12>(this, window);
}
GPUConstantBuffer* GPUDeviceDX12::CreateConstantBuffer(uint32 size, const StringView& name)
{
return New<GPUConstantBufferDX12>(this, size, name);
}
void GPUDeviceDX12::AddResourceToLateRelease(IGraphicsUnknown* resource, uint32 safeFrameCount)
{
if (resource == nullptr)

View File

@@ -197,6 +197,7 @@ public:
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};
/// <summary>

View File

@@ -127,18 +127,6 @@ GPUShaderProgram* GPUShaderDX12::CreateGPUShaderProgram(ShaderStage type, const
return shader;
}
GPUConstantBuffer* GPUShaderDX12::CreateCB(const String& name, uint32 size, MemoryReadStream& stream)
{
return new(_cbs) GPUConstantBufferDX12(_device, size);
}
void GPUShaderDX12::OnReleaseGPU()
{
_cbs.Clear();
GPUShader::OnReleaseGPU();
}
ID3D12PipelineState* GPUShaderProgramCSDX12::GetOrCreateState()
{
if (_state)

View File

@@ -14,14 +14,8 @@
class GPUConstantBufferDX12 : public GPUResourceDX12<GPUConstantBuffer>
{
public:
/// <summary>
/// Initializes a new instance of the <see cref="GPUConstantBufferDX12"/> class.
/// </summary>
/// <param name="device">The graphics device.</param>
/// <param name="size">The buffer size (in bytes).</param>
GPUConstantBufferDX12(GPUDeviceDX12* device, uint32 size) noexcept
: GPUResourceDX12(device, String::Empty)
GPUConstantBufferDX12(GPUDeviceDX12* device, uint32 size, const StringView& name) noexcept
: GPUResourceDX12(device, name)
, GPUAddress(0)
{
_size = size;
@@ -40,10 +34,6 @@ public:
/// </summary>
class GPUShaderDX12 : public GPUResourceDX12<GPUShader>
{
private:
Array<GPUConstantBufferDX12, FixedAllocation<MAX_CONSTANT_BUFFER_SLOTS>> _cbs;
public:
/// <summary>
@@ -60,8 +50,6 @@ protected:
// [GPUShader]
GPUShaderProgram* CreateGPUShaderProgram(ShaderStage type, const GPUShaderProgramInitializer& initializer, byte* cacheBytes, uint32 cacheSize, MemoryReadStream& stream) override;
GPUConstantBuffer* CreateCB(const String& name, uint32 size, MemoryReadStream& stream) override;
void OnReleaseGPU() override;
};
#endif