15. Image Operations

15.1. Image Operations Overview

Image Operations are steps performed by SPIR-V image instructions, where those instructions which take an OpTypeImage (representing a VkImageView) or OpTypeSampledImage (representing a (VkImageView, VkSampler) pair) and texel coordinates as operands, and return a value based on one or more neighboring texture elements (texels) in the image.

Note

Texel is a term which is a combination of the words texture and element. Early interactive computer graphics supported texture operations on textures, a small subset of the image operations on images described here. The discrete samples remain essentially equivalent, however, so we retain the historical term texel to refer to them.

SPIR-V Image Instructions include the following functionality:

  • OpImageSample* and OpImageSparseSample* read one or more neighboring texels of the image, and filter the texel values based on the state of the sampler.

    • Instructions with ImplicitLod in the name determine the LOD used in the sampling operation based on the coordinates used in neighboring fragments.

    • Instructions with ExplicitLod in the name determine the LOD used in the sampling operation based on additional coordinates.

    • Instructions with Proj in the name apply homogeneous projection to the coordinates.

  • OpImageFetch and OpImageSparseFetch return a single texel of the image. No sampler is used.

  • OpImage*Gather and OpImageSparse*Gather read neighboring texels and return a single component of each.

  • OpImageRead (and OpImageSparseRead) and OpImageWrite read and write, respectively, a texel in the image. No sampler is used.

  • OpImageSampleFootprintNV identifies and returns information about the set of texels in the image that would be accessed by an equivalent OpImageSample* instruction.

  • Instructions with Dref in the name apply depth comparison on the texel values.

  • Instructions with Sparse in the name additionally return a sparse residency code.

15.1.1. Texel Coordinate Systems

Images are addressed by texel coordinates. There are three texel coordinate systems:

  • normalized texel coordinates [0.0, 1.0]

  • unnormalized texel coordinates [0.0, width / height / depth)

  • integer texel coordinates [0, width / height / depth)

SPIR-V OpImageFetch, OpImageSparseFetch, OpImageRead, OpImageSparseRead, and OpImageWrite instructions use integer texel coordinates. Other image instructions can use either normalized or unnormalized texel coordinates (selected by the unnormalizedCoordinates state of the sampler used in the instruction), but there are limitations on what operations, image state, and sampler state is supported. Normalized coordinates are logically converted to unnormalized as part of image operations, and certain steps are only performed on normalized coordinates. The array layer coordinate is always treated as unnormalized even when other coordinates are normalized.

Normalized texel coordinates are referred to as (s,t,r,q,a), with the coordinates having the following meanings:

  • s: Coordinate in the first dimension of an image.

  • t: Coordinate in the second dimension of an image.

  • r: Coordinate in the third dimension of an image.

    • (s,t,r) are interpreted as a direction vector for Cube images.

  • q: Fourth coordinate, for homogeneous (projective) coordinates.

  • a: Coordinate for array layer.

The coordinates are extracted from the SPIR-V operand based on the dimensionality of the image variable and type of instruction. For Proj instructions, the components are in order (s, [t,] [r,] q) with t and r being conditionally present based on the Dim of the image. For non-Proj instructions, the coordinates are (s [,t] [,r] [,a]), with t and r being conditionally present based on the Dim of the image and a being conditionally present based on the Arrayed property of the image. Projective image instructions are not supported on Arrayed images.

Unnormalized texel coordinates are referred to as (u,v,w,a), with the coordinates having the following meanings:

  • u: Coordinate in the first dimension of an image.

  • v: Coordinate in the second dimension of an image.

  • w: Coordinate in the third dimension of an image.

  • a: Coordinate for array layer.

Only the u and v coordinates are directly extracted from the SPIR-V operand, because only 1D and 2D (non-Arrayed) dimensionalities support unnormalized coordinates. The components are in order (u [,v]), with v being conditionally present when the dimensionality is 2D. When normalized coordinates are converted to unnormalized coordinates, all four coordinates are used.

Integer texel coordinates are referred to as (i,j,k,l,n), and the first four in that order have the same meanings as unnormalized texel coordinates. They are extracted from the SPIR-V operand in order (i, [,j], [,k], [,l]), with j and k conditionally present based on the Dim of the image, and l conditionally present based on the Arrayed property of the image. n is the sample index and is taken from the Sample image operand.

For all coordinate types, unused coordinates are assigned a value of zero.

image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t i0j0 i1j0 i0j1 i1j1 (u-0.5, v-0.5) (u, v) i0j0' i1j0' i0j1' i1j1'
Figure 3. Texel Coordinate Systems

The Texel Coordinate Systems - For the example shown of an 8×4 texel two dimensional image.

  • Normalized texel coordinates:

    • The s coordinate goes from 0.0 to 1.0, left to right.

    • The t coordinate goes from 0.0 to 1.0, top to bottom.

  • Unnormalized texel coordinates:

    • The u coordinate goes from -1.0 to 9.0, left to right. The u coordinate within the range 0.0 to 8.0 is within the image, otherwise it is within the border.

    • The v coordinate goes from -1.0 to 5.0, top to bottom. The v coordinate within the range 0.0 to 4.0 is within the image, otherwise it is within the border.

  • Integer texel coordinates:

    • The i coordinate goes from -1 to 8, left to right. The i coordinate within the range 0 to 7 addresses texels within the image, otherwise it addresses a border texel.

    • The j coordinate goes from -1 to 5, top to bottom. The j coordinate within the range 0 to 3 addresses texels within the image, otherwise it addresses a border texel.

  • Also shown for linear filtering:

    • Given the unnormalized coordinates (u,v), the four texels selected are i0j0, i1j0, i0j1, and i1j1.

    • The weights α and β.

    • Given the offset Δi and Δj, the four texels selected by the offset are i0j'0, i1j'0, i0j'1, and i1j'1.

Note

For formats with reduced-resolution channels, Δi and Δj are relative to the resolution of the highest-resolution channel, and therefore may be divided by two relative to the unnormalized coordinate space of the lower-resolution channels.

image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t ij ij'
Figure 4. Texel Coordinate Systems

The Texel Coordinate Systems - For the example shown of an 8×4 texel two dimensional image.

  • Texel coordinates as above. Also shown for nearest filtering:

    • Given the unnormalized coordinates (u,v), the texel selected is ij.

    • Given the offset Δi and Δj, the texel selected by the offset is ij'.

For corner-sampled images, the texel samples are located at the grid intersections instead of the texel centers.

15.2. Conversion Formulas

15.2.1. RGB to Shared Exponent Conversion

An RGB color (red, green, blue) is transformed to a shared exponent color (redshared, greenshared, blueshared, expshared) as follows:

First, the components (red, green, blue) are clamped to (redclamped, greenclamped, blueclamped) as:

redclamped = max(0, min(sharedexpmax, red))

greenclamped = max(0, min(sharedexpmax, green))

blueclamped = max(0, min(sharedexpmax, blue))

Where:

\[\begin{aligned} N & = 9 & \text{number of mantissa bits per component} \\ B & = 15 & \text{exponent bias} \\ E_{max} & = 31 & \text{maximum possible biased exponent value} \\ sharedexp_{max} & = \frac{(2^N-1)}{2^N} \times 2^{(E_{max}-B)} \end{aligned}\]
Note

NaN, if supported, is handled as in IEEE 754-2008 minNum() and maxNum(). That is the result is a NaN is mapped to zero.

The largest clamped component, maxclamped is determined:

maxclamped = max(redclamped, greenclamped, blueclamped)

A preliminary shared exponent exp' is computed:

