import type { GlSkinPaletteTexture } from './contract.render-gl-types.ts'; import { createGlSkinPaletteTexture, destroyGlSkinPaletteTexture, initializeGlSkinPaletteTexture, uploadGlSkinPaletteTexture, } from './glSkinPaletteTexture.ts'; interface Call { name: string; args: unknown[]; } function makeGl(maxTextureSize = 4096): { gl: WebGL2RenderingContext; calls: Call[] } { const calls: Call[] = []; const record = (name: string, result?: unknown) => (...args: unknown[]): unknown => { calls.push({ name, args }); return result; }; const gl = { MAX_TEXTURE_SIZE: 0x0d33, TEXTURE_2D: 0x0de1, RGBA: 0x1908, RGBA32F: 0x8814, FLOAT: 0x1406, NEAREST: 0x2600, CLAMP_TO_EDGE: 0x812f, TEXTURE_MIN_FILTER: 0x2801, TEXTURE_MAG_FILTER: 0x2800, TEXTURE_WRAP_S: 0x2802, TEXTURE_WRAP_T: 0x2803, UNPACK_PREMULTIPLY_ALPHA_WEBGL: 0x9241, createTexture: record('createTexture', { id: 'tex' }), deleteTexture: record('deleteTexture'), bindTexture: record('bindTexture'), getParameter: (pname: number) => { calls.push({ name: 'getParameter', args: [pname] }); if (pname === 0x0d33) return maxTextureSize; return 0; }, texImage2D: record('texImage2D'), texSubImage2D: record('texSubImage2D'), pixelStorei: record('pixelStorei'), texParameteri: record('texParameteri'), } as unknown as WebGL2RenderingContext; return { gl, calls }; } // Two joints' worth of identity-ish palette (32 floats). Values are irrelevant to the upload path. function makePalette(jointCount: number): Float32Array { return new Float32Array(jointCount * 16); } describe('createGlSkinPaletteTexture', () => { it('allocates a texture with zero joint capacity', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); expect(palette.jointCapacity).toBe(0); expect(calls.some((c) => c.name === 'createTexture')).toBe(true); }); }); describe('destroyGlSkinPaletteTexture', () => { it('deletes the palette texture', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); destroyGlSkinPaletteTexture(gl, palette); const del = calls.find((c) => c.name === 'deleteTexture'); expect(del?.args[0]).toBe(palette.texture); }); }); describe('initializeGlSkinPaletteTexture', () => { it('is the construction initializer of createGlSkinPaletteTexture', () => { expect(typeof initializeGlSkinPaletteTexture).toBe('function'); }); }); describe('uploadGlSkinPaletteTexture', () => { it('allocates RGBA32F storage width = jointCount*4 and sets NEAREST/CLAMP on first upload', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); uploadGlSkinPaletteTexture(gl, palette, makePalette(3), 3); const alloc = calls.find((c) => c.name === 'texImage2D'); expect(alloc).toBeDefined(); expect(alloc?.args[3]).toBe(12); // width = 3 joints * 4 texels expect(alloc?.args[4]).toBe(1); // height expect(alloc?.args[2]).toBe(gl.RGBA32F); expect(palette.jointCapacity).toBe(3); // NEAREST min+mag, CLAMP_TO_EDGE s+t — four texParameteri calls. expect(calls.filter((c) => c.name === 'texParameteri').length).toBe(4); expect(calls.some((c) => c.name === 'texParameteri' && c.args[2] === gl.NEAREST)).toBe(true); expect(calls.some((c) => c.name === 'texParameteri' && c.args[2] === gl.CLAMP_TO_EDGE)).toBe(true); }); it('binds the palette texture so the caller can set the sampler unit', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); uploadGlSkinPaletteTexture(gl, palette, makePalette(1), 1); const bind = calls.find((c) => c.name === 'bindTexture'); expect(bind?.args[0]).toBe(gl.TEXTURE_2D); expect(bind?.args[1]).toBe(palette.texture); }); it('disables UNPACK_PREMULTIPLY_ALPHA_WEBGL before uploading', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); uploadGlSkinPaletteTexture(gl, palette, makePalette(2), 2); const psi = calls.find((c) => c.name === 'pixelStorei' && c.args[0] === gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL); expect(psi).toBeDefined(); expect(psi?.args[1]).toBe(false); const bindIdx = calls.findIndex((c) => c.name === 'bindTexture'); const psiIdx = calls.findIndex((c) => c.name === 'pixelStorei' && c.args[0] === gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL); const uploadIdx = calls.findIndex((c) => c.name === 'texImage2D'); expect(psiIdx).toBeGreaterThan(bindIdx); expect(psiIdx).toBeLessThan(uploadIdx); }); it('uses texSubImage2D without resetting parameters when the palette fits the current capacity', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); uploadGlSkinPaletteTexture(gl, palette, makePalette(4), 4); calls.length = 0; uploadGlSkinPaletteTexture(gl, palette, makePalette(2), 2); const sub = calls.find((c) => c.name === 'texSubImage2D'); expect(sub).toBeDefined(); expect(sub?.args[2]).toBe(0); // xoffset expect(sub?.args[3]).toBe(0); // yoffset expect(sub?.args[4]).toBe(8); // width = 2 joints * 4 expect(palette.jointCapacity).toBe(4); // capacity unchanged expect(calls.some((c) => c.name === 'texParameteri')).toBe(false); expect(calls.some((c) => c.name === 'texImage2D')).toBe(false); }); it('grows storage (reallocates) when the palette exceeds the current capacity', () => { const { gl, calls } = makeGl(); const palette = createGlSkinPaletteTexture(gl); uploadGlSkinPaletteTexture(gl, palette, makePalette(2), 2); calls.length = 0; uploadGlSkinPaletteTexture(gl, palette, makePalette(5), 5); const alloc = calls.find((c) => c.name === 'texImage2D'); expect(alloc?.args[3]).toBe(20); // width = 5 joints * 4 expect(palette.jointCapacity).toBe(5); }); it('is alias-safe: reads jointCount before growing capacity', () => { const { gl } = makeGl(); const palette: GlSkinPaletteTexture = createGlSkinPaletteTexture(gl); uploadGlSkinPaletteTexture(gl, palette, makePalette(6), 6); expect(palette.jointCapacity).toBe(6); }); it('throws when MAX_TEXTURE_SIZE is not positive', () => { const { gl } = makeGl(0); const palette = createGlSkinPaletteTexture(gl); expect(() => uploadGlSkinPaletteTexture(gl, palette, makePalette(1), 1)).toThrow( 'WebGL MAX_TEXTURE_SIZE must be greater than zero', ); }); it('wraps into multiple rows when totalTexels exceeds MAX_TEXTURE_SIZE', () => { const { gl, calls } = makeGl(16); const palette = createGlSkinPaletteTexture(gl); // 5 joints × 4 texels = 20 texels, max width 16 → 16 × 2 texture uploadGlSkinPaletteTexture(gl, palette, makePalette(5), 5); const alloc = calls.find((c) => c.name === 'texImage2D'); expect(alloc).toBeDefined(); expect(alloc?.args[3]).toBe(16); // width = MAX_TEXTURE_SIZE expect(alloc?.args[4]).toBe(2); // height = ceil(20 / 16) expect(alloc?.args[8]).toBeNull(); // allocated with null, then filled row by row expect(palette.jointCapacity).toBe(5); const subs = calls.filter((c) => c.name === 'texSubImage2D'); expect(subs.length).toBe(2); expect(subs[0].args[3]).toBe(0); // row 0 yoffset expect(subs[0].args[4]).toBe(16); // row 0 width = full row expect(subs[1].args[3]).toBe(1); // row 1 yoffset expect(subs[1].args[4]).toBe(4); // row 1 width = 20 - 16 remaining texels }); it('uploads multi-row sub-image when data fits existing multi-row capacity', () => { const { gl, calls } = makeGl(16); const palette = createGlSkinPaletteTexture(gl); // Allocate for 6 joints (24 texels → 16 × 2) uploadGlSkinPaletteTexture(gl, palette, makePalette(6), 6); calls.length = 0; // Upload 5 joints (20 texels) into the existing 16 × 2 texture uploadGlSkinPaletteTexture(gl, palette, makePalette(5), 5); expect(calls.some((c) => c.name === 'texImage2D')).toBe(false); const subs = calls.filter((c) => c.name === 'texSubImage2D'); expect(subs.length).toBe(2); expect(subs[0].args[4]).toBe(16); // row 0 full width expect(subs[1].args[4]).toBe(4); // row 1 partial (20 - 16) }); });