Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
hzqst committed Dec 26, 2023
1 parent f7b54b1 commit 379972d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 71 deletions.
28 changes: 0 additions & 28 deletions Plugins/Renderer/gl_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,34 +1380,6 @@ void Draw_MiptexTexture(cachewad_t *wad, byte *data)
}
}

DWORD ByteToUInt( byte *byte )
{
DWORD iValue = byte[0];
iValue += (byte[1]<<8);
iValue += (byte[2]<<16);
iValue += (byte[3]<<24);

return iValue;
}

qboolean PowerOfTwo(int iWidth,int iHeight)
{
int iWidthT = iWidth;
while(iWidthT != 1)
{
if((iWidthT % 2) != 0) return false;
iWidthT /=2;
}

int iHeightT = iHeight;
while(iHeightT != 1)
{
if((iHeightT % 2) != 0) return false;
iHeightT /=2;
}
return true;
}

bool LoadDDS(const char* filename, byte* buf, size_t bufsize, gl_loadtexture_state_t *state, qboolean throw_warning_on_missing)
{
DDS_FILEHEADER10 fileHeader10;
Expand Down
83 changes: 79 additions & 4 deletions Plugins/Renderer/gl_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <map>
#include "gl_local.h"

#define R_LOADSKYNAME_SIG_SVENGINE "\x83\x3D\x2A\x2A\x2A\x2A\x00\x0F\x84\x2A\x2A\x2A\x2A\x2A\x2A\x8B\x3D"
//#define R_LOADSKYNAME_SIG_SVENGINE "\x83\x3D\x2A\x2A\x2A\x2A\x00\x0F\x84\x2A\x2A\x2A\x2A\x2A\x2A\x8B\x3D"

#define MOD_POINTINLEAF_SIG_SVENGINE "\x2A\x8B\x2A\x24\x2A\x85\x2A\x2A\x2A\x8B\x2A\xA4\x00\x00\x00"
#define MOD_POINTINLEAF_SIG_HL25 "\x55\x8B\xEC\x56\x8B\x75\x0C\x85\x2A\x2A\x2A\x8B\x8E\xA4\x00\x00\x00\x85\xC9\x2A\x2A\x68"
Expand Down Expand Up @@ -82,12 +82,11 @@
#define R_CULLBOX_SIG_HL25 "\x55\x8B\xEC\x2A\x8B\x2A\x08\x2A\x2A\x8B\x2A\x0C\xBE\x2A\x2A\x2A\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x83\xC4\x0C\x83\xF8\x02"
#define R_CULLBOX_SIG_SVENGINE "\x2A\x8B\x2A\x24\x08\x2A\x2A\x8B\x2A\x24\x14\x2A\x2A\x2A\x2A\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\x83\xC4\x0C\x83\xF8\x02"

//inlined in HL25 and SvEngine
#define R_SETUPFRAME_SIG_BLOB "\xA1\x2A\x2A\x2A\x2A\x83\xEC\x18\x83\xF8\x01\x0F\x8E\x2A\x2A\x2A\x2A\xD9\x05\x2A\x2A\x2A\x2A\xD8\x1D\x2A\x2A\x2A\x2A\xDF\xE0\xF6\xC4\x2A\x2A\x2A\x68"
#define R_SETUPFRAME_SIG_BLOB2 "\x8B\x0D\x2A\x2A\x2A\x2A\x83\xEC\x18\x33\xC0\x83\xF9\x01\x0F\x9F\xC0\x50\xE8\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xA1"
#define R_SETUPFRAME_SIG_NEW "\x55\x8B\xEC\x83\xEC\x18\x8B\x0D\x2A\x2A\x2A\x2A\x33\xC0\x83\xF9\x01\x0F\x9F\xC0\x50\xE8\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xA1"
#define R_SETUPFRAME_SIG_HL25 ""
#define R_SETUPFRAME_SIG_SVENGINE ""
#define R_SETUPFRAME_SIG_HL25 "" //inlined
#define R_SETUPFRAME_SIG_SVENGINE "" //inlined

#define GL_BIND_SIG_BLOB "\x8B\x44\x24\x04\x8B\x0D\x2A\x2A\x2A\x2A\x2A\x8B\x2A\x2A\x2A\x10\x25\xFF\xFF\x00\x00"
#define GL_BIND_SIG_NEW2 GL_BIND_SIG_BLOB
Expand Down Expand Up @@ -418,6 +417,79 @@ void R_FillAddress(void)
}
}

