UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

194 lines (191 loc) 8.45 kB
import { Debug } from '../../core/debug.js'; import { math } from '../../core/math/math.js'; import { uniformTypeToName, UNIFORMTYPE_MAT4, UNIFORMTYPE_MAT4ARRAY, UNIFORMTYPE_BVEC4, UNIFORMTYPE_BVEC4ARRAY, UNIFORMTYPE_UVEC4, UNIFORMTYPE_UVEC4ARRAY, UNIFORMTYPE_IVEC4, UNIFORMTYPE_IVEC4ARRAY, UNIFORMTYPE_VEC4, UNIFORMTYPE_VEC4ARRAY, UNIFORMTYPE_BVEC3, UNIFORMTYPE_BVEC3ARRAY, UNIFORMTYPE_UVEC3, UNIFORMTYPE_UVEC3ARRAY, UNIFORMTYPE_IVEC3, UNIFORMTYPE_IVEC3ARRAY, UNIFORMTYPE_VEC3, UNIFORMTYPE_VEC3ARRAY, UNIFORMTYPE_BVEC2, UNIFORMTYPE_BVEC2ARRAY, UNIFORMTYPE_UVEC2, UNIFORMTYPE_UVEC2ARRAY, UNIFORMTYPE_IVEC2, UNIFORMTYPE_IVEC2ARRAY, UNIFORMTYPE_VEC2, UNIFORMTYPE_VEC2ARRAY, UNIFORMTYPE_BOOL, UNIFORMTYPE_BOOLARRAY, UNIFORMTYPE_UINT, UNIFORMTYPE_UINTARRAY, UNIFORMTYPE_INT, UNIFORMTYPE_INTARRAY, UNIFORMTYPE_FLOAT, UNIFORMTYPE_FLOATARRAY, UNIFORMTYPE_MAT2, UNIFORMTYPE_MAT3 } from './constants.js'; /** * @import { GraphicsDevice } from './graphics-device.js' * @import { ScopeId } from './scope-id.js' */ // map of UNIFORMTYPE_*** to number of 32bit components const uniformTypeToNumComponents = []; uniformTypeToNumComponents[UNIFORMTYPE_FLOAT] = 1; uniformTypeToNumComponents[UNIFORMTYPE_VEC2] = 2; uniformTypeToNumComponents[UNIFORMTYPE_VEC3] = 3; uniformTypeToNumComponents[UNIFORMTYPE_VEC4] = 4; uniformTypeToNumComponents[UNIFORMTYPE_INT] = 1; uniformTypeToNumComponents[UNIFORMTYPE_IVEC2] = 2; uniformTypeToNumComponents[UNIFORMTYPE_IVEC3] = 3; uniformTypeToNumComponents[UNIFORMTYPE_IVEC4] = 4; uniformTypeToNumComponents[UNIFORMTYPE_BOOL] = 1; uniformTypeToNumComponents[UNIFORMTYPE_BVEC2] = 2; uniformTypeToNumComponents[UNIFORMTYPE_BVEC3] = 3; uniformTypeToNumComponents[UNIFORMTYPE_BVEC4] = 4; uniformTypeToNumComponents[UNIFORMTYPE_MAT2] = 8; // 2 x vec4 uniformTypeToNumComponents[UNIFORMTYPE_MAT3] = 12; // 3 x vec4 uniformTypeToNumComponents[UNIFORMTYPE_MAT4] = 16; // 4 x vec4 uniformTypeToNumComponents[UNIFORMTYPE_UINT] = 1; uniformTypeToNumComponents[UNIFORMTYPE_UVEC2] = 2; uniformTypeToNumComponents[UNIFORMTYPE_UVEC3] = 3; uniformTypeToNumComponents[UNIFORMTYPE_UVEC4] = 4; /** * A class storing description of an individual uniform, stored inside a uniform buffer. * * @category Graphics */ class UniformFormat { /** * True if this is an array of elements (i.e. count > 0) * * @type {boolean} */ get isArrayType() { return this.count > 0; } /** * Create a new UniformFormat instance. * * @param {string} name - The name of the uniform. * @param {number} type - The type of the uniform. One of the UNIFORMTYPE_*** constants. * @param {number} count - The number of elements in the array. Defaults to 0, which represents * a single element (not an array). */ constructor(name, type, count = 0){ // just a name this.shortName = name; // name with [0] if this is an array this.name = count ? `${name}[0]` : name; this.type = type; this.numComponents = uniformTypeToNumComponents[type]; Debug.assert(this.numComponents, `Unhandled uniform format ${type} used for ${name}`); this.updateType = type; if (count > 0) { switch(type){ case UNIFORMTYPE_FLOAT: this.updateType = UNIFORMTYPE_FLOATARRAY; break; case UNIFORMTYPE_INT: this.updateType = UNIFORMTYPE_INTARRAY; break; case UNIFORMTYPE_UINT: this.updateType = UNIFORMTYPE_UINTARRAY; break; case UNIFORMTYPE_BOOL: this.updateType = UNIFORMTYPE_BOOLARRAY; break; case UNIFORMTYPE_VEC2: this.updateType = UNIFORMTYPE_VEC2ARRAY; break; case UNIFORMTYPE_IVEC2: this.updateType = UNIFORMTYPE_IVEC2ARRAY; break; case UNIFORMTYPE_UVEC2: this.updateType = UNIFORMTYPE_UVEC2ARRAY; break; case UNIFORMTYPE_BVEC2: this.updateType = UNIFORMTYPE_BVEC2ARRAY; break; case UNIFORMTYPE_VEC3: this.updateType = UNIFORMTYPE_VEC3ARRAY; break; case UNIFORMTYPE_IVEC3: this.updateType = UNIFORMTYPE_IVEC3ARRAY; break; case UNIFORMTYPE_UVEC3: this.updateType = UNIFORMTYPE_UVEC3ARRAY; break; case UNIFORMTYPE_BVEC3: this.updateType = UNIFORMTYPE_BVEC3ARRAY; break; case UNIFORMTYPE_VEC4: this.updateType = UNIFORMTYPE_VEC4ARRAY; break; case UNIFORMTYPE_IVEC4: this.updateType = UNIFORMTYPE_IVEC4ARRAY; break; case UNIFORMTYPE_UVEC4: this.updateType = UNIFORMTYPE_UVEC4ARRAY; break; case UNIFORMTYPE_BVEC4: this.updateType = UNIFORMTYPE_BVEC4ARRAY; break; case UNIFORMTYPE_MAT4: this.updateType = UNIFORMTYPE_MAT4ARRAY; break; default: Debug.error(`Uniform array of type ${uniformTypeToName[type]} is not supported when processing uniform '${name}'.`); Debug.call(()=>{ this.invalid = true; }); break; } } this.count = count; Debug.assert(!isNaN(count), `Unsupported uniform: ${name}[${count}]`); Debug.call(()=>{ if (isNaN(count)) { this.invalid = true; } }); let componentSize = this.numComponents; // component size for arrays is aligned up to vec4 if (count) { componentSize = math.roundUp(componentSize, 4); } this.byteSize = componentSize * 4; if (count) { this.byteSize *= count; } Debug.assert(this.byteSize, `Unknown byte size for uniform format ${type} used for ${name}`); } // std140 rules: https://registry.khronos.org/OpenGL/specs/gl/glspec45.core.pdf#page=159 // TODO: this supports limited subset of functionality, arrays and arrays of structs are not supported. calculateOffset(offset) { // Note: vec3 has the same alignment as vec4 let alignment = this.byteSize <= 8 ? this.byteSize : 16; // arrays have vec4 alignments if (this.count) { alignment = 16; } // align the start offset offset = math.roundUp(offset, alignment); this.offset = offset / 4; } } /** * A descriptor that defines the layout of of data inside the uniform buffer. * * @category Graphics */ class UniformBufferFormat { /** * Create a new UniformBufferFormat instance. * * @param {GraphicsDevice} graphicsDevice - The graphics device. * @param {UniformFormat[]} uniforms - An array of uniforms to be stored in the buffer */ constructor(graphicsDevice, uniforms){ /** * @type {number} * @ignore */ this.byteSize = 0; /** * @type {Map<string,UniformFormat>} * @ignore */ this.map = new Map(); this.scope = graphicsDevice.scope; /** @type {UniformFormat[]} */ this.uniforms = uniforms; // TODO: optimize uniforms ordering let offset = 0; for(let i = 0; i < uniforms.length; i++){ const uniform = uniforms[i]; uniform.calculateOffset(offset); offset = uniform.offset * 4 + uniform.byteSize; uniform.scopeId = this.scope.resolve(uniform.name); this.map.set(uniform.name, uniform); } // round up buffer size this.byteSize = math.roundUp(offset, 16); } /** * Returns format of a uniform with specified name. Returns undefined if the uniform is not found. * * @param {string} name - The name of the uniform. * @returns {UniformFormat|undefined} - The format of the uniform. */ get(name) { return this.map.get(name); } } export { UniformBufferFormat, UniformFormat };