Skip to content

Commit

Permalink
Upgrade to use the finalized (so-called) VKRay API.
Browse files Browse the repository at this point in the history
  • Loading branch information
feiy committed May 18, 2021
1 parent 615b93d commit 5b32a6f
Show file tree
Hide file tree
Showing 14 changed files with 1,235 additions and 349 deletions.
83 changes: 44 additions & 39 deletions Context_ex.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,44 @@ namespace VkInline
{
BaseLevelAS::BaseLevelAS(SVBuffer* indBuf, SVBuffer* posBuf)
{
VkAccelerationStructureCreateGeometryTypeInfoKHR acceleration_create_geometry_info = {};
acceleration_create_geometry_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_GEOMETRY_TYPE_INFO_KHR;
acceleration_create_geometry_info.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
acceleration_create_geometry_info.maxPrimitiveCount = (unsigned)(indBuf == nullptr ? posBuf->size() / 3: indBuf->size()/3);
acceleration_create_geometry_info.indexType = VK_INDEX_TYPE_NONE_KHR;
if (indBuf != nullptr)
acceleration_create_geometry_info.indexType = indBuf->elem_size() > 2? VK_INDEX_TYPE_UINT32: VK_INDEX_TYPE_UINT16;
acceleration_create_geometry_info.maxVertexCount = (unsigned)posBuf->size();

if (posBuf->name_elem_type() == "vec2")
acceleration_create_geometry_info.vertexFormat = VK_FORMAT_R32G32_SFLOAT;
else if(posBuf->name_elem_type() == "vec3")
acceleration_create_geometry_info.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;

VkAccelerationStructureGeometryKHR acceleration_geometry = {};
acceleration_geometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR;
acceleration_geometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
acceleration_geometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
acceleration_geometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
acceleration_geometry.geometry.triangles.vertexFormat = acceleration_create_geometry_info.vertexFormat;
acceleration_geometry.geometry.triangles.vertexData.deviceAddress = posBuf->internal()->get_device_address();
acceleration_geometry.geometry.triangles.vertexStride = posBuf->elem_size();
acceleration_geometry.geometry.triangles.indexType = acceleration_create_geometry_info.indexType;
acceleration_geometry.geometry.triangles.indexData.deviceAddress = indBuf==nullptr? 0: indBuf->internal()->get_device_address();

VkAccelerationStructureBuildOffsetInfoKHR acceleration_build_offset_info = {};
acceleration_build_offset_info.primitiveCount = (unsigned)(indBuf == nullptr ? posBuf->size() / 3 : indBuf->size() / 3);
acceleration_build_offset_info.primitiveOffset = 0x0;
acceleration_build_offset_info.firstVertex = 0;
acceleration_build_offset_info.transformOffset = 0x0;

m_blas = new Internal::BaseLevelAS(1, &acceleration_create_geometry_info, &acceleration_geometry, &acceleration_build_offset_info);
acceleration_geometry.geometry.triangles.vertexData.deviceAddress = posBuf->internal()->get_device_address();
acceleration_geometry.geometry.triangles.maxVertex = (unsigned)posBuf->size();
acceleration_geometry.geometry.triangles.vertexStride = posBuf->elem_size();
acceleration_geometry.geometry.triangles.indexData.deviceAddress = indBuf == nullptr ? 0 : indBuf->internal()->get_device_address();

if (posBuf->name_elem_type() == "vec2")
acceleration_geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32_SFLOAT;
else if (posBuf->name_elem_type() == "vec3")
acceleration_geometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;

acceleration_geometry.geometry.triangles.indexType = VK_INDEX_TYPE_NONE_KHR;
if (indBuf != nullptr)
acceleration_geometry.geometry.triangles.indexType = indBuf->elem_size() > 2 ? VK_INDEX_TYPE_UINT32 : VK_INDEX_TYPE_UINT16;

VkAccelerationStructureBuildGeometryInfoKHR geoBuildInfo{};
geoBuildInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR;
geoBuildInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
geoBuildInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
geoBuildInfo.geometryCount = 1;
geoBuildInfo.pGeometries = &acceleration_geometry;

VkAccelerationStructureBuildRangeInfoKHR range{};
range.primitiveCount = (unsigned)(indBuf == nullptr ? posBuf->size() / 3 : indBuf->size() / 3);
range.primitiveOffset = 0;
range.firstVertex = 0;
range.transformOffset = 0;
const VkAccelerationStructureBuildRangeInfoKHR* ranges = ⦥

m_blas = new Internal::BaseLevelAS(geoBuildInfo, &acceleration_geometry, &ranges);
}

BaseLevelAS::BaseLevelAS(SVBuffer* aabbBuf)
{
VkAccelerationStructureCreateGeometryTypeInfoKHR acceleration_create_geometry_info = {};
acceleration_create_geometry_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_GEOMETRY_TYPE_INFO_KHR;
acceleration_create_geometry_info.geometryType = VK_GEOMETRY_TYPE_AABBS_KHR;
acceleration_create_geometry_info.maxPrimitiveCount = 1;

VkAccelerationStructureGeometryKHR acceleration_geometry = {};
acceleration_geometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR;
acceleration_geometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
Expand All @@ -51,13 +48,21 @@ namespace VkInline
acceleration_geometry.geometry.aabbs.data.deviceAddress = aabbBuf->internal()->get_device_address();
acceleration_geometry.geometry.aabbs.stride = 0;

VkAccelerationStructureBuildOffsetInfoKHR acceleration_build_offset_info{};
acceleration_build_offset_info.primitiveCount = 1;
acceleration_build_offset_info.primitiveOffset = 0x0;
acceleration_build_offset_info.firstVertex = 0;
acceleration_build_offset_info.transformOffset = 0x0;

m_blas = new Internal::BaseLevelAS(1, &acceleration_create_geometry_info, &acceleration_geometry, &acceleration_build_offset_info);
VkAccelerationStructureBuildGeometryInfoKHR geoBuildInfo{};
geoBuildInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR;
geoBuildInfo.type = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR;
geoBuildInfo.flags = VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR;
geoBuildInfo.geometryCount = 1;
geoBuildInfo.pGeometries = &acceleration_geometry;

VkAccelerationStructureBuildRangeInfoKHR range{};
range.primitiveCount = 1;
range.primitiveOffset = 0;
range.firstVertex = 0;
range.transformOffset = 0;
const VkAccelerationStructureBuildRangeInfoKHR* ranges = ⦥

m_blas = new Internal::BaseLevelAS(geoBuildInfo, &acceleration_geometry, &ranges);
}

BaseLevelAS::~BaseLevelAS()
Expand Down
2 changes: 1 addition & 1 deletion SVBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace VkInline
m_name_view_type = std::string("Buf_") + Add_Dynamic_Code(code.c_str());
VkBufferUsageFlags usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
#ifdef _VkInlineEX
usage |= VK_BUFFER_USAGE_RAY_TRACING_BIT_KHR;
usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR;
#endif
m_data = new Internal::DeviceBuffer(m_elem_size*m_size, usage);
if (hdata != nullptr)
Expand Down
22 changes: 9 additions & 13 deletions internal/internal_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,22 @@ namespace VkInline


{
m_bufferDeviceAddressFeatures = {};
#ifndef _VkInlineEX
m_bufferDeviceAddressFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT;
#else
m_bufferDeviceAddressFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES;
#endif
m_bufferDeviceAddressFeatures.pNext = &m_descriptorIndexingFeatures;
m_descriptorIndexingFeatures = {};
m_descriptorIndexingFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT;
m_descriptorIndexingFeatures.pNext = &m_scalarBlockLayoutFeatures;
m_scalarBlockLayoutFeatures = {};
m_scalarBlockLayoutFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT;

#ifdef _VkInlineEX
m_scalarBlockLayoutFeatures.pNext = &m_raytracingFeatures;
m_raytracingFeatures = {};
m_raytracingFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_FEATURES_KHR;
m_scalarBlockLayoutFeatures.pNext = &m_rayTracingPipelineFeatures;
m_rayTracingPipelineFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR;
m_rayTracingPipelineFeatures.pNext = &m_accelerationStructureFeatures;
m_accelerationStructureFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR;
#endif

m_features2 = {};
m_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
m_features2.pNext = &m_bufferDeviceAddressFeatures;
vkGetPhysicalDeviceFeatures2(m_physicalDevice, &m_features2);
Expand All @@ -144,12 +140,11 @@ namespace VkInline

#ifdef _VkInlineEX
{
m_raytracingProperties = {};
m_raytracingProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PROPERTIES_KHR;
m_rayTracingPipelineProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_PROPERTIES_KHR;
VkPhysicalDeviceProperties2 props = {};
props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
props.pNext = &m_raytracingProperties;
vkGetPhysicalDeviceProperties2(m_physicalDevice, &props);
props.pNext = &m_rayTracingPipelineProperties;
vkGetPhysicalDeviceProperties2(m_physicalDevice, &props);
}
#endif

Expand Down Expand Up @@ -180,7 +175,8 @@ namespace VkInline
#else
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME,
VK_KHR_RAY_TRACING_EXTENSION_NAME,
VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME,
VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME,
VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME,
VK_KHR_PIPELINE_LIBRARY_EXTENSION_NAME,
#endif
Expand Down
23 changes: 12 additions & 11 deletions internal/internal_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace VkInline
const VkDevice& device() const { return m_device; }

#ifdef _VkInlineEX
const VkPhysicalDeviceRayTracingPropertiesKHR& raytracing_properties() const { return m_raytracingProperties; }
const VkPhysicalDeviceRayTracingPipelinePropertiesKHR& raytracing_properties() const { return m_rayTracingPipelineProperties; }
#endif

struct Stream
Expand All @@ -40,20 +40,21 @@ namespace VkInline
void Wait() const;

private:
VkDebugUtilsMessengerEXT m_debugMessenger;
VkInstance m_instance;
VkPhysicalDevice m_physicalDevice;
VkDebugUtilsMessengerEXT m_debugMessenger = VK_NULL_HANDLE;
VkInstance m_instance = VK_NULL_HANDLE;
VkPhysicalDevice m_physicalDevice = VK_NULL_HANDLE;

#ifndef _VkInlineEX
VkPhysicalDeviceBufferDeviceAddressFeaturesEXT m_bufferDeviceAddressFeatures;
VkPhysicalDeviceBufferDeviceAddressFeaturesEXT m_bufferDeviceAddressFeatures{};
#else
VkPhysicalDeviceBufferDeviceAddressFeatures m_bufferDeviceAddressFeatures;
VkPhysicalDeviceRayTracingFeaturesKHR m_raytracingFeatures;
VkPhysicalDeviceRayTracingPropertiesKHR m_raytracingProperties;
VkPhysicalDeviceBufferDeviceAddressFeatures m_bufferDeviceAddressFeatures{};
VkPhysicalDeviceRayTracingPipelineFeaturesKHR m_rayTracingPipelineFeatures{};
VkPhysicalDeviceAccelerationStructureFeaturesKHR m_accelerationStructureFeatures{};
VkPhysicalDeviceRayTracingPipelinePropertiesKHR m_rayTracingPipelineProperties{};
#endif
VkPhysicalDeviceDescriptorIndexingFeatures m_descriptorIndexingFeatures;
VkPhysicalDeviceScalarBlockLayoutFeatures m_scalarBlockLayoutFeatures;
VkPhysicalDeviceFeatures2 m_features2;
VkPhysicalDeviceDescriptorIndexingFeatures m_descriptorIndexingFeatures{};
VkPhysicalDeviceScalarBlockLayoutFeatures m_scalarBlockLayoutFeatures{};
VkPhysicalDeviceFeatures2 m_features2{};
uint32_t m_queueFamily;
float m_queuePriority;
VkDevice m_device;
Expand Down
22 changes: 13 additions & 9 deletions internal/internal_context_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ namespace VkInline
class BaseLevelAS : public AS
{
public:
BaseLevelAS(uint32_t geometryCount, const VkAccelerationStructureCreateGeometryTypeInfoKHR* geoTypeInfo, const VkAccelerationStructureGeometryKHR* pGeometries, const VkAccelerationStructureBuildOffsetInfoKHR* offsets);
BaseLevelAS(
const VkAccelerationStructureBuildGeometryInfoKHR& geoBuildInfo,
const VkAccelerationStructureGeometryKHR* pGeometries,
const VkAccelerationStructureBuildRangeInfoKHR** ranges);

virtual ~BaseLevelAS();

private:
Expand Down Expand Up @@ -60,10 +64,10 @@ namespace VkInline
const VkDescriptorSetLayout& layout_desc() const { return m_descriptorSetLayout; }
const VkPipelineLayout& layout_pipeline() const { return m_pipelineLayout; }
const VkPipeline& pipeline() const { return m_pipeline; }
const VkStridedBufferRegionKHR& sbt_entry_raygen() const { return m_sbt_entry_raygen; }
const VkStridedBufferRegionKHR& sbt_entry_miss() const { return m_sbt_entry_miss; }
const VkStridedBufferRegionKHR& sbt_entry_hit() const { return m_sbt_entry_hit; }
const VkStridedBufferRegionKHR& sbt_entry_callable() const { return m_sbt_entry_callable; }
const VkStridedDeviceAddressRegionKHR& sbt_entry_raygen() const { return m_sbt_entry_raygen; }
const VkStridedDeviceAddressRegionKHR& sbt_entry_miss() const { return m_sbt_entry_miss; }
const VkStridedDeviceAddressRegionKHR& sbt_entry_hit() const { return m_sbt_entry_hit; }
const VkStridedDeviceAddressRegionKHR& sbt_entry_callable() const { return m_sbt_entry_callable; }

size_t num_tlas() const { return m_num_tlas; }
size_t num_tex2d() const { return m_num_tex2d; }
Expand All @@ -78,10 +82,10 @@ namespace VkInline
VkPipelineLayout m_pipelineLayout;
VkPipeline m_pipeline;
DeviceBuffer* m_shaderBindingTableBuffer;
VkStridedBufferRegionKHR m_sbt_entry_raygen;
VkStridedBufferRegionKHR m_sbt_entry_miss;
VkStridedBufferRegionKHR m_sbt_entry_hit;
VkStridedBufferRegionKHR m_sbt_entry_callable;
VkStridedDeviceAddressRegionKHR m_sbt_entry_raygen;
VkStridedDeviceAddressRegionKHR m_sbt_entry_miss;
VkStridedDeviceAddressRegionKHR m_sbt_entry_hit;
VkStridedDeviceAddressRegionKHR m_sbt_entry_callable;

size_t m_num_tlas;
size_t m_num_tex2d;
Expand Down
Loading

0 comments on commit 5b32a6f

Please sign in to comment.