ray_tracing_gltf/hello_vulkan.cpp
auto HelloVulkan::primitiveToVkGeometry(const nvh::GltfPrimMesh& prim)
{
  // BLAS builder requires raw device addresses.
  VkDeviceAddress vertexAddress = nvvk::getBufferDeviceAddress(m_device, m_vertexBuffer.buffer);
  VkDeviceAddress indexAddress  = nvvk::getBufferDeviceAddress(m_device, m_indexBuffer.buffer);

  uint32_t maxPrimitiveCount = prim.indexCount / 3;

  // Describe buffer as array of VertexObj.
  VkAccelerationStructureGeometryTrianglesDataKHR triangles{VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR};
  triangles.vertexFormat             = VK_FORMAT_R32G32B32_SFLOAT;  // vec3 vertex position data.
  triangles.vertexData.deviceAddress = vertexAddress;
  triangles.vertexStride             = sizeof(nvmath::vec3f);
  // Describe index data (32-bit unsigned int)
  triangles.indexType               = VK_INDEX_TYPE_UINT32;
  triangles.indexData.deviceAddress = indexAddress;
  // Indicate identity transform by setting transformData to null device pointer.
  //triangles.transformData = {};
  triangles.maxVertex = prim.vertexCount;

  // Identify the above data as containing opaque triangles.
  VkAccelerationStructureGeometryKHR asGeom{VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR};
  asGeom.geometryType       = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
  asGeom.flags              = VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;  // For AnyHit
  asGeom.geometry.triangles = triangles;

  VkAccelerationStructureBuildRangeInfoKHR offset;
  offset.firstVertex     = prim.vertexOffset;
  offset.primitiveCount  = maxPrimitiveCount;
  offset.primitiveOffset = prim.firstIndex * sizeof(uint32_t);
  offset.transformOffset = 0;

  // Our blas is made from only one geometry, but could be made of many geometries
  nvvk::RaytracingBuilderKHR::BlasInput input;
  input.asGeometry.emplace_back(asGeom);
  input.asBuildOffsetInfo.emplace_back(offset);

  return input;
}
//--------------------------------------------------------------------------------------------------
// Converting a GLTF primitive in the Raytracing Geometry used for the BLAS
//
Converting a GLTF primitive in the Raytracing Geometry used for the BLAS
Converting a GLTF primitive in the Raytracing Geometry used for the BLAS
auto HelloVulkan::primitiveToVkGeometry(const nvh::GltfPrimMesh& prim)
{
  VkDeviceAddress vertexAddress = nvvk::getBufferDeviceAddress(m_device, m_vertexBuffer.buffer);
  VkDeviceAddress indexAddress  = nvvk::getBufferDeviceAddress(m_device, m_indexBuffer.buffer);
  uint32_t maxPrimitiveCount = prim.indexCount / 3;
  VkAccelerationStructureGeometryTrianglesDataKHR triangles{VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR};
  triangles.vertexFormat             = VK_FORMAT_R32G32B32_SFLOAT;  
  triangles.vertexData.deviceAddress = vertexAddress;
  triangles.vertexStride             = sizeof(nvmath::vec3f);
  triangles.indexType               = VK_INDEX_TYPE_UINT32;
  triangles.indexData.deviceAddress = indexAddress;
  triangles.maxVertex = prim.vertexCount;
  VkAccelerationStructureGeometryKHR asGeom{VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR};
  asGeom.geometryType       = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
  asGeom.flags              = VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR;  
  asGeom.geometry.triangles = triangles;
  VkAccelerationStructureBuildRangeInfoKHR offset;
  offset.firstVertex     = prim.vertexOffset;
  offset.primitiveCount  = maxPrimitiveCount;
  offset.primitiveOffset = prim.firstIndex * sizeof(uint32_t);
  offset.transformOffset = 0;
  nvvk::RaytracingBuilderKHR::BlasInput input;
  input.asGeometry.emplace_back(asGeom);
  input.asBuildOffsetInfo.emplace_back(offset);
  return input;
}
auto HelloVulkan :: primitiveToVkGeometry ( const nvh :: GltfPrimMesh & prim ) { VkDeviceAddress vertexAddress = nvvk :: getBufferDeviceAddress ( m_device , m_vertexBuffer . buffer ) ; VkDeviceAddress indexAddress = nvvk :: getBufferDeviceAddress ( m_device , m_indexBuffer . buffer ) ; uint32_t maxPrimitiveCount = prim . indexCount / 3 ; VkAccelerationStructureGeometryTrianglesDataKHR triangles { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR } ; triangles . vertexFormat = VK_FORMAT_R32G32B32_SFLOAT ; triangles . vertexData . deviceAddress = vertexAddress ; triangles . vertexStride = sizeof ( nvmath :: vec3f ) ; triangles . indexType = VK_INDEX_TYPE_UINT32 ; triangles . indexData . deviceAddress = indexAddress ; triangles . maxVertex = prim . vertexCount ; VkAccelerationStructureGeometryKHR asGeom { VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR } ; asGeom . geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR ; asGeom . flags = VK_GEOMETRY_NO_DUPLICATE_ANY_HIT_INVOCATION_BIT_KHR ; asGeom . geometry . triangles = triangles ; VkAccelerationStructureBuildRangeInfoKHR offset ; offset . firstVertex = prim . vertexOffset ; offset . primitiveCount = maxPrimitiveCount ; offset . primitiveOffset = prim . firstIndex * sizeof ( uint32_t ) ; offset . transformOffset = 0 ; nvvk :: RaytracingBuilderKHR :: BlasInput input ; input . asGeometry . emplace_back ( asGeom ) ; input . asBuildOffsetInfo . emplace_back ( offset ) ; return input ; }
Converting a GLTF primitive in the Raytracing Geometry used for the BLAS
Converting a GLTF primitive in the Raytracing Geometry used for the BLAS
// BLAS builder requires raw device addresses. // Describe buffer as array of VertexObj. // vec3 vertex position data. // Describe index data (32-bit unsigned int) // Indicate identity transform by setting transformData to null device pointer. //triangles.transformData = {}; // Identify the above data as containing opaque triangles. // For AnyHit // Our blas is made from only one geometry, but could be made of many geometries