Skip to content

Commit d8dc4db

Browse files
author
Ravbug
committed
VK: Begin implementing binding the descriptor buffer
1 parent 61b324b commit d8dc4db

File tree

6 files changed

+69
-3
lines changed

6 files changed

+69
-3
lines changed

include/RGL/Texture.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ namespace RGL {
126126
}
127127
#endif
128128
TextureView() {}
129+
130+
uint32_t GetReadonlyBindlessTextureHandle();
129131
};
130132

131133

src/Texture.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "Texture.hpp"
2+
#include "RGL.hpp"
3+
#include "VkTexture.hpp"
4+
#include <stdexcept>
5+
6+
namespace RGL {
7+
uint32_t TextureView::GetReadonlyBindlessTextureHandle() {
8+
auto backend = RGL::CurrentAPI();
9+
switch (backend) {
10+
case API::Direct3D12:
11+
return texture.dx.srvIDX;
12+
case API::Vulkan:
13+
return static_cast<const TextureVk*>(parent)->globalDescriptorIndex;
14+
default:
15+
throw std::runtime_error("Current backend does not support bindless texturing.");
16+
}
17+
return 0;
18+
}
19+
}

src/VkCommandBuffer.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ namespace RGL {
216216

217217
auto vktexture = static_cast<const TextureVk*>(texture.parent);
218218

219+
if (vktexture == nullptr) {
220+
221+
EncodeCommand(CmdBindlessSetTexture{
222+
.bda = texture.texture.vk.bindlessInfo.bda,
223+
.binding = index
224+
});
225+
226+
return;
227+
}
228+
219229
auto nextLayout = vktexture->createdConfig.usage.DepthStencilAttachment ? VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
220230

221231
auto activeLayout = currentRenderPipeline ? currentRenderPipeline->pipelineLayout : currentComputePipeline->pipelineLayout;
@@ -687,6 +697,29 @@ namespace RGL {
687697
swapchainImages.insert(key.texture);
688698
}
689699
},
700+
[this](const CmdBindlessSetTexture& arg) {
701+
// tell the command buffer about the descriptor buffer
702+
VkDescriptorBufferBindingInfoEXT descriptor_buffer_binding_info{
703+
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_BUFFER_BINDING_INFO_EXT,
704+
.address = arg.bda,
705+
.usage = VK_BUFFER_USAGE_2_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SAMPLER_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT
706+
};
707+
owningQueue->owningDevice->rgl_vkCmdBindDescriptorBuffersEXT(commandBuffer,1, &descriptor_buffer_binding_info);
708+
709+
uint32_t bufferIndices[] = { arg.binding };
710+
VkDeviceSize offset = 0;
711+
712+
// assign it a binding slot
713+
const bool isCompute = currentRenderPipeline ? false : true;
714+
const auto activeLayout = isCompute ? currentComputePipeline->pipelineLayout : currentRenderPipeline->pipelineLayout;
715+
owningQueue->owningDevice->rgl_vkCmdSetDescriptorBufferOffsetsEXT(commandBuffer,
716+
isCompute ? VK_PIPELINE_BIND_POINT_COMPUTE : VK_PIPELINE_BIND_POINT_GRAPHICS,
717+
activeLayout->layout,
718+
0,
719+
1,
720+
bufferIndices,
721+
&offset);
722+
},
690723
[this](const CmdDraw& arg) {
691724
vkCmdDraw(commandBuffer, arg.nVertices, arg.config.nInstances, arg.config.startVertex, arg.config.firstInstance);
692725
},

src/VkCommandBuffer.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ namespace RGL {
172172
uint32_t size;
173173
};
174174

175+
struct CmdBindlessSetTexture {
176+
const VkDeviceAddress bda;
177+
const uint32_t binding;
178+
};
179+
175180
struct CmdCopyTextureToTexture {
176181
TextureCopyConfig from, to;
177182
uint32_t fromMip, fromLayer, toMip, toLayer;
@@ -201,7 +206,8 @@ namespace RGL {
201206
CmdSetViewport,
202207
CmdSetScissor,
203208
CmdCopyBufferToBuffer,
204-
CmdCopyBuffertoTexture
209+
CmdCopyBuffertoTexture,
210+
CmdBindlessSetTexture
205211
>
206212
> renderCommands;
207213

src/VkDevice.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ namespace RGL {
257257
loadVulkanFunction(device, rgl_vkGetDescriptorSetLayoutSizeEXT, "vkGetDescriptorSetLayoutSizeEXT");
258258
loadVulkanFunction(device, rgl_vkGetDescriptorSetLayoutBindingOffsetEXT, "vkGetDescriptorSetLayoutBindingOffsetEXT");
259259
loadVulkanFunction(device, rgl_vkGetDescriptorEXT, "vkGetDescriptorEXT");
260+
loadVulkanFunction(device, rgl_vkCmdBindDescriptorBuffersEXT, "vkCmdBindDescriptorBuffersEXT");
261+
loadVulkanFunction(device, rgl_vkCmdSetDescriptorBufferOffsetsEXT, "vkCmdSetDescriptorBufferOffsetsEXT");
260262

261263
#ifndef NDEBUG
262264
loadVulkanFunction(device, rgl_vkDebugMarkerSetObjectNameEXT, "vkDebugMarkerSetObjectNameEXT");
@@ -305,11 +307,11 @@ namespace RGL {
305307
.pBindings = &set_layout_binding
306308
};
307309

308-
;
310+
;
309311

310312
VK_CHECK(vkCreateDescriptorSetLayout(device, &descriptor_layout_create_info, nullptr, &globalDescriptorSetLayout));
311313
rgl_vkGetDescriptorSetLayoutSizeEXT(device, globalDescriptorSetLayout, &globalDescriptorSetSize);
312-
globalDescriptorBufferAllocation = createBuffer(this, globalDescriptorSetSize,VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SAMPLER_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU, globalDescriptorBuffer);
314+
globalDescriptorBufferAllocation = createBuffer(this, globalDescriptorSetSize, VK_BUFFER_USAGE_2_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SAMPLER_DESCRIPTOR_BUFFER_BIT_EXT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VMA_MEMORY_USAGE_CPU_TO_GPU, globalDescriptorBuffer);
313315

314316
vmaMapMemory(vkallocator, globalDescriptorBufferAllocation, &globalDescriptorMappedMemory);
315317

@@ -330,6 +332,8 @@ namespace RGL {
330332
.buffer = globalDescriptorBuffer
331333
};
332334
globalDescriptorBDA = vkGetBufferDeviceAddress(device, &bdaInfo);
335+
336+
SetDebugNameForResource(globalDescriptorBuffer, VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT, "Bindless Image Descriptor Buffer");
333337
}
334338

335339
void DeviceVk::SetDebugNameForResource(void* resource, VkDebugReportObjectTypeEXT type, const char* debugName)

src/VkDevice.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace RGL {
3434
PFN_vkGetDescriptorSetLayoutSizeEXT rgl_vkGetDescriptorSetLayoutSizeEXT = nullptr;
3535
PFN_vkGetDescriptorSetLayoutBindingOffsetEXT rgl_vkGetDescriptorSetLayoutBindingOffsetEXT = nullptr;
3636
PFN_vkGetDescriptorEXT rgl_vkGetDescriptorEXT = nullptr;
37+
PFN_vkCmdBindDescriptorBuffersEXT rgl_vkCmdBindDescriptorBuffersEXT = nullptr;
38+
PFN_vkCmdSetDescriptorBufferOffsetsEXT rgl_vkCmdSetDescriptorBufferOffsetsEXT = nullptr;
3739

3840
virtual ~DeviceVk();
3941
DeviceVk(decltype(physicalDevice) physicalDevice);

0 commit comments

Comments
 (0)