Skip to content

Commit

Permalink
Refactor radiance cache propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Apr 17, 2024
1 parent faa088c commit 981bc25
Show file tree
Hide file tree
Showing 18 changed files with 104 additions and 107 deletions.
14 changes: 4 additions & 10 deletions internal/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,7 @@ struct cache_data_t {
int path_len;
};

enum eSpatialCacheMode {
None,
Update,
Query
};
enum eSpatialCacheMode { None, Update, Query };

struct scene_data_t {
const environment_t &env;
Expand Down Expand Up @@ -522,11 +518,9 @@ force_inline float calc_voxel_size(const uint32_t grid_level, const cache_grid_p
}

template <typename T> void rect_fill(Span<T> data, const int stride, const rect_t &rect, T &&val) {
for (int y = 0; y < rect.h; ++y) {
const int yy = rect.y + y;
for (int x = 0; x < rect.w; ++x) {
const int xx = rect.x + x;
data[yy * stride + xx] = val;
for (int y = rect.y; y < rect.y + rect.h; ++y) {
for (int x = rect.x; x < rect.x + rect.w; ++x) {
data[y * stride + x] = val;
}
}
}
Expand Down
37 changes: 20 additions & 17 deletions internal/CoreSIMD.h
Original file line number Diff line number Diff line change
Expand Up @@ -7381,6 +7381,7 @@ void Ray::NS::ShadeSurface(const pass_settings_t &ps, const float limits[2], con
use_cache |= simd_cast(cone_width > mix(fvec<S>{1.0f}, fvec<S>{1.5f}, cache_rand[0]) * voxel_size);
use_cache &= simd_cast(inter.t > mix(fvec<S>{1.0f}, fvec<S>{2.0f}, cache_rand[1]) * voxel_size);
use_cache &= is_active_lane;
use_cache &= (mat_type != int(eShadingNode::Emissive));
if (use_cache.not_all_zeros()) {
for (int i = 0; i < S; ++i) {
if (use_cache[i] == 0) {
Expand All @@ -7390,22 +7391,24 @@ void Ray::NS::ShadeSurface(const pass_settings_t &ps, const float limits[2], con
const fvec4 P = {surf.P[0][i], surf.P[1][i], surf.P[2][i], 0.0f};
const fvec4 plane_N = {surf.plane_N[0][i], surf.plane_N[1][i], surf.plane_N[2][i], 0.0f};

use_cache.set(i, 0);

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);

fvec4 color = fvec4{unpacked.radiance[0], unpacked.radiance[1], unpacked.radiance[2], 0.0f};
if (unpacked.sample_count) {
color /= float(unpacked.sample_count);
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};

out_rgba[0].set(i, color[0]);
out_rgba[1].set(i, color[1]);
out_rgba[2].set(i, color[2]);
out_rgba[3].set(i, 1.0f);
use_cache.set(i, -1);
}
color /= sc.spatial_cache_grid.exposure;
color *= fvec4{ray.c[0][i], ray.c[1][i], ray.c[2][i], 0.0f};

out_rgba[0].set(i, color[0]);
out_rgba[1].set(i, color[1]);
out_rgba[2].set(i, color[2]);
out_rgba[3].set(i, 1.0f);
}
}
}
Expand Down Expand Up @@ -8078,10 +8081,10 @@ void Ray::NS::SpatialCacheUpdate(const cache_grid_params_t &params, Span<const h
fvec4 rad = fvec4{radiance[y * img_w + x].v} * params.exposure;

cache_data_t &cache = cache_data[y * (img_w / RAD_CACHE_DOWNSAMPLING_FACTOR) + x];
cache.sample_weight[0][0] = r.c[0][j];
cache.sample_weight[0][1] = r.c[1][j];
cache.sample_weight[0][2] = r.c[2][j];
if (inter.v[j] < 0.0f || inter.obj_index[j] < 0) {
cache.sample_weight[0][0] *= r.c[0][j];
cache.sample_weight[0][1] *= r.c[1][j];
cache.sample_weight[0][2] *= r.c[2][j];
if (inter.v[j] < 0.0f || inter.obj_index[j] < 0 || cache.path_len == RAD_CACHE_PROPAGATION_DEPTH) {
for (int k = 0; k < cache.path_len; ++k) {
rad *= make_fvec3(cache.sample_weight[k]);
if (cache.cache_entries[k] != HASH_GRID_INVALID_CACHE_ENTRY) {
Expand All @@ -8094,12 +8097,12 @@ void Ray::NS::SpatialCacheUpdate(const cache_grid_params_t &params, Span<const h
memcpy(cache.sample_weight[k], cache.sample_weight[k - 1], 3 * sizeof(float));
}

cache.sample_weight[0][0] = cache.sample_weight[0][1] = cache.sample_weight[0][2] = 1.0f;
cache.cache_entries[0] = insert_entry(entries, P, N, params);
if (cache.cache_entries[0] != HASH_GRID_INVALID_CACHE_ENTRY) {
accumulate_cache_voxel(voxels_curr[cache.cache_entries[0]], rad, 1);
}

cache.path_len = std::min(cache.path_len + 1, RAD_CACHE_PROPAGATION_DEPTH - 1);
++cache.path_len;

for (int k = 1; k < cache.path_len; ++k) {
rad *= make_fvec3(cache.sample_weight[k]);
Expand Down
10 changes: 6 additions & 4 deletions internal/RadCacheRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ void Ray::Ref::SpatialCacheUpdate(const cache_grid_params_t &params, Span<const
fvec4 rad = fvec4{radiance[y * img_w + x].v} * params.exposure;

cache_data_t &cache = cache_data[y * (img_w / RAD_CACHE_DOWNSAMPLING_FACTOR) + x];
memcpy(cache.sample_weight[0], r.c, 3 * sizeof(float));
if (inter.v < 0.0f || inter.obj_index < 0) {
cache.sample_weight[0][0] *= r.c[0];
cache.sample_weight[0][1] *= r.c[1];
cache.sample_weight[0][2] *= r.c[2];
if (inter.v < 0.0f || inter.obj_index < 0 || cache.path_len == RAD_CACHE_PROPAGATION_DEPTH) {
for (int j = 0; j < cache.path_len; ++j) {
rad *= make_fvec3(cache.sample_weight[j]);
if (cache.cache_entries[j] != HASH_GRID_INVALID_CACHE_ENTRY) {
Expand All @@ -201,12 +203,12 @@ void Ray::Ref::SpatialCacheUpdate(const cache_grid_params_t &params, Span<const
memcpy(cache.sample_weight[j], cache.sample_weight[j - 1], 3 * sizeof(float));
}

cache.sample_weight[0][0] = cache.sample_weight[0][1] = cache.sample_weight[0][2] = 1.0f;
cache.cache_entries[0] = insert_entry(entries, P, N, params);
if (cache.cache_entries[0] != HASH_GRID_INVALID_CACHE_ENTRY) {
accumulate_cache_voxel(voxels_curr[cache.cache_entries[0]], rad, 1);
}

cache.path_len = std::min(cache.path_len + 1, RAD_CACHE_PROPAGATION_DEPTH - 1);
++cache.path_len;

for (int j = 1; j < cache.path_len; ++j) {
rad *= make_fvec3(cache.sample_weight[j]);
Expand Down
14 changes: 6 additions & 8 deletions internal/ShadeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ Ray::color_rgba_t Ray::Ref::ShadeSurface(const pass_settings_t &ps, const float
surf.T = cross(surf.N, surf.B);
#endif

if (cache_mode == eSpatialCacheMode::Query) {
if (cache_mode == eSpatialCacheMode::Query && mat->type != eShadingNode::Emissive) {
const fvec2 cache_rand = get_scrambled_2d_rand(rand_dim + RAND_DIM_CACHE, rand_hash, iteration - 1, rand_seq);

const uint32_t grid_level = calc_grid_level(surf.P, sc.spatial_cache_grid);
Expand All @@ -1348,14 +1348,12 @@ Ray::color_rgba_t Ray::Ref::ShadeSurface(const pass_settings_t &ps, const float
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);

fvec4 color = fvec4{unpacked.radiance[0], unpacked.radiance[1], unpacked.radiance[2], 0.0f};
if (unpacked.sample_count) {
color /= float(unpacked.sample_count);
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};
return color_rgba_t{color.get<0>(), color.get<1>(), color.get<2>(), color.get<3>()};
}
color /= sc.spatial_cache_grid.exposure;
color *= fvec4{ray.c[0], ray.c[1], ray.c[2], 0.0f};
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: 3 additions & 3 deletions internal/shaders/output/spatial_cache_update.comp.cso.inl

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions internal/shaders/output/spatial_cache_update.comp.spv.inl

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions internal/shaders/shade.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2115,33 +2115,33 @@ vec3 ShadeSurface(const hit_data_t inter, const ray_data_t ray, inout vec3 out_b
#endif

#if CACHE_QUERY
cache_grid_params_t params;
params.cam_pos_curr = g_params.cam_pos_and_exposure.xyz;
params.log_base = RAD_CACHE_GRID_LOGARITHM_BASE;
params.scale = RAD_CACHE_GRID_SCALE;
params.exposure = g_params.cam_pos_and_exposure.w;

const vec2 cache_rand = get_scrambled_2d_rand(rand_dim + RAND_DIM_CACHE, rand_hash, g_params.iteration - 1);

const uint grid_level = calc_grid_level(surf.P, params);
const float voxel_size = calc_voxel_size(grid_level, params);

bool use_cache = get_diff_depth(ray.depth) > 0;
use_cache = use_cache || (cone_width > mix(1.0, 1.5, cache_rand.x) * voxel_size);
use_cache = use_cache && (inter.t > mix(1.0, 2.0, cache_rand.y) * voxel_size);
if (use_cache) {
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);

vec3 color = unpacked.radiance;
if (unpacked.sample_count > 0) {
color /= float(unpacked.sample_count);
if (mat.type != EmissiveNode) {
cache_grid_params_t params;
params.cam_pos_curr = g_params.cam_pos_and_exposure.xyz;
params.log_base = RAD_CACHE_GRID_LOGARITHM_BASE;
params.scale = RAD_CACHE_GRID_SCALE;
params.exposure = g_params.cam_pos_and_exposure.w;

const vec2 cache_rand = get_scrambled_2d_rand(rand_dim + RAND_DIM_CACHE, rand_hash, g_params.iteration - 1);

const uint grid_level = calc_grid_level(surf.P, params);
const float voxel_size = calc_voxel_size(grid_level, params);

bool use_cache = get_diff_depth(ray.depth) > 0;
use_cache = use_cache || (cone_width > mix(1.0, 1.5, cache_rand.x) * voxel_size);
use_cache = use_cache && (inter.t > mix(1.0, 2.0, cache_rand.y) * voxel_size);
if (use_cache) {
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);
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]);
return color;
}
}
color /= params.exposure;
color *= vec3(ray.c[0], ray.c[1], ray.c[2]);
return color;
}
}
#endif
Expand Down
12 changes: 6 additions & 6 deletions internal/shaders/spatial_cache_update.comp.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ void main() {
vec3 rad = texelFetch(g_radiance_tex, ivec2(x, y), 0).rgb * grid_params.exposure;

cache_data_t cache = g_inout_cache_data[y * g_params.cache_w + x];
for (int k = 0; k < 3; ++k) {
cache.sample_weight[0][k] = ray.c[k];
}
if (inter.v < 0.0 || inter.obj_index < 0) {
cache.sample_weight[0][0] = ray.c[0];
cache.sample_weight[0][1] = ray.c[1];
cache.sample_weight[0][2] = ray.c[2];
if (inter.v < 0.0 || inter.obj_index < 0 || cache.path_len == RAD_CACHE_PROPAGATION_DEPTH) {
for (int j = 0; j < cache.path_len; ++j) {
rad *= vec3(cache.sample_weight[j][0], cache.sample_weight[j][1], cache.sample_weight[j][2]);
if (cache.cache_entries[j] != HASH_GRID_INVALID_CACHE_ENTRY) {
Expand All @@ -158,12 +158,12 @@ void main() {
}
}

cache.sample_weight[0][0] = cache.sample_weight[0][1] = cache.sample_weight[0][2] = 1.0;
cache.cache_entries[0] = insert_entry(P, N, grid_params);
if (cache.cache_entries[0] != HASH_GRID_INVALID_CACHE_ENTRY) {
accumulate_cache_voxel(cache.cache_entries[0], rad, 1);
}

cache.path_len = min(cache.path_len + 1, RAD_CACHE_PROPAGATION_DEPTH - 1);
++cache.path_len;

for (int j = 1; j < cache.path_len; ++j) {
rad *= vec3(cache.sample_weight[j][0], cache.sample_weight[j][1], cache.sample_weight[j][2]);
Expand Down

0 comments on commit 981bc25

Please sign in to comment.