From be137534b5a76a816684d6e7943dda71febec2dc Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 30 Dec 2014 12:38:16 -0800 Subject: [PATCH 1/3] first cut at registered quads to reduce copies of constantly changing quads --- interface/src/ui/ApplicationOverlay.cpp | 50 +++++++-- interface/src/ui/ApplicationOverlay.h | 9 +- libraries/render-utils/src/GeometryCache.cpp | 105 ++++++++++++++++--- libraries/render-utils/src/GeometryCache.h | 20 ++-- 4 files changed, 149 insertions(+), 35 deletions(-) diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index 6c68e0add1..120c5b3043 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -105,7 +105,7 @@ bool raySphereIntersect(const glm::vec3 &dir, const glm::vec3 &origin, float r, } } -void renderReticle(glm::quat orientation, float alpha) { +void ApplicationOverlay::renderReticle(glm::quat orientation, float alpha) { glPushMatrix(); { glm::vec3 axis = glm::axis(orientation); glRotatef(glm::degrees(glm::angle(orientation)), axis.x, axis.y, axis.z); @@ -114,9 +114,12 @@ void renderReticle(glm::quat orientation, float alpha) { glm::vec3 bottomLeft = getPoint(reticleSize / 2.0f, reticleSize / 2.0f); glm::vec3 bottomRight = getPoint(-reticleSize / 2.0f, reticleSize / 2.0f); glColor4f(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], alpha); + if (_reticleQuad == GeometryCache::UNKNOWN_QUAD_ID) { + _reticleQuad = DependencyManager::get()->allocateQuad(); + } DependencyManager::get()->renderQuad(topLeft, bottomLeft, bottomRight, topRight, glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), - glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)); + glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f), _reticleQuad); } glPopMatrix(); } @@ -126,8 +129,13 @@ ApplicationOverlay::ApplicationOverlay() : _lastMouseMove(0), _alpha(1.0f), _oculusUIRadius(1.0f), - _crosshairTexture(0) { - + _crosshairTexture(0), + _reticleQuad(GeometryCache::UNKNOWN_QUAD_ID), + _magnifierQuad(GeometryCache::UNKNOWN_QUAD_ID), + _audioRedQuad(GeometryCache::UNKNOWN_QUAD_ID), + _audioGreenQuad(GeometryCache::UNKNOWN_QUAD_ID), + _audioBlueQuad(GeometryCache::UNKNOWN_QUAD_ID) +{ memset(_reticleActive, 0, sizeof(_reticleActive)); memset(_magActive, 0, sizeof(_reticleActive)); memset(_magSizeMult, 0, sizeof(_magSizeMult)); @@ -389,12 +397,17 @@ void ApplicationOverlay::displayOverlayTexture3DTV(Camera& whichCamera, float as glColor3f(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2]); + if (_reticleQuad == GeometryCache::UNKNOWN_QUAD_ID) { + _reticleQuad = DependencyManager::get()->allocateQuad(); + } + DependencyManager::get()->renderQuad(glm::vec3(x + mouseX, y + mouseY, -distance), glm::vec3(x + mouseX + reticleSize, y + mouseY, -distance), glm::vec3(x + mouseX + reticleSize, y + mouseY - reticleSize, -distance), glm::vec3(x + mouseX, y + mouseY - reticleSize, -distance), glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f), - glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f)); + glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f), + _reticleQuad); glEnable(GL_DEPTH_TEST); @@ -696,7 +709,7 @@ void ApplicationOverlay::renderPointersOculus(const glm::vec3& eyePos) { } //Renders a small magnification of the currently bound texture at the coordinates -void ApplicationOverlay::renderMagnifier(glm::vec2 magPos, float sizeMult, bool showBorder) const { +void ApplicationOverlay::renderMagnifier(glm::vec2 magPos, float sizeMult, bool showBorder) { GLCanvas::SharedPointer glCanvas = DependencyManager::get(); const int widgetWidth = glCanvas->width(); @@ -742,11 +755,16 @@ void ApplicationOverlay::renderMagnifier(glm::vec2 magPos, float sizeMult, bool } glColor4f(1.0f, 1.0f, 1.0f, _alpha); + if (_magnifierQuad == GeometryCache::UNKNOWN_QUAD_ID) { + _magnifierQuad = DependencyManager::get()->allocateQuad(); + } + DependencyManager::get()->renderQuad(bottomLeft, bottomRight, topRight, topLeft, glm::vec2(magnifyULeft, magnifyVBottom), glm::vec2(magnifyURight, magnifyVBottom), glm::vec2(magnifyURight, magnifyVTop), - glm::vec2(magnifyULeft, magnifyVTop)); + glm::vec2(magnifyULeft, magnifyVTop), + _magnifierQuad); } glPopMatrix(); } @@ -845,10 +863,14 @@ void ApplicationOverlay::renderAudioMeter() { glColor3f(1, 1, 1); } // Draw Red Quad + if (_audioRedQuad == GeometryCache::UNKNOWN_QUAD_ID) { + _audioRedQuad = DependencyManager::get()->allocateQuad(); + } DependencyManager::get()->renderQuad(AUDIO_METER_X + AUDIO_METER_INSET + AUDIO_RED_START, audioMeterY + AUDIO_METER_INSET, audioLevel - AUDIO_RED_START, - AUDIO_METER_HEIGHT - AUDIO_METER_INSET); + AUDIO_METER_HEIGHT - AUDIO_METER_INSET, + _audioRedQuad); audioLevel = AUDIO_RED_START; } @@ -860,10 +882,14 @@ void ApplicationOverlay::renderAudioMeter() { glColor3f(1, 1, 1); } // Draw Green Quad + if (_audioGreenQuad == GeometryCache::UNKNOWN_QUAD_ID) { + _audioGreenQuad = DependencyManager::get()->allocateQuad(); + } DependencyManager::get()->renderQuad(AUDIO_METER_X + AUDIO_METER_INSET + AUDIO_GREEN_START, audioMeterY + AUDIO_METER_INSET, audioLevel - AUDIO_GREEN_START, - AUDIO_METER_HEIGHT - AUDIO_METER_INSET); + AUDIO_METER_HEIGHT - AUDIO_METER_INSET, + _audioGreenQuad); audioLevel = AUDIO_GREEN_START; } @@ -874,9 +900,13 @@ void ApplicationOverlay::renderAudioMeter() { glColor3f(1, 1, 1); } // Draw Blue (low level) quad + if (_audioBlueQuad == GeometryCache::UNKNOWN_QUAD_ID) { + _audioBlueQuad = DependencyManager::get()->allocateQuad(); + } DependencyManager::get()->renderQuad(AUDIO_METER_X + AUDIO_METER_INSET, audioMeterY + AUDIO_METER_INSET, - audioLevel, AUDIO_METER_HEIGHT - AUDIO_METER_INSET); + audioLevel, AUDIO_METER_HEIGHT - AUDIO_METER_INSET, + _audioBlueQuad); } void ApplicationOverlay::renderStatsAndLogs() { diff --git a/interface/src/ui/ApplicationOverlay.h b/interface/src/ui/ApplicationOverlay.h index 269adef4f3..20d38c879c 100644 --- a/interface/src/ui/ApplicationOverlay.h +++ b/interface/src/ui/ApplicationOverlay.h @@ -79,8 +79,9 @@ private: VerticesIndices _vbo; }; + void renderReticle(glm::quat orientation, float alpha); void renderPointers();; - void renderMagnifier(glm::vec2 magPos, float sizeMult, bool showBorder) const; + void renderMagnifier(glm::vec2 magPos, float sizeMult, bool showBorder); void renderControllerPointers(); void renderPointersOculus(const glm::vec3& eyePos); @@ -106,6 +107,12 @@ private: float _trailingAudioLoudness; GLuint _crosshairTexture; + + int _reticleQuad; + int _magnifierQuad; + int _audioRedQuad; + int _audioGreenQuad; + int _audioBlueQuad; }; #endif // hifi_ApplicationOverlay_h diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index a1cadf2c20..2eb54331c8 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -23,7 +23,13 @@ #include "TextureCache.h" #include "GeometryCache.h" -GeometryCache::GeometryCache() { +//#define WANT_DEBUG + +const int GeometryCache::UNKNOWN_QUAD_ID = -1; + +GeometryCache::GeometryCache() : + _nextQuadID(0) +{ } GeometryCache::~GeometryCache() { @@ -661,9 +667,23 @@ void GeometryCache::renderWireCube(float size) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight) { +void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, int quadID) { + + bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); Vec2Pair key(topLeft, bottomRight); - VerticesIndices& vbo = _quad2DVBOs[key]; + VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DVBOs[key]; + + // if this is a registered quad, and we have buffers, then clear them and rebuild + // TODO: would be nice to only rebuild if the geometry changed from last time. + if (registeredQuad && vbo.first != 0) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec2... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } + const int FLOATS_PER_VERTEX = 2; const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; @@ -700,7 +720,11 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom delete[] indexData; #ifdef WANT_DEBUG - qDebug() << "new quad VBO made -- _quad2DVBOs.size():" << _quad2DVBOs.size(); + if (quadID == UNKNOWN_QUAD_ID) { + qDebug() << "new quad VBO made -- _quad2DVBOs.size():" << _quad2DVBOs.size(); + } else { + qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); + } #endif } else { @@ -718,10 +742,23 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, - const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight) { + const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight, int quadID) { + + bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); Vec2PairPair key(Vec2Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); + VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DTextureVBOs[key]; - VerticesIndices& vbo = _quad2DTextureVBOs[key]; + // if this is a registered quad, and we have buffers, then clear them and rebuild + // TODO: would be nice to only rebuild if the geometry changed from last time. + if (registeredQuad && vbo.first != 0) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec2 + texture... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } + const int FLOATS_PER_VERTEX = 2 * 2; // text coords & vertices const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; @@ -770,7 +807,11 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom delete[] indexData; #ifdef WANT_DEBUG - qDebug() << "new quad + texture VBO made -- _quad2DTextureVBOs.size():" << _quad2DTextureVBOs.size(); + if (quadID == UNKNOWN_QUAD_ID) { + qDebug() << "new quad + texture VBO made -- _quad2DTextureVBOs.size():" << _quad2DTextureVBOs.size(); + } else { + qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); + } #endif } else { glBindBuffer(GL_ARRAY_BUFFER, vbo.first); @@ -791,9 +832,23 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight) { +void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, int quadID) { + + bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); Vec3Pair key(topLeft, bottomRight); - VerticesIndices& vbo = _quad3DVBOs[key]; + VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DVBOs[key]; + + // if this is a registered quad, and we have buffers, then clear them and rebuild + // TODO: would be nice to only rebuild if the geometry changed from last time. + if (registeredQuad && vbo.first != 0) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec3... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } + const int FLOATS_PER_VERTEX = 3; const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; @@ -838,7 +893,11 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom delete[] indexData; #ifdef WANT_DEBUG - qDebug() << "new quad VBO made -- _quad3DVBOs.size():" << _quad3DVBOs.size(); + if (quadID == UNKNOWN_QUAD_ID) { + qDebug() << "new quad VBO made -- _quad3DVBOs.size():" << _quad3DVBOs.size(); + } else { + qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); + } #endif } else { @@ -858,7 +917,7 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft, const glm::vec3& bottomRight, const glm::vec3& topRight, const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomLeft, - const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight) { + const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, int quadID) { #ifdef WANT_DEBUG qDebug() << "renderQuad() vec3 + texture VBO..."; @@ -869,10 +928,22 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom qDebug() << " texCoordTopLeft:" << texCoordTopLeft; qDebug() << " texCoordBottomRight:" << texCoordBottomRight; #endif //def WANT_DEBUG - - Vec3PairVec2Pair key(Vec3Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); - VerticesIndices& vbo = _quad3DTextureVBOs[key]; + bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); + Vec3PairVec2Pair key(Vec3Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); + VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DTextureVBOs[key]; + + // if this is a registered quad, and we have buffers, then clear them and rebuild + // TODO: would be nice to only rebuild if the geometry changed from last time. + if (registeredQuad && vbo.first != 0) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec3 + texture VBO... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } + const int FLOATS_PER_VERTEX = 5; // text coords & vertices const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; @@ -925,7 +996,11 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom delete[] indexData; #ifdef WANT_DEBUG - qDebug() << " _quad3DTextureVBOs.size():" << _quad3DTextureVBOs.size(); + if (quadID == UNKNOWN_QUAD_ID) { + qDebug() << " _quad3DTextureVBOs.size():" << _quad3DTextureVBOs.size(); + } else { + qDebug() << "new registered quad VBO made -- _registeredQuadVBOs.size():" << _registeredQuadVBOs.size(); + } #endif } else { glBindBuffer(GL_ARRAY_BUFFER, vbo.first); diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index 9a76973f77..5736cea116 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -90,23 +90,23 @@ public: void renderSolidCube(float size); void renderWireCube(float size); + int allocateQuad() { return _nextQuadID++; } + static const int UNKNOWN_QUAD_ID; - void renderQuad(int x, int y, int width, int height) { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height)); } - void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight); + void renderQuad(int x, int y, int width, int height, int quadID = UNKNOWN_QUAD_ID) + { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height), quadID); } + + void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, int quadID = UNKNOWN_QUAD_ID); void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, - const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight); + const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight, int quadID = UNKNOWN_QUAD_ID); - - void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight); - - //void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, - // const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight); + void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, int quadID = UNKNOWN_QUAD_ID); void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft, const glm::vec3& bottomRight, const glm::vec3& topRight, const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomLeft, - const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight); + const glm::vec2& texCoordBottomRight, const glm::vec2& texCoordTopRight, int quadID = UNKNOWN_QUAD_ID); /// Loads geometry from the specified URL. /// \param fallback a fallback URL to load if the desired one is unavailable @@ -136,6 +136,8 @@ private: QHash _quad2DTextureVBOs; QHash _quad3DVBOs; QHash _quad3DTextureVBOs; + QHash _registeredQuadVBOs; + int _nextQuadID; QHash _gridBuffers; From 28569836bb1b0ae9aa1b13d0564ebeebb8dba9c9 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 30 Dec 2014 13:24:31 -0800 Subject: [PATCH 2/3] rename parameters and reuse previously registered quad if geometry didn't change --- libraries/render-utils/src/GeometryCache.cpp | 161 +++++++++++-------- libraries/render-utils/src/GeometryCache.h | 13 +- 2 files changed, 107 insertions(+), 67 deletions(-) diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 2eb54331c8..a7ff3827f0 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -667,20 +667,28 @@ void GeometryCache::renderWireCube(float size) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, int quadID) { +void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int quadID) { bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); - Vec2Pair key(topLeft, bottomRight); + Vec2Pair key(minCorner, maxCorner); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DVBOs[key]; // if this is a registered quad, and we have buffers, then clear them and rebuild // TODO: would be nice to only rebuild if the geometry changed from last time. if (registeredQuad && vbo.first != 0) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + Vec2Pair& lastKey = _lastRegisteredQuad2D[quadID]; + if (lastKey != key) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec2... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec2... RELEASING REGISTERED QUAD"; + else { + qDebug() << "renderQuad() vec2... REUSING PREVIOUSLY REGISTERED QUAD"; + } #endif // def WANT_DEBUG } @@ -688,20 +696,21 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; const int indices = 4; - if (vbo.first == 0) { + if (vbo.first == 0) { + _lastRegisteredQuad2D[quadID] = key; int vertexPoints = vertices * FLOATS_PER_VERTEX; GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad GLfloat* vertex = vertexData; static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; - vertex[0] = topLeft.x; - vertex[1] = topLeft.y; - vertex[2] = bottomRight.x; - vertex[3] = topLeft.y; - vertex[4] = bottomRight.x; - vertex[5] = bottomRight.y; - vertex[6] = topLeft.x; - vertex[7] = bottomRight.y; + vertex[0] = minCorner.x; + vertex[1] = minCorner.y; + vertex[2] = maxCorner.x; + vertex[3] = minCorner.y; + vertex[4] = maxCorner.x; + vertex[5] = maxCorner.y; + vertex[6] = minCorner.x; + vertex[7] = maxCorner.y; glGenBuffers(1, &vbo.first); glBindBuffer(GL_ARRAY_BUFFER, vbo.first); @@ -741,21 +750,28 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom } -void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, - const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight, int quadID) { +void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, + const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, int quadID) { bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); - Vec2PairPair key(Vec2Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); + Vec2PairPair key(Vec2Pair(minCorner, maxCorner), Vec2Pair(texCoordMinCorner, texCoordMaxCorner)); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DTextureVBOs[key]; // if this is a registered quad, and we have buffers, then clear them and rebuild - // TODO: would be nice to only rebuild if the geometry changed from last time. if (registeredQuad && vbo.first != 0) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + Vec2PairPair& lastKey = _lastRegisteredQuad2DTexture[quadID]; + if (lastKey != key) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec2 + texture... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec2 + texture... RELEASING REGISTERED QUAD"; + else { + qDebug() << "renderQuad() vec2 + texture... REUSING PREVIOUSLY REGISTERED QUAD"; + } #endif // def WANT_DEBUG } @@ -763,32 +779,33 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; const int indices = 4; - if (vbo.first == 0) { + if (vbo.first == 0) { + _lastRegisteredQuad2DTexture[quadID] = key; int vertexPoints = vertices * FLOATS_PER_VERTEX; GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices GLfloat* vertex = vertexData; static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; int v = 0; - vertex[v++] = topLeft.x; - vertex[v++] = topLeft.y; - vertex[v++] = texCoordTopLeft.x; - vertex[v++] = texCoordTopLeft.y; + vertex[v++] = minCorner.x; + vertex[v++] = minCorner.y; + vertex[v++] = texCoordMinCorner.x; + vertex[v++] = texCoordMinCorner.y; - vertex[v++] = bottomRight.x; - vertex[v++] = topLeft.y; - vertex[v++] = texCoordBottomRight.x; - vertex[v++] = texCoordTopLeft.y; + vertex[v++] = maxCorner.x; + vertex[v++] = minCorner.y; + vertex[v++] = texCoordMaxCorner.x; + vertex[v++] = texCoordMinCorner.y; - vertex[v++] = bottomRight.x; - vertex[v++] = bottomRight.y; - vertex[v++] = texCoordBottomRight.x; - vertex[v++] = texCoordBottomRight.y; + vertex[v++] = maxCorner.x; + vertex[v++] = maxCorner.y; + vertex[v++] = texCoordMaxCorner.x; + vertex[v++] = texCoordMaxCorner.y; - vertex[v++] = topLeft.x; - vertex[v++] = bottomRight.y; - vertex[v++] = texCoordTopLeft.x; - vertex[v++] = texCoordBottomRight.y; + vertex[v++] = minCorner.x; + vertex[v++] = maxCorner.y; + vertex[v++] = texCoordMinCorner.x; + vertex[v++] = texCoordMaxCorner.y; glGenBuffers(1, &vbo.first); glBindBuffer(GL_ARRAY_BUFFER, vbo.first); @@ -832,20 +849,28 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, int quadID) { +void GeometryCache::renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, int quadID) { bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); - Vec3Pair key(topLeft, bottomRight); + Vec3Pair key(minCorner, maxCorner); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DVBOs[key]; // if this is a registered quad, and we have buffers, then clear them and rebuild // TODO: would be nice to only rebuild if the geometry changed from last time. if (registeredQuad && vbo.first != 0) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + Vec3Pair& lastKey = _lastRegisteredQuad3D[quadID]; + if (lastKey != key) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec3... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec3... RELEASING REGISTERED QUAD"; + else { + qDebug() << "renderQuad() vec3... REUSING PREVIOUSLY REGISTERED QUAD"; + } #endif // def WANT_DEBUG } @@ -854,27 +879,28 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom const int vertices = 4; const int indices = 4; if (vbo.first == 0) { + _lastRegisteredQuad3D[quadID] = key; int vertexPoints = vertices * FLOATS_PER_VERTEX; GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices GLfloat* vertex = vertexData; static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; int v = 0; - vertex[v++] = topLeft.x; - vertex[v++] = topLeft.y; - vertex[v++] = topLeft.z; + vertex[v++] = minCorner.x; + vertex[v++] = minCorner.y; + vertex[v++] = minCorner.z; - vertex[v++] = bottomRight.x; - vertex[v++] = topLeft.y; - vertex[v++] = topLeft.z; + vertex[v++] = maxCorner.x; + vertex[v++] = minCorner.y; + vertex[v++] = minCorner.z; - vertex[v++] = bottomRight.x; - vertex[v++] = bottomRight.y; - vertex[v++] = bottomRight.z; + vertex[v++] = maxCorner.x; + vertex[v++] = maxCorner.y; + vertex[v++] = maxCorner.z; - vertex[v++] = topLeft.x; - vertex[v++] = bottomRight.y; - vertex[v++] = bottomRight.z; + vertex[v++] = minCorner.x; + vertex[v++] = maxCorner.y; + vertex[v++] = maxCorner.z; glGenBuffers(1, &vbo.first); glBindBuffer(GL_ARRAY_BUFFER, vbo.first); @@ -936,11 +962,19 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom // if this is a registered quad, and we have buffers, then clear them and rebuild // TODO: would be nice to only rebuild if the geometry changed from last time. if (registeredQuad && vbo.first != 0) { - glDeleteBuffers(1, &vbo.first); - glDeleteBuffers(1, &vbo.second); - vbo.first = vbo.second = 0; + Vec3PairVec2Pair& lastKey = _lastRegisteredQuad3DTexture[quadID]; + if (lastKey != key) { + glDeleteBuffers(1, &vbo.first); + glDeleteBuffers(1, &vbo.second); + vbo.first = vbo.second = 0; + #ifdef WANT_DEBUG + qDebug() << "renderQuad() vec3 + texture VBO... RELEASING REGISTERED QUAD"; + #endif // def WANT_DEBUG + } #ifdef WANT_DEBUG - qDebug() << "renderQuad() vec3 + texture VBO... RELEASING REGISTERED QUAD"; + else { + qDebug() << "renderQuad() vec3 + texture... REUSING PREVIOUSLY REGISTERED QUAD"; + } #endif // def WANT_DEBUG } @@ -948,7 +982,8 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; const int indices = 4; - if (vbo.first == 0) { + if (vbo.first == 0) { + _lastRegisteredQuad3DTexture[quadID] = key; int vertexPoints = vertices * FLOATS_PER_VERTEX; GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices GLfloat* vertex = vertexData; diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index 5736cea116..f7b4489cc8 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -96,12 +96,12 @@ public: void renderQuad(int x, int y, int width, int height, int quadID = UNKNOWN_QUAD_ID) { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height), quadID); } - void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, int quadID = UNKNOWN_QUAD_ID); + void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int quadID = UNKNOWN_QUAD_ID); - void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, - const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight, int quadID = UNKNOWN_QUAD_ID); + void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, + const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, int quadID = UNKNOWN_QUAD_ID); - void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, int quadID = UNKNOWN_QUAD_ID); + void renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, int quadID = UNKNOWN_QUAD_ID); void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft, const glm::vec3& bottomRight, const glm::vec3& topRight, @@ -139,6 +139,11 @@ private: QHash _registeredQuadVBOs; int _nextQuadID; + QHash _lastRegisteredQuad2D; + QHash _lastRegisteredQuad2DTexture; + QHash _lastRegisteredQuad3D; + QHash _lastRegisteredQuad3DTexture; + QHash _gridBuffers; QHash > _networkGeometry; From 4e6ed4a449419392e2c4de800f196b219e46bf3c Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 30 Dec 2014 13:26:13 -0800 Subject: [PATCH 3/3] fix comments --- libraries/render-utils/src/GeometryCache.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index a7ff3827f0..7e415a5d0d 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -673,8 +673,7 @@ void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxC Vec2Pair key(minCorner, maxCorner); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DVBOs[key]; - // if this is a registered quad, and we have buffers, then clear them and rebuild - // TODO: would be nice to only rebuild if the geometry changed from last time. + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed if (registeredQuad && vbo.first != 0) { Vec2Pair& lastKey = _lastRegisteredQuad2D[quadID]; if (lastKey != key) { @@ -757,7 +756,7 @@ void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxC Vec2PairPair key(Vec2Pair(minCorner, maxCorner), Vec2Pair(texCoordMinCorner, texCoordMaxCorner)); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DTextureVBOs[key]; - // if this is a registered quad, and we have buffers, then clear them and rebuild + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed if (registeredQuad && vbo.first != 0) { Vec2PairPair& lastKey = _lastRegisteredQuad2DTexture[quadID]; if (lastKey != key) { @@ -855,8 +854,7 @@ void GeometryCache::renderQuad(const glm::vec3& minCorner, const glm::vec3& maxC Vec3Pair key(minCorner, maxCorner); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DVBOs[key]; - // if this is a registered quad, and we have buffers, then clear them and rebuild - // TODO: would be nice to only rebuild if the geometry changed from last time. + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed if (registeredQuad && vbo.first != 0) { Vec3Pair& lastKey = _lastRegisteredQuad3D[quadID]; if (lastKey != key) { @@ -959,8 +957,7 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom Vec3PairVec2Pair key(Vec3Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DTextureVBOs[key]; - // if this is a registered quad, and we have buffers, then clear them and rebuild - // TODO: would be nice to only rebuild if the geometry changed from last time. + // if this is a registered quad, and we have buffers, then check to see if the geometry changed and rebuild if needed if (registeredQuad && vbo.first != 0) { Vec3PairVec2Pair& lastKey = _lastRegisteredQuad3DTexture[quadID]; if (lastKey != key) {