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

@@ -364,6 +364,14 @@ public:
/// <returns>The native window swap chain.</returns>
virtual GPUSwapChain* CreateSwapChain(Window* window) = 0;
/// <summary>
/// Creates the constant buffer.
/// </summary>
/// <param name="name">The resource name.</param>
/// <param name="size">The buffer size (in bytes).</param>
/// <returns>The constant buffer.</returns>
virtual GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name = StringView::Empty) = 0;
/// <summary>
/// Creates the GPU tasks context.
/// </summary>

View File

@@ -16,7 +16,6 @@ public:
/// <summary>
/// Gets the buffer size (in bytes).
/// </summary>
/// <returns>The buffer size (in bytes).</returns>
FORCE_INLINE uint32 GetSize() const
{
return _size;

View File

@@ -4,6 +4,7 @@
#include "GPUConstantBuffer.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Math/Math.h"
#include "Engine/Graphics/GPUDevice.h"
#include "Engine/Serialization/MemoryReadStream.h"
GPUShaderProgramsContainer::GPUShaderProgramsContainer()
@@ -148,7 +149,7 @@ bool GPUShader::Create(MemoryReadStream& stream)
String name;
#endif
ASSERT(_constantBuffers[slotIndex] == nullptr);
const auto cb = CreateCB(name, size, stream);
const auto cb = GPUDevice::Instance->CreateConstantBuffer(size, name);
if (cb == nullptr)
{
LOG(Warning, "Failed to create shader constant buffer.");
@@ -196,7 +197,14 @@ GPUResource::ResourceType GPUShader::GetResourceType() const
void GPUShader::OnReleaseGPU()
{
for (GPUConstantBuffer*& cb : _constantBuffers)
{
if (cb)
{
SAFE_DELETE_GPU_RESOURCE(cb);
cb = nullptr;
}
}
_memoryUsage = 0;
_shaders.Clear();
Platform::MemoryClear(_constantBuffers, sizeof(_constantBuffers));
}

View File

@@ -179,7 +179,6 @@ public:
protected:
GPUShaderProgram* GetShader(ShaderStage stage, const StringAnsiView& name, int32 permutationIndex) const;
virtual GPUShaderProgram* CreateGPUShaderProgram(ShaderStage type, const GPUShaderProgramInitializer& initializer, byte* cacheBytes, uint32 cacheSize, MemoryReadStream& stream) = 0;
virtual GPUConstantBuffer* CreateCB(const String& name, uint32 size, MemoryReadStream& stream) = 0;
public:
// [GPUResource]

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

View File

@@ -189,6 +189,11 @@ GPUSwapChain* GPUDeviceNull::CreateSwapChain(Window* window)
return New<GPUSwapChainNull>(window);
}
GPUConstantBuffer* GPUDeviceNull::CreateConstantBuffer(uint32 size, const StringView& name)
{
return nullptr;
}
GPUDevice* CreateGPUDeviceNull()
{
return GPUDeviceNull::Create();

View File

@@ -48,6 +48,7 @@ public:
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};
extern GPUDevice* CreateGPUDeviceNull();

View File

@@ -19,11 +19,6 @@ protected:
return nullptr;
}
GPUConstantBuffer* CreateCB(const String& name, uint32 size, MemoryReadStream& stream) override
{
return nullptr;
}
public:
// [GPUShader]

View File

@@ -2045,6 +2045,11 @@ GPUSwapChain* GPUDeviceVulkan::CreateSwapChain(Window* window)
return New<GPUSwapChainVulkan>(this, window);
}
GPUConstantBuffer* GPUDeviceVulkan::CreateConstantBuffer(uint32 size, const StringView& name)
{
return New<GPUConstantBufferVulkan>(this, size);
}
SemaphoreVulkan::SemaphoreVulkan(GPUDeviceVulkan* device)
: _device(device)
{

View File

@@ -708,6 +708,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

@@ -234,16 +234,4 @@ GPUShaderProgram* GPUShaderVulkan::CreateGPUShaderProgram(ShaderStage type, cons
return shader;
}
GPUConstantBuffer* GPUShaderVulkan::CreateCB(const String& name, uint32 size, MemoryReadStream& stream)
{
return new(_cbs) GPUConstantBufferVulkan(_device, size);
}
void GPUShaderVulkan::OnReleaseGPU()
{
_cbs.Clear();
GPUShader::OnReleaseGPU();
}
#endif

View File

@@ -126,10 +126,6 @@ public:
/// </summary>
class GPUShaderVulkan : public GPUResourceVulkan<GPUShader>
{
private:
Array<GPUConstantBufferVulkan, FixedAllocation<MAX_CONSTANT_BUFFER_SLOTS>> _cbs;
public:
/// <summary>
@@ -146,8 +142,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