Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Dynamic VAO jerky animation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Deliverance   Members   -  Reputation: 309

Like
0Likes
Like

Posted 10 February 2014 - 03:20 AM

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, 10 February 2014 - 03:21 AM.


Sponsor:

#2 Aliii   Members   -  Reputation: 1447

Like
0Likes
Like

Posted 10 February 2014 - 05:33 AM

-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);






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS