Reduce volumetrics mem usage

This commit is contained in:
Lubos Lenco 2018-01-28 13:53:06 +01:00
parent 9b8d0249c8
commit 989b91720b
9 changed files with 251 additions and 88 deletions

View file

@ -0,0 +1,125 @@
// Exclusive to volumetric light for now
#version 450
#include "compiled.glsl"
uniform sampler2D tex;
uniform vec2 dir;
uniform vec2 screenSize;
in vec2 texCoord;
out vec4 fragColor;
const float weight[10] = float[] (0.132572, 0.125472, 0.106373, 0.08078, 0.05495, 0.033482, 0.018275, 0.008934, 0.003912, 0.001535);
float normpdf(float x, float sigma) {
return 0.39894 * exp(-0.5 * x * x / (sigma * sigma)) / sigma;
}
float normpdf3(vec3 v, float sigma) {
return 0.39894 * exp(-0.5 * dot(v, v) / (sigma * sigma)) / sigma;
}
void main() {
vec2 step = (dir / screenSize.xy);
vec3 colf = texture(tex, texCoord).rgb * weight[0];
float col;
float res;
float sumfactor = 0.0;
float factor;
float f = 1.0 / normpdf(0.0, 1.0);
col = texture(tex, texCoord + step).r;
factor = normpdf3(col - colf, 1.0) * f * weight[1];
sumfactor += factor;
res = factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[1];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[2];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[2];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[3];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[3];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[4];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[4];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[5];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[5];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[6];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[6];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[7];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[7];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[8];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[8];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[9];
sumfactor += factor;
res += factor * col;
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[9];
sumfactor += factor;
res += factor * col;
res /= sumfactor;
fragColor = vec4(volumAirColor * res, 1.0);
}

View file

@ -0,0 +1,30 @@
{
"contexts": [
{
"name": "blur_bilat_blend_pass_y",
"color_write_alpha": false,
"depth_write": false,
"compare_mode": "always",
"cull_mode": "none",
"blend_source": "blend_one",
"blend_destination": "blend_one",
"blend_operation": "add",
"alpha_blend_source": "blend_one",
"alpha_blend_destination": "blend_one",
"alpha_blend_operation": "add",
"links": [
{
"name": "dir",
"link": "_vec2y"
},
{
"name": "screenSize",
"link": "_screenSize"
}
],
"texture_params": [],
"vertex_shader": "../include/pass.vert.glsl",
"fragment_shader": "blur_bilat_blend_pass.frag.glsl"
}
]
}

View file

@ -8,7 +8,7 @@ uniform vec2 dir;
uniform vec2 screenSize;
in vec2 texCoord;
out vec4 fragColor;
out float fragColor;
const float weight[10] = float[] (0.132572, 0.125472, 0.106373, 0.08078, 0.05495, 0.033482, 0.018275, 0.008934, 0.003912, 0.001535);
@ -24,100 +24,100 @@ void main() {
vec2 step = (dir / screenSize.xy);
vec3 colf = texture(tex, texCoord).rgb * weight[0];
vec3 col;
float col;
float sumfactor = 0.0;
float factor;
float f = 1.0 / normpdf(0.0, 0.1);
float f = 1.0 / normpdf(0.0, 1.0);
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[1];
col = texture(tex, texCoord + step).r;
factor = normpdf3(col - colf, 1.0) * f * weight[1];
sumfactor += factor;
fragColor.rgb = factor * col;
fragColor = factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[1];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[1];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[2];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[2];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[2];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[2];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[3];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[3];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[3];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[3];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[4];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[4];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[4];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[4];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[5];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[5];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[5];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[5];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[6];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[6];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[6];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[6];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[7];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[7];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[7];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[7];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[8];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[8];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[8];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[8];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord + step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[9];
col = texture(tex, texCoord + step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[9];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
col = texture(tex, texCoord - step * 1.5).rgb;
factor = normpdf3(col - colf, 0.1) * f * weight[9];
col = texture(tex, texCoord - step * 1.5).r;
factor = normpdf3(col - colf, 1.0) * f * weight[9];
sumfactor += factor;
fragColor.rgb += factor * col;
fragColor += factor * col;
fragColor.rgb /= sumfactor;
fragColor /= sumfactor;
}

View file

@ -17,20 +17,18 @@ uniform mat4 invVP;
uniform mat4 LWVP;
uniform vec3 eye;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform float lightRadius;
uniform float shadowsBias;
uniform int lightShadow;
uniform vec2 lightPlane;
in vec4 wvpposition;
out vec4 fragColor;
out float fragColor;
const float tScat = 0.08;
const float tAbs = 0.0;
const float tExt = tScat + tAbs;
const float stepLen = 1.0 / volumSteps;
const float lighting = 0.4;
// float lighting(vec3 p) {
// vec3 L = lightPos.xyz - p.xyz;
@ -65,10 +63,8 @@ void rayStep(inout vec3 curPos, inout float curOpticalDepth, inout float scatter
void main() {
vec2 screenPosition = wvpposition.xy / wvpposition.w;
vec2 texCoord = screenPosition * 0.5 + 0.5;
// texCoord += vec2(0.5 / screenSize); // Half pixel offset
float pixelRayMarchNoise = texture(snoise, texCoord).r * 2.0 - 1.0;
pixelRayMarchNoise *= 0.2;
float pixelRayMarchNoise = texture(snoise, texCoord * 100).r * 2.0 - 1.0;
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
vec3 worldPos = getPos2(invVP, depth, texCoord);
@ -93,5 +89,5 @@ void main() {
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
}
fragColor = vec4(vec3(scatteredLightAmount * volumAirColor * normalize(lightColor.rgb) * volumAirTurbidity), 1.0);
fragColor = scatteredLightAmount * volumAirTurbidity;
}

View file

@ -38,10 +38,6 @@
"name": "lightRadius",
"link": "_lampRadius"
},
{
"name": "lightColor",
"link": "_lampColor"
},
{
"name": "snoise",
"link": "_noise8"

View file

@ -12,7 +12,11 @@ uniform sampler2D gbufferD;
#endif
uniform sampler2D snoise;
uniform mat4 LWVP;
#ifdef _CSM
//!uniform vec4 casData[shadowmapCascades * 4 + 4];
#else
uniform mat4 LWVP;
#endif
uniform float shadowsBias;
// uniform vec3 lightPos;
uniform vec3 lightColor;
@ -25,13 +29,12 @@ uniform vec2 cameraProj;
in vec2 texCoord;
in vec3 viewRay;
out vec4 fragColor;
out float fragColor;
const float tScat = 0.08;
const float tAbs = 0.0;
const float tExt = tScat + tAbs;
const float stepLen = 1.0 / volumSteps;
const float lighting = 0.4;
// float lighting(vec3 p) {
// vec3 L = lightPos.xyz - p.xyz;
@ -51,6 +54,9 @@ void rayStep(inout vec3 curPos, inout float curOpticalDepth, inout float scatter
curOpticalDepth *= exp(-tExt * stepLenWorld * density);
float visibility = 1.0;
#ifdef _CSM
mat4 LWVP = mat4(casData[4 + 0], casData[4 + 1], casData[4 + 2], casData[4 + 3]);
#endif
vec4 lampPos = LWVP * vec4(curPos, 1.0);
if (lampPos.w > 0.0) {
lampPos.xyz /= lampPos.w;
@ -62,8 +68,7 @@ void rayStep(inout vec3 curPos, inout float curOpticalDepth, inout float scatter
void main() {
float pixelRayMarchNoise = texture(snoise, texCoord).r * 2.0 - 1.0;
pixelRayMarchNoise *= 0.2;
float pixelRayMarchNoise = texture(snoise, texCoord * 100).r * 2.0 - 1.0;
float depth = texture(gbufferD, texCoord).r * 2.0 - 1.0;
vec3 worldPos = getPos(eye, eyeLook, viewRay, depth, cameraProj);
@ -88,5 +93,5 @@ void main() {
rayStep(curPos, curOpticalDepth, scatteredLightAmount, stepLenWorld, viewVecNorm);
}
fragColor = vec4(vec3(scatteredLightAmount * volumAirColor * normalize(lightColor.rgb) * volumAirTurbidity), 1.0);
fragColor = scatteredLightAmount * volumAirTurbidity;
}

View file

@ -12,7 +12,8 @@
},
{
"name": "LWVP",
"link": "_biasLampWorldViewProjectionMatrix"
"link": "_biasLampWorldViewProjectionMatrix",
"ifndef": ["_CSM"]
},
{
"name": "shadowsBias",
@ -28,10 +29,6 @@
"link": "_lampRadius",
"ifdef": ["_Disabled"]
},
{
"name": "lightColor",
"link": "_lampColor"
},
{
"name": "snoise",
"link": "_noise8"
@ -57,6 +54,11 @@
{
"name": "cameraProj",
"link": "_cameraPlaneProj"
},
{
"name": "casData",
"link": "_cascadeData",
"ifdef": ["_CSM"]
}
],
"texture_params": [],
@ -127,6 +129,11 @@
{
"name": "cameraProj",
"link": "_cameraPlaneProj"
},
{
"name": "casData",
"link": "_cascadeData",
"ifdef": ["_CSM"]
}
],
"texture_params": [],

View file

@ -138,7 +138,7 @@ class RenderPathCreator {
path.loadShader("shader_datas/volumetric_light_quad/volumetric_light_quad");
path.loadShader("shader_datas/volumetric_light/volumetric_light");
path.loadShader("shader_datas/blur_bilat_pass/blur_bilat_pass_x");
path.loadShader("shader_datas/blur_bilat_pass/blur_bilat_pass_y_blend");
path.loadShader("shader_datas/blur_bilat_blend_pass/blur_bilat_blend_pass_y");
{
var t = new RenderTargetRaw();
t.name = "bufvola";
@ -327,7 +327,7 @@ class RenderPathCreator {
path.setTarget("lbuf");
path.bindTarget("bufvolb", "tex");
path.drawShader("shader_datas/blur_bilat_pass/blur_bilat_pass_y_blend");
path.drawShader("shader_datas/blur_bilat_blend_pass/blur_bilat_blend_pass_y");
}
#end
@ -675,7 +675,7 @@ class RenderPathCreator {
path.loadShader("shader_datas/volumetric_light_quad/volumetric_light_quad");
path.loadShader("shader_datas/volumetric_light/volumetric_light");
path.loadShader("shader_datas/blur_bilat_pass/blur_bilat_pass_x");
path.loadShader("shader_datas/blur_bilat_pass/blur_bilat_pass_y_blend");
path.loadShader("shader_datas/blur_bilat_blend_pass/blur_bilat_blend_pass_y");
{
var t = new RenderTargetRaw();
t.name = "bufvola";
@ -683,8 +683,9 @@ class RenderPathCreator {
t.height = 0;
t.displayp = getDisplayp();
t.format = "R8";
// var ss = getSuperSampling();
t.scale = 0.5;
var ss = getSuperSampling();
if (ss != 1) t.scale = ss;
// t.scale = 0.5;
path.createRenderTarget(t);
}
{
@ -694,8 +695,9 @@ class RenderPathCreator {
t.height = 0;
t.displayp = getDisplayp();
t.format = "R8";
// var ss = getSuperSampling();
t.scale = 0.5;
var ss = getSuperSampling();
if (ss != 1) t.scale = ss;
// t.scale = 0.5;
path.createRenderTarget(t);
}
}
@ -1067,7 +1069,7 @@ class RenderPathCreator {
path.setTarget("tex");
path.bindTarget("bufvolb", "tex");
path.drawShader("shader_datas/blur_bilat_pass/blur_bilat_pass_y_blend");
path.drawShader("shader_datas/blur_bilat_blend_pass/blur_bilat_blend_pass_y");
}
#end
}

View file

@ -274,6 +274,8 @@ def build():
assets.add_shader_pass('volumetric_light_quad')
assets.add_shader_pass('volumetric_light')
assets.add_shader_pass('blur_bilat_pass')
assets.add_shader_pass('blur_bilat_blend_pass')
assets.add_embedded_data('noise8.png')
if rpdat.rp_decals:
assets.add_khafile_def('rp_decals')