descriptor heap static offset method

This commit is contained in:
Jorijn van der Graaf 2026-04-10 20:30:58 +02:00
commit 5427867fff
2 changed files with 25 additions and 7 deletions

View file

@ -13,7 +13,7 @@ int main() {
Window window(1280, 720, "HelloVulkan"); Window window(1280, 720, "HelloVulkan");
VkCommandBuffer cmd = window.StartInit(); VkCommandBuffer cmd = window.StartInit();
DescriptorHeapVulkan descriptorHeap; DescriptorHeapVulkan descriptorHeap;
descriptorHeap.Initialize(1,1,0); descriptorHeap.Initialize(1,2,0);
VkSpecializationMapEntry entry = { VkSpecializationMapEntry entry = {
.constantID = 0, .constantID = 0,

View file

@ -31,10 +31,11 @@ import :VulkanBuffer;
export namespace Crafter { export namespace Crafter {
struct DescriptorHeapVulkan { struct DescriptorHeapVulkan {
inline static VulkanBuffer<std::uint8_t, true, true> resourceHeap[Window::numFrames]; VulkanBuffer<std::uint8_t, true, true> resourceHeap[Window::numFrames];
inline static VulkanBuffer<std::uint8_t, true, true> samplerHeap[Window::numFrames]; VulkanBuffer<std::uint8_t, true, true> samplerHeap[Window::numFrames];
inline static std::uint32_t bufferStartOffset; std::uint32_t bufferStartOffset;
inline static std::uint16_t bufferStartElement; std::uint16_t bufferStartElement;
void Initialize(std::uint16_t images, std::uint16_t buffers, std::uint16_t samplers) { void Initialize(std::uint16_t images, std::uint16_t buffers, std::uint16_t samplers) {
std::uint32_t descriptorRegion = images * Device::descriptorHeapProperties.imageDescriptorSize + buffers * Device::descriptorHeapProperties.bufferDescriptorSize; std::uint32_t descriptorRegion = images * Device::descriptorHeapProperties.imageDescriptorSize + buffers * Device::descriptorHeapProperties.bufferDescriptorSize;
std::uint32_t alignedDescriptorRegion = (descriptorRegion + Device::descriptorHeapProperties.imageDescriptorAlignment - 1) & ~(Device::descriptorHeapProperties.imageDescriptorAlignment - 1); std::uint32_t alignedDescriptorRegion = (descriptorRegion + Device::descriptorHeapProperties.imageDescriptorAlignment - 1) & ~(Device::descriptorHeapProperties.imageDescriptorAlignment - 1);
@ -47,10 +48,27 @@ export namespace Crafter {
} }
bufferStartOffset = bufferStartElement * Device::descriptorHeapProperties.bufferDescriptorSize; bufferStartOffset = bufferStartElement * Device::descriptorHeapProperties.bufferDescriptorSize;
for(std::uint8_t i = 0; i < Window::numFrames; i++) { for(std::uint8_t i = 0; i < Window::numFrames; i++) {
resourceHeap[i].Create(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, resourceSize); resourceHeap[i].Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, resourceSize);
samplerHeap[i].Create(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, samplerSize); samplerHeap[i].Resize(VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_DESCRIPTOR_HEAP_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, samplerSize);
} }
} }
inline static std::uint32_t GetBufferOffset(std::uint16_t images, std::uint16_t buffers) {
std::uint32_t bufferStartElement = images * Device::descriptorHeapProperties.imageDescriptorSize / Device::descriptorHeapProperties.bufferDescriptorSize;
if(images > 0 && bufferStartElement == 0) {
bufferStartElement = 1;
}
return bufferStartElement * Device::descriptorHeapProperties.bufferDescriptorSize;
}
inline static std::uint16_t GetBufferOffsetElement(std::uint16_t images, std::uint16_t buffers) {
std::uint16_t bufferStartElement = images * Device::descriptorHeapProperties.imageDescriptorSize / Device::descriptorHeapProperties.bufferDescriptorSize;
if(images > 0 && bufferStartElement == 0) {
bufferStartElement = 1;
}
return bufferStartElement;
}
}; };
} }