ShaderCompiler/ShaderParser.h file

Input-format parser and mini preprocessor for ShaderCompiler.

A ".shader" file is a custom shader language: top-level directive keywords (program/batched/variant/render_mode/shader_type, all plain semicolon- terminated statements), "varying" and "attribute" declarations (an attribute is emitted as a vertex-stage-only "in" global, with a leading "layout(...)" qualifier kept in front of "in"), uniform hints ("texture_unit(N)" assigns the sampler's texture unit), shared globals and vertex()/fragment() entry points. ParseDocuments lowers the file into one or more ShaderDocuments (the internal representation with expanded per-stage line streams), preserving original line numbers for error reporting; everything downstream (variants, preprocessing, reflection, BuildStageSource, emission) runs per document.

Two modes exist. The default ("shader_type custom;", implied when no shader_type statement is present) has no template: globals are shared verbatim by both stages, vertex() becomes the vertex main() verbatim and the fragment() body becomes the fragment main() verbatim — COLOR is the fragment output variable itself ("out vec4 COLOR;"), undefined until written, there is no default and no epilogue, so an early "return" inside fragment() is safe (referencing fragColor is an error — that name does not exist). "shader_type canvas_item;" opts into the sprite template vertex stage, the UV/TEXTURE/COLOR/PALETTE_OFFSET built-ins and the batched twin declared by "batched <Name>;"; a canvas vertex() body is spliced verbatim before the real epilogue (gl_Position + varying stores), so "return" inside canvas vertex() is a parse error. Every program's variant list starts with the unnamed base variant (Name "", no defines, always Variants[0]); "variant <NAME>;" adds one named variant per declaration. A canvas document that references TEXTURE without declaring uTexture gets "uniform sampler2D uTexture;" auto-declared (with texture unit 0) — explicit declarations, with or without a "texture_unit(N)" hint, win. "#ifdef/#ifndef VERTEX_STAGE|FRAGMENT_STAGE" conditionals around shared globals are resolved at assembly time — the emitted sources contain no stage macros at all; stage-specific helper functions need no guards — unused functions are eliminated per stage after assembly (fixpoint; main is the root), and unused uniforms/blocks/defines/structs are eliminated per stage afterwards under a hard reflection-preservation rule (a uniform or block leaves a stage only when the same declaration survives in the other stage, so the merged reflection never changes). The optional "precision mediump|highp;" directive selects the GL ES float precision of the fragment prologue.

Preprocessor implements the object-like subset of the C preprocessor (define/undef, if/ifdef/ifndef/elif/else/endif with integer constant expressions) that is required to produce a per-variant declaration stream for reflection. It is NOT used for the emitted shader sources, which keep the original text verbatim. Two special rules apply (see README.md): GL_ES is never predefined (reflection is taken from the desktop GL view) and BATCH_SIZE is treated as a symbolic constant (defined with value 1 inside if expressions, kept symbolic when used as an array size).

Namespaces

namespace ShaderCompiler

Classes

struct ShaderCompiler::SourceLine
One line of source text together with its 1-based line number in the original ".shader" file.
struct ShaderCompiler::Diagnostic
Error description pointing into the input file.
struct ShaderCompiler::TextureDirective
Texture unit assignment for a sampler uniform — from a "texture_unit(N)" uniform hint or the implicit canvas TEXTURE registration.
struct ShaderCompiler::ShaderDocument
Lowered ".shader" document — directives plus raw (unpreprocessed) per-stage GLSL line streams.
class ShaderCompiler::ShaderParser
Parses and lowers the ".shader" input language.
class ShaderCompiler::Preprocessor
Object-like macro preprocessor used to produce the per-variant declaration stream for reflection.

Enums

enum RenderModeMask : std::uint32_t { RenderModeBlendMix = 0x01, RenderModeBlendAdd = 0x02, RenderModeBlendSub = 0x04, RenderModeBlendMul = 0x08, RenderModeBlendPremulAlpha = 0x10, RenderModeUnshaded = 0x20 }
"render_mode" flags (bit values match ShaderCompiler::RenderMode in the generated ShaderCompilerTypes.h)

Typedefs

using FileReader = std::function<bool(StringView path, String& content)>
Reads the content of the file at path into content, returns false on failure.