From 05661c600724fe71c4a804d00013959ab9e1be47 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Wed, 14 Aug 2024 18:20:06 -0700 Subject: [PATCH] fix haze in mirrors --- libraries/render-utils/src/DrawHaze.cpp | 42 +++++++++++++++---- libraries/render-utils/src/DrawHaze.h | 8 +++- libraries/render-utils/src/Haze.slf | 12 +++++- .../render-utils/src/RenderDeferredTask.cpp | 2 +- .../render-utils/src/render-utils/haze.slp | 1 + 5 files changed, 54 insertions(+), 11 deletions(-) diff --git a/libraries/render-utils/src/DrawHaze.cpp b/libraries/render-utils/src/DrawHaze.cpp index 012161ec03..71845be731 100644 --- a/libraries/render-utils/src/DrawHaze.cpp +++ b/libraries/render-utils/src/DrawHaze.cpp @@ -31,6 +31,10 @@ namespace gr { using graphics::slot::buffer::Buffer; } +gpu::PipelinePointer DrawHaze::_hazePipeline = nullptr; +gpu::PipelinePointer DrawHaze::_separateHazePipeline = nullptr; +gpu::PipelinePointer DrawHaze::_separateHazeBackgroundPipeline = nullptr; + void DrawHaze::run(const render::RenderContextPointer& renderContext, const Inputs& inputs) { const auto hazeFrame = inputs.get0(); const auto& hazeStage = renderContext->args->_scene->getStage(); @@ -55,15 +59,29 @@ void DrawHaze::run(const render::RenderContextPointer& renderContext, const Inpu if (!_hazePipeline) { gpu::ShaderPointer program = gpu::Shader::createProgram(shader::render_utils::program::haze); - gpu::StatePointer state = std::make_shared(); - state->setBlendFunction(true, - gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, - gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); + auto createState = []() { + gpu::StatePointer state = std::make_shared(); + state->setBlendFunction(true, + gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, + gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); + return state; + }; // Mask out haze on the tablet - PrepareStencil::testMask(*state); - _hazePipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state)); + auto hazeState = createState(); + PrepareStencil::testMask(*hazeState); + _hazePipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, hazeState)); + + // For our separated passes, we perform one pass on anything marked shape, and one on just the background + auto hazeSeparatedState = createState(); + PrepareStencil::testShape(*hazeSeparatedState); + _separateHazePipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, hazeSeparatedState)); + + gpu::ShaderPointer backgroundProgram = gpu::Shader::createProgram(shader::render_utils::program::haze_background); + auto hazeBackgroundSeparatedState = createState(); + PrepareStencil::testBackground(*hazeBackgroundSeparatedState); + _separateHazeBackgroundPipeline = gpu::PipelinePointer(gpu::Pipeline::create(backgroundProgram, hazeBackgroundSeparatedState)); } auto outputFramebufferSize = glm::ivec2(outputBuffer->getSize()); @@ -77,7 +95,6 @@ void DrawHaze::run(const render::RenderContextPointer& renderContext, const Inpu batch.resetViewTransform(); batch.setModelTransform(gpu::Framebuffer::evalSubregionTexcoordTransform(outputFramebufferSize, args->_viewport)); - batch.setPipeline(_hazePipeline); batch.setUniformBuffer(graphics::slot::buffer::Buffer::HazeParams, haze->getHazeParametersBuffer()); batch.setUniformBuffer(ru::Buffer::DeferredFrameTransform, transformBuffer->getFrameTransformBuffer()); @@ -92,6 +109,15 @@ void DrawHaze::run(const render::RenderContextPointer& renderContext, const Inpu } } - batch.draw(gpu::TRIANGLE_STRIP, 4); + if (!_separateBackgroundPass) { + batch.setPipeline(_hazePipeline); + batch.draw(gpu::TRIANGLE_STRIP, 4); + } else { + batch.setPipeline(_separateHazePipeline); + batch.draw(gpu::TRIANGLE_STRIP, 4); + + batch.setPipeline(_separateHazeBackgroundPipeline); + batch.draw(gpu::TRIANGLE_STRIP, 4); + } }); } diff --git a/libraries/render-utils/src/DrawHaze.h b/libraries/render-utils/src/DrawHaze.h index 9236543068..82e8beaa9b 100644 --- a/libraries/render-utils/src/DrawHaze.h +++ b/libraries/render-utils/src/DrawHaze.h @@ -33,10 +33,16 @@ public: using Inputs = render::VaryingSet6; using JobModel = render::Job::ModelI; + DrawHaze(bool separateBackgroundPass) : _separateBackgroundPass(separateBackgroundPass) {} + void run(const render::RenderContextPointer& renderContext, const Inputs& inputs); private: - gpu::PipelinePointer _hazePipeline; + bool _separateBackgroundPass { false }; + + static gpu::PipelinePointer _hazePipeline; + static gpu::PipelinePointer _separateHazePipeline; + static gpu::PipelinePointer _separateHazeBackgroundPipeline; }; #endif // hifi_render_utils_DrawHaze_h diff --git a/libraries/render-utils/src/Haze.slf b/libraries/render-utils/src/Haze.slf index 899f36b8eb..4f230ff642 100644 --- a/libraries/render-utils/src/Haze.slf +++ b/libraries/render-utils/src/Haze.slf @@ -21,10 +21,16 @@ <@include graphics/Haze.slh@> +<@if not HIFI_USE_BACKGROUND@> LAYOUT(binding=RENDER_UTILS_TEXTURE_HAZE_LINEAR_DEPTH) uniform sampler2D linearDepthMap; +<@endif@> vec4 unpackPositionFromZeye(vec2 texcoord) { +<@if not HIFI_USE_BACKGROUND@> float Zeye = -texture(linearDepthMap, texcoord).x; +<@else@> + float Zeye = 1.0; // We just want to get the direction first +<@endif@> float check = float(isStereo()); float check2 = check * float(texcoord.x > 0.5); @@ -44,8 +50,12 @@ void main(void) { } vec4 fragPositionES = unpackPositionFromZeye(varTexCoord0); - mat4 viewInverse = getViewInverse(); + +<@if HIFI_USE_BACKGROUND@> + fragPositionES = vec4(-32000.0 * normalize(fragPositionES.xyz), 1.0); +<@endif@> + vec4 fragPositionWS = viewInverse * fragPositionES; vec4 eyePositionWS = viewInverse[3]; diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index 4bcfc9663b..b8bc9a542b 100644 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -225,7 +225,7 @@ void RenderDeferredTask::build(JobModel& task, const render::Varying& input, ren task.addJob("DrawBackgroundDeferred", backgroundInputs); const auto drawHazeInputs = render::Varying(DrawHaze::Inputs(hazeFrame, lightingFramebuffer, linearDepthTarget, deferredFrameTransform, lightingModel, lightFrame)); - task.addJob("DrawHazeDeferred", drawHazeInputs); + task.addJob("DrawHazeDeferred", drawHazeInputs, depth > 0); // Render transparent objects forward in LightingBuffer const auto transparentsInputs = RenderTransparentDeferred::Inputs(transparents, hazeFrame, lightFrame, lightingModel, lightClusters, shadowFrame, jitter).asVarying(); diff --git a/libraries/render-utils/src/render-utils/haze.slp b/libraries/render-utils/src/render-utils/haze.slp index 805b855c8d..c469834837 100644 --- a/libraries/render-utils/src/render-utils/haze.slp +++ b/libraries/render-utils/src/render-utils/haze.slp @@ -1,2 +1,3 @@ VERTEX gpu::vertex::DrawViewportQuadTransformTexcoord FRAGMENT Haze +DEFINES background:f \ No newline at end of file