Class ShaderEmitter

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

public final class ShaderEmitter extends Object

Prints ShaderNode graphs as statements of one shading language. This is the part of a backend's shader generator that custom materials need; applications do not call it.

A backend creates one emitter per shader stage, calls emit for each hook it splices in, and uses the returned expression where the engine's own value would have gone. Every node that is not a constant or an input becomes one temporary, so a node shared by several hooks is computed once.

  • Constructor Details

    • ShaderEmitter

      public ShaderEmitter(ShaderEmitter.Dialect dialect, ShaderMaterial material, ShaderEmitter.Inputs inputs, StringBuilder out, String indent, String prefix)

      Creates an emitter that appends statements to out.

      Parameters
      • dialect: the language to print

      • material: the material whose uniforms and textures the graphs read

      • inputs: the backend's spelling of the inputs

      • out: where the temporaries are declared

      • indent: the indentation of each statement

      • prefix: a prefix for temporary names, distinct per stage

  • Method Details

    • emit

      public String emit(ShaderNode node)

      Declares whatever temporaries node needs and returns the expression that holds its value.

      Parameters
      • node: the graph
      Returns

      an expression of the node's type

    • literal

      public static String literal(float v)

      Returns a float literal every dialect reads the same way.

      Parameters
      • v: a finite value
      Returns

      the literal

    • typeName

      public String typeName(ShaderType t)

      Returns the dialect's name of a type.

      Parameters
      • t: the type
      Returns

      the type name

    • typeName

      public static String typeName(ShaderEmitter.Dialect d, ShaderType t)

      Returns a dialect's name of a type.

      Parameters
      • d: the dialect

      • t: the type

      Returns

      the type name

    • uses

      public static boolean uses(ShaderMaterial material, boolean fragment, String builtin)

      Returns true if any hook of the given stage reads the built-in input.

      Parameters
      • material: the material, or null

      • fragment: true for the fragment stage

      • builtin: the built-in's name, such as worldPos

      Returns

      true if it is read

    • uses

      public static boolean uses(ShaderMaterial material, String builtin)

      Returns true if any hook of either stage reads the built-in input.

      Parameters
      • material: the material, or null

      • builtin: the built-in's name

      Returns

      true if it is read

    • describe

      public static void describe(ShaderNode root, StringBuilder sb)

      Appends a structural description of a graph: equal for two graphs that print the same program. Shared nodes are written once and then referred to by number.

      Parameters
      • root: the graph

      • sb: where to append