From 2507a629cf2376fa855d60f3c2c77934e81ce532 Mon Sep 17 00:00:00 2001 From: Ivan Avdeev Date: Mon, 15 Jan 2024 12:08:56 -0500 Subject: [PATCH] vk: rt: move empirical light scaling to native code There are lots of empirical (and less-well undrestood things) scaling color values in native code, it makes sense to consolidate them in one place: - less weird math in shaders. shader should be as streamlined as possible - one big block of weird math is untangleable in the future when we get to work on light and clusters --- ref/vk/TODO.md | 6 ++++-- ref/vk/shaders/light.glsl | 9 --------- ref/vk/vk_light.c | 18 ++++++++++++++---- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ref/vk/TODO.md b/ref/vk/TODO.md index 33649a7d..caa6426d 100644 --- a/ref/vk/TODO.md +++ b/ref/vk/TODO.md @@ -2,12 +2,14 @@ - [x] filter out invalid (r=0, etc) lights in native - [-] already do; it seems that clusters are not getting updates → see #730 - [x] pass point lights r² directly? -- [ ] move empirical scaling to native code +- [x] move empirical scaling to native code - [ ] modify point light radius in entity patches -- [ ] adjust brightness based on radius? + - [ ] adjust brightness based on radius? - [ ] P NaNs - [ ] patchable sun angle - [ ] common intersection-local-normal-oriented basis +- [ ] add direct_{diff,spec} to rendertests + - [ ] and rerun tests for vulkan to get new gold images # 2024-01-12 E362 - [ ] point→spherical light sampling diff --git a/ref/vk/shaders/light.glsl b/ref/vk/shaders/light.glsl index 3472dd74..b63e0c6f 100644 --- a/ref/vk/shaders/light.glsl +++ b/ref/vk/shaders/light.glsl @@ -207,20 +207,11 @@ void computeLighting(vec3 P, vec3 N, vec3 view_dir, MaterialProperties material, #if LIGHT_POLYGON sampleEmissiveSurfaces(P, N, view_dir, material, cluster_index, diffuse, specular); - // These constants are empirical. There's no known math reason behind them - // TODO move to native code - diffuse /= 25.0; - specular /= 25.0; #endif #if LIGHT_POINT vec3 ldiffuse = vec3(0.), lspecular = vec3(0.); computePointLights(P, N, cluster_index, view_dir, material, ldiffuse, lspecular); - // These constants are empirical. There's no known math reason behind them - // TODO move to native code - ldiffuse /= 25.; - lspecular /= 25.; - diffuse += ldiffuse; specular += lspecular; #endif diff --git a/ref/vk/vk_light.c b/ref/vk/vk_light.c index e29a39fe..bed32243 100644 --- a/ref/vk/vk_light.c +++ b/ref/vk/vk_light.c @@ -868,6 +868,9 @@ static qboolean addDlight( const dlight_t *dlight ) { scaler = k_threshold / (max_comp * sphereSolidAngleFromDistDiv2Pi(k_light_radius, dlight->radius)); + // These constants are empirical. There's no known math reason behind them + scaler /= 25.; + VectorSet( color, dlight->color.r * scaler, @@ -887,11 +890,13 @@ static void processStaticPointLights( void ) { for (int i = 0; i < g_map_entities.num_lights; ++i) { const vk_light_entity_t *le = g_map_entities.lights + i; const float default_radius = 2.f; // FIXME tune - const float hack_attenuation = .1f; // FIXME tune - const float hack_attenuation_spot = .1f; // FIXME tune const float radius = le->radius > 0.f ? le->radius : default_radius; - int index; + // These constants are empirical. There's no known math reason behind them + const float hack_attenuation = .1f / 25.f; // FIXME tune + const float hack_attenuation_spot = .1f / 25.f; // FIXME tune + + int index; switch (le->type) { case LightTypePoint: index = addPointLight(le->origin, le->color, radius, le->style, hack_attenuation); @@ -1089,7 +1094,12 @@ int RT_LightAddPolygon(const rt_light_add_polygon_t *addpoly) { poly->vertices.offset = g_lights_.num_polygon_vertices; poly->vertices.count = addpoly->num_vertices; - VectorCopy(addpoly->emissive, poly->emissive); + { + // These constants are empirical. There's no known math reason behind them + const float hack_attenuation_poly = 1.f / 25.f; + VectorScale(addpoly->emissive, hack_attenuation_poly, poly->emissive); + } + VectorSet(poly->center, 0, 0, 0); VectorSet(normal, 0, 0, 0);