GlslShaderTranslator
class GlslShaderTranslator
Known subtypes: VulkanShaderTranslator
Package: dev.joid.lib.bridge.render.shader.source · Module: core · Source: GlslShaderTranslator.java
Rewrites the JOID GLSL stages of a shader into the dialect of a backend, with the declarations, uniforms and wrappers that JOID adds around the body.
Description
Section titled “Description”A translator turns a vertex and fragment pair of ShaderSource into complete sources for one GlslDialect and one UniformLayout, with the body numbered from line 1.
A backend passes one to the constructor of Shader, which translates both stages once; it extends this class when its compiler expects other declarations, as Vulkan adds binding and location qualifiers.
A stage that uses a ShaderFeature the dialect lacks throws UnsupportedOperationException with the feature and the version it needs.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
| GlslShaderTranslator(GlslDialect dialect, UniformLayout layout) | Creates a translator for a dialect and a uniform layout. |
Properties
Section titled “Properties”| Property | Type | Default | Summary |
|---|---|---|---|
| clampToBorder | boolean | false | If true, each texture(sampler, ...) call samples through a function driven by the vec3 uniform joid_Border_<sampler>, which returns transparent black outside the texture for a sampler in CLAMP_TO_BORDER mode, with an edge that fades over one texel, centered on the edge of the texture, for a LINEAR filter. |
| stencil | StencilEmulation.Pass | StencilEmulation.Pass.NONE | Stencil emulation of the fragment stage: NONE adds nothing, TEST discards the fragments that fail the stencil comparison, WRITE outputs the updated stencil value instead of a color. |
Methods
Section titled “Methods”| Returns | Signature | Summary |
|---|---|---|
String | translateVertex(ShaderSource vertex, ShaderSource fragment) | Returns the translated vertex stage. |
String | translateFragment(ShaderSource vertex, ShaderSource fragment) | Returns the translated fragment stage, with the stencil emulation set by stencil. |
ShaderTranslation | translate(ShaderSource vertex, ShaderSource fragment) | Translates both stages into a ShaderTranslation. |
UniformBlock | createBlock(ShaderSource vertex, ShaderSource fragment) | Returns the UniformBlock of the translated uniforms of both stages, with their std140 offsets. |
List<ShaderVariable> | getUniforms(ShaderSource vertex, ShaderSource fragment) | Lists the uniforms of the translated program in block order: the built-in uniforms that a stage uses, the uniforms that JOID adds, then those of the vertex and the fragment stages, each name once. |
List<ShaderVariable> | getSamplers(ShaderSource vertex, ShaderSource fragment) | Lists the samplers of the vertex then the fragment stage, each name once, then joid_Stencil under a stencil emulation; a sampler takes the unit of its place in the list, from 1. |
GlslDialect | getDialect() | Returns the dialect that the translator writes. |
UniformLayout | getLayout() | Returns the uniform layout of the translated program. |
For subclasses
Section titled “For subclasses”| Returns | Signature | Summary |
|---|---|---|
String | getBlockLayout() hook protected | Hook that returns the qualifiers written in layout(...) before the uniform block: std140 by default. |
String | declareSampler(ShaderVariable sampler, int unit) hook protected | Hook that declares a sampler; unit is its texture unit, from 1. |
String | declareAttribute(ShaderBuiltin builtin, int location) hook protected | Hook that declares a vertex attribute at its location from VertexAttribute: layout(location = n) in when the dialect has explicit locations, in or attribute otherwise. |
String | declareVarying(ShaderVariable varying, int location, boolean output) hook protected | Hook that declares a varying: output is true in the vertex stage and false in the fragment stage, and location is the same in both stages. |
Constants and fields
Section titled “Constants and fields”| Type | Name | Value | Summary |
|---|---|---|---|
String | BLOCK | "JoidUniforms" | Name of the uniform block that holds every uniform under UniformLayout.BLOCK. |
String | ALPHA_TEST | "joid_AlphaTest" | Name of the int uniform that turns on the alpha test: when it is not 0, the fragment stage discards the fragments whose alpha is at most ALPHA_THRESHOLD. |
String | ALPHA_THRESHOLD | "joid_AlphaThreshold" | Name of the float uniform that holds the alpha cutoff of the render state, written by Shader.builtins. |
String | STENCIL | "joid_Stencil" | Name of the sampler that holds the stencil values, one byte in the red channel, read at the position of the fragment. |
String | STENCIL_TEST | "joid_StencilTest" | Name of the int uniform that turns on the stencil comparison of the TEST pass, written by StencilEmulation.write with the other stencil uniforms. |
String | STENCIL_MASK | "joid_StencilMask" | Name of the int uniform that holds the mask applied to the reference and to the stored value before the comparison. |
String | STENCIL_FAIL | "joid_StencilFail" | Name of the int uniform that holds the operation of the WRITE pass when the comparison fails, as the ordinal of StencilOperation. |
String | STENCIL_PASS | "joid_StencilPass" | Name of the int uniform that holds the operation of the WRITE pass when the comparison passes, as the ordinal of StencilOperation. |
String | STENCIL_FUNCTION | "joid_StencilFunction" | Name of the int uniform that holds the comparison, as the ordinal of StencilFunction. |
String | STENCIL_REFERENCE | "joid_StencilReference" | Name of the int uniform that holds the reference of the comparison and of the REPLACE operation. |
String | LINE_WIDTH | "joid_LineWidth" | Name of the float uniform of a line shader that holds the line width of the render state, in screen pixels: lineWidth multiplied by the scale of the PixelGrid. |
String | LINE_VIEWPORT | "joid_LineViewport" | Name of the vec2 uniform of a line shader that holds the size of the viewport in pixels, which turns the width of a line into clip space. |
String | VERTEX_COLOR | "joid_VertexColor" | Name of the int uniform that tells whether the vertex buffer carries colors: when it is 0, every read of aColor gives CURRENT_COLOR. |
String | CURRENT_COLOR | "joid_CurrentColor" | Name of the vec4 uniform that holds the current color of the render state, for the vertices without a color. |
String | BORDER | "joid_Border_" | Prefix of the vec3 uniform of each sampler under clampToBorder, followed by the name of the sampler. |
Creation details
Section titled “Creation details”GlslShaderTranslator
Section titled “GlslShaderTranslator”GlslShaderTranslator(GlslDialect dialect, UniformLayout layout)
Creates a translator for a dialect and a uniform layout.
Throws IllegalArgumentException when the layout is BLOCK and the dialect has no uniform blocks.
Property details
Section titled “Property details”clampToBorder
Section titled “clampToBorder”this clampToBorder(boolean clampToBorder)fluentboolean isClampToBorder()
Default: false
If true, each texture(sampler, ...) call samples through a function driven by the vec3 uniform joid_Border_<sampler>, which returns transparent black outside the texture for a sampler in CLAMP_TO_BORDER mode, with an edge that fades over one texel, centered on the edge of the texture, for a LINEAR filter.
The constructor of Shader turns it on when the render bridge cannot wrap to CLAMP_TO_BORDER.
stencil
Section titled “stencil”this stencil(StencilEmulation.Pass stencil)fluentStencilEmulation.Pass getStencil()
Default: StencilEmulation.Pass.NONE
Stencil emulation of the fragment stage: NONE adds nothing, TEST discards the fragments that fail the stencil comparison, WRITE outputs the updated stencil value instead of a color.
Any value but NONE declares the stencil uniforms and sampler, adds the WRITE stage to translate, and needs GLSL 1.30 or ESSL 3.00.
Method details
Section titled “Method details”translateVertex
Section titled “translateVertex”String translateVertex(ShaderSource vertex, ShaderSource fragment)
Returns the translated vertex stage. It reads the fragment source too, so that both stages declare the same uniforms and the same varying locations.
translateFragment
Section titled “translateFragment”String translateFragment(ShaderSource vertex, ShaderSource fragment)
Returns the translated fragment stage, with the stencil emulation set by stencil.
translate
Section titled “translate”ShaderTranslation translate(ShaderSource vertex, ShaderSource fragment)
Translates both stages into a ShaderTranslation.
With a stencil emulation, the translation also carries the fragment stage of the WRITE pass; otherwise its stencil fragment is null.
createBlock
Section titled “createBlock”UniformBlock createBlock(ShaderSource vertex, ShaderSource fragment)
Returns the UniformBlock of the translated uniforms of both stages, with their std140 offsets.
getUniforms
Section titled “getUniforms”List<ShaderVariable> getUniforms(ShaderSource vertex, ShaderSource fragment)
Lists the uniforms of the translated program in block order: the built-in uniforms that a stage uses, the uniforms that JOID adds, then those of the vertex and the fragment stages, each name once. The samplers are not in the list.
getSamplers
Section titled “getSamplers”List<ShaderVariable> getSamplers(ShaderSource vertex, ShaderSource fragment)
Lists the samplers of the vertex then the fragment stage, each name once, then joid_Stencil under a stencil emulation; a sampler takes the unit of its place in the list, from 1.
getDialect
Section titled “getDialect”GlslDialect getDialect()
Returns the dialect that the translator writes.
getLayout
Section titled “getLayout”UniformLayout getLayout()
Returns the uniform layout of the translated program.
getBlockLayout
Section titled “getBlockLayout”String getBlockLayout() hook protected
Hook that returns the qualifiers written in layout(...) before the uniform block: std140 by default.
Override it to add a binding, as Vulkan does with std140, binding = 0.
declareSampler
Section titled “declareSampler”String declareSampler(ShaderVariable sampler, int unit) hook protected
Hook that declares a sampler; unit is its texture unit, from 1.
Override it to add a binding qualifier.
declareAttribute
Section titled “declareAttribute”String declareAttribute(ShaderBuiltin builtin, int location) hook protected
Hook that declares a vertex attribute at its location from VertexAttribute: layout(location = n) in when the dialect has explicit locations, in or attribute otherwise.
declareVarying
Section titled “declareVarying”String declareVarying(ShaderVariable varying, int location, boolean output) hook protected
Hook that declares a varying: output is true in the vertex stage and false in the fragment stage, and location is the same in both stages.
Override it to add a location qualifier.
Constant and field details
Section titled “Constant and field details”String BLOCK
Name of the uniform block that holds every uniform under UniformLayout.BLOCK.
ALPHA_TEST
Section titled “ALPHA_TEST”String ALPHA_TEST
Name of the int uniform that turns on the alpha test: when it is not 0, the fragment stage discards the fragments whose alpha is at most ALPHA_THRESHOLD.
Shader.builtins writes it from the alpha cutoff of the render state.
ALPHA_THRESHOLD
Section titled “ALPHA_THRESHOLD”String ALPHA_THRESHOLD
Name of the float uniform that holds the alpha cutoff of the render state, written by Shader.builtins.
STENCIL
Section titled “STENCIL”String STENCIL
Name of the sampler that holds the stencil values, one byte in the red channel, read at the position of the fragment.
The backend binds it for the TEST and WRITE passes.
STENCIL_TEST
Section titled “STENCIL_TEST”String STENCIL_TEST
Name of the int uniform that turns on the stencil comparison of the TEST pass, written by StencilEmulation.write with the other stencil uniforms.
STENCIL_MASK
Section titled “STENCIL_MASK”String STENCIL_MASK
Name of the int uniform that holds the mask applied to the reference and to the stored value before the comparison.
STENCIL_FAIL
Section titled “STENCIL_FAIL”String STENCIL_FAIL
Name of the int uniform that holds the operation of the WRITE pass when the comparison fails, as the ordinal of StencilOperation.
STENCIL_PASS
Section titled “STENCIL_PASS”String STENCIL_PASS
Name of the int uniform that holds the operation of the WRITE pass when the comparison passes, as the ordinal of StencilOperation.
STENCIL_FUNCTION
Section titled “STENCIL_FUNCTION”String STENCIL_FUNCTION
Name of the int uniform that holds the comparison, as the ordinal of StencilFunction.
STENCIL_REFERENCE
Section titled “STENCIL_REFERENCE”String STENCIL_REFERENCE
Name of the int uniform that holds the reference of the comparison and of the REPLACE operation.
LINE_WIDTH
Section titled “LINE_WIDTH”String LINE_WIDTH
Name of the float uniform of a line shader that holds the line width of the render state, in screen pixels: lineWidth multiplied by the scale of the PixelGrid.
LINE_VIEWPORT
Section titled “LINE_VIEWPORT”String LINE_VIEWPORT
Name of the vec2 uniform of a line shader that holds the size of the viewport in pixels, which turns the width of a line into clip space.
VERTEX_COLOR
Section titled “VERTEX_COLOR”String VERTEX_COLOR
Name of the int uniform that tells whether the vertex buffer carries colors: when it is 0, every read of aColor gives CURRENT_COLOR.
CURRENT_COLOR
Section titled “CURRENT_COLOR”String CURRENT_COLOR
Name of the vec4 uniform that holds the current color of the render state, for the vertices without a color.
BORDER
Section titled “BORDER”String BORDER
Prefix of the vec3 uniform of each sampler under clampToBorder, followed by the name of the sampler.
Shader.builtins writes the mode (0 off, 1 nearest, 2 linear), then the width and the height of the texture.
fluent returns the instance, typed for chaining · factory creates an instance · signal also accepts a value that follows signals, as a signal, an expression or a lambda (When does JOID compute this value again?) · hook called by JOID, override it · abstract you implement it · default interface method with a body · static called on the class · protected visible to subclasses
