FlixelShader
View sourceclass
org.flixelgdx.util.FlixelShader
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
SpriteBatchattribute contract. Fragment shaders receive the camera output asuniform sampler2D u_texturevia thev_texCoordsvarying. - HaxeFlixel mode (FlixelShader.fromHaxeFlixel(String)): write or copy a filter shader from HaxeFlixel using
#pragma header,#pragma body,bitmap,openfl_TextureCoordv, andflixel_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)
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:
| Name | Description |
|---|---|
fragSrc | GLSL ES 2.0 fragment shader source code. |
FlixelShader(String, String)
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:
| Name | Description |
|---|---|
vertSrc | GLSL ES 2.0 vertex shader source code. |
fragSrc | GLSL ES 2.0 fragment shader source code. |
Fields
main() {\n v_color = a_color;\n v_color.a = v_color.a * (255.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)
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 tou_texture)openfl_TextureCoordv- the UV coordinate varying (maps tov_texCoords)openfl_Alpha- global alpha value (constant1.0)openfl_TextureSize- texture dimensions uniform (maps tou_textureSize)openfl_HasColorTransform- color transform flag (constantfalse)flixel_texture2D(t, c)- texture sampling helper (maps totexture2D(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
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)
Creates a FlixelShader from a HaxeFlixel-style fragment shader source string.
The preprocessor performs three transformations before compilation:
- Prepends FlixelShader.HAXEFLIXEL_DEFINES so HaxeFlixel names alias to libGDX names.
- Replaces
#pragma headerwith FlixelShader.HAXEFLIXEL_HEADER_EXPANSION, which declares the texture sampler, size uniform, and UV varyings. - Removes any
#pragma bodylines, which have no meaning outside HaxeFlixel's own template system.
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:
| Name | Description |
|---|---|
fragSrc | HaxeFlixel fragment shader source, typically read from a .frag file. |
Returns: A compiled FlixelShader ready to assign to a FlixelCamera.
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()
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()
Returns the underlying compiled ShaderProgram, or null if FlixelShader.destroy() has been called.
Returns: The compiled shader program.
isCompiled()
Returns true if the shader compiled without errors and is ready to use.
Returns: Whether the underlying ShaderProgram compiled successfully.
getCompiled()
Returns whether this shader compiled without errors and is ready to use.
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)
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:
| Name | Description |
|---|---|
src | Raw HaxeFlixel fragment shader source. |
Returns: Preprocessed GLSL ES 2.0 fragment source ready for libGDX compilation.