Rendered from AUTHORING.md at commit af54ddad5. The package is imported here by its 0.2.0 name, typeshade.
GLSL float precision
The float precision option sets the default qualifier in the emitted GLSL stage. Its numeric and performance effects depend on the implementation and the shader.
GLSL ES 3.00 predeclares highp float precision in vertex shaders; fragment shaders have
no default float precision. TypeShade writes precision highp float; in both stages
unless the emit option selects mediump. This default applies to floating-point declarations
without an explicit precision qualifier. It does not override an explicit qualifier.
The floatPrecision option
emitGlslModule and emitGlslStages accept a floatPrecision value of 'highp' or
'mediump' in their options bag:
import { emitGlslModule } from 'typeshade'
const fs = emitGlslModule(m, 'fragment', { floatPrecision: 'mediump' })'highp' is the default and it is byte-neutral: omitting the option gives the same bytes
as passing it. 'mediump' moves exactly one token in the whole emit, the qualifier on the
float line.
The choice happens at build time. It is an emit option, so it is not a runtime probe of the device the program will run on. If you cache emitted source, put the precision into the cache key, because a key without it can hand a mediump program back to a caller that asked for highp.
What a whole-stage default covers
Mediump specifies minimum range and precision requirements; it does not require IEEE 754
binary16 storage or arithmetic. An implementation can provide higher precision, including
f32. The binary16 maximum finite value is therefore not a portable mediump range limit.
GLSL ES 3.00 sections 4.5.1 to 4.5.4
define the requirements, expression precision rules and stage defaults.
The default affects unqualified positions, coordinates, varyings and local variables. An expression’s precision also depends on its operands and surrounding context. A bounded final colour does not ensure that its intermediate calculations fit mediump range and precision.
Keep highp for calculations that need its range or precision, including the f32 components
of the fp64 emulation. Assess mediump against the range and error
budget of the complete shader. Since the option is per emit call, a program can use different
defaults for its vertex and fragment stages:
import { emitGlslModule } from 'typeshade'
const vertex = emitGlslModule(m, 'vertex') // positions stay highpconst fragment = emitGlslModule(m, 'fragment', { floatPrecision: 'mediump' })emitGlslStages(m, opts) takes the same options bag and applies it to both stages, so
use it when both stages want the same qualifier and you want the shared lowering it pays
for once.
What stays highp
The option spells the float line and nothing else. Two other precision lines in the header are load-bearing and stay at highp under either setting.
precision highp int; is one of them. GLSL ES 3.00 predeclares highp int precision in
vertex shaders and mediump int precision in fragment shaders. TypeShade explicitly selects
highp in both stages. The index math that reads storage through a data texture and the integer
half of a bitcast need its full integer range.
The sampler lines are the other. GLSL ES 3.00 predeclares a default precision for
sampler2D and samplerCube only, so a module that declares a sampler2DArray, a
usampler2D or an isampler2DArray gets its own precision highp <type>; line for each
shape it uses, and precision highp float; does not cover them. Those lines are a
compile requirement, so they keep their qualifier. A fragment stage that samples one
sampler2DArray, emitted at mediump, opens with:
#version 300 esprecision mediump float;precision highp int;precision highp sampler2DArray;What a CI run cannot tell you
The backend’s unit tests check the header shape. A real driver can check whether both settings compile and link for a particular shader. Numeric comparisons in CI can detect differences on that runner, but equal results do not establish equal behavior on other devices.
An implementation can evaluate mediump operations at higher precision. The values from
getShaderPrecisionFormat describe the reported range and precision; they do not measure
execution time or force every operation to use a particular hardware format.
Measure errors, range failures, rendering artifacts and performance on the target devices. These effects are not restricted to mobile hardware. Reduced precision can improve performance or power use on some implementations, but the qualifier alone guarantees neither benefit.
What a build can assert is that the option changed one line and left the rest of the emit alone:
import { emitGlslModule } from 'typeshade'
const highp = emitGlslModule(m, 'fragment')const mediump = emitGlslModule(m, 'fragment', { floatPrecision: 'mediump' })
mediump.replace('precision mediump float;', 'precision highp float;') === highp // trueUse mediump only where the shader meets its error budget on the target implementations. Retain highp where the required range or precision exceeds mediump’s portable guarantees.