diff --git a/src/gl/gl3immed.cpp b/src/gl/gl3immed.cpp index 1abaa0a..cebb75f 100644 --- a/src/gl/gl3immed.cpp +++ b/src/gl/gl3immed.cpp @@ -20,6 +20,9 @@ namespace rw { namespace gl3 { uint32 im2DVbo, im2DIbo; +#ifdef RW_GL_USE_VAOS +uint32 im2DVao; +#endif static int32 u_xform; #define STARTINDICES 10000 @@ -65,12 +68,16 @@ openIm2D(void) glGenBuffers(1, &im2DIbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glGenBuffers(1, &im2DVbo); glBindBuffer(GL_ARRAY_BUFFER, im2DVbo); glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + +#ifdef RW_GL_USE_VAOS + glGenVertexArrays(1, &im2DVao); + glBindVertexArray(im2DVao); + setAttribPointers(im2dattribDesc, 3); +#endif } void @@ -110,6 +117,10 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices) Camera *cam; cam = (Camera*)engine->currentCamera; +#ifdef RW_GL_USE_VAOS + glBindVertexArray(im2DVao); +#endif + glBindBuffer(GL_ARRAY_BUFFER, im2DVbo); glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im2DVertex), nil, GL_STREAM_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im2DVertex), vertices); @@ -120,13 +131,17 @@ im2DRenderPrimitive(PrimitiveType primType, void *vertices, int32 numVertices) xform[3] = 1.0f; im2dShader->use(); +#ifndef RW_GL_USE_VAOS setAttribPointers(im2dattribDesc, 3); +#endif glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform); flushCache(); glDrawArrays(primTypeMap[primType], 0, numVertices); +#ifndef RW_GL_USE_VAOS disableAttribPointers(im2dattribDesc, 3); +#endif } void @@ -138,7 +153,10 @@ im2DRenderIndexedPrimitive(PrimitiveType primType, Camera *cam; cam = (Camera*)engine->currentCamera; - // TODO: fixed size +#ifdef RW_GL_USE_VAOS + glBindVertexArray(im2DVao); +#endif + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im2DIbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, numIndices*2, indices); @@ -153,14 +171,18 @@ im2DRenderIndexedPrimitive(PrimitiveType primType, xform[3] = 1.0f; im2dShader->use(); +#ifndef RW_GL_USE_VAOS setAttribPointers(im2dattribDesc, 3); +#endif glUniform4fv(currentShader->uniformLocations[u_xform], 1, xform); flushCache(); glDrawElements(primTypeMap[primType], numIndices, GL_UNSIGNED_SHORT, nil); +#ifndef RW_GL_USE_VAOS disableAttribPointers(im2dattribDesc, 3); +#endif } @@ -177,6 +199,9 @@ static AttribDesc im3dattribDesc[3] = { sizeof(Im3DVertex), offsetof(Im3DVertex, u) }, }; static uint32 im3DVbo, im3DIbo; +#ifdef RW_GL_USE_VAOS +static uint32 im3DVao; +#endif static int32 num3DVertices; // not actually needed here void @@ -197,12 +222,16 @@ openIm3D(void) glGenBuffers(1, &im3DIbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, im3DIbo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, STARTINDICES*2, nil, GL_STREAM_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glGenBuffers(1, &im3DVbo); glBindBuffer(GL_ARRAY_BUFFER, im3DVbo); glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im3DVertex), nil, GL_STREAM_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); + +#ifdef RW_GL_USE_VAOS + glGenVertexArrays(1, &im3DVao); + glBindVertexArray(im3DVao); + setAttribPointers(im3dattribDesc, 3); +#endif } void @@ -228,11 +257,16 @@ im3DTransform(void *vertices, int32 numVertices, Matrix *world, uint32 flags) if((flags & im3d::VERTEXUV) == 0) SetRenderStatePtr(TEXTURERASTER, nil); - // TODO: fixed size +#ifdef RW_GL_USE_VAOS + glBindVertexArray(im2DVao); +#endif + glBindBuffer(GL_ARRAY_BUFFER, im3DVbo); glBufferData(GL_ARRAY_BUFFER, STARTVERTICES*sizeof(Im3DVertex), nil, GL_STREAM_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, numVertices*sizeof(Im3DVertex), vertices); +#ifndef RW_GL_USE_VAOS setAttribPointers(im3dattribDesc, 3); +#endif num3DVertices = numVertices; } @@ -243,7 +277,6 @@ im3DRenderPrimitive(PrimitiveType primType) flushCache(); glDrawArrays(primTypeMap[primType], 0, num3DVertices); - disableAttribPointers(im3dattribDesc, 3); } void @@ -256,12 +289,14 @@ im3DRenderIndexedPrimitive(PrimitiveType primType, void *indices, int32 numIndic flushCache(); glDrawElements(primTypeMap[primType], numIndices, GL_UNSIGNED_SHORT, nil); - disableAttribPointers(im3dattribDesc, 3); } void im3DEnd(void) { +#ifndef RW_GL_USE_VAOS + disableAttribPointers(im3dattribDesc, 3); +#endif } } diff --git a/src/gl/gl3matfx.cpp b/src/gl/gl3matfx.cpp index 0d44a5d..3734f3a 100644 --- a/src/gl/gl3matfx.cpp +++ b/src/gl/gl3matfx.cpp @@ -174,9 +174,13 @@ matfxRenderCB(Atomic *atomic, InstanceDataHeader *header) setWorldMatrix(atomic->getFrame()->getLTM()); lightingCB(atomic); - glBindBuffer(GL_ARRAY_BUFFER, header->vbo); +#ifdef RW_GL_USE_VAOS + glBindVertexArray(header->vao); +#else glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); + glBindBuffer(GL_ARRAY_BUFFER, header->vbo); setAttribPointers(header->attribDesc, header->numAttribs); +#endif lastEnvFrame = nil; @@ -198,7 +202,9 @@ matfxRenderCB(Atomic *atomic, InstanceDataHeader *header) } inst++; } +#ifndef RW_GL_USE_VAOS disableAttribPointers(header->attribDesc, header->numAttribs); +#endif } ObjPipeline* diff --git a/src/gl/gl3pipe.cpp b/src/gl/gl3pipe.cpp index 2bda84d..89c4004 100644 --- a/src/gl/gl3pipe.cpp +++ b/src/gl/gl3pipe.cpp @@ -32,6 +32,9 @@ freeInstanceData(Geometry *geometry) geometry->instData = nil; glDeleteBuffers(1, &header->ibo); glDeleteBuffers(1, &header->vbo); +#ifdef RW_GL_USE_VAOS + glDeleteBuffers(1, &header->vao); +#endif rwFree(header->indexBuffer); rwFree(header->vertexBuffer); rwFree(header->attribDesc); @@ -86,11 +89,14 @@ instanceMesh(rw::ObjPipeline *rwpipe, Geometry *geo) header->ibo = 0; header->vbo = 0; +#ifdef RW_GL_USE_VAOS + glGenVertexArrays(1, &header->vao); + glBindVertexArray(header->vao); +#endif glGenBuffers(1, &header->ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, header->totalNumIndex*2, header->indexBuffer, GL_STATIC_DRAW); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); return header; } @@ -282,10 +288,17 @@ defaultInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance) } } +#ifdef RW_GL_USE_VAOS + glBindVertexArray(header->vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); +#endif glBindBuffer(GL_ARRAY_BUFFER, header->vbo); glBufferData(GL_ARRAY_BUFFER, header->totalNumVertex*attribs[0].stride, header->vertexBuffer, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); +#ifdef RW_GL_USE_VAOS + setAttribPointers(header->attribDesc, header->numAttribs); + glBindVertexArray(0); +#endif } void diff --git a/src/gl/gl3render.cpp b/src/gl/gl3render.cpp index 8c35f18..e0d5156 100644 --- a/src/gl/gl3render.cpp +++ b/src/gl/gl3render.cpp @@ -128,9 +128,13 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header) setWorldMatrix(atomic->getFrame()->getLTM()); lightingCB(atomic); - glBindBuffer(GL_ARRAY_BUFFER, header->vbo); +#ifdef RW_GL_USE_VAOS + glBindVertexArray(header->vao); +#else glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); + glBindBuffer(GL_ARRAY_BUFFER, header->vbo); setAttribPointers(header->attribDesc, header->numAttribs); +#endif InstanceData *inst = header->inst; int32 n = header->numMeshes; @@ -156,7 +160,9 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header) drawInst(header, inst); inst++; } +#ifndef RW_GL_USE_VAOS disableAttribPointers(header->attribDesc, header->numAttribs); +#endif } diff --git a/src/gl/gl3skin.cpp b/src/gl/gl3skin.cpp index 9601a20..52ed51b 100644 --- a/src/gl/gl3skin.cpp +++ b/src/gl/gl3skin.cpp @@ -226,10 +226,17 @@ skinInstanceCB(Geometry *geo, InstanceDataHeader *header, bool32 reinstance) header->totalNumVertex, a->stride); } +#ifdef RW_GL_USE_VAOS + glBindVertexArray(header->vao); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); +#endif glBindBuffer(GL_ARRAY_BUFFER, header->vbo); glBufferData(GL_ARRAY_BUFFER, header->totalNumVertex*attribs[0].stride, header->vertexBuffer, GL_STATIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); +#ifdef RW_GL_USE_VAOS + setAttribPointers(header->attribDesc, header->numAttribs); + glBindVertexArray(0); +#endif } void @@ -281,9 +288,13 @@ skinRenderCB(Atomic *atomic, InstanceDataHeader *header) setWorldMatrix(atomic->getFrame()->getLTM()); lightingCB(atomic); - glBindBuffer(GL_ARRAY_BUFFER, header->vbo); +#ifdef RW_GL_USE_VAOS + glBindVertexArray(header->vao); +#else glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, header->ibo); + glBindBuffer(GL_ARRAY_BUFFER, header->vbo); setAttribPointers(header->attribDesc, header->numAttribs); +#endif InstanceData *inst = header->inst; int32 n = header->numMeshes; @@ -311,7 +322,9 @@ skinRenderCB(Atomic *atomic, InstanceDataHeader *header) drawInst(header, inst); inst++; } +#ifndef RW_GL_USE_VAOS disableAttribPointers(header->attribDesc, header->numAttribs); +#endif } ObjPipeline* diff --git a/src/gl/rwgl3.h b/src/gl/rwgl3.h index c0eeb45..863da2e 100644 --- a/src/gl/rwgl3.h +++ b/src/gl/rwgl3.h @@ -76,7 +76,7 @@ struct InstanceData struct InstanceDataHeader : rw::InstanceDataHeader { - uint32 serialNumber; // not really needed right now + uint32 serialNumber; uint32 numMeshes; uint16 *indexBuffer; uint32 primType; @@ -88,6 +88,9 @@ struct InstanceDataHeader : rw::InstanceDataHeader uint32 ibo; uint32 vbo; // or 2? +#ifdef RW_GL_USE_VAOS + uint32 vao; +#endif InstanceData *inst; }; diff --git a/src/rwbase.h b/src/rwbase.h index 63183d0..f930c18 100644 --- a/src/rwbase.h +++ b/src/rwbase.h @@ -11,6 +11,8 @@ #ifdef RW_GL3 #define RW_OPENGL #define RWDEVICE gl3 +// doesn't help +//#define RW_GL_USE_VAOS #endif #ifdef RW_GLES2