Improve packing

This commit is contained in:
luboslenco 2019-07-07 22:02:07 +02:00
parent 17a11473c5
commit f82be5bc64
9 changed files with 55 additions and 47 deletions

View File

@ -13,7 +13,7 @@ in vec2 texCoord;
out vec4 fragColor;
void main() {
float roughness = unpackFloat(textureLod(gbuffer0, texCoord, 0.0).b).y;
float roughness = textureLod(gbuffer0, texCoord, 0.0).b;
// if (roughness == 0.0) { // Always blur for now, non blured output can produce noise
// fragColor.rgb = textureLod(tex, texCoord).rgb;
// return;

View File

@ -171,11 +171,15 @@ void main() {
n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
n = normalize(n);
vec2 metrough = unpackFloat(g0.b);
float roughness = g0.b;
float metallic;
uint matid;
unpackFloatInt16(g0.a, 4, metallic, matid);
vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, spec/occ
vec2 occspec = unpackFloat2(g1.a);
vec3 albedo = surfaceAlbedo(g1.rgb, metrough.x); // g1.rgb - basecolor
vec3 f0 = surfaceF0(g1.rgb, metrough.x);
vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor
vec3 f0 = surfaceF0(g1.rgb, metallic);
float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
vec3 p = getPos(eye, eyeLook, normalize(viewRay), depth, cameraProj);
@ -187,7 +191,7 @@ void main() {
#endif
#ifdef _Brdf
vec2 envBRDF = textureLod(senvmapBrdf, vec2(metrough.y, 1.0 - dotNV), 0.0).xy;
vec2 envBRDF = textureLod(senvmapBrdf, vec2(roughness, 1.0 - dotNV), 0.0).xy;
#endif
// Envmap
@ -202,7 +206,7 @@ void main() {
#ifdef _Rad
vec3 reflectionWorld = reflect(-v, n);
float lod = getMipFromRoughness(metrough.y, envmapNumMipmaps);
float lod = getMipFromRoughness(roughness, envmapNumMipmaps);
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
#endif
@ -219,7 +223,7 @@ void main() {
envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y) * 1.5 * occspec.y;
#else
#ifdef _EnvCol
envl.rgb += backgroundCol * surfaceF0(g1.rgb, metrough.x); // f0
envl.rgb += backgroundCol * surfaceF0(g1.rgb, metallic); // f0
#endif
#endif
@ -281,7 +285,7 @@ void main() {
float sdotNL = dot(n, sunDir);
float svisibility = 1.0;
vec3 sdirect = lambertDiffuseBRDF(albedo, sdotNL) +
specularBRDF(f0, metrough.y, sdotNL, sdotNH, dotNV, sdotVH) * occspec.y;
specularBRDF(f0, roughness, sdotNL, sdotNH, dotNV, sdotVH) * occspec.y;
#ifdef _ShadowMap
#ifdef _CSM
@ -317,11 +321,11 @@ void main() {
// #ifdef _Hair // Aniso
// if (g0.a == 2.0) {
// const float shinyParallel = metrough.y;
// const float shinyParallel = roughness;
// const float shinyPerpendicular = 0.1;
// const vec3 v = vec3(0.99146, 0.11664, 0.05832);
// vec3 T = abs(dot(n, v)) > 0.99999 ? cross(n, vec3(0.0, 1.0, 0.0)) : cross(n, v);
// fragColor.rgb = orenNayarDiffuseBRDF(albedo, metrough.y, dotNV, dotNL, dotVH) + wardSpecular(n, h, dotNL, dotNV, dotNH, T, shinyParallel, shinyPerpendicular) * spec;
// fragColor.rgb = orenNayarDiffuseBRDF(albedo, roughness, dotNV, dotNL, dotVH) + wardSpecular(n, h, dotNL, dotNV, dotNH, T, shinyParallel, shinyPerpendicular) * spec;
// }
// #endif
@ -340,7 +344,7 @@ void main() {
#ifdef _SinglePoint
fragColor.rgb += sampleLight(
p, n, v, dotNV, pointPos, pointCol, albedo, metrough.y, occspec.y, f0
p, n, v, dotNV, pointPos, pointCol, albedo, roughness, occspec.y, f0
#ifdef _ShadowMap
, 0, pointBias
#endif
@ -392,7 +396,7 @@ void main() {
lightsArray[li * 2].xyz, // lp
lightsArray[li * 2 + 1].xyz, // lightCol
albedo,
metrough.y,
roughness,
occspec.y,
f0
#ifdef _ShadowMap

View File

@ -102,11 +102,15 @@ void main() {
n.xy = n.z >= 0.0 ? g0.xy : octahedronWrap(g0.xy);
n = normalize(n);
vec2 metrough = unpackFloat(g0.b);
float roughness = g0.b;
float metallic;
uint matid;
unpackFloatInt16(g0.a, 4, metallic, matid);
vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, spec/occ
vec2 occspec = unpackFloat2(g1.a);
vec3 albedo = surfaceAlbedo(g1.rgb, metrough.x); // g1.rgb - basecolor
vec3 f0 = surfaceF0(g1.rgb, metrough.x);
vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor
vec3 f0 = surfaceF0(g1.rgb, metallic);
float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
vec3 p = getPos(eye, eyeLook, normalize(viewRay), depth, cameraProj);
@ -114,7 +118,7 @@ void main() {
float dotNV = max(dot(n, v), 0.0);
#ifdef _Brdf
vec2 envBRDF = textureLod(senvmapBrdf, vec2(metrough.y, 1.0 - dotNV), 0.0).xy;
vec2 envBRDF = textureLod(senvmapBrdf, vec2(roughness, 1.0 - dotNV), 0.0).xy;
#endif
// Envmap
@ -129,7 +133,7 @@ void main() {
#ifdef _Rad
vec3 reflectionWorld = reflect(-v, n);
float lod = getMipFromRoughness(metrough.y, envmapNumMipmaps);
float lod = getMipFromRoughness(roughness, envmapNumMipmaps);
vec3 prefilteredColor = textureLod(senvmapRadiance, envMapEquirect(reflectionWorld), lod).rgb;
#endif
@ -146,7 +150,7 @@ void main() {
envl.rgb += prefilteredColor * (f0 * envBRDF.x + envBRDF.y) * 1.5 * occspec.y;
#else
#ifdef _EnvCol
envl.rgb += backgroundCol * surfaceF0(g1.rgb, metrough.x); // f0
envl.rgb += backgroundCol * surfaceF0(g1.rgb, metallic); // f0
#endif
#endif
@ -160,7 +164,7 @@ void main() {
float sdotNL = dot(n, sunDir);
float svisibility = 1.0;
vec3 sdirect = lambertDiffuseBRDF(albedo, sdotNL) +
specularBRDF(f0, metrough.y, sdotNL, sdotNH, dotNV, sdotVH) * occspec.y;
specularBRDF(f0, roughness, sdotNL, sdotNH, dotNV, sdotVH) * occspec.y;
#ifdef _ShadowMap
#ifdef _CSM
@ -176,7 +180,7 @@ void main() {
#ifdef _SinglePoint
fragColor.rgb += sampleLight(
p, n, v, dotNV, pointPos, pointCol, albedo, metrough.y, occspec.y, f0
p, n, v, dotNV, pointPos, pointCol, albedo, roughness, occspec.y, f0
#ifdef _ShadowMap
, 0, pointBias
#endif
@ -210,7 +214,7 @@ void main() {
lightsArray[li * 2].xyz, // lp
lightsArray[li * 2 + 1].xyz, // lightCol
albedo,
metrough.y,
roughness,
occspec.y,
f0
#ifdef _ShadowMap

View File

@ -23,7 +23,7 @@ void main() {
vec4 g0 = textureLod(gbuffer0, texCoord, 0.0); // Normal.xy, metallic/roughness, depth
float roughness = unpackFloat(g0.b).y;
float roughness = g0.b;
if (roughness > 0.95) {
fragColor.rgb = vec3(0.0);
return;

View File

@ -23,7 +23,7 @@ void main() {
vec4 g0 = textureLod(gbuffer0, texCoord, 0.0); // Normal.xy, metallic/roughness, depth
float roughness = unpackFloat(g0.b).y;
float roughness = g0.b;
if (roughness > 0.95) {
fragColor.rgb = vec3(0.0);
return;

View File

@ -63,7 +63,7 @@ vec4 rayCast(vec3 dir) {
void main() {
vec4 g0 = textureLod(gbuffer0, texCoord, 0.0);
float roughness = unpackFloat(g0.b).y;
float roughness = g0.b;
if (roughness == 1.0) { fragColor.rgb = vec3(0.0); return; }
float spec = fract(textureLod(gbuffer1, texCoord, 0.0).a);

View File

@ -129,24 +129,24 @@ vec3 decNor(uint val) {
// GBuffer helper - Sebastien Lagarde
// https://seblagarde.wordpress.com/2018/09/02/gbuffer-helper-packing-integer-and-float-together/
float packFloatInt(float f, uint i, uint numBitI, uint numBitTarget) {
float packFloatInt(const float f, const uint i, const uint numBitI, const uint numBitTarget) {
// Constant optimize by compiler
float prec = float(1 << numBitTarget);
float maxi = float(1 << numBitI);
float precMinusOne = prec - 1.0;
float t1 = ((prec / maxi) - 1.0) / precMinusOne;
float t2 = (prec / maxi) / precMinusOne;
const float prec = float(1 << numBitTarget);
const float maxi = float(1 << numBitI);
const float precMinusOne = prec - 1.0;
const float t1 = ((prec / maxi) - 1.0) / precMinusOne;
const float t2 = (prec / maxi) / precMinusOne;
// Code
return t1 * f + t2 * float(i);
}
void unpackFloatInt(float val, uint numBitI, uint numBitTarget, out float f, out uint i) {
void unpackFloatInt(const float val, const uint numBitI, const uint numBitTarget, out float f, out uint i) {
// Constant optimize by compiler
float prec = float(1 << numBitTarget);
float maxi = float(1 << numBitI);
float precMinusOne = prec - 1.0;
float t1 = ((prec / maxi) - 1.0) / precMinusOne;
float t2 = (prec / maxi) / precMinusOne;
const float prec = float(1 << numBitTarget);
const float maxi = float(1 << numBitI);
const float precMinusOne = prec - 1.0;
const float t1 = ((prec / maxi) - 1.0) / precMinusOne;
const float t2 = (prec / maxi) / precMinusOne;
// Code
// extract integer part
// + rcp(precMinusOne) to deal with precision issue
@ -156,19 +156,19 @@ void unpackFloatInt(float val, uint numBitI, uint numBitTarget, out float f, out
f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue
}
float packFloatInt8(float f, uint i, uint numBitI) {
float packFloatInt8(const float f, const uint i, const uint numBitI) {
return packFloatInt(f, i, numBitI, 8);
}
void unpackFloatInt8(float val, uint numBitI, out float f, out uint i) {
void unpackFloatInt8(const float val, const uint numBitI, out float f, out uint i) {
unpackFloatInt(val, numBitI, 8, f, i);
}
float packFloatInt16(float f, uint i, uint numBitI) {
float packFloatInt16(const float f, const uint i, const uint numBitI) {
return packFloatInt(f, i, numBitI, 16);
}
void unpackFloatInt16(float val, uint numBitI, out float f, out uint i) {
void unpackFloatInt16(const float val, const uint numBitI, out float f, out uint i) {
unpackFloatInt(val, numBitI, 16, f, i);
}

View File

@ -67,7 +67,7 @@ def make(context_id):
frag.write('n /= (abs(n.x) + abs(n.y) + abs(n.z));')
frag.write('n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);')
frag.write('fragColor[0] = vec4(n.xy, packFloat(metallic, roughness), opacity);')
frag.write('fragColor[0] = vec4(n.xy, roughness, opacity);')
frag.write('fragColor[1] = vec4(basecol.rgb, opacity);')
make_finalize.make(con_decal)

View File

@ -245,16 +245,16 @@ def make_deferred(con_mesh, rpasses):
frag.write('n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);')
if '_Emission' in wrd.world_defs or '_SSS' in wrd.world_defs or '_Hair' in wrd.world_defs:
frag.write('float matid = 0.0;')
frag.write('uint matid = 0;')
if '_Emission' in wrd.world_defs:
frag.write('if (emission > 0) { basecol *= emission; matid = 1.0; }')
frag.write('if (emission > 0) { basecol *= emission; matid = 1; }')
if '_SSS' in wrd.world_defs or '_Hair' in wrd.world_defs:
frag.add_uniform('int materialID')
frag.write('if (materialID == 2) matid = 2.0;')
frag.write('if (materialID == 2) matid = 2;')
else:
frag.write('const float matid = 0.0;')
frag.write('const uint matid = 0;')
frag.write('fragColor[0] = vec4(n.xy, packFloat(metallic, roughness), matid);')
frag.write('fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4));')
frag.write('fragColor[1] = vec4(basecol, packFloat2(occlusion, specular));')
if '_gbuffer2' in wrd.world_defs:
@ -537,7 +537,7 @@ def make_forward(con_mesh):
frag.write('n /= (abs(n.x) + abs(n.y) + abs(n.z));')
frag.write('n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);')
frag.write('fragColor[0] = vec4(direct + indirect, packFloat2(occlusion, specular));')
frag.write('fragColor[1] = vec4(n.xy, packFloat(metallic, roughness), 1.0);')
frag.write('fragColor[1] = vec4(n.xy, roughness, metallic);')
else:
frag.add_out('vec4 fragColor[1]')
frag.write('fragColor[0] = vec4(direct + indirect, 1.0);')