38. Micromap
38.1. Micromaps
Acceleration structures store and organize geometry for ray tracing, but in some cases it is beneficial to include some information within the geometry, particularly for triangles. A micromap organizes this data around a map of values corresponding to subdivided microtriangles which can be added to a triangle geometry when building a bottom level acceleration structure.
An opacity micromap is a type of micromap which stores information to control intersection opacity as described in Ray Opacity Micromap.
A micromap is considered to be constructed if a micromap build command or copy command has been executed with the given acceleration structure as the destination.
38.1.1. Building Micromaps
To build micromaps call:
// Provided by VK_EXT_opacity_micromap
void vkCmdBuildMicromapsEXT(
VkCommandBuffer commandBuffer,
uint32_t infoCount,
const VkMicromapBuildInfoEXT* pInfos);
-
commandBuffer
is the command buffer into which the command will be recorded. -
infoCount
is the number of micromaps to build. It specifies the number of thepInfos
structures that must be provided. -
pInfos
is a pointer to an array ofinfoCount
VkMicromapBuildInfoEXT structures defining the data used to build each micromap.
The vkCmdBuildMicromapsEXT
command provides the ability to initiate
multiple micromaps builds, however there is no ordering or synchronization
implied between any of the individual micromap builds.
Note
This means that there cannot be any memory aliasing between any micromap memories or scratch memories being used by any of the builds. |
Accesses to the micromap scratch buffers as identified by the
VkMicromapBuildInfoEXT::scratchData
buffer device addresses
must be synchronized with the
VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
(VK_ACCESS_2_MICROMAP_READ_BIT_EXT
|
VK_ACCESS_2_MICROMAP_WRITE_BIT_EXT
).
Accesses to VkMicromapBuildInfoEXT::dstMicromap
must be
synchronized with the
VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
VK_ACCESS_2_MICROMAP_WRITE_BIT_EXT
.
Accesses to other input buffers as identified by any used values of
VkMicromapBuildInfoEXT::data
or
VkMicromapBuildInfoEXT::triangleArray
must be
synchronized with the
VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
VK_ACCESS_SHADER_READ_BIT
.
Formats which can be set in VkMicromapUsageEXT::format
and
VkMicromapTriangleEXT::format
for micromap builds, are:
// Provided by VK_EXT_opacity_micromap
typedef enum VkOpacityMicromapFormatEXT {
VK_OPACITY_MICROMAP_FORMAT_2_STATE_EXT = 1,
VK_OPACITY_MICROMAP_FORMAT_4_STATE_EXT = 2,
} VkOpacityMicromapFormatEXT;
-
VK_OPACITY_MICROMAP_FORMAT_2_STATE_EXT
indicates that the given micromap format has one bit per subtriangle encoding either fully opaque or fully transparent. -
VK_OPACITY_MICROMAP_FORMAT_4_STATE_EXT
indicates that the given micromap format has two bits per subtriangle encoding four modes which can be interpreted as described in ray traversal.
Note
For compactness, these values are stored as 16-bit in some structures. |
The VkMicromapBuildInfoEXT
structure is defined as:
// Provided by VK_EXT_opacity_micromap
typedef struct VkMicromapBuildInfoEXT {
VkStructureType sType;
const void* pNext;
VkMicromapTypeEXT type;
VkBuildMicromapFlagsEXT flags;
VkBuildMicromapModeEXT mode;
VkMicromapEXT dstMicromap;
uint32_t usageCountsCount;
const VkMicromapUsageEXT* pUsageCounts;
const VkMicromapUsageEXT* const* ppUsageCounts;
VkDeviceOrHostAddressConstKHR data;
VkDeviceOrHostAddressKHR scratchData;
VkDeviceOrHostAddressConstKHR triangleArray;
VkDeviceSize triangleArrayStride;
} VkMicromapBuildInfoEXT;
-
sType
is a VkStructureType value identifying this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
type
is a VkMicromapTypeEXT value specifying the type of micromap being built. -
flags
is a bitmask of VkBuildMicromapFlagBitsEXT specifying additional parameters of the micromap. -
mode
is a VkBuildMicromapModeEXT value specifying the type of operation to perform. -
dstMicromap
is a pointer to the target micromap for the build. -
usageCountsCount
specifies the number of usage counts structures that will be used to determine the size of this micromap. -
pUsageCounts
is a pointer to an array of VkMicromapUsageEXT structures. -
ppUsageCounts
is a pointer to an array of pointers to VkMicromapUsageEXT structures. -
data
is the device or host address to memory which contains the data for the micromap. -
scratchData
is the device or host address to memory that will be used as scratch memory for the build. -
triangleArray
is the device or host address to memory containing the VkMicromapTriangleEXT data -
triangleArrayStride
is the stride in bytes between each element oftriangleArray
Only one of pUsageCounts
or ppUsageCounts
can be a valid
pointer, the other must be NULL
.
The elements of the non-NULL
array describe the total counts used to build
each micromap.
Each element contains a count
which is the number of micromap
triangles of that format
and subdivisionLevel
contained in the
micromap.
Multiple elements with the same format
and subdivisionLevel
are
allowed and the total count for that format
and subdivisionLevel
is the sum of the count
for each element.
Each micromap triangle refers to one element in triangleArray
which
contains the format
and subdivisionLevel
for that particular
triangle as well as a dataOffset
in bytes which is the location
relative to data
where that triangle’s micromap data begins.
The data at triangleArray
is laid out as a 4 byte unsigned integer for
the dataOffset
followed by a 2 byte unsigned integer for the
subdivision level then a 2 byte unsigned integer for the format.
In practice, compilers compile VkMicromapTriangleEXT to match this
pattern.
For opacity micromaps, the data at data
is packed as either one bit
per element for VK_OPACITY_MICROMAP_FORMAT_2_STATE_EXT
or two bits per
element for VK_OPACITY_MICROMAP_FORMAT_4_STATE_EXT
and is packed from
LSB to MSB in each byte.
The data at each index in those bytes is interpreted as discussed in
Ray Opacity Micromap.
The VkBuildMicromapModeEXT
enumeration is defined as:
// Provided by VK_EXT_opacity_micromap
typedef enum VkBuildMicromapModeEXT {
VK_BUILD_MICROMAP_MODE_BUILD_EXT = 0,
} VkBuildMicromapModeEXT;
-
VK_BUILD_MICROMAP_MODE_BUILD_EXT
specifies that the destination micromap will be built using the specified data.
The VkMicromapUsageEXT
structure is defined as:
// Provided by VK_EXT_opacity_micromap
typedef struct VkMicromapUsageEXT {
uint32_t count;
uint32_t subdivisionLevel;
uint32_t format;
} VkMicromapUsageEXT;
-
count
is the number of triangles in the usage format defined by thesubdivisionLevel
andformat
below in the micromap -
subdivisionLevel
is the subdivision level of this usage format -
format
is the format of this usage format
The format
is interpreted based on the type
of the micromap
using it.
The VkMicromapTriangleEXT
structure is defined as:
// Provided by VK_EXT_opacity_micromap
typedef struct VkMicromapTriangleEXT {
uint32_t dataOffset;
uint16_t subdivisionLevel;
uint16_t format;
} VkMicromapTriangleEXT;
-
dataOffset
is the offset in bytes of the start of the data for this triangle. This is a byte aligned value. -
subdivisionLevel
is the subdivision level of this triangle -
format
is the format of this triangle
The format
is interpreted based on the type
of the micromap
using it.
38.1.2. Copying Micromaps
An additional command exists for copying micromaps without updating their contents. Before copying, an application must query the size of the resulting micromap.
To query micromap size parameters call:
// Provided by VK_EXT_opacity_micromap
void vkCmdWriteMicromapsPropertiesEXT(
VkCommandBuffer commandBuffer,
uint32_t micromapCount,
const VkMicromapEXT* pMicromaps,
VkQueryType queryType,
VkQueryPool queryPool,
uint32_t firstQuery);
-
commandBuffer
is the command buffer into which the command will be recorded. -
micromapCount
is the count of micromaps for which to query the property. -
pMicromaps
is a pointer to an array of existing previously built micromaps. -
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 themicromapCount
number of results.
Accesses to any of the micromaps listed in pMicromaps
must be
synchronized with the
VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
VK_ACCESS_2_MICROMAP_READ_BIT_EXT
.
-
If
queryType
isVK_QUERY_TYPE_MICROMAP_SERIALIZATION_SIZE_EXT
, then the value written out is the number of bytes required by a serialized micromap. -
If
queryType
isVK_QUERY_TYPE_MICROMAP_COMPACTED_SIZE_EXT
, then the value written out is the number of bytes required by a compacted micromap.
To copy a micromap call:
// Provided by VK_EXT_opacity_micromap
void vkCmdCopyMicromapEXT(
VkCommandBuffer commandBuffer,
const VkCopyMicromapInfoEXT* pInfo);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is a pointer to a VkCopyMicromapInfoEXT structure defining the copy operation.
This command copies the pInfo->src
micromap to the pInfo->dst
micromap in the manner specified by pInfo->mode
.
Accesses to pInfo->src
and pInfo->dst
must be
synchronized with the
VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
VK_ACCESS_2_MICROMAP_READ_BIT_EXT
or
VK_ACCESS_2_MICROMAP_WRITE_BIT_EXT
as appropriate.
The VkCopyMicromapInfoEXT
structure is defined as:
// Provided by VK_EXT_opacity_micromap
typedef struct VkCopyMicromapInfoEXT {
VkStructureType sType;
const void* pNext;
VkMicromapEXT src;
VkMicromapEXT dst;
VkCopyMicromapModeEXT mode;
} VkCopyMicromapInfoEXT;
-
sType
is a VkStructureType value identifying this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
src
is the source micromap for the copy. -
dst
is the target micromap for the copy. -
mode
is a VkCopyMicromapModeEXT value specifying additional operations to perform during the copy.
Possible values of mode
specifying additional operations to perform
during the copy, are:
// Provided by VK_EXT_opacity_micromap
typedef enum VkCopyMicromapModeEXT {
VK_COPY_MICROMAP_MODE_CLONE_EXT = 0,
VK_COPY_MICROMAP_MODE_SERIALIZE_EXT = 1,
VK_COPY_MICROMAP_MODE_DESERIALIZE_EXT = 2,
VK_COPY_MICROMAP_MODE_COMPACT_EXT = 3,
} VkCopyMicromapModeEXT;
-
VK_COPY_MICROMAP_MODE_CLONE_EXT
creates a direct copy of the micromap specified insrc
into the one specified bydst
. Thedst
micromap must have been created with the same parameters assrc
. -
VK_COPY_MICROMAP_MODE_SERIALIZE_EXT
serializes the micromap to a semi-opaque format which can be reloaded on a compatible implementation. -
VK_COPY_MICROMAP_MODE_DESERIALIZE_EXT
deserializes the semi-opaque serialization format in the buffer to the micromap. -
VK_COPY_MICROMAP_MODE_COMPACT_EXT
creates a more compact version of a micromapsrc
intodst
. The micromapdst
must have been created with a size at least as large as that returned by vkCmdWriteMicromapsPropertiesEXT after the build of the micromap specified bysrc
.
To copy a micromap to device memory call:
// Provided by VK_EXT_opacity_micromap
void vkCmdCopyMicromapToMemoryEXT(
VkCommandBuffer commandBuffer,
const VkCopyMicromapToMemoryInfoEXT* pInfo);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is an a pointer to a VkCopyMicromapToMemoryInfoEXT structure defining the copy operation.
Accesses to pInfo->src
must be synchronized with the VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
VK_ACCESS_2_MICROMAP_READ_BIT_EXT
.
Accesses to the buffer indicated by pInfo->dst.deviceAddress
must be
synchronized with the VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an access type of VK_ACCESS_TRANSFER_WRITE_BIT
.
This command produces the same results as vkCopyMicromapToMemoryEXT, 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 vkCmdCopyMemoryToMicromapEXT or vkCopyMemoryToMicromapEXT.
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 vkGetDeviceMicromapCompatibilityEXT The serialized data is written to the buffer (or read from the buffer) according to the host endianness.
// Provided by VK_EXT_opacity_micromap
typedef struct VkCopyMicromapToMemoryInfoEXT {
VkStructureType sType;
const void* pNext;
VkMicromapEXT src;
VkDeviceOrHostAddressKHR dst;
VkCopyMicromapModeEXT mode;
} VkCopyMicromapToMemoryInfoEXT;
-
sType
is a VkStructureType value identifying this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
src
is the source micromap for the copy -
dst
is the device or host address to memory which is the target for the copy -
mode
is a VkCopyMicromapModeEXT value specifying additional operations to perform during the copy.
To copy device memory to a micromap call:
// Provided by VK_EXT_opacity_micromap
void vkCmdCopyMemoryToMicromapEXT(
VkCommandBuffer commandBuffer,
const VkCopyMemoryToMicromapInfoEXT* pInfo);
-
commandBuffer
is the command buffer into which the command will be recorded. -
pInfo
is a pointer to a VkCopyMicromapToMemoryInfoEXT structure defining the copy operation.
Accesses to pInfo->dst
must be synchronized with the VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an
access type of
VK_ACCESS_2_MICROMAP_READ_BIT_EXT
.
Accesses to the buffer indicated by pInfo->src.deviceAddress
must be
synchronized with the VK_PIPELINE_STAGE_2_MICROMAP_BUILD_BIT_EXT
pipeline stage and an access type of VK_ACCESS_TRANSFER_READ_BIT
.
This command can accept micromaps produced by either vkCmdCopyMicromapToMemoryEXT or vkCopyMicromapToMemoryEXT.
The VkCopyMemoryToMicromapInfoEXT
structure is defined as:
// Provided by VK_EXT_opacity_micromap
typedef struct VkCopyMemoryToMicromapInfoEXT {
VkStructureType sType;
const void* pNext;
VkDeviceOrHostAddressConstKHR src;
VkMicromapEXT dst;
VkCopyMicromapModeEXT mode;
} VkCopyMemoryToMicromapInfoEXT;
-
sType
is a VkStructureType value identifying this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
src
is the device or host address to memory containing the source data for the copy. -
dst
is the target micromap for the copy. -
mode
is a VkCopyMicromapModeEXT value specifying additional operations to perform during the copy.
To check if a serialized micromap is compatible with the current device call:
// Provided by VK_EXT_opacity_micromap
void vkGetDeviceMicromapCompatibilityEXT(
VkDevice device,
const VkMicromapVersionInfoEXT* pVersionInfo,
VkAccelerationStructureCompatibilityKHR* pCompatibility);
-
device
is the device to check the version against. -
pVersionInfo
is a pointer to a VkMicromapVersionInfoEXT structure specifying version information to check against the device. -
pCompatibility
is a pointer to a VkAccelerationStructureCompatibilityKHR value in which compatibility information is returned.
The VkMicromapVersionInfoEXT
structure is defined as:
// Provided by VK_EXT_opacity_micromap
typedef struct VkMicromapVersionInfoEXT {
VkStructureType sType;
const void* pNext;
const uint8_t* pVersionData;
} VkMicromapVersionInfoEXT;
-
sType
is a VkStructureType value identifying this structure. -
pNext
isNULL
or a pointer to a structure extending this structure. -
pVersionData
is a pointer to the version header of a micromap as defined in vkCmdCopyMicromapToMemoryEXT
Note
|
38.2. Host Micromap Operations
Implementations are also required to provide host implementations of the
micromap operations if the micromapHostCommands
feature is enabled:
-
vkBuildMicromapsEXT corresponding to vkCmdBuildMicromapsEXT
-
vkCopyMicromapEXT corresponding to vkCmdCopyMicromapEXT
-
vkCopyMicromapToMemoryEXT corresponding to vkCmdCopyMicromapToMemoryEXT
-
vkCopyMemoryToMicromapEXT corresponding to vkCmdCopyMemoryToMicromapEXT
-
vkWriteMicromapsPropertiesEXT corresponding to vkCmdWriteMicromapsPropertiesEXT
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 micromaps used by the host commands must be bound to host-visible memory, and all input data for micromap builds must be referenced using host addresses instead of device addresses. Applications are not required to map micromap memory when using the host commands.
Note
The vkBuildMicromapsEXT and vkCmdBuildMicromapsEXT may use different algorithms, and thus are not required to produce identical structures. Apart from these details, the host and device operations are interchangeable. |
Note
For efficient execution, micromaps manipulated using these commands should always be bound to host cached memory, as the implementation may need to repeatedly read and write this memory during the execution of the command. |
To build micromaps on the host, call:
// Provided by VK_EXT_opacity_micromap
VkResult vkBuildMicromapsEXT(
VkDevice device,
VkDeferredOperationKHR deferredOperation,
uint32_t infoCount,
const VkMicromapBuildInfoEXT* pInfos);
-
device
is theVkDevice
for which the micromaps are being built. -
deferredOperation
is an optional VkDeferredOperationKHR to request deferral for this command. -
infoCount
is the number of micromaps to build. It specifies the number of thepInfos
that must be provided. -
pInfos
is a pointer to an array ofinfoCount
VkMicromapBuildInfoEXT structures defining the geometry used to build each micromap.
This command fulfills the same task as vkCmdBuildMicromapsEXT but is executed by the host.
The vkBuildMicromapsEXT
command provides the ability to initiate
multiple micromaps builds, however there is no ordering or synchronization
implied between any of the individual micromap builds.
Note
This means that there cannot be any memory aliasing between any micromap memories or scratch memories being used by any of the builds. |
To copy or compact a micromap on the host, call:
// Provided by VK_EXT_opacity_micromap
VkResult vkCopyMicromapEXT(
VkDevice device,
VkDeferredOperationKHR deferredOperation,
const VkCopyMicromapInfoEXT* pInfo);
-
device
is the device which owns the micromaps. -
deferredOperation
is an optional VkDeferredOperationKHR to request deferral for this command. -
pInfo
is a pointer to a VkCopyMicromapInfoEXT structure defining the copy operation.
This command fulfills the same task as vkCmdCopyMicromapEXT but is executed by the host.
To copy host accessible memory to a micromap, call:
// Provided by VK_EXT_opacity_micromap
VkResult vkCopyMemoryToMicromapEXT(
VkDevice device,
VkDeferredOperationKHR deferredOperation,
const VkCopyMemoryToMicromapInfoEXT* pInfo);
-
device
is the device which ownspInfo->dst
. -
deferredOperation
is an optional VkDeferredOperationKHR to request deferral for this command. -
pInfo
is a pointer to a VkCopyMemoryToMicromapInfoEXT structure defining the copy operation.
This command fulfills the same task as vkCmdCopyMemoryToMicromapEXT but is executed by the host.
This command can accept micromaps produced by either vkCmdCopyMicromapToMemoryEXT or vkCopyMicromapToMemoryEXT.
To copy a micromap to host accessible memory, call:
// Provided by VK_EXT_opacity_micromap
VkResult vkCopyMicromapToMemoryEXT(
VkDevice device,
VkDeferredOperationKHR deferredOperation,
const VkCopyMicromapToMemoryInfoEXT* pInfo);
-
device
is the device which ownspInfo->src
. -
deferredOperation
is an optional VkDeferredOperationKHR to request deferral for this command. -
pInfo
is a pointer to a VkCopyMicromapToMemoryInfoEXT structure defining the copy operation.
This command fulfills the same task as vkCmdCopyMicromapToMemoryEXT but is executed by the host.
This command produces the same results as vkCmdCopyMicromapToMemoryEXT, 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 vkCmdCopyMemoryToMicromapEXT or vkCopyMemoryToMicromapEXT.
To query micromap size parameters on the host, call:
// Provided by VK_EXT_opacity_micromap
VkResult vkWriteMicromapsPropertiesEXT(
VkDevice device,
uint32_t micromapCount,
const VkMicromapEXT* pMicromaps,
VkQueryType queryType,
size_t dataSize,
void* pData,
size_t stride);
-
device
is the device which owns the micromaps inpMicromaps
. -
micromapCount
is the count of micromaps for which to query the property. -
pMicromaps
is a pointer to an array of existing previously built micromaps. -
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
.
This command fulfills the same task as vkCmdWriteMicromapsPropertiesEXT but is executed by the host.