---
title: "emitGlslModule(): function in Emit, TypeShade API reference"
description: "emitGlslModule, a function in Emit. Emit a module as GLSL ES 3.00 source."
url: https://typeshade.dev/api/emitglslmodule/
language: en
---

# `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 af54ddad5.

## Syntax

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

## Parameters

**`m` `ModuleDecl`**

the module to emit.

**`stage` optional `'vertex' | 'fragment'`**

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

**`opts` optional `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`](https://typeshade.dev/api/unsupportedfeatureerror/) ([`SD0030`](https://typeshade.dev/reference/errors/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`](https://typeshade.dev/api/emitglslstages/)’s `vertexEntry` or `fragmentEntry`), or one texture sampled with different samplers in the emitted artifact. Bind the same texture through separate texture declarations to give each its sampler.

## 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`](https://typeshade.dev/api/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`](https://typeshade.dev/api/lowercomputetofragment/).

`opts.floatPrecision` sets the `precision <p> float;` line; see [`GlslEmitOptions.floatPrecision`](https://typeshade.dev/api/glslemitoptions/) for when `'mediump'` is safe. `opts.overrideValues` pins specialization constants as `#define`s, since GLSL ES 3.00 has no pipeline-time equivalent. The shared [`EmitOptions`](https://typeshade.dev/api/emitoptions/) apply too: `parens`, `plugins` and `fp64Flavor`.

## Examples

### Example

```ts
import { emitGlslModule } from 'typeshade'

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

## Targets

| Target | Support | Notes |
| --- | --- | --- |
| 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

- [Emitting and reflection](https://typeshade.dev/guide/internals/emitting-and-reflection/)
- [Conditional programs](https://typeshade.dev/guide/internals/conditional-programs/)
- [GLSL float precision](https://typeshade.dev/guide/internals/glsl-float-precision/)
- [Production emit](https://typeshade.dev/guide/internals/production-emit/)

## See also

- [`wgslLayout`](https://typeshade.dev/api/wgsllayout/)
- [`lowerComputeToFragment`](https://typeshade.dev/api/lowercomputetofragment/)
- [`GlslEmitOptions`](https://typeshade.dev/api/glslemitoptions/)
- [`EmitOptions`](https://typeshade.dev/api/emitoptions/)
- [`UnsupportedFeatureError`](https://typeshade.dev/api/unsupportedfeatureerror/)
- [`emitGlslStages`](https://typeshade.dev/api/emitglslstages/)
- [`emitModule`](https://typeshade.dev/api/emitmodule/)

## Source

[`src/core/backends/glsl.ts`, line 109](https://github.com/typeshade/typeshade/blob/af54ddad5/src/core/backends/glsl.ts#L109), at commit af54ddad5
