From 189575efec41dd1944564140356a3b873549ee51 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 26 Nov 2022 21:17:05 +0100 Subject: [PATCH] Add `GPUDevice::CreateConstantBuffer` for custom constants buffers usage --- Source/Engine/Graphics/GPUDevice.h | 8 ++++++ .../Graphics/Shaders/GPUConstantBuffer.h | 1 - Source/Engine/Graphics/Shaders/GPUShader.cpp | 12 +++++++-- Source/Engine/Graphics/Shaders/GPUShader.h | 1 - .../DirectX/DX11/GPUContextDX11.cpp | 8 ++++++ .../DirectX/DX11/GPUDeviceDX11.cpp | 25 ++++++++++++++++++ .../DirectX/DX11/GPUDeviceDX11.h | 1 + .../DirectX/DX11/GPUShaderDX11.cpp | 26 ------------------- .../DirectX/DX11/GPUShaderDX11.h | 12 +-------- .../DirectX/DX12/GPUDeviceDX12.cpp | 6 ++++- .../DirectX/DX12/GPUDeviceDX12.h | 1 + .../DirectX/DX12/GPUShaderDX12.cpp | 12 --------- .../DirectX/DX12/GPUShaderDX12.h | 16 ++---------- .../GraphicsDevice/Null/GPUDeviceNull.cpp | 5 ++++ .../GraphicsDevice/Null/GPUDeviceNull.h | 1 + .../GraphicsDevice/Null/GPUShaderNull.h | 5 ---- .../GraphicsDevice/Vulkan/GPUDeviceVulkan.cpp | 5 ++++ .../GraphicsDevice/Vulkan/GPUDeviceVulkan.h | 1 + .../GraphicsDevice/Vulkan/GPUShaderVulkan.cpp | 12 --------- .../GraphicsDevice/Vulkan/GPUShaderVulkan.h | 6 ----- 20 files changed, 73 insertions(+), 91 deletions(-) diff --git a/Source/Engine/Graphics/GPUDevice.h b/Source/Engine/Graphics/GPUDevice.h index f4b8e88ae..5f0f6175c 100644 --- a/Source/Engine/Graphics/GPUDevice.h +++ b/Source/Engine/Graphics/GPUDevice.h @@ -364,6 +364,14 @@ public: /// The native window swap chain. virtual GPUSwapChain* CreateSwapChain(Window* window) = 0; + /// + /// Creates the constant buffer. + /// + /// The resource name. + /// The buffer size (in bytes). + /// The constant buffer. + virtual GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name = StringView::Empty) = 0; + /// /// Creates the GPU tasks context. /// diff --git a/Source/Engine/Graphics/Shaders/GPUConstantBuffer.h b/Source/Engine/Graphics/Shaders/GPUConstantBuffer.h index 0d6eb41f6..0c63ac6f5 100644 --- a/Source/Engine/Graphics/Shaders/GPUConstantBuffer.h +++ b/Source/Engine/Graphics/Shaders/GPUConstantBuffer.h @@ -16,7 +16,6 @@ public: /// /// Gets the buffer size (in bytes). /// - /// The buffer size (in bytes). FORCE_INLINE uint32 GetSize() const { return _size; diff --git a/Source/Engine/Graphics/Shaders/GPUShader.cpp b/Source/Engine/Graphics/Shaders/GPUShader.cpp index 805e48336..7e50b0009 100644 --- a/Source/Engine/Graphics/Shaders/GPUShader.cpp +++ b/Source/Engine/Graphics/Shaders/GPUShader.cpp @@ -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)); } diff --git a/Source/Engine/Graphics/Shaders/GPUShader.h b/Source/Engine/Graphics/Shaders/GPUShader.h index f10da5532..bbc9c4dad 100644 --- a/Source/Engine/Graphics/Shaders/GPUShader.h +++ b/Source/Engine/Graphics/Shaders/GPUShader.h @@ -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] diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp index 74ebdde32..300b1baeb 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUContextDX11.cpp @@ -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& 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& vertexBuffers, const uint32* void GPUContextDX11::BindIB(GPUBuffer* indexBuffer) { + PROFILE_CPU(); const auto ibDX11 = static_cast(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(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(bufferForArgs); @@ -873,6 +880,7 @@ void GPUContextDX11::flushOM() void GPUContextDX11::onDrawCall() { + PROFILE_CPU(); flushCBs(); flushSRVs(); flushUAVs(); diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp index 768cdd883..dbc5da870 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.cpp @@ -701,4 +701,29 @@ GPUSwapChain* GPUDeviceDX11::CreateSwapChain(Window* window) return New(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(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(this, size, memorySize, buffer, name); +} + #endif diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.h b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.h index 5ecd685e6..eb8a73af5 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUDeviceDX11.h @@ -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; }; /// diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp index 145ce948c..c523cd7d5 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.cpp @@ -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(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(); diff --git a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.h b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.h index 69624b12f..8f249aff9 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX11/GPUShaderDX11.h @@ -18,16 +18,7 @@ private: ID3D11Buffer* _resource; public: - - /// - /// Initializes a new instance of the class. - /// - /// The graphics device. - /// The resource name. - /// The buffer size (in bytes). - /// The buffer memory size (in bytes). - /// The buffer. - 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: diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.cpp b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.cpp index 260daf005..bd95b8911 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.cpp @@ -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(this, window); } +GPUConstantBuffer* GPUDeviceDX12::CreateConstantBuffer(uint32 size, const StringView& name) +{ + return New(this, size, name); +} + void GPUDeviceDX12::AddResourceToLateRelease(IGraphicsUnknown* resource, uint32 safeFrameCount) { if (resource == nullptr) diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.h b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.h index f9f30c24b..5913724a5 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUDeviceDX12.h @@ -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; }; /// diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.cpp b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.cpp index 85a22aaaa..926ea3ce4 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.cpp +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.cpp @@ -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) diff --git a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.h b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.h index 1d4cce7d2..db2cb905b 100644 --- a/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.h +++ b/Source/Engine/GraphicsDevice/DirectX/DX12/GPUShaderDX12.h @@ -14,14 +14,8 @@ class GPUConstantBufferDX12 : public GPUResourceDX12 { public: - - /// - /// Initializes a new instance of the class. - /// - /// The graphics device. - /// The buffer size (in bytes). - 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: /// class GPUShaderDX12 : public GPUResourceDX12 { -private: - - Array> _cbs; - public: /// @@ -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 diff --git a/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.cpp b/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.cpp index 53853671d..b0b980913 100644 --- a/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.cpp +++ b/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.cpp @@ -189,6 +189,11 @@ GPUSwapChain* GPUDeviceNull::CreateSwapChain(Window* window) return New(window); } +GPUConstantBuffer* GPUDeviceNull::CreateConstantBuffer(uint32 size, const StringView& name) +{ + return nullptr; +} + GPUDevice* CreateGPUDeviceNull() { return GPUDeviceNull::Create(); diff --git a/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.h b/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.h index 33dfa6687..dea05dfc1 100644 --- a/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.h +++ b/Source/Engine/GraphicsDevice/Null/GPUDeviceNull.h @@ -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(); diff --git a/Source/Engine/GraphicsDevice/Null/GPUShaderNull.h b/Source/Engine/GraphicsDevice/Null/GPUShaderNull.h index 953dd6d9e..e73a4517a 100644 --- a/Source/Engine/GraphicsDevice/Null/GPUShaderNull.h +++ b/Source/Engine/GraphicsDevice/Null/GPUShaderNull.h @@ -19,11 +19,6 @@ protected: return nullptr; } - GPUConstantBuffer* CreateCB(const String& name, uint32 size, MemoryReadStream& stream) override - { - return nullptr; - } - public: // [GPUShader] diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.cpp index e46786b0d..84735cbe3 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.cpp @@ -2045,6 +2045,11 @@ GPUSwapChain* GPUDeviceVulkan::CreateSwapChain(Window* window) return New(this, window); } +GPUConstantBuffer* GPUDeviceVulkan::CreateConstantBuffer(uint32 size, const StringView& name) +{ + return New(this, size); +} + SemaphoreVulkan::SemaphoreVulkan(GPUDeviceVulkan* device) : _device(device) { diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.h b/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.h index f7fc6a221..b4fec211e 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.h +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUDeviceVulkan.h @@ -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; }; /// diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.cpp index b6b2c16d1..43205574b 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.cpp @@ -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 diff --git a/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.h b/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.h index 4d0743fe5..06bf21bda 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.h +++ b/Source/Engine/GraphicsDevice/Vulkan/GPUShaderVulkan.h @@ -126,10 +126,6 @@ public: /// class GPUShaderVulkan : public GPUResourceVulkan { -private: - - Array> _cbs; - public: /// @@ -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