'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var extractStructAndGroups = require('./extractStructAndGroups.js');
var generateGpuLayoutGroups = require('./generateGpuLayoutGroups.js');
var generateLayoutHash = require('./generateLayoutHash.js');
var removeStructAndGroupDuplicates = require('./removeStructAndGroupDuplicates.js');

const _GpuProgram = class {
  constructor({ fragment, vertex, layout, gpuLayout, name }) {
    /** @internal */
    this._layoutKey = 0;
    this.name = name;
    this.fragment = fragment;
    this.vertex = vertex;
    if (fragment.source === vertex.source) {
      const structsAndGroups = extractStructAndGroups.extractStructAndGroups(fragment.source);
      this.structsAndGroups = structsAndGroups;
    } else {
      const vertexStructsAndGroups = extractStructAndGroups.extractStructAndGroups(vertex.source);
      const fragmentStructsAndGroups = extractStructAndGroups.extractStructAndGroups(fragment.source);
      this.structsAndGroups = removeStructAndGroupDuplicates.removeStructAndGroupDuplicates(vertexStructsAndGroups, fragmentStructsAndGroups);
    }
    this.layout = layout ?? generateLayoutHash.generateLayoutHash(this.structsAndGroups);
    this.gpuLayout = gpuLayout ?? generateGpuLayoutGroups.generateGpuLayoutGroups(this.structsAndGroups);
  }
  destroy() {
    this._gpuLayout = null;
    this.gpuLayout = null;
    this.layout = null;
    this.structsAndGroups = null;
    this.fragment = null;
    this.vertex = null;
  }
  static from(options) {
    const key = `${options.vertex.source}:${options.fragment.source}:${options.fragment.entryPoint}:${options.vertex.entryPoint}`;
    if (!_GpuProgram.programCached[key]) {
      _GpuProgram.programCached[key] = new _GpuProgram(options);
    }
    return _GpuProgram.programCached[key];
  }
};
let GpuProgram = _GpuProgram;
GpuProgram.programCached = /* @__PURE__ */ Object.create(null);

exports.GpuProgram = GpuProgram;
//# sourceMappingURL=GpuProgram.js.map
