diff --git a/ext/ghoul b/ext/ghoul index da8cbebb1f..dcc5dcb309 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit da8cbebb1f57f7c90d0af93cf834ced67d3cd843 +Subproject commit dcc5dcb309e3e0716548d693016e6b5b50abf968 diff --git a/include/openspace/interaction/luaconsole.h b/include/openspace/interaction/luaconsole.h index be8964325f..4cc1241e30 100644 --- a/include/openspace/interaction/luaconsole.h +++ b/include/openspace/interaction/luaconsole.h @@ -32,6 +32,8 @@ #include #include +#include + #include #include @@ -90,6 +92,8 @@ private: std::unique_ptr _program; GLuint _vao; GLuint _vbo; + + UniformCache(res, color, height, ortho) _uniformCache; }; } // namespace openspace diff --git a/include/openspace/rendering/framebufferrenderer.h b/include/openspace/rendering/framebufferrenderer.h index bdd057f473..6f61cd2238 100644 --- a/include/openspace/rendering/framebufferrenderer.h +++ b/include/openspace/rendering/framebufferrenderer.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -112,6 +113,7 @@ private: std::unique_ptr _hdrBackGroundProgram; std::unique_ptr _resolveProgram; + UniformCache(mainColorTexture, blackoutFactor, nAaSamples) _uniformCache; GLuint _screenQuad; GLuint _vertexPositionBuffer; diff --git a/include/openspace/rendering/loadingscreen.h b/include/openspace/rendering/loadingscreen.h index 48bff9f6f9..7b4ba69b2b 100644 --- a/include/openspace/rendering/loadingscreen.h +++ b/include/openspace/rendering/loadingscreen.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -95,6 +96,8 @@ private: int _nItems; std::unique_ptr _program; + UniformCache(logoTexture, useTexture, color) _uniformCache; + std::unique_ptr _logoTexture; std::shared_ptr _loadingFont; diff --git a/include/openspace/rendering/screenspacerenderable.h b/include/openspace/rendering/screenspacerenderable.h index 8e6cb31151..cd399da474 100644 --- a/include/openspace/rendering/screenspacerenderable.h +++ b/include/openspace/rendering/screenspacerenderable.h @@ -35,6 +35,7 @@ #include #include #include +#include #include @@ -112,6 +113,7 @@ protected: GLuint _quad; GLuint _vertexPositionBuffer; std::unique_ptr _texture; + UniformCache(occlusionDepth, alpha, modelTransform, viewProj, texture) _uniformCache; std::unique_ptr _shader; bool _useEuclideanCoordinates; diff --git a/modules/base/rendering/renderablemodel.cpp b/modules/base/rendering/renderablemodel.cpp index 6b8ef4aaaf..9f94d6a74f 100644 --- a/modules/base/rendering/renderablemodel.cpp +++ b/modules/base/rendering/renderablemodel.cpp @@ -163,6 +163,19 @@ void RenderableModel::initializeGL() { absPath("${MODULE_BASE}/shaders/model_fs.glsl") ); + _uniformCache.directionToSunViewSpace = _programObject->uniformLocation( + "directionToSunViewSpace" + ); + _uniformCache.modelViewTransform = _programObject->uniformLocation( + "modelViewTransform" + ); + _uniformCache.projectionTransform = _programObject->uniformLocation( + "projectionTransform" + ); + _uniformCache.performShading = _programObject->uniformLocation( + "performShading" + ); + _uniformCache.texture = _programObject->uniformLocation("texture1"); loadTexture(); _geometry->initialize(this); @@ -197,10 +210,22 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { glm::vec3 directionToSunViewSpace = glm::mat3(data.camera.combinedViewMatrix()) * directionToSun; - _programObject->setUniform("directionToSunViewSpace", directionToSunViewSpace); - _programObject->setUniform("modelViewTransform", glm::mat4(modelViewTransform)); - _programObject->setUniform("projectionTransform", data.camera.projectionMatrix()); - _programObject->setUniform("performShading", _performShading); + _programObject->setUniform( + _uniformCache.directionToSunViewSpace, + directionToSunViewSpace + ); + _programObject->setUniform( + _uniformCache.modelViewTransform, + glm::mat4(modelViewTransform) + ); + _programObject->setUniform( + _uniformCache.projectionTransform, + data.camera.projectionMatrix() + ); + _programObject->setUniform( + _uniformCache.performShading, + _performShading + ); _geometry->setUniforms(*_programObject); @@ -208,7 +233,7 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { ghoul::opengl::TextureUnit unit; unit.activate(); _texture->bind(); - _programObject->setUniform("texture1", unit); + _programObject->setUniform(_uniformCache.texture, unit); _geometry->render(); @@ -218,6 +243,20 @@ void RenderableModel::render(const RenderData& data, RendererTasks&) { void RenderableModel::update(const UpdateData&) { if (_programObject->isDirty()) { _programObject->rebuildFromFile(); + + _uniformCache.directionToSunViewSpace = _programObject->uniformLocation( + "directionToSunViewSpace" + ); + _uniformCache.modelViewTransform = _programObject->uniformLocation( + "modelViewTransform" + ); + _uniformCache.projectionTransform = _programObject->uniformLocation( + "projectionTransform" + ); + _uniformCache.performShading = _programObject->uniformLocation( + "performShading" + ); + _uniformCache.texture = _programObject->uniformLocation("texture1"); } _sunPos = OsEng.renderEngine().scene()->sceneGraphNode( diff --git a/modules/base/rendering/renderablemodel.h b/modules/base/rendering/renderablemodel.h index 7c65bc0fcb..433aa30b2a 100644 --- a/modules/base/rendering/renderablemodel.h +++ b/modules/base/rendering/renderablemodel.h @@ -32,6 +32,8 @@ #include #include +#include + #include namespace ghoul::opengl { @@ -72,6 +74,9 @@ private: properties::Mat3Property _modelTransform; std::unique_ptr _programObject; + UniformCache(directionToSunViewSpace, modelViewTransform, projectionTransform, + performShading, texture) _uniformCache; + std::unique_ptr _texture; glm::dvec3 _sunPos; diff --git a/modules/base/rendering/renderabletrail.cpp b/modules/base/rendering/renderabletrail.cpp index 19b50672c5..9301f7c939 100644 --- a/modules/base/rendering/renderabletrail.cpp +++ b/modules/base/rendering/renderabletrail.cpp @@ -226,6 +226,18 @@ void RenderableTrail::initializeGL() { absPath("${MODULE_BASE}/shaders/renderabletrail_fs.glsl") ); + _uniformCache.modelView = _programObject->uniformLocation("modelViewTransform"); + _uniformCache.projection = _programObject->uniformLocation("projectionTransform"); + _uniformCache.color = _programObject->uniformLocation("color"); + _uniformCache.useLineFade = _programObject->uniformLocation("useLineFade"); + _uniformCache.lineFade = _programObject->uniformLocation("lineFade"); + _uniformCache.vertexSorting = _programObject->uniformLocation("vertexSortingMethod"); + _uniformCache.idOffset = _programObject->uniformLocation("idOffset"); + _uniformCache.nVertices = _programObject->uniformLocation("nVertices"); + _uniformCache.stride = _programObject->uniformLocation("stride"); + _uniformCache.pointSize = _programObject->uniformLocation("pointSize"); + _uniformCache.renderPhase = _programObject->uniformLocation("renderPhase"); + setRenderBin(Renderable::RenderBin::Overlay); } @@ -249,12 +261,12 @@ void RenderableTrail::render(const RenderData& data, RendererTasks&) { glm::dmat4(data.modelTransform.rotation) * glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale)); - _programObject->setUniform("projectionTransform", data.camera.projectionMatrix()); + _programObject->setUniform(_uniformCache.projection, data.camera.projectionMatrix()); - _programObject->setUniform("color", _lineColor); - _programObject->setUniform("useLineFade", _useLineFade); + _programObject->setUniform(_uniformCache.color, _lineColor); + _programObject->setUniform(_uniformCache.useLineFade, _useLineFade); if (_useLineFade) { - _programObject->setUniform("lineFade", _lineFade); + _programObject->setUniform(_uniformCache.lineFade, _lineFade); } static std::map SortingMapping = { @@ -289,38 +301,32 @@ void RenderableTrail::render(const RenderData& data, RendererTasks&) { } auto render = [renderLines, renderPoints, p = _programObject.get(), &data, - &modelTransform, pointSize = _pointSize.value()] + &modelTransform, pointSize = _pointSize.value(), c = _uniformCache] (RenderInformation& info, int nVertices, int offset) { // We pass in the model view transformation matrix as double in order to maintain // high precision for vertices; especially for the trails, a high vertex precision // is necessary as they are usually far away from their reference p->setUniform( - "modelViewTransform", + c.modelView, data.camera.combinedViewMatrix() * modelTransform * info._localTransform ); // The vertex sorting method is used to tweak the fading along the trajectory - p->setUniform( - "vertexSortingMethod", - SortingMapping[info.sorting] - ); + p->setUniform(c.vertexSorting, SortingMapping[info.sorting]); // This value is subtracted from the vertex id in the case of a potential ring // buffer (as used in RenderableTrailOrbit) to keep the first vertex at its // brightest - p->setUniform( - "idOffset", - offset - ); + p->setUniform(c.idOffset, offset); - p->setUniform("nVertices", nVertices); + p->setUniform(c.nVertices, nVertices); if (renderPoints) { // The stride parameter determines the distance between larger points and // smaller ones - p->setUniform("stride", info.stride); - p->setUniform("pointSize", pointSize); + p->setUniform(c.stride, info.stride); + p->setUniform(c.pointSize, pointSize); } // Fragile! Keep in sync with fragment shader @@ -331,7 +337,7 @@ void RenderableTrail::render(const RenderData& data, RendererTasks&) { glBindVertexArray(info._vaoID); if (renderLines) { - p->setUniform("renderPhase", RenderPhaseLines); + p->setUniform(c.renderPhase, RenderPhaseLines); // Subclasses of this renderer might be using the index array or might now be // so we check if there is data available and if there isn't, we use the // glDrawArrays draw call; otherwise the glDrawElements @@ -355,7 +361,7 @@ void RenderableTrail::render(const RenderData& data, RendererTasks&) { // Subclasses of this renderer might be using the index array or might now be // so we check if there is data available and if there isn't, we use the // glDrawArrays draw call; otherwise the glDrawElements - p->setUniform("renderPhase", RenderPhasePoints); + p->setUniform(c.renderPhase, RenderPhasePoints); if (info._iBufferID == 0) { glDrawArrays(GL_POINTS, info.first, info.count); } diff --git a/modules/base/rendering/renderabletrail.h b/modules/base/rendering/renderabletrail.h index cbd5daca55..70ae56812f 100644 --- a/modules/base/rendering/renderabletrail.h +++ b/modules/base/rendering/renderabletrail.h @@ -35,6 +35,7 @@ #include #include +#include namespace ghoul::opengl { class ProgramObject; @@ -158,6 +159,9 @@ private: /// Program object used to render the data stored in RenderInformation std::unique_ptr _programObject; + + UniformCache(modelView, projection, color, useLineFade, lineFade, vertexSorting, + idOffset, nVertices, stride, pointSize, renderPhase) _uniformCache; }; } // namespace openspace diff --git a/modules/digitaluniverse/rendering/renderablebillboardscloud.cpp b/modules/digitaluniverse/rendering/renderablebillboardscloud.cpp index 7ad61eb643..5177a0c6f7 100644 --- a/modules/digitaluniverse/rendering/renderablebillboardscloud.cpp +++ b/modules/digitaluniverse/rendering/renderablebillboardscloud.cpp @@ -632,6 +632,32 @@ void RenderableBillboardsCloud::initializeGL() { absPath("${MODULE_DIGITALUNIVERSE}/shaders/billboard2_gs.glsl") ); + //_uniformCache.projection = _program->uniformLocation("projection"); + //_uniformCache.modelView = _program->uniformLocation("modelViewTransform"); + _uniformCache.modelViewProjection = _program->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.cameraPos = _program->uniformLocation("cameraPosition"); + _uniformCache.cameraLookup = _program->uniformLocation("cameraLookUp"); + _uniformCache.renderOption = _program->uniformLocation("renderOption"); + _uniformCache.centerSceenInWorldPos = _program->uniformLocation( + "centerScreenInWorldPosition" + ); + _uniformCache.minBillboardSize = _program->uniformLocation("minBillboardSize"); + _uniformCache.maxBillboardSize = _program->uniformLocation("maxBillboardSize"); + _uniformCache.color = _program->uniformLocation("color"); + //_uniformCache.sides = _program->uniformLocation("sides"); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + _uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.up = _program->uniformLocation("up"); + _uniformCache.right = _program->uniformLocation("right"); + _uniformCache.fadeInValue = _program->uniformLocation("fadeInValue"); + _uniformCache.screenSize = _program->uniformLocation("screenSize"); + _uniformCache.spriteTexture = _program->uniformLocation("spriteTexture"); + _uniformCache.polygonTexture = _program->uniformLocation("polygonTexture"); + _uniformCache.hasPolygon = _program->uniformLocation("hasPolygon"); + _uniformCache.hasColormap = _program->uniformLocation("hasColorMap"); + if (_hasPolygon) { createPolygonTexture(); } @@ -705,71 +731,68 @@ void RenderableBillboardsCloud::renderBillboards(const RenderData& data, _program->activate(); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::Yes); - glm::dmat4 projMatrix = glm::dmat4(data.camera.projectionMatrix()); _program->setUniform( "screenSize", glm::vec2(OsEng.renderEngine().renderingResolution()) ); - _program->setUniform("projection", projMatrix); - _program->setUniform("modelViewTransform", modelViewMatrix); - _program->setUniform("modelViewProjectionTransform", projMatrix * modelViewMatrix); - _program->setUniform("cameraPosition", glm::dvec3(worldToModelTransform * - glm::dvec4(data.camera.positionVec3(), 1.0))); - _program->setUniform("cameraLookUp", glm::dvec3(worldToModelTransform * - glm::dvec4(data.camera.lookUpVectorWorldSpace(), 1.0))); + //_program->setUniform(_uniformCache.projection, projMatrix); + //_program->setUniform(_uniformCache.modelView, modelViewMatrix); + _program->setUniform(_uniformCache.modelViewProjection, projMatrix * modelViewMatrix); + _program->setUniform( + _uniformCache.cameraPos, + glm::dvec3(worldToModelTransform * glm::dvec4(data.camera.positionVec3(), 1.0)) + ); + _program->setUniform( + _uniformCache.cameraLookup, + glm::dvec3(worldToModelTransform * + glm::dvec4(data.camera.lookUpVectorWorldSpace(), 1.0) + ) + ); //_program->setUniform("cameraPosition", data.camera.positionVec3()); //_program->setUniform("cameraLookUp", data.camera.lookUpVectorWorldSpace()); - _program->setUniform("renderOption", _renderOption.value()); + _program->setUniform(_uniformCache.renderOption, _renderOption.value()); glm::dvec4 centerScreenWorld = glm::inverse(data.camera.combinedViewMatrix()) * glm::dvec4(0.0, 0.0, 0.0, 1.0); - _program->setUniform("centerScreenInWorldPosition", centerScreenWorld); + _program->setUniform(_uniformCache.centerSceenInWorldPos, centerScreenWorld); - _program->setUniform("minBillboardSize", _billboardMinSize); // in pixels - _program->setUniform("maxBillboardSize", _billboardMaxSize); // in pixels - _program->setUniform("color", _pointColor); - _program->setUniform("sides", 4); - _program->setUniform("alphaValue", _alphaValue); - _program->setUniform("scaleFactor", _scaleFactor); + _program->setUniform(_uniformCache.minBillboardSize, _billboardMinSize); // in pixels + _program->setUniform(_uniformCache.maxBillboardSize, _billboardMaxSize); // in pixels + _program->setUniform(_uniformCache.color, _pointColor); + //_program->setUniform(_uniformCache.sides, 4); + _program->setUniform(_uniformCache.alphaValue, _alphaValue); + _program->setUniform(_uniformCache.scaleFactor, _scaleFactor); - _program->setUniform("up", orthoUp); - _program->setUniform("right", orthoRight); + _program->setUniform(_uniformCache.up, orthoUp); + _program->setUniform(_uniformCache.right, orthoRight); - _program->setUniform("fadeInValue", fadeInVariable); + _program->setUniform(_uniformCache.fadeInValue, fadeInVariable); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); - _program->setUniform("screenSize", glm::vec2(viewport[2], viewport[3])); + _program->setUniform(_uniformCache.screenSize, glm::vec2(viewport[2], viewport[3])); ghoul::opengl::TextureUnit spriteTextureUnit; if (_hasSpriteTexture) { spriteTextureUnit.activate(); _spriteTexture->bind(); - _program->setUniform("spriteTexture", spriteTextureUnit); + _program->setUniform(_uniformCache.spriteTexture, spriteTextureUnit); } ghoul::opengl::TextureUnit polygonTextureUnit; if (_hasPolygon) { polygonTextureUnit.activate(); glBindTexture(GL_TEXTURE_2D, _pTexture); - _program->setUniform("polygonTexture", polygonTextureUnit); - _program->setUniform("hasPolygon", _hasPolygon); + _program->setUniform(_uniformCache.polygonTexture, polygonTextureUnit); + _program->setUniform(_uniformCache.hasPolygon, _hasPolygon); } - - if (_hasColorMapFile) { - _program->setUniform("hasColorMap", true); - } - else { - _program->setUniform("hasColorMap", false); - } + _program->setUniform(_uniformCache.hasColormap, _hasColorMapFile); glBindVertexArray(_vao); const GLsizei nAstronomicalObjects = static_cast(_fullData.size() / @@ -777,8 +800,6 @@ void RenderableBillboardsCloud::renderBillboards(const RenderData& data, glDrawArrays(GL_POINTS, 0, nAstronomicalObjects); glBindVertexArray(0); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::No); _program->deactivate(); // Restores blending state diff --git a/modules/digitaluniverse/rendering/renderablebillboardscloud.h b/modules/digitaluniverse/rendering/renderablebillboardscloud.h index ad24e60402..2ff164d619 100644 --- a/modules/digitaluniverse/rendering/renderablebillboardscloud.h +++ b/modules/digitaluniverse/rendering/renderablebillboardscloud.h @@ -35,8 +35,9 @@ #include #include -#include #include +#include +#include #include #include @@ -139,6 +140,10 @@ private: std::unique_ptr _spriteTexture; std::unique_ptr _spriteTextureFile; std::unique_ptr _program; + UniformCache(projection, modelView, modelViewProjection, cameraPos, cameraLookup, + renderOption, centerSceenInWorldPos, minBillboardSize, maxBillboardSize, + color, sides, alphaValue, scaleFactor, up, right, fadeInValue, screenSize, + spriteTexture, polygonTexture, hasPolygon, hasColormap) _uniformCache; std::unique_ptr _fontRenderer; std::shared_ptr _font; diff --git a/modules/digitaluniverse/rendering/renderabledumeshes.cpp b/modules/digitaluniverse/rendering/renderabledumeshes.cpp index 63064053ce..229f990b6f 100644 --- a/modules/digitaluniverse/rendering/renderabledumeshes.cpp +++ b/modules/digitaluniverse/rendering/renderabledumeshes.cpp @@ -324,14 +324,14 @@ RenderableDUMeshes::RenderableDUMeshes(const ghoul::Dictionary& dictionary) if (dictionary.hasKey(TransparencyInfo.identifier)) { _alphaValue = static_cast( dictionary.value(TransparencyInfo.identifier) - ); + ); } addProperty(_alphaValue); if (dictionary.hasKey(ScaleFactorInfo.identifier)) { _scaleFactor = static_cast( dictionary.value(ScaleFactorInfo.identifier) - ); + ); } addProperty(_scaleFactor); @@ -343,7 +343,7 @@ RenderableDUMeshes::RenderableDUMeshes(const ghoul::Dictionary& dictionary) if (dictionary.hasKey(LabelFileInfo.identifier)) { _labelFile = absPath(dictionary.value( LabelFileInfo.identifier - )); + )); _hasLabel = true; if (dictionary.hasKey(TextColorInfo.identifier)) { @@ -406,6 +406,12 @@ void RenderableDUMeshes::initializeGL() { absPath("${MODULE_DIGITALUNIVERSE}/shaders/dumesh_fs.glsl") ); + _uniformCache.modelViewTransform = _program->uniformLocation("modelViewTransform"); + _uniformCache.projectionTransform = _program->uniformLocation("projectionTransform"); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + //_uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.color = _program->uniformLocation("color"); + bool success = loadData(); if (!success) { throw ghoul::RuntimeError("Error loading data"); @@ -474,40 +480,33 @@ void RenderableDUMeshes::renderMeshes(const RenderData&, _program->activate(); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::Yes); - - _program->setUniform("modelViewTransform", modelViewMatrix); - _program->setUniform("projectionTransform", projectionMatrix); - _program->setUniform("alphaValue", _alphaValue); - _program->setUniform("scaleFactor", _scaleFactor); + _program->setUniform(_uniformCache.modelViewTransform, modelViewMatrix); + _program->setUniform(_uniformCache.projectionTransform, projectionMatrix); + _program->setUniform(_uniformCache.alphaValue, _alphaValue); + //_program->setUniform(_uniformCache.scaleFactor, _scaleFactor); for (auto pair : _renderingMeshesMap) { - _program->setUniform("color", _meshColorMap[pair.second.colorIndex]); + _program->setUniform(_uniformCache.color, _meshColorMap[pair.second.colorIndex]); for (int i = 0; i < static_cast(pair.second.vaoArray.size()); ++i) { glBindVertexArray(pair.second.vaoArray[i]); - switch (pair.second.style) - { - case Solid: - break; - case Wire: - glLineWidth(2.0); - glDrawArrays(GL_LINE_STRIP, 0, pair.second.numV); - glLineWidth(lineWidth); - break; - case Point: - glDrawArrays(GL_POINTS, 0, pair.second.numV); - break; - default: - break; + switch (pair.second.style) { + case Solid: + break; + case Wire: + glLineWidth(2.0); + glDrawArrays(GL_LINE_STRIP, 0, pair.second.numV); + glLineWidth(lineWidth); + break; + case Point: + glDrawArrays(GL_POINTS, 0, pair.second.numV); + break; + default: + break; } } } glBindVertexArray(0); - - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::No); _program->deactivate(); // Restores blending state @@ -609,7 +608,21 @@ void RenderableDUMeshes::render(const RenderData& data, RendererTasks&) { } } -void RenderableDUMeshes::update(const UpdateData&) {} +void RenderableDUMeshes::update(const UpdateData&) { + if (_program->isDirty()) { + _program->rebuildFromFile(); + + _uniformCache.modelViewTransform = _program->uniformLocation( + "modelViewTransform" + ); + _uniformCache.projectionTransform = _program->uniformLocation( + "projectionTransform" + ); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + //_uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.color = _program->uniformLocation("color"); + } +} bool RenderableDUMeshes::loadData() { bool success = false; diff --git a/modules/digitaluniverse/rendering/renderabledumeshes.h b/modules/digitaluniverse/rendering/renderabledumeshes.h index 2d51ca169f..61de6221b2 100644 --- a/modules/digitaluniverse/rendering/renderabledumeshes.h +++ b/modules/digitaluniverse/rendering/renderabledumeshes.h @@ -34,8 +34,9 @@ #include #include -#include #include +#include +#include #include @@ -139,6 +140,8 @@ private: properties::OptionProperty _renderOption; std::unique_ptr _program; + UniformCache(modelViewTransform, projectionTransform, alphaValue, + scaleFactor, color) _uniformCache; std::unique_ptr _fontRenderer; std::shared_ptr _font; diff --git a/modules/digitaluniverse/rendering/renderableplanescloud.cpp b/modules/digitaluniverse/rendering/renderableplanescloud.cpp index cad4c1e0a6..4b18116573 100644 --- a/modules/digitaluniverse/rendering/renderableplanescloud.cpp +++ b/modules/digitaluniverse/rendering/renderableplanescloud.cpp @@ -522,10 +522,18 @@ void RenderablePlanesCloud::initializeGL() { _program = renderEngine.buildRenderProgram( "RenderablePlanesCloud", - absPath("${MODULE_DIGITALUNIVERSE}/shaders/plane2_vs.glsl"), - absPath("${MODULE_DIGITALUNIVERSE}/shaders/plane2_fs.glsl") + absPath("${MODULE_DIGITALUNIVERSE}/shaders/plane_vs.glsl"), + absPath("${MODULE_DIGITALUNIVERSE}/shaders/plane_fs.glsl") ); + _uniformCache.modelViewProjectionTransform = _program->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + //_uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.fadeInValue = _program->uniformLocation("fadeInValue"); + _uniformCache.galaxyTexture = _program->uniformLocation("galaxyTexture"); + createPlanes(); loadTextures(); @@ -548,7 +556,7 @@ void RenderablePlanesCloud::initializeGL() { void RenderablePlanesCloud::deleteDataGPU() { - for (auto renderingPlane : _renderingPlanesArray) { + for (RenderingPlane& renderingPlane : _renderingPlanesArray) { glDeleteVertexArrays(1, &renderingPlane.vao); glDeleteBuffers(1, &renderingPlane.vbo); } @@ -592,23 +600,23 @@ void RenderablePlanesCloud::renderPlanes(const RenderData&, _program->activate(); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::Yes); - glm::dmat4 modelViewProjectionMatrix = glm::dmat4(projectionMatrix) * modelViewMatrix; - _program->setUniform("modelViewProjectionTransform", modelViewProjectionMatrix); - _program->setUniform("alphaValue", _alphaValue); - _program->setUniform("scaleFactor", _scaleFactor); - _program->setUniform("fadeInValue", fadeInVariable); + _program->setUniform( + _uniformCache.modelViewProjectionTransform, + modelViewProjectionMatrix + ); + _program->setUniform(_uniformCache.alphaValue, _alphaValue); + _program->setUniform(_uniformCache.scaleFactor, _scaleFactor); + _program->setUniform(_uniformCache.fadeInValue, fadeInVariable); GLint viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); ghoul::opengl::TextureUnit unit; unit.activate(); - _program->setUniform("galaxyTexture", unit); + _program->setUniform(_uniformCache.galaxyTexture, unit); int currentTextureIndex = -1; - for (auto renderingPlane : _renderingPlanesArray) { + for (const RenderingPlane& renderingPlane : _renderingPlanesArray) { // For planes with undefined textures references if (renderingPlane.planeIndex == -1) { continue; @@ -654,9 +662,6 @@ void RenderablePlanesCloud::renderPlanes(const RenderData&, //} glBindVertexArray(0); - - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::No); _program->deactivate(); // Restores blending state @@ -834,6 +839,18 @@ void RenderablePlanesCloud::update(const UpdateData&) { createPlanes(); _dataIsDirty = false; } + + if (_program->isDirty()) { + _program->rebuildFromFile(); + + _uniformCache.modelViewProjectionTransform = _program->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + _uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.fadeInValue = _program->uniformLocation("fadeInValue"); + _uniformCache.galaxyTexture = _program->uniformLocation("galaxyTexture"); + } } bool RenderablePlanesCloud::loadData() { diff --git a/modules/digitaluniverse/rendering/renderableplanescloud.h b/modules/digitaluniverse/rendering/renderableplanescloud.h index d2cd1f7546..b6f92e2d82 100644 --- a/modules/digitaluniverse/rendering/renderableplanescloud.h +++ b/modules/digitaluniverse/rendering/renderableplanescloud.h @@ -35,8 +35,9 @@ #include #include -#include #include +#include +#include #include #include @@ -129,6 +130,8 @@ namespace openspace { properties::OptionProperty _renderOption; std::unique_ptr _program; + UniformCache(modelViewProjectionTransform, alphaValue, scaleFactor, fadeInValue, + galaxyTexture) _uniformCache; std::unique_ptr _fontRenderer; std::shared_ptr _font; std::unordered_map> _textureMap; diff --git a/modules/digitaluniverse/rendering/renderablepoints.cpp b/modules/digitaluniverse/rendering/renderablepoints.cpp index 7f0c5eb43a..246d1039eb 100644 --- a/modules/digitaluniverse/rendering/renderablepoints.cpp +++ b/modules/digitaluniverse/rendering/renderablepoints.cpp @@ -94,641 +94,651 @@ namespace { namespace openspace { - documentation::Documentation RenderablePoints::Documentation() { - using namespace documentation; - return { - "RenderablePoints", - "digitaluniverse_renderablepoints", +documentation::Documentation RenderablePoints::Documentation() { + using namespace documentation; + return { + "RenderablePoints", + "digitaluniverse_renderablepoints", + { { - { - "Type", - new StringEqualVerifier("RenderablePoints"), - Optional::No - }, - { - KeyFile, - new StringVerifier, - Optional::No, - "The path to the SPECK file that contains information about the " - "astronomical object being rendered." - }, - { - keyColor, - new Vector3Verifier, - Optional::No, - "Astronomical Object Color (r,g,b)." - }, - { - SpriteTextureInfo.identifier, - new StringVerifier, - Optional::Yes, - SpriteTextureInfo.description - }, - { - TransparencyInfo.identifier, - new DoubleVerifier, - Optional::No, - TransparencyInfo.description - }, - { - ScaleFactorInfo.identifier, - new DoubleVerifier, - Optional::Yes, - ScaleFactorInfo.description - }, - { - ColorMapInfo.identifier, - new StringVerifier, - Optional::Yes, - ColorMapInfo.description - }, + "Type", + new StringEqualVerifier("RenderablePoints"), + Optional::No + }, + { + KeyFile, + new StringVerifier, + Optional::No, + "The path to the SPECK file that contains information about the " + "astronomical object being rendered." + }, + { + keyColor, + new Vector3Verifier, + Optional::No, + "Astronomical Object Color (r,g,b)." + }, + { + SpriteTextureInfo.identifier, + new StringVerifier, + Optional::Yes, + SpriteTextureInfo.description + }, + { + TransparencyInfo.identifier, + new DoubleVerifier, + Optional::No, + TransparencyInfo.description + }, + { + ScaleFactorInfo.identifier, + new DoubleVerifier, + Optional::Yes, + ScaleFactorInfo.description + }, + { + ColorMapInfo.identifier, + new StringVerifier, + Optional::Yes, + ColorMapInfo.description + }, - } - }; - } - - - RenderablePoints::RenderablePoints(const ghoul::Dictionary& dictionary) - : Renderable(dictionary) - , _dataIsDirty(true) - , _hasSpriteTexture(false) - , _spriteTextureIsDirty(true) - , _hasColorMapFile(false) - , _alphaValue(TransparencyInfo, 1.f, 0.f, 1.f) - , _scaleFactor(ScaleFactorInfo, 1.f, 0.f, 64.f) - , _pointColor( - ColorInfo, - glm::vec3(1.f, 0.4f, 0.2f), - glm::vec3(0.f, 0.f, 0.f), - glm::vec3(1.0f, 1.0f, 1.0f) - ) - , _spriteTexturePath(SpriteTextureInfo) - , _spriteTexture(nullptr) - , _program(nullptr) - , _speckFile("") - , _colorMapFile("") - , _unit(Parsec) - , _nValuesPerAstronomicalObject(0) - , _vao(0) - , _vbo(0) - { - using File = ghoul::filesystem::File; - - documentation::testSpecificationAndThrow( - Documentation(), - dictionary, - "RenderablePoints" - ); - - _speckFile = absPath(dictionary.value(KeyFile)); - - if (dictionary.hasKey(keyUnit)) { - std::string unit = dictionary.value(keyUnit); - if (unit == MeterUnit) { - _unit = Meter; - } - else if (unit == KilometerUnit) { - _unit = Kilometer; - } - else if (unit == ParsecUnit) { - _unit = Parsec; - } - else if (unit == KiloparsecUnit) { - _unit = Kiloparsec; - } - else if (unit == MegaparsecUnit) { - _unit = Megaparsec; - } - else if (unit == GigaparsecUnit) { - _unit = Gigaparsec; - } - else if (unit == GigalightyearUnit) { - _unit = GigalightYears; - } - else { - LWARNING("No unit given for RenderablePoints. Using meters as units."); - _unit = Meter; - } } + }; +} - if (dictionary.hasKey(keyColor)) { - _pointColor = dictionary.value(keyColor); + +RenderablePoints::RenderablePoints(const ghoul::Dictionary& dictionary) + : Renderable(dictionary) + , _dataIsDirty(true) + , _hasSpriteTexture(false) + , _spriteTextureIsDirty(true) + , _hasColorMapFile(false) + , _alphaValue(TransparencyInfo, 1.f, 0.f, 1.f) + , _scaleFactor(ScaleFactorInfo, 1.f, 0.f, 64.f) + , _pointColor( + ColorInfo, + glm::vec3(1.f, 0.4f, 0.2f), + glm::vec3(0.f, 0.f, 0.f), + glm::vec3(1.0f, 1.0f, 1.0f) + ) + , _spriteTexturePath(SpriteTextureInfo) + , _spriteTexture(nullptr) + , _program(nullptr) + , _speckFile("") + , _colorMapFile("") + , _unit(Parsec) + , _nValuesPerAstronomicalObject(0) + , _vao(0) + , _vbo(0) +{ + using File = ghoul::filesystem::File; + + documentation::testSpecificationAndThrow( + Documentation(), + dictionary, + "RenderablePoints" + ); + + _speckFile = absPath(dictionary.value(KeyFile)); + + if (dictionary.hasKey(keyUnit)) { + std::string unit = dictionary.value(keyUnit); + if (unit == MeterUnit) { + _unit = Meter; } - addProperty(_pointColor); - - if (dictionary.hasKey(SpriteTextureInfo.identifier)) { - _spriteTexturePath = absPath(dictionary.value( - SpriteTextureInfo.identifier - )); - _spriteTextureFile = std::make_unique(_spriteTexturePath); - - _spriteTexturePath.onChange([&] { _spriteTextureIsDirty = true; }); - _spriteTextureFile->setCallback( - [&](const File&) { _spriteTextureIsDirty = true; } - ); - addProperty(_spriteTexturePath); - - _hasSpriteTexture = true; + else if (unit == KilometerUnit) { + _unit = Kilometer; } - - if (dictionary.hasKey(ColorMapInfo.identifier)) { - _colorMapFile = absPath(dictionary.value( - ColorMapInfo.identifier - )); - _hasColorMapFile = true; + else if (unit == ParsecUnit) { + _unit = Parsec; } - - if (dictionary.hasKey(TransparencyInfo.identifier)) { - _alphaValue = static_cast( - dictionary.value(TransparencyInfo.identifier) - ); + else if (unit == KiloparsecUnit) { + _unit = Kiloparsec; } - addProperty(_alphaValue); - - if (dictionary.hasKey(ScaleFactorInfo.identifier)) { - _scaleFactor = static_cast( - dictionary.value(ScaleFactorInfo.identifier) - ); + else if (unit == MegaparsecUnit) { + _unit = Megaparsec; } - addProperty(_scaleFactor); - - } - - bool RenderablePoints::isReady() const { - return (_program != nullptr) && (!_fullData.empty()); - } - - void RenderablePoints::initialize() { - bool success = loadData(); - if (!success) { - throw ghoul::RuntimeError("Error loading data"); + else if (unit == GigaparsecUnit) { + _unit = Gigaparsec; } - } - - void RenderablePoints::initializeGL() { - RenderEngine& renderEngine = OsEng.renderEngine(); - if (_hasSpriteTexture) { - _program = renderEngine.buildRenderProgram( - "RenderablePoints", - absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_vs.glsl"), - absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_sprite_fs.glsl") - ); + else if (unit == GigalightyearUnit) { + _unit = GigalightYears; } else { - _program = renderEngine.buildRenderProgram( - "RenderablePoints", - absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_vs.glsl"), - absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_fs.glsl") + LWARNING("No unit given for RenderablePoints. Using meters as units."); + _unit = Meter; + } + } + + if (dictionary.hasKey(keyColor)) { + _pointColor = dictionary.value(keyColor); + } + addProperty(_pointColor); + + if (dictionary.hasKey(SpriteTextureInfo.identifier)) { + _spriteTexturePath = absPath(dictionary.value( + SpriteTextureInfo.identifier + )); + _spriteTextureFile = std::make_unique(_spriteTexturePath); + + _spriteTexturePath.onChange([&] { _spriteTextureIsDirty = true; }); + _spriteTextureFile->setCallback( + [&](const File&) { _spriteTextureIsDirty = true; } + ); + addProperty(_spriteTexturePath); + + _hasSpriteTexture = true; + } + + if (dictionary.hasKey(ColorMapInfo.identifier)) { + _colorMapFile = absPath(dictionary.value( + ColorMapInfo.identifier + )); + _hasColorMapFile = true; + } + + if (dictionary.hasKey(TransparencyInfo.identifier)) { + _alphaValue = static_cast( + dictionary.value(TransparencyInfo.identifier) ); - } } + addProperty(_alphaValue); - void RenderablePoints::deinitializeGL() { - glDeleteBuffers(1, &_vbo); - _vbo = 0; - glDeleteVertexArrays(1, &_vao); - _vao = 0; - - RenderEngine& renderEngine = OsEng.renderEngine(); - if (_program) { - renderEngine.removeRenderProgram(_program); - _program = nullptr; - } - - if (_hasSpriteTexture) { - _spriteTexture = nullptr; - } + if (dictionary.hasKey(ScaleFactorInfo.identifier)) { + _scaleFactor = static_cast( + dictionary.value(ScaleFactorInfo.identifier) + ); } + addProperty(_scaleFactor); - void RenderablePoints::render(const RenderData& data, RendererTasks&) { - glDepthMask(false); - _program->activate(); +} - glm::dmat4 modelMatrix = glm::dmat4(1.0); +bool RenderablePoints::isReady() const { + return (_program != nullptr) && (!_fullData.empty()); +} - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::Yes); - _program->setUniform( - "modelViewProjectionTransform", - glm::dmat4(data.camera.projectionMatrix()) * - data.camera.combinedViewMatrix() * modelMatrix +void RenderablePoints::initialize() { + bool success = loadData(); + if (!success) { + throw ghoul::RuntimeError("Error loading data"); + } +} + +void RenderablePoints::initializeGL() { + RenderEngine& renderEngine = OsEng.renderEngine(); + if (_hasSpriteTexture) { + _program = renderEngine.buildRenderProgram( + "RenderablePoints", + absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_vs.glsl"), + absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_sprite_fs.glsl") ); - _program->setUniform("color", _pointColor); - _program->setUniform("sides", 4); - _program->setUniform("alphaValue", _alphaValue); - _program->setUniform("scaleFactor", _scaleFactor); + _uniformCache.modelViewProjectionTransform = _program->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.color = _program->uniformLocation("color"); + _uniformCache.sides = _program->uniformLocation("sides"); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + _uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.spriteTexture = _program->uniformLocation("spriteTexture"); + _uniformCache.hasColorMap = _program->uniformLocation("hasColorMap"); + } + else { + _program = renderEngine.buildRenderProgram( + "RenderablePoints", + absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_vs.glsl"), + absPath("${MODULE_DIGITALUNIVERSE}/shaders/points_fs.glsl") + ); - if (_hasSpriteTexture) { - ghoul::opengl::TextureUnit spriteTextureUnit; - spriteTextureUnit.activate(); - _spriteTexture->bind(); - _program->setUniform("spriteTexture", spriteTextureUnit); + _uniformCache.modelViewProjectionTransform = _program->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.color = _program->uniformLocation("color"); + _uniformCache.sides = _program->uniformLocation("sides"); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + _uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.hasColorMap = _program->uniformLocation("hasColorMap"); + } +} + +void RenderablePoints::deinitializeGL() { + glDeleteBuffers(1, &_vbo); + _vbo = 0; + glDeleteVertexArrays(1, &_vao); + _vao = 0; + + RenderEngine& renderEngine = OsEng.renderEngine(); + if (_program) { + renderEngine.removeRenderProgram(_program); + _program = nullptr; + } + + if (_hasSpriteTexture) { + _spriteTexture = nullptr; + } +} + +void RenderablePoints::render(const RenderData& data, RendererTasks&) { + glDepthMask(false); + _program->activate(); + + glm::dmat4 modelMatrix = glm::dmat4(1.0); + + _program->setUniform( + _uniformCache.modelViewProjectionTransform, + glm::dmat4(data.camera.projectionMatrix()) * + data.camera.combinedViewMatrix() * modelMatrix + ); + + _program->setUniform(_uniformCache.color, _pointColor); + _program->setUniform(_uniformCache.sides, 4); + _program->setUniform(_uniformCache.alphaValue, _alphaValue); + _program->setUniform(_uniformCache.scaleFactor, _scaleFactor); + + if (_hasSpriteTexture) { + ghoul::opengl::TextureUnit spriteTextureUnit; + spriteTextureUnit.activate(); + _spriteTexture->bind(); + _program->setUniform(_uniformCache.spriteTexture, spriteTextureUnit); + } + + _program->setUniform(_uniformCache.hasColorMap, _hasColorMapFile); + + glEnable(GL_PROGRAM_POINT_SIZE); + glBindVertexArray(_vao); + const GLsizei nAstronomicalObjects = static_cast( + _fullData.size() / _nValuesPerAstronomicalObject + ); + glDrawArrays(GL_POINTS, 0, nAstronomicalObjects); + + glDisable(GL_PROGRAM_POINT_SIZE); + glBindVertexArray(0); + _program->deactivate(); + + glDepthMask(true); +} + +void RenderablePoints::update(const UpdateData&) { + if (_dataIsDirty) { + LDEBUG("Regenerating data"); + + createDataSlice(); + + int size = static_cast(_slicedData.size()); + + if (_vao == 0) { + glGenVertexArrays(1, &_vao); + LDEBUG("Generating Vertex Array id '" << _vao << "'"); } + if (_vbo == 0) { + glGenBuffers(1, &_vbo); + LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); + } + + glBindVertexArray(_vao); + glBindBuffer(GL_ARRAY_BUFFER, _vbo); + glBufferData( + GL_ARRAY_BUFFER, + size * sizeof(double), + &_slicedData[0], + GL_STATIC_DRAW + ); + GLint positionAttrib = _program->attributeLocation("in_position"); if (_hasColorMapFile) { - _program->setUniform("hasColorMap", true); + + const size_t nAstronomicalObjects = _fullData.size() / + _nValuesPerAstronomicalObject; + // const size_t nValues = _slicedData.size() / nAstronomicalObjects; + // GLsizei stride = static_cast(sizeof(double) * nValues); + + glEnableVertexAttribArray(positionAttrib); + glVertexAttribLPointer( + positionAttrib, + 4, + GL_DOUBLE, + sizeof(double)*8, + nullptr + ); + + GLint colorMapAttrib = _program->attributeLocation("in_colormap"); + glEnableVertexAttribArray(colorMapAttrib); + glVertexAttribLPointer( + colorMapAttrib, + 4, + GL_DOUBLE, + sizeof(double) * 8, + reinterpret_cast(sizeof(double)*4) + ); } else { - _program->setUniform("hasColorMap", false); + glEnableVertexAttribArray(positionAttrib); + glVertexAttribLPointer( + positionAttrib, + 4, + GL_DOUBLE, + 0, + nullptr + ); } - glEnable(GL_PROGRAM_POINT_SIZE); - glBindVertexArray(_vao); - const GLsizei nAstronomicalObjects = static_cast( - _fullData.size() / _nValuesPerAstronomicalObject - ); - glDrawArrays(GL_POINTS, 0, nAstronomicalObjects); - - glDisable(GL_PROGRAM_POINT_SIZE); glBindVertexArray(0); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::No); - _program->deactivate(); - glDepthMask(true); + _dataIsDirty = false; } - void RenderablePoints::update(const UpdateData&) { - if (_dataIsDirty) { - LDEBUG("Regenerating data"); - - createDataSlice(); - - int size = static_cast(_slicedData.size()); - - if (_vao == 0) { - glGenVertexArrays(1, &_vao); - LDEBUG("Generating Vertex Array id '" << _vao << "'"); - } - if (_vbo == 0) { - glGenBuffers(1, &_vbo); - LDEBUG("Generating Vertex Buffer Object id '" << _vbo << "'"); - } - - glBindVertexArray(_vao); - glBindBuffer(GL_ARRAY_BUFFER, _vbo); - glBufferData( - GL_ARRAY_BUFFER, - size * sizeof(double), - &_slicedData[0], - GL_STATIC_DRAW + if (_hasSpriteTexture && _spriteTextureIsDirty) { + LDEBUG("Reloading Sprite Texture"); + _spriteTexture = nullptr; + if (_spriteTexturePath.value() != "") { + _spriteTexture = ghoul::io::TextureReader::ref().loadTexture( + absPath(_spriteTexturePath) ); - GLint positionAttrib = _program->attributeLocation("in_position"); - - if (_hasColorMapFile) { - - const size_t nAstronomicalObjects = _fullData.size() / - _nValuesPerAstronomicalObject; - // const size_t nValues = _slicedData.size() / nAstronomicalObjects; - // GLsizei stride = static_cast(sizeof(double) * nValues); - - glEnableVertexAttribArray(positionAttrib); - glVertexAttribLPointer( - positionAttrib, - 4, - GL_DOUBLE, - sizeof(double)*8, - nullptr - ); - - GLint colorMapAttrib = _program->attributeLocation("in_colormap"); - glEnableVertexAttribArray(colorMapAttrib); - glVertexAttribLPointer( - colorMapAttrib, - 4, - GL_DOUBLE, - sizeof(double) * 8, - reinterpret_cast(sizeof(double)*4) - ); - } - else { - glEnableVertexAttribArray(positionAttrib); - glVertexAttribLPointer( - positionAttrib, - 4, - GL_DOUBLE, - 0, - nullptr - ); + if (_spriteTexture) { + LDEBUG("Loaded texture from '" << absPath(_spriteTexturePath) << "'"); + _spriteTexture->uploadTexture(); } + _spriteTexture->setFilter( + ghoul::opengl::Texture::FilterMode::AnisotropicMipMap + ); - glBindVertexArray(0); - - _dataIsDirty = false; - } - - if (_hasSpriteTexture && _spriteTextureIsDirty) { - LDEBUG("Reloading Sprite Texture"); - _spriteTexture = nullptr; - if (_spriteTexturePath.value() != "") { - _spriteTexture = ghoul::io::TextureReader::ref().loadTexture( - absPath(_spriteTexturePath) - ); - if (_spriteTexture) { - LDEBUG("Loaded texture from '" << absPath(_spriteTexturePath) << "'"); - _spriteTexture->uploadTexture(); - } - _spriteTexture->setFilter( - ghoul::opengl::Texture::FilterMode::AnisotropicMipMap - ); - - _spriteTextureFile = std::make_unique( - _spriteTexturePath); - _spriteTextureFile->setCallback( - [&](const ghoul::filesystem::File&) { _spriteTextureIsDirty = true; } - ); - } - _spriteTextureIsDirty = false; + _spriteTextureFile = std::make_unique( + _spriteTexturePath); + _spriteTextureFile->setCallback( + [&](const ghoul::filesystem::File&) { _spriteTextureIsDirty = true; } + ); } + _spriteTextureIsDirty = false; } +} - bool RenderablePoints::loadData() { - std::string _file = _speckFile; - std::string cachedFile = FileSys.cacheManager()->cachedFilename( - _file, - ghoul::filesystem::CacheManager::Persistent::Yes +bool RenderablePoints::loadData() { + std::string _file = _speckFile; + std::string cachedFile = FileSys.cacheManager()->cachedFilename( + _file, + ghoul::filesystem::CacheManager::Persistent::Yes + ); + + bool hasCachedFile = FileSys.fileExists(cachedFile); + if (hasCachedFile) { + LINFO( + "Cached file '" << cachedFile << "' used for Speck file '" << _file << "'" ); - bool hasCachedFile = FileSys.fileExists(cachedFile); - if (hasCachedFile) { - LINFO( - "Cached file '" << cachedFile << "' used for Speck file '" << _file << "'" - ); - - bool success = loadCachedFile(cachedFile); - if (success) { - if (_hasColorMapFile) { - success &= readColorMapFile(); - } - return success; - } - else { - FileSys.cacheManager()->removeCacheFile(_file); - // Intentional fall-through to the 'else' to generate the cache file for - // the next run + bool success = loadCachedFile(cachedFile); + if (success) { + if (_hasColorMapFile) { + success &= readColorMapFile(); } + return success; } else { - LINFO("Cache for Speck file '" << _file << "' not found"); + FileSys.cacheManager()->removeCacheFile(_file); + // Intentional fall-through to the 'else' to generate the cache file for + // the next run } - LINFO("Loading Speck file '" << _file << "'"); + } + else { + LINFO("Cache for Speck file '" << _file << "' not found"); + } + LINFO("Loading Speck file '" << _file << "'"); - bool success = readSpeckFile(); - if (!success) { + bool success = readSpeckFile(); + if (!success) { + return false; + } + + LINFO("Saving cache"); + success = saveCachedFile(cachedFile); + + if (_hasColorMapFile) { + success &= readColorMapFile(); + } + + return success; +} + +bool RenderablePoints::readSpeckFile() { + std::string _file = _speckFile; + std::ifstream file(_file); + if (!file.good()) { + LERROR("Failed to open Speck file '" << _file << "'"); + return false; + } + + _nValuesPerAstronomicalObject = 0; + + // The beginning of the speck file has a header that either contains comments + // (signaled by a preceding '#') or information about the structure of the file + // (signaled by the keywords 'datavar', 'texturevar', and 'texture') + std::string line = ""; + while (true) { + std::streampos position = file.tellg(); + std::getline(file, line); + + if (line[0] == '#' || line.empty()) { + continue; + } + + if (line.substr(0, 7) != "datavar" && + line.substr(0, 10) != "texturevar" && + line.substr(0, 7) != "texture") + { + // we read a line that doesn't belong to the header, so we have to jump + // back before the beginning of the current line + file.seekg(position); + break; + } + + if (line.substr(0, 7) == "datavar") { + // datavar lines are structured as follows: + // datavar # description + // where # is the index of the data variable; so if we repeatedly + // overwrite the 'nValues' variable with the latest index, we will end up + // with the total number of values (+3 since X Y Z are not counted in the + // Speck file index) + std::stringstream str(line); + + std::string dummy; + str >> dummy; + str >> _nValuesPerAstronomicalObject; + // We want the number, but the index is 0 based + _nValuesPerAstronomicalObject += 1; + } + } + + // X Y Z are not counted in the Speck file indices + _nValuesPerAstronomicalObject += 3; + + do { + std::vector values(_nValuesPerAstronomicalObject); + + std::getline(file, line); + std::stringstream str(line); + + for (int i = 0; i < _nValuesPerAstronomicalObject; ++i) { + str >> values[i]; + } + + _fullData.insert(_fullData.end(), values.begin(), values.end()); + } while (!file.eof()); + + return true; +} + +bool RenderablePoints::readColorMapFile() { + std::string _file = _colorMapFile; + std::ifstream file(_file); + if (!file.good()) { + LERROR("Failed to open Color Map file '" << _file << "'"); + return false; + } + + std::size_t numberOfColors = 0; + + // The beginning of the speck file has a header that either contains comments + // (signaled by a preceding '#') or information about the structure of the file + // (signaled by the keywords 'datavar', 'texturevar', and 'texture') + std::string line = ""; + while (true) { + // std::streampos position = file.tellg(); + std::getline(file, line); + + if (line[0] == '#' || line.empty()) { + continue; + } + + // Initial number of colors + std::locale loc; + if (std::isdigit(line[0], loc)) { + std::string::size_type sz; + numberOfColors = std::stoi(line, &sz); + break; + } + else if (file.eof()) { + return false; + } + } + + for (size_t i = 0; i < numberOfColors; ++i) { + std::getline(file, line); + std::stringstream str(line); + + glm::vec4 color; + for (int j = 0; j < 4; ++j) { + str >> color[j]; + } + + _colorMapData.push_back(color); + } + + return true; +} + +bool RenderablePoints::loadCachedFile(const std::string& file) { + std::ifstream fileStream(file, std::ifstream::binary); + if (fileStream.good()) { + int8_t version = 0; + fileStream.read(reinterpret_cast(&version), sizeof(int8_t)); + if (version != CurrentCacheVersion) { + LINFO("The format of the cached file has changed: deleting old cache"); + fileStream.close(); + FileSys.deleteFile(file); return false; } - LINFO("Saving cache"); - success = saveCachedFile(cachedFile); + int32_t nValues = 0; + fileStream.read(reinterpret_cast(&nValues), sizeof(int32_t)); + fileStream.read( + reinterpret_cast(&_nValuesPerAstronomicalObject), + sizeof(int32_t) + ); - if (_hasColorMapFile) { - success &= readColorMapFile(); - } + _fullData.resize(nValues); + fileStream.read(reinterpret_cast(&_fullData[0]), + nValues * sizeof(_fullData[0])); + bool success = fileStream.good(); return success; } + else { + LERROR("Error opening file '" << file << "' for loading cache file"); + return false; + } +} - bool RenderablePoints::readSpeckFile() { - std::string _file = _speckFile; - std::ifstream file(_file); - if (!file.good()) { - LERROR("Failed to open Speck file '" << _file << "'"); +bool RenderablePoints::saveCachedFile(const std::string& file) const { + std::ofstream fileStream(file, std::ofstream::binary); + if (fileStream.good()) { + fileStream.write(reinterpret_cast(&CurrentCacheVersion), + sizeof(int8_t)); + + int32_t nValues = static_cast(_fullData.size()); + if (nValues == 0) { + LERROR("Error writing cache: No values were loaded"); return false; } + fileStream.write(reinterpret_cast(&nValues), sizeof(int32_t)); - _nValuesPerAstronomicalObject = 0; + int32_t nValuesPerAstronomicalObject = static_cast( + _nValuesPerAstronomicalObject + ); + fileStream.write( + reinterpret_cast(&nValuesPerAstronomicalObject), + sizeof(int32_t) + ); - // The beginning of the speck file has a header that either contains comments - // (signaled by a preceding '#') or information about the structure of the file - // (signaled by the keywords 'datavar', 'texturevar', and 'texture') - std::string line = ""; - while (true) { - std::streampos position = file.tellg(); - std::getline(file, line); + size_t nBytes = nValues * sizeof(_fullData[0]); + fileStream.write(reinterpret_cast(&_fullData[0]), nBytes); - if (line[0] == '#' || line.empty()) { - continue; - } + bool success = fileStream.good(); + return success; + } + else { + LERROR("Error opening file '" << file << "' for save cache file"); + return false; + } +} - if (line.substr(0, 7) != "datavar" && - line.substr(0, 10) != "texturevar" && - line.substr(0, 7) != "texture") - { - // we read a line that doesn't belong to the header, so we have to jump - // back before the beginning of the current line - file.seekg(position); - break; - } - - if (line.substr(0, 7) == "datavar") { - // datavar lines are structured as follows: - // datavar # description - // where # is the index of the data variable; so if we repeatedly - // overwrite the 'nValues' variable with the latest index, we will end up - // with the total number of values (+3 since X Y Z are not counted in the - // Speck file index) - std::stringstream str(line); - - std::string dummy; - str >> dummy; - str >> _nValuesPerAstronomicalObject; - // We want the number, but the index is 0 based - _nValuesPerAstronomicalObject += 1; - } - } - - // X Y Z are not counted in the Speck file indices - _nValuesPerAstronomicalObject += 3; - - do { - std::vector values(_nValuesPerAstronomicalObject); - - std::getline(file, line); - std::stringstream str(line); - - for (int i = 0; i < _nValuesPerAstronomicalObject; ++i) { - str >> values[i]; - } - - _fullData.insert(_fullData.end(), values.begin(), values.end()); - } while (!file.eof()); - - return true; +void RenderablePoints::createDataSlice() { + _slicedData.clear(); + if (_hasColorMapFile) { + _slicedData.reserve(8 * (_fullData.size() / _nValuesPerAstronomicalObject)); + } + else { + _slicedData.reserve(4 * (_fullData.size()/_nValuesPerAstronomicalObject)); } - bool RenderablePoints::readColorMapFile() { - std::string _file = _colorMapFile; - std::ifstream file(_file); - if (!file.good()) { - LERROR("Failed to open Color Map file '" << _file << "'"); - return false; + int colorIndex = 0; + for (size_t i = 0; i < _fullData.size(); i += _nValuesPerAstronomicalObject) { + glm::dvec3 p = glm::dvec3( + _fullData[i + 0], + _fullData[i + 1], + _fullData[i + 2] + ); + + // Converting untis + if (_unit == Kilometer) { + p *= 1E3; + } + else if (_unit == Parsec) { + p *= PARSEC; + } + else if (_unit == Kiloparsec) { + p *= 1E3 * PARSEC; + } + else if (_unit == Megaparsec) { + p *= 1E6 * PARSEC; + } + else if (_unit == Gigaparsec) { + p *= 1E9 * PARSEC; + } + else if (_unit == GigalightYears) { + p *= 306391534.73091 * PARSEC; } - std::size_t numberOfColors = 0; + glm::dvec4 position(p, 1.0); - // The beginning of the speck file has a header that either contains comments - // (signaled by a preceding '#') or information about the structure of the file - // (signaled by the keywords 'datavar', 'texturevar', and 'texture') - std::string line = ""; - while (true) { - // std::streampos position = file.tellg(); - std::getline(file, line); - - if (line[0] == '#' || line.empty()) { - continue; - } - - // Initial number of colors - std::locale loc; - if (std::isdigit(line[0], loc)) { - std::string::size_type sz; - numberOfColors = std::stoi(line, &sz); - break; - } - else if (file.eof()) { - return false; - } - } - - for (size_t i = 0; i < numberOfColors; ++i) { - std::getline(file, line); - std::stringstream str(line); - - glm::vec4 color; - for (int j = 0; j < 4; ++j) { - str >> color[j]; - } - - _colorMapData.push_back(color); - } - - return true; - } - - bool RenderablePoints::loadCachedFile(const std::string& file) { - std::ifstream fileStream(file, std::ifstream::binary); - if (fileStream.good()) { - int8_t version = 0; - fileStream.read(reinterpret_cast(&version), sizeof(int8_t)); - if (version != CurrentCacheVersion) { - LINFO("The format of the cached file has changed: deleting old cache"); - fileStream.close(); - FileSys.deleteFile(file); - return false; - } - - int32_t nValues = 0; - fileStream.read(reinterpret_cast(&nValues), sizeof(int32_t)); - fileStream.read( - reinterpret_cast(&_nValuesPerAstronomicalObject), - sizeof(int32_t) - ); - - _fullData.resize(nValues); - fileStream.read(reinterpret_cast(&_fullData[0]), - nValues * sizeof(_fullData[0])); - - bool success = fileStream.good(); - return success; - } - else { - LERROR("Error opening file '" << file << "' for loading cache file"); - return false; - } - } - - bool RenderablePoints::saveCachedFile(const std::string& file) const { - std::ofstream fileStream(file, std::ofstream::binary); - if (fileStream.good()) { - fileStream.write(reinterpret_cast(&CurrentCacheVersion), - sizeof(int8_t)); - - int32_t nValues = static_cast(_fullData.size()); - if (nValues == 0) { - LERROR("Error writing cache: No values were loaded"); - return false; - } - fileStream.write(reinterpret_cast(&nValues), sizeof(int32_t)); - - int32_t nValuesPerAstronomicalObject = static_cast( - _nValuesPerAstronomicalObject - ); - fileStream.write( - reinterpret_cast(&nValuesPerAstronomicalObject), - sizeof(int32_t) - ); - - size_t nBytes = nValues * sizeof(_fullData[0]); - fileStream.write(reinterpret_cast(&_fullData[0]), nBytes); - - bool success = fileStream.good(); - return success; - } - else { - LERROR("Error opening file '" << file << "' for save cache file"); - return false; - } - } - - void RenderablePoints::createDataSlice() { - _slicedData.clear(); if (_hasColorMapFile) { - _slicedData.reserve(8 * (_fullData.size() / _nValuesPerAstronomicalObject)); + for (auto j = 0; j < 4; ++j) { + _slicedData.push_back(position[j]); + } + for (auto j = 0; j < 4; ++j) { + _slicedData.push_back(_colorMapData[colorIndex][j]); + } } else { - _slicedData.reserve(4 * (_fullData.size()/_nValuesPerAstronomicalObject)); + for (auto j = 0; j < 4; ++j) { + _slicedData.push_back(position[j]); + } } - int colorIndex = 0; - for (size_t i = 0; i < _fullData.size(); i += _nValuesPerAstronomicalObject) { - glm::dvec3 p = glm::dvec3( - _fullData[i + 0], - _fullData[i + 1], - _fullData[i + 2] - ); - - // Converting untis - if (_unit == Kilometer) { - p *= 1E3; - } - else if (_unit == Parsec) { - p *= PARSEC; - } - else if (_unit == Kiloparsec) { - p *= 1E3 * PARSEC; - } - else if (_unit == Megaparsec) { - p *= 1E6 * PARSEC; - } - else if (_unit == Gigaparsec) { - p *= 1E9 * PARSEC; - } - else if (_unit == GigalightYears) { - p *= 306391534.73091 * PARSEC; - } - - glm::dvec4 position(p, 1.0); - - if (_hasColorMapFile) { - for (auto j = 0; j < 4; ++j) { - _slicedData.push_back(position[j]); - } - for (auto j = 0; j < 4; ++j) { - _slicedData.push_back(_colorMapData[colorIndex][j]); - } - } - else { - for (auto j = 0; j < 4; ++j) { - _slicedData.push_back(position[j]); - } - } - - colorIndex = - (colorIndex == static_cast(_colorMapData.size() - 1)) ? - 0 : - colorIndex + 1; - } + colorIndex = + (colorIndex == static_cast(_colorMapData.size() - 1)) ? + 0 : + colorIndex + 1; } +} } // namespace openspace diff --git a/modules/digitaluniverse/rendering/renderablepoints.h b/modules/digitaluniverse/rendering/renderablepoints.h index 146c276110..62866d92dc 100644 --- a/modules/digitaluniverse/rendering/renderablepoints.h +++ b/modules/digitaluniverse/rendering/renderablepoints.h @@ -34,6 +34,7 @@ #include #include +#include namespace ghoul::filesystem { class File; @@ -98,6 +99,8 @@ namespace openspace { std::unique_ptr _spriteTexture; std::unique_ptr _spriteTextureFile; std::unique_ptr _program; + UniformCache(modelViewProjectionTransform, color, sides, alphaValue, scaleFactor, + spriteTexture, hasColorMap) _uniformCache; std::string _speckFile; std::string _colorMapFile; diff --git a/modules/digitaluniverse/shaders/plane2_fs.glsl b/modules/digitaluniverse/shaders/plane_fs.glsl similarity index 100% rename from modules/digitaluniverse/shaders/plane2_fs.glsl rename to modules/digitaluniverse/shaders/plane_fs.glsl diff --git a/modules/digitaluniverse/shaders/plane2_vs.glsl b/modules/digitaluniverse/shaders/plane_vs.glsl similarity index 100% rename from modules/digitaluniverse/shaders/plane2_vs.glsl rename to modules/digitaluniverse/shaders/plane_vs.glsl diff --git a/modules/globebrowsing/globes/pointglobe.cpp b/modules/globebrowsing/globes/pointglobe.cpp index c4f23de237..74ba4ed7d0 100644 --- a/modules/globebrowsing/globes/pointglobe.cpp +++ b/modules/globebrowsing/globes/pointglobe.cpp @@ -72,6 +72,16 @@ void PointGlobe::initialize() { absPath("${MODULE_GLOBEBROWSING}/shaders/pointglobe_fs.glsl") ); + _uniformCache.lightIntensityClamped = _programObject->uniformLocation( + "lightIntensityClamped" + ); + _uniformCache.modelView = _programObject->uniformLocation( + "modelViewTransform" + ); + _uniformCache.projection = _programObject->uniformLocation( + "projectionTransform" + ); + glGenVertexArrays(1, &_vaoID); glGenBuffers(1, &_vertexBufferID); @@ -145,12 +155,18 @@ void PointGlobe::render(const RenderData& data, RendererTasks&) { glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * modelTransform; - _programObject->setUniform("lightIntensityClamped", lightIntensityClamped); + _programObject->setUniform( + _uniformCache.lightIntensityClamped, + lightIntensityClamped + ); //_programObject->setUniform("lightOverflow", lightOverflow); //_programObject->setUniform("directionToSunViewSpace", directionToSunViewSpace); - _programObject->setUniform("modelViewTransform", glm::mat4(modelViewTransform)); _programObject->setUniform( - "projectionTransform", + _uniformCache.modelView, + glm::mat4(modelViewTransform) + ); + _programObject->setUniform( + _uniformCache.projection, data.camera.sgctInternal.projectionMatrix() ); diff --git a/modules/globebrowsing/globes/pointglobe.h b/modules/globebrowsing/globes/pointglobe.h index 9c274f12b7..bb56b2e9ec 100644 --- a/modules/globebrowsing/globes/pointglobe.h +++ b/modules/globebrowsing/globes/pointglobe.h @@ -29,6 +29,7 @@ #include #include +#include namespace ghoul::opengl { class ProgramObject; } @@ -50,6 +51,7 @@ public: private: const RenderableGlobe& _owner; std::unique_ptr _programObject; + UniformCache(lightIntensityClamped, modelView, projection) _uniformCache; GLuint _vertexBufferID; GLuint _vaoID; diff --git a/modules/imgui/src/gui.cpp b/modules/imgui/src/gui.cpp index 9ce66d03b2..e750205bd5 100644 --- a/modules/imgui/src/gui.cpp +++ b/modules/imgui/src/gui.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include @@ -59,6 +60,7 @@ GLuint vao = 0; GLuint vbo = 0; GLuint vboElements = 0; std::unique_ptr _program; +UniformCache(tex, ortho) _uniformCache; std::unique_ptr _fontTexture; char* iniFileBuffer = nullptr; @@ -104,8 +106,8 @@ static void RenderDrawLists(ImDrawData* drawData) { ); _program->activate(); - _program->setUniform("tex", unit); - _program->setUniform("ortho", ortho); + _program->setUniform(_uniformCache.tex, unit); + _program->setUniform(_uniformCache.ortho, ortho); glBindVertexArray(vao); @@ -535,6 +537,10 @@ void GUI::initializeGL() { absPath("${MODULE_IMGUI}/shaders/gui_vs.glsl"), absPath("${MODULE_IMGUI}/shaders/gui_fs.glsl") ); + + _uniformCache.tex = _program->uniformLocation("tex"); + _uniformCache.ortho = _program->uniformLocation("ortho"); + if (!_program) { return; } @@ -682,6 +688,9 @@ void GUI::startFrame(float deltaTime, const glm::vec2& windowSize, void GUI::endFrame() { if (_program->isDirty()) { _program->rebuildFromFile(); + + _uniformCache.tex = _program->uniformLocation("tex"); + _uniformCache.ortho = _program->uniformLocation("ortho"); } bool perf = OsEng.renderEngine().doesPerformanceMeasurements(); diff --git a/modules/space/rendering/renderablerings.cpp b/modules/space/rendering/renderablerings.cpp index 5f1b01d9a8..1c33839775 100644 --- a/modules/space/rendering/renderablerings.cpp +++ b/modules/space/rendering/renderablerings.cpp @@ -184,20 +184,24 @@ bool RenderableRings::isReady() const { } void RenderableRings::initializeGL() { - if (!_shader) { - RenderEngine& renderEngine = OsEng.renderEngine(); - _shader = renderEngine.buildRenderProgram( - "RingProgram", - absPath("${MODULE_SPACE}/shaders/rings_vs.glsl"), - absPath("${MODULE_SPACE}/shaders/rings_fs.glsl") - ); - _shader->setIgnoreUniformLocationError( - ghoul::opengl::ProgramObject::IgnoreError::Yes - ); - } + _shader = OsEng.renderEngine().buildRenderProgram( + "RingProgram", + absPath("${MODULE_SPACE}/shaders/rings_vs.glsl"), + absPath("${MODULE_SPACE}/shaders/rings_fs.glsl") + ); + + _uniformCache.modelViewProjection = _shader->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.textureOffset = _shader->uniformLocation("textureOffset"); + _uniformCache.transparency = _shader->uniformLocation("transparency"); + _uniformCache.nightFactor = _shader->uniformLocation("_nightFactor"); + _uniformCache.sunPosition = _shader->uniformLocation("sunPosition"); + _uniformCache.texture = _shader->uniformLocation("texture1"); glGenVertexArrays(1, &_quad); glGenBuffers(1, &_vertexPositionBuffer); + createPlane(); loadTexture(); } @@ -227,15 +231,15 @@ void RenderableRings::render(const RenderData& data, RendererTasks&) { glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * modelTransform; _shader->setUniform( - "modelViewProjectionTransform", + _uniformCache.modelViewProjection, data.camera.projectionMatrix() * glm::mat4(modelViewTransform) ); - _shader->setUniform("textureOffset", _offset); - _shader->setUniform("transparency", _transparency); + _shader->setUniform(_uniformCache.textureOffset, _offset); + _shader->setUniform(_uniformCache.transparency, _transparency); - _shader->setUniform("_nightFactor", _nightFactor); + _shader->setUniform(_uniformCache.nightFactor, _nightFactor); _shader->setUniform( - "sunPosition", + _uniformCache.sunPosition, _sunPosition ); @@ -244,7 +248,7 @@ void RenderableRings::render(const RenderData& data, RendererTasks&) { ghoul::opengl::TextureUnit unit; unit.activate(); _texture->bind(); - _shader->setUniform("texture1", unit); + _shader->setUniform(_uniformCache.texture, unit); glDisable(GL_CULL_FACE); @@ -258,6 +262,15 @@ void RenderableRings::render(const RenderData& data, RendererTasks&) { void RenderableRings::update(const UpdateData& data) { if (_shader->isDirty()) { _shader->rebuildFromFile(); + + _uniformCache.modelViewProjection = _shader->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.textureOffset = _shader->uniformLocation("textureOffset"); + _uniformCache.transparency = _shader->uniformLocation("transparency"); + _uniformCache.nightFactor = _shader->uniformLocation("_nightFactor"); + _uniformCache.sunPosition = _shader->uniformLocation("sunPosition"); + _uniformCache.texture = _shader->uniformLocation("texture1"); } if (_planeIsDirty) { diff --git a/modules/space/rendering/renderablerings.h b/modules/space/rendering/renderablerings.h index a4aa3d5018..c3470716e4 100644 --- a/modules/space/rendering/renderablerings.h +++ b/modules/space/rendering/renderablerings.h @@ -32,6 +32,8 @@ #include #include +#include + namespace ghoul::filesystem { class File; } namespace ghoul::opengl { class ProgramObject; @@ -67,6 +69,8 @@ private: properties::FloatProperty _transparency; std::unique_ptr _shader; + UniformCache(modelViewProjection, textureOffset, transparency, nightFactor, + sunPosition, texture) _uniformCache; std::unique_ptr _texture; std::unique_ptr _textureFile; diff --git a/modules/space/rendering/renderablestars.cpp b/modules/space/rendering/renderablestars.cpp index 91d96c2ec2..5823510146 100644 --- a/modules/space/rendering/renderablestars.cpp +++ b/modules/space/rendering/renderablestars.cpp @@ -294,6 +294,17 @@ void RenderableStars::initializeGL() { absPath("${MODULE_SPACE}/shaders/star_ge.glsl") ); + _uniformCache.view = _program->uniformLocation("view"); + _uniformCache.projection = _program->uniformLocation("projection"); + _uniformCache.colorOption = _program->uniformLocation("colorOption"); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + _uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.minBillboardSize = _program->uniformLocation("minBillboardSize"); + _uniformCache.screenSize = _program->uniformLocation("screenSize"); + _uniformCache.scaling = _program->uniformLocation("scaling"); + _uniformCache.psfTexture = _program->uniformLocation("psfTexture"); + _uniformCache.colorTexture = _program->uniformLocation("colorTexture"); + bool success = loadData(); if (!success) { throw ghoul::RuntimeError("Error loading data"); @@ -324,47 +335,36 @@ void RenderableStars::render(const RenderData& data, RendererTasks&) { // is done twice? ---abock glm::vec2 scaling = glm::vec2(1, -19); - glm::mat4 modelMatrix = glm::mat4(1.0); - glm::mat4 viewMatrix = data.camera.viewMatrix(); - glm::mat4 projectionMatrix = data.camera.projectionMatrix(); + _program->setUniform(_uniformCache.view, data.camera.viewMatrix()); + _program->setUniform(_uniformCache.projection, data.camera.projectionMatrix()); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::Yes); - //_program->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); - //_program->setUniform("ModelTransform", glm::mat4(1.f)); - _program->setUniform("model", modelMatrix); - _program->setUniform("view", viewMatrix); - _program->setUniform("projection", projectionMatrix); - - _program->setUniform("colorOption", _colorOption); - _program->setUniform("alphaValue", _alphaValue); - _program->setUniform("scaleFactor", _scaleFactor); - _program->setUniform("minBillboardSize", _minBillboardSize); + _program->setUniform(_uniformCache.colorOption, _colorOption); + _program->setUniform(_uniformCache.alphaValue, _alphaValue); + _program->setUniform(_uniformCache.scaleFactor, _scaleFactor); + _program->setUniform(_uniformCache.minBillboardSize, _minBillboardSize); _program->setUniform( - "screenSize", + _uniformCache.screenSize, glm::vec2(OsEng.renderEngine().renderingResolution()) ); setPscUniforms(*_program.get(), data.camera, data.position); - _program->setUniform("scaling", scaling); + _program->setUniform(_uniformCache.scaling, scaling); ghoul::opengl::TextureUnit psfUnit; psfUnit.activate(); _pointSpreadFunctionTexture->bind(); - _program->setUniform("psfTexture", psfUnit); + _program->setUniform(_uniformCache.psfTexture, psfUnit); ghoul::opengl::TextureUnit colorUnit; colorUnit.activate(); _colorTexture->bind(); - _program->setUniform("colorTexture", colorUnit); + _program->setUniform(_uniformCache.colorTexture, colorUnit); glBindVertexArray(_vao); const GLsizei nStars = static_cast(_fullData.size() / _nValuesPerStar); glDrawArrays(GL_POINTS, 0, nStars); glBindVertexArray(0); - using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError; - _program->setIgnoreUniformLocationError(IgnoreError::No); _program->deactivate(); glDepthMask(true); @@ -548,6 +548,21 @@ void RenderableStars::update(const UpdateData&) { } _colorTextureIsDirty = false; } + + if (_program->isDirty()) { + _program->rebuildFromFile(); + + _uniformCache.view = _program->uniformLocation("view"); + _uniformCache.projection = _program->uniformLocation("projection"); + _uniformCache.colorOption = _program->uniformLocation("colorOption"); + _uniformCache.alphaValue = _program->uniformLocation("alphaValue"); + _uniformCache.scaleFactor = _program->uniformLocation("scaleFactor"); + _uniformCache.minBillboardSize = _program->uniformLocation("minBillboardSize"); + _uniformCache.screenSize = _program->uniformLocation("screenSize"); + _uniformCache.scaling = _program->uniformLocation("scaling"); + _uniformCache.psfTexture = _program->uniformLocation("psfTexture"); + _uniformCache.colorTexture = _program->uniformLocation("colorTexture"); + } } bool RenderableStars::loadData() { diff --git a/modules/space/rendering/renderablestars.h b/modules/space/rendering/renderablestars.h index 930afc0744..fd2cd92e5a 100644 --- a/modules/space/rendering/renderablestars.h +++ b/modules/space/rendering/renderablestars.h @@ -32,6 +32,7 @@ #include #include +#include namespace ghoul::filesystem { class File; } namespace ghoul::opengl { @@ -90,6 +91,8 @@ private: properties::FloatProperty _minBillboardSize; std::unique_ptr _program; + UniformCache(view, projection, colorOption, alphaValue, scaleFactor, + minBillboardSize, screenSize, scaling, psfTexture, colorTexture) _uniformCache; std::string _speckFile; diff --git a/modules/space/shaders/star_vs.glsl b/modules/space/shaders/star_vs.glsl index 51b7b9bb83..ed6ff8d193 100644 --- a/modules/space/shaders/star_vs.glsl +++ b/modules/space/shaders/star_vs.glsl @@ -37,7 +37,6 @@ out vec3 vs_velocity; out float vs_speed; out vec4 vs_gPosition; -uniform mat4 model; uniform mat4 view; uniform mat4 projection; diff --git a/modules/spacecraftinstruments/rendering/renderablefov.cpp b/modules/spacecraftinstruments/rendering/renderablefov.cpp index ac5e7dd0de..c34551e2d6 100644 --- a/modules/spacecraftinstruments/rendering/renderablefov.cpp +++ b/modules/spacecraftinstruments/rendering/renderablefov.cpp @@ -306,6 +306,26 @@ void RenderableFov::initializeGL() { absPath("${MODULE_SPACECRAFTINSTRUMENTS}/shaders/fov_fs.glsl") ); + _uniformCache.modelViewProjection = _programObject->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.defaultColorStart = _programObject->uniformLocation( + "defaultColorStart" + ); + _uniformCache.defaultColorEnd = _programObject->uniformLocation("defaultColorEnd"); + _uniformCache.activeColor = _programObject->uniformLocation("activeColor"); + _uniformCache.targetInFieldOfViewColor = _programObject->uniformLocation( + "targetInFieldOfViewColor" + ); + _uniformCache.intersectionStartColor = _programObject->uniformLocation( + "intersectionStartColor" + ); + _uniformCache.intersectionEndColor = _programObject->uniformLocation( + "intersectionEndColor" + ); + _uniformCache.squareColor = _programObject->uniformLocation("squareColor"); + _uniformCache.interpolation = _programObject->uniformLocation("interpolation"); + // Fetch information about the specific instrument SpiceManager::FieldOfViewResult res = SpiceManager::ref().fieldOfView( _instrument.name @@ -1201,21 +1221,27 @@ void RenderableFov::render(const RenderData& data, RendererTasks&) { modelTransform); _programObject->setUniform( - "modelViewProjectionTransform", + _uniformCache.modelViewProjection, modelViewProjectionTransform ); - _programObject->setUniform("defaultColorStart", _colors.defaultStart); - _programObject->setUniform("defaultColorEnd", _colors.defaultEnd); - _programObject->setUniform("activeColor", _colors.active); + _programObject->setUniform(_uniformCache.defaultColorStart, _colors.defaultStart); + _programObject->setUniform(_uniformCache.defaultColorEnd, _colors.defaultEnd); + _programObject->setUniform(_uniformCache.activeColor, _colors.active); _programObject->setUniform( - "targetInFieldOfViewColor", + _uniformCache.targetInFieldOfViewColor, _colors.targetInFieldOfView ); - _programObject->setUniform("intersectionStartColor", _colors.intersectionStart); - _programObject->setUniform("intersectionEndColor", _colors.intersectionEnd); - _programObject->setUniform("squareColor", _colors.square); - _programObject->setUniform("interpolation", _interpolationTime); + _programObject->setUniform( + _uniformCache.intersectionStartColor, + _colors.intersectionStart + ); + _programObject->setUniform( + _uniformCache.intersectionEndColor, + _colors.intersectionEnd + ); + _programObject->setUniform(_uniformCache.squareColor, _colors.square); + _programObject->setUniform(_uniformCache.interpolation, _interpolationTime); GLenum mode = _drawSolid ? GL_TRIANGLE_STRIP : GL_LINES; @@ -1259,6 +1285,33 @@ void RenderableFov::update(const UpdateData& data) { _interpolationTime = 0.f; } } + + if (_programObject->isDirty()) { + _programObject->rebuildFromFile(); + + + _uniformCache.modelViewProjection = _programObject->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.defaultColorStart = _programObject->uniformLocation( + "defaultColorStart" + ); + _uniformCache.defaultColorEnd = _programObject->uniformLocation( + "defaultColorEnd" + ); + _uniformCache.activeColor = _programObject->uniformLocation("activeColor"); + _uniformCache.targetInFieldOfViewColor = _programObject->uniformLocation( + "targetInFieldOfViewColor" + ); + _uniformCache.intersectionStartColor = _programObject->uniformLocation( + "intersectionStartColor" + ); + _uniformCache.intersectionEndColor = _programObject->uniformLocation( + "intersectionEndColor" + ); + _uniformCache.squareColor = _programObject->uniformLocation("squareColor"); + _uniformCache.interpolation = _programObject->uniformLocation("interpolation"); + } } std::pair RenderableFov::determineTarget(double time) { diff --git a/modules/spacecraftinstruments/rendering/renderablefov.h b/modules/spacecraftinstruments/rendering/renderablefov.h index 0c54fb7d72..909c2a0010 100644 --- a/modules/spacecraftinstruments/rendering/renderablefov.h +++ b/modules/spacecraftinstruments/rendering/renderablefov.h @@ -35,6 +35,7 @@ #include #include +#include namespace ghoul::opengl { class ProgramObject; @@ -92,6 +93,9 @@ private: properties::BoolProperty _drawSolid; properties::DoubleProperty _standOffDistance; std::unique_ptr _programObject; + UniformCache(modelViewProjection, defaultColorStart, defaultColorEnd, activeColor, + targetInFieldOfViewColor, intersectionStartColor, intersectionEndColor, + squareColor, interpolation) _uniformCache; // instance variables bool _rebuild = false; diff --git a/modules/spacecraftinstruments/rendering/renderablemodelprojection.cpp b/modules/spacecraftinstruments/rendering/renderablemodelprojection.cpp index faf130e40e..9cb7ac32dc 100644 --- a/modules/spacecraftinstruments/rendering/renderablemodelprojection.cpp +++ b/modules/spacecraftinstruments/rendering/renderablemodelprojection.cpp @@ -188,6 +188,25 @@ void RenderableModelProjection::initializeGL() { absPath("${MODULE_SPACECRAFTINSTRUMENTS}/shaders/renderableModel_fs.glsl") ); + _mainUniformCache.performShading = _programObject->uniformLocation("_performShading"); + _mainUniformCache.directionToSunViewSpace = _programObject->uniformLocation( + "directionToSunViewSpace" + ); + _mainUniformCache.modelViewTransform = _programObject->uniformLocation( + "modelViewTransform" + ); + _mainUniformCache.projectionTransform = _programObject->uniformLocation( + "projectionTransform" + ); + _mainUniformCache.projectionFading = _programObject->uniformLocation( + "_projectionFading" + ); + _mainUniformCache.baseTexture = _programObject->uniformLocation( + "baseTexture" + ); + _mainUniformCache.projectionTexture = _programObject->uniformLocation( + "projectionTexture" + ); _fboProgramObject = ghoul::opengl::ProgramObject::Build( "ProjectionPass", @@ -198,9 +217,17 @@ void RenderableModelProjection::initializeGL() { "${MODULE_SPACECRAFTINSTRUMENTS}/shaders/renderableModelProjection_fs.glsl" ) ); - _fboProgramObject->setIgnoreUniformLocationError( - ghoul::opengl::ProgramObject::IgnoreError::Yes + + _fboUniformCache.projectionTexture = _fboProgramObject->uniformLocation( + "projectionTexture" ); + _fboUniformCache.needShadowMap = _fboProgramObject->uniformLocation("needShadowMap"); + _fboUniformCache.ProjectorMatrix = _fboProgramObject->uniformLocation( + "ProjectorMatrix" + ); + _fboUniformCache.ModelTransform = _fboProgramObject->uniformLocation("ModelTransform"); + _fboUniformCache.boresight = _fboProgramObject->uniformLocation("boresight"); + _depthFboProgramObject = ghoul::opengl::ProgramObject::Build( "DepthPass", @@ -208,6 +235,13 @@ void RenderableModelProjection::initializeGL() { absPath("${MODULE_SPACECRAFTINSTRUMENTS}/shaders/renderableModelDepth_fs.glsl") ); + _depthFboUniformCache.ProjectorMatrix = _depthFboProgramObject->uniformLocation( + "ProjectorMatrix" + ); + _depthFboUniformCache.ModelTransform = _depthFboProgramObject->uniformLocation( + "ModelTransform" + ); + loadTextures(); _projectionComponent.initializeGL(); @@ -267,26 +301,34 @@ void RenderableModelProjection::render(const RenderData& data, RendererTasks&) { data.camera.combinedViewMatrix() ) * directionToSun; - _programObject->setUniform("_performShading", _performShading); - _programObject->setUniform("directionToSunViewSpace", directionToSunViewSpace); - _programObject->setUniform("modelViewTransform", glm::mat4(modelViewTransform)); - _programObject->setUniform("projectionTransform", data.camera.projectionMatrix()); + _programObject->setUniform(_mainUniformCache.performShading, _performShading); _programObject->setUniform( - "_projectionFading", + _mainUniformCache.directionToSunViewSpace, + directionToSunViewSpace + ); + _programObject->setUniform( + _mainUniformCache.modelViewTransform, + glm::mat4(modelViewTransform) + ); + _programObject->setUniform( + _mainUniformCache.projectionTransform, + data.camera.projectionMatrix() + ); + _programObject->setUniform( + _mainUniformCache.projectionFading, _projectionComponent.projectionFading() ); - _geometry->setUniforms(*_programObject); ghoul::opengl::TextureUnit unit[2]; unit[0].activate(); _baseTexture->bind(); - _programObject->setUniform("baseTexture", unit[0]); + _programObject->setUniform(_mainUniformCache.baseTexture, unit[0]); unit[1].activate(); _projectionComponent.projectionTexture().bind(); - _programObject->setUniform("projectionTexture", unit[1]); + _programObject->setUniform(_mainUniformCache.projectionTexture, unit[1]); _geometry->render(); @@ -296,16 +338,59 @@ void RenderableModelProjection::render(const RenderData& data, RendererTasks&) { void RenderableModelProjection::update(const UpdateData& data) { if (_programObject->isDirty()) { _programObject->rebuildFromFile(); + + _mainUniformCache.performShading = _programObject->uniformLocation( + "_performShading" + ); + _mainUniformCache.directionToSunViewSpace = _programObject->uniformLocation( + "directionToSunViewSpace" + ); + _mainUniformCache.modelViewTransform = _programObject->uniformLocation( + "modelViewTransform" + ); + _mainUniformCache.projectionTransform = _programObject->uniformLocation( + "projectionTransform" + ); + _mainUniformCache.projectionFading = _programObject->uniformLocation( + "_projectionFading" + ); + _mainUniformCache.baseTexture = _programObject->uniformLocation( + "baseTexture" + ); + _mainUniformCache.projectionTexture = _programObject->uniformLocation( + "projectionTexture" + ); } if (_fboProgramObject->isDirty()) { _fboProgramObject->rebuildFromFile(); + + _fboUniformCache.projectionTexture = _fboProgramObject->uniformLocation( + "projectionTexture" + ); + _fboUniformCache.needShadowMap = _fboProgramObject->uniformLocation( + "needShadowMap" + ); + _fboUniformCache.ProjectorMatrix = _fboProgramObject->uniformLocation( + "ProjectorMatrix" + ); + _fboUniformCache.ModelTransform = _fboProgramObject->uniformLocation( + "ModelTransform" + ); + _fboUniformCache.boresight = _fboProgramObject->uniformLocation("boresight"); } _projectionComponent.update(); if (_depthFboProgramObject->isDirty()) { _depthFboProgramObject->rebuildFromFile(); + + _depthFboUniformCache.ProjectorMatrix = _depthFboProgramObject->uniformLocation( + "ProjectorMatrix" + ); + _depthFboUniformCache.ModelTransform = _depthFboProgramObject->uniformLocation( + "ModelTransform" + ); } _time = data.time.j2000Seconds(); @@ -336,8 +421,14 @@ void RenderableModelProjection::imageProjectGPU( if (_projectionComponent.needsShadowMap()) { _projectionComponent.depthMapRenderBegin(); _depthFboProgramObject->activate(); - _depthFboProgramObject->setUniform("ProjectorMatrix", _projectorMatrix); - _depthFboProgramObject->setUniform("ModelTransform", _transform); + _depthFboProgramObject->setUniform( + _depthFboUniformCache.ProjectorMatrix, + _projectorMatrix + ); + _depthFboProgramObject->setUniform( + _depthFboUniformCache.ModelTransform, + _transform + ); _geometry->setUniforms(*_fboProgramObject); _geometry->render(); @@ -352,9 +443,12 @@ void RenderableModelProjection::imageProjectGPU( ghoul::opengl::TextureUnit unitFbo; unitFbo.activate(); projectionTexture->bind(); - _fboProgramObject->setUniform("projectionTexture", unitFbo); + _fboProgramObject->setUniform(_fboUniformCache.projectionTexture, unitFbo); - _fboProgramObject->setUniform("needShadowMap", _projectionComponent.needsShadowMap()); + _fboProgramObject->setUniform( + _fboUniformCache.needShadowMap, + _projectionComponent.needsShadowMap() + ); ghoul::opengl::TextureUnit unitDepthFbo; if (_projectionComponent.needsShadowMap()) { @@ -363,9 +457,9 @@ void RenderableModelProjection::imageProjectGPU( _fboProgramObject->setUniform("depthTexture", unitDepthFbo); } - _fboProgramObject->setUniform("ProjectorMatrix", _projectorMatrix); - _fboProgramObject->setUniform("ModelTransform", _transform); - _fboProgramObject->setUniform("boresight", _boresight); + _fboProgramObject->setUniform(_fboUniformCache.ProjectorMatrix, _projectorMatrix); + _fboProgramObject->setUniform(_fboUniformCache.ModelTransform, _transform); + _fboProgramObject->setUniform(_fboUniformCache.boresight, _boresight); _geometry->setUniforms(*_fboProgramObject); _geometry->render(); diff --git a/modules/spacecraftinstruments/rendering/renderablemodelprojection.h b/modules/spacecraftinstruments/rendering/renderablemodelprojection.h index 05e72c50c8..2f322e119b 100644 --- a/modules/spacecraftinstruments/rendering/renderablemodelprojection.h +++ b/modules/spacecraftinstruments/rendering/renderablemodelprojection.h @@ -27,15 +27,15 @@ #include -#include - #include +#include #include #include - #include +#include + namespace ghoul::opengl { class ProgramObject; class Texture; @@ -79,8 +79,14 @@ private: properties::StringProperty _colorTexturePath; std::unique_ptr _programObject; + UniformCache(performShading, directionToSunViewSpace, modelViewTransform, + projectionTransform, projectionFading, baseTexture, + projectionTexture) _mainUniformCache; std::unique_ptr _fboProgramObject; + UniformCache(projectionTexture, needShadowMap, ProjectorMatrix, ModelTransform, + boresight) _fboUniformCache; std::unique_ptr _depthFboProgramObject; + UniformCache(ProjectorMatrix, ModelTransform) _depthFboUniformCache; std::unique_ptr _baseTexture; diff --git a/modules/spacecraftinstruments/rendering/renderableplanetprojection.cpp b/modules/spacecraftinstruments/rendering/renderableplanetprojection.cpp index dc6eea09a8..09abc8a2cb 100644 --- a/modules/spacecraftinstruments/rendering/renderableplanetprojection.cpp +++ b/modules/spacecraftinstruments/rendering/renderableplanetprojection.cpp @@ -318,6 +318,26 @@ void RenderablePlanetProjection::initializeGL() { absPath("${MODULE_SPACECRAFTINSTRUMENTS}/shaders/renderablePlanet_fs.glsl") ); + _mainUniformCache.sunPos = _programObject->uniformLocation("sun_pos"); + _mainUniformCache.modelTransform = _programObject->uniformLocation("modelTransform"); + _mainUniformCache.modelViewProjectionTransform = _programObject->uniformLocation( + "modelViewProjectionTransform" + ); + _mainUniformCache.hasBaseMap = _programObject->uniformLocation("_hasBaseMap"); + _mainUniformCache.hasHeightMap = _programObject->uniformLocation("_hasHeightMap"); + _mainUniformCache.heightExaggeration = _programObject->uniformLocation( + "_heightExaggeration" + ); + _mainUniformCache.meridianShift = _programObject->uniformLocation("_meridianShift"); + _mainUniformCache.projectionFading = _programObject->uniformLocation( + "_projectionFading" + ); + _mainUniformCache.baseTexture = _programObject->uniformLocation("baseTexture"); + _mainUniformCache.projectionTexture = _programObject->uniformLocation( + "projectionTexture" + ); + _mainUniformCache.heightTexture = _programObject->uniformLocation("heightTexture"); + _fboProgramObject = ghoul::opengl::ProgramObject::Build( "fboPassProgram", absPath( @@ -328,6 +348,20 @@ void RenderablePlanetProjection::initializeGL() { ) ); + _fboUniformCache.projectionTexture = _fboProgramObject->uniformLocation( + "projectionTexture" + ); + _fboUniformCache.projectorMatrix = _fboProgramObject->uniformLocation( + "ProjectorMatrix" + ); + _fboUniformCache.modelTransform = _fboProgramObject->uniformLocation( + "ModelTransform" + ); + _fboUniformCache.scaling = _fboProgramObject->uniformLocation("_scaling"); + _fboUniformCache.boresight = _fboProgramObject->uniformLocation("boresight"); + _fboUniformCache.radius = _fboProgramObject->uniformLocation("_radius"); + _fboUniformCache.segments = _fboProgramObject->uniformLocation("_segments"); + loadColorTexture(); loadHeightTexture(); _projectionComponent.initializeGL(); @@ -394,17 +428,17 @@ void RenderablePlanetProjection::imageProjectGPU( ghoul::opengl::TextureUnit unitFbo; unitFbo.activate(); projectionTexture->bind(); - _fboProgramObject->setUniform("projectionTexture", unitFbo); + _fboProgramObject->setUniform(_fboUniformCache.projectionTexture, unitFbo); - _fboProgramObject->setUniform("ProjectorMatrix", _projectorMatrix); - _fboProgramObject->setUniform("ModelTransform" , _transform); - _fboProgramObject->setUniform("_scaling" , _camScaling); - _fboProgramObject->setUniform("boresight" , _boresight); + _fboProgramObject->setUniform(_fboUniformCache.projectorMatrix, _projectorMatrix); + _fboProgramObject->setUniform(_fboUniformCache.modelTransform, _transform); + _fboProgramObject->setUniform(_fboUniformCache.scaling, _camScaling); + _fboProgramObject->setUniform(_fboUniformCache.boresight, _boresight); if (_geometry->hasProperty("Radius")) { ghoul::any r = _geometry->property("Radius")->get(); if (glm::vec3* radius = ghoul::any_cast(&r)){ - _fboProgramObject->setUniform("_radius", radius); + _fboProgramObject->setUniform(_fboUniformCache.radius, radius); } } else { LERROR("Geometry object needs to provide radius"); @@ -412,7 +446,7 @@ void RenderablePlanetProjection::imageProjectGPU( if (_geometry->hasProperty("Segments")) { ghoul::any s = _geometry->property("Segments")->get(); if (int* segments = ghoul::any_cast(&s)) { - _fboProgramObject->setUniform("_segments", segments[0]); + _fboProgramObject->setUniform(_fboUniformCache.segments, segments[0]); } }else{ LERROR("Geometry object needs to provide segment count"); @@ -525,7 +559,7 @@ void RenderablePlanetProjection::render(const RenderData& data, RendererTasks&) // Main renderpass _programObject->activate(); - _programObject->setUniform("sun_pos", sun_pos.vec3()); + _programObject->setUniform(_mainUniformCache.sunPos, sun_pos.vec3()); //_programObject->setUniform("ViewProjection" , data.camera.viewProjectionMatrix()); //_programObject->setUniform("ModelTransform" , _transform); @@ -547,16 +581,21 @@ void RenderablePlanetProjection::render(const RenderData& data, RendererTasks&) glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * modelTransform; - _programObject->setUniform("modelTransform", glm::mat4(modelTransform)); - _programObject->setUniform("modelViewProjectionTransform", - data.camera.projectionMatrix() * glm::mat4(modelViewTransform)); + _programObject->setUniform(_mainUniformCache.modelTransform, glm::mat4(modelTransform)); + _programObject->setUniform(_mainUniformCache + .modelViewProjectionTransform, + data.camera.projectionMatrix() * glm::mat4(modelViewTransform) + ); - _programObject->setUniform("_hasBaseMap", _baseTexture != nullptr); - _programObject->setUniform("_hasHeightMap", _heightMapTexture != nullptr); - _programObject->setUniform("_heightExaggeration", _heightExaggeration); - _programObject->setUniform("_meridianShift", _meridianShift); + _programObject->setUniform(_mainUniformCache.hasBaseMap, _baseTexture != nullptr); _programObject->setUniform( - "_projectionFading", + _mainUniformCache.hasHeightMap, + _heightMapTexture != nullptr + ); + _programObject->setUniform(_mainUniformCache.heightExaggeration, _heightExaggeration); + _programObject->setUniform(_mainUniformCache.meridianShift, _meridianShift); + _programObject->setUniform( + _mainUniformCache.projectionFading, _projectionComponent.projectionFading() ); @@ -564,17 +603,17 @@ void RenderablePlanetProjection::render(const RenderData& data, RendererTasks&) if (_baseTexture) { unit[0].activate(); _baseTexture->bind(); - _programObject->setUniform("baseTexture", unit[0]); + _programObject->setUniform(_mainUniformCache.baseTexture, unit[0]); } unit[1].activate(); _projectionComponent.projectionTexture().bind(); - _programObject->setUniform("projectionTexture", unit[1]); + _programObject->setUniform(_mainUniformCache.projectionTexture, unit[1]); if (_heightMapTexture) { unit[2].activate(); _heightMapTexture->bind(); - _programObject->setUniform("heightTexture", unit[2]); + _programObject->setUniform(_mainUniformCache.heightTexture, unit[2]); } _geometry->render(); @@ -582,12 +621,52 @@ void RenderablePlanetProjection::render(const RenderData& data, RendererTasks&) } void RenderablePlanetProjection::update(const UpdateData& data) { - if (_fboProgramObject->isDirty()) { - _fboProgramObject->rebuildFromFile(); - } - if (_programObject->isDirty()) { _programObject->rebuildFromFile(); + + _mainUniformCache.sunPos = _programObject->uniformLocation("sun_pos"); + _mainUniformCache.modelTransform = _programObject->uniformLocation( + "modelTransform" + ); + _mainUniformCache.modelViewProjectionTransform = _programObject->uniformLocation( + "modelViewProjectionTransform" + ); + _mainUniformCache.hasBaseMap = _programObject->uniformLocation("_hasBaseMap"); + _mainUniformCache.hasHeightMap = _programObject->uniformLocation("_hasHeightMap"); + _mainUniformCache.heightExaggeration = _programObject->uniformLocation( + "_heightExaggeration" + ); + _mainUniformCache.meridianShift = _programObject->uniformLocation( + "_meridianShift" + ); + _mainUniformCache.projectionFading = _programObject->uniformLocation( + "_projectionFading" + ); + _mainUniformCache.baseTexture = _programObject->uniformLocation("baseTexture"); + _mainUniformCache.projectionTexture = _programObject->uniformLocation( + "projectionTexture" + ); + _mainUniformCache.heightTexture = _programObject->uniformLocation( + "heightTexture" + ); + } + + if (_fboProgramObject->isDirty()) { + _fboProgramObject->rebuildFromFile(); + + _fboUniformCache.projectionTexture = _fboProgramObject->uniformLocation( + "projectionTexture" + ); + _fboUniformCache.projectorMatrix = _fboProgramObject->uniformLocation( + "ProjectorMatrix" + ); + _fboUniformCache.modelTransform = _fboProgramObject->uniformLocation( + "ModelTransform" + ); + _fboUniformCache.scaling = _fboProgramObject->uniformLocation("_scaling"); + _fboUniformCache.boresight = _fboProgramObject->uniformLocation("boresight"); + _fboUniformCache.radius = _fboProgramObject->uniformLocation("_radius"); + _fboUniformCache.segments = _fboProgramObject->uniformLocation("_segments"); } if (_colorTextureDirty) { diff --git a/modules/spacecraftinstruments/rendering/renderableplanetprojection.h b/modules/spacecraftinstruments/rendering/renderableplanetprojection.h index 4052585ff7..517fea6599 100644 --- a/modules/spacecraftinstruments/rendering/renderableplanetprojection.h +++ b/modules/spacecraftinstruments/rendering/renderableplanetprojection.h @@ -32,6 +32,8 @@ #include #include +#include + namespace openspace { namespace documentation { struct Documentation; } @@ -76,6 +78,12 @@ private: std::unique_ptr _programObject; std::unique_ptr _fboProgramObject; + UniformCache(sunPos, modelTransform, modelViewProjectionTransform, hasBaseMap, + hasHeightMap, heightExaggeration, meridianShift, projectionFading, + baseTexture, projectionTexture, heightTexture) _mainUniformCache; + + UniformCache(projectionTexture, projectorMatrix, modelTransform, scaling, boresight, + radius, segments) _fboUniformCache; std::unique_ptr _baseTexture; std::unique_ptr _heightMapTexture; diff --git a/modules/spacecraftinstruments/rendering/renderableshadowcylinder.cpp b/modules/spacecraftinstruments/rendering/renderableshadowcylinder.cpp index 95241813e8..5de7efbecd 100644 --- a/modules/spacecraftinstruments/rendering/renderableshadowcylinder.cpp +++ b/modules/spacecraftinstruments/rendering/renderableshadowcylinder.cpp @@ -271,6 +271,13 @@ void RenderableShadowCylinder::initializeGL() { absPath("${MODULE_SPACECRAFTINSTRUMENTS}/shaders/terminatorshadow_vs.glsl"), absPath("${MODULE_SPACECRAFTINSTRUMENTS}/shaders/terminatorshadow_fs.glsl") ); + + _uniformCache.modelViewProjectionTransform = _shader->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.shadowColor = _shader->uniformLocation( + "shadowColor" + ); } void RenderableShadowCylinder::deinitializeGL() { @@ -301,15 +308,12 @@ void RenderableShadowCylinder::render(const RenderData& data, RendererTasks&) { glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale)); glm::dmat4 modelViewTransform = data.camera.combinedViewMatrix() * modelTransform; - _shader->setUniform("modelViewProjectionTransform", - data.camera.projectionMatrix() * glm::mat4(modelViewTransform)); + _shader->setUniform( + _uniformCache.modelViewProjectionTransform, + data.camera.projectionMatrix() * glm::mat4(modelViewTransform) + ); - //_shader->setUniform("ViewProjection", data.camera.viewProjectionMatrix()); - //_shader->setUniform("ModelTransform", glm::mat4(_stateMatrix)); - - - _shader->setUniform("shadowColor", _shadowColor); - //setPscUniforms(*_shader.get(), data.camera, data.position); + _shader->setUniform(_uniformCache.shadowColor, _shadowColor); glBindVertexArray(_vao); glDrawArrays(GL_TRIANGLE_STRIP, 0, static_cast(_vertices.size())); @@ -328,6 +332,13 @@ void RenderableShadowCylinder::update(const UpdateData& data) { ); if (_shader->isDirty()) { _shader->rebuildFromFile(); + + _uniformCache.modelViewProjectionTransform = _shader->uniformLocation( + "modelViewProjectionTransform" + ); + _uniformCache.shadowColor = _shader->uniformLocation( + "shadowColor" + ); } createCylinder(data.time.j2000Seconds()); } diff --git a/modules/spacecraftinstruments/rendering/renderableshadowcylinder.h b/modules/spacecraftinstruments/rendering/renderableshadowcylinder.h index badc656088..3d8c99e987 100644 --- a/modules/spacecraftinstruments/rendering/renderableshadowcylinder.h +++ b/modules/spacecraftinstruments/rendering/renderableshadowcylinder.h @@ -36,6 +36,7 @@ #include #include +#include namespace ghoul::opengl { class ProgramObject; } @@ -78,6 +79,7 @@ private: properties::OptionProperty _aberration; std::unique_ptr _shader; + UniformCache(modelViewProjectionTransform, shadowColor) _uniformCache; glm::dmat3 _stateMatrix; diff --git a/modules/touch/include/touchmarker.h b/modules/touch/include/touchmarker.h index 1ca7ae409f..0da92619a0 100644 --- a/modules/touch/include/touchmarker.h +++ b/modules/touch/include/touchmarker.h @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -46,28 +47,29 @@ namespace ghoul::opengl { class ProgramObject; } namespace openspace { class TouchMarker : public properties::PropertyOwner { - public: - TouchMarker(); +public: + TouchMarker(); - bool initialize(); - bool deinitialize(); + void initialize(); + void deinitialize(); - void render(const std::vector& list); + void render(const std::vector& list); - private: - void createVertexList(const std::vector& list); +private: + void createVertexList(const std::vector& list); - properties::BoolProperty _visible; - properties::FloatProperty _radiusSize; - properties::FloatProperty _transparency; - properties::FloatProperty _thickness; - properties::Vec3Property _color; + properties::BoolProperty _visible; + properties::FloatProperty _radiusSize; + properties::FloatProperty _transparency; + properties::FloatProperty _thickness; + properties::Vec3Property _color; - std::unique_ptr _shader; + std::unique_ptr _shader; + UniformCache(radius, transparency, thickness, color) _uniformCache; - GLuint _quad = 0; - GLuint _vertexPositionBuffer = 0; - int _numFingers = 0; + GLuint _quad = 0; + GLuint _vertexPositionBuffer = 0; + int _numFingers = 0; }; } // openspace namespace diff --git a/modules/touch/src/touchmarker.cpp b/modules/touch/src/touchmarker.cpp index 13816e1544..59bebe059d 100644 --- a/modules/touch/src/touchmarker.cpp +++ b/modules/touch/src/touchmarker.cpp @@ -89,24 +89,23 @@ TouchMarker::TouchMarker() addProperty(_color); } -bool TouchMarker::initialize() { +void TouchMarker::initialize() { glGenVertexArrays(1, &_quad); // generate array glGenBuffers(1, &_vertexPositionBuffer); // generate buffer - try { - _shader = OsEng.renderEngine().buildRenderProgram( - "MarkerProgram", - absPath("${MODULE_TOUCH}/shaders/marker_vs.glsl"), - absPath("${MODULE_TOUCH}/shaders/marker_fs.glsl") - ); - } - catch (const ghoul::opengl::ShaderObject::ShaderCompileError& e) { - LERRORC(e.component, e.what()); - } - return (_shader != nullptr); + _shader = OsEng.renderEngine().buildRenderProgram( + "MarkerProgram", + absPath("${MODULE_TOUCH}/shaders/marker_vs.glsl"), + absPath("${MODULE_TOUCH}/shaders/marker_fs.glsl") + ); + + _uniformCache.radius = _shader->uniformLocation("radius"); + _uniformCache.transparency = _shader->uniformLocation("transparency"); + _uniformCache.thickness = _shader->uniformLocation("thickness"); + _uniformCache.color = _shader->uniformLocation("color"); } -bool TouchMarker::deinitialize() { +void TouchMarker::deinitialize() { glDeleteVertexArrays(1, &_quad); _quad = 0; @@ -118,7 +117,6 @@ bool TouchMarker::deinitialize() { renderEngine.removeRenderProgram(_shader); _shader = nullptr; } - return true; } void TouchMarker::render(const std::vector& list) { @@ -126,10 +124,10 @@ void TouchMarker::render(const std::vector& list) { createVertexList(list); _shader->activate(); - _shader->setUniform("radius", _radiusSize); - _shader->setUniform("transparency", _transparency); - _shader->setUniform("thickness", _thickness); - _shader->setUniform("color", _color.value()); + _shader->setUniform(_uniformCache.radius, _radiusSize); + _shader->setUniform(_uniformCache.transparency, _transparency); + _shader->setUniform(_uniformCache.thickness, _thickness); + _shader->setUniform(_uniformCache.color, _color.value()); glEnable(GL_BLEND); glBlendEquation(GL_FUNC_ADD); diff --git a/src/interaction/luaconsole.cpp b/src/interaction/luaconsole.cpp index 79297e0df1..180ef6ba37 100644 --- a/src/interaction/luaconsole.cpp +++ b/src/interaction/luaconsole.cpp @@ -227,6 +227,11 @@ void LuaConsole::initialize() { absPath("${SHADERS}/luaconsole.frag") ); + _uniformCache.res = _program->uniformLocation("res"); + _uniformCache.color = _program->uniformLocation("color"); + _uniformCache.height = _program->uniformLocation("height"); + _uniformCache.ortho = _program->uniformLocation("ortho"); + GLfloat data[] = { 0.f, 0.f, 1.f, 1.f, @@ -587,7 +592,6 @@ bool LuaConsole::keyboardCallback(Key key, KeyModifier modifier, KeyAction actio default: return true; } - } void LuaConsole::charCallback(unsigned int codepoint, @@ -661,6 +665,11 @@ void LuaConsole::render() { if (_program->isDirty()) { _program->rebuildFromFile(); + + _uniformCache.res = _program->uniformLocation("res"); + _uniformCache.color = _program->uniformLocation("color"); + _uniformCache.height = _program->uniformLocation("height"); + _uniformCache.ortho = _program->uniformLocation("ortho"); } const glm::vec2 dpiScaling = OsEng.windowWrapper().dpiScaling(); @@ -676,11 +685,11 @@ void LuaConsole::render() { _program->activate(); - _program->setUniform("res", res); - _program->setUniform("color", _backgroundColor); - _program->setUniform("height", _currentHeight / res.y); + _program->setUniform(_uniformCache.res, res); + _program->setUniform(_uniformCache.color, _backgroundColor); + _program->setUniform(_uniformCache.height, _currentHeight / res.y); _program->setUniform( - "ortho", + _uniformCache.ortho, glm::ortho( 0.f, static_cast(res.x), 0.f, static_cast(res.y) ) @@ -691,12 +700,15 @@ void LuaConsole::render() { glDrawArrays(GL_TRIANGLES, 0, 6); // Draw the highlight lines above and below the background - _program->setUniform("color", _highlightColor); + _program->setUniform(_uniformCache.color, _highlightColor); glDrawArrays(GL_LINES, 1, 4); // Draw the separator between the current entry box and the history - _program->setUniform("color", _separatorColor); - _program->setUniform("height", _currentHeight / res.y - 2.5f * EntryFontSize / res.y); + _program->setUniform(_uniformCache.color, _separatorColor); + _program->setUniform( + _uniformCache.height, + _currentHeight / res.y - 2.5f * EntryFontSize / res.y + ); glDrawArrays(GL_LINES, 1, 2); _program->deactivate(); diff --git a/src/rendering/framebufferrenderer.cpp b/src/rendering/framebufferrenderer.cpp index bca8693bce..ef47c5a8f7 100644 --- a/src/rendering/framebufferrenderer.cpp +++ b/src/rendering/framebufferrenderer.cpp @@ -225,15 +225,15 @@ void FramebufferRenderer::initialize() { glBindFramebuffer(GL_FRAMEBUFFER, defaultFbo); - try { - _resolveProgram = ghoul::opengl::ProgramObject::Build( - "Framebuffer Resolve", - absPath("${SHADERS}/framebuffer/resolveframebuffer.vert"), - absPath("${SHADERS}/framebuffer/resolveframebuffer.frag") - ); - } catch (const ghoul::RuntimeError& e) { - LERRORC(e.component, e.message); - } + _resolveProgram = ghoul::opengl::ProgramObject::Build( + "Framebuffer Resolve", + absPath("${SHADERS}/framebuffer/resolveframebuffer.vert"), + absPath("${SHADERS}/framebuffer/resolveframebuffer.frag") + ); + + _uniformCache.mainColorTexture = _resolveProgram->uniformLocation("mainColorTexture"); + _uniformCache.blackoutFactor = _resolveProgram->uniformLocation("blackoutFactor"); + _uniformCache.nAaSamples = _resolveProgram->uniformLocation("nAaSamples"); OsEng.renderEngine().raycasterManager().addListener(*this); OsEng.renderEngine().deferredcasterManager().addListener(*this); @@ -298,12 +298,13 @@ void FramebufferRenderer::update() { } if (_resolveProgram->isDirty()) { - try { - _resolveProgram->rebuildFromFile(); - } - catch (const ghoul::RuntimeError& error) { - LERRORC(error.component, error.message); - } + _resolveProgram->rebuildFromFile(); + + _uniformCache.mainColorTexture = _resolveProgram->uniformLocation( + "mainColorTexture" + ); + _uniformCache.blackoutFactor = _resolveProgram->uniformLocation("blackoutFactor"); + _uniformCache.nAaSamples = _resolveProgram->uniformLocation("nAaSamples"); } for (auto& program : _exitPrograms) { @@ -1178,9 +1179,9 @@ void FramebufferRenderer::render(float blackoutFactor, bool doPerformanceMeasure mainColorTextureUnit.activate(); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _mainColorTexture); - _resolveProgram->setUniform("mainColorTexture", mainColorTextureUnit); - _resolveProgram->setUniform("blackoutFactor", blackoutFactor); - _resolveProgram->setUniform("nAaSamples", _nAaSamples); + _resolveProgram->setUniform(_uniformCache.mainColorTexture, mainColorTextureUnit); + _resolveProgram->setUniform(_uniformCache.blackoutFactor, blackoutFactor); + _resolveProgram->setUniform(_uniformCache.nAaSamples, _nAaSamples); glBindVertexArray(_screenQuad); glDrawArrays(GL_TRIANGLES, 0, 6); glBindVertexArray(0); diff --git a/src/rendering/loadingscreen.cpp b/src/rendering/loadingscreen.cpp index 997bac9a26..e710be8bd9 100644 --- a/src/rendering/loadingscreen.cpp +++ b/src/rendering/loadingscreen.cpp @@ -120,6 +120,10 @@ LoadingScreen::LoadingScreen(ShowMessage showMessage, ShowNodeNames showNodeName absPath("${SHADERS}/loadingscreen.frag") ); + _uniformCache.logoTexture = _program->uniformLocation("logoTexture"); + _uniformCache.useTexture = _program->uniformLocation("useTexture"); + _uniformCache.color = _program->uniformLocation("color"); + _loadingFont = OsEng.fontManager().font( "Loading", LoadingFontSize, @@ -287,15 +291,8 @@ void LoadingScreen::render() { unit.activate(); _logoTexture->bind(); - _program->setUniform( - "logoTexture", - unit - ); - - _program->setUniform( - "useTexture", - true - ); + _program->setUniform(_uniformCache.logoTexture, unit); + _program->setUniform(_uniformCache.useTexture, true); glDrawArrays(GL_TRIANGLES, 0, 6); @@ -341,16 +338,16 @@ void LoadingScreen::render() { glBindBuffer(GL_ARRAY_BUFFER, _progressbar.vboFill); glBufferData(GL_ARRAY_BUFFER, sizeof(dataFill), dataFill, GL_STATIC_DRAW); - _program->setUniform("useTexture", false); + _program->setUniform(_uniformCache.useTexture, false); switch (_phase) { case Phase::Construction: - _program->setUniform("color", PhaseColorConstruction); + _program->setUniform(_uniformCache.color, PhaseColorConstruction); break; case Phase::Synchronization: - _program->setUniform("color", PhaseColorSynchronization); + _program->setUniform(_uniformCache.color, PhaseColorSynchronization); break; case Phase::Initialization: - _program->setUniform("color", PhaseColorInitialization); + _program->setUniform(_uniformCache.color, PhaseColorInitialization); break; } glDrawArrays(GL_TRIANGLES, 0, 6); @@ -402,8 +399,8 @@ void LoadingScreen::render() { glBindBuffer(GL_ARRAY_BUFFER, _progressbar.vboBox); glBufferData(GL_ARRAY_BUFFER, sizeof(dataBox), dataBox, GL_STATIC_DRAW); - _program->setUniform("useTexture", false); - _program->setUniform("color", ProgressbarOutlineColor); + _program->setUniform(_uniformCache.useTexture, false); + _program->setUniform(_uniformCache.color, ProgressbarOutlineColor); glDrawArrays(GL_TRIANGLES, 0, 24); } diff --git a/src/rendering/screenspacerenderable.cpp b/src/rendering/screenspacerenderable.cpp index a0d923809d..f37a73e8b1 100644 --- a/src/rendering/screenspacerenderable.cpp +++ b/src/rendering/screenspacerenderable.cpp @@ -445,6 +445,12 @@ void ScreenSpaceRenderable::createShaders() { "${SHADERS}/render.frag", dict ); + + _uniformCache.occlusionDepth = _shader->uniformLocation("OcclusionDepth"); + _uniformCache.alpha = _shader->uniformLocation("Alpha"); + _uniformCache.modelTransform = _shader->uniformLocation("ModelTransform"); + _uniformCache.viewProj = _shader->uniformLocation("ViewProjectionMatrix"); + _uniformCache.texture = _shader->uniformLocation("texture1"); } } @@ -503,19 +509,19 @@ void ScreenSpaceRenderable::draw(glm::mat4 modelTransform) { glDisable(GL_CULL_FACE); _shader->activate(); - _shader->setUniform("OcclusionDepth", 1.f - _depth); - _shader->setUniform("Alpha", _alpha); - _shader->setUniform("ModelTransform", modelTransform); + _shader->setUniform(_uniformCache.occlusionDepth, 1.f - _depth); + _shader->setUniform(_uniformCache.alpha, _alpha); + _shader->setUniform(_uniformCache.modelTransform, modelTransform); _shader->setUniform( - "ViewProjectionMatrix", + _uniformCache.viewProj, OsEng.renderEngine().camera()->viewProjectionMatrix() ); ghoul::opengl::TextureUnit unit; unit.activate(); _texture->bind(); - _shader->setUniform("texture1", unit); + _shader->setUniform(_uniformCache.texture, unit); glBindVertexArray(_quad); glDrawArrays(GL_TRIANGLES, 0, 6);