Skip to content

Commit e6f025a

Browse files
committed
[Fix] (GL, Framebuffer): employ depth clear hack according to fbo status
1 parent 039171e commit e6f025a

3 files changed

Lines changed: 65 additions & 24 deletions

File tree

src/main/cpp/gl/framebuffer.cpp

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,12 @@
88
#include "FSR1/FSR1.h"
99

1010
#define DEBUG 0
11-
struct attachment_t {
12-
GLenum textarget;
13-
GLuint texture;
14-
GLint level;
15-
};
16-
struct framebuffer_t {
17-
bool initialized = false;
18-
attachment_t* color_attachments = nullptr;
19-
attachment_t depth_attachment = {0};
20-
attachment_t stencil_attachment = {0};
21-
};
11+
2212
static GLint MAX_COLOR_ATTACHMENTS = 0;
2313
static GLint MAX_DRAW_BUFFERS = 0;
24-
static GLuint current_draw_fbo = 0;
25-
static GLuint current_read_fbo = 0;
26-
static std::vector<framebuffer_t> framebuffers;
14+
GLuint current_draw_fbo = 0;
15+
GLuint current_read_fbo = 0;
16+
std::vector<framebuffer_t> framebuffers;
2717
void ensure_max_attachments() {
2818
if (MAX_COLOR_ATTACHMENTS == 0) {
2919
GLES.glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &MAX_COLOR_ATTACHMENTS);
@@ -96,32 +86,59 @@ void glFramebufferTexture(GLenum target, GLenum attachment, GLuint texture, GLin
9686
GLES.glFramebufferTexture(target, attachment, texture, level);
9787
}
9888
void glDrawBuffer(GLenum buffer) {
99-
GLint currentFBO;
100-
GLES.glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFBO);
101-
if (currentFBO == 0) {
89+
LOG()
90+
LOG_D("glDrawBuffer %d", buffer)
91+
92+
// GLint currentFBO;
93+
// GLES.glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFBO);
94+
if (current_draw_fbo == 0) {
10295
GLenum buffers[] = {buffer};
103-
GLES.glDrawBuffers(1, buffers);
96+
glDrawBuffers(1, buffers);
10497
} else {
10598
GLint maxAttachments;
10699
GLES.glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS, &maxAttachments);
107100

108101
if (buffer == GL_NONE) {
102+
framebuffers[current_draw_fbo].color_attachments_all_none = true;
109103
std::vector<GLenum> buffers(maxAttachments, GL_NONE);
110-
GLES.glDrawBuffers(maxAttachments, buffers.data());
104+
glDrawBuffers(maxAttachments, buffers.data());
111105
} else if (buffer >= GL_COLOR_ATTACHMENT0 && buffer < GL_COLOR_ATTACHMENT0 + maxAttachments) {
106+
framebuffers[current_draw_fbo].color_attachments_all_none = false;
112107
std::vector<GLenum> buffers(maxAttachments, GL_NONE);
113108
buffers[buffer - GL_COLOR_ATTACHMENT0] = buffer;
114-
GLES.glDrawBuffers(maxAttachments, buffers.data());
109+
glDrawBuffers(maxAttachments, buffers.data());
115110
}
116111
}
112+
CHECK_GL_ERROR;
117113
}
118114
void glDrawBuffers(GLsizei n, const GLenum* bufs) {
115+
LOG()
119116
if (current_draw_fbo == 0) {
120117
GLES.glDrawBuffers(n, bufs);
121118
return;
122119
}
123-
std::vector<GLenum> new_bufs(n);
120+
124121
framebuffer_t& fbo = framebuffers[current_draw_fbo];
122+
123+
bool all_none = true;
124+
for (int i = 0; i < n; ++i) {
125+
if (bufs[i] != GL_NONE) {
126+
all_none = false;
127+
break;
128+
}
129+
}
130+
131+
if (all_none) {
132+
LOG_D("glDrawBuffers, fb %d all_none true", current_draw_fbo)
133+
fbo.color_attachments_all_none = true;
134+
GLES.glDrawBuffers(n, bufs);
135+
return;
136+
} else {
137+
LOG_D("glDrawBuffers, fb %d all_none false", current_draw_fbo)
138+
fbo.color_attachments_all_none = false;
139+
}
140+
141+
std::vector<GLenum> new_bufs(n);
125142
for (int i = 0; i < n; i++) {
126143
if (bufs[i] >= GL_COLOR_ATTACHMENT0 && bufs[i] < GL_COLOR_ATTACHMENT0 + MAX_COLOR_ATTACHMENTS) {
127144
GLenum logical_attachment = bufs[i];

src/main/cpp/gl/framebuffer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@
88
#include <GL/gl.h>
99
#include <cstddef>
1010

11+
struct attachment_t {
12+
GLenum textarget;
13+
GLuint texture;
14+
GLint level;
15+
};
16+
struct framebuffer_t {
17+
bool initialized = false;
18+
bool color_attachments_all_none = false;
19+
attachment_t* color_attachments = nullptr;
20+
attachment_t depth_attachment = {0};
21+
attachment_t stencil_attachment = {0};
22+
};
23+
1124
#ifdef __cplusplus
1225
extern "C" {
1326
#endif

src/main/cpp/gl/gl.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
#include "../gles/loader.h"
1010
#include "../config/settings.h"
1111
#include "mg.h"
12+
#include "framebuffer.h"
1213

1314
#define DEBUG 0
1415

1516
static GLclampd currentDepthValue;
1617

18+
extern GLuint current_draw_fbo;
19+
extern std::vector<framebuffer_t> framebuffers;
20+
1721
void glClearDepth(GLclampd depth) {
1822
LOG()
1923
currentDepthValue = depth;
@@ -111,9 +115,17 @@ void glClear(GLbitfield mask) {
111115
LOG();
112116
LOG_D("glClear, mask = 0x%x", mask);
113117

118+
INIT_CHECK_GL_ERROR
119+
120+
GLES.glClear(mask);
121+
CHECK_GL_ERROR_NO_INIT
122+
114123
if (global_settings.angle == AngleMode::Enabled &&
115124
mask == GL_DEPTH_BUFFER_BIT &&
116-
fabs(currentDepthValue - 1.0f) <= 0.001f) {
125+
fabs(currentDepthValue - 1.0f) <= 0.001f
126+
&& framebuffers[current_draw_fbo].color_attachments_all_none
127+
) {
128+
LOG_D("doing depth workaround")
117129
if (global_settings.angle_depth_clear_fix_mode == AngleDepthClearFixMode::Mode1)
118130
// Workaround for ANGLE depth-clear bug: if depth≈1.0, draw a fullscreen triangle at z=1.0 to force actual depth buffer write.
119131
DrawDepthClearTri();
@@ -124,9 +136,8 @@ void glClear(GLbitfield mask) {
124136
}
125137
// Clear again
126138
}
127-
GLES.glClear(mask);
128139

129-
CHECK_GL_ERROR;
140+
CHECK_GL_ERROR_NO_INIT;
130141
}
131142

132143
void glHint(GLenum target, GLenum mode) {

0 commit comments

Comments
 (0)