Skip to content

Commit e4aa05e

Browse files
committed
improve es compatibility header and remove shader precision quantification
1 parent 7bf142c commit e4aa05e

File tree

5 files changed

+129
-28
lines changed

5 files changed

+129
-28
lines changed

code/graphics/opengl/es_compatibility.h

Lines changed: 81 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#include <glad/glad.h>
66
#include <KHR/khrplatform.h>
77
//TODO/Good to have:
8-
//-Message:GL_INVALID_OPERATION in glBlitFramebuffer(source and destination color buffer cannot be the same), this may not be easy to handle
98
//-IMGUI crashes on windows + opengl es
10-
//-limited color attachments to 4 in not a good way
11-
//-GL_PROXY_TEXTURE_2D and GL_TEXTURE_COMPRESSED_IMAGE_SIZE
9+
//-Deferred is broken
10+
//-MSAA causes ASSERTION: "GL_state.ValidForFlip()" at gropengl.cpp:141
11+
//-The entire MSAA path is broken in ES on multiple places
12+
//-GL_PROXY_TEXTURE_2D and GL_TEXTURE_COMPRESSED_IMAGE_SIZE solution
1213
//-glGetTexImage()
1314

1415
//Stubs Enums, this does not exist on GLES and need to be handled
@@ -134,7 +135,30 @@ static inline void convert_BGR_to_RGBA(const uint8_t* src, uint8_t* dst, size_t
134135
}
135136
}
136137

138+
// BGRA1555_REV -> RGB888
139+
static inline void convert_BGRA1555_REV_to_RGB888(const uint16_t* src, uint8_t* dstRGB8, size_t npx)
140+
{
141+
for (size_t i = 0; i < npx; ++i) {
142+
uint16_t s = src[i];
143+
uint8_t R5 = (s >> 10) & 0x1F;
144+
uint8_t G5 = (s >> 5) & 0x1F;
145+
uint8_t B5 = s & 0x1F;
146+
147+
// expand 5 to 8 bits
148+
uint8_t R = (R5 << 3) | (R5 >> 2);
149+
uint8_t G = (G5 << 3) | (G5 >> 2);
150+
uint8_t B = (B5 << 3) | (B5 >> 2);
151+
152+
size_t o = 3 * i;
153+
dstRGB8[o + 0] = R;
154+
dstRGB8[o + 1] = G;
155+
dstRGB8[o + 2] = B;
156+
}
157+
}
137158

159+
#ifdef glTexSubImage3D
160+
#undef glTexSubImage3D
161+
#endif
138162
static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint yoff, GLint zoff, GLsizei w, GLsizei h, GLsizei d, GLenum format, GLenum type, const void* data)
139163
{
140164
const size_t npx = size_t(w) * size_t(h) * size_t(d);
@@ -150,9 +174,20 @@ static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint
150174
std::vector<uint8_t> scratch(npx * 4); // RGBA8888 = 4 BPP
151175
convert_BGRA1555_REV_to_RGBA8888(reinterpret_cast<const uint16_t*>(data), scratch.data(), npx);
152176
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
153-
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
177+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
154178
return;
155179
}
180+
} else if (internalFormat == GL_RGB8) {
181+
format = GL_RGB;
182+
type = GL_UNSIGNED_BYTE;
183+
if (data != nullptr)
184+
{
185+
std::vector<uint8_t> scratch(npx * 3); // RGB888 = 3 BPP
186+
convert_BGRA1555_REV_to_RGB888(reinterpret_cast<const uint16_t*>(data), scratch.data(), npx);
187+
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
188+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
189+
return;
190+
}
156191
} else {
157192
format = GL_RGBA;
158193
type = GL_UNSIGNED_SHORT_5_5_5_1;
@@ -162,7 +197,7 @@ static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint
162197
reinterpret_cast<uint16_t*>(scratch.data()),
163198
npx);
164199
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
165-
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
200+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
166201
return;
167202
}
168203
}
@@ -177,7 +212,7 @@ static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint
177212
std::vector<uint8_t> scratch(npx * 4); // RGBA8888 = 4 BPP
178213
convert_BGR_to_RGBA(static_cast<const uint8_t*>(data), scratch.data(), npx);
179214
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
180-
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
215+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
181216
return;
182217
}
183218
}
@@ -188,7 +223,7 @@ static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint
188223
std::vector<uint8_t> scratch(npx * 3); // RGB888 = 3 BPP
189224
convert_BGR_to_RGB(static_cast<const uint8_t*>(data), scratch.data(), npx);
190225
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
191-
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
226+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
192227
return;
193228
}
194229
}
@@ -201,31 +236,62 @@ static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint
201236
std::vector<uint8_t> scratch(npx * 4);
202237
convert_BGRA8888_to_RGBA8888(reinterpret_cast<const uint8_t*>(data), scratch.data(), npx);
203238
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
204-
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, GL_RGBA, type, scratch.data());
239+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, GL_RGBA, type, scratch.data());
205240
return;
206241
}
207242
}
208243