if (1)
{
const char pattern[] = "\x68\x00\x1F\x00\x00\xFF";
PUCHAR SearchBegin = (PUCHAR)g_dwEngineTextBase;
PUCHAR SearchLimit = (PUCHAR)g_dwEngineTextBase + g_dwEngineTextSize;
while (SearchBegin < SearchLimit)
{
PUCHAR pFound = (PUCHAR)Search_Pattern_From_Size(SearchBegin, SearchLimit - SearchBegin, pattern);
if (pFound)
{
auto pCandidateFunction = g_pMetaHookAPI->ReverseSearchFunctionBegin(pFound, 0x80);
if (pCandidateFunction)
{
typedef struct
{
bool bFoundPushString;
}GL_InitSearchContext;

GL_InitSearchContext ctx = { 0 };

g_pMetaHookAPI->DisasmRanges(pCandidateFunction, 0x120, [](void* inst, PUCHAR address, size_t instLen, int instCount, int depth, PVOID context) {

auto pinst = (cs_insn*)inst;
auto ctx = (GL_InitSearchContext*)context;

if (pinst->id == X86_INS_PUSH &&
pinst->detail->x86.op_count == 1 &&
pinst->detail->x86.operands[0].type == X86_OP_IMM &&
(
((PUCHAR)pinst->detail->x86.operands[0].imm > (PUCHAR)g_dwEngineDataBase &&
(PUCHAR)pinst->detail->x86.operands[0].imm < (PUCHAR)g_dwEngineDataBase + g_dwEngineDataSize) ||
((PUCHAR)pinst->detail->x86.operands[0].imm > (PUCHAR)g_dwEngineRdataBase &&
(PUCHAR)pinst->detail->x86.operands[0].imm < (PUCHAR)g_dwEngineRdataBase + g_dwEngineRdataSize)
) )
{
auto pString = (PCHAR)pinst->detail->x86.operands[0].imm;
if (!memcmp(pString, "Failed to query GL vendor", sizeof("Failed to query GL vendor") - 1))
{
ctx->bFoundPushString = true;
}
}

if (ctx->bFoundPushString)
return TRUE;

if (address[0] == 0xCC)
return TRUE;

if (pinst->id == X86_INS_RET)
return TRUE;

return FALSE;

}, 0, &ctx);

if (ctx.bFoundPushString)
{
gPrivateFuncs.GL_Init = (decltype(gPrivateFuncs.GL_Init))pCandidateFunction;

break;
}
}

SearchBegin = pFound + Sig_Length(pattern);
}
else
{
break;
}
}
}
Sig_FuncNotFound(GL_Init);

#if 0//unused
if (g_iEngineType == ENGINE_SVENGINE)
{
Expand Down Expand Up @@ -7217,6 +7289,7 @@ void R_FillAddress(void)
}
}

