Skip to content

Commit

Permalink
Use exact number of cluster lights when assembling light lists in inj…
Browse files Browse the repository at this point in the history
…ect_model_lights()

Previously, space was made in the light list offsets that amounted to the maximum
number of lights in a cluster, seen over the lifetime of the map. Unfortunately,
this affect light sampling after a large spike of dynamic lights in a cluster,
possibly resulting in a more 'noisy' appearance - as described in NVIDIA#226.

However, the exact number of lights in a cluster was actually already counted
and can be used to tightly pack the light lists, avoiding the permant effect
on sampling after a spike of dynamic lights.
  • Loading branch information
apanteleev authored and res2k committed May 12, 2022
1 parent 04cf1bb commit f375f8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
6 changes: 0 additions & 6 deletions src/refresh/vkpt/shader/light_lists.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,6 @@ sample_polygonal_lights(

uint current_idx = light_buffer.light_list_lights[n_idx];

if(current_idx == ~0u)
{
light_masses[i] = 0;
continue;
}

LightPolygon light = get_light_polygon(current_idx);

float m = projected_tri_area(light.positions, p, n, V, phong_exp, phong_scale, phong_weight);
Expand Down
33 changes: 17 additions & 16 deletions src/refresh/vkpt/vertex_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,10 @@ vkpt_iqm_matrix_buffer_upload_staging(VkCommandBuffer cmd_buf)
static int local_light_counts[MAX_MAP_LEAFS];
static int cluster_light_counts[MAX_MAP_LEAFS];
static int light_list_tails[MAX_MAP_LEAFS];
static int max_cluster_model_lights[MAX_MAP_LEAFS];
static int max_model_lights;

void vkpt_light_buffer_reset_counts()
{
memset(max_cluster_model_lights, 0, sizeof(max_cluster_model_lights));
max_model_lights = 0;
}

Expand Down Expand Up @@ -505,19 +503,12 @@ inject_model_lights(bsp_mesh_t* bsp_mesh, bsp_t* bsp, int num_model_lights, ligh
}
}

// Update the max light counts per cluster

for (int c = 0; c < bsp_mesh->num_clusters; c++)
{
max_cluster_model_lights[c] = max(max_cluster_model_lights[c], cluster_light_counts[c]);
}

// Count the total required list size

int required_size = bsp_mesh->cluster_light_offsets[bsp_mesh->num_clusters];
for (int c = 0; c < bsp_mesh->num_clusters; c++)
{
required_size += max_cluster_model_lights[c];
required_size += cluster_light_counts[c];
}

// See if we have enough room in the interaction buffer
Expand All @@ -544,13 +535,10 @@ inject_model_lights(bsp_mesh_t* bsp_mesh, bsp_t* bsp, int num_model_lights, ligh
memcpy(dst_lists + tail, bsp_mesh->cluster_lights + bsp_mesh->cluster_light_offsets[c], sizeof(uint32_t) * original_size);
tail += original_size;

assert(tail + max_cluster_model_lights[c] < MAX_LIGHT_LIST_NODES);
assert(tail + cluster_light_counts[c] < MAX_LIGHT_LIST_NODES);

if (max_cluster_model_lights[c] > 0) {
memset(dst_lists + tail, 0xff, sizeof(uint32_t) * max_cluster_model_lights[c]);
}
light_list_tails[c] = tail;
tail += max_cluster_model_lights[c];
tail += cluster_light_counts[c];
}
dst_list_offsets[bsp_mesh->num_clusters] = tail;

Expand All @@ -566,12 +554,25 @@ inject_model_lights(bsp_mesh_t* bsp_mesh, bsp_t* bsp, int num_model_lights, ligh
if (mask[j] & (1 << k))
{
int other_cluster = j * 8 + k;
dst_lists[light_list_tails[other_cluster]++] = model_light_offset + nlight;
int list_index = light_list_tails[other_cluster]++;
assert(list_index < light_list_tails[other_cluster] + 1);
dst_lists[list_index] = model_light_offset + nlight;
}
}
}
}
}

#if defined(_DEBUG)
// Verify tight packing
for (int c = 0; c < bsp_mesh->num_clusters; c++)
{
int list_start = dst_list_offsets[c];
int list_end = dst_list_offsets[c + 1];
int original_size = bsp_mesh->cluster_light_offsets[c + 1] - bsp_mesh->cluster_light_offsets[c];
assert(list_end - list_start == original_size + cluster_light_counts[c]);
}
#endif
}

static inline void
Expand Down

0 comments on commit f375f8c

Please sign in to comment.