Add GPUVertexLayout to graphics backends

This commit is contained in:
Wojtek Figat
2024-12-13 09:20:01 +01:00
parent cedf4b1eb5
commit fc4e6f4972
31 changed files with 605 additions and 93 deletions

View File

@@ -10,11 +10,13 @@
#include "GPUTimerQueryDX11.h"
#include "GPUBufferDX11.h"
#include "GPUSamplerDX11.h"
#include "GPUVertexLayoutDX11.h"
#include "GPUSwapChainDX11.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Utilities.h"
#include "Engine/Threading/Threading.h"
#include "Engine/GraphicsDevice/DirectX/RenderToolsDX.h"
#include "Engine/Graphics/PixelFormatExtensions.h"
#include "Engine/Engine/CommandLine.h"
#if !USE_EDITOR && PLATFORM_WINDOWS
@@ -146,6 +148,29 @@ static bool TryCreateDevice(IDXGIAdapter* adapter, D3D_FEATURE_LEVEL maxFeatureL
return false;
}
GPUVertexLayoutDX11::GPUVertexLayoutDX11(GPUDeviceDX11* device, const Elements& elements)
: GPUResourceBase<GPUDeviceDX11, GPUVertexLayout>(device, StringView::Empty)
, InputElementsCount(elements.Count())
{
_elements = elements;
uint32 offsets[GPU_MAX_VB_BINDED] = {};
for (int32 i = 0; i < _elements.Count(); i++)
{
const VertexElement& src = _elements.Get()[i];
D3D11_INPUT_ELEMENT_DESC& dst = InputElements[i];
uint32& offset = offsets[src.Slot];
if (src.Offset != 0)
offset = src.Offset;
dst.SemanticName = RenderToolsDX::GetVertexInputSemantic(src.Type, dst.SemanticIndex);
dst.Format = RenderToolsDX::ToDxgiFormat(src.Format);
dst.InputSlot = src.Slot;
dst.AlignedByteOffset = offset;
dst.InputSlotClass = src.PerInstance ? D3D11_INPUT_PER_INSTANCE_DATA : D3D11_INPUT_PER_VERTEX_DATA;
dst.InstanceDataStepRate = src.PerInstance ? 1 : 0;
offset += PixelFormatExtensions::SizeInBytes(src.Format);
}
}
GPUDevice* GPUDeviceDX11::Create()
{
// Configuration
@@ -807,6 +832,11 @@ GPUSampler* GPUDeviceDX11::CreateSampler()
return New<GPUSamplerDX11>(this);
}
GPUVertexLayout* GPUDeviceDX11::CreateVertexLayout(const VertexElements& elements)
{
return New<GPUVertexLayoutDX11>(this, elements);
}
GPUSwapChain* GPUDeviceDX11::CreateSwapChain(Window* window)
{
return New<GPUSwapChainDX11>(this, window);

View File

@@ -128,6 +128,7 @@ public:
GPUTimerQuery* CreateTimerQuery() override;
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUVertexLayout* CreateVertexLayout(const VertexElements& elements) override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};

View File

