Skip to content

Commit

Permalink
library: set gl blend function when drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
Adamcake committed Dec 13, 2024
1 parent 729351f commit 24844ed
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/library/dll/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static void hook_glDeleteTextures(GLsizei, const GLuint*);
static void hook_glClear(GLbitfield);
static void hook_glViewport(GLint, GLint, GLsizei, GLsizei);
static void hook_glTexParameteri(GLenum, GLenum, GLint);
static void hook_glBlendFunc(GLenum, GLenum);

static HGLRC __stdcall hook_wglCreateContextAttribsARB(HDC, HGLRC, const int*);

Expand Down Expand Up @@ -99,6 +100,7 @@ DWORD __stdcall BOLT_STUB_ENTRYNAME(struct PluginInjectParams* data) {
// get the functions we need from libgl
HMODULE libgl_module = data->pGetModuleHandleW(L"opengl32.dll");
libgl.BindTexture = (void(*)(GLenum, GLuint))data->pGetProcAddress(libgl_module, "glBindTexture");
libgl.BlendFunc = (void(*)(GLenum, GLenum))data->pGetProcAddress(libgl_module, "glBlendFunc");
libgl.Clear = (void(*)(GLbitfield))data->pGetProcAddress(libgl_module, "glClear");
libgl.ClearColor = (void(*)(GLfloat, GLfloat, GLfloat, GLfloat))data->pGetProcAddress(libgl_module, "glClearColor");
libgl.DeleteTextures = (void(*)(GLsizei, const GLuint*))data->pGetProcAddress(libgl_module, "glDeleteTextures");
Expand Down Expand Up @@ -145,6 +147,7 @@ DWORD __stdcall BOLT_STUB_ENTRYNAME(struct PluginInjectParams* data) {
FNHOOK(glTexParameteri)
FNHOOK(glClear)
FNHOOK(glViewport)
FNHOOK(glBlendFunc)
}
if (bolt_cmp(module_name, "gdi32.dll", 0)) {
FNHOOKA(SwapBuffers, SWAPBUFFERS)
Expand Down Expand Up @@ -417,6 +420,13 @@ static void hook_glViewport(GLint x, GLint y, GLsizei width, GLsizei height) {
LOG("glViewport end\n");
}

static hook_glBlendFunc(GLenum sfactor, GLenum dfactor) {
LOG("glBlendFunc\n");
libgl.BlendFunc(sfactor, dfactor);
_bolt_gl_onBlendFunc(sfactor, dfactor);
LOG("glBlendFunc end\n");
}

static HGLRC __stdcall hook_wglCreateContextAttribsARB(HDC hdc, HGLRC hglrc, const int* attribList) {
LOGF("wglCreateContextAttribsARB(%llu)\n", (ULONGLONG)hglrc);
EnterCriticalSection(&wgl_lock);
Expand Down
31 changes: 31 additions & 0 deletions src/library/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ struct GLContext {
GLint viewport_y;
GLsizei viewport_w;
GLsizei viewport_h;
GLenum blend_rgb_s;
GLenum blend_rgb_d;
GLenum blend_alpha_s;
GLenum blend_alpha_d;
};

static unsigned int gl_width;
Expand Down Expand Up @@ -648,6 +652,10 @@ static void _bolt_glcontext_init(struct GLContext* context, void* egl_context, v
context->does_blit_3d_target = false;
context->recalculate_sSceneHDRTex = false;
context->depth_of_field_enabled = false;
context->blend_rgb_s = GL_ONE;
context->blend_rgb_d = GL_ZERO;
context->blend_alpha_s = GL_ONE;
context->blend_alpha_d = GL_ZERO;
if (shared) {
context->programs = shared->programs;
context->buffers = shared->buffers;
Expand Down Expand Up @@ -827,6 +835,7 @@ static void _bolt_gl_load(void* (*GetProcAddress)(const char*)) {
INIT_GL_FUNC(BindBuffer)
INIT_GL_FUNC(BindFramebuffer)
INIT_GL_FUNC(BindVertexArray)
INIT_GL_FUNC(BlendFuncSeparate)
INIT_GL_FUNC(BlitFramebuffer)
INIT_GL_FUNC(BufferData)
INIT_GL_FUNC(BufferStorage)
Expand Down Expand Up @@ -1594,6 +1603,15 @@ static void _bolt_glBlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint
LOG("glBlitFramebuffer end\n");
}

static void _bolt_glBlendFuncSeparate(GLenum srcRGB, GLenum dstRGB, GLenum srcAlpha, GLenum dstAlpha) {
gl.BlendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha);
struct GLContext* c = _bolt_context();
c->blend_rgb_s = srcRGB;
c->blend_rgb_d = dstRGB;
c->blend_alpha_s = srcAlpha;
c->blend_alpha_d = dstAlpha;
}

void* _bolt_gl_GetProcAddress(const char* name) {
#define PROC_ADDRESS_MAP(FUNC) if (!strcmp(name, "gl"#FUNC)) { return gl.FUNC ? _bolt_gl##FUNC : NULL; }
PROC_ADDRESS_MAP(CreateProgram)
Expand Down Expand Up @@ -1622,6 +1640,7 @@ void* _bolt_gl_GetProcAddress(const char* name) {
PROC_ADDRESS_MAP(DeleteVertexArrays)
PROC_ADDRESS_MAP(BindVertexArray)
PROC_ADDRESS_MAP(BlitFramebuffer)
PROC_ADDRESS_MAP(BlendFuncSeparate)
#undef PROC_ADDRESS_MAP
return NULL;
}
Expand Down Expand Up @@ -2222,6 +2241,14 @@ void _bolt_gl_onTexParameteri(GLenum target, GLenum pname, GLint param) {
}
}

void _bolt_gl_onBlendFunc(GLenum sfactor, GLenum dfactor) {
struct GLContext* c = _bolt_context();
c->blend_rgb_s = sfactor;
c->blend_rgb_d = dfactor;
c->blend_alpha_s = sfactor;
c->blend_alpha_d = dfactor;
}

static void _bolt_gl_plugin_drawelements_vertex2d_xy(size_t index, void* userdata, int32_t* out) {
struct GLPluginDrawElementsVertex2DUserData* data = userdata;
if (_bolt_get_attr_binding_int(data->c, data->position, data->indices[index], 2, out)) {
Expand Down Expand Up @@ -2531,8 +2558,10 @@ static void _bolt_gl_plugin_surface_drawtoscreen(void* _userdata, int sx, int sy
lgl->Disable(GL_DEPTH_TEST);
lgl->Disable(GL_SCISSOR_TEST);
lgl->Disable(GL_CULL_FACE);
lgl->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
lgl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);

gl.BlendFuncSeparate(c->blend_rgb_s, c->blend_rgb_d, c->blend_alpha_s, c->blend_alpha_d);
if (depth_test) lgl->Enable(GL_DEPTH_TEST);
if (scissor_test) lgl->Enable(GL_SCISSOR_TEST);
if (cull_face) lgl->Enable(GL_CULL_FACE);
Expand Down Expand Up @@ -2566,8 +2595,10 @@ static void _bolt_gl_plugin_surface_drawtosurface(void* _userdata, void* _target
lgl->Disable(GL_DEPTH_TEST);
lgl->Disable(GL_SCISSOR_TEST);
lgl->Disable(GL_CULL_FACE);
lgl->BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
lgl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);

gl.BlendFuncSeparate(c->blend_rgb_s, c->blend_rgb_d, c->blend_alpha_s, c->blend_alpha_d);
if (depth_test) lgl->Enable(GL_DEPTH_TEST);
if (scissor_test) lgl->Enable(GL_SCISSOR_TEST);
if (cull_face) lgl->Enable(GL_CULL_FACE);
Expand Down
9 changes: 9 additions & 0 deletions src/library/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ typedef uintptr_t GLsizeiptr;

/* consts used from libgl */
#define GL_NONE 0
#define GL_ZERO 0
#define GL_ONE 1
#define GL_SRC_ALPHA 770
#define GL_ONE_MINUS_SRC_ALPHA 771
#define GL_TEXTURE 5890
#define GL_TEXTURE_2D 3553
#define GL_TEXTURE_2D_MULTISAMPLE 37120
Expand Down Expand Up @@ -130,6 +134,7 @@ struct GLProcFunctions {
void (*BindBuffer)(GLenum, GLuint);
void (*BindFramebuffer)(GLenum, GLuint);
void (*BindVertexArray)(GLuint);
void (*BlendFuncSeparate)(GLenum, GLenum, GLenum, GLenum);
void (*BlitFramebuffer)(GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLbitfield, GLenum);
void (*BufferData)(GLenum, GLsizeiptr, const void*, GLenum);
void (*BufferStorage)(GLenum, GLsizeiptr, const void*, GLbitfield);
Expand Down Expand Up @@ -200,6 +205,7 @@ struct GLProcFunctions {
/// Struct representing all the OpenGL functions of interest to us that the game gets from static or dynamic linkage
struct GLLibFunctions {
void (*BindTexture)(GLenum, GLuint);
void (*BlendFunc)(GLenum, GLenum);
void (*Clear)(GLbitfield);
void (*ClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
void (*DeleteTextures)(GLsizei, const GLuint*);
Expand Down Expand Up @@ -274,4 +280,7 @@ void _bolt_gl_onViewport(GLint, GLint, GLsizei, GLsizei);
/// Call this in response to glTexParameteri, which needs to be hooked from libgl.
void _bolt_gl_onTexParameteri(GLenum, GLenum, GLint);

/// Call this in response to glBlendFunc, which needs to be hooked from libgl.
void _bolt_gl_onBlendFunc(GLenum, GLenum);

#endif
10 changes: 10 additions & 0 deletions src/library/so/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ static void _bolt_init_libgl(unsigned long addr, const Elf32_Word* gnu_hash_tabl
libgl_addr = (void*)addr;
const ElfW(Sym)* sym = _bolt_lookup_symbol("glBindTexture", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.BindTexture = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glBlendFunc", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.BlendFunc = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glClear", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.Clear = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glClearColor", gnu_hash_table, hash_table, string_table, symbol_table);
Expand Down Expand Up @@ -386,6 +388,13 @@ void glTexParameteri(GLenum target, GLenum pname, GLint param) {
LOG("glTexParameteri end\n");
}

void glBlendFunc(GLenum sfactor, GLenum dfactor) {
LOG("glBlendFunc\n");
libgl.BlendFunc(sfactor, dfactor);
_bolt_gl_onBlendFunc(sfactor, dfactor);
LOG("glBlendFunc end\n");
}

void* eglGetProcAddress(const char* name) {
LOGF("eglGetProcAddress('%s')\n", name);
void* ret = _bolt_gl_GetProcAddress(name);
Expand Down Expand Up @@ -766,6 +775,7 @@ static void* _bolt_dl_lookup(void* handle, const char* symbol) {
if (strcmp(symbol, "glClear") == 0) return glClear;
if (strcmp(symbol, "glViewport") == 0) return glViewport;
if (strcmp(symbol, "glTexParameteri") == 0) return glTexParameteri;
if (strcmp(symbol, "glBlendFunc") == 0) return glBlendFunc;
} else if (handle == libxcb_addr) {
if (strcmp(symbol, "xcb_poll_for_event") == 0) return xcb_poll_for_event;
if (strcmp(symbol, "xcb_poll_for_queued_event") == 0) return xcb_poll_for_queued_event;
Expand Down

0 comments on commit 24844ed

Please sign in to comment.