From 0244c96d637f47fa264d441a82d3dca78889373b Mon Sep 17 00:00:00 2001 From: ArielG-NV <159081215+ArielG-NV@users.noreply.github.com> Date: Fri, 16 May 2025 11:57:17 -0700 Subject: Fix correct bindings for bindless resource model [SPIRV and GLSL] (#7131) Fix correct bindings for bindless resource model [spirv and glsl] fixes: #6952 Problem: * Currently all bindless objects are placed in the same set (fine) and same binding (incorrect behavior for vulkan). This is incorrect since as per [spec](https://registry.khronos.org/vulkan/specs/latest/man/html/VkDescriptorType.html), only 1 resource type may be written to each index inside a set (these rules are loosened with VK_EXT_mutable_descriptor_type) * This means currently generated bindings do not work in practice if we (for example) use `Sampler2D.Handle` and `Texture1D.Handle` in a shader since we would place 2 incompatible objects in the same binding-index and set. Solution: * `__getDynamicResourceHeap` was modified to allow bindings to chosen dynamically for a descriptor * use `IOpaqueDescriptor` to check compile-time information of resource types so that we can identify different resources * Using this information of `IOpaqueDescriptor`, we modify `defaultGetDescriptorFromHandle` to provide a binding model (1 resource per binding-index) which produces legal spirv/glsl. * To support `VK_EXT_mutable_descriptor_type` the function `defaultGetDescriptorFromHandle` has a set of options (`BindlessDescriptorOptions`) for a user to pick-from to support their binding model. Capabilities are not used here for flexibility purposes (specifically old shaders mixed with modern vulkan extensions). Other changes: * Added `TexelBuffer` DescriptorKind to aid in generating correct bindings * format code * Add to docs bindless changes, make AccelerationStructure use its handle directly, adjust tests accordingly --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- docs/user-guide/03-convenience-features.md | 49 ++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'docs') diff --git a/docs/user-guide/03-convenience-features.md b/docs/user-guide/03-convenience-features.md index 1e9a79882..2c2eb6f42 100644 --- a/docs/user-guide/03-convenience-features.md +++ b/docs/user-guide/03-convenience-features.md @@ -604,6 +604,24 @@ When targeting SPIRV, Slang will introduce a global array of descriptors and fet The descriptor set ID of the global descriptor array can be configured with the `-bindless-space-index` (or `CompilerOptionName::BindlessSpaceIndex` when using the API) option. +Default behavior assigns the following binding-indicies to each descriptor type: +```slang +enum DefaultVkBindlessBindings : uint +{ + Sampler = 0, /// SAMPLER + CombinedTextureSampler = 1, /// COMBINED_IMAGE_SAMPLER + Texture_Read = 2, /// SAMPLED_IMAGE + Texture_ReadWrite = 3, /// STORAGE_IMAGE + TexelBuffer_Read = 4, /// UNIFORM_TEXEL_BUFFER + TexelBuffer_ReadWrite = 5, /// STORAGE_TEXEL_BUFFER + Buffer_Read = 6, /// UNIFORM_BUFFER + Buffer_ReadWrite = 7, /// STORAGE_BUFFER + Unknown = 8, /// Other +} +``` + +`ACCELERATION_STRUCTURE` is excluded from the list of types since slang by default uses the provided handle to a `RaytracingAccelerationStructure` as a GPU address into the respective `RaytracingAccelerationStructure`, casting the 64bit handle into the type. + > #### Note > The default implementation for SPIRV may change in the future if SPIRV is extended to provide what is > equivalent to D3D's `ResourceDescriptorHeap` construct. @@ -653,8 +671,32 @@ interface IOpaqueDescriptor The user can call `defaultGetDescriptorFromHandle` function from their implementation of `getDescriptorFromHandle` to dispatch to the default behavior. -The `kind` and `descriptorAccess` constants allows user code to fetch from different locations -depending on the type and access of the resource being requested. The `DescriptorKind` and +Additionally, `defaultGetDescriptorFromHandle` has the parameter `constexpr BindlessDescriptorOptions bindlessOptions`. This parameter provides some alternative presets for how bindless indexes are assigned (currently only relevant to SPIRV): + ```slang +public enum BindlessDescriptorOptions +{ + None = 0, /// Bind assuming regular binding model rules. + VkMutable = 1, /// Bind assuming `VK_EXT_mutable_descriptor_type` without mutable `AccelerationStructure` binding support. +} + ``` + +`VkMutable` provides the following bindings for descriptor types: +```slang +enum VkMutableBindlessBindings : uint +{ + Sampler = 0, /// SAMPLER + CombinedTextureSampler = 1, /// COMBINED_IMAGE_SAMPLER + Texture_Read = 2, /// SAMPLED_IMAGE + Texture_ReadWrite = 2, /// STORAGE_IMAGE + TexelBuffer_Read = 2, /// UNIFORM_TEXEL_BUFFER + TexelBuffer_ReadWrite = 2, /// STORAGE_TEXEL_BUFFER + Buffer_Read = 2, /// UNIFORM_BUFFER + Buffer_ReadWrite = 2, /// STORAGE_BUFFER, + Unknown = 3, /// Other +} +``` + +The `kind` and `descriptorAccess` constants allows user code to fetch resources from different locations depending on the type and access of the resource being requested. The `DescriptorKind` and `DescriptorAccess` enums are defined as: ```slang @@ -666,6 +708,7 @@ enum DescriptorKind Buffer, /// A buffer descriptor. Sampler, /// A sampler state descriptor. AccelerationStructure, /// A ray tracing acceleration structure descriptor. + TexelBuffer /// A texel buffer descriptor. } enum DescriptorAccess @@ -692,6 +735,8 @@ void test(DescriptorHandle t) If the resource pointer value is not uniform and `nonuniform` is not called, the result may be undefined. + + Extensions -------------------- Slang allows defining additional methods for a type outside its initial definition. For example, suppose we already have a type defined: -- cgit v1.2.3