Commit 782e1682 authored by Rye Mutt's avatar Rye Mutt 🍞
Browse files

Add DLS sharpening shader

parent e922a057
......@@ -49,6 +49,7 @@ static LLStaticHashedString tone_lottes_b("tone_lottes_b");
static LLStaticHashedString tone_uncharted_a("tone_uncharted_a");
static LLStaticHashedString tone_uncharted_b("tone_uncharted_b");
static LLStaticHashedString tone_uncharted_c("tone_uncharted_c");
static LLStaticHashedString sharpen_params("sharpen_params");
ALRenderUtil::ALRenderUtil()
{
......@@ -60,6 +61,9 @@ ALRenderUtil::ALRenderUtil()
gSavedSettings.getControl("RenderToneMapLottesB")->getSignal()->connect(boost::bind(&ALRenderUtil::setupTonemap, this));
gSavedSettings.getControl("RenderToneMapUchimuraA")->getSignal()->connect(boost::bind(&ALRenderUtil::setupTonemap, this));
gSavedSettings.getControl("RenderToneMapUchimuraB")->getSignal()->connect(boost::bind(&ALRenderUtil::setupTonemap, this));
gSavedSettings.getControl("RenderSharpenMethod")->getSignal()->connect(boost::bind(&ALRenderUtil::setupSharpen, this));
gSavedSettings.getControl("RenderSharpenCASParams")->getSignal()->connect(boost::bind(&ALRenderUtil::setupSharpen, this));
gSavedSettings.getControl("RenderSharpenDLSParams")->getSignal()->connect(boost::bind(&ALRenderUtil::setupSharpen, this));
}
void ALRenderUtil::restoreVertexBuffers()
......@@ -99,6 +103,7 @@ void ALRenderUtil::refreshState()
{
setupTonemap();
setupColorGrade();
setupSharpen();
}
bool ALRenderUtil::setupTonemap()
......@@ -325,4 +330,97 @@ void ALRenderUtil::renderTonemap(LLRenderTarget* src, LLRenderTarget* dst)
gGL.popMatrix();
gGL.matrixMode(LLRender::MM_MODELVIEW);
gGL.popMatrix();
}
bool ALRenderUtil::setupSharpen()
{
if (LLPipeline::sRenderDeferred)
{
mSharpenMethod = gSavedSettings.getU32("RenderSharpenMethod");
if (mSharpenMethod >= SHARPEN_COUNT)
{
mSharpenMethod = ALSharpen::SHARPEN_NONE;
}
LLGLSLShader* sharpen_shader = nullptr;
switch (mSharpenMethod)
{
case ALSharpen::SHARPEN_CAS:
{
sharpen_shader = &gDeferredPostCASProgram;
sharpen_shader->bind();
LLVector3 params = gSavedSettings.getVector3("RenderSharpenCASParams");
params.clamp(LLVector3::zero, LLVector3::all_one);
sharpen_shader->uniform3fv(sharpen_params, 1, params.mV);
sharpen_shader->unbind();
break;
}
case ALSharpen::SHARPEN_DLS:
{
sharpen_shader = &gDeferredPostDLSProgram;
sharpen_shader->bind();
LLVector3 params = gSavedSettings.getVector3("RenderSharpenDLSParams");
params.clamp(LLVector3::zero, LLVector3::all_one);
sharpen_shader->uniform3fv(sharpen_params, 1, params.mV);
sharpen_shader->unbind();
break;
}
default:
case ALSharpen::SHARPEN_NONE:
break;
}
}
else
{
mSharpenMethod = ALSharpen::SHARPEN_NONE;
}
return true;
}
void ALRenderUtil::renderSharpen(LLRenderTarget* src, LLRenderTarget* dst)
{
if (mSharpenMethod == ALSharpen::SHARPEN_NONE)
{
return;
}
LLGLSLShader* sharpen_shader = nullptr;
switch (mSharpenMethod)
{
case ALSharpen::SHARPEN_CAS:
sharpen_shader = &gDeferredPostCASProgram;
break;
case ALSharpen::SHARPEN_DLS:
sharpen_shader = &gDeferredPostDLSProgram;
break;
default:
case ALSharpen::SHARPEN_NONE:
break;
}
LLGLDepthTest depth(GL_FALSE, GL_FALSE);
LLGLDisable srgb(GL_FRAMEBUFFER_SRGB);
// Bind setup:
if (dst)
{
dst->bindTarget();
}
sharpen_shader->bind();
// Draw
src->bindTexture(0, 0, LLTexUnit::TFO_POINT);
gGL.getTexUnit(0)->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
mRenderBuffer->setBuffer(LLVertexBuffer::MAP_VERTEX);
mRenderBuffer->drawArrays(LLRender::TRIANGLES, 0, 3);
if (dst)
{
dst->flush();
}
sharpen_shader->unbind();
gGL.getTexUnit(0)->disable();
}
\ No newline at end of file
......@@ -62,10 +62,22 @@ class ALRenderUtil
TONEMAP_COUNT
};
bool setupTonemap();
bool setupColorGrade();
void renderTonemap(LLRenderTarget* src, LLRenderTarget* dst);
enum ALSharpen : uint32_t
{
SHARPEN_NONE = 0,
SHARPEN_CAS,
SHARPEN_DLS,
SHARPEN_COUNT
};
bool setupSharpen();
void renderSharpen(LLRenderTarget* src, LLRenderTarget* dst);
// End Deferred Only
bool setupColorGrade();
U32 getSharpenMethod() { return mSharpenMethod; };
private:
// Parameters
......@@ -81,6 +93,8 @@ class ALRenderUtil
LLVector3 mToneUnchartedParamB;
LLVector3 mToneUnchartedParamC;
U32 mSharpenMethod = ALSharpen::SHARPEN_NONE;
// Texture Data
U32 mCGLut;
LLVector4 mCGLutSize;
......
......@@ -596,28 +596,6 @@
<key>Value</key>
<integer>0</integer>
</map>
<key>AlchemyRenderCAS</key>
<map>
<key>Comment</key>
<string>Enable Contrast Adaptive Sharpening in Deferred(AA Required)</string>
<key>Persist</key>
<integer>1</integer>
<key>Type</key>
<string>Boolean</string>
<key>Value</key>
<integer>1</integer>
</map>
<key>AlchemyRenderCASSharpness</key>
<map>
<key>Comment</key>
<string>0-1 Maximum Sharpening for CAS</string>
<key>Persist</key>
<integer>1</integer>
<key>Type</key>
<string>F32</string>
<key>Value</key>
<real>0.8</real>
</map>
<key>AlchemyRenderSMAA</key>
<map>
<key>Comment</key>
......@@ -706,6 +684,47 @@
<key>Value</key>
<string></string>
</map>
<key>RenderSharpenMethod</key>
<map>
<key>Comment</key>
<string>Sharpening Method - 0 (None), 1 (Contrast Adaptive Sharpening), 2 (DLS)</string>
<key>Persist</key>
<integer>1</integer>
<key>Type</key>
<string>U32</string>
<key>Value</key>
<integer>1</integer>
</map>
<key>RenderSharpenCASParams</key>
<map>
<key>Comment</key>
<string>x (Sharpness 0-1), y (Contrast 0-1) z (UNUSED)</string>
<key>Persist</key>
<integer>1</integer>
<key>Type</key>
<string>Vector3</string>
<key>Value</key>
<array>
<real>1.0</real>
<real>0.6</real>
<real>0.0</real>
</array>
</map>
<key>RenderSharpenDLSParams</key>
<map>
<key>Comment</key>
<string>x (Sharpness 0-1), y (Denoise 0-1) z (UNUSED)</string>
<key>Persist</key>
<integer>1</integer>
<key>Type</key>
<string>Vector3</string>
<key>Value</key>
<array>
<real>0.6</real>
<real>0.17</real>
<real>0.0</real>
</array>
</map>
<key>RenderToneMapExposure</key>
<map>
<key>Comment</key>
......
......@@ -2,7 +2,7 @@
* @file CASF.glsl
*
* $LicenseInfo:firstyear=2021&license=viewerlgpl$
* Second Life Viewer Source Code
* Alchemy Viewer Source Code
* Copyright (C) 2021, Rye Mutt<rye@alchemyviewer.org>
*
* This library is free software; you can redistribute it and/or
......@@ -19,7 +19,6 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
......@@ -38,7 +37,7 @@ VARYING vec2 vary_fragcoord;
uniform sampler2D tex0;
uniform float sharpness;
uniform vec3 sharpen_params = vec3(1.0, 0.5, 0.0);
vec3 linear_to_srgb(vec3 cl);
......@@ -98,7 +97,8 @@ void main()
// Shaping amount of sharpening.
ampRGB = inversesqrt(ampRGB);
float peak = 8.0 - 3.0 * sharpness;
float peak = 8.0 - 3.0 * sharpen_params.y;
vec3 wRGB = -vec3(1)/(ampRGB * peak);
vec3 rcpWeightRGB = vec3(1)/(1.0 + 4.0 * wRGB);
......@@ -107,7 +107,8 @@ void main()
// 0 w 0
vec3 window = (b + d) + (f + h);
vec3 outColor = clamp((window * wRGB + e) * rcpWeightRGB,0,1);
vec3 outColor = clamp((window * wRGB + e) * rcpWeightRGB, 0, 1);
outColor = mix(e, outColor, sharpen_params.x);
frag_color = vec4(linear_to_srgb(outColor),alpha);
}
......
/**
* @file DLSF.glsl
*
* $LicenseInfo:firstyear=2021&license=viewerlgpl$
* Alchemy Viewer Source Code
* Copyright (C) 2021, Rye Mutt<rye@alchemyviewer.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#extension GL_ARB_shader_texture_lod : enable
#extension GL_EXT_gpu_shader4 : enable
/*[EXTRA_CODE_HERE]*/
#ifdef DEFINE_GL_FRAGCOLOR
out vec4 frag_color;
#else
#define frag_color gl_FragColor
#endif
VARYING vec2 vary_fragcoord;
uniform sampler2D tex0;
uniform vec3 sharpen_params;
// highlight fall-off start (prevents halos and noise in bright areas)
#define kHighBlock 0.65
// offset reducing sharpening in the shadows
#define kLowBlock (1.0 / 256.0)
#define kSharpnessMin (-1.0 / 14.0)
#define kSharpnessMax (-1.0 / 6.5)
#define kDenoiseMin (0.001)
#define kDenoiseMax (-0.1)
vec3 linear_to_srgb(vec3 cl);
vec3 srgb_to_linear(vec3 cl);
float GetLuma(vec4 p)
{
return 0.212655 * p.r + 0.715158 * p.g + 0.072187 * p.b;
}
float Square(float v)
{
return v * v;
}
void main()
{
/**
Image sharpening filter from GeForce Experience. Provided by NVIDIA Corporation.
Copyright 2019 Suketu J. Shah. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided
that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
vec4 x = texture2DLod(tex0, vary_fragcoord, 0.f);
vec4 a = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2(-1, 0));
vec4 b = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2( 1, 0));
vec4 c = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2( 0, 1));
vec4 d = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2( 0, -1));
vec4 e = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2(-1, -1));
vec4 f = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2( 1, 1));
vec4 g = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2(-1, 1));
vec4 h = texture2DLodOffset(tex0, vary_fragcoord, 0.0, ivec2( 1, -1));
float lx = GetLuma(x);
float la = GetLuma(a);
float lb = GetLuma(b);
float lc = GetLuma(c);
float ld = GetLuma(d);
float le = GetLuma(e);
float lf = GetLuma(f);
float lg = GetLuma(g);
float lh = GetLuma(h);
// cross min/max
float ncmin = min(min(le, lf), min(lg, lh));
float ncmax = max(max(le, lf), max(lg, lh));
// plus min/max
float npmin = min(min(min(la, lb), min(lc, ld)), lx);
float npmax = max(max(max(la, lb), max(lc, ld)), lx);
// compute "soft" local dynamic range -- average of 3x3 and plus shape
float lmin = 0.5 * min(ncmin, npmin) + 0.5 * npmin;
float lmax = 0.5 * max(ncmax, npmax) + 0.5 * npmax;
// compute local contrast enhancement kernel
float lw = lmin / (lmax + kLowBlock);
float hw = Square(1.0 - Square(max(lmax - kHighBlock, 0.0) / ((1.0 - kHighBlock))));
// noise suppression
// Note: Ensure that the denoiseFactor is in the range of (10, 1000) on the CPU-side prior to launching this shader.
// For example, you can do so by adding these lines
// const float kDenoiseMin = 0.001f;
// const float kDenoiseMax = 0.1f;
// float kernelDenoise = 1.0 / (kDenoiseMin + (kDenoiseMax - kDenoiseMin) * min(max(denoise, 0.0), 1.0));
// where kernelDenoise is the value to be passed in to this shader (the amount of noise suppression is inversely proportional to this value),
// denoise is the value chosen by the user, in the range (0, 1)
float kernelDenoise = 1.0 / (kDenoiseMin + (kDenoiseMax - kDenoiseMin) * sharpen_params.y);
float nw = Square((lmax - lmin) * kernelDenoise);
// pick conservative boost
float boost = min(min(lw, hw), nw);
// run variable-sigma 3x3 sharpening convolution
// Note: Ensure that the sharpenFactor is in the range of (-1.0/14.0, -1.0/6.5f) on the CPU-side prior to launching this shader.
// For example, you can do so by adding these lines
// const float kSharpnessMin = -1.0 / 14.0;
// const float kSharpnessMax = -1.0 / 6.5f;
// float kernelSharpness = kSharpnessMin + (kSharpnessMax - kSharpnessMin) * min(max(sharpen, 0.0), 1.0);
// where kernelSharpness is the value to be passed in to this shader,
// sharpen is the value chosen by the user, in the range (0, 1)
float kernelSharpness = kSharpnessMin + (kSharpnessMax - kSharpnessMin) * sharpen_params.x;
float k = boost * kernelSharpness;
float accum = lx;
accum += la * k;
accum += lb * k;
accum += lc * k;
accum += ld * k;
accum += le * (k * 0.5);
accum += lf * (k * 0.5);
accum += lg * (k * 0.5);
accum += lh * (k * 0.5);
// normalize (divide the accumulator by the sum of convolution weights)
accum /= 1.0 + 6.0 * k;
// accumulator is in linear light space
float delta = accum - lx;
x.x += delta;
x.y += delta;
x.z += delta;
x.rgb = linear_to_srgb(x.rgb);
frag_color = x;
}
......@@ -245,6 +245,7 @@ LLGLSLShader gDeferredSkinnedFullbrightShinyProgram;
LLGLSLShader gDeferredSkinnedFullbrightProgram;
LLGLSLShader gNormalMapGenProgram;
LLGLSLShader gDeferredPostCASProgram;
LLGLSLShader gDeferredPostDLSProgram;
LLGLSLShader gDeferredPostTonemapProgram[AL_TONEMAP_COUNT];
LLGLSLShader gDeferredPostColorGradeLUTProgram[AL_TONEMAP_COUNT];
// [RLVa:KB] - @setsphere
......@@ -833,6 +834,7 @@ void LLViewerShaderMgr::unloadShaders()
gDeferredSkinnedAlphaWaterProgram.unload();
gDeferredPostCASProgram.unload();
gDeferredPostDLSProgram.unload();
for (U32 i = 0; i < AL_TONEMAP_COUNT; ++i)
{
gDeferredPostTonemapProgram[i].unload();
......@@ -1299,6 +1301,7 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
}
gDeferredPostCASProgram.unload();
gDeferredPostDLSProgram.unload();
for (U32 i = 0; i < AL_TONEMAP_COUNT; ++i)
{
gDeferredPostTonemapProgram[i].unload();
......@@ -2984,6 +2987,17 @@ BOOL LLViewerShaderMgr::loadShadersDeferred()
success = gDeferredPostCASProgram.createShader(NULL, NULL);
}
if (success)
{
gDeferredPostDLSProgram.mName = "DLS Shader";
gDeferredPostDLSProgram.mFeatures.hasSrgb = true;
gDeferredPostDLSProgram.mShaderFiles.clear();
gDeferredPostDLSProgram.mShaderFiles.push_back(make_pair("alchemy/postNoTCV.glsl", GL_VERTEX_SHADER));
gDeferredPostDLSProgram.mShaderFiles.push_back(make_pair("alchemy/DLSF.glsl", GL_FRAGMENT_SHADER));
gDeferredPostDLSProgram.mShaderLevel = mShaderLevel[SHADER_DEFERRED];
success = gDeferredPostDLSProgram.createShader(NULL, NULL);
}
for (U32 i = 0; i < AL_TONEMAP_COUNT; ++i)
{
if (success)
......
......@@ -331,6 +331,7 @@ extern LLGLSLShader gDeferredSkinnedFullbrightShinyProgram;
extern LLGLSLShader gDeferredSkinnedFullbrightProgram;
extern LLGLSLShader gNormalMapGenProgram;
extern LLGLSLShader gDeferredPostCASProgram;
extern LLGLSLShader gDeferredPostDLSProgram;
extern LLGLSLShader gDeferredPostTonemapProgram[AL_TONEMAP_COUNT];
extern LLGLSLShader gDeferredPostColorGradeLUTProgram[AL_TONEMAP_COUNT];
// [RLVa:KB] - @setsphere
......
......@@ -299,7 +299,6 @@ static LLStaticHashedString sDistFactor("dist_factor");
static LLStaticHashedString sKern("kern");
static LLStaticHashedString sKernScale("kern_scale");
static LLStaticHashedString sSmaaRTMetrics("SMAA_RT_METRICS");
static LLStaticHashedString sSharpness("sharpness");
//----------------------------------------
std::string gPoolNames[] =
......@@ -8196,7 +8195,8 @@ void LLPipeline::renderFinalize()
}
static LLCachedControl<bool> use_smaa(gSavedSettings, "AlchemyRenderSMAA", true);
static LLCachedControl<bool> enable_cas(gSavedSettings, "AlchemyRenderCAS", true);
bool sharpen_enabled = mALRenderUtil->getSharpenMethod() != ALRenderUtil::ALSharpen::SHARPEN_NONE;
if (use_smaa && gGLManager.mGLVersion >= 3.1)
{
mFXAABuffer.copyContents(*pRenderBuffer, 0, 0, mFXAABuffer.getWidth(), mFXAABuffer.getHeight(), 0, 0,
......@@ -8301,13 +8301,13 @@ void LLPipeline::renderFinalize()
bound_shader->bind();
bound_shader->uniform4fv(sSmaaRTMetrics, 1, rt_metrics);
if (enable_cas)
if (sharpen_enabled)
{
bound_target->bindTarget();
bound_target->clear(GL_COLOR_BUFFER_BIT);
}
drawFullScreenRect();
if (enable_cas)
if (sharpen_enabled)
{
bound_target->flush();
}
......@@ -8379,13 +8379,13 @@ void LLPipeline::renderFinalize()
2.f / width * scale_x,
2.f / height * scale_y);
if (enable_cas)
if (sharpen_enabled)
{
bound_target->bindTarget();
bound_target->clear(GL_COLOR_BUFFER_BIT);
}
drawFullScreenRect();
if (enable_cas)
if (sharpen_enabled)
{
bound_target->flush();
}
......@@ -8397,26 +8397,8 @@ void LLPipeline::renderFinalize()
gGL.getTexUnit(channel)->disable();
}
if (enable_cas)
{
static LLCachedControl<F32> sharpness_cc(gSavedSettings, "AlchemyRenderCASSharpness", 0.8f);
LLGLDisable srgb(GL_FRAMEBUFFER_SRGB);
LLRenderTarget* previous_target = bound_target;
// Bind setup:
bound_shader = &gDeferredPostCASProgram;
// Draw
previous_target->bindTexture(0, 0, LLTexUnit::TFO_POINT);
gGL.getTexUnit(0)->setTextureColorSpace(LLTexUnit::TCS_LINEAR);
bound_shader->bind();
bound_shader->uniform1f(sSharpness, sharpness_cc);
drawFullScreenRect();
bound_shader->unbind();
gGL.getTexUnit(0)->disable();
mALRenderUtil->renderSharpen(bound_target, nullptr);
}
gGLViewport[0] = gViewerWindow->getWorldViewRectRaw().mLeft;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment