Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Hineven committed May 17, 2024
1 parent cb2393a commit feee415
Show file tree
Hide file tree
Showing 7 changed files with 1,032 additions and 1,788 deletions.
2,397 changes: 859 additions & 1,538 deletions src/core/src/render_techniques/migi/migi.comp

Large diffs are not rendered by default.

49 changes: 8 additions & 41 deletions src/core/src/render_techniques/migi/migi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ light_sampler->addProgramParameters(capsaicin, kernels_.program);
gfxProgramSetParameter(gfx_, kernels_.program, "g_PreviousUpdateErrorSplatTexture", tex_.update_error_splat[!(internal_frame_index_ & 1)]);


static_assert(SSRC_TILE_SIZE == 8);
gfxProgramSetParameter(gfx_, kernels_.program, "g_TileHiZ_Min", tex_.HiZ_min, 2);
gfxProgramSetParameter(gfx_, kernels_.program, "g_TileHiZ_Max", tex_.HiZ_max, 2);
static_assert(SSRC_TILE_SIZE == 16);
gfxProgramSetParameter(gfx_, kernels_.program, "g_TileHiZ_Min", tex_.HiZ_min, 3);
gfxProgramSetParameter(gfx_, kernels_.program, "g_TileHiZ_Max", tex_.HiZ_max, 3);


gfxProgramSetParameter(gfx_, kernels_.program, "g_ScreenCacheDimensions", glm::int2(options_.width, options_.height));
Expand Down Expand Up @@ -850,48 +850,15 @@ light_sampler->addProgramParameters(capsaicin, kernels_.program);
// Specify whether the GI output is copied to debug drawing as a background
bool debug_buffer_copied = false;

if(options_.active_debug_view == "SSRC_Coverage") {
const TimedSection timed_section(*this, "SSRC_Coverage");
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_visualize_coverage);
auto threads = gfxKernelGetNumThreads(gfx_, kernels_.DebugSSRC_visualize_coverage);
uint dispatch_size[] = {(options_.width + threads[0]-1) / threads[0], (options_.height + threads[1]-1) / threads[1]};
gfxCommandDispatch(gfx_, dispatch_size[0], dispatch_size[1], 1);
} else if(options_.active_debug_view == "SSRC_TileOccupancy") {
const TimedSection timed_section(*this, "SSRC_TileOccupancy");
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_visualize_tile_occupancy);
uint dispatch_size[] = {options_.width / SSRC_TILE_SIZE, options_.height / SSRC_TILE_SIZE};
gfxCommandDispatch(gfx_, dispatch_size[0], dispatch_size[1], 1);
} else if(options_.active_debug_view == "SSRC_Basis") {
const TimedSection timed_section(*this, "SSRC_Basis");
gfxCommandClearTexture(gfx_, capsaicin.getAOVBuffer("Debug"));
gfxCommandClearTexture(gfx_, tex_.depth);
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_basis);
gfxCommandMultiDrawIndexedIndirect(gfx_, buf_.draw_indexed_command, 1);
} else if(options_.active_debug_view == "SSRC_Basis3D") {
const TimedSection timed_section(*this, "SSRC_Basis3D");
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_generate_draw_indexed);
gfxCommandCopyTexture(gfx_, capsaicin.getAOVBuffer("Debug"), gi_output_aov);
gfxCommandClearTexture(gfx_, tex_.depth);
// Reuse the index buffer for the disk {0, 1, 2, ...} and wireframe draw mode for just 3 points
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_basis_3D);
gfxCommandMultiDrawIndexedIndirect(gfx_, buf_.draw_indexed_command, 1);
} else if(options_.active_debug_view == "SSRC_Difference") {
const TimedSection timed_section(*this, "SSRC_Difference");
if(options_.active_debug_view == "SSRC_ProbeAllocation") {
// TODO
} else if(options_.active_debug_view == "SSRC_Complexity") {
const TimedSection timed_section(*this, "SSRC_Complexity");
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_show_difference);
auto threads = gfxKernelGetNumThreads(gfx_, kernels_.DebugSSRC_show_difference);
uint dispatch_size[] = {divideAndRoundUp(options_.width, threads[0]), divideAndRoundUp(options_.height, threads[1])};
gfxCommandDispatch(gfx_, dispatch_size[0], dispatch_size[1], 1);
} else if(options_.active_debug_view == "SSRC_IncidentRadiance") {
// Visualize basis first
{
const TimedSection timed_section(*this, "SSRC_Basis3D");
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_generate_draw_indexed);
gfxCommandCopyTexture(gfx_, capsaicin.getAOVBuffer("Debug"), gi_output_aov);
gfxCommandClearTexture(gfx_, tex_.depth);
// Reuse the index buffer for the disk {0, 1, 2, ...} and wireframe draw mode for just 3 points
gfxCommandBindKernel(gfx_, kernels_.DebugSSRC_basis_3D);
gfxCommandMultiDrawIndexedIndirect(gfx_, buf_.draw_indexed_command, 1);
}
const TimedSection timed_section(*this, "SSRC_IncidentRadiance");
if(options_.cursor_dragging)
{
Expand Down Expand Up @@ -955,7 +922,7 @@ light_sampler->addProgramParameters(capsaicin, kernels_.program);
gfxCommandCopyBuffer(gfx_, buf_.readback[copy_idx], 0, buf_.active_basis_count, 0, sizeof(uint32_t));
// Removed for now
// gfxCommandCopyBuffer(gfx_, buf_.readback[copy_idx], 4, buf_.update_step_scale, 0, sizeof(float));
gfxCommandCopyBuffer(gfx_, buf_.readback[copy_idx], 8, buf_.update_ray_count, 0, sizeof(uint32_t));
// gfxCommandCopyBuffer(gfx_, buf_.readback[copy_idx], 8, buf_.update_ray_count, 0, sizeof(uint32_t));
gfxCommandCopyBuffer(gfx_, buf_.readback[copy_idx], 12, buf_.debug_visualize_incident_radiance_sum, 0, sizeof(float));
readback_pending_[copy_idx] = true;
}
Expand Down
129 changes: 40 additions & 89 deletions src/core/src/render_techniques/migi/migi.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,26 @@ class MIGI : public RenderTechnique
} tex_ {};

