From f3a6729940d07f8454ba5bb44ab39a1a240784be Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Sun, 6 Aug 2017 23:08:06 -0700 Subject: [PATCH] Fidling around with aa --- .../render-utils/src/AntialiasingEffect.cpp | 17 ++++++----- libraries/render-utils/src/fxaa.slf | 28 ++++++++++++++++++- libraries/render-utils/src/fxaa_blend.slf | 3 ++ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/libraries/render-utils/src/AntialiasingEffect.cpp b/libraries/render-utils/src/AntialiasingEffect.cpp index 1627908beb..ce7c418776 100644 --- a/libraries/render-utils/src/AntialiasingEffect.cpp +++ b/libraries/render-utils/src/AntialiasingEffect.cpp @@ -211,7 +211,8 @@ const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline() { gpu::ShaderPointer program = gpu::Shader::createProgram(vs, ps); gpu::Shader::BindingSet slotBindings; - slotBindings.insert(gpu::Shader::Binding(std::string("colorTexture"), 0)); + slotBindings.insert(gpu::Shader::Binding(std::string("historyTexture"), 0)); + slotBindings.insert(gpu::Shader::Binding(std::string("colorTexture"), 1)); gpu::Shader::makeProgram(*program, slotBindings); @@ -220,10 +221,7 @@ const gpu::PipelinePointer& Antialiasing::getAntialiasingPipeline() { gpu::StatePointer state = gpu::StatePointer(new gpu::State()); PrepareStencil::testMask(*state); - - // state->setDepthTest(false, false, gpu::LESS_EQUAL); - state->setBlendFunction(true, gpu::State::BlendArg::SRC_ALPHA, gpu::State::BlendOp::BLEND_OP_ADD, gpu::State::BlendArg::INV_SRC_ALPHA ); - + // Good to go add the brand new pipeline _antialiasingPipeline = gpu::Pipeline::create(program, state); } @@ -245,6 +243,8 @@ const gpu::PipelinePointer& Antialiasing::getBlendPipeline() { gpu::StatePointer state = gpu::StatePointer(new gpu::State()); PrepareStencil::testMask(*state); + state->setBlendFunction(true, gpu::State::BlendArg::SRC_ALPHA, gpu::State::BlendOp::BLEND_OP_ADD, gpu::State::BlendArg::INV_SRC_ALPHA ); + // Good to go add the brand new pipeline _blendPipeline = gpu::Pipeline::create(program, state); } @@ -262,13 +262,16 @@ void Antialiasing::run(const render::RenderContextPointer& renderContext, const batch.setViewportTransform(args->_viewport); // TAA step - batch.setResourceTexture(0, sourceBuffer->getRenderBuffer(0)); + getAntialiasingPipeline(); + batch.setResourceTexture(0, _antialiasingTexture); + batch.setResourceTexture(1, sourceBuffer->getRenderBuffer(0)); batch.setFramebuffer(_antialiasingBuffer); batch.setPipeline(getAntialiasingPipeline()); batch.draw(gpu::TRIANGLE_STRIP, 4); // Blend step - batch.setResourceTexture(0, _antialiasingTexture); + // batch.setResourceTexture(0, _antialiasingTexture); + batch.setResourceTexture(1, nullptr); batch.setFramebuffer(sourceBuffer); batch.setPipeline(getBlendPipeline()); batch.draw(gpu::TRIANGLE_STRIP, 4); diff --git a/libraries/render-utils/src/fxaa.slf b/libraries/render-utils/src/fxaa.slf index fce9f3b05f..8bbe2887f5 100644 --- a/libraries/render-utils/src/fxaa.slf +++ b/libraries/render-utils/src/fxaa.slf @@ -23,13 +23,39 @@ precision mediump int; #endif uniform sampler2D colorTexture; +uniform sampler2D historyTexture; uniform vec2 texcoordOffset; in vec2 varTexCoord0; out vec4 outFragColor; void main() { - outFragColor = vec4(texture(colorTexture, varTexCoord0).xyz, 0.3); + // outFragColor = vec4(texture(colorTexture, varTexCoord0).xyz, 0.3); + +// v2 + float ModulationFactor = 1.0 / 16.0; + + vec3 CurrentSubpixel = textureLod(colorTexture, varTexCoord0, 0.0).rgb; + vec3 History = textureLod(historyTexture, varTexCoord0, 0.0).rgb; + + vec3 NearColor0 = textureLodOffset(colorTexture, varTexCoord0, 0.0, ivec2(1, 0)).xyz; + vec3 NearColor1 = textureLodOffset(colorTexture, varTexCoord0, 0.0, ivec2(0, 1)).xyz; + vec3 NearColor2 = textureLodOffset(colorTexture, varTexCoord0, 0.0, ivec2(-1, 0)).xyz; + vec3 NearColor3 = textureLodOffset(colorTexture, varTexCoord0, 0.0, ivec2(0, -1)).xyz; + + vec3 BoxMin = min(CurrentSubpixel, min(NearColor0, min(NearColor1, min(NearColor2, NearColor3)))); + vec3 BoxMax = max(CurrentSubpixel, max(NearColor0, max(NearColor1, max(NearColor2, NearColor3))));; + + History = clamp(History, BoxMin, BoxMax); + + //if (gl_FragCoord.x > 800) { + outFragColor.xyz = History; + outFragColor.w = ModulationFactor; + /* } else { + outFragColor.xyz = CurrentSubpixel; + outFragColor.w = 1.0; + + }*/ /* // filter width limit for dependent "two-tap" texture samples float FXAA_SPAN_MAX = 8.0; diff --git a/libraries/render-utils/src/fxaa_blend.slf b/libraries/render-utils/src/fxaa_blend.slf index c38de8cb4e..c42bf5fecf 100644 --- a/libraries/render-utils/src/fxaa_blend.slf +++ b/libraries/render-utils/src/fxaa_blend.slf @@ -21,4 +21,7 @@ uniform sampler2D colorTexture; void main(void) { outFragColor = texture(colorTexture, varTexCoord0); + if (gl_FragCoord.x > 800) { + outFragColor.w = 1.0; + } }