diff --git a/include/openspace/engine/wrapper/sgctwindowwrapper.h b/include/openspace/engine/wrapper/sgctwindowwrapper.h index 02a0d25f73..6a1c170dd0 100644 --- a/include/openspace/engine/wrapper/sgctwindowwrapper.h +++ b/include/openspace/engine/wrapper/sgctwindowwrapper.h @@ -45,7 +45,9 @@ public: uint32_t mouseButtons(int maxNumber) const override; glm::ivec2 currentWindowSize() const override; glm::ivec2 currentWindowResolution() const override; - + glm::ivec2 currentDrawBufferResolution() const override; + int currentNumberOfAaSamples() const override; + bool isRegularRendering() const override; glm::mat4 viewProjectionMatrix() const override; diff --git a/include/openspace/engine/wrapper/windowwrapper.h b/include/openspace/engine/wrapper/windowwrapper.h index 31712d7fb2..6a6ce64dd3 100644 --- a/include/openspace/engine/wrapper/windowwrapper.h +++ b/include/openspace/engine/wrapper/windowwrapper.h @@ -26,6 +26,7 @@ #define __WINDOWWRAPPER_H__ #include +#include #include #include @@ -104,6 +105,18 @@ public: virtual glm::ivec2 currentWindowResolution() const; /** + * Returns the resolution of the currently active framebuffer in pixel coordinates. + * On default, this method returns the same size as #currentWindowSize. + * \return The resolution of the currently active window in pixel coordinates + */ + virtual glm::ivec2 currentDrawBufferResolution() const; + + /** + * Returns the number of anti-aliasing samples used in the current window. + */ + virtual int currentNumberOfAaSamples() const; + + /** * Returns true if the current rendering method is regular, i.e., it is * a flat projection without non-linear distortions. Returns false in * other cases, for example fisheye projections. On default, this method will return @@ -163,6 +176,10 @@ public: * Advises the windowing system to take a screenshot. This method defaults to a no-op. */ virtual void takeScreenshot() const; + + struct WindowWrapperException : public ghoul::RuntimeError { + explicit WindowWrapperException(const std::string& msg); + }; }; } // namespace openspace diff --git a/include/openspace/rendering/abufferrenderer.h b/include/openspace/rendering/abufferrenderer.h index d432adbb7c..3f4f6af4bd 100644 --- a/include/openspace/rendering/abufferrenderer.h +++ b/include/openspace/rendering/abufferrenderer.h @@ -104,6 +104,7 @@ private: GLuint _fragmentBuffer; GLuint _fragmentTexture; GLuint _vertexPositionBuffer; + int _nAaSamples; ghoul::Dictionary _rendererData; }; // ABufferRenderer diff --git a/shaders/abuffer/resolveabuffer.frag b/shaders/abuffer/resolveabuffer.frag index d4039d7b68..556cfeca3a 100644 --- a/shaders/abuffer/resolveabuffer.frag +++ b/shaders/abuffer/resolveabuffer.frag @@ -31,7 +31,7 @@ layout (location = 0) out vec4 finalColor; uniform float blackoutFactor; - +uniform int nAaSamples; void sortFragments(uint nFrags) { ABufferFragment tmp; @@ -95,7 +95,7 @@ void main() { uint nSamples = countSamples(accumulatedMask); color = _color_(fragments[i]); // TODO: Possibly weigh all samples together? - color.a *= float(nSamples) * 0.125; + color.a *= float(nSamples) / float(nAaSamples); finalColor = blend(finalColor, color); } diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 1b011bfd64..7f710c6019 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -537,7 +537,7 @@ void OpenSpaceEngine::loadFonts() { if (!initSuccess) LERROR("Error initializing default font renderer"); - ghoul::fontrendering::FontRenderer::defaultRenderer().setWindowSize(glm::vec2(_windowWrapper->currentWindowResolution())); + ghoul::fontrendering::FontRenderer::defaultRenderer().setWindowSize(glm::vec2(_windowWrapper->currentDrawBufferResolution())); } @@ -626,12 +626,12 @@ void OpenSpaceEngine::postSynchronizationPreDraw() { if (_isMaster && _gui->isEnabled() && _windowWrapper->isRegularRendering()) { glm::vec2 mousePosition = _windowWrapper->mousePosition(); - glm::ivec2 windowResolution = _windowWrapper->currentWindowResolution(); + glm::ivec2 drawBufferResolution = _windowWrapper->currentDrawBufferResolution(); uint32_t mouseButtons = _windowWrapper->mouseButtons(2); double dt = _windowWrapper->averageDeltaTime(); - _gui->startFrame(static_cast(dt), glm::vec2(windowResolution), mousePosition, mouseButtons); + _gui->startFrame(static_cast(dt), glm::vec2(drawBufferResolution), mousePosition, mouseButtons); } } diff --git a/src/engine/wrapper/sgctwindowwrapper.cpp b/src/engine/wrapper/sgctwindowwrapper.cpp index 40c4a92e5f..0d9ffb69d8 100644 --- a/src/engine/wrapper/sgctwindowwrapper.cpp +++ b/src/engine/wrapper/sgctwindowwrapper.cpp @@ -83,6 +83,24 @@ glm::ivec2 SGCTWindowWrapper::currentWindowResolution() const { sgct::Engine::instance()->getCurrentWindowPtr()->getFinalFBODimensions(x, y); return glm::ivec2(x, y); } + +glm::ivec2 SGCTWindowWrapper::currentDrawBufferResolution() const { + sgct_core::Viewport* viewport = sgct::Engine::instance()->getCurrentWindowPtr()->getViewport(0); + if (viewport != nullptr){ + if (viewport->hasSubViewports() && viewport->getNonLinearProjectionPtr()) { + int res = viewport->getNonLinearProjectionPtr()->getCubemapResolution(); + return glm::ivec2(res, res); + } else { + return currentWindowResolution(); + } + } + throw WindowWrapperException("No viewport available"); +} + +int SGCTWindowWrapper::currentNumberOfAaSamples() const { + return sgct::Engine::instance()->getCurrentWindowPtr()->getNumberOfAASamples(); +} + bool SGCTWindowWrapper::isRegularRendering() const { // TODO: Needs to implement the nonlinear rendering check ---abock diff --git a/src/engine/wrapper/windowwrapper.cpp b/src/engine/wrapper/windowwrapper.cpp index 2b0d74c9e9..db23097ce0 100644 --- a/src/engine/wrapper/windowwrapper.cpp +++ b/src/engine/wrapper/windowwrapper.cpp @@ -23,8 +23,14 @@ ****************************************************************************************/ #include +#include +#include namespace openspace { + +WindowWrapper::WindowWrapperException::WindowWrapperException(const std::string& msg) + : ghoul::RuntimeError(msg, "WindowWrapper") +{} void WindowWrapper::setBarrier(bool) {} @@ -54,6 +60,14 @@ glm::ivec2 WindowWrapper::currentWindowResolution() const { return currentWindowSize(); } +glm::ivec2 WindowWrapper::currentDrawBufferResolution() const { + return currentWindowSize(); +} + +int WindowWrapper::currentNumberOfAaSamples() const { + return 1; +} + bool WindowWrapper::isRegularRendering() const { return true; } diff --git a/src/rendering/abufferrenderer.cpp b/src/rendering/abufferrenderer.cpp index 69442ec291..3eaa32608c 100644 --- a/src/rendering/abufferrenderer.cpp +++ b/src/rendering/abufferrenderer.cpp @@ -96,6 +96,12 @@ void ABufferRenderer::initialize() { "${SHADERS}/abuffer/resolveabuffer.frag", dict); + _nAaSamples = OsEng.windowWrapper().currentNumberOfAaSamples(); + if (_nAaSamples > 8) { + LERROR("ABuffer does not support more than 8 MSAA samples."); + _nAaSamples = 8; + } + } void ABufferRenderer::deinitialize() { @@ -163,6 +169,7 @@ void ABufferRenderer::render(float blackoutFactor, bool doPerformanceMeasurement // Step 3: Resolve the buffer _resolveProgram->activate(); _resolveProgram->setUniform("blackoutFactor", blackoutFactor); + _resolveProgram->setUniform("nAaSamples", _nAaSamples); // todo: pre-ray-cast for each volume glBindVertexArray(_screenQuad); glDrawArrays(GL_TRIANGLES, 0, 6); diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index ce33acc61e..9583659e98 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -327,7 +327,7 @@ void RenderEngine::postSynchronizationPreDraw() { bool windowResized = OsEng.windowWrapper().windowHasResized(); if (windowResized) { - glm::ivec2 res = OsEng.windowWrapper().currentWindowResolution(); + glm::ivec2 res = OsEng.windowWrapper().currentDrawBufferResolution(); _renderer->setResolution(res); ghoul::fontrendering::FontRenderer::defaultRenderer().setWindowSize(glm::vec2(res)); } @@ -552,7 +552,7 @@ void RenderEngine::setRendererData(const ghoul::Dictionary& data) { * Set renderer */ void RenderEngine::setRenderer(std::unique_ptr renderer) { - glm::ivec2 res = OsEng.windowWrapper().currentWindowResolution(); + glm::ivec2 res = OsEng.windowWrapper().currentDrawBufferResolution(); if (_renderer) { _renderer->deinitialize();