Opengl, DrawArrays without binding VBO
Asked Answered
U

1

9

I am rendering array of points with a custom vertex shader. Shaders looks like:

void mainVP()
in varying int    in_vertex_id : VERTEXID
{
    foo(in_vertex_id);
}

So the only thing I need - is vertex id. But I need a lot of vertices and I don't want to store fake VBO for them (it takes around 16mb of memory).

I tried to run my code without binding any VBO. It works. So my rendering looks like:

size_t num_vertices = ...
glDrawArrays(GL_POINTS, 0, num_vertices);

But can I be sure that rendering without binding VBO is safe?

Uniparous answered 7/11, 2011 at 17:8 Comment(0)
L
14

But can I be sure that rendering without binding VBO is safe?

You can't.

The OpenGL specification's core profile (3.2 and above) clearly states that it should be allowed, that you can render with all attributes disabled. The OpenGL specification's compatibility profile or any versions before 3.2 just as clearly state that you cannot do this.

Of course, that doesn't matter anyway. NVIDIA drivers allow you to do this on any OpenGL version and profile. ATI's drivers don't allow you to do it on any OpenGL version or profile. They're both driver bugs, just in different ways.

You'll just have to accept that you need a dummy vertex attribute. However:

But I need a lot of vertices and I don't want to store fake VBO for them (it takes around 16mb of memory).

A dummy attribute would take up 4 bytes (a single float, or a 4-vector of normalized bytes. Remember: you don't care about the data). So you could fit 4 million of them in 16MB.

Alternatively, you could use instanced rendering via glDrawArraysInstanced. There, you just render one vertex, but with num_vertices instances. Your shader will have to use the instance ID, of course.

Liles answered 7/11, 2011 at 19:26 Comment(4)
glDrawArraysInstanced - great advice! Checked it today. It works. For 4 million vertices speed is the same as glDrawArrays. Thanks.Uniparous
Is this information outdated? I've just tried a program that renders an empty VAO on a Radeon, and it worked fine, just like on an Nvidia GPU. Targeting OpenGL 4.3 / GLSL 430.Adorl
@Vandroiy: It could be. It's been more than 5 years since I checked. But what about Intel?Liles
I just ran it on an Intel GPU; didn't see any issue there either. If I understand this correctly, the problem would occur if I create a new VAO, bind it, do not call glEnableVertexAttribArray, but do call glDrawArrays? Not sure if I'm misunderstanding something. But it wouldn't be too surprising if it simply works; OpenGL support has generally improved in recent years.Adorl

© 2022 - 2024 — McMap. All rights reserved.