@@ -14,7 +14,6 @@
class GPUConstantBufferDX11 : public GPUResourceDX11<GPUConstantBuffer>
{
private:
ID3D11Buffer* _resource;
public:
@@ -36,7 +35,6 @@ public:
}
public:
/// <summary>
/// Gets the constant buffer object.
/// </summary>
@@ -47,7 +45,6 @@ public:
}
public:
// [GPUResourceDX11]
ID3D11Resource* GetResource() override
{
@@ -55,7 +52,6 @@ public:
}
public:
// [GPUResourceDX11]
void OnReleaseGPU() final override
{
@@ -69,11 +65,9 @@ public:
class GPUShaderDX11 : public GPUResourceDX11<GPUShader>
{
private:
Array<GPUConstantBufferDX11, FixedAllocation<MAX_CONSTANT_BUFFER_SLOTS>> _cbs;
Array<GPUConstantBufferDX11, FixedAllocation<GPU_MAX_CB_BINDED>> _cbs;
public:
/// <summary>
/// Initializes a new instance of the <see cref="GPUShaderDX11"/> class.
/// </summary>
@@ -85,13 +79,11 @@ public:
}
protected:
// [GPUShader]
GPUShaderProgram* CreateGPUShaderProgram(ShaderStage type, const GPUShaderProgramInitializer& initializer, byte* cacheBytes, uint32 cacheSize, MemoryReadStream& stream) override;
void OnReleaseGPU() override;
public:
// [GPUResourceDX11]
ID3D11Resource* GetResource() final override
{

View File

@@ -0,0 +1,22 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#if GRAPHICS_API_DIRECTX11
#include "Engine/Graphics/Shaders/GPUVertexLayout.h"
#include "GPUDeviceDX11.h"
/// <summary>
/// Vertex layout object for DirectX 11 backend.
/// </summary>
class GPUVertexLayoutDX11 : public GPUResourceBase<GPUDeviceDX11, GPUVertexLayout>
{
public:
GPUVertexLayoutDX11(GPUDeviceDX11* device, const Elements& elements);
uint32 InputElementsCount;
D3D11_INPUT_ELEMENT_DESC InputElements[GPU_MAX_VS_ELEMENTS];
};
#endif

View File

@@ -10,10 +10,12 @@
#include "GPUTimerQueryDX12.h"
#include "GPUBufferDX12.h"
#include "GPUSamplerDX12.h"
#include "GPUVertexLayoutDX12.h"
#include "GPUSwapChainDX12.h"
#include "Engine/Engine/Engine.h"
#include "Engine/Engine/CommandLine.h"
#include "Engine/Graphics/RenderTask.h"
#include "Engine/Graphics/PixelFormatExtensions.h"
#include "Engine/GraphicsDevice/DirectX/RenderToolsDX.h"
#include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Core/Log.h"
@@ -34,6 +36,29 @@ static bool CheckDX12Support(IDXGIAdapter* adapter)
return false;
}
GPUVertexLayoutDX12::GPUVertexLayoutDX12(GPUDeviceDX12* device, const Elements& elements)
: GPUResourceDX12<GPUVertexLayout>(device, StringView::Empty)
, InputElementsCount(elements.Count())
{
_elements = elements;
uint32 offsets[GPU_MAX_VB_BINDED] = {};
for (int32 i = 0; i < _elements.Count(); i++)
{
const VertexElement& src = _elements.Get()[i];
D3D12_INPUT_ELEMENT_DESC& dst = InputElements[i];
uint32& offset = offsets[src.Slot];
if (src.Offset != 0)
offset = src.Offset;
dst.SemanticName = RenderToolsDX::GetVertexInputSemantic(src.Type, dst.SemanticIndex);
dst.Format = RenderToolsDX::ToDxgiFormat(src.Format);
dst.InputSlot = src.Slot;
dst.AlignedByteOffset = offset;
dst.InputSlotClass = src.PerInstance ? D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA : D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
dst.InstanceDataStepRate = src.PerInstance ? 1 : 0;
offset += PixelFormatExtensions::SizeInBytes(src.Format);
}
}
GPUDevice* GPUDeviceDX12::Create()
{
#if PLATFORM_XBOX_SCARLETT || PLATFORM_XBOX_ONE
@@ -843,6 +868,11 @@ GPUSampler* GPUDeviceDX12::CreateSampler()
return New<GPUSamplerDX12>(this);
}
GPUVertexLayout* GPUDeviceDX12::CreateVertexLayout(const VertexElements& elements)
{
return New<GPUVertexLayoutDX12>(this, elements);
}
GPUSwapChain* GPUDeviceDX12::CreateSwapChain(Window* window)
{
return New<GPUSwapChainDX12>(this, window);

View File

@@ -196,6 +196,7 @@ public:
GPUTimerQuery* CreateTimerQuery() override;
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUVertexLayout* CreateVertexLayout(const VertexElements& elements) override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};

View File

@@ -22,7 +22,6 @@ public:
}
public:
/// <summary>
/// Last uploaded data address.
/// </summary>
@@ -35,7 +34,6 @@ public:
class GPUShaderDX12 : public GPUResourceDX12<GPUShader>
{
public:
/// <summary>
/// Initializes a new instance of the <see cref="GPUShaderDX12"/> class.
/// </summary>
@@ -47,7 +45,6 @@ public:
}
protected:
// [GPUShader]
GPUShaderProgram* CreateGPUShaderProgram(ShaderStage type, const GPUShaderProgramInitializer& initializer, byte* cacheBytes, uint32 cacheSize, MemoryReadStream& stream) override;
};

View File

@@ -35,6 +35,7 @@ public:
{
return (void*)_data.Get();
}
uint32 GetBufferSize() const override
{
return _data.Count();
@@ -65,6 +66,7 @@ public:
{
return (void*)_inputLayout;
}
byte GetInputLayoutSize() const override
{
return _inputLayoutSize;

View File

@@ -0,0 +1,22 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#if GRAPHICS_API_DIRECTX12
#include "Engine/Graphics/Shaders/GPUVertexLayout.h"
#include "GPUDeviceDX12.h"
/// <summary>
/// Vertex layout object for DirectX 12 backend.
/// </summary>
class GPUVertexLayoutDX12 : public GPUResourceDX12<GPUVertexLayout>
{
public:
GPUVertexLayoutDX12(GPUDeviceDX12* device, const Elements& elements);
uint32 InputElementsCount;
D3D12_INPUT_ELEMENT_DESC InputElements[GPU_MAX_VS_ELEMENTS];
};
#endif

View File

@@ -273,4 +273,62 @@ String RenderToolsDX::GetD3DErrorString(HRESULT errorCode)
return sb.ToString();
}
LPCSTR RenderToolsDX::GetVertexInputSemantic(VertexElement::Types type, UINT& semanticIndex)
{
static_assert((int32)VertexElement::Types::MAX == 16, "Update code below.");
semanticIndex = 0;
switch (type)
{
case VertexElement::Types::Position:
return "POSITION";
case VertexElement::Types::Color:
return "COLOR";
case VertexElement::Types::Normal:
return "NORMAL";
case VertexElement::Types::Tangent:
return "TANGENT";
case VertexElement::Types::BlendIndices:
return "BLENDINDICES";
case VertexElement::Types::BlendWeight:
return "BLENDWEIGHT";
case VertexElement::Types::TexCoord0:
return "TEXCOORD";
case VertexElement::Types::TexCoord1:
semanticIndex = 1;
return "TEXCOORD";
case VertexElement::Types::TexCoord2:
semanticIndex = 2;
return "TEXCOORD";
case VertexElement::Types::TexCoord3:
semanticIndex = 3;
return "TEXCOORD";
case VertexElement::Types::TexCoord4:
semanticIndex = 4;
return "TEXCOORD";
case VertexElement::Types::TexCoord5:
semanticIndex = 5;
return "TEXCOORD";
case VertexElement::Types::TexCoord6:
semanticIndex = 6;
return "TEXCOORD";
case VertexElement::Types::TexCoord7:
semanticIndex = 7;
return "TEXCOORD";
case VertexElement::Types::Attribute0:
return "ATTRIBUTE";
case VertexElement::Types::Attribute1:
semanticIndex = 1;
return "ATTRIBUTE";
case VertexElement::Types::Attribute2:
semanticIndex = 2;
return "ATTRIBUTE";
case VertexElement::Types::Attribute3:
semanticIndex = 3;
return "ATTRIBUTE";
default:
LOG(Fatal, "Invalid vertex shader element semantic type");
return "";
}
}
#endif

View File

@@ -7,6 +7,7 @@
#include "Engine/Core/Types/BaseTypes.h"
#include "Engine/Graphics//RenderTools.h"
#include "Engine/Graphics/Enums.h"
#include "Engine/Graphics/Shaders/VertexElement.h"
#include "IncludeDirectXHeaders.h"
#include "Engine/Core/Log.h"
@@ -112,6 +113,8 @@ namespace RenderToolsDX
const String& errorString = GetD3DErrorString(result);
LOG(Error, "DirectX error: {0} at {1}:{2}", errorString, String(file), line);
}
LPCSTR GetVertexInputSemantic(VertexElement::Types type, UINT& semanticIndex);
};
#if GPU_ENABLE_ASSERTION

View File

@@ -11,6 +11,7 @@
#include "GPUTimerQueryNull.h"
#include "GPUBufferNull.h"
#include "GPUSamplerNull.h"
#include "GPUVertexLayoutNull.h"
#include "GPUSwapChainNull.h"
#include "Engine/Core/Log.h"
#include "Engine/Graphics/Async/GPUTasksManager.h"
@@ -172,6 +173,11 @@ GPUSampler* GPUDeviceNull::CreateSampler()
return New<GPUSamplerNull>();
}
GPUVertexLayout* GPUDeviceNull::CreateVertexLayout(const VertexElements& elements)
{
return New<GPUVertexLayoutNull>(elements);
}
GPUSwapChain* GPUDeviceNull::CreateSwapChain(Window* window)
{
return New<GPUSwapChainNull>(window);

View File

@@ -47,6 +47,7 @@ public:
GPUTimerQuery* CreateTimerQuery() override;
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUVertexLayout* CreateVertexLayout(const VertexElements& elements) override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};

View File

@@ -0,0 +1,22 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#if GRAPHICS_API_NULL
#include "Engine/Graphics/Shaders/GPUVertexLayout.h"
/// <summary>
/// Vertex layout for Null backend.
/// </summary>
class GPUVertexLayoutNull : public GPUVertexLayout
{
public:
GPUVertexLayoutNull(const Elements& elements)
: GPUVertexLayout()
{
_elements = elements;
}
};
#endif

