Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
esengine::resource::ShaderParser Class Reference

Parser for .esshader unified shader format. More...

#include <ShaderParser.hpp>

Classes

struct  AssembledStage
 Like assembleStage, but also reports the line count of the assembled prefix (version + variant + sharedCode). More...

Static Public Member Functions

static ParsedShader parse (const std::string &source)
 Parses shader source into structured format (no include support).
static ParsedShader parse (const std::string &source, const ShaderIncludeResolver &resolver)
 Parses shader source with #include "path" expansion.
static std::string assembleStage (const ParsedShader &parsed, ShaderStage stage, const std::string &platform="", const std::vector< std::string > &features={}, ShaderTargetLanguage target=ShaderTargetLanguage::GLSL_ES300)
 Assembles final GLSL source for a specific stage.
static std::string canonicalVaryingsWGSL (const ParsedShader &parsed)
 The WGSL varying struct the engine's vertex stage emits for parsed, or empty when the shader writes its own vertex stage.
static std::string variantKey (const std::vector< std::string > &features)
 Stable cache key for a feature set (order-independent), e.g. "GRAYSCALE|TINT". Consumers key their compiled-variant cache on (shader, variantKey(features)).
static std::vector< std::string > splitFeatures (const std::string &csv)
 Split a comma-separated feature/switch list into trimmed, non-empty entries — the form the boundary carries a permutation in.
