vk: rt: make normalmaps switchable between xyz.png and xy.ktx2

They have slightly different incompatible encodings. Switch between them
using #define at build time.

Old png code can be removed when we fully switch to KTX2.
This commit is contained in:
Ivan Avdeev 2023-10-06 13:05:42 -04:00
parent ab53aae359
commit 96e9ef20ab
1 changed files with 27 additions and 7 deletions

View File

@ -45,15 +45,35 @@ void primaryRayHit(rayQueryEXT rq, inout RayPayloadPrimary payload) {
T = normalize(T - dot(T, geom.normal_shading) * geom.normal_shading);
const vec3 B = normalize(cross(geom.normal_shading, T));
const mat3 TBN = mat3(T, B, geom.normal_shading);
//vec3 tnorm = sampleTexture(tex_normal, geom.uv, geom.uv_lods).xyz * 2. - 1.; // TODO is this sampling correct for normal data?
vec3 tnorm = sampleTexture(tex_normal, geom.uv, geom.uv_lods).xyz; // TODO is this sampling correct for normal data?
// Get to KTX2 normal maps eventually
//#define KTX2
#ifdef KTX2
// We expect KTX2 normalmaps to have only 2 SNORM components.
// TODO: BC6H only can do signed or unsigned 16-bit floats. It can't normalize them on its own. So we either deal with
// sub-par 10bit precision for <1 values. Or do normalization manually in shader. Manual normalization implies prepa-
// ring normalmaps in a special way, i.e. scaling vector components to full f16 scale.
#define NORMALMAP_SNORM
#define NORMALMAP_2COMP
#endif
#ifdef NORMALMAP_SNORM // [-1..1]
// TODO is this sampling correct for normal data?
vec3 tnorm = sampleTexture(tex_normal, geom.uv, geom.uv_lods).xyz;
#else // Older UNORM [0..1]
vec3 tnorm = sampleTexture(tex_normal, geom.uv, geom.uv_lods).xyz * 2. - 1.;
#endif
#ifndef NORMALMAP_2COMP
// Older 8-bit PNG suffers from quantization.
// Smoothen quantization by normalizing it
tnorm = normalize(tnorm);
#endif
tnorm.xy *= material.normal_scale;
//tnorm.z = sqrt(max(0., 1. - material.normal_scale * material.normal_scale * (1. - tnorm.z * tnorm.z)));
//tnorm.z = sqrt(max(0., 1. - dot(tnorm.xy, tnorm.xy)));
tnorm.z = 1. - dot(tnorm.xy, tnorm.xy);
//tnorm.xy = tnorm.yx;
tnorm = normalize(tnorm);
// Restore z based on scaled xy
tnorm.z = sqrt(max(0., 1. - dot(tnorm.xy, tnorm.xy)));
geom.normal_shading = normalize(TBN * tnorm);
}