@@ -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
136138static 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
0 commit comments