playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
39 lines (38 loc) • 1.63 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 { DebugHelper } from "../../core/debug.js";
import { BindGroupFormat, BindUniformBufferFormat } from "./bind-group-format.js";
import { BindGroup } from "./bind-group.js";
import { SHADERSTAGE_FRAGMENT, SHADERSTAGE_VERTEX, UNIFORM_BUFFER_DEFAULT_SLOT_NAME } from "./constants.js";
class DynamicBuffer {
constructor(device) {
/** @type {GraphicsDevice} */
__publicField(this, "device");
/**
* A cache of bind groups for each uniform buffer size, which is used to avoid creating a new
* bind group for each uniform buffer.
*
* @type {Map<number, BindGroup>}
*/
__publicField(this, "bindGroupCache", /* @__PURE__ */ new Map());
this.device = device;
this.bindGroupFormat = new BindGroupFormat(this.device, [
new BindUniformBufferFormat(UNIFORM_BUFFER_DEFAULT_SLOT_NAME, SHADERSTAGE_VERTEX | SHADERSTAGE_FRAGMENT)
]);
}
getBindGroup(ub) {
const ubSize = ub.format.byteSize;
let bindGroup = this.bindGroupCache.get(ubSize);
if (!bindGroup) {
bindGroup = new BindGroup(this.device, this.bindGroupFormat, ub);
DebugHelper.setName(bindGroup, `DynamicBuffer-BindGroup_${bindGroup.id}-${ubSize}`);
bindGroup.update();
this.bindGroupCache.set(ubSize, bindGroup);
}
return bindGroup;
}
}
export {
DynamicBuffer
};