Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Editor/Src/EditorWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ namespace Editor {
imguiPassParamsBufferView = imguiPassParamsBuffer->CreateBufferView(
RHI::BufferViewCreateInfo()
.SetType(RHI::BufferViewType::uniformBinding)
.SetSize(sizeof(ImGuiPassParams))
.SetOffset(0));
.SetSizeInBytes(sizeof(ImGuiPassParams))
.SetOffsetInBytes(0));

imguiBindGroupLayout = device.CreateBindGroupLayout(
RHI::BindGroupLayoutCreateInfo(0, "imguiBindGroupLayout")
Expand Down Expand Up @@ -404,7 +404,7 @@ namespace Editor {

imguiFontTexture = device.CreateTexture(
RHI::TextureCreateInfo()
.SetDimension(RHI::TextureDimension::t2D)
.SetType(RHI::TextureType::t2D)
.SetWidth(static_cast<uint32_t>(width))
.SetHeight(static_cast<uint32_t>(height))
.SetDepthOrArraySize(1)
Expand Down Expand Up @@ -487,8 +487,8 @@ namespace Editor {
imguiVertexBufferView = imguiVertexBuffer->CreateBufferView(
RHI::BufferViewCreateInfo()
.SetType(RHI::BufferViewType::vertex)
.SetSize(vertexBufferSize)
.SetOffset(0)
.SetSizeInBytes(vertexBufferSize)
.SetOffsetInBytes(0)
.SetExtendVertex(sizeof(ImDrawVert)));

const uint32_t indexBufferSize = static_cast<uint32_t>(imguiIndexBufferCapacity * sizeof(ImDrawIdx));
Expand All @@ -501,8 +501,8 @@ namespace Editor {
imguiIndexBufferView = imguiIndexBuffer->CreateBufferView(
RHI::BufferViewCreateInfo()
.SetType(RHI::BufferViewType::index)
.SetSize(indexBufferSize)
.SetOffset(0)
.SetSizeInBytes(indexBufferSize)
.SetOffsetInBytes(0)
.SetExtendIndex(Internal::imguiIndexFormat));
}

Expand Down
11 changes: 11 additions & 0 deletions Engine/Source/Common/Include/Common/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ namespace Common {
template <uint32_t A, CppIntegral T>
T AlignUp(T value);

template <CppIntegral T>
T AlignUp(T value, T alignment);

template <typename LHS, typename... RHS>
struct IsAllSame {};

Expand Down Expand Up @@ -116,6 +119,14 @@ namespace Common {
return (value + (A - 1)) & ~(A - 1);
}

template <CppIntegral T>
T AlignUp(T value, T alignment)
{
Assert(alignment > 0);
const T remainder = value % alignment;
return remainder == 0 ? value : value + alignment - remainder;
}

template <typename LHS, typename RHS0, typename... RHS>
struct IsAllSame<LHS, RHS0, RHS...> {
static constexpr bool value = std::is_same_v<std::remove_cvref_t<LHS>, std::remove_cvref_t<RHS0>> && IsAllSame<LHS, RHS...>::value;
Expand Down
2 changes: 2 additions & 0 deletions Engine/Source/Common/Test/UtilityTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ TEST(UtilityTest, AlignUpTest)
ASSERT_EQ(AlignUp<4>(3), 4);
ASSERT_EQ(AlignUp<4>(7), 8);
ASSERT_EQ(AlignUp<256>(258), 512);
ASSERT_EQ(AlignUp(768, 512), 1024);
ASSERT_EQ(AlignUp(1024, 512), 1024);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ namespace RHI::DirectX12 {
explicit DX12BindGroupLayout(const BindGroupLayoutCreateInfo& inCreateInfo);
~DX12BindGroupLayout() override;

uint8_t GetLayoutIndex() const;
[[nodiscard]] const std::vector<RootParameterKeyInfo>& GetRootParameterKeyInfos() const;
[[nodiscard]] const std::vector<CD3DX12_ROOT_PARAMETER1>& GetNativeRootParameters() const;

private:
void CreateNativeRootParameters(const BindGroupLayoutCreateInfo& inCreateInfo);

uint8_t layoutIndex;
std::vector<RootParameterKeyInfo> rootParameterKeyInfos;
std::vector<CD3DX12_ROOT_PARAMETER1> nativeRootParameters;
std::vector<CD3DX12_DESCRIPTOR_RANGE1> nativeDescriptorRanges;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/RHI-DirectX12/Include/RHI/DirectX12/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ namespace RHI::DirectX12 {

void* Map(MapMode inMapMode, size_t inOffset, size_t inLength) override;
void Unmap() override;
Common::UniquePtr<BufferView> CreateBufferView(const BufferViewCreateInfo& inCreateInfo) override;

ID3D12Resource* GetNative() const;
DX12Device& GetDevice() const;
BufferUsageFlags GetUsages() const;

private:
Common::UniquePtr<BufferView> CreateBufferViewInternal(const BufferViewCreateInfo& inCreateInfo) override;
void CreateNativeBuffer(DX12Device& inDevice, const BufferCreateInfo& inCreateInfo);

DX12Device& device;
Expand Down
13 changes: 9 additions & 4 deletions Engine/Source/RHI-DirectX12/Include/RHI/DirectX12/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,13 @@ namespace RHI::DirectX12 {
ECIMPL_ITEM(BufferState::indirect, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT)
ECIMPL_END(D3D12_RESOURCE_STATES)

ECIMPL_BEGIN(TextureDimension, D3D12_RESOURCE_DIMENSION)
ECIMPL_ITEM(TextureDimension::t1D, D3D12_RESOURCE_DIMENSION_TEXTURE1D)
ECIMPL_ITEM(TextureDimension::t2D, D3D12_RESOURCE_DIMENSION_TEXTURE2D)
ECIMPL_ITEM(TextureDimension::t3D, D3D12_RESOURCE_DIMENSION_TEXTURE3D)
ECIMPL_BEGIN(TextureType, D3D12_RESOURCE_DIMENSION)
ECIMPL_ITEM(TextureType::t1D, D3D12_RESOURCE_DIMENSION_TEXTURE1D)
ECIMPL_ITEM(TextureType::t2D, D3D12_RESOURCE_DIMENSION_TEXTURE2D)
ECIMPL_ITEM(TextureType::t2DArray, D3D12_RESOURCE_DIMENSION_TEXTURE2D)
ECIMPL_ITEM(TextureType::tCube, D3D12_RESOURCE_DIMENSION_TEXTURE2D)
ECIMPL_ITEM(TextureType::tCubeArray, D3D12_RESOURCE_DIMENSION_TEXTURE2D)
ECIMPL_ITEM(TextureType::t3D, D3D12_RESOURCE_DIMENSION_TEXTURE3D)
ECIMPL_END(D3D12_RESOURCE_DIMENSION)

ECIMPL_BEGIN(TextureState, D3D12_RESOURCE_STATES)
Expand All @@ -270,6 +273,8 @@ namespace RHI::DirectX12 {
ECIMPL_ITEM(TextureState::storage, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE)
ECIMPL_ITEM(TextureState::rwStorage, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)
ECIMPL_ITEM(TextureState::depthStencilReadonly, D3D12_RESOURCE_STATE_DEPTH_READ)
ECIMPL_ITEM(TextureState::depthReadStencilWrite, D3D12_RESOURCE_STATE_DEPTH_WRITE)
ECIMPL_ITEM(TextureState::depthWriteStencilRead, D3D12_RESOURCE_STATE_DEPTH_WRITE)
ECIMPL_ITEM(TextureState::depthStencilWrite, D3D12_RESOURCE_STATE_DEPTH_WRITE)
ECIMPL_ITEM(TextureState::present, D3D12_RESOURCE_STATE_PRESENT)
ECIMPL_END(D3D12_RESOURCE_STATES)
Expand Down
3 changes: 1 addition & 2 deletions Engine/Source/RHI-DirectX12/Include/RHI/DirectX12/Texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ namespace RHI::DirectX12 {
DX12Texture(DX12Device& inDevice, const TextureCreateInfo& inCreateInfo, ComPtr<ID3D12Resource>&& nativeResource);
~DX12Texture() override;

Common::UniquePtr<TextureView> CreateTextureView(const TextureViewCreateInfo& inCreateInfo) override;

ID3D12Resource* GetNative() const;

private:
Common::UniquePtr<TextureView> CreateTextureViewInternal(const TextureViewCreateInfo& inCreateInfo) override;
void CreateNativeTexture(const TextureCreateInfo& inCreateInfo);

DX12Device& device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#pragma once

#include <array>
#include <wrl/client.h>
#include <directx/d3dx12.h>
using Microsoft::WRL::ComPtr;
Expand All @@ -13,6 +14,7 @@ using Microsoft::WRL::ComPtr;
namespace RHI::DirectX12 {
class DX12Device;
class DX12Texture;
class DescriptorAllocation;

class DX12TextureView final : public TextureView {
public:
Expand All @@ -21,11 +23,12 @@ namespace RHI::DirectX12 {
~DX12TextureView() override;

CD3DX12_CPU_DESCRIPTOR_HANDLE GetNativeCpuDescriptorHandle() const;
CD3DX12_CPU_DESCRIPTOR_HANDLE GetNativeDepthStencilCpuDescriptorHandle(bool depthReadOnly, bool stencilReadOnly) const;

private:
void CreateNativeDescriptor(DX12Device& inDevice, const TextureViewCreateInfo& inCreateInfo);

DX12Texture& texture;
Common::UniquePtr<DescriptorAllocation> descriptorAllocation;
std::array<Common::UniquePtr<DescriptorAllocation>, 4> descriptorAllocations;
};
}
10 changes: 2 additions & 8 deletions Engine/Source/RHI-DirectX12/Src/BindGroupLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ namespace RHI::DirectX12 {

DX12BindGroupLayout::DX12BindGroupLayout(const BindGroupLayoutCreateInfo& inCreateInfo)
: BindGroupLayout(inCreateInfo)
, layoutIndex(inCreateInfo.layoutIndex)
{
CreateNativeRootParameters(inCreateInfo);
}

DX12BindGroupLayout::~DX12BindGroupLayout() = default;

uint8_t DX12BindGroupLayout::GetLayoutIndex() const
{
return layoutIndex;
}

const std::vector<RootParameterKeyInfo>& DX12BindGroupLayout::GetRootParameterKeyInfos() const
{
return rootParameterKeyInfos;
Expand All @@ -66,10 +60,10 @@ namespace RHI::DirectX12 {
nativeDescriptorRanges.emplace_back();

const auto& hlslBinding = std::get<HlslBinding>(entry.binding.platformBinding);
nativeDescriptorRanges.back().Init(EnumCast<HlslBindingRangeType, D3D12_DESCRIPTOR_RANGE_TYPE>(hlslBinding.rangeType), 1, hlslBinding.index, inCreateInfo.layoutIndex);
nativeDescriptorRanges.back().Init(EnumCast<HlslBindingRangeType, D3D12_DESCRIPTOR_RANGE_TYPE>(hlslBinding.rangeType), 1, hlslBinding.index, GetLayoutIndex());
nativeRootParameters.back().InitAsDescriptorTable(1, &nativeDescriptorRanges.back(), GetShaderVisibility(entry.shaderVisibility));

rootParameterKeyInfos.emplace_back(entry.binding.type, inCreateInfo.layoutIndex, hlslBinding, entry.shaderVisibility);
rootParameterKeyInfos.emplace_back(entry.binding.type, GetLayoutIndex(), hlslBinding, entry.shaderVisibility);
}
}
}
2 changes: 1 addition & 1 deletion Engine/Source/RHI-DirectX12/Src/Buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace RHI::DirectX12 {
nativeResource->Unmap(0, nullptr);
}

Common::UniquePtr<BufferView> DX12Buffer::CreateBufferView(const BufferViewCreateInfo& inCreateInfo)
Common::UniquePtr<BufferView> DX12Buffer::CreateBufferViewInternal(const BufferViewCreateInfo& inCreateInfo)
{
return Common::UniquePtr<BufferView>(new DX12BufferView(*this, inCreateInfo));
}
Expand Down
46 changes: 32 additions & 14 deletions Engine/Source/RHI-DirectX12/Src/BufferView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
#include <RHI/DirectX12/BufferView.h>
#include <RHI/DirectX12/Common.h>

namespace RHI::DirectX12::Internal {
struct StructuredBufferRange {
UINT64 firstElement;
UINT elementCount;
UINT stride;
};

static StructuredBufferRange GetStructuredBufferRange(const BufferViewCreateInfo& inCreateInfo)
{
const auto& storageViewInfo = std::get<StorageBufferViewInfo>(inCreateInfo.extend);
return {
inCreateInfo.offsetInBytes / storageViewInfo.stride,
inCreateInfo.sizeInBytes / storageViewInfo.stride,
storageViewInfo.stride
};
}
}

namespace RHI::DirectX12 {
DX12BufferView::DX12BufferView(DX12Buffer& inBuffer, const BufferViewCreateInfo& inCreateInfo)
: BufferView(inCreateInfo), buffer(inBuffer)
Expand Down Expand Up @@ -41,36 +59,36 @@ namespace RHI::DirectX12 {
Assert((bufferUsages & BufferUsageBits::uniform) != 0);

D3D12_CONSTANT_BUFFER_VIEW_DESC desc {};
desc.BufferLocation = buffer.GetNative()->GetGPUVirtualAddress() + inCreateInfo.offset;
desc.SizeInBytes = Common::AlignUp<D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT>(inCreateInfo.size);
desc.BufferLocation = buffer.GetNative()->GetGPUVirtualAddress() + inCreateInfo.offsetInBytes;
desc.SizeInBytes = Common::AlignUp<D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT>(inCreateInfo.sizeInBytes);

nativeView = buffer.GetDevice().AllocateCbvSrvUavDescriptor();
buffer.GetDevice().GetNative()->CreateConstantBufferView(&desc, std::get<Common::UniquePtr<DescriptorAllocation>>(nativeView)->GetCpuHandle());
} else if (inCreateInfo.type == BufferViewType::storageBinding) {
Assert((bufferUsages & BufferUsageBits::storage) != 0);
auto storageViewInfo = std::get<StorageBufferViewInfo>(inCreateInfo.extend);
const auto range = Internal::GetStructuredBufferRange(inCreateInfo);

D3D12_SHADER_RESOURCE_VIEW_DESC desc {};
desc.Format = DXGI_FORMAT_UNKNOWN;
desc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
desc.Buffer.FirstElement = inCreateInfo.offset;
desc.Buffer.NumElements = inCreateInfo.size / storageViewInfo.stride;
desc.Buffer.StructureByteStride = storageViewInfo.stride;
desc.Buffer.FirstElement = range.firstElement;
desc.Buffer.NumElements = range.elementCount;
desc.Buffer.StructureByteStride = range.stride;

nativeView = buffer.GetDevice().AllocateCbvSrvUavDescriptor();
buffer.GetDevice().GetNative()->CreateShaderResourceView(buffer.GetNative(), &desc, std::get<Common::UniquePtr<DescriptorAllocation>>(nativeView)->GetCpuHandle());
} else if (inCreateInfo.type == BufferViewType::rwStorageBinding) {
Assert((bufferUsages & BufferUsageBits::rwStorage) != 0);
auto storageViewInfo = std::get<StorageBufferViewInfo>(inCreateInfo.extend);
const auto range = Internal::GetStructuredBufferRange(inCreateInfo);

// TODO: check the uav typed load when it is necessary
D3D12_UNORDERED_ACCESS_VIEW_DESC desc {};
desc.Format = DXGI_FORMAT_UNKNOWN;
desc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
desc.Buffer.FirstElement = inCreateInfo.offset;
desc.Buffer.NumElements = inCreateInfo.size / storageViewInfo.stride;
desc.Buffer.StructureByteStride = storageViewInfo.stride;
desc.Buffer.FirstElement = range.firstElement;
desc.Buffer.NumElements = range.elementCount;
desc.Buffer.StructureByteStride = range.stride;

nativeView = buffer.GetDevice().AllocateCbvSrvUavDescriptor();
buffer.GetDevice().GetNative()->CreateUnorderedAccessView(buffer.GetNative(), nullptr, &desc, std::get<Common::UniquePtr<DescriptorAllocation>>(nativeView)->GetCpuHandle());
Expand All @@ -79,16 +97,16 @@ namespace RHI::DirectX12 {

nativeView = D3D12_VERTEX_BUFFER_VIEW();
D3D12_VERTEX_BUFFER_VIEW& vertexBufferView = std::get<D3D12_VERTEX_BUFFER_VIEW>(nativeView);
vertexBufferView.BufferLocation = buffer.GetNative()->GetGPUVirtualAddress() + inCreateInfo.offset;
vertexBufferView.SizeInBytes = inCreateInfo.size;
vertexBufferView.BufferLocation = buffer.GetNative()->GetGPUVirtualAddress() + inCreateInfo.offsetInBytes;
vertexBufferView.SizeInBytes = inCreateInfo.sizeInBytes;
vertexBufferView.StrideInBytes = std::get<VertexBufferViewInfo>(inCreateInfo.extend).stride;
} else if (inCreateInfo.type == BufferViewType::index) {
Assert((bufferUsages & BufferUsageBits::index) != 0);

nativeView = D3D12_INDEX_BUFFER_VIEW();
D3D12_INDEX_BUFFER_VIEW& indexBufferView = std::get<D3D12_INDEX_BUFFER_VIEW>(nativeView);
indexBufferView.BufferLocation = buffer.GetNative()->GetGPUVirtualAddress() + inCreateInfo.offset;
indexBufferView.SizeInBytes = inCreateInfo.size;
indexBufferView.BufferLocation = buffer.GetNative()->GetGPUVirtualAddress() + inCreateInfo.offsetInBytes;
indexBufferView.SizeInBytes = inCreateInfo.sizeInBytes;
indexBufferView.Format = EnumCast<IndexFormat, DXGI_FORMAT>(std::get<IndexBufferViewInfo>(inCreateInfo.extend).format);
} else {
Unimplement();
Expand Down
Loading
Loading