struct MIGIBuffers {
GfxBuffer active_basis_count {};
GfxBuffer active_basis_index {};
GfxBuffer basis_effective_radius {};
GfxBuffer basis_film_position {};
GfxBuffer basis_effective_radius_film {};
GfxBuffer basis_location {};
GfxBuffer basis_parameter {};
GfxBuffer quantilized_basis_step {};
GfxBuffer basis_average_gradient_scale {};
GfxBuffer basis_flags {};
GfxBuffer free_basis_indices {};
GfxBuffer free_basis_indices_count {};
GfxBuffer tile_basis_count {};
GfxBuffer tile_ray_count {};
GfxBuffer tile_ray_offset {};
GfxBuffer update_ray_direction {};
GfxBuffer update_ray_origin {};
GfxBuffer update_ray_radiance_inv_pdf {};
GfxBuffer update_ray_cache {};
GfxBuffer update_ray_count {};
GfxBuffer tile_update_error_sums {};
GfxBuffer tile_update_error {};
GfxBuffer tile_basis_index_injection {};
GfxBuffer tile_base_slot_offset {};
GfxBuffer tile_basis_index {};

GfxBuffer count {};
GfxBuffer dispatch_command {};
GfxBuffer dispatch_rays_command {};
GfxBuffer dispatch_count {};
GfxBuffer draw_command {};
GfxBuffer draw_indexed_command {};
GfxBuffer reduce_count {};
GfxBuffer probe_SG[2] {};
GfxBuffer allocated_probe_SG_count {};
GfxBuffer probe_update_ray_count {};
GfxBuffer probe_update_ray_offset {};
GfxBuffer update_ray_probe {};
GfxBuffer update_ray_direction {};
GfxBuffer update_ray_radiance_inv_pdf {};
GfxBuffer update_ray_linear_depth {};
GfxBuffer adaptive_probe_count {};
GfxBuffer probe_update_error {};

GfxBuffer debug_cursor_world_pos {};
GfxBuffer debug_visualize_incident_radiance {};
GfxBuffer debug_visualize_incident_radiance_sum {};
GfxBuffer debug_cursor_world_pos {};

GfxBuffer disk_index_buffer {};

GfxBuffer readback[kGfxConstant_BackBufferCount] {};
} buf_{};
Expand All @@ -126,65 +108,34 @@ class MIGI : public RenderTechnique

