Skip to content

Commit

Permalink
D3D12: adapt bindless to work with Vulkan
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jul 9, 2024
1 parent c2f1340 commit 6eeb5f1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/RGL/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ namespace RGL {
rtvIDX = unallocated,
srvIDX = unallocated,
uavIDX = unallocated;

bool representsBindless = false;

bool dsvAllocated() const {
return dsvIDX != unallocated;
Expand Down
2 changes: 1 addition & 1 deletion src/D3D12CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ namespace RGL {
bool isGraphics = (bool)currentRenderPipeline;

const auto pipelineLayout = isGraphics ? currentRenderPipeline->pipelineLayout : currentComputePipeline->pipelineLayout;
const auto textureSlot = pipelineLayout->slotForTextureIdx(index);
const auto textureSlot = pipelineLayout->slotForTextureIdx(index, texture.texture.dx.representsBindless);

// if this is a UAV, then we need the UAV state rather than pixel shader resource
if (textureSlot.isUAV) {
Expand Down
1 change: 1 addition & 0 deletions src/D3D12Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ namespace RGL {
.rtvIDX = 0,
.srvIDX = 0,
.uavIDX = 0,
.representsBindless = true,
.parentResource = nullptr, // bindless must set barriers elsewhere
.coveredMips = ALL_MIPS,
.coveredLayers = ALL_LAYERS
Expand Down
4 changes: 2 additions & 2 deletions src/D3D12RenderPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ namespace RGL {
.RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
.NumDescriptors = item.count,
.BaseShaderRegister = item.binding,
.RegisterSpace = 0,
.RegisterSpace = item.isBindless ? 1u : 0u,
.Flags = D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE,
.OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
});
textureBindingToRootSlot[item.binding] = { uint32_t(rootParameters.size()), false };
textureBindingToRootSlot[{item.binding, item.isBindless ? 1u : 0u}] = { uint32_t(rootParameters.size()), false };
rootParameters.emplace_back().InitAsDescriptorTable(1, &range);
}
break;
Expand Down
36 changes: 32 additions & 4 deletions src/D3D12RenderPipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
#include <unordered_map>
#include "D3D12PipelineShared.hpp"

namespace RGL {

struct textureBindingKey {
uint32_t binding;
uint32_t space = 0;
textureBindingKey(uint32_t binding) : binding(binding) {}
textureBindingKey(uint32_t binding, uint32_t space) : binding(binding), space(space) {}

bool operator==(const textureBindingKey& other) const {
return binding == other.binding && space == other.space;
}
};
}

namespace std {
template <> struct hash<RGL::textureBindingKey>
{
size_t operator()(const RGL::textureBindingKey& x) const
{
return size_t(x.binding) << 32 + x.space;
}
};
}


namespace RGL {
struct DeviceD3D12;
struct PipelineLayoutD3D12 : public IPipelineLayout {
Expand All @@ -25,8 +50,11 @@ namespace RGL {
uint32_t slot;
bool isUAV;
};



std::unordered_map<uint32_t, bufferBindInfo> bufferBindingToRootSlot;
std::unordered_map<uint32_t, textureBindInfo> textureBindingToRootSlot;
std::unordered_map<textureBindingKey, textureBindInfo> textureBindingToRootSlot;
std::unordered_map < uint32_t, uint32_t> samplerBindingtoRootSlot;

auto slotForBufferIdx(uint32_t bindingPos) {
Expand All @@ -37,8 +65,8 @@ namespace RGL {
return samplerBindingtoRootSlot.at(bindingPos);
}

auto slotForTextureIdx(uint32_t bindingPos) {
return textureBindingToRootSlot.at(bindingPos);
auto slotForTextureIdx(uint32_t bindingPos, bool isBindless) {
return textureBindingToRootSlot.at({ bindingPos, isBindless? 1u : 0u });
}

bool bufferIdxIsUAV(uint32_t bindingPos) {
Expand All @@ -58,4 +86,4 @@ namespace RGL {

RenderPipelineD3D12(decltype(owningDevice), const RenderPipelineDescriptor&);
};
}
}
2 changes: 0 additions & 2 deletions src/VkDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,7 @@ namespace RGL {
RGL::DeviceVk::~DeviceVk() {

vkDestroyDescriptorPool(device, globalDescriptorPool, VK_NULL_HANDLE);
vmaUnmapMemory(vkallocator, globalDescriptorBufferAllocation);
vkDestroyDescriptorSetLayout(device, globalDescriptorSetLayout, VK_NULL_HANDLE);
vmaFreeMemory(vkallocator, globalDescriptorBufferAllocation);
vmaDestroyAllocator(vkallocator);
vkDestroyCommandPool(device, commandPool, nullptr);
vkDestroyDevice(device, nullptr);
Expand Down
2 changes: 0 additions & 2 deletions src/VkDevice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ namespace RGL {

uint32_t frameIndex = 0;

VkBuffer globalDescriptorBuffer = VK_NULL_HANDLE;
VmaAllocation globalDescriptorBufferAllocation = VK_NULL_HANDLE;
VkDescriptorSetLayout globalDescriptorSetLayout = VK_NULL_HANDLE;

constexpr static uint32_t nDescriptors = 2048; // made-up number (matches the DX backend)
Expand Down

0 comments on commit 6eeb5f1

Please sign in to comment.