NIR Texture Instructions¶
Even though texture instructions could be supported as intrinsics, the vast number of combinations mean that doing so is practically impossible. Instead, NIR has a dedicated texture instruction. There are several texture operations:
Texture instruction opcode
Regular texture look-up
Texture look-up with LOD bias
Texture look-up with explicit LOD
Texture look-up with partial derivatives
Texel fetch with explicit LOD
Multisample texture fetch
Multisample texture fetch from framebuffer
Multisample compression value fetch
Texture lod query
Texture levels query
Texture samples query
Query whether all samples are definitely identical.
Regular texture look-up, eligible for pre-dispatch
Multisample fragment color texture fetch
Multisample fragment mask texture fetch
Returns a buffer or image descriptor.
Returns a sampler descriptor.
Returns the sampler’s LOD bias
- enumerator nir_texop_tex¶
As with other instruction types, there is still an array of sources, except that each source also has a type associated with it. There are various source types, each corresponding to a piece of information that the different texture operations require.
Texture instruction source type
Must have nir_tex_instr::coord_components components.
The texture coordinate (except for the array component, if any) is divided by this value before LOD computation and sampling.
Must be a float scalar.
For shadow sampling, the fetched texel values are compared against the shadow comparator using the compare op specified by the sampler object and converted to 1.0 if the comparison succeeds and 0.0 if it fails. Interpolation happens after this conversion so the actual result may be anywhere in the range [0.0, 1.0].
Only valid if nir_tex_instr::is_shadow and must be a float scalar.
An integer value that is added to the texel address before sampling. This is only allowed with operations that take an explicit LOD as it is applied in integer texel space after LOD selection and not normalized coordinate space.
This value is added to the computed LOD before mip-mapping.
The computed LOD is clamped to be at least as large as min_lod before mip-mapping.
MSAA sample index
Intel-specific MSAA compression data
Explicit horizontal (X-major) coordinate derivative
Explicit vertical (Y-major) coordinate derivative
Texture variable dereference
Sampler variable dereference
Texture index offset
This is added to nir_tex_instr::texture_index. Unless nir_tex_instr::texture_non_uniform is set, this is guaranteed to be dynamically uniform.
Dynamically uniform sampler index offset
This is added to nir_tex_instr::sampler_index. Unless nir_tex_instr::sampler_non_uniform
is set, this is guaranteed to be dynamically uniform. This should not be present until GLSL ES 3.20, GLSL 4.00, or ARB_gpu_shader5, because in ES 3.10 and GL 3.30 samplers said “When aggregated into arrays within a shader, samplers can only be indexed
with a constant integral expression.”
Bindless texture handle
This is, unfortunately, a bit overloaded at the moment. There are generally two types of bindless handles:
For GL_ARB_bindless bindless handles. These are part of the GL/Gallium-level API and are always a 64-bit integer.
HW-specific handles. GL_ARB_bindless handles may be lowered to these. Also, these are used by many Vulkan drivers to implement descriptor sets, especially for UPDATE_AFTER_BIND descriptors. The details of hardware handles (bit size, format, etc.) is HW-specific.
Because of this overloading and the resulting ambiguity, we currently don’t validate anything for these.
Bindless sampler handle
Plane index for multi-plane YCbCr textures
Backend-specific vec4 tex src argument.
Can be used to have NIR optimization (copy propagation, lower_vec_to_movs) apply to the packing of the tex srcs. This lowering must only happen after nir_lower_tex().
The nir_tex_instr_src_type() of this argument is float, so no lowering will happen if nir_lower_int_to_float is used.
Second backend-specific vec4 tex src argument, see nir_tex_src_backend1.
- enumerator nir_tex_src_coord¶
Of particular interest are the texture/sampler deref/index/handle source types. First, note that textures and samplers are specified separately in NIR. While not required for OpenGL, this is required for Vulkan and OpenCL. Some OpenGL [ES] drivers have to deal with hardware that does not have separate samplers and textures. While not recommended, an OpenGL-only driver may assume that the texture and sampler derefs will always point to the same resource, if needed. Note that this pretty well paints your compiler into a corner and makes any future port to Vulkan or OpenCL harder, so such assumptions should really only be made if targeting OpenGL ES 2.0 era hardware.
Also, like a lot of other resources, there are multiple ways to represent a
texture in NIR. It can be referenced by a variable dereference, an index, or a
bindless handle. When using an index or a bindless handle, the texture type
information is generally not available. To handle this, various information
from the type is redundantly stored in the
Represents a texture instruction
enum glsl_sampler_dim sampler_dim¶
Dimensionality of the texture operation
This will typically match the dimensionality of the texture deref type if a nir_tex_src_texture_deref is present. However, it may not if texture lowering has occurred.
ALU type of the destination
This is the canonical sampled type for this texture operation and may not exactly match the sampled type of the deref type when a nir_tex_src_texture_deref is present. For OpenCL, the sampled type of the texture deref will be GLSL_TYPE_VOID and this is allowed to be anything. With SPIR-V, the signedness of integer types is allowed to differ. For all APIs, the bit size may differ if the driver has done any sort of mediump or similar lowering since texture types always have 32-bit sampled types.
Array of sources
This array has nir_tex_instr::num_srcs elements
Number of sources
Number of components in the coordinate, if any
True if the texture instruction acts on an array texture
True if the texture instruction performs a shadow comparison
If this is true, the texture instruction must have a nir_tex_src_comparator.
If is_shadow is true, whether this is the old-style shadow that outputs 4 components or the new-style shadow that outputs 1 component.
True if this texture instruction should return a sparse residency code. The code is in the last component of the result.
nir_texop_tg4 component selector
This determines which RGBA component is gathered.
Validation needs to know this for gradient component count
True if the texture index or handle is not dynamically uniform
True if the sampler index or handle is not dynamically uniform.
This may be set when VK_EXT_descriptor_indexing is supported and the appropriate capability is enabled.
This should always be false in GLSL (GLSL ES 3.20 says “When aggregated
into arrays within a shader, opaque types can only be indexed with a
dynamically uniform integral expression”, and GLSL 4.60 says “When
aggregated into arrays within a shader, [texture, sampler, and
samplerShadow] types can only be indexed with a dynamically uniform
expression, or texture lookup will result in undefined values.”).
The texture index
If this texture instruction has a nir_tex_src_texture_offset source, then the texture index is given by texture_index + texture_offset.
The sampler index
The following operations do not require a sampler and, as such, this field should be ignored:
If this texture instruction has a nir_tex_src_sampler_offset source, then the sampler index is given by sampler_index + sampler_offset.
- nir_instr instr¶
A texture instruction source
Type of this source
- nir_src src¶
Texture instruction helpers¶
There are a number of helper functions for working with NIR texture instructions. They are documented here in no particular order.
nir_tex_instr *nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)¶
Creates a NIR texture instruction
bool nir_tex_instr_need_sampler(const nir_tex_instr *instr)¶
Returns true if the texture operation requires a sampler as a general rule
Note that the specific hw/driver backend could require to a sampler object/configuration packet in any case, for some other reason.
unsigned nir_tex_instr_result_size(const nir_tex_instr *instr)¶
Returns the number of components returned by this nir_tex_instr
Useful for code building texture instructions when you don’t want to think about how many components a particular texture op returns. This does not include the sparse residency code.
static inline unsigned nir_tex_instr_dest_size(const nir_tex_instr *instr)¶
Returns the destination size of this nir_tex_instr including the sparse residency code, if any.
bool nir_tex_instr_is_query(const nir_tex_instr *instr)¶
Returns true if this texture operation queries something about the texture rather than actually sampling it.
bool nir_tex_instr_has_implicit_derivative(const nir_tex_instr *instr)¶
Returns true if this texture instruction does implicit derivatives
This is important as there are extra control-flow rules around derivatives and texture instructions which perform them implicitly.
nir_alu_type nir_tex_instr_src_type(const nir_tex_instr *instr, unsigned src)¶
Returns the ALU type of the given texture instruction source
unsigned nir_tex_instr_src_size(const nir_tex_instr *instr, unsigned src)¶
Returns the number of components required by the given texture instruction source
static inline int nir_tex_instr_src_index(const nir_tex_instr *instr, nir_tex_src_type type)¶
Returns the index of the texture instruction source with the given nir_tex_src_type or -1 if no such source exists.
void nir_tex_instr_add_src(nir_tex_instr *tex, nir_tex_src_type src_type, nir_src src)¶
Adds a source to a texture instruction
void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)¶
Removes a source from a texture instruction
Texture instruction lowering¶
Because most hardware only supports some subset of all possible GLSL/SPIR-V texture operations, NIR provides a quite powerful lowering pass which is able to implement more complex texture operations in terms of simpler ones.
bool nir_lower_tex(nir_shader *shader, const nir_lower_tex_options *options)¶
Lowers complex texture instructions to simpler ones
bitmask of (1 << GLSL_SAMPLER_DIM_x) to control for which sampler types a texture projector is lowered.
If true, lower texture projector for any array sampler dims
If true, lower away nir_tex_src_offset for all texelfetch instructions.
If true, lower away nir_tex_src_offset for all rect textures.
If not NULL, this filter will return true for tex instructions that should lower away nir_tex_src_offset.
If true, lower rect textures to 2D, using txs to fetch the texture dimensions and dividing the texture coords by the texture dims to normalize.
If true, convert yuv to rgb.
To emulate certain texture wrap modes, this can be used to saturate the specified tex coord to [0.0, 1.0]. The bits are according to sampler #, ie. if, for example:
(conf->saturate_s & (1 << n))
is true, then the s coord for sampler n is saturated.
Note that clamping must happen after projector lowering so any projected texture sample instruction with a clamped coordinate gets automatically lowered, regardless of the ‘lower_txp’ setting.
Bitmap of textures that need srgb to linear conversion. If (lower_srgb & (1 << texture_index)) then the rgb (xyz) components of the texture are lowered to linear.
If true, lower nir_texop_txd on cube maps with nir_texop_txl.
If true, lower nir_texop_txd on 3D surfaces with nir_texop_txl.
If true, lower nir_texop_txd any array surfaces with nir_texop_txl.
If true, lower nir_texop_txd on shadow samplers (except cube maps) with nir_texop_txl. Notice that cube map shadow samplers are lowered with lower_txd_cube_map.
If true, lower nir_texop_txd on all samplers to a nir_texop_txl. Implies lower_txd_cube_map and lower_txd_shadow.
If true, lower nir_texop_txb that try to use shadow compare and min_lod at the same time to a nir_texop_lod, some math, and nir_texop_tex.
If true, lower nir_texop_txd on shadow samplers when it uses min_lod with nir_texop_txl. This includes cube maps.
If true, lower nir_texop_txd on when it uses both offset and min_lod with nir_texop_txl. This includes cube maps.
If true, lower nir_texop_txd with min_lod to a nir_texop_txl if the sampler is bindless.
If true, lower nir_texop_txd with min_lod to a nir_texop_txl if the sampler index is not statically determinable to be less than 16.
If true, lower nir_texop_txs with a non-0-lod into nir_texop_txs with 0-lod followed by a nir_ishr.
If true, lower nir_texop_txs for cube arrays to a nir_texop_txs with a 2D array type followed by a nir_idiv by 6.
If true, apply a .bagr swizzle on tg4 results to handle Broadcom’s mixed-up tg4 locations.
If true, lowers tg4 with 4 constant offsets to 4 tg4 calls
Lower txf_ms to fragment_mask_fetch and fragment_fetch and samples_identical to fragment_mask_fetch.
enum nir_lower_tex_packing lower_tex_packing¶
To lower packed sampler return formats.
Indexed by sampler-id.
If true, lower nir_texop_lod to return -FLT_MAX if the sum of the absolute values of derivatives is 0 for all coordinates.
Payload data to be sent to callback / filter functions.
- unsigned lower_txp¶
enumerator nir_lower_tex_packing_none = 0¶
The sampler returns up to 2 32-bit words of half floats or 16-bit signed or unsigned ints based on the sampler type
The sampler returns 1 32-bit word of 4x8 unorm
- enumerator nir_lower_tex_packing_none = 0¶