playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
335 lines (334 loc) • 11.9 kB
JavaScript
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
import { Debug } from "../../core/debug.js";
import {
uniformTypeToName,
UNIFORMTYPE_INT,
UNIFORMTYPE_FLOAT,
UNIFORMTYPE_VEC2,
UNIFORMTYPE_VEC3,
UNIFORMTYPE_VEC4,
UNIFORMTYPE_IVEC2,
UNIFORMTYPE_IVEC3,
UNIFORMTYPE_IVEC4,
UNIFORMTYPE_FLOATARRAY,
UNIFORMTYPE_VEC2ARRAY,
UNIFORMTYPE_VEC3ARRAY,
UNIFORMTYPE_MAT2,
UNIFORMTYPE_MAT3,
UNIFORMTYPE_UINT,
UNIFORMTYPE_UVEC2,
UNIFORMTYPE_UVEC3,
UNIFORMTYPE_UVEC4,
UNIFORMTYPE_INTARRAY,
UNIFORMTYPE_UINTARRAY,
UNIFORMTYPE_BOOLARRAY,
UNIFORMTYPE_IVEC2ARRAY,
UNIFORMTYPE_IVEC3ARRAY,
UNIFORMTYPE_UVEC2ARRAY,
UNIFORMTYPE_UVEC3ARRAY,
UNIFORMTYPE_BVEC2ARRAY,
UNIFORMTYPE_BVEC3ARRAY
} from "./constants.js";
import { DebugGraphics } from "./debug-graphics.js";
import { DynamicBufferAllocation } from "./dynamic-buffers.js";
const _updateFunctions = [];
_updateFunctions[UNIFORMTYPE_FLOAT] = function(uniformBuffer, value, offset) {
const dst = uniformBuffer.storageFloat32;
dst[offset] = value;
};
_updateFunctions[UNIFORMTYPE_VEC2] = (uniformBuffer, value, offset) => {
const dst = uniformBuffer.storageFloat32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
};
_updateFunctions[UNIFORMTYPE_VEC3] = (uniformBuffer, value, offset) => {
const dst = uniformBuffer.storageFloat32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
};
_updateFunctions[UNIFORMTYPE_VEC4] = (uniformBuffer, value, offset) => {
const dst = uniformBuffer.storageFloat32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
dst[offset + 3] = value[3];
};
_updateFunctions[UNIFORMTYPE_INT] = function(uniformBuffer, value, offset) {
const dst = uniformBuffer.storageInt32;
dst[offset] = value;
};
_updateFunctions[UNIFORMTYPE_IVEC2] = function(uniformBuffer, value, offset) {
const dst = uniformBuffer.storageInt32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
};
_updateFunctions[UNIFORMTYPE_IVEC3] = function(uniformBuffer, value, offset) {
const dst = uniformBuffer.storageInt32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
};
_updateFunctions[UNIFORMTYPE_IVEC4] = function(uniformBuffer, value, offset) {
const dst = uniformBuffer.storageInt32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
dst[offset + 3] = value[3];
};
_updateFunctions[UNIFORMTYPE_MAT2] = (uniformBuffer, value, offset) => {
const dst = uniformBuffer.storageFloat32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 4] = value[2];
dst[offset + 5] = value[3];
dst[offset + 8] = value[4];
dst[offset + 9] = value[5];
};
_updateFunctions[UNIFORMTYPE_MAT3] = (uniformBuffer, value, offset) => {
const dst = uniformBuffer.storageFloat32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
dst[offset + 4] = value[3];
dst[offset + 5] = value[4];
dst[offset + 6] = value[5];
dst[offset + 8] = value[6];
dst[offset + 9] = value[7];
dst[offset + 10] = value[8];
};
_updateFunctions[UNIFORMTYPE_FLOATARRAY] = function(uniformBuffer, value, offset, count) {
const dst = uniformBuffer.storageFloat32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i];
}
};
_updateFunctions[UNIFORMTYPE_VEC2ARRAY] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageFloat32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i * 2];
dst[offset + i * 4 + 1] = value[i * 2 + 1];
}
};
_updateFunctions[UNIFORMTYPE_VEC3ARRAY] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageFloat32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i * 3];
dst[offset + i * 4 + 1] = value[i * 3 + 1];
dst[offset + i * 4 + 2] = value[i * 3 + 2];
}
};
_updateFunctions[UNIFORMTYPE_UINT] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageUint32;
dst[offset] = value;
};
_updateFunctions[UNIFORMTYPE_UVEC2] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageUint32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
};
_updateFunctions[UNIFORMTYPE_UVEC3] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageUint32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
};
_updateFunctions[UNIFORMTYPE_UVEC4] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageUint32;
dst[offset] = value[0];
dst[offset + 1] = value[1];
dst[offset + 2] = value[2];
dst[offset + 3] = value[3];
};
_updateFunctions[UNIFORMTYPE_INTARRAY] = function(uniformBuffer, value, offset, count) {
const dst = uniformBuffer.storageInt32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i];
}
};
_updateFunctions[UNIFORMTYPE_BOOLARRAY] = _updateFunctions[UNIFORMTYPE_INTARRAY];
_updateFunctions[UNIFORMTYPE_UINTARRAY] = function(uniformBuffer, value, offset, count) {
const dst = uniformBuffer.storageUint32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i];
}
};
_updateFunctions[UNIFORMTYPE_IVEC2ARRAY] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageInt32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i * 2];
dst[offset + i * 4 + 1] = value[i * 2 + 1];
}
};
_updateFunctions[UNIFORMTYPE_BVEC2ARRAY] = _updateFunctions[UNIFORMTYPE_IVEC2ARRAY];
_updateFunctions[UNIFORMTYPE_UVEC2ARRAY] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageUint32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i * 2];
dst[offset + i * 4 + 1] = value[i * 2 + 1];
}
};
_updateFunctions[UNIFORMTYPE_IVEC3ARRAY] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageInt32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i * 3];
dst[offset + i * 4 + 1] = value[i * 3 + 1];
dst[offset + i * 4 + 2] = value[i * 3 + 2];
}
};
_updateFunctions[UNIFORMTYPE_BVEC3ARRAY] = _updateFunctions[UNIFORMTYPE_IVEC3ARRAY];
_updateFunctions[UNIFORMTYPE_UVEC3ARRAY] = (uniformBuffer, value, offset, count) => {
const dst = uniformBuffer.storageUint32;
for (let i = 0; i < count; i++) {
dst[offset + i * 4] = value[i * 3];
dst[offset + i * 4 + 1] = value[i * 3 + 1];
dst[offset + i * 4 + 2] = value[i * 3 + 2];
}
};
class UniformBuffer {
/**
* Create a new UniformBuffer instance.
*
* @param {GraphicsDevice} graphicsDevice - The graphics device used to manage this uniform
* buffer.
* @param {UniformBufferFormat} format - Format of the uniform buffer.
* @param {boolean} [persistent] - Whether the buffer is persistent. Defaults to true.
*/
constructor(graphicsDevice, format, persistent = true) {
__publicField(this, "device");
/** @type {boolean} */
__publicField(this, "persistent");
/** @type {DynamicBufferAllocation} */
__publicField(this, "allocation");
/** @type {Float32Array} */
__publicField(this, "storageFloat32");
/** @type {Int32Array} */
__publicField(this, "storageInt32");
/** @type {Uint32Array} */
__publicField(this, "storageUint32");
/**
* A render version used to track the last time the properties requiring bind group to be
* updated were changed.
*/
__publicField(this, "renderVersionDirty", 0);
this.device = graphicsDevice;
this.format = format;
this.persistent = persistent;
Debug.assert(format);
if (persistent) {
this.impl = graphicsDevice.createUniformBufferImpl(this);
const storage = new ArrayBuffer(format.byteSize);
this.assignStorage(new Int32Array(storage));
graphicsDevice._vram.ub += this.format.byteSize;
} else {
this.allocation = new DynamicBufferAllocation();
}
}
/**
* Frees resources associated with this uniform buffer.
*/
destroy() {
if (this.persistent) {
const device = this.device;
this.impl.destroy(device);
device._vram.ub -= this.format.byteSize;
}
}
get offset() {
return this.persistent ? 0 : this.allocation.offset;
}
/**
* Assign a storage to this uniform buffer.
*
* @param {Int32Array} storage - The storage to assign to this uniform buffer.
*/
assignStorage(storage) {
this.storageInt32 = storage;
this.storageUint32 = new Uint32Array(storage.buffer, storage.byteOffset, storage.byteLength / 4);
this.storageFloat32 = new Float32Array(storage.buffer, storage.byteOffset, storage.byteLength / 4);
}
/**
* Called when the rendering context was lost. It releases all context related resources.
*/
loseContext() {
this.impl?.loseContext();
}
/**
* Assign a value to the uniform specified by its format. This is the fast version of assigning
* a value to a uniform, avoiding any lookups.
*
* @param {UniformFormat} uniformFormat - The format of the uniform.
* @param {any} value - The value to assign to the uniform.
*/
setUniform(uniformFormat, value) {
Debug.assert(uniformFormat);
const offset = uniformFormat.offset;
if (value !== null && value !== void 0) {
const updateFunction = _updateFunctions[uniformFormat.updateType];
if (updateFunction) {
updateFunction(this, value, offset, uniformFormat.count);
} else {
this.storageFloat32.set(value, offset);
}
} else {
Debug.warnOnce(`Value was not set when assigning to uniform [${uniformFormat.name}], expected type ${uniformTypeToName[uniformFormat.type]} while rendering ${DebugGraphics.toString()}`);
}
}
/**
* Assign a value to the uniform specified by name.
*
* @param {string} name - The name of the uniform.
* @param {any} value - The value to assign to the uniform.
*/
set(name, value) {
const uniformFormat = this.format.map.get(name);
Debug.assert(uniformFormat, `Uniform name [${name}] is not part of the Uniform buffer.`);
if (uniformFormat) {
this.setUniform(uniformFormat, value);
}
}
startUpdate(dynamicBindGroup) {
if (!this.persistent) {
const allocation = this.allocation;
const oldGpuBuffer = allocation.gpuBuffer;
this.device.dynamicBuffers.alloc(allocation, this.format.byteSize);
this.assignStorage(allocation.storage);
if (dynamicBindGroup) {
dynamicBindGroup.bindGroup = allocation.gpuBuffer.getBindGroup(this);
dynamicBindGroup.offsets[0] = allocation.offset;
}
if (oldGpuBuffer !== allocation.gpuBuffer) {
this.renderVersionDirty = this.device.renderVersion;
}
}
}
endUpdate() {
if (this.persistent) {
this.impl.unlock(this);
} else {
this.storageFloat32 = null;
this.storageInt32 = null;
}
}
/**
* @param {DynamicBindGroup} [dynamicBindGroup] - The function fills in the info about the
* dynamic bind group for this frame, which uses this uniform buffer. Only used if the uniform
* buffer is non-persistent. This allows the uniform buffer to be used without having to create
* a bind group for it. Note that the bind group can only contains this single uniform buffer,
* and no other resources.
*/
update(dynamicBindGroup) {
this.startUpdate(dynamicBindGroup);
const uniforms = this.format.uniforms;
for (let i = 0; i < uniforms.length; i++) {
const value = uniforms[i].scopeId.value;
this.setUniform(uniforms[i], value);
}
this.endUpdate();
}
}
export {
UniformBuffer
};