Skip to content

Commit

Permalink
Use separate iteration index for cache update
Browse files Browse the repository at this point in the history
  • Loading branch information
sergcpp committed Jun 17, 2024
1 parent eb418a6 commit 3eb44b1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions RendererBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class RegionContext {

public:
int iteration = 0; ///< Number of rendered samples per pixel
int cache_iteration = 0;

explicit RegionContext(const rect_t &rect) : rect_(rect) {}

Expand Down
18 changes: 10 additions & 8 deletions internal/RendererCPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,8 @@ void Ray::Cpu::Renderer<SIMDPolicy>::UpdateSpatialCache(const SceneBase &scene,

std::shared_lock<std::shared_timed_mutex> scene_lock(s.mtx_);

++region.cache_iteration;

camera_t cam = s.cams_[s.current_cam()._index];
cam.fstop = 0.0f;
cam.filter = ePixelFilter::Box;
Expand Down Expand Up @@ -1064,16 +1066,16 @@ void Ray::Cpu::Renderer<SIMDPolicy>::UpdateSpatialCache(const SceneBase &scene,
const auto time_start = high_resolution_clock::now();

const uint32_t *rand_seq = __pmj02_samples;
const uint32_t rand_seed = Ref::hash(Ref::hash(region.iteration / RAND_SAMPLES_COUNT));
const uint32_t rand_seed = Ref::hash(Ref::hash((region.cache_iteration - 1) / RAND_SAMPLES_COUNT));

if (cam.type != eCamType::Geo) {
SIMDPolicy::GeneratePrimaryRays(cam, rect, (w_ / RAD_CACHE_DOWNSAMPLING_FACTOR),
(h_ / RAD_CACHE_DOWNSAMPLING_FACTOR), rand_seq, rand_seed, nullptr,
region.iteration + 1, nullptr, p.primary_rays, p.intersections);
region.cache_iteration, nullptr, p.primary_rays, p.intersections);
if (tlas_root != 0xffffffff) {
SIMDPolicy::TraceRays(p.primary_rays, cam.pass_settings.min_transp_depth,
cam.pass_settings.max_transp_depth, sc_data, tlas_root, false, s.tex_storages_,
rand_seq, rand_seed, region.iteration + 1, p.intersections);
rand_seq, rand_seed, region.cache_iteration, p.intersections);
}
} else {
assert(false && "Unsupported camera type!");
Expand All @@ -1085,13 +1087,13 @@ void Ray::Cpu::Renderer<SIMDPolicy>::UpdateSpatialCache(const SceneBase &scene,
int secondary_rays_count = 0, shadow_rays_count = 0;

SIMDPolicy::ShadePrimary(cam.pass_settings, p.intersections, p.primary_rays, rand_seq, rand_seed,
region.iteration + 1, eSpatialCacheMode::Update, sc_data, s.tex_storages_,
region.cache_iteration, eSpatialCacheMode::Update, sc_data, s.tex_storages_,
&p.secondary_rays[0], &secondary_rays_count, &p.shadow_rays[0], &shadow_rays_count,
nullptr, nullptr, w_, 1.0f, temp_buf_.data(), nullptr, raw_filtered_buf_.data());

SIMDPolicy::TraceShadowRays(Span<typename SIMDPolicy::ShadowRayType>{p.shadow_rays.data(), shadow_rays_count},
cam.pass_settings.max_transp_depth, cam.pass_settings.clamp_direct, sc_data, tlas_root,
rand_seq, rand_seed, region.iteration + 1, s.tex_storages_, w_, temp_buf_.data());
rand_seq, rand_seed, region.cache_iteration, s.tex_storages_, w_, temp_buf_.data());

rect_fill<cache_data_t>(temp_cache_data_, (w_ / RAD_CACHE_DOWNSAMPLING_FACTOR), rect, cache_data_t{});
SIMDPolicy::SpatialCacheUpdate(cache_grid_params, p.intersections, p.primary_rays, temp_cache_data_,
Expand All @@ -1116,7 +1118,7 @@ void Ray::Cpu::Renderer<SIMDPolicy>::UpdateSpatialCache(const SceneBase &scene,
auto intersections = Span<typename SIMDPolicy::HitDataType>{p.intersections.data(), secondary_rays_count};

SIMDPolicy::TraceRays(rays, cam.pass_settings.min_transp_depth, cam.pass_settings.max_transp_depth, sc_data,
tlas_root, true, s.tex_storages_, rand_seq, rand_seed, region.iteration + 1,
tlas_root, true, s.tex_storages_, rand_seq, rand_seed, region.cache_iteration,
p.intersections);

secondary_rays_count = 0;
Expand All @@ -1126,13 +1128,13 @@ void Ray::Cpu::Renderer<SIMDPolicy>::UpdateSpatialCache(const SceneBase &scene,
// Use direct clamping value only for the first intersection with lightsource
const float clamp_direct = (bounce == 1) ? cam.pass_settings.clamp_direct : cam.pass_settings.clamp_indirect;
SIMDPolicy::ShadeSecondary(cam.pass_settings, clamp_direct, intersections, rays, rand_seq, rand_seed,
region.iteration + 1, eSpatialCacheMode::Update, sc_data, s.tex_storages_,
region.cache_iteration, eSpatialCacheMode::Update, sc_data, s.tex_storages_,
&p.secondary_rays[0], &secondary_rays_count, &p.shadow_rays[0], &shadow_rays_count,
nullptr, nullptr, w_, temp_buf_.data(), nullptr, raw_filtered_buf_.data());

SIMDPolicy::TraceShadowRays(Span<typename SIMDPolicy::ShadowRayType>{p.shadow_rays.data(), shadow_rays_count},
cam.pass_settings.max_transp_depth, cam.pass_settings.clamp_indirect, sc_data,
tlas_root, rand_seq, rand_seed, region.iteration + 1, s.tex_storages_, w_,
tlas_root, rand_seq, rand_seed, region.cache_iteration, s.tex_storages_, w_,
temp_buf_.data());

SIMDPolicy::SpatialCacheUpdate(cache_grid_params, intersections, rays, temp_cache_data_, temp_buf_.data(),
Expand Down
18 changes: 10 additions & 8 deletions internal/RendererDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,8 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext

const auto &s = dynamic_cast<const Dx::Scene &>(scene);

++region.cache_iteration;

const uint32_t tlas_root = s.tlas_root_;

float root_min[3], cell_size[3];
Expand Down Expand Up @@ -1476,15 +1478,15 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
s.bindless_tex_data_.srv_descr_table.gpu_ptr = srv_gpu_handle.ptr;
}

const uint32_t rand_seed = Ref::hash(Ref::hash(region.iteration / RAND_SAMPLES_COUNT));
const uint32_t rand_seed = Ref::hash(Ref::hash((region.cache_iteration - 1) / RAND_SAMPLES_COUNT));

timestamps_[ctx_->backend_frame].cache_update[0] = ctx_->WriteTimestamp(cmd_buf, true);

{ // generate primary rays
DebugMarker _(ctx_.get(), cmd_buf, "GeneratePrimaryRays");
kernel_GeneratePrimaryRays(cmd_buf, cam, rand_seed, rect, (w_ / RAD_CACHE_DOWNSAMPLING_FACTOR),
(h_ / RAD_CACHE_DOWNSAMPLING_FACTOR), random_seq_buf_, filter_table_,
region.iteration + 1, false, required_samples_buf_, counters_buf_, prim_rays_buf_);
region.cache_iteration, false, required_samples_buf_, counters_buf_, prim_rays_buf_);
}

{ // prepare indirect args
Expand All @@ -1495,7 +1497,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{ // trace primary rays
DebugMarker _(ctx_.get(), cmd_buf, "IntersectScenePrimary");
kernel_IntersectScene(cmd_buf, indir_args_buf_[0], 0, counters_buf_, cam.pass_settings, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root, cam.fwd,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root, cam.fwd,
cam.clip_end - cam.clip_start, s.tex_atlases_, s.bindless_tex_data_, prim_rays_buf_,
prim_hits_buf_);
}
Expand All @@ -1504,7 +1506,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
DebugMarker _(ctx_.get(), cmd_buf, "ShadePrimaryHits");
kernel_ShadePrimaryHits(cmd_buf, cam.pass_settings, eSpatialCacheMode::Update, s.env_, indir_args_buf_[0], 0,
prim_hits_buf_, prim_rays_buf_, sc_data, random_seq_buf_, rand_seed,
region.iteration + 1, rect, s.tex_atlases_, s.bindless_tex_data_, temp_buf0_,
region.cache_iteration, rect, s.tex_atlases_, s.bindless_tex_data_, temp_buf0_,
secondary_rays_buf_, shadow_rays_buf_, {}, counters_buf_, temp_buf1_,
temp_depth_normals_buf_);
}
Expand All @@ -1517,7 +1519,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{ // trace shadow rays
DebugMarker _(ctx_.get(), cmd_buf, "TraceShadow");
kernel_IntersectSceneShadow(cmd_buf, cam.pass_settings, indir_args_buf_[1], 2, counters_buf_, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root,
cam.pass_settings.clamp_direct, s.tex_atlases_, s.bindless_tex_data_,
shadow_rays_buf_, temp_buf0_);
}
Expand Down Expand Up @@ -1552,7 +1554,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{ // trace secondary rays
DebugMarker _(ctx_.get(), cmd_buf, "IntersectSceneSecondary");
kernel_IntersectScene(cmd_buf, indir_args_buf_[1], 0, counters_buf_, cam.pass_settings, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root, nullptr, -1.0f,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root, nullptr, -1.0f,
s.tex_atlases_, s.bindless_tex_data_, secondary_rays_buf_, prim_hits_buf_);
}

Expand All @@ -1567,7 +1569,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
const float clamp_val = (bounce == 1) ? cam.pass_settings.clamp_direct : cam.pass_settings.clamp_indirect;
kernel_ShadeSecondaryHits(cmd_buf, cam.pass_settings, eSpatialCacheMode::Update, clamp_val, s.env_,
indir_args_buf_[1], 0, prim_hits_buf_, secondary_rays_buf_, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, s.tex_atlases_,
random_seq_buf_, rand_seed, region.cache_iteration, s.tex_atlases_,
s.bindless_tex_data_, temp_buf0_, prim_rays_buf_, shadow_rays_buf_, {},
counters_buf_, temp_depth_normals_buf_);
}
Expand All @@ -1580,7 +1582,7 @@ void Ray::Dx::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{ // trace shadow rays
DebugMarker _(ctx_.get(), cmd_buf, "TraceShadow");
kernel_IntersectSceneShadow(cmd_buf, cam.pass_settings, indir_args_buf_[0], 2, counters_buf_, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root,
cam.pass_settings.clamp_indirect, s.tex_atlases_, s.bindless_tex_data_,
shadow_rays_buf_, temp_buf0_);
}
Expand Down
22 changes: 12 additions & 10 deletions internal/RendererVK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,8 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext

const auto &s = dynamic_cast<const Vk::Scene &>(scene);

++region.cache_iteration;

const uint32_t tlas_root = s.tlas_root_;

float root_min[3], cell_size[3];
Expand Down Expand Up @@ -1491,15 +1493,15 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
ctx_->supported_stages_mask(),
0, 1, &mem_barrier, 0, nullptr, 0, nullptr);

const uint32_t rand_seed = Ref::hash(Ref::hash(region.iteration / RAND_SAMPLES_COUNT));
const uint32_t rand_seed = Ref::hash(Ref::hash((region.cache_iteration - 1) / RAND_SAMPLES_COUNT));

timestamps_[ctx_->backend_frame].cache_update[0] = ctx_->WriteTimestamp(cmd_buf, true);

{ // generate primary rays
DebugMarker _(ctx_.get(), cmd_buf, "GeneratePrimaryRays");
kernel_GeneratePrimaryRays(cmd_buf, cam, rand_seed, rect, (w_ / RAD_CACHE_DOWNSAMPLING_FACTOR),
(h_ / RAD_CACHE_DOWNSAMPLING_FACTOR), random_seq_buf_, filter_table_,
region.iteration + 1, false, required_samples_buf_, counters_buf_, prim_rays_buf_);
region.cache_iteration, false, required_samples_buf_, counters_buf_, prim_rays_buf_);
}

const bool use_rt_pipeline = (use_hwrt_ && ENABLE_RT_PIPELINE);
Expand All @@ -1513,12 +1515,12 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
DebugMarker _(ctx_.get(), cmd_buf, "IntersectScenePrimary");
if (use_rt_pipeline) {
kernel_IntersectScene_RTPipe(cmd_buf, indir_args_buf_[0], 1, cam.pass_settings, sc_data, random_seq_buf_,
rand_seed, region.iteration + 1, tlas_root, cam.fwd,
rand_seed, region.cache_iteration, tlas_root, cam.fwd,
cam.clip_end - cam.clip_start, s.tex_atlases_, s.bindless_tex_data_,
prim_rays_buf_, prim_hits_buf_);
} else {
kernel_IntersectScene(cmd_buf, indir_args_buf_[0], 0, counters_buf_, cam.pass_settings, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root, cam.fwd,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root, cam.fwd,
cam.clip_end - cam.clip_start, s.tex_atlases_, s.bindless_tex_data_, prim_rays_buf_,
prim_hits_buf_);
}
Expand All @@ -1528,7 +1530,7 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
DebugMarker _(ctx_.get(), cmd_buf, "ShadePrimaryHits");
kernel_ShadePrimaryHits(cmd_buf, cam.pass_settings, eSpatialCacheMode::Update, s.env_, indir_args_buf_[0], 0,
prim_hits_buf_, prim_rays_buf_, sc_data, random_seq_buf_, rand_seed,
region.iteration + 1, rect, s.tex_atlases_, s.bindless_tex_data_, temp_buf0_,
region.cache_iteration, rect, s.tex_atlases_, s.bindless_tex_data_, temp_buf0_,
secondary_rays_buf_, shadow_rays_buf_, {}, counters_buf_, temp_buf1_,
temp_depth_normals_buf_);
}
Expand All @@ -1541,7 +1543,7 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{ // trace shadow rays
DebugMarker _(ctx_.get(), cmd_buf, "TraceShadow");
kernel_IntersectSceneShadow(cmd_buf, cam.pass_settings, indir_args_buf_[1], 2, counters_buf_, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root,
cam.pass_settings.clamp_direct, s.tex_atlases_, s.bindless_tex_data_,
shadow_rays_buf_, temp_buf0_);
}
Expand Down Expand Up @@ -1577,12 +1579,12 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
DebugMarker _(ctx_.get(), cmd_buf, "IntersectSceneSecondary");
if (use_rt_pipeline) {
kernel_IntersectScene_RTPipe(cmd_buf, indir_args_buf_[1], 1, cam.pass_settings, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root, nullptr,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root, nullptr,
-1.0f, s.tex_atlases_, s.bindless_tex_data_, secondary_rays_buf_,
prim_hits_buf_);
} else {
kernel_IntersectScene(cmd_buf, indir_args_buf_[1], 0, counters_buf_, cam.pass_settings, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root, nullptr, -1.0f,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root, nullptr, -1.0f,
s.tex_atlases_, s.bindless_tex_data_, secondary_rays_buf_, prim_hits_buf_);
}
}
Expand All @@ -1598,7 +1600,7 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
const float clamp_val = (bounce == 1) ? cam.pass_settings.clamp_direct : cam.pass_settings.clamp_indirect;
kernel_ShadeSecondaryHits(cmd_buf, cam.pass_settings, eSpatialCacheMode::Update, clamp_val, s.env_,
indir_args_buf_[1], 0, prim_hits_buf_, secondary_rays_buf_, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, s.tex_atlases_,
random_seq_buf_, rand_seed, region.cache_iteration, s.tex_atlases_,
s.bindless_tex_data_, temp_buf0_, prim_rays_buf_, shadow_rays_buf_, {},
counters_buf_, temp_depth_normals_buf_);
}
Expand All @@ -1611,7 +1613,7 @@ void Ray::Vk::Renderer::UpdateSpatialCache(const SceneBase &scene, RegionContext
{ // trace shadow rays
DebugMarker _(ctx_.get(), cmd_buf, "TraceShadow");
kernel_IntersectSceneShadow(cmd_buf, cam.pass_settings, indir_args_buf_[0], 2, counters_buf_, sc_data,
random_seq_buf_, rand_seed, region.iteration + 1, tlas_root,
random_seq_buf_, rand_seed, region.cache_iteration, tlas_root,
cam.pass_settings.clamp_indirect, s.tex_atlases_, s.bindless_tex_data_,
shadow_rays_buf_, temp_buf0_);
}
Expand Down

0 comments on commit 3eb44b1

Please sign in to comment.