Fix packing inline

This commit is contained in:
luboslenco
2019-07-16 14:05:29 +02:00
parent 89a9e7a8b4
commit dd73873a7b
6 changed files with 40 additions and 21 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ vec3 n = normalize(wnormal);
n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);
uint matid = 0;
if (emission > 0) { basecol *= emission; matid = 1; }
fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4));
fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid));
fragColor[1] = vec4(basecol, packFloat2(occlusion, specular));
vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5;
vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5;
+1 -1
View File
@@ -30,7 +30,7 @@ vec3 n = normalize(wnormal);
n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);
uint matid = 0;
if (emission > 0) { basecol *= emission; matid = 1; }
fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4));
fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid));
fragColor[1] = vec4(basecol, packFloat2(occlusion, specular));
vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5;
vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5;
+1 -1
View File
@@ -23,7 +23,7 @@ vec3 n = normalize(wnormal);
n.xy = n.z >= 0.0 ? n.xy : octahedronWrap(n.xy);
uint matid = 0;
if (emission > 0) { basecol *= emission; matid = 1; }
fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid, 4));
fragColor[0] = vec4(n.xy, roughness, packFloatInt16(metallic, matid));
fragColor[1] = vec4(basecol, packFloat2(occlusion, specular));
vec2 posa = (wvpposition.xy / wvpposition.w) * 0.5 + 0.5;
vec2 posb = (prevwvpposition.xy / prevwvpposition.w) * 0.5 + 0.5;
+1 -1
View File
@@ -174,7 +174,7 @@ void main() {
float roughness = g0.b;
float metallic;
uint matid;
unpackFloatInt16(g0.a, 4, metallic, matid);
unpackFloatInt16(g0.a, metallic, matid);
vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, spec/occ
vec2 occspec = unpackFloat2(g1.a);
vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor
@@ -175,7 +175,7 @@ void main() {
float roughness = g0.b;
float metallic;
uint matid;
unpackFloatInt16(g0.a, 4, metallic, matid);
unpackFloatInt16(g0.a, metallic, matid);
vec4 g1 = textureLod(gbuffer1, texCoord, 0.0); // Basecolor.rgb, spec/occ
vec2 occspec = unpackFloat2(g1.a);
vec3 albedo = surfaceAlbedo(g1.rgb, metallic); // g1.rgb - basecolor
+35 -16
View File
@@ -121,8 +121,10 @@ vec3 decNor(uint val) {
// GBuffer helper - Sebastien Lagarde
// https://seblagarde.wordpress.com/2018/09/02/gbuffer-helper-packing-integer-and-float-together/
float packFloatInt(const float f, const uint i, const uint numBitI, const uint numBitTarget) {
float packFloatInt8(const float f, const uint i) {
// Constant optimize by compiler
const int numBitTarget = 8;
const int numBitI = 4;
const float prec = float(1 << numBitTarget);
const float maxi = float(1 << numBitI);
const float precMinusOne = prec - 1.0;
@@ -132,8 +134,23 @@ float packFloatInt(const float f, const uint i, const uint numBitI, const uint n
return t1 * f + t2 * float(i);
}
void unpackFloatInt(const float val, const uint numBitI, const uint numBitTarget, out float f, out uint i) {
float packFloatInt16(const float f, const uint i) {
// Constant optimize by compiler
const int numBitTarget = 16;
const int numBitI = 4;
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 unpackFloatInt8(const float val, out float f, out uint i) {
// Constant optimize by compiler
const int numBitTarget = 8;
const int numBitI = 4;
const float prec = float(1 << numBitTarget);
const float maxi = float(1 << numBitI);
const float precMinusOne = prec - 1.0;
@@ -148,20 +165,22 @@ void unpackFloatInt(const float val, const uint numBitI, const uint numBitTarget
f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue
}
float packFloatInt8(const float f, const uint i, const uint numBitI) {
return packFloatInt(f, i, numBitI, 8);
}
void unpackFloatInt8(const float val, const uint numBitI, out float f, out uint i) {
unpackFloatInt(val, numBitI, 8, f, i);
}
float packFloatInt16(const float f, const uint i, const uint numBitI) {
return packFloatInt(f, i, numBitI, 16);
}
void unpackFloatInt16(const float val, const uint numBitI, out float f, out uint i) {
unpackFloatInt(val, numBitI, 16, f, i);
void unpackFloatInt16(const float val, out float f, out uint i) {
// Constant optimize by compiler
const int numBitTarget = 16;
const int numBitI = 4;
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
i = int((val / t2) + (1.0 / precMinusOne));
// Now that we have i, solve formula in packFloatInt for f
//f = (val - t2 * float(i)) / t1 => convert in mads form
f = clamp((-t2 * float(i) + val) / t1, 0.0, 1.0); // Saturate in case of precision issue
}
#endif