From a281768ac9ff9376073e5b6f861b17ac3c8c6a1b Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 22 Jan 2023 12:09:12 +0100 Subject: [PATCH] BHit performance improvements Use ray trace directly instead of build-in drawline command. --- r5dev/tier1/IConVar.cpp | 3 +++ r5dev/tier1/cvar.cpp | 6 ++++++ r5dev/tier1/cvar.h | 6 ++++++ r5dev/vstdlib/callback.cpp | 23 ++++++++++++++--------- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/r5dev/tier1/IConVar.cpp b/r5dev/tier1/IConVar.cpp index 387e603b..5bc31e02 100644 --- a/r5dev/tier1/IConVar.cpp +++ b/r5dev/tier1/IConVar.cpp @@ -119,6 +119,7 @@ void ConVar::Init(void) #endif // DEDICATED #endif // !CLIENT_DLL #if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) + bhit_zbuffer = ConVar::Create("bhit_zbuffer" , "1", FCVAR_DEVELOPMENTONLY | FCVAR_REPLICATED, "Use z-buffer for bullet ray trace overlay.", false, 0.f, false, 0.f, nullptr, nullptr); bhit_abs_origin = ConVar::Create("bhit_abs_origin", "1", FCVAR_DEVELOPMENTONLY | FCVAR_REPLICATED, "Draw entity's predicted abs origin upon bullet impact for trajectory debugging.", false, 0.f, false, 0.f, nullptr, nullptr); #endif // !GAMEDLL_S0 && !GAMEDLL_S1 //------------------------------------------------------------------------- @@ -254,6 +255,8 @@ void ConVar::InitShipped(void) #ifndef DEDICATED rui_defaultDebugFontFace = g_pCVar->FindVar("rui_defaultDebugFontFace"); #endif // !DEDICATED + r_visualizetraces = g_pCVar->FindVar("r_visualizetraces"); + r_visualizetraces_duration = g_pCVar->FindVar("r_visualizetraces_duration"); staticProp_no_fade_scalar = g_pCVar->FindVar("staticProp_no_fade_scalar"); staticProp_gather_size_weight = g_pCVar->FindVar("staticProp_gather_size_weight"); stream_overlay = g_pCVar->FindVar("stream_overlay"); diff --git a/r5dev/tier1/cvar.cpp b/r5dev/tier1/cvar.cpp index 48412c51..f0c6c13a 100644 --- a/r5dev/tier1/cvar.cpp +++ b/r5dev/tier1/cvar.cpp @@ -41,6 +41,11 @@ ConVar* r_drawWorldMeshes = nullptr; ConVar* r_drawWorldMeshesDepthOnly = nullptr; ConVar* r_drawWorldMeshesDepthAtTheEnd = nullptr; +#ifndef DEDICATED +ConVar* r_visualizetraces = nullptr; +ConVar* r_visualizetraces_duration = nullptr; +#endif // !DEDICATED + ConVar* stream_overlay = nullptr; ConVar* stream_overlay_mode = nullptr; //----------------------------------------------------------------------------- @@ -90,6 +95,7 @@ ConVar* sv_visualizetraces = nullptr; ConVar* sv_visualizetraces_duration = nullptr; #if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) ConVar* bhit_enable = nullptr; +ConVar* bhit_zbuffer = nullptr; ConVar* bhit_abs_origin = nullptr; #endif // !GAMEDLL_S0 && !GAMEDLL_S1 //----------------------------------------------------------------------------- diff --git a/r5dev/tier1/cvar.h b/r5dev/tier1/cvar.h index 4e3b3442..747926e6 100644 --- a/r5dev/tier1/cvar.h +++ b/r5dev/tier1/cvar.h @@ -38,6 +38,11 @@ extern ConVar* r_drawWorldMeshes; extern ConVar* r_drawWorldMeshesDepthOnly; extern ConVar* r_drawWorldMeshesDepthAtTheEnd; +#ifndef DEDICATED +extern ConVar* r_visualizetraces; +extern ConVar* r_visualizetraces_duration; +#endif // !DEDICATED + extern ConVar* stream_overlay; extern ConVar* stream_overlay_mode; //------------------------------------------------------------------------- @@ -86,6 +91,7 @@ extern ConVar* sv_visualizetraces; extern ConVar* sv_visualizetraces_duration; #if !defined (GAMEDLL_S0) && !defined (GAMEDLL_S1) extern ConVar* bhit_enable; +extern ConVar* bhit_zbuffer; extern ConVar* bhit_abs_origin; #endif // !GAMEDLL_S0 && !GAMEDLL_S1 //------------------------------------------------------------------------- diff --git a/r5dev/vstdlib/callback.cpp b/r5dev/vstdlib/callback.cpp index 6ca5b06a..ef85352a 100644 --- a/r5dev/vstdlib/callback.cpp +++ b/r5dev/vstdlib/callback.cpp @@ -19,6 +19,7 @@ #include "engine/net.h" #include "engine/host_cmd.h" #include "engine/host_state.h" +#include "engine/enginetrace.h" #ifndef CLIENT_DLL #include "engine/server/server.h" #endif // !CLIENT_DLL @@ -47,6 +48,8 @@ #include "vstdlib/callback.h" #ifndef DEDICATED #include "materialsystem/cmaterialglue.h" +#include "public/bspflags.h" +#include "public/cmodel.h" #include "public/idebugoverlay.h" #endif // !DEDICATED #ifndef CLIENT_DLL @@ -1161,21 +1164,23 @@ void BHit_f(const CCommand& args) vecBulletAngles.z = 180.f; // Flipped axis. AngleVectors(vecBulletAngles, &vecAbsEnd); - static char szBuf[2048]; // Render physics trace. - snprintf(szBuf, sizeof(szBuf), "drawline %g %g %g %g %g %g", - vecAbsStart.x, vecAbsStart.y, vecAbsStart.z, - vecAbsStart.x + vecAbsEnd.x * MAX_COORD_RANGE, - vecAbsStart.y + vecAbsEnd.y * MAX_COORD_RANGE, - vecAbsStart.z + vecAbsEnd.z * MAX_COORD_RANGE); - Cbuf_AddText(Cbuf_GetCurrentPlayer(), szBuf, cmd_source_t::kCommandSrcCode); + vecAbsEnd.MulAdd(vecAbsStart, vecAbsEnd, MAX_COORD_RANGE); - if (bhit_abs_origin->GetBool()) + Ray_t ray(vecAbsStart, vecAbsEnd); + trace_t trace; + + g_pEngineTraceServer->TraceRay(ray, TRACE_MASK_NPCWORLDSTATIC, &trace); + + g_pDebugOverlay->AddLineOverlay(trace.startpos, trace.endpos, 0, 255, 0, bhit_zbuffer->GetBool(), sv_visualizetraces_duration->GetFloat()); + g_pDebugOverlay->AddLineOverlay(trace.endpos, vecAbsEnd, 255, 0, 0, bhit_zbuffer->GetBool(), sv_visualizetraces_duration->GetFloat()); + + if (bhit_abs_origin->GetBool() && r_visualizetraces->GetBool()) { const int iEnt = atoi(args[2]); if (const IClientEntity* pEntity = g_pClientEntityList->GetClientEntity(iEnt)) { g_pDebugOverlay->AddSphereOverlay( // Render a debug sphere at the client's predicted entity origin. - pEntity->GetAbsOrigin(), 10.f, 8, 6, 20, 60, 255, 0, sv_visualizetraces_duration->GetFloat()); + pEntity->GetAbsOrigin(), 10.f, 8, 6, 20, 60, 255, 0, r_visualizetraces_duration->GetFloat()); } } #endif // !DEDICATED