Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Sample the sun shadow with hardware comparison, one fetch per tap (part of #74)

Nguyen Xuan Anfad1b9616f2e
diff --git a/bench/reference/baseline.json b/bench/reference/baseline.json
index f213949b..d5a79061 100644
--- a/bench/reference/baseline.json
+++ b/bench/reference/baseline.json
@@ -43,7 +43,7 @@
"eevee": 0.9
},
"foliage": {
- "cycles": 5.0,
+ "cycles": 5,
"eevee": 0.4
},
"glass": {
@@ -71,7 +71,7 @@
"eevee": 0.7
},
"metal-room": {
- "cycles": 9.0,
+ "cycles": 9,
"eevee": 0.3
},
"metal": {
@@ -91,7 +91,7 @@
"eevee": 0.3
},
"temple-hall": {
- "cycles": 16.0,
+ "cycles": 16,
"eevee": 0.8
}
},
@@ -102,7 +102,7 @@
"scores": {
"brick-flat": {
"gpu-vs-cycles": 0.1267,
- "gpu-vs-eevee": 0.0467,
+ "gpu-vs-eevee": 0.0466,
"cpu-vs-cycles": 0.1264,
"cpu-vs-eevee": 0.0463,
"eevee-vs-cycles": 0.1099,
@@ -149,15 +149,15 @@
"gpu-vs-cpu": 0.0065
},
"dark-shaft": {
- "gpu-vs-cycles": 0.0559,
+ "gpu-vs-cycles": 0.0558,
"gpu-vs-eevee": 0.2639,
"cpu-vs-cycles": 0.0559,
"cpu-vs-eevee": 0.2637,
"eevee-vs-cycles": 0.2654,
- "gpu-vs-cpu": 0.0031,
+ "gpu-vs-cpu": 0.003,
"region-pixels": 0.0989,
- "region-gpu-vs-cycles": 0.1285,
- "region-gpu-vs-eevee": 0.2563,
+ "region-gpu-vs-cycles": 0.1284,
+ "region-gpu-vs-eevee": 0.2562,
"region-eevee-vs-cycles": 0.2645
},
"fog": {
@@ -166,7 +166,7 @@
"cpu-vs-cycles": 0.2205,
"cpu-vs-eevee": 0.1095,
"eevee-vs-cycles": 0.2662,
- "gpu-vs-cpu": 0.0087
+ "gpu-vs-cpu": 0.0086
},
"fog-room": {
"gpu-vs-cycles": 0.1036,
@@ -174,19 +174,19 @@
"cpu-vs-cycles": 0.1036,
"cpu-vs-eevee": 0.3631,
"eevee-vs-cycles": 0.373,
- "gpu-vs-cpu": 0.0066,
+ "gpu-vs-cpu": 0.0065,
"region-pixels": 0.2428,
"region-gpu-vs-cycles": 0.1615,
"region-gpu-vs-eevee": 0.3273,
"region-eevee-vs-cycles": 0.3147
},
"fog-valley": {
- "gpu-vs-cycles": 0.1857,
- "gpu-vs-eevee": 0.1443,
- "cpu-vs-cycles": 0.1859,
- "cpu-vs-eevee": 0.1447,
+ "gpu-vs-cycles": 0.1856,
+ "gpu-vs-eevee": 0.1444,
+ "cpu-vs-cycles": 0.1858,
+ "cpu-vs-eevee": 0.1448,
"eevee-vs-cycles": 0.1952,
- "gpu-vs-cpu": 0.0119
+ "gpu-vs-cpu": 0.0116
},
"foliage": {
"gpu-vs-cycles": 0.099,
@@ -202,7 +202,7 @@
"cpu-vs-cycles": 0.1035,
"cpu-vs-eevee": 0.1001,
"eevee-vs-cycles": 0.1008,
- "gpu-vs-cpu": 0.0083
+ "gpu-vs-cpu": 0.0082
},
"light-linking": {
"gpu-vs-cycles": 0.0998,
@@ -245,7 +245,7 @@
"gpu-vs-cpu": 0.0276
},
"metal": {
- "gpu-vs-cycles": 0.1037,
+ "gpu-vs-cycles": 0.1036,
"gpu-vs-eevee": 0.0781,
"cpu-vs-cycles": 0.1033,
"cpu-vs-eevee": 0.0782,
@@ -270,7 +270,7 @@
},
"outdoor-hdri": {
"gpu-vs-cycles": 0.0451,
- "gpu-vs-eevee": 0.0281,
+ "gpu-vs-eevee": 0.0282,
"cpu-vs-cycles": 0.0445,
"cpu-vs-eevee": 0.0279,
"eevee-vs-cycles": 0.0403,
@@ -290,7 +290,7 @@
"cpu-vs-cycles": 0.064,
"cpu-vs-eevee": 0.2318,
"eevee-vs-cycles": 0.2341,
- "gpu-vs-cpu": 0.0022,
+ "gpu-vs-cpu": 0.0021,
"region-pixels": 0.0961,
"region-gpu-vs-cycles": 0.2362,
"region-gpu-vs-eevee": 0.2844,
diff --git a/bench/runtime/PROFILE.md b/bench/runtime/PROFILE.md
index 40670174..7acd1894 100644
--- a/bench/runtime/PROFILE.md
+++ b/bench/runtime/PROFILE.md
@@ -277,3 +277,26 @@ The last row is the finding: in the busy state a constant fragment shader costs
Inferred, not isolated: SwiftShader still pays for a branch that pixels in the same quad do not all take, so the first two behave as the earlier `fillIntensity` skip did. `textureGather` would cut 36 fetches to 9 but is not in WebGL 2 (GLSL ES 3.00).
**What to try next.** Measure the two full-screen passes against the vertex stage on `large-mesh` and `many-materials` with a profile that does not depend on the machine state (pin the cores, or count instructions), since the lit shader is no longer the proven bottleneck. Draw the environment background only where no mesh covers the pixel was already tried on 2026-11-01 and moved nothing.
+
+## Sun shadow as one comparison fetch per tap (2026-11-03)
+
+Measured on `perf/blended-shadow-taps` at 960 by 540, SwiftShader, `--cpu-only --frames 6`, Chrome headless. Each row alternates the old build, the new build and three.js in one window for 3 rounds, so machine load cancels; the machine ran at about 12 percent CPU from other sessions between runs. Frame in ms, then the median ratio to three.js (all three medians in the same window).
+
+The shadow depth texture now has `TEXTURE_COMPARE_MODE` and linear filtering, and the shader samples it as `sampler2DShadow`. One fetch returns the 2 by 2 percentage-closer result that four `texelFetch` calls and a `step` each used to build by hand, so the 9-tap filter is 9 fetches, not 36. Edges clamp the same way.
+
+| Scene | Before (ms) | After (ms) | three.js (ms) | Ratio before to after |
+| --- | --- | --- | --- | --- |
+| `transparency` | 424 to 431 | 364 to 367 | 51 to 52 | 8.4 to 7.1 |
+| `skinned-characters` | 186 to 192 | 159 to 165 | 25 to 26 | 7.6 to 6.6 |
+| `instanced-objects` | 171 to 177 | 100 to 148 | 31 to 32 | 5.5 to 4.0 |
+| `moving-shadows` | 70 to 72 | 65 | 19 | 3.8 to 3.5 |
+| `many-materials` | 561 to 570 | 478 to 490 | 173 to 175 | 3.25 to 2.79 |
+| `many-lights` | 439 to 445 | 392 to 397 | 150 to 153 | 2.89 to 2.59 |
+| `large-mesh` | 550 to 554 | 475 to 481 | 196 to 205 | 2.79 to 2.44 |
+| `large-outdoor` | 239 to 258 | 229 to 243 | 787 to 804 | 0.31 to 0.29 |
+
+`instanced-objects` has the widest spread after the change (100 to 148), so its ratio is a range of about 3.2 to 4.7. The 8 scenes keep their pictures: `npm run reference -- --skip-blender` scores all 23 reference scenes within 0.0001 of the stored scores against Cycles and EEVEE (largest move -0.0001), on both the GPU and SwiftShader renders. Nine rows of `baseline.json` changed in the fourth digit and were updated.
+
+Not kept: 4 taps in place of 9 (two by two, spaced 3.5 texels) took `transparency` from 366 to 351 ms, 4 percent, for a smaller and different filter. The remaining distance on `transparency` (7.1 times) and `skinned-characters` (6.6 times) is not the shadow filter any more; they stay above the issue 74 target of 3 times.
+
+The fog volume shares the shadow texture and now reads it through the comparison sampler, so its single tap is filtered over 2 by 2 texels where it was nearest. The main program also sets every 2D sampler to unit 1 when it links, because an unset sampler reads unit 0 and one unit may not hold two sampler types.
diff --git a/changes/74-shadow-taps.md b/changes/74-shadow-taps.md
new file mode 100644
index 00000000..dc298ef2
--- /dev/null
+++ b/changes/74-shadow-taps.md
@@ -0,0 +1 @@
+- Make the sun shadow filter one hardware comparison fetch per tap instead of four texel fetches (part of issue #74). The shadow depth texture now compares on fetch with linear filtering (`TEXTURE_COMPARE_MODE`), so the 9-tap filter costs 9 fetches, not 36, with the same result (every reference scene is within 0.0001 of Cycles before and after). On SwiftShader, alternated with three.js, the frame fell from 8.4 to 7.1 times three.js on `transparency`, 7.6 to 6.6 on `skinned-characters`, 5.5 to 4.0 on `instanced-objects` and 3.8 to 3.5 on `moving-shadows`. The fog volume reads the shadow through the same sampler, so its single tap is now filtered over 2 by 2 texels. Cutting the filter to 4 taps saved only 4 percent more and was not kept.
diff --git a/src/renderer.js b/src/renderer.js
index 02e3a896..7d527f85 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -624,15 +624,12 @@ export class Renderer67 {
// two sampler types, so every 2D sampler starts on unit 1 until a draw sets it.
keepSamplerTypesApart() {
const context = this.context;
- const previousProgram = context.getParameter(context.CURRENT_PROGRAM);
context.useProgram(this.program);
const count = context.getProgramParameter(this.program, context.ACTIVE_UNIFORMS);
for (let index = 0; index < count; index++) {
const uniform = context.getActiveUniform(this.program, index);
- if (uniform.type !== context.SAMPLER_2D) continue;
- context.uniform1iv(context.getUniformLocation(this.program, uniform.name), new Int32Array(uniform.size).fill(1));
+ if (uniform.type === context.SAMPLER_2D) context.uniform1iv(context.getUniformLocation(this.program, uniform.name), new Int32Array(uniform.size).fill(1));
}
- context.useProgram(previousProgram);
}
get samples() {
@@ -1237,11 +1234,10 @@ export class Renderer67 {
context.activeTexture(context.TEXTURE0);
context.bindTexture(context.TEXTURE_2D, this.shadowTexture);
if (allocateShadow) context.texImage2D(context.TEXTURE_2D, 0, context.DEPTH_COMPONENT24, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, 0, context.DEPTH_COMPONENT, context.UNSIGNED_INT, null);
- // Compare on fetch with linear filtering gives a 2 x 2 percentage-closer result from one fetch.
+ // Compare on fetch (the default function is LEQUAL) with linear filtering gives a 2 x 2 percentage-closer result from one fetch.
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.LINEAR);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.LINEAR);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_COMPARE_MODE, context.COMPARE_REF_TO_TEXTURE);
- context.texParameteri(context.TEXTURE_2D, context.TEXTURE_COMPARE_FUNC, context.LEQUAL);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
if (!this.shadowFramebuffer) this.shadowFramebuffer = context.createFramebuffer();
diff --git a/tests/shadow-hardware-compare.test.js b/tests/shadow-hardware-compare.test.js
new file mode 100644
index 00000000..a2d43490
--- /dev/null
+++ b/tests/shadow-hardware-compare.test.js
@@ -0,0 +1,17 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { createFragmentShader, fragmentShader } from '../src/shaders.js';
+import { createPostFragment } from '../src/post.js';
+
+test('the sun shadow is one hardware comparison fetch for each filter tap', () => {
+ assert.match(fragmentShader, /uniform highp sampler2DShadow shadowMap;/);
+ assert.match(createFragmentShader([]), /uniform highp sampler2DShadow shadowMap;/);
+ assert.equal(fragmentShader.includes('texelFetch(shadowMap'), false);
+ assert.equal(fragmentShader.split('texture(shadowMap').length - 1, 1);
+});
+
+test('the fog volume reads the same shadow texture through a comparison sampler', () => {
+ const post = createPostFragment();
+ assert.match(post, /uniform highp sampler2DShadow volumeShadowMap;/);
+ assert.match(post, /texture\(volumeShadowMap, vec3\(/);
+});