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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(render): the 2D shadow mask is read the way the backend stored it
esengine8cf5c9d1191d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 55e497d63..1572b81bc 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -60,10 +60,13 @@ published separately; it ships inside the editor.which costs a sprite batch one merge slot in a scene that has 2D shadows and nothing atall in one that does not.- KNOWN GAP: on the WebGPU backend a scene with more than one casting light loses the- shadows of some of them — the lights still light it, and one casting light is correct- on both backends. WebGL2 is correct throughout. Documented under the lighting guide's- Known gaps; the criterion that shows it runs on WebGL2 only and says why.++ The mask is a target the frame renders and then samples, and the two backends store+ the rows of one in opposite order — so the height in the lighting block carries a+ SIGN saying which. It cannot be a compile-time branch: one shader source serves both+ backends, because the WGSL a material runs is translated from the GLSL. Read upside+ down the effect is invisible in any scene that is symmetric about its middle, which+ every criterion here was until one of them stopped being.- **One curve, sampled by everything that has one.** A timeline channel carried keyswith tangents and six interpolations, drawn by an editor that understood them; adiff --git a/docs/astro/src/content/docs/graphics/lighting.mdx b/docs/astro/src/content/docs/graphics/lighting.mdxindex 72bebc858..9090938bf 100644--- a/docs/astro/src/content/docs/graphics/lighting.mdx+++ b/docs/astro/src/content/docs/graphics/lighting.mdx@@ -290,13 +290,6 @@ saying something about its art direction; the fix is to turn shadows off (`shadoof 0 on a sun, or a smaller `radius` that takes a light out of view) on the ones whoseshadows nobody looks at.-### Known gaps--- **WebGPU, more than one casting light.** On the WebGPU backend a scene with two or- more shadow-casting lights loses the shadows of some of them; the lights still light- it. One casting light is correct on both backends, and WebGL2 is correct throughout.- The criterion that shows it (`shadow2d-light-cap`) runs on WebGL2 only, and says so.-## Linear color spaceBy default the engine renders **gamma-space** — colors blend as-authored, the way 2Ddiff --git a/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx b/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdxindex fc1443342..d819990f6 100644--- a/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx+++ b/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx@@ -270,12 +270,6 @@ cmds.spawn()处理办法是把没人会看那道影子的光关掉阴影(太阳的 `shadowDistance` 设 0,或者用更小的 `radius`让某个光离开视野)。-### 已知缺口--- **WebGPU 上,投射光多于一个时**:同屏有两个及以上投射阴影的光时,WebGPU 后端会丢掉其中一部分光的- 阴影(光本身照常照亮)。**一个投射光在两个后端都正确**,WebGL2 全程正确。能暴露它的判据- (`shadow2d-light-cap`)只在 WebGL2 上跑,并在注册表里注明了原因。-## 线性色彩空间引擎默认以**伽马空间**渲染——颜色按作者所写直接混合,这是 2D 管线的经典方式。项目可以切换到diff --git a/fixtures/scenes/mat-builtin-lit/lit.esshader b/fixtures/scenes/mat-builtin-lit/lit.esshaderindex ead69ad8f..333eb529e 100644--- a/fixtures/scenes/mat-builtin-lit/lit.esshader+++ b/fixtures/scenes/mat-builtin-lit/lit.esshader@@ -27,7 +27,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 5330079245724fa2-// assembled-hash: 6d58d9851d30eb16+// assembled-hash: cb37ad9d1a961ffbstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -108,7 +108,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 5330079245724fa2-// assembled-hash: 6d58d9851d30eb16+// assembled-hash: cb37ad9d1a961ffbstruct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,@@ -307,96 +307,113 @@ fn shadowFactor3D_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf2_u003b_i1_u003b_i1_u003fn shadowFactor2D_u0028_f1_u003b_vf2_u003b(channel: ptr<function, f32>, worldPos_1: ptr<function, vec2<f32>>) -> f32 {var clip: vec4<f32>;+ var ndc: vec2<f32>;+ var v: f32;+ var local: f32;var uv: vec2<f32>;var m: vec4<f32>;var hidden: f32;- var local: f32;var local_1: f32;var local_2: f32;+ var local_3: f32;var phi_194_: bool;- let _e93 = (*channel);- let _e94 = (_e93 < 0f);- phi_194_ = _e94;- if !(_e94) {- let _e98 = unnamed_1.u_shadow2DRect[2u];- phi_194_ = (_e98 <= 0f);+ let _e96 = (*channel);+ let _e97 = (_e96 < 0f);+ phi_194_ = _e97;+ if !(_e97) {+ let _e101 = unnamed_1.u_shadow2DRect[2u];+ phi_194_ = (_e101 <= 0f);}- let _e101 = phi_194_;- if _e101 {+ let _e104 = phi_194_;+ if _e104 {return 1f;}- let _e103 = unnamed.u_projection;- let _e104 = (*worldPos_1);- clip = (_e103 * vec4<f32>(_e104.x, _e104.y, 0f, 1f));- let _e110 = clip[3u];- if (_e110 <= 0f) {+ let _e106 = unnamed.u_projection;+ let _e107 = (*worldPos_1);+ clip = (_e106 * vec4<f32>(_e107.x, _e107.y, 0f, 1f));+ let _e113 = clip[3u];+ if (_e113 <= 0f) {return 1f;}- let _e113 = unnamed_1.u_shadow2DRect;let _e115 = clip;let _e118 = clip[3u];- let _e125 = unnamed_1.u_shadow2DRect;- uv = (_e113.xy + ((((_e115.xy / vec2(_e118)) * 0.5f) + vec2(0.5f)) * _e125.zw));- let _e129 = uv;- let _e130 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e129, 0f);- m = _e130;- let _e131 = (*channel);- if (_e131 < 0.5f) {- let _e134 = m[0u];- local = _e134;+ ndc = (((_e115.xy / vec2(_e118)) * 0.5f) + vec2(0.5f));+ let _e126 = unnamed_1.u_shadow2DRect[3u];+ if (_e126 < 0f) {+ let _e129 = ndc[1u];+ local = (1f - _e129);+ } else {+ let _e132 = ndc[1u];+ local = _e132;+ }+ let _e133 = local;+ v = _e133;+ let _e135 = unnamed_1.u_shadow2DRect;+ let _e138 = ndc[0u];+ let _e141 = unnamed_1.u_shadow2DRect[2u];+ let _e143 = v;+ let _e146 = unnamed_1.u_shadow2DRect[3u];+ uv = (_e135.xy + vec2<f32>((_e138 * _e141), (_e143 * abs(_e146))));+ let _e151 = uv;+ let _e152 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e151, 0f);+ m = _e152;+ let _e153 = (*channel);+ if (_e153 < 0.5f) {+ let _e156 = m[0u];+ local_1 = _e156;} else {- let _e135 = (*channel);- if (_e135 < 1.5f) {- let _e138 = m[1u];- local_1 = _e138;+ let _e157 = (*channel);+ if (_e157 < 1.5f) {+ let _e160 = m[1u];+ local_2 = _e160;} else {- let _e139 = (*channel);- if (_e139 < 2.5f) {- let _e142 = m[2u];- local_2 = _e142;+ let _e161 = (*channel);+ if (_e161 < 2.5f) {+ let _e164 = m[2u];+ local_3 = _e164;} else {- let _e144 = m[3u];- local_2 = _e144;+ let _e166 = m[3u];+ local_3 = _e166;}- let _e145 = local_2;- local_1 = _e145;+ let _e167 = local_3;+ local_2 = _e167;}- let _e146 = local_1;- local = _e146;+ let _e168 = local_2;+ local_1 = _e168;}- let _e147 = local;- hidden = _e147;- let _e148 = hidden;- return (1f - min(_e148, 1f));+ let _e169 = local_1;+ hidden = _e169;+ let _e170 = hidden;+ return (1f - min(_e170, 1f));}fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<f32>>, sh: ptr<function, vec4<f32>>, toLight: ptr<function, vec3<f32>>, dist: ptr<function, f32>) -> f32 {var axis: vec2<f32>;- var local_3: vec2<f32>;- var toFrag: vec2<f32>;var local_4: vec2<f32>;+ var toFrag: vec2<f32>;+ var local_5: vec2<f32>;let _e92 = (*sp);let _e94 = (*sp);if (dot(_e92.xy, _e94.xy) > 0.00000001f) {let _e98 = (*sp);- local_3 = normalize(_e98.xy);+ local_4 = normalize(_e98.xy);} else {- local_3 = vec2<f32>(0f, -1f);+ local_4 = vec2<f32>(0f, -1f);}- let _e101 = local_3;+ let _e101 = local_4;axis = _e101;let _e102 = (*dist);if (_e102 > 0.0001f) {let _e104 = (*toLight);let _e107 = (*dist);- local_4 = (-(_e104.xy) / vec2(_e107));+ local_5 = (-(_e104.xy) / vec2(_e107));} else {let _e110 = axis;- local_4 = _e110;+ local_5 = _e110;}- let _e111 = local_4;+ let _e111 = local_5;toFrag = _e111;let _e113 = (*sp)[3u];let _e115 = (*sp)[2u];@@ -406,18 +423,18 @@ fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<}fn towardLight_u0028_vf3_u003b_vf3_u003b(toLight_1: ptr<function, vec3<f32>>, N_1: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_5: vec3<f32>;+ var local_6: vec3<f32>;let _e87 = (*toLight_1);let _e88 = (*toLight_1);if (dot(_e87, _e88) > 0.00000001f) {let _e91 = (*toLight_1);- local_5 = normalize(_e91);+ local_6 = normalize(_e91);} else {let _e93 = (*N_1);- local_5 = _e93;+ local_6 = _e93;}- let _e94 = local_5;+ let _e94 = local_6;return _e94;}@@ -521,7 +538,7 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_10: vec3<f32>;var param_11: vec3<f32>;var toLight_3: vec2<f32>;- var local_6: vec2<f32>;+ var local_7: vec2<f32>;var sp_1: vec4<f32>;var toL_1: vec3<f32>;var param_12: vec4<f32>;@@ -560,8 +577,8 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_38: f32;var param_39: vec3<f32>;var param_40: f32;- var phi_787_: bool;- var phi_841_: bool;+ var phi_809_: bool;+ var phi_863_: bool;let _e154 = (*albedo);let _e155 = (*metallic);@@ -629,21 +646,21 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0let _e222 = pd_1;if (dot(_e220.xy, _e222.xy) > 0.00000001f) {let _e226 = pd_1;- local_6 = normalize(-(_e226.xy));+ local_7 = normalize(-(_e226.xy));} else {- local_6 = vec2<f32>(0f, 0f);+ local_7 = vec2<f32>(0f, 0f);}- let _e230 = local_6;+ let _e230 = local_7;toLight_3 = _e230;let _e232 = sh_3[1u];let _e233 = (_e232 > 0f);- phi_787_ = _e233;+ phi_809_ = _e233;if _e233 {let _e234 = toLight_3;let _e235 = toLight_3;- phi_787_ = (dot(_e234, _e235) > 0.5f);+ phi_809_ = (dot(_e234, _e235) > 0.5f);}- let _e239 = phi_787_;+ let _e239 = phi_809_;castShadow = _e239;let _e240 = (*worldPos_3);let _e242 = toLight_3;@@ -688,12 +705,12 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}}let _e275 = castShadow;- phi_841_ = _e275;+ phi_863_ = _e275;if _e275 {let _e277 = col[3u];- phi_841_ = (_e277 > 0f);+ phi_863_ = (_e277 > 0f);}- let _e280 = phi_841_;+ let _e280 = phi_863_;let _e281 = atten;if (_e280 && (_e281 > 0f)) {let _e284 = i;@@ -808,18 +825,18 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}fn viewDirection_u0028_vf3_u003b(worldPos_4: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_7: vec3<f32>;+ var local_8: vec3<f32>;let _e88 = unnamed.u_camera[3u];if (_e88 > 0.5f) {let _e91 = unnamed.u_camera;let _e93 = (*worldPos_4);- local_7 = normalize((_e91.xyz - _e93));+ local_8 = normalize((_e91.xyz - _e93));} else {let _e97 = unnamed.u_camera;- local_7 = _e97.xyz;+ local_8 = _e97.xyz;}- let _e99 = local_7;+ let _e99 = local_8;return _e99;}diff --git a/fixtures/scenes/mat-lit-flatnormal/lit-fn.esshader b/fixtures/scenes/mat-lit-flatnormal/lit-fn.esshaderindex 6c733423d..351316e28 100644--- a/fixtures/scenes/mat-lit-flatnormal/lit-fn.esshader+++ b/fixtures/scenes/mat-lit-flatnormal/lit-fn.esshader@@ -47,7 +47,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: a8853729e0f76d55-// assembled-hash: ec80a65441a56f13+// assembled-hash: ee5bb8b28abd70dastruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -120,7 +120,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: a8853729e0f76d55-// assembled-hash: ec80a65441a56f13+// assembled-hash: ee5bb8b28abd70dastruct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,@@ -313,96 +313,113 @@ fn shadowFactor3D_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf2_u003b_i1_u003b_i1_u003fn shadowFactor2D_u0028_f1_u003b_vf2_u003b(channel: ptr<function, f32>, worldPos_1: ptr<function, vec2<f32>>) -> f32 {var clip: vec4<f32>;+ var ndc: vec2<f32>;+ var v: f32;+ var local: f32;var uv: vec2<f32>;var m: vec4<f32>;var hidden: f32;- var local: f32;var local_1: f32;var local_2: f32;+ var local_3: f32;var phi_194_: bool;- let _e92 = (*channel);- let _e93 = (_e92 < 0f);- phi_194_ = _e93;- if !(_e93) {- let _e97 = unnamed_1.u_shadow2DRect[2u];- phi_194_ = (_e97 <= 0f);+ let _e95 = (*channel);+ let _e96 = (_e95 < 0f);+ phi_194_ = _e96;+ if !(_e96) {+ let _e100 = unnamed_1.u_shadow2DRect[2u];+ phi_194_ = (_e100 <= 0f);}- let _e100 = phi_194_;- if _e100 {+ let _e103 = phi_194_;+ if _e103 {return 1f;}- let _e102 = unnamed.u_projection;- let _e103 = (*worldPos_1);- clip = (_e102 * vec4<f32>(_e103.x, _e103.y, 0f, 1f));- let _e109 = clip[3u];- if (_e109 <= 0f) {+ let _e105 = unnamed.u_projection;+ let _e106 = (*worldPos_1);+ clip = (_e105 * vec4<f32>(_e106.x, _e106.y, 0f, 1f));+ let _e112 = clip[3u];+ if (_e112 <= 0f) {return 1f;}- let _e112 = unnamed_1.u_shadow2DRect;let _e114 = clip;let _e117 = clip[3u];- let _e124 = unnamed_1.u_shadow2DRect;- uv = (_e112.xy + ((((_e114.xy / vec2(_e117)) * 0.5f) + vec2(0.5f)) * _e124.zw));- let _e128 = uv;- let _e129 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e128, 0f);- m = _e129;- let _e130 = (*channel);- if (_e130 < 0.5f) {- let _e133 = m[0u];- local = _e133;+ ndc = (((_e114.xy / vec2(_e117)) * 0.5f) + vec2(0.5f));+ let _e125 = unnamed_1.u_shadow2DRect[3u];+ if (_e125 < 0f) {+ let _e128 = ndc[1u];+ local = (1f - _e128);+ } else {+ let _e131 = ndc[1u];+ local = _e131;+ }+ let _e132 = local;+ v = _e132;+ let _e134 = unnamed_1.u_shadow2DRect;+ let _e137 = ndc[0u];+ let _e140 = unnamed_1.u_shadow2DRect[2u];+ let _e142 = v;+ let _e145 = unnamed_1.u_shadow2DRect[3u];+ uv = (_e134.xy + vec2<f32>((_e137 * _e140), (_e142 * abs(_e145))));+ let _e150 = uv;+ let _e151 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e150, 0f);+ m = _e151;+ let _e152 = (*channel);+ if (_e152 < 0.5f) {+ let _e155 = m[0u];+ local_1 = _e155;} else {- let _e134 = (*channel);- if (_e134 < 1.5f) {- let _e137 = m[1u];- local_1 = _e137;+ let _e156 = (*channel);+ if (_e156 < 1.5f) {+ let _e159 = m[1u];+ local_2 = _e159;} else {- let _e138 = (*channel);- if (_e138 < 2.5f) {- let _e141 = m[2u];- local_2 = _e141;+ let _e160 = (*channel);+ if (_e160 < 2.5f) {+ let _e163 = m[2u];+ local_3 = _e163;} else {- let _e143 = m[3u];- local_2 = _e143;+ let _e165 = m[3u];+ local_3 = _e165;}- let _e144 = local_2;- local_1 = _e144;+ let _e166 = local_3;+ local_2 = _e166;}- let _e145 = local_1;- local = _e145;+ let _e167 = local_2;+ local_1 = _e167;}- let _e146 = local;- hidden = _e146;- let _e147 = hidden;- return (1f - min(_e147, 1f));+ let _e168 = local_1;+ hidden = _e168;+ let _e169 = hidden;+ return (1f - min(_e169, 1f));}fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<f32>>, sh: ptr<function, vec4<f32>>, toLight: ptr<function, vec3<f32>>, dist: ptr<function, f32>) -> f32 {var axis: vec2<f32>;- var local_3: vec2<f32>;- var toFrag: vec2<f32>;var local_4: vec2<f32>;+ var toFrag: vec2<f32>;+ var local_5: vec2<f32>;let _e91 = (*sp);let _e93 = (*sp);if (dot(_e91.xy, _e93.xy) > 0.00000001f) {let _e97 = (*sp);- local_3 = normalize(_e97.xy);+ local_4 = normalize(_e97.xy);} else {- local_3 = vec2<f32>(0f, -1f);+ local_4 = vec2<f32>(0f, -1f);}- let _e100 = local_3;+ let _e100 = local_4;axis = _e100;let _e101 = (*dist);if (_e101 > 0.0001f) {let _e103 = (*toLight);let _e106 = (*dist);- local_4 = (-(_e103.xy) / vec2(_e106));+ local_5 = (-(_e103.xy) / vec2(_e106));} else {let _e109 = axis;- local_4 = _e109;+ local_5 = _e109;}- let _e110 = local_4;+ let _e110 = local_5;toFrag = _e110;let _e112 = (*sp)[3u];let _e114 = (*sp)[2u];@@ -412,18 +429,18 @@ fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<}fn towardLight_u0028_vf3_u003b_vf3_u003b(toLight_1: ptr<function, vec3<f32>>, N_1: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_5: vec3<f32>;+ var local_6: vec3<f32>;let _e86 = (*toLight_1);let _e87 = (*toLight_1);if (dot(_e86, _e87) > 0.00000001f) {let _e90 = (*toLight_1);- local_5 = normalize(_e90);+ local_6 = normalize(_e90);} else {let _e92 = (*N_1);- local_5 = _e92;+ local_6 = _e92;}- let _e93 = local_5;+ let _e93 = local_6;return _e93;}@@ -527,7 +544,7 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_10: vec3<f32>;var param_11: vec3<f32>;var toLight_3: vec2<f32>;- var local_6: vec2<f32>;+ var local_7: vec2<f32>;var sp_1: vec4<f32>;var toL_1: vec3<f32>;var param_12: vec4<f32>;@@ -566,8 +583,8 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_38: f32;var param_39: vec3<f32>;var param_40: f32;- var phi_787_: bool;- var phi_841_: bool;+ var phi_809_: bool;+ var phi_863_: bool;let _e153 = (*albedo);let _e154 = (*metallic);@@ -635,21 +652,21 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0let _e221 = pd_1;if (dot(_e219.xy, _e221.xy) > 0.00000001f) {let _e225 = pd_1;- local_6 = normalize(-(_e225.xy));+ local_7 = normalize(-(_e225.xy));} else {- local_6 = vec2<f32>(0f, 0f);+ local_7 = vec2<f32>(0f, 0f);}- let _e229 = local_6;+ let _e229 = local_7;toLight_3 = _e229;let _e231 = sh_3[1u];let _e232 = (_e231 > 0f);- phi_787_ = _e232;+ phi_809_ = _e232;if _e232 {let _e233 = toLight_3;let _e234 = toLight_3;- phi_787_ = (dot(_e233, _e234) > 0.5f);+ phi_809_ = (dot(_e233, _e234) > 0.5f);}- let _e238 = phi_787_;+ let _e238 = phi_809_;castShadow = _e238;let _e239 = (*worldPos_3);let _e241 = toLight_3;@@ -694,12 +711,12 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}}let _e274 = castShadow;- phi_841_ = _e274;+ phi_863_ = _e274;if _e274 {let _e276 = col[3u];- phi_841_ = (_e276 > 0f);+ phi_863_ = (_e276 > 0f);}- let _e279 = phi_841_;+ let _e279 = phi_863_;let _e280 = atten;if (_e279 && (_e280 > 0f)) {let _e283 = i;@@ -814,18 +831,18 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}fn viewDirection_u0028_vf3_u003b(worldPos_4: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_7: vec3<f32>;+ var local_8: vec3<f32>;let _e87 = unnamed.u_camera[3u];if (_e87 > 0.5f) {let _e90 = unnamed.u_camera;let _e92 = (*worldPos_4);- local_7 = normalize((_e90.xyz - _e92));+ local_8 = normalize((_e90.xyz - _e92));} else {let _e96 = unnamed.u_camera;- local_7 = _e96.xyz;+ local_8 = _e96.xyz;}- let _e98 = local_7;+ let _e98 = local_8;return _e98;}diff --git a/fixtures/scenes/mat-lit-normal/lit-normal.esshader b/fixtures/scenes/mat-lit-normal/lit-normal.esshaderindex 2a233b336..3db252db8 100644--- a/fixtures/scenes/mat-lit-normal/lit-normal.esshader+++ b/fixtures/scenes/mat-lit-normal/lit-normal.esshader@@ -47,7 +47,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 577c1ff7b66006ff-// assembled-hash: df25379d17a5c76a+// assembled-hash: 69321241a50459dbstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -120,7 +120,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 577c1ff7b66006ff-// assembled-hash: df25379d17a5c76a+// assembled-hash: 69321241a50459dbstruct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,@@ -313,96 +313,113 @@ fn shadowFactor3D_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf2_u003b_i1_u003b_i1_u003fn shadowFactor2D_u0028_f1_u003b_vf2_u003b(channel: ptr<function, f32>, worldPos_1: ptr<function, vec2<f32>>) -> f32 {var clip: vec4<f32>;+ var ndc: vec2<f32>;+ var v: f32;+ var local: f32;var uv: vec2<f32>;var m: vec4<f32>;var hidden: f32;- var local: f32;var local_1: f32;var local_2: f32;+ var local_3: f32;var phi_194_: bool;- let _e92 = (*channel);- let _e93 = (_e92 < 0f);- phi_194_ = _e93;- if !(_e93) {- let _e97 = unnamed_1.u_shadow2DRect[2u];- phi_194_ = (_e97 <= 0f);+ let _e95 = (*channel);+ let _e96 = (_e95 < 0f);+ phi_194_ = _e96;+ if !(_e96) {+ let _e100 = unnamed_1.u_shadow2DRect[2u];+ phi_194_ = (_e100 <= 0f);}- let _e100 = phi_194_;- if _e100 {+ let _e103 = phi_194_;+ if _e103 {return 1f;}- let _e102 = unnamed.u_projection;- let _e103 = (*worldPos_1);- clip = (_e102 * vec4<f32>(_e103.x, _e103.y, 0f, 1f));- let _e109 = clip[3u];- if (_e109 <= 0f) {+ let _e105 = unnamed.u_projection;+ let _e106 = (*worldPos_1);+ clip = (_e105 * vec4<f32>(_e106.x, _e106.y, 0f, 1f));+ let _e112 = clip[3u];+ if (_e112 <= 0f) {return 1f;}- let _e112 = unnamed_1.u_shadow2DRect;let _e114 = clip;let _e117 = clip[3u];- let _e124 = unnamed_1.u_shadow2DRect;- uv = (_e112.xy + ((((_e114.xy / vec2(_e117)) * 0.5f) + vec2(0.5f)) * _e124.zw));- let _e128 = uv;- let _e129 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e128, 0f);- m = _e129;- let _e130 = (*channel);- if (_e130 < 0.5f) {- let _e133 = m[0u];- local = _e133;+ ndc = (((_e114.xy / vec2(_e117)) * 0.5f) + vec2(0.5f));+ let _e125 = unnamed_1.u_shadow2DRect[3u];+ if (_e125 < 0f) {+ let _e128 = ndc[1u];+ local = (1f - _e128);+ } else {+ let _e131 = ndc[1u];+ local = _e131;+ }+ let _e132 = local;+ v = _e132;+ let _e134 = unnamed_1.u_shadow2DRect;+ let _e137 = ndc[0u];+ let _e140 = unnamed_1.u_shadow2DRect[2u];+ let _e142 = v;+ let _e145 = unnamed_1.u_shadow2DRect[3u];+ uv = (_e134.xy + vec2<f32>((_e137 * _e140), (_e142 * abs(_e145))));+ let _e150 = uv;+ let _e151 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e150, 0f);+ m = _e151;+ let _e152 = (*channel);+ if (_e152 < 0.5f) {+ let _e155 = m[0u];+ local_1 = _e155;} else {- let _e134 = (*channel);- if (_e134 < 1.5f) {- let _e137 = m[1u];- local_1 = _e137;+ let _e156 = (*channel);+ if (_e156 < 1.5f) {+ let _e159 = m[1u];+ local_2 = _e159;} else {- let _e138 = (*channel);- if (_e138 < 2.5f) {- let _e141 = m[2u];- local_2 = _e141;+ let _e160 = (*channel);+ if (_e160 < 2.5f) {+ let _e163 = m[2u];+ local_3 = _e163;} else {- let _e143 = m[3u];- local_2 = _e143;+ let _e165 = m[3u];+ local_3 = _e165;}- let _e144 = local_2;- local_1 = _e144;+ let _e166 = local_3;+ local_2 = _e166;}- let _e145 = local_1;- local = _e145;+ let _e167 = local_2;+ local_1 = _e167;}- let _e146 = local;- hidden = _e146;- let _e147 = hidden;- return (1f - min(_e147, 1f));+ let _e168 = local_1;+ hidden = _e168;+ let _e169 = hidden;+ return (1f - min(_e169, 1f));}fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<f32>>, sh: ptr<function, vec4<f32>>, toLight: ptr<function, vec3<f32>>, dist: ptr<function, f32>) -> f32 {var axis: vec2<f32>;- var local_3: vec2<f32>;- var toFrag: vec2<f32>;var local_4: vec2<f32>;+ var toFrag: vec2<f32>;+ var local_5: vec2<f32>;let _e91 = (*sp);let _e93 = (*sp);if (dot(_e91.xy, _e93.xy) > 0.00000001f) {let _e97 = (*sp);- local_3 = normalize(_e97.xy);+ local_4 = normalize(_e97.xy);} else {- local_3 = vec2<f32>(0f, -1f);+ local_4 = vec2<f32>(0f, -1f);}- let _e100 = local_3;+ let _e100 = local_4;axis = _e100;let _e101 = (*dist);if (_e101 > 0.0001f) {let _e103 = (*toLight);let _e106 = (*dist);- local_4 = (-(_e103.xy) / vec2(_e106));+ local_5 = (-(_e103.xy) / vec2(_e106));} else {let _e109 = axis;- local_4 = _e109;+ local_5 = _e109;}- let _e110 = local_4;+ let _e110 = local_5;toFrag = _e110;let _e112 = (*sp)[3u];let _e114 = (*sp)[2u];@@ -412,18 +429,18 @@ fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<}fn towardLight_u0028_vf3_u003b_vf3_u003b(toLight_1: ptr<function, vec3<f32>>, N_1: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_5: vec3<f32>;+ var local_6: vec3<f32>;let _e86 = (*toLight_1);let _e87 = (*toLight_1);if (dot(_e86, _e87) > 0.00000001f) {let _e90 = (*toLight_1);- local_5 = normalize(_e90);+ local_6 = normalize(_e90);} else {let _e92 = (*N_1);- local_5 = _e92;+ local_6 = _e92;}- let _e93 = local_5;+ let _e93 = local_6;return _e93;}@@ -527,7 +544,7 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_10: vec3<f32>;var param_11: vec3<f32>;var toLight_3: vec2<f32>;- var local_6: vec2<f32>;+ var local_7: vec2<f32>;var sp_1: vec4<f32>;var toL_1: vec3<f32>;var param_12: vec4<f32>;@@ -566,8 +583,8 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_38: f32;var param_39: vec3<f32>;var param_40: f32;- var phi_787_: bool;- var phi_841_: bool;+ var phi_809_: bool;+ var phi_863_: bool;let _e153 = (*albedo);let _e154 = (*metallic);@@ -635,21 +652,21 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0let _e221 = pd_1;if (dot(_e219.xy, _e221.xy) > 0.00000001f) {let _e225 = pd_1;- local_6 = normalize(-(_e225.xy));+ local_7 = normalize(-(_e225.xy));} else {- local_6 = vec2<f32>(0f, 0f);+ local_7 = vec2<f32>(0f, 0f);}- let _e229 = local_6;+ let _e229 = local_7;toLight_3 = _e229;let _e231 = sh_3[1u];let _e232 = (_e231 > 0f);- phi_787_ = _e232;+ phi_809_ = _e232;if _e232 {let _e233 = toLight_3;let _e234 = toLight_3;- phi_787_ = (dot(_e233, _e234) > 0.5f);+ phi_809_ = (dot(_e233, _e234) > 0.5f);}- let _e238 = phi_787_;+ let _e238 = phi_809_;castShadow = _e238;let _e239 = (*worldPos_3);let _e241 = toLight_3;@@ -694,12 +711,12 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}}let _e274 = castShadow;- phi_841_ = _e274;+ phi_863_ = _e274;if _e274 {let _e276 = col[3u];- phi_841_ = (_e276 > 0f);+ phi_863_ = (_e276 > 0f);}- let _e279 = phi_841_;+ let _e279 = phi_863_;let _e280 = atten;if (_e279 && (_e280 > 0f)) {let _e283 = i;@@ -814,18 +831,18 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}fn viewDirection_u0028_vf3_u003b(worldPos_4: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_7: vec3<f32>;+ var local_8: vec3<f32>;let _e87 = unnamed.u_camera[3u];if (_e87 > 0.5f) {let _e90 = unnamed.u_camera;let _e92 = (*worldPos_4);- local_7 = normalize((_e90.xyz - _e92));+ local_8 = normalize((_e90.xyz - _e92));} else {let _e96 = unnamed.u_camera;- local_7 = _e96.xyz;+ local_8 = _e96.xyz;}- let _e98 = local_7;+ let _e98 = local_8;return _e98;}diff --git a/fixtures/scenes/mat-lit/lit.esshader b/fixtures/scenes/mat-lit/lit.esshaderindex 6971ea43e..02cd613c3 100644--- a/fixtures/scenes/mat-lit/lit.esshader+++ b/fixtures/scenes/mat-lit/lit.esshader@@ -53,7 +53,7 @@ void main() {// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 8c118f2b442c055e-// assembled-hash: 5d4306ee1d4d91ec+// assembled-hash: 76e79c2465ad522fstruct gl_PerVertex {@builtin(position) gl_Position: vec4<f32>,gl_PointSize: f32,@@ -122,7 +122,7 @@ fn vs_main(@location(0) a_position: vec3<f32>, @location(1) a_color: vec4<f32>,// GENERATED by tools/gen-shader-twins.mjs (glslang + naga) - do not edit.// Regenerate after changing the GLSL stages above.// source-hash: 8c118f2b442c055e-// assembled-hash: 5d4306ee1d4d91ec+// assembled-hash: 76e79c2465ad522fstruct FrameConstants {u_projection: mat4x4<f32>,u_camera: vec4<f32>,@@ -311,96 +311,113 @@ fn shadowFactor3D_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf2_u003b_i1_u003b_i1_u003fn shadowFactor2D_u0028_f1_u003b_vf2_u003b(channel: ptr<function, f32>, worldPos_1: ptr<function, vec2<f32>>) -> f32 {var clip: vec4<f32>;+ var ndc: vec2<f32>;+ var v: f32;+ var local: f32;var uv: vec2<f32>;var m: vec4<f32>;var hidden: f32;- var local: f32;var local_1: f32;var local_2: f32;+ var local_3: f32;var phi_170_: bool;- let _e90 = (*channel);- let _e91 = (_e90 < 0f);- phi_170_ = _e91;- if !(_e91) {- let _e95 = unnamed_1.u_shadow2DRect[2u];- phi_170_ = (_e95 <= 0f);+ let _e93 = (*channel);+ let _e94 = (_e93 < 0f);+ phi_170_ = _e94;+ if !(_e94) {+ let _e98 = unnamed_1.u_shadow2DRect[2u];+ phi_170_ = (_e98 <= 0f);}- let _e98 = phi_170_;- if _e98 {+ let _e101 = phi_170_;+ if _e101 {return 1f;}- let _e100 = unnamed.u_projection;- let _e101 = (*worldPos_1);- clip = (_e100 * vec4<f32>(_e101.x, _e101.y, 0f, 1f));- let _e107 = clip[3u];- if (_e107 <= 0f) {+ let _e103 = unnamed.u_projection;+ let _e104 = (*worldPos_1);+ clip = (_e103 * vec4<f32>(_e104.x, _e104.y, 0f, 1f));+ let _e110 = clip[3u];+ if (_e110 <= 0f) {return 1f;}- let _e110 = unnamed_1.u_shadow2DRect;let _e112 = clip;let _e115 = clip[3u];- let _e122 = unnamed_1.u_shadow2DRect;- uv = (_e110.xy + ((((_e112.xy / vec2(_e115)) * 0.5f) + vec2(0.5f)) * _e122.zw));- let _e126 = uv;- let _e127 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e126, 0f);- m = _e127;- let _e128 = (*channel);- if (_e128 < 0.5f) {- let _e131 = m[0u];- local = _e131;+ ndc = (((_e112.xy / vec2(_e115)) * 0.5f) + vec2(0.5f));+ let _e123 = unnamed_1.u_shadow2DRect[3u];+ if (_e123 < 0f) {+ let _e126 = ndc[1u];+ local = (1f - _e126);+ } else {+ let _e129 = ndc[1u];+ local = _e129;+ }+ let _e130 = local;+ v = _e130;+ let _e132 = unnamed_1.u_shadow2DRect;+ let _e135 = ndc[0u];+ let _e138 = unnamed_1.u_shadow2DRect[2u];+ let _e140 = v;+ let _e143 = unnamed_1.u_shadow2DRect[3u];+ uv = (_e132.xy + vec2<f32>((_e135 * _e138), (_e140 * abs(_e143))));+ let _e148 = uv;+ let _e149 = textureSampleLevel(u_shadow2D_est, u_shadow2D_ess, _e148, 0f);+ m = _e149;+ let _e150 = (*channel);+ if (_e150 < 0.5f) {+ let _e153 = m[0u];+ local_1 = _e153;} else {- let _e132 = (*channel);- if (_e132 < 1.5f) {- let _e135 = m[1u];- local_1 = _e135;+ let _e154 = (*channel);+ if (_e154 < 1.5f) {+ let _e157 = m[1u];+ local_2 = _e157;} else {- let _e136 = (*channel);- if (_e136 < 2.5f) {- let _e139 = m[2u];- local_2 = _e139;+ let _e158 = (*channel);+ if (_e158 < 2.5f) {+ let _e161 = m[2u];+ local_3 = _e161;} else {- let _e141 = m[3u];- local_2 = _e141;+ let _e163 = m[3u];+ local_3 = _e163;}- let _e142 = local_2;- local_1 = _e142;+ let _e164 = local_3;+ local_2 = _e164;}- let _e143 = local_1;- local = _e143;+ let _e165 = local_2;+ local_1 = _e165;}- let _e144 = local;- hidden = _e144;- let _e145 = hidden;- return (1f - min(_e145, 1f));+ let _e166 = local_1;+ hidden = _e166;+ let _e167 = hidden;+ return (1f - min(_e167, 1f));}fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<f32>>, sh: ptr<function, vec4<f32>>, toLight: ptr<function, vec3<f32>>, dist: ptr<function, f32>) -> f32 {var axis: vec2<f32>;- var local_3: vec2<f32>;- var toFrag: vec2<f32>;var local_4: vec2<f32>;+ var toFrag: vec2<f32>;+ var local_5: vec2<f32>;let _e89 = (*sp);let _e91 = (*sp);if (dot(_e89.xy, _e91.xy) > 0.00000001f) {let _e95 = (*sp);- local_3 = normalize(_e95.xy);+ local_4 = normalize(_e95.xy);} else {- local_3 = vec2<f32>(0f, -1f);+ local_4 = vec2<f32>(0f, -1f);}- let _e98 = local_3;+ let _e98 = local_4;axis = _e98;let _e99 = (*dist);if (_e99 > 0.0001f) {let _e101 = (*toLight);let _e104 = (*dist);- local_4 = (-(_e101.xy) / vec2(_e104));+ local_5 = (-(_e101.xy) / vec2(_e104));} else {let _e107 = axis;- local_4 = _e107;+ local_5 = _e107;}- let _e108 = local_4;+ let _e108 = local_5;toFrag = _e108;let _e110 = (*sp)[3u];let _e112 = (*sp)[2u];@@ -410,18 +427,18 @@ fn spotCone_u0028_vf4_u003b_vf4_u003b_vf3_u003b_f1_u003b(sp: ptr<function, vec4<}fn towardLight_u0028_vf3_u003b_vf3_u003b(toLight_1: ptr<function, vec3<f32>>, N_1: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_5: vec3<f32>;+ var local_6: vec3<f32>;let _e84 = (*toLight_1);let _e85 = (*toLight_1);if (dot(_e84, _e85) > 0.00000001f) {let _e88 = (*toLight_1);- local_5 = normalize(_e88);+ local_6 = normalize(_e88);} else {let _e90 = (*N_1);- local_5 = _e90;+ local_6 = _e90;}- let _e91 = local_5;+ let _e91 = local_6;return _e91;}@@ -525,7 +542,7 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_10: vec3<f32>;var param_11: vec3<f32>;var toLight_3: vec2<f32>;- var local_6: vec2<f32>;+ var local_7: vec2<f32>;var sp_1: vec4<f32>;var toL_1: vec3<f32>;var param_12: vec4<f32>;@@ -564,8 +581,8 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0var param_38: f32;var param_39: vec3<f32>;var param_40: f32;- var phi_768_: bool;- var phi_822_: bool;+ var phi_790_: bool;+ var phi_844_: bool;let _e151 = (*albedo);let _e152 = (*metallic);@@ -633,21 +650,21 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0let _e219 = pd_1;if (dot(_e217.xy, _e219.xy) > 0.00000001f) {let _e223 = pd_1;- local_6 = normalize(-(_e223.xy));+ local_7 = normalize(-(_e223.xy));} else {- local_6 = vec2<f32>(0f, 0f);+ local_7 = vec2<f32>(0f, 0f);}- let _e227 = local_6;+ let _e227 = local_7;toLight_3 = _e227;let _e229 = sh_3[1u];let _e230 = (_e229 > 0f);- phi_768_ = _e230;+ phi_790_ = _e230;if _e230 {let _e231 = toLight_3;let _e232 = toLight_3;- phi_768_ = (dot(_e231, _e232) > 0.5f);+ phi_790_ = (dot(_e231, _e232) > 0.5f);}- let _e236 = phi_768_;+ let _e236 = phi_790_;castShadow = _e236;let _e237 = (*worldPos_3);let _e239 = toLight_3;@@ -692,12 +709,12 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}}let _e272 = castShadow;- phi_822_ = _e272;+ phi_844_ = _e272;if _e272 {let _e274 = col[3u];- phi_822_ = (_e274 > 0f);+ phi_844_ = (_e274 > 0f);}- let _e277 = phi_822_;+ let _e277 = phi_844_;let _e278 = atten;if (_e277 && (_e278 > 0f)) {let _e281 = i;@@ -812,18 +829,18 @@ fn applyLightingPBR_u0028_vf3_u003b_vf3_u003b_vf3_u003b_vf3_u003b_f1_u003b_f1_u0}fn viewDirection_u0028_vf3_u003b(worldPos_4: ptr<function, vec3<f32>>) -> vec3<f32> {- var local_7: vec3<f32>;+ var local_8: vec3<f32>;let _e85 = unnamed.u_camera[3u];if (_e85 > 0.5f) {let _e88 = unnamed.u_camera;let _e90 = (*worldPos_4);- local_7 = normalize((_e88.xyz - _e90));+ local_8 = normalize((_e88.xyz - _e90));} else {let _e94 = unnamed.u_camera;- local_7 = _e94.xyz;+ local_8 = _e94.xyz;}- let _e96 = local_7;+ let _e96 = local_8;return _e96;}diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cppindex ed74eeeb2..d9aed2b10 100644--- a/src/esengine/renderer/frame/RenderFrame.cpp+++ b/src/esengine/renderer/frame/RenderFrame.cpp@@ -266,11 +266,15 @@ void RenderFrame::drawScene() {if (shadow2DActive() && width_ > 0 && height_ > 0) {const f32 w = static_cast<f32>(width_);const f32 h = static_cast<f32>(height_);+ // The height's SIGN carries which way this backend stores the rows it just drew.+ // One shader source serves both backends — the WGSL a material runs is translated+ // from the GLSL — so the convention travels as data rather than a #ifdef.+ const f32 rows = device_.textureOriginTopLeft() ? -1.0f : 1.0f;context_.lights().setShadow2DRect(glm::vec4(static_cast<f32>(std::max(scene_viewport_.x, 0)) / w,static_cast<f32>(std::max(scene_viewport_.y, 0)) / h,static_cast<f32>(scene_viewport_.w) / w,- static_cast<f32>(scene_viewport_.h) / h));+ static_cast<f32>(scene_viewport_.h) / h * rows));} else {context_.lights().setShadow2DRect(glm::vec4(0.0f));}diff --git a/src/esengine/renderer/rhi/GLDevice.hpp b/src/esengine/renderer/rhi/GLDevice.hppindex 33993dc23..13d7aa39f 100644--- a/src/esengine/renderer/rhi/GLDevice.hpp+++ b/src/esengine/renderer/rhi/GLDevice.hpp@@ -85,6 +85,9 @@ public:bool supportsShaderLanguage(GfxShaderLanguage language) const override {return language == GfxShaderLanguage::GLSL_ES300;}++ /// GL reads a framebuffer from the bottom up.+ bool textureOriginTopLeft() const override { return false; }ShaderHandle createProgram(const GfxShaderSource& source,const GfxAttribBinding* bindings, u32 bindingCount,std::string* outLog, GfxShaderStage* outFailedStage) override;diff --git a/src/esengine/renderer/rhi/GfxDevice.hpp b/src/esengine/renderer/rhi/GfxDevice.hppindex 4708e973d..b92f833ad 100644--- a/src/esengine/renderer/rhi/GfxDevice.hpp+++ b/src/esengine/renderer/rhi/GfxDevice.hpp@@ -421,6 +421,16 @@ public:/** @brief Whether this backend compiles @p language (GL: GLSL ES 300; WebGPU: WGSL). */virtual bool supportsShaderLanguage(GfxShaderLanguage language) const = 0;+ /**+ * @brief Whether texel (0,0) of a rendered texture is its TOP-left corner.+ *+ * @details GL stores a framebuffer bottom-up and WebGPU top-down, so one geometry+ * through one matrix lands in opposite rows. A shader sampling a target+ * this frame rendered cannot ask — one source serves both backends — so+ * the answer travels as data.+ */+ virtual bool textureOriginTopLeft() const = 0;+/** @brief Deletes a shader program */virtual void deleteProgram(ShaderHandle program) = 0;diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex 40b71af62..337000dbc 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -148,6 +148,9 @@ public:bool supportsShaderLanguage(GfxShaderLanguage language) const override {return language == GfxShaderLanguage::WGSL;}++ /// WebGPU stores a texture from the top down.+ bool textureOriginTopLeft() const override { return true; }ShaderHandle createProgram(const GfxShaderSource& source,const GfxAttribBinding* bindings, u32 bindingCount,std::string* outLog, GfxShaderStage* outFailedStage) override;diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cppindex 42f544146..56ccf34b2 100644--- a/src/esengine/resource/ShaderParser.cpp+++ b/src/esengine/resource/ShaderParser.cpp@@ -1011,12 +1011,11 @@ fn shadowFactor2D(channel : f32, worldPos : vec2f) -> f32 {if (channel < 0.0 || lc.u_shadow2DRect.z <= 0.0) { return 1.0; }let clip = frame.projection * vec4f(worldPos, 0.0, 1.0);if (clip.w <= 0.0) { return 1.0; }- // The ONE place the twins must not agree: v = 0 samples the bottom of a texture in- // GL and the top here, and the rect arrives in GL's terms — so the camera's corner- // and the point inside it are both turned over.+ // A negative height is the backend saying its texture rows run the other way.let ndc = clip.xy / clip.w * 0.5 + 0.5;- let base = vec2f(lc.u_shadow2DRect.x, 1.0 - lc.u_shadow2DRect.y - lc.u_shadow2DRect.w);- let uv = base + vec2f(ndc.x, 1.0 - ndc.y) * lc.u_shadow2DRect.zw;+ let v = select(ndc.y, 1.0 - ndc.y, lc.u_shadow2DRect.w < 0.0);+ let uv = lc.u_shadow2DRect.xy+ + vec2f(ndc.x * lc.u_shadow2DRect.z, v * abs(lc.u_shadow2DRect.w));let m = textureSampleLevel(t7, s7, uv, 0.0);var hidden = m.a;if (channel < 0.5) { hidden = m.r; }@@ -1708,8 +1707,11 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p// things in — and drawn through this same camera, so the landing agrees." highp vec4 clip = u_projection * vec4(worldPos, 0.0, 1.0);\n"" if (clip.w <= 0.0) return 1.0;\n"+ // A negative height is the backend saying its texture rows run the other way.+ " highp vec2 ndc = clip.xy / clip.w * 0.5 + 0.5;\n"+ " highp float v = u_shadow2DRect.w < 0.0 ? 1.0 - ndc.y : ndc.y;\n"" highp vec2 uv = u_shadow2DRect.xy\n"- " + (clip.xy / clip.w * 0.5 + 0.5) * u_shadow2DRect.zw;\n"+ " + vec2(ndc.x * u_shadow2DRect.z, v * abs(u_shadow2DRect.w));\n"// An explicit level, not an implicit one: this is read inside the light loop,// where a twin's WGSL forbids a sampled derivative — and the mask has no// mip chain for one to choose from anyway.diff --git a/tests/renderer/MockGfxDevice.hpp b/tests/renderer/MockGfxDevice.hppindex 025d1357d..ba5a5245e 100644--- a/tests/renderer/MockGfxDevice.hpp+++ b/tests/renderer/MockGfxDevice.hpp@@ -199,6 +199,10 @@ struct MockGfxDevice final : GfxDevice {bool supportsShaderLanguage(GfxShaderLanguage language) const override {return language == GfxShaderLanguage::GLSL_ES300 || (wgslSupported && language == GfxShaderLanguage::WGSL);}++ /// Answers as GL does; a test that cares sets it.+ bool originTopLeft = false;+ bool textureOriginTopLeft() const override { return originTopLeft; }ShaderHandle createProgram(const GfxShaderSource& source, const GfxAttribBinding*, u32,std::string*, GfxShaderStage* stage) override {++createProgramCalls;diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex 3efed1d63..ece0a6b83 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -565,9 +565,9 @@ export const SCENES = [// 0.64ms of a frame here against 0.057ms with the casters off.{ id: "shadow2d-load", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/shadow2d-load.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/shadow2d-load.textures.json", ESTELLA_VERIFY_W: "1280", ESTELLA_VERIFY_H: "720", ESTELLA_VERIFY_STEPS: "6", ESTELLA_VERIFY_COUNTERS: "{\"render.shadow2d.occluders\":100,\"render.shadow2d.lights\":4}" } },// Five lights that want to cast and a mask with four channels: the fifth goes- // without, which is a lit floor rather than a dark one. WebGL2 only — WebGPU loses- // some shadows once more than one light casts (docs/graphics/lighting: Known gaps).- { id: "shadow2d-light-cap", tier: "pr", env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/shadow2d-light-cap.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/shadow2d-light-cap.textures.json", ESTELLA_VERIFY_W: "200", ESTELLA_VERIFY_H: "1000", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.70,\"y\":0.1,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.3,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.7,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.9,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.5,\"rgb\":[0,120,0],\"tol\":70}]" } },+ // without, which is a lit floor rather than a dark one. The scene is asymmetric on+ // purpose — a symmetric one cannot see a mask sampled upside down.+ { id: "shadow2d-light-cap", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/shadow2d-light-cap.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/shadow2d-light-cap.textures.json", ESTELLA_VERIFY_W: "200", ESTELLA_VERIFY_H: "1000", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.70,\"y\":0.1,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.3,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.7,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.9,\"rgb\":[0,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.5,\"rgb\":[0,120,0],\"tol\":70}]" } },// Two cameras with half the frame each: what a camera draws is bounded by the rect// it asked for, and one covering the other is what taking that rect off the device// after the collect came to.