Gamua / Sparrow-Framework

The Open Source Game Engine for iOS
http://www.sparrow-framework.org
Other
294 stars 83 forks source link

Method for creating shader program outside render time so we can create ... #30

Closed TwoPints closed 10 years ago

TwoPints commented 10 years ago

...the program before creating a VAO as the attributes need setting before we can call glVertexAttribPointer etc. Probably needs implementing a bit better than this.

PrimaryFeather commented 10 years ago

Could you give me a little more information about what you're trying to achieve? Maybe a sample code of how you want to call that additional method? (And why you can't call 'prepareToDraw' instead.) Just so that I know exactly was this is about, and can make the change in a way that's most useful. Thanks!

TwoPints commented 10 years ago

prepareToDraw works, it however has the extra overhead of needless gl calls, and the function name itself implies something in this context that you're not doing.

This is code within a classes init: method so not called within a render. The suggestion I'm asking for is just a separation of concerns, so the body within prepareForRender can call to generateProgram (or a more appropriate name), I should have done that as part of the pull request in hindsight to make it more clear. As is, it also has the problem of having to have a valid texture to create a textured program, even though it's not necessarily used for a while, just cached.

The more I think about it, the more the pull request needs changing. :)

for (int i=0; i<NUM_VERTEX_VBO_BUFFERS; ++i) {
                baseEffect[i] = [[SPBaseEffect alloc]init];
                baseEffect[i].texture = [mTextures lastObject];
                [baseEffect[i] prepareToDraw];
                //[baseEffect[i] createProgram];

                numQuads[i] = activeQuads;
                numVertices[i] = activeQuads * 4;
                numIndices[i] = activeQuads * 6;

                // Generate dynamic vbo so we can change the data later, copy the prefab data to stop any bitching.
                glGenBuffers(1, &vertexVBO[i]);
                glBindBuffer(GL_ARRAY_BUFFER, vertexVBO[i]);
                glBufferData(GL_ARRAY_BUFFER, _numVertices * sizeof(TiledVertexDef), vertexBuffer, GL_DYNAMIC_DRAW);
                glBindBuffer(GL_ARRAY_BUFFER, 0);

                // Create vertex array object for speed, not sure if it helps in the scheme of things as presentRenderBuffer takes more time (unless this is idle time?)
                glGenVertexArraysOES(1, &vao[i]);
                glBindVertexArrayOES(vao[i]);

                glBindBuffer(GL_ARRAY_BUFFER, vertexVBO[i]);
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBO);
                glVertexAttribPointer(baseEffect[i].attribPosition, 2, GL_SHORT, GL_FALSE, sizeof(TiledVertexDef), (void *)(offsetof(TiledVertexDef, x)));
                glEnableVertexAttribArray(baseEffect[i].attribPosition);
                glVertexAttribPointer(baseEffect[i].attribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(TiledVertexDef), (void *)(offsetof(TiledVertexDef, color)));
                glEnableVertexAttribArray(baseEffect[i].attribColor);
                glVertexAttribPointer(baseEffect[i].attribTexCoords, 2, GL_FLOAT, GL_TRUE, sizeof(TiledVertexDef), (void *)(offsetof(TiledVertexDef, u)));
                glEnableVertexAttribArray(baseEffect[i].attribTexCoords);
        glBindVertexArrayOES(0);
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
                glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

etc

PrimaryFeather commented 10 years ago

Why did you close the request? Did you find another solution / or do you want to send a new one with the final version?

HexTank commented 10 years ago

I've closed it as I realised creating the program requires a texture as its parameter rather than a bool even though it's not used (apart from checking it as if it were a bool) so I need to do more changes than the initial pull request and probably need to think about it more.