Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Give the tint pass's comparison sampler a depth placeholder while the sun map renders (#709)

Nguyen Xuan Andd1e3ac7fa46
diff --git a/src/renderer.js b/src/renderer.js
index fd21ce59..615ccce5 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -1493,6 +1493,23 @@ export class Renderer67 {
return Boolean(this.tintedShadowsActive && batch.tintedShadow);
}
+ // The shadow program's opaqueShadow sampler is a comparison sampler, so while a depth map renders it needs a comparison-capable
+ // depth texture on its unit that is not the render target: a 2D map left there, or the sun map itself, fails the draw.
+ bindPlaceholderOpaqueShadow() {
+ const context = this.context;
+ context.activeTexture(context.TEXTURE0 + OPAQUE_SHADOW_UNIT);
+ if (!this.placeholderShadowTexture) {
+ this.placeholderShadowTexture = context.createTexture();
+ context.bindTexture(context.TEXTURE_2D, this.placeholderShadowTexture);
+ context.texImage2D(context.TEXTURE_2D, 0, context.DEPTH_COMPONENT24, 1, 1, 0, context.DEPTH_COMPONENT, context.UNSIGNED_INT, null);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.NEAREST);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.NEAREST);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_COMPARE_MODE, context.COMPARE_REF_TO_TEXTURE);
+ }
+ context.bindTexture(context.TEXTURE_2D, this.placeholderShadowTexture);
+ context.activeTexture(context.TEXTURE0);
+ }
+
allocateTintTarget() {
const context = this.context;
this.tintTexture = context.createTexture();
@@ -1594,6 +1611,7 @@ export class Renderer67 {
if (this.tintedShadowsActive) {
context.uniform1f(this.depthAlphaUniforms.tintPass, 0);
context.uniform1i(this.depthAlphaUniforms.opaqueShadow, OPAQUE_SHADOW_UNIT);
+ this.bindPlaceholderOpaqueShadow();
}
context.enable(context.POLYGON_OFFSET_FILL);
context.polygonOffset(1, 1);
@@ -2553,6 +2571,7 @@ export class Renderer67 {
viewTransformShader.dispose?.(this);
if (this.localShadowTexture) this.context.deleteTexture(this.localShadowTexture);
if (this.localShadowFramebuffer) this.context.deleteFramebuffer(this.localShadowFramebuffer);
+ if (this.placeholderShadowTexture) this.context.deleteTexture(this.placeholderShadowTexture);
if (this.tintFramebuffer) {
this.context.deleteTexture(this.tintTexture);
this.context.deleteFramebuffer(this.tintFramebuffer);
diff --git a/tests/tinted-shadows.test.js b/tests/tinted-shadows.test.js
index a63cbdd5..7325e443 100644
--- a/tests/tinted-shadows.test.js
+++ b/tests/tinted-shadows.test.js
@@ -92,3 +92,19 @@ test('the shadow program never samples the sun map it is rendering into (that fe
const unitBeforeBind = calls.findIndex(([name, unit]) => name === 'activeTexture' && unit - 1 === unitSet[2]);
assert.ok(unitBeforeBind >= 0, 'the tint draw binds the sun map on the sampler unit');
});
+
+test('while a depth map renders, the comparison sampler of the shadow program reads a depth placeholder, not a 2D map or the target (WebGL error 1282 on SwiftShader)', () => {
+ const { renderer, calls } = tintRenderer([opaqueBatch, glassBatch]);
+ renderer.shadowTexture = { name: 'sunMap' };
+ renderer.depthAlphaUniforms = { opaqueShadow: 'opaqueShadow' };
+ renderer.depthWindUniforms = {};
+ renderer.drawShadowCasters = Renderer67.prototype.drawShadowCasters;
+ renderer.bindPlaceholderOpaqueShadow = Renderer67.prototype.bindPlaceholderOpaqueShadow;
+ renderer.drawShadowCasters(renderer.lightProjection, 2048, true);
+ const unitSet = calls.find(([name, location]) => name === 'uniform1i' && location === 'opaqueShadow')[2];
+ const placeholderBind = calls.filter(([name]) => name === 'bindTexture').pop();
+ assert.ok(placeholderBind[2] && placeholderBind[2] !== renderer.shadowTexture, 'a placeholder texture, not the sun map');
+ const unitBeforeBind = calls.slice(0, calls.indexOf(placeholderBind)).filter(([name]) => name === 'activeTexture').pop();
+ assert.equal(unitBeforeBind[1] - 1, unitSet, 'bound on the unit the sampler reads');
+ assert.ok(calls.some(([name, , parameter]) => name === 'texParameteri' && parameter === 1), 'the placeholder compares on fetch');
+});