diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 8bb2941c01..dc24127054 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -802,7 +801,7 @@ void Application::paintGL() { DependencyManager::get()->prepare(); // Viewport is assigned to the size of the framebuffer - QSize size = DependencyManager::get()->getPrimaryFramebufferObject()->size(); + QSize size = DependencyManager::get()->getFrameBufferSize(); glViewport(0, 0, size.width(), size.height()); glMatrixMode(GL_MODELVIEW); @@ -2642,8 +2641,9 @@ void Application::updateShadowMap() { activeRenderingThread = QThread::currentThread(); PerformanceTimer perfTimer("shadowMap"); - QOpenGLFramebufferObject* fbo = DependencyManager::get()->getShadowFramebufferObject(); - fbo->bind(); + auto shadowFramebuffer = DependencyManager::get()->getShadowFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(shadowFramebuffer)); + glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT); @@ -2659,16 +2659,18 @@ void Application::updateShadowMap() { loadViewFrustum(_myCamera, _viewFrustum); int matrixCount = 1; - int targetSize = fbo->width(); + //int targetSize = fbo->width(); + int sourceSize = shadowFramebuffer->getWidth(); + int targetSize = shadowFramebuffer->getWidth(); float targetScale = 1.0f; if (Menu::getInstance()->isOptionChecked(MenuOption::CascadedShadows)) { matrixCount = CASCADED_SHADOW_MATRIX_COUNT; - targetSize = fbo->width() / 2; + targetSize = sourceSize / 2; targetScale = 0.5f; } for (int i = 0; i < matrixCount; i++) { const glm::vec2& coord = MAP_COORDS[i]; - glViewport(coord.s * fbo->width(), coord.t * fbo->height(), targetSize, targetSize); + glViewport(coord.s * sourceSize, coord.t * sourceSize, targetSize, targetSize); // if simple shadow then since the resolution is twice as much as with cascaded, cover 2 regions with the map, not just one int regionIncrement = (matrixCount == 1 ? 2 : 1); @@ -2791,7 +2793,7 @@ void Application::updateShadowMap() { glMatrixMode(GL_MODELVIEW); } - fbo->release(); + // fbo->release(); glViewport(0, 0, _glWidget->getDeviceWidth(), _glWidget->getDeviceHeight()); activeRenderingThread = nullptr; @@ -2837,7 +2839,8 @@ PickRay Application::computePickRay(float x, float y) { } QImage Application::renderAvatarBillboard() { - DependencyManager::get()->getPrimaryFramebufferObject()->bind(); + auto primaryFramebuffer = DependencyManager::get()->getPrimaryFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(primaryFramebuffer)); // the "glow" here causes an alpha of one Glower glower; @@ -2850,7 +2853,7 @@ QImage Application::renderAvatarBillboard() { QImage image(BILLBOARD_SIZE, BILLBOARD_SIZE, QImage::Format_ARGB32); glReadPixels(0, 0, BILLBOARD_SIZE, BILLBOARD_SIZE, GL_BGRA, GL_UNSIGNED_BYTE, image.bits()); - DependencyManager::get()->getPrimaryFramebufferObject()->release(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); return image; } diff --git a/interface/src/devices/OculusManager.cpp b/interface/src/devices/OculusManager.cpp index c865e82187..883775b507 100644 --- a/interface/src/devices/OculusManager.cpp +++ b/interface/src/devices/OculusManager.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -35,6 +34,8 @@ #include "InterfaceLogging.h" #include "Application.h" +#include + template void for_each_eye(Function function) { for (ovrEyeType eye = ovrEyeType::ovrEye_Left; @@ -530,7 +531,8 @@ void OculusManager::display(const glm::quat &bodyOrientation, const glm::vec3 &p if (Menu::getInstance()->isOptionChecked(MenuOption::EnableGlowEffect)) { DependencyManager::get()->prepare(); } else { - DependencyManager::get()->getPrimaryFramebufferObject()->bind(); + auto primaryFBO = DependencyManager::get()->getPrimaryFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(primaryFBO)); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } @@ -621,15 +623,15 @@ void OculusManager::display(const glm::quat &bodyOrientation, const glm::vec3 &p glPopMatrix(); - QOpenGLFramebufferObject * finalFbo = nullptr; + gpu::FramebufferPointer finalFbo; //Bind the output texture from the glow shader. If glow effect is disabled, we just grab the texture if (Menu::getInstance()->isOptionChecked(MenuOption::EnableGlowEffect)) { //Full texture viewport for glow effect glViewport(0, 0, _renderTargetSize.w, _renderTargetSize.h); finalFbo = DependencyManager::get()->render(true); } else { - finalFbo = DependencyManager::get()->getPrimaryFramebufferObject(); - finalFbo->release(); + finalFbo = DependencyManager::get()->getPrimaryFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); } glMatrixMode(GL_PROJECTION); @@ -653,7 +655,7 @@ void OculusManager::display(const glm::quat &bodyOrientation, const glm::vec3 &p //Left over from when OR was not connected. glClear(GL_COLOR_BUFFER_BIT); - glBindTexture(GL_TEXTURE_2D, finalFbo->texture()); + glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(finalFbo->getRenderBuffer(0))); //Renders the distorted mesh onto the screen renderDistortionMesh(eyeRenderPose); diff --git a/interface/src/devices/TV3DManager.cpp b/interface/src/devices/TV3DManager.cpp index f082c6de47..4311e0092f 100644 --- a/interface/src/devices/TV3DManager.cpp +++ b/interface/src/devices/TV3DManager.cpp @@ -11,8 +11,6 @@ #include "InterfaceConfig.h" -#include - #include #include diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index 9eb9648478..4b145b55a0 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -25,7 +25,8 @@ Batch::Batch() : _textures(), _streamFormats(), _transforms(), - _pipelines() + _pipelines(), + _framebuffers() { } @@ -41,7 +42,8 @@ void Batch::clear() { _textures.clear(); _streamFormats.clear(); _transforms.clear(); - _pipelines.clear(); + _pipelines.clear(); + _framebuffers.clear(); } uint32 Batch::cacheData(uint32 size, const void* data) { @@ -186,3 +188,10 @@ void Batch::setUniformTexture(uint32 slot, const TextureView& view) { setUniformTexture(slot, view._texture); } +void Batch::setFramebuffer(const FramebufferPointer& framebuffer) { + ADD_COMMAND(setUniformTexture); + + _params.push_back(_framebuffers.cache(framebuffer)); + +} + diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 6e8a2d1da6..b6bc05ab40 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -23,6 +23,8 @@ #include "Pipeline.h" +#include "Framebuffer.h" + #if defined(NSIGHT_FOUND) #include "nvToolsExt.h" class ProfileRange { @@ -112,6 +114,8 @@ public: void setUniformTexture(uint32 slot, const TexturePointer& view); void setUniformTexture(uint32 slot, const TextureView& view); // not a command, just a shortcut from a TextureView + // Framebuffer Stage + void setFramebuffer(const FramebufferPointer& framebuffer); // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long @@ -170,6 +174,8 @@ public: COMMAND_setUniformBuffer, COMMAND_setUniformTexture, + COMMAND_setFramebuffer, + // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long // term strategy is to get rid of any GL calls in favor of the HIFI GPU API @@ -266,6 +272,7 @@ public: typedef Cache::Vector StreamFormatCaches; typedef Cache::Vector TransformCaches; typedef Cache::Vector PipelineCaches; + typedef Cache::Vector FramebufferCaches; // Cache Data in a byte array if too big to fit in Param // FOr example Mat4s are going there @@ -289,6 +296,7 @@ public: StreamFormatCaches _streamFormats; TransformCaches _transforms; PipelineCaches _pipelines; + FramebufferCaches _framebuffers; protected: }; diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index 8276ff5f95..022ca02d6d 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -15,8 +15,8 @@ #include "Resource.h" #include "Texture.h" -#include "Shader.h" #include "Pipeline.h" +#include "Framebuffer.h" namespace gpu { @@ -47,8 +47,8 @@ public: }; template< typename T > - static void setGPUObject(const Buffer& buffer, T* bo) { - buffer.setGPUObject(bo); + static void setGPUObject(const Buffer& buffer, T* object) { + buffer.setGPUObject(object); } template< typename T > static T* getGPUObject(const Buffer& buffer) { @@ -56,8 +56,8 @@ public: } template< typename T > - static void setGPUObject(const Texture& texture, T* to) { - texture.setGPUObject(to); + static void setGPUObject(const Texture& texture, T* object) { + texture.setGPUObject(object); } template< typename T > static T* getGPUObject(const Texture& texture) { @@ -65,8 +65,8 @@ public: } template< typename T > - static void setGPUObject(const Shader& shader, T* so) { - shader.setGPUObject(so); + static void setGPUObject(const Shader& shader, T* object) { + shader.setGPUObject(object); } template< typename T > static T* getGPUObject(const Shader& shader) { @@ -74,8 +74,8 @@ public: } template< typename T > - static void setGPUObject(const Pipeline& pipeline, T* po) { - pipeline.setGPUObject(po); + static void setGPUObject(const Pipeline& pipeline, T* object) { + pipeline.setGPUObject(object); } template< typename T > static T* getGPUObject(const Pipeline& pipeline) { @@ -83,14 +83,23 @@ public: } template< typename T > - static void setGPUObject(const State& state, T* so) { - state.setGPUObject(so); + static void setGPUObject(const State& state, T* object) { + state.setGPUObject(object); } template< typename T > static T* getGPUObject(const State& state) { return reinterpret_cast(state.getGPUObject()); } + template< typename T > + static void setGPUObject(const Framebuffer& framebuffer, T* object) { + framebuffer.setGPUObject(object); + } + template< typename T > + static T* getGPUObject(const Framebuffer& framebuffer) { + return reinterpret_cast(framebuffer.getGPUObject()); + } + protected: }; diff --git a/libraries/gpu/src/gpu/Format.h b/libraries/gpu/src/gpu/Format.h index 7f36797374..e43c190819 100644 --- a/libraries/gpu/src/gpu/Format.h +++ b/libraries/gpu/src/gpu/Format.h @@ -34,6 +34,8 @@ typedef glm::mat3 Mat3; typedef glm::vec4 Vec4; typedef glm::vec3 Vec3; typedef glm::vec2 Vec2; +typedef glm::ivec2 Vec2i; +typedef glm::uvec2 Vec2u; // Description of a scalar type enum Type { @@ -118,7 +120,8 @@ enum Semantic { INDEX, //used by index buffer of a mesh PART, // used by part buffer of a mesh - DEPTH, // Depth buffer + DEPTH, // Depth only buffer + STENCIL, // Stencil only buffer DEPTH_STENCIL, // Depth Stencil buffer SRGB, @@ -171,12 +174,28 @@ public: return getRaw() != right.getRaw(); } + static const Element COLOR_RGBA_32; + protected: uint8 _semantic; uint8 _dimension : 4; uint8 _type : 4; }; + +enum ComparisonFunction { + NEVER = 0, + LESS, + EQUAL, + LESS_EQUAL, + GREATER, + NOT_EQUAL, + GREATER_EQUAL, + ALWAYS, + + NUM_COMPARISON_FUNCS, +}; + }; diff --git a/libraries/gpu/src/gpu/Framebuffer.cpp b/libraries/gpu/src/gpu/Framebuffer.cpp new file mode 100755 index 0000000000..96bd3d3002 --- /dev/null +++ b/libraries/gpu/src/gpu/Framebuffer.cpp @@ -0,0 +1,279 @@ +// +// Framebuffer.cpp +// libraries/gpu/src/gpu +// +// Created by Sam Gateau on 4/12/2015. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "Framebuffer.h" +#include +#include + +using namespace gpu; + +Framebuffer::~Framebuffer() { +} + +Framebuffer* Framebuffer::create() { + auto framebuffer = new Framebuffer(); + framebuffer->_renderBuffers.resize(MAX_NUM_RENDER_BUFFERS); + return framebuffer; +} + + +Framebuffer* Framebuffer::create( const Format& colorBufferFormat, uint16 width, uint16 height) { + auto framebuffer = Framebuffer::create(); + + auto colorTexture = TexturePointer(Texture::create2D(colorBufferFormat, width, height, Sampler(Sampler::FILTER_MIN_MAG_POINT))); + + framebuffer->setRenderBuffer(0, colorTexture); + + return framebuffer; +} + +Framebuffer* Framebuffer::create( const Format& colorBufferFormat, const Format& depthStencilBufferFormat, uint16 width, uint16 height) { + auto framebuffer = Framebuffer::create(); + + auto colorTexture = TexturePointer(Texture::create2D(colorBufferFormat, width, height, Sampler(Sampler::FILTER_MIN_MAG_POINT))); + auto depthTexture = TexturePointer(Texture::create2D(depthStencilBufferFormat, width, height, Sampler(Sampler::FILTER_MIN_MAG_POINT))); + + framebuffer->setRenderBuffer(0, colorTexture); + framebuffer->setDepthStencilBuffer(depthTexture, depthStencilBufferFormat); + + return framebuffer; +} + +Framebuffer* Framebuffer::createShadowmap(uint16 width) { + auto framebuffer = Framebuffer::create(); + auto depthTexture = TexturePointer(Texture::create2D(Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH), width, width)); + + Sampler::Desc samplerDesc; + samplerDesc._borderColor = glm::vec4(1.0f); + samplerDesc._wrapModeU = Sampler::WRAP_BORDER; + samplerDesc._wrapModeV = Sampler::WRAP_BORDER; + samplerDesc._filter = Sampler::FILTER_MIN_MAG_LINEAR; + samplerDesc._comparisonFunc = LESS_EQUAL; + + depthTexture->setSampler(Sampler(samplerDesc)); + + framebuffer->setDepthStencilBuffer(depthTexture, Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH)); + + return framebuffer; +} + +bool Framebuffer::isSwapchain() const { + return _swapchain != 0; +} + +uint32 Framebuffer::getFrameCount() const { + if (_swapchain) { + return _swapchain->getFrameCount(); + } else { + return _frameCount; + } +} + +bool Framebuffer::validateTargetCompatibility(const Texture& texture, uint32 subresource) const { + if (texture.getType() == Texture::TEX_1D) { + return false; + } + + if (isEmpty()) { + return true; + } else { + if ((texture.getWidth() == getWidth()) && + (texture.getHeight() == getHeight()) && + (texture.getNumSamples() == getNumSamples())) { + return true; + } else { + return false; + } + } +} + +void Framebuffer::updateSize(const TexturePointer& texture) { + if (!isEmpty()) { + return; + } + + if (texture) { + _width = texture->getWidth(); + _height = texture->getHeight(); + _numSamples = texture->getNumSamples(); + } else { + _width = _height = _numSamples = 0; + } +} + +void Framebuffer::resize(uint16 width, uint16 height, uint16 numSamples) { + if (width && height && numSamples && !isEmpty() && !isSwapchain()) { + if ((width != _width) || (height != _height) || (numSamples != _numSamples)) { + for (uint32 i = 0; i < _renderBuffers.size(); ++i) { + if (_renderBuffers[i]) { + _renderBuffers[i]._texture->resize2D(width, height, numSamples); + _numSamples = _renderBuffers[i]._texture->getNumSamples(); + } + } + + if (_depthStencilBuffer) { + _depthStencilBuffer._texture->resize2D(width, height, numSamples); + _numSamples = _depthStencilBuffer._texture->getNumSamples(); + } + + _width = width; + _height = height; + // _numSamples = numSamples; + } + } +} + +uint16 Framebuffer::getWidth() const { + if (isSwapchain()) { + return getSwapchain()->getWidth(); + } else { + return _width; + } +} + +uint16 Framebuffer::getHeight() const { + if (isSwapchain()) { + return getSwapchain()->getHeight(); + } else { + return _height; + } +} + +uint16 Framebuffer::getNumSamples() const { + if (isSwapchain()) { + return getSwapchain()->getNumSamples(); + } else { + return _numSamples; + } +} + +// Render buffers +int Framebuffer::setRenderBuffer(uint32 slot, const TexturePointer& texture, uint32 subresource) { + if (isSwapchain()) { + return -1; + } + + // Check for the slot + if (slot >= getMaxNumRenderBuffers()) { + return -1; + } + + // Check for the compatibility of size + if (texture) { + if (!validateTargetCompatibility(*texture, subresource)) { + return -1; + } + } + + updateSize(texture); + + // assign the new one + _renderBuffers[slot] = TextureView(texture, subresource); + + // update the mask + int mask = (1<getDepthStencilBufferFormat(); + return _depthStencilBuffer._element; + } else { + return _depthStencilBuffer._element; + } +} \ No newline at end of file diff --git a/libraries/gpu/src/gpu/Framebuffer.h b/libraries/gpu/src/gpu/Framebuffer.h new file mode 100755 index 0000000000..69e507f824 --- /dev/null +++ b/libraries/gpu/src/gpu/Framebuffer.h @@ -0,0 +1,164 @@ +// +// Framebuffer.h +// libraries/gpu/src/gpu +// +// Created by Sam Gateau on 4/12/2015. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#ifndef hifi_gpu_Framebuffer_h +#define hifi_gpu_Framebuffer_h + +#include "Texture.h" +#include + +namespace gpu { + +typedef Element Format; + +class Swapchain { +public: + // Properties + uint16 getWidth() const { return _width; } + uint16 getHeight() const { return _height; } + uint16 getNumSamples() const { return _numSamples; } + + bool hasDepthStencil() const { return _hasDepthStencil; } + bool isFullscreen() const { return _isFullscreen; } + + uint32 getSwapInterval() const { return _swapInterval; } + + bool isStereo() const { return _isStereo; } + + uint32 getFrameCount() const { return _frameCount; } + + // Pure interface + void setSwapInterval(uint32 interval); + + void resize(uint16 width, uint16 height); + void setFullscreen(bool fullscreen); + + Swapchain() {} + Swapchain(const Swapchain& swapchain) {} + virtual ~Swapchain() {} + +protected: + mutable uint32 _frameCount = 0; + + Format _colorFormat; + uint16 _width = 1; + uint16 _height = 1; + uint16 _numSamples = 1; + uint16 _swapInterval = 0; + + bool _hasDepthStencil = false; + bool _isFullscreen = false; + bool _isStereo = false; + + // Non exposed + + friend class Framebuffer; +}; +typedef std::shared_ptr SwapchainPointer; + + +class Framebuffer { +public: + enum BufferMask { + BUFFER_COLOR0 = 1, + BUFFER_COLOR1 = 2, + BUFFER_COLOR2 = 4, + BUFFER_COLOR3 = 8, + BUFFER_COLOR4 = 16, + BUFFER_COLOR5 = 32, + BUFFER_COLOR6 = 64, + BUFFER_COLOR7 = 128, + BUFFER_COLORS = 0x000000FF, + + BUFFER_DEPTH = 0x40000000, + BUFFER_STENCIL = 0x80000000, + BUFFER_DEPTHSTENCIL = 0xC0000000, + }; + + ~Framebuffer(); + + static Framebuffer* create(const SwapchainPointer& swapchain); + static Framebuffer* create(); + static Framebuffer* create(const Format& colorBufferFormat, uint16 width, uint16 height); + static Framebuffer* create(const Format& colorBufferFormat, const Format& depthStencilBufferFormat, uint16 width, uint16 height); + static Framebuffer* createShadowmap(uint16 width); + + bool isSwapchain() const; + SwapchainPointer getSwapchain() const { return _swapchain; } + + uint32 getFrameCount() const; + + // Render buffers + void removeRenderBuffers(); + uint32 getNumRenderBuffers() const; + const TextureViews& getRenderBuffers() const { return _renderBuffers; } + + int32 setRenderBuffer(uint32 slot, const TexturePointer& texture, uint32 subresource = 0); + TexturePointer getRenderBuffer(uint32 slot) const; + uint32 getRenderBufferSubresource(uint32 slot) const; + + bool setDepthStencilBuffer(const TexturePointer& texture, const Format& format, uint32 subresource = 0); + TexturePointer getDepthStencilBuffer() const; + uint32 getDepthStencilBufferSubresource() const; + Format getDepthStencilBufferFormat() const; + + + // Properties + uint32 getBufferMask() const { return _bufferMask; } + bool isEmpty() const { return (_bufferMask == 0); } + bool hasColor() const { return (getBufferMask() & BUFFER_COLORS); } + bool hasDepthStencil() const { return (getBufferMask() & BUFFER_DEPTHSTENCIL); } + + bool validateTargetCompatibility(const Texture& texture, uint32 subresource = 0) const; + + Vec2u getSize() const { return Vec2u(getWidth(), getHeight()); } + uint16 getWidth() const; + uint16 getHeight() const; + uint16 getNumSamples() const; + + float getAspectRatio() const { return getWidth() / (float) getHeight() ; } + + // If not a swapchain canvas, resize can resize all the render buffers and depth stencil attached in one call + void resize( uint16 width, uint16 height, uint16 samples = 1 ); + + static const uint32 MAX_NUM_RENDER_BUFFERS = 8; + static uint32 getMaxNumRenderBuffers() { return MAX_NUM_RENDER_BUFFERS; } + +protected: + SwapchainPointer _swapchain; + + TextureViews _renderBuffers; + TextureView _depthStencilBuffer; + + uint32 _bufferMask = 0; + + uint32 _frameCount = 0; + + uint16 _width = 0; + uint16 _height = 0; + uint16 _numSamples = 0; + + void updateSize(const TexturePointer& texture); + + // Non exposed + Framebuffer(const Framebuffer& framebuffer) {} + Framebuffer() {} + + // This shouldn't be used by anything else than the Backend class with the proper casting. + mutable GPUObject* _gpuObject = NULL; + void setGPUObject(GPUObject* gpuObject) const { _gpuObject = gpuObject; } + GPUObject* getGPUObject() const { return _gpuObject; } + friend class Backend; +}; +typedef std::shared_ptr FramebufferPointer; + +} + +#endif \ No newline at end of file diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index 8fcc2362c1..c9de132b73 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -32,6 +32,9 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] = (&::gpu::GLBackend::do_setUniformBuffer), (&::gpu::GLBackend::do_setUniformTexture), + (&::gpu::GLBackend::do_setFramebuffer), + + (&::gpu::GLBackend::do_glEnable), (&::gpu::GLBackend::do_glDisable), @@ -67,7 +70,8 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] = GLBackend::GLBackend() : _input(), _transform(), - _pipeline() + _pipeline(), + _output() { initTransform(); } diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index ea97fd8908..b725fa46d0 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -53,6 +53,7 @@ public: Stamp _storageStamp; Stamp _contentStamp; GLuint _texture; + GLenum _target; GLuint _size; GLTexture(); @@ -61,6 +62,9 @@ public: static GLTexture* syncGPUObject(const Texture& texture); static GLuint getTextureID(const TexturePointer& texture); + // very specific for now + static void syncSampler(const Sampler& sampler, Texture::Type type, GLTexture* object); + class GLShader : public GPUObject { public: GLuint _shader; @@ -133,14 +137,25 @@ public: class GLPipeline : public GPUObject { public: - GLShader* _program; - GLState* _state; + GLShader* _program = 0; + GLState* _state = 0; GLPipeline(); ~GLPipeline(); }; static GLPipeline* syncGPUObject(const Pipeline& pipeline); + + class GLFramebuffer : public GPUObject { + public: + GLuint _fbo = 0; + + GLFramebuffer(); + ~GLFramebuffer(); + }; + static GLFramebuffer* syncGPUObject(const Framebuffer& framebuffer); + static GLuint getFramebufferID(const FramebufferPointer& framebuffer); + static const int MAX_NUM_ATTRIBUTES = Stream::NUM_INPUT_SLOTS; static const int MAX_NUM_INPUT_BUFFERS = 16; @@ -276,8 +291,8 @@ protected: State::Signature _stateSignatureCache; GLState* _state; - bool _invalidState; - bool _needStateSync; + bool _invalidState = false; + bool _needStateSync = true; PipelineStageState() : _pipeline(), @@ -291,6 +306,16 @@ protected: {} } _pipeline; + // Output stage + void do_setFramebuffer(Batch& batch, uint32 paramOffset); + + struct OutputStageState { + + FramebufferPointer _framebuffer = nullptr; + + OutputStageState() {} + } _output; + // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long // term strategy is to get rid of any GL calls in favor of the HIFI GPU API diff --git a/libraries/gpu/src/gpu/GLBackendOutput.cpp b/libraries/gpu/src/gpu/GLBackendOutput.cpp new file mode 100755 index 0000000000..bd8a2ab457 --- /dev/null +++ b/libraries/gpu/src/gpu/GLBackendOutput.cpp @@ -0,0 +1,157 @@ +// +// GLBackendTexture.cpp +// libraries/gpu/src/gpu +// +// Created by Sam Gateau on 1/19/2015. +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#include "GPULogging.h" +#include "GLBackendShared.h" + + +GLBackend::GLFramebuffer::GLFramebuffer() {} + +GLBackend::GLFramebuffer::~GLFramebuffer() { + if (_fbo != 0) { + glDeleteFramebuffers(1, &_fbo); + } +} + +GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffer) { + GLFramebuffer* object = Backend::getGPUObject(framebuffer); + + // If GPU object already created and in sync + bool needUpdate = false; + if (object) { + return object; + } else if (framebuffer.isEmpty()) { + // NO framebuffer definition yet so let's avoid thinking + return nullptr; + } + + // need to have a gpu object? + if (!object) { + GLuint fbo; + glGenFramebuffers(1, &fbo); + CHECK_GL_ERROR(); + + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + unsigned int nbColorBuffers = 0; + GLenum colorBuffers[16]; + if (framebuffer.hasColor()) { + static const GLenum colorAttachments[] = { + GL_COLOR_ATTACHMENT0, + GL_COLOR_ATTACHMENT1, + GL_COLOR_ATTACHMENT2, + GL_COLOR_ATTACHMENT3, + GL_COLOR_ATTACHMENT4, + GL_COLOR_ATTACHMENT5, + GL_COLOR_ATTACHMENT6, + GL_COLOR_ATTACHMENT7, + GL_COLOR_ATTACHMENT8, + GL_COLOR_ATTACHMENT9, + GL_COLOR_ATTACHMENT10, + GL_COLOR_ATTACHMENT11, + GL_COLOR_ATTACHMENT12, + GL_COLOR_ATTACHMENT13, + GL_COLOR_ATTACHMENT14, + GL_COLOR_ATTACHMENT15 }; + + int unit = 0; + for (auto& b : framebuffer.getRenderBuffers()) { + auto surface = b._texture; + if (surface) { + auto gltexture = GLBackend::syncGPUObject(*surface); + if (gltexture) { + glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D, gltexture->_texture, 0); + } + colorBuffers[nbColorBuffers] = colorAttachments[unit]; + nbColorBuffers++; + unit++; + } + } + } + + if (framebuffer.hasDepthStencil()) { + auto surface = framebuffer.getDepthStencilBuffer(); + if (surface) { + auto gltexture = GLBackend::syncGPUObject(*surface); + if (gltexture) { + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, gltexture->_texture, 0); + } + } + } + + // Last but not least, define where we draw + if (nbColorBuffers > 0) { + glDrawBuffers(nbColorBuffers, colorBuffers); + } else { + glDrawBuffer( GL_NONE ); + } + + // Now check for completness + GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); + bool result = false; + switch (status) { + case GL_FRAMEBUFFER_COMPLETE : + // Success ! + result = true; + break; + case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT : + qCDebug(gpulogging) << "GLFramebuffer::syncGPUObject : Framebuffer not valid, GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT."; + break; + case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : + qCDebug(gpulogging) << "GLFramebuffer::syncGPUObject : Framebuffer not valid, GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT."; + break; + case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER : + qCDebug(gpulogging) << "GLFramebuffer::syncGPUObject : Framebuffer not valid, GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER."; + break; + case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER : + qCDebug(gpulogging) << "GLFramebuffer::syncGPUObject : Framebuffer not valid, GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER."; + break; + case GL_FRAMEBUFFER_UNSUPPORTED : + qCDebug(gpulogging) << "GLFramebuffer::syncGPUObject : Framebuffer not valid, GL_FRAMEBUFFER_UNSUPPORTED."; + break; + } + if (!result && fbo) { + glDeleteFramebuffers( 1, &fbo ); + return nullptr; + } + + + // All is green, assign the gpuobject to the Framebuffer + object = new GLFramebuffer(); + object->_fbo = fbo; + Backend::setGPUObject(framebuffer, object); + } + + return object; +} + + + +GLuint GLBackend::getFramebufferID(const FramebufferPointer& framebuffer) { + if (!framebuffer) { + return 0; + } + GLFramebuffer* object = GLBackend::syncGPUObject(*framebuffer); + if (object) { + return object->_fbo; + } else { + return 0; + } +} + +void GLBackend::do_setFramebuffer(Batch& batch, uint32 paramOffset) { + auto framebuffer = batch._framebuffers.get(batch._params[paramOffset]._uint); + + if (_output._framebuffer != framebuffer) { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, getFramebufferID(framebuffer)); + _output._framebuffer = framebuffer; + } +} + diff --git a/libraries/gpu/src/gpu/GLBackendState.cpp b/libraries/gpu/src/gpu/GLBackendState.cpp index 7707922b81..fb85d30fe2 100644 --- a/libraries/gpu/src/gpu/GLBackendState.cpp +++ b/libraries/gpu/src/gpu/GLBackendState.cpp @@ -237,26 +237,26 @@ void GLBackend::resetPipelineState(State::Signature nextSignature) { } } -State::ComparisonFunction comparisonFuncFromGL(GLenum func) { +ComparisonFunction comparisonFuncFromGL(GLenum func) { if (func == GL_NEVER) { - return State::NEVER; + return NEVER; } else if (func == GL_LESS) { - return State::LESS; + return LESS; } else if (func == GL_EQUAL) { - return State::EQUAL; + return EQUAL; } else if (func == GL_LEQUAL) { - return State::LESS_EQUAL; + return LESS_EQUAL; } else if (func == GL_GREATER) { - return State::GREATER; + return GREATER; } else if (func == GL_NOTEQUAL) { - return State::NOT_EQUAL; + return NOT_EQUAL; } else if (func == GL_GEQUAL) { - return State::GREATER_EQUAL; + return GREATER_EQUAL; } else if (func == GL_ALWAYS) { - return State::ALWAYS; + return ALWAYS; } - return State::ALWAYS; + return ALWAYS; } State::StencilOp stencilOpFromGL(GLenum stencilOp) { diff --git a/libraries/gpu/src/gpu/GLBackendTexture.cpp b/libraries/gpu/src/gpu/GLBackendTexture.cpp index 2bbb3d633f..b8f9ab0bf1 100755 --- a/libraries/gpu/src/gpu/GLBackendTexture.cpp +++ b/libraries/gpu/src/gpu/GLBackendTexture.cpp @@ -16,7 +16,8 @@ GLBackend::GLTexture::GLTexture() : _storageStamp(0), _contentStamp(0), _texture(0), - _size(0) + _size(0), + _target(GL_TEXTURE_2D) {} GLBackend::GLTexture::~GLTexture() { @@ -144,7 +145,38 @@ public: texel.internalFormat = GL_RED; break; case gpu::DEPTH: + texel.format = GL_DEPTH_COMPONENT; // It's depth component to load it texel.internalFormat = GL_DEPTH_COMPONENT; + switch (dstFormat.getType()) { + case gpu::UINT32: + case gpu::INT32: + case gpu::NUINT32: + case gpu::NINT32: { + texel.internalFormat = GL_DEPTH_COMPONENT32; + break; + } + case gpu::NFLOAT: + case gpu::FLOAT: { + texel.internalFormat = GL_DEPTH_COMPONENT32F; + break; + } + case gpu::UINT16: + case gpu::INT16: + case gpu::NUINT16: + case gpu::NINT16: + case gpu::HALF: + case gpu::NHALF: { + texel.internalFormat = GL_DEPTH_COMPONENT16; + break; + } + case gpu::UINT8: + case gpu::INT8: + case gpu::NUINT8: + case gpu::NINT8: { + texel.internalFormat = GL_DEPTH_COMPONENT24; + break; + } + } break; default: qCDebug(gpulogging) << "Unknown combination of texel format"; @@ -254,66 +286,77 @@ GLBackend::GLTexture* GLBackend::syncGPUObject(const Texture& texture) { // GO through the process of allocating the correct storage and/or update the content switch (texture.getType()) { case Texture::TEX_2D: { - if (needUpdate) { - if (texture.isStoredMipAvailable(0)) { - GLint boundTex = -1; - glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTex); - - Texture::PixelsPointer mip = texture.accessStoredMip(0); - const GLvoid* bytes = mip->_sysmem.read(); - Element srcFormat = mip->_format; - - GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); - - glBindTexture(GL_TEXTURE_2D, object->_texture); - glTexSubImage2D(GL_TEXTURE_2D, 0, - texelFormat.internalFormat, texture.getWidth(), texture.getHeight(), 0, - texelFormat.format, texelFormat.type, bytes); - - if (texture.isAutogenerateMips()) { - glGenerateMipmap(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - } - - // At this point the mip piels have been loaded, we can notify - texture.notifyGPULoaded(0); - - glBindTexture(GL_TEXTURE_2D, boundTex); - object->_contentStamp = texture.getDataStamp(); - } - } else { - const GLvoid* bytes = 0; - Element srcFormat = texture.getTexelFormat(); - if (texture.isStoredMipAvailable(0)) { - Texture::PixelsPointer mip = texture.accessStoredMip(0); - - bytes = mip->_sysmem.read(); - srcFormat = mip->_format; - - object->_contentStamp = texture.getDataStamp(); - } - + if (texture.getNumSlices() == 1) { GLint boundTex = -1; glGetIntegerv(GL_TEXTURE_BINDING_2D, &boundTex); glBindTexture(GL_TEXTURE_2D, object->_texture); - GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); - - glTexImage2D(GL_TEXTURE_2D, 0, - texelFormat.internalFormat, texture.getWidth(), texture.getHeight(), 0, - texelFormat.format, texelFormat.type, bytes); + if (needUpdate) { + if (texture.isStoredMipAvailable(0)) { + Texture::PixelsPointer mip = texture.accessStoredMip(0); + const GLvoid* bytes = mip->_sysmem.read(); + Element srcFormat = mip->_format; + + GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); - if (bytes && texture.isAutogenerateMips()) { - glGenerateMipmap(GL_TEXTURE_2D); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glBindTexture(GL_TEXTURE_2D, object->_texture); + glTexSubImage2D(GL_TEXTURE_2D, 0, + texelFormat.internalFormat, texture.getWidth(), texture.getHeight(), 0, + texelFormat.format, texelFormat.type, bytes); + + if (texture.isAutogenerateMips()) { + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + } + + object->_target = GL_TEXTURE_2D; + + syncSampler(texture.getSampler(), texture.getType(), object); + + + // At this point the mip piels have been loaded, we can notify + texture.notifyGPULoaded(0); + + object->_contentStamp = texture.getDataStamp(); + } + } else { + const GLvoid* bytes = 0; + Element srcFormat = texture.getTexelFormat(); + if (texture.isStoredMipAvailable(0)) { + Texture::PixelsPointer mip = texture.accessStoredMip(0); + + bytes = mip->_sysmem.read(); + srcFormat = mip->_format; + + object->_contentStamp = texture.getDataStamp(); + } + + GLTexelFormat texelFormat = GLTexelFormat::evalGLTexelFormat(texture.getTexelFormat(), srcFormat); + + glTexImage2D(GL_TEXTURE_2D, 0, + texelFormat.internalFormat, texture.getWidth(), texture.getHeight(), 0, + texelFormat.format, texelFormat.type, bytes); + + if (bytes && texture.isAutogenerateMips()) { + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + } else { + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + } + + object->_target = GL_TEXTURE_2D; + + syncSampler(texture.getSampler(), texture.getType(), object); + + // At this point the mip piels have been loaded, we can notify + texture.notifyGPULoaded(0); + + object->_storageStamp = texture.getStamp(); + object->_size = texture.getSize(); } - // At this point the mip piels have been loaded, we can notify - texture.notifyGPULoaded(0); - glBindTexture(GL_TEXTURE_2D, boundTex); - object->_storageStamp = texture.getStamp(); - object->_size = texture.getSize(); } break; } @@ -339,3 +382,70 @@ GLuint GLBackend::getTextureID(const TexturePointer& texture) { } } +void GLBackend::syncSampler(const Sampler& sampler, Texture::Type type, GLTexture* object) { + if (!object) return; + if (!object->_texture) return; + + class GLFilterMode { + public: + GLint minFilter; + GLint magFilter; + }; + static const GLFilterMode filterModes[] = { + {GL_NEAREST, GL_NEAREST}, //FILTER_MIN_MAG_POINT, + {GL_NEAREST, GL_LINEAR}, //FILTER_MIN_POINT_MAG_LINEAR, + {GL_LINEAR, GL_NEAREST}, //FILTER_MIN_LINEAR_MAG_POINT, + {GL_LINEAR, GL_LINEAR}, //FILTER_MIN_MAG_LINEAR, + + {GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST}, //FILTER_MIN_MAG_MIP_POINT, + {GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST}, //FILTER_MIN_MAG_MIP_POINT, + {GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST}, //FILTER_MIN_MAG_POINT_MIP_LINEAR, + {GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR}, //FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT, + {GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR}, //FILTER_MIN_POINT_MAG_MIP_LINEAR, + {GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST}, //FILTER_MIN_LINEAR_MAG_MIP_POINT, + {GL_LINEAR_MIPMAP_LINEAR, GL_NEAREST}, //FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR, + {GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR}, //FILTER_MIN_MAG_LINEAR_MIP_POINT, + {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}, //FILTER_MIN_MAG_MIP_LINEAR, + {GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR} //FILTER_ANISOTROPIC, + }; + + auto fm = filterModes[sampler.getFilter()]; + glTexParameteri(object->_target, GL_TEXTURE_MIN_FILTER, fm.minFilter); + glTexParameteri(object->_target, GL_TEXTURE_MAG_FILTER, fm.magFilter); + + static const GLenum comparisonFuncs[] = { + GL_NEVER, + GL_LESS, + GL_EQUAL, + GL_LEQUAL, + GL_GREATER, + GL_NOTEQUAL, + GL_GEQUAL, + GL_ALWAYS }; + + if (sampler.doComparison()) { + glTexParameteri(object->_target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); + glTexParameteri(object->_target, GL_TEXTURE_COMPARE_FUNC, comparisonFuncs[sampler.getComparisonFunction()]); + } else { + glTexParameteri(object->_target, GL_TEXTURE_COMPARE_MODE, GL_NONE); + } + + static const GLenum wrapModes[] = { + GL_REPEAT, // WRAP_REPEAT, + GL_MIRRORED_REPEAT, // WRAP_MIRROR, + GL_CLAMP_TO_EDGE, // WRAP_CLAMP, + GL_CLAMP_TO_BORDER, // WRAP_BORDER, + GL_MIRROR_CLAMP_TO_EDGE_EXT }; // WRAP_MIRROR_ONCE, + + glTexParameteri(object->_target, GL_TEXTURE_WRAP_S, wrapModes[sampler.getWrapModeU()]); + glTexParameteri(object->_target, GL_TEXTURE_WRAP_T, wrapModes[sampler.getWrapModeV()]); + glTexParameteri(object->_target, GL_TEXTURE_WRAP_R, wrapModes[sampler.getWrapModeW()]); + + glTexParameterfv(object->_target, GL_TEXTURE_BORDER_COLOR, (const float*) &sampler.getBorderColor()); + glTexParameteri(object->_target, GL_TEXTURE_BASE_LEVEL, sampler.getMipOffset()); + glTexParameterf(object->_target, GL_TEXTURE_MIN_LOD, (float) sampler.getMinMip()); + glTexParameterf(object->_target, GL_TEXTURE_MAX_LOD, (sampler.getMaxMip() == Sampler::MAX_MIP_LEVEL ? 1000.f : sampler.getMaxMip())); + glTexParameterf(object->_target, GL_TEXTURE_MAX_ANISOTROPY_EXT, sampler.getMaxAnisotropy()); + CHECK_GL_ERROR(); + +} diff --git a/libraries/gpu/src/gpu/Resource.cpp b/libraries/gpu/src/gpu/Resource.cpp index 708ca0f627..7d2757e15c 100644 --- a/libraries/gpu/src/gpu/Resource.cpp +++ b/libraries/gpu/src/gpu/Resource.cpp @@ -16,6 +16,8 @@ using namespace gpu; +const Element Element::COLOR_RGBA_32 = Element(VEC4, UINT8, RGBA); + Resource::Size Resource::Sysmem::allocateMemory(Byte** dataAllocated, Size size) { if ( !dataAllocated ) { qWarning() << "Buffer::Sysmem::allocateMemory() : Must have a valid dataAllocated pointer."; diff --git a/libraries/gpu/src/gpu/State.h b/libraries/gpu/src/gpu/State.h index 84f44d8efd..9307882bab 100755 --- a/libraries/gpu/src/gpu/State.h +++ b/libraries/gpu/src/gpu/State.h @@ -42,19 +42,8 @@ public: virtual ~State(); const Stamp getStamp() const { return _stamp; } - - enum ComparisonFunction { - NEVER = 0, - LESS, - EQUAL, - LESS_EQUAL, - GREATER, - NOT_EQUAL, - GREATER_EQUAL, - ALWAYS, - - NUM_COMPARISON_FUNCS, - }; + + typedef ::gpu::ComparisonFunction ComparisonFunction; enum FillMode { FILL_POINT = 0, @@ -415,5 +404,4 @@ typedef std::vector< StatePointer > States; }; - #endif diff --git a/libraries/gpu/src/gpu/Texture.cpp b/libraries/gpu/src/gpu/Texture.cpp index 43d8c991b6..76cac74a59 100755 --- a/libraries/gpu/src/gpu/Texture.cpp +++ b/libraries/gpu/src/gpu/Texture.cpp @@ -93,23 +93,23 @@ bool Texture::Storage::assignMipData(uint16 level, const Element& format, Size s return allocated == size; } -Texture* Texture::create1D(const Element& texelFormat, uint16 width) { - return create(TEX_1D, texelFormat, width, 1, 1, 1, 1); +Texture* Texture::create1D(const Element& texelFormat, uint16 width, const Sampler& sampler) { + return create(TEX_1D, texelFormat, width, 1, 1, 1, 1, sampler); } -Texture* Texture::create2D(const Element& texelFormat, uint16 width, uint16 height) { - return create(TEX_2D, texelFormat, width, height, 1, 1, 1); +Texture* Texture::create2D(const Element& texelFormat, uint16 width, uint16 height, const Sampler& sampler) { + return create(TEX_2D, texelFormat, width, height, 1, 1, 1, sampler); } -Texture* Texture::create3D(const Element& texelFormat, uint16 width, uint16 height, uint16 depth) { - return create(TEX_3D, texelFormat, width, height, depth, 1, 1); +Texture* Texture::create3D(const Element& texelFormat, uint16 width, uint16 height, uint16 depth, const Sampler& sampler) { + return create(TEX_3D, texelFormat, width, height, depth, 1, 1, sampler); } -Texture* Texture::createCube(const Element& texelFormat, uint16 width) { - return create(TEX_CUBE, texelFormat, width, width, 1, 1, 1); +Texture* Texture::createCube(const Element& texelFormat, uint16 width, const Sampler& sampler) { + return create(TEX_CUBE, texelFormat, width, width, 1, 1, 1, sampler); } -Texture* Texture::create(Type type, const Element& texelFormat, uint16 width, uint16 height, uint16 depth, uint16 numSamples, uint16 numSlices) +Texture* Texture::create(Type type, const Element& texelFormat, uint16 width, uint16 height, uint16 depth, uint16 numSamples, uint16 numSlices, const Sampler& sampler) { Texture* tex = new Texture(); tex->_storage.reset(new Storage()); @@ -118,6 +118,8 @@ Texture* Texture::create(Type type, const Element& texelFormat, uint16 width, ui tex->_maxMip = 0; tex->resize(type, texelFormat, width, height, depth, numSamples, numSlices); + tex->_sampler = sampler; + return tex; } @@ -346,3 +348,8 @@ uint16 Texture::evalNumSamplesUsed(uint16 numSamplesTried) { return sample; } + +void Texture::setSampler(const Sampler& sampler) { + _sampler = sampler; + _samplerStamp++; +} diff --git a/libraries/gpu/src/gpu/Texture.h b/libraries/gpu/src/gpu/Texture.h index 3eed52c686..1d1e82123a 100755 --- a/libraries/gpu/src/gpu/Texture.h +++ b/libraries/gpu/src/gpu/Texture.h @@ -16,6 +16,85 @@ namespace gpu { +class Sampler { +public: + + enum Filter { + FILTER_MIN_MAG_POINT, // top mip only + FILTER_MIN_POINT_MAG_LINEAR, // top mip only + FILTER_MIN_LINEAR_MAG_POINT, // top mip only + FILTER_MIN_MAG_LINEAR, // top mip only + + FILTER_MIN_MAG_MIP_POINT, + FILTER_MIN_MAG_POINT_MIP_LINEAR, + FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT, + FILTER_MIN_POINT_MAG_MIP_LINEAR, + FILTER_MIN_LINEAR_MAG_MIP_POINT, + FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR, + FILTER_MIN_MAG_LINEAR_MIP_POINT, + FILTER_MIN_MAG_MIP_LINEAR, + FILTER_ANISOTROPIC, + + NUM_FILTERS, + }; + + enum WrapMode { + WRAP_REPEAT = 0, + WRAP_MIRROR, + WRAP_CLAMP, + WRAP_BORDER, + WRAP_MIRROR_ONCE, + + NUM_WRAP_MODES + }; + + static const uint8 MAX_MIP_LEVEL = 0xFF; + + class Desc { + public: + glm::vec4 _borderColor{ 1.0f }; + uint32 _maxAnisotropy = 16; + + uint8 _wrapModeU = WRAP_REPEAT; + uint8 _wrapModeV = WRAP_REPEAT; + uint8 _wrapModeW = WRAP_REPEAT; + + uint8 _filter = FILTER_MIN_MAG_POINT; + uint8 _comparisonFunc = ALWAYS; + + uint8 _mipOffset = 0; + uint8 _minMip = 0; + uint8 _maxMip = MAX_MIP_LEVEL; + + Desc() {} + Desc(const Filter filter) : _filter(filter) {} + }; + + Sampler() {} + Sampler(const Filter filter) : _desc(filter) {} + Sampler(const Desc& desc) : _desc(desc) {} + ~Sampler() {} + + const glm::vec4& getBorderColor() const { return _desc._borderColor; } + + uint32 getMaxAnisotropy() const { return _desc._maxAnisotropy; } + + WrapMode getWrapModeU() const { return WrapMode(_desc._wrapModeU); } + WrapMode getWrapModeV() const { return WrapMode(_desc._wrapModeV); } + WrapMode getWrapModeW() const { return WrapMode(_desc._wrapModeW); } + + Filter getFilter() const { return Filter(_desc._filter); } + ComparisonFunction getComparisonFunction() const { return ComparisonFunction(_desc._comparisonFunc); } + bool doComparison() const { return getComparisonFunction() != ALWAYS; } + + uint8 getMipOffset() const { return _desc._mipOffset; } + uint8 getMinMip() const { return _desc._minMip; } + uint8 getMaxMip() const { return _desc._maxMip; } + +protected: + Desc _desc; +}; + class Texture : public Resource { public: @@ -61,10 +140,10 @@ public: TEX_CUBE, }; - static Texture* create1D(const Element& texelFormat, uint16 width); - static Texture* create2D(const Element& texelFormat, uint16 width, uint16 height); - static Texture* create3D(const Element& texelFormat, uint16 width, uint16 height, uint16 depth); - static Texture* createCube(const Element& texelFormat, uint16 width); + static Texture* create1D(const Element& texelFormat, uint16 width, const Sampler& sampler = Sampler()); + static Texture* create2D(const Element& texelFormat, uint16 width, uint16 height, const Sampler& sampler = Sampler()); + static Texture* create3D(const Element& texelFormat, uint16 width, uint16 height, uint16 depth, const Sampler& sampler = Sampler()); + static Texture* createCube(const Element& texelFormat, uint16 width, const Sampler& sampler = Sampler()); static Texture* createFromStorage(Storage* storage); @@ -181,11 +260,21 @@ public: bool isDefined() const { return _defined; } + + // Own sampler + void setSampler(const Sampler& sampler); + const Sampler& getSampler() const { return _sampler; } + const Stamp getSamplerStamp() const { return _samplerStamp; } + protected: std::unique_ptr< Storage > _storage; Stamp _stamp; + Sampler _sampler; + Stamp _samplerStamp; + + uint32 _size; Element _texelFormat; @@ -202,7 +291,7 @@ protected: bool _autoGenerateMips; bool _defined; - static Texture* create(Type type, const Element& texelFormat, uint16 width, uint16 height, uint16 depth, uint16 numSamples, uint16 numSlices); + static Texture* create(Type type, const Element& texelFormat, uint16 width, uint16 height, uint16 depth, uint16 numSamples, uint16 numSlices, const Sampler& sampler); Texture(); Size resize(Type type, const Element& texelFormat, uint16 width, uint16 height, uint16 depth, uint16 numSamples, uint16 numSlices); @@ -240,15 +329,25 @@ public: _subresource(0), _element(element) {}; - TextureView(const TexturePointer& texture, const Element& element) : + TextureView(const TexturePointer& texture, uint16 subresource, const Element& element) : _texture(texture), - _subresource(0), + _subresource(subresource), _element(element) {}; + + TextureView(const TexturePointer& texture, uint16 subresource) : + _texture(texture), + _subresource(subresource) + {}; + ~TextureView() {} TextureView(const TextureView& view) = default; TextureView& operator=(const TextureView& view) = default; + + explicit operator bool() const { return (_texture); } + bool operator !() const { return (!_texture); } }; +typedef std::vector TextureViews; }; diff --git a/libraries/render-utils/src/AmbientOcclusionEffect.cpp b/libraries/render-utils/src/AmbientOcclusionEffect.cpp index 40a02a60a4..f58419ec6e 100644 --- a/libraries/render-utils/src/AmbientOcclusionEffect.cpp +++ b/libraries/render-utils/src/AmbientOcclusionEffect.cpp @@ -12,7 +12,7 @@ // include this before QOpenGLFramebufferObject, which includes an earlier version of OpenGL #include -#include +#include #include @@ -107,8 +107,8 @@ void AmbientOcclusionEffect::render() { glBindTexture(GL_TEXTURE_2D, _rotationTextureID); // render with the occlusion shader to the secondary/tertiary buffer - QOpenGLFramebufferObject* freeFBO = DependencyManager::get()->getFreeFramebufferObject(); - freeFBO->bind(); + auto freeFramebuffer = DependencyManager::get()->getFreeFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFramebuffer)); float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; @@ -118,9 +118,10 @@ void AmbientOcclusionEffect::render() { glGetIntegerv(GL_VIEWPORT, viewport); const int VIEWPORT_X_INDEX = 0; const int VIEWPORT_WIDTH_INDEX = 2; - QOpenGLFramebufferObject* primaryFBO = DependencyManager::get()->getPrimaryFramebufferObject(); - float sMin = viewport[VIEWPORT_X_INDEX] / (float)primaryFBO->width(); - float sWidth = viewport[VIEWPORT_WIDTH_INDEX] / (float)primaryFBO->width(); + + auto framebufferSize = DependencyManager::get()->getFrameBufferSize(); + float sMin = viewport[VIEWPORT_X_INDEX] / (float)framebufferSize.width(); + float sWidth = viewport[VIEWPORT_WIDTH_INDEX] / (float)framebufferSize.width(); _occlusionProgram->bind(); _occlusionProgram->setUniformValue(_nearLocation, nearVal); @@ -128,7 +129,7 @@ void AmbientOcclusionEffect::render() { _occlusionProgram->setUniformValue(_leftBottomLocation, left, bottom); _occlusionProgram->setUniformValue(_rightTopLocation, right, top); _occlusionProgram->setUniformValue(_noiseScaleLocation, viewport[VIEWPORT_WIDTH_INDEX] / (float)ROTATION_WIDTH, - primaryFBO->height() / (float)ROTATION_HEIGHT); + framebufferSize.height() / (float)ROTATION_HEIGHT); _occlusionProgram->setUniformValue(_texCoordOffsetLocation, sMin, 0.0f); _occlusionProgram->setUniformValue(_texCoordScaleLocation, sWidth, 1.0f); @@ -136,22 +137,24 @@ void AmbientOcclusionEffect::render() { _occlusionProgram->release(); - freeFBO->release(); - + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); // now render secondary to primary with 4x4 blur - DependencyManager::get()->getPrimaryFramebufferObject()->bind(); - + auto primaryFramebuffer = DependencyManager::get()->getPrimaryFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(primaryFramebuffer)); + glEnable(GL_BLEND); glBlendFuncSeparate(GL_ZERO, GL_SRC_COLOR, GL_ZERO, GL_ONE); - glBindTexture(GL_TEXTURE_2D, freeFBO->texture()); + auto freeFramebufferTexture = freeFramebuffer->getRenderBuffer(0); + glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(freeFramebufferTexture)); _blurProgram->bind(); - _blurProgram->setUniformValue(_blurScaleLocation, 1.0f / primaryFBO->width(), 1.0f / primaryFBO->height()); + _blurProgram->setUniformValue(_blurScaleLocation, 1.0f / framebufferSize.width(), 1.0f / framebufferSize.height()); renderFullscreenQuad(sMin, sMin + sWidth); diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index a4f45802a4..440f75e32e 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -12,7 +12,6 @@ // include this before QOpenGLFramebufferObject, which includes an earlier version of OpenGL #include -#include #include #include @@ -183,15 +182,18 @@ void DeferredLightingEffect::render() { auto textureCache = DependencyManager::get(); - QOpenGLFramebufferObject* primaryFBO = textureCache->getPrimaryFramebufferObject(); - primaryFBO->release(); + glBindFramebuffer(GL_FRAMEBUFFER, 0 ); + + QSize framebufferSize = textureCache->getFrameBufferSize(); - QOpenGLFramebufferObject* freeFBO = DependencyManager::get()->getFreeFramebufferObject(); - freeFBO->bind(); + auto freeFBO = DependencyManager::get()->getFreeFramebuffer(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO)); + glClear(GL_COLOR_BUFFER_BIT); // glEnable(GL_FRAMEBUFFER_SRGB); - glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); + // glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); + glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID()); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID()); @@ -209,11 +211,13 @@ void DeferredLightingEffect::render() { const int VIEWPORT_Y_INDEX = 1; const int VIEWPORT_WIDTH_INDEX = 2; const int VIEWPORT_HEIGHT_INDEX = 3; - float sMin = viewport[VIEWPORT_X_INDEX] / (float)primaryFBO->width(); - float sWidth = viewport[VIEWPORT_WIDTH_INDEX] / (float)primaryFBO->width(); - float tMin = viewport[VIEWPORT_Y_INDEX] / (float)primaryFBO->height(); - float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)primaryFBO->height(); - + + float sMin = viewport[VIEWPORT_X_INDEX] / (float)framebufferSize.width(); + float sWidth = viewport[VIEWPORT_WIDTH_INDEX] / (float)framebufferSize.width(); + float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height(); + float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height(); + + // Fetch the ViewMatrix; glm::mat4 invViewMat; _viewState->getViewTransform().getMatrix(invViewMat); @@ -245,7 +249,7 @@ void DeferredLightingEffect::render() { program->bind(); } program->setUniformValue(locations->shadowScale, - 1.0f / textureCache->getShadowFramebufferObject()->width()); + 1.0f / textureCache->getShadowFramebuffer()->getWidth()); } else { if (_ambientLightMode > -1) { @@ -428,7 +432,9 @@ void DeferredLightingEffect::render() { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); - freeFBO->release(); + //freeFBO->release(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + // glDisable(GL_FRAMEBUFFER_SRGB); glDisable(GL_CULL_FACE); @@ -437,9 +443,12 @@ void DeferredLightingEffect::render() { glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); glColorMask(true, true, true, false); - primaryFBO->bind(); + auto primaryFBO = gpu::GLBackend::getFramebufferID(textureCache->getPrimaryFramebuffer()); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, primaryFBO); + + //primaryFBO->bind(); - glBindTexture(GL_TEXTURE_2D, freeFBO->texture()); + glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(freeFBO->getRenderBuffer(0))); glEnable(GL_TEXTURE_2D); glPushMatrix(); diff --git a/libraries/render-utils/src/GlowEffect.cpp b/libraries/render-utils/src/GlowEffect.cpp index 2ba1d7df13..6addd84da6 100644 --- a/libraries/render-utils/src/GlowEffect.cpp +++ b/libraries/render-utils/src/GlowEffect.cpp @@ -24,6 +24,7 @@ #include "TextureCache.h" #include "RenderUtilsLogging.h" +#include "gpu/GLBackend.h" GlowEffect::GlowEffect() : _initialized(false), @@ -45,10 +46,10 @@ GlowEffect::~GlowEffect() { } } -QOpenGLFramebufferObject* GlowEffect::getFreeFramebufferObject() const { +gpu::FramebufferPointer GlowEffect::getFreeFramebuffer() const { return (_isOddFrame ? - DependencyManager::get()->getSecondaryFramebufferObject(): - DependencyManager::get()->getTertiaryFramebufferObject()); + DependencyManager::get()->getSecondaryFramebuffer(): + DependencyManager::get()->getTertiaryFramebuffer()); } static ProgramObject* createProgram(const QString& name) { @@ -105,7 +106,10 @@ int GlowEffect::getDeviceHeight() const { void GlowEffect::prepare() { - DependencyManager::get()->getPrimaryFramebufferObject()->bind(); + auto primaryFBO = DependencyManager::get()->getPrimaryFramebuffer(); + GLuint fbo = gpu::GLBackend::getFramebufferID(primaryFBO); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); _isEmpty = true; @@ -124,25 +128,28 @@ void GlowEffect::end() { glBlendColor(0.0f, 0.0f, 0.0f, _intensity = _intensityStack.pop()); } -static void maybeBind(QOpenGLFramebufferObject* fbo) { +static void maybeBind(const gpu::FramebufferPointer& fbo) { if (fbo) { - fbo->bind(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(fbo)); } } -static void maybeRelease(QOpenGLFramebufferObject* fbo) { +static void maybeRelease(const gpu::FramebufferPointer& fbo) { if (fbo) { - fbo->release(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); } } -QOpenGLFramebufferObject* GlowEffect::render(bool toTexture) { +gpu::FramebufferPointer GlowEffect::render(bool toTexture) { PerformanceTimer perfTimer("glowEffect"); auto textureCache = DependencyManager::get(); - QOpenGLFramebufferObject* primaryFBO = textureCache->getPrimaryFramebufferObject(); - primaryFBO->release(); - glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); + + auto primaryFBO = gpu::GLBackend::getFramebufferID(textureCache->getPrimaryFramebuffer()); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + + glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID()); + auto framebufferSize = textureCache->getFrameBufferSize(); glPushMatrix(); glLoadIdentity(); @@ -155,12 +162,14 @@ QOpenGLFramebufferObject* GlowEffect::render(bool toTexture) { glDisable(GL_DEPTH_TEST); glDepthMask(GL_FALSE); - QOpenGLFramebufferObject* destFBO = toTexture ? - textureCache->getSecondaryFramebufferObject() : NULL; + gpu::FramebufferPointer destFBO = toTexture ? + textureCache->getSecondaryFramebuffer() : nullptr; if (!_enabled || _isEmpty) { // copy the primary to the screen if (destFBO && QOpenGLFramebufferObject::hasOpenGLFramebufferBlit()) { - QOpenGLFramebufferObject::blitFramebuffer(destFBO, primaryFBO); + glBindFramebuffer(GL_READ_FRAMEBUFFER, primaryFBO); + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(destFBO)); + glBlitFramebuffer(0, 0, framebufferSize.width(), framebufferSize.height(), 0, 0, framebufferSize.width(), framebufferSize.height(), GL_COLOR_BUFFER_BIT, GL_NEAREST); } else { maybeBind(destFBO); if (!destFBO) { @@ -175,35 +184,35 @@ QOpenGLFramebufferObject* GlowEffect::render(bool toTexture) { } } else { // diffuse into the secondary/tertiary (alternating between frames) - QOpenGLFramebufferObject* oldDiffusedFBO = - textureCache->getSecondaryFramebufferObject(); - QOpenGLFramebufferObject* newDiffusedFBO = - textureCache->getTertiaryFramebufferObject(); + auto oldDiffusedFBO = + textureCache->getSecondaryFramebuffer(); + auto newDiffusedFBO = + textureCache->getTertiaryFramebuffer(); if (_isOddFrame) { qSwap(oldDiffusedFBO, newDiffusedFBO); } - newDiffusedFBO->bind(); + glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(newDiffusedFBO)); if (_isFirstFrame) { glClear(GL_COLOR_BUFFER_BIT); } else { glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, oldDiffusedFBO->texture()); + glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(oldDiffusedFBO->getRenderBuffer(0))); _diffuseProgram->bind(); - QSize size = primaryFBO->size(); - _diffuseProgram->setUniformValue(_diffusionScaleLocation, 1.0f / size.width(), 1.0f / size.height()); + + _diffuseProgram->setUniformValue(_diffusionScaleLocation, 1.0f / framebufferSize.width(), 1.0f / framebufferSize.height()); renderFullscreenQuad(); _diffuseProgram->release(); } - newDiffusedFBO->release(); - + glBindFramebuffer(GL_FRAMEBUFFER, 0); + // add diffused texture to the primary - glBindTexture(GL_TEXTURE_2D, newDiffusedFBO->texture()); + glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(newDiffusedFBO->getRenderBuffer(0))); if (toTexture) { destFBO = oldDiffusedFBO; diff --git a/libraries/render-utils/src/GlowEffect.h b/libraries/render-utils/src/GlowEffect.h index 895cd4bbce..6b7bad7689 100644 --- a/libraries/render-utils/src/GlowEffect.h +++ b/libraries/render-utils/src/GlowEffect.h @@ -13,6 +13,7 @@ #define hifi_GlowEffect_h #include +#include #include #include @@ -20,8 +21,6 @@ #include -class QOpenGLFramebufferObject; - class ProgramObject; /// A generic full screen glow effect. @@ -33,7 +32,7 @@ public: /// Returns a pointer to the framebuffer object that the glow effect is *not* using for persistent state /// (either the secondary or the tertiary). - QOpenGLFramebufferObject* getFreeFramebufferObject() const; + gpu::FramebufferPointer getFreeFramebuffer() const; void init(QGLWidget* widget, bool enabled); @@ -53,7 +52,7 @@ public: /// Renders the glow effect. To be called after rendering the scene. /// \param toTexture whether to render to a texture, rather than to the frame buffer /// \return the framebuffer object to which we rendered, or NULL if to the frame buffer - QOpenGLFramebufferObject* render(bool toTexture = false); + gpu::FramebufferPointer render(bool toTexture = false); public slots: void toggleGlowEffect(bool enabled); diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 22358f7453..0a36ffa76a 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -130,7 +130,7 @@ void Model::RenderPipelineLib::addRenderPipeline(Model::RenderKey key, } // Z test depends if transparent or not - state->setDepthTest(true, !key.isTranslucent(), gpu::State::LESS_EQUAL); + state->setDepthTest(true, !key.isTranslucent(), gpu::LESS_EQUAL); // Blend on transparent state->setBlendFunction(key.isTranslucent(), @@ -673,8 +673,6 @@ bool Model::renderCore(float alpha, RenderMode mode, RenderArgs* args) { return false; } - // auto glowEffectIntensity = DependencyManager::get()->getIntensity(); - // Let's introduce a gpu::Batch to capture all the calls to the graphics api _renderBatch.clear(); gpu::Batch& batch = _renderBatch; @@ -703,35 +701,12 @@ bool Model::renderCore(float alpha, RenderMode mode, RenderArgs* args) { batch.setViewTransform(_transforms[0]); - // GLBATCH(glDisable)(GL_COLOR_MATERIAL); - - // taking care of by the state? - /* if (mode == RenderArgs::DIFFUSE_RENDER_MODE || mode == RenderArgs::NORMAL_RENDER_MODE) { - GLBATCH(glDisable)(GL_CULL_FACE); - } else { - GLBATCH(glEnable)(GL_CULL_FACE); - if (mode == RenderArgs::SHADOW_RENDER_MODE) { - GLBATCH(glCullFace)(GL_FRONT); - } - } - */ - - // render opaque meshes with alpha testing - -// GLBATCH(glDisable)(GL_BLEND); -// GLBATCH(glEnable)(GL_ALPHA_TEST); - - /* if (mode == RenderArgs::SHADOW_RENDER_MODE) { - GLBATCH(glAlphaFunc)(GL_EQUAL, 0.0f); - } - */ - /*DependencyManager::get()->setPrimaryDrawBuffers( mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::DIFFUSE_RENDER_MODE, mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::NORMAL_RENDER_MODE, mode == RenderArgs::DEFAULT_RENDER_MODE); */ - { + /*if (mode != RenderArgs::SHADOW_RENDER_MODE)*/ { GLenum buffers[3]; int bufferCount = 0; @@ -748,6 +723,7 @@ bool Model::renderCore(float alpha, RenderMode mode, RenderArgs* args) { buffers[bufferCount++] = GL_COLOR_ATTACHMENT2; } GLBATCH(glDrawBuffers)(bufferCount, buffers); + // batch.setFramebuffer(DependencyManager::get()->getPrimaryOpaqueFramebuffer()); } const float DEFAULT_ALPHA_THRESHOLD = 0.5f; @@ -790,12 +766,6 @@ bool Model::renderCore(float alpha, RenderMode mode, RenderArgs* args) { translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_OPAQUE_THRESHOLD, false, true, true, false, args, true); translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_OPAQUE_THRESHOLD, false, true, true, true, args, true); - // GLBATCH(glDisable)(GL_ALPHA_TEST); - /* GLBATCH(glEnable)(GL_BLEND); - GLBATCH(glDepthMask)(false); - GLBATCH(glDepthFunc)(GL_LEQUAL); - */ - //DependencyManager::get()->setPrimaryDrawBuffers(true); { GLenum buffers[1]; int bufferCount = 0; @@ -805,6 +775,8 @@ bool Model::renderCore(float alpha, RenderMode mode, RenderArgs* args) { // if (mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::DIFFUSE_RENDER_MODE) { if (mode != RenderArgs::SHADOW_RENDER_MODE) { + // batch.setFramebuffer(DependencyManager::get()->getPrimaryTransparentFramebuffer()); + const float MOSTLY_TRANSPARENT_THRESHOLD = 0.0f; translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, false, false, false, args, true); translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, false, false, true, args, true); @@ -814,6 +786,8 @@ bool Model::renderCore(float alpha, RenderMode mode, RenderArgs* args) { translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, true, false, true, args, true); translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, true, true, false, args, true); translucentMeshPartsRendered += renderMeshes(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, true, true, true, args, true); + + // batch.setFramebuffer(DependencyManager::get()->getPrimaryOpaqueFramebuffer()); } GLBATCH(glDepthMask)(true); @@ -1758,14 +1732,6 @@ void Model::setupBatchTransform(gpu::Batch& batch, RenderArgs* args) { void Model::endScene(RenderMode mode, RenderArgs* args) { PROFILE_RANGE(__FUNCTION__); - // auto glowEffectIntensity = DependencyManager::get()->getIntensity(); - - - #if defined(ANDROID) - #else - glPushMatrix(); - #endif - RenderArgs::RenderSide renderSide = RenderArgs::MONO; if (args) { renderSide = args->_renderSide; @@ -1792,34 +1758,15 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { _sceneRenderBatch.clear(); gpu::Batch& batch = _sceneRenderBatch; - // GLBATCH(glDisable)(GL_COLOR_MATERIAL); - - /* if (mode == RenderArgs::DIFFUSE_RENDER_MODE || mode == RenderArgs::NORMAL_RENDER_MODE) { - GLBATCH(glDisable)(GL_CULL_FACE); - } else { - GLBATCH(glEnable)(GL_CULL_FACE); - if (mode == RenderArgs::SHADOW_RENDER_MODE) { - GLBATCH(glCullFace)(GL_FRONT); - } - }*/ - - // render opaque meshes with alpha testing - - // GLBATCH(glDisable)(GL_BLEND); - // GLBATCH(glEnable)(GL_ALPHA_TEST); - - /* if (mode == RenderArgs::SHADOW_RENDER_MODE) { - GLBATCH(glAlphaFunc)(GL_EQUAL, 0.0f); - } -*/ - /*DependencyManager::get()->setPrimaryDrawBuffers( mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::DIFFUSE_RENDER_MODE, mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::NORMAL_RENDER_MODE, mode == RenderArgs::DEFAULT_RENDER_MODE); */ - { + + /* if (mode != RenderArgs::SHADOW_RENDER_MODE) */{ GLenum buffers[3]; + int bufferCount = 0; // if (mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::DIFFUSE_RENDER_MODE) { if (mode != RenderArgs::SHADOW_RENDER_MODE) { @@ -1834,6 +1781,8 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { buffers[bufferCount++] = GL_COLOR_ATTACHMENT2; } GLBATCH(glDrawBuffers)(bufferCount, buffers); + + // batch.setFramebuffer(DependencyManager::get()->getPrimaryOpaqueFramebuffer()); } const float DEFAULT_ALPHA_THRESHOLD = 0.5f; @@ -1856,7 +1805,6 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { opaqueMeshPartsRendered += renderMeshesForModelsInScene(batch, mode, false, DEFAULT_ALPHA_THRESHOLD, true, true, true, false, args); // render translucent meshes afterwards - //DependencyManager::get()->setPrimaryDrawBuffers(false, true, true); { GLenum buffers[2]; int bufferCount = 0; @@ -1876,21 +1824,19 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_OPAQUE_THRESHOLD, false, true, true, false, args); translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_OPAQUE_THRESHOLD, false, true, true, true, args); - // GLBATCH(glDisable)(GL_ALPHA_TEST); - /* GLBATCH(glEnable)(GL_BLEND); - GLBATCH(glDepthMask)(false); - GLBATCH(glDepthFunc)(GL_LEQUAL); - */ - //DependencyManager::get()->setPrimaryDrawBuffers(true); + { GLenum buffers[1]; int bufferCount = 0; buffers[bufferCount++] = GL_COLOR_ATTACHMENT0; GLBATCH(glDrawBuffers)(bufferCount, buffers); + // batch.setFramebuffer(DependencyManager::get()->getPrimaryTransparentFramebuffer()); } // if (mode == RenderArgs::DEFAULT_RENDER_MODE || mode == RenderArgs::DIFFUSE_RENDER_MODE) { if (mode != RenderArgs::SHADOW_RENDER_MODE) { + // batch.setFramebuffer(DependencyManager::get()->getPrimaryTransparentFramebuffer()); + const float MOSTLY_TRANSPARENT_THRESHOLD = 0.0f; translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, false, false, false, args); translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, false, false, true, args); @@ -1900,6 +1846,8 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, true, false, true, args); translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, true, true, false, args); translucentParts += renderMeshesForModelsInScene(batch, mode, true, MOSTLY_TRANSPARENT_THRESHOLD, false, true, true, true, args); + + // batch.setFramebuffer(DependencyManager::get()->getPrimaryOpaqueFramebuffer()); } GLBATCH(glDepthMask)(true); @@ -1938,10 +1886,10 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { // Back to no program GLBATCH(glUseProgram)(0); - if (args) { - args->_translucentMeshPartsRendered = translucentParts; - args->_opaqueMeshPartsRendered = opaqueMeshPartsRendered; - } + if (args) { + args->_translucentMeshPartsRendered = translucentParts; + args->_opaqueMeshPartsRendered = opaqueMeshPartsRendered; + } } @@ -1951,12 +1899,6 @@ void Model::endScene(RenderMode mode, RenderArgs* args) { backend.render(_sceneRenderBatch); } - - #if defined(ANDROID) - #else - glPopMatrix(); - #endif - // restore all the default material settings _viewState->setupWorldLight(); @@ -2334,17 +2276,15 @@ void Model::pickPrograms(gpu::Batch& batch, RenderMode mode, bool translucent, f RenderKey key(mode, translucent, alphaThreshold, hasLightmap, hasTangents, hasSpecular, isSkinned); auto pipeline = _renderPipelineLib.find(key.getRaw()); if (pipeline == _renderPipelineLib.end()) { - qDebug() << "No good, couldn;t find a pipeline from the key ?" << key.getRaw(); + qDebug() << "No good, couldn't find a pipeline from the key ?" << key.getRaw(); return; } gpu::ShaderPointer program = (*pipeline).second._pipeline->getProgram(); locations = (*pipeline).second._locations.get(); - //GLuint glprogram = gpu::GLBackend::getShaderID(program); - //GLBATCH(glUseProgram)(glprogram); - // dare! + // Setup the One pipeline batch.setPipeline((*pipeline).second._pipeline); if ((locations->alphaThreshold > -1) && (mode != RenderArgs::SHADOW_RENDER_MODE)) { @@ -2354,9 +2294,6 @@ void Model::pickPrograms(gpu::Batch& batch, RenderMode mode, bool translucent, f if ((locations->glowIntensity > -1) && (mode != RenderArgs::SHADOW_RENDER_MODE)) { GLBATCH(glUniform1f)(locations->glowIntensity, DependencyManager::get()->getIntensity()); } - // if (!(translucent && alphaThreshold == 0.0f) && (mode != RenderArgs::SHADOW_RENDER_MODE)) { - // GLBATCH(glAlphaFunc)(GL_EQUAL, DependencyManager::get()->getIntensity()); - // } } int Model::renderMeshesForModelsInScene(gpu::Batch& batch, RenderMode mode, bool translucent, float alphaThreshold, @@ -2383,10 +2320,7 @@ int Model::renderMeshesForModelsInScene(gpu::Batch& batch, RenderMode mode, bool } } } - // if we selected a program, then unselect it - if (!pickProgramsNeeded) { - // GLBATCH(glUseProgram)(0); - } + return meshPartsRendered; } @@ -2415,8 +2349,6 @@ int Model::renderMeshes(gpu::Batch& batch, RenderMode mode, bool translucent, fl args, locations); meshPartsRendered = renderMeshesFromList(list, batch, mode, translucent, alphaThreshold, args, locations, forceRenderSomeMeshes); - // GLBATCH(glUseProgram)(0); - return meshPartsRendered; } @@ -2427,7 +2359,7 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod PROFILE_RANGE(__FUNCTION__); auto textureCache = DependencyManager::get(); - // auto glowEffect = DependencyManager::get(); + QString lastMaterialID; int meshPartsRendered = 0; updateVisibleJointStates(); @@ -2531,13 +2463,6 @@ int Model::renderMeshesFromList(QVector& list, gpu::Batch& batch, RenderMod qCDebug(renderutils) << "NEW part.materialID:" << part.materialID; } -/* if (locations->glowIntensity >= 0) { - GLBATCH(glUniform1f)(locations->glowIntensity, glowEffect->getIntensity()); - } - if (!(translucent && alphaThreshold == 0.0f)) { - GLBATCH(glAlphaFunc)(GL_EQUAL, glowEffect->getIntensity()); - } -*/ if (locations->materialBufferUnit >= 0) { batch.setUniformBuffer(locations->materialBufferUnit, material->getSchemaBuffer()); } diff --git a/libraries/render-utils/src/TextureCache.cpp b/libraries/render-utils/src/TextureCache.cpp index 9b2a458231..63ca43725b 100644 --- a/libraries/render-utils/src/TextureCache.cpp +++ b/libraries/render-utils/src/TextureCache.cpp @@ -32,13 +32,6 @@ TextureCache::TextureCache() : _permutationNormalTexture(0), _whiteTexture(0), _blueTexture(0), - _primaryDepthTextureID(0), - _primaryNormalTextureID(0), - _primarySpecularTextureID(0), - _primaryFramebufferObject(NULL), - _secondaryFramebufferObject(NULL), - _tertiaryFramebufferObject(NULL), - _shadowFramebufferObject(NULL), _frameBufferSize(100, 100), _associatedWidget(NULL) { @@ -47,24 +40,6 @@ TextureCache::TextureCache() : } TextureCache::~TextureCache() { - - if (_primaryFramebufferObject) { - glDeleteTextures(1, &_primaryDepthTextureID); - glDeleteTextures(1, &_primaryNormalTextureID); - glDeleteTextures(1, &_primarySpecularTextureID); - } - - if (_primaryFramebufferObject) { - delete _primaryFramebufferObject; - } - - if (_secondaryFramebufferObject) { - delete _secondaryFramebufferObject; - } - - if (_tertiaryFramebufferObject) { - delete _tertiaryFramebufferObject; - } } void TextureCache::setFrameBufferSize(QSize frameBufferSize) { @@ -72,26 +47,15 @@ void TextureCache::setFrameBufferSize(QSize frameBufferSize) { if (_frameBufferSize != frameBufferSize) { _frameBufferSize = frameBufferSize; - if (_primaryFramebufferObject) { - delete _primaryFramebufferObject; - _primaryFramebufferObject = NULL; - glDeleteTextures(1, &_primaryDepthTextureID); - _primaryDepthTextureID = 0; - glDeleteTextures(1, &_primaryNormalTextureID); - _primaryNormalTextureID = 0; - glDeleteTextures(1, &_primarySpecularTextureID); - _primarySpecularTextureID = 0; - } + _primaryFramebuffer.reset(); + _primaryDepthTexture.reset(); + _primaryColorTexture.reset(); + _primaryNormalTexture.reset(); + _primarySpecularTexture.reset(); - if (_secondaryFramebufferObject) { - delete _secondaryFramebufferObject; - _secondaryFramebufferObject = NULL; - } + _secondaryFramebuffer.reset(); - if (_tertiaryFramebufferObject) { - delete _tertiaryFramebufferObject; - _tertiaryFramebufferObject = NULL; - } + _tertiaryFramebuffer.reset(); } } @@ -206,58 +170,78 @@ NetworkTexturePointer TextureCache::getTexture(const QUrl& url, TextureType type return texture; } -QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { +void TextureCache::createPrimaryFramebuffer() { + _primaryFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create()); - if (!_primaryFramebufferObject) { - _primaryFramebufferObject = createFramebufferObject(); + auto colorFormat = gpu::Element(gpu::VEC4, gpu::NUINT8, gpu::RGBA); + auto width = _frameBufferSize.width(); + auto height = _frameBufferSize.height(); - glGenTextures(1, &_primaryDepthTextureID); - glBindTexture(GL_TEXTURE_2D, _primaryDepthTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, _frameBufferSize.width(), _frameBufferSize.height(), - 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - glGenTextures(1, &_primaryNormalTextureID); - glBindTexture(GL_TEXTURE_2D, _primaryNormalTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), - 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glBindTexture(GL_TEXTURE_2D, 0); - - glGenTextures(1, &_primarySpecularTextureID); - glBindTexture(GL_TEXTURE_2D, _primarySpecularTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _frameBufferSize.width(), _frameBufferSize.height(), - 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glBindTexture(GL_TEXTURE_2D, 0); - - _primaryFramebufferObject->bind(); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _primaryDepthTextureID, 0); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, _primaryNormalTextureID, 0); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, _primarySpecularTextureID, 0); - _primaryFramebufferObject->release(); + auto defaultSampler = gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_POINT); + _primaryColorTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); + _primaryNormalTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); + _primarySpecularTexture = gpu::TexturePointer(gpu::Texture::create2D(colorFormat, width, height, defaultSampler)); + + _primaryFramebuffer->setRenderBuffer(0, _primaryColorTexture); + _primaryFramebuffer->setRenderBuffer(1, _primaryNormalTexture); + _primaryFramebuffer->setRenderBuffer(2, _primarySpecularTexture); + + + auto depthFormat = gpu::Element(gpu::SCALAR, gpu::FLOAT, gpu::DEPTH); + _primaryDepthTexture = gpu::TexturePointer(gpu::Texture::create2D(depthFormat, width, height, defaultSampler)); + + _primaryFramebuffer->setDepthStencilBuffer(_primaryDepthTexture, depthFormat); +} + +gpu::FramebufferPointer TextureCache::getPrimaryFramebuffer() { + if (!_primaryFramebuffer) { + createPrimaryFramebuffer(); } - return _primaryFramebufferObject; + return _primaryFramebuffer; +} + +gpu::TexturePointer TextureCache::getPrimaryDepthTexture() { + if (!_primaryDepthTexture) { + createPrimaryFramebuffer(); + } + return _primaryDepthTexture; +} + +gpu::TexturePointer TextureCache::getPrimaryColorTexture() { + if (!_primaryColorTexture) { + createPrimaryFramebuffer(); + } + return _primaryColorTexture; +} + +gpu::TexturePointer TextureCache::getPrimaryNormalTexture() { + if (!_primaryNormalTexture) { + createPrimaryFramebuffer(); + } + return _primaryNormalTexture; +} + +gpu::TexturePointer TextureCache::getPrimarySpecularTexture() { + if (!_primarySpecularTexture) { + createPrimaryFramebuffer(); + } + return _primarySpecularTexture; } GLuint TextureCache::getPrimaryDepthTextureID() { - // ensure that the primary framebuffer object is initialized before returning the depth texture id - getPrimaryFramebufferObject(); - return _primaryDepthTextureID; + return gpu::GLBackend::getTextureID(getPrimaryDepthTexture()); +} + +GLuint TextureCache::getPrimaryColorTextureID() { + return gpu::GLBackend::getTextureID(getPrimaryColorTexture()); } GLuint TextureCache::getPrimaryNormalTextureID() { - // ensure that the primary framebuffer object is initialized before returning the normal texture id - getPrimaryFramebufferObject(); - return _primaryNormalTextureID; + return gpu::GLBackend::getTextureID(getPrimaryNormalTexture()); } GLuint TextureCache::getPrimarySpecularTextureID() { - getPrimaryFramebufferObject(); - return _primarySpecularTextureID; + return gpu::GLBackend::getTextureID(getPrimarySpecularTexture()); } void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) { @@ -275,70 +259,50 @@ void TextureCache::setPrimaryDrawBuffers(bool color, bool normal, bool specular) glDrawBuffers(bufferCount, buffers); } -QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { - if (!_secondaryFramebufferObject) { - _secondaryFramebufferObject = createFramebufferObject(); +gpu::FramebufferPointer TextureCache::getSecondaryFramebuffer() { + if (!_secondaryFramebuffer) { + _secondaryFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create(gpu::Element::COLOR_RGBA_32, _frameBufferSize.width(), _frameBufferSize.height())); } - return _secondaryFramebufferObject; + return _secondaryFramebuffer; } -QOpenGLFramebufferObject* TextureCache::getTertiaryFramebufferObject() { - if (!_tertiaryFramebufferObject) { - _tertiaryFramebufferObject = createFramebufferObject(); +gpu::FramebufferPointer TextureCache::getTertiaryFramebuffer() { + if (!_tertiaryFramebuffer) { + _tertiaryFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::create(gpu::Element::COLOR_RGBA_32, _frameBufferSize.width(), _frameBufferSize.height())); } - return _tertiaryFramebufferObject; + return _tertiaryFramebuffer; } -QOpenGLFramebufferObject* TextureCache::getShadowFramebufferObject() { - if (!_shadowFramebufferObject) { + +gpu::FramebufferPointer TextureCache::getShadowFramebuffer() { + if (!_shadowFramebuffer) { const int SHADOW_MAP_SIZE = 2048; - _shadowFramebufferObject = new QOpenGLFramebufferObject(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, - QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGB); - - glGenTextures(1, &_shadowDepthTextureID); - glBindTexture(GL_TEXTURE_2D, _shadowDepthTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, - 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - const float DISTANT_BORDER[] = { 1.0f, 1.0f, 1.0f, 1.0f }; - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, DISTANT_BORDER); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); - glBindTexture(GL_TEXTURE_2D, 0); - - _shadowFramebufferObject->bind(); - glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, _shadowDepthTextureID, 0); - _shadowFramebufferObject->release(); + _shadowFramebuffer = gpu::FramebufferPointer(gpu::Framebuffer::createShadowmap(SHADOW_MAP_SIZE)); + + _shadowTexture = _shadowFramebuffer->getDepthStencilBuffer(); } - return _shadowFramebufferObject; + return _shadowFramebuffer; } GLuint TextureCache::getShadowDepthTextureID() { // ensure that the shadow framebuffer object is initialized before returning the depth texture id - getShadowFramebufferObject(); - return _shadowDepthTextureID; + getShadowFramebuffer(); + return gpu::GLBackend::getTextureID(_shadowTexture); } bool TextureCache::eventFilter(QObject* watched, QEvent* event) { if (event->type() == QEvent::Resize) { QSize size = static_cast(event)->size(); - if (_primaryFramebufferObject && _primaryFramebufferObject->size() != size) { - delete _primaryFramebufferObject; - _primaryFramebufferObject = NULL; - glDeleteTextures(1, &_primaryDepthTextureID); - glDeleteTextures(1, &_primaryNormalTextureID); - glDeleteTextures(1, &_primarySpecularTextureID); - } - if (_secondaryFramebufferObject && _secondaryFramebufferObject->size() != size) { - delete _secondaryFramebufferObject; - _secondaryFramebufferObject = NULL; - } - if (_tertiaryFramebufferObject && _tertiaryFramebufferObject->size() != size) { - delete _tertiaryFramebufferObject; - _tertiaryFramebufferObject = NULL; + if (_frameBufferSize != size) { + _primaryFramebuffer.reset(); + _primaryColorTexture.reset(); + _primaryDepthTexture.reset(); + _primaryNormalTexture.reset(); + _primarySpecularTexture.reset(); + + _secondaryFramebuffer.reset(); + + _tertiaryFramebuffer.reset(); } } return false; @@ -359,17 +323,6 @@ void TextureCache::associateWithWidget(QGLWidget* widget) { _associatedWidget->installEventFilter(this); } -QOpenGLFramebufferObject* TextureCache::createFramebufferObject() { - QOpenGLFramebufferObject* fbo = new QOpenGLFramebufferObject(_frameBufferSize); - - glBindTexture(GL_TEXTURE_2D, fbo->texture()); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glBindTexture(GL_TEXTURE_2D, 0); - - return fbo; -} - Texture::Texture() { } @@ -559,7 +512,7 @@ void NetworkTexture::setImage(const QImage& image, bool translucent, const QColo formatGPU = gpu::Element(gpu::VEC4, gpu::UINT8, (isLinearRGB ? gpu::RGBA : gpu::SRGBA)); formatMip = gpu::Element(gpu::VEC4, gpu::UINT8, (isLinearRGB ? gpu::BGRA : gpu::SBGRA)); } - _gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, image.width(), image.height())); + _gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, image.width(), image.height(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR))); _gpuTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits()); _gpuTexture->autoGenerateMips(-1); } @@ -598,7 +551,7 @@ QSharedPointer DilatableNetworkTexture::getDilatedTexture(float dilatio formatGPU = gpu::Element(gpu::VEC4, gpu::UINT8, (isLinearRGB ? gpu::RGBA : gpu::SRGBA)); formatMip = gpu::Element(gpu::VEC4, gpu::UINT8, (isLinearRGB ? gpu::BGRA : gpu::BGRA)); } - texture->_gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, dilatedImage.width(), dilatedImage.height())); + texture->_gpuTexture = gpu::TexturePointer(gpu::Texture::create2D(formatGPU, dilatedImage.width(), dilatedImage.height(), gpu::Sampler(gpu::Sampler::FILTER_MIN_MAG_MIP_LINEAR))); texture->_gpuTexture->assignStoredMip(0, formatMip, dilatedImage.byteCount(), dilatedImage.constBits()); texture->_gpuTexture->autoGenerateMips(-1); diff --git a/libraries/render-utils/src/TextureCache.h b/libraries/render-utils/src/TextureCache.h index 3ea46a4421..42c1cecde3 100644 --- a/libraries/render-utils/src/TextureCache.h +++ b/libraries/render-utils/src/TextureCache.h @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -22,8 +23,6 @@ #include #include -class QOpenGLFramebufferObject; - class NetworkTexture; typedef QSharedPointer NetworkTexturePointer; @@ -60,11 +59,17 @@ public: /// Returns a pointer to the primary framebuffer object. This render target includes a depth component, and is /// used for scene rendering. - QOpenGLFramebufferObject* getPrimaryFramebufferObject(); - + gpu::FramebufferPointer getPrimaryFramebuffer(); + + gpu::TexturePointer getPrimaryDepthTexture(); + gpu::TexturePointer getPrimaryColorTexture(); + gpu::TexturePointer getPrimaryNormalTexture(); + gpu::TexturePointer getPrimarySpecularTexture(); + /// Returns the ID of the primary framebuffer object's depth texture. This contains the Z buffer used in rendering. GLuint getPrimaryDepthTextureID(); - + GLuint getPrimaryColorTextureID(); + /// Returns the ID of the primary framebuffer object's normal texture. GLuint getPrimaryNormalTextureID(); @@ -76,15 +81,16 @@ public: /// Returns a pointer to the secondary framebuffer object, used as an additional render target when performing full /// screen effects. - QOpenGLFramebufferObject* getSecondaryFramebufferObject(); + gpu::FramebufferPointer getSecondaryFramebuffer(); /// Returns a pointer to the tertiary framebuffer object, used as an additional render target when performing full /// screen effects. - QOpenGLFramebufferObject* getTertiaryFramebufferObject(); - - /// Returns a pointer to the framebuffer object used to render shadow maps. - QOpenGLFramebufferObject* getShadowFramebufferObject(); + gpu::FramebufferPointer getTertiaryFramebuffer(); + /// Returns the framebuffer object used to render shadow maps; + gpu::FramebufferPointer getShadowFramebuffer(); + + /// Returns the ID of the shadow framebuffer object's depth texture. GLuint getShadowDepthTextureID(); @@ -99,24 +105,26 @@ private: TextureCache(); virtual ~TextureCache(); friend class DilatableNetworkTexture; - - QOpenGLFramebufferObject* createFramebufferObject(); gpu::TexturePointer _permutationNormalTexture; gpu::TexturePointer _whiteTexture; gpu::TexturePointer _blueTexture; + QHash > _dilatableNetworkTextures; - - GLuint _primaryDepthTextureID; - GLuint _primaryNormalTextureID; - GLuint _primarySpecularTextureID; - QOpenGLFramebufferObject* _primaryFramebufferObject; - QOpenGLFramebufferObject* _secondaryFramebufferObject; - QOpenGLFramebufferObject* _tertiaryFramebufferObject; - - QOpenGLFramebufferObject* _shadowFramebufferObject; - GLuint _shadowDepthTextureID; + + gpu::TexturePointer _primaryDepthTexture; + gpu::TexturePointer _primaryColorTexture; + gpu::TexturePointer _primaryNormalTexture; + gpu::TexturePointer _primarySpecularTexture; + gpu::FramebufferPointer _primaryFramebuffer; + void createPrimaryFramebuffer(); + + gpu::FramebufferPointer _secondaryFramebuffer; + gpu::FramebufferPointer _tertiaryFramebuffer; + + gpu::FramebufferPointer _shadowFramebuffer; + gpu::TexturePointer _shadowTexture; QSize _frameBufferSize; QGLWidget* _associatedWidget;