hook_t* g_phook_GL_Init = NULL;
hook_t *g_phook_GL_BeginRendering = NULL;
hook_t *g_phook_GL_EndRendering = NULL;
hook_t *g_phook_R_RenderView_SvEngine = NULL;
Expand Down Expand Up @@ -7246,6 +7319,7 @@ hook_t *g_phook_DLL_SetModKey = NULL;
void R_UninstallHooksForEngineDLL(void)
{
//Engine
Uninstall_Hook(GL_Init);
Uninstall_Hook(GL_BeginRendering);
Uninstall_Hook(GL_EndRendering);

Expand Down Expand Up @@ -7284,6 +7358,7 @@ void R_UninstallHooksForEngineDLL(void)

void R_InstallHooks(void)
{
Install_InlineHook(GL_Init);
Install_InlineHook(GL_BeginRendering);
Install_InlineHook(GL_EndRendering);

Expand Down
2 changes: 0 additions & 2 deletions Plugins/Renderer/gl_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ extern bool g_bPortalClipPlaneEnabled[6];

extern vec4_t g_PortalClipPlane[6];

extern bool g_bIsGLInit;

//gl extension

extern int gl_max_texture_size;
Expand Down
57 changes: 25 additions & 32 deletions Plugins/Renderer/gl_rmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ bool g_bPortalClipPlaneEnabled[6] = { false };

vec4_t g_PortalClipPlane[6] = {0};

bool g_bIsGLInit = false;

float r_entity_matrix[4][4];
float r_entity_color[4];

Expand Down Expand Up @@ -1344,8 +1342,9 @@ void GL_GenerateFrameBuffers(void)
GL_FreeFBO(&s_DepthLinearFBO);
GL_FreeFBO(&s_HBAOCalcFBO);
GL_FreeFBO(&s_ShadowFBO);

glEnable(GL_TEXTURE_2D);

//Probably useless
//glEnable(GL_TEXTURE_2D);

s_FinalBufferFBO.iWidth = glwidth;
s_FinalBufferFBO.iHeight = glheight;
Expand Down Expand Up @@ -1560,6 +1559,11 @@ void GLAPIENTRY GL_DebugOutputCallback(GLenum source, GLenum type, GLuint id, GL

void GL_Init(void)
{
gPrivateFuncs.GL_Init();

//Just like what GL_SetMode does
g_pMetaHookAPI->GetVideoMode(&glwidth, &glheight, NULL, NULL);

auto err = glewInit();

if (GLEW_OK != err)
Expand Down Expand Up @@ -1630,8 +1634,6 @@ void GL_Init(void)

GL_GenerateFrameBuffers();
GL_InitShaders();

g_bIsGLInit = true;
}

void GL_Shutdown(void)
Expand Down Expand Up @@ -1704,37 +1706,28 @@ void GL_BeginRendering(int *x, int *y, int *width, int *height)
{
gPrivateFuncs.GL_BeginRendering(x, y, width, height);

if (g_bIsGLInit)
//Window resized?
if ((*width) != glwidth || (*height) != glheight)
{
//Window resized?
if ((*x) != glx || (*y) != gly || (*width) != glwidth || (*height) != glheight)
{
glx = *x;
gly = *y;
glwidth = *width;
glheight = *height;
GL_GenerateFrameBuffers();
}
else
{
glx = *x;
gly = *y;
glwidth = *width;
glheight = *height;
}
GL_GenerateFrameBuffers();
}

//No V_RenderView calls when level changes so don't clear final buffer
if (SCR_IsLoadingVisible())
{
glx = (*x);
gly = (*y);
glwidth = (*width);
glheight = (*height);

}
else
{
GL_FlushFinalBuffer();
}
//No V_RenderView calls when level changes so don't clear final buffer
if (SCR_IsLoadingVisible())
{

R_RenderStartFrame();
}
else
{
GL_FlushFinalBuffer();
}

R_RenderStartFrame();

r_renderview_pass = 0;
*c_alias_polys = 0;
Expand Down
6 changes: 3 additions & 3 deletions Plugins/Renderer/gl_rmisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,13 @@ void GL_ClearFBO(FBO_Container_t* s)
void GL_FreeFBO(FBO_Container_t* s)
{
if (s->s_hBackBufferFBO)
glDeleteFramebuffersEXT(1, &s->s_hBackBufferFBO);
glDeleteFramebuffers(1, &s->s_hBackBufferFBO);

if (s->s_hBackBufferCB)
glDeleteRenderbuffersEXT(1, &s->s_hBackBufferCB);
glDeleteRenderbuffers(1, &s->s_hBackBufferCB);

if (s->s_hBackBufferDB)
glDeleteRenderbuffersEXT(1, &s->s_hBackBufferDB);
glDeleteRenderbuffers(1, &s->s_hBackBufferDB);

if (s->s_hBackBufferTex)
glDeleteTextures(1, &s->s_hBackBufferTex);
Expand Down
2 changes: 0 additions & 2 deletions Plugins/Renderer/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ void IPluginsV4::LoadClient(cl_exportfuncs_t *pExportFunc)

memcpy(&gExportfuncs, pExportFunc, sizeof(gExportfuncs));

GL_Init();

pExportFunc->HUD_GetStudioModelInterface = HUD_GetStudioModelInterface;
pExportFunc->HUD_Redraw = HUD_Redraw;
pExportFunc->HUD_Init = HUD_Init;
Expand Down
2 changes: 2 additions & 0 deletions Plugins/Renderer/privatehook.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct
void (*GL_SelectTexture)(GLenum target);
void (*GL_DisableMultitexture)(void);
void (*GL_EnableMultitexture)(void);
void (*GL_Init)(void);
void (*GL_BeginRendering)(int *x, int *y, int *width, int *height);
void (*GL_EndRendering)(void);
void (*EmitWaterPolys)(msurface_t *fa, int direction);
Expand Down Expand Up @@ -136,6 +137,7 @@ typedef struct

}private_funcs_t;

extern hook_t* g_phook_GL_Init;
extern hook_t *g_phook_GL_BeginRendering;
extern hook_t *g_phook_GL_EndRendering;
extern hook_t *g_phook_R_RenderView_SvEngine;
Expand Down

0 comments on commit 379972d

Please sign in to comment.