GfxProgram program {};

GfxKernel precompute_HiZ_min {};
GfxKernel precompute_HiZ_max {};

GfxKernel SSRC_clear_active_counter {};
GfxKernel SSRC_reproject_and_filter {};
GfxKernel SSRC_clear_tile_injection_index {};
GfxKernel SSRC_inject_generate_draw_indexed {};
GfxKernel SSRC_inject_reprojected_basis {};
GfxKernel SSRC_clip_overflow_tile_index {};
GfxKernel SSRC_allocate_extra_slot_for_basis_generation {};
GfxKernel SSRC_compress_tile_basis_index {};
GfxKernel SSRC_reproject_previous_update_error {};
GfxKernel SSRC_precompute_ray_budget_for_tiles {};
GfxKernel SSRC_tiles_set_reduce_count_32 {};
GfxKernel SSRC_tiles_set_reduce_count {};
GfxKernel SSRC_allocate_update_rays {};
GfxKernel SSRC_sample_update_rays {};
GfxKernel SSRC_generate_trace_update_rays {};
GfxKernel SSRC_trace_update_rays {};
GfxKernel purge_tiles {};
GfxKernel clear_counters {};
GfxKernel clear_reservoirs {};
GfxKernel generate_reservoirs {};
GfxKernel compact_reservoirs {};
GfxKernel resample_reservoirs {};
GfxKernel populate_cells {};
GfxKernel generate_update_tiles_dispatch {};
GfxKernel update_tiles {};
GfxKernel resolve_cells {};
GfxKernel SSRC_precompute_cache_update {};
GfxKernel SSRC_compute_cache_update_step {};
GfxKernel SSRC_normalize_cache_update {};
GfxKernel SSRC_normalize_cache_update_set_reduce_count {};
GfxKernel SSRC_apply_cache_update {};
GfxKernel SSRC_spawn_new_basis {};
GfxKernel SSRC_clip_over_allocation {};
GfxKernel SSRC_integrate_ASG {};
GfxKernel SSRC_accumulate_update_error {};

GfxKernel SSRC_reset {};

GfxKernel DebugSSRC_visualize_coverage {};
GfxKernel DebugSSRC_visualize_tile_occupancy {};
GfxKernel DebugSSRC_basis {};
GfxKernel DebugSSRC_basis_3D {};
GfxKernel DebugSSRC_generate_draw_indexed {};
GfxKernel DebugSSRC_show_difference {};
GfxKernel DebugSSRC_fetch_cursor_pos {};
GfxKernel DebugSSRC_precompute_incident_radiance {};
GfxKernel DebugSSRC_incident_radiance {};
GfxKernel DebugSSRC_prepare_update_rays {};
GfxKernel DebugSSRC_update_rays {};
GfxKernel DebugSSRC_light {};

GfxKernel generate_dispatch {};
GfxKernel generate_dispatch_rays {};

GfxKernel debug_hash_grid_cells {};

GfxKernel PrecomputeHiZ_min {};
GfxKernel PrecomputeHiZ_max {};

GfxKernel SSRC_ClearCounters {};
GfxKernel SSRC_AllocateUniformProbes {};
GfxKernel SSRC_AllocateAdaptiveProbes[SSRC_MAX_ADAPTIVE_PROBE_LAYERS] {};
GfxKernel SSRC_WriteProbeDispatchParameters {};
GfxKernel SSRC_ReprojectProbeHistory {};
GfxKernel SSRC_AllocateUpdateRays {};
GfxKernel SSRC_SampleUpdateRays {};
GfxKernel SSRC_GenerateTraceUpdateRays {};
GfxKernel SSRC_TraceUpdateRaysMain {};
GfxKernel SSRC_ReprojectPreviousUpdateError {};
GfxKernel ClearReservoirs {};
GfxKernel GenerateReservoirs {};
GfxKernel CompactReservoirs {};
GfxKernel ResampleReservoirs {};
GfxKernel PopulateCellsMain {};
GfxKernel GenerateUpdateTilesDispatch {};
GfxKernel UpdateTiles {};
GfxKernel ResolveCells {};
GfxKernel SSRC_UpdateProbes {};
GfxKernel SSRC_IntegrateASG {};
GfxKernel DebugSSRC_FetchCursorPos {};
GfxKernel DebugSSRC_PrepareUpdateRays {};