209-
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, data);
244+
glad_glTexSubImage3D(target, level, xoff, yoff, zoff, w, h, d, format, type, data);
210245
}
211246

247+
#ifdef glTexImage2D
248+
#undef glTexImage2D
249+
#endif
212250
static inline void glTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void* data)
213251
{
214252
if (internalformat == GL_RGBA16F) {
215-
glTexImage2D_glad(target, level, internalformat, width, height, border, GL_RGBA, GL_HALF_FLOAT, data);
253+
glad_glTexImage2D(target, level, internalformat, width, height, border, GL_RGBA, GL_HALF_FLOAT, data);
216254
} else if (internalformat == GL_RGBA8) {
217-
glTexImage2D_glad(target, level, internalformat, width, height, border, GL_RGBA, GL_UNSIGNED_BYTE, data);
255+
glad_glTexImage2D(target, level, internalformat, width, height, border, GL_RGBA, GL_UNSIGNED_BYTE, data);
218256
} else if (internalformat == GL_DEPTH_COMPONENT24) {
219-
glTexImage2D_glad(target, level, internalformat, width, height, border, format, GL_UNSIGNED_INT, data);
257+
glad_glTexImage2D(target, level, internalformat, width, height, border, format, GL_UNSIGNED_INT, data);
258+
} else {
259+
glad_glTexImage2D(target, level, internalformat, width, height, border, format, type, data);
260+
}
261+
}
262+
263+
#ifdef glTexImage3D
264+
#undef glTexImage3D
265+
#endif
266+
static inline void glTexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const void *data)
267+
{
268+
if (internalformat == GL_DEPTH_COMPONENT24) {
269+
glad_glTexImage3D(target, level, internalformat, width, height, depth, border, format, GL_UNSIGNED_INT, data);
270+
} else if (type == GL_UNSIGNED_INT_8_8_8_8_REV) {
271+
if (internalformat == GL_RGBA16F) {
272+
glad_glTexImage3D(target, level, internalformat, width, height, depth, border, format, GL_HALF_FLOAT, data);
273+
} if (internalformat == GL_RGBA32F) {
274+
glad_glTexImage3D(target, level, internalformat, width, height, depth, border, format, GL_FLOAT, data);
275+
} else {
276+
glad_glTexImage3D(target, level, internalformat, width, height, depth, border, format, GL_UNSIGNED_BYTE, data);
277+
}
220278
} else {
221-
glTexImage2D_glad(target, level, internalformat, width, height, border, format, type, data);
279+
glad_glTexImage3D(target, level, internalformat, width, height, depth, border, format, type, data);
222280
}
223281
}
224282

283+
#ifdef glReadPixels
284+
#undef glReadPixels
285+
#endif
286+
static inline void glReadPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* data)
287+
{
288+
if (type == GL_UNSIGNED_INT_8_8_8_8_REV)
289+
type = GL_UNSIGNED_BYTE;
290+
glad_glReadPixels(x, y, width, height, format, type, data);
291+
}
292+
225293
inline void glDrawBuffer(GLenum data)
226294
{
227-
if (data >= GL_COLOR_ATTACHMENT4)
228-
return; //limit color attachments to 4
229295
const GLenum buffer[] = { data };
230296
glDrawBuffers(1, buffer);
231297
}

