Skip to content

Commit 8930aa4

Browse files
committed
small refactor of glTexSubImage3D
1 parent 1f6acb9 commit 8930aa4

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

code/graphics/opengl/es_compatibility.h

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,15 @@ static inline void convert_BGRA1555_REV_to_RGBA8888(const uint16_t* src, uint8_t
9494
}
9595

9696
// BGR -> RGB
97-
static inline void convert_BGR_to_RGB(uint8_t* p, size_t npx)
97+
static inline void convert_BGR_to_RGB(const uint8_t* src, uint8_t* dst, size_t npx)
9898
{
99-
for (size_t i = 0; i < npx; ++i) {
100-
uint8_t* px = p + 3 * i;
101-
uint8_t tmp = px[0]; // <- B
102-
px[0] = px[2]; // R -> B
103-
px[2] = tmp; // B -> R
99+
for (size_t i = 0, s = 0, t = 0; i < npx; ++i, s += 3, t += 3) {
100+
uint8_t b = src[s + 0];
101+
uint8_t g = src[s + 1];
102+
uint8_t r = src[s + 2];
103+
dst[t + 0] = r;
104+
dst[t + 1] = g;
105+
dst[t + 2] = b;
104106
}
105107
}
106108

@@ -135,57 +137,56 @@ static inline void convert_BGR_to_RGBA(const uint8_t* src, uint8_t* dst, size_t
135137

136138
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)
137139
{
140+
const size_t npx = size_t(w) * size_t(h) * size_t(d);
141+
138142
if (format == GL_BGRA && type == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
139143
GLint internalFormat = query_internalformat_3d(target,level);
140-
const size_t npx = size_t(w) * size_t(h);
141-
if (internalFormat != GL_RGBA8)
144+
if (internalFormat == GL_RGBA8)
142145
{
143146
format = GL_RGBA;
144-
type = GL_UNSIGNED_SHORT_5_5_5_1;
145-
if (data != nullptr) {
146-
std::vector<uint8_t> scratch(npx * 2);
147-
convert_BGRA1555_REV_to_RGBA5551(reinterpret_cast<const uint16_t*>(data),reinterpret_cast<uint16_t*>(scratch.data()),npx);
147+
type = GL_UNSIGNED_BYTE;
148+
if (data != nullptr)
149+
{
150+
std::vector<uint8_t> scratch(npx * 4); // RGBA8888 = 4 BPP
151+
convert_BGRA1555_REV_to_RGBA8888(reinterpret_cast<const uint16_t*>(data), scratch.data(), npx);
148152
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
149153
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
150154
return;
151-
}
155+
}
152156
} else {
153157
format = GL_RGBA;
154-
type = GL_UNSIGNED_BYTE;
158+
type = GL_UNSIGNED_SHORT_5_5_5_1;
155159
if (data != nullptr) {
156-
std::vector<uint8_t> scratch(npx * 4);
157-
convert_BGRA1555_REV_to_RGBA8888(reinterpret_cast<const uint16_t*>(data), scratch.data(), npx);
160+
std::vector<uint8_t> scratch(npx * 2); // RGBA5551 = 2 BPP
161+
convert_BGRA1555_REV_to_RGBA5551(reinterpret_cast<const uint16_t*>(data),
162+
reinterpret_cast<uint16_t*>(scratch.data()),
163+
npx);
158164
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
159165
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
160166
return;
161-
}
162-
167+
}
163168
}
164169
}
165170

166171
if (format == GL_BGR && type == GL_UNSIGNED_BYTE) {
167172
GLint internalFormat = query_internalformat_3d(target, level);
168-
if (internalFormat != GL_RGBA8)
173+
if (internalFormat == GL_RGBA8)
169174
{
170-
format = GL_RGB;
171-
if (data != nullptr)
172-
{
173-
std::vector<uint8_t> scratch(w * h * 3);
174-
memcpy(scratch.data(), data, scratch.size());
175-
convert_BGR_to_RGB(scratch.data(), (size_t)w * (size_t)h);
175+
format = GL_RGBA;
176+
if (data != nullptr) {
177+
std::vector<uint8_t> scratch(npx * 4); // RGBA8888 = 4 BPP
178+
convert_BGR_to_RGBA(static_cast<const uint8_t*>(data), scratch.data(), npx);
176179
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
177180
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
178181
return;
179182
}
180183
}
181184
else
182185
{
183-
format = GL_RGBA;
184-
if (data != nullptr)
185-
{
186-
const size_t npx = size_t(w) * size_t(h) * size_t(d);
187-
std::vector<uint8_t> scratch(npx * 4);
188-
convert_BGR_to_RGBA(static_cast<const uint8_t*>(data), scratch.data(), npx);
186+
format = GL_RGB;
187+
if (data != nullptr) {
188+
std::vector<uint8_t> scratch(npx * 3); // RGB888 = 3 BPP
189+
convert_BGR_to_RGB(static_cast<const uint8_t*>(data), scratch.data(), npx);
189190
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
190191
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, format, type, scratch.data());
191192
return;
@@ -195,13 +196,13 @@ static inline void glTexSubImage3D(GLenum target, GLint level, GLint xoff, GLint
195196

196197
if (format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV) {
197198
type = GL_UNSIGNED_BYTE;
198-
if (data != nullptr /* && !GLAD_GL_EXT_texture_format_BGRA8888 //this check dosent work*/) {
199-
// do conversion
200-
const size_t npx = size_t(w) * size_t(h);
199+
if (data != nullptr /* && !GLAD_GL_EXT_texture_format_BGRA8888*/) {
200+
// Conversion forced on because the check either does not work or buggy impl on Mali
201201
std::vector<uint8_t> scratch(npx * 4);
202202
convert_BGRA8888_to_RGBA8888(reinterpret_cast<const uint8_t*>(data), scratch.data(), npx);
203203
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
204204
glTexSubImage3D_glad(target, level, xoff, yoff, zoff, w, h, d, GL_RGBA, type, scratch.data());
205+
return;
205206
}
206207
}
207208

code/graphics/opengl/gropengl.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,18 +1369,17 @@ bool gr_opengl_init(std::unique_ptr<os::GraphicsOperations>&& graphicsOps)
13691369
mprintf(( " GLSL Version : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)));
13701370
mprintf(( "\n" ));
13711371
mprintf(("Extensions: \n"));
1372-
mprintf((" Geo shader support : %s\n", GL_ARB_gpu_shader5 ? NOX("YES") : NOX("NO")));
1372+
mprintf((" Geo shader support : %s\n", GLAD_GL_ARB_gpu_shader5 ? NOX("YES") : NOX("NO")));
13731373
mprintf((" S3TC texture support : %s\n", GLAD_GL_EXT_texture_compression_s3tc ? NOX("YES") : NOX("NO")));
13741374
mprintf((" BPTC texture support : %s\n", GLAD_GL_ARB_texture_compression_bptc ? NOX("YES") : NOX("NO")));
13751375
#ifdef USE_OPENGL_ES
13761376
mprintf((" ASTC texture support : %s\n", GLAD_GL_KHR_texture_compression_astc_ldr ? NOX("YES") : NOX("NO")));
1377-
mprintf((" Cull distance support : %s\n", GL_EXT_clip_cull_distance ? NOX("YES") : NOX("NO")));
1378-
mprintf((" Precompiled shaders support : %s\n", GL_OES_get_program_binary ? NOX("YES") : NOX("NO")));
1379-
mprintf((" Immutable buffer storage support : %s\n", GL_EXT_buffer_storage? NOX("YES") : NOX("NO")));
1380-
mprintf((" BGRA8888 format support: %s\n", GL_EXT_texture_format_BGRA8888 ? NOX("YES") : NOX("NO")));
1381-
mprintf((" Anisotropic filter support: %s\n", GL_EXT_texture_filter_anisotropic ? NOX("YES") : NOX("NO")));
1382-
mprintf((" Render to texture support: %s\n", GL_EXT_multisampled_render_to_texture ? NOX("YES") : NOX("NO")));
1383-
mprintf((" Render to texture2 support: %s\n", GL_EXT_multisampled_render_to_texture2 ? NOX("YES") : NOX("NO")));
1377+
mprintf((" Cull distance support : %s\n", GLAD_GL_EXT_clip_cull_distance ? NOX("YES") : NOX("NO")));
1378+
mprintf((" Precompiled shaders support : %s\n", GLAD_GL_OES_get_program_binary ? NOX("YES") : NOX("NO")));
1379+
mprintf((" Immutable buffer storage support : %s\n", GLAD_GL_EXT_buffer_storage ? NOX("YES") : NOX("NO")));
1380+
mprintf((" BGRA8888 format support: %s\n", GLAD_GL_EXT_texture_format_BGRA8888 ? NOX("YES") : NOX("NO")));
1381+
mprintf((" Anisotropic filter support: %s\n", GLAD_GL_EXT_texture_filter_anisotropic ? NOX("YES") : NOX("NO")));
1382+
mprintf((" Multisampled render to texture support: %s\n", GLAD_GL_EXT_multisampled_render_to_texture ? NOX("YES") : NOX("NO")));
13841383
#endif
13851384
mprintf(("\n"));
13861385

0 commit comments

Comments
 (0)