Skip to content

Commit

Permalink
fix out of EntityRenderComponent.
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Feb 25, 2025
1 parent 8957cf4 commit 1e39076
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 260 deletions.
2 changes: 2 additions & 0 deletions Plugins/Renderer/enginedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ typedef struct alight_s
#define STUDIO_NF_ALPHA 0x0010
#define STUDIO_NF_ADDITIVE 0x0020
#define STUDIO_NF_MASKED 0x0040

// renderer-private options
#define STUDIO_NF_CELSHADE 0x1000
#define STUDIO_NF_CELSHADE_FACE 0x2000
#define STUDIO_NF_CELSHADE_HAIR 0x4000
Expand Down
97 changes: 47 additions & 50 deletions Plugins/Renderer/gl_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,77 @@
#include "pm_defs.h"
#include <algorithm>

#define MAX_ENTITY_COMPONENTS 1024

CEntityComponentContainer gEntityComponentPool[MAX_ENTITY_COMPONENTS];

CEntityComponentContainer*gpEntityComponentActive = NULL;
CEntityComponentContainer*gpEntityComponentFree = NULL;

std::vector<CEntityComponentContainer*> g_ClientEntityRenderComponents;
std::vector<CEntityComponentContainer*> g_TempEntityRenderComponents;
CEntityComponentContainer* g_ViewEntityRenderComponent = NULL;

void R_InitEntityComponents(void)
{
for (int i = 0; i < MAX_ENTITY_COMPONENTS; i++)
{
gEntityComponentPool[i].pNext = &gEntityComponentPool[i + 1];
gEntityComponentPool[i].Decals.clear();
gEntityComponentPool[i].WaterVBOs.clear();
gEntityComponentPool[i].ReflectCaches.clear();
gEntityComponentPool[i].DeferredStudioPasses.clear();
}

gEntityComponentPool[MAX_ENTITY_COMPONENTS - 1].pNext = NULL;
gpEntityComponentFree = &gEntityComponentPool[0];
gpEntityComponentActive = NULL;

g_ClientEntityRenderComponents.clear();
g_TempEntityRenderComponents.clear();
g_ViewEntityRenderComponent = nullptr;
}

void R_ShutdownEntityComponents(void)
{
R_InitEntityComponents();
}

CEntityComponentContainer *R_AllocateEntityComponentContainer(void)
{
if (!gpEntityComponentFree)
for (auto itor = g_ClientEntityRenderComponents.begin(); itor != g_ClientEntityRenderComponents.end(); itor++)
{
gEngfuncs.Con_DPrintf("Overflow entity component container!\n");
return NULL;
auto pContainer = (*itor);

if (pContainer)
{
delete pContainer;
}
}
g_ClientEntityRenderComponents.clear();

auto pTemp = gpEntityComponentFree;
gpEntityComponentFree = pTemp->pNext;
for (auto itor = g_TempEntityRenderComponents.begin(); itor != g_TempEntityRenderComponents.end(); itor++)
{
auto pContainer = (*itor);

pTemp->pNext = gpEntityComponentActive;
gpEntityComponentActive = pTemp;
if (pContainer)
{
delete pContainer;
}
}
g_TempEntityRenderComponents.clear();

return pTemp;
if (g_ViewEntityRenderComponent)
{
delete g_ViewEntityRenderComponent;
g_ViewEntityRenderComponent = nullptr;
}
}

