Skip to content

Commit

Permalink
Updated denoising, improved highlights and vignette correction, chang…
Browse files Browse the repository at this point in the history
…ed default capture sharpening
  • Loading branch information
eszdman committed Nov 8, 2023
1 parent ffe9f9d commit c500519
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 47 deletions.
38 changes: 28 additions & 10 deletions app/src/main/assets/shaders/esd3d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ uniform sampler2D NoiseMap;
uniform ivec2 size;
uniform vec2 mapsize;
uniform int yOffset;
uniform float noiseS;
uniform float noiseO;
out vec4 Output;

#define SIGMA 10.0
Expand All @@ -23,24 +25,38 @@ out vec4 Output;
#define INTENSE 1.0
#define PI 3.1415926535897932384626433832795
#import gaussian
float normpdf(in float x, in float sigma)
{
return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
}
float normpdf3(in vec3 v, in float sigma)
{
return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;
}
float normpdf2(in vec2 v, in float sigma)
{
return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;
}

float lum(in vec4 color) {
return length(color.xyz);
}

float atan2(in float y, in float x) {
bool s = (abs(x) > abs(y));
return mix(PI/2.0 - atan(x,y), atan(y,x), s);
return mix(PI/2.0 - atan(x,y+0.00001), atan(y,x+0.00001), s);
}

