Skip to content

Commit

Permalink
library: more robust surface-drawing code
Browse files Browse the repository at this point in the history
Specifically, it seems like leftover scissor regions and depth test
settings from the game's previous render were causing issues with bolt's
own renders, even to internal surfaces.
  • Loading branch information
Adamcake committed Dec 1, 2024
1 parent 7477d4f commit 63a819b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/library/dll/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,13 @@ DWORD __stdcall BOLT_STUB_ENTRYNAME(struct PluginInjectParams* data) {
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");
libgl.Disable = (void(*)(GLenum))data->pGetProcAddress(libgl_module, "glDisable");
libgl.DrawArrays = (void(*)(GLenum, GLint, GLsizei))data->pGetProcAddress(libgl_module, "glDrawArrays");
libgl.DrawElements = (void(*)(GLenum, GLsizei, GLenum, const void*))data->pGetProcAddress(libgl_module, "glDrawElements");
libgl.Enable = (void(*)(GLenum))data->pGetProcAddress(libgl_module, "glEnable");
libgl.Flush = (void(*)(void))data->pGetProcAddress(libgl_module, "glFlush");
libgl.GenTextures = (void(*)(GLsizei, GLuint*))data->pGetProcAddress(libgl_module, "glGenTextures");
libgl.GetBooleanv = (void(*)(GLenum, GLboolean*))data->pGetProcAddress(libgl_module, "glGetBooleanv");
libgl.GetError = (GLenum(*)(void))data->pGetProcAddress(libgl_module, "glGetError");
libgl.ReadPixels = (void(*)(GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, void*))data->pGetProcAddress(libgl_module, "glReadPixels");
libgl.TexParameteri = (void(*)(GLenum, GLenum, GLint))data->pGetProcAddress(libgl_module, "glTexParameteri");
Expand Down
31 changes: 31 additions & 0 deletions src/library/gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,10 @@ static void _bolt_gl_plugin_surface_subimage(void* _userdata, int x, int y, int
static void _bolt_gl_plugin_surface_drawtoscreen(void* _userdata, int sx, int sy, int sw, int sh, int dx, int dy, int dw, int dh) {
struct PluginSurfaceUserdata* userdata = _userdata;
struct GLContext* c = _bolt_context();
GLboolean depth_test, scissor_test, cull_face;
lgl->GetBooleanv(GL_DEPTH_TEST, &depth_test);
lgl->GetBooleanv(GL_SCISSOR_TEST, &scissor_test);
lgl->GetBooleanv(GL_CULL_FACE, &cull_face);

gl.UseProgram(program_direct_screen);
lgl->BindTexture(GL_TEXTURE_2D, userdata->renderbuffer);
Expand All @@ -2130,8 +2134,14 @@ static void _bolt_gl_plugin_surface_drawtoscreen(void* _userdata, int sx, int sy
gl.Uniform4i(program_direct_screen_s_xywh, sx, sy, sw, sh);
gl.Uniform4i(program_direct_screen_src_wh_dest_wh, userdata->width, userdata->height, gl_width, gl_height);
lgl->Viewport(0, 0, gl_width, gl_height);
lgl->Disable(GL_DEPTH_TEST);
lgl->Disable(GL_SCISSOR_TEST);
lgl->Disable(GL_CULL_FACE);
lgl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);

if (depth_test) lgl->Enable(GL_DEPTH_TEST);
if (scissor_test) lgl->Enable(GL_SCISSOR_TEST);
if (cull_face) lgl->Enable(GL_CULL_FACE);
lgl->Viewport(c->viewport_x, c->viewport_y, c->viewport_w, c->viewport_h);
const struct GLTexture2D* original_tex = c->texture_units[c->active_texture];
lgl->BindTexture(GL_TEXTURE_2D, original_tex ? original_tex->id : 0);
Expand All @@ -2144,6 +2154,10 @@ static void _bolt_gl_plugin_surface_drawtosurface(void* _userdata, void* _target
struct PluginSurfaceUserdata* userdata = _userdata;
struct PluginSurfaceUserdata* target = _target;
struct GLContext* c = _bolt_context();
GLboolean depth_test, scissor_test, cull_face;
lgl->GetBooleanv(GL_DEPTH_TEST, &depth_test);
lgl->GetBooleanv(GL_SCISSOR_TEST, &scissor_test);
lgl->GetBooleanv(GL_CULL_FACE, &cull_face);

gl.UseProgram(program_direct_surface);
lgl->BindTexture(GL_TEXTURE_2D, userdata->renderbuffer);
Expand All @@ -2154,8 +2168,14 @@ static void _bolt_gl_plugin_surface_drawtosurface(void* _userdata, void* _target
gl.Uniform4i(program_direct_surface_s_xywh, sx, sy, sw, sh);
gl.Uniform4i(program_direct_surface_src_wh_dest_wh, userdata->width, userdata->height, target->width, target->height);
lgl->Viewport(0, 0, target->width, target->height);
lgl->Disable(GL_DEPTH_TEST);
lgl->Disable(GL_SCISSOR_TEST);
lgl->Disable(GL_CULL_FACE);
lgl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);

if (depth_test) lgl->Enable(GL_DEPTH_TEST);
if (scissor_test) lgl->Enable(GL_SCISSOR_TEST);
if (cull_face) lgl->Enable(GL_CULL_FACE);
lgl->Viewport(c->viewport_x, c->viewport_y, c->viewport_w, c->viewport_h);
const struct GLTexture2D* original_tex = c->texture_units[c->active_texture];
lgl->BindTexture(GL_TEXTURE_2D, original_tex ? original_tex->id : 0);
Expand All @@ -2167,14 +2187,25 @@ static void _bolt_gl_plugin_surface_drawtosurface(void* _userdata, void* _target
static void _bolt_gl_plugin_draw_region_outline(void* userdata, int16_t x, int16_t y, uint16_t width, uint16_t height) {
struct PluginSurfaceUserdata* target = userdata;
struct GLContext* c = _bolt_context();
GLboolean depth_test, scissor_test, cull_face;
lgl->GetBooleanv(GL_DEPTH_TEST, &depth_test);
lgl->GetBooleanv(GL_SCISSOR_TEST, &scissor_test);
lgl->GetBooleanv(GL_CULL_FACE, &cull_face);

gl.UseProgram(program_region);
gl.BindVertexArray(program_direct_vao);
gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, target->framebuffer);
lgl->Viewport(0, 0, gl_width, gl_height);
lgl->Disable(GL_DEPTH_TEST);
lgl->Disable(GL_SCISSOR_TEST);
lgl->Disable(GL_CULL_FACE);
gl.Uniform4i(program_region_xywh, x, y, width, height);
gl.Uniform2i(program_region_dest_wh, gl_width, gl_height);
lgl->DrawArrays(GL_TRIANGLE_STRIP, 0, 4);

if (depth_test) lgl->Enable(GL_DEPTH_TEST);
if (scissor_test) lgl->Enable(GL_SCISSOR_TEST);
if (cull_face) lgl->Enable(GL_CULL_FACE);
lgl->Viewport(c->viewport_x, c->viewport_y, c->viewport_w, c->viewport_h);
gl.BindFramebuffer(GL_DRAW_FRAMEBUFFER, c->current_draw_framebuffer);
gl.BindVertexArray(c->bound_vao->id);
Expand Down
6 changes: 6 additions & 0 deletions src/library/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@ struct GLLibFunctions {
void (*Clear)(GLbitfield);
void (*ClearColor)(GLfloat, GLfloat, GLfloat, GLfloat);
void (*DeleteTextures)(GLsizei, const GLuint*);
void (*Disable)(GLenum);
void (*DrawArrays)(GLenum, GLint, GLsizei);
void (*DrawElements)(GLenum, GLsizei, GLenum, const void*);
void (*Enable)(GLenum);
void (*Flush)(void);
void (*GenTextures)(GLsizei, GLuint*);
void (*GetBooleanv)(GLenum, GLboolean*);
GLenum (*GetError)(void);
void (*ReadPixels)(GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, void*);
void (*TexParameteri)(GLenum, GLenum, GLint);
Expand Down Expand Up @@ -127,6 +130,9 @@ struct GLLibFunctions {
#define GL_TEXTURE_WRAP_S 10242
#define GL_TEXTURE_WRAP_T 10243
#define GL_CLAMP_TO_EDGE 33071
#define GL_DEPTH_TEST 2929
#define GL_SCISSOR_TEST 3089
#define GL_CULL_FACE 2884
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 33776
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 33777
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 33778
Expand Down
6 changes: 6 additions & 0 deletions src/library/so/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,20 @@ static void _bolt_init_libgl(unsigned long addr, const Elf32_Word* gnu_hash_tabl
if (sym) libgl.ClearColor = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glDeleteTextures", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.DeleteTextures = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glDisable", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.Disable = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glDrawArrays", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.DrawArrays = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glDrawElements", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.DrawElements = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glEnable", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.Enable = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glFlush", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.Flush = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glGenTextures", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.GenTextures = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glGetBooleanv", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.GetBooleanv = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glGetError", gnu_hash_table, hash_table, string_table, symbol_table);
if (sym) libgl.GetError = sym->st_value + libgl_addr;
sym = _bolt_lookup_symbol("glReadPixels", gnu_hash_table, hash_table, string_table, symbol_table);
Expand Down

0 comments on commit 63a819b

Please sign in to comment.