diff options
author | Emmanuel Gil Peyrot <emmanuel.peyrot@collabora.com> | 2015-12-09 21:39:28 +0000 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.com> | 2015-12-11 15:29:00 +0100 |
commit | 463739f44e75798dbdb1239c7374ed5254a9e9d4 (patch) | |
tree | 670585678b89f492d0f356ce2cc2c114610eac1e /slideshow | |
parent | c831e4851f35ddef5ef45062ba291d07dda4b1b4 (diff) |
slideshow: Only upload vertex data on prepare
Change-Id: If87f441607adf2ed0aa31023352774e7372b7edb
Diffstat (limited to 'slideshow')
-rw-r--r-- | slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.cxx | 117 | ||||
-rw-r--r-- | slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.hxx | 5 |
2 files changed, 87 insertions, 35 deletions
diff --git a/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.cxx b/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.cxx index 649190af242a..d277aa26b7db 100644 --- a/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.cxx +++ b/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.cxx @@ -111,15 +111,41 @@ void OGLTransitionImpl::uploadModelViewProjectionMatrices() } } +static std::vector<int> uploadPrimitives(const Primitives_t& primitives) +{ + int size = 0; + for (const Primitive& primitive: primitives) + size += primitive.getVerticesSize(); + + CHECK_GL_ERROR(); + glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW); + CHECK_GL_ERROR(); + Vertex *buf = static_cast<Vertex*>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)); + + std::vector<int> indices; + int last_pos = 0; + for (const Primitive& primitive: primitives) { + indices.push_back(last_pos); + int num = primitive.writeVertices(buf); + buf += num; + last_pos += num; + } + + CHECK_GL_ERROR(); + glUnmapBuffer(GL_ARRAY_BUFFER); + CHECK_GL_ERROR(); + return indices; +} + void OGLTransitionImpl::prepare( sal_Int32 glLeavingSlideTex, sal_Int32 glEnteringSlideTex ) { + m_nProgramObject = makeShader(); + const SceneObjects_t& rSceneObjects(maScene.getSceneObjects()); for(size_t i(0); i != rSceneObjects.size(); ++i) { - rSceneObjects[i]->prepare(); + rSceneObjects[i]->prepare(m_nProgramObject); } - m_nProgramObject = makeShader(); - CHECK_GL_ERROR(); if( m_nProgramObject ) { glUseProgram( m_nProgramObject ); @@ -149,6 +175,9 @@ void OGLTransitionImpl::prepare( sal_Int32 glLeavingSlideTex, sal_Int32 glEnteri glGenBuffers(1, &m_nVertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, m_nVertexBufferObject); + // In practice both leaving and entering slides share the same primitives. + m_nFirstIndices = uploadPrimitives(getScene().getLeavingSlide()); + // Attribute bindings m_nPositionLocation = glGetAttribLocation(m_nProgramObject, "a_position"); if (m_nPositionLocation != -1) { @@ -170,6 +199,8 @@ void OGLTransitionImpl::prepare( sal_Int32 glLeavingSlideTex, sal_Int32 glEnteri glVertexAttribPointer( m_nTexCoordLocation, 2, GL_FLOAT, false, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texcoord)) ); CHECK_GL_ERROR(); } + + glBindBuffer(GL_ARRAY_BUFFER, 0); } CHECK_GL_ERROR(); @@ -240,6 +271,7 @@ void OGLTransitionImpl::display( double nTime, sal_Int32 glLeavingSlideTex, sal_ const double SlideHeightScale = SlideHeight/DispHeight; CHECK_GL_ERROR(); + glBindVertexArray(m_nVertexArrayObject); prepare( nTime, SlideWidth, SlideHeight, DispWidth, DispHeight ); CHECK_GL_ERROR(); @@ -260,30 +292,8 @@ void OGLTransitionImpl::applyOverallOperations( double nTime, double SlideWidthS CHECK_GL_ERROR(); } -static void display_primitives(const Primitives_t& primitives, GLint primitiveTransformLocation, double nTime, double WidthScale, double HeightScale) +static void displayPrimitives(const Primitives_t& primitives, GLint primitiveTransformLocation, double nTime, double WidthScale, double HeightScale, std::vector<int>::const_iterator first) { - int size = 0; - for (const Primitive& primitive: primitives) - size += primitive.getVerticesSize(); - - CHECK_GL_ERROR(); - glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STREAM_DRAW); - CHECK_GL_ERROR(); - Vertex *buf = static_cast<Vertex*>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)); - - std::vector<int> first_elements; - int last_pos = 0; - for (const Primitive& primitive: primitives) { - first_elements.push_back(last_pos); - int num = primitive.writeVertices(buf); - buf += num; - last_pos += num; - } - auto first = first_elements.begin(); - - CHECK_GL_ERROR(); - glUnmapBuffer(GL_ARRAY_BUFFER); - for (const Primitive& primitive: primitives) primitive.display(primitiveTransformLocation, nTime, WidthScale, HeightScale, *first++); } @@ -299,7 +309,7 @@ OGLTransitionImpl::displaySlide( CHECK_GL_ERROR(); glUniformMatrix4fv(m_nSceneTransformLocation, 1, false, glm::value_ptr(glm::mat4())); CHECK_GL_ERROR(); - display_primitives(primitives, m_nPrimitiveTransformLocation, nTime, SlideWidthScale, SlideHeightScale); + displayPrimitives(primitives, m_nPrimitiveTransformLocation, nTime, SlideWidthScale, SlideHeightScale, m_nFirstIndices.cbegin()); CHECK_GL_ERROR(); } @@ -344,7 +354,7 @@ void SceneObject::display(GLint sceneTransformLocation, GLint primitiveTransform CHECK_GL_ERROR(); glUniformMatrix4fv(sceneTransformLocation, 1, false, glm::value_ptr(matrix)); CHECK_GL_ERROR(); - display_primitives(maPrimitives, primitiveTransformLocation, nTime, 1, 1); + displayPrimitives(maPrimitives, primitiveTransformLocation, nTime, 1, 1, maFirstIndices.cbegin()); CHECK_GL_ERROR(); } @@ -370,22 +380,26 @@ class Iris : public SceneObject public: Iris() = default; - virtual void prepare() override; + virtual void prepare(GLuint program) override; virtual void display(GLint sceneTransformLocation, GLint primitiveTransformLocation, double nTime, double SlideWidth, double SlideHeight, double DispWidth, double DispHeight) const override; virtual void finish() override; private: GLuint maTexture = 0; + GLuint maBuffer = 0; + GLuint maVertexArray = 0; }; void Iris::display(GLint sceneTransformLocation, GLint primitiveTransformLocation, double nTime, double SlideWidth, double SlideHeight, double DispWidth, double DispHeight ) const { + glBindVertexArray(maVertexArray); + CHECK_GL_ERROR(); glBindTexture(GL_TEXTURE_2D, maTexture); CHECK_GL_ERROR(); SceneObject::display(sceneTransformLocation, primitiveTransformLocation, nTime, SlideWidth, SlideHeight, DispWidth, DispHeight); } -void Iris::prepare() +void Iris::prepare(GLuint program) { CHECK_GL_ERROR(); static const GLubyte img[3] = { 80, 80, 80 }; @@ -395,14 +409,49 @@ void Iris::prepare() glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, img); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); CHECK_GL_ERROR(); + + glGenVertexArrays(1, &maVertexArray); + glBindVertexArray(maVertexArray); + + glGenBuffers(1, &maBuffer); + glBindBuffer(GL_ARRAY_BUFFER, maBuffer); + maFirstIndices = uploadPrimitives(maPrimitives); + + // Attribute bindings + GLint location = glGetAttribLocation(program, "a_position"); + if (location != -1) { + glEnableVertexAttribArray(location); + glVertexAttribPointer( location, 3, GL_FLOAT, false, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, position)) ); + CHECK_GL_ERROR(); + } + + location = glGetAttribLocation(program, "a_normal"); + if (location != -1) { + glEnableVertexAttribArray(location); + glVertexAttribPointer( location, 3, GL_FLOAT, false, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, normal)) ); + CHECK_GL_ERROR(); + } + + location = glGetAttribLocation(program, "a_texCoord"); + if (location != -1) { + glEnableVertexAttribArray(location); + glVertexAttribPointer( location, 2, GL_FLOAT, false, sizeof(Vertex), reinterpret_cast<void*>(offsetof(Vertex, texcoord)) ); + CHECK_GL_ERROR(); + } + + glBindBuffer(GL_ARRAY_BUFFER, 0); } void Iris::finish() { CHECK_GL_ERROR(); + glDeleteBuffers(1, &maBuffer); + CHECK_GL_ERROR(); + glDeleteVertexArrays(1, &maVertexArray); + CHECK_GL_ERROR(); glDeleteTextures(1, &maTexture); CHECK_GL_ERROR(); } @@ -1529,7 +1578,7 @@ void VortexTransition::prepare( double, double, double, double, double ) glVertexAttribPointer(mnTileInfoLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr); CHECK_GL_ERROR(); - glBindBuffer(GL_ARRAY_BUFFER, m_nVertexBufferObject); + glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERROR(); } @@ -1592,7 +1641,7 @@ void VortexTransition::prepareTransition( sal_Int32 glLeavingSlideTex, sal_Int32 glBufferData(GL_ARRAY_BUFFER, mvTileInfo.size()*sizeof(GLfloat), mvTileInfo.data(), GL_STATIC_DRAW); CHECK_GL_ERROR(); - glBindBuffer(GL_ARRAY_BUFFER, m_nVertexBufferObject); + glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERROR(); } diff --git a/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.hxx b/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.hxx index 756800657e85..f33968528f37 100644 --- a/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.hxx +++ b/slideshow/source/engine/OGLTrans/generic/OGLTrans_TransitionImpl.hxx @@ -230,6 +230,8 @@ private: GLuint m_nVertexArrayObject = -1; + std::vector<int> m_nFirstIndices; + protected: /** GLSL program object */ @@ -273,7 +275,7 @@ public: SceneObject(); virtual ~SceneObject(); - virtual void prepare() {} + virtual void prepare(GLuint /* program */) {} virtual void display(GLint sceneTransformLocation, GLint primitiveTransformLocation, double nTime, double SlideWidth, double SlideHeight, double DispWidth, double DispHeight ) const; virtual void finish() {} @@ -283,6 +285,7 @@ protected: /** All the surrounding scene primitives */ Primitives_t maPrimitives; + std::vector<int> maFirstIndices; }; struct Vertex |