void main() {
ivec2 xy = ivec2(gl_FragCoord.xy);
xy+=ivec2(0,yOffset);
vec3 cin = vec3(texelFetch(InputBuffer, xy, 0).rgb);
float noisefactor = texture(NoiseMap, vec2(xy)/vec2(INSIZE)).g;
float noisefactor = dot(cin,vec3(0.1,0.8,0.1));
{
vec3 final_colour = vec3(0.0);
float sigX = 2.5;
float sigY = sqrt(noisefactor*NOISES + NOISEO);
float sigY = (noisefactor*noisefactor*NOISES + NOISEO + 0.0000001);
//float sigY = 1.0;
//sigY = max(0.01,sigY);
//create the 1-D kernel
float Z = 0.0;
Expand All @@ -49,10 +65,10 @@ void main() {
float sum = 0.0001;
vec2 baseGrad = texelFetch(GradBuffer, xy, 0).rg;
for (int i=-KSIZE; i <= KSIZE; i++){
float k0 = pdf(float(i)/sigX);
float k0 = normpdf(float(i),sigX);
for (int j=-KSIZE; j <= KSIZE; j++){
vec2 temp = texelFetch(GradBuffer, xy+ivec2(i,j), 0).rg;
float k = pdf(float(j)/sigX)*k0*pdf((temp-baseGrad)/sigY);
float k = normpdf(float(j),sigX)*k0*normpdf2((temp-baseGrad),1.0);
temp *= k;
dxy+=temp;
dxyabs+=abs(temp);
Expand All @@ -62,7 +78,7 @@ void main() {
dxy/=sum;
dxyabs/=sum;

float angle = atan2(dxy.x,dxy.y) + 3.14f/2.f;
float angle = atan2(dxy.x,dxy.y);
vec3 cc;
float factor;
float sino = sin(angle);
Expand All @@ -73,9 +89,9 @@ void main() {
float pwrful = clamp(length(dxy)*2.5,0.0,1.0);
vec2 sigo = vec2(3.5*1.5*(1.0 + pwrful),3.5/(4.35*(1.0 + pwrful)));
//Improve energy
sigo*=min(sqrt(sigY)*15.1,1.0);
//sigo*=min(sqrt(sigY)*15.1,1.0);
sigo*=sigo;
sigo*=1.0+sigY;
//sigo*=1.0+sigY;

float a = (coso)/(2.0*sigo.x) + (sino)/(2.0*sigo.y);
float b = -(sin2o)/(4.0*sigo.x) + (sin2o)/(4.0*sigo.y);
Expand All @@ -86,8 +102,10 @@ void main() {
for (int j=-KSIZE; j <= KSIZE; ++j)
{
cc = vec3(texelFetch(InputBuffer, xy+ivec2(i,j), 0).rgb);
factor = pdf((cc-cin)/sigY)*
fastExp(
float diff = length(abs(cc-cin));
factor = //normpdf3((cc-cin),sigY)*
(1.0-diff*diff/(diff*diff + sigY)) *
pdf(
(a*float(i*i) +
2.0*b*float(i*j) +
c*float(j*j))
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/assets/shaders/exposebayer2.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ vec4 reinhard_extended(vec4 v, float max_white)
vec4 numerator = v * (vec4(1.0f) + (v / vec4(max_white * max_white)));
return numerator / (vec4(1.0f) + v);
}

float stddev(vec3 XYZ) {
float avg = (XYZ.r + XYZ.g + XYZ.b) / 3.;
vec3 diff = XYZ - avg;
Expand Down Expand Up @@ -65,6 +66,8 @@ void main() {
result.g = stddev(v3);

v3 = brIn(inp,STRHIGH);


br = luminocity(v3);
br = gammaEncode(clamp(br-DH,0.0,1.0));
result.b = br;
Expand Down
45 changes: 26 additions & 19 deletions app/src/main/assets/shaders/initial.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ uniform sampler2D GammaCurve;
uniform sampler2D LookupTable;
uniform sampler2D FusionMap;
uniform sampler2D IntenseCurve;
uniform sampler2D GainMap;

//uniform vec3 neutralPoint;
//uniform float saturation0;
Expand Down Expand Up @@ -249,9 +250,17 @@ vec3 saturate(vec3 rgb, float sat2, float sat) {
}
#define TONEMAPSWITCH (0.05)
#define TONEMAPAMP (1.0)

vec3 reinhard_extended(vec3 v, float max_white)
{
vec3 numerator = v * (vec3(1.0f) + (v / vec3(max_white * max_white)));
return numerator / (vec3(1.0f) + v);
}

vec3 applyColorSpace(vec3 pRGB,float tonemapGain){
vec3 neutralPoint = vec3(NEUTRALPOINT);
pRGB = clamp(pRGB, vec3(0.0), neutralPoint);
//pRGB = clamp(reinhard_extended(pRGB*tonemapGain,max(1.0,tonemapGain)), vec3(0.0), neutralPoint);
pRGB *= tonemapGain;
#if CCT == 0
mat3 corr = intermediateToSRGB;
#endif
Expand All @@ -270,15 +279,7 @@ vec3 applyColorSpace(vec3 pRGB,float tonemapGain){
#endif
pRGB = corr*sensorToIntermediate*pRGB;

pRGB = clamp(pRGB,0.0,1.0);

//pRGB/=pRGB.r+pRGB.g+pRGB.b;
//pRGB*=br*MINP;
//pRGB = rgbToHsluv(pRGB);
float br = (pRGB.r+pRGB.g+pRGB.b)/3.0;
pRGB/=br;
br=clamp(br*tonemapGain,0.0,1.0);
pRGB*=br;
pRGB = clamp(reinhard_extended(pRGB,max(1.0,tonemapGain*2.0)),vec3(0.0),vec3(1.0));

//ISO tint correction
//pRGB = mix(vec3(pRGB.r*0.99*(TINT2),pRGB.g*(TINT),pRGB.b*1.025*(TINT2)),pRGB,clamp(br*10.0,0.0,1.0));
Expand All @@ -305,6 +306,8 @@ float getLm(ivec2 coordsShift){
vec3 inrgb = texelFetch(InputBuffer, coordsShift, 0).rgb;
return inrgb.r+inrgb.g+inrgb.b;
}


void main() {
ivec2 xy = ivec2(gl_FragCoord.xy);
xy = mirrorCoords(xy,activeSize);
Expand All @@ -314,8 +317,8 @@ void main() {

float tonemapGain = 1.f;
#if FUSION == 1
vec2 grad = vec2(texelFetch(InputBuffer, xy+ivec2(-1,0), 0).g-texelFetch(InputBuffer, xy+ivec2(1,0), 0).g,
texelFetch(InputBuffer, xy+ivec2(0,-1), 0).g-texelFetch(InputBuffer, xy+ivec2(0,1), 0).g);
//vec2 grad = vec2(texelFetch(InputBuffer, xy+ivec2(-1,0), 0).g-texelFetch(InputBuffer, xy+ivec2(1,0), 0).g,
//texelFetch(InputBuffer, xy+ivec2(0,-1), 0).g-texelFetch(InputBuffer, xy+ivec2(0,1), 0).g);
/*tonemapGain = textureBicubic(FusionMap, vec2(xy+ivec2(0,0))/vec2(textureSize(InputBuffer, 0))).r/(sRGB.r+sRGB.g+sRGB.b);
t=texelFetch(InputBuffer, xy+ivec2(0,2), 0).rgb;
tonemapGain += textureBicubic(FusionMap, vec2(xy+ivec2(0,2))/vec2(textureSize(InputBuffer, 0))).r/(t.r+t.g+t.b);
Expand Down Expand Up @@ -388,16 +391,19 @@ void main() {
float brCorrected = 0.f;
float blurInitial = 0.f;
float blurCorrected = 0.f;

for(int i = -1; i<=2;i++){
for(int j = -1; j<=2;j++){
blurInitial += texelFetch(InputBuffer, xy+ivec2(i,j), 0).g;
blurCorrected += texelFetch(InputBuffer, xy+ivec2(i,j), 0).g*getGain(vec2(ivec2(i,j)));
float v = dot(texelFetch(InputBuffer, xy+ivec2(i,j), 0).rgb, vec3(1.f));
blurInitial += v;
blurCorrected += v*getGain(vec2(ivec2(i,j)));
}
}
float detail = texelFetch(InputBuffer, xy, 0).g-blurInitial;

float detail = dot(texelFetch(InputBuffer, xy, 0).rgb,vec3(1.f))-blurInitial;
float brightening = blurCorrected/(blurInitial+EPS);
float corrected = blurCorrected + detail*brightening;
tonemapGain = corrected/(texelFetch(InputBuffer, xy, 0).g+EPS);
tonemapGain = corrected/(dot(texelFetch(InputBuffer, xy, 0).rgb,vec3(1.f))+EPS);
//tonemapGain = texture(FusionMap, (gl_FragCoord.xy)/vec2(INSIZE)).r;
//tonemapGain = getGain(vec2(0.0,0.0));
//if(tonemapGain > 0.0){
Expand All @@ -406,17 +412,18 @@ void main() {
//float mixG = (sRGB.g-minImg+0.001)/(maxImg-minImg+0.001);
//mixG = clamp(mixG,0.0,1.0);
//tonemapGain = mix(maxG,minG,mixG);
tonemapGain = mix(1.0,tonemapGain,texture(IntenseCurve, vec2(sRGB.g,0.0)).r);
tonemapGain = mix(1.0,tonemapGain,texture(IntenseCurve, vec2(dot(sRGB.rgb,vec3(1.0/3.0)),0.0)).r);
#endif

vec4 gains = textureBicubicHardware(GainMap, vec2(xy)/vec2(textureSize(InputBuffer, 0)));
tonemapGain *= (gains.r+gains.g+gains.b+gains.a)/4.0;
float br = (sRGB.r+sRGB.g+sRGB.b)/3.0;
sRGB = applyColorSpace(sRGB,tonemapGain);
//sRGB = clamp(sRGB,0.0,1.0);
#if LUT == 1
//sRGB = lookup(sRGB);
#endif
//Rip Shadowing applied
br = (clamp(br-0.0008,0.0,0.007)*(1.0/0.007));
//br = (clamp(br-0.0008,0.0,0.007)*(1.0/0.007));
//br*= (clamp(3.0-sRGB.r+sRGB.g+sRGB.b,0.0,0.006)*(1.0/0.006));


Expand Down
12 changes: 6 additions & 6 deletions app/src/main/assets/shaders/lsharpening2.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ void main() {
float avr = 0.0;
for(int i = -2; i<=2;i++){
for(int j = -2; j<=2;j++){
vec4 temp = texelFetch(InputBuffer, mirrorCoords2(xy+ivec2(i,j), ivec2(INSIZE)), 0);
edges[(i+2)*5 + j + 2] = temp.g;
MIN = min(temp.g,MIN);
MAX = max(temp.g,MAX);
avr+=temp.g;
float temp = dot(texelFetch(InputBuffer, mirrorCoords2(xy+ivec2(i,j), ivec2(INSIZE)), 0).rgb,vec3(0.1,0.8,0.1));
edges[(i+2)*5 + j + 2] = temp;
MIN = min(temp,MIN);
MAX = max(temp,MAX);
avr+=temp;
}
}
avr/=25.0;
Expand Down Expand Up @@ -65,6 +65,6 @@ void main() {
//float W2 = 1.0-pdf((Output.g/ksum - center.g)/N);
//W*=W2;
//W = max(W,-0.90/ksum);
Output = (Output*W - center.g*W*ksum)/(W*ksum + 1.0) + center.rgb;
Output = (Output*W - dot(center.rgb,vec3(0.1,0.8,0.1))*W*ksum)/(W*ksum + 1.0) + center.rgb;
Output = clamp(Output,0.0,1.0);
}
5 changes: 5 additions & 0 deletions app/src/main/assets/shaders/tofloat.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ out vec3 Output;
#else
out float Output;
#endif



void main() {
ivec2 xy = ivec2(gl_FragCoord.xy) - ivec2(OFFSET);
ivec2 fact = (xy)%2;
Expand All @@ -36,6 +39,8 @@ void main() {
float balance;
vec4 gains = textureBicubicHardware(GainMap, vec2(xy)/vec2(RawSize));
gains.rgb = vec3(gains.r,(gains.g+gains.b)/2.0,gains.a);
gains.rgb /= dot(gains.rgb,vec3(1.0/3.0));
gains.rgb = vec3(1.f);
vec3 level = vec3(blackLevel.r,(blackLevel.g+blackLevel.b)/2.0,blackLevel.a);
#if RGBLAYOUT == 1
//Output = vec3(texelFetch(InputBuffer, (xy+ivec2(0,0)), 0).rgb)/float(whitelevel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GLBasePipeline implements AutoCloseable {
private long timeStart;
private static final String TAG = "BasePipeline";
private final int[] bind = new int[1];
public GLTexture main1,main2,main3,main4;
public GLTexture main1,main2,main3,main4, main5;
public Settings mSettings;
public Parameters mParameters;
public Properties mProp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ public void Run() {
((PostPipeline) basePipeline).GainMap = GainMapTex;
glProg.closed = true;
in.close();
GainMapTex.close();
//GainMapTex.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import com.particlesdevs.photoncamera.processing.render.NoiseModeler;

public class ESD3D extends Node {

public ESD3D() {
boolean needClose = false;
public ESD3D(boolean closing) {
super("", "ES3D");
needClose = closing;
}

@Override
Expand All @@ -19,7 +20,8 @@ public void Compile() {

@Override
public void Run() {
GLTexture map = glUtils.medianDown(previousNode.WorkingTexture,4);
if(basePipeline.main4 == null)
basePipeline.main4 = glUtils.medianDown(previousNode.WorkingTexture,4);
GLTexture grad;

if(previousNode.WorkingTexture != basePipeline.main3){
Expand All @@ -39,14 +41,18 @@ public void Run() {
Log.d(Name, "NoiseS:" + basePipeline.noiseS + ", NoiseO:" + basePipeline.noiseO);
glProg.setDefine("NOISES", basePipeline.noiseS);
glProg.setDefine("NOISEO", basePipeline.noiseO);
glProg.setDefine("INSIZE", previousNode.WorkingTexture.mSize);

glProg.setDefine("INSIZE", basePipeline.mParameters.rawSize);
glProg.useAssetProgram("esd3d");
glProg.setTexture("NoiseMap", map);
glProg.setTexture("NoiseMap", basePipeline.main4);
glProg.setTexture("InputBuffer", previousNode.WorkingTexture);
glProg.setTexture("GradBuffer", grad);
glProg.drawBlocks(WorkingTexture);
}
glProg.closed = true;
map.close();
if(needClose) {
basePipeline.main4.close();
basePipeline.main4 = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void Run() {
glProg.setTexture("GammaCurve",GammaTexture);
glProg.setTexture("InputBuffer",super.previousNode.WorkingTexture);
glProg.setTexture("IntenseCurve",interpolatedCurve);
//glProg.setTexture("GainMap", ((PostPipeline)basePipeline).GainMap);
glProg.setTexture("GainMap", ((PostPipeline)basePipeline).GainMap);
//glProg.setVar("toneMapCoeffs", Converter.CUSTOM_ACR3_TONEMAP_CURVE_COEFFS);
glProg.setVar("sensorToIntermediate",basePipeline.mParameters.sensorToProPhoto);
glProg.setVar("intermediateToSRGB",cct);
Expand All @@ -265,5 +265,6 @@ public void Run() {
//glProg.setVar("saturation",0.f);
//WorkingTexture = new GLTexture(super.previousNode.WorkingTexture.mSize,new GLFormat(GLFormat.DataType.FLOAT_16, GLConst.WorkDim),null);
WorkingTexture = basePipeline.getMain();
//((PostPipeline)basePipeline).GainMap.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public Bitmap Run(ByteBuffer inBuffer, Parameters parameters) {
noiseS = 0.f;
}
noiseO = Math.max(noiseO, Float.MIN_NORMAL);
noiseS = Math.max(noiseS, Float.MIN_NORMAL);
Point rawSliced = parameters.rawSize;
if (PhotonCamera.getSettings().aspect169) {
if (rawSliced.x > rawSliced.y) {
Expand Down Expand Up @@ -144,7 +145,8 @@ private void BuildDefaultPipeline() {
if (PhotonCamera.getSettings().hdrxNR) {
if (nightMode)
add(new Wavelet());
add(new ESD3D());
add(new ESD3D(false));
add(new ESD3D(true));
}

//add(new AWB());
Expand Down
Binary file modified app/src/main/jniLibs/arm64-v8a/libhdrxgpu.so
Binary file not shown.
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sat Oct 28 17:34:10 MSK 2023
VERSION_BUILD=15914
#Thu Nov 09 00:57:27 MSK 2023
VERSION_BUILD=16078

0 comments on commit c500519

Please sign in to comment.