Class ShaderEvaluator

java.lang.Object
com.codename1.gpu.shader.ShaderEvaluator

public final class ShaderEvaluator extends Object

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.

  • Constructor Details

    • ShaderEvaluator

      public ShaderEvaluator(ShaderMaterial material)

      Creates an evaluator for the graphs of a material.

      Parameters
      • material: 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: the built-in's name, such as worldPos

      • values: 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: the sampler, or null
      Returns

      this evaluator

    • evaluate

      public float[] evaluate(ShaderNode node)

      Evaluates a graph.

      Parameters
      • node: 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: the graph

      • out: 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: the graph
      Returns

      its value