36. Ray Tracing
Ray tracing uses a separate rendering pipeline from both the graphics and compute pipelines (see Ray Tracing Pipeline).
Within the ray tracing pipeline, OpTraceRayKHR
can be called to
perform a ray traversal that invokes the various ray
tracing shader stages during its execution.
The relationship between the ray tracing pipeline object and the geometries
present in the acceleration structure traversed is passed into the ray
tracing command in a VkBuffer object known as a shader binding
table.
OpExecuteCallableKHR
can also be used in ray tracing pipelines to
invoke a callable shader.
During execution, control alternates between scheduling and other operations. The scheduling functionality is implementation-specific and is responsible for workload execution. The shader stages are programmable. Traversal, which refers to the process of traversing acceleration structures to find potential intersections of rays with geometry, is fixed function.
The programmable portions of the pipeline are exposed in a single-ray
programming model, with each invocation handling one ray at a time.
Memory operations can be synchronized using standard memory barriers.
The Workgroup
scope and variables with a storage class of
Workgroup
must not be used in the ray tracing pipeline.
36.1. Shader Call Instructions
A shader call is an instruction which may cause execution to continue elsewhere by creating one or more invocations that execute a different shader stage.
The shader call instructions are:
-
OpTraceRayKHR
which may invoke intersection, any-hit, closest hit, or miss shaders, -
OpReportIntersectionKHR
which may invoke any-hit shaders, and -
OpExecuteCallableKHR
which will invoke a callable shader.
The invocations created by shader call instructions are grouped into subgroups by the implementation. Those subgroups may be unrelated to the subgroup of the parent invocation.
Shader call instructions can be used recursively; invoked shaders can
themselves execute shader call instructions, to a maximum depth defined by
the maxRecursionDepth
limit.
Shaders directly invoked from the API always have a recursion depth of 0;
each shader executed by a shader call instruction has a recursion depth one
higher than the recursion depth of the shader which invoked it.
Applications must not invoke a shader with a recursion depth greater than
the value of maxRecursionDepth
specified in the pipeline.
An invocation repack instruction is a ray tracing shader call instruction
where the implementation may change the set of invocations that are
executing.
When a repack instruction is encountered, the invocation is suspended and a
new invocation begins and executes the instruction.
After executing the repack instruction (which may result in other ray
tracing shader stages executing) the new invocation ends and the original
invocation is resumed, but it may be resumed in a different subgroup or at
a different SubgroupLocalInvocationId
within the same subgroup.
When a subset of invocations in a subgroup execute the invocation repack
instruction, those that do not execute it remain in the same subgroup at the
same SubgroupLocalInvocationId
.
The OpTraceRayKHR
, OpReportIntersectionKHR
, and
OpExecuteCallableKHR
instructions are invocation repack instructions.
The invocations that are executing before an invocation repack instruction, after the instruction, or are created by the instruction, are shader-call-related.
If the implementation changes the composition of subgroups, the values of
SubgroupLocalInvocationId
,
SMIDNV
, WarpIDNV
,
and builtin variables that are derived from them (SubgroupEqMask
,
SubgroupGeMask
, SubgroupGtMask
, SubgroupLeMask
,
SubgroupLtMask
) must be changed accordingly by the invocation repack
instruction.
Note
Subgroup operations are permitted in the programmable ray tracing shader stages. However, shader call instructions place a bound on where results of subgroup instructions or subgroup-scoped instructions that execute the dynamic instance of that instruction are potentially valid. For example, care must be taken when using the result of a ballot operation that was computed before an invocation repack instruction, after that repack instruction. The ballot may be incorrect as the set of invocations could have changed. For clock operations, the value of a |
When a ray tracing shader executes a dynamic instance of an invocation repack instruction which results in another ray tracing shader being invoked, their instructions are related by shader-call-order.
For ray tracing invocations that are shader-call-related:
-
memory operations on
StorageBuffer
,Image
, andShaderRecordBufferKHR
storage classes can be synchronized using theShaderCallKHR
scope. -
the
CallableDataKHR
,IncomingCallableDataKHR
,RayPayloadKHR
,HitAttributeKHR
, andIncomingRayPayloadKHR
storage classes are system-synchronized and no application availability and visibility operations are required. -
memory operations within a single invocation before and after the invocation repack instruction are ordered by program-order and do not require explicit synchronzation.
36.2. Ray Tracing Commands
Ray tracing commands provoke work in the ray tracing pipeline. Ray tracing commands are recorded into a command buffer and when executed by a queue will produce work that executes according to the currently bound ray tracing pipeline. A ray tracing pipeline must be bound to a command buffer before any ray tracing commands are recorded in that command buffer.
To dispatch ray tracing use:
// Provided by VK_NV_ray_tracing
void vkCmdTraceRaysNV(
VkCommandBuffer commandBuffer,
VkBuffer raygenShaderBindingTableBuffer,
VkDeviceSize raygenShaderBindingOffset,
VkBuffer missShaderBindingTableBuffer,
VkDeviceSize missShaderBindingOffset,
VkDeviceSize missShaderBindingStride,
VkBuffer hitShaderBindingTableBuffer,
VkDeviceSize hitShaderBindingOffset,
VkDeviceSize hitShaderBindingStride,
VkBuffer callableShaderBindingTableBuffer,
VkDeviceSize callableShaderBindingOffset,
VkDeviceSize callableShaderBindingStride,
uint32_t width,
uint32_t height,
uint32_t depth);
-
commandBuffer
is the command buffer into which the command will be recorded. -
raygenShaderBindingTableBuffer
is the buffer object that holds the shader binding table data for the ray generation shader stage. -
raygenShaderBindingOffset
is the offset in bytes (relative toraygenShaderBindingTableBuffer
) of the ray generation shader being used for the trace. -
missShaderBindingTableBuffer
is the buffer object that holds the shader binding table data for the miss shader stage. -
missShaderBindingOffset
is the offset in bytes (relative tomissShaderBindingTableBuffer
) of the miss shader being used for the trace. -
missShaderBindingStride
is the size in bytes of each shader binding table record inmissShaderBindingTableBuffer
. -
hitShaderBindingTableBuffer
is the buffer object that holds the shader binding table data for the hit shader stages. -
hitShaderBindingOffset
is the offset in bytes (relative tohitShaderBindingTableBuffer
) of the hit shader group being used for the trace. -
hitShaderBindingStride
is the size in bytes of each shader binding table record inhitShaderBindingTableBuffer
. -
callableShaderBindingTableBuffer
is the buffer object that holds the shader binding table data for the callable shader stage. -
callableShaderBindingOffset
is the offset in bytes (relative tocallableShaderBindingTableBuffer
) of the callable shader being used for the trace. -
callableShaderBindingStride
is the size in bytes of each shader binding table record incallableShaderBindingTableBuffer
. -
width
is the width of the ray trace query dimensions. -
height
is height of the ray trace query dimensions. -
depth
is depth of the ray trace query dimensions.
When the command is executed, a ray generation group of width
× height
× depth
rays is assembled.
To dispatch ray tracing use:
// Provided by VK_KHR_ray_tracing
void vkCmdTraceRaysKHR(
VkCommandBuffer commandBuffer,
const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
const VkStridedBufferRegionKHR* pMissShaderBindingTable,
const VkStridedBufferRegionKHR* pHitShaderBindingTable,
const VkStridedBufferRegionKHR* pCallableShaderBindingTable,
uint32_t width,
uint32_t height,
uint32_t depth);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pRaygenShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the ray generation shader stage. -
pMissShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the miss shader stage. -
pHitShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the hit shader stage. -
pCallableShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the callable shader stage. -
width
is the width of the ray trace query dimensions. -
height
is height of the ray trace query dimensions. -
depth
is depth of the ray trace query dimensions.
When the command is executed, a ray generation group of width
× height
× depth
rays is assembled.
The VkStridedBufferRegionKHR
structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkStridedBufferRegionKHR {
VkBuffer buffer;
VkDeviceSize offset;
VkDeviceSize stride;
VkDeviceSize size;
} VkStridedBufferRegionKHR;
-
buffer
is the buffer containing this region. -
offset
is the byte offset inbuffer
at which the region starts. -
stride
is the byte stride between consecutive elements. -
size
is the size in bytes of the region starting atoffset
.
To dispatch ray tracing, with some parameters sourced on the device, use:
// Provided by VK_KHR_ray_tracing
void vkCmdTraceRaysIndirectKHR(
VkCommandBuffer commandBuffer,
const VkStridedBufferRegionKHR* pRaygenShaderBindingTable,
const VkStridedBufferRegionKHR* pMissShaderBindingTable,
const VkStridedBufferRegionKHR* pHitShaderBindingTable,
const VkStridedBufferRegionKHR* pCallableShaderBindingTable,
VkBuffer buffer,
VkDeviceSize offset);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pRaygenShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the ray generation shader stage. -
pMissShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the miss shader stage. -
pHitShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the hit shader stage. -
pCallableShaderBindingTable
is a VkStridedBufferRegionKHR that holds the shader binding table data for the callable shader stage. -
buffer
is the buffer containing the trace ray parameters. -
offset
is the byte offset intobuffer
where parameters begin.
vkCmdTraceRaysIndirectKHR
behaves similarly to vkCmdTraceRaysKHR
except that the ray trace query dimensions are read by the device from
buffer
during execution.
The parameters of trace ray are encoded in the
VkTraceRaysIndirectCommandKHR structure located at offset
bytes
in buffer
.
The VkTraceRaysIndirectCommandKHR
structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkTraceRaysIndirectCommandKHR {
uint32_t width;
uint32_t height;
uint32_t depth;
} VkTraceRaysIndirectCommandKHR;
-
width
is the width of the ray trace query dimensions. -
height
is height of the ray trace query dimensions. -
depth
is depth of the ray trace query dimensions.
The members of VkTraceRaysIndirectCommandKHR
have the same meaning as
the similarly named parameters of vkCmdTraceRaysKHR.
36.3. Shader Binding Table
A shader binding table is a resource which establishes the relationship between the ray tracing pipeline and the acceleration structures that were built for the ray tracing pipeline. It indicates the shaders that operate on each geometry in an acceleration structure. In addition, it contains the resources accessed by each shader, including indices of textures, buffer device addresses, and constants. The application allocates and manages shader binding tables as VkBuffer objects.
Each entry in the shader binding table consists of
shaderGroupHandleSize
bytes of data as queried by
vkGetRayTracingShaderGroupHandlesKHR to refer to the shader that it
invokes.
The remainder of the data specified by the stride is application-visible
data that can be referenced by a ShaderRecordBufferKHR
block in the
shader.
The shader binding tables to use in a ray tracing pipeline are passed to the
vkCmdTraceRaysNV,
vkCmdTraceRaysKHR, or vkCmdTraceRaysIndirectKHR commands.
Shader binding tables are read-only in shaders that are executing on the ray
tracing pipeline.
Accesses to the shader binding table from ray tracing pipelines must be
synchronized with the
VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR
pipeline stage and an
access type of
VK_ACCESS_SHADER_READ_BIT
.
36.3.1. Indexing Rules
In order to execute the correct shaders and access the correct resources during a ray tracing dispatch, the implementation must be able to locate shader binding table entries at various stages of execution. This is accomplished by defining a set of indexing rules that compute shader binding table record positions relative to the buffer’s base address in memory. The application must organize the contents of the shader binding table’s memory in a way that application of the indexing rules will lead to correct records.
Ray Generation Shaders
Only one ray generation shader is executed per ray tracing dispatch.
Its location is passed into vkCmdTraceRaysKHR using the
pRaygenShaderBindingTable->buffer
and
pRaygenShaderBindingTable->offset
parameters — there is no indexing.
pRaygenShaderBindingTable->stride
is unused.
For vkCmdTraceRaysNV, the offset and stride come from direct
parameters, so the buffer and offset come from
raygenShaderBindingBuffer
and raygenShaderBindingOffset
All data accessed must be less than pRaygenShaderBindingTable->size
bytes from the base offset.
Hit Shaders
The base for the computation of intersection, any-hit and closest hit shader
locations is the instanceShaderBindingTableRecordOffset
value stored
with each instance of a top-level acceleration structure.
This value determines the beginning of the shader binding table records for
a given instance.
Each geometry in the instance must map to at least one hit program record.
In the following rule, geometryIndex refers to the location of the
geometry within the instance.
This index is available to ray shaders via the RayGeometryIndexKHR
built-in.
The sbtRecordStride
and sbtRecordOffset
values are passed in as
parameters to
traceNV
()
or
traceRayEXT
()
calls made in the shaders.
See Section 8.19 (Ray Tracing Functions) of the OpenGL Shading Language
Specification for more details.
In SPIR-V, these correspond to the SBTOffset
and SBTStride
parameters to the OpTraceRayKHR
instruction.
The result of this computation is then added to
pHitShaderBindingTable->offset
or hitShaderBindingOffset
, a base offset passed to vkCmdTraceRaysKHR
or vkCmdTraceRaysNV respectively
.
The complete rule to compute a hit shader binding table record address in
the pHitShaderBindingTable->buffer
is:
-
pHitShaderBindingTable->offset
+pHitShaderBindingTable->stride
× (instanceShaderBindingTableRecordOffset
+ geometryIndex ×sbtRecordStride
+sbtRecordOffset
)
All data accessed must be less than pHitShaderBindingTable->size
bytes from the base offset.
For vkCmdTraceRaysNV, the offset and stride come from direct parameters, so the full rule is equivalently:
-
hitShaderBindingOffset
+hitShaderBindingStride
× (instanceShaderBindingTableRecordOffset
+ geometryIndex ×sbtRecordStride
+sbtRecordOffset
)
Miss Shaders
A miss shader is executed whenever a ray query fails to find an intersection for the given scene geometry. Multiple miss shaders may be executed throughout a ray tracing dispatch.
The base for the computation of miss shader locations is
pMissShaderBindingTable->offset
, a base offset passed into
vkCmdTraceRaysKHR.
The missIndex
value is passed in as a parameter to
traceNV
()
or
traceRayEXT
()
calls made in the shaders.
See Section 8.19 (Ray Tracing Functions) of the OpenGL Shading Language
Specification for more details.
In SPIR-V, this corresponds to the MissIndex
parameter to the
OpTraceRayKHR
instruction.
The complete rule to compute a miss shader binding table record address in
the pMissShaderBindingTable->buffer
is:
-
pMissShaderBindingTable->offset
+pMissShaderBindingTable->stride
×missIndex
All data accessed must be less than pMissShaderBindingTable->size
bytes from the base offset.
For vkCmdTraceRaysNV, the offset and stride come from direct parameters, so the full rule is equivalently:
-
missShaderBindingOffset
+missShaderBindingStride
×missIndex
Callable Shaders
A callable shader is executed when requested by a ray tracing shader. Multiple callable shaders may be executed throughout a ray tracing dispatch.
The base for the computation of callable shader locations is
pCallableShaderBindingTable->offset
, a base offset passed into
vkCmdTraceRaysKHR.
The sbtRecordIndex
value is passed in as a parameter to
executeCallableNV
()
or
executeCallableEXT
()
calls made in the shaders.
See Section 8.19 (Ray Tracing Functions) of the OpenGL Shading Language
Specification for more details.
In SPIR-V, this corresponds to the SBTIndex
parameter to the
OpExecuteCallableKHR
instruction.
The complete rule to compute a callable shader binding table record address
in the pCallableShaderBindingTable->buffer
is:
-
pCallableShaderBindingTable->offset
+pCallableShaderBindingTable->stride
×sbtRecordIndex
All data accessed must be less than pCallableShaderBindingTable->size
bytes from the base offset.
For vkCmdTraceRaysNV, the offset and stride come from direct parameters, so the full rule is equivalently:
-
callableShaderBindingOffset
+callableShaderBindingStride
×sbtRecordIndex
36.4. Acceleration Structures
Acceleration structures are data structures used by the implementation to efficiently manage scene geometry as it is traversed during a ray tracing query. The application is responsible for managing acceleration structure objects (see Acceleration Structures), including allocation, destruction, executing builds or updates, and synchronizing resources used during ray tracing queries.
There are two types of acceleration structures, top level acceleration structures and bottom level acceleration structures.
36.4.2. Inactive Primitives and Instances
Acceleration structures allow the use of particular input values to signal inactive primitives or instances.
An inactive triangle is one for which the first (X) component of each vertex is NaN. If any other vertex component is NaN, and the first is not, the behavior is undefined. If the vertex format does not have a NaN representation, then all triangles are considered active.
An inactive instance is one whose acceleration structure handle is VK_NULL_HANDLE.
An inactive AABB is one for which the minimum X coordinate is NaN. If any other component is NaN, and the first is not, the behavior is undefined.
In the above definitions, "NaN" refers to any type of NaN. Signaling, non-signaling, quiet, loud, or otherwise.
An inactive object is considered invisible to all rays, and should not be represented in the acceleration structure. Implementations should ensure that the presence of inactive objects does not seriously degrade ray tracing performance.
Inactive objects are counted in the auto-generated index sequences which are
provided to shaders via InstanceId
and PrimitiveId
SPIR-V
decorations.
This allows objects in the scene to change freely between the active and
inactive states, without affecting the layout of any arrays which are being
indexed using the ID values.
Any transition between the active and inactive states requires a full acceleration structure rebuild. Applications must not perform an acceleration structure update where an object is active in the source acceleration structure but would be inactive in the destination, or vice versa.
36.4.3. Top Level Acceleration Structures
Opaque acceleration structure for an array of instances. The descriptor referencing this is the starting point for tracing
36.4.4. Bottom Level Acceleration Structures
Opaque acceleration structure for an array of geometries.
36.4.5. Building Acceleration Structures
To build an acceleration structure call:
// Provided by VK_NV_ray_tracing
void vkCmdBuildAccelerationStructureNV(
VkCommandBuffer commandBuffer,
const VkAccelerationStructureInfoNV* pInfo,
VkBuffer instanceData,
VkDeviceSize instanceOffset,
VkBool32 update,
VkAccelerationStructureKHR dst,
VkAccelerationStructureKHR src,
VkBuffer scratch,
VkDeviceSize scratchOffset);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
contains the shared information for the acceleration structure’s structure. -
instanceData
is the buffer containing an array of VkAccelerationStructureInstanceKHR structures defining acceleration structures. This parameter must beNULL
for bottom level acceleration structures. -
instanceOffset
is the offset in bytes (relative to the start ofinstanceData
) at which the instance data is located. -
update
specifies whether to update thedst
acceleration structure with the data insrc
. -
dst
is a pointer to the target acceleration structure for the build. -
src
is a pointer to an existing acceleration structure that is to be used to update thedst
acceleration structure. -
scratch
is the VkBuffer that will be used as scratch memory for the build. -
scratchOffset
is the offset in bytes relative to the start ofscratch
that will be used as a scratch memory.
Accesses to scratch
must be synchronized with the
VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR
pipeline stage and an
access type of
VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR
or
VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR
.
To build acceleration structures call:
// Provided by VK_KHR_ray_tracing
void vkCmdBuildAccelerationStructureKHR(
VkCommandBuffer commandBuffer,
uint32_t infoCount,
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
const VkAccelerationStructureBuildOffsetInfoKHR* const* ppOffsetInfos);
-
commandBuffer
is the command buffer into which the command will be recorded. -
infoCount
is the number of acceleration structures to build. It specifies the number of thepInfos
structures andppOffsetInfos
pointers that must be provided. -
pInfos
is an array ofinfoCount
VkAccelerationStructureBuildGeometryInfoKHR structures defining the geometry used to build each acceleration structure. -
ppOffsetInfos
is an array ofinfoCount
pointers to arrays of VkAccelerationStructureBuildOffsetInfoKHR structures. EachppOffsetInfos
[i] is an array ofpInfos
[i].geometryCount
VkAccelerationStructureBuildOffsetInfoKHR structures defining dynamic offsets to the addresses where geometry data is stored, as defined bypInfos
[i].
The vkCmdBuildAccelerationStructureKHR
command provides the ability to
initiate multiple acceleration structures builds, however there is no
ordering or synchronization implied between any of the individual
acceleration structure builds.
Note
This means that an application cannot build a top-level acceleration structure in the same vkCmdBuildAccelerationStructureKHR call as the associated bottom-level or instance acceleration structures are being built. There also cannot be any memory aliasing between any acceleration structure memories or scratch memories being used by any of the builds. |
Accesses to the acceleration structure scratch buffers as identified by the
VkAccelerationStructureBuildGeometryInfoKHR→scratchData
buffer
device addresses must be synchronized with
the VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR
pipeline stage and an
access type of
VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR
or
VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR
.
To build an acceleration structure with some parameters sourced on the device call:
// Provided by VK_KHR_ray_tracing
void vkCmdBuildAccelerationStructureIndirectKHR(
VkCommandBuffer commandBuffer,
const VkAccelerationStructureBuildGeometryInfoKHR* pInfo,
VkBuffer indirectBuffer,
VkDeviceSize indirectOffset,
uint32_t indirectStride);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is a pointer to a VkAccelerationStructureBuildGeometryInfoKHR structure defining the geometry used to build the acceleration structure. -
indirectBuffer
is the VkBuffer containingpInfo->geometryCount
VkAccelerationStructureBuildOffsetInfoKHR structures defining dynamic offsets to the addresses where geometry data is stored, as defined bypInfo
. -
indirectOffset
is the byte offset intoindirectBuffer
where offset parameters begin. -
stride
is the byte stride between successive sets of offset parameters.
The VkAccelerationStructureBuildGeometryInfoKHR
structure is defined
as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureBuildGeometryInfoKHR {
VkStructureType sType;
const void* pNext;
VkAccelerationStructureTypeKHR type;
VkBuildAccelerationStructureFlagsKHR flags;
VkBool32 update;
VkAccelerationStructureKHR srcAccelerationStructure;
VkAccelerationStructureKHR dstAccelerationStructure;
VkBool32 geometryArrayOfPointers;
uint32_t geometryCount;
const VkAccelerationStructureGeometryKHR* const* ppGeometries;
VkDeviceOrHostAddressKHR scratchData;
} VkAccelerationStructureBuildGeometryInfoKHR;
-
sType
is the type of this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
type
is a VkAccelerationStructureTypeKHR value specifying the type of acceleration structure being built. -
flags
is a bitmask of VkBuildAccelerationStructureFlagBitsKHR specifying additional parameters of the acceleration structure. -
update
specifies whether to updatedstAccelerationStructure
with the data insrcAccelerationStructure
or not. -
srcAccelerationStructure
points to an existing acceleration structure that is to be used to update thedst
acceleration structure whenupdate
isVK_TRUE
. -
dstAccelerationStructure
points to the target acceleration structure for the build. -
geometryArrayOfPointers
specifies whetherppGeometries
is used as a pointer to an array of pointers or a pointer to a pointer to an array. -
geometryCount
specifies the number of geometries that will be built intodstAccelerationStructure
. -
ppGeometries
is either a pointer to an array of pointers to VkAccelerationStructureGeometryKHR structures ifgeometryArrayOfPointers
isVK_TRUE
, or a pointer to a pointer to an array of VkAccelerationStructureGeometryKHR structures if it isVK_FALSE
. Each element of the array describes the data used to build each acceleration structure geometry. -
scratchData
is the device or host address to memory that will be used as scratch memory for the build.
Note
Elements of
|
The VkDeviceOrHostAddressKHR
union is defined as:
// Provided by VK_KHR_ray_tracing
typedef union VkDeviceOrHostAddressKHR {
VkDeviceAddress deviceAddress;
void* hostAddress;
} VkDeviceOrHostAddressKHR;
-
deviceAddress
is a buffer device address as returned by the vkGetBufferDeviceAddressKHR command. -
hostAddress
is a host memory address.
The VkDeviceOrHostAddressConstKHR
union is defined as:
// Provided by VK_KHR_ray_tracing
typedef union VkDeviceOrHostAddressConstKHR {
VkDeviceAddress deviceAddress;
const void* hostAddress;
} VkDeviceOrHostAddressConstKHR;
-
deviceAddress
is a buffer device address as returned by the vkGetBufferDeviceAddressKHR command. -
hostAddress
is a const host memory address.
The VkAccelerationStructureGeometryKHR
structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureGeometryKHR {
VkStructureType sType;
const void* pNext;
VkGeometryTypeKHR geometryType;
VkAccelerationStructureGeometryDataKHR geometry;
VkGeometryFlagsKHR flags;
} VkAccelerationStructureGeometryKHR;
-
sType
is the type of this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
geometryType
describes which type of geometry thisVkAccelerationStructureGeometryKHR
refers to. -
geometry
is a VkAccelerationStructureGeometryDataKHR union describing the geometry data for the relevant geometry type. -
flags
is a bitmask of VkGeometryFlagBitsKHR values describing additional properties of how the geometry should be built.
The VkAccelerationStructureGeometryDataKHR
union is defined as:
// Provided by VK_KHR_ray_tracing
typedef union VkAccelerationStructureGeometryDataKHR {
VkAccelerationStructureGeometryTrianglesDataKHR triangles;
VkAccelerationStructureGeometryAabbsDataKHR aabbs;
VkAccelerationStructureGeometryInstancesDataKHR instances;
} VkAccelerationStructureGeometryDataKHR;
-
triangles
is a VkAccelerationStructureGeometryTrianglesDataKHR structure. -
aabbs
is a VkAccelerationStructureGeometryAabbsDataKHR struture. -
instances
is a VkAccelerationStructureGeometryInstancesDataKHR structure.
The VkAccelerationStructureGeometryTrianglesDataKHR
structure is
defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureGeometryTrianglesDataKHR {
VkStructureType sType;
const void* pNext;
VkFormat vertexFormat;
VkDeviceOrHostAddressConstKHR vertexData;
VkDeviceSize vertexStride;
VkIndexType indexType;
VkDeviceOrHostAddressConstKHR indexData;
VkDeviceOrHostAddressConstKHR transformData;
} VkAccelerationStructureGeometryTrianglesDataKHR;
-
sType
is the type of this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
vertexFormat
is the VkFormat of each vertex element. -
vertexData
is a device or host address to memory containing vertex data for this geometry. -
vertexStride
is the stride in bytes between each vertex. -
indexType
is the VkIndexType of each index element. -
indexData
is a device or host address to memory containing index data for this geometry. -
transformData
is a device or host address to memory containing an optional reference to a VkTransformMatrixKHR structure defining a transformation that should be applied to vertices in this geometry.
The VkTransformMatrixKHR
structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkTransformMatrixKHR {
float matrix[3][4];
} VkTransformMatrixKHR;
or the equivalent
// Provided by VK_NV_ray_tracing
typedef VkTransformMatrixKHR VkTransformMatrixNV;
-
matrix
is a 3x4 row-major affine transformation matrix.
The VkAccelerationStructureGeometryAabbsDataKHR
structure is defined
as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureGeometryAabbsDataKHR {
VkStructureType sType;
const void* pNext;
VkDeviceOrHostAddressConstKHR data;
VkDeviceSize stride;
} VkAccelerationStructureGeometryAabbsDataKHR;
-
sType
is the type of this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
data
is a device or host address to memory containing VkAabbPositionsKHR structures containing position data for each axis-aligned bounding box in the geometry. -
stride
is the stride in bytes between each entry indata
.
The VkAabbPositionsKHR structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAabbPositionsKHR {
float minX;
float minY;
float minZ;
float maxX;
float maxY;
float maxZ;
} VkAabbPositionsKHR;
or the equivalent
// Provided by VK_NV_ray_tracing
typedef VkAabbPositionsKHR VkAabbPositionsNV;
-
minX
is the x position of one opposing corner of a bounding box. -
minY
is the y position of one opposing corner of a bounding box. -
minZ
is the z position of one opposing corner of a bounding box. -
maxX
is the x position of the other opposing corner of a bounding box. -
maxY
is the y position of the other opposing corner of a bounding box. -
maxZ
is the z position of the other opposing corner of a bounding box.
The VkAccelerationStructureGeometryInstancesDataKHR
structure is
defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureGeometryInstancesDataKHR {
VkStructureType sType;
const void* pNext;
VkBool32 arrayOfPointers;
VkDeviceOrHostAddressConstKHR data;
} VkAccelerationStructureGeometryInstancesDataKHR;
-
sType
is the type of this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
arrayOfPointers
specifies whetherdata
is used as an array of addresses or just an array. -
data
is either the address of an array of device or host addresses referencing individual VkAccelerationStructureInstanceKHR structures ifarrayOfPointers
isVK_TRUE
, or the address of an array of VkAccelerationStructureInstanceKHR structures.
Acceleration structure instances can be built into top-level acceleration structures. Each acceleration structure instance is a separate entry in the top-level acceleration structure which includes all the geometry of a bottom-level acceleration structure at a transformed location. Multiple instances can point to the same bottom level acceleration structure.
An acceleration structure instance is defined by the structure:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureInstanceKHR {
VkTransformMatrixKHR transform;
uint32_t instanceCustomIndex:24;
uint32_t mask:8;
uint32_t instanceShaderBindingTableRecordOffset:24;
VkGeometryInstanceFlagsKHR flags:8;
uint64_t accelerationStructureReference;
} VkAccelerationStructureInstanceKHR;
or the equivalent
// Provided by VK_NV_ray_tracing
typedef VkAccelerationStructureInstanceKHR VkAccelerationStructureInstanceNV;
-
transform
is a VkTransformMatrixKHR structure describing a transformation to be applied to the acceleration structure. -
instanceCustomIndex
is a 24-bit user-specified index value accessible to ray shaders in theInstanceCustomIndexKHR
built-in. -
mask
is an 8-bit visibility mask for the geometry. The instance may only be hit ifrayMask & instance.mask != 0
-
instanceShaderBindingTableRecordOffset
is a 24-bit offset used in calculating the hit shader binding table index. -
flags
is an 8-bit mask of VkGeometryInstanceFlagBitsKHR values to apply to this instance. -
accelerationStructureReference
is either:-
a device address containing the value obtained from vkGetAccelerationStructureDeviceAddressKHR or vkGetAccelerationStructureHandleNV (used by device operations which reference acceleration structures) or,
-
a VkAccelerationStructureKHR object (used by host operations which reference acceleration structures).
-
The C language spec does not define the ordering of bit-fields, but in practice, this struct produces the correct layout with existing compilers. The intended bit pattern is for the following:
-
instanceCustomIndex
andmask
occupy the same memory as if a singleint32_t
was specified in their place-
instanceCustomIndex
occupies the 24 least significant bits of that memory -
mask
occupies the 8 most significant bits of that memory
-
-
instanceShaderBindingTableRecordOffset
andflags
occupy the same memory as if a singleint32_t
was specified in their place-
instanceShaderBindingTableRecordOffset
occupies the 24 least significant bits of that memory -
flags
occupies the 8 most significant bits of that memory
-
If a compiler produces code that diverges from that pattern, applications must employ another method to set values according to the correct bit pattern.
Possible values of flags
in the instance modifying the behavior of
that instance are:
// Provided by VK_KHR_ray_tracing
typedef enum VkGeometryInstanceFlagBitsKHR {
VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR = 0x00000001,
VK_GEOMETRY_INSTANCE_TRIANGLE_FRONT_COUNTERCLOCKWISE_BIT_KHR = 0x00000002,
VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR = 0x00000004,
VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR = 0x00000008,
// Provided by VK_NV_ray_tracing
VK_GEOMETRY_INSTANCE_TRIANGLE_CULL_DISABLE_BIT_NV = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR,
// Provided by VK_NV_ray_tracing
VK_GEOMETRY_INSTANCE_TRIANGLE_FRONT_COUNTERCLOCKWISE_BIT_NV = VK_GEOMETRY_INSTANCE_TRIANGLE_FRONT_COUNTERCLOCKWISE_BIT_KHR,
// Provided by VK_NV_ray_tracing
VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_NV = VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR,
// Provided by VK_NV_ray_tracing
VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_NV = VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR,
} VkGeometryInstanceFlagBitsKHR;
or the equivalent
// Provided by VK_NV_ray_tracing
typedef VkGeometryInstanceFlagBitsKHR VkGeometryInstanceFlagBitsNV;
-
VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR
disables face culling for this instance. -
VK_GEOMETRY_INSTANCE_TRIANGLE_FRONT_COUNTERCLOCKWISE_BIT_KHR
indicates that the front face of the triangle for culling purposes is the face that is counter clockwise in object space relative to the ray origin. Because the facing is determined in object space, an instance transform matrix does not change the winding, but a geometry transform does. -
VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR
causes this instance to act as thoughVK_GEOMETRY_OPAQUE_BIT_KHR
were specified on all geometries referenced by this instance. This behavior can be overridden by the SPIR-VNoOpaqueKHR
ray flag. -
VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR
causes this instance to act as thoughVK_GEOMETRY_OPAQUE_BIT_KHR
were not specified on all geometries referenced by this instance. This behavior can be overridden by the SPIR-VOpaqueKHR
ray flag.
VK_GEOMETRY_INSTANCE_FORCE_NO_OPAQUE_BIT_KHR
and
VK_GEOMETRY_INSTANCE_FORCE_OPAQUE_BIT_KHR
must not be used in the
same flag.
// Provided by VK_KHR_ray_tracing
typedef VkFlags VkGeometryInstanceFlagsKHR;
or the equivalent
// Provided by VK_NV_ray_tracing
typedef VkGeometryInstanceFlagsKHR VkGeometryInstanceFlagsNV;
VkGeometryInstanceFlagsKHR
is a bitmask type for setting a mask of
zero or more VkGeometryInstanceFlagBitsKHR.
VkAccelerationStructureBuildOffsetInfoKHR
is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureBuildOffsetInfoKHR {
uint32_t primitiveCount;
uint32_t primitiveOffset;
uint32_t firstVertex;
uint32_t transformOffset;
} VkAccelerationStructureBuildOffsetInfoKHR;
-
primitiveCount
defines the number of primitives for a corresponding acceleration structure geometry. -
primitiveOffset
defines an offset in bytes into the memory where primitive data is defined. -
firstVertex
is the index of the first vertex to build from for triangle geometry. -
transformOffset
defines an offset in bytes into the memory where a transform matrix is defined.
The primitive count and primitive offset are interpreted differently depending on the VkGeometryTypeKHR used:
-
For geometries of type
VK_GEOMETRY_TYPE_TRIANGLES_KHR
,primitiveCount
is the number of triangles to be built, where each triangle is treated as 3 vertices.-
If the geometry uses indices,
primitiveCount
× 3 indices are consumed from VkAccelerationStructureGeometryTrianglesDataKHR::indexData
, starting at an offset ofprimitiveOffset
. The value offirstVertex
is added to the index values before fetching vertices. -
If the geometry does not use indices,
primitiveCount
× 3 vertices are consumed from VkAccelerationStructureGeometryTrianglesDataKHR::vertexData
, starting at an offset ofprimitiveOffset
+ VkAccelerationStructureGeometryTrianglesDataKHR::vertexStride
×firstVertex
. -
A single VkTransformMatrixKHR structure is consumed from VkAccelerationStructureGeometryTrianglesDataKHR::
transformData
, at an offset oftransformOffset
. This transformation matrix is used by all triangles.
-
-
For geometries of type
VK_GEOMETRY_TYPE_AABBS_KHR
,primitiveCount
is the number of axis-aligned bounding boxes.primitiveCount
VkAabbPositionsKHR structures are consumed from VkAccelerationStructureGeometryAabbsDataKHR::data
, starting at an offset ofprimitiveOffset
. -
For geometries of type
VK_GEOMETRY_TYPE_INSTANCES_KHR
,primitiveCount
is the number of acceleration structures.primitiveCount
VkAccelerationStructureInstanceKHR structures are consumed from VkAccelerationStructureGeometryInstancesDataKHR::data
, starting at an offset ofprimitiveOffset
.
36.4.6. Copying Acceleration Structures
An additional command exists for copying acceleration structures without updating their contents. The acceleration structure object can be compacted in order to improve performance. Before copying, an application must query the size of the resulting acceleration structure.
To query acceleration structure size parameters call:
// Provided by VK_KHR_ray_tracing
void vkCmdWriteAccelerationStructuresPropertiesKHR(
VkCommandBuffer commandBuffer,
uint32_t accelerationStructureCount,
const VkAccelerationStructureKHR* pAccelerationStructures,
VkQueryType queryType,
VkQueryPool queryPool,
uint32_t firstQuery);
or the equivalent command
// Provided by VK_NV_ray_tracing
void vkCmdWriteAccelerationStructuresPropertiesNV(
VkCommandBuffer commandBuffer,
uint32_t accelerationStructureCount,
const VkAccelerationStructureKHR* pAccelerationStructures,
VkQueryType queryType,
VkQueryPool queryPool,
uint32_t firstQuery);
-
commandBuffer
is the command buffer into which the command will be recorded. -
accelerationStructureCount
is the count of acceleration structures for which to query the property. -
pAccelerationStructures
is a pointer to an array of existing previously built acceleration structures. -
queryType
is a VkQueryType value specifying the type of queries managed by the pool. -
queryPool
is the query pool that will manage the results of the query. -
firstQuery
is the first query index within the query pool that will contain theaccelerationStructureCount
number of results.
To copy an acceleration structure call:
// Provided by VK_NV_ray_tracing
void vkCmdCopyAccelerationStructureNV(
VkCommandBuffer commandBuffer,
VkAccelerationStructureKHR dst,
VkAccelerationStructureKHR src,
VkCopyAccelerationStructureModeKHR mode);
-
commandBuffer
is the command buffer into which the command will be recorded. -
dst
is a pointer to the target acceleration structure for the copy. -
src
is a pointer to the source acceleration structure for the copy. -
mode
is a VkCopyAccelerationStructureModeKHR value specifying additional operations to perform during the copy.
To copy an acceleration structure call:
// Provided by VK_KHR_ray_tracing
void vkCmdCopyAccelerationStructureKHR(
VkCommandBuffer commandBuffer,
const VkCopyAccelerationStructureInfoKHR* pInfo);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is a pointer to a VkCopyAccelerationStructureInfoKHR structure defining the copy operation.
The VkCopyAccelerationStructureInfoKHR
structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkCopyAccelerationStructureInfoKHR {
VkStructureType sType;
const void* pNext;
VkAccelerationStructureKHR src;
VkAccelerationStructureKHR dst;
VkCopyAccelerationStructureModeKHR mode;
} VkCopyAccelerationStructureInfoKHR;
-
src
is the source acceleration structure for the copy. -
dst
is the target acceleration structure for the copy. -
mode
is a VkCopyAccelerationStructureModeKHR value that specifies additional operations to perform during the copy.
Possible values of mode
specifying additional operations to perform
during the copy, are:
// Provided by VK_KHR_ray_tracing
typedef enum VkCopyAccelerationStructureModeKHR {
VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_KHR = 0,
VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_KHR = 1,
VK_COPY_ACCELERATION_STRUCTURE_MODE_SERIALIZE_KHR = 2,
VK_COPY_ACCELERATION_STRUCTURE_MODE_DESERIALIZE_KHR = 3,
// Provided by VK_NV_ray_tracing
VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_NV = VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_KHR,
// Provided by VK_NV_ray_tracing
VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_NV = VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_KHR,
} VkCopyAccelerationStructureModeKHR;
or the equivalent
// Provided by VK_NV_ray_tracing
typedef VkCopyAccelerationStructureModeKHR VkCopyAccelerationStructureModeNV;
-
VK_COPY_ACCELERATION_STRUCTURE_MODE_CLONE_KHR
creates a direct copy of the acceleration structure specified insrc
into the one specified bydst
. Thedst
acceleration structure must have been created with the same parameters assrc
. -
VK_COPY_ACCELERATION_STRUCTURE_MODE_COMPACT_KHR
creates a more compact version of an acceleration structuresrc
intodst
. The acceleration structuredst
must have been created with acompactedSize
corresponding to the one returned by vkCmdWriteAccelerationStructuresPropertiesKHR after the build of the acceleration structure specified bysrc
. -
VK_COPY_ACCELERATION_STRUCTURE_MODE_SERIALIZE_KHR
serializes the acceleration structure to a semi-opaque format which can be reloaded on a compatible implementation. -
VK_COPY_ACCELERATION_STRUCTURE_MODE_DESERIALIZE_KHR
deserializes the semi-opaque serialization format in the buffer to the acceleration structure.
To copy an acceleration structure to device memory call:
// Provided by VK_KHR_ray_tracing
void vkCmdCopyAccelerationStructureToMemoryKHR(
VkCommandBuffer commandBuffer,
const VkCopyAccelerationStructureToMemoryInfoKHR* pInfo);
This command produces the same results as vkCopyAccelerationStructureToMemoryKHR, but writes its result to a device address, and is executed on the device rather than the host. The output may not necessarily be bit-for-bit identical, but it can be equally used by either vkCmdCopyMemoryToAccelerationStructureKHR or vkCopyMemoryToAccelerationStructureKHR.
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is an a pointer to a VkCopyAccelerationStructureToMemoryInfoKHR structure defining the copy operation.
The defined header structure for the serialized data consists of:
-
VK_UUID_SIZE
bytes of data matchingVkPhysicalDeviceIDProperties
::driverUUID
-
VK_UUID_SIZE
bytes of data identifying the compatibility for comparison using vkGetDeviceAccelerationStructureCompatibilityKHR -
A 64-bit integer of the total size matching the value queried using
VK_QUERY_TYPE_ACCELERATION_STRUCTURE_SERIALIZATION_SIZE_KHR
-
A 64-bit integer of the deserialized size to be passed in to
VkAccelerationStructureCreateInfoKHR
::compactedSize
-
A 64-bit integer of the count of the number of acceleration structure handles following. This will be zero for a bottom-level acceleration structure.
The corresponding handles matching the values returned by vkGetAccelerationStructureDeviceAddressKHR or vkGetAccelerationStructureHandleNV are tightly packed in the buffer following the count. The application is expected to store a mapping between those handles and the original application-generated bottom-level acceleration structures to provide when deserializing.
// Provided by VK_KHR_ray_tracing
typedef struct VkCopyAccelerationStructureToMemoryInfoKHR {
VkStructureType sType;
const void* pNext;
VkAccelerationStructureKHR src;
VkDeviceOrHostAddressKHR dst;
VkCopyAccelerationStructureModeKHR mode;
} VkCopyAccelerationStructureToMemoryInfoKHR;
-
src
is the source acceleration structure for the copy -
dst
is the device or host address to memory which is the target for the copy -
mode
is a VkCopyAccelerationStructureModeKHR value that specifies additional operations to perform during the copy.
To copy device memory to an acceleration structure call:
// Provided by VK_KHR_ray_tracing
void vkCmdCopyMemoryToAccelerationStructureKHR(
VkCommandBuffer commandBuffer,
const VkCopyMemoryToAccelerationStructureInfoKHR* pInfo);
This command can accept acceleration structures produced by either vkCmdCopyAccelerationStructureToMemoryKHR or vkCopyAccelerationStructureToMemoryKHR.
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is a pointer to a VkCopyMemoryToAccelerationStructureInfoKHR structure defining the copy operation.
The structure provided as input to deserialize is as described in vkCmdCopyAccelerationStructureToMemoryKHR, with any acceleration structure handles filled in with the newly-queried handles to bottom level acceleration structures created before deserialization. These do not need to be built at deserialize time, but must be created.
The VkCopyMemoryToAccelerationStructureInfoKHR
structure is defined
as:
// Provided by VK_KHR_ray_tracing
typedef struct VkCopyMemoryToAccelerationStructureInfoKHR {
VkStructureType sType;
const void* pNext;
VkDeviceOrHostAddressConstKHR src;
VkAccelerationStructureKHR dst;
VkCopyAccelerationStructureModeKHR mode;
} VkCopyMemoryToAccelerationStructureInfoKHR;
-
src
is the device or host address to memory containing the source data for the copy. -
dst
is the target acceleration structure for the copy. -
mode
is a VkCopyAccelerationStructureModeKHR value that specifies additional operations to perform during the copy.
To check if a serialized acceleration structure is compatible with the current device call:
// Provided by VK_KHR_ray_tracing
VkResult vkGetDeviceAccelerationStructureCompatibilityKHR(
VkDevice device,
const VkAccelerationStructureVersionKHR* version);
-
device
is the device to check the version against. -
version
points to the VkAccelerationStructureVersionKHR version information to check against the device.
This possible return values for
vkGetDeviceAccelerationStructureCompatibilityKHR
are:
-
VK_SUCCESS
is returned if an acceleration structure serialized withversion
as the version information is compatible withdevice
. -
VK_ERROR_INCOMPATIBLE_VERSION_KHR
is returned if an acceleration structure serialized withversion
as the version information is not compatible withdevice
.
The VkAccelerationStructureVersionKHR
structure is defined as:
// Provided by VK_KHR_ray_tracing
typedef struct VkAccelerationStructureVersionKHR {
VkStructureType sType;
const void* pNext;
const uint8_t* versionData;
} VkAccelerationStructureVersionKHR;
-
versionData
is a pointer to the version header as defined in VkCopyAccelerationStructureModeKHR
36.5. Host Acceleration Structure Operations
Implementations are also required to provide host implementations of the
acceleration structure operations if the
rayTracingHostAccelerationStructureCommands
feature is enabled:
-
vkBuildAccelerationStructureKHR corresponding to vkCmdBuildAccelerationStructureKHR
-
vkCopyAccelerationStructureKHR corresponding to vkCmdCopyAccelerationStructureKHR
-
vkCopyAccelerationStructureToMemoryKHR corresponding to vkCmdCopyAccelerationStructureToMemoryKHR
-
vkCopyMemoryToAccelerationStructureKHR corresponding to vkCmdCopyMemoryToAccelerationStructureKHR
-
vkWriteAccelerationStructuresPropertiesKHR corresponding to vkCmdWriteAccelerationStructuresPropertiesKHR
These commands are functionally equivalent to their device counterparts, except that they are executed on the host timeline, rather than being enqueued into command buffers.
All acceleration structures used by the host commands must be bound to host-visible memory, and all input data for acceleration structure builds must be referenced using host addresses instead of device addresses. Applications are not required to map acceleration structure memory when using the host commands.
Note
The vkBuildAccelerationStructureKHR and vkCmdBuildAccelerationStructureKHR may use different algorithms, and thus are not required to produce identical structures. The structures produced by these two commands may exhibit different memory footprints or traversal performance, but should strive to be similar where possible. Apart from these details, the host and device operations are interchangable. For example, an application can use vkBuildAccelerationStructureKHR to build a structure, compact it on the device using vkCmdCopyAccelerationStructureKHR, and serialize the result using vkCopyAccelerationStructureToMemoryKHR. |
To build acceleration structures on the host, call:
// Provided by VK_KHR_ray_tracing
VkResult vkBuildAccelerationStructureKHR(
VkDevice device,
uint32_t infoCount,
const VkAccelerationStructureBuildGeometryInfoKHR* pInfos,
const VkAccelerationStructureBuildOffsetInfoKHR* const* ppOffsetInfos);
This command fulfills the same task as vkCmdBuildAccelerationStructureKHR but executed by the host.
-
device
is theVkDevice
for which the acceleration structures are being built. -
infoCount
is the number of acceleration structures to build. It specifies the number of thepInfos
structures andppOffsetInfos
pointers that must be provided. -
pInfos
is a pointer to an array ofinfoCount
VkAccelerationStructureBuildGeometryInfoKHR structures defining the geometry used to build each acceleration structure. -
ppOffsetInfos
is an array ofinfoCount
pointers to arrays of VkAccelerationStructureBuildOffsetInfoKHR structures. EachppOffsetInfos
[i] is an array ofpInfos
[i].geometryCount
VkAccelerationStructureBuildOffsetInfoKHR structures defining dynamic offsets to the addresses where geometry data is stored, as defined bypInfos
[i].
The vkBuildAccelerationStructureKHR
command provides the ability to
initiate multiple acceleration structures builds, however there is no
ordering or synchronization implied between any of the individual
acceleration structure builds.
Note
This means that an application cannot build a top-level acceleration structure in the same vkBuildAccelerationStructureKHR call as the associated bottom-level or instance acceleration structures are being built. There also cannot be any memory aliasing between any acceleration structure memories or scratch memories being used by any of the builds. |
If the VkDeferredOperationInfoKHR structure is included in the
pNext
chain of any VkAccelerationStructureBuildGeometryInfoKHR
structure, the operation of this command is deferred, as defined in the
Deferred Host Operations chapter.
To copy or compact an acceleration structure on the host, call:
// Provided by VK_KHR_ray_tracing
VkResult vkCopyAccelerationStructureKHR(
VkDevice device,
const VkCopyAccelerationStructureInfoKHR* pInfo);
This command fulfills the same task as vkCmdCopyAccelerationStructureKHR but executed by the host.
-
device
is the device which owns the acceleration structures. -
pInfo
is a pointer to a VkCopyAccelerationStructureInfoKHR structure defining the copy operation.
If the VkDeferredOperationInfoKHR structure is included in the
pNext
chain of the VkCopyAccelerationStructureInfoKHR structure,
the operation of this command is deferred, as defined in the
Deferred Host Operations chapter.
// Provided by VK_KHR_ray_tracing
VkResult vkCopyMemoryToAccelerationStructureKHR(
VkDevice device,
const VkCopyMemoryToAccelerationStructureInfoKHR* pInfo);
This command fulfills the same task as vkCmdCopyMemoryToAccelerationStructureKHR but is executed by the host.
This command can accept acceleration structures produced by either vkCmdCopyAccelerationStructureToMemoryKHR or vkCopyAccelerationStructureToMemoryKHR.
-
device
is the device which ownspInfo->dst
. -
pInfo
is a pointer to a VkCopyMemoryToAccelerationStructureInfoKHR structure defining the copy operation.
If the VkDeferredOperationInfoKHR structure is included in the
pNext
chain of the VkCopyMemoryToAccelerationStructureInfoKHR
structure, the operation of this command is deferred, as defined in the
Deferred Host Operations chapter.
// Provided by VK_KHR_ray_tracing
VkResult vkCopyAccelerationStructureToMemoryKHR(
VkDevice device,
const VkCopyAccelerationStructureToMemoryInfoKHR* pInfo);
This command fulfills the same task as vkCmdCopyAccelerationStructureToMemoryKHR but executed by the host.
This command produces the same results as vkCmdCopyAccelerationStructureToMemoryKHR, but writes its result directly to a host pointer, and is executed on the host rather than the device. The output may not necessarily be bit-for-bit identical, but it can be equally used by either vkCmdCopyMemoryToAccelerationStructureKHR or vkCopyMemoryToAccelerationStructureKHR.
-
device
is the device which ownspInfo->src
. -
pInfo
is a pointer to a VkCopyAccelerationStructureToMemoryInfoKHR structure defining the copy operation.
If the VkDeferredOperationInfoKHR structure is included in the
pNext
chain of the VkCopyAccelerationStructureToMemoryInfoKHR
structure, the operation of this command is deferred, as defined in the
Deferred Host Operations chapter.
// Provided by VK_KHR_ray_tracing
VkResult vkWriteAccelerationStructuresPropertiesKHR(
VkDevice device,
uint32_t accelerationStructureCount,
const VkAccelerationStructureKHR* pAccelerationStructures,
VkQueryType queryType,
size_t dataSize,
void* pData,
size_t stride);
This command fulfills the same task as vkCmdWriteAccelerationStructuresPropertiesKHR but executed by the host.
-
device
is the device which owns the acceleration structures inpAccelerationStructures
. -
accelerationStructureCount
is the count of acceleration structures for which to query the property. -
pAccelerationStructures
points to an array of existing previously built acceleration structures. -
queryType
is a VkQueryType value specifying the property to be queried. -
dataSize
is the size in bytes of the buffer pointed to bypData
. -
pData
is a pointer to a user-allocated buffer where the results will be written. -
stride
is the stride in bytes between results for individual queries withinpData
.