void R_EntityComponents_PreFrame(void)
CEntityComponentContainer *R_AllocateEntityComponentContainer(void)
{
auto p = gpEntityComponentActive;
while (p)
{
p->Decals.clear();
p->WaterVBOs.clear();
p->ReflectCaches.clear();
p->DeferredStudioPasses.clear();
p->AimEntity = nullptr;
return new CEntityComponentContainer();
}

auto temp = p->pNext;
void R_EntityComponents_StartFrame(void)
{
for (auto itor = g_ClientEntityRenderComponents.begin(); itor != g_ClientEntityRenderComponents.end(); itor++)
{
auto pContainer = (*itor);

p->pNext = gpEntityComponentFree;
gpEntityComponentFree = p;
if (pContainer)
{
pContainer->Reset();
}
}
for (auto itor = g_TempEntityRenderComponents.begin(); itor != g_TempEntityRenderComponents.end(); itor++)
{
auto pContainer = (*itor);

p = temp;
if (pContainer)
{
pContainer->Reset();
}
}
if (g_ViewEntityRenderComponent)
{
g_ViewEntityRenderComponent->Reset();
}
gpEntityComponentActive = NULL;

g_ClientEntityRenderComponents.clear();
g_TempEntityRenderComponents.clear();
}

int EngineGetMaxClientEdicts(void)
Expand Down
13 changes: 10 additions & 3 deletions Plugins/Renderer/gl_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,15 @@ class bspentity_t
class CEntityComponentContainer
{
public:
CEntityComponentContainer* pNext{};
//std::vector<cl_entity_t *> FollowEnts;
void Reset()
{
Decals.clear();
WaterVBOs.clear();
ReflectCaches.clear();
DeferredStudioPasses.clear();
AimEntity = nullptr;
}

std::vector<decal_t *> Decals;
std::vector<CWaterSurfaceModel *> WaterVBOs;
std::vector<water_reflect_cache_t *> ReflectCaches;
Expand All @@ -49,4 +56,4 @@ void R_InitEntityComponents(void);

void R_ShutdownEntityComponents(void);

void R_EntityComponents_PreFrame(void);
void R_EntityComponents_StartFrame(void);
82 changes: 41 additions & 41 deletions Plugins/Renderer/gl_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7895,9 +7895,9 @@ void sub_1D1A030()
int ZerodReg_InstCount;
int Candidate_locallight_InstCount;
PVOID Candidate_locallight;
int Candidate_numlight_instCount;
int Candidate_numlight_reg;
PVOID Candidate_numlight;
int Candidate_numlights_instCount;
int Candidate_numlights_reg;
PVOID Candidate_numlights;

int Candidate_locallight2_InstCount;
int Candidate_locallight2_reg;
Expand Down Expand Up @@ -8007,7 +8007,7 @@ void sub_1D1A030()
locallight = (decltype(locallight))ctx->Candidate_locallight2;
}

if (!numlight &&
if (!numlights &&
pinst->id == X86_INS_MOV &&
pinst->detail->x86.op_count == 2 &&
pinst->detail->x86.operands[1].type == X86_OP_MEM &&
Expand All @@ -8017,43 +8017,43 @@ void sub_1D1A030()
(PUCHAR)pinst->detail->x86.operands[1].mem.disp < (PUCHAR)g_dwEngineDataBase + g_dwEngineDataSize &&
pinst->detail->x86.operands[0].type == X86_OP_REG)
{
ctx->Candidate_numlight = (decltype(ctx->Candidate_numlight))pinst->detail->x86.operands[1].mem.disp;
ctx->Candidate_numlight_instCount = instCount;
ctx->Candidate_numlight_reg = pinst->detail->x86.operands[0].reg;
ctx->Candidate_numlights = (decltype(ctx->Candidate_numlights))pinst->detail->x86.operands[1].mem.disp;
ctx->Candidate_numlights_instCount = instCount;
ctx->Candidate_numlights_reg = pinst->detail->x86.operands[0].reg;
}

if (!numlight &&
ctx->Candidate_numlight &&
ctx->Candidate_numlight_instCount &&
instCount > ctx->Candidate_numlight_instCount &&
instCount < ctx->Candidate_numlight_instCount + 30 &&
if (!numlights &&
ctx->Candidate_numlights &&
ctx->Candidate_numlights_instCount &&
instCount > ctx->Candidate_numlights_instCount &&
instCount < ctx->Candidate_numlights_instCount + 30 &&
pinst->id == X86_INS_CMP &&
pinst->detail->x86.op_count == 2 &&
pinst->detail->x86.operands[1].type == X86_OP_REG &&
pinst->detail->x86.operands[0].type == X86_OP_REG &&
((pinst->detail->x86.operands[0].reg == ctx->Candidate_numlight_reg &&
((pinst->detail->x86.operands[0].reg == ctx->Candidate_numlights_reg &&
pinst->detail->x86.operands[1].reg == ctx->ZerodReg) ||
(pinst->detail->x86.operands[1].reg == ctx->Candidate_numlight_reg &&
(pinst->detail->x86.operands[1].reg == ctx->Candidate_numlights_reg &&
pinst->detail->x86.operands[0].reg == ctx->ZerodReg)) )
{
numlight = (decltype(numlight))ctx->Candidate_numlight;
numlights = (decltype(numlights))ctx->Candidate_numlights;
}

if (!numlight &&
ctx->Candidate_numlight &&
ctx->Candidate_numlight_instCount &&
instCount > ctx->Candidate_numlight_instCount &&
instCount < ctx->Candidate_numlight_instCount + 30 &&
if (!numlights &&
ctx->Candidate_numlights &&
ctx->Candidate_numlights_instCount &&
instCount > ctx->Candidate_numlights_instCount &&
instCount < ctx->Candidate_numlights_instCount + 30 &&
pinst->id == X86_INS_TEST &&
pinst->detail->x86.op_count == 2 &&
pinst->detail->x86.operands[1].type == X86_OP_REG &&
pinst->detail->x86.operands[0].type == X86_OP_REG &&
pinst->detail->x86.operands[0].reg == ctx->Candidate_numlight_reg)
pinst->detail->x86.operands[0].reg == ctx->Candidate_numlights_reg)
{
numlight = (decltype(numlight))ctx->Candidate_numlight;
numlights = (decltype(numlights))ctx->Candidate_numlights;
}

if (locallight && numlight)
if (locallight && numlights)
return TRUE;

if (address[0] == 0xCC)
Expand All @@ -8066,7 +8066,7 @@ void sub_1D1A030()
}, 0, &ctx);

Sig_VarNotFound(locallight);
Sig_VarNotFound(numlight);
Sig_VarNotFound(numlights);
}
else
{
Expand All @@ -8086,9 +8086,9 @@ void sub_1D1A030()
int ZerodReg_InstCount;
int Candidate_InstCount;
PVOID Candidate_locallight;
int Candidate_numlight_instCount;
int Candidate_numlight_reg;
PVOID Candidate_numlight;
int Candidate_numlights_instCount;
int Candidate_numlights_reg;
PVOID Candidate_numlights;
}R_LightStrength_Context;

R_LightStrength_Context ctx = { 0 };
Expand All @@ -8107,7 +8107,7 @@ void sub_1D1A030()
auto pinst = (cs_insn*)inst;
auto ctx = (R_LightStrength_Context*)context;

if (locallight && numlight)
if (locallight && numlights)
return TRUE;

if (ctx->code.size() > ctx->max_insts)
Expand Down Expand Up @@ -8188,7 +8188,7 @@ void sub_1D1A030()
locallight = (decltype(locallight))ctx->Candidate_locallight;
}

if (!numlight &&
if (!numlights &&
pinst->id == X86_INS_MOV &&
pinst->detail->x86.op_count == 2 &&
pinst->detail->x86.operands[1].type == X86_OP_MEM &&
Expand All @@ -8198,26 +8198,26 @@ void sub_1D1A030()
(PUCHAR)pinst->detail->x86.operands[1].mem.disp < (PUCHAR)g_dwEngineDataBase + g_dwEngineDataSize &&
pinst->detail->x86.operands[0].type == X86_OP_REG)
{
ctx->Candidate_numlight = (decltype(ctx->Candidate_numlight))pinst->detail->x86.operands[1].mem.disp;
ctx->Candidate_numlight_instCount = instCount;
ctx->Candidate_numlight_reg = pinst->detail->x86.operands[0].reg;
ctx->Candidate_numlights = (decltype(ctx->Candidate_numlights))pinst->detail->x86.operands[1].mem.disp;
ctx->Candidate_numlights_instCount = instCount;
ctx->Candidate_numlights_reg = pinst->detail->x86.operands[0].reg;
}

if (!numlight &&
ctx->Candidate_numlight &&
ctx->Candidate_numlight_instCount &&
instCount > ctx->Candidate_numlight_instCount &&
instCount < ctx->Candidate_numlight_instCount + 30 &&
if (!numlights &&
ctx->Candidate_numlights &&
ctx->Candidate_numlights_instCount &&
instCount > ctx->Candidate_numlights_instCount &&
instCount < ctx->Candidate_numlights_instCount + 30 &&
pinst->id == X86_INS_CMP &&
pinst->detail->x86.op_count == 2 &&
pinst->detail->x86.operands[1].type == X86_OP_REG &&
pinst->detail->x86.operands[0].type == X86_OP_REG &&
((pinst->detail->x86.operands[0].reg == ctx->Candidate_numlight_reg &&
((pinst->detail->x86.operands[0].reg == ctx->Candidate_numlights_reg &&
pinst->detail->x86.operands[1].reg == ctx->ZerodReg) ||
(pinst->detail->x86.operands[1].reg == ctx->Candidate_numlight_reg &&
(pinst->detail->x86.operands[1].reg == ctx->Candidate_numlights_reg &&
pinst->detail->x86.operands[0].reg == ctx->ZerodReg)))
{
numlight = (decltype(numlight))ctx->Candidate_numlight;
numlights = (decltype(numlights))ctx->Candidate_numlights;
}

if ((pinst->id == X86_INS_JMP || (pinst->id >= X86_INS_JAE && pinst->id <= X86_INS_JS)) &&
Expand Down Expand Up @@ -8248,7 +8248,7 @@ void sub_1D1A030()
}

Sig_VarNotFound(locallight);
Sig_VarNotFound(numlight);
Sig_VarNotFound(numlights);
}

g_pMetaHookAPI->DisasmRanges(gEngfuncs.pfnSetFilterMode, 0x50, [](void *inst, PUCHAR address, size_t instLen, int instCount, int depth, PVOID context) {
Expand Down
8 changes: 4 additions & 4 deletions Plugins/Renderer/gl_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ void Mod_LoadStudioModel(model_t* mod, void* buffer)

if (studiohdr)
{
auto VBOData = R_AllocateStudioVBO(mod, studiohdr);
auto pRenderData = R_CreateStudioRenderData(mod, studiohdr);

if (VBOData)
if (pRenderData)
{
R_StudioLoadExternalFile(mod, studiohdr, VBOData);
R_StudioLoadTextureModel(mod, studiohdr, VBOData);
R_StudioLoadExternalFile(mod, studiohdr, pRenderData);
R_StudioLoadTextureModel(mod, studiohdr, pRenderData);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Plugins/Renderer/gl_rmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ bool SCR_IsLoadingVisible()

void R_RenderPreFrame()
{
R_EntityComponents_PreFrame();
R_EntityComponents_StartFrame();
}

void R_RenderStartView()
Expand Down
Loading

0 comments on commit 1e39076

Please sign in to comment.