Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ShaderParser.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17// =============================================================================
18// Includes
19// =============================================================================
20
21#include "../core/Types.hpp"
22
23#include <functional>
24#include <optional>
25#include <string>
26#include <vector>
27#include <unordered_map>
28
29namespace esengine::resource {
30
31// =============================================================================
32// Include Resolver
33// =============================================================================
34
42using ShaderIncludeResolver = std::function<std::optional<std::string>(const std::string&)>;
43
44// =============================================================================
45// Source Line Mapping
46// =============================================================================
47
54struct SourceLine {
55 std::string file;
56 u32 line = 0;
57};
58
59// =============================================================================
60// Shader Stage Enum
61// =============================================================================
62
73 WGSL = 1,
74};
75
76enum class ShaderStage : u8 {
79};
80
81// =============================================================================
82// Shader Property
83// =============================================================================
84
95
97 std::string name;
99 std::string defaultValue;
100 std::string displayName;
101
105 bool fromParam = false;
106
114 bool layered = false;
115
118 bool hasRange = false;
119 f32 rangeMin = 0.0f;
120 f32 rangeMax = 1.0f;
121 std::string ui;
125};
126
133 std::string name;
134 bool defaultOn = false;
135};
136
137// =============================================================================
138// Parsed Shader
139// =============================================================================
140
148 std::string name;
149 std::string version;
150 std::string sharedCode;
151 std::unordered_map<ShaderStage, std::string> stages;
152 std::unordered_map<std::string, std::string> variants;
153 std::vector<std::string> features;
156 bool lightingDirect = false;
157 std::vector<ShaderSwitch> switches;
158 std::vector<ShaderProperty> properties;
159 std::string domain = "Unlit";
163 std::string coverageParam;
167 std::string tipParam;
172 std::string hairShadowParam;
175 bool usesGBuffer = false;
179 bool usesLights = false;
182 bool highPrecision = false;
187 std::string vertexHook;
188 std::string wgslVertexHook;
191 std::string depthInput;
195 float vertexBounds = 0.0f;
199 std::string errorMessage;
200 bool valid = false;
201
210 std::vector<SourceLine> expandedLineMap;
211
219 std::unordered_map<ShaderStage, std::vector<SourceLine>> stageLineMaps;
220
230 std::unordered_map<ShaderStage, std::string> wgslStages;
231
233 std::unordered_map<ShaderStage, std::vector<SourceLine>> wgslStageLineMaps;
234
243 std::unordered_map<ShaderStage, bool> wgslStageFull;
244
252
260 bool vertexIsCanonical = false;
261};
262
263// =============================================================================
264// ShaderParser Class
265// =============================================================================
266
297public:
303 static ParsedShader parse(const std::string& source);
304
311 static ParsedShader parse(const std::string& source,
312 const ShaderIncludeResolver& resolver);
313
323 static std::string assembleStage(const ParsedShader& parsed,
324 ShaderStage stage,
325 const std::string& platform = "",
326 const std::vector<std::string>& features = {},
328
336 static std::string canonicalVaryingsWGSL(const ParsedShader& parsed);
337
342 static std::string variantKey(const std::vector<std::string>& features);
343
348 static std::vector<std::string> splitFeatures(const std::string& csv);
349
350 static std::vector<std::string> featuresRead(const ParsedShader& parsed,
351 const std::vector<std::string>& features,
353
361 static void setLinearColorSpace(bool linear);
362 static bool linearColorSpace();
363
373 std::string source;
375 };
376
377 static AssembledStage assembleStageEx(const ParsedShader& parsed,
378 ShaderStage stage,
379 const std::string& platform = "",
380 const std::vector<std::string>& features = {},
382
398 static std::string remapCompilerLog(const std::string& log,
399 const std::vector<SourceLine>& stageMap,
400 u32 headerLineOffset);
401
402private:
403 static void parseDirective(const std::string& line,
404 std::string& directive,
405 std::string& argument);
406
408 static ShaderProperty parseParamDirective(const std::string& argument);
409
412 static void computeMaterialLayout(ParsedShader& shader);
413
414public:
416 static u32 materialOverflowUnits(const ParsedShader& shader);
417
418private:
420 static i32 takeOverflowUnit(u32& free);
421
422 static ShaderProperty parsePropertyAnnotation(const std::string& line);
423
424 static ShaderPropertyType stringToPropertyType(const std::string& typeStr);
425
426 static std::string trim(const std::string& str);
427
428private:
429 static bool s_linearColor;
430
431};
432
433} // namespace esengine::resource
Core type definitions and utilities for ESEngine.
2D texture for GPU rendering
Definition Texture.hpp:116
Parser for .esshader unified shader format.
static void setLinearColorSpace(bool linear)
Process-global color-space mode. When linear, every assembled stage compiles with ES_LINEAR defined (...
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 c...
static ParsedShader parse(const std::string &source)
Parses shader source into structured format (no include support).
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 i...
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 co...
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 read...
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::vector< std::string > featuresRead(const ParsedShader &parsed, const std::vector< std::string > &features, ShaderTargetLanguage target=ShaderTargetLanguage::GLSL_ES300)
static AssembledStage assembleStageEx(const ParsedShader &parsed, ShaderStage stage, const std::string &platform="", const std::vector< std::string > &features={}, ShaderTargetLanguage target=ShaderTargetLanguage::GLSL_ES300)
std::function< std::optional< std::string >(const std::string &)> ShaderIncludeResolver
Resolves an include path to its file contents.
ShaderTargetLanguage
Target language for stage assembly (REARCH_WGSL dual emission).
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::int8_t i8
Signed 8-bit integer.
Definition Types.hpp:50
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
float f32
32-bit floating point
Definition Types.hpp:59
Result of parsing an .esshader file.
std::string version
GLSL version.
std::string errorMessage
Parse error if any.
std::unordered_map< ShaderStage, std::vector< SourceLine > > wgslStageLineMaps
Line maps for wgslStages, same convention as stageLineMaps.
std::unordered_map< ShaderStage, std::vector< SourceLine > > stageLineMaps
Per-stage line maps, parallel to stages[].
std::unordered_map< ShaderStage, std::string > stages
Stage source code.
std::unordered_map< ShaderStage, std::string > wgslStages
WGSL twin stage bodies (#pragma vertex wgsl / #pragma fragment wgsl sections), keyed like stages[].
bool valid
True if parsing succeeded.
std::vector< SourceLine > expandedLineMap
Line map for the expanded pre-parse source.
std::vector< std::string > features
std::string sharedCode
Code shared by all stages.
std::string name
Shader name.
std::unordered_map< ShaderStage, bool > wgslStageFull
Stages whose WGSL twin is a SELF-CONTAINED program (#pragma vertex wgsl full / #pragma fragment wgsl ...
bool wgslVertexIsCanonical
True when the WGSL vertex stage is the injected canonical one. The WGSL fragment assembly then also i...
bool vertexIsCanonical
True when the GLSL vertex stage is the injected canonical one.
std::vector< ShaderSwitch > switches
Material-controlled pragma switch toggles (name + default).
std::unordered_map< std::string, std::string > variants
Platform variants.
std::vector< ShaderProperty > properties
Exposed material params (pragma param / properties block).
Like assembleStage, but also reports the line count of the assembled prefix (version + variant + shar...
i32 textureUnit
Sampler unit for a texture param (>= MATERIAL_TEXTURE_UNIT_BASE), or -1 otherwise.
std::string defaultValue
Default value as string (comma-separated for vectors).
std::string name
Uniform name.
std::string displayName
Display name for editor.
ShaderPropertyType type
Property type.
A material-controlled static switch (pragma switch): a boolean the material sets, selecting a compile...
A (file, line) pair identifying an original source location.