Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate the zbuffer for broader ReShade compatability #70

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
ce3a19e
Attempt to write to depth buffer
Jul 10, 2018
e8ca4fd
Update screen.c
Jul 10, 2018
975ee13
Update screen.h
Jul 10, 2018
b2d3019
Creating depth buffers
Jul 10, 2018
468c235
Add zrgb value
Jul 10, 2018
fed52a7
Update rdp.h
Jul 10, 2018
198f05f
Update gl_screen.c
Jul 10, 2018
5007a7d
Update screen.c
Jul 10, 2018
c9ab467
Just a guess
Jul 10, 2018
0ff599f
Update vi.c
Jul 10, 2018
fe7ce7f
Update vi.c
Jul 10, 2018
fe7bbb5
Update screen.h
Jul 10, 2018
0eaf3d9
Update gl_screen.c
Jul 10, 2018
321d555
Update gl_screen.c
Jul 10, 2018
4c9dc42
Trying to write to gl_FragDepth
Jul 10, 2018
08fd8a9
Update gl_screen.c
Jul 10, 2018
46a34ed
Update gl_screen.c
Jul 10, 2018
c889de5
Update gl_screen.c
Jul 10, 2018
d7d48f0
syntax error fix
Jul 10, 2018
06e0056
Messing around with opengl
Jul 10, 2018
72c8e13
Update gl_screen.c
Jul 10, 2018
384a513
Trying to unpack the depth correctly
Jul 10, 2018
f8902b2
Update gl_screen.c
Jul 10, 2018
1091c29
Update gl_screen.c
Jul 10, 2018
42abb7c
Another guess
Jul 10, 2018
57ca2d6
Merge branch 'master' of https://github.com/AWBuchanan7/angrylion-rdp…
Jul 10, 2018
899d8c2
Checking in because I think I'm getting somewhere...
Jul 11, 2018
73b25bf
getting REALLY close now
Jul 11, 2018
d3f147f
updates, confusion
Jul 11, 2018
47ac548
Going to leave off here for today
Jul 11, 2018
9e8509e
Trying to get a grip on opengl flow
Jul 11, 2018
1624e71
Rendering only the depth and not the image
Jul 11, 2018
febfe65
Looks like I've got it
Jul 11, 2018
8113200
Some tidy up
Jul 11, 2018
b0f9c26
Update README.md
Jun 1, 2019
d2e3fbf
Rename .sln
Jun 2, 2019
19c87a7
Update .gitignore
Jun 10, 2019
fcf5036
merge
Jun 10, 2019
7d41476
move .gitmodules to top level
Jun 10, 2019
ec22e41
Revert "move .gitmodules to top level"
Jun 10, 2019
c1b93f2
Revert "merge"
Jun 10, 2019
eb1d95f
Revert "Update .gitignore"
Jun 10, 2019
bbe0240
Fragile: including CUDA in gl_screen.c
Jun 11, 2019
dd37277
Tiny bit more of CUDA setup
Jun 12, 2019
00b778e
Revert "Tiny bit more of CUDA setup"
Jun 13, 2019
29a56eb
Revert "Fragile: including CUDA in gl_screen.c"
Jun 13, 2019
a7de73c
setup plugin-cuda-common
Jun 13, 2019
b9c8407
Update angrylion-plus-x.sln
Jun 13, 2019
2f94f86
propping up CUDA
Jun 13, 2019
857f3b2
Tidying up dummy project
Jun 13, 2019
48bdb77
organization
Jun 13, 2019
e2138f9
Update README.md
Jun 13, 2019
e341068
Update README.md
Jun 13, 2019
76943d8
Various (and needed) tweaks
Jun 13, 2019
82dba35
Merge branch 'master' of https://github.com/AWBuchanan7/angrylion-rdp-x
Jun 13, 2019
be81956
Revert changes unrelated to depth shaders
AWBuchanan7 Nov 14, 2023
d44a9da
Whitespace corrections
AWBuchanan7 Nov 14, 2023
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 src/core/rdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct rdp_config
struct rdp_frame_buffer
{
uint32_t* pixels;
uint32_t* depth;
uint32_t width;
uint32_t height;
uint32_t pitch;
Expand Down
13 changes: 11 additions & 2 deletions src/core/rdp/vi.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static uint32_t tvfadeoutstate[PRESCALE_HEIGHT];

// prescale buffer
static uint32_t prescale[PRESCALE_WIDTH * PRESCALE_HEIGHT];
static uint32_t prescale_depth[PRESCALE_WIDTH * PRESCALE_HEIGHT];
static uint32_t prescale_ptr;
static int32_t linecount;

Expand Down Expand Up @@ -115,6 +116,7 @@ static void vi_init(void)
vi_restore_init();

memset(prescale, 0, sizeof(prescale));
memset(prescale_depth, 0, sizeof(prescale_depth));

prevvicurrent = 0;
emucontrolsvicurrent = -1;
Expand Down Expand Up @@ -527,6 +529,7 @@ static void vi_process_end(void)
struct rdp_frame_buffer fb;
fb.pixels = prescale;
fb.pitch = PRESCALE_WIDTH;
fb.depth = prescale_depth;

int32_t output_height;

Expand Down Expand Up @@ -596,12 +599,14 @@ static void vi_process_fast(uint32_t worker_id)
int32_t x;
int32_t line = y * vi_width_low;
uint32_t* dst = prescale + y * hres_raw;
uint32_t* d = prescale_depth + y * hres_raw;

for (x = 0; x < hres_raw; x++) {
uint32_t r, g, b;
uint32_t r, g, b, zr, zg, zb;

switch (config.vi.mode) {
case VI_MODE_COLOR:
zr = zg = zb = rdram_read_idx16((rdp_states[worker_id].zb_address >> 1) + line + x) >> 8;
switch (ctrl.type) {
case VI_TYPE_RGBA5551: {
uint16_t pix = rdram_read_idx16((frame_buffer >> 1) + line + x);
Expand All @@ -625,7 +630,7 @@ static void vi_process_fast(uint32_t worker_id)
break;

case VI_MODE_DEPTH: {
r = g = b = rdram_read_idx16((rdp_states[0].zb_address >> 1) + line + x) >> 8;
r = g = b = zr = zg = zb = rdram_read_idx16((rdp_states[0].zb_address >> 1) + line + x) >> 8;
break;
}

Expand All @@ -635,6 +640,7 @@ static void vi_process_fast(uint32_t worker_id)
uint16_t pix;
rdram_read_pair16(&pix, &hval, (frame_buffer >> 1) + line + x);
r = g = b = (((pix & 1) << 2) | hval) << 5;
zr = zg = zb = rdram_read_idx16((rdp_states[0].zb_address >> 1) + line + x) >> 8;
break;
}

Expand All @@ -643,8 +649,10 @@ static void vi_process_fast(uint32_t worker_id)
}

gamma_filters(&r, &g, &b, ctrl, &rdp_states[worker_id].rand_vi);
gamma_filters(&zr, &zg, &zb, ctrl, &rdp_states[worker_id].rand_vi);

dst[x] = (b << 16) | (g << 8) | r;
d[x] = (zb << 16) | (zg << 8) | zr;
}
}
}
Expand All @@ -656,6 +664,7 @@ static void vi_process_end_fast(void)
fb.width = hres_raw;
fb.height = vres_raw;
fb.pitch = hres_raw;
fb.depth = prescale_depth;

int32_t filtered_height = (vres << 1) * V_SYNC_NTSC / v_sync;
int32_t output_height = hres_raw * filtered_height / hres;
Expand Down
112 changes: 93 additions & 19 deletions src/plugin/common/gl_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

static GLuint program;
static GLuint vao;
static GLuint texture;
static GLuint texture = 1;
static GLuint depth_texture = 2;

static GLint colorValueTextureLocation;
static GLint depthValueTextureLocation;

static int32_t tex_width;
static int32_t tex_height;
Expand Down Expand Up @@ -136,39 +140,63 @@ void gl_screen_init(struct rdp_config* config)
SHADER_HEADER
"in vec2 uv;\n"
"layout(location = 0) out vec4 color;\n"
"uniform sampler2D tex0;\n"

"uniform sampler2D ColorValueTexture;\n"
"uniform sampler2D DepthValueTexture;\n"

"void main(void) {\n"
#ifdef GLES
" color = texture(tex0, uv);\n"
" color = texture(ColorValueTexture, uv);\n"
#else
" color.bgra = texture(tex0, uv);\n"
" color.bgra = texture(ColorValueTexture, uv);\n"
#endif
" gl_FragDepth = texture(DepthValueTexture, uv).r;\n"
"}\n";

// compile and link OpenGL program
GLuint vert = gl_shader_compile(GL_VERTEX_SHADER, vert_shader);
GLuint frag = gl_shader_compile(GL_FRAGMENT_SHADER, frag_shader);
program = gl_shader_link(vert, frag);