View File

@@ -14,6 +14,7 @@
#include "GPUTimerQueryVulkan.h"
#include "GPUBufferVulkan.h"
#include "GPUSamplerVulkan.h"
#include "GPUVertexLayoutVulkan.h"
#include "GPUSwapChainVulkan.h"
#include "RenderToolsVulkan.h"
#include "QueueVulkan.h"
@@ -448,6 +449,50 @@ uint32 GetHash(const FramebufferVulkan::Key& key)
return hash;
}
GPUVertexLayoutVulkan::GPUVertexLayoutVulkan(GPUDeviceVulkan* device, const Elements& elements)
: GPUResourceVulkan<GPUVertexLayout>(device, StringView::Empty)
{
_elements = elements;
uint32 offsets[GPU_MAX_VB_BINDED] = {};
for (int32 i = 0; i < GPU_MAX_VB_BINDED; i++)
{
VkVertexInputBindingDescription& binding = Bindings[i];
binding.binding = i;
binding.stride = 0;
binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
}
uint32 bindingsCount = 0;
for (int32 i = 0; i < _elements.Count(); i++)
{
const VertexElement& src = _elements.Get()[i];
uint32& offset = offsets[src.Slot];
if (src.Offset != 0)
offset = src.Offset;
const int32 size = PixelFormatExtensions::SizeInBytes(src.Format);
ASSERT_LOW_LAYER(src.Slot < GPU_MAX_VB_BINDED);
VkVertexInputBindingDescription& binding = Bindings[src.Slot];
binding.binding = src.Slot;
binding.stride = Math::Max(binding.stride, (uint32_t)(offset + size));
binding.inputRate = src.PerInstance ? VK_VERTEX_INPUT_RATE_INSTANCE : VK_VERTEX_INPUT_RATE_VERTEX;
VkVertexInputAttributeDescription& attribute = Attributes[i];
attribute.location = i;
attribute.binding = src.Slot;
attribute.format = RenderToolsVulkan::ToVulkanFormat(src.Format);
attribute.offset = offset;
bindingsCount = Math::Max(bindingsCount, (uint32)src.Slot + 1);
offset += size;
}
RenderToolsVulkan::ZeroStruct(CreateInfo, VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO);
CreateInfo.vertexBindingDescriptionCount = bindingsCount;
CreateInfo.pVertexBindingDescriptions = Bindings;
CreateInfo.vertexAttributeDescriptionCount = _elements.Count();
CreateInfo.pVertexAttributeDescriptions = Attributes;
}
FramebufferVulkan::FramebufferVulkan(GPUDeviceVulkan* device, const Key& key, const VkExtent2D& extent, uint32 layers)
: Device(device)
, Handle(VK_NULL_HANDLE)
@@ -2072,6 +2117,11 @@ GPUSampler* GPUDeviceVulkan::CreateSampler()
return New<GPUSamplerVulkan>(this);
}
GPUVertexLayout* GPUDeviceVulkan::CreateVertexLayout(const VertexElements& elements)
{
return New<GPUVertexLayoutVulkan>(this, elements);
}
GPUSwapChain* GPUDeviceVulkan::CreateSwapChain(Window* window)
{
return New<GPUSwapChainVulkan>(this, window);

View File

@@ -610,6 +610,7 @@ public:
GPUTimerQuery* CreateTimerQuery() override;
GPUBuffer* CreateBuffer(const StringView& name) override;
GPUSampler* CreateSampler() override;
GPUVertexLayout* CreateVertexLayout(const VertexElements& elements) override;
GPUSwapChain* CreateSwapChain(Window* window) override;
GPUConstantBuffer* CreateConstantBuffer(uint32 size, const StringView& name) override;
};

View File

@@ -0,0 +1,23 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#if GRAPHICS_API_VULKAN
#include "Engine/Graphics/Shaders/GPUVertexLayout.h"
#include "GPUDeviceVulkan.h"
/// <summary>
/// Vertex layout object for Vulkan backend.
/// </summary>
class GPUVertexLayoutVulkan : public GPUResourceVulkan<GPUVertexLayout>
{
public:
GPUVertexLayoutVulkan(GPUDeviceVulkan* device, const Elements& elements);
VkPipelineVertexInputStateCreateInfo CreateInfo;
VkVertexInputBindingDescription Bindings[GPU_MAX_VB_BINDED];
VkVertexInputAttributeDescription Attributes[GPU_MAX_VS_ELEMENTS];
};
#endif