Results 1 to 4 of 4

Thread: glDrawArrays race condition

  1. #1
    Junior Member
    Join Date
    Oct 2008
    Posts
    2

    glDrawArrays race condition

    Hello everybody,

    I'm experiencing a strange problem with glDrawArrays()on the Ipod Touch (OS version 2.0, OGL ES 1.1), and while I think I've already found a working solution, I still don't really understand why it actually works. I was hoping somebody could clarify the following for me:

    Code :
    void Foo::drawSome()
    {
    	GLfloat *vertices = new GLfloat[strLength*12];
    	GLfloat	*coordinates = new GLfloat[strLength*18];
     
    	// fill with correct verts and coords...
     
    	// draw stuff
    	glPushMatrix();
     
    	glTranslatef(x, y, 0.0f);
     
    	glBindTexture(GL_TEXTURE_2D, m_fontTex.name);
    	glVertexPointer(3, GL_FLOAT, 0, vertices);
    	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
    	glDrawArrays(GL_TRIANGLES, 0, strLength*6);
     
    	glPopMatrix();
     
    	// here it crashes, basically complaining that I'm freeing memory still in use	
    	delete [] vertices;
    	delete [] coordinates;
    }

    The crash occurs as soon as I reach a certain amount of verts. Obviously the arrays are still in use when I try to free the memory.

    The solution is to use static arrays like this:
    Code :
    void Foo::drawSome()
    {
    	GLfloat vertices[256*18];
    	GLfloat coordinates[256*12];
     
    	// fill with correct verts and coords...
     
    	// draw stuff
    	glPushMatrix();
     
    	glTranslatef(x, y, 0.0f);
     
    	glBindTexture(GL_TEXTURE_2D, m_fooTex.name);
    	glVertexPointer(3, GL_FLOAT, 0, vertices);
    	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
    	glDrawArrays(GL_TRIANGLES, 0, strLength*6);
     
    	glPopMatrix();	
    }

    I don't understand why this works and dynamically allocated arrays don't. Shouldn't the static arrays be gone as soon as the function returns?
    So what's the real difference? Does the data on the stack live longer? Long enough for it to work?

    Please explain...

  2. #2
    Member
    Join Date
    Sep 2003
    Location
    Silicon Valley
    Posts
    57

    Re: glDrawArrays race condition

    I don't know the guts of their implementation, so I'm just speculating: it's possible that delete[] can detect that the GL driver is still using that memory, while stack memory is freed just by adjusting the stack pointer, which might not be detected and/or detectable.

    It's risky to do either, since the only way to know that the driver is done with the vertices you pass in is to do a server round trip such as glFinish() - which itself may kill performance, as may dynamic allocations on every call. Especially if the arrays are as tiny as your code suggests.

    I suggest you instead look at using either vertex buffer objects, or client memory that doesn't get reallocated every time you call your draw routine.
    Jon Leech
    EGL & OpenGL Specification Editor / OpenGL ARB Ecosystem TSG Chair / Khronos API Registrar

  3. #3
    Junior Member
    Join Date
    Oct 2008
    Posts
    2

    Re: glDrawArrays race condition

    Quote Originally Posted by oddhack
    I don't know the guts of their implementation, so I'm just speculating: it's possible that delete[] can detect that the GL driver is still using that memory, while stack memory is freed just by adjusting the stack pointer, which might not be detected and/or detectable.

    It's risky to do either, since the only way to know that the driver is done with the vertices you pass in is to do a server round trip such as glFinish() - which itself may kill performance, as may dynamic allocations on every call. Especially if the arrays are as tiny as your code suggests.

    I suggest you instead look at using either vertex buffer objects, or client memory that doesn't get reallocated every time you call your draw routine.
    Well, I have to admit that I didn't read the specification in detail. So glDrawArrays() returning doesn't guarantee that the vertex data has been transfered to the GL?

  4. #4
    Member
    Join Date
    Sep 2003
    Location
    Silicon Valley
    Posts
    57

    Re: glDrawArrays race condition

    Quote Originally Posted by weak
    Well, I have to admit that I didn't read the specification in detail. So glDrawArrays() returning doesn't guarantee that the vertex data has been transfered to the GL?
    According to the GL spec, data binding should happen when you call DrawArrays. But the error you report is consistent with the implementation not actually doing this, and implementations have been known to do a lot of wacky things (I'm envisioning something like a command buffer that just stores a command token and pointer value, that doesn't get emptied by the driver until well after you call delete[] / return from your draw routine).

    Whether or not that's happening, allocating vertex buffer memory in your inner drawing loop is still something to avoid.
    Jon Leech
    EGL & OpenGL Specification Editor / OpenGL ARB Ecosystem TSG Chair / Khronos API Registrar

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2013, 03:46 AM
  2. Condition in accordance with OpenMAX
    By 4osuke in forum OpenMAX
    Replies: 1
    Last Post: 03-30-2009, 03:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •