Fix envmap lod sampling

This commit is contained in:
luboslenco
2025-07-10 18:04:20 +02:00
parent 72bd1e1534
commit 27f1e542fa
12 changed files with 156 additions and 28 deletions
Binary file not shown.
Binary file not shown.
-6
View File
@@ -1,6 +0,0 @@
#?RADIANCE
# Made with Adobe Photoshop
FORMAT=32-bit_rle_rgbe
-Y 2 +X 4
–…w~€†“€„�zh~öÕÂ}ÊÊÒ~¿¹¸~ëÈ´}
-6
View File
@@ -1,6 +0,0 @@
#?RADIANCE
# Made with Adobe Photoshop
FORMAT=32-bit_rle_rgbe
-Y 1 +X 2
¿¿~¿¿~
-6
View File
@@ -1,6 +0,0 @@
#?RADIANCE
# Made with Adobe Photoshop
FORMAT=32-bit_rle_rgbe
-Y 1 +X 1
¿¿~
Binary file not shown.
+46 -2
View File
@@ -34,6 +34,21 @@ const senvmap_brdf: tex2d;
#[set(everything)]
const senvmap_radiance: tex2d;
#[set(everything)]
const senvmap_radiance0: tex2d;
#[set(everything)]
const senvmap_radiance1: tex2d;
#[set(everything)]
const senvmap_radiance2: tex2d;
#[set(everything)]
const senvmap_radiance3: tex2d;
#[set(everything)]
const senvmap_radiance4: tex2d;
#[set(everything)]
const ssaotex: tex2d;
@@ -167,6 +182,25 @@ fun envmap_equirect(normal: float3, angle: float): float2 {
return float2(theta / PI2, phi / PI);
}
fun envmap_sample(lod: float, coord: float2): float3 {
if (lod == 0.0) {
return sample_lod(senvmap_radiance, sampler_linear, coord, 0.0).rgb;
}
if (lod == 1.0) {
return sample_lod(senvmap_radiance0, sampler_linear, coord, 0.0).rgb;
}
if (lod == 2.0) {
return sample_lod(senvmap_radiance1, sampler_linear, coord, 0.0).rgb;
}
if (lod == 3.0) {
return sample_lod(senvmap_radiance2, sampler_linear, coord, 0.0).rgb;
}
if (lod == 4.0) {
return sample_lod(senvmap_radiance3, sampler_linear, coord, 0.0).rgb;
}
return sample_lod(senvmap_radiance4, sampler_linear, coord, 0.0).rgb;
}
fun deferred_light_frag(input: vert_out): float4 {
// normal.xy, roughness, metallic/matid
var g0: float4 = sample_lod(gbuffer0, sampler_linear, input.tex, 0.0);
@@ -217,8 +251,18 @@ fun deferred_light_frag(input: vert_out): float4 {
envl = envl / PI;
var reflection_world: float3 = reflect(-v, n);
var lod: float = mip_from_roughness(roughness, float(constants.envmap_num_mipmaps));
var prefiltered_color: float3 = sample_lod(senvmap_radiance, sampler_linear, envmap_equirect(reflection_world, constants.envmap_data.x), lod).rgb;
// var lod: float = mip_from_roughness(roughness, float(constants.envmap_num_mipmaps));
// var prefiltered_color: float3 = sample_lod(senvmap_radiance, sampler_linear, envmap_equirect(reflection_world, constants.envmap_data.x), lod).rgb;
var lod: float = mip_from_roughness(roughness, 5.0);
var lod0: float = floor(lod);
var lod1: float = ceil(lod);
var lodf: float = lod - lod0;
var envmap_coord: float2 = envmap_equirect(reflection_world, constants.envmap_data.x);
var lodc0: float3 = envmap_sample(lod0, envmap_coord);
var lodc1: float3 = envmap_sample(lod1, envmap_coord);
var prefiltered_color: float3 = lerp3(lodc0, lodc1, lodf);
envl.rgb = envl.rgb * albedo;
// Indirect specular
+30
View File
@@ -53,6 +53,36 @@ function uniforms_set_context_consts(context: shader_context_t, bind_params: str
gpu_set_texture(context._.tex_units[j], w._.radiance);
}
}
else if (tulink == "_envmap_radiance0") {
let w: world_data_t = scene_world;
if (w != null) {
gpu_set_texture(context._.tex_units[j], w._.radiance_mipmaps[0]);
}
}
else if (tulink == "_envmap_radiance1") {
let w: world_data_t = scene_world;
if (w != null) {
gpu_set_texture(context._.tex_units[j], w._.radiance_mipmaps[1]);
}
}
else if (tulink == "_envmap_radiance2") {
let w: world_data_t = scene_world;
if (w != null) {
gpu_set_texture(context._.tex_units[j], w._.radiance_mipmaps[2]);
}
}
else if (tulink == "_envmap_radiance3") {
let w: world_data_t = scene_world;
if (w != null) {
gpu_set_texture(context._.tex_units[j], w._.radiance_mipmaps[3]);
}
}
else if (tulink == "_envmap_radiance4") {
let w: world_data_t = scene_world;
if (w != null) {
gpu_set_texture(context._.tex_units[j], w._.radiance_mipmaps[4]);
}
}
else if (tulink == "_envmap") {
let w: world_data_t = scene_world;
if (w != null) {
+21
View File
@@ -360,6 +360,27 @@ fun envmap_equirect(normal: float3, angle: float): float2 { \
} \
";
let str_envmap_sample: string = "\
fun envmap_sample(lod: float, coord: float2): float3 { \
if (lod == 0.0) { \
return sample_lod(senvmap_radiance, sampler_linear, coord, 0.0).rgb; \
} \
if (lod == 1.0) { \
return sample_lod(senvmap_radiance0, sampler_linear, coord, 0.0).rgb; \
} \
if (lod == 2.0) { \
return sample_lod(senvmap_radiance1, sampler_linear, coord, 0.0).rgb; \
} \
if (lod == 3.0) { \
return sample_lod(senvmap_radiance2, sampler_linear, coord, 0.0).rgb; \
} \
if (lod == 4.0) { \
return sample_lod(senvmap_radiance3, sampler_linear, coord, 0.0).rgb; \
} \
return sample_lod(senvmap_radiance4, sampler_linear, coord, 0.0).rgb; \
} \
";
let str_get_pos_nor_from_depth: string = "\
fun get_pos_from_depth(uv: float2, invVP: flaot4x4): float3 { \
var depth: float = sample_lod(gbufferD, sampler_linear, float2(uv.x, 1.0 - uv.y), 0.0).r; \