Handmade Hero»Forums»Code
107 posts
indexbuffer or not for sprites/textured quads?
Edited by C_Worm on

Hello, im wondering if I should bother using a elementbuffer/indexbuffer for my sprite batch in my renderer, or just use the VBO.

If im not using indexbuffer, that means that each textured quad has to use 6 vertices.

But with the indexbuffer each textured quad will require 4 vertices and 6 indices (u32's).

So this this is a map of the number of datatypes required if im not wrong?


f = float
i = int

No indexbuffer (1 textures quad):
ff ff ffff ff ff
ff ff ffff ff ff
ff ff ffff ff ff
ff ff ffff ff ff
ff ff ffff ff ff
ff ff ffff ff ff

With indexbuffer (1 textures quad):
ff ff ffff ff ff
ff ff ffff ff ff
ff ff ffff ff ff
ff ff ffff ff ff
iii iii

f = float i = int

No indexbuffer (1 textures quad): ff ff ffff ff ff ff ff ffff ff ff ff ff ffff ff ff ff ff ffff ff ff ff ff ffff ff ff ff ff ffff ff ff

With indexbuffer (1 textures quad): ff ff ffff ff ff ff ff ffff ff ff ff ff ffff ff ff ff ff ffff ff ff iii iii [/code]

So clearly using and indexbuffer is worth the job in terms of memory usage, right?

Simon Anciaux
1340 posts
indexbuffer or not for sprites/textured quads?

As far as I know (I'm not an expert) there are no advantages to using indices except for less memory bandwidth required. So it's up to you to choose what work for your case.

To save more memory you could use a single u32 for color instead of 4 floats (you'll need to unpack it in the shader). You can use u16 indices instead of u32 if you need less then 65535 vertices per VBO, and use triangle strip to save 1 index (you'll need 4 indices + 1 primitive restart value).

Mārtiņš Možeiko
2561 posts / 2 projects
indexbuffer or not for sprites/textured quads?
Edited by Mārtiņš Možeiko on

If you have very complex vertex shader, then index buffer helps a lot, because GPU caches vertex shader output - so if same index comes in, then it can directly use previous output without running full vertex shader again.

To save more memory you could use a single u32 for color instead of 4 floats (you'll need to unpack it in the shader).

There is nothing to unpack. GPU will do that for you. In OpenGL you do

glVertexAttribPointer(color_index, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, offset);

And GPU will unpack 4 bytes with range from 0..255 to vec4 float with range 0..1 automatically.

Simon Anciaux
1340 posts
indexbuffer or not for sprites/textured quads?
Replying to mmozeiko (#25556)

I didn't knew about that, thanks.