Skip to content

Commit

Permalink
tried VAOs, didnt help
Browse files Browse the repository at this point in the history
  • Loading branch information
aap committed May 15, 2020
1 parent 90ce0f9 commit d541301
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 15 deletions.
51 changes: 43 additions & 8 deletions src/gl/gl3immed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
}


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -243,7 +277,6 @@ im3DRenderPrimitive(PrimitiveType primType)

flushCache();
glDrawArrays(primTypeMap[primType], 0, num3DVertices);
disableAttribPointers(im3dattribDesc, 3);
}

void
Expand All @@ -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
}

}
Expand Down
8 changes: 7 additions & 1 deletion src/gl/gl3matfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -198,7 +202,9 @@ matfxRenderCB(Atomic *atomic, InstanceDataHeader *header)
}
inst++;
}
#ifndef RW_GL_USE_VAOS
disableAttribPointers(header->attribDesc, header->numAttribs);
#endif
}

ObjPipeline*
Expand Down
17 changes: 15 additions & 2 deletions src/gl/gl3pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/gl/gl3render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -156,7 +160,9 @@ defaultRenderCB(Atomic *atomic, InstanceDataHeader *header)
drawInst(header, inst);
inst++;
}
#ifndef RW_GL_USE_VAOS
disableAttribPointers(header->attribDesc, header->numAttribs);
#endif
}


Expand Down
17 changes: 15 additions & 2 deletions src/gl/gl3skin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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*
Expand Down
5 changes: 4 additions & 1 deletion src/gl/rwgl3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -88,6 +88,9 @@ struct InstanceDataHeader : rw::InstanceDataHeader

uint32 ibo;
uint32 vbo; // or 2?
#ifdef RW_GL_USE_VAOS
uint32 vao;
#endif

InstanceData *inst;
};
Expand Down
2 changes: 2 additions & 0 deletions src/rwbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d541301

Please sign in to comment.