emitGlslModule
On this page

emitGlslModule()

Function in Emit

Emit a module as GLSL ES 3.00 source.

import { emitGlslModule } from 'typeshade'

The signature, the description and the examples come from the compiler's own source at commit 26de7be8.

Syntax

function emitGlslModule(
m: ModuleDecl,
stage?: 'vertex' | 'fragment',
opts?: GlslEmitOptions,
): string

Parameters

m ModuleDecl

the module to emit.

stageoptional 'vertex' | 'fragment'

which stage to emit, or omitted for the whole module.

optsoptional GlslEmitOptions

the GLSL options and the shared emit options.

Return value

string

the GLSL ES 3.00 source, #version 300 es first.

Exceptions

@link ValidationError

when the module fails a core rule, and UnsupportedFeatureError (SD0030) when this target cannot spell something the module needs: a @compute entry that is not declared portable, a multisampled texture load, f16, subgroups, a read_write storage binding, a storage element type outside the list above, a vertex entry returning a bare non-struct output, a raw statement with no glsl text, or a stage with more than one entry, which has one main() to give them (name it with emitGlslStages’s vertexEntry or fragmentEntry).

Description

Pass 'vertex' or 'fragment' to get one compilable stage, the string gl.shaderSource takes: that stage’s entry becomes void main(), and only the structs, bindings and helper functions the entry reaches are declared. Omit the stage for a whole-module string; a module with entries for both stages then contains two main() functions, which no GLSL compiler accepts, so the whole-module form is for modules without entries and for inspecting output.

The source is assembled in this order: #version 300 es, any #extension directive a declared capability needs, the precision lines, a #define per override, plain structs, consts and module variables, layout(std140) uniform blocks and sampler uniforms, helper functions, then the entry: its in/out varyings, a <name>_impl function where the body cannot be inlined into main(), and main() itself. A uniform struct binding becomes a std140 block whose byte offsets are the ones wgslLayout reports, so a host packs one buffer for both backends. A storage binding, which WebGL2 cannot express, is read from a data texture instead: array<f32>, array<u32>, array<i32>, array<vecN<f32>> and arrays of structs with f32, u32 and vecN<f32> fields are supported, and data[i] becomes a texelFetch at (i % width, i / width) in a sampler2D (R32F), usampler2D (R32UI) or isampler2D (R32I) of the same name. A compute entry declared portable: true is emitted as a fragment pass; see lowerComputeToFragment.

opts.floatPrecision sets the precision <p> float; line; see GlslEmitOptions.floatPrecision for when 'mediump' is safe. opts.overrideValues pins specialization constants as #defines, since GLSL ES 3.00 has no pipeline-time equivalent. The shared EmitOptions apply too: parens, plugins and fp64Flavor.

Examples

Example

import { emitGlslModule } from 'typeshade'
const vs = emitGlslModule(MODULE, 'vertex')
const fs = emitGlslModule(MODULE, 'fragment', { floatPrecision: 'mediump' })

Targets

TargetSupportNotes
WGSL (WebGPU) Does not apply
GLSL ES 3.00 (WebGL2) Supported

Writes GLSL ES 3.00 for WebGL2.

CPU oracle Does not apply

In the guide

See also

Source

src/core/backends/glsl.ts, line 2461, at commit 26de7be8

Edit this page Report a problem