14.5. Shader Resource Interface

When a shader stage accesses buffer or image resources, as described in the Resource Descriptors section, the shader resource variables must be matched with the pipeline layout that is provided at pipeline creation time.

The set of shader resources that form the shader resource interface for a stage are the variables statically used by OpEntryPoint with the storage class of Uniform, UniformConstant, or PushConstant. For the fragment shader, this includes the fragment input attachment interface.

The shader resource interface consists of two sub-interfaces: the push constant interface and the descriptor set interface.

14.5.1. Push Constant Interface

The shader variables defined with a storage class of PushConstant that are statically used by the shader entry points for the pipeline define the push constant interface. They must be:

  • typed as OpTypeStruct,
  • identified with a Block decoration, and
  • laid out explicitly using the Offset, ArrayStride, and MatrixStride decorations as specified in Offset and Stride Assignment.

There must be no more than one push constant block statically used per shader entry point.

Each variable in a push constant block must be placed at an Offset such that the entire constant value is entirely contained within the VkPushConstantRange for each OpEntryPoint that uses it, and the stageFlags for that range must specify the appropriate VkShaderStageFlagBits for that stage. The Offset decoration for any variable in a push constant block must not cause the space required for that variable to extend outside the range $[0, \mathit{maxPushConstantsSize})$ .

Any variable in a push constant block that is declared as an array must only be accessed with dynamically uniform indices.

14.5.2. Descriptor Set Interface

The descriptor set interface is comprised of the shader variables with the storage class of Uniform or UniformConstant (including the variables in the fragment input attachment interface) that are statically used by the shader entry points for the pipeline.

These variables must have DescriptorSet and Binding decorations specified, which are assigned and matched with the VkDescriptorSetLayout objects in the pipeline layout as described in DescriptorSet and Binding Assignment.

Variables identified with the UniformConstant storage class are used only as handles to refer to opaque resources. Such variables must be typed as OpTypeImage, OpTypeSampler, OpTypeSampledImage, or arrays of only these types. Variables of type OpTypeImage must have a Sampled operand of 1 (sampled image) or 2 (storage image).

Any array of these types must only be indexed with constant integral expressions, except under the following conditions:

  • For arrays of OpTypeImage variables with Sampled operand of 2, if the shaderStorageImageArrayDynamicIndexing feature is enabled and the shader module declares the StorageImageArrayDynamicIndexing capability, the array must only be indexed by dynamically uniform expressions.
  • For arrays of OpTypeSampler, OpTypeSampledImage variables, or OpTypeImage variables with Sampled operand of 1, if the shaderSampledImageArrayDynamicIndexing feature is enabled and the shader module declares the SampledImageArrayDynamicIndexing capability, the array must only be indexed by dynamically uniform expressions.

The Sampled Type of an OpTypeImage declaration must match the same basic data type as the corresponding resource, or the values obtained by reading or sampling from this image are undefined.

The Image Format of an OpTypeImage declaration must not be Unknown, for variables which are used for OpImageRead or OpImageWrite operations, except under the following conditions:

  • For OpImageWrite, if the shaderStorageImageWriteWithoutFormat feature is enabled and the shader module declares the StorageImageWriteWithoutFormat capability.
  • For OpImageRead, if the shaderStorageImageReadWithoutFormat feature is enabled and the shader module declares the StorageImageReadWithoutFormat capability.

Variables identified with the Uniform storage class are used to access transparent buffer backed resources. Such variables must be:

  • typed as OpTypeStruct, or arrays of only this type,
  • identified with a Block or BufferBlock decoration, and
  • laid out explicitly using the Offset, ArrayStride, and MatrixStride decorations as specified in Offset and Stride Assignment.

Any array of these types must only be indexed with constant integral expressions, except under the following conditions.

  • For arrays of Block variables, if the shaderUniformBufferArrayDynamicIndexing feature is enabled and the shader module declares the UniformBufferArrayDynamicIndexing capability, the array must only be indexed by dynamically uniform expressions.
  • For arrays of BufferBlock variables, if the shaderStorageBufferArrayDynamicIndexing feature is enabled and the shader module declares the StorageBufferArrayDynamicIndexing capability, the array must only be indexed by dynamically uniform expressions.

The Offset decoration for any variable in a Block must not cause the space required for that variable to extend outside the range $[0, \mathit{maxUniformBufferRange})$ . The Offset decoration for any variable in a BufferBlock must not cause the space required for that variable to extend outside the range $[0, \mathit{maxStorageBufferRange})$ .

Variables identified with a storage class of UniformConstant and a decoration of InputAttachmentIndex must be declared as described in Fragment Input Attachment Interface.

Each shader variable declaration must refer to the same type of resource as is indicated by the descriptorType. See Shader Resource and Descriptor Type Correspondence for the relationship between shader declarations and descriptor types.

Table 14.2. Shader Resource and Descriptor Type Correspondence

Resource type Descriptor Type

sampler

VK_DESCRIPTOR_TYPE_SAMPLER

sampled image

VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE

storage image

VK_DESCRIPTOR_TYPE_STORAGE_IMAGE

combined image sampler

VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER

uniform texel buffer

VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER

storage texel buffer

VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER

uniform buffer

VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC

storage buffer

