Delete VBOs at run time?

Started by
6 comments, last by Neosettler 11 years, 2 months ago

Greetings GL Masters,

I recently run my application with gDEBugger GL: http://www.gremedy.com/download.php

I was chock to my very core that all these years, I had video memory leaks. After endless efforts, I managed to find the source of the leaks. All I needed to do was to match every glGenBuffers with glDeleteBuffers and my life was peachy again.

each VBO looks somewhat like this:

glGenBuffers(1, &l_id1);
glBindBuffer(GL_ARRAY_BUFFER...
glBufferData(GL_ARRAY_BUFFER...
glBufferSubData(GL_ARRAY_BUFFER...

glGenBuffers(1, &l_id2);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER...
glBufferData(GL_ELEMENT_ARRAY_BUFFER...

glDeleteBuffers(1, &l_id1)
glDeleteBuffers(1, &l_id2)

The problem is, all the while this is working fine when opening and closing the API. Deleting buffers at run time makes the next draw call ends with an access violation.

I cant find any relevant info on how to properly delete VBOs buffers at run time so far. Any wisdom of the ancestral would be very welcome.

Thx

Advertisement

Deleting buffers at run time makes the next draw call ends with an access violation.

This one makes me seriously wonder what you're doing.

Are you creating and deleting the buffer objects for every frame? That is a very big no-no. What you need to do is create them once and delete them when you're done with them, at the end of the game, or at the end of the level, or whenever you don't need the objects anymore. But don't create and recreate them on a per-frame basis.

I can also interpret your question as a serious ownership-problem; you are deleting objects that shouldn't be deleted in the first place because you are still using them. In that case, you should read up on resource ownership and what it means with something owning a resource and controlling when it's being deleted. In no case should a resource be deleted that you either need or have a replacement for (such as a dummy resource that you can use instead of the actual resource isn't available).

Thank you for your input Bob, I used this to delete the VBO now:

glBindBuffer(GL_ARRAY_BUFFER, in_id);
for (UInt i = 0; i < 16; ++i)
{
glDisableVertexAttribArray(i);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &in_id);

The crash is gone but I'm still not sure if this is the right way of doing it.

There's no problems with the way you're deleting the buffer objects. I'm questioning where and when you're doing it.

There's no problems with the way you're deleting the buffer objects. I'm questioning where and when you're doing it.


Likewise I would ask this question. I don't mean to sound insulting, and I may be very wrong here, but you may be someone who has previously only ever used immediate mode, has received advice to use buffer objects instead, but is trying to use buffer objects in the same way as you used to use immediate mode - i.e. specify vertex data on the fly and on a fairly ad-hoc basis. If that's the case then you've got a deeper design problem that you also need to resolve, as well as a requirement to better understand how buffer objects are supposed to be used.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

My apologies, I find pasting code here very painful, I guess I tried cutting corners in my explanation but my code
looks more like this:

void OpenGL::SetVertexBuffer(VertexBuffer *in_buffer)

{

UInt &l_id = in_buffer->GetArrayId();


if (l_id == 0)

{

glGenBuffers(1, &l_id);

}

glBindBuffer(GL_ARRAY_BUFFER, l_id);


if (in_buffer->IsUpdated())

{

glBufferData(GL_ARRAY_BUFFER, in_buffer->GetArraySize(), NULL, gl_BufferTypes[in_buffer->GetType()]);

}

}


template <class T> void OpenGL::SetVertexData(ArrayBuffer<T> &in_array)

{

if (in_array.IsValid())

{

glBufferSubData(GL_ARRAY_BUFFER, in_array.GetOffset(), in_array.GetSize(), in_array.GetData());

}

}


template <class T> void OpenGL::SetVertexAttribute(ArrayBuffer<T> &in_array)

{

if (in_array.IsValid())

{

e_VertexAttributes &l_index = in_array.GetIndex();

glVertexAttribPointer(l_index, gl_AttributeSizes[l_index], gl_AttributeTypes[l_index], gl_AttributeNormalized[l_index], gl_AttributeStrides[l_index], (void*) in_array.GetOffset());


glEnableVertexAttribArray(l_index);

}

else

glDisableVertexAttribArray(in_array.GetIndex());

}


template <class T> void OpenGL::Draw(VertexBuffer *in_buffer, ArrayBuffer<T> &in_indices, UInt in_offset, const e_RenderTypes &in_type)

{

UInt &l_id = in_buffer->GetElementId();


if (l_id == 0)

{

glGenBuffers(1, &l_id);

}

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, l_id);


if (in_buffer->IsUpdated())

{

in_buffer->SetUpdated(false);

glBufferData(GL_ELEMENT_ARRAY_BUFFER, in_indices.GetSize(), in_indices.GetData(), gl_BufferTypes[in_buffer->GetType()]); /// Only if reseted.

}

glDrawRangeElements(gl_RenderTypes[in_type], in_offset, in_offset + in_indices.GetSize(), in_indices.GetSize(), GL_UNSIGNED_INT, (void*) in_indices.GetOffset());

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

}

If my understanding is correct, unbinding any subData attached to the buffers was my missing link before deletion at run time.

void OpenGL::DeleteVertexBuffer(UInt &in_id)
{
if (in_id != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, in_id);

for (UInt i = 0; i < 16; ++i)
{
glDisableVertexAttribArray(i);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &in_id);
in_id = 0;
}
}

Your code looks very suspicions. Why are you checking the validity of the object and uploading data almost everywhere in the code? I don't see why, for example, the draw function should have to be bothered in the first place with allocating buffer object names or uploading data anywhere. Shouldn't the draw function just be responsible for drawing a buffer, and nothing else?

With name allocation and buffer data uploads in almost every single function, it's going to be very difficult to track down incorrect usage. It sounds like the fundamental design itself is wrong somewhere, and that requires a fundamental design change to the code as well.

Why are you checking the validity of the object and uploading data almost everywhere in the code?

The validity check comes from:

- In the old days, resizing a window was emptying the video memory. (Not a valid argument anymore)

- I'm using a mixture of SFML and QT and for some reason, it does prevent me from creating buffer at initialization of my API as there is a switch GL context or some funny stuff similar to this. (I will start by investigate this.)

- I have the option of resetting programs at run time. (I'll make re-initialization on reset() instead.)

The uploading data is rather tricky, as my approach supports multi-materials, I have 2 different techniques that I would like to debate:

- Geometries hold vertex attributes and meshes.

- Meshes hold face ids.

For each geometry:

1 - One VBO for the vertex attributes and one VBO for each mesh(i).

2 - One VBO for the vertex attributes and one VBO for all mesh(i) using buffer offset.

Surprisingly, number 2 is giving better performance even if it has to call subData every draw.

To resume, You are right, I'm in the process of redesign and I'd love some insights.

This topic is closed to new replies.

Advertisement