Skip to content

Commit

Permalink
Critical graphics bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMoroz committed Jan 3, 2021
1 parent f24118b commit 2e1f7e3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 173 deletions.
4 changes: 2 additions & 2 deletions game_folder/shaders/compute/main/Shading_step.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void main() {
float td = dot(dir.xyz, sph.xyz - pos.xyz);//traveled distance
pos = sph;
dir.w += td;

vec3 color = shading(pos, dir, fovray, illuminationDirect, vec3(vec2(global_pos)*res_ratio, 2.*td*fovray/res_ratio));
vec3 direct = bilinear_surface(illuminationDirect, dir.w, 2.*td*fovray/res_ratio, vec2(global_pos)*res_ratio).xyz;
vec3 color = shading(pos, dir, fovray, direct);

if(!isnan(color.x) && !isnan(color.y) && !isnan(color.z))
{
Expand Down
13 changes: 12 additions & 1 deletion game_folder/shaders/compute/post_processing/TXAA.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ shared vec4 de_sph[group_size][group_size];

#define blur 0.05

vec4 interp_bicubic(vec2 coord)
{
ivec2 i = ivec2(coord);
vec2 d = coord - floor(coord);
vec4 p0 = cubic(val(color_HDR0, i, -1,-1), val(color_HDR0, i, 0,-1), val(color_HDR0, i, 1,-1), val(color_HDR0, i, 2,-1), d.x);
vec4 p1 = cubic(val(color_HDR0, i, -1, 0), val(color_HDR0, i, 0, 0), val(color_HDR0, i, 1, 0), val(color_HDR0, i, 2, 0), d.x);
vec4 p2 = cubic(val(color_HDR0, i, -1, 1), val(color_HDR0, i, 0, 1), val(color_HDR0, i, 1, 1), val(color_HDR0, i, 2, 1), d.x);
vec4 p3 = cubic(val(color_HDR0, i, -1, 2), val(color_HDR0, i, 0, 2), val(color_HDR0, i, 1, 2), val(color_HDR0, i, 2, 2), d.x);
return abs(cubic(p0, p1, p2, p3, d.y));
}

void main() {
ivec2 global_pos = ivec2(gl_GlobalInvocationID.xy);
ivec2 local_indx = ivec2(gl_LocalInvocationID.xy);
Expand All @@ -64,7 +75,7 @@ void main() {

//getting the previous frame pixel and sampling it bicubically
vec2 lastCoord = reproject(pos.xyz + td*dir.xyz, vec2(global_pos)/img_size);
vec4 lastColor = interp_bicubic(color_HDR0, lastCoord);
vec4 lastColor = interp_bicubic(lastCoord);

vec3 antialiased = vec3(0.);
antialiased = lastColor.xyz;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ vec3 decodePalYuv(vec3 yuv)
return rgb; // gamma correction
}

vec4 interp_bicubic(vec2 coord)
{
ivec2 i = ivec2(coord);
vec2 d = coord - floor(coord);
vec4 p0 = cubic(val(HDR0, i, -1,-1), val(HDR0, i, 0,-1), val(HDR0, i, 1,-1), val(HDR0, i, 2,-1), d.x);
vec4 p1 = cubic(val(HDR0, i, -1, 0), val(HDR0, i, 0, 0), val(HDR0, i, 1, 0), val(HDR0, i, 2, 0), d.x);
vec4 p2 = cubic(val(HDR0, i, -1, 1), val(HDR0, i, 0, 1), val(HDR0, i, 1, 1), val(HDR0, i, 2, 1), d.x);
vec4 p3 = cubic(val(HDR0, i, -1, 2), val(HDR0, i, 0, 2), val(HDR0, i, 1, 2), val(HDR0, i, 2, 2), d.x);
return abs(cubic(p0, p1, p2, p3, d.y));
}

void main() {
ivec2 global_pos = ivec2(gl_GlobalInvocationID.xy);
ivec2 local_indx = ivec2(gl_LocalInvocationID.xy);
Expand All @@ -55,7 +66,7 @@ void main() {
//getting the previous frame pixel and sampling it bicubically
vec2 lastCoord = reproject(pos.xyz, (vec2(global_pos))/img_size);

vec4 lastColor = interp_bicubic(HDR0, clamp(lastCoord/res_ratio,vec2(2.),img_size-2.));
vec4 lastColor = interp_bicubic(clamp(lastCoord/res_ratio,vec2(2.),img_size-2.));
vec4 lastPos = interp(DE_previous, round(lastCoord));

ray pr = get_ray(PrevCamera, clamp(lastCoord,vec2(0.),imageSize(DE_input)-1.)/imageSize(DE_input));
Expand Down
173 changes: 18 additions & 155 deletions game_folder/shaders/compute/utility/interpolation.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,184 +19,47 @@ float sq(vec4 x)
}


#define val(T, a, b, c) imageLoad(T, a+ivec2(b,c))

//better to use a sampler though
vec4 interp(layout (rgba32f) image2D text, vec2 coord)
{
//coord += vec2(0.5);
ivec2 ci = ivec2(coord);
vec2 d = coord - floor(coord);
return (imageLoad(text, ci)*(1-d.x)*(1-d.y) +
imageLoad(text, ci+ivec2(1,0))*d.x*(1-d.y) +
imageLoad(text, ci+ivec2(0,1))*(1-d.x)*d.y +
imageLoad(text, ci+ivec2(1,1))*d.x*d.y);
}
#define sample_quad(T, X) mat4(val(T, ivec2(X), 0, 0),val(T, ivec2(X), 1, 0),val(T, ivec2(X), 0, 1),val(T, ivec2(X), 1, 1))
#define coords_quad(X) ((vec4(1, 0, 1, 0) + vec4(-1, 1, -1, 1)*vec4((X).x - floor((X).x)))*(vec4(1, 1, 0, 0) + vec4(-1, -1, 1, 1)*vec4((X).y - floor((X).y))))

#define interp(T,X) (sample_quad(T,X)*coords_quad(X))

vec4 cubic(vec4 p0, vec4 p1, vec4 p2, vec4 p3, float x)
{
return p1 + 0.5 * x*(p2 - p0 + x*(2.0*p0 - 5.0*p1 + 4.0*p2 - p3 + x*(3.0*(p1 - p2) + p3 - p0)));
}

vec4 val(layout (rgba32f) image2D T, ivec2 a, int b, int c)
{
return imageLoad(T, a+ivec2(b,c));
}

vec4 interp_bicubic(layout (rgba32f) image2D T, vec2 coord)
{
ivec2 i = ivec2(coord);
vec2 d = coord - floor(coord);
vec4 p0 = cubic(val(T, i, -1,-1), val(T, i, 0,-1), val(T, i, 1,-1), val(T, i, 2,-1), d.x);
vec4 p1 = cubic(val(T, i, -1, 0), val(T, i, 0, 0), val(T, i, 1, 0), val(T, i, 2, 0), d.x);
vec4 p2 = cubic(val(T, i, -1, 1), val(T, i, 0, 1), val(T, i, 1, 1), val(T, i, 2, 1), d.x);
vec4 p3 = cubic(val(T, i, -1, 2), val(T, i, 0, 2), val(T, i, 1, 2), val(T, i, 2, 2), d.x);
return abs(cubic(p0, p1, p2, p3, d.y));
}

vec4 bicubic_surface(layout (rgba32f) image2D T, float td, float sz, vec2 coord)
{
ivec2 ic = ivec2(coord);
vec2 d = coord - floor(coord);
//load data
vec4 data[4][4];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
{
data[i][j] = val(T, ic, i-1, j-1);
}

//3d distance interpolation
vec4 datai[4][4];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
{
vec4 sum = vec4(0);
float k = 0.;
for(int ii = 0; ii < 4; ii++)
for(int jj = 0; jj < 4; jj++)
{
float c = 1./( sq(td - data[ii][jj].w) + sq(sz)*(sq(vec2(i-ii,j-jj)) + 0.001) );
sum += data[ii][jj]*c;
k += c;
}
datai[i][j] = sum/k;
}

//2d bicubic
vec4 p0 = cubic(datai[0][0], datai[1][0], datai[2][0], datai[3][0], d.x);
vec4 p1 = cubic(datai[0][1], datai[1][1], datai[2][1], datai[3][1], d.x);
vec4 p2 = cubic(datai[0][2], datai[1][2], datai[2][2], datai[3][2], d.x);
vec4 p3 = cubic(datai[0][3], datai[1][3], datai[2][3], datai[3][3], d.x);
return cubic(p0, p1, p2, p3, d.y);
}

vec4 interp_sharp(layout (rgba32f) image2D text, vec2 coord, float sharpness)
{
//coord += vec2(0.5);
ivec2 ci = ivec2(coord);
vec2 d = coord - floor(coord);
float b0 = tanh(0.5*sharpness);
vec2 k = (tanh(sharpness*(d - 0.5))+b0)*0.5/b0;
vec4 r1 = mix(imageLoad(text, ci), imageLoad(text, ci+ivec2(1,0)), k.x);
vec4 r2 = mix(imageLoad(text, ci+ivec2(0,1)), imageLoad(text, ci+ivec2(1,1)), k.x);
vec4 c = mix(r1, r2, k.y);
return c;
}

//2d interpolation that is aware of the 3d positions of our points
vec4 bilinear_surface(layout (rgba32f) image2D text, float td, float sz, vec2 coord)
vec4 bilinear_surface_interp(mat4 quad, float td, float sz, vec2 coord)
{
ivec2 ci = ivec2(coord);
vec2 d = coord - floor(coord);

vec4 A1 = imageLoad(text, ci);
vec4 A2 = imageLoad(text, ci+ivec2(1,0));
vec4 A3 = imageLoad(text, ci+ivec2(0,1));
vec4 A4 = imageLoad(text, ci+ivec2(1,1));

float td1 = A1.w;
float td2 = A2.w;
float td3 = A3.w;
float td4 = A4.w;
float td1 = quad[0].w;
float td2 = quad[1].w;
float td3 = quad[2].w;
float td4 = quad[3].w;

float w1 = (1-d.x)*(1-d.y)/(sz*sz+(td-td1)*(td-td1));
float w2 = (d.x)*(1-d.y)/(sz*sz+(td-td2)*(td-td2));
float w3 = (1-d.x)*(d.y)/(sz*sz+(td-td3)*(td-td3));
float w4 = (d.x)*(d.y)/(sz*sz+(td-td4)*(td-td4));

//a fix for gamma ruining the interpolation
return pow((pow(A1,vec4(1.f/gamma_camera))*w1 + pow(A2,vec4(1.f/gamma_camera))*w2 + pow(A3,vec4(1.f/gamma_camera))*w3 + pow(A4,vec4(1.f/gamma_camera))*w4)/(w1+w2+w3+w4),vec4(gamma_camera));
return quad*vec4(w1,w2,w3,w4)/(w1+w2+w3+w4);
}

#define bilinear_surface(T, td, sz, coord) bilinear_surface_interp(sample_quad(T,coord),td,sz,coord)

vec4 gm(vec4 a)
{
return vec4(pow(a.xyz,vec3(1.f/gamma_camera)),a.w);
}


//2d interpolation which modulates the color by the normal
vec4 bilinear_surface_enhance(layout (rgba32f) image2D T, layout (rgba32f) image2D N, float td, float sz, float enh, vec3 normal, vec2 coord)
{
ivec2 ci = ivec2(coord);
vec2 d = coord - floor(coord);

vec4 A1 = gm(imageLoad(T, ci));
vec4 A2 = gm(imageLoad(T, ci+ivec2(1,0)));
vec4 A3 = gm(imageLoad(T, ci+ivec2(0,1)));
vec4 A4 = gm(imageLoad(T, ci+ivec2(1,1)));

vec3 N1 = imageLoad(N, ci).xyz;
vec3 N2 = imageLoad(N, ci+ivec2(1,0)).xyz;
vec3 N3 = imageLoad(N, ci+ivec2(0,1)).xyz;
vec3 N4 = imageLoad(N, ci+ivec2(1,1)).xyz;

////color(normal) remodulation
vec3 Navg = 0.25*(N2+N4+N1+N3);
vec4 Aavg = 0.25*(A1+A2+A3+A4);
vec3 dNx = 0.5*(N2+N4-N1-N3);
vec3 dNy = 0.5*(N3+N4-N1-N2);

vec2 D = vec2(dot(Camera.dirx, dNx),dot(Camera.diry, dNy));

vec3 dAx = 0.5*(A2.xyz+A4.xyz-A1.xyz-A3.xyz)*D.x/(sq(D.x) + 0.01);
vec3 dAy = 0.5*(A3.xyz+A4.xyz-A1.xyz-A2.xyz)*D.y/(sq(D.y) + 0.01);

vec3 dN = normal - Navg;
vec3 colormod = Aavg.xyz*tanh(80*(dot(Camera.dirx, dN)*dAx + dot(Camera.diry, dN)*dAy)/Aavg.xyz);
////

float w1 = (1-d.x)*(1-d.y)/(sq(sz)*(1. + 50.*sq(normal - N1))+sq(td-A1.w));
float w2 = (d.x)*(1-d.y)/(sq(sz)*(1. + 50.*sq(normal - N2))+sq(td-A2.w));
float w3 = (1-d.x)*(d.y)/(sq(sz)*(1. + 50.*sq(normal - N3))+sq(td-A3.w));
float w4 = (d.x)*(d.y)/(sq(sz)*(1. + 50.*sq(normal - N4))+sq(td-A4.w));

//a fix for gamma ruining the interpolation
return pow((A1*w1 + A2*w2 + A3*w3 + A4*w4)/(w1+w2+w3+w4) + enh*vec4(colormod,0.),vec4(gamma_camera));
}

//image part sampling functions
vec4 subImage(layout (rgba32f) image2D T, ivec2 coord, ivec2 sub, ivec2 subsize)
{
ivec2 imgsize = imageSize(T);
ivec2 subnum = imgsize/subsize;
return imageLoad(T, clamp(coord, ivec2(1), subsize-1) + sub*subsize);
}

void storeSub(layout (rgba32f) image2D T, vec4 val, ivec2 coord, ivec2 sub, ivec2 subsize)
{
ivec2 imgsize = imageSize(T);
ivec2 subnum = imgsize/subsize;
imageStore(T, clamp(coord, ivec2(1), subsize-1) + sub*subsize, val);
}

vec4 subInterp(layout (rgba32f) image2D T, vec2 coord, ivec2 sub, ivec2 subsize)
{
//coord += vec2(0.5);
ivec2 ci = ivec2(coord);
vec2 d = coord - floor(coord);

return (subImage(T, ci, sub, subsize)*(1-d.x)*(1-d.y) +
subImage(T, ci+ivec2(1,0), sub, subsize)*d.x*(1-d.y) +
subImage(T, ci+ivec2(0,1), sub, subsize)*(1-d.x)*d.y +
subImage(T, ci+ivec2(1,1), sub, subsize)*d.x*d.y);
}
//image part sampling defines
#define subImage(T, X, sub, subsize) imageLoad(T, clamp(X, ivec2(1), subsize-1) + sub*subsize)
#define storeSub(T, val, X, sub, subsize) imageStore(T, clamp(X, ivec2(1), subsize-1) + sub*subsize, val)
#define subQuad(T, X, sub, subsize) mat4(subImage(T, ivec2(X), sub, subsize),subImage(T, ivec2(X) + ivec2(1, 0), sub, subsize),subImage(T, ivec2(X) + ivec2(0, 1), sub, subsize),subImage(T, ivec2(X) + ivec2(1, 1), sub, subsize))
#define subInterp(T,X,sub,subsiz) (subQuad(T,X,sub,subsiz)*coords_quad(X))
12 changes: 1 addition & 11 deletions game_folder/shaders/compute/utility/random_stuff.glsl
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@

float min_distance(layout (rgba32f) image2D T, vec3 cur, vec2 lastPos, int scale)
{
ivec2 rp = ivec2(round(lastPos));
float mdist = 1e10;
for(int i = -scale; i <= scale; i++)
for(int j = -scale; j <= scale; j++)
{
mdist = min(length(cur - imageLoad(T, rp+ivec2(i,j)).xyz), mdist);
}
return mdist;
}


vec3 ambient_sky_color(in vec3 pos)
{
Expand Down
4 changes: 1 addition & 3 deletions game_folder/shaders/compute/utility/shading.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ vec3 marble_render(in vec4 pos, in vec4 dir, in vec4 norm, float fov, vec3 GI)
}


vec3 shading(in vec4 pos, in vec4 dir, float fov, layout (rgba32f) image2D illuminationDirect, vec3 RR)
vec3 shading(in vec4 pos, in vec4 dir, float fov, vec3 direct)
{
if(pos.w < max(2*fovray*dir.w, MIN_DIST))
{
Expand All @@ -167,8 +167,6 @@ vec3 shading(in vec4 pos, in vec4 dir, float fov, layout (rgba32f) image2D illu
vec4 norm = calcNormal(pos.xyz, max(MIN_DIST, error));
norm.xyz = normalize(norm.xyz);

vec3 direct = bilinear_surface(illuminationDirect, dir.w, RR.z, RR.xy).xyz;

if(norm.w < -error)
{
return COL(pos.xyz).xyz;
Expand Down

0 comments on commit 2e1f7e3

Please sign in to comment.