import { ab as $, N as E, aR as X, ac as T, aS as ae, I as N, aT as G, v as ie, a9 as k, ad as ce } from "./index-DSGwv2Yx.js";
import { b as se } from "./backend_util-qwSFfxYx.js";
const w = {}, M = {
  alpha: !1,
  antialias: !1,
  premultipliedAlpha: !1,
  preserveDrawingBuffer: !1,
  depth: !1,
  stencil: !1,
  failIfMajorPerformanceCaveat: !0
};
function ut(e, t) {
  w[e] = t;
}
function A(e, t) {
  if (!(e in w) || t != null) {
    const r = le(e, t);
    if (r !== null)
      w[e] = r;
    else
      return console.log("Could not get context for WebGL version", e), null;
  }
  const n = w[e];
  return n == null || n.isContextLost() ? (delete w[e], A(e)) : (n.disable(n.DEPTH_TEST), n.disable(n.STENCIL_TEST), n.disable(n.BLEND), n.disable(n.DITHER), n.disable(n.POLYGON_OFFSET_FILL), n.disable(n.SAMPLE_COVERAGE), n.enable(n.SCISSOR_TEST), n.enable(n.CULL_FACE), n.cullFace(n.BACK), w[e]);
}
function ue(e) {
  if (!$().getBool("IS_SAFARI") && typeof OffscreenCanvas < "u" && e === 2)
    return new OffscreenCanvas(300, 150);
  if (typeof document < "u")
    return document.createElement("canvas");
  throw new Error("Cannot create a canvas in this context");
}
function le(e, t) {
  if (e !== 1 && e !== 2)
    throw new Error("Cannot get WebGL rendering context, WebGL is disabled.");
  const n = t ?? ue(e);
  return n.addEventListener("webglcontextlost", (r) => {
    r.preventDefault(), delete w[e];
  }, !1), $().getBool("SOFTWARE_WEBGL_ENABLED") && (M.failIfMajorPerformanceCaveat = !1), e === 1 ? (
    // tslint:disable-next-line
    n.getContext("webgl", M) || n.getContext("experimental-webgl", M)
  ) : n.getContext("webgl2", M);
}
var H;
(function(e) {
  e[e.DENSE = 0] = "DENSE", e[e.SHARED_BATCH = 1] = "SHARED_BATCH";
})(H || (H = {}));
var q;
(function(e) {
  e[e.RENDER = 0] = "RENDER", e[e.UPLOAD = 1] = "UPLOAD", e[e.PIXELS = 2] = "PIXELS", e[e.DOWNLOAD = 3] = "DOWNLOAD";
})(q || (q = {}));
var j;
(function(e) {
  e[e.UNPACKED_FLOAT16 = 0] = "UNPACKED_FLOAT16", e[e.UNPACKED_FLOAT32 = 1] = "UNPACKED_FLOAT32", e[e.PACKED_4X1_UNSIGNED_BYTE = 2] = "PACKED_4X1_UNSIGNED_BYTE", e[e.PACKED_2X2_FLOAT32 = 3] = "PACKED_2X2_FLOAT32", e[e.PACKED_2X2_FLOAT16 = 4] = "PACKED_2X2_FLOAT16";
})(j || (j = {}));
function lt(e, t) {
  return [t, e];
}
function ft(e, t) {
  return e * t;
}
function xt(e) {
  const t = E(e), n = Math.ceil(t / 4);
  return X(n);
}
function fe(e, t) {
  return [
    Math.max(1, Math.ceil(t / 2)),
    Math.max(1, Math.ceil(e / 2))
  ];
}
function dt(e, t) {
  const [n, r] = fe(e, t);
  return n * r * 4;
}
function K(e, t) {
  const n = e;
  let r, o, a, i, c, s, l, u, f, d;
  return $().getNumber("WEBGL_VERSION") === 2 ? (r = n.R32F, o = n.R16F, a = n.RGBA16F, i = n.RGBA32F, c = n.RED, l = 4, u = 1, f = n.HALF_FLOAT, d = n.FLOAT, s = n.RGBA8) : (r = e.RGBA, o = e.RGBA, a = e.RGBA, i = n.RGBA, c = e.RGBA, l = 4, u = 4, f = t != null ? t.HALF_FLOAT_OES : null, d = e.FLOAT, s = e.RGBA), {
    internalFormatFloat: r,
    internalFormatHalfFloat: o,
    internalFormatPackedHalfFloat: a,
    internalFormatPackedFloat: i,
    textureFormatFloat: c,
    downloadTextureFormat: s,
    downloadUnpackNumChannels: l,
    defaultNumChannels: u,
    textureTypeHalfFloat: f,
    textureTypeFloat: d
  };
}
function v(e, t) {
  const n = t();
  return $().getBool("DEBUG") && xe(e), n;
}
function xe(e) {
  const t = e.getError();
  if (t !== e.NO_ERROR)
    throw new Error("WebGL Error: " + pe(e, t));
}
const de = 596e-10, he = 65504;
function ht(e) {
  return !!($().getBool("WEBGL_RENDER_FLOAT32_ENABLED") || e === 0 || de < Math.abs(e) && Math.abs(e) < he);
}
function pe(e, t) {
  switch (t) {
    case e.NO_ERROR:
      return "NO_ERROR";
    case e.INVALID_ENUM:
      return "INVALID_ENUM";
    case e.INVALID_VALUE:
      return "INVALID_VALUE";
    case e.INVALID_OPERATION:
      return "INVALID_OPERATION";
    case e.INVALID_FRAMEBUFFER_OPERATION:
      return "INVALID_FRAMEBUFFER_OPERATION";
    case e.OUT_OF_MEMORY:
      return "OUT_OF_MEMORY";
    case e.CONTEXT_LOST_WEBGL:
      return "CONTEXT_LOST_WEBGL";
    default:
      return `Unknown error code ${t}`;
  }
}
function pt(e, t) {
  return C(e, () => e.getExtension(t), 'Extension "' + t + '" not supported on this browser.');
}
function vt(e, t) {
  const n = C(e, () => e.createShader(e.VERTEX_SHADER), "Unable to create vertex WebGLShader.");
  if (v(e, () => e.shaderSource(n, t)), v(e, () => e.compileShader(n)), e.getShaderParameter(n, e.COMPILE_STATUS) === !1)
    throw console.log(e.getShaderInfoLog(n)), new Error("Failed to compile vertex shader.");
  return n;
}
function ve(e, t) {
  const n = C(e, () => e.createShader(e.FRAGMENT_SHADER), "Unable to create fragment WebGLShader.");
  if (v(e, () => e.shaderSource(n, t)), v(e, () => e.compileShader(n)), $().get("ENGINE_COMPILE_ONLY"))
    return n;
  if (e.getShaderParameter(n, e.COMPILE_STATUS) === !1)
    throw me(t, e.getShaderInfoLog(n)), new Error("Failed to compile fragment shader.");
  return n;
}
const $e = /ERROR: [0-9]+:([0-9]+):/g;
function me(e, t) {
  const n = $e.exec(t);
  if (n == null) {
    console.log(`Couldn't parse line number in error: ${t}`), console.log(e);
    return;
  }
  const r = +n[1], o = e.split(`
`), a = o.length.toString().length + 2, i = o.map((f, d) => G((d + 1).toString(), a) + f);
  let c = 0;
  for (let f = 0; f < i.length; f++)
    c = Math.max(i[f].length, c);
  const s = i.slice(0, r - 1), l = i.slice(r - 1, r), u = i.slice(r);
  console.log(s.join(`
`)), console.log(t.split(`
`)[0]), console.log(`%c ${G(l[0], c)}`, "border:1px solid red; background-color:#e3d2d2; color:#a61717"), console.log(u.join(`
`));
}
function $t(e) {
  return C(e, () => e.createProgram(), "Unable to create WebGLProgram.");
}
function mt(e, t) {
  if (v(e, () => e.linkProgram(t)), !$().get("ENGINE_COMPILE_ONLY") && e.getProgramParameter(t, e.LINK_STATUS) === !1)
    throw console.log(e.getProgramInfoLog(t)), new Error("Failed to link vertex and fragment shaders.");
}
function Tt(e, t) {
  if (v(e, () => e.validateProgram(t)), e.getProgramParameter(t, e.VALIDATE_STATUS) === !1)
    throw console.log(e.getProgramInfoLog(t)), new Error("Shader program validation failed.");
}
function St(e, t) {
  const n = C(e, () => e.createBuffer(), "Unable to create WebGLBuffer");
  return v(e, () => e.bindBuffer(e.ARRAY_BUFFER, n)), v(e, () => e.bufferData(e.ARRAY_BUFFER, t, e.STATIC_DRAW)), n;
}
function Rt(e, t) {
  const n = C(e, () => e.createBuffer(), "Unable to create WebGLBuffer");
  return v(e, () => e.bindBuffer(e.ELEMENT_ARRAY_BUFFER, n)), v(e, () => e.bufferData(e.ELEMENT_ARRAY_BUFFER, t, e.STATIC_DRAW)), n;
}
function Et(e) {
  return C(e, () => e.createTexture(), "Unable to create WebGLTexture.");
}
function Ct(e, t) {
  const n = $().getNumber("WEBGL_MAX_TEXTURE_SIZE");
  if (e <= 0 || t <= 0) {
    const r = `[${e}x${t}]`;
    throw new Error("Requested texture size " + r + " is invalid.");
  }
  if (e > n || t > n) {
    const r = `[${e}x${t}]`, o = `[${n}x${n}]`;
    throw new Error("Requested texture size " + r + " greater than WebGL maximum on this browser / GPU " + o + ".");
  }
}
function Ft(e) {
  return C(e, () => e.createFramebuffer(), "Unable to create WebGLFramebuffer.");
}
function _t(e, t, n, r, o, a, i) {
  const c = e.getAttribLocation(t, n);
  return c === -1 ? !1 : (v(e, () => e.bindBuffer(e.ARRAY_BUFFER, r)), v(e, () => e.vertexAttribPointer(c, o, e.FLOAT, !1, a, i)), v(e, () => e.enableVertexAttribArray(c)), !0);
}
function Te(e, t, n) {
  Re(e, n), v(e, () => e.activeTexture(e.TEXTURE0 + n)), v(e, () => e.bindTexture(e.TEXTURE_2D, t));
}
function At(e, t, n) {
  return C(e, () => e.getUniformLocation(t, n), 'uniform "' + n + '" not present in program.');
}
function wt(e, t, n) {
  return e.getUniformLocation(t, n);
}
function Nt(e, t, n, r) {
  v(e, () => Te(e, t, r)), v(e, () => e.uniform1i(n, r));
}
function Ot(e, t, n) {
  v(e, () => e.bindFramebuffer(e.FRAMEBUFFER, n)), v(e, () => e.framebufferTexture2D(e.FRAMEBUFFER, e.COLOR_ATTACHMENT0, e.TEXTURE_2D, t, 0));
}
function It(e, t) {
  v(e, () => e.bindFramebuffer(e.FRAMEBUFFER, t)), v(e, () => e.framebufferTexture2D(e.FRAMEBUFFER, e.COLOR_ATTACHMENT0, e.TEXTURE_2D, null, 0));
}
function Lt(e) {
  const t = e.checkFramebufferStatus(e.FRAMEBUFFER);
  if (t !== e.FRAMEBUFFER_COMPLETE)
    throw new Error("Error binding framebuffer: " + Se(e, t));
}
function Se(e, t) {
  switch (t) {
    case e.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
      return "FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
    case e.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
      return "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
    case e.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
      return "FRAMEBUFFER_INCOMPLETE_DIMENSIONS";
    case e.FRAMEBUFFER_UNSUPPORTED:
      return "FRAMEBUFFER_UNSUPPORTED";
    default:
      return `unknown error ${t}`;
  }
}
function C(e, t, n) {
  const r = v(e, () => t());
  if (r == null)
    throw new Error(n);
  return r;
}
function Re(e, t) {
  const n = e.MAX_COMBINED_TEXTURE_IMAGE_UNITS - 1, r = t + e.TEXTURE0;
  if (r < e.TEXTURE0 || r > n) {
    const o = `[gl.TEXTURE0, gl.TEXTURE${n}]`;
    throw new Error(`textureUnit must be in ${o}.`);
  }
}
function Z(e, t = 2) {
  return E(e.slice(0, e.length - t));
}
function J(e) {
  if (e.length === 0)
    throw Error("Cannot get rows and columns of an empty shape array.");
  return [
    e.length > 1 ? e[e.length - 2] : 1,
    e[e.length - 1]
  ];
}
function Ut(e) {
  let t = [1, 1, 1];
  return e.length === 0 || e.length === 1 && e[0] === 1 || (t = [Z(e), ...J(e)]), t;
}
function Dt(e, t = !1) {
  let n = $().getNumber("WEBGL_MAX_TEXTURE_SIZE"), r = $().getNumber("WEBGL_MAX_SIZE_FOR_NARROW_TEXTURE");
  r === 1 / 0 && $().getBool("WEBGL_AUTO_SQUARIFY_NARROW_TEXTURE_SHAPE") && (r = n / 2), t && (n = n * 2, r = r * 2, e = e.map((c, s) => s >= e.length - 2 ? ae(e[s]) : e[s]), e.length === 1 && (e = [2, e[0]])), e.length !== 2 && (e = N(e).newShape);
  let o = E(e), a = null;
  e.length <= 1 && o <= n ? a = [1, o] : e.length === 2 && e[0] <= n && e[1] <= n ? a = e : e.length === 3 && e[0] * e[1] <= n && e[2] <= n ? a = [e[0] * e[1], e[2]] : e.length === 3 && e[0] <= n && e[1] * e[2] <= n ? a = [e[0], e[1] * e[2]] : e.length === 4 && e[0] * e[1] * e[2] <= n && e[3] <= n ? a = [e[0] * e[1] * e[2], e[3]] : e.length === 4 && e[0] <= n && e[1] * e[2] * e[3] <= n && (a = [e[0], e[1] * e[2] * e[3]]);
  const i = a != null && Math.max(...a) > r && Math.min(...a) <= (t ? 2 : 1) && Math.min(...a) > 0;
  if (a == null || i)
    if (t) {
      const c = Z(e);
      let s = 2, l = 2;
      e.length && ([s, l] = J(e)), o = c * (s / 2) * (l / 2), a = X(o).map((u) => u * 2);
    } else
      a = X(o);
  return a;
}
function B(e) {
  return e % 2 === 0;
}
function gt(e, t) {
  if (e = e.slice(-2), t = t.slice(-2), T(e, t) || !e.length || !t.length || e[0] === 0 || e[1] === 0 || t[0] === 0 || t[1] === 0)
    return !0;
  if (e.length !== t.length) {
    const n = e[e.length - 1], r = t[t.length - 1];
    if (n === r || B(n) && B(r) && (e[0] === 1 || t[0] === 1))
      return !0;
  }
  return e[1] === t[1] && B(e[0]) && B(t[0]);
}
let P, V;
function bt(e) {
  if (P == null) {
    const t = A(e);
    P = t.getParameter(t.MAX_TEXTURE_SIZE);
  }
  return P;
}
function kt(e) {
  if (V == null) {
    const t = A(e);
    V = t.getParameter(t.MAX_TEXTURE_IMAGE_UNITS);
  }
  return Math.min(16, V);
}
function Mt(e) {
  if (e === 0)
    return 0;
  let t;
  const n = A(e);
  return _(n, "EXT_disjoint_timer_query_webgl2") && e === 2 ? t = 2 : _(n, "EXT_disjoint_timer_query") ? t = 1 : t = 0, t;
}
function _(e, t) {
  return e.getExtension(t) != null;
}
function Bt(e) {
  try {
    if (A(e) != null)
      return !0;
  } catch (t) {
    return console.log("Error when getting WebGL context: ", t), !1;
  }
  return !1;
}
function yt(e) {
  if (e === 0)
    return !1;
  const t = A(e);
  if (e === 1) {
    if (!_(t, "OES_texture_float"))
      return !1;
  } else if (!_(t, "EXT_color_buffer_float"))
    return !1;
  return W(t);
}
function Pt(e) {
  if (e === 0)
    return !1;
  const t = A(e);
  if (e === 1) {
    if (!_(t, "OES_texture_float") || !_(t, "WEBGL_color_buffer_float"))
      return !1;
  } else {
    if (_(t, "EXT_color_buffer_float"))
      return W(t);
    const r = "EXT_color_buffer_half_float";
    if (_(t, r)) {
      const o = t.getExtension(r);
      return Ee(t, o);
    }
    return !1;
  }
  return W(t);
}
function W(e) {
  const t = K(e), n = e.createTexture();
  e.bindTexture(e.TEXTURE_2D, n), e.texImage2D(e.TEXTURE_2D, 0, t.internalFormatFloat, 1, 1, 0, t.textureFormatFloat, t.textureTypeFloat, null);
  const a = e.createFramebuffer();
  e.bindFramebuffer(e.FRAMEBUFFER, a), e.framebufferTexture2D(e.FRAMEBUFFER, e.COLOR_ATTACHMENT0, e.TEXTURE_2D, n, 0);
  const i = e.checkFramebufferStatus(e.FRAMEBUFFER) === e.FRAMEBUFFER_COMPLETE;
  return e.bindTexture(e.TEXTURE_2D, null), e.bindFramebuffer(e.FRAMEBUFFER, null), e.deleteTexture(n), e.deleteFramebuffer(a), i;
}
function Ee(e, t) {
  const n = K(e, t), r = e.createTexture();
  e.bindTexture(e.TEXTURE_2D, r), e.texImage2D(e.TEXTURE_2D, 0, n.internalFormatHalfFloat, 1, 1, 0, n.textureFormatFloat, n.textureTypeHalfFloat, null);
  const i = e.createFramebuffer();
  e.bindFramebuffer(e.FRAMEBUFFER, i), e.framebufferTexture2D(e.FRAMEBUFFER, e.COLOR_ATTACHMENT0, e.TEXTURE_2D, r, 0);
  const c = e.checkFramebufferStatus(e.FRAMEBUFFER) === e.FRAMEBUFFER_COMPLETE;
  return e.bindTexture(e.TEXTURE_2D, null), e.bindFramebuffer(e.FRAMEBUFFER, null), e.deleteTexture(r), e.deleteFramebuffer(i), c;
}
function Vt(e) {
  return e !== 2 ? !1 : A(e).fenceSync != null;
}
function Xt(e, t) {
  Array.isArray(e) || (e = [e]), e.forEach((n) => {
    n != null && ie(n.dtype !== "complex64", () => `${t} does not support complex64 tensors in the WebGL backend.`);
  });
}
function L() {
  let e, t, n, r, o, a, i, c, s, l;
  return $().getNumber("WEBGL_VERSION") === 2 ? (e = "#version 300 es", t = "in", n = "out", r = "in", o = "texture", a = "outputColor", i = "out vec4 outputColor;", c = $().getBool("WEBGL2_ISNAN_CUSTOM") ? `
      bool isnan_custom(float val) {
        uint floatToUint = floatBitsToUint(val);
        return (floatToUint & 0x7fffffffu) > 0x7f800000u;
      }

      bvec4 isnan_custom(vec4 val) {
        return bvec4(isnan_custom(val.x),
          isnan_custom(val.y), isnan_custom(val.z), isnan_custom(val.w));
      }

      #define isnan(value) isnan_custom(value)
    ` : "", s = "", l = `
      #define round(value) newRound(value)
      int newRound(float value) {
        return int(floor(value + 0.5));
      }

      ivec4 newRound(vec4 value) {
        return ivec4(floor(value + vec4(0.5)));
      }
    `) : (e = "", t = "attribute", n = "varying", r = "varying", o = "texture2D", a = "gl_FragColor", i = "", c = `
      #define isnan(value) isnan_custom(value)
      bool isnan_custom(float val) {
        return (val > 0. || val < 1. || val == 0.) ? false : true;
      }
      bvec4 isnan_custom(vec4 val) {
        return bvec4(isnan(val.x), isnan(val.y), isnan(val.z), isnan(val.w));
      }
    `, s = `
      uniform float INFINITY;

      bool isinf(float val) {
        return abs(val) == INFINITY;
      }
      bvec4 isinf(vec4 val) {
        return equal(abs(val), vec4(INFINITY));
      }
    `, l = `
      int round(float value) {
        return int(floor(value + 0.5));
      }

      ivec4 round(vec4 value) {
        return ivec4(floor(value + vec4(0.5)));
      }
    `), {
    version: e,
    attribute: t,
    varyingVs: n,
    varyingFs: r,
    texture2D: o,
    output: a,
    defineOutput: i,
    defineSpecialNaN: c,
    defineSpecialInf: s,
    defineRound: l
  };
}
function y(e, t, n = "index") {
  const r = k(t);
  return r.map((o, a) => {
    const i = `int ${e[a]} = ${n} / ${o}`, c = a === r.length - 1 ? `int ${e[a + 1]} = ${n} - ${e[a]} * ${o}` : `index -= ${e[a]} * ${o}`;
    return `${i}; ${c};`;
  }).join("");
}
function Q(e, t, n = "index") {
  const r = k(t);
  return r.map((o, a) => {
    const i = `int ${e[a]} = ${n} / outShapeStrides[${a}]`, c = a === r.length - 1 ? `int ${e[a + 1]} = ${n} - ${e[a]} * outShapeStrides[${a}]` : `index -= ${e[a]} * outShapeStrides[${a}]`;
    return `${i}; ${c};`;
  }).join("");
}
function Ce(e, t) {
  const n = e.length, r = e.map((a) => `${t}[${a}]`), o = new Array(n - 1);
  o[n - 2] = r[n - 1];
  for (let a = n - 3; a >= 0; --a)
    o[a] = `(${o[a + 1]} * ${r[a + 1]})`;
  return o;
}
function Wt(e, t, n = "index") {
  const r = e.map((a, i) => i), o = Ce(r, t);
  return o.map((a, i) => {
    const c = `int ${e[i]} = ${n} / ${o[i]}`, s = i === o.length - 1 ? `int ${e[i + 1]} = ${n} - ${e[i]} * ${o[i]}` : `index -= ${e[i]} * ${o[i]}`;
    return `${c}; ${s};`;
  }).join("");
}
function zt(e) {
  const t = k(e).map((n) => n.toString());
  return `
  int getFlatIndex(ivec3 coords) {
    return coords.x * ${t[0]} + coords.y * ${t[1]} + coords.z;
  }
`;
}
function Gt() {
  return `
  int getFlatIndex(ivec3 coords) {
    return coords.x * outShapeStrides[0] + coords.y * outShapeStrides[1] + coords.z;
  }
`;
}
const Ht = `
  const float FLOAT_MAX = 1.70141184e38;
  const float FLOAT_MIN = 1.17549435e-38;

  lowp vec4 encode_float(highp float v) {
    if (isnan(v)) {
      return vec4(255, 255, 255, 255);
    }

    highp float av = abs(v);

    if(av < FLOAT_MIN) {
      return vec4(0.0, 0.0, 0.0, 0.0);
    } else if(v > FLOAT_MAX) {
      return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;
    } else if(v < -FLOAT_MAX) {
      return vec4(0.0, 0.0,  128.0, 255.0) / 255.0;
    }

    highp vec4 c = vec4(0,0,0,0);

    highp float e = floor(log2(av));
    highp float m = exp2(fract(log2(av))) - 1.0;

    c[2] = floor(128.0 * m);
    m -= c[2] / 128.0;
    c[1] = floor(32768.0 * m);
    m -= c[1] / 32768.0;
    c[0] = floor(8388608.0 * m);

    highp float ebias = e + 127.0;
    c[3] = floor(ebias / 2.0);
    ebias -= c[3] * 2.0;
    c[2] += floor(ebias) * 128.0;

    c[3] += 128.0 * step(0.0, -v);

    return c / 255.0;
  }
`;
const { getBroadcastDims: ee } = se;
function Fe(e, t, n) {
  const r = [];
  if (e.forEach((x) => {
    const h = E(x.shapeInfo.logicalShape);
    if (x.shapeInfo.isUniform ? r.push(`uniform float ${x.name}${h > 1 ? `[${h}]` : ""};`) : (r.push(`uniform sampler2D ${x.name};`), r.push(`uniform int offset${x.name};`)), n.enableShapeUniforms) {
      const { uniformShape: p } = z(n.packedInputs, x.shapeInfo.logicalShape, x.shapeInfo.texShape);
      switch (p.length) {
        case 1:
          r.push(`uniform int ${x.name}Shape;`);
          break;
        case 2:
          r.push(`uniform ivec2 ${x.name}Shape;`);
          break;
        case 3:
          r.push(`uniform ivec3 ${x.name}Shape;`);
          break;
        case 4:
          r.push(`uniform ivec4 ${x.name}Shape;`);
          break;
      }
      r.push(`uniform ivec2 ${x.name}TexShape;`);
    }
  }), n.enableShapeUniforms) {
    switch (t.logicalShape.length) {
      case 1:
        r.push("uniform int outShape;");
        break;
      case 2:
        r.push("uniform ivec2 outShape;"), r.push("uniform int outShapeStrides;");
        break;
      case 3:
        r.push("uniform ivec3 outShape;"), r.push("uniform ivec2 outShapeStrides;");
        break;
      case 4:
        r.push("uniform ivec4 outShape;"), r.push("uniform ivec3 outShapeStrides;");
        break;
    }
    r.push("uniform ivec2 outTexShape;");
  }
  n.customUniforms && n.customUniforms.forEach((x) => {
    r.push(`uniform ${x.type} ${x.name}${x.arrayIndex ? `[${x.arrayIndex}]` : ""};`);
  });
  const o = r.join(`
`), a = e.map((x) => _e(x, t, n.packedInputs, n.enableShapeUniforms)).join(`
`), i = t.texShape, c = L(), s = Ne(c);
  let l, u, f = Le(c);
  return t.isPacked ? (l = Ae(t.logicalShape, i, n.enableShapeUniforms), u = Ie(c)) : (l = we(t.logicalShape, i, n.enableShapeUniforms), u = Oe(c)), n.packedInputs && (f += be), [
    f,
    s,
    u,
    o,
    l,
    a,
    n.userCode
  ].join(`
`);
}
function U(e, t = !1) {
  const n = e.shapeInfo.logicalShape;
  switch (n.length) {
    case 0:
      return qe(e, t);
    case 1:
      return Ye(e, t);
    case 2:
      return Ze(e, t);
    case 3:
      return Qe(e, t);
    case 4:
      return tt(e, t);
    case 5:
      return nt(e);
    case 6:
      return rt(e);
    default:
      throw new Error(`${n.length}-D input sampling is not yet supported`);
  }
}
function te(e, t) {
  switch (e.shapeInfo.logicalShape.length) {
    case 0:
      return He(e);
    case 1:
      return je(e, t);
    case 2:
      return Ke(e, t);
    case 3:
      return Je(e, t);
    default:
      return et(e, t);
  }
}
function _e(e, t, n = !1, r) {
  let o = "";
  n ? o += te(e, r) : o += U(e, r);
  const a = e.shapeInfo.logicalShape, i = t.logicalShape;
  return a.length <= i.length && (n ? o += ot(e, t) : o += at(e, t)), o;
}
function Ae(e, t, n) {
  switch (e.length) {
    case 0:
      return ne();
    case 1:
      return ke(e, t, n);
    case 2:
      return ze(e, t, n);
    case 3:
      return Be(e, t, n);
    default:
      return Pe(e, t, n);
  }
}
function we(e, t, n) {
  switch (e.length) {
    case 0:
      return ne();
    case 1:
      return Me(e, t, n);
    case 2:
      return Ge(e, t, n);
    case 3:
      return ye(e, t, n);
    case 4:
      return Ve(e, t, n);
    case 5:
      return Xe(e, t);
    case 6:
      return We(e, t);
    default:
      throw new Error(`${e.length}-D output sampling is not yet supported`);
  }
}
function Ne(e) {
  return `
    float sampleTexture(sampler2D textureSampler, vec2 uv) {
      return ${e.texture2D}(textureSampler, uv).r;
    }
  `;
}
function Oe(e) {
  return `
    void setOutput(float val) {
      ${e.output} = vec4(val, 0, 0, 0);
    }
  `;
}
function Ie(e) {
  return `
    void setOutput(vec4 val) {
      ${e.output} = val;
    }
  `;
}
function Le(e) {
  return `${e.version}
    precision highp float;
    precision highp int;
    precision highp sampler2D;
    ${e.varyingFs} vec2 resultUV;
    ${e.defineOutput}
    const vec2 halfCR = vec2(0.5, 0.5);

    struct ivec5
    {
      int x;
      int y;
      int z;
      int w;
      int u;
    };

    struct ivec6
    {
      int x;
      int y;
      int z;
      int w;
      int u;
      int v;
    };

    uniform float NAN;
    ${e.defineSpecialNaN}
    ${e.defineSpecialInf}
    ${e.defineRound}

    int imod(int x, int y) {
      return x - y * (x / y);
    }

    int idiv(int a, int b, float sign) {
      int res = a / b;
      int mod = imod(a, b);
      if (sign < 0. && mod != 0) {
        res -= 1;
      }
      return res;
    }

    //Based on the work of Dave Hoskins
    //https://www.shadertoy.com/view/4djSRW
    #define HASHSCALE1 443.8975
    float random(float seed){
      vec2 p = resultUV * seed;
      vec3 p3  = fract(vec3(p.xyx) * HASHSCALE1);
      p3 += dot(p3, p3.yzx + 19.19);
      return fract((p3.x + p3.y) * p3.z);
    }

    ${Ue}
    ${De}
    ${ge}
  `;
}
const Ue = `
vec2 uvFromFlat(int texNumR, int texNumC, int index) {
  int texR = index / texNumC;
  int texC = index - texR * texNumC;
  return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 packedUVfrom1D(int texNumR, int texNumC, int index) {
  int texelIndex = index / 2;
  int texR = texelIndex / texNumC;
  int texC = texelIndex - texR * texNumC;
  return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`, De = `
vec2 packedUVfrom2D(int texelsInLogicalRow, int texNumR,
  int texNumC, int row, int col) {
  int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2);
  int texR = texelIndex / texNumC;
  int texC = texelIndex - texR * texNumC;
  return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`, ge = `
vec2 packedUVfrom3D(int texNumR, int texNumC,
    int texelsInBatch, int texelsInLogicalRow, int b,
    int row, int col) {
  int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2);
  int texR = index / texNumC;
  int texC = index - texR * texNumC;
  return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`, be = `
  float getChannel(vec4 frag, vec2 innerDims) {
    vec2 modCoord = mod(innerDims, 2.);
    return modCoord.x == 0. ?
      (modCoord.y == 0. ? frag.r : frag.g) :
      (modCoord.y == 0. ? frag.b : frag.a);
  }
  float getChannel(vec4 frag, int dim) {
    float modCoord = mod(float(dim), 2.);
    return modCoord == 0. ? frag.r : frag.g;
  }
`;
function ne() {
  return `
    int getOutputCoords() {
      return 0;
    }
  `;
}
function ke(e, t, n) {
  const r = [Math.ceil(t[0] / 2), Math.ceil(t[1] / 2)];
  return r[0] === 1 ? n ? `
      int getOutputCoords() {
        return 2 * int(resultUV.x * ceil(float(outTexShape[1]) / 2.0));
      }
    ` : `
      int getOutputCoords() {
        return 2 * int(resultUV.x * ${r[1]}.0);
      }
    ` : r[1] === 1 ? n ? `
      int getOutputCoords() {
        return 2 * int(resultUV.y * ceil(float(outTexShape[0]) / 2.0));
      }
    ` : `
      int getOutputCoords() {
        return 2 * int(resultUV.y * ${r[0]}.0);
      }
    ` : n ? `
    int getOutputCoords() {
      ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(packedTexShape[0], packedTexShape[1]));
      return 2 * (resTexRC.x * packedTexShape[1] + resTexRC.y);
    }
  ` : `
    int getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${r[0]}, ${r[1]}));
      return 2 * (resTexRC.x * ${r[1]} + resTexRC.y);
    }
  `;
}
function Me(e, t, n) {
  return t[0] === 1 ? n ? `
      int getOutputCoords() {
        return int(resultUV.x * float(outTexShape[1]));
      }
    ` : `
      int getOutputCoords() {
        return int(resultUV.x * ${t[1]}.0);
      }
    ` : t[1] === 1 ? n ? `
      int getOutputCoords() {
        return int(resultUV.y * float(outTexShape[0]));
      }
    ` : `
      int getOutputCoords() {
        return int(resultUV.y * ${t[0]}.0);
      }
    ` : n ? `
    int getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(outTexShape[0], outTexShape[1]));
      return resTexRC.x * outTexShape[1] + resTexRC.y;
    }
  ` : `
    int getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${t[0]}, ${t[1]}));
      return resTexRC.x * ${t[1]} + resTexRC.y;
    }
  `;
}
function Be(e, t, n) {
  if (n)
    return `
    ivec3 getOutputCoords() {
      ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
      int texelsInLogicalRow = int(ceil(float(outShape[2]) / 2.0));
      int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[1]) / 2.0));
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(packedTexShape[0], packedTexShape[1]));
      int index = resTexRC.x * packedTexShape[1] + resTexRC.y;

      int b = index / texelsInBatch;
      index -= b * texelsInBatch;

      int r = 2 * (index / texelsInLogicalRow);
      int c = imod(index, texelsInLogicalRow) * 2;

      return ivec3(b, r, c);
    }
  `;
  const r = [Math.ceil(t[0] / 2), Math.ceil(t[1] / 2)], o = Math.ceil(e[2] / 2), a = o * Math.ceil(e[1] / 2);
  return `
    ivec3 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${r[0]}, ${r[1]}));
      int index = resTexRC.x * ${r[1]} + resTexRC.y;

      int b = index / ${a};
      index -= b * ${a};

      int r = 2 * (index / ${o});
      int c = imod(index, ${o}) * 2;

      return ivec3(b, r, c);
    }
  `;
}
function ye(e, t, n) {
  if (n)
    return `
  ivec3 getOutputCoords() {
    ivec2 resTexRC = ivec2(resultUV.yx *
                           vec2(outTexShape[0], outTexShape[1]));
    int index = resTexRC.x * outTexShape[1] + resTexRC.y;
    ${Q(["r", "c", "d"], e)}
    return ivec3(r, c, d);
  }
`;
  const r = y(["r", "c", "d"], e);
  return `
    ivec3 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${t[0]}, ${t[1]}));
      int index = resTexRC.x * ${t[1]} + resTexRC.y;
      ${r}
      return ivec3(r, c, d);
    }
  `;
}
function Pe(e, t, n) {
  if (n)
    return `
    ivec4 getOutputCoords() {
      ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(packedTexShape[0], packedTexShape[1]));
      int index = resTexRC.x * packedTexShape[1] + resTexRC.y;

      int texelsInLogicalRow = int(ceil(float(outShape[3]) / 2.0));
      int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[2]) / 2.0));
      int texelsInBatchN = texelsInBatch * outShape[1];

      int b2 = index / texelsInBatchN;
      index -= b2 * texelsInBatchN;

      int b = index / texelsInBatch;
      index -= b * texelsInBatch;

      int r = 2 * (index / texelsInLogicalRow);
      int c = imod(index, texelsInLogicalRow) * 2;

      return ivec4(b2, b, r, c);
    }
  `;
  const r = [Math.ceil(t[0] / 2), Math.ceil(t[1] / 2)], o = Math.ceil(e[e.length - 1] / 2), a = o * Math.ceil(e[e.length - 2] / 2);
  let i = a, c = "", s = "b, r, c";
  for (let l = 2; l < e.length - 1; l++)
    i *= e[e.length - l - 1], c = `
      int b${l} = index / ${i};
      index -= b${l} * ${i};
    ` + c, s = `b${l}, ` + s;
  return `
    ivec${e.length} getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${r[0]}, ${r[1]}));
      int index = resTexRC.x * ${r[1]} + resTexRC.y;

      ${c}

      int b = index / ${a};
      index -= b * ${a};

      int r = 2 * (index / ${o});
      int c = imod(index, ${o}) * 2;

      return ivec${e.length}(${s});
    }
  `;
}
function Ve(e, t, n) {
  if (n)
    return `
    ivec4 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
        vec2(outTexShape[0], outTexShape[1]));
      int index = resTexRC.x * outTexShape[1] + resTexRC.y;
      ${Q(["r", "c", "d", "d2"], e)}
      return ivec4(r, c, d, d2);
    }
  `;
  const r = y(["r", "c", "d", "d2"], e);
  return `
    ivec4 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
        vec2(${t[0]}, ${t[1]}));
      int index = resTexRC.x * ${t[1]} + resTexRC.y;
      ${r}
      return ivec4(r, c, d, d2);
    }
  `;
}
function Xe(e, t) {
  const n = y(["r", "c", "d", "d2", "d3"], e);
  return `
    ivec5 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx * vec2(${t[0]},
                             ${t[1]}));

      int index = resTexRC.x * ${t[1]} + resTexRC.y;

      ${n}

      ivec5 outShape = ivec5(r, c, d, d2, d3);
      return outShape;
    }
  `;
}
function We(e, t) {
  const n = y(["r", "c", "d", "d2", "d3", "d4"], e);
  return `
    ivec6 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
        vec2(${t[0]}, ${t[1]}));
      int index = resTexRC.x * ${t[1]} + resTexRC.y;

      ${n}

      ivec6 result = ivec6(r, c, d, d2, d3, d4);
      return result;
    }
  `;
}
function ze(e, t, n) {
  const r = [Math.ceil(t[0] / 2), Math.ceil(t[1] / 2)];
  if (T(e, t))
    return n ? `
      ivec2 getOutputCoords() {
        ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
        return 2 * ivec2(resultUV.yx * vec2(packedTexShape[0], packedTexShape[1]));
      }
    ` : `
      ivec2 getOutputCoords() {
        return 2 * ivec2(resultUV.yx * vec2(${r[0]}, ${r[1]}));
      }
    `;
  const o = Math.ceil(e[1] / 2);
  return n ? `
    ivec2 getOutputCoords() {
      ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
      int texelsInLogicalRow = int(ceil(float(outShape[1]) / 2.0));
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(packedTexShape[0], packedTexShape[1]));

      int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
      int r = 2 * (index / texelsInLogicalRow);
      int c = imod(index, texelsInLogicalRow) * 2;

      return ivec2(r, c);
    }
  ` : `
    ivec2 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${r[0]}, ${r[1]}));

      int index = resTexRC.x * ${r[1]} + resTexRC.y;
      int r = 2 * (index / ${o});
      int c = imod(index, ${o}) * 2;

      return ivec2(r, c);
    }
  `;
}
function Ge(e, t, n) {
  return T(e, t) ? n ? `
      ivec2 getOutputCoords() {
        return ivec2(resultUV.yx * vec2(outTexShape[0], outTexShape[1]));
      }
    ` : `
      ivec2 getOutputCoords() {
        return ivec2(resultUV.yx * vec2(${t[0]}, ${t[1]}));
      }
    ` : e[1] === 1 ? n ? `
      ivec2 getOutputCoords() {
        ivec2 resTexRC = ivec2(resultUV.yx *
                               vec2(outTexShape[0], outTexShape[1]));
        int index = resTexRC.x * outTexShape[1] + resTexRC.y;
        return ivec2(index, 0);
      }
    ` : `
      ivec2 getOutputCoords() {
        ivec2 resTexRC = ivec2(resultUV.yx *
                               vec2(${t[0]}, ${t[1]}));
        int index = resTexRC.x * ${t[1]} + resTexRC.y;
        return ivec2(index, 0);
      }
    ` : e[0] === 1 ? n ? `
      ivec2 getOutputCoords() {
        ivec2 resTexRC = ivec2(resultUV.yx *
                               vec2(outTexShape[0], outTexShape[1]));
        int index = resTexRC.x * outTexShape[1] + resTexRC.y;
        return ivec2(0, index);
      }
    ` : `
      ivec2 getOutputCoords() {
        ivec2 resTexRC = ivec2(resultUV.yx *
                               vec2(${t[0]}, ${t[1]}));
        int index = resTexRC.x * ${t[1]} + resTexRC.y;
        return ivec2(0, index);
      }
    ` : n ? `
    ivec2 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(outTexShape[0], outTexShape[1]));
      int index = resTexRC.x * outTexShape[1] + resTexRC.y;
      int r = index / outShape[1];
      int c = index - r * outShape[1];
      return ivec2(r, c);
    }
  ` : `
    ivec2 getOutputCoords() {
      ivec2 resTexRC = ivec2(resultUV.yx *
                             vec2(${t[0]}, ${t[1]}));
      int index = resTexRC.x * ${t[1]} + resTexRC.y;
      int r = index / ${e[1]};
      int c = index - r * ${e[1]};
      return ivec2(r, c);
    }
  `;
}
function O(e) {
  return `offset${e}`;
}
function He(e) {
  const t = e.name, n = "get" + t.charAt(0).toUpperCase() + t.slice(1), r = L();
  return `
    vec4 ${n}() {
      return ${r.texture2D}(${t}, halfCR);
    }
  `;
}
function qe(e, t) {
  const n = e.name, r = "get" + n.charAt(0).toUpperCase() + n.slice(1);
  if (e.shapeInfo.isUniform)
    return `float ${r}() {return ${n};}`;
  const [o, a] = e.shapeInfo.texShape;
  if (o === 1 && a === 1)
    return `
      float ${r}() {
        return sampleTexture(${n}, halfCR);
      }
    `;
  const i = O(n);
  if (t)
    return `
    float ${r}() {
      vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], ${i});
      return sampleTexture(${n}, uv);
    }
  `;
  const [c, s] = e.shapeInfo.texShape;
  return `
    float ${r}() {
      vec2 uv = uvFromFlat(${c}, ${s}, ${i});
      return sampleTexture(${n}, uv);
    }
  `;
}
function je(e, t) {
  const n = e.name, r = "get" + n.charAt(0).toUpperCase() + n.slice(1), o = e.shapeInfo.texShape, a = L();
  if (t)
    return `
    vec4 ${r}(int index) {
      ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
      vec2 uv = packedUVfrom1D(
        packedTexShape[0], packedTexShape[1], index);
      return ${a.texture2D}(${n}, uv);
    }
  `;
  const i = [Math.ceil(o[0] / 2), Math.ceil(o[1] / 2)];
  return `
    vec4 ${r}(int index) {
      vec2 uv = packedUVfrom1D(
        ${i[0]}, ${i[1]}, index);
      return ${a.texture2D}(${n}, uv);
    }
  `;
}
function Ye(e, t) {
  const n = e.name, r = "get" + n.charAt(0).toUpperCase() + n.slice(1);
  if (e.shapeInfo.isUniform)
    return `
      float ${r}(int index) {
        ${D(e)}
      }
    `;
  const o = e.shapeInfo.texShape, a = o[0], i = o[1];
  if (i === 1 && a === 1)
    return `
      float ${r}(int index) {
        return sampleTexture(${n}, halfCR);
      }
    `;
  const c = O(n);
  return i === 1 ? t ? `
      float ${r}(int index) {
        vec2 uv = vec2(0.5, (float(index + ${c}) + 0.5) / float(${n}TexShape[0]));
        return sampleTexture(${n}, uv);
      }
    ` : `
      float ${r}(int index) {
        vec2 uv = vec2(0.5, (float(index + ${c}) + 0.5) / ${a}.0);
        return sampleTexture(${n}, uv);
      }
    ` : a === 1 ? t ? `
      float ${r}(int index) {
        vec2 uv = vec2((float(index + ${c}) + 0.5) / float(${n}TexShape[1]), 0.5);
        return sampleTexture(${n}, uv);
      }
    ` : `
      float ${r}(int index) {
        vec2 uv = vec2((float(index + ${c}) + 0.5) / ${i}.0, 0.5);
        return sampleTexture(${n}, uv);
      }
    ` : t ? `
    float ${r}(int index) {
      vec2 uv = uvFromFlat(${n}TexShape[0], ${n}TexShape[1], index + ${c});
      return sampleTexture(${n}, uv);
    }
  ` : `
    float ${r}(int index) {
      vec2 uv = uvFromFlat(${a}, ${i}, index + ${c});
      return sampleTexture(${n}, uv);
    }
  `;
}
function Ke(e, t) {
  const n = e.shapeInfo.logicalShape, r = e.name, o = "get" + r.charAt(0).toUpperCase() + r.slice(1), a = e.shapeInfo.texShape, i = a[0], c = a[1], s = L();
  if (a != null && T(n, a))
    return t ? `
      vec4 ${o}(int row, int col) {
        vec2 uv = (vec2(col, row) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);

        return ${s.texture2D}(${r}, uv);
      }
    ` : `
      vec4 ${o}(int row, int col) {
        vec2 uv = (vec2(col, row) + halfCR) / vec2(${c}.0, ${i}.0);

        return ${s.texture2D}(${r}, uv);
      }
    `;
  if (t)
    return `
    vec4 ${o}(int row, int col) {
      ivec2 packedTexShape = ivec2(ceil(float(${r}TexShape[0]) / 2.0), ceil(float(${r}TexShape[1]) / 2.0));
      int valuesPerRow = int(ceil(float(${r}Shape[1]) / 2.0));
      vec2 uv = packedUVfrom2D(valuesPerRow, packedTexShape[0], packedTexShape[1], row, col);
      return ${s.texture2D}(${r}, uv);
    }
  `;
  const l = [Math.ceil(a[0] / 2), Math.ceil(a[1] / 2)], u = Math.ceil(n[1] / 2);
  return `
    vec4 ${o}(int row, int col) {
      vec2 uv = packedUVfrom2D(${u}, ${l[0]}, ${l[1]}, row, col);
      return ${s.texture2D}(${r}, uv);
    }
  `;
}
function Ze(e, t) {
  const n = e.shapeInfo.logicalShape, r = e.name, o = "get" + r.charAt(0).toUpperCase() + r.slice(1), a = e.shapeInfo.texShape;
  if (a != null && T(n, a)) {
    if (t)
      return `
      float ${o}(int row, int col) {
        vec2 uv = (vec2(col, row) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
        return sampleTexture(${r}, uv);
      }
    `;
    const d = a[0], x = a[1];
    return `
    float ${o}(int row, int col) {
      vec2 uv = (vec2(col, row) + halfCR) / vec2(${x}.0, ${d}.0);
      return sampleTexture(${r}, uv);
    }
  `;
  }
  const { newShape: i, keptDims: c } = N(n), s = i;
  if (s.length < n.length) {
    const d = g(e, s), x = ["row", "col"];
    return `
      ${U(d, t)}
      float ${o}(int row, int col) {
        return ${o}(${b(x, c)});
      }
    `;
  }
  if (e.shapeInfo.isUniform)
    return `
      float ${o}(int row, int col) {
        int index = round(dot(vec2(row, col), vec2(${n[1]}, 1)));
        ${D(e)}
      }
    `;
  const l = a[0], u = a[1], f = O(r);
  return u === 1 ? t ? `
      float ${o}(int row, int col) {
        float index = dot(vec3(row, col, ${f}), vec3(${r}Shape[1], 1, 1));
        vec2 uv = vec2(0.5, (index + 0.5) / float(${r}TexShape[0]));
        return sampleTexture(${r}, uv);
      }
    ` : `
    float ${o}(int row, int col) {
      float index = dot(vec3(row, col, ${f}), vec3(${n[1]}, 1, 1));
      vec2 uv = vec2(0.5, (index + 0.5) / ${l}.0);
      return sampleTexture(${r}, uv);
    }
  ` : l === 1 ? t ? `
      float ${o}(int row, int col) {
        float index = dot(vec3(row, col, ${f}), vec3(${r}Shape[1], 1, 1));
        vec2 uv = vec2((index + 0.5) / float(${r}TexShape[1]), 0.5);
        return sampleTexture(${r}, uv);
      }
    ` : `
    float ${o}(int row, int col) {
      float index = dot(vec3(row, col, ${f}), vec3(${n[1]}, 1, 1));
      vec2 uv = vec2((index + 0.5) / ${u}.0, 0.5);
      return sampleTexture(${r}, uv);
    }
  ` : t ? `
      float ${o}(int row, int col) {
        // Explicitly use integer operations as dot() only works on floats.
        int index = row * ${r}Shape[1] + col + ${f};
        vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index);
        return sampleTexture(${r}, uv);
      }
    ` : `
  float ${o}(int row, int col) {
    // Explicitly use integer operations as dot() only works on floats.
    int index = row * ${n[1]} + col + ${f};
    vec2 uv = uvFromFlat(${l}, ${u}, index);
    return sampleTexture(${r}, uv);
  }
`;
}
function Je(e, t) {
  const n = e.shapeInfo.logicalShape, r = e.name, o = "get" + r.charAt(0).toUpperCase() + r.slice(1), a = e.shapeInfo.texShape, i = [Math.ceil(a[0] / 2), Math.ceil(a[1] / 2)];
  if (n[0] === 1) {
    const d = n.slice(1), x = [1, 2], h = g(e, d), p = ["b", "row", "col"];
    return `
        ${te(h, t)}
        vec4 ${o}(int b, int row, int col) {
          return ${o}(${b(p, x)});
        }
      `;
  }
  const c = L();
  if (t)
    return `
    vec4 ${o}(int b, int row, int col) {
      ivec2 packedTexShape = ivec2(ceil(float(${r}TexShape[0]) / 2.0), ceil(float(${r}TexShape[1]) / 2.0));
      int valuesPerRow = int(ceil(float(${r}Shape[2]) / 2.0));
      int texelsInBatch = valuesPerRow * int(ceil(float(${r}Shape[1]) / 2.0));
      vec2 uv = packedUVfrom3D(
        packedTexShape[0], packedTexShape[1], texelsInBatch, valuesPerRow, b, row, col);
      return ${c.texture2D}(${r}, uv);
    }
  `;
  const s = i[0], l = i[1], u = Math.ceil(n[2] / 2), f = u * Math.ceil(n[1] / 2);
  return `
    vec4 ${o}(int b, int row, int col) {
      vec2 uv = packedUVfrom3D(
        ${s}, ${l}, ${f}, ${u}, b, row, col);
      return ${c.texture2D}(${r}, uv);
    }
  `;
}
function Qe(e, t) {
  const n = e.shapeInfo.logicalShape, r = e.name, o = "get" + r.charAt(0).toUpperCase() + r.slice(1), a = n[1] * n[2], i = n[2], { newShape: c, keptDims: s } = N(n), l = c;
  if (l.length < n.length) {
    const p = g(e, l), m = ["row", "col", "depth"];
    return `
        ${U(p, t)}
        float ${o}(int row, int col, int depth) {
          return ${o}(${b(m, s)});
        }
      `;
  }
  if (e.shapeInfo.isUniform)
    return `
      float ${o}(int row, int col, int depth) {
        int index = round(dot(vec3(row, col, depth),
                          vec3(${a}, ${i}, 1)));
        ${D(e)}
      }
    `;
  const u = e.shapeInfo.texShape, f = u[0], d = u[1], x = e.shapeInfo.flatOffset;
  if (d === a && x == null)
    return t ? `
      float ${o}(int row, int col, int depth) {
        int stride1 = ${r}Shape[2];
        float texR = float(row);
        float texC = dot(vec2(col, depth), vec2(stride1, 1));
        vec2 uv = (vec2(texC, texR) + halfCR) /
                   vec2(${r}TexShape[1], ${r}TexShape[0]);
        return sampleTexture(${r}, uv);
      }
    ` : `
        float ${o}(int row, int col, int depth) {
          float texR = float(row);
          float texC = dot(vec2(col, depth), vec2(${i}, 1));
          vec2 uv = (vec2(texC, texR) + halfCR) /
                     vec2(${d}.0, ${f}.0);
          return sampleTexture(${r}, uv);
        }
      `;
  if (d === i && x == null)
    return t ? `
      float ${o}(int row, int col, int depth) {
        float texR = dot(vec2(row, col), vec2(${r}Shape[1], 1));
        float texC = float(depth);
        vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${r}TexShape[1], ${r}TexShape[0]);
        return sampleTexture(${r}, uv);
      }
    ` : `
    float ${o}(int row, int col, int depth) {
      float texR = dot(vec2(row, col), vec2(${n[1]}, 1));
      float texC = float(depth);
      vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${d}.0, ${f}.0);
      return sampleTexture(${r}, uv);
    }
  `;
  const h = O(r);
  return t ? `
    float ${o}(int row, int col, int depth) {
      // Explicitly use integer operations as dot() only works on floats.
      int stride0 = ${r}Shape[1] * ${r}Shape[2];
      int stride1 = ${r}Shape[2];
      int index = row * stride0 + col * stride1 + depth + ${h};
      vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index);
      return sampleTexture(${r}, uv);
    }
    ` : `
      float ${o}(int row, int col, int depth) {
        // Explicitly use integer operations as dot() only works on floats.
        int index = row * ${a} + col * ${i} + depth + ${h};
        vec2 uv = uvFromFlat(${f}, ${d}, index);
        return sampleTexture(${r}, uv);
      }
  `;
}
function et(e, t) {
  const n = e.name, r = "get" + n.charAt(0).toUpperCase() + n.slice(1), o = L();
  if (t)
    return `
    vec4 ${r}(int b2, int b, int row, int col) {
      int valuesPerRow = int(ceil(float(${n}Shape[3]) / 2.0));
      int texelsInBatch = valuesPerRow * int(ceil(float(${n}Shape[2]) / 2.0));
      int index = b * texelsInBatch + (row / 2) * valuesPerRow + (col / 2);
      texelsInBatch *= ${n}Shape[1];
      index = b2 * texelsInBatch + index;
      ivec2 packedTexShape = ivec2(ceil(float(${n}TexShape[0]) / 2.0), ceil(float(${n}TexShape[1]) / 2.0));
      int texR = index / packedTexShape[1];
      int texC = index - texR * packedTexShape[1];
      vec2 uv = (vec2(texC, texR) + halfCR) / vec2(packedTexShape[1], packedTexShape[0]); return ${o.texture2D}(${n}, uv);
    }
  `;
  const a = e.shapeInfo.logicalShape, i = a.length, c = e.shapeInfo.texShape, s = [Math.ceil(c[0] / 2), Math.ceil(c[1] / 2)], l = s[0], u = s[1], f = Math.ceil(a[i - 1] / 2);
  let d = f * Math.ceil(a[i - 2] / 2), x = "int b, int row, int col", h = `b * ${d} + (row / 2) * ${f} + (col / 2)`;
  for (let p = 2; p < i - 1; p++)
    x = `int b${p}, ` + x, d *= a[i - p - 1], h = `b${p} * ${d} + ` + h;
  return `
    vec4 ${r}(${x}) {
      int index = ${h};
      int texR = index / ${u};
      int texC = index - texR * ${u};
      vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${u}, ${l});
      return ${o.texture2D}(${n}, uv);
    }
  `;
}
function tt(e, t) {
  const n = e.shapeInfo.logicalShape, r = e.name, o = "get" + r.charAt(0).toUpperCase() + r.slice(1), a = n[3], i = n[2] * a, c = n[1] * i, { newShape: s, keptDims: l } = N(n);
  if (s.length < n.length) {
    const R = g(e, s), F = ["row", "col", "depth", "depth2"];
    return `
      ${U(R, t)}
      float ${o}(int row, int col, int depth, int depth2) {
        return ${o}(${b(F, l)});
      }
    `;
  }
  if (e.shapeInfo.isUniform)
    return `
      float ${o}(int row, int col, int depth, int depth2) {
        int index = round(dot(vec4(row, col, depth, depth2),
                          vec4(${c}, ${i}, ${a}, 1)));
        ${D(e)}
      }
    `;
  const u = e.shapeInfo.flatOffset, f = e.shapeInfo.texShape, d = f[0], x = f[1], h = `int stride2 = ${r}Shape[3];`, p = `int stride1 = ${r}Shape[2] * stride2;`, m = `int stride0 = ${r}Shape[1] * stride1;`;
  if (x === c && u == null)
    return t ? `
      float ${o}(int row, int col, int depth, int depth2) {
        ${h}
        ${p}
        float texR = float(row);
        float texC =
            dot(vec3(col, depth, depth2),
                vec3(stride1, stride2, 1));
        vec2 uv = (vec2(texC, texR) + halfCR) /
                   vec2(${r}TexShape[1], ${r}TexShape[0]);
        return sampleTexture(${r}, uv);
      }
    ` : `
      float ${o}(int row, int col, int depth, int depth2) {
        float texR = float(row);
        float texC =
            dot(vec3(col, depth, depth2),
                vec3(${i}, ${a}, 1));
        vec2 uv = (vec2(texC, texR) + halfCR) /
                   vec2(${x}.0, ${d}.0);
        return sampleTexture(${r}, uv);
      }
    `;
  if (x === a && u == null)
    return t ? `
      float ${o}(int row, int col, int depth, int depth2) {
        float texR = dot(vec3(row, col, depth),
                         vec3(${r}Shape[1] * ${r}Shape[2], ${r}Shape[2], 1));
        float texC = float(depth2);
        vec2 uv = (vec2(texC, texR) + halfCR) /
                  vec2(${r}TexShape[1], ${r}TexShape[0]);
        return sampleTexture(${r}, uv);
      }
    ` : `
      float ${o}(int row, int col, int depth, int depth2) {
        float texR = dot(vec3(row, col, depth),
                         vec3(${n[1] * n[2]}, ${n[2]}, 1));
        float texC = float(depth2);
        vec2 uv = (vec2(texC, texR) + halfCR) /
                  vec2(${x}.0, ${d}.0);
        return sampleTexture(${r}, uv);
      }
    `;
  const S = O(r);
  return t ? `
    float ${o}(int row, int col, int depth, int depth2) {
      // Explicitly use integer operations as dot() only works on floats.
      ${h}
      ${p}
      ${m}
      int index = row * stride0 + col * stride1 +
          depth * stride2 + depth2;
      vec2 uv = uvFromFlat(${r}TexShape[0], ${r}TexShape[1], index + ${S});
      return sampleTexture(${r}, uv);
    }
  ` : `
    float ${o}(int row, int col, int depth, int depth2) {
      // Explicitly use integer operations as dot() only works on floats.
      int index = row * ${c} + col * ${i} +
          depth * ${a} + depth2;
      vec2 uv = uvFromFlat(${d}, ${x}, index + ${S});
      return sampleTexture(${r}, uv);
    }
  `;
}
function nt(e) {
  const t = e.shapeInfo.logicalShape, n = e.name, r = "get" + n.charAt(0).toUpperCase() + n.slice(1), o = t[4], a = t[3] * o, i = t[2] * a, c = t[1] * i, { newShape: s, keptDims: l } = N(t);
  if (s.length < t.length) {
    const p = g(e, s), m = ["row", "col", "depth", "depth2", "depth3"];
    return `
      ${U(p)}
      float ${r}(int row, int col, int depth, int depth2, int depth3) {
        return ${r}(${b(m, l)});
      }
    `;
  }
  if (e.shapeInfo.isUniform)
    return `
      float ${r}(int row, int col, int depth, int depth2, int depth3) {
        float index = dot(
          vec4(row, col, depth, depth2),
          vec4(${c}, ${i}, ${a}, ${o})) +
          depth3;
        ${D(e)}
      }
    `;
  const u = e.shapeInfo.flatOffset, f = e.shapeInfo.texShape, d = f[0], x = f[1];
  if (x === c && u == null)
    return `
      float ${r}(int row, int col, int depth, int depth2, int depth3) {
        int texR = row;
        float texC = dot(vec4(col, depth, depth2, depth3),
                         vec4(${i}, ${a}, ${o}, 1));
        vec2 uv = (vec2(texC, texR) + halfCR) /
                   vec2(${x}.0, ${d}.0);
        return sampleTexture(${n}, uv);
      }
    `;
  if (x === o && u == null)
    return `
      float ${r}(int row, int col, int depth, int depth2, int depth3) {
        float texR = dot(
          vec4(row, col, depth, depth2),
          vec4(${t[1] * t[2] * t[3]},
               ${t[2] * t[3]}, ${t[3]}, 1));
        int texC = depth3;
        vec2 uv = (vec2(texC, texR) + halfCR) /
                  vec2(${x}.0, ${d}.0);
        return sampleTexture(${n}, uv);
      }
    `;
  const h = O(n);
  return `
    float ${r}(int row, int col, int depth, int depth2, int depth3) {
      // Explicitly use integer operations as dot() only works on floats.
      int index = row * ${c} + col * ${i} + depth * ${a} +
          depth2 * ${o} + depth3 + ${h};
      vec2 uv = uvFromFlat(${d}, ${x}, index);
      return sampleTexture(${n}, uv);
    }
  `;
}
function rt(e) {
  const t = e.shapeInfo.logicalShape, n = e.name, r = "get" + n.charAt(0).toUpperCase() + n.slice(1), { newShape: o, keptDims: a } = N(t);
  if (o.length < t.length) {
    const m = g(e, o), S = ["row", "col", "depth", "depth2", "depth3", "depth4"];
    return `
      ${U(m)}
      float ${r}(int row, int col, int depth,
                    int depth2, int depth3, int depth4) {
        return ${r}(${b(S, a)});
      }
    `;
  }
  const i = t[5], c = t[4] * i, s = t[3] * c, l = t[2] * s, u = t[1] * l;
  if (e.shapeInfo.isUniform)
    return `
      float ${r}(int row, int col, int depth,
                  int depth2, int depth3, int depth4) {
        int index = round(dot(
          vec4(row, col, depth, depth2),
          vec4(${u}, ${l}, ${s}, ${c})) +
          dot(
            vec2(depth3, depth4),
            vec2(${i}, 1)));
        ${D(e)}
      }
    `;
  const f = e.shapeInfo.flatOffset, d = e.shapeInfo.texShape, x = d[0], h = d[1];
  if (h === u && f == null)
    return `
      float ${r}(int row, int col, int depth,
                    int depth2, int depth3, int depth4) {
        int texR = row;
        float texC = dot(vec4(col, depth, depth2, depth3),
          vec4(${l}, ${s}, ${c}, ${i})) +
               float(depth4);
        vec2 uv = (vec2(texC, texR) + halfCR) /
                   vec2(${h}.0, ${x}.0);
        return sampleTexture(${n}, uv);
      }
    `;
  if (h === i && f == null)
    return `
      float ${r}(int row, int col, int depth,
                    int depth2, int depth3, int depth4) {
        float texR = dot(vec4(row, col, depth, depth2),
          vec4(${t[1] * t[2] * t[3] * t[4]},
               ${t[2] * t[3] * t[4]},
               ${t[3] * t[4]},
               ${t[4]})) + float(depth3);
        int texC = depth4;
        vec2 uv = (vec2(texC, texR) + halfCR) /
                  vec2(${h}.0, ${x}.0);
        return sampleTexture(${n}, uv);
      }
    `;
  const p = O(n);
  return `
    float ${r}(int row, int col, int depth,
                  int depth2, int depth3, int depth4) {
      // Explicitly use integer operations as dot() only works on floats.
      int index = row * ${u} + col * ${l} + depth * ${s} +
          depth2 * ${c} + depth3 * ${i} + depth4 + ${p};
      vec2 uv = uvFromFlat(${x}, ${h}, index);
      return sampleTexture(${n}, uv);
    }
  `;
}
function D(e) {
  const t = e.name, n = E(e.shapeInfo.logicalShape);
  return n < 2 ? `return ${t};` : `
    for (int i = 0; i < ${n}; i++) {
      if (i == index) {
        return ${t}[i];
      }
    }
  `;
}
function ot(e, t) {
  const n = e.name, r = n.charAt(0).toUpperCase() + n.slice(1), o = "get" + r + "AtOutCoords", a = e.shapeInfo.logicalShape.length, i = t.logicalShape.length, c = ee(e.shapeInfo.logicalShape, t.logicalShape), s = re(i), l = i - a;
  let u;
  const f = ["x", "y", "z", "w", "u", "v"];
  a === 0 ? u = "" : i < 2 && c.length >= 1 ? u = "coords = 0;" : u = c.map((R) => `coords.${f[R + l]} = 0;`).join(`
`);
  let d = "";
  i < 2 && a > 0 ? d = "coords" : d = e.shapeInfo.logicalShape.map((R, F) => `coords.${f[F + l]}`).join(", ");
  let x = "return outputValue;";
  const p = E(e.shapeInfo.logicalShape) === 1, S = E(t.logicalShape) === 1;
  if (a === 1 && !p && !S)
    x = `
      return vec4(outputValue.xy, outputValue.xy);
    `;
  else if (p && !S)
    i === 1 ? x = `
        return vec4(outputValue.x, outputValue.x, 0., 0.);
      ` : x = `
        return vec4(outputValue.x);
      `;
  else if (c.length) {
    const R = a - 2, F = a - 1;
    c.indexOf(R) > -1 && c.indexOf(F) > -1 ? x = "return vec4(outputValue.x);" : c.indexOf(R) > -1 ? x = "return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);" : c.indexOf(F) > -1 && (x = "return vec4(outputValue.xx, outputValue.zz);");
  }
  return `
    vec4 ${o}() {
      ${s} coords = getOutputCoords();
      ${u}
      vec4 outputValue = get${r}(${d});
      ${x}
    }
  `;
}
function at(e, t) {
  const n = e.name, r = n.charAt(0).toUpperCase() + n.slice(1), o = "get" + r + "AtOutCoords", a = t.texShape, i = e.shapeInfo.texShape, c = e.shapeInfo.logicalShape.length, s = t.logicalShape.length;
  if (!e.shapeInfo.isUniform && c === s && e.shapeInfo.flatOffset == null && T(i, a))
    return `
      float ${o}() {
        return sampleTexture(${n}, resultUV);
      }
    `;
  const l = re(s), u = ee(e.shapeInfo.logicalShape, t.logicalShape), f = s - c;
  let d;
  const x = ["x", "y", "z", "w", "u", "v"];
  c === 0 ? d = "" : s < 2 && u.length >= 1 ? d = "coords = 0;" : d = u.map((p) => `coords.${x[p + f]} = 0;`).join(`
`);
  let h = "";
  return s < 2 && c > 0 ? h = "coords" : h = e.shapeInfo.logicalShape.map((p, m) => `coords.${x[m + f]}`).join(", "), `
    float ${o}() {
      ${l} coords = getOutputCoords();
      ${d}
      return get${r}(${h});
    }
  `;
}
function re(e) {
  if (e <= 1)
    return "int";
  if (e === 2)
    return "ivec2";
  if (e === 3)
    return "ivec3";
  if (e === 4)
    return "ivec4";
  if (e === 5)
    return "ivec5";
  if (e === 6)
    return "ivec6";
  throw Error(`GPU for rank ${e} is not yet supported`);
}
function z(e, t, n) {
  const { newShape: r, keptDims: o } = N(t), a = t.length, i = e && a === 3 && t[0] === 1, c = i ? t.slice(1) : r, s = !e && a > 1 && !T(t, n) && r.length < a || i;
  return { useSqueezeShape: s, uniformShape: s ? c : t, keptDims: o };
}
function g(e, t) {
  const n = JSON.parse(JSON.stringify(e));
  return n.shapeInfo.logicalShape = t, n;
}
function b(e, t) {
  return t.map((n) => e[n]).join(", ");
}
function qt(e, t, n, r) {
  const o = n.map((u, f) => {
    const d = {
      logicalShape: u.shape,
      texShape: u.isUniform ? null : u.texData.texShape,
      isUniform: u.isUniform,
      isPacked: u.isUniform ? !1 : u.texData.isPacked,
      flatOffset: null
    };
    return u.texData != null && u.texData.slice != null && u.texData.slice.flatOffset > 0 && (d.flatOffset = u.texData.slice.flatOffset), { name: t.variableNames[f], shapeInfo: d };
  }), a = o.map((u) => u.shapeInfo), i = {
    logicalShape: r.shape,
    texShape: r.texData.texShape,
    isUniform: !1,
    isPacked: r.texData.isPacked,
    flatOffset: null
  }, c = Fe(o, i, t), s = ve(e.gl, c), l = e.createProgram(s);
  return $().get("ENGINE_COMPILE_ONLY") ? {
    program: t,
    fragmentShader: s,
    source: c,
    webGLProgram: l,
    inShapeInfos: a,
    outShapeInfo: i,
    variablesLocations: null,
    customUniformLocations: null,
    infLoc: null,
    nanLoc: null,
    outShapeLocation: null,
    outShapeStridesLocation: null,
    outTexShapeLocation: null
  } : (e.buildVao(l), Object.assign({
    program: t,
    fragmentShader: s,
    source: c,
    webGLProgram: l,
    inShapeInfos: a,
    outShapeInfo: i
  }, it(e, t, l)));
}
function it(e, t, n) {
  const r = [], o = [];
  let a, i, c, s = null, l = null;
  l = e.getUniformLocation(n, "NAN", !1), $().getNumber("WEBGL_VERSION") === 1 && (s = e.getUniformLocation(n, "INFINITY", !1));
  const u = !1;
  for (const f of t.variableNames) {
    const d = {
      name: f,
      uniform: e.getUniformLocation(n, f, u),
      offset: e.getUniformLocation(n, `offset${f}`, u)
    };
    t.enableShapeUniforms && (d.shape = e.getUniformLocation(n, `${f}Shape`, u), d.texShape = e.getUniformLocation(n, `${f}TexShape`, u)), r.push(d);
  }
  if (t.enableShapeUniforms && (a = e.getUniformLocation(n, "outShape", u), c = e.getUniformLocation(n, "outShapeStrides", u), i = e.getUniformLocation(n, "outTexShape", u)), t.customUniforms)
    for (const f of t.customUniforms)
      o.push(e.getUniformLocation(n, f.name, u));
  return {
    variablesLocations: r,
    customUniformLocations: o,
    infLoc: s,
    nanLoc: l,
    outShapeLocation: a,
    outShapeStridesLocation: c,
    outTexShapeLocation: i
  };
}
function Y(e, t) {
  if (e.length !== t.length)
    throw Error(`Binary was compiled with ${e.length} inputs, but was executed with ${t.length} inputs`);
  e.forEach((n, r) => {
    const o = n.logicalShape, a = t[r], i = a.shape;
    if (!T(o, i))
      throw Error(`Binary was compiled with different shapes than the current args. Shapes ${o} and ${i} must match`);
    if (n.isUniform && a.isUniform)
      return;
    const c = n.texShape, s = a.isUniform ? null : a.texData.texShape;
    if (!T(c, s))
      throw Error(`Binary was compiled with different texture shapes than the current args. Shape ${c} and ${s} must match`);
  });
}
function jt(e, t, n, r, o) {
  t.program.enableShapeUniforms || (Y(t.inShapeInfos, n), Y([t.outShapeInfo], [r]));
  const a = r.texData.texture, i = r.texData.texShape;
  r.texData.isPacked ? e.setOutputPackedMatrixTexture(a.texture, i[0], i[1]) : e.setOutputMatrixTexture(a.texture, i[0], i[1]), e.setProgram(t.webGLProgram), e.bindVertexArray(t.webGLProgram.vao), $().getNumber("WEBGL_VERSION") === 1 && t.infLoc !== null && e.gl.uniform1f(t.infLoc, 1 / 0), t.nanLoc !== null && e.gl.uniform1f(t.nanLoc, NaN);
  for (let s = 0; s < n.length; ++s) {
    const l = n[s], { uniform: u, offset: f, shape: d, texShape: x } = t.variablesLocations[s];
    if (d) {
      const { uniformShape: h } = z(t.program.packedInputs, l.shape, l.texData.texShape);
      switch (h.length) {
        case 1:
          e.gl.uniform1iv(d, new Int32Array(h));
          break;
        case 2:
          e.gl.uniform2iv(d, new Int32Array(h));
          break;
        case 3:
          e.gl.uniform3iv(d, new Int32Array(h));
          break;
        case 4:
          e.gl.uniform4iv(d, new Int32Array(h));
          break;
      }
    }
    if (x && e.gl.uniform2i(x, l.texData.texShape[0], l.texData.texShape[1]), u != null) {
      if (l.isUniform) {
        if (E(l.shape) < 2)
          e.gl.uniform1f(u, l.uniformValues[0]);
        else {
          let h = l.uniformValues;
          h instanceof Float32Array || (h = new Float32Array(h)), e.gl.uniform1fv(u, h);
        }
        continue;
      }
      l.texData.slice != null && f != null && e.gl.uniform1i(f, l.texData.slice.flatOffset), e.setInputMatrixTexture(l.texData.texture.texture, u, s);
    }
  }
  const c = t.outShapeLocation;
  if (c)
    switch (r.shape.length) {
      case 1:
        e.gl.uniform1iv(c, new Int32Array(r.shape));
        break;
      case 2:
        e.gl.uniform2iv(c, new Int32Array(r.shape));
        break;
      case 3:
        e.gl.uniform3iv(c, new Int32Array(r.shape));
        break;
      case 4:
        e.gl.uniform4iv(c, new Int32Array(r.shape));
        break;
    }
  if (t.outShapeStridesLocation) {
    const s = k(r.shape);
    switch (r.shape.length) {
      case 2:
        e.gl.uniform1iv(t.outShapeStridesLocation, new Int32Array(s));
        break;
      case 3:
        e.gl.uniform2iv(t.outShapeStridesLocation, new Int32Array(s));
        break;
      case 4:
        e.gl.uniform3iv(t.outShapeStridesLocation, new Int32Array(s));
        break;
    }
  }
  if (t.outTexShapeLocation && e.gl.uniform2i(t.outTexShapeLocation, r.texData.texShape[0], r.texData.texShape[1]), t.program.customUniforms && o)
    for (let s = 0; s < t.program.customUniforms.length; ++s) {
      const l = t.program.customUniforms[s], u = t.customUniformLocations[s], f = o[s];
      if (l.type === "float")
        e.gl.uniform1fv(u, f);
      else if (l.type === "vec2")
        e.gl.uniform2fv(u, f);
      else if (l.type === "vec3")
        e.gl.uniform3fv(u, f);
      else if (l.type === "vec4")
        e.gl.uniform4fv(u, f);
      else if (l.type === "int")
        e.gl.uniform1iv(u, f);
      else if (l.type === "ivec2")
        e.gl.uniform2iv(u, f);
      else if (l.type === "ivec3")
        e.gl.uniform3iv(u, f);
      else if (l.type === "ivec4")
        e.gl.uniform4iv(u, f);
      else
        throw Error(`uniform type ${l.type} is not supported yet.`);
    }
  e.executeProgram();
}
function Yt(e, t, n) {
  let r = "";
  t.concat(n).forEach((i) => {
    const c = i.texData != null && i.texData.slice != null && i.texData.slice.flatOffset > 0;
    if (e.enableShapeUniforms && !i.isUniform) {
      const s = i.texData.texShape, { useSqueezeShape: l, uniformShape: u, keptDims: f } = z(e.packedInputs, i.shape, s);
      let d = "", x = "", h = "";
      if (u.length === 1 && e.packedInputs) {
        const I = [Math.ceil(s[0] / 2), Math.ceil(s[1] / 2)];
        d = `${I[0] > 1}_${I[1] > 1}`;
      } else if (u.length === 2 && !e.packedInputs)
        x = `${u[0] > 1}_${u[1] > 1}`;
      else if (u.length > 2 && !e.packedInputs) {
        const I = k(u);
        h = `${I[0] === s[1]}_${I[I.length - 1] === s[1]}`;
      }
      const p = i.shape.length, m = u.length === 2 && T(i.shape, s), S = E(i.shape) === 1, R = ce(i.shape, n.shape), F = !e.packedInputs && p === n.shape.length && T(s, n.texData.texShape), oe = e.packedInputs || u.length > 2 ? "" : `${s[0] > 1}_${s[1] > 1}`;
      r += `${p}_${F}_${l ? f : ""}_${u.length}_${S}_${R}_${m}_${d}_${x}_${h}_${oe}_${c}`;
    } else {
      const s = i.isUniform ? "uniform" : i.texData.texShape;
      r += `${i.shape}_${s}_${c}`;
    }
  });
  const o = e.userCode;
  let a = e.constructor.name;
  return a += "_" + r + "_" + o + `${$().getNumber("WEBGL_VERSION")}`, a;
}
function Kt(e) {
  return $().getBool("WEBGL_USE_SHAPES_UNIFORMS") && e <= 4;
}
export {
  jt as $,
  fe as A,
  ft as B,
  dt as C,
  A as D,
  Ht as E,
  pt as F,
  _ as G,
  Ft as H,
  K as I,
  It as J,
  $t as K,
  mt as L,
  Tt as M,
  At as N,
  wt as O,
  H as P,
  Nt as Q,
  Lt as R,
  Ot as S,
  q as T,
  ut as U,
  j as V,
  Ut as W,
  xt as X,
  ht as Y,
  Yt as Z,
  qt as _,
  zt as a,
  Dt as a0,
  me as a1,
  it as a2,
  Xt as a3,
  Wt as b,
  y as c,
  Z as d,
  J as e,
  re as f,
  Gt as g,
  Bt as h,
  gt as i,
  bt as j,
  kt as k,
  Mt as l,
  yt as m,
  Pt as n,
  Vt as o,
  L as p,
  Q as q,
  St as r,
  Rt as s,
  v as t,
  Kt as u,
  vt as v,
  _t as w,
  Ct as x,
  Et as y,
  lt as z
};
