Skip to content

Commit

Permalink
Begin work on bindless texturing
Browse files Browse the repository at this point in the history
DX12: add ability to bind the entire CBV_SRV_UAV heap at once
  • Loading branch information
Ravbug committed Jul 4, 2024
1 parent 2df1b38 commit a83433f
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 6 deletions.
6 changes: 6 additions & 0 deletions include/RGL/CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ struct TextureView;
virtual void SetVertexTexture(const TextureView& texture, uint32_t index) = 0;
virtual void SetFragmentTexture(const TextureView& texture, uint32_t index) = 0;
virtual void SetComputeTexture(const TextureView& texture, uint32_t index) = 0;

struct MakeResidentConfig {
bool written = false;
};
virtual void MakeResident(const TextureView& texture, const MakeResidentConfig& config) = 0;


virtual void Draw(uint32_t nVertices, const DrawInstancedConfig& = {}) = 0;
virtual void DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig& = {}) = 0;
Expand Down
4 changes: 4 additions & 0 deletions include/RGL/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace RGL {
} mtlData;
};

struct TextureView;

struct IDevice {
virtual ~IDevice() {}
static RGLDevicePtr CreateSystemDefaultDevice();
Expand All @@ -53,6 +55,8 @@ namespace RGL {

virtual RGLCommandQueuePtr CreateCommandQueue(QueueType type) = 0;

virtual TextureView GetGlobalBindlessTextureHeap() const = 0;

virtual RGLComputePipelinePtr CreateComputePipeline(const struct ComputePipelineDescriptor&) = 0;

virtual size_t GetTotalVRAM() const = 0;
Expand Down
1 change: 1 addition & 0 deletions include/RGL/Pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace RGL {
struct PipelineLayoutDescriptor {
struct LayoutBindingDesc {
uint32_t binding = 0;
uint32_t count = 1;
BindingType type;
BindingVisibility stageFlags;
bool writable = false;
Expand Down
2 changes: 2 additions & 0 deletions include/RGL/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <limits>
#include <string_view>
#include <array>
#include "TextureFormat.hpp"
#include "SubresourceRange.hpp"

Expand Down Expand Up @@ -125,6 +126,7 @@ namespace RGL {
TextureUsage usage;
TextureAspect aspect;
uint32_t width = 0, height = 0, depth = 1, mipLevels = 1, arrayLayers = 1;
std::array<float, 4> optimizedClearValue{ 0,0,0,0 };
TextureType imageType = decltype(imageType)::T2D;
TextureFormat format;
TilingMode mode = decltype(mode)::Optimal;
Expand Down
12 changes: 11 additions & 1 deletion src/D3D12CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ namespace RGL {
else {
assert(thisTexture.srvAllocated(), "Cannot bind this texture because it is not in a SRV heap!");
}
auto& heap = thisTexture.parentResource->owningDevice->CBV_SRV_UAVHeap;
auto& heap = owningQueue->owningDevice->CBV_SRV_UAVHeap;

if (isGraphics) {
commandList->SetGraphicsRootDescriptorTable(textureSlot.slot, heap->GetGpuHandle(textureSlot.isUAV ? thisTexture.uavIDX : thisTexture.srvIDX));
Expand All @@ -297,6 +297,12 @@ namespace RGL {
{
SetFragmentTexture(texture, index);
}

void CommandBufferD3D12::MakeResident(const TextureView& texture, const MakeResidentConfig& config)
{

}

void CommandBufferD3D12::Draw(uint32_t nVertices, const DrawInstancedConfig& config)
{
commandList->DrawInstanced(nVertices, config.nInstances, config.startVertex, config.firstInstance);
Expand Down Expand Up @@ -625,6 +631,10 @@ namespace RGL {
}
void CommandBufferD3D12::SyncIfNeeded(TextureView texture, D3D12_RESOURCE_STATES needed, bool written)
{
if (texture.texture.dx.parentResource == nullptr) {
return;
}

D3D12TextureLastUseKey key{texture.texture.dx.parentResource, texture.texture.dx.coveredMips, texture.texture.dx.coveredLayers};
auto parent = texture.texture.dx.parentResource;

Expand Down
2 changes: 2 additions & 0 deletions src/D3D12CommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ namespace RGL {
void SetFragmentTexture(const TextureView& texture, uint32_t index) final;
void SetComputeTexture(const TextureView& texture, uint32_t index) final;

void MakeResident(const TextureView& texture, const MakeResidentConfig& config) final;

void Draw(uint32_t nVertices, const DrawInstancedConfig & = {}) final;
void DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig & = {}) final;

Expand Down
14 changes: 14 additions & 0 deletions src/D3D12Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,20 @@ namespace RGL {
};
}

TextureView DeviceD3D12::GetGlobalBindlessTextureHeap() const
{

return TextureView{ {
.dsvIDX = 0,
.rtvIDX = 0,
.srvIDX = 0,
.uavIDX = 0,
.parentResource = nullptr, // bindless must set barriers elsewhere
.coveredMips = ALL_MIPS,
.coveredLayers = ALL_LAYERS
} };
}

RGLCommandQueuePtr RGL::DeviceD3D12::CreateCommandQueue(QueueType type)
{
return std::make_shared<CommandQueueD3D12>(this, type);
Expand Down
2 changes: 2 additions & 0 deletions src/D3D12Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace RGL {

DeviceData GetDeviceData() final;

TextureView GetGlobalBindlessTextureHeap() const final;

RGLCommandQueuePtr CreateCommandQueue(QueueType type) final;
RGLFencePtr CreateFence(bool preSignaled) final;
void BlockUntilIdle() final;
Expand Down
8 changes: 5 additions & 3 deletions src/D3D12RenderPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ namespace RGL {
{
auto& range = ranges.emplace_back(D3D12_DESCRIPTOR_RANGE1{
.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER,
.NumDescriptors = 1,
.NumDescriptors = item.count,
.BaseShaderRegister = item.binding,
.RegisterSpace = 0,
.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
Expand All @@ -202,9 +202,10 @@ namespace RGL {
{
auto& range = ranges.emplace_back(D3D12_DESCRIPTOR_RANGE1{
.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
.NumDescriptors = 1,
.NumDescriptors = item.count,
.BaseShaderRegister = item.binding,
.RegisterSpace = 0,
.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE,
.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
});
textureBindingToRootSlot[item.binding] = { uint32_t(rootParameters.size()), false };
Expand All @@ -216,9 +217,10 @@ namespace RGL {
// UAV
auto& range = ranges.emplace_back(D3D12_DESCRIPTOR_RANGE1{
.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV,
.NumDescriptors = 1,
.NumDescriptors = item.count,
.BaseShaderRegister = item.binding,
.RegisterSpace = 0,
.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE,
.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
});
textureBindingToRootSlot[item.binding] = { uint32_t(rootParameters.size()), true };
Expand Down
2 changes: 1 addition & 1 deletion src/D3D12Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ namespace RGL {
nativeState = D3D12_RESOURCE_STATE_COMMON;
if (isDS) {
resourceDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
optimizedClearValue.DepthStencil = { 0,0 };
optimizedClearValue.DepthStencil = { config.optimizedClearValue[0], 0};
if (!config.usage.Sampled) {
nativeState |= D3D12_RESOURCE_STATE_DEPTH_WRITE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/RGLD3D12Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <atlbase.h>
#include <dxcapi.h>

#define DX12_USE_AGILITY 0
#define DX12_USE_AGILITY 1

// Exports for the Agility SDK. For Windows 10 users, Go here: https://www.nuget.org/packages/Microsoft.Direct3D.D3D12/1.614.0 then unzip it, and place
// D3D12Core.dll and d3d12SDKLayers.dll in a folder named D3D12 next to the executable.
Expand Down
6 changes: 6 additions & 0 deletions src/VkCommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ namespace RGL {
{
SetFragmentTexture(texture, index);
}

void CommandBufferVk::MakeResident(const TextureView& texture, const MakeResidentConfig& config)
{

}

void CommandBufferVk::Draw(uint32_t nVertices, const DrawInstancedConfig& config)
{
EncodeCommand(CmdDraw{ nVertices, config });
Expand Down
2 changes: 2 additions & 0 deletions src/VkCommandBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ namespace RGL {
void SetFragmentTexture(const TextureView& texture, uint32_t index) final;
void SetComputeTexture(const TextureView& texture, uint32_t index) final;

void MakeResident(const TextureView& texture, const MakeResidentConfig& config) final;

void Draw(uint32_t nVertices, const DrawInstancedConfig & = {}) final;
void DrawIndexed(uint32_t nIndices, const DrawIndexedInstancedConfig & = {}) final;

Expand Down
5 changes: 5 additions & 0 deletions src/VkDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ namespace RGL {
};
}

TextureView DeviceVk::GetGlobalBindlessTextureHeap() const
{
return TextureView();
}

RGLCommandQueuePtr DeviceVk::CreateCommandQueue(QueueType type)
{
return std::make_shared<CommandQueueVk>(shared_from_this()); // vulkan does not use the queue type
Expand Down
2 changes: 2 additions & 0 deletions src/VkDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace RGL {

DeviceData GetDeviceData() final;

TextureView GetGlobalBindlessTextureHeap() const final;

RGLCommandQueuePtr CreateCommandQueue(QueueType type) final;
RGLFencePtr CreateFence(bool preSignaled) final;
void BlockUntilIdle() final;
Expand Down

0 comments on commit a83433f

Please sign in to comment.