Skip to content

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.

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.

ReturnsSignatureSummary
GlslShaderTranslator(GlslDialect dialect, UniformLayout layout)Creates a translator for a dialect and a uniform layout.

PropertyTypeDefaultSummary
clampToBorderbooleanfalseIf 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.
stencilStencilEmulation.PassStencilEmulation.Pass.NONEStencil 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.

ReturnsSignatureSummary
StringtranslateVertex(ShaderSource vertex, ShaderSource fragment)Returns the translated vertex stage.
StringtranslateFragment(ShaderSource vertex, ShaderSource fragment)Returns the translated fragment stage, with the stencil emulation set by stencil.
ShaderTranslationtranslate(ShaderSource vertex, ShaderSource fragment)Translates both stages into a ShaderTranslation.
UniformBlockcreateBlock(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.
GlslDialectgetDialect()Returns the dialect that the translator writes.
UniformLayoutgetLayout()Returns the uniform layout of the translated program.
ReturnsSignatureSummary
StringgetBlockLayout() hook protectedHook that returns the qualifiers written in layout(...) before the uniform block: std140 by default.
StringdeclareSampler(ShaderVariable sampler, int unit) hook protectedHook that declares a sampler; unit is its texture unit, from 1.
StringdeclareAttribute(ShaderBuiltin builtin, int location) hook protectedHook that declares a vertex attribute at its location from VertexAttribute: layout(location = n) in when the dialect has explicit locations, in or attribute otherwise.
StringdeclareVarying(ShaderVariable varying, int location, boolean output) hook protectedHook 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.
TypeNameValueSummary
StringBLOCK"JoidUniforms"Name of the uniform block that holds every uniform under UniformLayout.BLOCK.
StringALPHA_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.
StringALPHA_THRESHOLD"joid_AlphaThreshold"Name of the float uniform that holds the alpha cutoff of the render state, written by Shader.builtins.
StringSTENCIL"joid_Stencil"Name of the sampler that holds the stencil values, one byte in the red channel, read at the position of the fragment.
StringSTENCIL_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.
StringSTENCIL_MASK"joid_StencilMask"Name of the int uniform that holds the mask applied to the reference and to the stored value before the comparison.
StringSTENCIL_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.
StringSTENCIL_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.
StringSTENCIL_FUNCTION"joid_StencilFunction"Name of the int uniform that holds the comparison, as the ordinal of StencilFunction.
StringSTENCIL_REFERENCE"joid_StencilReference"Name of the int uniform that holds the reference of the comparison and of the REPLACE operation.
StringLINE_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.
StringLINE_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.
StringVERTEX_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.
StringCURRENT_COLOR"joid_CurrentColor"Name of the vec4 uniform that holds the current color of the render state, for the vertices without a color.
StringBORDER"joid_Border_"Prefix of the vec3 uniform of each sampler under clampToBorder, followed by the name of the sampler.

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.

  • this clampToBorder(boolean clampToBorder) fluent
  • boolean 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.

  • this stencil(StencilEmulation.Pass stencil) fluent
  • StencilEmulation.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.

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.

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. With a stencil emulation, the translation also carries the fragment stage of the WRITE pass; otherwise its stencil fragment is null.

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. The samplers are not in the list.

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.

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.

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.

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. Override it to add a location qualifier.

String BLOCK

Name of the uniform block that holds every uniform under UniformLayout.BLOCK.

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.

String ALPHA_THRESHOLD

Name of the float uniform that holds the alpha cutoff of the render state, written by Shader.builtins.

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.

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.

String STENCIL_MASK

Name of the int uniform that holds the mask applied to the reference and to the stored value before the comparison.

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.

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.

String STENCIL_FUNCTION

Name of the int uniform that holds the comparison, as the ordinal of StencilFunction.

String STENCIL_REFERENCE

Name of the int uniform that holds the reference of the comparison and of the REPLACE operation.

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.

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.

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.

String CURRENT_COLOR

Name of the vec4 uniform that holds the current color of the render state, for the vertices without a color.

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

Report a problem with this page