From ab4d1c12c74cac7c9ccfbbbb5cf7a88244765fdf Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sat, 25 Dec 2021 00:30:02 +0300 Subject: [PATCH 01/19] new flashlight --- common/render_api.h | 1 + engine/client/cl_tent.c | 16 ++++++++++- ref_vk/vk_light.c | 64 +++++++++++++++++++++++++++++++++++++++++ ref_vk/vk_rmain.c | 3 ++ ref_vk/vk_scene.c | 15 ++++++++++ 5 files changed, 98 insertions(+), 1 deletion(-) diff --git a/common/render_api.h b/common/render_api.h index f41d297f..9592ccb8 100644 --- a/common/render_api.h +++ b/common/render_api.h @@ -63,6 +63,7 @@ GNU General Public License for more details. #define PARM_TEX_MEMORY 38 // returns total memory of uploaded texture in bytes #define PARM_DELUXEDATA 39 // nasty hack, convert int to pointer #define PARM_SHADOWDATA 40 // nasty hack, convert int to pointer +#define PARM_MODERNFLASHLIGHT 41 // new dynamic flashlight, initially for Vulkan render // skybox ordering enum diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index 6c9aeb47..4be66256 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -2696,7 +2696,21 @@ void CL_AddEntityEffects( cl_entity_t *ent ) if( FBitSet( ent->curstate.effects, EF_BRIGHTFIELD )) R_EntityParticles( ent ); - if( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) + if (REF_GET_PARM( PARM_MODERNFLASHLIGHT, 1 ) == true) + { + if( !ent->player ) // TODO: need testing + { + if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT ) ) + { + dlight_t *dl = CL_AllocDlight( ent->index ); + dl->color.r = dl->color.g = dl->color.b = 100; + dl->radius = COM_RandomFloat( 200, 231 ); + VectorCopy( ent->origin, dl->origin ); + dl->die = cl.time + 0.001; + } + } + } + else if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) { if( ent->player && !Host_IsQuakeCompatible( )) { diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 4fddde50..adefc147 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -15,6 +15,8 @@ #include #include // isalnum... +#include "camera.h" + #define PROFILER_SCOPES(X) \ X(finalize , "VK_LightsFrameFinalize"); \ X(emissive_surface, "VK_LightsAddEmissiveSurface"); \ @@ -808,6 +810,68 @@ static int addSpotLight( const vk_light_entity_t *le, float radius, int lightsty return index; } +void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t *g_camera ) { + vec3_t color; + vec3_t origin; + const int index = g_lights.num_point_lights; + vk_point_light_t *const plight = g_lights.point_lights + index; + *plight = (vk_point_light_t){0}; + + // parameters + const float hack_attenuation = 0.1; + const float radius = 2.0; + const float _cone = 1.0; + const float _cone2 = 30.0; + const vec3_t light_color = {255, 255, 192}; + const float light_intensity = 200; + + VectorCopy(light_color, color); + + // prepare colors + VectorScale(light_color, light_intensity / 255.0f, color); + + // convert colors by weirdGoldsrcLightScaling + float l1 = Q_max(color[0], Q_max(color[1], color[2])); + l1 = l1 * l1 / 10; + VectorScale(color, l1, color); + + /* + gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f)\n", + origin[0], origin[1], origin[2], + color[0], color[1], color[2]); + */ + + // position + VectorCopy(ent->origin, origin); + // TODO: relative position of the flashlight as if it were in the left hand (need modify values from g_camera->vforward, HOW?) + //origin[0] += 20; // forward-back + //origin[1] += -10; // left-right + origin[2] += 30; // up-down // HACK + VectorCopy(g_camera->vforward, plight->dir); + //VectorSet(plight->dir, g_camera->vforward[0], g_camera->vforward[1], g_camera->vforward[2]); + + // convert stopdots by parseStopDot + plight->stopdot = cosf(_cone * M_PI / 180.f); + plight->stopdot2 = cosf(_cone2 * M_PI / 180.f); + + VectorCopy(origin, plight->origin); + plight->radius = radius; + + VectorScale(color, hack_attenuation, plight->base_color); + VectorCopy(plight->base_color, plight->color); + + /* + gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f) dir=(%f %f %f)\n", + plight->origin[0], plight->origin[1], plight->origin[2], + plight->color[0], plight->color[1], plight->color[2], + plight->dir[0], plight->dir[1], plight->dir[2]); + */ + + addPointLightToClusters( index ); + g_lights.num_point_lights++; +} + + static float sphereSolidAngleFromDistDiv2Pi(float r, float d) { return 1. - sqrt(d*d - r*r)/d; } diff --git a/ref_vk/vk_rmain.c b/ref_vk/vk_rmain.c index f368621e..8fc66c51 100644 --- a/ref_vk/vk_rmain.c +++ b/ref_vk/vk_rmain.c @@ -228,6 +228,7 @@ static const char *getParmName(int parm) case PARM_TEX_MEMORY: return "PARM_TEX_MEMORY"; case PARM_DELUXEDATA: return "PARM_DELUXEDATA"; case PARM_SHADOWDATA: return "PARM_SHADOWDATA"; + case PARM_MODERNFLASHLIGHT: return "PARM_MODERNFLASHLIGHT"; default: return "UNKNOWN"; } } @@ -248,6 +249,8 @@ static int VK_RefGetParm( int parm, int arg ) case PARM_TEX_FLAGS: tex = findTexture(arg); return tex->flags; + case PARM_MODERNFLASHLIGHT: + return true; } PRINT_NOT_IMPLEMENTED_ARGS("(%s(%d), %d)", getParmName(parm), parm, arg); diff --git a/ref_vk/vk_scene.c b/ref_vk/vk_scene.c index b4e9c7c6..c4e099b3 100644 --- a/ref_vk/vk_scene.c +++ b/ref_vk/vk_scene.c @@ -597,6 +597,8 @@ static float g_frametime = 0; void VK_SceneRender( const ref_viewpass_t *rvp ) { + cl_entity_t *entPlayer; + int current_pipeline_index = kRenderNormal; g_frametime = /*FIXME VK RP_NORMALPASS( )) ? */ @@ -660,6 +662,19 @@ void VK_SceneRender( const ref_viewpass_t *rvp ) // Draw transparent beams gEngine.CL_DrawEFX( g_frametime, true ); + // Draw flashlight // TODO: REFACTORING? + entPlayer = gEngine.GetLocalPlayer(); + if( entPlayer->player ) { // && !Host_IsQuakeCompatible( ) ??? + if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { + //gEngine.Con_Printf( S_WARN "FLASHLIGHT! \n"); + //dlight_t *dl = gEngine.gEfxApi.CL_AllocDlight( entPlayer->index ); // not work + //dlight_t *dl = CL_AllocDlight( entPlayer->index ); // need copy CL_AllocDlight + //dl->die = gpGlobals->time + 0.01f; // die on next frame + // TODO: try allocate CL_AllocDlight from cl_tent.c but blank + VK_AddFlashlight(entPlayer, g_camera); // FIXME: buggy with vk_rtx_freeze + } + } + VK_RenderDebugLabelEnd(); if (ui_infotool->value > 0) From 8c124794472120bb7b3098765cc1b03ac03f32d8 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sat, 25 Dec 2021 01:16:19 +0300 Subject: [PATCH 02/19] fix error: implicit declaration of function --- ref_vk/vk_light.c | 4 ++-- ref_vk/vk_scene.c | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index adefc147..8a813aa5 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -810,7 +810,7 @@ static int addSpotLight( const vk_light_entity_t *le, float radius, int lightsty return index; } -void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t *g_camera ) { +void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t g_camera ) { vec3_t color; vec3_t origin; const int index = g_lights.num_point_lights; @@ -847,7 +847,7 @@ void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t *g_camera ) { //origin[0] += 20; // forward-back //origin[1] += -10; // left-right origin[2] += 30; // up-down // HACK - VectorCopy(g_camera->vforward, plight->dir); + VectorCopy(g_camera.vforward, plight->dir); //VectorSet(plight->dir, g_camera->vforward[0], g_camera->vforward[1], g_camera->vforward[2]); // convert stopdots by parseStopDot diff --git a/ref_vk/vk_scene.c b/ref_vk/vk_scene.c index c4e099b3..af75c164 100644 --- a/ref_vk/vk_scene.c +++ b/ref_vk/vk_scene.c @@ -595,6 +595,8 @@ static void drawEntity( cl_entity_t *ent, int render_mode ) static float g_frametime = 0; +void VK_AddFlashlight(cl_entity_t *ent, vk_global_camera_t g_camera); + void VK_SceneRender( const ref_viewpass_t *rvp ) { cl_entity_t *entPlayer; From 0aba565ab88e2b7f3eb890ef4394bfa424edbc60 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sun, 26 Dec 2021 01:00:06 +0300 Subject: [PATCH 03/19] more correct flashlight more correct work in the first person add third person support add multiplayer support (need testing) --- ref_vk/vk_light.c | 98 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 17 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 8a813aa5..7a271dea 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -16,6 +16,8 @@ #include // isalnum... #include "camera.h" +#include "pm_defs.h" +#include "pmtrace.h" #define PROFILER_SCOPES(X) \ X(finalize , "VK_LightsFrameFinalize"); \ @@ -813,21 +815,23 @@ static int addSpotLight( const vk_light_entity_t *le, float radius, int lightsty void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t g_camera ) { vec3_t color; vec3_t origin; + vec3_t angles; const int index = g_lights.num_point_lights; vk_point_light_t *const plight = g_lights.point_lights + index; *plight = (vk_point_light_t){0}; // parameters const float hack_attenuation = 0.1; - const float radius = 2.0; - const float _cone = 1.0; + const float radius = 1.0; + // TODO: better tune it + const float _cone = 10.0; const float _cone2 = 30.0; const vec3_t light_color = {255, 255, 192}; - const float light_intensity = 200; + const float light_intensity = 250; VectorCopy(light_color, color); - // prepare colors + // prepare colors by parseEntPropRgbav VectorScale(light_color, light_intensity / 255.0f, color); // convert colors by weirdGoldsrcLightScaling @@ -835,20 +839,80 @@ void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t g_camera ) { l1 = l1 * l1 / 10; VectorScale(color, l1, color); - /* - gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f)\n", - origin[0], origin[1], origin[2], - color[0], color[1], color[2]); - */ + float angle; + float thirdperson_offset = 15; + vec3_t forward, view_ofs; + vec3_t vecSrc, vecEnd; + pmtrace_t *trace; + if( ent->index == gEngine.EngineGetParm(PARM_PLAYER_INDEX, 0)) + { + // local player case + // position + if (gEngine.EngineGetParm(PARM_THIRDPERSON, 0)) { // thirdperson - // position - VectorCopy(ent->origin, origin); - // TODO: relative position of the flashlight as if it were in the left hand (need modify values from g_camera->vforward, HOW?) - //origin[0] += 20; // forward-back - //origin[1] += -10; // left-right - origin[2] += 30; // up-down // HACK - VectorCopy(g_camera.vforward, plight->dir); - //VectorSet(plight->dir, g_camera->vforward[0], g_camera->vforward[1], g_camera->vforward[2]); + AngleVectors( g_camera.viewangles, forward, NULL, NULL ); + view_ofs[0] = view_ofs[1] = 0.0f; + if( ent->curstate.usehull == 1 ) { + view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; + } else { + view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT + } + VectorAdd( ent->origin, view_ofs, vecSrc ); + VectorMA( vecSrc, thirdperson_offset, forward, vecEnd ); + trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); + VectorCopy( trace->endpos, origin ); + VectorAngles( forward, angles ); + + // convert angles by parseAngles + angle = angles[1]; + angle *= M_PI / 180.f; + plight->dir[2] = 0; + plight->dir[0] = cosf(angle); + plight->dir[1] = sinf(angle); + angle = angles[0]; + angle *= M_PI / 180.f; + plight->dir[2] = sinf(angle); + plight->dir[0] *= cosf(angle); + plight->dir[1] *= cosf(angle); + } else { // firstperson + // based on https://github.com/SNMetamorph/PrimeXT/blob/0869b1abbddd13c1229769d8cd71941610be0bf3/client/flashlight.cpp#L35 + // TODO: tune it + origin[0] = g_camera.vieworg[0] + (g_camera.vright[0] * 5.0f) + (g_camera.vforward[0] * 2.0f); // forward-back + origin[1] = g_camera.vieworg[1] + (g_camera.vright[1] * 5.0f) + (g_camera.vforward[1] * 2.0f); // left-right + origin[2] = g_camera.vieworg[2] + 6.0f + (g_camera.vright[2] * 5.0f) + (g_camera.vforward[2] * 2.0f); // up-down + VectorCopy(g_camera.vforward, plight->dir); + } + } + else // non-local player case + { + // TODO: need to test! + VectorCopy(ent->origin, origin); + VectorCopy(ent->angles, angles); + AngleVectors( ent->angles, forward, NULL, NULL ); // TODO: maybe improve turning sensitivity + view_ofs[0] = view_ofs[1] = 0.0f; + if( ent->curstate.usehull == 1 ) { + view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; + } else { + view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT + } + VectorAdd( ent->origin, view_ofs, vecSrc ); + VectorMA( vecSrc, thirdperson_offset, forward, vecEnd ); + trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); + VectorCopy( trace->endpos, origin ); + VectorAngles( forward, angles ); + + // convert angles by parseAngles + angle = angles[1]; + angle *= M_PI / 180.f; + plight->dir[2] = 0; + plight->dir[0] = cosf(angle); + plight->dir[1] = sinf(angle); + angle = angles[0]; + angle *= M_PI / 180.f; + plight->dir[2] = sinf(angle); + plight->dir[0] *= cosf(angle); + plight->dir[1] *= cosf(angle); + } // convert stopdots by parseStopDot plight->stopdot = cosf(_cone * M_PI / 180.f); From e68546cd8b7ebcfb7f560de4d80e422c86a222dd Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sun, 26 Dec 2021 01:54:32 +0300 Subject: [PATCH 04/19] refactoring --- engine/client/cl_tent.c | 8 +++++++- ref_vk/vk_light.c | 12 ++++++++++-- ref_vk/vk_scene.c | 17 ----------------- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index 4be66256..847afde2 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -2698,7 +2698,13 @@ void CL_AddEntityEffects( cl_entity_t *ent ) if (REF_GET_PARM( PARM_MODERNFLASHLIGHT, 1 ) == true) { - if( !ent->player ) // TODO: need testing + if( ent->player ) // && !Host_IsQuakeCompatible( ) ??? + { + dlight_t *dl = CL_AllocDlight( ent->index ); + dl->radius = -1; + dl->die = cl.time + 0.01f; // die on next frame + } + else { if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT ) ) { diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 7a271dea..b3d38b77 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -812,7 +812,7 @@ static int addSpotLight( const vk_light_entity_t *le, float radius, int lightsty return index; } -void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t g_camera ) { +void VK_AddFlashlight( cl_entity_t *ent ) { vec3_t color; vec3_t origin; vec3_t angles; @@ -827,7 +827,7 @@ void VK_AddFlashlight( cl_entity_t *ent, vk_global_camera_t g_camera ) { const float _cone = 10.0; const float _cone2 = 30.0; const vec3_t light_color = {255, 255, 192}; - const float light_intensity = 250; + const float light_intensity = 300; VectorCopy(light_color, color); @@ -941,6 +941,8 @@ static float sphereSolidAngleFromDistDiv2Pi(float r, float d) { } static void addDlight( const dlight_t *dlight ) { + cl_entity_t *entPlayer; + const float k_light_radius = 2.f; const float k_threshold = 2.f; @@ -952,6 +954,12 @@ static void addDlight( const dlight_t *dlight ) { if( !dlight || dlight->die < gpGlobals->time || !dlight->radius ) return; + // Draw flashlight + entPlayer = gEngine.GetLocalPlayer(); + if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { + VK_AddFlashlight(entPlayer); + } + max_comp = Q_max(dlight->color.r, Q_max(dlight->color.g, dlight->color.b)); if (max_comp < k_threshold || dlight->radius <= k_light_radius) return; diff --git a/ref_vk/vk_scene.c b/ref_vk/vk_scene.c index af75c164..b4e9c7c6 100644 --- a/ref_vk/vk_scene.c +++ b/ref_vk/vk_scene.c @@ -595,12 +595,8 @@ static void drawEntity( cl_entity_t *ent, int render_mode ) static float g_frametime = 0; -void VK_AddFlashlight(cl_entity_t *ent, vk_global_camera_t g_camera); - void VK_SceneRender( const ref_viewpass_t *rvp ) { - cl_entity_t *entPlayer; - int current_pipeline_index = kRenderNormal; g_frametime = /*FIXME VK RP_NORMALPASS( )) ? */ @@ -664,19 +660,6 @@ void VK_SceneRender( const ref_viewpass_t *rvp ) // Draw transparent beams gEngine.CL_DrawEFX( g_frametime, true ); - // Draw flashlight // TODO: REFACTORING? - entPlayer = gEngine.GetLocalPlayer(); - if( entPlayer->player ) { // && !Host_IsQuakeCompatible( ) ??? - if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { - //gEngine.Con_Printf( S_WARN "FLASHLIGHT! \n"); - //dlight_t *dl = gEngine.gEfxApi.CL_AllocDlight( entPlayer->index ); // not work - //dlight_t *dl = CL_AllocDlight( entPlayer->index ); // need copy CL_AllocDlight - //dl->die = gpGlobals->time + 0.01f; // die on next frame - // TODO: try allocate CL_AllocDlight from cl_tent.c but blank - VK_AddFlashlight(entPlayer, g_camera); // FIXME: buggy with vk_rtx_freeze - } - } - VK_RenderDebugLabelEnd(); if (ui_infotool->value > 0) From 74a9809b0841f9baf7d1c1daf7818056d9dba473 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sun, 26 Dec 2021 03:17:22 +0300 Subject: [PATCH 05/19] formatting --- ref_vk/vk_light.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index b3d38b77..1bfa95f7 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -879,7 +879,8 @@ void VK_AddFlashlight( cl_entity_t *ent ) { // TODO: tune it origin[0] = g_camera.vieworg[0] + (g_camera.vright[0] * 5.0f) + (g_camera.vforward[0] * 2.0f); // forward-back origin[1] = g_camera.vieworg[1] + (g_camera.vright[1] * 5.0f) + (g_camera.vforward[1] * 2.0f); // left-right - origin[2] = g_camera.vieworg[2] + 6.0f + (g_camera.vright[2] * 5.0f) + (g_camera.vforward[2] * 2.0f); // up-down + origin[2] = g_camera.vieworg[2] + (g_camera.vright[2] * 5.0f) + (g_camera.vforward[2] * 2.0f); // up-down + origin[2] += 6.0f; VectorCopy(g_camera.vforward, plight->dir); } } From 6dbaf76b6cc2c6525cafe80c4ea47bc63f0b2161 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sun, 26 Dec 2021 04:06:19 +0300 Subject: [PATCH 06/19] add flashlight for classic render (workaround) --- ref_vk/vk_render.c | 78 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/ref_vk/vk_render.c b/ref_vk/vk_render.c index da741d69..3b88cbbf 100644 --- a/ref_vk/vk_render.c +++ b/ref_vk/vk_render.c @@ -14,6 +14,10 @@ #include "xash3d_mathlib.h" #include "protocol.h" // MAX_DLIGHTS +#include "camera.h" +#include "pm_defs.h" +#include "pmtrace.h" + #include #define MAX_UNIFORM_SLOTS (MAX_SCENE_ENTITIES * 2 /* solid + trans */ + 1) @@ -547,22 +551,70 @@ static uint32_t writeDlightsToUBO( void ) // TODO this should not be here (where? vk_scene?) for (int i = 0; i < MAX_DLIGHTS && num_lights < ARRAYSIZE(ubo_lights->light); ++i) { - const dlight_t *l = gEngine.GetDynamicLight(i); + dlight_t *l = gEngine.GetDynamicLight(i); if( !l || l->die < gpGlobals->time || !l->radius ) continue; - Vector4Set( - ubo_lights->light[num_lights].color, - l->color.r / 255.f, - l->color.g / 255.f, - l->color.b / 255.f, - 1.f); - Vector4Set( - ubo_lights->light[num_lights].pos_r, - l->origin[0], - l->origin[1], - l->origin[2], - l->radius); + // Draw flashlight (workaround) + cl_entity_t *entPlayer; + entPlayer = gEngine.GetLocalPlayer(); + if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { + #define FLASHLIGHT_DISTANCE 2000 // in units + pmtrace_t *trace; + vec3_t forward, view_ofs; + vec3_t vecSrc, vecEnd; + float falloff; + trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); + // compute falloff + falloff = trace->fraction * FLASHLIGHT_DISTANCE; + if( falloff < 500.0f ) falloff = 1.0f; + else falloff = 500.0f / falloff; + falloff *= falloff; + + AngleVectors( g_camera.viewangles, forward, NULL, NULL ); + view_ofs[0] = view_ofs[1] = 0.0f; + if( entPlayer->curstate.usehull == 1 ) { + view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; + } else { + view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT + } + VectorAdd( entPlayer->origin, view_ofs, vecSrc ); + VectorMA( vecSrc, FLASHLIGHT_DISTANCE, forward, vecEnd ); + trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); + VectorMA( trace->endpos, -30, forward, l->origin ); + + // apply brigthness to dlight + l->color.r = bound( 0, falloff * 255, 255 ); + l->color.g = bound( 0, falloff * 255, 255 ); + l->color.b = bound( 0, falloff * 255, 255 ); + l->radius = 60; + + Vector4Set( + ubo_lights->light[num_lights].color, + l->color.r / 255.f, + l->color.g / 255.f, + l->color.b / 255.f, + 1.f); + Vector4Set( + ubo_lights->light[num_lights].pos_r, + l->origin[0], + l->origin[1], + l->origin[2], + l->radius); + } else { + Vector4Set( + ubo_lights->light[num_lights].color, + l->color.r / 255.f, + l->color.g / 255.f, + l->color.b / 255.f, + 1.f); + Vector4Set( + ubo_lights->light[num_lights].pos_r, + l->origin[0], + l->origin[1], + l->origin[2], + l->radius); + } num_lights++; } From cf0c88bc08258f05ce6308e36809a06af4d82363 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Sun, 26 Dec 2021 04:19:39 +0300 Subject: [PATCH 07/19] flashlight tuning for classic render --- ref_vk/vk_render.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ref_vk/vk_render.c b/ref_vk/vk_render.c index 3b88cbbf..393ea8f1 100644 --- a/ref_vk/vk_render.c +++ b/ref_vk/vk_render.c @@ -564,7 +564,7 @@ static uint32_t writeDlightsToUBO( void ) vec3_t forward, view_ofs; vec3_t vecSrc, vecEnd; float falloff; - trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); + trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_NORMAL ); // compute falloff falloff = trace->fraction * FLASHLIGHT_DISTANCE; if( falloff < 500.0f ) falloff = 1.0f; @@ -580,14 +580,14 @@ static uint32_t writeDlightsToUBO( void ) } VectorAdd( entPlayer->origin, view_ofs, vecSrc ); VectorMA( vecSrc, FLASHLIGHT_DISTANCE, forward, vecEnd ); - trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); - VectorMA( trace->endpos, -30, forward, l->origin ); + trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_NORMAL ); + VectorMA( trace->endpos, -10, forward, l->origin ); // apply brigthness to dlight l->color.r = bound( 0, falloff * 255, 255 ); l->color.g = bound( 0, falloff * 255, 255 ); l->color.b = bound( 0, falloff * 255, 255 ); - l->radius = 60; + l->radius = 75; Vector4Set( ubo_lights->light[num_lights].color, From 5f7127bd0063b794cbc38256cbdd66c7cb5091d2 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Mon, 27 Dec 2021 19:27:31 +0300 Subject: [PATCH 08/19] replace parseAngles function code by AngleVectors and tune thirdperson_offset --- ref_vk/vk_light.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 731c3e30..482f9a00 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -868,7 +868,7 @@ void VK_AddFlashlight( cl_entity_t *ent ) { VectorScale(color, l1, color); float angle; - float thirdperson_offset = 15; + float thirdperson_offset = 25; vec3_t forward, view_ofs; vec3_t vecSrc, vecEnd; pmtrace_t *trace; @@ -877,7 +877,6 @@ void VK_AddFlashlight( cl_entity_t *ent ) { // local player case // position if (gEngine.EngineGetParm(PARM_THIRDPERSON, 0)) { // thirdperson - AngleVectors( g_camera.viewangles, forward, NULL, NULL ); view_ofs[0] = view_ofs[1] = 0.0f; if( ent->curstate.usehull == 1 ) { @@ -890,18 +889,8 @@ void VK_AddFlashlight( cl_entity_t *ent ) { trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); VectorCopy( trace->endpos, origin ); VectorAngles( forward, angles ); - - // convert angles by parseAngles - angle = angles[1]; - angle *= M_PI / 180.f; - plight->dir[2] = 0; - plight->dir[0] = cosf(angle); - plight->dir[1] = sinf(angle); - angle = angles[0]; - angle *= M_PI / 180.f; - plight->dir[2] = sinf(angle); - plight->dir[0] *= cosf(angle); - plight->dir[1] *= cosf(angle); + angles[PITCH] = -angles[PITCH]; + AngleVectors(angles, plight->dir, NULL, NULL); } else { // firstperson // based on https://github.com/SNMetamorph/PrimeXT/blob/0869b1abbddd13c1229769d8cd71941610be0bf3/client/flashlight.cpp#L35 // TODO: tune it @@ -929,18 +918,9 @@ void VK_AddFlashlight( cl_entity_t *ent ) { trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); VectorCopy( trace->endpos, origin ); VectorAngles( forward, angles ); + angles[PITCH] = -angles[PITCH]; + AngleVectors(angles, plight->dir, NULL, NULL); - // convert angles by parseAngles - angle = angles[1]; - angle *= M_PI / 180.f; - plight->dir[2] = 0; - plight->dir[0] = cosf(angle); - plight->dir[1] = sinf(angle); - angle = angles[0]; - angle *= M_PI / 180.f; - plight->dir[2] = sinf(angle); - plight->dir[0] *= cosf(angle); - plight->dir[1] *= cosf(angle); } // convert stopdots by parseStopDot From 2395469d0cfb459d29a6554611b45170059b8bab Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 00:34:39 +0300 Subject: [PATCH 09/19] cleanup --- ref_vk/vk_light.c | 45 ++++++++++++++------------------------------- 1 file changed, 14 insertions(+), 31 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 482f9a00..428d856b 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -844,9 +844,7 @@ void VK_AddFlashlight( cl_entity_t *ent ) { vec3_t color; vec3_t origin; vec3_t angles; - const int index = g_lights.num_point_lights; - vk_point_light_t *const plight = g_lights.point_lights + index; - *plight = (vk_point_light_t){0}; + vk_light_entity_t le; // parameters const float hack_attenuation = 0.1; @@ -857,17 +855,14 @@ void VK_AddFlashlight( cl_entity_t *ent ) { const vec3_t light_color = {255, 255, 192}; const float light_intensity = 300; - VectorCopy(light_color, color); - // prepare colors by parseEntPropRgbav VectorScale(light_color, light_intensity / 255.0f, color); // convert colors by weirdGoldsrcLightScaling float l1 = Q_max(color[0], Q_max(color[1], color[2])); l1 = l1 * l1 / 10; - VectorScale(color, l1, color); + VectorScale(color, l1, le.color); - float angle; float thirdperson_offset = 25; vec3_t forward, view_ofs; vec3_t vecSrc, vecEnd; @@ -888,9 +883,7 @@ void VK_AddFlashlight( cl_entity_t *ent ) { VectorMA( vecSrc, thirdperson_offset, forward, vecEnd ); trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); VectorCopy( trace->endpos, origin ); - VectorAngles( forward, angles ); - angles[PITCH] = -angles[PITCH]; - AngleVectors(angles, plight->dir, NULL, NULL); + VectorCopy( forward, le.dir); } else { // firstperson // based on https://github.com/SNMetamorph/PrimeXT/blob/0869b1abbddd13c1229769d8cd71941610be0bf3/client/flashlight.cpp#L35 // TODO: tune it @@ -898,15 +891,13 @@ void VK_AddFlashlight( cl_entity_t *ent ) { origin[1] = g_camera.vieworg[1] + (g_camera.vright[1] * 5.0f) + (g_camera.vforward[1] * 2.0f); // left-right origin[2] = g_camera.vieworg[2] + (g_camera.vright[2] * 5.0f) + (g_camera.vforward[2] * 2.0f); // up-down origin[2] += 6.0f; - VectorCopy(g_camera.vforward, plight->dir); + VectorCopy(g_camera.vforward, le.dir); } } else // non-local player case { // TODO: need to test! - VectorCopy(ent->origin, origin); - VectorCopy(ent->angles, angles); - AngleVectors( ent->angles, forward, NULL, NULL ); // TODO: maybe improve turning sensitivity + AngleVectors( ent->angles, angles, NULL, NULL ); // TODO: maybe improve turning sensitivity view_ofs[0] = view_ofs[1] = 0.0f; if( ent->curstate.usehull == 1 ) { view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; @@ -914,25 +905,13 @@ void VK_AddFlashlight( cl_entity_t *ent ) { view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT } VectorAdd( ent->origin, view_ofs, vecSrc ); - VectorMA( vecSrc, thirdperson_offset, forward, vecEnd ); + VectorMA( vecSrc, thirdperson_offset, angles, vecEnd ); trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); VectorCopy( trace->endpos, origin ); - VectorAngles( forward, angles ); - angles[PITCH] = -angles[PITCH]; - AngleVectors(angles, plight->dir, NULL, NULL); - + angles[ROLL] = -angles[ROLL]; + VectorCopy( angles, le.dir ); } - // convert stopdots by parseStopDot - plight->stopdot = cosf(_cone * M_PI / 180.f); - plight->stopdot2 = cosf(_cone2 * M_PI / 180.f); - - VectorCopy(origin, plight->origin); - plight->radius = radius; - - VectorScale(color, hack_attenuation, plight->base_color); - VectorCopy(plight->base_color, plight->color); - /* gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f) dir=(%f %f %f)\n", plight->origin[0], plight->origin[1], plight->origin[2], @@ -940,8 +919,12 @@ void VK_AddFlashlight( cl_entity_t *ent ) { plight->dir[0], plight->dir[1], plight->dir[2]); */ - addPointLightToClusters( index ); - g_lights.num_point_lights++; + VectorCopy(origin, le.origin); + // convert stopdots by parseStopDot + le.stopdot = cosf(_cone * M_PI / 180.f); + le.stopdot2 = cosf(_cone2 * M_PI / 180.f); + + addSpotLight(&le, radius, 0, hack_attenuation, false); } From 0b1b181f26ad5f867df4201615a97fbfd7441091 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 00:50:39 +0300 Subject: [PATCH 10/19] remove workaround for classic render --- engine/client/cl_tent.c | 8 +---- ref_vk/vk_render.c | 75 +++++++---------------------------------- ref_vk/vk_rmain.c | 6 +++- 3 files changed, 19 insertions(+), 70 deletions(-) diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index 847afde2..dfb4e067 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -2698,13 +2698,7 @@ void CL_AddEntityEffects( cl_entity_t *ent ) if (REF_GET_PARM( PARM_MODERNFLASHLIGHT, 1 ) == true) { - if( ent->player ) // && !Host_IsQuakeCompatible( ) ??? - { - dlight_t *dl = CL_AllocDlight( ent->index ); - dl->radius = -1; - dl->die = cl.time + 0.01f; // die on next frame - } - else + if( !ent->player ) { if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT ) ) { diff --git a/ref_vk/vk_render.c b/ref_vk/vk_render.c index 393ea8f1..520894f9 100644 --- a/ref_vk/vk_render.c +++ b/ref_vk/vk_render.c @@ -551,70 +551,21 @@ static uint32_t writeDlightsToUBO( void ) // TODO this should not be here (where? vk_scene?) for (int i = 0; i < MAX_DLIGHTS && num_lights < ARRAYSIZE(ubo_lights->light); ++i) { - dlight_t *l = gEngine.GetDynamicLight(i); + const dlight_t *l = gEngine.GetDynamicLight(i); if( !l || l->die < gpGlobals->time || !l->radius ) continue; - - // Draw flashlight (workaround) - cl_entity_t *entPlayer; - entPlayer = gEngine.GetLocalPlayer(); - if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { - #define FLASHLIGHT_DISTANCE 2000 // in units - pmtrace_t *trace; - vec3_t forward, view_ofs; - vec3_t vecSrc, vecEnd; - float falloff; - trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_NORMAL ); - // compute falloff - falloff = trace->fraction * FLASHLIGHT_DISTANCE; - if( falloff < 500.0f ) falloff = 1.0f; - else falloff = 500.0f / falloff; - falloff *= falloff; - - AngleVectors( g_camera.viewangles, forward, NULL, NULL ); - view_ofs[0] = view_ofs[1] = 0.0f; - if( entPlayer->curstate.usehull == 1 ) { - view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; - } else { - view_ofs[2] = 28.0f; // DEFAULT_VIEWHEIGHT - } - VectorAdd( entPlayer->origin, view_ofs, vecSrc ); - VectorMA( vecSrc, FLASHLIGHT_DISTANCE, forward, vecEnd ); - trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_NORMAL ); - VectorMA( trace->endpos, -10, forward, l->origin ); - - // apply brigthness to dlight - l->color.r = bound( 0, falloff * 255, 255 ); - l->color.g = bound( 0, falloff * 255, 255 ); - l->color.b = bound( 0, falloff * 255, 255 ); - l->radius = 75; - - Vector4Set( - ubo_lights->light[num_lights].color, - l->color.r / 255.f, - l->color.g / 255.f, - l->color.b / 255.f, - 1.f); - Vector4Set( - ubo_lights->light[num_lights].pos_r, - l->origin[0], - l->origin[1], - l->origin[2], - l->radius); - } else { - Vector4Set( - ubo_lights->light[num_lights].color, - l->color.r / 255.f, - l->color.g / 255.f, - l->color.b / 255.f, - 1.f); - Vector4Set( - ubo_lights->light[num_lights].pos_r, - l->origin[0], - l->origin[1], - l->origin[2], - l->radius); - } + Vector4Set( + ubo_lights->light[num_lights].color, + l->color.r / 255.f, + l->color.g / 255.f, + l->color.b / 255.f, + 1.f); + Vector4Set( + ubo_lights->light[num_lights].pos_r, + l->origin[0], + l->origin[1], + l->origin[2], + l->radius); num_lights++; } diff --git a/ref_vk/vk_rmain.c b/ref_vk/vk_rmain.c index 8fc66c51..3ef89370 100644 --- a/ref_vk/vk_rmain.c +++ b/ref_vk/vk_rmain.c @@ -1,4 +1,5 @@ #include "vk_core.h" +#include "vk_cvar.h" #include "vk_common.h" #include "vk_textures.h" #include "vk_renderstate.h" @@ -250,7 +251,10 @@ static int VK_RefGetParm( int parm, int arg ) tex = findTexture(arg); return tex->flags; case PARM_MODERNFLASHLIGHT: - return true; + if (CVAR_TO_BOOL( vk_rtx )) { + return true; + } + return false; } PRINT_NOT_IMPLEMENTED_ARGS("(%s(%d), %d)", getParmName(parm), parm, arg); From 8848502572be2c25ff346c4bd93e17c1f1c08bb7 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 00:58:26 +0300 Subject: [PATCH 11/19] cleanup again --- ref_vk/vk_light.c | 1 - ref_vk/vk_render.c | 5 +---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 428d856b..ae0965ad 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -927,7 +927,6 @@ void VK_AddFlashlight( cl_entity_t *ent ) { addSpotLight(&le, radius, 0, hack_attenuation, false); } - static float sphereSolidAngleFromDistDiv2Pi(float r, float d) { return 1. - sqrt(d*d - r*r)/d; } diff --git a/ref_vk/vk_render.c b/ref_vk/vk_render.c index 520894f9..da741d69 100644 --- a/ref_vk/vk_render.c +++ b/ref_vk/vk_render.c @@ -14,10 +14,6 @@ #include "xash3d_mathlib.h" #include "protocol.h" // MAX_DLIGHTS -#include "camera.h" -#include "pm_defs.h" -#include "pmtrace.h" - #include #define MAX_UNIFORM_SLOTS (MAX_SCENE_ENTITIES * 2 /* solid + trans */ + 1) @@ -566,6 +562,7 @@ static uint32_t writeDlightsToUBO( void ) l->origin[1], l->origin[2], l->radius); + num_lights++; } From 13914830b7dd4df6457a53b16b5876b0f19661d4 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 02:16:55 +0300 Subject: [PATCH 12/19] add workaround for multiplayer --- engine/client/cl_tent.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index dfb4e067..1253cb03 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -2709,6 +2709,10 @@ void CL_AddEntityEffects( cl_entity_t *ent ) dl->die = cl.time + 0.001; } } + else if (ent->player && ent->index != ( cl.playernum + 1 )) + { + CL_UpdateFlashlight( ent ); + } } else if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) { From 9420fc3e39f9de5be45e53add055d867debdd115 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 15:09:29 +0300 Subject: [PATCH 13/19] general fix flashlight for other players in multiplayer mode --- engine/client/cl_tent.c | 4 ---- ref_vk/vk_light.c | 13 ++++++------- ref_vk/vk_scene.c | 4 ++++ 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index 1253cb03..dfb4e067 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -2709,10 +2709,6 @@ void CL_AddEntityEffects( cl_entity_t *ent ) dl->die = cl.time + 0.001; } } - else if (ent->player && ent->index != ( cl.playernum + 1 )) - { - CL_UpdateFlashlight( ent ); - } } else if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) { diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index ae0965ad..ce6a02f6 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -896,8 +896,7 @@ void VK_AddFlashlight( cl_entity_t *ent ) { } else // non-local player case { - // TODO: need to test! - AngleVectors( ent->angles, angles, NULL, NULL ); // TODO: maybe improve turning sensitivity + AngleVectors( ent->angles, angles, NULL, NULL ); // FIXME: UP-DOWN logic view_ofs[0] = view_ofs[1] = 0.0f; if( ent->curstate.usehull == 1 ) { view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; @@ -908,15 +907,15 @@ void VK_AddFlashlight( cl_entity_t *ent ) { VectorMA( vecSrc, thirdperson_offset, angles, vecEnd ); trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); VectorCopy( trace->endpos, origin ); - angles[ROLL] = -angles[ROLL]; + angles[ROLL] = -angles[ROLL]; // FIXME: UP-DOWN logic VectorCopy( angles, le.dir ); } /* gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f) dir=(%f %f %f)\n", - plight->origin[0], plight->origin[1], plight->origin[2], - plight->color[0], plight->color[1], plight->color[2], - plight->dir[0], plight->dir[1], plight->dir[2]); + le.origin[0], le.origin[1], le.origin[2], + le.color[0], le.color[1], le.color[2], + le.dir[0], le.dir[1], le.dir[2]); */ VectorCopy(origin, le.origin); @@ -1102,7 +1101,7 @@ void VK_LightsFrameFinalize( void ) { continue; addDlight(dlight); } - // Draw flashlight + // Draw flashlight for local player entPlayer = gEngine.GetLocalPlayer(); if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { VK_AddFlashlight(entPlayer); diff --git a/ref_vk/vk_scene.c b/ref_vk/vk_scene.c index 6e27fa2e..854b3f61 100644 --- a/ref_vk/vk_scene.c +++ b/ref_vk/vk_scene.c @@ -634,6 +634,10 @@ void VK_SceneRender( const ref_viewpass_t *rvp ) { { cl_entity_t *ent = g_lists.draw_list->solid_entities[i]; drawEntity(ent, kRenderNormal); + // Draw flashlight for other players + if( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) { + VK_AddFlashlight(ent); + } } // Draw opaque beams From 2d6de8b149a4d6ac76b82fb8b50fbba3ccd6968a Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 15:16:46 +0300 Subject: [PATCH 14/19] fix error: implicit declaration of function --- ref_vk/vk_scene.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ref_vk/vk_scene.c b/ref_vk/vk_scene.c index 854b3f61..0c557e68 100644 --- a/ref_vk/vk_scene.c +++ b/ref_vk/vk_scene.c @@ -595,6 +595,8 @@ static void drawEntity( cl_entity_t *ent, int render_mode ) static float g_frametime = 0; +void VK_AddFlashlight(cl_entity_t *ent); + void VK_SceneRender( const ref_viewpass_t *rvp ) { int current_pipeline_index = kRenderNormal; From 09493cbcba5e36252e26d0bf09b5c1b2483d26f0 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Tue, 28 Dec 2021 18:25:14 +0300 Subject: [PATCH 15/19] fix multiplayer angles --- ref_vk/vk_light.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index ce6a02f6..fbbc218f 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -848,20 +848,12 @@ void VK_AddFlashlight( cl_entity_t *ent ) { // parameters const float hack_attenuation = 0.1; - const float radius = 1.0; + float radius = 1.0; // TODO: better tune it const float _cone = 10.0; const float _cone2 = 30.0; const vec3_t light_color = {255, 255, 192}; - const float light_intensity = 300; - - // prepare colors by parseEntPropRgbav - VectorScale(light_color, light_intensity / 255.0f, color); - - // convert colors by weirdGoldsrcLightScaling - float l1 = Q_max(color[0], Q_max(color[1], color[2])); - l1 = l1 * l1 / 10; - VectorScale(color, l1, le.color); + float light_intensity = 300; float thirdperson_offset = 25; vec3_t forward, view_ofs; @@ -896,7 +888,17 @@ void VK_AddFlashlight( cl_entity_t *ent ) { } else // non-local player case { - AngleVectors( ent->angles, angles, NULL, NULL ); // FIXME: UP-DOWN logic + thirdperson_offset = 10; + radius = 10; + light_intensity = 60; + + VectorCopy( ent->angles, angles ); + // NOTE: pitch divided by 3.0 twice. So we need apply 3^2 = 9 + angles[PITCH] = ent->curstate.angles[PITCH] * 9.0f; + angles[YAW] = ent->angles[YAW]; + angles[ROLL] = 0.0f; // roll not used + + AngleVectors( angles, angles, NULL, NULL ); view_ofs[0] = view_ofs[1] = 0.0f; if( ent->curstate.usehull == 1 ) { view_ofs[2] = 12.0f; // VEC_DUCK_VIEW; @@ -907,10 +909,23 @@ void VK_AddFlashlight( cl_entity_t *ent ) { VectorMA( vecSrc, thirdperson_offset, angles, vecEnd ); trace = gEngine.EV_VisTraceLine( vecSrc, vecEnd, PM_STUDIO_BOX ); VectorCopy( trace->endpos, origin ); - angles[ROLL] = -angles[ROLL]; // FIXME: UP-DOWN logic VectorCopy( angles, le.dir ); } + VectorCopy(origin, le.origin); + + // prepare colors by parseEntPropRgbav + VectorScale(light_color, light_intensity / 255.0f, color); + + // convert colors by weirdGoldsrcLightScaling + float l1 = Q_max(color[0], Q_max(color[1], color[2])); + l1 = l1 * l1 / 10; + VectorScale(color, l1, le.color); + + // convert stopdots by parseStopDot + le.stopdot = cosf(_cone * M_PI / 180.f); + le.stopdot2 = cosf(_cone2 * M_PI / 180.f); + /* gEngine.Con_Printf("flashlight: origin=(%f %f %f) color=(%f %f %f) dir=(%f %f %f)\n", le.origin[0], le.origin[1], le.origin[2], @@ -918,11 +933,6 @@ void VK_AddFlashlight( cl_entity_t *ent ) { le.dir[0], le.dir[1], le.dir[2]); */ - VectorCopy(origin, le.origin); - // convert stopdots by parseStopDot - le.stopdot = cosf(_cone * M_PI / 180.f); - le.stopdot2 = cosf(_cone2 * M_PI / 180.f); - addSpotLight(&le, radius, 0, hack_attenuation, false); } From 1501f7fea4bca1a8ac10b981b4cc047440683232 Mon Sep 17 00:00:00 2001 From: NightFox <0x4E69676874466F78@users.noreply.github.com> Date: Wed, 29 Dec 2021 12:21:33 +0300 Subject: [PATCH 16/19] more correct flashlight position in first person by @MaxG2D --- ref_vk/vk_light.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index fbbc218f..766033b7 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -878,11 +878,10 @@ void VK_AddFlashlight( cl_entity_t *ent ) { VectorCopy( forward, le.dir); } else { // firstperson // based on https://github.com/SNMetamorph/PrimeXT/blob/0869b1abbddd13c1229769d8cd71941610be0bf3/client/flashlight.cpp#L35 - // TODO: tune it - origin[0] = g_camera.vieworg[0] + (g_camera.vright[0] * 5.0f) + (g_camera.vforward[0] * 2.0f); // forward-back - origin[1] = g_camera.vieworg[1] + (g_camera.vright[1] * 5.0f) + (g_camera.vforward[1] * 2.0f); // left-right - origin[2] = g_camera.vieworg[2] + (g_camera.vright[2] * 5.0f) + (g_camera.vforward[2] * 2.0f); // up-down - origin[2] += 6.0f; + origin[0] = g_camera.vieworg[0] + (g_camera.vright[0] * (-4.0f)) + (g_camera.vforward[0] * 14.0); // forward-back + origin[1] = g_camera.vieworg[1] + (g_camera.vright[1] * (-4.0f)) + (g_camera.vforward[1] * 14.0); // left-right + origin[2] = g_camera.vieworg[2] + (g_camera.vright[2] * (-4.0f)) + (g_camera.vforward[2] * 14.0); // up-down + origin[2] += 2.0f; VectorCopy(g_camera.vforward, le.dir); } } From cf09305f79cc00ece48133c04e2d5f3046ccf62b Mon Sep 17 00:00:00 2001 From: Ivan Avdeev Date: Wed, 29 Dec 2021 19:05:40 -0800 Subject: [PATCH 17/19] prettify modern flashlight addition to cl_tent --- engine/client/cl_tent.c | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/engine/client/cl_tent.c b/engine/client/cl_tent.c index dfb4e067..30fa652e 100644 --- a/engine/client/cl_tent.c +++ b/engine/client/cl_tent.c @@ -2696,26 +2696,15 @@ void CL_AddEntityEffects( cl_entity_t *ent ) if( FBitSet( ent->curstate.effects, EF_BRIGHTFIELD )) R_EntityParticles( ent ); - if (REF_GET_PARM( PARM_MODERNFLASHLIGHT, 1 ) == true) + if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) { - if( !ent->player ) + if ( ent->player && !Host_IsQuakeCompatible( )) { - if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT ) ) + if ( !REF_GET_PARM( PARM_MODERNFLASHLIGHT, 1)) { - dlight_t *dl = CL_AllocDlight( ent->index ); - dl->color.r = dl->color.g = dl->color.b = 100; - dl->radius = COM_RandomFloat( 200, 231 ); - VectorCopy( ent->origin, dl->origin ); - dl->die = cl.time + 0.001; + CL_UpdateFlashlight( ent ); } } - } - else if ( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) - { - if( ent->player && !Host_IsQuakeCompatible( )) - { - CL_UpdateFlashlight( ent ); - } else { dlight_t *dl = CL_AllocDlight( ent->index ); From 51a29c012c59cd7271f836458d17bbef255c020d Mon Sep 17 00:00:00 2001 From: Ivan Avdeev Date: Wed, 29 Dec 2021 19:27:46 -0800 Subject: [PATCH 18/19] make flashlight handling a bit cleaner --- ref_vk/vk_light.c | 20 +++++++------------- ref_vk/vk_light.h | 3 +++ ref_vk/vk_scene.c | 16 +++++++++++----- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/ref_vk/vk_light.c b/ref_vk/vk_light.c index 766033b7..2b8d108d 100644 --- a/ref_vk/vk_light.c +++ b/ref_vk/vk_light.c @@ -840,12 +840,7 @@ static int addSpotLight( const vk_light_entity_t *le, float radius, int lightsty return index; } -void VK_AddFlashlight( cl_entity_t *ent ) { - vec3_t color; - vec3_t origin; - vec3_t angles; - vk_light_entity_t le; - +void R_LightAddFlashlight(const struct cl_entity_s *ent, qboolean local_player ) { // parameters const float hack_attenuation = 0.1; float radius = 1.0; @@ -855,11 +850,16 @@ void VK_AddFlashlight( cl_entity_t *ent ) { const vec3_t light_color = {255, 255, 192}; float light_intensity = 300; + vec3_t color; + vec3_t origin; + vec3_t angles; + vk_light_entity_t le; + float thirdperson_offset = 25; vec3_t forward, view_ofs; vec3_t vecSrc, vecEnd; pmtrace_t *trace; - if( ent->index == gEngine.EngineGetParm(PARM_PLAYER_INDEX, 0)) + if( local_player ) { // local player case // position @@ -1077,7 +1077,6 @@ void XVK_GetEmissiveForTexture( vec3_t out, int texture_id ) { void VK_LightsFrameFinalize( void ) { APROF_SCOPE_BEGIN_EARLY(finalize); const model_t* const world = gEngine.pfnGetModelByIndex( 1 ); - cl_entity_t *entPlayer; if (g_lights.num_emissive_surfaces > UINT8_MAX) { ERROR_THROTTLED(10, "Too many emissive surfaces found: %d; some areas will be dark", g_lights.num_emissive_surfaces); @@ -1110,11 +1109,6 @@ void VK_LightsFrameFinalize( void ) { continue; addDlight(dlight); } - // Draw flashlight for local player - entPlayer = gEngine.GetLocalPlayer(); - if( FBitSet( entPlayer->curstate.effects, EF_DIMLIGHT )) { - VK_AddFlashlight(entPlayer); - } APROF_SCOPE_END(dlights); if (debug_dump_lights.enabled) { diff --git a/ref_vk/vk_light.h b/ref_vk/vk_light.h index 9c144ad1..d61aa045 100644 --- a/ref_vk/vk_light.h +++ b/ref_vk/vk_light.h @@ -92,3 +92,6 @@ void XVK_GetEmissiveForTexture( vec3_t out, int texture_id ); void VK_LightsFrameFinalize( void ); int R_LightCellIndex( const int light_cell[3] ); + +struct cl_entity_s; +void R_LightAddFlashlight( const struct cl_entity_s *ent, qboolean local_player ); diff --git a/ref_vk/vk_scene.c b/ref_vk/vk_scene.c index 0c557e68..ac68bcbb 100644 --- a/ref_vk/vk_scene.c +++ b/ref_vk/vk_scene.c @@ -595,10 +595,8 @@ static void drawEntity( cl_entity_t *ent, int render_mode ) static float g_frametime = 0; -void VK_AddFlashlight(cl_entity_t *ent); - void VK_SceneRender( const ref_viewpass_t *rvp ) { - int current_pipeline_index = kRenderNormal; + const cl_entity_t* const local_player = gEngine.GetLocalPlayer(); g_frametime = /*FIXME VK RP_NORMALPASS( )) ? */ gpGlobals->time - gpGlobals->oldtime @@ -631,14 +629,22 @@ void VK_SceneRender( const ref_viewpass_t *rvp ) { } } + { + // Draw flashlight for local player + if( FBitSet( local_player->curstate.effects, EF_DIMLIGHT )) { + R_LightAddFlashlight(local_player, true); + } + } + // Draw opaque entities for (int i = 0; i < g_lists.draw_list->num_solid_entities; ++i) { cl_entity_t *ent = g_lists.draw_list->solid_entities[i]; drawEntity(ent, kRenderNormal); + // Draw flashlight for other players - if( FBitSet( ent->curstate.effects, EF_DIMLIGHT )) { - VK_AddFlashlight(ent); + if( FBitSet( ent->curstate.effects, EF_DIMLIGHT ) && ent != local_player) { + R_LightAddFlashlight(ent, false); } } From 8b7ff39efb60273122b98c988df978d0e0b0f6eb Mon Sep 17 00:00:00 2001 From: Ivan 'provod' Avdeev Date: Wed, 5 Jan 2022 20:04:37 -0800 Subject: [PATCH 19/19] print more info about vulkan memory and allocations --- ref_vk/vk_core.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- ref_vk/vk_core.h | 5 +++-- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/ref_vk/vk_core.c b/ref_vk/vk_core.c index 4d3488a7..cb0b25ab 100644 --- a/ref_vk/vk_core.c +++ b/ref_vk/vk_core.c @@ -440,6 +440,30 @@ static int enumerateDevices( vk_available_device_t **available_devices ) { return this_device - *available_devices; } +static void devicePrintMemoryInfo(const VkPhysicalDeviceMemoryProperties *props, const VkPhysicalDeviceMemoryBudgetPropertiesEXT *budget) { + gEngine.Con_Printf("Memory heaps: %d\n", props->memoryHeapCount); + for (int i = 0; i < (int)props->memoryHeapCount; ++i) { + const VkMemoryHeap* const heap = props->memoryHeaps + i; + gEngine.Con_Printf(" %d: size=%dMb used=%dMb avail=%dMb device_local=%d\n", i, + (int)(heap->size / (1024 * 1024)), + (int)(budget->heapUsage[i] / (1024 * 1024)), + (int)(budget->heapBudget[i] / (1024 * 1024)), + !!(heap->flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)); + } + + gEngine.Con_Printf("Memory types: %d\n", props->memoryTypeCount); + for (int i = 0; i < (int)props->memoryTypeCount; ++i) { + const VkMemoryType* const type = props->memoryTypes + i; + gEngine.Con_Printf(" %d: heap=%d flags=%c%c%c%c%c\n", i, type->heapIndex, + type->propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ? 'D' : '.', + type->propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? 'V' : '.', + type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? 'C' : '.', + type->propertyFlags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT ? '$' : '.', + type->propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT ? 'L' : '.' + ); + } +} + static qboolean createDevice( void ) { void *head = NULL; vk_available_device_t *available_devices; @@ -531,13 +555,20 @@ static qboolean createDevice( void ) { .ppEnabledExtensionNames = device_extensions, }; - // FIXME do only once - vkGetPhysicalDeviceMemoryProperties(candidate_device->device, &vk_core.physical_device.memory_properties); + { + vk_core.physical_device.memory_properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2; + vk_core.physical_device.memory_properties2.pNext = &vk_core.physical_device.memory_budget; + vk_core.physical_device.memory_budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT; + vk_core.physical_device.memory_budget.pNext = NULL; + vkGetPhysicalDeviceMemoryProperties2(candidate_device->device, &vk_core.physical_device.memory_properties2); + } gEngine.Con_Printf("Trying device #%d: %04x:%04x %d %s %u.%u.%u %u.%u.%u\n", i, candidate_device->props.vendorID, candidate_device->props.deviceID, candidate_device->props.deviceType, candidate_device->props.deviceName, XVK_PARSE_VERSION(candidate_device->props.driverVersion), XVK_PARSE_VERSION(candidate_device->props.apiVersion)); + devicePrintMemoryInfo(&vk_core.physical_device.memory_properties2.memoryProperties, &vk_core.physical_device.memory_budget); + { const VkResult result = vkCreateDevice(candidate_device->device, &create_info, NULL, &vk_core.device); if (result != VK_SUCCESS) { @@ -873,11 +904,11 @@ void destroyFence(VkFence fence) { } static uint32_t findMemoryWithType(uint32_t type_index_bits, VkMemoryPropertyFlags flags) { - for (uint32_t i = 0; i < vk_core.physical_device.memory_properties.memoryTypeCount; ++i) { + for (uint32_t i = 0; i < vk_core.physical_device.memory_properties2.memoryProperties.memoryTypeCount; ++i) { if (!(type_index_bits & (1 << i))) continue; - if ((vk_core.physical_device.memory_properties.memoryTypes[i].propertyFlags & flags) == flags) + if ((vk_core.physical_device.memory_properties2.memoryProperties.memoryTypes[i].propertyFlags & flags) == flags) return i; } @@ -900,6 +931,15 @@ device_memory_t allocateDeviceMemory(VkMemoryRequirements req, VkMemoryPropertyF .memoryTypeIndex = findMemoryWithType(req.memoryTypeBits, props), }; + gEngine.Con_Reportf("allocateDeviceMemory size=%zu memoryTypeBits=0x%x memoryProperties=%c%c%c%c%c flags=0x%x => typeIndex=%d\n", req.size, req.memoryTypeBits, + props & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ? 'D' : '.', + props & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? 'V' : '.', + props & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? 'C' : '.', + props & VK_MEMORY_PROPERTY_HOST_CACHED_BIT ? '$' : '.', + props & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT ? 'L' : '.', + flags, + mai.memoryTypeIndex); + ASSERT(mai.memoryTypeIndex != UINT32_MAX); XVK_CHECK(vkAllocateMemory(vk_core.device, &mai, NULL, &ret.device_memory)); return ret; diff --git a/ref_vk/vk_core.h b/ref_vk/vk_core.h index dbf6d408..e3944ddd 100644 --- a/ref_vk/vk_core.h +++ b/ref_vk/vk_core.h @@ -38,7 +38,8 @@ typedef struct vk_buffer_s typedef struct physical_device_s { VkPhysicalDevice device; - VkPhysicalDeviceMemoryProperties memory_properties; + VkPhysicalDeviceMemoryProperties2 memory_properties2; + VkPhysicalDeviceMemoryBudgetPropertiesEXT memory_budget; VkPhysicalDeviceProperties properties; VkPhysicalDeviceProperties2 properties2; VkPhysicalDeviceAccelerationStructurePropertiesKHR properties_accel; @@ -133,7 +134,7 @@ do { \ X(vkGetPhysicalDeviceFeatures2) \ X(vkGetPhysicalDeviceQueueFamilyProperties) \ X(vkGetPhysicalDeviceSurfaceSupportKHR) \ - X(vkGetPhysicalDeviceMemoryProperties) \ + X(vkGetPhysicalDeviceMemoryProperties2) \ X(vkGetPhysicalDeviceSurfacePresentModesKHR) \ X(vkGetPhysicalDeviceSurfaceFormatsKHR) \ X(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \