1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| interface UBOField { name: string; type: 'float' | 'vec2' | 'vec3' | 'vec4' | 'mat3' | 'mat4' | 'int' | 'ivec2' | 'ivec3' | 'ivec4'; arraySize?: number; offset: number; size: number; alignment: number; } interface UBOLayout { name: string; fields: UBOField[]; totalSize: number; alignment: number; } private static readonly STD140_ALIGNMENTS = { 'float': 4, 'vec2': 8, 'vec3': 16, 'vec4': 16, 'mat3': 16, 'mat4': 16, 'int': 4, 'ivec2': 8, 'ivec3': 16, 'ivec4': 16 }; private static readonly STD140_SIZES = { 'float': 4, 'vec2': 8, 'vec3': 12, 'vec4': 16, 'mat3': 48, 'mat4': 64, 'int': 4, 'ivec2': 8, 'ivec3': 12, 'ivec4': 16 }; public static calculateLayout(fields: Partial<UBOField>[]): UBOLayout { const layout: UBOLayout = { name: '', fields: [], totalSize: 0, alignment: 16 }; let currentOffset = 0; for (const fieldDef of fields) { const field: UBOField = { name: fieldDef.name!, type: fieldDef.type!, arraySize: fieldDef.arraySize || 1, offset: 0, size: 0, alignment: 0 }; const baseSize = this.STD140_SIZES[field.type]; field.alignment = baseAlignment; if (field.arraySize > 1) { field.alignment = elementAlignment; field.size = elementAlignment * field.arraySize; } else { field.size = baseSize; } field.offset = currentOffset; layout.fields.push(field); currentOffset += field.size; } layout.totalSize = this.alignOffset(currentOffset, 16); return layout; } private static alignOffset(offset: number, alignment: number): number { return Math.ceil(offset / alignment) * alignment; } public static generateUBOStruct(layout: UBOLayout, uboName: string): string { let glslCode = `layout(std140) uniform ${uboName} {\n`; for (const field of layout.fields) { const arrayPart = field.arraySize > 1 ? `[${field.arraySize}]` : ''; glslCode += ` ${field.type} ${field.name}${arrayPart};\n`; } glslCode += '};\n'; glslCode += `\n// Memory Layout (std140):\n`; glslCode += `// Total Size: ${layout.totalSize} bytes\n`; for (const field of layout.fields) { glslCode += `// ${field.name}: offset=${field.offset}, size=${field.size}, align=${field.alignment}\n`; } return glslCode; } }
private buffer: ArrayBuffer; private view: DataView; private layout: UBOLayout; constructor(layout: UBOLayout) { this.layout = layout; this.buffer = new ArrayBuffer(layout.totalSize); this.view = new DataView(this.buffer); } public setFloat(fieldName: string, value: number, arrayIndex: number = 0): void { const field = this.getField(fieldName); const offset = field.offset + arrayIndex * 16; this.view.setFloat32(offset, value, true); } public setVec2(fieldName: string, value: [number, number], arrayIndex: number = 0): void { const field = this.getField(fieldName); const offset = field.offset + arrayIndex * 16; this.view.setFloat32(offset + 0, value[0], true); this.view.setFloat32(offset + 4, value[1], true); } public setVec3(fieldName: string, value: [number, number, number], arrayIndex: number = 0): void { const field = this.getField(fieldName); const offset = field.offset + arrayIndex * 16; this.view.setFloat32(offset + 0, value[0], true); this.view.setFloat32(offset + 4, value[1], true); this.view.setFloat32(offset + 8, value[2], true); } public setVec4(fieldName: string, value: [number, number, number, number], arrayIndex: number = 0): void { const field = this.getField(fieldName); const offset = field.offset + arrayIndex * 16; this.view.setFloat32(offset + 0, value[0], true); this.view.setFloat32(offset + 4, value[1], true); this.view.setFloat32(offset + 8, value[2], true); this.view.setFloat32(offset + 12, value[3], true); } public setMat4(fieldName: string, matrix: Float32Array | number[], arrayIndex: number = 0): void { const field = this.getField(fieldName); const offset = field.offset + arrayIndex * 64; for (let i = 0; i < 16; i++) { this.view.setFloat32(offset + i * 4, matrix[i], true); } } public setInt(fieldName: string, value: number, arrayIndex: number = 0): void { const field = this.getField(fieldName); const offset = field.offset + arrayIndex * 16; this.view.setInt32(offset, value, true); } private getField(fieldName: string): UBOField { const field = this.layout.fields.find(f => f.name === fieldName); if (!field) { throw new Error(`UBO字段不存�? ${fieldName}`); } return field; } public getBuffer(): ArrayBuffer { return this.buffer; } public getUint8Array(): Uint8Array { return new Uint8Array(this.buffer); } }
|