nvidia fix, soft shadows

This commit is contained in:
2023-02-13 18:35:25 +03:00
parent c8dcd5e9c0
commit 36540468dc
17 changed files with 358 additions and 107 deletions

View File

@@ -23,7 +23,8 @@ uniform sampler2D tex_0, tex_1, tex_2, tex_3, tex_4, tex_sh;
//uniform sampler2DShadow tex_shadow[16];
uniform sampler2DArray tex_shadows_cone;
uniform samplerCube tex_env;
uniform int lights_start, lights_count;
uniform int lights_start, lights_count, soft_shadows_samples = 16;
uniform bool soft_shadows_enabled = false;
uniform vec4 fog_color = vec4(0.5, 0.5, 0.5, 1);
uniform float fog_decay = 10, fog_density = 0;
@@ -36,7 +37,7 @@ const float PI = 3.1416;
vec4 pos, lpos, shp;
vec3 li, si, ldir, halfV, bn, bn2, lwdir;
vec3 vds, vds2;
float rough_diff, rough_spec, dist, NdotL, NdotH, spot, ldist, diff, spec, sdist, shadow;
float rough_diff, rough_spec, dist, NdotL, NdotH, spot, ldist, diff, spec, sdist, shadow, shadow_dz;
uint flags;
@@ -47,8 +48,8 @@ vec4 mapScreenToShadow(in int light_index, in vec3 offset) {
}
float getShadow(in vec3 uvz, in int layer) {
vec2 uvpix = uvz.xy * shadow_size + vec2(0.5f);
float getShadow(in vec3 uvz, in int layer, in vec2 uv_offset) {
vec2 uvpix = (uvz.xy + uv_offset) * shadow_size + vec2(0.5f);
vec2 uvp = fract(uvpix), iuvp = vec2(1.f) - uvp;
vec4 gt = textureGather(tex_shadows_cone, vec3(floor(uvpix.xy) / shadow_size, layer), 0);
vec4 uvv;
@@ -56,7 +57,18 @@ float getShadow(in vec3 uvz, in int layer) {
uvv[1] = uvp.x * uvp.y;
uvv[2] = uvp.x * iuvp.y;
uvv[3] = iuvp.x * iuvp.y;
return dot(step(vec4(uvz.z), gt), uvv);
shadow_dz = max(max(uvz.z - gt[0], uvz.z - gt[1]), max(uvz.z - gt[2], uvz.z - gt[3]));
return clamp(dot(step(vec4(uvz.z), gt), uvv), 0, 1);
}
float rand(vec2 co) {
float a = 12.9898;
float b = 78.233;
float c = 43758.5453;
float dt= dot(co.xy ,vec2(a,b));
float sn= mod(dt,3.14);
return fract(sin(sn) * c) - 0.5;
}
@@ -75,25 +87,42 @@ void calcLight(in int index, in vec3 n, in vec3 v) {
spot *= scos * step(qgl_light_parameter[index].angles.w, scos);
spot *= smoothstep(qgl_light_parameter[index].angles.w, qgl_light_parameter[index].angles.y, scos);
if (qgl_light_parameter[index].flags == 1 && bitfieldExtract(flags, 3, 1) == 1) {
if (int(round(qgl_light_parameter[index].flags)) == 1 && bitfieldExtract(flags, 3, 1) == 1) {
float ds = ldist/300.;
//float bias = ldist * 0.05;
//vds = ds * bn.xyz;
//vds2 = ds * bn2.xyz;
int layer = index - lights_start;
float shadow = 0.;
vec4 shp = mapScreenToShadow(index, vec3(0));
shp.xy /= shp.w;
if (soft_shadows_enabled) {
float ds = (ldist / 2.) / qgl_light_parameter[index].size;
vds = ds * bn.xyz;
vds2 = ds * bn2.xyz;
vec2 so;
for (int i = 1; i <= soft_shadows_samples; ++i) {
//shadow += step(shp.z, texture(tex_shadows_cone, vec3(shp.xy, layer)).r);
so = vec2(rand(vec2(shp.x + i, shp.y)), rand(vec2(shp.x + i + 1, shp.y)));
vec4 shp = mapScreenToShadow(index, vds * so.x + vds2 * so.y);
shp.xy /= shp.w;
shadow += getShadow(shp.xyz, layer, vec2(0));
}
//shadow = getShadow(shp.xyz, layer, vec2(0));
spot *= shadow / (soft_shadows_samples + 0);// / 25.f;
} else {
shp.xy /= shp.w;
spot *= getShadow(shp.xyz, layer, vec2(0));
}
//shp.z -= bias;
//for (int xi = -2; xi <= 2; ++xi) {
// for (int yi = -2; yi <= 2; ++yi) {
//shadow += step(shp.z, texture(tex_shadows_cone, vec3(shp.xy, layer), ivec2(0, 0)).r);
shadow = getShadow(shp.xyz, layer);
// }
//}
spot *= shadow;// / 25.f;
//shadow += getShadow(shp.xyz, layer, vec2(0));
//spot *= dot(so,vec2(1)) / scount + 0.5;// / 25.f;
//spot *= shadow_dz;
//spot = texture(tex_shadows_cone, shp.xyz).r/20;
//spot = sz;
@@ -167,7 +196,6 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) {
}
void main(void) {
ivec2 tc = ivec2(gl_FragCoord.xy);
vec4 v1 = texelFetch(tex_1, tc, 0);
@@ -193,12 +221,12 @@ void main(void) {
float reflectivity = v2.b;
float NdotV = dot(normal, v);
float roughness3 = roughness*roughness*roughness;
//bn = normalize(cross(normal, view_dir));
//bn2 = normalize(cross(normal, bn));
bn = normalize(cross(normal, view_dir));
bn2 = normalize(cross(normal, bn));
rough_diff = max(roughness, _min_rough);
rough_spec = max(roughness3, _min_rough);
float shlick = clamp(metalness + (1 - metalness) * pow(1 - NdotV, 5), 0, 1);
flags = uint(v2.w);
flags = uint(round(v2.w));
li = vec3(0.);//qgl_AmbientLight.color.rgb * qgl_AmbientLight.intensity;
si = vec3(0.);
@@ -229,19 +257,26 @@ void main(void) {
}
qgl_FragColor = vec4(res_col, alpha);
//qgl_FragColor.rgb = vec3((v2.w/15));
//qgl_FragColor.rgb = diffuse.rgb;
/*
//qgl_FragColor.rgb = vec3(qgl_light_parameter[0].size);
#ifdef SPOT
vec4 wpos = vec4(world_dir * z, 1);
vec4 shp = qgl_light_position[lights_start].shadow_matrix * (pos);
//vec4 shp = mapScreenToShadow(0, vec3(0));
//shp.xy /= shp.w;
//shp.z -= bias;
//for (int xi = -2; xi <= 2; ++xi) {
// for (int yi = -2; yi <= 2; ++yi) {
//qgl_FragColor.rgb = vec3(step(shp.z, texture(tex_shadows_cone, vec3(shp.xy, 0)).r));
//qgl_FragColor.r = texture(tex_shadows_cone, vec3(0)).r;
//qgl_FragColor.gb = vec2(1);
//qgl_FragColor.rg = shp.xy;//vec4(res_col, alpha);
//shp.z += 0.095;
//qgl_FragColor.rgb = vec3(texture(tex_sh, shp.xy).rgb);
qgl_FragColor.rgb = vec3(textureProj(tex_shadow[0], shp));
//qgl_FragColor.rgb = vec3(textureProj(tex_shadow[0], shp));
//vec4 rp = qgl_ViewProjMatrix*vec4(qgl_FragTexture.xy,z,1);
//qgl_FragColor.rgb = vec3(shp.xy,0);
//qgl_FragColor.rgb = vec3(texture(tex_shadows_cone, vec3(gl_FragCoord.xy/1000, 0)).r);
#endif
*/
//vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
//qgl_FragColor.rgb = vec3(shlick * brdf.x + brdf.y);
//qgl_FragColor.rgb = vec3(alpha);

View File

@@ -22,5 +22,4 @@ void main(void) {
l = 1 - exp(-l*g);
res = max(vec3(0.f), res * l);
qgl_FragColor = vec4(res, dot(res, luma));
//qgl_FragColor = vec4(0.5);
}