Home  >  Q&A  >  body text

WebGL does not support index drawing using index 255

<p>This is the simplest WebGL test program, which creates an index buffer with only one uint8 value of 255. It should draw a red square of size 64px, but it doesn't (the index value doesn't matter for drawing). </p> <p>Somehow WebGL ignores the element at index 255 even though it is in the unsigned byte range. Using 254 or other values, the red square appears as expected. </p> <p>Is this a bug in WebGL or expected behavior? I can't find any relevant information. </p> <pre class="brush:php;toolbar:false;"><canvas width="800" height="600"></canvas> <script type="module"> let canvas = document.querySelector("canvas"); let gl = canvas.getContext("webgl2", {antialias: false}); let vert = gl.createShader(gl.VERTEX_SHADER); let frag = gl.createShader(gl.FRAGMENT_SHADER); let prog = gl.createProgram(); let index_buf = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buf); gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, new Uint8Array([255]), gl.STATIC_DRAW ); gl.shaderSource(vert, `#version 300 es void main() { gl_Position = vec4(pos, 1); gl_PointSize = 64.0; } `); gl.shaderSource(frag, `#version 300 es precision highp float; out vec4 color; void main() { color = vec4(1,0,0,1); } `); gl.compileShader(vert); gl.compileShader(frag); gl.attachShader(prog, vert); gl.attachShader(prog, frag); gl.linkProgram(prog); gl.clearColor(0, 0, 0, 1); gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(prog); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, index_buf); gl.drawElements(gl.POINTS, 1, gl.UNSIGNED_BYTE, 0); </script></pre> <p><br /></p>
P粉265724930P粉265724930450 days ago448

reply all(1)I'll reply

  • P粉476883986

    P粉4768839862023-08-18 00:14:57

    View WebGL 2.0 Specification - PRIMITIVE_RESTART_FIXED_INDEX is always enabled :

    Therefore, if the index type is UNSIGNED_BYTE, index 255 is the original restart index and cannot be used to index vertices.

    reply
    0
  • Cancelreply