static std::vector< std::string > featuresRead (const ParsedShader &parsed, const std::vector< std::string > &features, ShaderTargetLanguage target=ShaderTargetLanguage::GLSL_ES300)
static void setLinearColorSpace (bool linear)
 Process-global color-space mode. When linear, every assembled stage compiles with ES_LINEAR defined (GLSL #define / WGSL assembly-time preprocessor). Set BEFORE the renderer compiles shaders; flipping later requires a reload. Variant caches must fold linearColorSpace() into their keys if they can outlive a flip.
static bool linearColorSpace ()
static AssembledStage assembleStageEx (const ParsedShader &parsed, ShaderStage stage, const std::string &platform="", const std::vector< std::string > &features={}, ShaderTargetLanguage target=ShaderTargetLanguage::GLSL_ES300)
static std::string remapCompilerLog (const std::string &log, const std::vector< SourceLine > &stageMap, u32 headerLineOffset)
 Rewrites GL compile-log line references back to original files.
static u32 materialOverflowUnits (const ParsedShader &shader)
 The units below the material base a texture param may take on shader (a mask): the ones it never reads from the engine.

Detailed Description

Parser for .esshader unified shader format.

Parses shader files that contain multiple stages in a single file. Uses #pragma directives to separate sections.

File format:

#pragma shader "MyShader"
#pragma version 300 es
#pragma properties
uniform sampler2D u_texture; // @property(type=texture)
#pragma end
#pragma vertex
// vertex shader code (GLSL)
#pragma end
#pragma fragment
// fragment shader code (GLSL)
#pragma end
#pragma fragment wgsl
// WGSL twin of the fragment stage (assembled for the WGSL target;
// fragment-only 2D shaders get the canonical WGSL vertex + VSOut injected)
#pragma end

Definition at line 296 of file ShaderParser.hpp.

Member Function Documentation

◆ assembleStage()

std::string esengine::resource::ShaderParser::assembleStage ( const ParsedShader & parsed,
ShaderStage stage,
const std::string & platform = "",
const std::vector< std::string > & features = {},
ShaderTargetLanguage target = ShaderTargetLanguage::GLSL_ES300 )
static

Assembles final GLSL source for a specific stage.

Parameters
parsedThe parsed shader data
stageThe shader stage to assemble
platformPlatform variant name (empty for default)
featuresFeature keywords to enable; each is injected as #define <name> 1 right after #version, so shaders can #ifdef compile-time variants.
Returns
Complete GLSL source ready for compilation

Definition at line 1235 of file ShaderParser.cpp.

References assembleStageEx(), and esengine::resource::ShaderParser::AssembledStage::source.

Referenced by esengine::esshader_cookInfo(), featuresRead(), esengine::ImmediateDraw::init(), esengine::ParticlePlugin::init(), esengine::ShapePlugin::init(), esengine::material_compileEsshader(), esengine::MaterialStore::prepassCoverageProgram(), and esengine::MaterialStore::shadowProgram().

◆ assembleStageEx()

◆ canonicalVaryingsWGSL()

std::string esengine::resource::ShaderParser::canonicalVaryingsWGSL ( const ParsedShader & parsed)
static

The WGSL varying struct the engine's vertex stage emits for parsed, or empty when the shader writes its own vertex stage.

A fragment twin generated for a shader on the engine's vertex stage reads its inputs at these locations, so every vertex source the engine retargets the material to feeds the same fragment.

Definition at line 3029 of file ShaderParser.cpp.

Referenced by esengine::esshader_cookInfo().

◆ featuresRead()

std::vector< std::string > esengine::resource::ShaderParser::featuresRead ( const ParsedShader & parsed,
const std::vector< std::string > & features,
ShaderTargetLanguage target = ShaderTargetLanguage::GLSL_ES300 )
static

◆ linearColorSpace()

bool esengine::resource::ShaderParser::linearColorSpace ( )
static

Definition at line 3035 of file ShaderParser.cpp.

Referenced by esengine::RenderFrame::init().

◆ materialOverflowUnits()

u32 esengine::resource::ShaderParser::materialOverflowUnits ( const ParsedShader & shader)
static

The units below the material base a texture param may take on shader (a mask): the ones it never reads from the engine.

The units below MATERIAL_TEXTURE_UNIT_BASE a material texture may take on shader, as a mask: those the engine binds per draw that this shader never reads. Read are what its domain's injected code takes (Lit: shadow map, reflections, bake, both 2D masks; every mesh: morph deltas) and what its own text names in either language: batch textures (batchUnitsRead), an engine sampler, a tN/sN unit. A fullscreen pass and the UI reach their low units through their own conventions and keep none free.

Definition at line 4854 of file ShaderParser.cpp.

References esengine::ENV_MAP_TEXTURE_UNIT, esengine::LIGHTMAP_TEXTURE_UNIT, esengine::MORPH_DELTA_TEXTURE_UNIT, esengine::SHADOW_2D_TEXTURE_UNIT, esengine::SHADOW_MAP_TEXTURE_UNIT, and esengine::SHAPE_2D_TEXTURE_UNIT.

◆ parse() [1/2]

ParsedShader esengine::resource::ShaderParser::parse ( const std::string & source)
static

Parses shader source into structured format (no include support).

Parameters
sourceThe complete .esshader file content
Returns
Parsed shader data with valid flag indicating success

Definition at line 898 of file ShaderParser.cpp.

References parse().

Referenced by esengine::esshader_cookInfo(), esengine::ImmediateDraw::init(), esengine::ParticlePlugin::init(), esengine::ShapePlugin::init(), esengine::material_compileEsshader(), parse(), esengine::MaterialStore::prepassCoverageProgram(), and esengine::MaterialStore::shadowProgram().

◆ parse() [2/2]

ParsedShader esengine::resource::ShaderParser::parse ( const std::string & source,
const ShaderIncludeResolver & resolver )
static

Parses shader source with #include "path" expansion.

Parameters
sourceThe complete .esshader file content
resolverCallback used to fetch included file contents
Returns
Parsed shader data with valid flag indicating success

Definition at line 902 of file ShaderParser.cpp.

References esengine::resource::ParsedShader::coverageParam, esengine::resource::ShaderSwitch::defaultOn, esengine::resource::ParsedShader::depthInput, esengine::resource::ParsedShader::domain, esengine::resource::ParsedShader::errorMessage, esengine::resource::ParsedShader::expandedLineMap, esengine::resource::ParsedShader::features, esengine::resource::Fragment, esengine::resource::Global, esengine::resource::ParsedShader::hairShadowParam, esengine::resource::ParsedShader::highPrecision, esengine::resource::ParsedShader::lightingDirect, esengine::resource::ParsedShader::name, esengine::resource::ShaderProperty::name, esengine::resource::ShaderSwitch::name, esengine::resource::Properties, esengine::resource::ParsedShader::properties, esengine::resource::ParsedShader::sharedCode, esengine::resource::ParsedShader::stageLineMaps, esengine::resource::ParsedShader::stages, esengine::resource::ParsedShader::switches, esengine::resource::ParsedShader::tipParam, esengine::resource::ParsedShader::usesGBuffer, esengine::resource::ParsedShader::usesLights, esengine::resource::ParsedShader::valid, esengine::resource::Variant, esengine::resource::ParsedShader::variants, esengine::resource::ParsedShader::version, esengine::resource::Vertex, esengine::resource::ParsedShader::vertexBounds, esengine::resource::VertexHook, esengine::resource::ParsedShader::vertexHook, esengine::resource::ParsedShader::vertexIsCanonical, esengine::resource::ParsedShader::wgslStageFull, esengine::resource::ParsedShader::wgslStageLineMaps, esengine::resource::ParsedShader::wgslStages, esengine::resource::ParsedShader::wgslVertexHook, and esengine::resource::ParsedShader::wgslVertexIsCanonical.

◆ remapCompilerLog()

std::string esengine::resource::ShaderParser::remapCompilerLog ( const std::string & log,
const std::vector< SourceLine > & stageMap,
u32 headerLineOffset )
static

Rewrites GL compile-log line references back to original files.

Scans log for 0:N: and 0(N) patterns (the two common vendor prefixes) and rewrites each occurrence to file:origLine:. Lines before headerLineOffset are treated as synthetic header (e.g. #version, sharedCode) and left unchanged.

Parameters
logGL driver log (from glGetShaderInfoLog / glGetProgramInfoLog)
stageMapPer-stage line map from ParsedShader::stageLineMaps
headerLineOffsetLine count prepended by assembleStage before the stage body (typically 1 for #version + sharedCode line count)
Returns
Log with line references rewritten to original locations

Definition at line 4680 of file ShaderParser.cpp.

◆ setLinearColorSpace()

void esengine::resource::ShaderParser::setLinearColorSpace ( bool linear)
static

Process-global color-space mode. When linear, every assembled stage compiles with ES_LINEAR defined (GLSL #define / WGSL assembly-time preprocessor). Set BEFORE the renderer compiles shaders; flipping later requires a reload. Variant caches must fold linearColorSpace() into their keys if they can outlive a flip.

Definition at line 3034 of file ShaderParser.cpp.

Referenced by esengine::renderer_setColorSpace(), and esengine::RenderFrame::setColorSpace().

◆ splitFeatures()

std::vector< std::string > esengine::resource::ShaderParser::splitFeatures ( const std::string & csv)
static

Split a comma-separated feature/switch list into trimmed, non-empty entries — the form the boundary carries a permutation in.

Definition at line 1254 of file ShaderParser.cpp.

Referenced by esengine::esshader_cookInfo(), and esengine::material_compileEsshader().

◆ variantKey()

std::string esengine::resource::ShaderParser::variantKey ( const std::vector< std::string > & features)
static

Stable cache key for a feature set (order-independent), e.g. "GRAYSCALE|TINT". Consumers key their compiled-variant cache on (shader, variantKey(features)).

Definition at line 1243 of file ShaderParser.cpp.


The documentation for this class was generated from the following files: