ShaderProgram

actual class ShaderProgram(source: String)

Android's own runtime shaders, which arrived in version 33.

The language is the same one Skia uses, with a smaller set of built in functions, so a program written for the desktop usually runs here unchanged. Below version 33 there is no way to run one at all: available is false and a drawing that needs one has to fall back to shapes or be left out of the list on that device.

expect class ShaderProgram(source: String)

A small program that runs once for every pixel, compiled from source at runtime.

This is the one thing that changes what the library can draw rather than how well. Everything else here builds a picture out of lines and circles, which means the cost grows with how much detail is wanted and a full screen of swirling colour costs a full screen of shapes. A shader is handed a pixel and asked what colour it should be, and the graphics card asks a few million times at once. Plasma, flowing noise, fractals and raymarched tunnels all become one draw call.

The language is SkSL, which is close enough to GLSL to read. Write a main that takes a position and returns a colour:

uniform float2 uResolution;

half4 main(float2 position) {
float2 uv = position / uResolution;
return half4(uv.x, uv.y, 0.5, 1.0);
}

The source declares every value it reads, as uResolution is declared here; nothing is added to it. Set those values with uniform and child, then hand brush to any Compose draw call. Values persist between frames, so only what changed has to be set again.

Not every device can run one. Android gained the ability in version 33, and anything older reports available as false, so a drawing that needs a shader has to say what it does instead.

actual class ShaderProgram(source: String)

Desktop and iOS both draw through Skia, so both get this one.

The compiled program and the builder holding its values are made once and reused. Compiling on every frame would be far more expensive than the drawing.

Constructors

Link copied to clipboard
actual constructor(source: String)
expect constructor(source: String)
actual constructor(source: String)

Properties

Link copied to clipboard
actual val available: Boolean

False when this device cannot run shaders at all, or when source did not compile.

expect val available: Boolean

False when this device cannot run shaders at all, or when source did not compile.

actual val available: Boolean

False when this device cannot run shaders at all, or when source did not compile.

Link copied to clipboard
actual val error: String?

What the compiler said, with line numbers, or null when it compiled.

expect val error: String?

What the compiler said, with line numbers, or null when it compiled.

actual val error: String?

What the compiler said, with line numbers, or null when it compiled.

Functions

Link copied to clipboard
actual fun brush(): Brush?

Something to paint with, or null when this device cannot run the program.

expect fun brush(): Brush?

Something to paint with, or null when this device cannot run the program.

actual fun brush(): Brush?

Something to paint with, or null when this device cannot run the program.

Link copied to clipboard
actual fun child(name: String, image: ImageBitmap, tiled: Boolean)

Hands the shader a picture to read, declared as uniform shader name.

expect fun child(name: String, image: ImageBitmap, tiled: Boolean = false)

Hands the shader a picture to read, declared as uniform shader name.

actual fun child(name: String, image: ImageBitmap, tiled: Boolean)

Hands the shader a picture to read, declared as uniform shader name.

Link copied to clipboard
actual fun uniform(name: String, value: Float)
actual fun uniform(name: String, x: Float, y: Float)
actual fun uniform(name: String, x: Float, y: Float, z: Float)
actual fun uniform(name: String, x: Float, y: Float, z: Float, w: Float)
expect fun uniform(name: String, value: Float)
expect fun uniform(name: String, x: Float, y: Float)
expect fun uniform(name: String, x: Float, y: Float, z: Float)
expect fun uniform(name: String, x: Float, y: Float, z: Float, w: Float)
actual fun uniform(name: String, value: Float)
actual fun uniform(name: String, x: Float, y: Float)
actual fun uniform(name: String, x: Float, y: Float, z: Float)
actual fun uniform(name: String, x: Float, y: Float, z: Float, w: Float)
Link copied to clipboard
actual fun uniforms(name: String, values: FloatArray)

Sets an array of values, for anything declared as uniform float3 name[8] or similar.

expect fun uniforms(name: String, values: FloatArray)

Sets an array of values, for anything declared as uniform float3 name[8] or similar.

actual fun uniforms(name: String, values: FloatArray)

Sets an array of values, for anything declared as uniform float3 name[8] or similar.