|
| 1 | +import p5 from '../../../src/app.js'; |
| 2 | +import rendererWebGPU from "../../../src/webgpu/p5.RendererWebGPU"; |
| 3 | + |
| 4 | +p5.registerAddon(rendererWebGPU); |
| 5 | + |
| 6 | +suite('WebGPU p5.RendererWebGPU', function() { |
| 7 | + let myp5; |
| 8 | + let prevPixelRatio; |
| 9 | + |
| 10 | + beforeAll(async function() { |
| 11 | + prevPixelRatio = window.devicePixelRatio; |
| 12 | + window.devicePixelRatio = 1; |
| 13 | + myp5 = new p5(function(p) { |
| 14 | + p.setup = function() {}; |
| 15 | + }); |
| 16 | + }); |
| 17 | + |
| 18 | + beforeEach(async function() { |
| 19 | + await myp5.createCanvas(50, 50, 'webgpu'); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(function() { |
| 23 | + myp5.remove(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(function() { |
| 27 | + window.devicePixelRatio = prevPixelRatio; |
| 28 | + }); |
| 29 | + |
| 30 | + suite('Buffer Pooling', function() { |
| 31 | + test('drawing geometry twice reuses vertex buffers', async function() { |
| 32 | + // Create a simple geometry |
| 33 | + const geom = myp5.buildGeometry(() => { |
| 34 | + myp5.triangle(0, 0, 10, 0, 5, 10); |
| 35 | + }); |
| 36 | + |
| 37 | + // Draw the geometry once |
| 38 | + myp5.background(255); |
| 39 | + myp5.model(geom); |
| 40 | + |
| 41 | + // Check the vertex buffer pool size for position attribute |
| 42 | + const poolForVertexBuffer = geom._vertexBufferPools?.vertexBuffer; |
| 43 | + expect(poolForVertexBuffer).to.exist; |
| 44 | + const initialPoolSize = poolForVertexBuffer.length; |
| 45 | + const initialInUseSize = geom._vertexBuffersInUse?.vertexBuffer?.length || 0; |
| 46 | + |
| 47 | + // Draw the geometry again |
| 48 | + myp5.background(255); |
| 49 | + myp5.model(geom); |
| 50 | + |
| 51 | + // Verify the pool hasn't grown - buffers should be reused |
| 52 | + const finalPoolSize = poolForVertexBuffer.length; |
| 53 | + const finalInUseSize = geom._vertexBuffersInUse?.vertexBuffer?.length || 0; |
| 54 | + |
| 55 | + // Pool size should stay the same or be smaller (buffers moved from pool to in-use) |
| 56 | + // The total number of buffers (pool + in-use) should remain constant |
| 57 | + expect(initialPoolSize + initialInUseSize).to.equal(finalPoolSize + finalInUseSize); |
| 58 | + }); |
| 59 | + |
| 60 | + test('freeGeometry causes new buffer allocation on next draw', async function() { |
| 61 | + // Create a simple geometry |
| 62 | + const geom = myp5.buildGeometry(() => { |
| 63 | + myp5.triangle(0, 0, 10, 0, 5, 10); |
| 64 | + }); |
| 65 | + |
| 66 | + // Draw the geometry once |
| 67 | + myp5.background(255); |
| 68 | + myp5.model(geom); |
| 69 | + |
| 70 | + // Get initial buffer count |
| 71 | + const poolForVertexBuffer = geom._vertexBufferPools?.vertexBuffer; |
| 72 | + expect(poolForVertexBuffer).to.exist; |
| 73 | + const initialTotalBuffers = poolForVertexBuffer.length + |
| 74 | + (geom._vertexBuffersInUse?.vertexBuffer?.length || 0); |
| 75 | + |
| 76 | + // Free the geometry |
| 77 | + myp5.freeGeometry(geom); |
| 78 | + |
| 79 | + // Draw the geometry again |
| 80 | + myp5.background(255); |
| 81 | + myp5.model(geom); |
| 82 | + |
| 83 | + // After freeGeometry, new buffers should be allocated |
| 84 | + const finalTotalBuffers = poolForVertexBuffer.length + |
| 85 | + (geom._vertexBuffersInUse?.vertexBuffer?.length || 0); |
| 86 | + |
| 87 | + // We should have more buffers now since freeGeometry marks geometry as dirty |
| 88 | + // and new buffers need to be created |
| 89 | + expect(finalTotalBuffers).to.be.greaterThan(initialTotalBuffers); |
| 90 | + }); |
| 91 | + |
| 92 | + test('immediate mode geometry reuses buffers across frames', async function() { |
| 93 | + // Function to draw the same shape using immediate mode |
| 94 | + const drawSameShape = () => { |
| 95 | + myp5.background(255); |
| 96 | + myp5.beginShape(); |
| 97 | + myp5.vertex(0, 0); |
| 98 | + myp5.vertex(10, 0); |
| 99 | + myp5.vertex(5, 10); |
| 100 | + myp5.endShape(); |
| 101 | + }; |
| 102 | + |
| 103 | + // Draw the shape for the first frame |
| 104 | + drawSameShape(); |
| 105 | + await myp5._renderer.finishDraw(); |
| 106 | + |
| 107 | + // Get the immediate mode geometry (shapeBuilder geometry) |
| 108 | + const immediateGeom = myp5._renderer.shapeBuilder.geometry; |
| 109 | + const poolForVertexBuffer = immediateGeom._vertexBufferPools?.vertexBuffer; |
| 110 | + expect(poolForVertexBuffer).to.exist; |
| 111 | + |
| 112 | + const initialTotalBuffers = poolForVertexBuffer.length + |
| 113 | + (immediateGeom._vertexBuffersInUse?.vertexBuffer?.length || 0); |
| 114 | + |
| 115 | + // Draw the same shape for several more frames |
| 116 | + for (let frame = 0; frame < 5; frame++) { |
| 117 | + drawSameShape(); |
| 118 | + await myp5._renderer.finishDraw(); |
| 119 | + |
| 120 | + // Check that total buffer count hasn't increased |
| 121 | + const currentTotalBuffers = poolForVertexBuffer.length + |
| 122 | + (immediateGeom._vertexBuffersInUse?.vertexBuffer?.length || 0); |
| 123 | + |
| 124 | + expect(currentTotalBuffers).to.equal(initialTotalBuffers, |
| 125 | + `Buffer count should stay constant across frames (frame ${frame})`); |
| 126 | + } |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments