(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports"], factory); } else if (typeof exports !== "undefined") { factory(exports); } else { var mod = { exports: {} }; factory(mod.exports); global.WebGPUBindings = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.default = void 0; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var WebGPUBindings = /*#__PURE__*/function () { function WebGPUBindings(device, info, properties, textures, renderPipelines, computePipelines, attributes, nodes) { _classCallCheck(this, WebGPUBindings); this.device = device; this.info = info; this.properties = properties; this.textures = textures; this.renderPipelines = renderPipelines; this.computePipelines = computePipelines; this.attributes = attributes; this.nodes = nodes; this.uniformsData = new WeakMap(); this.updateMap = new WeakMap(); } _createClass(WebGPUBindings, [{ key: "get", value: function get(object) { var data = this.uniformsData.get(object); if (data === undefined) { // each object defines an array of bindings (ubos, textures, samplers etc.) var nodeBuilder = this.nodes.get(object); var bindings = nodeBuilder.getBindings(); // setup (static) binding layout and (dynamic) binding group var renderPipeline = this.renderPipelines.get(object); var bindLayout = renderPipeline.pipeline.getBindGroupLayout(0); var bindGroup = this._createBindGroup(bindings, bindLayout); data = { layout: bindLayout, group: bindGroup, bindings: bindings }; this.uniformsData.set(object, data); } return data; } }, { key: "getForCompute", value: function getForCompute(param) { var data = this.uniformsData.get(param); if (data === undefined) { // bindings are not yet retrieved via node material var bindings = param.bindings !== undefined ? param.bindings.slice() : []; var computePipeline = this.computePipelines.get(param); var bindLayout = computePipeline.getBindGroupLayout(0); var bindGroup = this._createBindGroup(bindings, bindLayout); data = { layout: bindLayout, group: bindGroup, bindings: bindings }; this.uniformsData.set(param, data); } return data; } }, { key: "update", value: function update(object) { var textures = this.textures; var data = this.get(object); var bindings = data.bindings; var updateMap = this.updateMap; var frame = this.info.render.frame; var needsBindGroupRefresh = false; // iterate over all bindings and check if buffer updates or a new binding group is required var _iterator = _createForOfIteratorHelper(bindings), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var binding = _step.value; var isShared = binding.isShared; var isUpdated = updateMap.get(binding) === frame; if (isShared && isUpdated) continue; if (binding.isUniformBuffer) { var buffer = binding.getBuffer(); var bufferGPU = binding.bufferGPU; var needsBufferWrite = binding.update(); if (needsBufferWrite === true) { this.device.queue.writeBuffer(bufferGPU, 0, buffer, 0); } } else if (binding.isStorageBuffer) { var attribute = binding.attribute; this.attributes.update(attribute, false, binding.usage); } else if (binding.isSampler) { var texture = binding.getTexture(); textures.updateSampler(texture); var samplerGPU = textures.getSampler(texture); if (binding.samplerGPU !== samplerGPU) { binding.samplerGPU = samplerGPU; needsBindGroupRefresh = true; } } else if (binding.isSampledTexture) { var _texture = binding.getTexture(); var needsTextureRefresh = textures.updateTexture(_texture); var textureGPU = textures.getTextureGPU(_texture); if (textureGPU !== undefined && binding.textureGPU !== textureGPU || needsTextureRefresh === true) { binding.textureGPU = textureGPU; needsBindGroupRefresh = true; } } updateMap.set(binding, frame); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (needsBindGroupRefresh === true) { data.group = this._createBindGroup(bindings, data.layout); } } }, { key: "dispose", value: function dispose() { this.uniformsData = new WeakMap(); this.updateMap = new WeakMap(); } }, { key: "_createBindGroup", value: function _createBindGroup(bindings, layout) { var bindingPoint = 0; var entries = []; var _iterator2 = _createForOfIteratorHelper(bindings), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var binding = _step2.value; if (binding.isUniformBuffer) { if (binding.bufferGPU === null) { var byteLength = binding.getByteLength(); binding.bufferGPU = this.device.createBuffer({ size: byteLength, usage: binding.usage }); } entries.push({ binding: bindingPoint, resource: { buffer: binding.bufferGPU } }); } else if (binding.isStorageBuffer) { if (binding.bufferGPU === null) { var attribute = binding.attribute; this.attributes.update(attribute, false, binding.usage); binding.bufferGPU = this.attributes.get(attribute).buffer; } entries.push({ binding: bindingPoint, resource: { buffer: binding.bufferGPU } }); } else if (binding.isSampler) { if (binding.samplerGPU === null) { binding.samplerGPU = this.textures.getDefaultSampler(); } entries.push({ binding: bindingPoint, resource: binding.samplerGPU }); } else if (binding.isSampledTexture) { if (binding.textureGPU === null) { if (binding.isSampledCubeTexture) { binding.textureGPU = this.textures.getDefaultCubeTexture(); } else { binding.textureGPU = this.textures.getDefaultTexture(); } } entries.push({ binding: bindingPoint, resource: binding.textureGPU.createView({ dimension: binding.dimension }) }); } bindingPoint++; } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } return this.device.createBindGroup({ layout: layout, entries: entries }); } }]); return WebGPUBindings; }(); var _default = WebGPUBindings; _exports.default = _default; });