code/graphics/opengl/gropengldeferred.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,22 @@ void gr_opengl_deferred_lighting_begin(bool clearNonColorBufs)
8888
} else {
8989
// Copy the existing color data into the emissive part of the G-buffer since everything that already existed is
9090
// treated as emissive
91+
#ifndef USE_OPENGL_ES
9192
glDrawBuffer(GL_COLOR_ATTACHMENT4);
9293
glReadBuffer(GL_COLOR_ATTACHMENT0);
9394
glBlitFramebuffer(0, 0, gr_screen.max_w, gr_screen.max_h, 0, 0, gr_screen.max_w, gr_screen.max_h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
94-
95+
#else
96+
// one again ES does not consider GL_COLOR_ATTACHMENT4 as valid draw buffer
97+
GLint prev_read_fbo = 0, prev_tex2d = 0;
98+
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &prev_read_fbo);
99+
glGetIntegerv(GL_TEXTURE_BINDING_2D, &prev_tex2d);
100+
glBindFramebuffer(GL_READ_FRAMEBUFFER, Scene_framebuffer);
101+
glReadBuffer(GL_COLOR_ATTACHMENT0);
102+
glBindTexture(GL_TEXTURE_2D, Scene_emissive_texture);
103+
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, gr_screen.max_w, gr_screen.max_h);
104+
glBindTexture(GL_TEXTURE_2D, prev_tex2d);
105+
glBindFramebuffer(GL_READ_FRAMEBUFFER, prev_read_fbo);
106+
#endif
95107
}
96108

97109
glDrawBuffers(6, buffers);
@@ -231,8 +243,8 @@ void gr_opengl_deferred_lighting_finish()
231243
// GL_state.DepthMask(GL_FALSE);
232244

233245
opengl_shader_set_current(gr_opengl_maybe_create_shader(SDR_TYPE_DEFERRED_LIGHTING, ENVMAP > 0 ? SDR_FLAG_ENV_MAP : 0));
234-
235-
// Render on top of the composite buffer texture
246+
247+
#ifndef USE_OPENGL_ES
236248
glDrawBuffer(GL_COLOR_ATTACHMENT5);
237249
glReadBuffer(GL_COLOR_ATTACHMENT4);
238250
glBlitFramebuffer(0,
@@ -245,6 +257,18 @@ void gr_opengl_deferred_lighting_finish()
245257
gr_screen.max_h,
246258
GL_COLOR_BUFFER_BIT,
247259
GL_NEAREST);
260+
#else
261+
//Another case of ES not considering GL_COLOR_ATTACHMENT5 as valid draw buffer
262+
GLint prev_read_fbo = 0, prev_tex2d = 0;
263+
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &prev_read_fbo);
264+
glGetIntegerv(GL_TEXTURE_BINDING_2D, &prev_tex2d);
265+
glBindFramebuffer(GL_READ_FRAMEBUFFER, Scene_framebuffer);
266+
glReadBuffer(GL_COLOR_ATTACHMENT4);
267+
glBindTexture(GL_TEXTURE_2D, Scene_composite_texture);
268+
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, gr_screen.max_w, gr_screen.max_h);
269+
glBindTexture(GL_TEXTURE_2D, prev_tex2d);
270+
glBindFramebuffer(GL_READ_FRAMEBUFFER, prev_read_fbo);
271+
#endif
248272

249273
GL_state.Texture.Enable(0, GL_TEXTURE_2D, Scene_color_texture);
250274
GL_state.Texture.Enable(1, GL_TEXTURE_2D, Scene_normal_texture);

code/graphics/opengl/gropengldraw.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,11 +809,28 @@ void gr_opengl_copy_effect_texture()
809809
return;
810810
}
811811

