GLDRAWELEMENTSINSTAN(3G)

πŸ‘‘ GLDRAWELEMENTSINSTAN(3G) πŸ‘‘

okay so this is a page from the [FIXME: manual] situation β€” yes bestie, the manual name literally says FIXME, we don't ask questions πŸ’€ β€” and it's all about one extremely specific OpenGL function that lets you draw the same pile of elements a whole bunch of times, except every round gets its own little offset for instanced attributes. iykyk ✨


🌸 NAME 🌸

glDrawElementsInstancedBaseInstance β€” draws multiple instances of a set of elements, with offset applied to instanced attributes. it's giving "one draw call, many vibes" πŸ’…


πŸŽ€ C SPECIFICATION πŸŽ€

void glDrawElementsInstancedBaseInstance(GLenum mode, GLsizei count,
                                         GLenum type,
                                         const void * indices,
                                         GLsizei primcount,
                                         GLuint baseinstance);

πŸ¦‹ PARAMETERS πŸ¦‹

  • mode β€” tells the GPU what kind of primitives to render. the accepted symbolic constants are GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_LINE_STRIP_ADJACENCY, GL_LINES_ADJACENCY, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_TRIANGLE_STRIP_ADJACENCY, GL_TRIANGLES_ADJACENCY and GL_PATCHES. that's the whole guest list, no plus-ones πŸ’–
  • count β€” how many elements you're rendering. the headcount, basically.
  • type β€” the type of the values sitting in indices. has to be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT. pick wrong and it's not gonna be cute πŸ”₯
  • indices β€” a pointer to the location where the indices are stored. where the lore lives.
  • primcount β€” the number of instances of the specified range of indices to be rendered. yes, we're doing this multiple times. main character energy πŸŽ€
  • baseinstance β€” the base instance used when fetching instanced vertex attributes. it's a little offset with a big job 🫢

✨ DESCRIPTION ✨

so glDrawElementsInstancedBaseInstance behaves identically to glDrawElements(), except that primcount instances of the set of elements are executed, and the value of the internal counter instanceID advances for each iteration. instanceID is an internal 32-bit integer counter that a vertex shader can read as gl_InstanceID. literally the shader's little window into which instance it's currently on 😭

glDrawElementsInstancedBaseInstance has the same effect as this whole situation right here:

if (mode, count, or type is invalid )
    generate appropriate error
else {
    for (int i = 0; i < primcount ; i++) {
        instanceID = i;
        glDrawElements(mode, count, type, indices);
    }
    instanceID = 0;
}

specific vertex attributes may be classified as instanced through the use of glVertexAttribDivisor(). instanced vertex attributes supply per-instance vertex data to the vertex shader. the index of the vertex fetched from the enabled instanced vertex attribute arrays is calculated as gl _ InstanceID divisor + baseInstance. ⚠️ and note bestie: baseinstance does not affect the shader-visible value of gl_InstanceID. it's a sneaky little thing that only touches the attribute fetching, not the counter itself πŸ™ˆ


πŸ’œ NOTES πŸ’œ

  • glDrawElementsInstancedBaseInstance is available only if the GL version is 4.2 or greater. no 4.2, no function, no exceptions πŸ’€
  • GL_LINE_STRIP_ADJACENCY, GL_LINES_ADJACENCY, GL_TRIANGLE_STRIP_ADJACENCY and GL_TRIANGLES_ADJACENCY are available only if the GL version is 3.2 or greater. so those four have their own whole entry requirement, iconic behavior honestly πŸ‘‘

πŸ”₯ ERRORS πŸ”₯

  • GL_INVALID_ENUM is generated if mode is not one of GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, or GL_TRIANGLES. (notice this list is shorter than the parameter list β€” yes, we're being precise, don't @ me ✨)
  • GL_INVALID_VALUE is generated if count or primcount are negative. negative. nope. absolutely not 🚫
  • GL_INVALID_OPERATION is generated if a geometry shader is active and mode is incompatible with the input primitive type of the geometry shader in the currently installed program object. geometry shader said "that's not my type" πŸ’…
  • GL_INVALID_OPERATION is generated if a non-zero buffer object name is bound to an enabled array and the buffer object's data store is currently mapped. mapped memory and drawing are not on speaking terms rn 😭

🌈 VERSION SUPPORT 🌈

the feature name is glDrawElementsInstancedBaseInstance β€” here's the vibe check across OpenGL versions, βœ” meaning supported and - meaning nope:

  • OpenGL 2.0 β€” -
  • OpenGL 2.1 β€” -
  • OpenGL 3.0 β€” -
  • OpenGL 3.1 β€” -
  • OpenGL 3.2 β€” -
  • OpenGL 3.3 β€” -
  • OpenGL 4.0 β€” -
  • OpenGL 4.1 β€” -
  • OpenGL 4.2 β€” βœ”
  • OpenGL 4.3 β€” βœ”
  • OpenGL 4.4 β€” βœ”
  • OpenGL 4.5 β€” βœ”

so like… it shows up at 4.2 and then never leaves. consistent queen πŸŽ€


πŸ‹ SEE ALSO πŸ‹

  • glDrawElements()
  • glDrawArraysInstanced()

go read those next, bestie, they're the supporting cast 🫢


Copyright Β© 2011-2014 Khronos Group. This material may be distributed subject to the terms and conditions set forth in the Open Publication License, v 1.0, 8 June 1999. http://opencontent.org/openpub/.

Copyright Β© 2011-2014 Khronos Group


ok that's the whole page, no skips, no cap πŸ’–

and finally, the sign-off: this beauty came from [FIXME: source] and it's dated 08/09/2021, so go forth and instance responsibly, you absolute icon πŸ§šβ€β™€οΈπŸ’œ

πŸ“¦ where this page comes from