A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(render): share uniform-arity classifier between material and draw
esengine40a13e039574
diff --git a/sdk/src/draw.ts b/sdk/src/draw.tsindex 74de9155e..857549bb5 100644--- a/sdk/src/draw.ts+++ b/sdk/src/draw.ts@@ -9,7 +9,7 @@ import type { ESEngineModule } from './wasm';import type { Vec2, Color } from './types';import type { GeometryHandle } from './geometry';import type { ShaderHandle, MaterialHandle } from './material';-import { Material, isTextureRef } from './material';+import { Material, isTextureRef, classifyUniformArity, type UniformValue, type TextureRef } from './material';import { BlendMode } from './blend';import { handleWasmError } from './wasmError';import { log } from './logger';@@ -385,38 +385,23 @@ export const Draw: DrawAPI = {if (nameId < 0) continue;if (isTextureRef(value)) {+ // Texture refs use a separate type code (10) since+ // they carry slot + textureId rather than packed floats.uniformBuffer[idx++] = 10;uniformBuffer[idx++] = nameId;uniformBuffer[idx++] = value.slot ?? autoTextureSlot++;uniformBuffer[idx++] = value.textureId;- } else if (typeof value === 'number') {- uniformBuffer[idx++] = 1;- uniformBuffer[idx++] = nameId;- uniformBuffer[idx++] = value;- } else if (Array.isArray(value)) {- uniformBuffer[idx++] = value.length;- uniformBuffer[idx++] = nameId;- for (let i = 0; i < value.length; i++) {- uniformBuffer[idx++] = value[i];- }- } else if ('w' in value) {- uniformBuffer[idx++] = 4;- uniformBuffer[idx++] = nameId;- uniformBuffer[idx++] = value.x;- uniformBuffer[idx++] = value.y;- uniformBuffer[idx++] = value.z;- uniformBuffer[idx++] = value.w;- } else if ('z' in value) {- uniformBuffer[idx++] = 3;- uniformBuffer[idx++] = nameId;- uniformBuffer[idx++] = value.x;- uniformBuffer[idx++] = value.y;- uniformBuffer[idx++] = value.z;} else {- uniformBuffer[idx++] = 2;+ // Scalar/vec uniforms: type code in this layout is+ // one-indexed arity (1=float, 2=vec2, 3=vec3, 4=vec4).+ const { arity, values } = classifyUniformArity(+ value as Exclude<UniformValue, TextureRef>,+ );+ uniformBuffer[idx++] = arity;uniformBuffer[idx++] = nameId;- uniformBuffer[idx++] = value.x;- uniformBuffer[idx++] = value.y;+ for (let i = 0; i < arity; i++) {+ uniformBuffer[idx++] = values[i];+ }}}diff --git a/sdk/src/material.ts b/sdk/src/material.tsindex 028655dcc..4efb0fd80 100644--- a/sdk/src/material.ts+++ b/sdk/src/material.ts@@ -31,6 +31,40 @@ export function isTextureRef(v: UniformValue): v is TextureRef {return typeof v === 'object' && v !== null && '__textureRef' in v;}+/**+ * Decompose a non-texture uniform value into an arity (1–4) and packed+ * float values. Shared by the material-registration serializer (in this+ * file) and the per-draw uniform path in draw.ts, which previously had+ * their own parallel branches that drifted in subtle ways.+ *+ * The two serializers apply different *type codes* on top of the arity —+ * material.ts uses 0..3 (float, vec2, vec3, vec4), while draw.ts uses+ * 1..4 plus 10 for texture refs. Those layouts match two distinct WASM+ * APIs (material-reflection vs per-draw uniform binding), so the code+ * offsets stay at each call site; only the arity-and-extract step is+ * centralized here.+ */+export interface UniformArity {+ readonly arity: 1 | 2 | 3 | 4;+ readonly values: readonly [number, number, number, number];+}++export function classifyUniformArity(value: Exclude<UniformValue, TextureRef>): UniformArity {+ if (typeof value === 'number') {+ return { arity: 1, values: [value, 0, 0, 0] };+ }+ if (Array.isArray(value)) {+ const arity = Math.max(1, Math.min(value.length, 4)) as 1 | 2 | 3 | 4;+ return {+ arity,+ values: [value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0],+ };+ }+ if ('w' in value) return { arity: 4, values: [value.x, value.y, value.z, value.w] };+ if ('z' in value) return { arity: 3, values: [value.x, value.y, value.z, 0] };+ return { arity: 2, values: [value.x, value.y, 0, 0] };+}+export interface MaterialOptions {shader: ShaderHandle;uniforms?: Record<string, UniformValue>;@@ -394,29 +428,11 @@ function serializeUniforms(uniforms: Map<string, UniformValue>): { ptr: number;heap8.set(nameBytes, bufferPtr + offset);offset += namePadded;- let type = 0;- let values = [0, 0, 0, 0];-- if (typeof value === 'number') {- type = 0;- values[0] = value;- } else if (Array.isArray(value)) {- type = Math.min(value.length - 1, 3);- for (let i = 0; i < Math.min(value.length, 4); i++) {- values[i] = value[i];- }- } else if ('w' in value) {- type = 3;- values = [value.x, value.y, value.z, value.w];- } else if ('z' in value) {- type = 2;- values = [value.x, value.y, value.z, 0];- } else if ('y' in value) {- type = 1;- values = [value.x, value.y, 0, 0];- }-- heap32[(bufferPtr + offset) >> 2] = type;+ // Type codes in this layout are zero-indexed (0=float, 1=vec2,+ // 2=vec3, 3=vec4); classifyUniformArity returns arity 1..4 so we+ // subtract one to match.+ const { arity, values } = classifyUniformArity(value as Exclude<UniformValue, TextureRef>);+ heap32[(bufferPtr + offset) >> 2] = arity - 1;offset += 4;for (let i = 0; i < 4; i++) {diff --git a/sdk/tests/draw.test.ts b/sdk/tests/draw.test.tsindex 5a8bc2b13..2537a1399 100644--- a/sdk/tests/draw.test.ts+++ b/sdk/tests/draw.test.ts@@ -8,6 +8,19 @@ vi.mock('../src/material', () => {isTextureRef: vi.fn((v: any) => typeof v === 'object' && v !== null && '__textureRef' in v,),+ classifyUniformArity: (value: any) => {+ if (typeof value === 'number') return { arity: 1, values: [value, 0, 0, 0] };+ if (Array.isArray(value)) {+ const arity = Math.max(1, Math.min(value.length, 4));+ return {+ arity,+ values: [value[0] ?? 0, value[1] ?? 0, value[2] ?? 0, value[3] ?? 0],+ };+ }+ if ('w' in value) return { arity: 4, values: [value.x, value.y, value.z, value.w] };+ if ('z' in value) return { arity: 3, values: [value.x, value.y, value.z, 0] };+ return { arity: 2, values: [value.x, value.y, 0, 0] };+ },};});diff --git a/sdk/tests/uniform-arity.test.ts b/sdk/tests/uniform-arity.test.tsnew file mode 100644index 000000000..a5c117d16--- /dev/null+++ b/sdk/tests/uniform-arity.test.ts@@ -0,0 +1,60 @@+/**+ * @file uniform-arity.test.ts+ * @brief classifyUniformArity — shared classifier used by both the+ * material-registration serializer and the per-draw uniform+ * binder. Previously each site had its own parallel branches.+ */+import { describe, expect, it } from 'vitest';+import { classifyUniformArity } from '../src/material';++describe('classifyUniformArity', () => {+ it('classifies a plain number as arity 1', () => {+ const out = classifyUniformArity(3.14);+ expect(out.arity).toBe(1);+ expect(out.values).toEqual([3.14, 0, 0, 0]);+ });++ it('classifies Vec2 as arity 2', () => {+ const out = classifyUniformArity({ x: 1, y: 2 });+ expect(out.arity).toBe(2);+ expect(out.values).toEqual([1, 2, 0, 0]);+ });++ it('classifies Vec3 as arity 3', () => {+ const out = classifyUniformArity({ x: 1, y: 2, z: 3 });+ expect(out.arity).toBe(3);+ expect(out.values).toEqual([1, 2, 3, 0]);+ });++ it('classifies Vec4 as arity 4', () => {+ const out = classifyUniformArity({ x: 1, y: 2, z: 3, w: 4 });+ expect(out.arity).toBe(4);+ expect(out.values).toEqual([1, 2, 3, 4]);+ });++ it('treats w presence as precedence (w > z > y detection order)', () => {+ // Safety: an object with { x, y, z, w } matches the w-branch first so+ // we never mis-classify a Vec4 as Vec3.+ const out = classifyUniformArity({ x: 1, y: 2, z: 3, w: 4 });+ expect(out.arity).toBe(4);+ });++ it('packs number[] up to length 4, zero-pads remaining slots', () => {+ expect(classifyUniformArity([5])).toEqual({ arity: 1, values: [5, 0, 0, 0] });+ expect(classifyUniformArity([5, 6])).toEqual({ arity: 2, values: [5, 6, 0, 0] });+ expect(classifyUniformArity([5, 6, 7])).toEqual({ arity: 3, values: [5, 6, 7, 0] });+ expect(classifyUniformArity([5, 6, 7, 8])).toEqual({ arity: 4, values: [5, 6, 7, 8] });+ });++ it('truncates number[] longer than 4 at arity 4', () => {+ const out = classifyUniformArity([5, 6, 7, 8, 9, 10]);+ expect(out.arity).toBe(4);+ expect(out.values).toEqual([5, 6, 7, 8]);+ });++ it('handles an empty number[] as arity 1 (degenerate but safe)', () => {+ const out = classifyUniformArity([]);+ expect(out.arity).toBe(1);+ expect(out.values).toEqual([0, 0, 0, 0]);+ });+});