diff --git a/interface/src/gpu/Batch.cpp b/interface/src/gpu/Batch.cpp index f21ddb3fbc..43c6872072 100644 --- a/interface/src/gpu/Batch.cpp +++ b/interface/src/gpu/Batch.cpp @@ -12,6 +12,8 @@ #include +#define DO_IT_NOW(call, offset) int param = _params.size() - (offset); do##call(param); + using namespace gpu; Batch::Batch() : @@ -61,5 +63,406 @@ void Batch::drawIndexedInstanced( uint32 nbInstances, Primitive primitiveType, i _params.push_back(nbInstances); } +// TODO: As long as we have gl calls explicitely issued from interface +// code, we need to be able to record and batch these calls. THe long +// term strategy is to get rid of any GL calls in favor of the HIFI GPU API + +void Batch::_glEnable(GLenum cap) { + _commands.push_back(COMMAND_glEnable); + _params.push_back(cap); + + DO_IT_NOW(_glEnable, 1); +} +void Batch::do_glEnable(int ¶mOffset) { + glEnable(_params[paramOffset++]._uint); +} + +void Batch::_glDisable(GLenum cap) { + _commands.push_back(COMMAND_glDisable); + _params.push_back(cap); + + DO_IT_NOW(_glDisable, 1); +} +void Batch::do_glDisable(int ¶mOffset) { + glDisable(_params[paramOffset++]._uint); +} + +void Batch::_glEnableClientState(GLenum array) { + _commands.push_back(COMMAND_glEnableClientState); + _params.push_back(array); + + DO_IT_NOW(_glEnableClientState, 1 ); +} +void Batch::do_glEnableClientState(int ¶mOffset) { + glEnableClientState(_params[paramOffset++]._uint); +} + +void Batch::_glDisableClientState(GLenum array) { + _commands.push_back(COMMAND_glDisableClientState); + _params.push_back(array); + + DO_IT_NOW(_glDisableClientState, 1); +} +void Batch::do_glDisableClientState(int ¶mOffset) { + glDisableClientState(_params[paramOffset++]._uint); +} + +void Batch::_glCullFace(GLenum mode) { + _commands.push_back(COMMAND_glCullFace); + _params.push_back(mode); + + DO_IT_NOW(_glCullFace, 1); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glAlphaFunc(GLenum func, GLclampf ref) { + _commands.push_back(COMMAND_glAlphaFunc); + _params.push_back(ref); + _params.push_back(func); + + DO_IT_NOW(_glAlphaFunc, 1); +} +void Batch::do_glAlphaFunc(int ¶mOffset) { + glAlphaFunc(_params[paramOffset++]._uint, _params[paramOffset++]._float); +} + +void Batch::_glDepthFunc(GLenum func) { + _commands.push_back(COMMAND_glDepthFunc); + _params.push_back(func); + + DO_IT_NOW(_glDepthFunc, 1); +} +void Batch::do_glDepthFunc(int ¶mOffset) { + glDepthFunc(_params[paramOffset++]._uint); +} + +void Batch::_glDepthMask(GLboolean flag) { + _commands.push_back(COMMAND_glDepthMask); + _params.push_back(flag); + + DO_IT_NOW(_glDepthMask, 1); +} +void Batch::do_glDepthMask(int ¶mOffset) { + glDepthMask(_params[paramOffset++]._uint); +} + +void Batch::_glDepthRange(GLclampd zNear, GLclampd zFar) { + _commands.push_back(COMMAND_glDepthRange); + _params.push_back(zFar); + _params.push_back(zNear); + + DO_IT_NOW(_glDepthRange, 2); +} +void Batch::do_glDepthRange(int ¶mOffset) { + glDepthRange(_params[paramOffset++]._double, _params[paramOffset++]._double); +} + +void Batch::_glBindBuffer(GLenum target, GLuint buffer) { + _commands.push_back(COMMAND_glBindBuffer); + _params.push_back(buffer); + _params.push_back(target); + + DO_IT_NOW(_glBindBuffer, 2); +} +void Batch::do_glBindBuffer(int ¶mOffset) { + glBindBuffer(_params[paramOffset++]._uint, _params[paramOffset++]._uint); +} + +void Batch::_glBindTexture(GLenum target, GLuint texture) { + _commands.push_back(COMMAND_glBindTexture); + _params.push_back(texture); + _params.push_back(target); + + DO_IT_NOW(_glBindTexture, 2); +} +void Batch::do_glBindTexture(int ¶mOffset) { + glBindTexture(_params[paramOffset++]._uint, _params[paramOffset++]._uint); +} + +void Batch::_glActiveTexture(GLenum texture) { + _commands.push_back(COMMAND_glActiveTexture); + _params.push_back(texture); + + DO_IT_NOW(_glActiveTexture, 1); +} +void Batch::do_glActiveTexture(int ¶mOffset) { + glActiveTexture(_params[paramOffset++]._uint); +} + +void Batch::_glDrawBuffers(GLsizei n, const GLenum* bufs) { + _commands.push_back(COMMAND_glDrawBuffers); + _params.push_back(bufs); + _params.push_back(n); + + DO_IT_NOW(_glDrawBuffers, 2); +} +void Batch::do_glDrawBuffers(int ¶mOffset) { + glDrawBuffers(_params[paramOffset++]._uint, (const GLenum*) _params[paramOffset++]._constPointer); +} + +void Batch::_glUseProgram(GLuint program) { + _commands.push_back(COMMAND_glUseProgram); + _params.push_back(program); + + DO_IT_NOW(_glUseProgram, 1); +} +void Batch::do_glUseProgram(int ¶mOffset) { + glUseProgram(_params[paramOffset++]._uint); +} + +void Batch::_glUniform1f(GLint location, GLfloat v0) { + _commands.push_back(COMMAND_glUniform1f); + _params.push_back(v0); + _params.push_back(location); + + DO_IT_NOW(_glUniform1f, 1); +} +void Batch::do_glUniform1f(int ¶mOffset) { + glUniform1f(_params[paramOffset++]._float); +} + +void Batch::_glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) { + _commands.push_back(COMMAND_glUniformMatrix4fv); + _params.push_back(value); + _params.push_back(transpose); + _params.push_back(count); + _params.push_back(location); + + DO_IT_NOW(_glUniformMatrix4fv, 4); +} +void Batch::do_glUniformMatrix4fv(int ¶mOffset) { + glUniformMatrix4fv(_params[paramOffset++]._int, _params[paramOffset++]._uint, _params[paramOffset++]._uint, _params[paramOffset++]._constPointer); +} + +void Batch::_glMatrixMode(GLenum mode) { + _commands.push_back(COMMAND_glMatrixMode); + _params.push_back(mode); + + DO_IT_NOW(_glMatrixMode, 1); +} +void Batch::do_glMatrixMode(int ¶mOffset) { + glMatrixMode(_params[paramOffset++]._uint); +} + +void Batch::_glPushMatrix() { + _commands.push_back(COMMAND_glPushMatrix); + + DO_IT_NOW(_glPushMatrix, 0); +} +void Batch::do_glPushMatrix(int ¶mOffset) { + glPushMatrix(); +} + +void Batch::_glPopMatrix() { + _commands.push_back(COMMAND_glPopMatrix); + + DO_IT_NOW(_glPopMatrix, 0); +} +void Batch::do_glPopMatrix(int ¶mOffset) { + glPopMatrix(); +} + +void Batch::_glMultMatrixf(const GLfloat *m) { + _commands.push_back(COMMAND_glMultMatrixf); + _params.push_back(m); + + DO_IT_NOW(_glMultMatrixf, 1); +} +void Batch::do_glMultMatrixf(int ¶mOffset) { + glMultMatrixf((const GLfloat*) _params[paramOffset++]._constPointer); +} + +void Batch::_glLoadMatrixf(const GLfloat *m) { + _commands.push_back(COMMAND_glLoadMatrixf); + _params.push_back(m); + + DO_IT_NOW(_glLoadMatrixf, 1); +} +void Batch::do_glLoadMatrixf(int ¶mOffset) { + glLoadMatrixf((const GLfloat*)_params[paramOffset++]._constPointer); +} + +void Batch::_glLoadIdentity(void) { + _commands.push_back(COMMAND_glLoadIdentity); + + DO_IT_NOW(_glLoadIdentity, 0); +} +void Batch::do_glLoadIdentity(int ¶mOffset) { + glLoadIdentity(); +} + +void Batch::_glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { + _commands.push_back(COMMAND_glRotatef); + _params.push_back(z); + _params.push_back(y); + _params.push_back(x); + _params.push_back(angle); + + DO_IT_NOW(_glRotatef, 4); +} +void Batch::do_glRotatef(int ¶mOffset) { + glRotatef(_params[paramOffset++]._float, _params[paramOffset++]._float, _params[paramOffset++]._float, _params[paramOffset++]._float); +} + +void Batch::_glScalef(GLfloat x, GLfloat y, GLfloat z) { + _commands.push_back(COMMAND_glScalef); + _params.push_back(z); + _params.push_back(y); + _params.push_back(x); + + DO_IT_NOW(_glScalef, 3); +} +void Batch::do_glScalef(int ¶mOffset) { + glScalef(_params[paramOffset++]._float, _params[paramOffset++]._float, _params[paramOffset++]._float); +} + +void Batch::_glTranslatef(GLfloat x, GLfloat y, GLfloat z) { + _commands.push_back(COMMAND_glTranslatef); + _params.push_back(z); + _params.push_back(y); + _params.push_back(x); + + DO_IT_NOW(_glTranslatef, 3); +} +void Batch::do_glTranslatef(int ¶mOffset) { + glTranslatef(_params[paramOffset++]._float, _params[paramOffset++]._float, _params[paramOffset++]._float); +} + +void Batch::_glDrawArrays(GLenum mode, GLint first, GLsizei count) { + _commands.push_back(COMMAND_glDrawArrays); + _params.push_back(count); + _params.push_back(first); + _params.push_back(mode); + + DO_IT_NOW(_glDrawArrays, 3); +} +void Batch::do_glDrawArrays(int ¶mOffset) { + glDrawArrays(_params[paramOffset++]._uint, _params[paramOffset++]._int, _params[paramOffset++]._int); +} + +void Batch::_glDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices) { + _commands.push_back(COMMAND_glDrawRangeElements); + _params.push_back(indices); + _params.push_back(type); + _params.push_back(count); + _params.push_back(end); + _params.push_back(start); + _params.push_back(mode); + + DO_IT_NOW(_glDrawRangeElements, 6); +} +void Batch::do_glDrawRangeElements(int ¶mOffset) { + glDrawRangeElements(_params[paramOffset++]._uint, _params[paramOffset++]._uint, _params[paramOffset++]._uint, _params[paramOffset++]._int, _params[paramOffset++]._uint, _params[paramOffset++]._constPointer); +} + +void Batch::_glColorPointer(GLint size, GLenum type, GLsizei stride, const void *pointer) { + _commands.push_back(COMMAND_glColorPointer); + _params.push_back(pointer); + _params.push_back(stride); + _params.push_back(type); + _params.push_back(size); + + DO_IT_NOW(_glColorPointer, 4); +} +void Batch::do_glColorPointer(int ¶mOffset) { + glColorPointer(_params[paramOffset++]._int, _params[paramOffset++]._uint, _params[paramOffset++]._int, _params[paramOffset++]._constPointer); +} + +void Batch::_glNormalPointer(GLenum type, GLsizei stride, const void *pointer) { + _commands.push_back(COMMAND_glNormalPointer); + _params.push_back(pointer); + _params.push_back(stride); + _params.push_back(type); + + DO_IT_NOW(_glNormalPointer, 4); +} +void Batch::do_glNormalPointer(int ¶mOffset) { + glNormalPointer(_params[paramOffset++]._uint, _params[paramOffset++]._int, _params[paramOffset++]._constPointer); +} + +void Batch::_glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const void *pointer) { + _commands.push_back(COMMAND_glTexCoordPointer); + _params.push_back(pointer); + _params.push_back(stride); + _params.push_back(type); + _params.push_back(size); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glVertexPointer(GLint size, GLenum type, GLsizei stride, const void *pointer) { + _commands.push_back(COMMAND_glVertexPointer); + _params.push_back(pointer); + _params.push_back(stride); + _params.push_back(type); + _params.push_back(size); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + + +void Batch::_glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer) { + _commands.push_back(COMMAND_glVertexPointer); + _params.push_back(pointer); + _params.push_back(stride); + _params.push_back(normalized); + _params.push_back(type); + _params.push_back(size); + _params.push_back(index); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glEnableVertexArrayAttrib(GLint location) { + _commands.push_back(COMMAND_glEnableVertexArrayAttrib); + _params.push_back(location); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glDisableVertexArrayAttrib(GLint location) { + _commands.push_back(COMMAND_glDisableVertexArrayAttrib); + _params.push_back(location); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) { + _commands.push_back(COMMAND_glColor4f); + _params.push_back(alpha); + _params.push_back(blue); + _params.push_back(green); + _params.push_back(red); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glMaterialf(GLenum face, GLenum pname, GLfloat param) { + _commands.push_back(COMMAND_glMaterialf); + _params.push_back(param); + _params.push_back(pname); + _params.push_back(face); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} + +void Batch::_glMaterialfv(GLenum face, GLenum pname, const GLfloat *params) { + _commands.push_back(COMMAND_glMaterialfv); + _params.push_back(params); + _params.push_back(pname); + _params.push_back(face); +} +void Batch::do_glCullFace(int ¶mOffset) { + glCullFace(_params[paramOffset++]._uint); +} diff --git a/interface/src/gpu/Batch.h b/interface/src/gpu/Batch.h index 51e2ce61b6..a03c4ac1b7 100644 --- a/interface/src/gpu/Batch.h +++ b/interface/src/gpu/Batch.h @@ -47,10 +47,64 @@ public: void clear(); - void draw( Primitive primitiveType, int nbVertices, int startVertex = 0); - void drawIndexed( Primitive primitiveType, int nbIndices, int startIndex = 0 ); - void drawInstanced( uint32 nbInstances, Primitive primitiveType, int nbVertices, int startVertex = 0, int startInstance = 0); - void drawIndexedInstanced( uint32 nbInstances, Primitive primitiveType, int nbIndices, int startIndex = 0, int startInstance = 0); + void draw( Primitive primitiveType, int nbVertices, int startVertex = 0); + void drawIndexed( Primitive primitiveType, int nbIndices, int startIndex = 0 ); + void drawInstanced( uint32 nbInstances, Primitive primitiveType, int nbVertices, int startVertex = 0, int startInstance = 0); + void drawIndexedInstanced( uint32 nbInstances, Primitive primitiveType, int nbIndices, int startIndex = 0, int startInstance = 0); + + // TODO: As long as we have gl calls explicitely issued from interface + // code, we need to be able to record and batch these calls. THe long + // term strategy is to get rid of any GL calls in favor of the HIFI GPU API + void _glEnable(GLenum cap); + void _glDisable(GLenum cap); + + void _glEnableClientState(GLenum array); + void _glDisableClientState(GLenum array); + + void _glCullFace(GLenum mode); + void _glAlphaFunc(GLenum func, GLclampf ref); + + void _glDepthFunc(GLenum func); + void _glDepthMask(GLboolean flag); + void _glDepthRange(GLclampd zNear, GLclampd zFar); + + void _glBindBuffer(GLenum target, GLuint buffer); + + void _glBindTexture(GLenum target, GLuint texture); + void _glActiveTexture(GLenum texture); + + void _glDrawBuffers(GLsizei n, const GLenum* bufs); + + void _glUseProgram(GLuint program); + void _glUniform1f(GLint location, GLfloat v0); + void _glUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat* value); + + void _glMatrixMode(GLenum mode); + void _glPushMatrix(); + void _glPopMatrix(); + void _glMultMatrixf(const GLfloat *m); + void _glLoadMatrixf(const GLfloat *m); + void _glLoadIdentity(void); + void _glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z); + void _glScalef(GLfloat x, GLfloat y, GLfloat z); + void _glTranslatef(GLfloat x, GLfloat y, GLfloat z); + + void _glDrawArrays(GLenum mode, GLint first, GLsizei count); + void _glDrawRangeElements(GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const void *indices); + + void _glColorPointer(GLint size, GLenum type, GLsizei stride, const void *pointer); + void _glNormalPointer(GLenum type, GLsizei stride, const void *pointer); + void _glTexCoordPointer(GLint size, GLenum type, GLsizei stride, const void *pointer); + void _glVertexPointer(GLint size, GLenum type, GLsizei stride, const void *pointer); + + void _glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); + void _glEnableVertexArrayAttrib(GLint location); + void _glDisableVertexArrayAttrib(GLint location); + + void _glColor4f(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha); + + void _glMaterialf(GLenum face, GLenum pname, GLfloat param); + void _glMaterialfv(GLenum face, GLenum pname, const GLfloat *params); protected: @@ -67,18 +121,77 @@ protected: COMMAND_SET_VERTEX_STREAM, COMMAND_SET_INDEX_STREAM, - COMMAND_GL_SET_UNIFORM, - + // TODO: As long as we have gl calls explicitely issued from interface + // code, we need to be able to record and batch these calls. THe long + // term strategy is to get rid of any GL calls in favor of the HIFI GPU API + COMMAND_glEnable, + COMMAND_glDisable, + + COMMAND_glEnableClientState, + COMMAND_glDisableClientState, + + COMMAND_glCullFace, + COMMAND_glAlphaFunc, + + COMMAND_glDepthFunc, + COMMAND_glDepthMask, + COMMAND_glDepthRange, + + COMMAND_glBindBuffer, + + COMMAND_glBindTexture, + COMMAND_glActiveTexture, + + COMMAND_glDrawBuffers, + + COMMAND_glUseProgram, + COMMAND_glUniform1f, + COMMAND_glUniformMatrix4fv, + + COMMAND_glMatrixMode, + COMMAND_glPushMatrix, + COMMAND_glPopMatrix, + COMMAND_glMultMatrixf, + COMMAND_glLoadMatrixf, + COMMAND_glLoadIdentity, + COMMAND_glRotatef, + COMMAND_glScalef, + COMMAND_glTranslatef, + + COMMAND_glDrawArrays, + COMMAND_glDrawRangeElements, + + COMMAND_glColorPointer, + COMMAND_glNormalPointer, + COMMAND_glTexCoordPointer, + COMMAND_glVertexPointer, + + COMMAND_glVertexAttribPointer, + COMMAND_glEnableVertexArrayAttrib, + COMMAND_glDisableVertexArrayAttrib, + + COMMAND_glColor4f, + + COMMAND_glMaterialf, + COMMAND_glMaterialfv, }; typedef std::vector Commands; - - class Param { - public: - union { - uint32 _uint; - char _chars[4]; - }; - Param( uint32 val ): _uint(val) {} + + class Param { + public: + union { + int32 _int; + uint32 _uint; + float _float; + char _chars[4]; + const void* _constPointer; + double _double; + }; + Param(int32 val) : _int(val) {} + Param(uint32 val) : _uint(val) {} + Param(float val) : _float(val) {} + Param(const void* val) : _constPointer(val) {} + Param(double val) : _double(val) {} }; typedef std::vector Params; @@ -91,6 +204,60 @@ protected: Commands _commands; Params _params; Resources _resources; + + // TODO: As long as we have gl calls explicitely issued from interface + // code, we need to be able to record and batch these calls. THe long + // term strategy is to get rid of any GL calls in favor of the HIFI GPU API + void do_glEnable(int ¶mOffset); + void do_glDisable(int ¶mOffset); + + void do_glEnableClientState(int ¶mOffset); + void do_glDisableClientState(int ¶mOffset); + + void do_glCullFace(int ¶mOffset); + void do_glAlphaFunc(int ¶mOffset); + + void do_glDepthFunc(int ¶mOffset); + void do_glDepthMask(int ¶mOffset); + void do_glDepthRange(int ¶mOffset); + + void do_glBindBuffer(int ¶mOffset); + + void do_glBindTexture(int ¶mOffset); + void do_glActiveTexture(int ¶mOffset); + + void do_glDrawBuffers(int ¶mOffset); + + void do_glUseProgram(int ¶mOffset); + void do_glUniform1f(int ¶mOffset); + void do_glUniformMatrix4fv(int ¶mOffset); + + void do_glMatrixMode(int ¶mOffset); + void do_glPushMatrix(int ¶mOffset); + void do_glPopMatrix(int ¶mOffset); + void do_glMultMatrixf(int ¶mOffset); + void do_glLoadMatrixf(int ¶mOffset); + void do_glLoadIdentity(int ¶mOffset); + void do_glRotatef(int ¶mOffset); + void do_glScalef(int ¶mOffset); + void do_glTranslatef(int ¶mOffset); + + void do_glDrawArrays(int ¶mOffset); + void do_glDrawRangeElements(int ¶mOffset); + + void do_glColorPointer(int ¶mOffset); + void do_glNormalPointer(int ¶mOffset); + void do_glTexCoordPointer(int ¶mOffset); + void do_glVertexPointer(int ¶mOffset); + + void do_glVertexAttribPointer(int ¶mOffset); + void do_glEnableVertexArrayAttrib(int ¶mOffset); + void do_glDisableVertexArrayAttrib(int ¶mOffset); + + void do_glColor4f(int ¶mOffset); + + void do_glMaterialf(int ¶mOffset); + void do_glMaterialfv(int ¶mOffset); }; }; diff --git a/interface/src/renderer/Model.cpp b/interface/src/renderer/Model.cpp index 3800e78008..6032ae4d69 100644 --- a/interface/src/renderer/Model.cpp +++ b/interface/src/renderer/Model.cpp @@ -26,6 +26,9 @@ #include "Application.h" #include "Model.h" +#include "gpu/Batch.h" +#define GLBATCH( call ) batch._##call + using namespace std; static int modelPointerTypeId = qRegisterMetaType >(); @@ -1616,6 +1619,10 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, } activeProgram->setUniformValue(activeLocations->alphaThreshold, alphaThreshold); + // Try to use the Batch + gpu::Batch batch; + + // i is the "index" from the original networkMeshes QVector... foreach (int i, list) { @@ -1631,7 +1638,8 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, const NetworkMesh& networkMesh = networkMeshes.at(i); const FBXMesh& mesh = geometry.meshes.at(i); - const_cast(networkMesh.indexBuffer).bind(); + //const_cast(networkMesh.indexBuffer).bind(); + GLBATCH(glBindBuffer)( GL_INDEX_ARRAY, const_cast(networkMesh.indexBuffer).bufferId() ); int vertexCount = mesh.vertices.size(); if (vertexCount == 0) { @@ -1668,12 +1676,16 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, const_cast(networkMesh.vertexBuffer).bind(); - glPushMatrix(); - Application::getInstance()->loadTranslatedViewMatrix(_translation); + GLBATCH(glPushMatrix)(); + //Application::getInstance()->loadTranslatedViewMatrix(_translation); + GLBATCH(glLoadMatrixf)((const GLfloat*)&Application::getInstance()->getUntranslatedViewMatrix()); + glm::vec3 viewMatTranslation = Application::getInstance()->getViewMatrixTranslation(); + GLBATCH(glTranslatef)(_translation.x + viewMatTranslation.x, _translation.y + viewMatTranslation.y, + _translation.z + viewMatTranslation.z); const MeshState& state = _meshStates.at(i); if (state.clusterMatrices.size() > 1) { - glUniformMatrix4fvARB(skinLocations->clusterMatrices, state.clusterMatrices.size(), false, + GLBATCH(glUniformMatrix4fv)(skinLocations->clusterMatrices, state.clusterMatrices.size(), false, (const float*)state.clusterMatrices.constData()); int offset = (mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3) + mesh.texCoords.size() * sizeof(glm::vec2) + @@ -1684,7 +1696,7 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, skinProgram->enableAttributeArray(skinLocations->clusterIndices); skinProgram->enableAttributeArray(skinLocations->clusterWeights); } else { - glMultMatrixf((const GLfloat*)&state.clusterMatrices[0]); + GLBATCH(glMultMatrixf)((const GLfloat*)&state.clusterMatrices[0]); } if (mesh.blendshapes.isEmpty()) { @@ -1692,9 +1704,9 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, activeProgram->setAttributeBuffer(activeLocations->tangent, GL_FLOAT, vertexCount * 2 * sizeof(glm::vec3), 3); activeProgram->enableAttributeArray(activeLocations->tangent); } - glColorPointer(3, GL_FLOAT, 0, (void*)(vertexCount * 2 * sizeof(glm::vec3) + + GLBATCH(glColorPointer)(3, GL_FLOAT, 0, (void*)(vertexCount * 2 * sizeof(glm::vec3) + mesh.tangents.size() * sizeof(glm::vec3))); - glTexCoordPointer(2, GL_FLOAT, 0, (void*)(vertexCount * 2 * sizeof(glm::vec3) + + GLBATCH(glTexCoordPointer)(2, GL_FLOAT, 0, (void*)(vertexCount * 2 * sizeof(glm::vec3) + (mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3))); } else { @@ -1702,20 +1714,20 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, activeProgram->setAttributeBuffer(activeLocations->tangent, GL_FLOAT, 0, 3); activeProgram->enableAttributeArray(activeLocations->tangent); } - glColorPointer(3, GL_FLOAT, 0, (void*)(mesh.tangents.size() * sizeof(glm::vec3))); - glTexCoordPointer(2, GL_FLOAT, 0, (void*)((mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3))); + GLBATCH(glColorPointer)(3, GL_FLOAT, 0, (void*)(mesh.tangents.size() * sizeof(glm::vec3))); + GLBATCH(glTexCoordPointer)(2, GL_FLOAT, 0, (void*)((mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3))); _blendedVertexBuffers[i].bind(); } - glVertexPointer(3, GL_FLOAT, 0, 0); - glNormalPointer(GL_FLOAT, 0, (void*)(vertexCount * sizeof(glm::vec3))); + GLBATCH(glVertexPointer)(3, GL_FLOAT, 0, 0); + GLBATCH(glNormalPointer)(GL_FLOAT, 0, (void*)(vertexCount * sizeof(glm::vec3))); if (!mesh.colors.isEmpty()) { - glEnableClientState(GL_COLOR_ARRAY); + GLBATCH(glEnableClientState)(GL_COLOR_ARRAY); } else { - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + GLBATCH(glColor4f)(1.0f, 1.0f, 1.0f, 1.0f); } if (!mesh.texCoords.isEmpty()) { - glEnableClientState(GL_TEXTURE_COORD_ARRAY); + GLBATCH(glEnableClientState)(GL_TEXTURE_COORD_ARRAY); } qint64 offset = 0; @@ -1728,7 +1740,7 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, } // apply material properties if (mode == SHADOW_RENDER_MODE) { - glBindTexture(GL_TEXTURE_2D, 0); + GLBATCH(glBindTexture)(GL_TEXTURE_2D, 0); } else { if (dontReduceMaterialSwitches || lastMaterialID != part.materialID) { @@ -1741,36 +1753,36 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, glm::vec4 diffuse = glm::vec4(part.diffuseColor, part.opacity); if (!(translucent && alphaThreshold == 0.0f)) { - glAlphaFunc(GL_EQUAL, diffuse.a = Application::getInstance()->getGlowEffect()->getIntensity()); + GLBATCH(glAlphaFunc)(GL_EQUAL, diffuse.a = Application::getInstance()->getGlowEffect()->getIntensity()); } glm::vec4 specular = glm::vec4(part.specularColor, 1.0f); - glMaterialfv(GL_FRONT, GL_AMBIENT, (const float*)&diffuse); - glMaterialfv(GL_FRONT, GL_DIFFUSE, (const float*)&diffuse); - glMaterialfv(GL_FRONT, GL_SPECULAR, (const float*)&specular); - glMaterialf(GL_FRONT, GL_SHININESS, part.shininess); + GLBATCH(glMaterialfv)(GL_FRONT, GL_AMBIENT, (const float*)&diffuse); + GLBATCH(glMaterialfv)(GL_FRONT, GL_DIFFUSE, (const float*)&diffuse); + GLBATCH(glMaterialfv)(GL_FRONT, GL_SPECULAR, (const float*)&specular); + GLBATCH(glMaterialf)(GL_FRONT, GL_SHININESS, part.shininess); Texture* diffuseMap = networkPart.diffuseTexture.data(); if (mesh.isEye && diffuseMap) { diffuseMap = (_dilatedTextures[i][j] = static_cast(diffuseMap)->getDilatedTexture(_pupilDilation)).data(); } - glBindTexture(GL_TEXTURE_2D, !diffuseMap ? + GLBATCH(glBindTexture)(GL_TEXTURE_2D, !diffuseMap ? Application::getInstance()->getTextureCache()->getWhiteTextureID() : diffuseMap->getID()); if (!mesh.tangents.isEmpty()) { - glActiveTexture(GL_TEXTURE1); + GLBATCH(glActiveTexture)(GL_TEXTURE1); Texture* normalMap = networkPart.normalTexture.data(); - glBindTexture(GL_TEXTURE_2D, !normalMap ? + GLBATCH(glBindTexture)(GL_TEXTURE_2D, !normalMap ? Application::getInstance()->getTextureCache()->getBlueTextureID() : normalMap->getID()); - glActiveTexture(GL_TEXTURE0); + GLBATCH(glActiveTexture)(GL_TEXTURE0); } if (specularTextureUnit) { - glActiveTexture(specularTextureUnit); + GLBATCH(glActiveTexture)(specularTextureUnit); Texture* specularMap = networkPart.specularTexture.data(); - glBindTexture(GL_TEXTURE_2D, !specularMap ? + GLBATCH(glBindTexture)(GL_TEXTURE_2D, !specularMap ? Application::getInstance()->getTextureCache()->getWhiteTextureID() : specularMap->getID()); - glActiveTexture(GL_TEXTURE0); + GLBATCH(glActiveTexture)(GL_TEXTURE0); } if (args) { args->_materialSwitches++; @@ -1783,12 +1795,12 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, meshPartsRendered++; if (part.quadIndices.size() > 0) { - glDrawRangeElementsEXT(GL_QUADS, 0, vertexCount - 1, part.quadIndices.size(), GL_UNSIGNED_INT, (void*)offset); + GLBATCH(glDrawRangeElements)(GL_QUADS, 0, vertexCount - 1, part.quadIndices.size(), GL_UNSIGNED_INT, (void*)offset); offset += part.quadIndices.size() * sizeof(int); } if (part.triangleIndices.size() > 0) { - glDrawRangeElementsEXT(GL_TRIANGLES, 0, vertexCount - 1, part.triangleIndices.size(), + GLBATCH(glDrawRangeElements)(GL_TRIANGLES, 0, vertexCount - 1, part.triangleIndices.size(), GL_UNSIGNED_INT, (void*)offset); offset += part.triangleIndices.size() * sizeof(int); } @@ -1802,35 +1814,39 @@ int Model::renderMeshes(RenderMode mode, bool translucent, float alphaThreshold, } if (!mesh.colors.isEmpty()) { - glDisableClientState(GL_COLOR_ARRAY); + GLBATCH(glDisableClientState)(GL_COLOR_ARRAY); } if (!mesh.texCoords.isEmpty()) { - glDisableClientState(GL_TEXTURE_COORD_ARRAY); + GLBATCH(glDisableClientState)(GL_TEXTURE_COORD_ARRAY); } if (!(mesh.tangents.isEmpty() || mode == SHADOW_RENDER_MODE)) { - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE0); + GLBATCH(glActiveTexture)(GL_TEXTURE1); + GLBATCH(glBindTexture)(GL_TEXTURE_2D, 0); + GLBATCH(glActiveTexture)(GL_TEXTURE0); - activeProgram->disableAttributeArray(activeLocations->tangent); + // activeProgram->disableAttributeArray(activeLocations->tangent); + GLBATCH(glDisableVertexArrayAttrib)(activeLocations->tangent); } if (specularTextureUnit) { - glActiveTexture(specularTextureUnit); - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE0); + GLBATCH(glActiveTexture)(specularTextureUnit); + GLBATCH(glBindTexture)(GL_TEXTURE_2D, 0); + GLBATCH(glActiveTexture)(GL_TEXTURE0); } if (state.clusterMatrices.size() > 1) { - skinProgram->disableAttributeArray(skinLocations->clusterIndices); - skinProgram->disableAttributeArray(skinLocations->clusterWeights); + // skinProgram->disableAttributeArray(skinLocations->clusterIndices); + GLBATCH(glDisableVertexArrayAttrib)(skinLocations->clusterIndices); + // skinProgram->disableAttributeArray(skinLocations->clusterWeights); + GLBATCH(glDisableVertexArrayAttrib)(skinLocations->clusterWeights); } - glPopMatrix(); + GLBATCH(glPopMatrix)(); } - activeProgram->release(); - + //activeProgram->release(); + GLBATCH(glUseProgram)(0); + return meshPartsRendered; }