在 WebGL 2 实例化中访问 gl_InstanceID

Access to gl_InstanceID in WebGL 2 Instancing

我正在尝试在 WebGL 2 中进行实例化。我想使用内置变量 gl_InstanceID 来索引统一的浮点数组。

我收到以下错误:

glDrawElementsInstancedANGLE: attempt to draw with all attributes having non-zero divisors

在 WebGL 2 实例化中是否只允许使用顶点属性(实例化数组)?

此外,spec 是了解这些功能的唯一权威场所吗?

根据下面的错误报告,该问题似乎已得到解决。这是一个小的工作示例

function main() {
  const gl = document.querySelector('canvas').getContext('webgl2');
  if (!gl) {
    return alert('need webgl2');
  }
  const vs = `#version 300 es
  void main() {
    float angle = float(gl_InstanceID) / 10.0 * 2.0 * radians(180.0);
    float radius = float(gl_VertexID + 1) / 4.0 * 0.8;
    gl_Position = vec4(vec2(sin(angle), cos(angle)) * radius, 0, 1);
    gl_PointSize = mix(5.0, 20.0, float(gl_VertexID) / 3.0);
  }
  `;

  const fs = `#version 300 es
  precision highp float;
  out vec4 foo;
  void main() {
    foo = vec4(1, 0, 0, 1);
  }
  `;
  
  const prg = twgl.createProgram(gl, [vs, fs]);
  gl.useProgram(prg);
  gl.drawArraysInstanced(gl.POINTS, 0, 4, 10);
}

main();
<canvas></canvas>
<script src="https://twgljs.org/dist/4.x/twgl.min.js"></script>

---旧答案---

spec says it's based on the OpenGL ES 3.0 spec

The remaining sections of this document are intended to be read in conjunction with the OpenGL ES 3.0 specification (3.0.4 at the time of this writing, available from the Khronos OpenGL ES API Registry). Unless otherwise specified, the behavior of each method is defined by the OpenGL ES 3.0 specification. This specification may diverge from OpenGL ES 3.0 in order to ensure interoperability or security, often defining areas that OpenGL ES 3.0 leaves implementation-defined. These differences are summarized in the Differences Between WebGL and OpenGL ES 3.0 section.

不幸的是,他们似乎忘记指定至少一个属性必须具有非零除数,这与 OpenGL ES 3.0 不同。 I filed a bug

需要补充的部分是

INVALID_OPERATION is generated by DrawArraysInstanced or DrawElementsInstanced if there is not at least one enabled vertex attribute array that has a divisor of zero and is bound to an active generic attribute value in the program used for the draw command.