\[\begin{aligned} exp' = \begin{cases} \left \lfloor \log_2(max_{clamped}) \right \rfloor + (B+1) & \text{for}\ max_{clamped} > 2^{-(B+1)} \\ 0 & \text{for}\ max_{clamped} \leq 2^{-(B+1)} \end{cases} \end{aligned}\]

The shared exponent expshared is computed:

\[\begin{aligned} max_{shared} = \left \lfloor { \frac{max_{clamped}}{2^{(exp'-B-N)}} + \frac{1}{2} } \right \rfloor \end{aligned}\]
\[\begin{aligned} exp_{shared} = \begin{cases} exp' & \text{for}\ 0 \leq max_{shared} < 2^N \\ exp'+1 & \text{for}\ max_{shared} = 2^N \end{cases} \end{aligned}\]

Finally, three integer values in the range 0 to 2N are computed:

\[\begin{aligned} red_{shared} & = \left \lfloor { \frac{red_{clamped}}{2^{(exp_{shared}-B-N)}}+ \frac{1}{2} } \right \rfloor \\ green_{shared} & = \left \lfloor { \frac{green_{clamped}}{2^{(exp_{shared}-B-N)}}+ \frac{1}{2} } \right \rfloor \\ blue_{shared} & = \left \lfloor { \frac{blue_{clamped}}{2^{(exp_{shared}-B-N)}}+ \frac{1}{2} } \right \rfloor \end{aligned}\]

15.2.2. Shared Exponent to RGB

A shared exponent color (redshared, greenshared, blueshared, expshared) is transformed to an RGB color (red, green, blue) as follows:

\(red = red_{shared} \times {2^{(exp_{shared}-B-N)}}\)

\(green = green_{shared} \times {2^{(exp_{shared}-B-N)}}\)

\(blue = blue_{shared} \times {2^{(exp_{shared}-B-N)}}\)

Where:

N = 9 (number of mantissa bits per component)

B = 15 (exponent bias)

15.3. Texel Input Operations

Texel input instructions are SPIR-V image instructions that read from an image. Texel input operations are a set of steps that are performed on state, coordinates, and texel values while processing a texel input instruction, and which are common to some or all texel input instructions. They include the following steps, which are performed in the listed order:

For texel input instructions involving multiple texels (for sampling or gathering), these steps are applied for each texel that is used in the instruction. Depending on the type of image instruction, other steps are conditionally performed between these steps or involving multiple coordinate or texel values.

If Chroma Reconstruction is implicit, Texel Filtering instead takes place during chroma reconstruction, before sampler Y’CBCR conversion occurs.

15.3.1. Texel Input Validation Operations

Texel input validation operations inspect instruction/image/sampler state or coordinates, and in certain circumstances cause the texel value to be replaced or become undefined. There are a series of validations that the texel undergoes.

Instruction/Sampler/Image View Validation

There are a number of cases where a SPIR-V instruction can mismatch with the sampler, the image view, or both. There are a number of cases where the sampler can mismatch with the image view. In such cases the value of the texel returned is undefined.

These cases include:

  • The sampler borderColor is an integer type and the image view format is not one of the VkFormat integer types or a stencil component of a depth/stencil format.

  • The sampler borderColor is a float type and the image view format is not one of the VkFormat float types or a depth component of a depth/stencil format.

  • The sampler borderColor is one of the opaque black colors (VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK or VK_BORDER_COLOR_INT_OPAQUE_BLACK) and the image view VkComponentSwizzle for any of the VkComponentMapping components is not VK_COMPONENT_SWIZZLE_IDENTITY.

  • The VkImageLayout of any subresource in the image view does not match that specified in VkDescriptorImageInfo::imageLayout used to write the image descriptor.

  • If the instruction is OpImageRead or OpImageSparseRead and the shaderStorageImageReadWithoutFormat feature is not enabled, or the instruction is OpImageWrite and the shaderStorageImageWriteWithoutFormat feature is not enabled, then the SPIR-V Image Format must be compatible with the image view’s format.

  • The sampler unnormalizedCoordinates is VK_TRUE and any of the limitations of unnormalized coordinates are violated.

  • The SPIR-V instruction is one of the OpImage*Dref* instructions and the sampler compareEnable is VK_FALSE

  • The SPIR-V instruction is not one of the OpImage*Dref* instructions and the sampler compareEnable is VK_TRUE

  • The SPIR-V instruction is one of the OpImage*Dref* instructions and the image view format is not one of the depth/stencil formats with a depth component, or the image view aspect is not VK_IMAGE_ASPECT_DEPTH_BIT.

  • The SPIR-V instruction’s image variable’s properties are not compatible with the image view:

    • Rules for viewType:

      • VK_IMAGE_VIEW_TYPE_1D must have Dim = 1D, Arrayed = 0, MS = 0.

      • VK_IMAGE_VIEW_TYPE_2D must have Dim = 2D, Arrayed = 0.

      • VK_IMAGE_VIEW_TYPE_3D must have Dim = 3D, Arrayed = 0, MS = 0.

      • VK_IMAGE_VIEW_TYPE_CUBE must have Dim = Cube, Arrayed = 0, MS = 0.

      • VK_IMAGE_VIEW_TYPE_1D_ARRAY must have Dim = 1D, Arrayed = 1, MS = 0.

      • VK_IMAGE_VIEW_TYPE_2D_ARRAY must have Dim = 2D, Arrayed = 1.

      • VK_IMAGE_VIEW_TYPE_CUBE_ARRAY must have Dim = Cube, Arrayed = 1, MS = 0.

    • If the image was created with VkImageCreateInfo::samples equal to VK_SAMPLE_COUNT_1_BIT, the instruction must have MS = 0.

    • If the image was created with VkImageCreateInfo::samples not equal to VK_SAMPLE_COUNT_1_BIT, the instruction must have MS = 1.

  • If the image was created with VkImageCreateInfo::flags containing VK_IMAGE_CREATE_CORNER_SAMPLED_BIT_NV, the sampler addressing modes must only use a VkSamplerAddressMode of VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE.

  • The SPIR-V instruction is OpImageSampleFootprintNV with Dim = 2D and addressModeU or addressModeV in the sampler is not VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE.

  • The SPIR-V instruction is OpImageSampleFootprintNV with Dim = 3D and addressModeU, addressModeV, or addressModeW in the sampler is not VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE.

Only OpImageSample* and OpImageSparseSample* can be used with a sampler that enables sampler Y’CBCR conversion.

OpImageFetch, OpImageSparseFetch, OpImage*Gather, and OpImageSparse*Gather must not be used with a sampler that enables sampler Y'CBCR conversion.

The ConstOffset and Offset operands must not be used with a sampler that enables sampler Y’CBCR conversion.

Integer Texel Coordinate Validation

Integer texel coordinates are validated against the size of the image level, and the number of layers and number of samples in the image. For SPIR-V instructions that use integer texel coordinates, this is performed directly on the integer coordinates. For instructions that use normalized or unnormalized texel coordinates, this is performed on the coordinates that result after conversion to integer texel coordinates.

If the integer texel coordinates do not satisfy all of the conditions

0 ≤ i < ws

0 ≤ j < hs

0 ≤ k < ds

0 ≤ l < layers

0 ≤ n < samples

where:

ws = width of the image level

hs = height of the image level

ds = depth of the image level

layers = number of layers in the image

samples = number of samples per texel in the image

then the texel fails integer texel coordinate validation.

There are four cases to consider:

  1. Valid Texel Coordinates

    • If the texel coordinates pass validation (that is, the coordinates lie within the image),

    then the texel value comes from the value in image memory.

  2. Border Texel

    • If the texel coordinates fail validation, and

    • If the read is the result of an image sample instruction or image gather instruction, and

    • If the image is not a cube image,

    then the texel is a border texel and texel replacement is performed.

  3. Invalid Texel

    • If the texel coordinates fail validation, and

    • If the read is the result of an image fetch instruction, image read instruction, or atomic instruction,

    then the texel is an invalid texel and texel replacement is performed.

  4. Cube Map Edge or Corner

    Otherwise the texel coordinates lie on the borders along the edges and corners of a cube map image, and Cube map edge handling is performed.

Cube Map Edge Handling

If the texel coordinates lie on the borders along the edges and corners of a cube map image, the following steps are performed. Note that this only occurs when using VK_FILTER_LINEAR filtering within a mip level, since VK_FILTER_NEAREST is treated as using VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE.

  • Cube Map Edge Texel

    • If the texel lies along the border in either only i or only j

    then the texel lies along an edge, so the coordinates (i,j) and the array layer l are transformed to select the adjacent texel from the appropriate neighboring face.

  • Cube Map Corner Texel

    • If the texel lies along the border in both i and j

    then the texel lies at a corner and there is no unique neighboring face from which to read that texel. The texel should be replaced by the average of the three values of the adjacent texels in each incident face. However, implementations may replace the cube map corner texel by other methods, subject to the constraint that if the three available samples have the same value, the replacement texel also has that value.

Sparse Validation

If the texel reads from an unbound region of a sparse image, the texel is a sparse unbound texel, and processing continues with texel replacement.

Layout Validation

If all planes of a disjoint multi-planar image are not in the same image layout when the image is sampled with sampler Y’CBCR conversion, the result of texel reads is undefined.

15.3.2. Format Conversion

Texels undergo a format conversion from the VkFormat of the image view to a vector of either floating point or signed or unsigned integer components, with the number of components based on the number of components present in the format.

  • Color formats have one, two, three, or four components, according to the format.

  • Depth/stencil formats are one component. The depth or stencil component is selected by the aspectMask of the image view.

Each component is converted based on its type and size (as defined in the Format Definition section for each VkFormat), using the appropriate equations in 16-Bit Floating-Point Numbers, Unsigned 11-Bit Floating-Point Numbers, Unsigned 10-Bit Floating-Point Numbers, Fixed-Point Data Conversion, and Shared Exponent to RGB. Signed integer components smaller than 32 bits are sign-extended.

If the image view format is sRGB, the color components are first converted as if they are UNORM, and then sRGB to linear conversion is applied to the R, G, and B components as described in the “sRGB EOTF” section of the Khronos Data Format Specification. The A component, if present, is unchanged.

If the image view format is block-compressed, then the texel value is first decoded, then converted based on the type and number of components defined by the compressed format.

15.3.3. Texel Replacement

A texel is replaced if it is one (and only one) of:

  • a border texel,

  • an invalid texel, or

  • a sparse unbound texel.

Border texels are replaced with a value based on the image format and the borderColor of the sampler. The border color is:

Table 22. Border Color B
Sampler borderColor Corresponding Border Color

VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK

B = (0.0, 0.0, 0.0, 0.0)

VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK

B = (0.0, 0.0, 0.0, 1.0)

VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE

B = (1.0, 1.0, 1.0, 1.0)

VK_BORDER_COLOR_INT_TRANSPARENT_BLACK

B = (0, 0, 0, 0)

VK_BORDER_COLOR_INT_OPAQUE_BLACK

B = (0, 0, 0, 1)

VK_BORDER_COLOR_INT_OPAQUE_WHITE

B = (1, 1, 1, 1)

Note

The names VK_BORDER_COLOR_*_TRANSPARENT_BLACK, VK_BORDER_COLOR_*_OPAQUE_BLACK, and VK_BORDER_COLOR_*_OPAQUE_WHITE are meant to describe which components are zeros and ones in the vocabulary of compositing, and are not meant to imply that the numerical value of VK_BORDER_COLOR_INT_OPAQUE_WHITE is a saturating value for integers.

This is substituted for the texel value by replacing the number of components in the image format

Table 23. Border Texel Components After Replacement
Texel Aspect or Format Component Assignment

Depth aspect

D = Br

Stencil aspect

S = Br

One component color format

Cr = Br

Two component color format

Crg = (Br,Bg)

Three component color format

Crgb = (Br,Bg,Bb)

Four component color format

Crgba = (Br,Bg,Bb,Ba)

The value returned by a read of an invalid texel is undefined, unless that read operation is from a buffer resource and the robustBufferAccess feature is enabled. In that case, an invalid texel is replaced as described by the robustBufferAccess feature.

If the VkPhysicalDeviceSparseProperties::residencyNonResidentStrict property is VK_TRUE, a sparse unbound texel is replaced with 0 or 0.0 values for integer and floating-point components of the image format, respectively.

If residencyNonResidentStrict is VK_FALSE, the value of the sparse unbound texel is undefined.

15.3.4. Depth Compare Operation

If the image view has a depth/stencil format, the depth component is selected by the aspectMask, and the operation is a Dref instruction, a depth comparison is performed. The value of the result D is 1.0 if the result of the compare operation is true, and 0.0 otherwise. The compare operation is selected by the compareOp member of the sampler.

\[\begin{aligned} D & = 1.0 & \begin{cases} D_{\textit{ref}} \leq D & \text{for LEQUAL} \\ D_{\textit{ref}} \geq D & \text{for GEQUAL} \\ D_{\textit{ref}} < D & \text{for LESS} \\ D_{\textit{ref}} > D & \text{for GREATER} \\ D_{\textit{ref}} = D & \text{for EQUAL} \\ D_{\textit{ref}} \neq D & \text{for NOTEQUAL} \\ \textit{true} & \text{for ALWAYS} \\ \textit{false} & \text{for NEVER} \end{cases} \\ D & = 0.0 & \text{otherwise} \end{aligned}\]

where, in the depth comparison:

Dref = shaderOp.Dref (from optional SPIR-V operand)

D (texel depth value)

15.3.5. Conversion to RGBA

The texel is expanded from one, two, or three to four components based on the image base color:

Table 24. Texel Color After Conversion To RGBA
Texel Aspect or Format RGBA Color

Depth aspect

Crgba = (D,0,0,one)

Stencil aspect

Crgba = (S,0,0,one)

One component color format

Crgba = (Cr,0,0,one)

Two component color format

Crgba = (Crg,0,one)

Three component color format

Crgba = (Crgb,one)

Four component color format

Crgba = Crgba

where one = 1.0f for floating-point formats and depth aspects, and one = 1 for integer formats and stencil aspects.

15.3.6. Component Swizzle

All texel input instructions apply a swizzle based on:

The swizzle can rearrange the components of the texel, or substitute zero and one for any components. It is defined as follows for the R component, and operates similarly for the other components.

\[\begin{aligned} C'_{rgba}[R] & = \begin{cases} C_{rgba}[R] & \text{for RED swizzle} \\ C_{rgba}[G] & \text{for GREEN swizzle} \\ C_{rgba}[B] & \text{for BLUE swizzle} \\ C_{rgba}[A] & \text{for ALPHA swizzle} \\ 0 & \text{for ZERO swizzle} \\ one & \text{for ONE swizzle} \\ C_{rgba}[R] & \text{for IDENTITY swizzle} \end{cases} \end{aligned}\]

where:

\[\begin{aligned} C_{rgba}[R] & \text{is the RED component} \\ C_{rgba}[G] & \text{is the GREEN component} \\ C_{rgba}[B] & \text{is the BLUE component} \\ C_{rgba}[A] & \text{is the ALPHA component} \\ one & = 1.0\text{f} & \text{for floating point components} \\ one & = 1 & \text{for integer components} \end{aligned}\]

For each component this is applied to, the VK_COMPONENT_SWIZZLE_IDENTITY swizzle selects the corresponding component from Crgba.

If the border color is one of the VK_BORDER_COLOR_*_OPAQUE_BLACK enums and the VkComponentSwizzle is not VK_COMPONENT_SWIZZLE_IDENTITY for all components (or the equivalent identity mapping), the value of the texel after swizzle is undefined.

15.3.7. Sparse Residency

OpImageSparse* instructions return a structure which includes a residency code indicating whether any texels accessed by the instruction are sparse unbound texels. This code can be interpreted by the OpImageSparseTexelsResident instruction which converts the residency code to a boolean value.

15.3.8. Chroma Reconstruction

In some color models, the color representation is defined in terms of monochromatic light intensity (often called “luma”) and color differences relative to this intensity, often called “chroma”. It is common for color models other than RGB to represent the chroma channels at lower spatial resolution than the luma channel. This approach is used to take advantage of the eye’s lower spatial sensitivity to color compared with its sensitivity to brightness. Less commonly, the same approach is used with additive color, since the green channel dominates the eye’s sensitivity to light intensity and the spatial sensitivity to color introduced by red and blue is lower.

Lower-resolution channels are “downsampled” by resizing them to a lower spatial resolution than the channel representing luminance. The process of reconstructing a full color value for texture access involves accessing both chroma and luma values at the same location. To generate the color accurately, the values of the lower-resolution channels at the location of the luma samples must be reconstructed from the lower-resolution sample locations, an operation known here as “chroma reconstruction” irrespective of the actual color model.

The location of the chroma samples relative to the luma coordinates is determined by the xChromaOffset and yChromaOffset members of the VkSamplerYcbcrConversionCreateInfo structure used to create the sampler Y’CBCR conversion.

The following diagrams show the relationship between unnormalized (u,v) coordinates and (i,j) integer texel positions in the luma channel (shown in black, with circles showing integer sample positions) and the texel coordinates of reduced-resolution chroma channels, shown as crosses in red.

Note

If the chroma values are reconstructed at the locations of the luma samples by means of interpolation, chroma samples from outside the image bounds are needed; these are determined according to Wrapping Operation. These diagrams represent this by showing the bounds of the “chroma texel” extending beyond the image bounds, and including additional chroma sample positions where required for interpolation. The limits of a sample for NEAREST sampling is shown as a grid.

image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t 0,2 1,2 2,2 3,2 0,1 1,1 2,1 3,1 0,0 1,0 2,0 3,0 0,3 1,3 2,3 3,3
Figure 5. 422 downsampling, xChromaOffset=COSITED_EVEN
image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t 0,2 1,2 2,2 3,2 0,1 1,1 2,1 3,1 0,0 1,0 2,0 3,0 0,3 1,3 2,3 3,3
Figure 6. 422 downsampling, xChromaOffset=MIDPOINT
image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t 0,1 1,1 2,1 3,1 0,0 1,0 2,0 3,0
Figure 7. 420 downsampling, xChromaOffset=COSITED_EVEN, yChromaOffset=COSITED_EVEN
image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t 0,1 1,1 2,1 3,1 0,0 1,0 2,0 3,0
Figure 8. 420 downsampling, xChromaOffset=MIDPOINT, yChromaOffset=COSITED_EVEN
image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t 0,1 1,1 2,1 3,1 0,0 1,0 2,0 3,0
Figure 9. 420 downsampling, xChromaOffset=COSITED_EVEN, yChromaOffset=MIDPOINT
image/svg+xml 3 2 1 0 0 1 2 3 4 5 6 7 j i 0.0 8.0 u 0.0 1.0 s 4.0 0.0 v 1.0 0.0 t 0,1 1,1 2,1 3,1 0,0 1,0 2,0 3,0
Figure 10. 420 downsampling, xChromaOffset=MIDPOINT, yChromaOffset=MIDPOINT

Reconstruction is implemented in one of two ways:

If the format of the image that is to be sampled sets VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_CHROMA_RECONSTRUCTION_EXPLICIT_BIT, or the VkSamplerYcbcrConversionCreateInfo’s forceExplicitReconstruction is set to VK_TRUE, reconstruction is performed as an explicit step independent of filtering, described in the Explicit Reconstruction section.

If the format of the image that is to be sampled does not set VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_CHROMA_RECONSTRUCTION_EXPLICIT_BIT and if the VkSamplerYcbcrConversionCreateInfo’s forceExplicitReconstruction is set to VK_FALSE, reconstruction is performed as an implicit part of filtering prior to color model conversion, with no separate post-conversion texel filtering step, as described in the Implicit Reconstruction section.

Explicit Reconstruction
  • If the chromaFilter member of the VkSamplerYcbcrConversionCreateInfo structure is VK_FILTER_NEAREST:

    • If the format’s R and B channels are reduced in resolution in just width by a factor of two relative to the G channel (i.e. this is a “_422” format), the \(\tau_{ijk}[level]\) values accessed by texel filtering are reconstructed as follows:

      \[\begin{aligned} \tau_R'(i, j) & = \tau_R(\lfloor{i\times 0.5}\rfloor, j)[level] \\ \tau_B'(i, j) & = \tau_B(\lfloor{i\times 0.5}\rfloor, j)[level] \end{aligned}\]
    • If the format’s R and B channels are reduced in resolution in width and height by a factor of two relative to the G channel (i.e. this is a “_420” format), the \(\tau_{ijk}[level]\) values accessed by texel filtering are reconstructed as follows:

      \[\begin{aligned} \tau_R'(i, j) & = \tau_R(\lfloor{i\times 0.5}\rfloor, \lfloor{j\times 0.5}\rfloor)[level] \\ \tau_B'(i, j) & = \tau_B(\lfloor{i\times 0.5}\rfloor, \lfloor{j\times 0.5}\rfloor)[level] \end{aligned}\]
      Note

      xChromaOffset and yChromaOffset have no effect if chromaFilter is VK_FILTER_NEAREST for explicit reconstruction.

  • If the chromaFilter member of the VkSamplerYcbcrConversionCreateInfo structure is VK_FILTER_LINEAR:

    • If the format’s R and B channels are reduced in resolution in just width by a factor of two relative to the G channel (i.e. this is a “422” format):

      • If xChromaOffset is VK_CHROMA_LOCATION_COSITED_EVEN:

        \[\tau_{RB}'(i,j) = \begin{cases} \tau_{RB}(\lfloor{i\times 0.5}\rfloor,j)[level], & 0.5 \times i = \lfloor{0.5 \times i}\rfloor\\ 0.5\times\tau_{RB}(\lfloor{i\times 0.5}\rfloor,j)[level] + \\ 0.5\times\tau_{RB}(\lfloor{i\times 0.5}\rfloor + 1,j)[level], & 0.5 \times i \neq \lfloor{0.5 \times i}\rfloor \end{cases}\]
      • If xChromaOffset is VK_CHROMA_LOCATION_MIDPOINT:

        \[\tau_{RB}(i,j)' = \begin{cases} 0.25 \times \tau_{RB}(\lfloor{i\times 0.5}\rfloor - 1,j)[level] + \\ 0.75 \times \tau_{RB}(\lfloor{i\times 0.5}\rfloor,j)[level], & 0.5 \times i = \lfloor{0.5 \times i}\rfloor\\ 0.75 \times \tau_{RB}(\lfloor{i\times 0.5}\rfloor,j)[level] + \\ 0.25 \times \tau_{RB}(\lfloor{i\times 0.5}\rfloor + 1,j)[level], & 0.5 \times i \neq \lfloor{0.5 \times i}\rfloor \end{cases}\]
    • If the format’s R and B channels are reduced in resolution in width and height by a factor of two relative to the G channel (i.e. this is a “420” format), a similar relationship applies. Due to the number of options, these formulae are expressed more concisely as follows:

      xChromaOffset δi

      COSITED_EVEN

      0

      MIDPOINT

      0.5

      yChromaOffset δj

      COSITED_EVEN

      0

      MIDPOINT

      0.5

      \[\begin{aligned} \tau_{RB}'(i,j) = &\\ &\tau_{RB}(\lfloor 0.5\times(i-\delta_i)\rfloor, \lfloor 0.5\times(j-\delta_j)\rfloor)[level] && \times (1 - (0.5\times(i-\delta_i) - \lfloor 0.5\times(i-\delta_i)\rfloor)) && \times (1 - (0.5\times(j-\delta_j) - \lfloor 0.5\times(j-\delta_j)\rfloor)) +\\ &\tau_{RB}(1+\lfloor 0.5\times(i-\delta_i)\rfloor, \lfloor 0.5\times(j-\delta_j)\rfloor)[level] && \times (0.5\times(i-\delta_i) - \lfloor 0.5\times(i-\delta_i)\rfloor) && \times (1 - (0.5\times(j-\delta_j) - \lfloor 0.5\times(j-\delta_j)\rfloor)) +\\ &\tau_{RB}(\lfloor 0.5\times(i-\delta_i)\rfloor, 1+\lfloor 0.5\times(j-\delta_j)\rfloor)[level] && \times (1 - (0.5\times(i-\delta_i) - \lfloor 0.5\times(i-\delta_i)\rfloor)) && \times (0.5\times(j-\delta_j) - \lfloor 0.5\times(j-\delta_j)\rfloor) +\\ &\tau_{RB}(1+\lfloor 0.5\times(i-\delta_i)\rfloor, 1+\lfloor 0.5\times(j-\delta_j)\rfloor)[level] && \times (0.5\times(i-\delta_i) - \lfloor 0.5\times(i-\delta_i)\rfloor) && \times (0.5\times(j-\delta_j) - \lfloor 0.5\times(j-\delta_j)\rfloor) \end{aligned}\]
Note

In the case where the texture itself is bilinearly interpolated as described in Texel Filtering, thus requiring four full-color samples for the filtering operation, and where the reconstruction of these samples uses bilinear interpolation in the chroma channels due to chromaFilter=VK_FILTER_LINEAR, up to nine chroma samples may be required, depending on the sample location.

Implicit Reconstruction

Implicit reconstruction takes place by the samples being interpolated, as required by the filter settings of the sampler, except that chromaFilter takes precedence for the chroma samples.

If chromaFilter is VK_FILTER_NEAREST, an implementation may behave as if xChromaOffset and yChromaOffset were both VK_CHROMA_LOCATION_MIDPOINT, irrespective of the values set.

Note

This will not have any visible effect if the locations of the luma samples coincide with the location of the samples used for rasterization.

The sample coordinates are adjusted by the downsample factor of the channel (such that, for example, the sample coordinates are divided by two if the channel has a downsample factor of two relative to the luma channel):

\[\begin{aligned} u_{RB}' (422/420) &= \begin{cases} 0.5\times (u + 0.5), & \textrm{xChromaOffset = COSITED}\_\textrm{EVEN} \\ 0.5\times u, & \textrm{xChromaOffset = MIDPOINT} \end{cases} \\ v_{RB}' (420) &= \begin{cases} 0.5\times (v + 0.5), & \textrm{yChromaOffset = COSITED}\_\textrm{EVEN} \\ 0.5\times v, & \textrm{yChromaOffset = MIDPOINT} \end{cases} \end{aligned}\]

15.3.9. Sampler Y’CBCR Conversion

Sampler Y’CBCR conversion performs the following operations, which an implementation may combine into a single mathematical operation:

Sampler Y’CBCR Range Expansion

Sampler Y’CBCR range expansion is applied to color channel values after all texel input operations which are not specific to sampler Y’CBCR conversion. For example, the input values to this stage have been converted using the normal format conversion rules.

Sampler Y’CBCR range expansion is not applied if ycbcrModel is VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY. That is, the shader receives the vector C'rgba as output by the Component Swizzle stage without further modification.

For other values of ycbcrModel, range expansion is applied to the texel channel values output by the Component Swizzle defined by the components member of VkSamplerYcbcrConversionCreateInfo. Range expansion applies independently to each channel of the image. For the purposes of range expansion and Y’CBCR model conversion, the R and B channels contain color difference (chroma) values and the G channel contains luma. The A channel is not modified by sampler Y’CBCR range expansion.

The range expansion to be applied is defined by the ycbcrRange member of the VkSamplerYcbcrConversionCreateInfo structure:

  • If ycbcrRange is VK_SAMPLER_YCBCR_RANGE_ITU_FULL, the following transformations are applied:

    \[\begin{aligned} Y' &= C'_{rgba}[G] \\ C_B &= C'_{rgba}[B] - {{2^{(n-1)}}\over{(2^n) - 1}} \\ C_R &= C'_{rgba}[R] - {{2^{(n-1)}}\over{(2^n) - 1}} \end{aligned}\]
    Note

    These formulae correspond to the “full range” encoding in the Khronos Data Format Specification.

    Should any future amendments be made to the ITU specifications from which these equations are derived, the formulae used by Vulkan may also be updated to maintain parity.

  • If ycbcrRange is VK_SAMPLER_YCBCR_RANGE_ITU_NARROW, the following transformations are applied:

    \[\begin{aligned} Y' &= {{C'_{rgba}[G] \times (2^n-1) - 16\times 2^{n-8}}\over{219\times 2^{n-8}}} \\ C_B &= {{C'_{rgba}[B] \times \left(2^n-1\right) - 128\times 2^{n-8}}\over{224\times 2^{n-8}}} \\ C_R &= {{C'_{rgba}[R] \times \left(2^n-1\right) - 128\times 2^{n-8}}\over{224\times 2^{n-8}}} \end{aligned}\]
    Note

    These formulae correspond to the “narrow range” encoding in the Khronos Data Format Specification.

  • n is the bit-depth of the channels in the format.

The precision of the operations performed during range expansion must be at least that of the source format.

An implementation may clamp the results of these range expansion operations such that Y' falls in the range [0,1], and/or such that CB and CR fall in the range [-0.5,0.5].

Sampler Y’CBCR Model Conversion

The range-expanded values are converted between color models, according to the color model conversion specified in the ycbcrModel member:

VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY

The color channels are not modified by the color model conversion since they are assumed already to represent the desired color model in which the shader is operating; Y’CBCR range expansion is also ignored.

VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY

The color channels are not modified by the color model conversion and are assumed to be treated as though in Y’CBCR form both in memory and in the shader; Y’CBCR range expansion is applied to the channels as for other Y’CBCR models, with the vector (CR,Y',CB,A) provided to the shader.

VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709

The color channels are transformed from a Y’CBCR representation to an R’G’B' representation as described in the “BT.709 Y’CBCR conversion” section of the Khronos Data Format Specification.

VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601

The color channels are transformed from a Y’CBCR representation to an R’G’B' representation as described in the “BT.601 Y’CBCR conversion” section of the Khronos Data Format Specification.

VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020

The color channels are transformed from a Y’CBCR representation to an R’G’B' representation as described in the “BT.2020 Y’CBCR conversion” section of the Khronos Data Format Specification.

In this operation, each output channel is dependent on each input channel.

An implementation may clamp the R’G’B' results of these conversions to the range [0,1].

The precision of the operations performed during model conversion must be at least that of the source format.

The alpha channel is not modified by these model conversions.

Note

Sampling operations in a non-linear color space can introduce color and intensity shifts at sharp transition boundaries. To avoid this issue, the technically precise color correction sequence described in the “Introduction to Color Conversions” chapter of the Khronos Data Format Specification may be performed as follows:

The additional calculations and, especially, additional number of sampling operations in the VK_FILTER_LINEAR case can be expected to have a performance impact compared with using the outputs directly; since the variation from “correct” results are subtle for most content, the application author should determine whether a more costly implementation is strictly necessary. Note that if chromaFilter and minFilter/magFilter are both VK_FILTER_NEAREST, these operations are redundant and sampling using sampler Y’CBCR conversion at the desired sample coordinates will produce the “correct” results without further processing.

15.4. Texel Output Operations

Texel output instructions are SPIR-V image instructions that write to an image. Texel output operations are a set of steps that are performed on state, coordinates, and texel values while processing a texel output instruction, and which are common to some or all texel output instructions. They include the following steps, which are performed in the listed order:

15.4.1. Texel Output Validation Operations

Texel output validation operations inspect instruction/image state or coordinates, and in certain circumstances cause the write to have no effect. There are a series of validations that the texel undergoes.

Texel Format Validation

If the image format of the OpTypeImage is not compatible with the VkImageView’s format, the effect of the write on the image view’s memory is undefined, but the write must not access memory outside of the image view.

15.4.2. Integer Texel Coordinate Validation

The integer texel coordinates are validated according to the same rules as for texel input coordinate validation.

If the texel fails integer texel coordinate validation, then the write has no effect.

15.4.3. Sparse Texel Operation

If the texel attempts to write to an unbound region of a sparse image, the texel is a sparse unbound texel. In such a case, if the VkPhysicalDeviceSparseProperties::residencyNonResidentStrict property is VK_TRUE, the sparse unbound texel write has no effect. If residencyNonResidentStrict is VK_FALSE, the write may have a side effect that becomes visible to other accesses to unbound texels in any resource, but will not be visible to any device memory allocated by the application.

15.4.4. Texel Output Format Conversion

If the image format is sRGB, a linear to sRGB conversion is applied to the R, G, and B components as described in the “sRGB EOTF” section of the Khronos Data Format Specification. The A component, if present, is unchanged.

Texels then undergo a format conversion from the floating point, signed, or unsigned integer type of the texel data to the VkFormat of the image view. Any unused components are ignored.

Each component is converted based on its type and size (as defined in the Format Definition section for each VkFormat). Floating-point outputs are converted as described in Floating-Point Format Conversions and Fixed-Point Data Conversion. Integer outputs are converted such that their value is preserved. The converted value of any integer that cannot be represented in the target format is undefined.

15.5. Derivative Operations

SPIR-V derivative instructions include OpDPdx, OpDPdy, OpDPdxFine, OpDPdyFine, OpDPdxCoarse, and OpDPdyCoarse. Derivative instructions are only available in compute and fragment shaders.

image/svg+xml s t dPdx 0 dPdx 1 dPdy 0 dPdy 1
Figure 11. Implicit Derivatives

Derivatives are computed as if there is a 2×2 neighborhood of fragments for each fragment shader invocation. These neighboring fragments are used to compute derivatives with the assumption that the values of P in the neighborhood are piecewise linear. It is further assumed that the values of P in the neighborhood are locally continuous, therefore derivatives in non-uniform control flow are undefined.

\[\begin{aligned} dPdx_{i_1,j_0} & = dPdx_{i_0,j_0} & = P_{i_1,j_0} - P_{i_0,j_0} \\ dPdx_{i_1,j_1} & = dPdx_{i_0,j_1} & = P_{i_1,j_1} - P_{i_0,j_1} \\ \\ dPdy_{i_0,j_1} & = dPdy_{i_0,j_0} & = P_{i_0,j_1} - P_{i_0,j_0} \\ dPdy_{i_1,j_1} & = dPdy_{i_1,j_0} & = P_{i_1,j_1} - P_{i_1,j_0} \end{aligned}\]

The Fine derivative instructions must return the values above, for a group of fragments in a 2×2 neighborhood. Coarse derivatives may return only two values. In this case, the values should be:

\[\begin{aligned} dPdx & = \begin{cases} dPdx_{i_0,j_0} & \text{preferred}\\ dPdx_{i_0,j_1} \end{cases} \\ dPdy & = \begin{cases} dPdy_{i_0,j_0} & \text{preferred}\\ dPdy_{i_1,j_0} \end{cases} \end{aligned}\]

OpDPdx and OpDPdy must return the same result as either OpDPdxFine or OpDPdxCoarse and either OpDPdyFine or OpDPdyCoarse, respectively. Implementations must make the same choice of either coarse or fine for both OpDPdx and OpDPdy, and implementations should make the choice that is more efficient to compute.

15.5.1. Compute Shader Derivatives

For compute shaders, derivatives are also evaluated using a 2×2 logical neighborhood of compute shader invocations. Compute shader invocations are arranged into neighborhoods according to one of two SPIR-V execution modes. For the DerivativeGroupQuadsNV execution mode, each neighborhood is assembled from a 2×2×1 region of invocations based on the LocalInvocationId built-in. For the DerivativeGroupLinearNV execution mode, each neighborhood is assembled from a group of four invocations based on the LocalInvocationIndex built-in. The Compute shader derivative group assignments table specifies the LocalInvocationId or LocalInvocationIndex values for the four values of P in each neighborhood, where x and y are per-neighborhood integer values.

Table 25. Compute shader derivative group assignments
Value DerivativeGroupQuadsNV DerivativeGroupLinearNV

Pi0,j0

(2x + 0, 2y + 0, z)

4x + 0

Pi1,j0

(2x + 1, 2y + 0, z)

4x + 1

Pi0,j1

(2x + 0, 2y + 1, z)

4x + 2

Pi1,j1

(2x + 1, 2y + 1, z)

4x + 3

For multi-planar formats, the derivatives are computed based on the plane with the largest dimensions.

15.6. Normalized Texel Coordinate Operations

If the image sampler instruction provides normalized texel coordinates, some of the following operations are performed.

15.6.1. Projection Operation

For Proj image operations, the normalized texel coordinates (s,t,r,q,a) and (if present) the Dref coordinate are transformed as follows:

\[\begin{aligned} s & = \frac{s}{q}, & \text{for 1D, 2D, or 3D image} \\ \\ t & = \frac{t}{q}, & \text{for 2D or 3D image} \\ \\ r & = \frac{r}{q}, & \text{for 3D image} \\ \\ D_{\textit{ref}} & = \frac{D_{\textit{ref}}}{q}, & \text{if provided} \end{aligned}\]

15.6.2. Derivative Image Operations

Derivatives are used for LOD selection. These derivatives are either implicit (in an ImplicitLod image instruction in a fragment shader) or explicit (provided explicitly by shader to the image instruction in any shader).

For implicit derivatives image instructions, the derivatives of texel coordinates are calculated in the same manner as derivative operations above. That is:

\[\begin{aligned} \partial{s}/\partial{x} & = dPdx(s), & \partial{s}/\partial{y} & = dPdy(s), & \text{for 1D, 2D, Cube, or 3D image} \\ \partial{t}/\partial{x} & = dPdx(t), & \partial{t}/\partial{y} & = dPdy(t), & \text{for 2D, Cube, or 3D image} \\ \partial{u}/\partial{x} & = dPdx(u), & \partial{u}/\partial{y} & = dPdy(u), & \text{for Cube or 3D image} \end{aligned}\]

Partial derivatives not defined above for certain image dimensionalities are set to zero.

For explicit LOD image instructions, if the optional SPIR-V operand Grad is provided, then the operand values are used for the derivatives. The number of components present in each derivative for a given image dimensionality matches the number of partial derivatives computed above.

If the optional SPIR-V operand Lod is provided, then derivatives are set to zero, the cube map derivative transformation is skipped, and the scale factor operation is skipped. Instead, the floating point scalar coordinate is directly assigned to λbase as described in Level-of-Detail Operation.

For implicit derivative image instructions, the partial derivative values may be computed by linear approximation using a 2×2 neighborhood of shader invocations (known as a quad), as described above. If the instruction is in control flow that is not uniform across the quad, then the derivative values and hence the implicit LOD values are undefined.

If the image or sampler object used by an implicit derivative image instruction is not uniform across the quad and quadDivergentImplicitLod is not supported, then the derivative and LOD values are undefined. Implicit derivatives are well-defined when the image and sampler and control flow are uniform across the quad, even if they diverge between different quads.

If quadDivergentImplicitLod is supported, then derivatives and implicit LOD values are well-defined even if the image or sampler object are not uniform within a quad. The derivatives are computed as specified above, and the implicit LOD calculation proceeds for each shader invocation using its respective image and sampler object.

For the purposes of implicit derivatives, Flat fragment input variables are uniform within a quad.

15.6.3. Cube Map Face Selection and Transformations

For cube map image instructions, the (s,t,r) coordinates are treated as a direction vector (rx,ry,rz). The direction vector is used to select a cube map face. The direction vector is transformed to a per-face texel coordinate system (sface,tface), The direction vector is also used to transform the derivatives to per-face derivatives.

15.6.4. Cube Map Face Selection

The direction vector selects one of the cube map’s faces based on the largest magnitude coordinate direction (the major axis direction). Since two or more coordinates can have identical magnitude, the implementation must have rules to disambiguate this situation.

The rules should have as the first rule that rz wins over ry and rx, and the second rule that ry wins over rx. An implementation may choose other rules, but the rules must be deterministic and depend only on (rx,ry,rz).

The layer number (corresponding to a cube map face), the coordinate selections for sc, tc, rc, and the selection of derivatives, are determined by the major axis direction as specified in the following two tables.

Table 26. Cube map face and coordinate selection
Major Axis Direction Layer Number Cube Map Face sc tc rc

+rx

0

Positive X

-rz

-ry

rx

-rx

1

Negative X

+rz

-ry

rx

+ry

2

Positive Y

+rx

+rz

ry

-ry

3

Negative Y

+rx

-rz

ry

+rz

4

Positive Z

+rx

-ry

rz

-rz

5

Negative Z

-rx

-ry

rz

Table 27. Cube map derivative selection
Major Axis Direction ∂sc / ∂x ∂sc / ∂y ∂tc / ∂x ∂tc / ∂y ∂rc / ∂x ∂rc / ∂y

+rx

-∂rz / ∂x

-∂rz / ∂y

-∂ry / ∂x

-∂ry / ∂y

+∂rx / ∂x

+∂rx / ∂y

-rx

+∂rz / ∂x

+∂rz / ∂y

-∂ry / ∂x

-∂ry / ∂y

-∂rx / ∂x

-∂rx / ∂y

+ry

+∂rx / ∂x

+∂rx / ∂y

+∂rz / ∂x

+∂rz / ∂y

+∂ry / ∂x

+∂ry / ∂y

-ry

+∂rx / ∂x

+∂rx / ∂y

-∂rz / ∂x

-∂rz / ∂y

-∂ry / ∂x

-∂ry / ∂y

+rz

+∂rx / ∂x

+∂rx / ∂y

-∂ry / ∂x

-∂ry / ∂y

+∂rz / ∂x

+∂rz / ∂y

-rz

-∂rx / ∂x

-∂rx / ∂y

-∂ry / ∂x

-∂ry / ∂y

-∂rz / ∂x

-∂rz / ∂y

15.6.5. Cube Map Coordinate Transformation

\[\begin{aligned} s_{\textit{face}} & = \frac{1}{2} \times \frac{s_c}{|r_c|} + \frac{1}{2} \\ t_{\textit{face}} & = \frac{1}{2} \times \frac{t_c}{|r_c|} + \frac{1}{2} \\ \end{aligned}\]

15.6.6. Cube Map Derivative Transformation

\[\begin{aligned} \frac{\partial{s_{\textit{face}}}}{\partial{x}} &= \frac{\partial}{\partial{x}} \left ( \frac{1}{2} \times \frac{s_{c}}{|r_{c}|} + \frac{1}{2}\right ) \\ \frac{\partial{s_{\textit{face}}}}{\partial{x}} &= \frac{1}{2} \times \frac{\partial}{\partial{x}} \left ( \frac{s_{c}}{|r_{c}|} \right ) \\ \frac{\partial{s_{\textit{face}}}}{\partial{x}} &= \frac{1}{2} \times \left ( \frac{ |r_{c}| \times \partial{s_c}/\partial{x} -s_c \times {\partial{r_{c}}}/{\partial{x}}} {\left ( r_{c} \right )^2} \right ) \end{aligned}\]
\[\begin{aligned} \frac{\partial{s_{\textit{face}}}}{\partial{y}} &= \frac{1}{2} \times \left ( \frac{ |r_{c}| \times \partial{s_c}/\partial{y} -s_c \times {\partial{r_{c}}}/{\partial{y}}} {\left ( r_{c} \right )^2} \right )\\ \frac{\partial{t_{\textit{face}}}}{\partial{x}} &= \frac{1}{2} \times \left ( \frac{ |r_{c}| \times \partial{t_c}/\partial{x} -t_c \times {\partial{r_{c}}}/{\partial{x}}} {\left ( r_{c} \right )^2} \right ) \\ \frac{\partial{t_{\textit{face}}}}{\partial{y}} &= \frac{1}{2} \times \left ( \frac{ |r_{c}| \times \partial{t_c}/\partial{y} -t_c \times {\partial{r_{c}}}/{\partial{y}}} {\left ( r_{c} \right )^2} \right ) \end{aligned}\]

15.6.7. Scale Factor Operation, Level-of-Detail Operation and Image Level(s) Selection

LOD selection can be either explicit (provided explicitly by the image instruction) or implicit (determined from a scale factor calculated from the derivatives). The implicit LOD selected can be queried using the SPIR-V instruction OpImageQueryLod, which gives access to the λ' and dl values, defined below.

Scale Factor Operation

The magnitude of the derivatives are calculated by:

mux = |∂s/∂x| × wbase

mvx = |∂t/∂x| × hbase

mwx = |∂r/∂x| × dbase

muy = |∂s/∂y| × wbase

mvy = |∂t/∂y| × hbase

mwy = |∂r/∂y| × dbase

where:

∂t/∂x = ∂t/∂y = 0 (for 1D images)

∂r/∂x = ∂r/∂y = 0 (for 1D, 2D or Cube images)

and

wbase = image.w

hbase = image.h

dbase = image.d

(for the baseMipLevel, from the image descriptor).

For corner-sampled images, the wbase, hbase, and dbase are instead:

wbase = image.w - 1

hbase = image.h - 1

dbase = image.d - 1

A point sampled in screen space has an elliptical footprint in texture space. The minimum and maximum scale factors min, ρmax) should be the minor and major axes of this ellipse.

The scale factors ρx and ρy, calculated from the magnitude of the derivatives in x and y, are used to compute the minimum and maximum scale factors.

ρx and ρy may be approximated with functions fx and fy, subject to the following constraints:

\[\begin{aligned} & f_x \text{\ is\ continuous\ and\ monotonically\ increasing\ in\ each\ of\ } m_{ux}, m_{vx}, \text{\ and\ } m_{wx} \\ & f_y \text{\ is\ continuous\ and\ monotonically\ increasing\ in\ each\ of\ } m_{uy}, m_{vy}, \text{\ and\ } m_{wy} \end{aligned}\]
\[\begin{aligned} \max(|m_{ux}|, |m_{vx}|, |m_{wx}|) \leq f_{x} \leq \sqrt{2} (|m_{ux}| + |m_{vx}| + |m_{wx}|) \\ \max(|m_{uy}|, |m_{vy}|, |m_{wy}|) \leq f_{y} \leq \sqrt{2} (|m_{uy}| + |m_{vy}| + |m_{wy}|) \end{aligned}\]

The minimum and maximum scale factors minmax) are determined by:

ρmax = max(ρx, ρy)

ρmin = min(ρx, ρy)

The ratio of anisotropy is determined by:

η = min(ρmaxmin, maxAniso)

where:

sampler.maxAniso = maxAnisotropy (from sampler descriptor)

limits.maxAniso = maxSamplerAnisotropy (from physical device limits)

maxAniso = min(sampler.maxAniso, limits.maxAniso)

If ρmax = ρmin = 0, then all the partial derivatives are zero, the fragment’s footprint in texel space is a point, and N should be treated as 1. If ρmax ≠ 0 and ρmin = 0 then all partial derivatives along one axis are zero, the fragment’s footprint in texel space is a line segment, and η should be treated as maxAniso. However, anytime the footprint is small in texel space the implementation may use a smaller value of η, even when ρmin is zero or close to zero. If either VkPhysicalDeviceFeatures::samplerAnisotropy or VkSamplerCreateInfo::anisotropyEnable are VK_FALSE, maxAniso is set to 1.

If η = 1, sampling is isotropic. If η > 1, sampling is anisotropic.

The sampling rate (N) is derived as:

N = ⌈η⌉

An implementation may round N up to the nearest supported sampling rate. An implementation may use the value of N as an approximation of η.

Level-of-Detail Operation

The LOD parameter λ is computed as follows:

\[\begin{aligned} \lambda_{base}(x,y) & = \begin{cases} shaderOp.Lod & \text{(from optional SPIR-V operand)} \\ \log_2 \left ( \frac{\rho_{max}}{\eta} \right ) & \text{otherwise} \end{cases} \\ \lambda'(x,y) & = \lambda_{base} + \mathbin{clamp}(sampler.bias + shaderOp.bias,-maxSamplerLodBias,maxSamplerLodBias) \\ \lambda & = \begin{cases} lod_{max}, & \lambda' > lod_{max} \\ \lambda', & lod_{min} \leq \lambda' \leq lod_{max} \\ lod_{min}, & \lambda' < lod_{min} \\ \textit{undefined}, & lod_{min} > lod_{max} \end{cases} \end{aligned}\]

where:

\[\begin{aligned} sampler.bias & = mipLodBias & \text{(from sampler descriptor)} \\ shaderOp.bias & = \begin{cases} Bias & \text{(from optional SPIR-V operand)} \\ 0 & \text{otherwise} \end{cases} \\ sampler.lod_{min} & = minLod & \text{(from sampler descriptor)} \\ shaderOp.lod_{min} & = \begin{cases} MinLod & \text{(from optional SPIR-V operand)} \\ 0 & \text{otherwise} \end{cases} \\ \\ lod_{min} & = \max(sampler.lod_{min}, shaderOp.lod_{min}) \\ lod_{max} & = maxLod & \text{(from sampler descriptor)} \end{aligned}\]

and maxSamplerLodBias is the value of the VkPhysicalDeviceLimits feature maxSamplerLodBias.

Image Level(s) Selection

The image level(s) d, dhi, and dlo which texels are read from are determined by an image-level parameter dl, which is computed based on the LOD parameter, as follows:

\[\begin{aligned} d_{l} = \begin{cases} nearest(d'), & \text{mipmapMode is VK\_SAMPLER\_MIPMAP\_MODE\_NEAREST} \\ d', & \text{otherwise} \end{cases} \end{aligned}\]

where:

\[\begin{aligned} d' = level_{base} + \text{clamp}(\lambda, 0, q) \end{aligned}\]
\[\begin{aligned} nearest(d') & = \begin{cases} \left \lceil d' + 0.5\right \rceil - 1, & \text{preferred} \\ \left \lfloor d' + 0.5\right \rfloor, & \text{alternative} \end{cases} \end{aligned}\]

and

levelbase = baseMipLevel

q = levelCount - 1

baseMipLevel and levelCount are taken from the subresourceRange of the image view.

If the sampler’s mipmapMode is VK_SAMPLER_MIPMAP_MODE_NEAREST, then the level selected is d = dl.

If the sampler’s mipmapMode is VK_SAMPLER_MIPMAP_MODE_LINEAR, two neighboring levels are selected:

\[\begin{aligned} d_{hi} & = \lfloor d_{l} \rfloor \\ d_{lo} & = min( d_{hi} + 1, q ) \\ \delta & = d_{l} - d_{hi} \end{aligned}\]

δ is the fractional value used for linear filtering between levels.

15.6.8. (s,t,r,q,a) to (u,v,w,a) Transformation

The normalized texel coordinates are scaled by the image level dimensions and the array layer is selected. This transformation is performed once for each level (d or dhi and dlo) used in filtering.

\[\begin{aligned} u(x,y) & = s(x,y) \times width_{scale} \\ v(x,y) & = \begin{cases} 0 & \text{for 1D images} \\ t(x,y) \times height_{scale} & \text{otherwise} \end{cases} \\ w(x,y) & = \begin{cases} 0 & \text{for 2D or Cube images} \\ r(x,y) \times depth_{scale} & \text{otherwise} \end{cases} \\ \\ a(x,y) & = \begin{cases} a(x,y) & \text{for array images} \\ 0 & \text{otherwise} \end{cases} \end{aligned}\]

where

widthscale = widthlevel

heightscale = heightlevel

depthscale = depthlevel

for conventional images , and

widthscale = widthlevel - 1

heightscale = heightlevel - 1

depthscale = depthlevel - 1

for corner-sampled images .

Operations then proceed to Unnormalized Texel Coordinate Operations.

15.7. Unnormalized Texel Coordinate Operations

15.7.1. (u,v,w,a) to (i,j,k,l,n) Transformation And Array Layer Selection

The unnormalized texel coordinates are transformed to integer texel coordinates relative to the selected mipmap level.

The layer index l is computed as:

l = clamp(RNE(a), 0, layerCount - 1) + baseArrayLayer

where layerCount is the number of layers in the image subresource range of the image view, baseArrayLayer is the first layer from the subresource range, and where:

\[\begin{aligned} \mathbin{RNE}(a) & = \begin{cases} \mathbin{roundTiesToEven}(a) & \text{preferred, from IEEE Std 754-2008 Floating-Point Arithmetic} \\ \left \lfloor a + 0.5 \right \rfloor & \text{alternative} \end{cases} \end{aligned}\]

The sample index n is assigned the value zero.

Nearest filtering (VK_FILTER_NEAREST) computes the integer texel coordinates that the unnormalized coordinates lie within:

\[\begin{aligned} i &= \lfloor u + shift \rfloor \\ j &= \lfloor v + shift \rfloor \\ k &= \lfloor w + shift \rfloor \end{aligned}\]

where

shift = 0.0

for conventional images , and

shift = 0.5

for corner-sampled images .

Linear filtering (VK_FILTER_LINEAR) computes a set of neighboring coordinates which bound the unnormalized coordinates. The integer texel coordinates are combinations of i0 or i1, j0 or j1, k0 or k1, as well as weights α, β, and γ.

\[\begin{aligned} i_0 &= \lfloor u - shift \rfloor \\ i_1 &= i_0 + 1 \\ j_0 &= \lfloor v - shift \rfloor \\ j_1 &= j_0 + 1 \\ k_0 &= \lfloor w - shift \rfloor \\ k_1 &= k_0 + 1 \\ \alpha &= \left(u - shift\right) - i_0 \\ \beta &= \left(v - shift\right) - j_0 \\ \gamma &= \left(w - shift\right) - k_0 \end{aligned}\]

where

shift = 0.5

for conventional images , and

shift = 0.0

for corner-sampled images .

Cubic filtering (VK_FILTER_CUBIC_IMG) computes a set of neighboring coordinates which bound the unnormalized coordinates. The integer texel coordinates are combinations of i0, i1, i2 or i3, j0, j1, j2 or j3, as well as weights α and β.

\[\begin{aligned} i_{0} & = \left \lfloor u - \frac{3}{2} \right \rfloor & i_{1} & = i_{0} + 1 & i_{2} & = i_{1} + 1 & i_{3} & = i_{2} + 1 \\[1em] j_{0} & = \left \lfloor v - \frac{3}{2} \right \rfloor & j_{1} & = j_{0} + 1 & j_{2} & = j_{1} + 1 & j_{3} & = j_{2} + 1 \\ \\ \alpha & = \mathbin{frac} \left ( u - \frac{1}{2} \right ) \\[1em] \beta & = \mathbin{frac} \left ( v - \frac{1}{2} \right ) \end{aligned}\]

If the image instruction includes a ConstOffset operand, the constant offsets i, Δj, Δk) are added to (i,j,k) components of the integer texel coordinates.

15.8. Integer Texel Coordinate Operations

Integer texel coordinate operations may supply a LOD which texels are to be read from or written to using the optional SPIR-V operand Lod. If the Lod is provided then it must be an integer.

The image level selected is:

\[\begin{aligned} d & = level_{base} + \begin{cases} Lod & \text{(from optional SPIR-V operand)} \\ 0 & \text{otherwise} \end{cases} \\ \end{aligned}\]

If d does not lie in the range [baseMipLevel, baseMipLevel + levelCount) then any values fetched are undefined, and any writes are discarded.

15.9. Image Sample Operations

15.9.1. Wrapping Operation

Cube images ignore the wrap modes specified in the sampler. Instead, if VK_FILTER_NEAREST is used within a mip level then VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE is used, and if VK_FILTER_LINEAR is used within a mip level then sampling at the edges is performed as described earlier in the Cube map edge handling section.

The first integer texel coordinate i is transformed based on the addressModeU parameter of the sampler.

\[\begin{aligned} i &= \begin{cases} i \bmod size & \text{for repeat} \\ (size - 1) - \mathbin{mirror} ((i \bmod (2 \times size)) - size) & \text{for mirrored repeat} \\ \mathbin{clamp}(i,0,size-1) & \text{for clamp to edge} \\ \mathbin{clamp}(i,-1,size) & \text{for clamp to border} \\ \mathbin{clamp}(\mathbin{mirror}(i),0,size-1) & \text{for mirror clamp to edge} \end{cases} \end{aligned}\]

where:

\[\begin{aligned} & \mathbin{mirror}(n) = \begin{cases} n & \text{for}\ n \geq 0 \\ -(1+n) & \text{otherwise} \end{cases} \end{aligned}\]

j (for 2D and Cube image) and k (for 3D image) are similarly transformed based on the addressModeV and addressModeW parameters of the sampler, respectively.

15.9.2. Texel Gathering

SPIR-V instructions with Gather in the name return a vector derived from a 2×2 rectangular region of texels in the base level of the image view. The rules for the VK_FILTER_LINEAR minification filter are applied to identify the four selected texels. Each texel is then converted to an RGBA value according to conversion to RGBA and then swizzled. A four-component vector is then assembled by taking the component indicated by the Component value in the instruction from the swizzled color value of the four texels:

\[\begin{aligned} \tau[R] &= \tau_{i0j1}[level_{base}][comp] \\ \tau[G] &= \tau_{i1j1}[level_{base}][comp] \\ \tau[B] &= \tau_{i1j0}[level_{base}][comp] \\ \tau[A] &= \tau_{i0j0}[level_{base}][comp] \end{aligned}\]

where:

\[\begin{aligned} \tau[level_{base}][comp] &= \begin{cases} \tau[level_{base}][R], & \text{for}\ comp = 0 \\ \tau[level_{base}][G], & \text{for}\ comp = 1 \\ \tau[level_{base}][B], & \text{for}\ comp = 2 \\ \tau[level_{base}][A], & \text{for}\ comp = 3 \end{cases}\\ comp & \,\text{from SPIR-V operand Component} \end{aligned}\]

OpImage*Gather must not be used on a sampled image with sampler Y’CBCR conversion enabled.

15.9.3. Texel Filtering

If λ is less than or equal to zero, the texture is said to be magnified, and the filter mode within a mip level is selected by the magFilter in the sampler. If λ is greater than zero, the texture is said to be minified, and the filter mode within a mip level is selected by the minFilter in the sampler.

Within a mip level, VK_FILTER_NEAREST filtering selects a single value using the (i, j, k) texel coordinates, with all texels taken from layer l.

\[\begin{aligned} \tau[level] &= \begin{cases} \tau_{ijk}[level], & \text{for 3D image} \\ \tau_{ij}[level], & \text{for 2D or Cube image} \\ \tau_{i}[level], & \text{for 1D image} \end{cases} \end{aligned}\]

Within a mip level, VK_FILTER_LINEAR filtering combines 8 (for 3D), 4 (for 2D or Cube), or 2 (for 1D) texel values, using the weights computed earlier:

\[\begin{aligned} \tau_{3D}[level] & = reduce((1-\alpha)(1-\beta)(1-\gamma),\tau_{i0j0k0}[level], \\ & \, (\alpha)(1-\beta)(1-\gamma),\tau_{i1j0k0}[level], \\ & \, (1-\alpha)(\beta)(1-\gamma),\tau_{i0j1k0}[level], \\ & \, (\alpha)(\beta)(1-\gamma),\tau_{i1j1k0}[level], \\ & \, (1-\alpha)(1-\beta)(\gamma),\tau_{i0j0k1}[level], \\ & \, (\alpha)(1-\beta)(\gamma),\tau_{i1j0k1}[level], \\ & \, (1-\alpha)(\beta)(\gamma),\tau_{i0j1k1}[level], \\ & \, (\alpha)(\beta)(\gamma),\tau_{i1j1k1}[level]) \end{aligned}\]
\[\begin{aligned} \tau_{2D}[level] & = reduce((1-\alpha)(1-\beta),\tau_{i0j0}[level], \\ & \, (\alpha)(1-\beta),\tau_{i1j0}[level], \\ & \, (1-\alpha)(\beta),\tau_{i0j1}[level], \\ & \, (\alpha)(\beta),\tau_{i1j1}[level]) \end{aligned}\]
\[\begin{aligned} \tau_{1D}[level] & = reduce((1-\alpha),\tau_{i0}[level], \\ & \, (\alpha),\tau_{i1}[level]) \end{aligned}\]
\[\begin{aligned} \tau[level] &= \begin{cases} \tau_{3D}[level], & \text{for 3D image} \\ \tau_{2D}[level], & \text{for 2D or Cube image} \\ \tau_{1D}[level], & \text{for 1D image} \end{cases} \end{aligned}\]

The function reduce() is defined to operate on pairs of weights and texel values as follows. When using linear or anisotropic filtering, the values of multiple texels are combined using a weighted average to produce a filtered texture value. However, a filtered texture value can also be produced by computing per-component minimum and maximum values over the set of texels that would normally be averaged. The VkSamplerReductionModeCreateInfoEXT::reductionMode controls the process by which multiple texels are combined to produce a filtered texture value. When set to VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE_EXT, a weighted average is computed. If the reduction mode is VK_SAMPLER_REDUCTION_MODE_MIN_EXT or VK_SAMPLER_REDUCTION_MODE_MAX_EXT, reduce() computes a component-wise minimum or maximum, respectively, of the components of the set of provided texels with non-zero weights.

Within a mip level, VK_FILTER_CUBIC_IMG filtering computes a weighted average of 16 (for 2D), or 4 (for 1D) texel values, using the weights computed during texel selection.

Catmull-Rom Spine interpolation of four points is defined by the equation:

\[\begin{aligned} cinterp(\tau_0, \tau_1, \tau_2, \tau_3, \omega) = \frac{1}{2} \begin{bmatrix}1 & \omega & \omega^2 & \omega^3 \end{bmatrix} \times \begin{bmatrix} 0 & 2 & 0 & 0 \\ -1 & 0 & 1 & 0 \\ 2 & -5 & 4 & 1 \\ -1 & 3 & -3 & 1 \end{bmatrix} \times \begin{bmatrix} \tau_0 \\ \tau_1 \\ \tau_2 \\ \tau_3 \end{bmatrix} \end{aligned}\]

Using the values calculated in texel selection, this equation is applied to the four points in 1D images. For 2D images, the this equation is evaluated first for each row, and the result is then fed back into the equation and interpolated again:

τ1D[level] = cinterp(τi0[level], τi1[level], τi2[level], τi3[level], α)

τj0[level] = cinterp(τi0j0[level], τi1j0[level], τi2j0[level], τi3j0[level], α)

τj1[level] = cinterp(τi0j1[level], τi1j1[level], τi2j1[level], τi3j1[level], α)

τj2[level] = cinterp(τi0j2[level], τi1j2[level], τi2j2[level], τi3j2[level], α)

τj3[level] = cinterp(τi0j3[level], τi1j3[level], τi2j3[level], τi3j3[level], α)

τ2D[level] = cinterp(τj0[level], τj1[level], τj2[level], τj3[level], β)

\[\begin{aligned} \tau[level] &= \begin{cases} \tau_{2D}[level], & \text{for 2D image} \\ \tau_{1D}[level], & \text{for 1D image} \end{cases} \end{aligned}\]

Finally, mipmap filtering either selects a value from one mip level or computes a weighted average between neighboring mip levels:

\[\begin{aligned} \tau &= \begin{cases} \tau[d], & \text{for mip mode BASE or NEAREST} \\ reduce((1-\delta),\tau[d_{hi}],\delta,\tau[d_{lo}]), & \text{for mip mode LINEAR} \end{cases} \end{aligned}\]

15.9.4. Texel Anisotropic Filtering

Anisotropic filtering is enabled by the anisotropyEnable in the sampler. When enabled, the image filtering scheme accounts for a degree of anisotropy.

The particular scheme for anisotropic texture filtering is implementation dependent. Implementations should consider the magFilter, minFilter and mipmapMode of the sampler to control the specifics of the anisotropic filtering scheme used. In addition, implementations should consider minLod and maxLod of the sampler.

The following describes one particular approach to implementing anisotropic filtering for the 2D Image case, implementations may choose other methods:

Given a magFilter, minFilter of VK_FILTER_LINEAR and a mipmapMode of VK_SAMPLER_MIPMAP_MODE_NEAREST:

Instead of a single isotropic sample, N isotropic samples are be sampled within the image footprint of the image level d to approximate an anisotropic filter. The sum τ2Daniso is defined using the single isotropic τ2D(u,v) at level d.

\[\begin{aligned} \tau_{2Daniso} & = \frac{1}{N}\sum_{i=1}^{N} {\tau_{2D}\left ( u \left ( x - \frac{1}{2} + \frac{i}{N+1} , y \right ), \left ( v \left (x-\frac{1}{2}+\frac{i}{N+1} \right ), y \right ) \right )}, & \text{when}\ \rho_{x} > \rho_{y} \\ \tau_{2Daniso} &= \frac{1}{N}\sum_{i=1}^{N} {\tau_{2D}\left ( u \left ( x, y - \frac{1}{2} + \frac{i}{N+1} \right ), \left ( v \left (x,y-\frac{1}{2}+\frac{i}{N+1} \right ) \right ) \right )}, & \text{when}\ \rho_{y} \geq \rho_{x} \end{aligned}\]

When VkSamplerReductionModeCreateInfoEXT::reductionMode is set to VK_SAMPLER_REDUCTION_MODE_WEIGHTED_AVERAGE_EXT, the above summation is used. If the reduction mode is VK_SAMPLER_REDUCTION_MODE_MIN_EXT or VK_SAMPLER_REDUCTION_MODE_MAX_EXT, then the value is instead computed as \tau_{2Daniso} = reduce(\tau_1, …​, \tau_N), combining all texel values with non-zero weights.

15.10. Texel Footprint Evaluation

The SPIR-V instruction OpImageSampleFootprintNV evaluates the set of texels from a single mip level that would be accessed during a texel filtering operation. In addition to the inputs that would be accepted by an equivalent OpImageSample* instruction, OpImageSampleFootprintNV accepts two additional inputs. The Granularity input is an integer identifying the size of texel groups used to evaluate the footprint. Each bit in the returned footprint mask corresponds to an aligned block of texels whose size is given by the following table:

Table 28. Texel footprint granularity values
Granularity Dim = 2D Dim = 3D

0

unsupported

unsupported

1

2x2

2x2x2

2

4x2

unsupported

3

4x4

4x4x2

4

8x4

unsupported

5

8x8

unsupported

6

16x8

unsupported

7

16x16

unsupported

8

unsupported

unsupported

9

unsupported

unsupported

10

unsupported

16x16x16

11

64x64

32x16x16

12

128x64

32x32x16

13

128x128

32x32x32

14

256x128

64x32x32

15

256x256

unsupported

The Coarse input is used to select between the two mip levels that may be accessed during texel filtering when using a mipmapMode of VK_SAMPLER_MIPMAP_MODE_LINEAR. When filtering between two mip levels, a Coarse value of true requests the footprint in the lower-resolution mip level (higher level number), while false requests the footprint in the higher-resolution mip level. If texel filtering would access only a single mip level, the footprint in that level would be returned when Coarse is set to false; an empty footprint would be returned when Coarse is set to true.

The footprint for OpImageSampleFootprintNV is returned in a structure with six members:

  • The first member is a boolean value that is true if the texel filtering operation would access only a single mip level.

  • The second member is a two- or three-component integer vector holding the footprint anchor location. For two-dimensional images, the returned components are in units of eight texel groups. For three-dimensional images, the returned components are in units of four texel groups.

  • The third member is a two- or three-component integer vector holding a footprint offset relative to the anchor. All returned components are in units of texel groups.

  • The fourth member is a two-component integer vector mask, which holds a bitfield identifying the set of texel groups in an 8x8 or 4x4x4 neighborhood relative to the anchor and offset.

  • The fifth member is an integer identifying the mip level containing the footprint identified by the anchor, offset, and mask.

  • The sixth member is an integer identifying the granularity of the returned footprint.

For footprints in two-dimensional images (Dim2D), the mask returned by OpImageSampleFootprintNV indicates whether each texel group in a 8x8 local neighborhood of texel groups would have one or more texels accessed during texel filtering. In the mask, the texel group with local group coordinates \((lgx,lgy)\) is considered covered if and only if

\[\begin{aligned} 0 \neq ((mask.x + (mask.y << 32)) \text{ \& } (1 << (lgy \times 8 + lgx))) \end{aligned}\]

where

  • \(0<=lgx<8\) and \(0<=lgy<8\); and

  • \(mask\) is the returned two-component mask.

The local group with coordinates \((lgx,lgy)\) in the mask is considered covered if and only if the texel filtering operation would access one or more texels \(\tau_{ij}\) in the returned miplevel where

\[\begin{aligned} i0 & = \begin{cases} gran.x \times (8 \times anchor.x + lgx), & \text{if } lgx + offset.x < 8 \\ gran.x \times (8 \times (anchor.x - 1) + lgx), & \text{otherwise} \end{cases} \\ i1 & = i0 + gran.x - 1 \\ j0 & = \begin{cases} gran.y \times (8 \times anchor.y + lgy), & \text{if } lgy + offset.y < 8 \\ gran.y \times (8 \times (anchor.y - 1) + lgy), & otherwise \end{cases} \\ j1 & = j0 + gran.y - 1 \end{aligned}\]

and

  • \(i0<=i<=i1\) and \(j0<=j<=j1\);

  • \(gran\) is a two-component vector holding the width and height of the texel group identified by the granularity;

  • \(anchor\) is the returned two-component anchor vector; and

  • \(offset\) is the returned two-component offset vector.

For footprints in three-dimensional images (Dim3D), the mask returned by OpImageSampleFootprintNV indicates whether each texel group in a 4x4x4 local neighborhood of texel groups would have one or more texels accessed during texel filtering. In the mask, the texel group with local group coordinates \((lgx,lgy,lgz)\), is considered covered if and only if:

\[\begin{aligned} 0 \neq ((mask.x + (mask.y << 32)) \text{ \& } (1 << (lgz \times 16 + lgy \times 4 + lgx))) \end{aligned}\]

where

  • \(0<=lgx<4\), \(0<=lgy<4\), and \(0<=lgz<4\); and

  • \(mask\) is the returned two-component mask.

The local group with coordinates \((lgx,lgy,lgz)\) in the mask is considered covered if and only if the texel filtering operation would access one or more texels \(\tau_{ijk}\) in the returned miplevel where

\[\begin{aligned} i0 & = \begin{cases} gran.x \times (4 \times anchor.x + lgx), & \text{if } lgx + offset.x < 4 \\ gran.x \times (4 \times (anchor.x - 1) + lgx), & \text{otherwise} \end{cases} \\ i1 & = i0 + gran.x - 1 \\ j0 & = \begin{cases} gran.y \times (4 \times anchor.y + lgy), & \text{if } lgy + offset.y < 4 \\ gran.y \times (4 \times (anchor.y - 1) + lgy), & otherwise \end{cases} \\ j1 & = j0 + gran.y - 1 \\ k0 & = \begin{cases} gran.z \times (4 \times anchor.z + lgz), & \text{if } lgz + offset.z < 4 \\ gran.z \times (4 \times (anchor.z - 1) + lgz), & otherwise \end{cases} \\ k1 & = k0 + gran.z - 1 \end{aligned}\]

and

  • \(i0<=i<=i1\), \(j0<=j<=j1\), \(k0<=k<=k1\);

  • \(gran\) is a three-component vector holding the width, height, and depth of the texel group identified by the granularity;

  • \(anchor\) is the returned three-component anchor vector; and

  • \(offset\) is the returned three-component offset vector.

If the sampler used by OpImageSampleFootprintNV enables anisotropic texel filtering via anisotropyEnable, it is possible that the set of texel groups accessed in a mip level may be too large to be expressed using an 8x8 or 4x4x4 mask using the granularity requested in the instruction. In this case, the implementation uses a texel group larger than the requested granularity. When a larger texel group size is used, OpImageSampleFootprintNV returns an integer granularity value that can be interpreted in the same manner as the granularity value provided to the instruction to determine the texel group size used. If anisotropic texel filtering is disabled in the sampler, or if an anisotropic footprint can be represented as an 8x8 or 4x4x4 mask with the requested granularity, OpImageSampleFootprintNV will use the requested granularity as-is and return a granularity value of zero.

OpImageSampleFootprintNV supports only two- and three-dimensional image accesses (Dim2D and Dim3D) and the footprint returned is undefined if a sampler uses an addressing mode other than VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE.

15.11. Image Operation Steps

Each step described in this chapter is performed by a subset of the image instructions:

  • Texel Input Validation Operations, Format Conversion, Texel Replacement, Conversion to RGBA, and Component Swizzle: Performed by all instructions except OpImageWrite.

  • Depth Comparison: Performed by OpImage*Dref instructions.

  • All Texel output operations: Performed by OpImageWrite.

  • Projection: Performed by all OpImage*Proj instructions.

  • Derivative Image Operations, Cube Map Operations, Scale Factor Operation, Level-of-Detail Operation and Image Level(s) Selection, and Texel Anisotropic Filtering: Performed by all OpImageSample* and OpImageSparseSample* instructions.

  • (s,t,r,q,a) to (u,v,w,a) Transformation, Wrapping, and (u,v,w,a) to (i,j,k,l,n) Transformation And Array Layer Selection: Performed by all OpImageSample, OpImageSparseSample, and OpImage*Gather instructions.

  • Texel Gathering: Performed by OpImage*Gather instructions.

  • Texel Footprint Evaluation: Performed by OpImageSampleFootprint instructions.

  • Texel Filtering: Performed by all OpImageSample* and OpImageSparseSample* instructions.

  • Sparse Residency: Performed by all OpImageSparse* instructions.