Skip to content

Commit

Permalink
atan2 and smartDenoise
Browse files Browse the repository at this point in the history
  • Loading branch information
patriciogonzalezvivo committed Oct 14, 2023
1 parent e72f62e commit 0a4b69b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
70 changes: 70 additions & 0 deletions filter/smartDeNoise.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
original_author: Michele Morrone
description: |
Fast glsl spatial deNoise filter, with circular gaussian kernel and smart/flexible/adaptable -> full configurable
* Standard Deviation sigma radius
* K factor sigma coefficient
* Edge sharpening threshold
More about it at https://github.com/BrutPitt/glslSmartDeNoise/
use: <vec4> smartDeNoise(<sampler2D> tex, <vec2> uv, <vec2> pixel, <float> sigma, <float> kSigma, <float> threshold)
options:
- SMARTDENOISE_TYPE
- SMARTDENOISE_SAMPLER_FNC(TEX, UV)
- SAMPLER_FNC(TEX, UV)
license: |
BSD 2-Clause License
Copyright (c) 2018-2020 Michele Morrone
All rights reserved.
*/

#include "../sample.glsl"
#include "../math/const.glsl"

#ifndef SMARTDENOISE_TYPE
#define SMARTDENOISE_TYPE vec4
#endif

#ifndef SMARTDENOISE_SAMPLER_FNC
#define SMARTDENOISE_SAMPLER_FNC(TEX, UV) SAMPLER_FNC(TEX, UV)
#endif

#ifndef FNC_SMARTDENOISE
#define FNC_SMARTDENOISE

SMARTDENOISE_TYPE smartDeNoise(SAMPLER_TYPE tex, vec2 uv, vec2 pixel, float sigma, float kSigma, float threshold) {
float radius = floor(kSigma*sigma + 0.5);
float radQ = radius * radius;

float invSigmaQx2 = 0.5 / (sigma * sigma); // 1.0 / (sigma^2 * 2.0)
float invSigmaQx2PI = INV_PI * invSigmaQx2; // 1.0 / (sqrt(PI) * sigma)

float invThresholdSqx2 = 0.5 / (threshold * threshold); // 1.0 / (sigma^2 * 2.0)
float invThresholdSqrt2PI = INV_SQRT_TAU / threshold; // 1.0 / (sqrt(2*PI) * sigma)

SMARTDENOISE_TYPE centrPx = SMARTDENOISE_SAMPLER_FNC(tex, uv);

float zBuff = 0.0;
SMARTDENOISE_TYPE aBuff = SMARTDENOISE_TYPE(0.0);
for(float x=-radius; x <= radius; x++) {
// circular kernel
float pt = sqrt(radQ-x*x);
for(float y=-pt; y <= pt; y++) {
vec2 d = vec2(x,y);

// gaussian factor
float blurFactor = exp( -dot(d , d) * invSigmaQx2 ) * invSigmaQx2PI;
SMARTDENOISE_TYPE walkPx = SMARTDENOISE_SAMPLER_FNC(tex,uv+d*pixel);

// adaptive
SMARTDENOISE_TYPE dC = walkPx-centrPx;
float deltaFactor = exp( -dot(dC, dC) * invThresholdSqx2) * invThresholdSqrt2PI * blurFactor;

zBuff += deltaFactor;
aBuff += deltaFactor*walkPx;
}
}
return aBuff/zBuff;
}

#endif
15 changes: 15 additions & 0 deletions math/atan2.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "const.glsl"

/*
original_author: Alexander Griffis
description: |
The range here in degrees is 0 to pi (0-180 degrees) and -pi to 0 (181 to 359 degrees
More about it at https://github.com/Yaazarai/GLSL-ATAN2-DOC
use: <float> atan2(<float>y, <float> x)
*/

#ifndef FNC_ATAN2
#define FNC_ATAN2
float atan2(float y, float x) { return mod(atan(y,x) + PI, TAU); }
#endif

0 comments on commit 0a4b69b

Please sign in to comment.