Skip to content

Commit

Permalink
Implemented bindless textures, closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
adepke committed Jan 18, 2021
1 parent c71423f commit cc9dbcf
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 434 deletions.
3 changes: 3 additions & 0 deletions VanguardEngine/Assets/Shaders/Base.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Copyright (c) 2019-2021 Andrew Depke

Texture2D textures[] : register(t0, space0);
11 changes: 10 additions & 1 deletion VanguardEngine/Assets/Shaders/Default_PS.hlsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Copyright (c) 2019-2021 Andrew Depke

#include "Default_RS.hlsli"
#include "Base.hlsli"

SamplerState defaultSampler : register(s0);
Texture2D albedoMap : register(t1);

struct Material
{
uint albedo;
};

ConstantBuffer<Material> material : register(b0);

struct Input
{
Expand All @@ -23,6 +30,8 @@ struct Output
[RootSignature(RS)]
Output main(Input input)
{
Texture2D albedoMap = textures[material.albedo];

Output output;
output.Color = albedoMap.Sample(defaultSampler, input.uv);

Expand Down
5 changes: 3 additions & 2 deletions VanguardEngine/Assets/Shaders/Default_RS.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"CBV(b0, visibility = SHADER_VISIBILITY_VERTEX)," \
"SRV(t0, visibility = SHADER_VISIBILITY_VERTEX)," \
"CBV(b1, visibility = SHADER_VISIBILITY_VERTEX)," \
"CBV(b0, visibility = SHADER_VISIBILITY_PIXEL)," \
"DescriptorTable(" \
"SRV(t1)," \
"SRV(t0, space = 0, numDescriptors = unbounded, flags = DESCRIPTORS_VOLATILE)," \
"visibility = SHADER_VISIBILITY_PIXEL)," \
"StaticSampler(" \
"s0," \
"space = 0," \
"filter = FILTER_MIN_MAG_MIP_LINEAR," \
"addressU = TEXTURE_ADDRESS_WRAP," \
"addressV = TEXTURE_ADDRESS_WRAP," \
Expand All @@ -20,5 +22,4 @@
"borderColor = STATIC_BORDER_COLOR_TRANSPARENT_BLACK," \
"minLOD = 0.f," \
"maxLOD = 0.f," \
"space = 0," \
"visibility = SHADER_VISIBILITY_PIXEL)"
20 changes: 14 additions & 6 deletions VanguardEngine/Assets/Shaders/UserInterface_PS.hlsl
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Copyright (c) 2019-2021 Andrew Depke

#include "UserInterface_RS.hlsli"
#include "Base.hlsli"

struct PS_INPUT
SamplerState defaultSampler : register(s0);

struct BindlessTexture
{
uint index;
};

ConstantBuffer<BindlessTexture> bindlessTexture : register(b0);

struct Input
{
float4 position : SV_POSITION;
float4 color : COLOR0;
float2 uv : TEXCOORD0;
};

SamplerState sampler0 : register(s0);
Texture2D texture0 : register(t1);

[RootSignature(RS)]
float4 main(PS_INPUT input) : SV_Target
float4 main(Input input) : SV_Target
{
float4 output = input.color * texture0.Sample(sampler0, input.uv);
Texture2D textureTarget = textures[bindlessTexture.index];
float4 output = input.color * textureTarget.Sample(defaultSampler, input.uv);
return output;
}
3 changes: 2 additions & 1 deletion VanguardEngine/Assets/Shaders/UserInterface_RS.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"DENY_DOMAIN_SHADER_ROOT_ACCESS |" \
"DENY_GEOMETRY_SHADER_ROOT_ACCESS)," \
"RootConstants(num32BitConstants = 16, b0, visibility = SHADER_VISIBILITY_VERTEX)," \
"RootConstants(num32BitConstants = 1, b0, visibility = SHADER_VISIBILITY_PIXEL)," \
"SRV(t0, visibility = SHADER_VISIBILITY_VERTEX)," \
"DescriptorTable(" \
"SRV(t1, numDescriptors = 1)," \
"SRV(t0, numDescriptors = unbounded, flags = DESCRIPTORS_VOLATILE)," \
"visibility = SHADER_VISIBILITY_PIXEL)," \
"StaticSampler(" \
"s0," \
Expand Down
13 changes: 9 additions & 4 deletions VanguardEngine/Assets/Shaders/UserInterface_VS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ struct Vertex

StructuredBuffer<Vertex> vertexBuffer : register(t0);

struct PS_INPUT
struct Input
{
uint vertexID : SV_VertexID;
};

struct Output
{
float4 position : SV_POSITION;
float4 color : COLOR0;
float2 uv : TEXCOORD0;
};

[RootSignature(RS)]
PS_INPUT main(uint vertexID : SV_VertexID)
Output main(Input input)
{
Vertex vertex = vertexBuffer[vertexID];
Vertex vertex = vertexBuffer[input.vertexID];

PS_INPUT output;
Output output;
output.position = mul(projections.projectionMatrix, float4(vertex.position.xy, 0.f, 1.f));
output.color.r = float((vertex.color >> 0) & 0xFF) / 255.f;
output.color.g = float((vertex.color >> 8) & 0xFF) / 255.f;
Expand Down
32 changes: 29 additions & 3 deletions VanguardEngine/Source/Asset/AssetLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,24 @@ namespace AssetLoader
auto trimmedPath = path;
trimmedPath.remove_filename();

std::vector<std::shared_ptr<Material>> materials{};
std::vector<Material> materials{};
materials.reserve(scene->mNumMaterials);

for (uint32_t i = 0; i < scene->mNumMaterials; ++i)
{
materials.emplace_back();
const auto& mat = *scene->mMaterials[i];

materials.emplace_back(std::make_shared<Material>());
// Create the material table.
BufferDescription matTableDesc{
.updateRate = ResourceFrequency::Static,
.bindFlags = BindFlag::ConstantBuffer,
.accessFlags = AccessFlag::CPUWrite,
.size = 1,
.stride = sizeof(uint32_t)
};

materials[i].materialBuffer = device.GetResourceManager().Create(matTableDesc, VGText("Material table"));

aiString texturePath;

Expand All @@ -86,11 +96,26 @@ namespace AssetLoader

// Prefer PBR texture types, but fall back to legacy types (which may still be PBR, just incorrectly set in the asset).

// Only use the albedo while working on bindless.
if (SearchTextureType(std::array{ aiTextureType_BASE_COLOR, aiTextureType_DIFFUSE }))
{
materials[i]->albedo = LoadTexture(device, trimmedPath / texturePath.C_Str());
const auto textureHandle = LoadTexture(device, trimmedPath / texturePath.C_Str());
const TextureComponent& textureComponent = device.GetResourceManager().Get(textureHandle);
const auto bindlessIndex = textureComponent.SRV->bindlessIndex;

std::vector<uint32_t> materialTable{};
materialTable.emplace_back(bindlessIndex);

std::vector<uint8_t> materialTableBytes{};
materialTableBytes.resize(materialTable.size() * sizeof(uint32_t));
std::memcpy(materialTableBytes.data(), materialTable.data(), materialTableBytes.size());

device.GetResourceManager().Write(materials[i].materialBuffer, materialTableBytes);

device.GetDirectList().TransitionBarrier(materials[i].materialBuffer, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
}

/*
if (SearchTextureType(std::array{ aiTextureType_NORMAL_CAMERA, aiTextureType_NORMALS, aiTextureType_HEIGHT }))
{
materials[i]->normal = LoadTexture(device, trimmedPath / texturePath.C_Str());
Expand All @@ -115,6 +140,7 @@ namespace AssetLoader
{
//Materials[Iter]->ambientOcclusion = LoadTexture(Device, TrimmedPath / TexturePath.C_Str());
}
*/
}

// Sum vertices/indices from all the submeshes.
Expand Down
8 changes: 3 additions & 5 deletions VanguardEngine/Source/Editor/EditorRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

namespace EditorRenderer
{
void Render(entt::registry& registry)
void Render(RenderDevice* device, entt::registry& registry, TextureHandle sceneTexture)
{
// #TODO: Docking layout is disabled until bindless is implemented, which will allow
// render graph textures to be used with ImGui.
//EditorUI::Get().DrawLayout();
//EditorUI::Get().DrawScene();
EditorUI::Get().DrawLayout();
EditorUI::Get().DrawScene(device, sceneTexture);
EditorUI::Get().DrawEntityHierarchy(registry);
EditorUI::Get().DrawEntityPropertyViewer(registry);
}
Expand Down
5 changes: 4 additions & 1 deletion VanguardEngine/Source/Editor/EditorRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

#pragma once

#include <Rendering/Resource.h>
#include <Rendering/Viewport.h>

#include <entt/entt.hpp>

class RenderDevice;

// Rendering interface for the entire editor, this is the single point of rendering access outside of the Editor module.

namespace EditorRenderer
{
void Render(entt::registry& registry);
void Render(RenderDevice* device, entt::registry& registry, TextureHandle sceneTexture);
};
8 changes: 6 additions & 2 deletions VanguardEngine/Source/Editor/EditorUI.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2019-2021 Andrew Depke

#include <Editor/EditorUI.h>
#include <Rendering/Device.h>
#include <Core/CoreComponents.h>
#include <Rendering/RenderComponents.h>
#include <Editor/EntityReflection.h>
Expand Down Expand Up @@ -33,10 +34,12 @@ void EditorUI::DrawLayout()
const auto dockSpaceId = ImGui::GetID("DockSpace");
ImGui::DockSpace(dockSpaceId, { 0.f, 0.f }, ImGuiDockNodeFlags_None);

ImGui::End();

ImGui::PopStyleVar(3);
}

void EditorUI::DrawScene()
void EditorUI::DrawScene(RenderDevice* device, TextureHandle sceneTexture)
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 0.f, 0.f }); // Remove window padding.

Expand All @@ -45,7 +48,8 @@ void EditorUI::DrawScene()

ImGui::Begin("Scene", nullptr, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoCollapse);

// #TODO: Use ImGui::Image() to draw the back buffer.
const auto& sceneTextureComponent = device->GetResourceManager().Get(sceneTexture);
ImGui::Image((ImTextureID)sceneTextureComponent.SRV->bindlessIndex, { (float)sceneTextureComponent.description.width, (float)sceneTextureComponent.description.height });

ImGui::End();

Expand Down
3 changes: 2 additions & 1 deletion VanguardEngine/Source/Editor/EditorUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <Rendering/Resource.h>
#include <Rendering/Viewport.h>
#include <Utility/Singleton.h>

Expand All @@ -14,7 +15,7 @@ class EditorUI : public Singleton<EditorUI>

public:
void DrawLayout();
void DrawScene();
void DrawScene(RenderDevice* device, TextureHandle sceneTexture);
void DrawEntityHierarchy(entt::registry& registry);
void DrawEntityPropertyViewer(entt::registry& registry);
};
11 changes: 2 additions & 9 deletions VanguardEngine/Source/Rendering/CommandList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,8 @@ void CommandList::BindDescriptorAllocator(DescriptorAllocator& allocator)
{
VGScopedCPUStat("Bind Descriptor Allocator");

std::vector<ID3D12DescriptorHeap*> heaps;
heaps.reserve(allocator.onlineHeaps.size());

for (auto& heap : allocator.onlineHeaps)
{
heaps.push_back(heap[device->GetFrameIndex()].Native());
}

list->SetDescriptorHeaps(static_cast<UINT>(heaps.size()), heaps.data());
auto* descriptorHeap = allocator.defaultHeap.Native();
list->SetDescriptorHeaps(1, &descriptorHeap);
}

HRESULT CommandList::Close()
Expand Down
66 changes: 5 additions & 61 deletions VanguardEngine/Source/Rendering/DescriptorAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,13 @@
#include <Rendering/DescriptorAllocator.h>
#include <Rendering/Device.h>

// #TEMP
#include <Rendering/Renderer.h>

void DescriptorAllocator::Initialize(RenderDevice* inDevice, size_t offlineDescriptors, size_t onlineDescriptors)
void DescriptorAllocator::Initialize(RenderDevice* inDevice, size_t shaderDescriptors, size_t renderTargetDescriptors, size_t depthStencilDescriptors)
{
VGScopedCPUStat("Descriptor Allocator Initialize");

offlineHeaps[0].Create(inDevice, DescriptorType::Default, offlineDescriptors, false);
offlineHeaps[1].Create(inDevice, DescriptorType::Sampler, offlineDescriptors, false);

onlineHeaps[0].resize(inDevice->frameCount);
onlineHeaps[1].resize(inDevice->frameCount);

for (auto& heap : onlineHeaps[0])
{
heap.Create(inDevice, DescriptorType::Default, onlineDescriptors, true);
}

for (auto& heap : onlineHeaps[1])
{
heap.Create(inDevice, DescriptorType::Sampler, onlineDescriptors, true);
}

renderTargetHeap.Create(inDevice, DescriptorType::RenderTarget, onlineDescriptors, false);
depthStencilHeap.Create(inDevice, DescriptorType::DepthStencil, onlineDescriptors, false);
defaultHeap.Create(inDevice, DescriptorType::Default, shaderDescriptors, true);
renderTargetHeap.Create(inDevice, DescriptorType::RenderTarget, renderTargetDescriptors, false);
depthStencilHeap.Create(inDevice, DescriptorType::DepthStencil, depthStencilDescriptors, false);
}

DescriptorHandle DescriptorAllocator::Allocate(DescriptorType type)
Expand All @@ -36,7 +18,7 @@ DescriptorHandle DescriptorAllocator::Allocate(DescriptorType type)
{
case DescriptorType::Default:
case DescriptorType::Sampler:
return offlineHeaps[static_cast<size_t>(type)].Allocate();
return defaultHeap.Allocate();
case DescriptorType::RenderTarget:
return renderTargetHeap.Allocate();
case DescriptorType::DepthStencil:
Expand All @@ -46,45 +28,7 @@ DescriptorHandle DescriptorAllocator::Allocate(DescriptorType type)
return {};
}

void DescriptorAllocator::AddTableEntry(DescriptorHandle& handle, DescriptorTableEntryType type)
{
pendingTableEntries.push_back(PendingDescriptorTableEntry{ handle, type });
}

void DescriptorAllocator::BuildTable(RenderDevice& device, CommandList& list, uint32_t rootParameter)
{
VGScopedCPUStat("Build Descriptor Table");

D3D12_GPU_DESCRIPTOR_HANDLE tableStart{};

bool first = true;
for (auto& entry : pendingTableEntries)
{
const auto targetHeapIndex = entry.type == DescriptorTableEntryType::Sampler ? 1 : 0;
auto& targetHeap = onlineHeaps[targetHeapIndex][device.GetFrameIndex()];

auto destination = targetHeap.Allocate();

if (first)
{
tableStart = destination;
first = false;
}

device.Native()->CopyDescriptorsSimple(1, destination, entry.handle, targetHeapIndex == 0 ? D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV : D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
}

pendingTableEntries.clear();

list.Native()->SetGraphicsRootDescriptorTable(rootParameter, tableStart);
}

void DescriptorAllocator::FrameStep(size_t frameIndex)
{
VGScopedCPUStat("Descriptor Allocator Frame Step");

for (auto& onlineHeap : onlineHeaps)
{
onlineHeap[frameIndex].Reset();
}
}

0 comments on commit cc9dbcf

Please sign in to comment.