ShaderImage QML Type

Declares a GPU image resource that shaders can read and write. More...

Import Statement: import QtQuick3D
Since: Qt 6.13
Inherits:

Object3D

Properties

Detailed Description

A ShaderImage is a GPU-side image resource with an explicit shape, size and pixel format, with no content of its own. Unlike Texture, it is not loaded from an image file or a TextureData. It is meant to be written to or read from directly in shader code (typically in a ComputePass, via imageStore()/imageLoad(), but also from a CustomMaterial or an Effect), rather than sampled as a regular texture.

A ShaderImage is exposed to a shader by using it as a property in the ComputePass, RenderPass, Effect or CustomMaterial:

 ShaderImage {
     id: myImage
     width: 256
     height: 256
 }

 ComputePass {
     computeShader: "generate.comp"
     property ShaderImage targetImage: myImage
 }

In the shader, the property is declared with the SHADER_IMAGE macro, using the same name as the QML property (targetImage in the example above), and is then used like a regular GLSL image variable:

 SHADER_IMAGE("rgba8", targetImage, image2D, "writeonly");

 void main() {
     imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(1.0, 0.0, 0.0, 1.0));
 }

The first argument to SHADER_IMAGE is the GLSL image format (such as "rgba8" or "r32ui"), which must match the ShaderImage's format. The third argument is the GLSL image type, which must match the ShaderImage's shape, with an i or u prefix for an integer format (for example image2D, or uimage3D for a three-dimensional image in an unsigned integer format). A ShaderImage whose format or shape does not match the declaration is reported with a warning, and the shader cannot be used; this also applies when the format or the shape changes later. The last argument is a memory qualifier string made up of any combination of readonly, writeonly, coherent, volatile, and restrict, matching the GLSL memory qualifiers of the same names; an empty string means implicit read-write access. Any other word is rejected with a warning, and the shader cannot be used.

Note: On OpenGL ES, implicit read-write access is only allowed for the r32f, r32i, and r32ui formats. An image in any other format has to be declared either readonly or writeonly, otherwise the shader fails to compile on OpenGL ES. Not all formats are available on OpenGL ES either; see format.

The declaration is recognized by scanning the shader source before any preprocessing takes place. The format and the memory qualifiers must therefore be written as literal strings, and the name as a plain identifier; a #define'd format is not recognized. Each name can be declared only once in a shader, even in different preprocessor branches, and its format must match the format property. Declarations inside comments are ignored. A declaration that does not follow this form is left in the shader source unprocessed, which typically shows up as a shader compilation error.

Note: Atomic operations on images (imageAtomicAdd() and friends) are not supported with the Metal graphics API. Such shaders translate and compile, but the results are undefined, because the translation relies on a hidden helper buffer that is never bound. Atomics on a ShaderStorageBuffer are not affected and work on all backends.

See also ComputePass and ShaderStorageBuffer.

Property Documentation

arraySize : int

This property holds the number of images in the array. It is only used by an Image2DArray shape. The default value is 1. Values smaller than 1 are ignored.

depth : int

This property holds the depth of the image, in texels. It is only used by an Image3D shape. The default value is 1. Values smaller than 1 are ignored.

This is a size, not the number of bits per pixel that QImage::depth() returns.

format : enumeration

This property holds the pixel format of the image. The default value is ShaderImage.RGBA8, an 8-bit-per-channel unsigned normalized RGBA image.

Each value corresponds to one GLSL image format qualifier, which is what the shader's SHADER_IMAGE declaration has to name as its format argument. The two must match: a mismatch is reported as a warning and leaves the image unusable by that shader.

ConstantDescription
ShaderImage.RGBA8"rgba8", the default.
ShaderImage.R8"r8"
ShaderImage.RG8"rg8"
ShaderImage.R16"r16"
ShaderImage.RG16"rg16"
ShaderImage.RGBA16F"rgba16f"
ShaderImage.RGBA32F"rgba32f"
ShaderImage.R16F"r16f"
ShaderImage.R32F"r32f"
ShaderImage.RGB10A2"rgb10_a2", a packed format with 10 bits per color channel and 2 bits of alpha.
ShaderImage.R8SI"r8i", a signed integer format.
ShaderImage.R32SI"r32i"
ShaderImage.RG32SI"rg32i"
ShaderImage.RGBA32SI"rgba32i"
ShaderImage.R8UI"r8ui", an unsigned integer format.
ShaderImage.R32UI"r32ui"
ShaderImage.RG32UI"rg32ui"
ShaderImage.RGBA32UI"rgba32ui"

An integer format must be declared with a matching integer image type in the shader (iimage2D for the signed and uimage2D for the unsigned variants, rather than image2D), and is read and written with ivec4/\c uvec4 rather than vec4.

Note: OpenGL ES supports only a subset of these formats for images: RGBA8, RGBA16F, RGBA32F, R32F, R32SI, RGBA32SI, R32UI, and RGBA32UI. The other formats (R8, RG8, R16, RG16, R16F, RGB10A2, R8SI, R8UI, RG32SI, and RG32UI) are only available with desktop OpenGL, Vulkan, Metal, and Direct3D. On OpenGL ES, a shader that declares one of them fails to build. Of the supported formats, all except R32F, R32SI, and R32UI also need a readonly or writeonly qualifier in the SHADER_IMAGE declaration on OpenGL ES.

Setting a value outside this enumeration is ignored, with a warning, and the previous format is retained.

height : int

This property holds the height of the image, in texels. It is not used by an Image1D shape. The default value is 1. Values smaller than 1 are ignored.

shape : enumeration

This property holds the shape of the image: the kind of GPU texture that is created, and the GLSL image type that the shader's SHADER_IMAGE declaration has to use. The default value is ShaderImage.Image2D.

ConstantDescription
ShaderImage.Image1DA one-dimensional image of width texels, image1D in the shader.
ShaderImage.Image2DA two-dimensional image of width by height texels, image2D in the shader.
ShaderImage.Image3DA three-dimensional image of width by height by depth texels, image3D in the shader.
ShaderImage.Image2DArrayAn array of arraySize two-dimensional images of width by height texels, image2DArray in the shader.
ShaderImage.ImageCubeA cube map with six faces of width by height texels, imageCube in the shader. The width and the height must be equal.

The size properties that a shape does not use are ignored. With an integer format, the image type has an i or u prefix, such as iimage2D or uimage3D.

Note: One-dimensional images are not supported with OpenGL ES and Direct3D. Cube map images are not supported with Direct3D, which has no read-write cube texture type; a shader declaring an imageCube fails to build there. Arrays of one-dimensional images and arrays of cube maps are not supported.

Setting a value outside this enumeration is ignored, with a warning, and the previous shape is retained.

width : int

This property holds the width of the image, in texels. The default value is 1. Values smaller than 1 are ignored.