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.

fix(render): WebGPU post-processing keeps the picture the right way up

esenginebf0766f5116f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e959d6d16..755e79165 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ published separately; it ships inside the editor.
### Fixed
+- WebGPU post-processing keeps the picture the right way up. Every fullscreen pass sampled its input upside down, so a frame came out inverted whenever its chain had an odd number of them — a linear project with no post-process effect, for one — and an effect that reads depth, such as SSAO or distance fog, laid its result over the colour mirrored.
- Ambient occlusion works behind another post-process effect. The scene's image went back to the pool after the last pass that read its colour, taking its depth with it, so an SSAO placed after a colour grade — as ORCA Bistro's volume has it — found no depth and occluded nothing, at any radius or intensity. The scene now lives until the chain's last pass.
- A shipped game's textures are sampled through mip levels. An image the engine uploads itself — in a web export, a mini-game and the editor's Play — got no mip chain on WebGL2, so a texture drawn smaller than itself took one texel per pixel: in ORCA Bistro the trees turned to noise and the shutters to broken bright lines, while the editor's viewport, which uploads through its own path, looked right. The chain is now built from the pixels and rebuilt whenever they change, as that path does. WebGPU does not build one yet.
diff --git a/sdk/src/postprocess/postProcessEffects.ts b/sdk/src/postprocess/postProcessEffects.ts
index 677062ded..93b409cab 100644
--- a/sdk/src/postprocess/postProcessEffects.ts
+++ b/sdk/src/postprocess/postProcessEffects.ts
@@ -21,9 +21,8 @@ import { Material } from '../render/material';
* definition each: a fix that reached only the first of three copies compiled
* fine and drew a black screen.
*/
-const SSAO_GLSL = `/// A SCREEN texel: +y up, origin bottom-left, and the same number on both
-/// backends. Its inverse round-trips through a uv, which is backend-independent
-/// because one vertex stage serves both.
+const SSAO_GLSL = `/// A texel of the pass's uv, which runs in the device's own row order: up the
+/// screen here, down it on WebGPU. Its inverse round-trips through that uv.
ivec2 screenTexel(vec2 uv, ivec2 size) {
return clamp(ivec2(uv * vec2(size)), ivec2(0), size - ivec2(1));
}
@@ -56,11 +55,10 @@ const SSAO_WGSL = `fn screenTexel(uv : vec2f, size : vec2i) -> vec2i {
fn uvOfTexel(t : vec2i, size : vec2i) -> vec2f {
return (vec2f(t) + 0.5) / vec2f(size);
}
-// The one place the row order matters: row 0 is the TOP here and the BOTTOM on
-// GL, so a screen texel is the mirrored row.
+// A fullscreen pass's uv already runs down the rows here, so a texel is its row.
fn depthAt(t : vec2i, size : vec2i) -> f32 {
let q = clamp(t, vec2i(0), size - vec2i(1));
- return textureLoad(t7, vec2i(q.x, size.y - 1 - q.y), 0);
+ return textureLoad(t7, q, 0);
}
fn worldAt(t : vec2i, size : vec2i) -> vec3f {
let q = clamp(t, vec2i(0), size - vec2i(1));
@@ -896,7 +894,7 @@ void main() {
* SSAO, pass 1 of 4: the occlusion term, at half resolution. No G-buffer —
* scene depth and the injected `worldFromDepth` are the whole input, and
* positions come back in WORLD space, so the radius is a world length.
- * Coordinates are SCREEN texels (+y up); only `depthAt` knows the row order.
+ * Coordinates are the pass uv's texels, which run in each device's own row order.
*/
createSsaoAo(): ShaderHandle {
const source = `#pragma shader "PP SSAO"
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 324a0b023..83d4e925e 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -372,7 +372,9 @@ std::string wgslPPVSOut() {
"};\n";
}
-// WGSL twin of canonicalPPVertexStage.
+// WGSL twin of canonicalPPVertexStage. The uv comes out in this device's texture
+// space, row 0 at the top: the screen's y-up uv would sample a target this device
+// stored top-down upside down, and every fullscreen pass would turn the image over.
std::string canonicalPPVertexStageWGSL() {
std::string src = wgslPPVSOut();
src +=
@@ -385,7 +387,7 @@ std::string canonicalPPVertexStageWGSL() {
"@vertex fn vs_main(v : VSIn) -> VSOut {\n"
" var out : VSOut;\n"
" out.pos = vec4f(v.a_position, 0.0, 1.0);\n"
- " out.v_texCoord = v.a_texCoord;\n"
+ " out.v_texCoord = vec2f(v.a_texCoord.x, 1.0 - v.a_texCoord.y);\n"
" return out;\n"
"}\n";
return src;
@@ -938,9 +940,9 @@ const char* kFrameHeaderWGSL =
" if (frame.camera.w > 0.5) { return normalize(frame.camera.xyz - worldPos); }\n"
" return frame.camera.xyz;\n"
"}\n"
- // The z line is the twin's whole difference: this device clips z to [0, 1].
+ // A fullscreen pass's uv runs down the screen here, and this device clips z to [0, 1].
"fn worldFromDepth(uv : vec2f, depth : f32) -> vec3f {\n"
- " let clip = vec4f(uv * 2.0 - 1.0, depth, 1.0);\n"
+ " let clip = vec4f(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, depth, 1.0);\n"
" let world = frame.inverseViewProjection * clip;\n"
" return world.xyz / world.w;\n"
"}\n";
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 20140692a..4b4abfb9a 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -1262,6 +1262,8 @@ export const NOTED = {
{ note: 'A cutout\'s mip levels keep its coverage' },
'feat(import): images several materials share get every setting they ask for':
{ note: 'A cutout\'s mip levels keep its coverage' },
+ 'fix(render): WebGPU post-processing keeps the picture the right way up':
+ { note: 'WebGPU post-processing keeps the picture the right way up' },
'fix(render): ambient occlusion works behind another effect':
{ note: 'Ambient occlusion works behind another post-process effect' },
'fix(render): a shipped game\'s textures are mipmapped':
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index be56a4611..51c575b72 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -682,9 +682,9 @@ export const SCENES = [
{ id: "aa-edge-post", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/aa-edge-post.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.2353,\"y\":0.502,\"rgb\":[128,128,128],\"tol\":64},{\"x\":0.2392,\"y\":0.502,\"rgb\":[128,128,128],\"tol\":64},{\"x\":0.749,\"y\":0.502,\"rgb\":[128,128,128],\"tol\":64},{\"x\":0.7529,\"y\":0.502,\"rgb\":[128,128,128],\"tol\":64},{\"x\":0.502,\"y\":0.502,\"rgb\":[255,255,255],\"tol\":6},{\"x\":0.0784,\"y\":0.0784,\"rgb\":[0,0,0],\"tol\":6}]" } },
{ id: "ssao", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/ssao.esscene", ESTELLA_VERIFY_W: "320", ESTELLA_VERIFY_H: "240", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5016,\"y\":0.8996,\"rgb\":[191,191,191],\"tol\":6},{\"x\":0.3762,\"y\":0.4937,\"rgb\":[144,144,144],\"tol\":20},{\"x\":0.627,\"y\":0.4812,\"rgb\":[172,172,172],\"tol\":9},{\"x\":0.8527,\"y\":0.4686,\"rgb\":[147,147,147],\"tol\":9},{\"x\":0.1881,\"y\":0.5272,\"rgb\":[123,141,113],\"tol\":22},{\"x\":0.1285,\"y\":0.1967,\"rgb\":[133,136,148],\"tol\":9},{\"x\":0.627,\"y\":0.2008,\"rgb\":[179,184,199],\"tol\":5}]" } },
// The same occlusion with a neutral colour grade ahead of it in the chain: a pass
- // behind another one still samples the scene's depth. WebGL2 only: on WebGPU each
- // fullscreen pass turns the colour upside down while the occlusion stays upright.
- { id: "ssao-after-grade", tier: "pr", webgpu: false, env: { ESTELLA_VERIFY_SCENE: "/scenes/ssao-after-grade.esscene", ESTELLA_VERIFY_W: "320", ESTELLA_VERIFY_H: "240", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5016,\"y\":0.8996,\"rgb\":[191,191,191],\"tol\":6},{\"x\":0.3762,\"y\":0.4937,\"rgb\":[144,144,144],\"tol\":20},{\"x\":0.627,\"y\":0.4812,\"rgb\":[172,172,172],\"tol\":9},{\"x\":0.8527,\"y\":0.4686,\"rgb\":[147,147,147],\"tol\":9},{\"x\":0.1881,\"y\":0.5272,\"rgb\":[123,141,113],\"tol\":22},{\"x\":0.1285,\"y\":0.1967,\"rgb\":[133,136,148],\"tol\":9},{\"x\":0.627,\"y\":0.2008,\"rgb\":[179,184,199],\"tol\":5}]" } },
+ // behind another one still samples the scene's depth, and on WebGPU the colour it
+ // lays the occlusion over is the right way up.
+ { id: "ssao-after-grade", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/ssao-after-grade.esscene", ESTELLA_VERIFY_W: "320", ESTELLA_VERIFY_H: "240", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5016,\"y\":0.8996,\"rgb\":[191,191,191],\"tol\":6},{\"x\":0.3762,\"y\":0.4937,\"rgb\":[144,144,144],\"tol\":20},{\"x\":0.627,\"y\":0.4812,\"rgb\":[172,172,172],\"tol\":9},{\"x\":0.8527,\"y\":0.4686,\"rgb\":[147,147,147],\"tol\":9},{\"x\":0.1881,\"y\":0.5272,\"rgb\":[123,141,113],\"tol\":22},{\"x\":0.1285,\"y\":0.1967,\"rgb\":[133,136,148],\"tol\":9},{\"x\":0.627,\"y\":0.2008,\"rgb\":[179,184,199],\"tol\":5}]" } },
{ id: "depth-outline-flat", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/depth-outline-flat.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[230,102,26],\"tol\":20}]" } },
{ id: "mesh-opaque", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-depth.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-depth.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.42,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.58,\"y\":0.5,\"rgb\":[255,0,0],\"tol\":40}]" } },
{ id: "mesh-opaque-off", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SET_FIELD: "{\"entity\":2,\"component\":\"MeshRenderer\",\"key\":\"opaque\",\"value\":false}", ESTELLA_VERIFY_SCENE: "/scenes/mesh-depth.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh-depth.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.42,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.58,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40}]" } },