Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class ShaderEvaluator

  1. Object
  2. ShaderEvaluator

Evaluates ShaderNode graphs on the CPU, with the same results the GPU computes from the same graph. Uses:

  • Gameplay that follows a shader. A boat that rides the waves a vertex offset draws evaluates the same graph at its own position, so it floats on the water the player sees rather than on an approximation.
  • The software renderer, which has no shader compiler.
  • Tests of a graph’s values.
import com.codename1.gpu.shader.*;
import com.codename1.gpu.shaders.Water;

ShaderMaterial water = Water.ocean();
float now = 2.5f, boatX = 3f, boatZ = -4f;
ShaderEvaluator ev = new ShaderEvaluator(water)
        .setInput("time", now)
        .setInput("position", boatX, 0f, boatZ);
float lift = ev.evaluate(water.get(ShaderMaterial.Hook.VERTEX_OFFSET))[1];

Inputs are the names of the ShaderNode built ins (position, normal, uv, worldPos, worldNormal, viewDir, cameraPos, sunDir, sunColor, time, baseColor); an input never set reads as zero. Uniforms are read from the material at each evaluation, so a changed value needs nothing else. A graph is compiled to a flat program the first time it is evaluated and kept; evaluating it again allocates nothing except through evaluate(ShaderNode), which returns a new array.

An evaluator is not thread safe.

Nested types

interface ShaderEvaluator.TextureSamplerReads a material texture for the evaluator.

Constructors

public ShaderEvaluator(ShaderMaterial material)Creates an evaluator for the graphs of a material.

Methods

public ShaderEvaluator setInput(String name, float... values)Sets a built-in input.
public ShaderEvaluator setTextureSampler(ShaderEvaluator.TextureSampler sampler)Sets what reads the material’s textures.
public float[] evaluate(ShaderNode node)Evaluates a graph.
public void evaluate(ShaderNode node, float[] out)Evaluates a graph into an array, without allocating.
public float evaluateFloat(ShaderNode node)Evaluates a FLOAT graph.

Inherited methods

Constructor details

ShaderEvaluator

public ShaderEvaluator(ShaderMaterial material)
Creates an evaluator for the graphs of a material.

Parameters

material ShaderMaterial
the material whose uniforms and textures the graphs read, or null when they read none

Method details

setInput

public ShaderEvaluator setInput(String name, float... values)
Sets a built-in input.

Parameters

name String
the built-in’s name, such as worldPos
values float...
its components; missing ones are zero

Returns

this evaluator

setTextureSampler

public ShaderEvaluator setTextureSampler(ShaderEvaluator.TextureSampler sampler)
Sets what reads the material’s textures. Without one every texture reads as opaque white.

Parameters

sampler ShaderEvaluator.TextureSampler
the sampler, or null

Returns

this evaluator

evaluate

public float[] evaluate(ShaderNode node)
Evaluates a graph.

Parameters

node ShaderNode
the graph

Returns

a new array of four floats; components past the node’s type are zero

evaluate

public void evaluate(ShaderNode node, float[] out)
Evaluates a graph into an array, without allocating.

Parameters

node ShaderNode
the graph
out float[]
receives the value; at least as long as the node’s type, and up to four floats are written

evaluateFloat

public float evaluateFloat(ShaderNode node)
Evaluates a FLOAT graph.

Parameters

node ShaderNode
the graph

Returns

its value