From 118b00eeadfcbd7c6442aa3a357cb95a012e8983 Mon Sep 17 00:00:00 2001 From: John Goetz Date: Wed, 4 Jun 2025 13:54:52 -0700 Subject: [PATCH 1/3] added pNext allocInfo for createBufferAndMemory() --- include/vsg/state/Buffer.h | 2 +- src/vsg/state/Buffer.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/vsg/state/Buffer.h b/include/vsg/state/Buffer.h index 9a4a3815da..f4c6530154 100644 --- a/include/vsg/state/Buffer.h +++ b/include/vsg/state/Buffer.h @@ -80,6 +80,6 @@ namespace vsg }; VSG_type_name(vsg::Buffer); - extern VSG_DECLSPEC ref_ptr createBufferAndMemory(Device* device, VkDeviceSize in_size, VkBufferUsageFlags in_usage, VkSharingMode in_sharingMode, VkMemoryPropertyFlags memoryProperties); + extern VSG_DECLSPEC ref_ptr createBufferAndMemory(Device* device, VkDeviceSize in_size, VkBufferUsageFlags in_usage, VkSharingMode in_sharingMode, VkMemoryPropertyFlags memoryProperties, void* pNextAllocInfo = nullptr); } // namespace vsg diff --git a/src/vsg/state/Buffer.cpp b/src/vsg/state/Buffer.cpp index a4e22c8e7c..28bf024c1e 100644 --- a/src/vsg/state/Buffer.cpp +++ b/src/vsg/state/Buffer.cpp @@ -153,13 +153,13 @@ size_t Buffer::totalReservedSize() const return _memorySlots.totalReservedSize(); } -ref_ptr vsg::createBufferAndMemory(Device* device, VkDeviceSize size, VkBufferUsageFlags usage, VkSharingMode sharingMode, VkMemoryPropertyFlags memoryProperties) +ref_ptr vsg::createBufferAndMemory(Device* device, VkDeviceSize size, VkBufferUsageFlags usage, VkSharingMode sharingMode, VkMemoryPropertyFlags memoryProperties, void* pNextAllocInfo) { auto buffer = vsg::Buffer::create(size, usage, sharingMode); buffer->compile(device); auto memRequirements = buffer->getMemoryRequirements(device->deviceID); - auto memory = vsg::DeviceMemory::create(device, memRequirements, memoryProperties); + auto memory = vsg::DeviceMemory::create(device, memRequirements, memoryProperties, pNextAllocInfo); buffer->bind(memory, 0); return buffer; From d686d8c5c7abbb068b86e9fd1b2a00988728f9d5 Mon Sep 17 00:00:00 2001 From: John Goetz Date: Wed, 4 Jun 2025 13:56:56 -0700 Subject: [PATCH 2/3] set allocate device address bit for ray tracing memory --- src/vsg/raytracing/AccelerationStructure.cpp | 6 ++++-- src/vsg/raytracing/RayTracingPipeline.cpp | 5 ++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/vsg/raytracing/AccelerationStructure.cpp b/src/vsg/raytracing/AccelerationStructure.cpp index 29a92053d4..90846b4028 100644 --- a/src/vsg/raytracing/AccelerationStructure.cpp +++ b/src/vsg/raytracing/AccelerationStructure.cpp @@ -62,8 +62,10 @@ void AccelerationStructure::compile(Context& context) _geometryPrimitiveCounts.data(), &accelerationStructureBuildSizesInfo); - _buffer = vsg::createBufferAndMemory(context.device, accelerationStructureBuildSizesInfo.accelerationStructureSize, - VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + VkMemoryAllocateFlagsInfo memFlags = {}; + memFlags.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO; + memFlags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; + _buffer = vsg::createBufferAndMemory(context.device, accelerationStructureBuildSizesInfo.accelerationStructureSize, VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &memFlags); _accelerationStructureInfo.buffer = _buffer->vk(context.deviceID); _accelerationStructureInfo.size = accelerationStructureBuildSizesInfo.accelerationStructureSize; diff --git a/src/vsg/raytracing/RayTracingPipeline.cpp b/src/vsg/raytracing/RayTracingPipeline.cpp index d24c5d93f3..021deacd37 100644 --- a/src/vsg/raytracing/RayTracingPipeline.cpp +++ b/src/vsg/raytracing/RayTracingPipeline.cpp @@ -179,9 +179,12 @@ RayTracingPipeline::Implementation::Implementation(Context& context, RayTracingP //auto bindingTableBuffer = bindingTableBufferInfo.buffer; //auto bindingTableMemory = bindingTableBuffer->getDeviceMemory(context.deviceID); std::vector> bindingTableBuffers(rayTracingShaderGroups.size()); + VkMemoryAllocateFlagsInfo memFlags = {}; + memFlags.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO; + memFlags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; for (size_t i = 0; i < bindingTableBuffers.size(); ++i) { - bindingTableBuffers[i] = createBufferAndMemory(_device, handleSizeAligned, VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + bindingTableBuffers[i] = createBufferAndMemory(_device, handleSizeAligned, VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &memFlags); } std::vector shaderHandleStorage(sbtSize); From 9bf5c970efb6560a5b77c7acdaa3514d6ef95248 Mon Sep 17 00:00:00 2001 From: John Goetz Date: Wed, 4 Jun 2025 13:58:27 -0700 Subject: [PATCH 3/3] set allocate device address bit for memory allocations This resolves vulkan validation errors when creating memory with the device address usage bit but without the bit set in the pNext structure. --- src/vsg/raytracing/AccelerationGeometry.cpp | 12 ++++++++++-- src/vsg/vk/Context.cpp | 5 ++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/vsg/raytracing/AccelerationGeometry.cpp b/src/vsg/raytracing/AccelerationGeometry.cpp index 655cfe1277..57f7db9f1a 100644 --- a/src/vsg/raytracing/AccelerationGeometry.cpp +++ b/src/vsg/raytracing/AccelerationGeometry.cpp @@ -55,9 +55,17 @@ void AccelerationGeometry::compile(Context& context) auto vertexBufferInfo = vsg::createBufferAndTransferData(context, vertexDataList, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE); auto indexBufferInfo = vsg::createBufferAndTransferData(context, indexDataList, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_SHARING_MODE_EXCLUSIVE); #else - auto vertexBufferInfo = vsg::createHostVisibleBuffer(context.device, vertexDataList, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE); + auto vertexBufferInfo = vsg::createHostVisibleBuffer(context.device, vertexDataList, + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | + VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | + VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, + VK_SHARING_MODE_EXCLUSIVE); vsg::copyDataListToBuffers(context.device, vertexBufferInfo); - auto indexBufferInfo = vsg::createHostVisibleBuffer(context.device, indexDataList, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE); + auto indexBufferInfo = vsg::createHostVisibleBuffer(context.device, indexDataList, + VK_BUFFER_USAGE_INDEX_BUFFER_BIT | + VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | + VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR, + VK_SHARING_MODE_EXCLUSIVE); vsg::copyDataListToBuffers(context.device, indexBufferInfo); #endif diff --git a/src/vsg/vk/Context.cpp b/src/vsg/vk/Context.cpp index 7fc6ccbcf5..fb2fa0215d 100644 --- a/src/vsg/vk/Context.cpp +++ b/src/vsg/vk/Context.cpp @@ -286,7 +286,10 @@ bool Context::record() // create scratch buffer and issue build acceleration structure commands if (scratchBufferSize > 0) { - ref_ptr scratchBuffer = vsg::createBufferAndMemory(device, scratchBufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + VkMemoryAllocateFlagsInfo memFlags = {}; + memFlags.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO; + memFlags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT; + ref_ptr scratchBuffer = vsg::createBufferAndMemory(device, scratchBufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VK_SHARING_MODE_EXCLUSIVE, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &memFlags); for (auto& command : buildAccelerationStructureCommands) {