GfxKernel GenerateDispatch {};
GfxKernel GenerateDispatchRays {};

} kernels_;

Expand Down
5 changes: 3 additions & 2 deletions src/core/src/render_techniques/migi/migi_common.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ struct ProbeHeader {
// Screen pixel position of the probe
int2 ScreenPosition;
int BasisOffset;
// 0: 1, 1: 2, 2: 4, 3: 8, 4: 12
int Rank;
// 0: 1, 1: 2, 2: 4, 3: 8, no larger than 8
int Class;
bool bValid;
float LinearDepth;
float3 Position;
Expand Down Expand Up @@ -227,6 +227,7 @@ static_assert((1 << SSRC_TILE_SIZE_L2) == SSRC_TILE_SIZE, "SSRC_TILE_SIZE != 1<<
#endif

#define SSRC_MAX_NUM_BASIS_PER_PROBE 8
#define SSRC_MAX_NUM_UPDATE_RAY_PER_PROBE 128

#ifdef __cplusplus
}// namespace Capsaicin
Expand Down
25 changes: 14 additions & 11 deletions src/core/src/render_techniques/migi/migi_inc.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

// Use heuristic for direction update
// #define HEURISTIC_DIRECTION_UPDATE
// Use numerical approx for color update
// #define OPTIMAL_COLOR_UPDATE
// Use RMSE to guide update ray allocation
// #define ERROR_RMSE

Expand Down Expand Up @@ -69,8 +71,6 @@ RWTexture2D<float4> g_RWGlobalIlluminationOutput;

// Buffers
// Sparse screen space cache
// The count of overall allocated probes
RWStructuredBuffer<uint> g_RWActiveProbeCountBuffer;
// Probe headers
// Use textures for better texture cache utilization (2x2)
// BasisOffset : 24 bits
Expand All @@ -92,19 +92,22 @@ RWStructuredBuffer<uint> g_RWProbeSGBuffer;
RWStructuredBuffer<uint> g_RWPreviousProbeSGBuffer;
// Used when allocating SGs to probes
RWStructuredBuffer<uint> g_RWAllocatedProbeSGCountBuffer;
// Irradiance for SSRC probes
// Irradiance (actually mean radiance in all incident directions on the sphere) for SSRC probes
// Color : 16*3, Unused: 16
RWStructuredBuffer<uint> g_RWProbeIrradianceBuffer;
RWStructuredBuffer<uint> g_RWPreviousProbeIrradianceBuffer;
// Exponential moving average of gradient squares (color, lambda, direction), 16*2, 32
RWStructuredBuffer<uint2> g_RWProbeSGGradientScaleBuffer;
RWStructuredBuffer<uint2> g_RWPreviousProbeSGGradientScaleBuffer;
RWTexture2D<float4> g_RWProbeIrradianceTexture;
RWTexture2D<float4> g_RWPreviousProbeIrradianceBuffer;

// Number of update rays allocated for each probe
// Must be a multiple of WAVE_SIZE
RWStructuredBuffer<uint> g_RWTileUpdateRayCountBuffer;

// Sampling, tracing and updating are done in a single kernel, so we do not need to store update rays
RWStructuredBuffer<uint> g_RWProbeUpdateRayCountBuffer;
RWStructuredBuffer<uint> g_RWProbeUpdateRayOffsetBuffer;
// Probe index, unorm16x2 packed
RWStructuredBuffer<uint> g_RWUpdateRayProbeBuffer;
// Octahedral packed direction for each update ray (fp16x2)
RWStructuredBuffer<uint> g_RWUpdateRayDirectionBuffer;
// Traced Radiance & InvPdf for each update ray
RWStructuredBuffer<uint2> g_RWUpdateRayRadianceInvPdfBuffer;
RWStructuredBuffer<float> g_RWUpdateRayLinearDepthBuffer;

// Number of adaptive probes within each tile
RWTexture2D<uint> g_RWTileAdaptiveProbeCountTexture;
Expand Down
Loading

0 comments on commit feee415

Please sign in to comment.