812+
#ifndef USE_OPENGL_ES
812813
//Make sure we're reading from the up-to-date color texture
813814
glReadBuffer(GL_COLOR_ATTACHMENT0);
814815
glDrawBuffer(GL_COLOR_ATTACHMENT5);
815816
glBlitFramebuffer(0, 0, gr_screen.max_w, gr_screen.max_h, 0, 0, gr_screen.max_w, gr_screen.max_h, GL_COLOR_BUFFER_BIT, GL_NEAREST);
816817
glDrawBuffer(GL_COLOR_ATTACHMENT0);
818+
#else
819+
// For some reason, ES considers glDrawBuffer(GL_COLOR_ATTACHMENT5) as invalid here
820+
// try to copy to Scene_composite_texture in an alternative way
821+
// save states
822+
GLint prev_read_fbo = 0, prev_tex2d = 0;
823+
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &prev_read_fbo);
824+
glGetIntegerv(GL_TEXTURE_BINDING_2D, &prev_tex2d);
825+
// copy texture to scene_composite_texture
826+
glBindFramebuffer(GL_READ_FRAMEBUFFER, Scene_framebuffer);
827+
glReadBuffer(GL_COLOR_ATTACHMENT0);
828+
glBindTexture(GL_TEXTURE_2D, Scene_composite_texture);
829+
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, gr_screen.max_w, gr_screen.max_h);
830+
// restore states
831+
glBindTexture(GL_TEXTURE_2D, prev_tex2d);
832+
glBindFramebuffer(GL_READ_FRAMEBUFFER, prev_read_fbo);
833+
#endif
817834
}
818835

819836
void gr_opengl_render_shield_impact(shield_material* material_info,

code/graphics/opengl/gropenglshader.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,9 @@ static SCP_string opengl_shader_get_header(shader_type type_id, int flags, bool
309309
#ifndef USE_OPENGL_ES
310310
sflags << "#version " << GLSL_version << " core\n";
311311
#else
312-
sflags << "#version " << GLSL_version << " es\n";
313-
sflags << "precision highp float;" << "\n";
314-
sflags << "precision highp int;" << "\n";
315-
sflags << "precision highp sampler2D;" << "\n";
316-
sflags << "precision highp sampler2DArray;" << "\n";
317-
sflags << "precision highp samplerCube;" << "\n";
318-
sflags << "precision highp samplerBuffer;" << "\n";
319-
312+
sflags << "#version " << GLSL_version << " es\n";
320313
#endif
314+
321315
if (Detail.lighting < 3) {
322316
sflags << "#define FLAG_LIGHT_MODEL_BLINN_PHONG\n";
323317
}

lib/opengl/gl/glad_es/include/glad/glad.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ GLAPI PFNGLSTENCILOPSEPARATEPROC glad_glStencilOpSeparate;
14691469
#define glStencilOpSeparate glad_glStencilOpSeparate
14701470
typedef void (APIENTRYP PFNGLTEXIMAGE2DPROC)(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const void *pixels);
14711471
GLAPI PFNGLTEXIMAGE2DPROC glad_glTexImage2D;
1472-
#define glTexImage2D_glad glad_glTexImage2D //Name was changed to handle glTexImage2D on es_compatibility.h
1472+
#define glTexImage2D glad_glTexImage2D
14731473
typedef void (APIENTRYP PFNGLTEXPARAMETERFPROC)(GLenum target, GLenum pname, GLfloat param);
14741474
GLAPI PFNGLTEXPARAMETERFPROC glad_glTexParameterf;
14751475
#define glTexParameterf glad_glTexParameterf
@@ -1593,7 +1593,7 @@ GLAPI PFNGLTEXIMAGE3DPROC glad_glTexImage3D;
15931593
#define glTexImage3D glad_glTexImage3D
15941594
typedef void (APIENTRYP PFNGLTEXSUBIMAGE3DPROC)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const void *pixels);
15951595
GLAPI PFNGLTEXSUBIMAGE3DPROC glad_glTexSubImage3D;
1596-
#define glTexSubImage3D_glad glad_glTexSubImage3D // name changed to be handled on es_compatibility.h
1596+
#define glTexSubImage3D glad_glTexSubImage3D
15971597
typedef void (APIENTRYP PFNGLCOPYTEXSUBIMAGE3DPROC)(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);
15981598
GLAPI PFNGLCOPYTEXSUBIMAGE3DPROC glad_glCopyTexSubImage3D;
15991599
#define glCopyTexSubImage3D glad_glCopyTexSubImage3D

0 commit comments

Comments
 (0)