Advertisement Jump to content
Sign in to follow this  
Deliverance

OpenGL Dynamic VAO jerky animation

This topic is 1803 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

 

This is my first post in a long time on the forums. I've kind of missed the discussions here (:
 

I'm working on a small side scroller demo on the iOS platform where the terrain and all the world elements are generated procedurally based on the player position and camera orientation (geometry generation is done based on the camera frustum -  so only what's visible is generated)

I'm using VAO's with dynamic VBO and everything is generated and renders fine. 

 

I'm seeing some jerky animation from time to time and I'm wondering if there's something wrong with the dynamic vbo implementation. I'm using a round robin buffer where the render frame is maxnumber back from the update frame. 

So if I have 3 buffers, the current render buffer is 0, while the buffers that get updated are 1 and 2. 

The render buffer is incremented every frame. To be honest, I've skipped low level OpenGL programming a lot lately and I may miss obvious things.

I'm currently testing on an iPhone 4 device with OpenGL ES 2.0.

Here's the code:
 

class VertexArrayObject
{
private:
#if ENABLE_VAO == 1
    GLuint VAO;
#endif
    static const int numBuffers = 3;
    GLuint buffers[numBuffers];
    GLsync fences[numBuffers];
    
    int currBuffIndex;
public:
    
    int currentRenderFrame()
    {
        return currBuffIndex;
    }
    
    int currentUpdateFrame()
    {
        return (currentRenderFrame() + numBuffers + 1) % numBuffers;
    }
    
    void create()
    {
#if ENABLE_VAO == 1
        glGenVertexArraysOES(1, &VAO);
        glBindVertexArrayOES(VAO);
#endif
        for (int i=0; i<numBuffers; i++)
        {
            glGenBuffers(1, &buffers[i]);
            glBindBuffer(GL_ARRAY_BUFFER, buffers[i]);
            
            // allocate a fixed size buffer, 512 vertices
            glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * 512, 0, GL_DYNAMIC_DRAW);
            
            glEnableVertexAttribArray(GLKVertexAttribPosition);
            glEnableVertexAttribArray(GLKVertexAttribNormal);
            glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
            
            glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
            
            glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float)*3));
            
            glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float)*6));
        }
        
        glBindVertexArrayOES(0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);

        currBuffIndex = 0;
    }
    
    void bind()
    {
#if ENABLE_VAO == 1
        glBindVertexArrayOES(VAO);
#else
        glBindBuffer(GL_ARRAY_BUFFER, buffers[currentRenderFrame()]);
        
        glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
        glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float)*3));
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*) (sizeof(float)*6));
#endif
    }
    
    void uploadData(float *data, int sizeInBytes)
    {
        const int buffer_number = currentUpdateFrame();
        
        // Wait until buffer is free to use, in most cases this should not wait // because we are using three buffers in chain, glClientWaitSync
        // function can be used for check if the TIMEOUT is zero
        GLenum result = glClientWaitSyncAPPLE(fences[buffer_number], 0, 1.0f / 30.0f);
        if (result == GL_TIMEOUT_EXPIRED_APPLE || result == GL_WAIT_FAILED_APPLE) {
            // Something is wrong
            
            NSLog(@"GL_TIMEOUT_EXPIRED_APPLE");
        }
        glDeleteSyncAPPLE(fences[buffer_number]);
        
        glBindBuffer(GL_ARRAY_BUFFER, buffers[buffer_number]);
        
        void *ptr = glMapBufferRangeEXT(GL_ARRAY_BUFFER, 0, sizeInBytes, GL_MAP_WRITE_BIT_EXT | GL_MAP_UNSYNCHRONIZED_BIT_EXT);
        
        memcpy(ptr, data, sizeInBytes);
        glUnmapBufferOES(GL_ARRAY_BUFFER); // Use buffer in draw operation
        
        // Put fence into command queue
        fences[buffer_number] = glFenceSyncAPPLE(GL_SYNC_GPU_COMMANDS_COMPLETE_APPLE, 0);
        
        currBuffIndex = (1 + currBuffIndex) % numBuffers;
    }
    
    void unbind()
    {
#if ENABLE_VAO == 1
        glBindVertexArrayOES(0);
#endif
    }
    
    void free()
    {
        for (int i=0; i<numBuffers; i++)
        {
            glDeleteBuffers(1, &buffers[i]); buffers[i] = 0;
        }
        
#if ENABLE_VAO == 1
        glDeleteVertexArraysOES(1, &VAO); VAO = 0;
#endif
        
    }
};
Edited by Deliverance

Share this post


Link to post
Share on other sites
Advertisement

-Try to identify the slow part by using glFinish() and a performance counter.

-Try glBufferSubData(...)

-check for errors

-unbind the buffers after using them. glBindBuffer( GL_ARRAY_BUFFER, 0);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!