Skip to content

Shader translation

JOID writes each shader once, in JOID GLSL, and the core translates it for each backend. A backend never edits shader code: it picks a dialect and a uniform layout, compiles what the translator gives it, and sends the uniform values at each draw. The language itself and its built-in names are on Write JOID GLSL.

  1. ShaderSource.from reads one stage: it drops #version, collects the in, out and uniform declarations and the samplers, notes which built-in names the code uses, and detects the ShaderFeatures it needs.
  2. Your render bridge receives both stages in createShader(vertex, fragment, blend) and builds its Shader subclass with a GlslShaderTranslator.
  3. The constructor of Shader builds the UniformBlock and the samplers, numbered from unit 1, and translates both stages into a ShaderTranslation: the dialect, the vertex code, the fragment code and, with stencil emulation, a second fragment.
  4. The first call of isActive() calls your compileProgram(translation).

CoreShader lists the shaders of the core (default, font, bitmap, rounded, border, shadow, blur, circle, gradient, line), read from /assets/shaders/<name>/<name>.vsh and .fsh.

GlslDialect covers GLSL 1.10 to 4.50 and ESSL 1.00 and 3.00. The dialect decides how the translator writes each declaration:

CapabilityFromBelow it, the translator writes
in and outGLSL 1.30, ESSL 3.00attribute, varying, gl_FragColor and texture2D
Uniform blocksGLSL 1.40, ESSL 3.00loose uniforms only
Explicit locationsGLSL 3.30, ESSL 3.00no layout(location); the OpenGL module binds the locations before linking
Implicit conversionsGLSL 1.20, never on ESSLinteger literals rewritten as floats where a float is expected; an integer variable still needs float(...)

The OpenGL module picks the highest desktop dialect of the context, up to GLSL 3.30. The Vulkan backend uses GLSL 4.50 with VulkanShaderTranslator, which adds the binding and location qualifiers of SPIR-V.

A shader that uses a ShaderFeature that the dialect lacks (Write JOID GLSL lists them), or the derivatives on ESSL 1.00, fails to translate with an UnsupportedOperationException that names the feature and the dialect it needs. In dev mode, the core also warns once about any feature beyond GLSL 1.20, which every OpenGL 2.1 context accepts, so that a shader written on a recent machine still runs everywhere.

UniformLayoutDeclaresSend the values with
BLOCKOne std140 block named JoidUniformsUniformBlock.pack, which returns true when a value changed, then the bytes of UniformBlock.getData
LOOSEOne uniform per valueUniformBlock.upload, which calls you for each changed UniformMember

BLOCK needs a dialect with uniform blocks: the constructor of GlslShaderTranslator throws an IllegalArgumentException otherwise. The UniformBlock computes the std140 offsets and strides of every UniformType, scalars, vectors and matrices, arrays included, and marks each member dirty when its value changes.

To change the declarations for your API, extend GlslShaderTranslator and override its hooks:

HookWrites
getBlockLayout()The qualifiers of the uniform block, std140 by default (Vulkan writes std140, binding = 0).
declareSampler(sampler, unit)The declaration of a sampler and its unit.
declareAttribute(builtin, location)The declaration of a vertex attribute and its location.
declareVarying(varying, location, output)The declaration of a varying, as an output of the vertex stage or an input of the fragment stage.

Besides the declarations, each translated shader carries uniforms that the core fills from the state at each draw: Shader.builtins fills all of them but the stencil ones, which StencilEmulation.write fills.

UniformFilled fromEffect
joid_AlphaTest, joid_AlphaThresholdalphaCutoffThe fragment is discarded when its alpha is at most the threshold.
joid_CurrentColor, joid_VertexColorcolor(...), isColor()Every read of aColor becomes the vertex color, or the current color when the buffer has none.
joid_LineWidth, joid_LineViewportlineWidth multiplied by the scale of the PixelGrid, viewportThe line variant of the shader widens each segment in screen space and smooths its edges.
joid_Border_<sampler>the wrap of the samplerWith border emulation, a sample outside the texture fades to transparent.
joid_Stencil…the stencil stateWith stencil emulation, the stencil test and write in the fragment shader.

IShader.getLineShader derives a second shader from the same sources: its vertex stage receives both ends of each segment and expands them into a quad of the requested width. The tessellator binds it for wide or smooth lines; a bridge receives ordinary triangles.

When IRenderBridge.canWrap refuses CLAMP_TO_BORDER, the constructor of Shader turns on clampToBorder in the translator. Each texture(sampler, uv) then goes through a function that returns transparent outside [0, 1], with a filtered edge for a LINEAR sampler, so the result matches a real transparent border.

An API without a stencil buffer on its targets can emulate it. StencilEmulation.Pass.TEST, given to translator.stencil(...), makes the fragment shader read a stencil texture (joid_Stencil) and discard what fails the test; the translation then carries getStencilFragment(), the pass that writes the new stencil value as a color. StencilEmulation.from computes the test and the operations from the state, and write(block) puts them into the uniforms. The emulation needs GLSL 1.30, and no official backend uses it: OpenGL and Vulkan have a real stencil buffer on the screen.

GlslExport, in the testkit, writes every core shader translated into every dialect into a folder, to read them or to pass them to an external compiler such as glslangValidator. The render contract suite compiles every core shader on your backend (compilesCoreShaders), and checks that a shader that fails to compile is skipped (Testing a backend).

Report a problem with this page