text: import {Program} from "../../../../../webgl/Program.js";
  import {createRTCViewMat, getPlaneRTCPos} from "../../../../../math/rtcCoords.js";
  import {math} from "../../../../../math/math.js";
  import {WEBGL_INFO} from "../../../../../webglInfo.js";
  
  const tempVec3a = math.vec3();
  
  /**
   * @private
   */
  class PointsInstancingPickDepthRenderer {
  
      constructor(scene) {
          this._scene = scene;
          this._hash = this._getHash();
          this._allocate();
      }
  
      getValid() {
          return this._hash === this._getHash();
      };
  
      _getHash() {
          return this._scene._sectionPlanesState.getHash() + this._scene.pointsMaterial.hash;
      }
  
      drawLayer(frameCtx, instancingLayer, renderPass) {
  
          const model = instancingLayer.model;
          const scene = model.scene;
          const gl = scene.canvas.gl;
          const state = instancingLayer._state;
          const instanceExt = this._instanceExt;
          const rtcCenter = instancingLayer._state.rtcCenter;
          const pointsMaterial = scene.pointsMaterial._state;
  
          if (!this._program) {
              this._allocate(instancingLayer);
              if (this.errors) {
                  return;
              }
          }
  
          if (frameCtx.lastProgramId !== this._program.id) {
              frameCtx.lastProgramId = this._program.id;
              this._bindProgram();
          }
  
          const camera = scene.camera;
  
          gl.uniform1i(this._uRenderPass, renderPass);
  
          gl.uniform1i(this._uPickInvisible, frameCtx.pickInvisible);
  
          const pickViewMatrix = frameCtx.pickViewMatrix || camera.viewMatrix;
          const rtcPickViewMatrix = (rtcCenter) ? createRTCViewMat(pickViewMatrix, rtcCenter) : pickViewMatrix;
  
          gl.uniformMatrix4fv(this._uViewMatrix, false, rtcPickViewMatrix);
          gl.uniformMatrix4fv(this._uWorldMatrix, false, model.worldMatrix);
  
          gl.uniformMatrix4fv(this._uProjMatrix, false, frameCtx.pickProjMatrix);
  
          gl.uniform1f(this._uPickZNear, frameCtx.pickZNear);
          gl.uniform1f(this._uPickZFar, frameCtx.pickZFar);
  
          if (scene.logarithmicDepthBufferEnabled) {
              const logDepthBufFC = 2.0 / (Math.log(frameCtx.pickZFar + 1.0) / Math.LN2); // TODO: Far from pick project matrix
              gl.uniform1f(this._uLogDepthBufFC, logDepthBufFC);
          }
  
          const numSectionPlanes = scene._sectionPlanesState.sectionPlanes.length;
          if (numSectionPlanes > 0) {
              const sectionPlanes = scene._sectionPlanesState.sectionPlanes;
              const baseIndex = instancingLayer.layerIndex * numSectionPlanes;
              const renderFlags = model.renderFlags;
              for (let sectionPlaneIndex = 0; sectionPlaneIndex < numSectionPlanes; sectionPlaneIndex++) {
                  const sectionPlaneUniforms = this._uSectionPlanes[sectionPlaneIndex];
                  if (sectionPlaneUniforms) {
                      const active = renderFlags.sectionPlanesActivePerLayer[baseIndex + sectionPlaneIndex];
                      gl.uniform1i(sectionPlaneUniforms.active, active ? 1 : 0);
                      if (active) {
                          const sectionPlane = sectionPlanes[sectionPlaneIndex];
                          if (rtcCenter) {
                              const rtcSectionPlanePos = getPlaneRTCPos(sectionPlane.dist, sectionPlane.dir, rtcCenter, tempVec3a);
                              gl.uniform3fv(sectionPlaneUniforms.pos, rtcSectionPlanePos);
                          } else {
                              gl.uniform3fv(sectionPlaneUniforms.pos, sectionPlane.pos);
                          }
                          gl.uniform3fv(sectionPlaneUniforms.dir, sectionPlane.dir);
                      }
                  }
              }
          }
  
          gl.uniformMatrix4fv(this._uPositionsDecodeMatrix, false, instancingLayer._state.positionsDecodeMatrix);
  
          this._aModelMatrixCol0.bindArrayBuffer(state.modelMatrixCol0Buf);
          this._aModelMatrixCol1.bindArrayBuffer(state.modelMatrixCol1Buf);
          this._aModelMatrixCol2.bindArrayBuffer(state.modelMatrixCol2Buf);
  
          instanceExt.vertexAttribDivisorANGLE(this._aModelMatrixCol0.location, 1);
          instanceExt.vertexAttribDivisorANGLE(this._aModelMatrixCol1.location, 1);
          instanceExt.vertexAttribDivisorANGLE(this._aModelMatrixCol2.location, 1);
  
          this._aPosition.bindArrayBuffer(state.positionsBuf);
  
          this._aFlags.bindArrayBuffer(state.flagsBuf);
          instanceExt.vertexAttribDivisorANGLE(this._aFlags.location, 1);
  
          if (this._aFlags2) {
              this._aFlags2.bindArrayBuffer(state.flags2Buf);
              instanceExt.vertexAttribDivisorANGLE(this._aFlags2.location, 1);
          }
  
          if (this._aOffset) {
              this._aOffset.bindArrayBuffer(state.offsetsBuf);
              instanceExt.vertexAttribDivisorANGLE(this._aOffset.location, 1);
          }
  
          gl.uniform1f(this._uPointSize, pointsMaterial.pointSize);
          const nearPlaneHeight = (scene.camera.projection === "ortho") ? 1.0 : (gl.drawingBufferHeight / (2 * Math.tan(0.5 * scene.camera.perspective.fov * Math.PI / 180.0)));
          gl.uniform1f(this._uNearPlaneHeight, nearPlaneHeight);
  
          instanceExt.drawArraysInstancedANGLE(gl.POINTS, 0, state.positionsBuf.numItems, state.numInstances);
  
          // Cleanup
  
          instanceExt.vertexAttribDivisorANGLE(this._aModelMatrixCol0.location, 0);
          instanceExt.vertexAttribDivisorANGLE(this._aModelMatrixCol1.location, 0);
          instanceExt.vertexAttribDivisorANGLE(this._aModelMatrixCol2.location, 0);
  
          instanceExt.vertexAttribDivisorANGLE(this._aFlags.location, 0);
          if (this._aFlags2) { // Won't be in shader when not clipping
              instanceExt.vertexAttribDivisorANGLE(this._aFlags2.location, 0);
          }
  
          if (this._aOffset) {
              instanceExt.vertexAttribDivisorANGLE(this._aOffset.location, 0);
          }
      }
  
      _allocate() {
  
          const scene = this._scene;
          const gl = scene.canvas.gl;
  
          this._program = new Program(gl, this._buildShader());
  
          if (this._program.errors) {
              this.errors = this._program.errors;
              return;
          }
  
          this._instanceExt = gl.getExtension("ANGLE_instanced_arrays");
  
          const program = this._program;
  
          this._uRenderPass = program.getLocation("renderPass");
          this._uPickInvisible = program.getLocation("pickInvisible");
          this._uPositionsDecodeMatrix = program.getLocation("positionsDecodeMatrix");
          this._uWorldMatrix = program.getLocation("worldMatrix");
          this._uViewMatrix = program.getLocation("viewMatrix");
          this._uProjMatrix = program.getLocation("projMatrix");
          this._uSectionPlanes = [];
  
          for (let i = 0, len = scene._sectionPlanesState.sectionPlanes.length; i < len; i++) {
              this._uSectionPlanes.push({
                  active: program.getLocation("sectionPlaneActive" + i),
                  pos: program.getLocation("sectionPlanePos" + i),
                  dir: program.getLocation("sectionPlaneDir" + i)
              });
          }
  
          this._aPosition = program.getAttribute("position");
          this._aOffset = program.getAttribute("offset");
          this._aFlags = program.getAttribute("flags");
          this._aFlags2 = program.getAttribute("flags2");
          this._aModelMatrixCol0 = program.getAttribute("modelMatrixCol0");
          this._aModelMatrixCol1 = program.getAttribute("modelMatrixCol1");
          this._aModelMatrixCol2 = program.getAttribute("modelMatrixCol2");
  
          this._uPickZNear = program.getLocation("pickZNear");
          this._uPickZFar = program.getLocation("pickZFar");
  
          this._uPointSize = program.getLocation("pointSize");
          this._uNearPlaneHeight = program.getLocation("nearPlaneHeight");
  
          if (scene.logarithmicDepthBufferEnabled) {
              this._uLogDepthBufFC = program.getLocation("logDepthBufFC");
          }
      }
  
      _bindProgram() {
          this._program.bind();
  
      }
  
      _buildShader() {
          return {
              vertex: this._buildVertexShader(),
              fragment: this._buildFragmentShader()
          };
      }
  
      _buildVertexShader() {
          const scene = this._scene;
          const sectionPlanesState = scene._sectionPlanesState;
          const clipping = sectionPlanesState.sectionPlanes.length > 0;
          const pointsMaterial = scene.pointsMaterial._state;
          const src = [];
  
          src.push("// Points instancing pick depth vertex shader");
  
          if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
              src.push("#extension GL_EXT_frag_depth : enable");
          }
  
          src.push("uniform int renderPass;");
  
          src.push("attribute vec3 position;");
  
          if (scene.entityOffsetsEnabled) {
              src.push("attribute vec3 offset;");
          }
  
          src.push("attribute vec4 flags;");
          src.push("attribute vec4 flags2;");
          src.push("attribute vec4 modelMatrixCol0;"); // Modeling matrix
          src.push("attribute vec4 modelMatrixCol1;");
          src.push("attribute vec4 modelMatrixCol2;");
  
          src.push("uniform bool pickInvisible;");
          src.push("uniform mat4 worldMatrix;");
          src.push("uniform mat4 viewMatrix;");
          src.push("uniform mat4 projMatrix;")
          src.push("uniform mat4 positionsDecodeMatrix;");
  
          src.push("uniform float pointSize;");
          if (pointsMaterial.perspectivePoints) {
              src.push("uniform float nearPlaneHeight;");
          }
  
          if (scene.logarithmicDepthBufferEnabled) {
              src.push("uniform float logDepthBufFC;");
              if (WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
                  src.push("varying float vFragDepth;");
              }
          }
  
          if (clipping) {
              src.push("varying vec4 vWorldPosition;");
              src.push("varying vec4 vFlags2;");
          }
  
          src.push("varying vec4 vViewPosition;");
          src.push("void main(void) {");
  
          // flags.w = NOT_RENDERED | PICK
          // renderPass = PICK
  
          src.push(`if (int(flags.w) != renderPass) {`);
          src.push("   gl_Position = vec4(0.0, 0.0, 0.0, 0.0);"); // Cull vertex
  
          src.push("} else {");
          src.push("  vec4 worldPosition = positionsDecodeMatrix * vec4(position, 1.0); ");
          src.push("  worldPosition = worldMatrix * vec4(dot(worldPosition, modelMatrixCol0), dot(worldPosition, modelMatrixCol1), dot(worldPosition, modelMatrixCol2), 1.0);");
  
          if (scene.entityOffsetsEnabled) {
              src.push("      worldPosition.xyz = worldPosition.xyz + offset;");
          }
          src.push("  vec4 viewPosition  = viewMatrix * worldPosition; ");
          if (clipping) {
              src.push("  vWorldPosition = worldPosition;");
              src.push("  vFlags2 = flags2;");
          }
          src.push("  vViewPosition = viewPosition;");
          src.push("vec4 clipPos = projMatrix * viewPosition;");
          if (scene.logarithmicDepthBufferEnabled) {
              if (WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
                  src.push("vFragDepth = 1.0 + clipPos.w;");
              } else {
                  src.push("clipPos.z = log2( max( 1e-6, clipPos.w + 1.0 ) ) * logDepthBufFC - 1.0;");
                  src.push("clipPos.z *= clipPos.w;");
              }
          }
          src.push("gl_Position = clipPos;");
          if (pointsMaterial.perspectivePoints) {
              src.push("gl_PointSize = (nearPlaneHeight * pointSize) / clipPos.w;");
              src.push("gl_PointSize = max(gl_PointSize, " + Math.floor(pointsMaterial.minPerspectivePointSize) + ".0);");
              src.push("gl_PointSize = min(gl_PointSize, " + Math.floor(pointsMaterial.maxPerspectivePointSize) + ".0);");
          } else {
              src.push("gl_PointSize = pointSize;");
          }
          src.push("}");
          src.push("}");
          return src;
      }
  
      _buildFragmentShader() {
          const scene = this._scene;
          const sectionPlanesState = scene._sectionPlanesState;
          const clipping = sectionPlanesState.sectionPlanes.length > 0;
          const src = [];
          src.push("// Points instancing pick depth fragment shader");
  
          if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
              src.push("#extension GL_EXT_frag_depth : enable");
          }
  
          src.push("#ifdef GL_FRAGMENT_PRECISION_HIGH");
          src.push("precision highp float;");
          src.push("precision highp int;");
          src.push("#else");
          src.push("precision mediump float;");
          src.push("precision mediump int;");
          src.push("#endif");
  
          if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
              src.push("uniform float logDepthBufFC;");
              src.push("varying float vFragDepth;");
          }
  
          src.push("uniform float pickZNear;");
          src.push("uniform float pickZFar;");
  
          if (clipping) {
              src.push("varying vec4 vWorldPosition;");
              src.push("varying vec4 vFlags2;");
              for (let i = 0; i < sectionPlanesState.sectionPlanes.length; i++) {
                  src.push("uniform bool sectionPlaneActive" + i + ";");
                  src.push("uniform vec3 sectionPlanePos" + i + ";");
                  src.push("uniform vec3 sectionPlaneDir" + i + ";");
              }
          }
          src.push("varying vec4 vViewPosition;");
          src.push("vec4 packDepth(const in float depth) {");
          src.push("  const vec4 bitShift = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);");
          src.push("  const vec4 bitMask  = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);");
          src.push("  vec4 res = fract(depth * bitShift);");
          src.push("  res -= res.xxyz * bitMask;");
          src.push("  return res;");
          src.push("}");
          src.push("void main(void) {");
  
          if (scene.pointsMaterial.roundPoints) {
              src.push("  vec2 cxy = 2.0 * gl_PointCoord - 1.0;");
              src.push("  float r = dot(cxy, cxy);");
              src.push("  if (r > 1.0) {");
              src.push("       discard;");
              src.push("  }");
          }
  
          if (clipping) {
              src.push("  bool clippable = (float(vFlags2.x) > 0.0);");
              src.push("  if (clippable) {");
              src.push("  float dist = 0.0;");
              for (let i = 0; i < sectionPlanesState.sectionPlanes.length; i++) {
                  src.push("if (sectionPlaneActive" + i + ") {");
                  src.push("   dist += clamp(dot(-sectionPlaneDir" + i + ".xyz, vWorldPosition.xyz - sectionPlanePos" + i + ".xyz), 0.0, 1000.0);");
                  src.push("}");
              }
              src.push("if (dist > 0.0) { discard; }");
              src.push("}");
          }
          if (scene.logarithmicDepthBufferEnabled && WEBGL_INFO.SUPPORTED_EXTENSIONS["EXT_frag_depth"]) {
              src.push("gl_FragDepthEXT = log2( vFragDepth ) * logDepthBufFC * 0.5;");
          }
          src.push("    float zNormalizedDepth = abs((pickZNear + vViewPosition.z) / (pickZFar - pickZNear));");
          src.push("    gl_FragColor = packDepth(zNormalizedDepth); "); // Must be linear depth
          src.push("}");
          return src;
      }
  
      webglContextRestored() {
          this._program = null;
      }
  
      destroy() {
          if (this._program) {
              this._program.destroy();
          }
          this._program = null;
      }
  }
  
  export {PointsInstancingPickDepthRenderer};