Skip to content

3D engine that AI agents drive

Release incomplete framebuffers and reject empty attachment lists (#2630)

abu-tahir-06cea8c37b456
diff --git a/changes/2630.md b/changes/2630.md
new file mode 100644
index 00000000..6ab234e6
--- /dev/null
+++ b/changes/2630.md
@@ -0,0 +1 @@
+- The real-time path tracer releases an incomplete framebuffer and refuses an empty attachment list with an actionable message (#2630)
diff --git a/src/path-tracer-realtime-gl.js b/src/path-tracer-realtime-gl.js
index 978bd842..cab1ce16 100644
--- a/src/path-tracer-realtime-gl.js
+++ b/src/path-tracer-realtime-gl.js
@@ -51,6 +51,7 @@ export function createFloatTexture(gl, width, height) {
/** A framebuffer from `{ slot, texture }` entries; slots without an entry are not drawn to. */
export function createFramebuffer(gl, attachments) {
+ if (attachments.length === 0) throw new Error('A real-time path tracer framebuffer needs at least one { slot, texture } attachment.');
const framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
const slotCount = Math.max(...attachments.map(entry => entry.slot)) + 1;
@@ -60,7 +61,10 @@ export function createFramebuffer(gl, attachments) {
drawBuffers[slot] = gl.COLOR_ATTACHMENT0 + slot;
}
gl.drawBuffers(drawBuffers);
- if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) throw new Error('The real-time path tracer cannot render to float targets here.');
+ if (gl.checkFramebufferStatus(gl.FRAMEBUFFER) !== gl.FRAMEBUFFER_COMPLETE) {
+ gl.deleteFramebuffer(framebuffer);
+ throw new Error('The real-time path tracer cannot render to float targets here.');
+ }
return framebuffer;
}
diff --git a/tests/path-tracer-realtime-gl-framebuffer.test.js b/tests/path-tracer-realtime-gl-framebuffer.test.js
new file mode 100644
index 00000000..69602da3
--- /dev/null
+++ b/tests/path-tracer-realtime-gl-framebuffer.test.js
@@ -0,0 +1,57 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { createFramebuffer } from '../src/path-tracer-realtime-gl.js';
+
+const GL = { FRAMEBUFFER: 1, COLOR_ATTACHMENT0: 2, TEXTURE_2D: 3, NONE: 4, FRAMEBUFFER_COMPLETE: 5 };
+
+// A stand-in context that counts the framebuffers still alive.
+function countingGl({ complete = true } = {}) {
+ const liveFramebuffers = new Set();
+ let created = 0;
+ const drawBufferCalls = [];
+ return {
+ ...GL,
+ liveFramebuffers,
+ get created() {
+ return created;
+ },
+ drawBufferCalls,
+ createFramebuffer() {
+ created += 1;
+ const framebuffer = {};
+ liveFramebuffers.add(framebuffer);
+ return framebuffer;
+ },
+ bindFramebuffer() {},
+ framebufferTexture2D() {},
+ drawBuffers(buffers) {
+ drawBufferCalls.push(buffers);
+ },
+ checkFramebufferStatus() {
+ return complete ? GL.FRAMEBUFFER_COMPLETE : 0;
+ },
+ deleteFramebuffer(framebuffer) {
+ liveFramebuffers.delete(framebuffer);
+ },
+ };
+}
+
+test('an incomplete framebuffer is released before the error is thrown', () => {
+ const gl = countingGl({ complete: false });
+ assert.throws(() => createFramebuffer(gl, [{ slot: 0, texture: {} }]), /cannot render to float targets/);
+ assert.equal(gl.liveFramebuffers.size, 0);
+});
+
+test('an empty attachment list is refused before a framebuffer is created', () => {
+ const gl = countingGl();
+ assert.throws(() => createFramebuffer(gl, []), /at least one \{ slot, texture \} attachment/);
+ assert.equal(gl.created, 0);
+ assert.equal(gl.liveFramebuffers.size, 0);
+});
+
+test('a complete framebuffer stays alive and draws to the attachment slots', () => {
+ const gl = countingGl();
+ const framebuffer = createFramebuffer(gl, [{ slot: 0, texture: {} }, { slot: 2, texture: {} }]);
+ assert.equal(gl.liveFramebuffers.has(framebuffer), true);
+ assert.deepEqual(gl.drawBufferCalls, [[GL.COLOR_ATTACHMENT0, GL.NONE, GL.COLOR_ATTACHMENT0 + 2]]);
+});