UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

74 lines (73 loc) 2.69 kB
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 { BINDGROUP_VIEW } from "./constants.js"; class ShaderProcessorOptions { /** * Constructs shader processing options, used to process the shader for uniform buffer support. * * @param {UniformBufferFormat} [viewUniformFormat] - Format of the uniform buffer. * @param {BindGroupFormat} [viewBindGroupFormat] - Format of the bind group. * @param {VertexFormat} [vertexFormat] - Format of the vertex buffer. */ constructor(viewUniformFormat, viewBindGroupFormat, vertexFormat) { /** @type {UniformBufferFormat[]} */ __publicField(this, "uniformFormats", []); /** @type {BindGroupFormat[]} */ __publicField(this, "bindGroupFormats", []); /** @type {VertexFormat[]} */ __publicField(this, "vertexFormat"); this.uniformFormats[BINDGROUP_VIEW] = viewUniformFormat; this.bindGroupFormats[BINDGROUP_VIEW] = viewBindGroupFormat; this.vertexFormat = vertexFormat; } /** * Get the bind group index for the uniform name. * * @param {string} name - The name of the uniform. * @returns {boolean} - Returns true if the uniform exists, false otherwise. */ hasUniform(name) { for (let i = 0; i < this.uniformFormats.length; i++) { const uniformFormat = this.uniformFormats[i]; if (uniformFormat?.get(name)) { return true; } } return false; } /** * Get the bind group texture slot for the texture uniform name. * * @param {string} name - The name of the texture uniform. * @returns {boolean} - Returns true if the texture uniform exists, false otherwise. */ hasTexture(name) { for (let i = 0; i < this.bindGroupFormats.length; i++) { const groupFormat = this.bindGroupFormats[i]; if (groupFormat?.getTexture(name)) { return true; } } return false; } getVertexElement(semantic) { return this.vertexFormat?.elements.find((element) => element.name === semantic); } /** * Generate unique key representing the processing options. * * @param {GraphicsDevice} device - The device. * @returns {string} - Returns the key. */ generateKey(device) { let key = JSON.stringify(this.uniformFormats) + JSON.stringify(this.bindGroupFormats); if (device.isWebGPU) { key += this.vertexFormat?.shaderProcessingHashString; } return key; } } export { ShaderProcessorOptions };