rtx: add light clusters debug helpers

1. add a way to enable only certain lights
2. add commented out code to highligh affected light clusters, culling
   modes, etc.
3. add affected light clusters counter when collecting lights
This commit is contained in:
Ivan Avdeev 2021-10-03 13:39:54 -07:00 committed by Ivan Avdeev
parent 20369dd9ab
commit 19b8025fc6
6 changed files with 66 additions and 3 deletions

View File

@ -59,7 +59,7 @@ layout (set = 0, binding = 8, align = 1) readonly buffer UBOLightClusters {
layout(set = 0, binding = 9, rgba8) uniform readonly image2D previous_frame;
layout (push_constant) uniform PC__ {
layout (push_constant) uniform PC_ {
PushConstants push_constants;
};
@ -101,19 +101,25 @@ vec3 sampleSurfaceTriangle(vec3 view_dir, MaterialProperties material, mat4x3 em
const float light_dir_normal_dot = dot(light_dir, payload.normal);
if (light_dir_normal_dot <= 0.)
return vec3(0.);
//return vec3(1., 0., 1.);
// Consider area light sources as planes, take the first normal
const vec3 normal = normalize(emissive_transform_normal * vertices[vi1].normal);
const float light_dot = -dot(light_dir, normal);
if (light_dot <= 0.)
return vec3(0.);
return vec3(0.);//1., 0., 0.);
//return vec3(1., 0., 0.);
// TODO emissive normals and areas can be precomputed
const float area = .5 * length(cross(v1 - v2, v1 - v3)) * meters_per_unit * meters_per_unit;
const float light_dist2 = dot(light_dir, light_dir);
float pdf = light_dist2 / (area * light_dot);
if (pdf > 100.)
//return vec3(0., 1., 0.);
return vec3(0.);//, 1., 0.);
light_dir = normalize(light_dir);
if (shadowed(payload.hit_pos_t.xyz, light_dir, sqrt(light_dist2)))
@ -162,9 +168,16 @@ vec3 computeLighting(vec3 view_dir, MaterialProperties material) {
}
for (uint i = begin_i; i < end_i; ++i) {
#else
for (uint i = 0; i < num_emissive_kusochki; ++i) {
#endif
const uint index_into_emissive_kusochki = uint(light_grid.clusters[cluster_index].emissive_surfaces[i]);
if (push_constants.debug_light_index_begin < push_constants.debug_light_index_end) {
if (index_into_emissive_kusochki < push_constants.debug_light_index_begin || index_into_emissive_kusochki >= push_constants.debug_light_index_end)
continue;
}
const EmissiveKusok ek = lights.kusochki[index_into_emissive_kusochki];
const uint emissive_kusok_index = lights.kusochki[index_into_emissive_kusochki].kusok_index;
const Kusok ekusok = kusochki[emissive_kusok_index];
@ -222,6 +235,8 @@ vec3 computeLighting(vec3 view_dir, MaterialProperties material) {
return C;
}
const float color_factor = 10000.;
void main() {
rand01_state = push_constants.random_seed + gl_LaunchIDEXT.x * 1833 + gl_LaunchIDEXT.y * 31337;
vec2 uv = (gl_LaunchIDEXT.xy + .5) / gl_LaunchSizeEXT.xy * 2. - 1.;
@ -260,6 +275,36 @@ void main() {
break;
}
// light clusters debugging
if (false)
{
const ivec3 light_cell = ivec3(floor(payload.hit_pos_t.xyz / LIGHT_GRID_CELL_SIZE)) - light_grid.grid_min;
const uint cluster_index = uint(dot(light_cell, ivec3(1, light_grid.grid_size.x, light_grid.grid_size.x * light_grid.grid_size.y)));
if (any(greaterThanEqual(light_cell, light_grid.grid_size)) || cluster_index >= MAX_LIGHT_CLUSTERS) {
C = vec3(1., 0., 0.) * color_factor;
break;
}
const uint num_emissive_kusochki = uint(light_grid.clusters[cluster_index].num_emissive_surfaces);
for (uint i = 0; i < num_emissive_kusochki; ++i) {
const uint index_into_emissive_kusochki = uint(light_grid.clusters[cluster_index].emissive_surfaces[i]);
if (push_constants.debug_light_index_begin < push_constants.debug_light_index_end) {
if (index_into_emissive_kusochki < push_constants.debug_light_index_begin || index_into_emissive_kusochki >= push_constants.debug_light_index_end)
continue;
}
//C = vec3(0., 0., 1.) * color_factor;
}
/* const uvec3 cellrand = pcg3d(uvec3(light_cell)); */
/* C = .02 * color_factor * vec3( */
/* uintToFloat01(cellrand.r), */
/* uintToFloat01(cellrand.g), */
/* uintToFloat01(cellrand.b)); */
//break;
}
MaterialProperties material;
material.baseColor = payload.base_color;
material.metalness = 0.f;
@ -311,7 +356,6 @@ void main() {
throughput *= brdfWeight;
} // for all bounces
const float color_factor = 10000.;
{
vec3 prev_frame = imageLoad(previous_frame, ivec2(gl_LaunchIDEXT.xy)).rgb;
prev_frame *= prev_frame;

View File

@ -40,6 +40,7 @@ struct PushConstants {
int bounces;
float prev_frame_blend_factor;
float pixel_cone_spread_angle;
uint debug_light_index_begin, debug_light_index_end;
};
#ifndef GLSL

View File

@ -14,4 +14,7 @@ void VK_LoadCvars( void )
r_drawentities = gEngine.pfnGetCvarPointer( "r_drawentities", 0 );
vk_rtx_bounces = gEngine.Cvar_Get( "vk_rtx_bounces", "2", FCVAR_ARCHIVE, "RTX path tracing ray bounces" );
vk_rtx_prev_frame_blend_factor = gEngine.Cvar_Get("vk_rtx_prev_frame_blend_factor", "0.1", FCVAR_ARCHIVE, "RTX path tracer ghetto temporal denoiser strength");
vk_rtx_light_begin = gEngine.Cvar_Get( "vk_rtx_light_begin", "0", 0, "DEBUG: disable lights with index lower than this");
vk_rtx_light_end = gEngine.Cvar_Get( "vk_rtx_light_end", "0", 0, "DEBUG: disable lights with index higher than this ");
}

View File

@ -11,6 +11,8 @@ void VK_LoadCvars( void );
X(cl_lightstyle_lerping) \
X(vk_rtx_bounces) \
X(vk_rtx_prev_frame_blend_factor) \
X(vk_rtx_light_begin) \
X(vk_rtx_light_end) \
#define EXTERN_CVAR(cvar) extern cvar_t *cvar;
DECLARE_CVAR(EXTERN_CVAR)

View File

@ -1,6 +1,7 @@
#include "vk_light.h"
#include "vk_textures.h"
#include "vk_brush.h"
#include "vk_cvar.h"
#include "mod_local.h"
#include "xash3d_mathlib.h"
@ -878,12 +879,22 @@ void VK_LightsFrameFinalize( void )
}
{
int num_clusters_with_lights_in_range = 0;
for (int i = 0; i < g_lights.map.grid_cells; ++i) {
const vk_lights_cell_t *cluster = g_lights.cells + i;
if (cluster->num_emissive_surfaces > 0) {
gEngine.Con_Reportf(" cluster %d: emissive_surfaces=%d\n", i, cluster->num_emissive_surfaces);
}
for (int j = 0; j < cluster->num_emissive_surfaces; ++j) {
const int index = cluster->emissive_surfaces[j];
if (index >= vk_rtx_light_begin->value && index < vk_rtx_light_end->value) {
++num_clusters_with_lights_in_range;
}
}
}
gEngine.Con_Reportf("Clusters with filtered lights: %d\n", num_clusters_with_lights_in_range);
}
#endif
}

View File

@ -614,6 +614,8 @@ static qboolean rayTrace( VkCommandBuffer cmdbuf, VkImage frame_dst, float fov_a
.bounces = vk_rtx_bounces->value,
.prev_frame_blend_factor = vk_rtx_prev_frame_blend_factor->value,
.pixel_cone_spread_angle = atanf((2.0f*tanf(fov_angle_y * 0.5f)) / (float)FRAME_HEIGHT),
.debug_light_index_begin = (uint32_t)(vk_rtx_light_begin->value),
.debug_light_index_end = (uint32_t)(vk_rtx_light_end->value),
};
vkCmdPushConstants(cmdbuf, g_rtx.descriptors.pipeline_layout, VK_SHADER_STAGE_RAYGEN_BIT_KHR, 0, sizeof(push_constants), &push_constants);
}