From 2cd2c959407932ee57335856a863027cba15445d Mon Sep 17 00:00:00 2001 From: samcake Date: Tue, 16 May 2017 16:12:31 -0700 Subject: [PATCH] Adding more debugging tool --- .../src/RenderableZoneEntityItem.cpp | 5 +- libraries/model/src/model/Light.h | 1 - .../render-utils/src/BackgroundStage.cpp | 1 - .../src/DeferredLightingEffect.cpp | 29 +++++--- .../render-utils/src/DeferredLightingEffect.h | 1 + libraries/render-utils/src/LightStage.h | 1 - .../render-utils/src/RenderDeferredTask.cpp | 2 +- libraries/render-utils/src/ZoneRenderer.cpp | 68 +++++++++++++++---- libraries/render-utils/src/ZoneRenderer.h | 14 +++- .../render-utils/src/zone_drawAmbient.slf | 44 +++++++++--- .../render-utils/src/zone_drawSkybox.slf | 54 +++++++++++++++ 11 files changed, 178 insertions(+), 42 deletions(-) create mode 100644 libraries/render-utils/src/zone_drawSkybox.slf diff --git a/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp b/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp index 92682d46df..6758134a7e 100644 --- a/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableZoneEntityItem.cpp @@ -354,11 +354,8 @@ void RenderableZoneEntityItem::updateKeyAmbientFromEntity(RenderableZoneEntityIt // Set the keylight - ambientLight->setColor(ColorUtils::toVec3(this->getKeyLightProperties().getColor())); ambientLight->setAmbientIntensity(this->getKeyLightProperties().getAmbientIntensity()); - // ambientLight->setIntensity(this->getKeyLightProperties().getIntensity()); - ambientLight->setDirection(this->getKeyLightProperties().getDirection()); - + if (this->getKeyLightProperties().getAmbientURL().isEmpty()) { keyZonePayload.setAmbientURL(this->getSkyboxProperties().getURL()); } else { diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index ee242bfdf2..4c82eb5d77 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -113,7 +113,6 @@ public: void setIntensity(float intensity); bool isRanged() const { return (getType() == POINT) || (getType() == SPOT); } - bool hasAmbient() const { return (getType() == AMBIENT); } // FalloffRradius is the physical radius of the light sphere through which energy shines, // expressed in meters. It is used only to calculate the falloff curve of the light. diff --git a/libraries/render-utils/src/BackgroundStage.cpp b/libraries/render-utils/src/BackgroundStage.cpp index f3ccfa6485..1a85a70863 100644 --- a/libraries/render-utils/src/BackgroundStage.cpp +++ b/libraries/render-utils/src/BackgroundStage.cpp @@ -138,5 +138,4 @@ void DrawBackgroundStage::run(const render::RenderContextPointer& renderContext, } */ - backgroundStage->_currentFrame.clear(); } \ No newline at end of file diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 71d60df2ae..b799a7c49e 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -146,15 +146,21 @@ void DeferredLightingEffect::init() { auto textureCache = DependencyManager::get(); - QString skyboxUrl { PathUtils::resourcesPath() + "images/Default-Sky-9-cubemap.ktx" }; -// QString skyboxAmbientUrl { PathUtils::resourcesPath() + "images/Default-Sky-9-cubemap.ktx" }; + { + PROFILE_RANGE(render, "Process Default Skybox"); + auto textureCache = DependencyManager::get(); - _defaultSkyboxTexture = _defaultSkyboxAmbientTexture = textureCache->getImageTexture(skyboxUrl, image::TextureUsage::CUBE_TEXTURE, { { "generateIrradiance", false } }); - // _defaultSkyboxAmbientTexture = textureCache->getImageTexture(skyboxAmbientUrl, image::TextureUsage::CUBE_TEXTURE, { { "generateIrradiance", true } }); + auto skyboxUrl = PathUtils::resourcesPath().toStdString() + "images/Default-Sky-9-cubemap.ktx"; - _defaultSkybox->setCubemap(_defaultSkyboxTexture); + _defaultSkyboxTexture = gpu::Texture::unserialize(skyboxUrl); + _defaultSkyboxAmbientTexture = _defaultSkyboxTexture; - lp->setAmbientMap(_defaultSkyboxTexture); + _defaultSkybox->setCubemap(_defaultSkyboxTexture); + } + + + lp->setAmbientIntensity(0.5f); + lp->setAmbientMap(_defaultSkyboxAmbientTexture); } @@ -177,7 +183,7 @@ void DeferredLightingEffect::setupKeyLightBatch(gpu::Batch& batch, int lightBuff if (lightBufferUnit >= 0) { batch.setUniformBuffer(lightBufferUnit, keySunLight->getLightSchemaBuffer()); } - if (keyAmbiLight->hasAmbient() && (ambientBufferUnit >= 0)) { + if (ambientBufferUnit >= 0) { batch.setUniformBuffer(ambientBufferUnit, keyAmbiLight->getAmbientSchemaBuffer()); } @@ -315,15 +321,20 @@ static void loadLightVolumeProgram(const char* vertSource, const char* fragSourc } void DeferredLightingEffect::setGlobalLight(const model::LightPointer& light) { - auto globalLight = _allocatedLights.front(); + /* auto globalLight = _allocatedLights.front(); globalLight->setDirection(light->getDirection()); globalLight->setColor(light->getColor()); globalLight->setIntensity(light->getIntensity()); globalLight->setAmbientIntensity(light->getAmbientIntensity()); globalLight->setAmbientSphere(light->getAmbientSphere()); - globalLight->setAmbientMap(light->getAmbientMap()); + globalLight->setAmbientMap(light->getAmbientMap());*/ } +const model::LightPointer& DeferredLightingEffect::getGlobalLight() const { + return _allocatedLights.front(); +} + + #include model::MeshPointer DeferredLightingEffect::getPointLightMesh() { diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 3a1e7d2b4b..2f4f9901c3 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -53,6 +53,7 @@ public: // update global lighting void setGlobalLight(const model::LightPointer& light); + const model::LightPointer& getGlobalLight() const; const LightStagePointer& getLightStage() { return _lightStage; } const BackgroundStagePointer& getBackgroundStage() { return _backgroundStage; } diff --git a/libraries/render-utils/src/LightStage.h b/libraries/render-utils/src/LightStage.h index 01131cac73..edbdff28fd 100644 --- a/libraries/render-utils/src/LightStage.h +++ b/libraries/render-utils/src/LightStage.h @@ -77,7 +77,6 @@ public: }; using Descs = std::vector; - Index findLight(const LightPointer& light) const; Index addLight(const LightPointer& light); diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index 492875a4ac..21df88d4c4 100644 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -207,7 +207,7 @@ void RenderDeferredTask::build(JobModel& task, const render::Varying& input, ren task.addJob("DrawStatus", opaques, DrawStatus(statusIconMap)); } - task.addJob("DebugZoneLighting"); + task.addJob("DebugZoneLighting", deferredFrameTransform); } diff --git a/libraries/render-utils/src/ZoneRenderer.cpp b/libraries/render-utils/src/ZoneRenderer.cpp index e7bcda2036..c838954b72 100644 --- a/libraries/render-utils/src/ZoneRenderer.cpp +++ b/libraries/render-utils/src/ZoneRenderer.cpp @@ -20,6 +20,7 @@ #include "DeferredLightingEffect.h" #include "zone_drawAmbient_frag.h" +#include "zone_drawSkybox_frag.h" using namespace render; @@ -50,6 +51,9 @@ void ZoneRendererTask::build(JobModel& task, const Varying& input, Varying& oupu void SetupZones::run(const RenderContextPointer& context, const Inputs& inputs) { + auto backgroundStage = DependencyManager::get()->getBackgroundStage(); + backgroundStage->_currentFrame.clear(); + // call render in the correct order first... render::renderItems(context, inputs); @@ -68,37 +72,63 @@ const gpu::PipelinePointer& DebugZoneLighting::getAmbientPipeline() { gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps); gpu::Shader::BindingSet slotBindings; - //slotBindings.insert(gpu::Shader::Binding(std::string("blurParamsBuffer"), BlurTask_ParamsSlot)); - //slotBindings.insert(gpu::Shader::Binding(std::string("sourceMap"), BlurTask_SourceSlot)); + slotBindings.insert(gpu::Shader::Binding(std::string("deferredFrameTransformBuffer"), ZONE_DEFERRED_TRANSFORM_BUFFER)); + slotBindings.insert(gpu::Shader::Binding(std::string("lightAmbientBuffer"), ZONE_AMBIENT_BUFFER)); + slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), ZONE_AMBIENT_MAP)); + gpu::Shader::makeProgram(*program, slotBindings); gpu::StatePointer state = gpu::StatePointer(new gpu::State()); + + state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA); _ambientPipeline = gpu::Pipeline::create(program, state); } return _ambientPipeline; } const gpu::PipelinePointer& DebugZoneLighting::getBackgroundPipeline() { if (!_backgroundPipeline) { + auto vs = gpu::StandardShaderLib::getDrawTransformUnitQuadVS(); + auto ps = gpu::Shader::createPixel(std::string(zone_drawSkybox_frag)); + gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps); + + gpu::Shader::BindingSet slotBindings; + slotBindings.insert(gpu::Shader::Binding(std::string("deferredFrameTransformBuffer"), ZONE_DEFERRED_TRANSFORM_BUFFER)); + slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), ZONE_SKYBOX_MAP)); + + gpu::Shader::makeProgram(*program, slotBindings); + + gpu::StatePointer state = gpu::StatePointer(new gpu::State()); + + state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA); + _backgroundPipeline = gpu::Pipeline::create(program, state); } return _backgroundPipeline; } -void DebugZoneLighting::run(const render::RenderContextPointer& context) { +void DebugZoneLighting::run(const render::RenderContextPointer& context, const Inputs& inputs) { RenderArgs* args = context->args; + auto deferredTransform = inputs; + auto lightStage = DependencyManager::get()->getLightStage(); - const auto light = lightStage->getLight(0); model::LightPointer keyAmbiLight; if (lightStage && lightStage->_currentFrame._ambientLights.size()) { keyAmbiLight = lightStage->getLight(lightStage->_currentFrame._ambientLights.front()); } else { - // keyAmbiLight = _allocatedLights[_globalLights.front()]; + keyAmbiLight = DependencyManager::get()->getGlobalLight(); } -/* if (lightBufferUnit >= 0) { - batch.setUniformBuffer(lightBufferUnit, keySunLight->getLightSchemaBuffer()); - }*/ + auto backgroundStage = DependencyManager::get()->getBackgroundStage(); + model::SkyboxPointer skybox; + if (backgroundStage && backgroundStage->_currentFrame._backgrounds.size()) { + auto background = backgroundStage->getBackground(backgroundStage->_currentFrame._backgrounds.front()); + if (background) { + skybox = background->getSkybox(); + } + } else { + skybox = DependencyManager::get()->getDefaultSkybox(); + } gpu::doInBatch(args->_context, [=](gpu::Batch& batch) { @@ -108,21 +138,29 @@ void DebugZoneLighting::run(const render::RenderContextPointer& context) { batch.resetViewTransform(); Transform model; - model.setTranslation(glm::vec3(0.0, 0.0, -10.0)); - batch.setModelTransform(model); + model.setTranslation(glm::vec3(-4.0, 0.0, -10.0)); + + batch.setUniformBuffer(ZONE_DEFERRED_TRANSFORM_BUFFER, deferredTransform->getFrameTransformBuffer()); batch.setPipeline(getAmbientPipeline()); + batch.setModelTransform(model); if (keyAmbiLight) { - if (keyAmbiLight->hasAmbient()) { - batch.setUniformBuffer(0, keyAmbiLight->getAmbientSchemaBuffer()); - } + batch.setUniformBuffer(ZONE_AMBIENT_BUFFER, keyAmbiLight->getAmbientSchemaBuffer()); if (keyAmbiLight->getAmbientMap()) { - batch.setResourceTexture(0, keyAmbiLight->getAmbientMap()); + batch.setResourceTexture(ZONE_AMBIENT_MAP, keyAmbiLight->getAmbientMap()); } } - batch.draw(gpu::TRIANGLE_STRIP, 4); + batch.setPipeline(getBackgroundPipeline()); + model.setTranslation(glm::vec3(-4.0, 3.0, -10.0)); + batch.setModelTransform(model); + if (skybox) { + batch.setResourceTexture(ZONE_SKYBOX_MAP, skybox->getCubemap()); + } + batch.draw(gpu::TRIANGLE_STRIP, 4); + + }); } diff --git a/libraries/render-utils/src/ZoneRenderer.h b/libraries/render-utils/src/ZoneRenderer.h index 1ca34892c9..ee3754b7eb 100644 --- a/libraries/render-utils/src/ZoneRenderer.h +++ b/libraries/render-utils/src/ZoneRenderer.h @@ -14,6 +14,8 @@ #include "render/Engine.h" +#include "DeferredFrameTransform.h" + class ZoneRendererConfig : public render::Task::Config { Q_OBJECT Q_PROPERTY(int maxDrawn MEMBER maxDrawn NOTIFY dirty) @@ -52,14 +54,22 @@ protected: class DebugZoneLighting { public: - using JobModel = render::Job::Model; + using Inputs = DeferredFrameTransformPointer; + using JobModel = render::Job::ModelI; DebugZoneLighting() {} - void run(const render::RenderContextPointer& context); + void run(const render::RenderContextPointer& context, const Inputs& inputs); protected: + enum Slots { + ZONE_DEFERRED_TRANSFORM_BUFFER = 0, + ZONE_AMBIENT_BUFFER, + ZONE_AMBIENT_MAP, + ZONE_SKYBOX_MAP, + }; + gpu::PipelinePointer _keyLightPipeline; gpu::PipelinePointer _ambientPipeline; gpu::PipelinePointer _backgroundPipeline; diff --git a/libraries/render-utils/src/zone_drawAmbient.slf b/libraries/render-utils/src/zone_drawAmbient.slf index aff4bcb47f..f14bed5c03 100644 --- a/libraries/render-utils/src/zone_drawAmbient.slf +++ b/libraries/render-utils/src/zone_drawAmbient.slf @@ -9,14 +9,15 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include DeferredTransform.slh@> +<$declareDeferredFrameTransform()$> + <@include model/Light.slh@> <@include LightingModel.slh@> - !> <$declareLightAmbientBuffer()$> <@include LightAmbient.slh@> - !> <$declareLightingAmbient(_SCRIBE_NULL, 1, _SCRIBE_NULL, _SCRIBE_NULL)$> @@ -25,15 +26,42 @@ out vec4 _fragColor; void main(void) { + const float INNER_RADIUS = 1.0; + const float INNER_RADIUS2 = INNER_RADIUS * INNER_RADIUS; + const float OUTER_RADIUS = 1.05; + const float OUTER_RADIUS2 = OUTER_RADIUS * OUTER_RADIUS; + vec2 sphereUV = (varTexCoord0.xy * 2.0 - vec2(1.0)) * OUTER_RADIUS; + float sphereR2 = dot(sphereUV.xy, sphereUV.xy); + if (sphereR2 > OUTER_RADIUS * OUTER_RADIUS) { + discard; + } + if (sphereR2 > INNER_RADIUS2) { + float falloff = (sphereR2 - OUTER_RADIUS2) / (OUTER_RADIUS2 - INNER_RADIUS2); + _fragColor = vec4(0.0, 0.0, 0.0, falloff * falloff); + return; + } + vec3 spherePos = normalize(vec3(sphereUV, sqrt(1.0 - sphereR2))); + + + vec3 fragNormal = vec3(getViewInverse() * vec4(spherePos, 0.0)); + + LightAmbient lightAmbient = getLightAmbient(); - - // _fragColor = vec4(varTexCoord0, 0.0, 1.0); - float z = sqrt( dot(varTexCoord0.xy, varTexCoord0.xy)); - vec3 dir = vec3(varTexCoord0.xy, z); - vec3 ambient = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(lightAmbient), dir).xyz; - _fragColor = vec4(ambient, 1.0); + float roughness = 0.1; + float levels = getLightAmbientMapNumMips(lightAmbient); + float lod = min(((roughness)* levels), levels); + vec3 ambientMap = evalSkyboxLight(fragNormal, lod).xyz; + vec3 ambientSH = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(lightAmbient), fragNormal).xyz; + + // vec3 ambient = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(lightAmbient), fragNormal).xyz; + // _fragColor = vec4( 0.5 * (fragNormal + vec3(1.0)), 1.0); + + vec3 ambient = (sphereUV.x > 0 ? ambientMap : ambientSH); + + const float INV_GAMMA_22 = 1.0 / 2.2; + _fragColor = vec4(pow(ambient, vec3(INV_GAMMA_22)), 1.0); } diff --git a/libraries/render-utils/src/zone_drawSkybox.slf b/libraries/render-utils/src/zone_drawSkybox.slf new file mode 100644 index 0000000000..77b144379b --- /dev/null +++ b/libraries/render-utils/src/zone_drawSkybox.slf @@ -0,0 +1,54 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// Created by Sam Gateau on 5/16/17. +// Copyright 2017 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 DeferredTransform.slh@> +<$declareDeferredFrameTransform()$> + +// declareSkyboxMap +uniform samplerCube skyboxMap; + +vec4 evalSkyboxLight(vec3 direction, float lod) { + // textureQueryLevels is not available until #430, so we require explicit lod + // float mipmapLevel = lod * textureQueryLevels(skyboxMap); + return textureLod(skyboxMap, direction, lod); +} + +in vec2 varTexCoord0; +out vec4 _fragColor; + +void main(void) { + + const float INNER_RADIUS = 1.0; + const float INNER_RADIUS2 = INNER_RADIUS * INNER_RADIUS; + const float OUTER_RADIUS = 1.05; + const float OUTER_RADIUS2 = OUTER_RADIUS * OUTER_RADIUS; + vec2 sphereUV = (varTexCoord0.xy * 2.0 - vec2(1.0)) * OUTER_RADIUS; + float sphereR2 = dot(sphereUV.xy, sphereUV.xy); + if (sphereR2 > OUTER_RADIUS * OUTER_RADIUS) { + discard; + } + if (sphereR2 > INNER_RADIUS2) { + float falloff = (sphereR2 - OUTER_RADIUS2) / (OUTER_RADIUS2 - INNER_RADIUS2); + _fragColor = vec4(0.0, 0.0, 0.0, falloff * falloff); + return; + } + vec3 spherePos = normalize(vec3(sphereUV, -sqrt(1.0 - sphereR2))); + + vec3 fragNormal = vec3(getViewInverse() * vec4(spherePos, 0.0)); + + float lod = 0; + vec3 ambient = evalSkyboxLight(fragNormal, lod).xyz; + + const float INV_GAMMA_22 = 1.0 / 2.2; + _fragColor = vec4(pow(ambient, vec3(INV_GAMMA_22)), 1.0); +} + +