From c41ebcae788bcfb94bc784fc80af354c6e836b8d Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 26 Jun 2015 15:40:08 -0700 Subject: [PATCH] Covering all the case for rendering the light passes --- libraries/gpu/src/gpu/Batch.cpp | 28 + libraries/gpu/src/gpu/Batch.h | 21 +- libraries/gpu/src/gpu/Context.h | 2 +- libraries/gpu/src/gpu/Format.h | 17 + libraries/gpu/src/gpu/GLBackend.cpp | 32 +- libraries/gpu/src/gpu/GLBackend.h | 8 + libraries/gpu/src/gpu/GLBackendOutput.cpp | 11 +- libraries/gpu/src/gpu/GLBackendPipeline.cpp | 6 + libraries/gpu/src/gpu/GLBackendShader.cpp | 5 + libraries/gpu/src/gpu/GLBackendTransform.cpp | 20 +- libraries/gpu/src/gpu/Resource.cpp | 3 + libraries/gpu/src/gpu/StandardShaderLib.cpp | 9 + libraries/gpu/src/gpu/StandardShaderLib.h | 6 + libraries/gpu/src/gpu/State.h | 2 +- libraries/gpu/src/gpu/Transform.slh | 11 + libraries/model/src/model/Geometry.h | 2 + .../src/DeferredLightingEffect.cpp | 530 ++++++++++++++---- .../render-utils/src/DeferredLightingEffect.h | 36 +- libraries/render-utils/src/GeometryCache.cpp | 1 - libraries/render-utils/src/GeometryCache.h | 1 + .../render-utils/src/RenderDeferredTask.cpp | 3 +- .../src/deferred_light_limited.slv | 20 +- libraries/render-utils/src/spot_light.slf | 3 +- 23 files changed, 636 insertions(+), 141 deletions(-) diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index 973e5d01d2..ac4046503c 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -103,6 +103,23 @@ void Batch::clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, floa _params.push_back(targets); } +void Batch::clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color) { + clearFramebuffer(targets & Framebuffer::BUFFER_COLORS, color, 1.0f, 0); +} + +void Batch::clearDepthFramebuffer(float depth) { + clearFramebuffer(Framebuffer::BUFFER_DEPTH, Vec4(0.0f), depth, 0); +} + +void Batch::clearStencilFramebuffer(int stencil) { + clearFramebuffer(Framebuffer::BUFFER_STENCIL, Vec4(0.0f), 1.0f, stencil); +} + +void Batch::clearDepthStencilFramebuffer(float depth, int stencil) { + clearFramebuffer(Framebuffer::BUFFER_DEPTHSTENCIL, Vec4(0.0f), depth, stencil); +} + + void Batch::setInputFormat(const Stream::FormatPointer& format) { ADD_COMMAND(setInputFormat); @@ -141,6 +158,10 @@ void Batch::setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset _params.push_back(type); } +void Batch::setIndexBuffer(const BufferView& buffer) { + setIndexBuffer(buffer._element.getType(), buffer._buffer, buffer._offset); +} + void Batch::setModelTransform(const Transform& model) { ADD_COMMAND(setModelTransform); @@ -159,6 +180,13 @@ void Batch::setProjectionTransform(const Mat4& proj) { _params.push_back(cacheData(sizeof(Mat4), &proj)); } +void Batch::setViewportTransform(const Vec4i& viewport) { + ADD_COMMAND(setViewportTransform); + + _params.push_back(cacheData(sizeof(Vec4i), &viewport)); +} + + void Batch::setPipeline(const PipelinePointer& pipeline) { ADD_COMMAND(setPipeline); diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 9c97db65ef..64e2f7c6ca 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -44,19 +44,6 @@ namespace gpu { -enum Primitive { - POINTS = 0, - LINES, - LINE_STRIP, - TRIANGLES, - TRIANGLE_STRIP, - TRIANGLE_FAN, - QUADS, - QUAD_STRIP, - - NUM_PRIMITIVES, -}; - enum ReservedSlot { /* TRANSFORM_OBJECT_SLOT = 6, TRANSFORM_CAMERA_SLOT = 7, @@ -82,7 +69,12 @@ public: void drawIndexedInstanced(uint32 nbInstances, Primitive primitiveType, uint32 nbIndices, uint32 startIndex = 0, uint32 startInstance = 0); // Clear framebuffer layers + // Targets can be any of the render buffers contained in the Framebuffer void clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, float depth, int stencil); + void clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color); // not a command, just a shortcut for clearFramebuffer, mask out targets to make sure it touches only color targets + void clearDepthFramebuffer(float depth); // not a command, just a shortcut for clearFramebuffer, it touches only depth target + void clearStencilFramebuffer(int stencil); // not a command, just a shortcut for clearFramebuffer, it touches only stencil target + void clearDepthStencilFramebuffer(float depth, int stencil); // not a command, just a shortcut for clearFramebuffer, it touches depth and stencil target // Input Stage // InputFormat @@ -95,6 +87,7 @@ public: void setInputStream(Slot startChannel, const BufferStream& stream); // not a command, just unroll into a loop of setInputBuffer void setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset); + void setIndexBuffer(const BufferView& buffer); // not a command, just a shortcut from a BufferView // Transform Stage // Vertex position is transformed by ModelTransform from object space to world space @@ -105,6 +98,7 @@ public: void setModelTransform(const Transform& model); void setViewTransform(const Transform& view); void setProjectionTransform(const Mat4& proj); + void setViewportTransform(const Vec4i& viewport); // Viewport is xy = low left corner in the framebuffer, zw = width height of the viewport // Pipeline Stage void setPipeline(const PipelinePointer& pipeline); @@ -177,6 +171,7 @@ public: COMMAND_setModelTransform, COMMAND_setViewTransform, COMMAND_setProjectionTransform, + COMMAND_setViewportTransform, COMMAND_setPipeline, COMMAND_setStateBlendFactor, diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index 98ddc7fb64..da4882ab65 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -42,7 +42,7 @@ public: Mat4 _projectionViewUntranslated; Mat4 _projection; Mat4 _projectionInverse; - Vec4 _viewport; + Vec4i _viewport; }; template< typename T > diff --git a/libraries/gpu/src/gpu/Format.h b/libraries/gpu/src/gpu/Format.h index 7cf913430d..981a560965 100644 --- a/libraries/gpu/src/gpu/Format.h +++ b/libraries/gpu/src/gpu/Format.h @@ -38,6 +38,7 @@ typedef uint32 Offset; typedef glm::mat4 Mat4; typedef glm::mat3 Mat3; typedef glm::vec4 Vec4; +typedef glm::ivec4 Vec4i; typedef glm::vec3 Vec3; typedef glm::vec2 Vec2; typedef glm::ivec2 Vec2i; @@ -181,6 +182,9 @@ public: } static const Element COLOR_RGBA_32; + static const Element VEC3F_XYZ; + static const Element INDEX_UINT16; + static const Element PART_DRAWCALL; protected: uint8 _semantic; @@ -202,6 +206,19 @@ enum ComparisonFunction { NUM_COMPARISON_FUNCS, }; +enum Primitive { + POINTS = 0, + LINES, + LINE_STRIP, + TRIANGLES, + TRIANGLE_STRIP, + TRIANGLE_FAN, + QUADS, + QUAD_STRIP, + + NUM_PRIMITIVES, +}; + }; diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index 9004c4a8fe..ec93e00117 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -29,6 +29,7 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] = (&::gpu::GLBackend::do_setModelTransform), (&::gpu::GLBackend::do_setViewTransform), (&::gpu::GLBackend::do_setProjectionTransform), + (&::gpu::GLBackend::do_setViewportTransform), (&::gpu::GLBackend::do_setPipeline), (&::gpu::GLBackend::do_setStateBlendFactor), @@ -219,13 +220,31 @@ void GLBackend::do_clearFramebuffer(Batch& batch, uint32 paramOffset) { glmask |= GL_DEPTH_BUFFER_BIT; } + std::vector drawBuffers; if (masks & Framebuffer::BUFFER_COLORS) { - glClearColor(color.x, color.y, color.z, color.w); - glmask |= GL_COLOR_BUFFER_BIT; + for (int i = 0; i < Framebuffer::MAX_NUM_RENDER_BUFFERS; i++) { + if (masks & (1 << i)) { + drawBuffers.push_back(GL_COLOR_ATTACHMENT0 + i); + } + } + + if (!drawBuffers.empty()) { + glDrawBuffers(drawBuffers.size(), drawBuffers.data()); + glClearColor(color.x, color.y, color.z, color.w); + glmask |= GL_COLOR_BUFFER_BIT; + } } glClear(glmask); + // Restore the color draw buffers only if a frmaebuffer is bound + if (_output._framebuffer && !drawBuffers.empty()) { + auto glFramebuffer = syncGPUObject(*_output._framebuffer); + if (glFramebuffer) { + glDrawBuffers(glFramebuffer->_colorBuffers.size(), glFramebuffer->_colorBuffers.data()); + } + } + (void) CHECK_GL_ERROR(); } @@ -584,10 +603,11 @@ void GLBackend::do_glUniform4fv(Batch& batch, uint32 paramOffset) { return; } updatePipeline(); - glUniform4fv( - batch._params[paramOffset + 2]._int, - batch._params[paramOffset + 1]._uint, - (const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint)); + + GLint location = batch._params[paramOffset + 2]._int; + GLsizei count = batch._params[paramOffset + 1]._uint; + const GLfloat* value = (const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint); + glUniform4fv(location, count, value); (void) CHECK_GL_ERROR(); } diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index 28236c68c9..939b43c968 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -96,6 +96,7 @@ public: #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + GLuint _transformObject_model = -1; GLuint _transformCamera_viewInverse = -1; #endif @@ -179,6 +180,7 @@ public: class GLFramebuffer : public GPUObject { public: GLuint _fbo = 0; + std::vector _colorBuffers; GLFramebuffer(); ~GLFramebuffer(); @@ -267,6 +269,7 @@ protected: void do_setModelTransform(Batch& batch, uint32 paramOffset); void do_setViewTransform(Batch& batch, uint32 paramOffset); void do_setProjectionTransform(Batch& batch, uint32 paramOffset); + void do_setViewportTransform(Batch& batch, uint32 paramOffset); void initTransform(); void killTransform(); @@ -281,9 +284,11 @@ protected: Transform _model; Transform _view; Mat4 _projection; + Vec4i _viewport; bool _invalidModel; bool _invalidView; bool _invalidProj; + bool _invalidViewport; GLenum _lastMode; @@ -293,9 +298,11 @@ protected: _model(), _view(), _projection(), + _viewport(0,0,1,1), _invalidModel(true), _invalidView(true), _invalidProj(false), + _invalidViewport(false), _lastMode(GL_TEXTURE) {} } _transform; @@ -329,6 +336,7 @@ protected: #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + GLint _program_transformObject_model = -1; GLint _program_transformCamera_viewInverse = -1; #endif diff --git a/libraries/gpu/src/gpu/GLBackendOutput.cpp b/libraries/gpu/src/gpu/GLBackendOutput.cpp index 903c97f45b..30b60ad183 100755 --- a/libraries/gpu/src/gpu/GLBackendOutput.cpp +++ b/libraries/gpu/src/gpu/GLBackendOutput.cpp @@ -40,8 +40,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe glBindFramebuffer(GL_FRAMEBUFFER, fbo); - unsigned int nbColorBuffers = 0; - GLenum colorBuffers[16]; + std::vector colorBuffers; if (framebuffer.hasColor()) { static const GLenum colorAttachments[] = { GL_COLOR_ATTACHMENT0, @@ -69,8 +68,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe if (gltexture) { glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D, gltexture->_texture, 0); } - colorBuffers[nbColorBuffers] = colorAttachments[unit]; - nbColorBuffers++; + colorBuffers.push_back(colorAttachments[unit]); unit++; } } @@ -100,8 +98,8 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe } // Last but not least, define where we draw - if (nbColorBuffers > 0) { - glDrawBuffers(nbColorBuffers, colorBuffers); + if (!colorBuffers.empty()) { + glDrawBuffers(colorBuffers.size(), colorBuffers.data()); } else { glDrawBuffer( GL_NONE ); } @@ -139,6 +137,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe // All is green, assign the gpuobject to the Framebuffer object = new GLFramebuffer(); object->_fbo = fbo; + object->_colorBuffers = colorBuffers; Backend::setGPUObject(framebuffer, object); } diff --git a/libraries/gpu/src/gpu/GLBackendPipeline.cpp b/libraries/gpu/src/gpu/GLBackendPipeline.cpp index f4449e9ea1..2ad8302d12 100755 --- a/libraries/gpu/src/gpu/GLBackendPipeline.cpp +++ b/libraries/gpu/src/gpu/GLBackendPipeline.cpp @@ -73,6 +73,7 @@ void GLBackend::do_setPipeline(Batch& batch, uint32 paramOffset) { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + _pipeline._program_transformObject_model = -1; _pipeline._program_transformCamera_viewInverse = -1; #endif @@ -91,6 +92,7 @@ void GLBackend::do_setPipeline(Batch& batch, uint32 paramOffset) { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + _pipeline._program_transformObject_model = pipelineObject->_program->_transformObject_model; _pipeline._program_transformCamera_viewInverse = pipelineObject->_program->_transformCamera_viewInverse; #endif } @@ -143,6 +145,10 @@ void GLBackend::updatePipeline() { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + // If shader program needs the model we need to provide it + if (_pipeline._program_transformObject_model >= 0) { + glUniformMatrix4fv(_pipeline._program_transformObject_model, 1, false, (const GLfloat*) &_transform._transformObject._model); + } // If shader program needs the inverseView we need to provide it if (_pipeline._program_transformCamera_viewInverse >= 0) { glUniformMatrix4fv(_pipeline._program_transformCamera_viewInverse, 1, false, (const GLfloat*) &_transform._transformCamera._viewInverse); diff --git a/libraries/gpu/src/gpu/GLBackendShader.cpp b/libraries/gpu/src/gpu/GLBackendShader.cpp index 45adbcdb3c..6058b8f8a9 100755 --- a/libraries/gpu/src/gpu/GLBackendShader.cpp +++ b/libraries/gpu/src/gpu/GLBackendShader.cpp @@ -111,6 +111,11 @@ void makeBindings(GLBackend::GLShader* shader) { shader->_transformCameraSlot = gpu::TRANSFORM_CAMERA_SLOT; } #else + loc = glGetUniformLocation(glprogram, "transformObject_model"); + if (loc >= 0) { + shader->_transformObject_model = loc; + } + loc = glGetUniformLocation(glprogram, "transformCamera_viewInverse"); if (loc >= 0) { shader->_transformCamera_viewInverse = loc; diff --git a/libraries/gpu/src/gpu/GLBackendTransform.cpp b/libraries/gpu/src/gpu/GLBackendTransform.cpp index 2e3c2dca70..faddacc176 100755 --- a/libraries/gpu/src/gpu/GLBackendTransform.cpp +++ b/libraries/gpu/src/gpu/GLBackendTransform.cpp @@ -15,7 +15,6 @@ using namespace gpu; // Transform Stage - void GLBackend::do_setModelTransform(Batch& batch, uint32 paramOffset) { _transform._model = batch._transforms.get(batch._params[paramOffset]._uint); _transform._invalidModel = true; @@ -31,6 +30,11 @@ void GLBackend::do_setProjectionTransform(Batch& batch, uint32 paramOffset) { _transform._invalidProj = true; } +void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) { + memcpy(&_transform._viewport, batch.editData(batch._params[paramOffset]._uint), sizeof(Vec4i)); + _transform._invalidViewport = true; +} + void GLBackend::initTransform() { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) glGenBuffers(1, &_transform._transformObjectBuffer); @@ -57,10 +61,13 @@ void GLBackend::killTransform() { } void GLBackend::syncTransformStateCache() { + _transform._invalidViewport = true; _transform._invalidProj = true; _transform._invalidView = true; _transform._invalidModel = true; + glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport); + GLint currentMode; glGetIntegerv(GL_MATRIX_MODE, ¤tMode); _transform._lastMode = currentMode; @@ -78,6 +85,13 @@ void GLBackend::updateTransform() { GLint originalMatrixMode; glGetIntegerv(GL_MATRIX_MODE, &originalMatrixMode); // Check all the dirty flags and update the state accordingly + if (_transform._invalidViewport) { + _transform._transformCamera._viewport = _transform._viewport; + + // Where we assign the GL viewport + glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w); + } + if (_transform._invalidProj) { _transform._transformCamera._projection = _transform._projection; _transform._transformCamera._projectionInverse = glm::inverse(_transform._projection); @@ -100,7 +114,7 @@ void GLBackend::updateTransform() { } #if (GPU_TRANSFORM_PROFILE == GPU_CORE) - if (_transform._invalidView || _transform._invalidProj) { + if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) { glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_CAMERA_SLOT, 0); glBindBuffer(GL_ARRAY_BUFFER, _transform._transformCameraBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformCamera), (const void*) &_transform._transformCamera, GL_DYNAMIC_DRAW); @@ -162,7 +176,7 @@ void GLBackend::updateTransform() { #endif // Flags are clean - _transform._invalidView = _transform._invalidProj = _transform._invalidModel = false; + _transform._invalidView = _transform._invalidProj = _transform._invalidModel = _transform._invalidViewport = false; glMatrixMode(originalMatrixMode); } diff --git a/libraries/gpu/src/gpu/Resource.cpp b/libraries/gpu/src/gpu/Resource.cpp index 046cf9fe40..5498e24189 100644 --- a/libraries/gpu/src/gpu/Resource.cpp +++ b/libraries/gpu/src/gpu/Resource.cpp @@ -15,6 +15,9 @@ using namespace gpu; const Element Element::COLOR_RGBA_32 = Element(VEC4, UINT8, RGBA); +const Element Element::VEC3F_XYZ = Element(VEC3, FLOAT, XYZ); +const Element Element::INDEX_UINT16 = Element(SCALAR, UINT16, INDEX); +const Element Element::PART_DRAWCALL = Element(VEC4, UINT32, PART); Resource::Size Resource::Sysmem::allocateMemory(Byte** dataAllocated, Size size) { if ( !dataAllocated ) { diff --git a/libraries/gpu/src/gpu/StandardShaderLib.cpp b/libraries/gpu/src/gpu/StandardShaderLib.cpp index fa56eb2051..581ce47cde 100755 --- a/libraries/gpu/src/gpu/StandardShaderLib.cpp +++ b/libraries/gpu/src/gpu/StandardShaderLib.cpp @@ -13,11 +13,13 @@ #include "StandardShaderLib.h" #include "DrawTransformUnitQuad_vert.h" +#include "DrawViewportQuadTransformTexcoord_vert.h" #include "DrawTexture_frag.h" using namespace gpu; ShaderPointer StandardShaderLib::_drawTransformUnitQuadVS; +ShaderPointer StandardShaderLib::_drawViewportQuadTransformTexcoordVS; ShaderPointer StandardShaderLib::_drawTexturePS; ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() { @@ -27,6 +29,13 @@ ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() { return _drawTransformUnitQuadVS; } +ShaderPointer StandardShaderLib::getDrawViewportQuadTransformTexcoordVS() { + if (!_drawViewportQuadTransformTexcoordVS) { + _drawViewportQuadTransformTexcoordVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawViewportQuadTransformTexcoord_vert))); + } + return _drawViewportQuadTransformTexcoordVS; +} + ShaderPointer StandardShaderLib::getDrawTexturePS() { if (!_drawTexturePS) { _drawTexturePS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(DrawTexture_frag))); diff --git a/libraries/gpu/src/gpu/StandardShaderLib.h b/libraries/gpu/src/gpu/StandardShaderLib.h index cabdbad66b..a8fc5126f8 100755 --- a/libraries/gpu/src/gpu/StandardShaderLib.h +++ b/libraries/gpu/src/gpu/StandardShaderLib.h @@ -22,13 +22,19 @@ namespace gpu { class StandardShaderLib { public: + // Shader draw the unit quad objectPos = ([(-1,-1),(1,1)]) and transform it by the full model transform stack (Model, View, Proj). + // A texcoord attribute is also generated texcoord = [(0,0),(1,1)] static ShaderPointer getDrawTransformUnitQuadVS(); + // Shader draws the unit quad in the full viewport clipPos = ([(-1,-1),(1,1)]) and transform the texcoord = [(0,0),(1,1)] by the model transform. + static ShaderPointer getDrawViewportQuadTransformTexcoordVS(); + static ShaderPointer getDrawTexturePS(); protected: static ShaderPointer _drawTransformUnitQuadVS; + static ShaderPointer _drawViewportQuadTransformTexcoordVS; static ShaderPointer _drawTexturePS; }; diff --git a/libraries/gpu/src/gpu/State.h b/libraries/gpu/src/gpu/State.h index 07198d8162..59fbacc089 100755 --- a/libraries/gpu/src/gpu/State.h +++ b/libraries/gpu/src/gpu/State.h @@ -341,7 +341,7 @@ public: // Color write mask void setColorWriteMask(uint8 mask) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, mask, _values.colorWriteMask); } - void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, (WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha), _values.colorWriteMask); } + void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { uint32 value = ((WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha)); SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, value, _values.colorWriteMask); } uint8 getColorWriteMask() const { return _values.colorWriteMask; } // All the possible fields diff --git a/libraries/gpu/src/gpu/Transform.slh b/libraries/gpu/src/gpu/Transform.slh index d01fe128ae..fad2e9ada8 100644 --- a/libraries/gpu/src/gpu/Transform.slh +++ b/libraries/gpu/src/gpu/Transform.slh @@ -86,6 +86,7 @@ TransformCamera getTransformCamera() { return camera; } +uniform mat4 transformObject_model; uniform mat4 transformCamera_viewInverse; <@endif@> @@ -122,6 +123,16 @@ uniform mat4 transformCamera_viewInverse; <@endif@> <@endfunc@> +<@func transformModelToWorldPos(objectTransform, modelPos, worldPos)@> +<@if GPU_TRANSFORM_PROFILE == GPU_CORE@> + { // transformModelToWorldPos + <$worldPos$> = (<$objectTransform$>._model * <$modelPos$>); + } +<@else@> + <$worldPos$> = vec3(transformObject_model * <$modelPos$>); +<@endif@> +<@endfunc@> + <@func transformModelToEyeDir(cameraTransform, objectTransform, modelDir, eyeDir)@> <@if GPU_TRANSFORM_PROFILE == GPU_CORE@> { // transformModelToEyeDir diff --git a/libraries/model/src/model/Geometry.h b/libraries/model/src/model/Geometry.h index ddefaf4e96..16ebb60b72 100755 --- a/libraries/model/src/model/Geometry.h +++ b/libraries/model/src/model/Geometry.h @@ -113,6 +113,8 @@ public: // Generate a BufferStream on the mesh vertices and attributes const gpu::BufferStream makeBufferStream() const; + static gpu::Primitive topologyToPrimitive(Topology topo) { return static_cast(topo); } + protected: gpu::Stream::FormatPointer _vertexFormat; diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 03dca38462..afcc116aea 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -106,7 +106,7 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); { - auto VSFS = gpu::StandardShaderLib::getDrawTransformUnitQuadVS(); + auto VSFS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS(); auto PSBlit = gpu::StandardShaderLib::getDrawTexturePS(); auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(VSFS, PSBlit)); gpu::Shader::makeProgram(*blitProgram); @@ -225,51 +225,73 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu } void DeferredLightingEffect::prepare(RenderArgs* args) { - // clear the normal and specular buffers + auto textureCache = DependencyManager::get(); + gpu::Batch batch; + // batch.setFramebuffer(textureCache->getPrimaryFramebuffer()); + + // clear the normal and specular buffers + batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR1, glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)); + const float MAX_SPECULAR_EXPONENT = 128.0f; + batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR2, glm::vec4(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT)); + + args->_context->syncCache(); + args->_context->render(batch); +/* textureCache->setPrimaryDrawBuffers(false, true, false); glClear(GL_COLOR_BUFFER_BIT); textureCache->setPrimaryDrawBuffers(false, false, true); // clearing to zero alpha for specular causes problems on my Nvidia card; clear to lowest non-zero value instead const float MAX_SPECULAR_EXPONENT = 128.0f; glClearColor(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT); - glClear(GL_COLOR_BUFFER_BIT); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT);*/ + /* glClearColor(0.0f, 0.0f, 0.0f, 0.0f); textureCache->setPrimaryDrawBuffers(true, false, false); -} +*/} void DeferredLightingEffect::render(RenderArgs* args) { + gpu::Batch batch; + // perform deferred lighting, rendering to free fbo - glDisable(GL_BLEND); + /* glDisable(GL_BLEND); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glDisable(GL_COLOR_MATERIAL); glDepthMask(false); - + */ auto textureCache = DependencyManager::get(); - glBindFramebuffer(GL_FRAMEBUFFER, 0 ); + // glBindFramebuffer(GL_FRAMEBUFFER, 0 ); QSize framebufferSize = textureCache->getFrameBufferSize(); // binding the first framebuffer auto freeFBO = DependencyManager::get()->getFreeFramebuffer(); - glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO)); + + batch.setFramebuffer(freeFBO); + + //glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO)); - glClear(GL_COLOR_BUFFER_BIT); - // glEnable(GL_FRAMEBUFFER_SRGB); + batch.clearColorFramebuffer(freeFBO->getBufferMask(), glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)); + + // glClear(GL_COLOR_BUFFER_BIT); // glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID()); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID()); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID()); + batch.setUniformTexture(0, textureCache->getPrimaryColorTexture()); + + // glActiveTexture(GL_TEXTURE1); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID()); + batch.setUniformTexture(1, textureCache->getPrimaryNormalTexture()); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimarySpecularTextureID()); + // glActiveTexture(GL_TEXTURE2); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimarySpecularTextureID()); + batch.setUniformTexture(2, textureCache->getPrimarySpecularTexture()); - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryDepthTextureID()); + // glActiveTexture(GL_TEXTURE3); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryDepthTextureID()); + batch.setUniformTexture(3, textureCache->getPrimaryDepthTexture()); // get the viewport side (left, right, both) int viewport[4]; @@ -290,50 +312,55 @@ void DeferredLightingEffect::render(RenderArgs* args) { glm::mat4 invViewMat; _viewState->getViewTransform().getMatrix(invViewMat); - ProgramObject* program = &_directionalLight; + // ProgramObject* program = &_directionalLight; + auto& program = _directionalLight; const LightLocations* locations = &_directionalLightLocations; bool shadowsEnabled = _viewState->getShadowsEnabled(); if (shadowsEnabled) { - glActiveTexture(GL_TEXTURE4); - glBindTexture(GL_TEXTURE_2D, textureCache->getShadowDepthTextureID()); - program = &_directionalLightShadowMap; + // glActiveTexture(GL_TEXTURE4); + // glBindTexture(GL_TEXTURE_2D, textureCache->getShadowDepthTextureID()); + batch.setUniformTexture(4, textureCache->getShadowFramebuffer()->getDepthStencilBuffer()); + + program = _directionalLightShadowMap; locations = &_directionalLightShadowMapLocations; if (_viewState->getCascadeShadowsEnabled()) { - program = &_directionalLightCascadedShadowMap; + program = _directionalLightCascadedShadowMap; locations = &_directionalLightCascadedShadowMapLocations; if (useSkyboxCubemap) { - program = &_directionalSkyboxLightCascadedShadowMap; + program = _directionalSkyboxLightCascadedShadowMap; locations = &_directionalSkyboxLightCascadedShadowMapLocations; } else if (_ambientLightMode > -1) { - program = &_directionalAmbientSphereLightCascadedShadowMap; + program = _directionalAmbientSphereLightCascadedShadowMap; locations = &_directionalAmbientSphereLightCascadedShadowMapLocations; } - program->bind(); - program->setUniform(locations->shadowDistances, _viewState->getShadowDistances()); + batch.setPipeline(program); + //program->bind(); + // program->setUniform(locations->shadowDistances, _viewState->getShadowDistances()); + batch._glUniform3fv(locations->shadowDistances, 1, (const GLfloat*) &_viewState->getShadowDistances()); } else { if (useSkyboxCubemap) { - program = &_directionalSkyboxLightShadowMap; + program = _directionalSkyboxLightShadowMap; locations = &_directionalSkyboxLightShadowMapLocations; } else if (_ambientLightMode > -1) { - program = &_directionalAmbientSphereLightShadowMap; + program = _directionalAmbientSphereLightShadowMap; locations = &_directionalAmbientSphereLightShadowMapLocations; } - program->bind(); + batch.setPipeline(program); } - program->setUniformValue(locations->shadowScale, - 1.0f / textureCache->getShadowFramebuffer()->getWidth()); + // program->setUniformValue(locations->shadowScale, 1.0f / textureCache->getShadowFramebuffer()->getWidth()); + batch._glUniform1f(locations->shadowScale, 1.0f / textureCache->getShadowFramebuffer()->getWidth()); } else { if (useSkyboxCubemap) { - program = &_directionalSkyboxLight; + program = _directionalSkyboxLight; locations = &_directionalSkyboxLightLocations; } else if (_ambientLightMode > -1) { - program = &_directionalAmbientSphereLight; + program = _directionalAmbientSphereLight; locations = &_directionalAmbientSphereLightLocations; } - program->bind(); + batch.setPipeline(program); } { @@ -344,71 +371,116 @@ void DeferredLightingEffect::render(RenderArgs* args) { if (useSkyboxCubemap && _skybox->getCubemap()->getIrradiance()) { sh = (*_skybox->getCubemap()->getIrradiance()); } - for (int i =0; i ambientSphere, gpu::SphericalHarmonics::NUM_COEFFICIENTS, (const GLfloat*) (&sh)); + + for (int i =0; i ambientSphere + i, 1, (const GLfloat*) (&sh) + i * 4); + } + /* for (int i =0; i setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); - } + }*/ } if (useSkyboxCubemap) { - glActiveTexture(GL_TEXTURE5); - glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap())); + // glActiveTexture(GL_TEXTURE5); + // glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap())); + batch.setUniformTexture(5, _skybox->getCubemap()); } if (locations->lightBufferUnit >= 0) { - gpu::Batch batch; + //gpu::Batch batch; batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + //gpu::GLBackend::renderBatch(batch); } if (_atmosphere && (locations->atmosphereBufferUnit >= 0)) { - gpu::Batch batch; + //gpu::Batch batch; batch.setUniformBuffer(locations->atmosphereBufferUnit, _atmosphere->getDataBuffer()); - gpu::GLBackend::renderBatch(batch); + //gpu::GLBackend::renderBatch(batch); } - glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + // glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + batch._glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); } float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; _viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); - program->setUniformValue(locations->nearLocation, nearVal); + // program->setUniformValue(locations->nearLocation, nearVal); + batch._glUniform1f(locations->nearLocation, nearVal); + float depthScale = (farVal - nearVal) / farVal; - program->setUniformValue(locations->depthScale, depthScale); + // program->setUniformValue(locations->depthScale, depthScale); + batch._glUniform1f(locations->depthScale, depthScale); + float nearScale = -1.0f / nearVal; float depthTexCoordScaleS = (right - left) * nearScale / sWidth; float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight; float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS; float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; - program->setUniformValue(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); - program->setUniformValue(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + // program->setUniformValue(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + // program->setUniformValue(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + batch._glUniform2f(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); - - program->release(); + Transform model; + model.setTranslation(glm::vec3(sMin, tMin, 0.0)); + model.setScale(glm::vec3(sWidth, tHeight, 1.0)); + batch.setModelTransform(model); + + batch.setProjectionTransform(glm::mat4()); + batch.setViewTransform(Transform()); + + { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + } + + // renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); + // batch.draw(gpu::TRIANGLE_STRIP, 4); // full screen quad + + // args->_context->syncCache(); + // args->_context->render(batch); + //program->release(); if (useSkyboxCubemap) { - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - if (!shadowsEnabled) { - glActiveTexture(GL_TEXTURE3); - } + batch.setUniformTexture(5, nullptr); + + // glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + // if (!shadowsEnabled) { + // glActiveTexture(GL_TEXTURE3); + // } } if (shadowsEnabled) { - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE3); + batch.setUniformTexture(4, nullptr); + // glBindTexture(GL_TEXTURE_2D, 0); + // glActiveTexture(GL_TEXTURE3); } - + + // additive blending - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE); + // glEnable(GL_BLEND); + // glBlendFunc(GL_ONE, GL_ONE); - glEnable(GL_CULL_FACE); + // glEnable(GL_CULL_FACE); + glm::vec4 sCoefficients(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f); glm::vec4 tCoefficients(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f); - glTexGenfv(GL_S, GL_OBJECT_PLANE, (const GLfloat*)&sCoefficients); - glTexGenfv(GL_T, GL_OBJECT_PLANE, (const GLfloat*)&tCoefficients); - + // glTexGenfv(GL_S, GL_OBJECT_PLANE, (const GLfloat*)&sCoefficients); + // glTexGenfv(GL_T, GL_OBJECT_PLANE, (const GLfloat*)&tCoefficients); + // texcoordMat + auto texcoordMat = glm::mat4(); + texcoordMat[0] = glm::vec4(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f); + texcoordMat[1] = glm::vec4(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f); + texcoordMat[2] = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f); + texcoordMat[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); + // enlarge the scales slightly to account for tesselation const float SCALE_EXPANSION = 0.05f; @@ -417,102 +489,199 @@ void DeferredLightingEffect::render(RenderArgs* args) { auto geometryCache = DependencyManager::get(); + glm::mat4 projMat; + Transform viewMat; + args->_viewFrustum->evalProjectionMatrix(projMat); + args->_viewFrustum->evalViewTransform(viewMat); + if (args->_renderMode == RenderArgs::MIRROR_RENDER_MODE) { + viewMat.postScale(glm::vec3(-1.0f, 1.0f, 1.0f)); + } + batch.setProjectionTransform(projMat); + batch.setViewTransform(viewMat); + if (!_pointLights.empty()) { - _pointLight.bind(); - _pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); - _pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); - _pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); - _pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + batch.setPipeline(_pointLight); + batch._glUniform1f(_pointLightLocations.nearLocation, nearVal); + batch._glUniform1f(_pointLightLocations.depthScale, depthScale); + batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + + //_pointLight.bind(); + //_pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); + //_pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); + //_pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + //_pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + + batch._glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + + //glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + batch._glUniformMatrix4fv(_pointLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat)); for (auto lightID : _pointLights) { - auto light = _allocatedLights[lightID]; - + auto& light = _allocatedLights[lightID]; + light->setShowContour(true); if (_pointLightLocations.lightBufferUnit >= 0) { - gpu::Batch batch; + // gpu::Batch batch; batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + // gpu::GLBackend::renderBatch(batch); } - glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - glPushMatrix(); + + // glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { - glLoadIdentity(); + + /* glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - - renderFullscreenQuad(); - + */ + // renderFullscreenQuad(); + Transform model; + model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); + batch.setModelTransform(model); + + { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + } + /* glPopMatrix(); glMatrixMode(GL_MODELVIEW); - + */ } else { - glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); - geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + Transform model; + model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); + batch.setModelTransform(model); + // glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); + + // geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + geometryCache->renderSphere(batch, expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); } - glPopMatrix(); + // glPopMatrix(); } _pointLights.clear(); - _pointLight.release(); + // _pointLight.release(); } if (!_spotLights.empty()) { - _spotLight.bind(); + batch.setPipeline(_pointLight); + batch._glUniform1f(_spotLightLocations.nearLocation, nearVal); + batch._glUniform1f(_spotLightLocations.depthScale, depthScale); + batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + +/* _spotLight.bind(); _spotLight.setUniformValue(_spotLightLocations.nearLocation, nearVal); _spotLight.setUniformValue(_spotLightLocations.depthScale, depthScale); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - + */ + + + batch._glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + + //glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + batch._glUniformMatrix4fv(_spotLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat)); + for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; if (_spotLightLocations.lightBufferUnit >= 0) { - gpu::Batch batch; + // gpu::Batch batch; batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + // gpu::GLBackend::renderBatch(batch); } - glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + // glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - glPushMatrix(); + // glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle()); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { - glLoadIdentity(); + /*glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - renderFullscreenQuad(); - - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); + renderFullscreenQuad();*/ + Transform model; + model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); + batch.setModelTransform(model); + + { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + } + + /* glPopMatrix(); + glMatrixMode(GL_MODELVIEW);*/ } else { + Transform model; + model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); + + glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); + glm::vec3 axis = glm::axis(spotRotation); + + model.postRotate(spotRotation); + model.postTranslate(glm::vec3(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f))); + + float base = expandedRadius * glm::tan(light->getSpotAngle()); + float height = expandedRadius; + model.postScale(glm::vec3(base, base, height)); + + batch.setModelTransform(model); + auto& mesh = getSpotLightMesh(); + + + batch.setIndexBuffer(mesh->getIndexBuffer()); + batch.setInputBuffer(0, mesh->getVertexBuffer()); + batch.setInputFormat(mesh->getVertexFormat()); + + + auto& part = mesh->getPartBuffer().get(); + + batch.drawIndexed(model::Mesh::topologyToPrimitive(part._topology), part._numIndices, part._startIndex); + + + //geometryCache->renderCone(batch, expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); + + /* glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); glm::vec3 axis = glm::axis(spotRotation); glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); - geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), - expandedRadius, 32, 1); + geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); + */ } glPopMatrix(); } _spotLights.clear(); - _spotLight.release(); + // _spotLight.release(); } - + +/* glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE2); @@ -523,8 +692,16 @@ void DeferredLightingEffect::render(RenderArgs* args) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); - // glDisable(GL_FRAMEBUFFER_SRGB); - +*/ + // Probably not necessary in the long run because the gpu layer would unbound this texture if used as render target + batch.setUniformTexture(0, nullptr); + batch.setUniformTexture(1, nullptr); + batch.setUniformTexture(2, nullptr); + batch.setUniformTexture(3, nullptr); + + args->_context->syncCache(); + args->_context->render(batch); + // End of the Lighting pass } @@ -568,7 +745,6 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { */ batch.setProjectionTransform(glm::mat4()); - batch.setModelTransform(Transform()); batch.setViewTransform(Transform()); int viewport[4]; @@ -583,6 +759,14 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height(); float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height(); + Transform model; + model.setTranslation(glm::vec3(sMin, tMin, 0.0)); + model.setScale(glm::vec3(sWidth, tHeight, 1.0)); + batch.setModelTransform(model); + + + batch.setViewportTransform(glm::ivec4(viewport[0], viewport[1], viewport[2], viewport[3])); + batch.draw(gpu::TRIANGLE_STRIP, 4); @@ -609,7 +793,7 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU auto globalLight = _allocatedLights[_globalLights.front()]; args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); } - +/* void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations) { program.addShaderFromSourceCode(QGLShader::Vertex, (limited ? deferred_light_limited_vert : deferred_light_vert)); program.addShaderFromSourceCode(QGLShader::Fragment, fragSource); @@ -672,6 +856,60 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit program.release(); } +*/ + + +void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) { + auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string((lightVolume ? deferred_light_limited_vert : deferred_light_vert)))); + auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource))); + + gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS)); + + gpu::Shader::BindingSet slotBindings; + slotBindings.insert(gpu::Shader::Binding(std::string("diffuseMap"), 0)); + slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), 1)); + slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), 2)); + slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), 3)); + slotBindings.insert(gpu::Shader::Binding(std::string("shadowMap"), 4)); + slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), 5)); + const GLint LIGHT_GPU_SLOT = 3; + slotBindings.insert(gpu::Shader::Binding(std::string("lightBuffer"), LIGHT_GPU_SLOT)); + const GLint ATMOSPHERE_GPU_SLOT = 4; + slotBindings.insert(gpu::Shader::Binding(std::string("atmosphereBufferUnit"), ATMOSPHERE_GPU_SLOT)); + + gpu::Shader::makeProgram(*program, slotBindings); + + locations.shadowDistances = program->getUniforms().findLocation("shadowDistances"); + locations.shadowScale = program->getUniforms().findLocation("shadowScale"); + locations.nearLocation = program->getUniforms().findLocation("near"); + locations.depthScale = program->getUniforms().findLocation("depthScale"); + locations.depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset"); + locations.depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale"); + locations.radius = program->getUniforms().findLocation("radius"); + locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); + locations.invViewMat = program->getUniforms().findLocation("invViewMat"); + locations.texcoordMat = program->getUniforms().findLocation("texcoordMat"); + +#if (GPU_FEATURE_PROFILE == GPU_CORE) + locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); + locations.atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit"); +#else + locations.lightBufferUnit = program->getUniforms().findLocation("lightBuffer"); + locations.atmosphereBufferUnit = program->getUniforms().findLocation("atmosphereBufferUnit"); +#endif + + gpu::StatePointer state = gpu::StatePointer(new gpu::State()); + if (lightVolume) { + state->setCullMode(gpu::State::CULL_BACK); + + // additive blending + state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE); + } else { + state->setCullMode(gpu::State::CULL_BACK); + } + pipeline.reset(gpu::Pipeline::create(program, state)); + +} void DeferredLightingEffect::setAmbientLightMode(int preset) { if ((preset >= 0) && (preset < gpu::SphericalHarmonics::NUM_PRESET)) { @@ -695,3 +933,89 @@ void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const gl void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) { _skybox = skybox; } + +model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { + if (!_spotLightMesh) { + _spotLightMesh.reset(new model::Mesh()); + + int slices = 32; + int stacks = 1; + int vertices = (stacks + 2) * slices; + int baseTriangles = slices - 2; + int indices = 6 * slices * stacks + 3 * baseTriangles; + + GLfloat* vertexData = new GLfloat[vertices * 3]; + GLfloat* vertex = vertexData; + // cap + for (int i = 0; i < slices; i++) { + float theta = TWO_PI * i / slices; + + //normals + /* *(vertex++) = 0.0f; + *(vertex++) = 0.0f; + *(vertex++) = -1.0f; + */ + // vertices + *(vertex++) = cosf(theta); + *(vertex++) = sinf(theta); + *(vertex++) = 0.0f; + } + // body + for (int i = 0; i <= stacks; i++) { + float z = (float)i / stacks; + float radius = 1.0f - z; + + for (int j = 0; j < slices; j++) { + float theta = TWO_PI * j / slices; + + //normals + /* *(vertex++) = cosf(theta) / SQUARE_ROOT_OF_2; + *(vertex++) = sinf(theta) / SQUARE_ROOT_OF_2; + *(vertex++) = 1.0f / SQUARE_ROOT_OF_2; + */ + // vertices + *(vertex++) = radius * cosf(theta); + *(vertex++) = radius * sinf(theta); + *(vertex++) = z; + } + } + + _spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLfloat) * vertices, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ)); + delete[] vertexData; + + + GLushort* indexData = new GLushort[indices]; + GLushort* index = indexData; + for (int i = 0; i < baseTriangles; i++) { + *(index++) = 0; + *(index++) = i + 2; + *(index++) = i + 1; + } + for (int i = 1; i <= stacks; i++) { + GLushort bottom = i * slices; + GLushort top = bottom + slices; + for (int j = 0; j < slices; j++) { + int next = (j + 1) % slices; + + *(index++) = bottom + j; + *(index++) = top + next; + *(index++) = top + j; + + *(index++) = bottom + j; + *(index++) = bottom + next; + *(index++) = top + next; + } + } + + _spotLightMesh->setIndexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLushort) * indices, (gpu::Byte*) indexData), gpu::Element::INDEX_UINT16)); + delete[] indexData; + + model::Mesh::Part part(0, vertices - 1, 0, model::Mesh::TRIANGLES); + + _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL)); + + _spotLightMesh->makeBufferStream(); + } + return _spotLightMesh; +} + diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index a18add8585..040acd39d5 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -21,6 +21,7 @@ #include "model/Light.h" #include "model/Stage.h" +#include "model/Geometry.h" class AbstractViewStateInterface; class RenderArgs; @@ -95,9 +96,14 @@ private: int lightBufferUnit; int atmosphereBufferUnit; int invViewMat; + int texcoordMat; }; + + model::MeshPointer _spotLightMesh; + model::MeshPointer getSpotLightMesh(); - static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); + // static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); + static void loadLightProgram(const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocations& locations); gpu::PipelinePointer _simpleProgram; gpu::PipelinePointer _simpleProgramCullNone; @@ -106,6 +112,32 @@ private: gpu::PipelinePointer _blitLightBuffer; + gpu::PipelinePointer _directionalSkyboxLight; + LightLocations _directionalSkyboxLightLocations; + gpu::PipelinePointer _directionalSkyboxLightShadowMap; + LightLocations _directionalSkyboxLightShadowMapLocations; + gpu::PipelinePointer _directionalSkyboxLightCascadedShadowMap; + LightLocations _directionalSkyboxLightCascadedShadowMapLocations; + + gpu::PipelinePointer _directionalAmbientSphereLight; + LightLocations _directionalAmbientSphereLightLocations; + gpu::PipelinePointer _directionalAmbientSphereLightShadowMap; + LightLocations _directionalAmbientSphereLightShadowMapLocations; + gpu::PipelinePointer _directionalAmbientSphereLightCascadedShadowMap; + LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations; + + gpu::PipelinePointer _directionalLight; + LightLocations _directionalLightLocations; + gpu::PipelinePointer _directionalLightShadowMap; + LightLocations _directionalLightShadowMapLocations; + gpu::PipelinePointer _directionalLightCascadedShadowMap; + LightLocations _directionalLightCascadedShadowMapLocations; + + gpu::PipelinePointer _pointLight; + LightLocations _pointLightLocations; + gpu::PipelinePointer _spotLight; + LightLocations _spotLightLocations; +/* ProgramObject _directionalSkyboxLight; LightLocations _directionalSkyboxLightLocations; ProgramObject _directionalSkyboxLightShadowMap; @@ -131,7 +163,7 @@ private: LightLocations _pointLightLocations; ProgramObject _spotLight; LightLocations _spotLightLocations; - +*/ class PointLight { public: glm::vec4 position; diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 3e7e9a0adf..b49721d9c7 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -401,7 +401,6 @@ void GeometryCache::renderCone(float base, float height, int slices, int stacks) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - void GeometryCache::renderGrid(int xDivisions, int yDivisions, const glm::vec4& color) { gpu::Batch batch; renderGrid(batch, xDivisions, yDivisions, color); diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index f97ab8a773..83891bbf49 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -301,6 +301,7 @@ private: }; QHash _coneVBOs; + int _nextID; QHash _lastRegisteredQuad3DTexture; diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index ced83da5e9..9a5fdf69ce 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -38,8 +38,9 @@ void ResolveDeferred::run(const SceneContextPointer& sceneContext, const RenderC } RenderDeferredTask::RenderDeferredTask() : Task() { - _jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred"))); _jobs.push_back(Job(new DrawBackground::JobModel("DrawBackground"))); + + _jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred"))); _jobs.push_back(Job(new FetchItems::JobModel("FetchOpaque", FetchItems( [] (const RenderContextPointer& context, int count) { diff --git a/libraries/render-utils/src/deferred_light_limited.slv b/libraries/render-utils/src/deferred_light_limited.slv index e3051d43f7..e657f36598 100644 --- a/libraries/render-utils/src/deferred_light_limited.slv +++ b/libraries/render-utils/src/deferred_light_limited.slv @@ -12,9 +12,23 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include gpu/Transform.slh@> + +<$declareStandardTransform()$> + +uniform mat4 texcoordMat; + void main(void) { - gl_Position = ftransform(); + // gl_Position = ftransform(); + + // standard transform + TransformCamera cam = getTransformCamera(); + TransformObject obj = getTransformObject(); + <$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>; + vec4 projected = gl_Position / gl_Position.w; - gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, - dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); + // gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, + // dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); + gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w, + dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w); } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index e6c3938b71..72aa41344c 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -28,7 +28,8 @@ void main(void) { // Grab the fragment data from the uv vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; DeferredFragment frag = unpackDeferredFragment(texCoord); - + + // Kill if in front of the light volume float depth = frag.depthVal; if (depth < gl_FragCoord.z) {