Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ltw/src/main/tinywrapper/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ LOCAL_SRC_FILES := \
basevertex.c \
blending.c \
query.c \
depth.c \
shader_wrapper.c \
string_utils.c \
framebuffer.c \
Expand Down
86 changes: 86 additions & 0 deletions ltw/src/main/tinywrapper/depth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// Created by whbex on 29.12.2025.
//

#include "egl.h"
#include "GL/gl.h"
#include "GL/glext.h"
#include "string.h"

const char * v_shader_source = ""
"#version 300 es\n"
"void main() {\n"
"gl_Position = vec4(2.0 * vec2(gl_VertexID % 2, gl_VertexID / 2) - 1.0, 0.0, 1.0);\n"
"}\0";
const char * f_shader_source = "#version 300 es\n"
"void main() {\n"
"}\0";

static GLuint current_program = 0;
static GLuint current_vao = 0;

static GLuint build_shader(GLuint program, GLenum type, const char* source){
GLuint shader = es3_functions.glCreateShader(type);
es3_functions.glShaderSource(shader, 1, &source, NULL);
es3_functions.glCompileShader(shader);
GLint status;
char log[512];
es3_functions.glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (!status){
printf("LTW: Depth clear shader compile failed!\n");
es3_functions.glGetShaderInfoLog(shader, sizeof(log), NULL,log);
printf("LTW: Compile log: %s\n", log);
return shader;
}
es3_functions.glAttachShader(program, shader);
return shader;
}

void depthfix_init_program(){
if (!current_context->clear_depth_emu) return;

current_program = es3_functions.glCreateProgram();
GLuint vertex = build_shader(current_program, GL_VERTEX_SHADER, v_shader_source);
GLuint frag = build_shader(current_program, GL_FRAGMENT_SHADER, f_shader_source);
es3_functions.glLinkProgram(current_program);
es3_functions.glDeleteShader(vertex);
es3_functions.glDeleteShader(frag);

es3_functions.glGenVertexArrays(1, &current_vao);
}

void glClear(GLbitfield mask){
if (!current_context->clear_depth_emu) goto real;
// printf("LTW: CLEAR BEGIN!!");
if (mask != GL_DEPTH_BUFFER_BIT) goto real;

// This is hard
GLint depth_func;
GLboolean depth_mask;
GLboolean color_mask[4];
es3_functions.glGetIntegerv(GL_DEPTH_FUNC, &depth_func);
es3_functions.glGetBooleanv(GL_DEPTH_WRITEMASK, &depth_mask);
es3_functions.glGetBooleanv(GL_COLOR_WRITEMASK, color_mask);
es3_functions.glDepthFunc(GL_ALWAYS);
es3_functions.glEnable(GL_DEPTH_TEST);
es3_functions.glDepthMask(GL_TRUE);
es3_functions.glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

es3_functions.glUseProgram(current_program);
es3_functions.glBindVertexArray(current_vao);
es3_functions.glDrawArrays(GL_QUADS, 0, 4);
es3_functions.glUseProgram(0);
es3_functions.glBindVertexArray(0);
// printf("LTW: CLEAR END!!");


es3_functions.glDisable(GL_DEPTH_TEST);
es3_functions.glDepthFunc(depth_func);
es3_functions.glDepthMask(depth_mask);
// es3_functions.glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
es3_functions.glColorMask(color_mask[0], color_mask[1], color_mask[2], color_mask[3]);

return;
real:
es3_functions.glClear(mask);
}
10 changes: 10 additions & 0 deletions ltw/src/main/tinywrapper/depth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Created by whbex on 29.12.2025.
//

#ifndef POJAVLAUNCHER_DEPTH_H
#define POJAVLAUNCHER_DEPTH_H

void depthfix_init_program();

#endif //POJAVLAUNCHER_DEPTH_H
6 changes: 6 additions & 0 deletions ltw/src/main/tinywrapper/egl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "unordered_map/int_hash.h"
#include "string_utils.h"
#include "env.h"
#include "depth.h"
#include <string.h>

thread_local context_t *current_context;
Expand Down Expand Up @@ -212,6 +213,11 @@ static void init_incontext(context_t* tw_context) {
basevertex_init(tw_context);
buffer_copier_init(tw_context);
es3_functions.glGenBuffers(1, &tw_context->multidraw_element_buffer);
if(env_istrue_d("LTW_EMULATE_DEPTHCLEAR", false)){
printf("LTW: Enabling ANGLE depth clear workaround\n");
tw_context->clear_depth_emu = true;
depthfix_init_program();
}
}

EGLContext eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list) {
Expand Down
2 changes: 1 addition & 1 deletion ltw/src/main/tinywrapper/egl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ typedef struct {
typedef struct {
EGLContext phys_context;
bool context_rdy;
bool es31, es32, buffer_storage, buffer_texture_ext, multidraw_indirect, timer_query;
bool es31, es32, buffer_storage, buffer_texture_ext, multidraw_indirect, timer_query, clear_depth_emu;
GLint shader_version;
basevertex_renderer_t basevertex;
PFNGLDRAWELEMENTSBASEVERTEXPROC drawelementsbasevertex;
Expand Down
3 changes: 2 additions & 1 deletion ltw/src/main/tinywrapper/es3_overrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ GLESOVERRIDE(glGetError)
GLESOVERRIDE(glTexBuffer)
GLESOVERRIDE(glTexBufferRange)
GLESOVERRIDE(glMapBufferRange)
GLESOVERRIDE(glFlushMappedBufferRange)
GLESOVERRIDE(glFlushMappedBufferRange)
GLESOVERRIDE(glClear)