Skip to content

Commit

Permalink
Properly clamp cached radiance
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Apr 17, 2024
1 parent 4d9b968 commit f926a96
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 27 deletions.
6 changes: 5 additions & 1 deletion internal/CoreSIMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -7396,12 +7396,16 @@ void Ray::NS::ShadeSurface(const pass_settings_t &ps, const float limits[2], con
const uint32_t cache_entry = find_entry(sc.spatial_cache_entries, P, plane_N, sc.spatial_cache_grid);
if (cache_entry != HASH_GRID_INVALID_CACHE_ENTRY) {
const packed_cache_voxel_t &voxel = sc.spatial_cache_voxels[cache_entry];
cache_voxel_t unpacked = unpack_voxel_data(voxel);
const cache_voxel_t unpacked = unpack_voxel_data(voxel);
if (unpacked.sample_count >= RAD_CACHE_SAMPLE_COUNT_MIN) {
fvec4 color = fvec4{unpacked.radiance[0], unpacked.radiance[1], unpacked.radiance[2], 0.0f} /
float(unpacked.sample_count);
color /= sc.spatial_cache_grid.exposure;
color *= fvec4{ray.c[0][i], ray.c[1][i], ray.c[2][i], 0.0f};
const float sum = hsum(color);
if (sum > limits[1]) {
color *= (limits[1] / sum);
}

out_rgba[0].set(i, color[0]);
out_rgba[1].set(i, color[1]);
Expand Down
6 changes: 5 additions & 1 deletion internal/ShadeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,11 +1347,15 @@ Ray::color_rgba_t Ray::Ref::ShadeSurface(const pass_settings_t &ps, const float
find_entry(sc.spatial_cache_entries, surf.P, surf.plane_N, sc.spatial_cache_grid);
if (cache_entry != HASH_GRID_INVALID_CACHE_ENTRY) {
const packed_cache_voxel_t &voxel = sc.spatial_cache_voxels[cache_entry];
cache_voxel_t unpacked = unpack_voxel_data(voxel);
const cache_voxel_t unpacked = unpack_voxel_data(voxel);
if (unpacked.sample_count >= RAD_CACHE_SAMPLE_COUNT_MIN) {
fvec4 color = make_fvec3(unpacked.radiance) / float(unpacked.sample_count);
color /= sc.spatial_cache_grid.exposure;
color *= fvec4{ray.c[0], ray.c[1], ray.c[2], 0.0f};
const float sum = hsum(color);
if (sum > limits[1]) {
color *= (limits[1] / sum);
}
return color_rgba_t{color.get<0>(), color.get<1>(), color.get<2>(), color.get<3>()};
}
}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion internal/shaders/shade.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2134,11 +2134,15 @@ vec3 ShadeSurface(const hit_data_t inter, const ray_data_t ray, inout vec3 out_b
const uint cache_entry = find_entry(surf.P, surf.plane_N, params);
if (cache_entry != HASH_GRID_INVALID_CACHE_ENTRY) {
const uvec4 voxel = g_cache_voxels[cache_entry];
cache_voxel_t unpacked = unpack_voxel_data(voxel);
const cache_voxel_t unpacked = unpack_voxel_data(voxel);
if (unpacked.sample_count >= RAD_CACHE_SAMPLE_COUNT_MIN) {
vec3 color = unpacked.radiance / float(unpacked.sample_count);
color /= params.exposure;
color *= vec3(ray.c[0], ray.c[1], ray.c[2]);
const float sum = color.r + color.g + color.b;
if (sum > g_params.limit_indirect) {
color *= (g_params.limit_indirect / sum);
}
return color;
}
}
Expand Down

0 comments on commit f926a96

Please sign in to comment.