scene: strips in one plain draw, not instanced
On SwiftShader the instanced draw (one instance per strip, four vertices) costs about 1.9 s per frame at 320x180 against 11.6 ms for the old march, nearly all of it per-instance overhead: the same strips as six vertices decoded from gl_VertexID in one drawArrays take about 11 ms. The vertex shader now reads strip and corner from gl_VertexID; no buffers, no instancing, same quads, same painter's order. VERTICES_PER_STRIP and LOOKUPS_PER_STRIP follow the new corner layout. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
50b5155b0e
commit
8732cd549d
5 changed files with 43 additions and 39 deletions
|
|
@ -79,7 +79,7 @@ not evaluated.
|
|||
`voxel/Voxel.ts` `createVoxelScene` takes the same options and returns the same `{ login, dispose }`
|
||||
as `createScene`, so the door swaps one for the other. It is the cheap background: WebGL2 core
|
||||
only (no extension is requested, no float target is allocated). It draws the terrain painter's order,
|
||||
as instanced vertical strips, instead of marching a ray per pixel.
|
||||
as vertical strips, instead of marching a ray per pixel.
|
||||
|
||||
- A wrapping ring of 256 x 256 world cells (8 units each) around the camera, in an RG8 texture
|
||||
holding a 16-bit height per cell. Cell (x, z) lives at texel (x mod 256, z mod 256). When the
|
||||
|
|
@ -88,16 +88,17 @@ as instanced vertical strips, instead of marching a ray per pixel.
|
|||
Phacelle per texel, the ray-march scene's terrain without its plateau mask. A cell's height is a
|
||||
pure function of the cell, so the order of writes cannot change the ring. A move of a whole ring
|
||||
or more, or the first frame, rewrites all of it.
|
||||
- `shaders/voxelStrips.vert.glsl` draws the terrain: one instance is a screen column (one buffer
|
||||
pixel wide) at one depth step, and the host draws the steps far to near with no depth buffer
|
||||
and no blending, so a nearer strip overwrites what it hides. A strip's vertices fetch its height
|
||||
- `shaders/voxelStrips.vert.glsl` draws the terrain: a strip is a screen column (one buffer
|
||||
pixel wide) at one depth step, six vertices decoded from `gl_VertexID` (no buffers) in one
|
||||
`drawArrays`. The host draws the steps far to near with no depth buffer and no blending, so a nearer strip overwrites what it hides. A strip's vertices fetch its height
|
||||
and its nearer neighbour's from the ring (vertex texture fetch, core WebGL2) and project them
|
||||
with the shear `y = horizon + (h - camY) / (z * tanHalf.y)`; the quad runs from its own top
|
||||
down to the nearer neighbour's top (the screen bottom for the first strip drawn), and collapses
|
||||
when the neighbour is higher. Colour is chosen once per strip, by height, slope along the view
|
||||
down to the nearer neighbour's top (the screen bottom for the first strip drawn); where the
|
||||
neighbour is higher the quad is upside down but lies under that nearer top, which the nearer
|
||||
strips drawn after it cover. Colour is chosen once per strip, by height, slope along the view
|
||||
ray and fog, and leaves as a flat varying (the last vertex of each strip triangle is a top
|
||||
vertex); `shaders/voxel.frag.glsl` writes it with a dither. Depths follow `voxelWorld.ts`
|
||||
`stepDepth` (about a cell up close, 2% of the depth far away; the same closed form in GLSL and
|
||||
`stepDepth` (about a cell up close, 3% of the depth far away; the same closed form in GLSL and
|
||||
TypeScript), strips nearer than any terrain could reach the screen are not drawn, and strips
|
||||
past a column's far plane (120 cells) are clipped in the vertex shader.
|
||||
- `shaders/voxelBackdrop.frag.glsl` is the sky behind the strips, one fullscreen triangle.
|
||||
|
|
@ -112,7 +113,7 @@ as instanced vertical strips, instead of marching a ray per pixel.
|
|||
- `rails.ts` is the camera: a straight rail with the yaw wandering either side and sway, constant
|
||||
altitude above the terrain; `login()` lifts and tips it down.
|
||||
- Measurement without extensions: `onFrameStats` gives the cadence, the buffer size, the vertices
|
||||
submitted and the ring lookups the frame costs (two per shaded strip vertex, counted on the host
|
||||
submitted and the ring lookups the frame costs (eight per shaded strip, counted on the host
|
||||
from the same schedule the shader runs). Setting `onTraceStats` makes one frame in eight time
|
||||
the draw with a one-texel synchronous read, then draw the strips again off screen, each fragment
|
||||
adding one count, and read that back: lookups per pixel, and fragment overdraw per pixel.
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// The terrain, painter's order. One instance is one strip: a screen column (one pixel wide) at one
|
||||
// The terrain, painter's order. Six vertices are one strip: a screen column (one pixel wide) at one
|
||||
// depth step. The host draws steps far to near with no depth test, so a nearer strip overwrites
|
||||
// what it hides, which is the classic voxel renderer's occlusion without a running horizon.
|
||||
//
|
||||
|
|
@ -10,11 +10,11 @@
|
|||
// read both heights to place itself. No pixel below a strip's top is left unpainted either:
|
||||
// the nearer strips cover the rest.
|
||||
//
|
||||
// Four vertices (a triangle strip): 0,1 are the bottom edge, 2,3 the top edge, 0,2 the left side.
|
||||
// A bottom vertex reads the nearer neighbour's height; a top vertex its own. The colour is
|
||||
// computed on the top vertices (which also read the neighbour's, for the slope) and leaves as a
|
||||
// flat varying; the provoking vertex of a GL ES triangle is its last, which for both triangles of
|
||||
// the strip is a top vertex.
|
||||
// One plain drawArrays of triangles, no vertex buffers, no instancing: gl_VertexID alone says which
|
||||
// strip and which corner. The six vertices are BL BR TL | TL BR TR. A bottom vertex reads the nearer
|
||||
// neighbour's height; a top vertex its own. The colour is computed on the two corners that are the
|
||||
// last vertex of a triangle (TL and TR, which also read the neighbour's height, for the slope) and
|
||||
// leaves as a flat varying: the provoking vertex of a GL ES triangle is its last.
|
||||
uniform sampler2D u_height; // RG8, the ring of 16-bit heights
|
||||
uniform sampler2D u_sky; // baked sky, gamma-encoded
|
||||
// u_camPos, u_tanHalf (x: tan(fov/2) * aspect, y: tan(fov/2)) and the palette come from the shared block.
|
||||
|
|
@ -44,9 +44,11 @@ float stepDepth(int k)
|
|||
|
||||
void main()
|
||||
{
|
||||
int strip = gl_InstanceID;
|
||||
bool top = gl_VertexID >= 2;
|
||||
float side = float(gl_VertexID & 1) * 2.0 - 1.0;
|
||||
int strip = gl_VertexID / 6;
|
||||
int corner = gl_VertexID - strip * 6;
|
||||
bool top = corner == 2 || corner == 3 || corner == 5;
|
||||
bool carriesColour = corner == 2 || corner == 5;
|
||||
float side = (corner == 1 || corner == 4 || corner == 5) ? 1.0 : -1.0;
|
||||
int column = strip % u_columns;
|
||||
int k = VOX_STEPS - 1 - strip / u_columns; // far to near
|
||||
|
||||
|
|
@ -79,6 +81,8 @@ void main()
|
|||
}
|
||||
float h = cellHeight(ivec2(floor(base + dcell * z)));
|
||||
gl_Position.y = clamp(u_horizon + (h - u_camPos.y) / (z * u_tanHalf.y), -1.0, 1.0);
|
||||
if (!carriesColour)
|
||||
return;
|
||||
// Colour by height and slope along the view ray, then fog toward the sky's horizon colour.
|
||||
float hp = cellHeight(ivec2(floor(base + dcell * zp)));
|
||||
float slope = (h - hp) / max((z - zp) * dl, 1e-3);
|
||||
|
|
|
|||
|
|
@ -97,8 +97,8 @@ describe('voxel ring generation', () => {
|
|||
it('the first frame bakes the sky, fills the whole ring once, and draws backdrop then strips', () => {
|
||||
const { calls } = build();
|
||||
runFrame(1000);
|
||||
expect(calls.drawArrays).toHaveBeenCalledTimes(3); // sky bake, ring fill, backdrop
|
||||
expect(calls.drawArraysInstanced).toHaveBeenCalledTimes(1);
|
||||
expect(calls.drawArrays).toHaveBeenCalledTimes(4); // sky bake, ring fill, backdrop, strips
|
||||
expect(calls.drawArraysInstanced).toBeUndefined(); // one plain draw
|
||||
expect(calls.scissor.mock.calls).toEqual([[0, 0, N, N]]);
|
||||
});
|
||||
|
||||
|
|
@ -106,8 +106,7 @@ describe('voxel ring generation', () => {
|
|||
const { calls } = build();
|
||||
runFrame(1000);
|
||||
runFrame(1034);
|
||||
expect(calls.drawArrays).toHaveBeenCalledTimes(4); // one more backdrop
|
||||
expect(calls.drawArraysInstanced).toHaveBeenCalledTimes(2);
|
||||
expect(calls.drawArrays).toHaveBeenCalledTimes(6); // two more: backdrop, strips
|
||||
expect(calls.scissor).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
|
|
@ -134,16 +133,15 @@ describe('voxel ring generation', () => {
|
|||
});
|
||||
|
||||
describe('voxel draw pass', () => {
|
||||
it('draws one instance per column and step, four vertices each, with no depth buffer', () => {
|
||||
it('draws one strip per column and step, six vertices each, in one plain draw with no depth buffer', () => {
|
||||
const { calls, canvas } = build();
|
||||
runFrame(1000);
|
||||
const [mode, first, count, instances] = calls.drawArraysInstanced.mock.calls[0];
|
||||
expect(count).toBe(4);
|
||||
const [, first, count] = calls.drawArrays.mock.calls.at(-1)!;
|
||||
expect(first).toBe(0);
|
||||
expect(mode).toBeDefined();
|
||||
expect(instances % canvas.width).toBe(0); // whole columns
|
||||
expect(instances / canvas.width).toBeLessThanOrEqual(STEPS);
|
||||
expect(instances / canvas.width).toBeGreaterThan(STEPS / 2); // the near steps below the screen are skipped, not most of them
|
||||
const strips = count / 6;
|
||||
expect(strips % canvas.width).toBe(0); // whole columns
|
||||
expect(strips / canvas.width).toBeLessThanOrEqual(STEPS);
|
||||
expect(strips / canvas.width).toBeGreaterThan(STEPS / 2); // the near steps below the screen are skipped, not most of them
|
||||
expect(canvas.getContext.mock.calls[0][1].depth).toBe(false); // no depth buffer: order is the draw order
|
||||
});
|
||||
|
||||
|
|
@ -239,8 +237,7 @@ describe('the door picks the style', () => {
|
|||
);
|
||||
expect(scene).not.toBeNull();
|
||||
runFrame(1000);
|
||||
expect(calls.drawArrays).toHaveBeenCalledTimes(3);
|
||||
expect(calls.drawArraysInstanced).toHaveBeenCalledTimes(1);
|
||||
expect(calls.drawArrays).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
|
||||
it("'raymarch' keeps the sphere-traced scene", async () => {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// The voxel terrain scene: a wrapping ring of cell heights around the camera, drawn painter's order
|
||||
// as instanced vertical strips over a baked sky. Same createScene contract as the ray-march scene,
|
||||
// as vertical strips over a baked sky. Same createScene contract as the ray-march scene,
|
||||
// but WebGL2 core only: no extensions, no float targets. Heights are 16 bits in an RG8 ring that the
|
||||
// strips' vertex shader reads; the sky is baked once into RGBA8; the drawing buffer is a fixed pixel
|
||||
// budget (320x180 at the top of the ladder) in the canvas's aspect, and CSS scales it up.
|
||||
|
|
@ -227,8 +227,8 @@ export function createVoxelScene(opts: SceneOptions): Scene | null {
|
|||
// The shear: the horizon sits above the centre by the amount the camera looks down.
|
||||
const horizon = -Math.tan(pose.pitch) / tanY;
|
||||
const first = firstStep(py, horizon, tanY);
|
||||
const instances = canvasW * (STEPS - first);
|
||||
const vertices = instances * VERTICES_PER_STRIP + 3;
|
||||
const strips = canvasW * (STEPS - first);
|
||||
const vertices = strips * VERTICES_PER_STRIP + 3;
|
||||
const lookups = liveStrips(canvasW, first, tanX) * LOOKUPS_PER_STRIP;
|
||||
|
||||
const timed = measuring && frame % READBACK_EVERY === 0;
|
||||
|
|
@ -253,10 +253,11 @@ export function createVoxelScene(opts: SceneOptions): Scene | null {
|
|||
gl.bindTexture(gl.TEXTURE_2D, skyTex);
|
||||
gl.uniform1i(loc(p, 'u_sky'), 1);
|
||||
};
|
||||
// Far to near, no depth test, no blending: the later strip is the nearer one and overwrites.
|
||||
// Far to near, no depth test, no blending: the later strip is the nearer one and overwrites. One
|
||||
// plain draw: instancing multiplies per-draw cost by the instance count in software GL.
|
||||
const drawStrips = (p: WebGLProgram) => {
|
||||
bindWorld(p);
|
||||
gl.drawArraysInstanced(gl.TRIANGLE_STRIP, 0, VERTICES_PER_STRIP, instances);
|
||||
gl.drawArrays(gl.TRIANGLES, 0, strips * VERTICES_PER_STRIP);
|
||||
};
|
||||
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
||||
gl.viewport(0, 0, canvasW, canvasH);
|
||||
|
|
|
|||
|
|
@ -80,11 +80,12 @@ export function liveStrips(columns: number, first: number, tanX: number): number
|
|||
}
|
||||
|
||||
/**
|
||||
* Ring lookups per live strip: each bottom vertex reads the nearer neighbour's height, each top
|
||||
* vertex its own, and the two top vertices that carry the strip's colour read the neighbour's too.
|
||||
* Ring lookups per live strip: each of the three bottom vertices reads the nearer neighbour's
|
||||
* height, each of the three top vertices its own, and the two that carry the strip's colour read
|
||||
* the neighbour's too.
|
||||
*/
|
||||
export const LOOKUPS_PER_STRIP = 6;
|
||||
export const VERTICES_PER_STRIP = 4;
|
||||
export const LOOKUPS_PER_STRIP = 8;
|
||||
export const VERTICES_PER_STRIP = 6;
|
||||
|
||||
const f = (n: number) => (Number.isInteger(n) ? `${n}.0` : `${n}`);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue