Skip to main content

FlixelShader

View source

class

org.flixelgdx.util.FlixelShader

public class FlixelShader extends FlixelBasic

A compiled GLSL shader program with a FlixelGDX lifecycle.

FlixelShader wraps a libGDX ShaderProgram and integrates with the standard FlixelGDX update/destroy pipeline via FlixelBasic. This allows time-based or state-driven uniform updates in FlixelShader.update(...) and guaranteed GPU resource cleanup on FlixelShader.destroy().

Camera-level post-processing is the primary use case: assign a shader to a FlixelCamera via FlixelCamera.setShader(FlixelShader) and the camera will automatically render its scene into a framebuffer, then composite the result to screen using this shader each frame.

Two modes are available:

  • libGDX mode (constructors): write raw GLSL ES 2.0 targeting the standard libGDX SpriteBatch attribute contract. Fragment shaders receive the camera output as uniform sampler2D u_texture via the v_texCoords varying.
  • HaxeFlixel mode (FlixelShader.fromHaxeFlixel(String)): write or copy a filter shader from HaxeFlixel using #pragma header, #pragma body, bitmap, openfl_TextureCoordv, and flixel_texture2D(...). The preprocessor rewrites those to libGDX-compatible names before compilation.

Raw libGDX mode example - a grayscale filter applied to the default camera:

FlixelShader gray = new FlixelShader(
"varying vec2 v_texCoords;\n" +
"uniform sampler2D u_texture;\n" +
"void main() {\n" +
" vec4 c = texture2D(u_texture, v_texCoords);\n" +
" float g = dot(c.rgb, vec3(0.299, 0.587, 0.114));\n" +
" gl_FragColor = vec4(g, g, g, c.a);\n" +
"}"
);
Flixel.cameras.first().setShader(gray);

HaxeFlixel mode example using a .frag file from disk:

String src = Gdx.files.internal("shaders/bloom.frag").readString();
FlixelShader bloom = FlixelShader.fromHaxeFlixel(src);
Flixel.cameras.first().setShader(bloom);

To drive per-frame uniforms such as u_time, subclass FlixelShader and override FlixelShader.update(...) to track state and FlixelShader.applyUniforms() to upload it:

public class TimedShader extends FlixelShader {
private float time;
private final int timeLoc;

public TimedShader(String fragSrc) {
super(fragSrc);
timeLoc = getProgram().fetchUniformLocation("u_time", false);
}

public void update(float elapsed) {
super.update(elapsed);
time += elapsed;
}

public void applyUniforms() {
if (timeLoc >= 0) Gdx.gl20.glUniform1f(timeLoc, time);
}
}

Constructors

FlixelShader(String)

public FlixelShader(String fragSrc)

Compiles a shader using a built-in pass-through vertex shader and the given fragment source.

The fragment shader receives the camera's framebuffer output as uniform sampler2D u_texture and can sample via the v_texCoords varying. Compilation errors are logged via Flixel.error(String, Object).

Parameters:

NameDescription
fragSrcGLSL ES 2.0 fragment shader source code.

FlixelShader(String, String)

public FlixelShader(String vertSrc, String fragSrc)

Compiles a shader from explicit vertex and fragment source strings.

Use this constructor when you need a custom vertex shader, for example to apply mesh deformation or pass additional varyings to the fragment stage. Compilation errors are logged via Flixel.error(String, Object).

Parameters:

NameDescription
vertSrcGLSL ES 2.0 vertex shader source code.
fragSrcGLSL ES 2.0 fragment shader source code.

Fields

main() {\n v_color = a_color;\n v_color.a = v_color.a * (255.0 /)

public static final String DEFAULT_VERT = "#ifdef GL_ES\nprecision mediump float;\n#endif\nattribute vec4 a_position;\nattribute vec4 a_color;\nattribute vec2 a_texCoord0;\nuniform mat4 u_projTrans;\nvarying vec4 v_color;\nvarying vec2 v_texCoords;\nvoid main() {\n v_color = a_color;\n v_color.a = v_color.a * (255.0 / 254.0)

The default pass-through vertex shader used when no custom vertex source is provided.

Attribute and uniform names match the libGDX SpriteBatch contract so the composite draw in FlixelCamera works without additional setup.

Exposed as public so subclasses that add custom constructors or factory methods (for example a TimedShader.fromHaxeFlixel() companion) can reuse the same vertex source without duplicating it.


flixel_texture2D(t, c), c)

protected static final String HAXEFLIXEL_DEFINES = "#define bitmap u_texture\n#define openfl_TextureCoordv v_texCoords\n#define openfl_Alpha 1.0\n#define openfl_TextureSize u_textureSize\n#define openfl_HasColorTransform false\n#define flixel_texture2D(t, c) texture2D(t, c)

GLSL #define macros prepended to every HaxeFlixel fragment shader.

These alias HaxeFlixel / OpenFL variable and function names to their libGDX equivalents so the shader source compiles without modification:

  • bitmap - the main texture sampler (maps to u_texture)
  • openfl_TextureCoordv - the UV coordinate varying (maps to v_texCoords)
  • openfl_Alpha - global alpha value (constant 1.0)
  • openfl_TextureSize - texture dimensions uniform (maps to u_textureSize)
  • openfl_HasColorTransform - color transform flag (constant false)
  • flixel_texture2D(t, c) - texture sampling helper (maps to texture2D(t, c))

Exposed as protected so subclasses can compose their own extended preprocessing pipelines that build on top of the standard HaxeFlixel environment.


HAXEFLIXEL_HEADER_EXPANSION

protected static final String HAXEFLIXEL_HEADER_EXPANSION = "#ifdef GL_ES\nprecision mediump float;\n#endif\nuniform sampler2D u_texture;\nuniform vec2 u_textureSize;\nvarying vec4 v_color;\nvarying vec2 v_texCoords;\n"

The GLSL source block that replaces #pragma header in HaxeFlixel shaders.

Declares the uniform sampler, UV coordinate varying, and color varying that the compositing pipeline feeds into the fragment shader each frame. Using the libGDX-native names here means the FlixelShader.HAXEFLIXEL_DEFINES aliases resolve correctly.

Exposed as protected so subclasses can compose their own extended preprocessing pipelines that build on top of the standard HaxeFlixel environment.


Methods

fromHaxeFlixel(String)

public static FlixelShader fromHaxeFlixel(String fragSrc)

Creates a FlixelShader from a HaxeFlixel-style fragment shader source string.

The preprocessor performs three transformations before compilation:

The built-in pass-through vertex shader is used, so no custom vertex source is needed. Compilation errors are logged via Flixel.error(String, Object).

Example:

String src = Gdx.files.internal("shaders/crt.frag").readString();
FlixelShader crt = FlixelShader.fromHaxeFlixel(src);
Flixel.cameras.first().setShader(crt);

Parameters:

NameDescription
fragSrcHaxeFlixel fragment shader source, typically read from a .frag file.

Returns: A compiled FlixelShader ready to assign to a FlixelCamera.


destroy()

public void destroy()

Releases the compiled ShaderProgram and marks this shader as destroyed.

After this call, FlixelShader.getProgram() returns null and the shader must not be used for rendering. Any FlixelCamera that holds a reference to this shader should have it cleared via setShader(null) before calling this method.


applyUniforms()

public void applyUniforms()

Uploads per-frame uniforms to the shader while it is bound for rendering.

Called automatically by the framework immediately after the composite SpriteBatch begins the post-processing draw pass, at which point the shader is already the active GL program. Do not call this yourself unless the program is already bound - writing uniforms to an unbound program has no effect.

The base implementation is a no-op. Subclasses should override this to upload whatever uniforms they need and call super.applyUniforms() for future-proofing.


getProgram()

public ShaderProgram getProgram()

Returns the underlying compiled ShaderProgram, or null if FlixelShader.destroy() has been called.

Returns: The compiled shader program.


isCompiled()

public boolean isCompiled()

Returns true if the shader compiled without errors and is ready to use.

Returns: Whether the underlying ShaderProgram compiled successfully.


getCompiled()

public boolean getCompiled()

Returns whether this shader compiled without errors and is ready to use.


getLog()

public String getLog()

Returns the compilation log from the underlying ShaderProgram, useful for diagnosing compilation errors. Returns an empty string if the program is null.

Returns: The GLSL compiler log.


preprocessHaxeFlixel(String)

public static String preprocessHaxeFlixel(String src)

Runs the HaxeFlixel-to-libGDX preprocessing pipeline on a raw fragment shader source string.

Exposed as public so subclasses can call it from their own factory methods, for example a TimedShader.fromHaxeFlixel() that needs to preprocess the source before handing it to the two-argument constructor.

Parameters:

NameDescription
srcRaw HaxeFlixel fragment shader source.

Returns: Preprocessed GLSL ES 2.0 fragment source ready for libGDX compilation.