Skip to content

Commit

Permalink
re-using accelration-assets across frames, minor refactoring in shade…
Browse files Browse the repository at this point in the history
…rs and doc
  • Loading branch information
crocdialer committed Jul 1, 2023
1 parent e04af24 commit 28e41e8
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 19 deletions.
3 changes: 3 additions & 0 deletions include/vierkant/RayBuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ class RayBuilder

//! request to provide all vertex/index/material-buffers and textures.
bool use_scene_assets = true;

//! optionally provide a handle to a previous context, in order to re-use existing acceleration-assets.
const scene_acceleration_context_t* previous_context = nullptr;
};

/**
Expand Down
10 changes: 10 additions & 0 deletions include/vierkant/RayTracer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ class RayTracer
*/
void trace_rays(tracable_t tracable, VkCommandBuffer commandbuffer);

/**
* @return the current frame-index.
*/
[[nodiscard]] uint32_t current_index() const { return m_current_index; }

/**
* @return the number of concurrent (in-flight) frames.
*/
[[nodiscard]] uint32_t num_concurrent_frames() const { return static_cast<uint32_t>(m_trace_assets.size()); }

friend void swap(RayTracer &lhs, RayTracer &rhs) noexcept;

private:
Expand Down
2 changes: 1 addition & 1 deletion shaders/fullscreen/ao_rayquery.frag
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ float raytraced_occlusion(vec3 position, vec3 world_normal, float max_distance,
for(uint i = 0; i < num_rays; ++i)
{
vec2 Xi = fract(Hammersley(i, num_rays) + vec2(sample_offset));
vec3 cos_dir = sample_cosine(Xi);
vec3 cos_dir = sample_hemisphere_cosine(Xi);
vec3 direction = frame * cos_dir;

rayQueryEXT query;
Expand Down
2 changes: 1 addition & 1 deletion shaders/fullscreen/ao_screenspace.frag
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ float screenspace_occlusion(sampler2D depth_sampler, vec2 coord, vec3 eye_normal
{
// sample a direction from cosine distribution
vec2 Xi = fract(Hammersley(i, SSAO_KERNEL_SIZE) + vec2(sample_offset));
vec3 direction = frame * sample_cosine(Xi);
vec3 direction = frame * sample_hemisphere_cosine(Xi);

// project
vec3 new_pos = position + direction * ssao_radius;
Expand Down
2 changes: 1 addition & 1 deletion shaders/ray/bsdf_disney.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ bsdf_sample_t sample_disney(in material_t material, vec3 N, vec3 V, float eta, i
// reflection - diffuse
if (rnd(rng_state) < diffuseRatio)
{
ret.direction = frame * sample_cosine(Xi);
ret.direction = frame * sample_hemisphere_cosine(Xi);

H = normalize(ret.direction + V);

Expand Down
2 changes: 1 addition & 1 deletion shaders/renderer/importance_sampling.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ vec3 ImportanceSampleDiffuse(vec3 N, samplerCube cubemap)
for (uint i = 0; i < numSamples; ++i)
{
vec2 Xi = Hammersley(i, numSamples);
vec3 L = local_frame * sample_cosine(Xi);
vec3 L = local_frame * sample_hemisphere_cosine(Xi);

float NoL = max(dot(N, L), 0.0);

Expand Down
4 changes: 2 additions & 2 deletions shaders/utils/sampling.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ vec3 sample_unit_sphere_cap(vec2 Xi, float sigma)
}

//! random point on a unit-hemisphere
vec3 sample_unit_hemisphere(vec2 Xi)
vec3 sample_hemisphere_uniform(vec2 Xi)
{
// [0, 2pi]
const float theta = 2.0 * PI * Xi.y;
Expand All @@ -66,7 +66,7 @@ vec3 sample_unit_hemisphere(vec2 Xi)
}

//! sample a cosine-weighted hemisphere-distribution
vec3 sample_cosine(vec2 Xi)
vec3 sample_hemisphere_cosine(vec2 Xi)
{
float cosTheta = sqrt(max(1.0 - Xi.y, 0.0));
float sinTheta = sqrt(max(1.0 - cosTheta * cosTheta, 0.0));
Expand Down
3 changes: 3 additions & 0 deletions src/PBRDeferred.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// Created by crocdialer on 6/19/20.
//
//#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG

#include <crocore/gaussian.hpp>

Expand All @@ -18,6 +19,8 @@ namespace vierkant
PBRDeferred::PBRDeferred(const DevicePtr &device, const create_info_t &create_info) : m_device(device)
{
m_logger = create_info.logger_name.empty() ? spdlog::default_logger() : spdlog::get(create_info.logger_name);
// m_logger->set_pattern("[%Y-%m-%d %H:%M:%S %z] %v [%! | %s:%#]");
// SPDLOG_LOGGER_DEBUG(m_logger, "PBRDeferred initialized");
m_logger->debug("PBRDeferred initialized");

m_queue = create_info.queue ? create_info.queue : device->queue();
Expand Down
5 changes: 5 additions & 0 deletions src/PBRPathTracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ void PBRPathTracer::update_trace_descriptors(frame_asset_t &frame_asset, const C
void PBRPathTracer::update_acceleration_structures(PBRPathTracer::frame_asset_t &frame_asset,
const SceneConstPtr &scene, const std::set<std::string> & /*tags*/)
{
size_t last_index = (m_ray_tracer.current_index() + m_ray_tracer.num_concurrent_frames() - 1) %
m_ray_tracer.num_concurrent_frames();
const auto &last_context = m_frame_assets[last_index].scene_acceleration_context;

// set environment
m_environment = scene->environment();
bool use_environment = m_environment && frame_asset.settings.draw_skybox;
Expand All @@ -484,6 +488,7 @@ void PBRPathTracer::update_acceleration_structures(PBRPathTracer::frame_asset_t
build_scene_params.scene = scene;
build_scene_params.use_compaction = frame_asset.settings.compaction;
build_scene_params.use_scene_assets = true;
build_scene_params.previous_context = last_context.get();
frame_asset.scene_ray_acceleration =
m_ray_builder.build_scene_acceleration(frame_asset.scene_acceleration_context, build_scene_params);
}
Expand Down
24 changes: 11 additions & 13 deletions src/RayBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,21 +740,19 @@ RayBuilder::build_scene_acceleration(const scene_acceleration_context_ptr &conte
auto &prev_accleration_assets = prev_it->second;

// reset scratch-buffers for acceleration-assets we keep
// for(auto &acceleration_asset: prev_accleration_assets) { acceleration_asset->scratch_buffer.reset(); }
for(auto &acceleration_asset: prev_accleration_assets) { acceleration_asset->scratch_buffer.reset(); }
context->mesh_assets[mesh] = prev_accleration_assets;
}
// else
// {
// // no previous acceleration-structure, check other frames
// for(const auto &asset: m_frame_assets)
// {
// if(&asset != &frame_asset)
// {
// auto mesh_it = asset.mesh_assets.find(mesh);
// if(mesh_it != asset.mesh_assets.end()) { frame_asset.mesh_assets[mesh] = mesh_it->second; }
// }
// }
// }
else if(params.previous_context)
{
// optionally try re-using existing assets from a previous context
auto prev_context_it = params.previous_context->mesh_assets.find(mesh);

if(prev_context_it != params.previous_context->mesh_assets.end())
{
context->mesh_assets[mesh] = prev_context_it->second;
}
}
}

if((!use_mesh_compute && !context->mesh_assets.contains(mesh)) ||
Expand Down

0 comments on commit 28e41e8

Please sign in to comment.