Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(webgpu): WGSL twins move into their .esshaders - one creation path, SDF/LIT variants land (WGSL phase 3 slice 3)

yhh5c1b2e7555d4
diff --git a/desktop/scripts/engine-parity.mjs b/desktop/scripts/engine-parity.mjs
index c06550c3a..b95bcaf45 100644
--- a/desktop/scripts/engine-parity.mjs
+++ b/desktop/scripts/engine-parity.mjs
@@ -132,11 +132,14 @@ app.whenReady().then(async () => {
// - green sprite: world center (192,176) size 48 → screen (192, 80)
// - magenta circle: world center (128,80) r 32 → screen (128, 176)
// - clear color corner (8, 8)
+ // - lit sprite: white sprite lit by a red point light centered on it plus a
+ // 0.2 white ambient → white * (0.2 + red) ≈ (255, 51, 51) at (64, 176)
const POINTS = {
checkerA: { x: 48, y: 96 }, // lower-left quarter center
checkerB: { x: 48, y: 64 }, // upper-left quarter center (opposite texel)
green: { x: 192, y: 80 },
circle: { x: 128, y: 176 },
+ lit: { x: 64, y: 176 },
corner: { x: 8, y: 8 },
};
@@ -154,11 +157,12 @@ app.whenReady().then(async () => {
(near(p.checkerA.r, 0) && near(p.checkerA.b, 255) && near(p.checkerB.r, 255) && near(p.checkerB.b, 0));
const greenOk = near(p.green.r, 0) && near(p.green.g, 255) && near(p.green.b, 0);
const circleOk = near(p.circle.r, 255) && near(p.circle.g, 0) && near(p.circle.b, 255);
+ const litOk = near(p.lit.r, 255) && near(p.lit.g, 51) && near(p.lit.b, 51);
const cornerOk = near(p.corner.r, 13) && near(p.corner.g, 13) && near(p.corner.b, 77, 60);
const runOk = run.data.framesOk && run.data.errors.length === 0 &&
- checkerOk && greenOk && circleOk && cornerOk;
+ checkerOk && greenOk && circleOk && litOk && cornerOk;
console.log(`[${run.name}] frames=${run.data.framesOk} errors=${run.data.errors.length} ` +
- `checker=${checkerOk} green=${greenOk} circle=${circleOk} corner=${cornerOk}`);
+ `checker=${checkerOk} green=${greenOk} circle=${circleOk} lit=${litOk} corner=${cornerOk}`);
ok = ok && runOk;
}
diff --git a/src/esengine/data/shaders/batch.esshader b/src/esengine/data/shaders/batch.esshader
index c3ecd29b4..7bbb3258f 100644
--- a/src/esengine/data/shaders/batch.esshader
+++ b/src/esengine/data/shaders/batch.esshader
@@ -84,3 +84,93 @@ void main() {
#endif
}
#pragma end
+
+#pragma vertex wgsl
+struct FrameConstants { projection : mat4x4f };
+@group(0) @binding(0) var<uniform> frame : FrameConstants;
+
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_color : vec4f,
+ @location(2) a_texCoord : vec2f,
+ @location(3) a_texIndex : f32,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_color : vec4f,
+ @location(1) v_texCoord : vec2f,
+ @location(2) v_texIndex : f32,
+#ifdef LIT
+ @location(3) v_worldPos : vec2f,
+#endif
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ var out : VSOut;
+ out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
+ out.v_color = v.a_color;
+ out.v_texCoord = v.a_texCoord;
+ out.v_texIndex = v.a_texIndex;
+#ifdef LIT
+ out.v_worldPos = v.a_position;
+#endif
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+@group(1) @binding(0) var t0 : texture_2d<f32>;
+@group(1) @binding(1) var t1 : texture_2d<f32>;
+@group(1) @binding(2) var t2 : texture_2d<f32>;
+@group(1) @binding(3) var t3 : texture_2d<f32>;
+@group(1) @binding(4) var t4 : texture_2d<f32>;
+@group(1) @binding(5) var t5 : texture_2d<f32>;
+@group(1) @binding(6) var t6 : texture_2d<f32>;
+@group(1) @binding(7) var t7 : texture_2d<f32>;
+@group(1) @binding(8) var s0 : sampler;
+@group(1) @binding(9) var s1 : sampler;
+@group(1) @binding(10) var s2 : sampler;
+@group(1) @binding(11) var s3 : sampler;
+@group(1) @binding(12) var s4 : sampler;
+@group(1) @binding(13) var s5 : sampler;
+@group(1) @binding(14) var s6 : sampler;
+@group(1) @binding(15) var s7 : sampler;
+
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_color : vec4f,
+ @location(1) v_texCoord : vec2f,
+ @location(2) v_texIndex : f32,
+#ifdef LIT
+ @location(3) v_worldPos : vec2f,
+#endif
+};
+
+// textureSampleLevel(…, 0) keeps sampling uniform-flow-legal inside the
+// constant branch chain; 2D sprites sample mip 0.
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let idx = i32(v.v_texIndex + 0.5);
+ var texColor : vec4f;
+ if (idx == 0) { texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0); }
+ else if (idx == 1) { texColor = textureSampleLevel(t1, s1, v.v_texCoord, 0.0); }
+ else if (idx == 2) { texColor = textureSampleLevel(t2, s2, v.v_texCoord, 0.0); }
+ else if (idx == 3) { texColor = textureSampleLevel(t3, s3, v.v_texCoord, 0.0); }
+ else if (idx == 4) { texColor = textureSampleLevel(t4, s4, v.v_texCoord, 0.0); }
+ else if (idx == 5) { texColor = textureSampleLevel(t5, s5, v.v_texCoord, 0.0); }
+ else if (idx == 6) { texColor = textureSampleLevel(t6, s6, v.v_texCoord, 0.0); }
+ else { texColor = textureSampleLevel(t7, s7, v.v_texCoord, 0.0); }
+#ifdef SDF
+ // Same coverage math as the GLSL stage; fwidth here is in uniform control
+ // flow (main scope), which WGSL requires of derivative builtins.
+ let dist = texColor.a;
+ let screenPxDist = (dist - 0.5) / max(fwidth(dist), 1e-6);
+ let coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
+ return vec4f(v.v_color.rgb, v.v_color.a * coverage);
+#elif defined(LIT)
+ let base = texColor * v.v_color;
+ return vec4f(applyLighting2D(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos), base.a);
+#else
+ return texColor * v.v_color;
+#endif
+}
+#pragma end
diff --git a/src/esengine/data/shaders/blit.esshader b/src/esengine/data/shaders/blit.esshader
new file mode 100644
index 000000000..339f5ffc6
--- /dev/null
+++ b/src/esengine/data/shaders/blit.esshader
@@ -0,0 +1,59 @@
+#pragma shader "Blit"
+#pragma version 300 es
+#pragma domain PostProcess
+
+#pragma vertex
+layout(location = 0) in vec2 a_position;
+layout(location = 1) in vec2 a_texCoord;
+
+out vec2 v_texCoord;
+
+void main() {
+ v_texCoord = a_texCoord;
+ gl_Position = vec4(a_position, 0.0, 1.0);
+}
+#pragma end
+
+#pragma fragment
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+out vec4 fragColor;
+
+void main() {
+ fragColor = texture(u_texture, v_texCoord);
+}
+#pragma end
+
+#pragma vertex wgsl
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_texCoord : vec2f,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ var out : VSOut;
+ out.pos = vec4f(v.a_position, 0.0, 1.0);
+ out.v_texCoord = v.a_texCoord;
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+@group(1) @binding(0) var t0 : texture_2d<f32>;
+@group(1) @binding(8) var s0 : sampler;
+
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+};
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ return textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+}
+#pragma end
diff --git a/src/esengine/data/shaders/particle.esshader b/src/esengine/data/shaders/particle.esshader
index 3b12da956..f79aa1bac 100644
--- a/src/esengine/data/shaders/particle.esshader
+++ b/src/esengine/data/shaders/particle.esshader
@@ -52,3 +52,59 @@ void main() {
fragColor = texColor * v_color;
}
#pragma end
+
+#pragma vertex wgsl
+struct FrameConstants { projection : mat4x4f };
+@group(0) @binding(0) var<uniform> frame : FrameConstants;
+
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_texCoord : vec2f,
+ @location(2) a_inst_position : vec2f,
+ @location(3) a_inst_size : vec2f,
+ @location(4) a_inst_rotation : f32,
+ @location(5) a_inst_color : vec4f,
+ @location(6) a_inst_uv_offset : vec2f,
+ @location(7) a_inst_uv_scale : vec2f,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+ @location(1) v_color : vec4f,
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ let scaled = v.a_position * v.a_inst_size;
+
+ let cosR = cos(v.a_inst_rotation);
+ let sinR = sin(v.a_inst_rotation);
+ let rotated = vec2f(
+ scaled.x * cosR - scaled.y * sinR,
+ scaled.x * sinR + scaled.y * cosR
+ );
+
+ let worldPos = rotated + v.a_inst_position;
+
+ var out : VSOut;
+ out.pos = frame.projection * vec4f(worldPos, 0.0, 1.0);
+ out.v_texCoord = v.a_texCoord * v.a_inst_uv_scale + v.a_inst_uv_offset;
+ out.v_color = v.a_inst_color;
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+@group(1) @binding(0) var t0 : texture_2d<f32>;
+@group(1) @binding(8) var s0 : sampler;
+
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+ @location(1) v_color : vec4f,
+};
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+ return texColor * v.v_color;
+}
+#pragma end
diff --git a/src/esengine/data/shaders/shape.esshader b/src/esengine/data/shaders/shape.esshader
index 7bd95efb0..82ff145c9 100644
--- a/src/esengine/data/shaders/shape.esshader
+++ b/src/esengine/data/shaders/shape.esshader
@@ -60,3 +60,66 @@ void main() {
fragColor = vec4(v_color.rgb, v_color.a * alpha);
}
#pragma end
+
+#pragma vertex wgsl
+struct FrameConstants { projection : mat4x4f };
+@group(0) @binding(0) var<uniform> frame : FrameConstants;
+
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_texCoord : vec2f,
+ @location(2) a_color : vec4f,
+ @location(3) a_shapeInfo : vec4f,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_uv : vec2f,
+ @location(1) v_color : vec4f,
+ @location(2) v_shapeInfo : vec4f,
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ var out : VSOut;
+ out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
+ out.v_uv = v.a_texCoord;
+ out.v_color = v.a_color;
+ out.v_shapeInfo = v.a_shapeInfo;
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_uv : vec2f,
+ @location(1) v_color : vec4f,
+ @location(2) v_shapeInfo : vec4f,
+};
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let halfSize = v.v_shapeInfo.yz;
+ let cornerRadius = v.v_shapeInfo.w;
+ let p = v.v_uv * halfSize;
+ let shapeType = v.v_shapeInfo.x;
+
+ var dist : f32;
+ if (shapeType < 0.5) {
+ let r = min(halfSize.x, halfSize.y);
+ dist = length(p) - r;
+ } else if (shapeType < 1.5) {
+ let r = min(halfSize.x, halfSize.y);
+ let elongation = halfSize - vec2f(r, r);
+ let q = abs(p) - elongation;
+ dist = length(max(q, vec2f(0.0, 0.0))) - r;
+ } else {
+ let r = min(cornerRadius, min(halfSize.x, halfSize.y));
+ let q = abs(p) - halfSize + vec2f(r, r);
+ dist = length(max(q, vec2f(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - r;
+ }
+
+ let fw = fwidth(dist);
+ let alpha = 1.0 - smoothstep(-fw, fw, dist);
+ if (alpha < 0.001) { discard; }
+ return vec4f(v.v_color.rgb, v.v_color.a * alpha);
+}
+#pragma end
diff --git a/src/esengine/renderer/ImmediateDraw.cpp b/src/esengine/renderer/ImmediateDraw.cpp
index 029d71aaf..3d95bd4e2 100644
--- a/src/esengine/renderer/ImmediateDraw.cpp
+++ b/src/esengine/renderer/ImmediateDraw.cpp
@@ -19,7 +19,6 @@
#include "RenderContext.hpp"
#include "Shader.hpp"
#include "ShaderEmbeds.generated.hpp"
-#include "webgpu/WGSLTwins.hpp"
#include "BatchVertex.hpp"
#include "../resource/ShaderParser.hpp"
#include "../resource/ResourceManager.hpp"
@@ -64,19 +63,14 @@ void ImmediateDraw::init() {
pool_.init();
white_texture_id_ = context_.getWhiteTextureId();
- resource::ShaderHandle handle;
- if (resource_manager_.preferredShaderLanguage() == GfxShaderLanguage::GLSL_ES300) {
- auto parsed = resource::ShaderParser::parse(ShaderEmbeds::BATCH);
- handle = resource_manager_.createShaderWithBindings(
- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex),
- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment),
- {{0, "a_position"}, {1, "a_color"}, {2, "a_texCoord"}}
- );
- } else {
- handle = resource_manager_.createShaderWithBindings(
- webgpu::kBatchWGSL_Vertex, webgpu::kBatchWGSL_Fragment,
- {}, GfxShaderLanguage::WGSL);
- }
+ // Reuses the batch shader's default variant — one .esshader, both languages.
+ const auto target = resource_manager_.preferredShaderTarget();
+ auto parsed = resource::ShaderParser::parse(ShaderEmbeds::BATCH);
+ resource::ShaderHandle handle = resource_manager_.createShaderWithBindings(
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", {}, target),
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", {}, target),
+ {{0, "a_position"}, {1, "a_color"}, {2, "a_texCoord"}},
+ resource_manager_.preferredShaderLanguage());
Shader* shader = resource_manager_.getShader(handle);
if (shader && shader->isValid()) {
batch_shader_ = shader->handle();
diff --git a/src/esengine/renderer/PostProcessPipeline.cpp b/src/esengine/renderer/PostProcessPipeline.cpp
index 1cf240600..8b4870ae5 100644
--- a/src/esengine/renderer/PostProcessPipeline.cpp
+++ b/src/esengine/renderer/PostProcessPipeline.cpp
@@ -15,8 +15,9 @@
#include "RenderContext.hpp"
#include "GfxDevice.hpp"
#include "Shader.hpp"
-#include "webgpu/WGSLTwins.hpp"
+#include "ShaderEmbeds.generated.hpp"
#include "../resource/ResourceManager.hpp"
+#include "../resource/ShaderParser.hpp"
#include "../core/Log.hpp"
#include <algorithm>
@@ -24,32 +25,6 @@
namespace esengine {
-static const char* BLIT_VERTEX = R"(#version 300 es
-precision highp float;
-
-layout(location = 0) in vec2 a_position;
-layout(location = 1) in vec2 a_texCoord;
-
-out vec2 v_texCoord;
-
-void main() {
- v_texCoord = a_texCoord;
- gl_Position = vec4(a_position, 0.0, 1.0);
-}
-)";
-
-static const char* BLIT_FRAGMENT = R"(#version 300 es
-precision highp float;
-
-in vec2 v_texCoord;
-uniform sampler2D u_texture;
-out vec4 fragColor;
-
-void main() {
- fragColor = texture(u_texture, v_texCoord);
-}
-)";
-
PostProcessPipeline::PostProcessPipeline(GfxDevice& device,
RenderContext& context,
resource::ResourceManager& resourceManager)
@@ -70,14 +45,16 @@ void PostProcessPipeline::init(u32 width, u32 height) {
width_ = width;
height_ = height;
- if (resourceManager_.preferredShaderLanguage() == GfxShaderLanguage::GLSL_ES300) {
- blitShader_ = resourceManager_.createShader(BLIT_VERTEX, BLIT_FRAGMENT);
- } else {
- blitShader_ = resourceManager_.createShader(webgpu::kBlitWGSL_Vertex,
- webgpu::kBlitWGSL_Fragment,
- /*rewriteLoose=*/false,
- GfxShaderLanguage::WGSL);
- }
+ // The pass-through copy, authored as blit.esshader (WGSL twin included).
+ // No loose non-sampler uniforms, so the DrawParams rewrite has nothing to
+ // lift; u_texture seeds via setUniform (GLSL-only, guarded there).
+ const auto target = resourceManager_.preferredShaderTarget();
+ auto parsed = resource::ShaderParser::parse(ShaderEmbeds::BLIT);
+ blitShader_ = resourceManager_.createShader(
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", {}, target),
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", {}, target),
+ /*rewriteLoose=*/false,
+ resourceManager_.preferredShaderLanguage());
if (!blitShader_.isValid()) {
ES_LOG_ERROR("PostProcessPipeline: Failed to create blit shader");
return;
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index 514bbc015..0f558949e 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -3,7 +3,6 @@
#include "RenderFrame.hpp"
#include "Shader.hpp"
#include "ShaderEmbeds.generated.hpp"
-#include "webgpu/WGSLTwins.hpp"
#include "LightStore.hpp"
#include "../ecs/components/Transform.hpp"
#include "../ecs/components/Light2D.hpp"
@@ -625,34 +624,23 @@ u32 RenderFrame::batchProgram(const std::vector<std::string>& features) {
}
u32 RenderFrame::compileBatchVariant(const std::vector<std::string>& features) {
- resource::ShaderHandle handle;
- if (resource_manager_.preferredShaderLanguage() == GfxShaderLanguage::WGSL) {
- // Only the default variant has a hand-written twin; the SDF/LIT feature
- // variants arrive with the Phase 3 dual-language emitter.
- if (!features.empty()) {
- const std::string vk = resource::ShaderParser::variantKey(features);
- ES_LOG_ERROR("Batch shader variant '{}' has no WGSL twin yet", vk.c_str());
- return 0;
- }
- handle = resource_manager_.createShaderWithBindings(
- webgpu::kBatchWGSL_Vertex, webgpu::kBatchWGSL_Fragment,
- {}, GfxShaderLanguage::WGSL);
- } else {
- // The batch shader is authored as a single .esshader, embedded for the web build.
- // Parse it and assemble the two GLSL ES 3.00 stages (single source of truth),
- // injecting the requested feature #defines (e.g. SDF).
- auto parsed = resource::ShaderParser::parse(ShaderEmbeds::BATCH);
- // LIT is a Lit2D-domain variant: the domain drives the lighting injection,
- // same path as Lit2D material shaders.
- if (std::find(features.begin(), features.end(), "LIT") != features.end()) {
- parsed.domain = "Lit2D";
- }
- handle = resource_manager_.createShaderWithBindings(
- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", features),
- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", features),
- {{0, "a_position"}, {1, "a_color"}, {2, "a_texCoord"}}
- );
+ // The batch shader is authored as a single .esshader with its WGSL twin,
+ // embedded for the web build. Parse it and assemble the two stages for the
+ // preferred target (single source of truth), injecting the requested
+ // feature permutation — GLSL as #defines, WGSL through the assembly-time
+ // preprocessor.
+ const auto target = resource_manager_.preferredShaderTarget();
+ auto parsed = resource::ShaderParser::parse(ShaderEmbeds::BATCH);
+ // LIT is a Lit2D-domain variant: the domain drives the lighting injection,
+ // same path as Lit2D material shaders.
+ if (std::find(features.begin(), features.end(), "LIT") != features.end()) {
+ parsed.domain = "Lit2D";
}
+ resource::ShaderHandle handle = resource_manager_.createShaderWithBindings(
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", features, target),
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", features, target),
+ {{0, "a_position"}, {1, "a_color"}, {2, "a_texCoord"}},
+ resource_manager_.preferredShaderLanguage());
Shader* shader = resource_manager_.getShader(handle);
if (shader && shader->isValid()) {
diff --git a/src/esengine/renderer/ShaderEmbeds.generated.hpp b/src/esengine/renderer/ShaderEmbeds.generated.hpp
index 0701f00e6..29642dcce 100644
--- a/src/esengine/renderer/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/ShaderEmbeds.generated.hpp
@@ -88,6 +88,157 @@ void main() {
#endif
}
#pragma end
+
+#pragma vertex wgsl
+struct FrameConstants { projection : mat4x4f };
+@group(0) @binding(0) var<uniform> frame : FrameConstants;
+
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_color : vec4f,
+ @location(2) a_texCoord : vec2f,
+ @location(3) a_texIndex : f32,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_color : vec4f,
+ @location(1) v_texCoord : vec2f,
+ @location(2) v_texIndex : f32,
+#ifdef LIT
+ @location(3) v_worldPos : vec2f,
+#endif
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ var out : VSOut;
+ out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
+ out.v_color = v.a_color;
+ out.v_texCoord = v.a_texCoord;
+ out.v_texIndex = v.a_texIndex;
+#ifdef LIT
+ out.v_worldPos = v.a_position;
+#endif
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+@group(1) @binding(0) var t0 : texture_2d<f32>;
+@group(1) @binding(1) var t1 : texture_2d<f32>;
+@group(1) @binding(2) var t2 : texture_2d<f32>;
+@group(1) @binding(3) var t3 : texture_2d<f32>;
+@group(1) @binding(4) var t4 : texture_2d<f32>;
+@group(1) @binding(5) var t5 : texture_2d<f32>;
+@group(1) @binding(6) var t6 : texture_2d<f32>;
+@group(1) @binding(7) var t7 : texture_2d<f32>;
+@group(1) @binding(8) var s0 : sampler;
+@group(1) @binding(9) var s1 : sampler;
+@group(1) @binding(10) var s2 : sampler;
+@group(1) @binding(11) var s3 : sampler;
+@group(1) @binding(12) var s4 : sampler;
+@group(1) @binding(13) var s5 : sampler;
+@group(1) @binding(14) var s6 : sampler;
+@group(1) @binding(15) var s7 : sampler;
+
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_color : vec4f,
+ @location(1) v_texCoord : vec2f,
+ @location(2) v_texIndex : f32,
+#ifdef LIT
+ @location(3) v_worldPos : vec2f,
+#endif
+};
+
+// textureSampleLevel(…, 0) keeps sampling uniform-flow-legal inside the
+// constant branch chain; 2D sprites sample mip 0.
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let idx = i32(v.v_texIndex + 0.5);
+ var texColor : vec4f;
+ if (idx == 0) { texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0); }
+ else if (idx == 1) { texColor = textureSampleLevel(t1, s1, v.v_texCoord, 0.0); }
+ else if (idx == 2) { texColor = textureSampleLevel(t2, s2, v.v_texCoord, 0.0); }
+ else if (idx == 3) { texColor = textureSampleLevel(t3, s3, v.v_texCoord, 0.0); }
+ else if (idx == 4) { texColor = textureSampleLevel(t4, s4, v.v_texCoord, 0.0); }
+ else if (idx == 5) { texColor = textureSampleLevel(t5, s5, v.v_texCoord, 0.0); }
+ else if (idx == 6) { texColor = textureSampleLevel(t6, s6, v.v_texCoord, 0.0); }
+ else { texColor = textureSampleLevel(t7, s7, v.v_texCoord, 0.0); }
+#ifdef SDF
+ // Same coverage math as the GLSL stage; fwidth here is in uniform control
+ // flow (main scope), which WGSL requires of derivative builtins.
+ let dist = texColor.a;
+ let screenPxDist = (dist - 0.5) / max(fwidth(dist), 1e-6);
+ let coverage = clamp(screenPxDist + 0.5, 0.0, 1.0);
+ return vec4f(v.v_color.rgb, v.v_color.a * coverage);
+#elif defined(LIT)
+ let base = texColor * v.v_color;
+ return vec4f(applyLighting2D(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos), base.a);
+#else
+ return texColor * v.v_color;
+#endif
+}
+#pragma end
+)esshader";
+
+inline constexpr const char* BLIT = R"esshader(#pragma shader "Blit"
+#pragma version 300 es
+#pragma domain PostProcess
+
+#pragma vertex
+layout(location = 0) in vec2 a_position;
+layout(location = 1) in vec2 a_texCoord;
+
+out vec2 v_texCoord;
+
+void main() {
+ v_texCoord = a_texCoord;
+ gl_Position = vec4(a_position, 0.0, 1.0);
+}
+#pragma end
+
+#pragma fragment
+precision highp float;
+
+in vec2 v_texCoord;
+uniform sampler2D u_texture;
+out vec4 fragColor;
+
+void main() {
+ fragColor = texture(u_texture, v_texCoord);
+}
+#pragma end
+
+#pragma vertex wgsl
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_texCoord : vec2f,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ var out : VSOut;
+ out.pos = vec4f(v.a_position, 0.0, 1.0);
+ out.v_texCoord = v.a_texCoord;
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+@group(1) @binding(0) var t0 : texture_2d<f32>;
+@group(1) @binding(8) var s0 : sampler;
+
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+};
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ return textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+}
+#pragma end
)esshader";
inline constexpr const char* PARTICLE = R"esshader(#pragma shader "ParticleInstance"
@@ -144,6 +295,62 @@ void main() {
fragColor = texColor * v_color;
}
#pragma end
+
+#pragma vertex wgsl
+struct FrameConstants { projection : mat4x4f };
+@group(0) @binding(0) var<uniform> frame : FrameConstants;
+
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_texCoord : vec2f,
+ @location(2) a_inst_position : vec2f,
+ @location(3) a_inst_size : vec2f,
+ @location(4) a_inst_rotation : f32,
+ @location(5) a_inst_color : vec4f,
+ @location(6) a_inst_uv_offset : vec2f,
+ @location(7) a_inst_uv_scale : vec2f,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+ @location(1) v_color : vec4f,
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ let scaled = v.a_position * v.a_inst_size;
+
+ let cosR = cos(v.a_inst_rotation);
+ let sinR = sin(v.a_inst_rotation);
+ let rotated = vec2f(
+ scaled.x * cosR - scaled.y * sinR,
+ scaled.x * sinR + scaled.y * cosR
+ );
+
+ let worldPos = rotated + v.a_inst_position;
+
+ var out : VSOut;
+ out.pos = frame.projection * vec4f(worldPos, 0.0, 1.0);
+ out.v_texCoord = v.a_texCoord * v.a_inst_uv_scale + v.a_inst_uv_offset;
+ out.v_color = v.a_inst_color;
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+@group(1) @binding(0) var t0 : texture_2d<f32>;
+@group(1) @binding(8) var s0 : sampler;
+
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_texCoord : vec2f,
+ @location(1) v_color : vec4f,
+};
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let texColor = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
+ return texColor * v.v_color;
+}
+#pragma end
)esshader";
inline constexpr const char* SHAPE = R"esshader(#pragma shader "Shape"
@@ -208,6 +415,69 @@ void main() {
fragColor = vec4(v_color.rgb, v_color.a * alpha);
}
#pragma end
+
+#pragma vertex wgsl
+struct FrameConstants { projection : mat4x4f };
+@group(0) @binding(0) var<uniform> frame : FrameConstants;
+
+struct VSIn {
+ @location(0) a_position : vec2f,
+ @location(1) a_texCoord : vec2f,
+ @location(2) a_color : vec4f,
+ @location(3) a_shapeInfo : vec4f,
+};
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_uv : vec2f,
+ @location(1) v_color : vec4f,
+ @location(2) v_shapeInfo : vec4f,
+};
+
+@vertex fn vs_main(v : VSIn) -> VSOut {
+ var out : VSOut;
+ out.pos = frame.projection * vec4f(v.a_position, 0.0, 1.0);
+ out.v_uv = v.a_texCoord;
+ out.v_color = v.a_color;
+ out.v_shapeInfo = v.a_shapeInfo;
+ return out;
+}
+#pragma end
+
+#pragma fragment wgsl
+struct VSOut {
+ @builtin(position) pos : vec4f,
+ @location(0) v_uv : vec2f,
+ @location(1) v_color : vec4f,
+ @location(2) v_shapeInfo : vec4f,
+};
+
+@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
+ let halfSize = v.v_shapeInfo.yz;
+ let cornerRadius = v.v_shapeInfo.w;
+ let p = v.v_uv * halfSize;
+ let shapeType = v.v_shapeInfo.x;
+
+ var dist : f32;
+ if (shapeType < 0.5) {
+ let r = min(halfSize.x, halfSize.y);
+ dist = length(p) - r;
+ } else if (shapeType < 1.5) {
+ let r = min(halfSize.x, halfSize.y);
+ let elongation = halfSize - vec2f(r, r);
+ let q = abs(p) - elongation;
+ dist = length(max(q, vec2f(0.0, 0.0))) - r;
+ } else {
+ let r = min(cornerRadius, min(halfSize.x, halfSize.y));
+ let q = abs(p) - halfSize + vec2f(r, r);
+ dist = length(max(q, vec2f(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - r;
+ }
+
+ let fw = fwidth(dist);
+ let alpha = 1.0 - smoothstep(-fw, fw, dist);
+ if (alpha < 0.001) { discard; }
+ return vec4f(v.v_color.rgb, v.v_color.a * alpha);
+}
+#pragma end
)esshader";
} // namespace esengine::ShaderEmbeds
diff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cpp
index cfb7eff4f..a617c7d4b 100644
--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp
+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp
@@ -6,7 +6,6 @@
#include "../RenderFrame.hpp"
#include "../Shader.hpp"
#include "../ShaderEmbeds.generated.hpp"
-#include "../webgpu/WGSLTwins.hpp"
#include "../Texture.hpp"
#include "../BatchVertex.hpp" // packColor
#include "../../resource/ShaderParser.hpp"
@@ -35,20 +34,15 @@ static_assert(sizeof(ParticleInstanceData) == 40, "instance stride must match th
} // namespace
void ParticlePlugin::init(RenderFrameContext& ctx) {
- // Particle instancing shader, authored as particle.esshader (single source) and
- // embedded for the web build. Attribute locations are explicit, so no name bindings.
- resource::ShaderHandle handle;
- if (ctx.resources.preferredShaderLanguage() == GfxShaderLanguage::GLSL_ES300) {
- auto parsed = resource::ShaderParser::parse(ShaderEmbeds::PARTICLE);
- handle = ctx.resources.createShaderWithBindings(
- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex),
- resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment),
- {});
- } else {
- handle = ctx.resources.createShaderWithBindings(
- webgpu::kParticleWGSL_Vertex, webgpu::kParticleWGSL_Fragment,
- {}, GfxShaderLanguage::WGSL);
- }
+ // Particle instancing shader, authored as particle.esshader (single source,
+ // WGSL twin included) and embedded for the web build. Attribute locations
+ // are explicit, so no name bindings.
+ const auto target = ctx.resources.preferredShaderTarget();
+ auto parsed = resource::ShaderParser::parse(ShaderEmbeds::PARTICLE);
+ resource::ShaderHandle handle = ctx.resources.createShaderWithBindings(
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Vertex, "", {}, target),
+ resource::ShaderParser::assembleStage(parsed, resource::ShaderStage::Fragment, "", {}, target),
+ {}, ctx.resources.preferredShaderLanguage());
Shader* shader = ctx.resources.getShader(handle);
if (shader && shader->isValid()) {
particle_shader_id_ = shader->getProgramId();
diff --git a/src/esengine/renderer/plugins/ShapePlugin.cpp b/src/esengine/renderer/plugins/ShapePlugin.cpp
index 604e03216..aa5ce03bf 100644
--- a/src/esengine/renderer/plugins/ShapePlugin.cpp
+++ b/src/esengine/renderer/plugins/ShapePlugin.cpp
@@ -6,7 +6,6 @@
#include "../RenderFrame.hpp"
#include "../Shader.hpp"
#include "../ShaderEmbeds.generated.hpp"
-#include "../webgpu/WGSLTwins.hpp"
#include "../../ecs/components/Transform.hpp"
#include "../../ecs/components/ShapeRenderer.hpp"
#include "../../resource/ShaderParser.hpp"
@@ -16,20 +15,16 @@
namespace esengine {
void ShapePlugin::init(RenderFrameContext& ctx) {
- if (ctx.resources.preferredShaderLanguage() == GfxShaderLanguage::GLSL_ES300) {
- auto shapeParsed = resource::ShaderParser::parse(ShaderEmbeds::SHAPE);
- shape_shader_handle_ = ctx.resources.createShaderWithBindings(
- resource::ShaderParser::assembleStage(shapeParsed, resource::ShaderStage::Vertex),
- resource::ShaderParser::assembleStage(shapeParsed, resource::ShaderStage::Fragment),
- {{0, "a_position"}, {1, "a_texCoord"}, {2, "a_color"}, {3, "a_shapeInfo"}}
- );
- } else {
- // The WGSL twin fixes its attribute locations in-source; name bindings
- // are a GL link-time concept.
- shape_shader_handle_ = ctx.resources.createShaderWithBindings(
- webgpu::kShapeWGSL_Vertex, webgpu::kShapeWGSL_Fragment,
- {}, GfxShaderLanguage::WGSL);
- }
+ // One path for both backends: the WGSL twin rides shape.esshader, so the
+ // stages assemble for the preferred target. Attribute name bindings are a
+ // GL link-time concept; the WGSL twin fixes its locations in-source.
+ const auto target = ctx.resources.preferredShaderTarget();
+ auto shapeParsed = resource::ShaderParser::parse(ShaderEmbeds::SHAPE);
+ shape_shader_handle_ = ctx.resources.createShaderWithBindings(
+ resource::ShaderParser::assembleStage(shapeParsed, resource::ShaderStage::Vertex, "", {}, target),
+ resource::ShaderParser::assembleStage(shapeParsed, resource::ShaderStage::Fragment, "", {}, target),
+ {{0, "a_position"}, {1, "a_texCoord"}, {2, "a_color"}, {3, "a_shapeInfo"}},
+ ctx.resources.preferredShaderLanguage());
Shader* shader = ctx.resources.getShader(shape_shader_handle_);
shape_shader_id_ = shader ? shader->getProgramId() : 0;
diff --git a/src/esengine/renderer/webgpu/WGSLTwins.hpp b/src/esengine/renderer/webgpu/WGSLTwins.hpp
deleted file mode 100644
index 16f10a54a..000000000
--- a/src/esengine/renderer/webgpu/WGSLTwins.hpp
+++ /dev/null
@@ -1,280 +0,0 @@
-// SPDX-License-Identifier: Apache-2.0
-// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
-/**
- * @file WGSLTwins.hpp
- * @brief Hand-authored WGSL twins of the engine's embedded shaders (REARCH_WGSL Phase 2).
- * @details Each twin mirrors its .esshader source line for line: same attribute
- * locations, same UBO blocks at the same binding slots (group 0), and
- * the multi-texture set as bind group 1 — 8 texture_2d at bindings 0..7
- * paired with 8 samplers at bindings 8..15 (GL's combined
- * texture+sampler state arrives de-combined; sampler i carries texture
- * i's filter/wrap params). Consumed by the embedded-pipeline creation
- * sites when ResourceManager::preferredShaderLanguage() reports WGSL,
- * and by the WebGPU bring-up harnesses; Phase 3's dual-language
- * emitter replaces the hand-written pairs.
- *
- * The device reflects bind-group contents by scanning these
- * declarations and builds explicit layouts from them, so a declared-
- * but-unused binding is legal (dummy-backfilled) — but twins still
- * declare only what they use, keeping the scan output minimal.
- *
- * Plain string constants — safe in every build; no WebGPU headers.
- *
- * @author ESEngine Team
- * @date 2026
- *
- * @copyright Copyright (c) 2026 ESEngine Team
- * Licensed under the Apache License, Version 2.0.
- */
-#pragma once
-
-namespace esengine::webgpu {
-
-// =============================================================================
-// shape.esshader — SDF shapes over the Shape stream (locations 0..3), the
-// FrameConstants block at group 0 binding 0, the same SDF math per shape type.
-// `v` instead of `in` (a WGSL reserved word).
-// =============================================================================
-
-inline constexpr const char* kShapeWGSL_Vertex = R"(
-struct FrameConstants { projection : mat4x4f };
-@group(0) @binding(0) var<uniform> frame : FrameConstants;
-
-struct VSIn {
- @location(0) position : vec2f,
- @location(1) uv : vec2f,
- @location(2) color : vec4f,
- @location(3) shapeInfo : vec4f,
-};
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) uv : vec2f,
- @location(1) color : vec4f,
- @location(2) shapeInfo : vec4f,
-};
-
-@vertex fn vs_main(v : VSIn) -> VSOut {
- var out : VSOut;
- out.pos = frame.projection * vec4f(v.position, 0.0, 1.0);
- out.uv = v.uv;
- out.color = v.color;
- out.shapeInfo = v.shapeInfo;
- return out;
-}
-)";
-
-inline constexpr const char* kShapeWGSL_Fragment = R"(
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) uv : vec2f,
- @location(1) color : vec4f,
- @location(2) shapeInfo : vec4f,
-};
-
-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
- let halfSize = v.shapeInfo.yz;
- let cornerRadius = v.shapeInfo.w;
- let p = v.uv * halfSize;
- let shapeType = v.shapeInfo.x;
-
- var dist : f32;
- if (shapeType < 0.5) {
- let r = min(halfSize.x, halfSize.y);
- dist = length(p) - r;
- } else if (shapeType < 1.5) {
- let r = min(halfSize.x, halfSize.y);
- let elongation = halfSize - vec2f(r, r);
- let q = abs(p) - elongation;
- dist = length(max(q, vec2f(0.0, 0.0))) - r;
- } else {
- let r = min(cornerRadius, min(halfSize.x, halfSize.y));
- let q = abs(p) - halfSize + vec2f(r, r);
- dist = length(max(q, vec2f(0.0, 0.0))) + min(max(q.x, q.y), 0.0) - r;
- }
-
- let fw = fwidth(dist);
- let alpha = 1.0 - smoothstep(-fw, fw, dist);
- if (alpha < 0.001) { discard; }
- return vec4f(v.color.rgb, v.color.a * alpha);
-}
-)";
-
-// =============================================================================
-// batch.esshader (default variant) — the Batch stream (pos, unorm8x4 color, uv,
-// texIndex at locations 0..3), FrameConstants at group 0, and u_textures[8] as
-// the group-1 texture/sampler pairs. textureSampleLevel(…, 0) keeps sampling
-// uniform-flow-legal inside the branch chain; 2D sprites sample mip 0 (the
-// mipped path is a Phase 3 topic).
-// =============================================================================
-
-inline constexpr const char* kBatchWGSL_Vertex = R"(
-struct FrameConstants { projection : mat4x4f };
-@group(0) @binding(0) var<uniform> frame : FrameConstants;
-
-struct VSIn {
- @location(0) position : vec2f,
- @location(1) color : vec4f,
- @location(2) uv : vec2f,
- @location(3) texIndex : f32,
-};
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) color : vec4f,
- @location(1) uv : vec2f,
- @location(2) texIndex : f32,
-};
-
-@vertex fn vs_main(v : VSIn) -> VSOut {
- var out : VSOut;
- out.pos = frame.projection * vec4f(v.position, 0.0, 1.0);
- out.color = v.color;
- out.uv = v.uv;
- out.texIndex = v.texIndex;
- return out;
-}
-)";
-
-inline constexpr const char* kBatchWGSL_Fragment = R"(
-@group(1) @binding(0) var t0 : texture_2d<f32>;
-@group(1) @binding(1) var t1 : texture_2d<f32>;
-@group(1) @binding(2) var t2 : texture_2d<f32>;
-@group(1) @binding(3) var t3 : texture_2d<f32>;
-@group(1) @binding(4) var t4 : texture_2d<f32>;
-@group(1) @binding(5) var t5 : texture_2d<f32>;
-@group(1) @binding(6) var t6 : texture_2d<f32>;
-@group(1) @binding(7) var t7 : texture_2d<f32>;
-@group(1) @binding(8) var s0 : sampler;
-@group(1) @binding(9) var s1 : sampler;
-@group(1) @binding(10) var s2 : sampler;
-@group(1) @binding(11) var s3 : sampler;
-@group(1) @binding(12) var s4 : sampler;
-@group(1) @binding(13) var s5 : sampler;
-@group(1) @binding(14) var s6 : sampler;
-@group(1) @binding(15) var s7 : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) color : vec4f,
- @location(1) uv : vec2f,
- @location(2) texIndex : f32,
-};
-
-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
- let idx = i32(v.texIndex + 0.5);
- var c : vec4f;
- if (idx == 0) { c = textureSampleLevel(t0, s0, v.uv, 0.0); }
- else if (idx == 1) { c = textureSampleLevel(t1, s1, v.uv, 0.0); }
- else if (idx == 2) { c = textureSampleLevel(t2, s2, v.uv, 0.0); }
- else if (idx == 3) { c = textureSampleLevel(t3, s3, v.uv, 0.0); }
- else if (idx == 4) { c = textureSampleLevel(t4, s4, v.uv, 0.0); }
- else if (idx == 5) { c = textureSampleLevel(t5, s5, v.uv, 0.0); }
- else if (idx == 6) { c = textureSampleLevel(t6, s6, v.uv, 0.0); }
- else { c = textureSampleLevel(t7, s7, v.uv, 0.0); }
- return c * v.color;
-}
-)";
-
-// =============================================================================
-// particle.esshader — the instanced particle quad: quad corner + uv per vertex
-// (locations 0..1), instance stream at locations 2..7, FrameConstants at
-// group 0. u_texture is single-texture, so the twin declares just the t0/s0
-// pair of the group-1 convention (declare-exactly-what-you-use — the binding
-// masks are scanned from the source).
-// =============================================================================
-
-inline constexpr const char* kParticleWGSL_Vertex = R"(
-struct FrameConstants { projection : mat4x4f };
-@group(0) @binding(0) var<uniform> frame : FrameConstants;
-
-struct VSIn {
- @location(0) position : vec2f,
- @location(1) uv : vec2f,
- @location(2) instPosition : vec2f,
- @location(3) instSize : vec2f,
- @location(4) instRotation : f32,
- @location(5) instColor : vec4f,
- @location(6) instUvOffset : vec2f,
- @location(7) instUvScale : vec2f,
-};
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) uv : vec2f,
- @location(1) color : vec4f,
-};
-
-@vertex fn vs_main(v : VSIn) -> VSOut {
- let scaled = v.position * v.instSize;
-
- let cosR = cos(v.instRotation);
- let sinR = sin(v.instRotation);
- let rotated = vec2f(
- scaled.x * cosR - scaled.y * sinR,
- scaled.x * sinR + scaled.y * cosR
- );
-
- let worldPos = rotated + v.instPosition;
-
- var out : VSOut;
- out.pos = frame.projection * vec4f(worldPos, 0.0, 1.0);
- out.uv = v.uv * v.instUvScale + v.instUvOffset;
- out.color = v.instColor;
- return out;
-}
-)";
-
-inline constexpr const char* kParticleWGSL_Fragment = R"(
-@group(1) @binding(0) var t0 : texture_2d<f32>;
-@group(1) @binding(8) var s0 : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) uv : vec2f,
- @location(1) color : vec4f,
-};
-
-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
- let texColor = textureSampleLevel(t0, s0, v.uv, 0.0);
- return texColor * v.color;
-}
-)";
-
-// =============================================================================
-// The post-process blit (PostProcessPipeline's inline GLSL pair) — a clip-space
-// quad passing uv through, sampling the captured target as t0/s0. Effect
-// shaders are Phase 3; the blit keeps the pipeline initialized and its
-// passthrough path working.
-// =============================================================================
-
-inline constexpr const char* kBlitWGSL_Vertex = R"(
-struct VSIn {
- @location(0) position : vec2f,
- @location(1) uv : vec2f,
-};
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) uv : vec2f,
-};
-
-@vertex fn vs_main(v : VSIn) -> VSOut {
- var out : VSOut;
- out.pos = vec4f(v.position, 0.0, 1.0);
- out.uv = v.uv;
- return out;
-}
-)";
-
-inline constexpr const char* kBlitWGSL_Fragment = R"(
-@group(1) @binding(0) var t0 : texture_2d<f32>;
-@group(1) @binding(8) var s0 : sampler;
-
-struct VSOut {
- @builtin(position) pos : vec4f,
- @location(0) uv : vec2f,
-};
-
-@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
- return textureSampleLevel(t0, s0, v.uv, 0.0);
-}
-)";
-
-} // namespace esengine::webgpu
diff --git a/src/esengine/resource/ResourceManager.cpp b/src/esengine/resource/ResourceManager.cpp
index bc57d293e..4e812309d 100644
--- a/src/esengine/resource/ResourceManager.cpp
+++ b/src/esengine/resource/ResourceManager.cpp
@@ -12,6 +12,7 @@
*/
#include "ResourceManager.hpp"
+#include "ShaderParser.hpp"
#include "../text/BitmapFont.hpp"
#include "../core/Log.hpp"
#include "../renderer/GfxDevice.hpp"
@@ -70,6 +71,12 @@ GfxShaderLanguage ResourceManager::preferredShaderLanguage() const {
: GfxShaderLanguage::GLSL_ES300;
}
+ShaderTargetLanguage ResourceManager::preferredShaderTarget() const {
+ return preferredShaderLanguage() == GfxShaderLanguage::WGSL
+ ? ShaderTargetLanguage::WGSL
+ : ShaderTargetLanguage::GLSL_ES300;
+}
+
ShaderHandle ResourceManager::createShader(const std::string& vertSrc, const std::string& fragSrc,
bool rewriteLoose, GfxShaderLanguage language) {
if (!device_) return {};
diff --git a/src/esengine/resource/ResourceManager.hpp b/src/esengine/resource/ResourceManager.hpp
index 251668ed0..c8eff913f 100644
--- a/src/esengine/resource/ResourceManager.hpp
+++ b/src/esengine/resource/ResourceManager.hpp
@@ -37,6 +37,8 @@ namespace esengine { class GfxDevice; }
namespace esengine::resource {
+enum class ShaderTargetLanguage : u8; // ShaderParser.hpp
+
// =============================================================================
// Resource Manager Statistics
// =============================================================================
@@ -136,6 +138,14 @@ public:
*/
GfxShaderLanguage preferredShaderLanguage() const;
+ /**
+ * @brief preferredShaderLanguage() as a ShaderParser assembly target.
+ * @details The other half of the seam: creation sites parse one .esshader
+ * (whose WGSL twin rides the same file) and assemble both stages
+ * for this target — no per-backend branches.
+ */
+ ShaderTargetLanguage preferredShaderTarget() const;
+
/**
* @brief Creates a shader from source strings
* @param vertSrc Vertex shader GLSL source
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 46585b0bb..642c27b29 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -139,6 +139,8 @@ if(EMSCRIPTEN)
renderer/webgpu_bringup.cpp
${CMAKE_SOURCE_DIR}/src/esengine/renderer/webgpu/WebGPUDevice.cpp
${CMAKE_SOURCE_DIR}/src/esengine/core/Log.cpp
+ # The twins assemble from the embedded .esshaders (WGSL target).
+ ${CMAKE_SOURCE_DIR}/src/esengine/resource/ShaderParser.cpp
)
target_include_directories(webgpu_bringup PRIVATE
${CMAKE_SOURCE_DIR}/src
diff --git a/tests/renderer/webgpu_bringup.cpp b/tests/renderer/webgpu_bringup.cpp
index f0bb09492..5e80a7932 100644
--- a/tests/renderer/webgpu_bringup.cpp
+++ b/tests/renderer/webgpu_bringup.cpp
@@ -6,8 +6,8 @@
* A browser-run program: the host page acquires a GPUDevice (navigator.gpu) and
* hands it over via Module.preinitializedWebGPUDevice; this program then drives
* the REAL WebGPUDevice through the same RHI calls the engine's render path
- * makes, with the engine's WGSL twins (WGSLTwins.hpp). Each frame is THREE
- * passes:
+ * makes, with the WGSL twins assembled from the embedded .esshaders by
+ * ShaderParser (the production path's source). Each frame is THREE passes:
* A. offscreen: a framebuffer with color + depth-stencil attachments, all
* load-op cleared (color green) — the right batch quad then SAMPLES the
* rendered color target
@@ -26,7 +26,8 @@
* The electron runner (desktop/scripts/webgpu-bringup.mjs) asserts the pixels.
*/
#include "esengine/renderer/webgpu/WebGPUDevice.hpp"
-#include "esengine/renderer/webgpu/WGSLTwins.hpp"
+#include "esengine/renderer/ShaderEmbeds.generated.hpp"
+#include "esengine/resource/ShaderParser.hpp"
#include <emscripten.h>
#include <emscripten/html5.h>
@@ -256,16 +257,24 @@ int main() {
g_offscreenFb = device.createFramebuffer(fbDesc);
// --- Programs + layouts + pipelines (the engine's streams, byte for byte,
- // with the engine's WGSL twins).
+ // with the WGSL twins assembled from the embedded .esshaders).
+ const auto assembleWGSL = [](const char* embed, resource::ShaderStage stage) {
+ auto parsed = resource::ShaderParser::parse(embed);
+ return resource::ShaderParser::assembleStage(parsed, stage, "", {},
+ resource::ShaderTargetLanguage::WGSL);
+ };
+ const std::string shapeVs = assembleWGSL(ShaderEmbeds::SHAPE, resource::ShaderStage::Vertex);
+ const std::string shapeFs = assembleWGSL(ShaderEmbeds::SHAPE, resource::ShaderStage::Fragment);
+ const std::string batchVs = assembleWGSL(ShaderEmbeds::BATCH, resource::ShaderStage::Vertex);
+ const std::string batchFs = assembleWGSL(ShaderEmbeds::BATCH, resource::ShaderStage::Fragment);
ShaderHandle shapeProgram = device.createProgram(
- GfxShaderSource{GfxShaderLanguage::WGSL,
- webgpu::kShapeWGSL_Vertex, webgpu::kShapeWGSL_Fragment},
+ GfxShaderSource{GfxShaderLanguage::WGSL, shapeVs.c_str(), shapeFs.c_str()},
nullptr, 0, nullptr, nullptr);
ShaderHandle batchProgram = device.createProgram(
- GfxShaderSource{GfxShaderLanguage::WGSL,
- webgpu::kBatchWGSL_Vertex, webgpu::kBatchWGSL_Fragment},
+ GfxShaderSource{GfxShaderLanguage::WGSL, batchVs.c_str(), batchFs.c_str()},
nullptr, 0, nullptr, nullptr);
- if (shapeProgram == ShaderHandle::Invalid || batchProgram == ShaderHandle::Invalid) {
+ if (shapeVs.empty() || shapeFs.empty() || batchVs.empty() || batchFs.empty() ||
+ shapeProgram == ShaderHandle::Invalid || batchProgram == ShaderHandle::Invalid) {
std::printf("BRINGUP_FAIL programs\n");
return 1;
}
diff --git a/tests/renderer/webgpu_engine_bringup.cpp b/tests/renderer/webgpu_engine_bringup.cpp
index 7587039a0..745545107 100644
--- a/tests/renderer/webgpu_engine_bringup.cpp
+++ b/tests/renderer/webgpu_engine_bringup.cpp
@@ -24,6 +24,7 @@
#include "esengine/ecs/components/Transform.hpp"
#include "esengine/ecs/components/Sprite.hpp"
#include "esengine/ecs/components/ShapeRenderer.hpp"
+#include "esengine/ecs/components/Light2D.hpp"
#include "esengine/renderer/RenderContext.hpp"
#include "esengine/renderer/RenderFrame.hpp"
#include "esengine/renderer/webgpu/WebGPUDevice.hpp"
@@ -199,6 +200,42 @@ bool buildScene(EstellaContext& ctx, ecs::Registry& registry) {
s.color = {1.0f, 0.0f, 1.0f, 1.0f};
s.size = {64.0f, 64.0f};
}
+
+ // Lit sprite: white, bottom-left (world center 64,80, size 48) — drives
+ // the batch LIT variant through the production path. Lighting is pinned by
+ // the two explicit lights below: white * (0.2 ambient + red point) ≈
+ // (255, 51, 51) at the sprite center on BOTH backends.
+ const u8 white[4] = {255, 255, 255, 255};
+ auto whiteTex = rm.createTexture(1, 1, ConstSpan<u8>(white, sizeof(white)),
+ TextureFormat::RGBA8);
+ if (!whiteTex.isValid()) return false;
+ {
+ auto e = registry.create();
+ auto& t = registry.emplace<ecs::Transform>(e);
+ t.position = {64.0f, 80.0f, 0.0f};
+ auto& s = registry.emplace<ecs::Sprite>(e);
+ s.texture = whiteTex;
+ s.size = {48.0f, 48.0f};
+ s.lit = true;
+ }
+ {
+ auto e = registry.create();
+ auto& t = registry.emplace<ecs::Transform>(e);
+ t.position = {64.0f, 80.0f, 0.0f}; // centered on the lit sprite: ndotl = 1, atten = 1
+ auto& l = registry.emplace<ecs::Light2D>(e);
+ l.type = static_cast<i32>(ecs::Light2DType::Point);
+ l.color = {1.0f, 0.0f, 0.0f, 1.0f};
+ l.intensity = 1.0f;
+ l.radius = 200.0f;
+ }
+ {
+ auto e = registry.create();
+ registry.emplace<ecs::Transform>(e);
+ auto& l = registry.emplace<ecs::Light2D>(e);
+ l.type = static_cast<i32>(ecs::Light2DType::Ambient);
+ l.color = {1.0f, 1.0f, 1.0f, 1.0f};
+ l.intensity = 0.2f;
+ }
return true;
}
@@ -259,6 +296,15 @@ int main() {
if (!compileEmitterProbe(context, useGL)) {
return 1;
}
+ // The batch feature variants compile in both languages now (SDF has no
+ // scene here — a text glyph atlas is out of scope — but a bad module
+ // surfaces as a validation error the runner counts; LIT also renders via
+ // the lit sprite above).
+ auto& rf = context.require<RenderFrame>();
+ if (rf.batchProgram({"SDF"}) == 0 || rf.batchProgram({"LIT"}) == 0) {
+ std::printf("PARITY_FAIL batch variants\n");
+ return 1;
+ }
std::printf("PARITY_INIT_OK\n");
emscripten_set_main_loop(renderFrame, 0, /*simulate_infinite_loop=*/false);