diff --git a/src/core/CoreNode.ts b/src/core/CoreNode.ts index 43636263..4f113da0 100644 --- a/src/core/CoreNode.ts +++ b/src/core/CoreNode.ts @@ -1346,7 +1346,9 @@ export class CoreNode extends EventEmitter { // Maintain a full scene global transform for bounds detection this.sceneGlobalTransform = Matrix3d.copy( - parent.globalTransform || Matrix3d.identity(Matrix3d.temp), + parent.sceneGlobalTransform || + parent.globalTransform || + Matrix3d.identity(Matrix3d.temp), this.sceneGlobalTransform, ).translateOrMultiply(lt); @@ -2211,7 +2213,7 @@ export class CoreNode extends EventEmitter { } if (inRttCluster === true) { - node.markChildrenWithRTT(this); + node.markChildrenWithRTT(); } children.push(node); @@ -3074,7 +3076,11 @@ export class CoreNode extends EventEmitter { const parentFramebufferDimensions = this.parentFramebufferDimensions; clipY = parentFramebufferDimensions !== null - ? parentFramebufferDimensions.h - this.props.h + ? Math.round( + parentFramebufferDimensions.h - + clipHeight - + this.clippingRect.y, + ) : 0; } diff --git a/src/core/renderers/webgl/WebGlRenderer.rtt.test.ts b/src/core/renderers/webgl/WebGlRenderer.rtt.test.ts index 237da5e8..2eb0f89d 100644 --- a/src/core/renderers/webgl/WebGlRenderer.rtt.test.ts +++ b/src/core/renderers/webgl/WebGlRenderer.rtt.test.ts @@ -277,6 +277,39 @@ describe('RTT — parentHasRenderTexture propagation', () => { expect(child.parentHasRenderTexture).toBe(true); expect(grandchild.parentHasRenderTexture).toBe(true); }); + + it('propagates to descendants of a subtree attached to an RTT node', () => { + const rttNode = new CoreNode(stage, makeDefaultProps()); + rttNode['props'].rtt = true; + + const item = new CoreNode(stage, makeDefaultProps()); + const child = new CoreNode(stage, makeDefaultProps()); + const grandchild = new CoreNode(stage, makeDefaultProps()); + child.parent = item; + grandchild.parent = child; + + item.parent = rttNode; + + expect(item.parentHasRenderTexture).toBe(true); + expect(child.parentHasRenderTexture).toBe(true); + expect(grandchild.parentHasRenderTexture).toBe(true); + }); + + it('propagates to descendants of a subtree attached inside an RTT cluster', () => { + const rttNode = new CoreNode(stage, makeDefaultProps()); + rttNode['props'].rtt = true; + const list = new CoreNode(stage, makeDefaultProps()); + list.parent = rttNode; + + const item = new CoreNode(stage, makeDefaultProps()); + const child = new CoreNode(stage, makeDefaultProps()); + child.parent = item; + + item.parent = list; + + expect(item.parentHasRenderTexture).toBe(true); + expect(child.parentHasRenderTexture).toBe(true); + }); }); // --------------------------------------------------------------------------- @@ -520,3 +553,121 @@ describe('RTT — RTT_NOTIFY_MASK gate in update()', () => { expect(spy).not.toHaveBeenCalled(); }); }); + +// --------------------------------------------------------------------------- +// RTT — sceneGlobalTransform for nested RTT roots +// --------------------------------------------------------------------------- + +describe('RTT — nested sceneGlobalTransform', () => { + const clippingRect = { + x: 0, + y: 0, + w: 1920, + h: 1080, + valid: false, + clipRadius: 0, + }; + + it('children of a nested RTT node keep their real scene position', () => { + const stage = makeStage(); + const outer = new CoreNode(stage, { + ...makeDefaultProps(), + x: 100, + y: 200, + }); + outer['props'].rtt = true; + + const inner = new CoreNode(stage, { ...makeDefaultProps(), x: 10, y: 20 }); + inner.parent = outer; + inner['props'].rtt = true; + + const child = new CoreNode(stage, { ...makeDefaultProps(), x: 1, y: 2 }); + child.parent = inner; + + outer.update(0, clippingRect); + + expect(inner.sceneGlobalTransform?.tx).toBe(110); + expect(inner.sceneGlobalTransform?.ty).toBe(220); + expect(child.sceneGlobalTransform?.tx).toBe(111); + expect(child.sceneGlobalTransform?.ty).toBe(222); + }); +}); + +// --------------------------------------------------------------------------- +// RTT — scissor rect inside a framebuffer +// --------------------------------------------------------------------------- + +describe('RTT — scissor inside a framebuffer', () => { + it('derives the scissor from the clipping rect, not the drawn node height', () => { + const stage = makeMockStage({ + platform: { canvas: { height: 1080 } } as unknown as Stage['platform'], + shManager: { useShader: vi.fn() } as unknown as Stage['shManager'], + }); + const rttNode = new CoreNode(stage, makeDefaultProps()); + rttNode['props'].rtt = true; + rttNode.framebufferDimensions = { w: 400, h: 300 }; + + const node = new CoreNode(stage, { ...makeDefaultProps(), h: 20 }); + node.parent = rttNode; + node['props'].shader = { + program: { bindRenderOp: vi.fn() }, + } as unknown as CoreNodeProps['shader']; + node.clippingRect = { + x: 10, + y: 50, + w: 200, + h: 100, + valid: true, + clipRadius: 0, + }; + + const glw = { + setScissorTest: vi.fn(), + scissor: vi.fn(), + drawElements: vi.fn(), + }; + node.draw({ glw, stage } as unknown as WebGlRenderer); + + expect(glw.scissor).toHaveBeenCalledWith(10, 150, 200, 100); + }); + + it('disables the scissor before clearing an RTT framebuffer', () => { + const stage = makeStage(); + const node = new CoreNode(stage, makeDefaultProps()); + node.hasRTTupdates = true; + node.worldAlpha = 1; + node.renderState = CoreNodeRenderState.InViewport; + node['props'].texture = { + state: 'loaded', + ctxTexture: { framebuffer: {}, w: 100, h: 100 }, + } as unknown as CoreNodeProps['texture']; + + const calls: string[] = []; + const glw = { + canvas: { width: 1920, height: 1080 }, + bindFramebuffer: vi.fn(), + viewport: vi.fn(), + clearColor: vi.fn(), + setScissorTest: vi.fn((enable: boolean) => + calls.push(`scissor:${enable}`), + ), + clear: vi.fn(() => calls.push('clear')), + }; + + const r = Object.create(WebGlRenderer.prototype) as WebGlRenderer; + Object.assign(r, { + glw, + rttNodes: [node], + renderOps: [], + useDirtyRepaints: false, + clearColor: { normalized: [0, 0, 0, 0] }, + _sdfBufferPlain: null, + _sdfBufferRich: null, + render: vi.fn(), + }); + + r.renderRTTNodes(); + + expect(calls).toEqual(['scissor:false', 'clear']); + }); +}); diff --git a/src/core/renderers/webgl/WebGlRenderer.ts b/src/core/renderers/webgl/WebGlRenderer.ts index e95fccf8..6037ec7f 100644 --- a/src/core/renderers/webgl/WebGlRenderer.ts +++ b/src/core/renderers/webgl/WebGlRenderer.ts @@ -1493,6 +1493,8 @@ export class WebGlRenderer extends CoreRenderer { glw.viewport(0, 0, ctxTexture.w, ctxTexture.h); // Set the clear color to transparent glw.clearColor(0, 0, 0, 0); + // gl.clear honours the scissor left over from the previous draw + glw.setScissorTest(false); glw.clear(); if (useDirty === true) { diff --git a/src/core/renderers/webgl/WebGlShaderProgram.test.ts b/src/core/renderers/webgl/WebGlShaderProgram.test.ts index 1f7022a9..477eca7a 100644 --- a/src/core/renderers/webgl/WebGlShaderProgram.test.ts +++ b/src/core/renderers/webgl/WebGlShaderProgram.test.ts @@ -178,6 +178,32 @@ describe('WebGlShaderProgram.bindRenderOp', () => { expect(uniform1f).toHaveBeenCalledWith('u_pixelRatio', 1.0); expect(uniform2f).toHaveBeenCalledWith('u_resolution', 640, 360); }); + + it('binds a nested RTT node with its parent RTT dimensions', () => { + const { program, uniform1f, uniform2f } = createProgram(); + const renderOp = { + framebufferDimensions: { w: 320, h: 180 }, + isCoreNode: true, + parentFramebufferDimensions: { w: 640, h: 360 }, + parentHasRenderTexture: true, + quadBufferCollection: {}, + renderOpTextures: [], + rtt: true, + shader: { + uniforms: { hasStoredUniforms: false }, + }, + stage: { pixelRatio: 2 }, + time: 0, + worldAlpha: 1, + w: 320, + h: 180, + } as any; + + program.bindRenderOp(renderOp); + + expect(uniform1f).toHaveBeenCalledWith('u_pixelRatio', 1.0); + expect(uniform2f).toHaveBeenCalledWith('u_resolution', 640, 360); + }); }); describe('WebGlShaderProgram.bindBufferCollection', () => { diff --git a/src/core/renderers/webgl/WebGlShaderProgram.ts b/src/core/renderers/webgl/WebGlShaderProgram.ts index acfac738..6626affd 100644 --- a/src/core/renderers/webgl/WebGlShaderProgram.ts +++ b/src/core/renderers/webgl/WebGlShaderProgram.ts @@ -319,11 +319,6 @@ export class WebGlShaderProgram implements CoreShaderProgram { ? renderOp.parentFramebufferDimensions : renderOp.framebufferDimensions; - // Skip if the parent and current operation both have render textures - if (renderOp.rtt === true && parentHasRenderTexture === true) { - return; - } - // Resolve target pixel ratio / resolution, then compare-and-set against // the program's shadow state to skip value-identical re-uploads. let pixelRatio: number;