// get the uniform variables location
depthValueTextureLocation = glGetUniformLocation(program, "DepthValueTexture");
colorValueTextureLocation = glGetUniformLocation(program, "ColorValueTexture");

// specify the shader program to use
glUseProgram(program);

// bind the uniform variables locations
glUniform1i(depthValueTextureLocation, 0);
glUniform1i(colorValueTextureLocation, 1);

// prepare dummy VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

// prepare texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

// select interpolation method
GLint filter;
switch (config->vi.interp) {
case VI_INTERP_LINEAR:
filter = GL_LINEAR;
break;
case VI_INTERP_NEAREST:
default:
filter = GL_NEAREST;
case VI_INTERP_LINEAR:
filter = GL_LINEAR;
break;
case VI_INTERP_NEAREST:
default:
filter = GL_NEAREST;
}

// prepare depth texture
glActiveTexture(GL_TEXTURE0 + 0);
glGenTextures(1, &depth_texture);
glBindTexture(GL_TEXTURE_2D, depth_texture);
// configure interpolation method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);

// prepare color texture
glActiveTexture(GL_TEXTURE0 + 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// configure interpolation method
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);

Expand All @@ -185,18 +213,51 @@ bool gl_screen_write(struct rdp_frame_buffer* fb, int32_t output_height)
tex_width = fb->width;
tex_height = fb->height;

// select the color value binding
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture);

// set pitch for all unpacking operations
glPixelStorei(GL_UNPACK_ROW_LENGTH, fb->pitch);
// reallocate texture buffer on GPU
glDepthMask(false);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, TEX_FORMAT, TEX_TYPE, fb->pixels);
glDepthMask(true);

// select the depth value binding
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, depth_texture);

// set pitch for all unpacking operations
glPixelStorei(GL_UNPACK_ROW_LENGTH, fb->pitch);
// reallocate texture buffer on GPU
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width,
tex_height, 0, TEX_FORMAT, TEX_TYPE, fb->pixels);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_DEPTH_TEST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, TEX_FORMAT, TEX_TYPE, fb->depth);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_DEPTH_TEST);

msg_debug("%s: resized framebuffer texture: %dx%d", __FUNCTION__, tex_width, tex_height);
} else {
// select the color value binding
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture);

// copy local buffer to GPU texture buffer
glDepthMask(false);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_width, tex_height, TEX_FORMAT, TEX_TYPE, fb->pixels);
glDepthMask(true);

// select the depth value binding
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, depth_texture);

// copy local buffer to GPU texture buffer
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_width, tex_height,
TEX_FORMAT, TEX_TYPE, fb->pixels);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_DEPTH_TEST);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, tex_width, tex_height, TEX_FORMAT, TEX_TYPE, fb->depth);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_DEPTH_TEST);
}

// update output size
Expand Down Expand Up @@ -239,8 +300,21 @@ void gl_screen_render(int32_t win_width, int32_t win_height, int32_t win_x, int3
// configure viewport
glViewport(win_x, win_y, win_width, win_height);

// draw fullscreen triangle
// select the color value binding
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, texture);

// draw
glDrawArrays(GL_TRIANGLES, 0, 3);

// select the depth value binding
glActiveTexture(GL_TEXTURE0 + 0);
glBindTexture(GL_TEXTURE_2D, depth_texture);

// draw
glEnable(GL_DEPTH_TEST);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisable(GL_DEPTH_TEST);

// check if there was an error when using any of the commands above
gl_check_errors();
Expand Down