VK_DESCRIPTOR_TYPE_STORAGE_BUFFER VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC

input attachment

VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT


Table 14.3. Shader Resource and Storage Class Correspondence

Resource type Storage Class Type Decoration(s)1

sampler

UniformConstant

OpTypeSampler

sampled image

UniformConstant

OpTypeImage (Sampled=1)

storage image

UniformConstant

OpTypeImage (Sampled=2)

combined image sampler

UniformConstant

OpTypeSampledImage

uniform texel buffer

UniformConstant

OpTypeImage (Dim=Buffer, Sampled=1)

storage texel buffer

UniformConstant

OpTypeImage (Dim=Buffer, Sampled=2)

uniform buffer

Uniform

OpTypeStruct

Block, Offset, (ArrayStride), (MatrixStride)

storage buffer

Uniform

OpTypeStruct

BufferBlock, Offset, (ArrayStride), (MatrixStride)

input attachment

UniformConstant

OpTypeImage (Dim=SubpassData, Sampled=2)

InputAttachmentIndex


1
in addition to DescriptorSet and Binding

14.5.3. DescriptorSet and Binding Assignment

A variable identified with a DescriptorSet decoration of $s$ and a Binding decoration of $b$ indicates that this variable is associated with the VkDescriptorSetLayoutBinding that has a binding equal to $b$ in pSetLayouts[s] that was specified in VkPipelineLayoutCreateInfo.

The range of descriptor sets is between zero and maxBoundDescriptorSets minus one. If a descriptor set value is statically used by an entry point there must be an associated pSetLayout in the corresponding pipeline layout as described in Pipeline Layouts consistency.

If the Binding decoration is used with an array, the entire array is identified with that binding value. The size of the array declaration must be no larger than the descriptorCount of that VkDescriptorSetLayoutBinding. The index of each element of the array is referred to as the arrayElement. For the purposes of interface matching and descriptor set operations, if a resource variable is not an array, it is treated as if it has an arrayElement of zero.

The binding can be any 32-bit unsigned integer value, as described in Section 13.2.1, “Descriptor Set Layout”. Each descriptor set has its own binding name space.

There is a limit on the number of resources of each type that can be accessed by a pipeline stage as shown in Shader Resource Limits. The “Resources Per Stage” column gives the limit on the number each type of resource that can be statically used for an entry point in any given stage in a pipeline. The “Resource Types” column lists which resource types are counted against the limit. Some resource types count against multiple limits.

If multiple entry points in the same pipeline refer to the same set and binding, all variable definitions with that DescriptorSet and Binding must have the same basic type.

Not all descriptor sets and bindings specified in a pipeline layout need to be used in a particular shader stage or pipeline, but if a DescriptorSet and Binding decoration is specified for a variable that is statically used in that shader there must be a pipeline layout entry identified with that descriptor set and binding and the corresponding stageFlags must specify the appropriate VkShaderStageFlagBits for that stage.

Table 14.4. Shader Resource Limits

Resources per Stage Resource Types

maxPerStageDescriptorSamplers

sampler

combined image sampler

maxPerStageDescriptorSampledImages

sampled image

combined image sampler

uniform texel buffer

maxPerStageDescriptorStorageImages

storage image

storage texel buffer

maxPerStageDescriptorUniformBuffers

uniform buffer

uniform buffer dynamic

maxPerStageDescriptorStorageBuffers

storage buffer

storage buffer dynamic

maxPerStageDescriptorInputAttachments

input attachment1


1
Input attachments can only be used in the fragment shader stage

14.5.4. Offset and Stride Assignment

All variables with a storage class of PushConstant or Uniform must be explicitly laid out using the Offset, ArrayStride, and MatrixStride decorations. There are two different layouts requirements depending on the specific resources.

Standard Uniform Buffer Layout

Member variables of an OpTypeStruct with storage class of Uniform and a decoration of Block (uniform buffers) must be laid out according to the following rules.

  • The Offset Decoration must be a multiple of its base alignment, computed recursively as follows:

    • a scalar of size $N$ has a base alignment of $N$
    • a two-component vector, with components of size $N$ , has a base alignment of $2N$
    • a three- or four-component vector, with components of size $N$ , has a base alignment of $4N$
    • an array has a base alignment equal to the base alignment of its element type, rounded up to a multiple of $16$
    • a structure has a base alignment equal to the largest base alignment of any of its members, rounded up to a multiple of $16$
    • a row-major matrix of $C$ columns has a base alignment equal to the base alignment of vector of $C$ matrix components
    • a column-major matrix has a base alignment equal to the base alignment of the matrix column type
  • Any ArrayStride or MatrixStride decoration must be an integer multiple of the base alignment of the array or matrix from above.
  • The Offset Decoration of a member immediately following a structure or an array must be greater than or equal to the next multiple of the base alignment of that structure or array.
[Note]Note

The std140 layout in GLSL satisfies these rules.

Standard Storage Buffer Layout

Member variables of an OpTypeStruct with a storage class of PushConstant (push constants), or a storage class of Uniform with a decoration of BufferBlock (storage buffers) must be laid out as above, except for array and structure base alignment which do not need to be rounded up to a multiple of $16$ .

[Note]Note

The std430 layout in GLSL satisfies these rules.