| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* OpenGL bitmap vtable |
| 12 |
* |
| 13 |
* By Elias Pschernig. |
| 14 |
* OpenGL ES 1.1 support by Trent Gamblin. |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <math.h> |
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#include "allegro5/allegro.h" |
| 22 |
#include "allegro5/allegro_opengl.h" |
| 23 |
#include "allegro5/internal/aintern.h" |
| 24 |
#include "allegro5/internal/aintern_display.h" |
| 25 |
#include "allegro5/internal/aintern_memblit.h" |
| 26 |
#include "allegro5/internal/aintern_opengl.h" |
| 27 |
#include "allegro5/internal/aintern_pixels.h" |
| 28 |
#include "allegro5/internal/aintern_system.h" |
| 29 |
#include "allegro5/internal/aintern_transform.h" |
| 30 |
|
| 31 |
#if defined ALLEGRO_ANDROID |
| 32 |
#include "allegro5/internal/aintern_android.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "ogl_helpers.h" |
| 36 |
|
| 37 |
ALLEGRO_DEBUG_CHANNEL("opengl") |
| 38 |
|
| 39 |
/* OpenGL does not support "locking", i.e. direct access to a memory |
| 40 |
* buffer with pixel data. Instead, the data can be copied from/to |
| 41 |
* client memory. Because OpenGL stores pixel data starting with the |
| 42 |
* pixel in the lower left corner, that's also how we return locked |
| 43 |
* data. Otherwise (and as was done in earlier versions of this code) |
| 44 |
* we would have to flip all locked memory after receiving and before |
| 45 |
* sending it to OpenGL. |
| 46 |
* |
| 47 |
* Also, we do support old OpenGL drivers where textures must have |
| 48 |
* power-of-two dimensions. If a non-power-of-two bitmap is created in |
| 49 |
* such a case, we use a texture with the next larger POT dimensions, |
| 50 |
* and just keep some unused padding space to the right/bottom of the |
| 51 |
* pixel data. |
| 52 |
* |
| 53 |
* Putting it all together, if we have an Allegro bitmap like this, |
| 54 |
* with Allegro's y-coordinates: |
| 55 |
* 0 ########### |
| 56 |
* 1 #111 # |
| 57 |
* 2 #222 # |
| 58 |
* 3 #333 # |
| 59 |
* 4 ########### |
| 60 |
* |
| 61 |
* Then the corresponding texture looks like this with OpenGL |
| 62 |
* y-coordinates (assuming we use an old driver which needs padding to |
| 63 |
* POT): |
| 64 |
* 7 ................ |
| 65 |
* 6 ................ |
| 66 |
* 5 ................ |
| 67 |
* 4 ###########..... |
| 68 |
* 3 #333 #..... |
| 69 |
* 2 #222 #..... |
| 70 |
* 1 #111 #..... |
| 71 |
* 0 ###########..... |
| 72 |
*/ |
| 73 |
|
| 74 |
/* Conversion table from Allegro's pixel formats to corresponding OpenGL |
| 75 |
* formats. The three entries are: |
| 76 |
* - GL internal format: the number of color components in the texture |
| 77 |
* - GL pixel type: Specifies the data type of the pixel data. |
| 78 |
* - GL format: Specifies the format of the pixel data. |
| 79 |
* |
| 80 |
* GL does not support RGB_555 and BGR_555 directly so we use |
| 81 |
* GL_UNSIGNED_SHORT_1_5_5_5_REV when transferring pixel data, and ensure that |
| 82 |
* the alpha bit (the "1" component) is present by setting GL_ALPHA_BIAS. |
| 83 |
* |
| 84 |
* Desktop OpenGL 3.0+ has no GL_LUMINANCE, so we have to adjust depending on |
| 85 |
* the runtime version. |
| 86 |
*/ |
| 87 |
#define get_glformat(f, c) _al_ogl_get_glformat((f), (c)) |
| 88 |
int _al_ogl_get_glformat(int format, int component) |
| 89 |
{ |
| 90 |
#if !defined ALLEGRO_CFG_OPENGLES |
| 91 |
static int glformats[ALLEGRO_NUM_PIXEL_FORMATS][3] = { |
| 92 |
/* Skip pseudo formats */ |
| 93 |
{0, 0, 0}, |
| 94 |
{0, 0, 0}, |
| 95 |
{0, 0, 0}, |
| 96 |
{0, 0, 0}, |
| 97 |
{0, 0, 0}, |
| 98 |
{0, 0, 0}, |
| 99 |
{0, 0, 0}, |
| 100 |
{0, 0, 0}, |
| 101 |
{0, 0, 0}, |
| 102 |
/* Actual formats */ |
| 103 |
{GL_RGBA8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_BGRA}, /* ARGB_8888 */ |
| 104 |
{GL_RGBA8, GL_UNSIGNED_INT_8_8_8_8, GL_RGBA}, /* RGBA_8888 */ |
| 105 |
{GL_RGBA4, GL_UNSIGNED_SHORT_4_4_4_4_REV, GL_BGRA}, /* ARGB_4444 */ |
| 106 |
{GL_RGB8, GL_UNSIGNED_BYTE, GL_BGR}, /* RGB_888 */ |
| 107 |
{GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB}, /* RGB_565 */ |
| 108 |
{GL_RGB5, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA}, /* RGB_555 - see above */ |
| 109 |
{GL_RGB5_A1, GL_UNSIGNED_SHORT_5_5_5_1, GL_RGBA}, /* RGBA_5551 */ |
| 110 |
{GL_RGB5_A1, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_BGRA}, /* ARGB_1555 */ |
| 111 |
{GL_RGBA8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_RGBA}, /* ABGR_8888 */ |
| 112 |
{GL_RGBA8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_RGBA}, /* XBGR_8888 */ |
| 113 |
{GL_RGB8, GL_UNSIGNED_BYTE, GL_RGB}, /* BGR_888 */ |
| 114 |
{GL_RGB, GL_UNSIGNED_SHORT_5_6_5_REV, GL_RGB}, /* BGR_565 */ |
| 115 |
{GL_RGB5, GL_UNSIGNED_SHORT_1_5_5_5_REV, GL_RGBA}, /* BGR_555 - see above */ |
| 116 |
{GL_RGBA8, GL_UNSIGNED_INT_8_8_8_8, GL_RGBA}, /* RGBX_8888 */ |
| 117 |
{GL_RGBA8, GL_UNSIGNED_INT_8_8_8_8_REV, GL_BGRA}, /* XRGB_8888 */ |
| 118 |
{GL_RGBA32F_ARB, GL_FLOAT, GL_RGBA}, /* ABGR_F32 */ |
| 119 |
{GL_RGBA, GL_UNSIGNED_BYTE, GL_RGBA}, /* ABGR_8888_LE */ |
| 120 |
{GL_RGBA4, GL_UNSIGNED_SHORT_4_4_4_4, GL_RGBA}, /* RGBA_4444 */ |
| 121 |
{GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE}, /* SINGLE_CHANNEL_8 */ |
| 122 |
{GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, GL_UNSIGNED_INT_8_8_8_8, GL_RGBA}, /* RGBA_DXT1 */ |
| 123 |
{GL_COMPRESSED_RGBA_S3TC_DXT3_EXT, GL_UNSIGNED_INT_8_8_8_8, GL_RGBA}, /* RGBA_DXT3 */ |
| 124 |
{GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, GL_UNSIGNED_INT_8_8_8_8, GL_RGBA}, /* RGBA_DXT5 */ |
| 125 |
}; |
| 126 |
|
| 127 |
if (al_get_opengl_version() >= _ALLEGRO_OPENGL_VERSION_3_0) { |
| 128 |
glformats[ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8][0] = GL_RED; |
| 129 |
glformats[ALLEGRO_PIXEL_FORMAT_SINGLE_CHANNEL_8][2] = GL_RED; |
| 130 |
} |
| 131 |
#else |
| 132 |
// TODO: Check supported formats by various GLES versions |
| 133 |
static const int glformats[ALLEGRO_NUM_PIXEL_FORMATS][3] = { |
| 134 |
/* Skip pseudo formats */ |
| 135 |
{0, 0, 0}, |
| 136 |
{0, 0, 0}, |
| 137 |
{0, 0, 0}, |
| 138 |
{0, 0, 0}, |
| 139 |
{0, 0, 0}, |
| 140 |
{0, 0, 0}, |
| 141 |
{0, 0, 0}, |
| 142 |
{0, 0, 0}, |
| 143 |
{0, 0, 0}, |
| 144 |
/* Actual formats */ |
| 145 |
{0, 0, 0}, |
| 146 |
{0, 0, 0}, |
| 147 |
{0, 0, 0}, |
| 148 |
{0, 0, 0}, |
| 149 |
{GL_RGB, GL_UNSIGNED_SHORT_5_6_5, GL_RGB}, /* RGB_565 */ |
| 150 |
{0, 0, 0}, |
| 151 |
{GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, GL_RGBA}, /* RGBA_5551 */ |
| 152 |
{0, 0, 0}, |
| 153 |
{0, 0, 0}, |
| 154 |
{0, 0, 0}, |
| 155 |
{0, 0, 0}, |
| 156 |
{0, 0, 0}, |
| 157 |
{0, 0, 0}, |
| 158 |
{0, 0, 0}, |
| 159 |
{0, 0, 0}, |
| 160 |
{0, 0, 0}, |
| 161 |
{GL_RGBA, GL_UNSIGNED_BYTE, GL_RGBA}, /* ABGR_8888_LE */ |
| 162 |
{GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, GL_RGBA}, /* RGBA_4444 */ |
| 163 |
{GL_LUMINANCE, GL_UNSIGNED_BYTE, GL_LUMINANCE}, /* SINGLE_CHANNEL_8 */ |
| 164 |
{0, 0, 0}, |
| 165 |
{0, 0, 0}, |
| 166 |
{0, 0, 0}, |
| 167 |
}; |
| 168 |
#endif |
| 169 |
|
| 170 |
return glformats[format][component]; |
| 171 |
} |
| 172 |
|
| 173 |
static ALLEGRO_BITMAP_INTERFACE glbmp_vt; |
| 174 |
|
| 175 |
|
| 176 |
#define SWAP(type, x, y) {type temp = x; x = y; y = temp;} |
| 177 |
|
| 178 |
#define ERR(e) case e: return #e; |
| 179 |
char const *_al_gl_error_string(GLenum e) |
| 180 |
{ |
| 181 |
switch (e) { |
| 182 |
ERR(GL_NO_ERROR) |
| 183 |
ERR(GL_INVALID_ENUM) |
| 184 |
ERR(GL_INVALID_VALUE) |
| 185 |
ERR(GL_INVALID_OPERATION) |
| 186 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 187 |
ERR(GL_STACK_OVERFLOW) |
| 188 |
ERR(GL_STACK_UNDERFLOW) |
| 189 |
#endif |
| 190 |
ERR(GL_OUT_OF_MEMORY) |
| 191 |
#ifdef ALLEGRO_CFG_OPENGL_PROGRAMMABLE_PIPELINE |
| 192 |
ERR(GL_INVALID_FRAMEBUFFER_OPERATION) |
| 193 |
#endif |
| 194 |
} |
| 195 |
return "UNKNOWN"; |
| 196 |
} |
| 197 |
#undef ERR |
| 198 |
|
| 199 |
static INLINE void transform_vertex(float* x, float* y, float* z) |
| 200 |
{ |
| 201 |
al_transform_coordinates_3d(al_get_current_transform(), x, y, z); |
| 202 |
} |
| 203 |
|
| 204 |
static void draw_quad(ALLEGRO_BITMAP *bitmap, |
| 205 |
ALLEGRO_COLOR tint, |
| 206 |
float sx, float sy, float sw, float sh, |
| 207 |
int flags) |
| 208 |
{ |
| 209 |
float tex_l, tex_t, tex_r, tex_b, w, h, true_w, true_h; |
| 210 |
float dw = sw, dh = sh; |
| 211 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = bitmap->extra; |
| 212 |
ALLEGRO_OGL_BITMAP_VERTEX *verts; |
| 213 |
ALLEGRO_DISPLAY *disp = al_get_current_display(); |
| 214 |
|
| 215 |
(void)flags; |
| 216 |
|
| 217 |
if (disp->num_cache_vertices != 0 && ogl_bitmap->texture != disp->cache_texture) { |
| 218 |
disp->vt->flush_vertex_cache(disp); |
| 219 |
} |
| 220 |
disp->cache_texture = ogl_bitmap->texture; |
| 221 |
|
| 222 |
verts = disp->vt->prepare_vertex_cache(disp, 6); |
| 223 |
|
| 224 |
tex_l = ogl_bitmap->left; |
| 225 |
tex_r = ogl_bitmap->right; |
| 226 |
tex_t = ogl_bitmap->top; |
| 227 |
tex_b = ogl_bitmap->bottom; |
| 228 |
|
| 229 |
w = bitmap->w; |
| 230 |
h = bitmap->h; |
| 231 |
true_w = ogl_bitmap->true_w; |
| 232 |
true_h = ogl_bitmap->true_h; |
| 233 |
|
| 234 |
tex_l += sx / true_w; |
| 235 |
tex_t -= sy / true_h; |
| 236 |
tex_r -= (w - sx - sw) / true_w; |
| 237 |
tex_b += (h - sy - sh) / true_h; |
| 238 |
|
| 239 |
verts[0].x = 0; |
| 240 |
verts[0].y = dh; |
| 241 |
verts[0].z = 0; |
| 242 |
verts[0].tx = tex_l; |
| 243 |
verts[0].ty = tex_b; |
| 244 |
verts[0].r = tint.r; |
| 245 |
verts[0].g = tint.g; |
| 246 |
verts[0].b = tint.b; |
| 247 |
verts[0].a = tint.a; |
| 248 |
|
| 249 |
verts[1].x = 0; |
| 250 |
verts[1].y = 0; |
| 251 |
verts[1].z = 0; |
| 252 |
verts[1].tx = tex_l; |
| 253 |
verts[1].ty = tex_t; |
| 254 |
verts[1].r = tint.r; |
| 255 |
verts[1].g = tint.g; |
| 256 |
verts[1].b = tint.b; |
| 257 |
verts[1].a = tint.a; |
| 258 |
|
| 259 |
verts[2].x = dw; |
| 260 |
verts[2].y = dh; |
| 261 |
verts[2].z = 0; |
| 262 |
verts[2].tx = tex_r; |
| 263 |
verts[2].ty = tex_b; |
| 264 |
verts[2].r = tint.r; |
| 265 |
verts[2].g = tint.g; |
| 266 |
verts[2].b = tint.b; |
| 267 |
verts[2].a = tint.a; |
| 268 |
|
| 269 |
verts[4].x = dw; |
| 270 |
verts[4].y = 0; |
| 271 |
verts[4].z = 0; |
| 272 |
verts[4].tx = tex_r; |
| 273 |
verts[4].ty = tex_t; |
| 274 |
verts[4].r = tint.r; |
| 275 |
verts[4].g = tint.g; |
| 276 |
verts[4].b = tint.b; |
| 277 |
verts[4].a = tint.a; |
| 278 |
|
| 279 |
if (disp->cache_enabled) { |
| 280 |
/* If drawing is batched, we apply transformations manually. */ |
| 281 |
transform_vertex(&verts[0].x, &verts[0].y, &verts[0].z); |
| 282 |
transform_vertex(&verts[1].x, &verts[1].y, &verts[1].z); |
| 283 |
transform_vertex(&verts[2].x, &verts[2].y, &verts[2].z); |
| 284 |
transform_vertex(&verts[4].x, &verts[4].y, &verts[4].z); |
| 285 |
} |
| 286 |
verts[3] = verts[1]; |
| 287 |
verts[5] = verts[2]; |
| 288 |
|
| 289 |
if (!disp->cache_enabled) |
| 290 |
disp->vt->flush_vertex_cache(disp); |
| 291 |
} |
| 292 |
#undef SWAP |
| 293 |
|
| 294 |
|
| 295 |
static void ogl_draw_bitmap_region(ALLEGRO_BITMAP *bitmap, |
| 296 |
ALLEGRO_COLOR tint, float sx, float sy, |
| 297 |
float sw, float sh, int flags) |
| 298 |
{ |
| 299 |
// FIXME: hack |
| 300 |
// FIXME: need format conversion if they don't match |
| 301 |
ALLEGRO_BITMAP *target = al_get_target_bitmap(); |
| 302 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_target; |
| 303 |
ALLEGRO_DISPLAY *disp = _al_get_bitmap_display(target); |
| 304 |
|
| 305 |
/* For sub-bitmaps */ |
| 306 |
if (target->parent) { |
| 307 |
target = target->parent; |
| 308 |
} |
| 309 |
|
| 310 |
ogl_target = target->extra; |
| 311 |
|
| 312 |
if (!(al_get_bitmap_flags(bitmap) & ALLEGRO_MEMORY_BITMAP) && !bitmap->locked && |
| 313 |
!target->locked) { |
| 314 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_source = bitmap->extra; |
| 315 |
if (ogl_source->is_backbuffer) { |
| 316 |
/* Our source bitmap is the OpenGL backbuffer, the target |
| 317 |
* is an OpenGL texture. |
| 318 |
*/ |
| 319 |
float xtrans, ytrans; |
| 320 |
|
| 321 |
/* Source and target cannot both be the back-buffer. */ |
| 322 |
ASSERT(!ogl_target->is_backbuffer); |
| 323 |
|
| 324 |
/* If we only translate, we can do this fast. */ |
| 325 |
if (_al_transform_is_translation(al_get_current_transform(), |
| 326 |
&xtrans, &ytrans)) { |
| 327 |
/* In general, we can't modify the texture while it's |
| 328 |
* FBO bound - so we temporarily disable the FBO. |
| 329 |
*/ |
| 330 |
if (ogl_target->fbo_info) |
| 331 |
_al_ogl_set_target_bitmap(disp, bitmap); |
| 332 |
|
| 333 |
/* We need to do clipping because glCopyTexSubImage2D |
| 334 |
* fails otherwise. |
| 335 |
*/ |
| 336 |
if (xtrans < target->cl) { |
| 337 |
sx -= xtrans - target->cl; |
| 338 |
sw += xtrans - target->cl; |
| 339 |
xtrans = target->cl; |
| 340 |
} |
| 341 |
if (ytrans < target->ct) { |
| 342 |
sy -= ytrans - target->ct; |
| 343 |
sh += ytrans - target->ct; |
| 344 |
ytrans = target->ct; |
| 345 |
} |
| 346 |
if (xtrans + sw > target->cr_excl) { |
| 347 |
sw = target->cr_excl - xtrans; |
| 348 |
} |
| 349 |
if (ytrans + sh > target->cb_excl) { |
| 350 |
sh = target->cb_excl - ytrans; |
| 351 |
} |
| 352 |
|
| 353 |
/* Note: Allegro 5.0.0 does not support blending or |
| 354 |
* tinting if the source bitmap is the screen. So it is |
| 355 |
* correct to ignore them here. |
| 356 |
*/ |
| 357 |
|
| 358 |
glBindTexture(GL_TEXTURE_2D, ogl_target->texture); |
| 359 |
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, |
| 360 |
xtrans, target->h - ytrans - sh, |
| 361 |
sx, bitmap->h - sy - sh, |
| 362 |
sw, sh); |
| 363 |
/* Fix up FBO again after the copy. */ |
| 364 |
if (ogl_target->fbo_info) |
| 365 |
_al_ogl_set_target_bitmap(disp, target); |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
/* Drawing a deformed backbuffer is not supported. */ |
| 370 |
ASSERT(0); |
| 371 |
} |
| 372 |
} |
| 373 |
if (disp->ogl_extras->opengl_target == target) { |
| 374 |
draw_quad(bitmap, tint, sx, sy, sw, sh, flags); |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
/* If all else fails, fall back to software implementation. */ |
| 379 |
_al_draw_bitmap_region_memory(bitmap, tint, sx, sy, sw, sh, 0, 0, flags); |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
/* Helper to get smallest fitting power of two. */ |
| 384 |
static int pot(int x) |
| 385 |
{ |
| 386 |
int y = 1; |
| 387 |
while (y < x) y *= 2; |
| 388 |
return y; |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
static GLint ogl_bitmap_wrap(ALLEGRO_BITMAP_WRAP wrap) |
| 394 |
{ |
| 395 |
switch (wrap) { |
| 396 |
default: |
| 397 |
case ALLEGRO_BITMAP_WRAP_DEFAULT: |
| 398 |
return GL_CLAMP_TO_EDGE; |
| 399 |
case ALLEGRO_BITMAP_WRAP_REPEAT: |
| 400 |
return GL_REPEAT; |
| 401 |
case ALLEGRO_BITMAP_WRAP_CLAMP: |
| 402 |
return GL_CLAMP_TO_EDGE; |
| 403 |
case ALLEGRO_BITMAP_WRAP_MIRROR: |
| 404 |
return GL_MIRRORED_REPEAT; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
// FIXME: need to do all the logic AllegroGL does, checking extensions, |
| 411 |
// proxy textures, formats, limits ... |
| 412 |
static bool ogl_upload_bitmap(ALLEGRO_BITMAP *bitmap) |
| 413 |
{ |
| 414 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = bitmap->extra; |
| 415 |
int w = bitmap->w; |
| 416 |
int h = bitmap->h; |
| 417 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 418 |
int bitmap_flags = al_get_bitmap_flags(bitmap); |
| 419 |
bool post_generate_mipmap = false; |
| 420 |
GLenum e; |
| 421 |
int filter; |
| 422 |
int gl_filters[] = { |
| 423 |
GL_NEAREST, GL_LINEAR, |
| 424 |
GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR |
| 425 |
}; |
| 426 |
|
| 427 |
if (ogl_bitmap->texture == 0) { |
| 428 |
glGenTextures(1, &ogl_bitmap->texture); |
| 429 |
e = glGetError(); |
| 430 |
if (e) { |
| 431 |
ALLEGRO_ERROR("glGenTextures failed: %s\n", _al_gl_error_string(e)); |
| 432 |
} |
| 433 |
else { |
| 434 |
ALLEGRO_DEBUG("Created new OpenGL texture %d (%dx%d, format %s)\n", |
| 435 |
ogl_bitmap->texture, |
| 436 |
ogl_bitmap->true_w, ogl_bitmap->true_h, |
| 437 |
_al_pixel_format_name(bitmap_format)); |
| 438 |
} |
| 439 |
} |
| 440 |
glBindTexture(GL_TEXTURE_2D, ogl_bitmap->texture); |
| 441 |
e = glGetError(); |
| 442 |
if (e) { |
| 443 |
ALLEGRO_ERROR("glBindTexture for texture %d failed (%s).\n", |
| 444 |
ogl_bitmap->texture, _al_gl_error_string(e)); |
| 445 |
} |
| 446 |
|
| 447 |
/* Wrap, Min/Mag should always come before glTexImage2D so the texture is "complete" */ |
| 448 |
// NOTE: on OGLES CLAMP_TO_EDGE is only one supported with NPOT textures |
| 449 |
ALLEGRO_BITMAP_WRAP wrap_u, wrap_v; |
| 450 |
_al_get_bitmap_wrap(bitmap, &wrap_u, &wrap_v); |
| 451 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, ogl_bitmap_wrap(wrap_u)); |
| 452 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, ogl_bitmap_wrap(wrap_v)); |
| 453 |
|
| 454 |
filter = (bitmap_flags & ALLEGRO_MIPMAP) ? 2 : 0; |
| 455 |
if (bitmap_flags & ALLEGRO_MIN_LINEAR) { |
| 456 |
filter++; |
| 457 |
} |
| 458 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filters[filter]); |
| 459 |
|
| 460 |
filter = 0; |
| 461 |
if (bitmap_flags & ALLEGRO_MAG_LINEAR) { |
| 462 |
filter++; |
| 463 |
} |
| 464 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filters[filter]); |
| 465 |
|
| 466 |
// TODO: To support anisotropy, we would need an API for it. Something |
| 467 |
// like: |
| 468 |
// al_set_new_bitmap_option(ALLEGRO_ANISOTROPY, 16.0); |
| 469 |
#if 0 |
| 470 |
if (al_get_opengl_extension_list()->ALLEGRO_GL_EXT_texture_filter_anisotropic) { |
| 471 |
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, anisotropy); |
| 472 |
} |
| 473 |
#endif |
| 474 |
|
| 475 |
if (bitmap_flags & ALLEGRO_MIPMAP) { |
| 476 |
/* If using FBOs, use glGenerateMipmapEXT instead of the GL_GENERATE_MIPMAP |
| 477 |
* texture parameter. GL_GENERATE_MIPMAP is deprecated in GL 3.0 so we |
| 478 |
* may want to use the new method in other cases as well. |
| 479 |
*/ |
| 480 |
if (al_get_opengl_extension_list()->ALLEGRO_GL_EXT_framebuffer_object || |
| 481 |
al_get_opengl_extension_list()->ALLEGRO_GL_OES_framebuffer_object || |
| 482 |
IS_OPENGLES /* FIXME */) { |
| 483 |
post_generate_mipmap = true; |
| 484 |
} |
| 485 |
else { |
| 486 |
#ifdef ALLEGRO_CFG_OPENGL_FIXED_FUNCTION |
| 487 |
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); |
| 488 |
e = glGetError(); |
| 489 |
if (e) { |
| 490 |
ALLEGRO_ERROR("glTexParameteri for texture %d failed (%s).\n", |
| 491 |
ogl_bitmap->texture, _al_gl_error_string(e)); |
| 492 |
} |
| 493 |
#endif |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
/* If there's unused space around the bitmap, we need to clear it |
| 498 |
* else linear filtering will cause artifacts from the random |
| 499 |
* data there. We also clear for floating point formats because |
| 500 |
* NaN values in the texture cause some blending modes to fail on |
| 501 |
* those pixels |
| 502 |
*/ |
| 503 |
if (!IS_OPENGLES) { |
| 504 |
if (ogl_bitmap->true_w != bitmap->w || |
| 505 |
ogl_bitmap->true_h != bitmap->h || |
| 506 |
bitmap_format == ALLEGRO_PIXEL_FORMAT_ABGR_F32) { |
| 507 |
unsigned char *buf; |
| 508 |
buf = al_calloc(ogl_bitmap->true_h, ogl_bitmap->true_w); |
| 509 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 510 |
glTexImage2D(GL_TEXTURE_2D, 0, get_glformat(bitmap_format, 0), |
| 511 |
ogl_bitmap->true_w, ogl_bitmap->true_h, 0, |
| 512 |
GL_ALPHA, GL_UNSIGNED_BYTE, buf); |
| 513 |
e = glGetError(); |
| 514 |
al_free(buf); |
| 515 |
} |
| 516 |
else { |
| 517 |
glTexImage2D(GL_TEXTURE_2D, 0, get_glformat(bitmap_format, 0), |
| 518 |
ogl_bitmap->true_w, ogl_bitmap->true_h, 0, |
| 519 |
get_glformat(bitmap_format, 2), get_glformat(bitmap_format, 1), |
| 520 |
NULL); |
| 521 |
e = glGetError(); |
| 522 |
} |
| 523 |
} |
| 524 |
else { |
| 525 |
unsigned char *buf; |
| 526 |
int pix_size = al_get_pixel_size(bitmap_format); |
| 527 |
buf = al_calloc(pix_size, |
| 528 |
ogl_bitmap->true_h * ogl_bitmap->true_w); |
| 529 |
glPixelStorei(GL_UNPACK_ALIGNMENT, pix_size); |
| 530 |
glTexImage2D(GL_TEXTURE_2D, 0, get_glformat(bitmap_format, 0), |
| 531 |
ogl_bitmap->true_w, ogl_bitmap->true_h, 0, |
| 532 |
get_glformat(bitmap_format, 2), |
| 533 |
get_glformat(bitmap_format, 1), buf); |
| 534 |
e = glGetError(); |
| 535 |
al_free(buf); |
| 536 |
} |
| 537 |
|
| 538 |
if (e) { |
| 539 |
ALLEGRO_ERROR("glTexImage2D for format %s, size %dx%d failed (%s)\n", |
| 540 |
_al_pixel_format_name(bitmap_format), |
| 541 |
ogl_bitmap->true_w, ogl_bitmap->true_h, |
| 542 |
_al_gl_error_string(e)); |
| 543 |
glDeleteTextures(1, &ogl_bitmap->texture); |
| 544 |
ogl_bitmap->texture = 0; |
| 545 |
// FIXME: Should we convert it into a memory bitmap? Or if the size is |
| 546 |
// the problem try to use multiple textures? |
| 547 |
return false; |
| 548 |
} |
| 549 |
|
| 550 |
if (post_generate_mipmap) { |
| 551 |
glGenerateMipmapEXT(GL_TEXTURE_2D); |
| 552 |
e = glGetError(); |
| 553 |
if (e) { |
| 554 |
ALLEGRO_ERROR("glGenerateMipmapEXT for texture %d failed (%s).\n", |
| 555 |
ogl_bitmap->texture, _al_gl_error_string(e)); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
ogl_bitmap->left = 0; |
| 560 |
ogl_bitmap->right = (float) w / ogl_bitmap->true_w; |
| 561 |
ogl_bitmap->top = (float) h / ogl_bitmap->true_h; |
| 562 |
ogl_bitmap->bottom = 0; |
| 563 |
|
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
static void ogl_update_clipping_rectangle(ALLEGRO_BITMAP *bitmap) |
| 570 |
{ |
| 571 |
ALLEGRO_DISPLAY *ogl_disp = al_get_current_display(); |
| 572 |
ALLEGRO_BITMAP *target_bitmap = bitmap; |
| 573 |
|
| 574 |
if (bitmap->parent) { |
| 575 |
target_bitmap = bitmap->parent; |
| 576 |
} |
| 577 |
|
| 578 |
if (ogl_disp->ogl_extras->opengl_target == target_bitmap) { |
| 579 |
_al_ogl_setup_bitmap_clipping(bitmap); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
static void ogl_destroy_bitmap(ALLEGRO_BITMAP *bitmap) |
| 586 |
{ |
| 587 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = bitmap->extra; |
| 588 |
ALLEGRO_DISPLAY *disp; |
| 589 |
ALLEGRO_DISPLAY *bmp_disp; |
| 590 |
ALLEGRO_DISPLAY *old_disp = NULL; |
| 591 |
|
| 592 |
ASSERT(!al_is_sub_bitmap(bitmap)); |
| 593 |
|
| 594 |
bmp_disp = _al_get_bitmap_display(bitmap); |
| 595 |
disp = al_get_current_display(); |
| 596 |
if (bmp_disp->ogl_extras->is_shared == false && |
| 597 |
bmp_disp != disp) { |
| 598 |
old_disp = disp; |
| 599 |
_al_set_current_display_only(bmp_disp); |
| 600 |
} |
| 601 |
|
| 602 |
if (bmp_disp->ogl_extras->opengl_target == bitmap) { |
| 603 |
bmp_disp->ogl_extras->opengl_target = NULL; |
| 604 |
} |
| 605 |
|
| 606 |
al_remove_opengl_fbo(bitmap); |
| 607 |
|
| 608 |
if (ogl_bitmap->texture) { |
| 609 |
glDeleteTextures(1, &ogl_bitmap->texture); |
| 610 |
ogl_bitmap->texture = 0; |
| 611 |
} |
| 612 |
|
| 613 |
if (old_disp) { |
| 614 |
_al_set_current_display_only(old_disp); |
| 615 |
} |
| 616 |
|
| 617 |
al_free(ogl_bitmap); |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static void ogl_bitmap_pointer_changed(ALLEGRO_BITMAP *bitmap, |
| 623 |
ALLEGRO_BITMAP *old) |
| 624 |
{ |
| 625 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra = bitmap->extra; |
| 626 |
if (extra && extra->fbo_info) { |
| 627 |
ASSERT(extra->fbo_info->owner == old); |
| 628 |
extra->fbo_info->owner = bitmap; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
static bool can_flip_blocks(ALLEGRO_PIXEL_FORMAT format) |
| 634 |
{ |
| 635 |
switch (format) { |
| 636 |
case ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT1: |
| 637 |
case ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT3: |
| 638 |
case ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT5: |
| 639 |
return true; |
| 640 |
default: |
| 641 |
return false; |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
static void ogl_flip_blocks(ALLEGRO_LOCKED_REGION *lr, int wc, int hc) |
| 647 |
{ |
| 648 |
#define SWAP(x, y) do { unsigned char t = x; x = y; y = t; } while (0) |
| 649 |
int x, y; |
| 650 |
unsigned char* data = lr->data; |
| 651 |
ASSERT(can_flip_blocks(lr->format)); |
| 652 |
switch (lr->format) { |
| 653 |
case ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT1: { |
| 654 |
for (y = 0; y < hc; y++) { |
| 655 |
unsigned char* row = data; |
| 656 |
for (x = 0; x < wc; x++) { |
| 657 |
/* Skip color table */ |
| 658 |
row += 4; |
| 659 |
|
| 660 |
/* Swap colors */ |
| 661 |
SWAP(row[0], row[3]); |
| 662 |
SWAP(row[2], row[1]); |
| 663 |
|
| 664 |
/* Skip bit-map */ |
| 665 |
row += 4; |
| 666 |
} |
| 667 |
data += lr->pitch; |
| 668 |
} |
| 669 |
break; |
| 670 |
} |
| 671 |
case ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT3: { |
| 672 |
for (y = 0; y < hc; y++) { |
| 673 |
unsigned char* row = data; |
| 674 |
for (x = 0; x < wc; x++) { |
| 675 |
/* Swap alpha */ |
| 676 |
SWAP(row[0], row[6]); |
| 677 |
SWAP(row[1], row[7]); |
| 678 |
SWAP(row[2], row[4]); |
| 679 |
SWAP(row[3], row[5]); |
| 680 |
|
| 681 |
/* Skip alpha bit-map */ |
| 682 |
row += 8; |
| 683 |
|
| 684 |
/* Skip color table */ |
| 685 |
row += 4; |
| 686 |
|
| 687 |
/* Swap colors */ |
| 688 |
SWAP(row[0], row[3]); |
| 689 |
SWAP(row[2], row[1]); |
| 690 |
|
| 691 |
/* Skip bit-map */ |
| 692 |
row += 4; |
| 693 |
} |
| 694 |
data += lr->pitch; |
| 695 |
} |
| 696 |
break; |
| 697 |
} |
| 698 |
case ALLEGRO_PIXEL_FORMAT_COMPRESSED_RGBA_DXT5: { |
| 699 |
for (y = 0; y < hc; y++) { |
| 700 |
unsigned char* row = data; |
| 701 |
for (x = 0; x < wc; x++) { |
| 702 |
uint16_t bit_row0, bit_row1, bit_row2, bit_row3; |
| 703 |
|
| 704 |
/* Skip the alpha table */ |
| 705 |
row += 2; |
| 706 |
|
| 707 |
bit_row0 = (((uint16_t)row[0]) | (uint16_t)row[1] << 8) << 4; |
| 708 |
bit_row1 = (((uint16_t)row[1]) | (uint16_t)row[2] << 8) >> 4; |
| 709 |
bit_row2 = (((uint16_t)row[3]) | (uint16_t)row[4] << 8) << 4; |
| 710 |
bit_row3 = (((uint16_t)row[4]) | (uint16_t)row[5] << 8) >> 4; |
| 711 |
|
| 712 |
row[0] = (unsigned char)(bit_row3 & 0x00ff); |
| 713 |
row[1] = (unsigned char)((bit_row2 & 0x00ff) | ((bit_row3 & 0xff00) >> 8)); |
| 714 |
row[2] = (unsigned char)((bit_row2 & 0xff00) >> 8); |
| 715 |
|
| 716 |
row[3] = (unsigned char)(bit_row1 & 0x00ff); |
| 717 |
row[4] = (unsigned char)((bit_row0 & 0x00ff) | ((bit_row1 & 0xff00) >> 8)); |
| 718 |
row[5] = (unsigned char)((bit_row0 & 0xff00) >> 8); |
| 719 |
|
| 720 |
/* Skip the alpha bit-map */ |
| 721 |
row += 6; |
| 722 |
|
| 723 |
/* Skip color table */ |
| 724 |
row += 4; |
| 725 |
|
| 726 |
/* Swap colors */ |
| 727 |
SWAP(row[0], row[3]); |
| 728 |
SWAP(row[2], row[1]); |
| 729 |
|
| 730 |
/* Skip bit-map */ |
| 731 |
row += 4; |
| 732 |
} |
| 733 |
data += lr->pitch; |
| 734 |
} |
| 735 |
break; |
| 736 |
} |
| 737 |
default: |
| 738 |
(void)x; |
| 739 |
(void)y; |
| 740 |
(void)data; |
| 741 |
(void)wc; |
| 742 |
(void)hc; |
| 743 |
} |
| 744 |
#undef SWAP |
| 745 |
} |
| 746 |
|
| 747 |
static ALLEGRO_LOCKED_REGION *ogl_lock_compressed_region(ALLEGRO_BITMAP *bitmap, |
| 748 |
int x, int y, int w, int h, int flags) |
| 749 |
{ |
| 750 |
#if !defined ALLEGRO_CFG_OPENGLES |
| 751 |
ALLEGRO_BITMAP_EXTRA_OPENGL *const ogl_bitmap = bitmap->extra; |
| 752 |
ALLEGRO_DISPLAY *disp; |
| 753 |
ALLEGRO_DISPLAY *old_disp = NULL; |
| 754 |
GLenum e; |
| 755 |
bool ok = true; |
| 756 |
int bitmap_format = al_get_bitmap_format(bitmap); |
| 757 |
int block_width = al_get_pixel_block_width(bitmap_format); |
| 758 |
int block_height = al_get_pixel_block_height(bitmap_format); |
| 759 |
int block_size = al_get_pixel_block_size(bitmap_format); |
| 760 |
int xc = x / block_width; |
| 761 |
int yc = y / block_width; |
| 762 |
int wc = w / block_width; |
| 763 |
int hc = h / block_width; |
| 764 |
int true_wc = ogl_bitmap->true_w / block_width; |
| 765 |
int true_hc = ogl_bitmap->true_h / block_height; |
| 766 |
int gl_yc = _al_get_least_multiple(bitmap->h, block_height) / block_height - yc - hc; |
| 767 |
|
| 768 |
if (!can_flip_blocks(bitmap_format)) { |
| 769 |
return NULL; |
| 770 |
} |
| 771 |
|
| 772 |
if (flags & ALLEGRO_LOCK_WRITEONLY) { |
| 773 |
int pitch = wc * block_size; |
| 774 |
ogl_bitmap->lock_buffer = al_malloc(pitch * hc); |
| 775 |
if (ogl_bitmap->lock_buffer == NULL) { |
| 776 |
return NULL; |
| 777 |
} |
| 778 |
|
| 779 |
bitmap->locked_region.data = ogl_bitmap->lock_buffer + pitch * (hc - 1); |
| 780 |
bitmap->locked_region.format = bitmap_format; |
| 781 |
bitmap->locked_region.pitch = -pitch; |
| 782 |
bitmap->locked_region.pixel_size = block_size; |
| 783 |
return &bitmap->locked_region; |
| 784 |
} |
| 785 |
|
| 786 |
disp = al_get_current_display(); |
| 787 |
|
| 788 |
/* Change OpenGL context if necessary. */ |
| 789 |
if (!disp || |
| 790 |
(_al_get_bitmap_display(bitmap)->ogl_extras->is_shared == false && |
| 791 |
_al_get_bitmap_display(bitmap) != disp)) |
| 792 |
{ |
| 793 |
old_disp = disp; |
| 794 |
_al_set_current_display_only(_al_get_bitmap_display(bitmap)); |
| 795 |
} |
| 796 |
|
| 797 |
/* Set up the pixel store state. We will need to match it when unlocking. |
| 798 |
* There may be other pixel store state we should be setting. |
| 799 |
* See also pitfalls 7 & 8 from: |
| 800 |
* http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/ |
| 801 |
*/ |
| 802 |
int previous_alignment; |
| 803 |
glGetIntegerv(GL_PACK_ALIGNMENT, &previous_alignment); |
| 804 |
if (previous_alignment != 1) { |
| 805 |
glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 806 |
e = glGetError(); |
| 807 |
if (e) { |
| 808 |
ALLEGRO_ERROR("glPixelStorei(GL_PACK_ALIGNMENT, %d) failed (%s).\n", |
| 809 |
1, _al_gl_error_string(e)); |
| 810 |
ok = false; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
if (ok) { |
| 815 |
ogl_bitmap->lock_buffer = al_malloc(true_wc * true_hc * block_size); |
| 816 |
|
| 817 |
if (ogl_bitmap->lock_buffer != NULL) { |
| 818 |
glBindTexture(GL_TEXTURE_2D, ogl_bitmap->texture); |
| 819 |
glGetCompressedTexImage(GL_TEXTURE_2D, 0, ogl_bitmap->lock_buffer); |
| 820 |
e = glGetError(); |
| 821 |
if (e) { |
| 822 |
ALLEGRO_ERROR("glGetCompressedTexImage for format %s failed (%s).\n", |
| 823 |
_al_pixel_format_name(bitmap_format), _al_gl_error_string(e)); |
| 824 |
al_free(ogl_bitmap->lock_buffer); |
| 825 |
ogl_bitmap->lock_buffer = NULL; |
| 826 |
ok = false; |
| 827 |
} |
| 828 |
else { |
| 829 |
if (flags == ALLEGRO_LOCK_READWRITE) { |
| 830 |
/* Need to make the locked memory contiguous, as |
| 831 |
* glCompressedTexSubImage2D cannot read strided |
| 832 |
* memory. */ |
| 833 |
int y; |
| 834 |
int src_pitch = true_wc * block_size; |
| 835 |
int dest_pitch = wc * block_size; |
| 836 |
char* dest_ptr = (char*)ogl_bitmap->lock_buffer; |
| 837 |
char* src_ptr = (char*)ogl_bitmap->lock_buffer + |
| 838 |
src_pitch * gl_yc + block_size * xc; |
| 839 |
for (y = 0; y < hc; y++) { |
| 840 |
memmove(dest_ptr, src_ptr, dest_pitch); |
| 841 |
src_ptr += src_pitch; |
| 842 |
dest_ptr += dest_pitch; |
| 843 |
} |
| 844 |
bitmap->locked_region.data = ogl_bitmap->lock_buffer + |
| 845 |
dest_pitch * (hc - 1); |
| 846 |
bitmap->locked_region.pitch = -dest_pitch; |
| 847 |
} |
| 848 |
else { |
| 849 |
int pitch = true_wc * block_size; |
| 850 |
bitmap->locked_region.data = ogl_bitmap->lock_buffer + |
| 851 |
pitch * (gl_yc + hc - 1) + block_size * xc; |
| 852 |
bitmap->locked_region.pitch = -pitch; |
| 853 |
} |
| 854 |
bitmap->locked_region.format = bitmap_format; |
| 855 |
bitmap->locked_region.pixel_size = block_size; |
| 856 |
} |
| 857 |
} |
| 858 |
else { |
| 859 |
ok = false; |
| 860 |
} |
| 861 |
} |
| 862 |
|
| 863 |
if (previous_alignment != 1) { |
| 864 |
glPixelStorei(GL_PACK_ALIGNMENT, previous_alignment); |
| 865 |
} |
| 866 |
|
| 867 |
if (old_disp != NULL) { |
| 868 |
_al_set_current_display_only(old_disp); |
| 869 |
} |
| 870 |
|
| 871 |
if (ok) { |
| 872 |
ogl_flip_blocks(&bitmap->locked_region, wc, hc); |
| 873 |
return &bitmap->locked_region; |
| 874 |
} |
| 875 |
|
| 876 |
ALLEGRO_ERROR("Failed to lock region\n"); |
| 877 |
ASSERT(ogl_bitmap->lock_buffer == NULL); |
| 878 |
return NULL; |
| 879 |
#else |
| 880 |
(void)bitmap; |
| 881 |
(void)x; |
| 882 |
(void)y; |
| 883 |
(void)w; |
| 884 |
(void)h; |
| 885 |
(void)flags; |
| 886 |
return NULL; |
| 887 |
#endif |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
static void ogl_unlock_compressed_region(ALLEGRO_BITMAP *bitmap) |
| 892 |
{ |
| 893 |
#if !defined ALLEGRO_CFG_OPENGLES |
| 894 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = bitmap->extra; |
| 895 |
int lock_format = bitmap->locked_region.format; |
| 896 |
ALLEGRO_DISPLAY *old_disp = NULL; |
| 897 |
ALLEGRO_DISPLAY *disp; |
| 898 |
GLenum e; |
| 899 |
int block_size = al_get_pixel_block_size(lock_format); |
| 900 |
int block_width = al_get_pixel_block_width(lock_format); |
| 901 |
int block_height = al_get_pixel_block_height(lock_format); |
| 902 |
int data_size = bitmap->lock_h * bitmap->lock_w / |
| 903 |
(block_width * block_height) * block_size; |
| 904 |
int gl_y = _al_get_least_multiple(bitmap->h, block_height) - bitmap->lock_y - bitmap->lock_h; |
| 905 |
|
| 906 |
/* It shouldn't be possible for this to fail, as we wouldn't have been able |
| 907 |
* to lock earlier */ |
| 908 |
ASSERT(can_flip_blocks(bitmap->locked_region.format)); |
| 909 |
|
| 910 |
if ((bitmap->lock_flags & ALLEGRO_LOCK_READONLY)) { |
| 911 |
goto EXIT; |
| 912 |
} |
| 913 |
|
| 914 |
ogl_flip_blocks(&bitmap->locked_region, bitmap->lock_w / block_width, |
| 915 |
bitmap->lock_h / block_height); |
| 916 |
|
| 917 |
disp = al_get_current_display(); |
| 918 |
|
| 919 |
/* Change OpenGL context if necessary. */ |
| 920 |
if (!disp || |
| 921 |
(_al_get_bitmap_display(bitmap)->ogl_extras->is_shared == false && |
| 922 |
_al_get_bitmap_display(bitmap) != disp)) |
| 923 |
{ |
| 924 |
old_disp = disp; |
| 925 |
_al_set_current_display_only(_al_get_bitmap_display(bitmap)); |
| 926 |
} |
| 927 |
|
| 928 |
/* Keep this in sync with ogl_lock_compressed_region. */ |
| 929 |
int previous_alignment; |
| 930 |
glGetIntegerv(GL_UNPACK_ALIGNMENT, &previous_alignment); |
| 931 |
if (previous_alignment != 1) { |
| 932 |
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 933 |
e = glGetError(); |
| 934 |
if (e) { |
| 935 |
ALLEGRO_ERROR("glPixelStorei(GL_UNPACK_ALIGNMENT, %d) failed (%s).\n", |
| 936 |
1, _al_gl_error_string(e)); |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
glBindTexture(GL_TEXTURE_2D, ogl_bitmap->texture); |
| 941 |
glCompressedTexSubImage2D(GL_TEXTURE_2D, 0, |
| 942 |
bitmap->lock_x, gl_y, |
| 943 |
bitmap->lock_w, bitmap->lock_h, |
| 944 |
get_glformat(lock_format, 0), |
| 945 |
data_size, |
| 946 |
ogl_bitmap->lock_buffer); |
| 947 |
|
| 948 |
e = glGetError(); |
| 949 |
if (e) { |
| 950 |
ALLEGRO_ERROR("glCompressedTexSubImage2D for format %s failed (%s).\n", |
| 951 |
_al_pixel_format_name(lock_format), _al_gl_error_string(e)); |
| 952 |
} |
| 953 |
|
| 954 |
if (previous_alignment != 1) { |
| 955 |
glPixelStorei(GL_UNPACK_ALIGNMENT, previous_alignment); |
| 956 |
} |
| 957 |
|
| 958 |
if (old_disp) { |
| 959 |
_al_set_current_display_only(old_disp); |
| 960 |
} |
| 961 |
|
| 962 |
EXIT: |
| 963 |
al_free(ogl_bitmap->lock_buffer); |
| 964 |
ogl_bitmap->lock_buffer = NULL; |
| 965 |
#else |
| 966 |
(void)bitmap; |
| 967 |
#endif |
| 968 |
} |
| 969 |
|
| 970 |
static void ogl_backup_dirty_bitmap(ALLEGRO_BITMAP *b) |
| 971 |
{ |
| 972 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = b->extra; |
| 973 |
ALLEGRO_LOCKED_REGION *lr; |
| 974 |
int bitmap_flags = al_get_bitmap_flags(b); |
| 975 |
|
| 976 |
if (b->parent) |
| 977 |
return; |
| 978 |
|
| 979 |
if ((bitmap_flags & ALLEGRO_MEMORY_BITMAP) || |
| 980 |
(bitmap_flags & ALLEGRO_NO_PRESERVE_TEXTURE) || |
| 981 |
!b->dirty || |
| 982 |
ogl_bitmap->is_backbuffer) |
| 983 |
return; |
| 984 |
|
| 985 |
ALLEGRO_DEBUG("Backing up dirty bitmap %p\n", b); |
| 986 |
|
| 987 |
lr = al_lock_bitmap( |
| 988 |
b, |
| 989 |
_al_get_bitmap_memory_format(b), |
| 990 |
ALLEGRO_LOCK_READONLY |
| 991 |
); |
| 992 |
|
| 993 |
if (lr) { |
| 994 |
int line_size = al_get_pixel_size(lr->format) * b->w; |
| 995 |
int y; |
| 996 |
for (y = 0; y < b->h; y++) { |
| 997 |
unsigned char *p = ((unsigned char *)lr->data) + lr->pitch * y; |
| 998 |
unsigned char *p2; |
| 999 |
p2 = ((unsigned char *)b->memory) + line_size * (b->h-1-y); |
| 1000 |
memcpy(p2, p, line_size); |
| 1001 |
} |
| 1002 |
al_unlock_bitmap(b); |
| 1003 |
b->dirty = false; |
| 1004 |
} |
| 1005 |
else { |
| 1006 |
ALLEGRO_WARN("Failed to lock dirty bitmap %p\n", b); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
|
| 1010 |
/* Obtain a reference to this driver. */ |
| 1011 |
static ALLEGRO_BITMAP_INTERFACE *ogl_bitmap_driver(void) |
| 1012 |
{ |
| 1013 |
if (glbmp_vt.draw_bitmap_region) { |
| 1014 |
return &glbmp_vt; |
| 1015 |
} |
| 1016 |
|
| 1017 |
glbmp_vt.draw_bitmap_region = ogl_draw_bitmap_region; |
| 1018 |
glbmp_vt.upload_bitmap = ogl_upload_bitmap; |
| 1019 |
glbmp_vt.update_clipping_rectangle = ogl_update_clipping_rectangle; |
| 1020 |
glbmp_vt.destroy_bitmap = ogl_destroy_bitmap; |
| 1021 |
glbmp_vt.bitmap_pointer_changed = ogl_bitmap_pointer_changed; |
| 1022 |
#if defined(ALLEGRO_CFG_OPENGLES) |
| 1023 |
glbmp_vt.lock_region = _al_ogl_lock_region_gles; |
| 1024 |
glbmp_vt.unlock_region = _al_ogl_unlock_region_gles; |
| 1025 |
#else |
| 1026 |
glbmp_vt.lock_region = _al_ogl_lock_region_new; |
| 1027 |
glbmp_vt.unlock_region = _al_ogl_unlock_region_new; |
| 1028 |
#endif |
| 1029 |
glbmp_vt.lock_compressed_region = ogl_lock_compressed_region; |
| 1030 |
glbmp_vt.unlock_compressed_region = ogl_unlock_compressed_region; |
| 1031 |
glbmp_vt.backup_dirty_bitmap = ogl_backup_dirty_bitmap; |
| 1032 |
|
| 1033 |
return &glbmp_vt; |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
ALLEGRO_BITMAP *_al_ogl_create_bitmap(ALLEGRO_DISPLAY *d, int w, int h, |
| 1039 |
int format, int flags) |
| 1040 |
{ |
| 1041 |
ALLEGRO_BITMAP *bitmap; |
| 1042 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra; |
| 1043 |
int true_w; |
| 1044 |
int true_h; |
| 1045 |
int block_width; |
| 1046 |
int block_height; |
| 1047 |
ALLEGRO_SYSTEM *system = al_get_system_driver(); |
| 1048 |
(void)d; |
| 1049 |
|
| 1050 |
format = _al_get_real_pixel_format(d, format); |
| 1051 |
ASSERT(_al_pixel_format_is_real(format)); |
| 1052 |
|
| 1053 |
block_width = al_get_pixel_block_width(format); |
| 1054 |
block_height = al_get_pixel_block_width(format); |
| 1055 |
true_w = _al_get_least_multiple(w, block_width); |
| 1056 |
true_h = _al_get_least_multiple(h, block_height); |
| 1057 |
|
| 1058 |
if (_al_pixel_format_is_compressed(format)) { |
| 1059 |
if (!al_get_opengl_extension_list()->ALLEGRO_GL_EXT_texture_compression_s3tc) { |
| 1060 |
ALLEGRO_DEBUG("Device does not support S3TC compressed textures.\n"); |
| 1061 |
return NULL; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
|
| 1065 |
if (!d->extra_settings.settings[ALLEGRO_SUPPORT_NPOT_BITMAP]) { |
| 1066 |
true_w = pot(true_w); |
| 1067 |
true_h = pot(true_h); |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* This used to be an iOS/Android only workaround - but |
| 1071 |
* Intel is making GPUs with the same chips now. Very |
| 1072 |
* small textures can have garbage pixels and FBOs don't |
| 1073 |
* work with them on some of these chips. This is a |
| 1074 |
* workaround. |
| 1075 |
*/ |
| 1076 |
if (true_w < system->min_bitmap_size) true_w = system->min_bitmap_size; |
| 1077 |
if (true_h < system->min_bitmap_size) true_h = system->min_bitmap_size; |
| 1078 |
|
| 1079 |
/* glReadPixels requires 32 byte aligned rows */ |
| 1080 |
if (IS_ANDROID) { |
| 1081 |
int mod = true_w % 32; |
| 1082 |
if (mod != 0) { |
| 1083 |
true_w += 32 - mod; |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|
| 1087 |
ASSERT(true_w % block_width == 0); |
| 1088 |
ASSERT(true_h % block_height == 0); |
| 1089 |
|
| 1090 |
bitmap = al_calloc(1, sizeof *bitmap); |
| 1091 |
ASSERT(bitmap); |
| 1092 |
bitmap->extra = al_calloc(1, sizeof(ALLEGRO_BITMAP_EXTRA_OPENGL)); |
| 1093 |
ASSERT(bitmap->extra); |
| 1094 |
extra = bitmap->extra; |
| 1095 |
|
| 1096 |
bitmap->vt = ogl_bitmap_driver(); |
| 1097 |
bitmap->_memory_format = |
| 1098 |
_al_pixel_format_is_compressed(format) ? ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE : format; |
| 1099 |
bitmap->pitch = true_w * al_get_pixel_size(bitmap->_memory_format); |
| 1100 |
bitmap->_format = format; |
| 1101 |
bitmap->_flags = flags | _ALLEGRO_INTERNAL_OPENGL; |
| 1102 |
|
| 1103 |
extra->true_w = true_w; |
| 1104 |
extra->true_h = true_h; |
| 1105 |
|
| 1106 |
if (!(flags & ALLEGRO_NO_PRESERVE_TEXTURE)) { |
| 1107 |
bitmap->memory = al_calloc(1, al_get_pixel_size(bitmap->_memory_format)*w*h); |
| 1108 |
} |
| 1109 |
|
| 1110 |
return bitmap; |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
/* lets you setup the memory pointer to skip a lock/unlock copy |
| 1116 |
* if it's unnecessary |
| 1117 |
* 'ptr' should be tightly packed or NULL if no texture data |
| 1118 |
* upload is desired. |
| 1119 |
*/ |
| 1120 |
void _al_ogl_upload_bitmap_memory(ALLEGRO_BITMAP *bitmap, int format, void *ptr) |
| 1121 |
{ |
| 1122 |
int w = bitmap->w; |
| 1123 |
int h = bitmap->h; |
| 1124 |
int pixsize = al_get_pixel_size(format); |
| 1125 |
int y; |
| 1126 |
ALLEGRO_BITMAP *tmp; |
| 1127 |
ALLEGRO_LOCKED_REGION *lr; |
| 1128 |
uint8_t *dst; |
| 1129 |
uint8_t *src; |
| 1130 |
|
| 1131 |
ASSERT(al_get_current_display() == _al_get_bitmap_display(bitmap)); |
| 1132 |
|
| 1133 |
tmp = _al_create_bitmap_params(_al_get_bitmap_display(bitmap), w, h, format, |
| 1134 |
al_get_bitmap_flags(bitmap), 0, 0); |
| 1135 |
ASSERT(tmp); |
| 1136 |
|
| 1137 |
if (ptr != NULL) { |
| 1138 |
lr = al_lock_bitmap(tmp, format, ALLEGRO_LOCK_WRITEONLY); |
| 1139 |
ASSERT(lr); |
| 1140 |
|
| 1141 |
dst = (uint8_t *)lr->data; |
| 1142 |
// we need to flip it |
| 1143 |
src = ((uint8_t *)ptr) + (pixsize * w * (h-1)); |
| 1144 |
|
| 1145 |
for (y = 0; y < h; y++) { |
| 1146 |
memcpy(dst, src, pixsize * w); |
| 1147 |
dst += lr->pitch; |
| 1148 |
src -= pixsize * w; // minus because it's flipped |
| 1149 |
} |
| 1150 |
|
| 1151 |
al_unlock_bitmap(tmp); |
| 1152 |
} |
| 1153 |
|
| 1154 |
((ALLEGRO_BITMAP_EXTRA_OPENGL *)bitmap->extra)->texture = |
| 1155 |
((ALLEGRO_BITMAP_EXTRA_OPENGL *)tmp->extra)->texture; |
| 1156 |
((ALLEGRO_BITMAP_EXTRA_OPENGL *)tmp->extra)->texture = 0; |
| 1157 |
al_destroy_bitmap(tmp); |
| 1158 |
} |
| 1159 |
|
| 1160 |
/* Function: al_get_opengl_texture |
| 1161 |
*/ |
| 1162 |
GLuint al_get_opengl_texture(ALLEGRO_BITMAP *bitmap) |
| 1163 |
{ |
| 1164 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra; |
| 1165 |
if (bitmap->parent) |
| 1166 |
bitmap = bitmap->parent; |
| 1167 |
if (!(al_get_bitmap_flags(bitmap) & _ALLEGRO_INTERNAL_OPENGL)) |
| 1168 |
return 0; |
| 1169 |
extra = bitmap->extra; |
| 1170 |
return extra->texture; |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* Function: al_remove_opengl_fbo |
| 1174 |
*/ |
| 1175 |
void al_remove_opengl_fbo(ALLEGRO_BITMAP *bitmap) |
| 1176 |
{ |
| 1177 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap; |
| 1178 |
if (bitmap->parent) |
| 1179 |
bitmap = bitmap->parent; |
| 1180 |
if (!(al_get_bitmap_flags(bitmap) & _ALLEGRO_INTERNAL_OPENGL)) |
| 1181 |
return; |
| 1182 |
ogl_bitmap = bitmap->extra; |
| 1183 |
if (!ogl_bitmap->fbo_info) |
| 1184 |
return; |
| 1185 |
|
| 1186 |
ASSERT(ogl_bitmap->fbo_info->fbo_state > FBO_INFO_UNUSED); |
| 1187 |
ASSERT(ogl_bitmap->fbo_info->fbo != 0); |
| 1188 |
|
| 1189 |
ALLEGRO_FBO_INFO *info = ogl_bitmap->fbo_info; |
| 1190 |
_al_ogl_del_fbo(info); |
| 1191 |
|
| 1192 |
if (info->fbo_state == FBO_INFO_PERSISTENT) { |
| 1193 |
al_free(info); |
| 1194 |
} |
| 1195 |
else { |
| 1196 |
_al_ogl_reset_fbo_info(info); |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
/* Function: al_get_opengl_fbo |
| 1201 |
*/ |
| 1202 |
GLuint al_get_opengl_fbo(ALLEGRO_BITMAP *bitmap) |
| 1203 |
{ |
| 1204 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap; |
| 1205 |
if (bitmap->parent) |
| 1206 |
bitmap = bitmap->parent; |
| 1207 |
|
| 1208 |
if (!(al_get_bitmap_flags(bitmap) & _ALLEGRO_INTERNAL_OPENGL)) |
| 1209 |
return 0; |
| 1210 |
|
| 1211 |
ogl_bitmap = bitmap->extra; |
| 1212 |
|
| 1213 |
if (!ogl_bitmap->fbo_info) { |
| 1214 |
if (!_al_ogl_create_persistent_fbo(bitmap)) { |
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
if (ogl_bitmap->fbo_info->fbo_state == FBO_INFO_TRANSIENT) { |
| 1220 |
ogl_bitmap->fbo_info = _al_ogl_persist_fbo(_al_get_bitmap_display(bitmap), |
| 1221 |
ogl_bitmap->fbo_info); |
| 1222 |
} |
| 1223 |
return ogl_bitmap->fbo_info->fbo; |
| 1224 |
} |
| 1225 |
|
| 1226 |
/* Function: al_get_opengl_texture_size |
| 1227 |
*/ |
| 1228 |
bool al_get_opengl_texture_size(ALLEGRO_BITMAP *bitmap, int *w, int *h) |
| 1229 |
{ |
| 1230 |
/* The designers of OpenGL ES 1.0 forgot to add a function to query |
| 1231 |
* texture sizes, so this will be the only way there to get the texture |
| 1232 |
* size. On normal OpenGL also glGetTexLevelParameter could be used. |
| 1233 |
*/ |
| 1234 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap; |
| 1235 |
if (bitmap->parent) |
| 1236 |
bitmap = bitmap->parent; |
| 1237 |
|
| 1238 |
if (!(al_get_bitmap_flags(bitmap) & _ALLEGRO_INTERNAL_OPENGL)) { |
| 1239 |
*w = 0; |
| 1240 |
*h = 0; |
| 1241 |
return false; |
| 1242 |
} |
| 1243 |
|
| 1244 |
ogl_bitmap = bitmap->extra; |
| 1245 |
*w = ogl_bitmap->true_w; |
| 1246 |
*h = ogl_bitmap->true_h; |
| 1247 |
return true; |
| 1248 |
} |
| 1249 |
|
| 1250 |
/* Function: al_get_opengl_texture_position |
| 1251 |
*/ |
| 1252 |
void al_get_opengl_texture_position(ALLEGRO_BITMAP *bitmap, int *u, int *v) |
| 1253 |
{ |
| 1254 |
ASSERT(bitmap); |
| 1255 |
ASSERT(u); |
| 1256 |
ASSERT(v); |
| 1257 |
|
| 1258 |
*u = bitmap->xofs; |
| 1259 |
*v = bitmap->yofs; |
| 1260 |
} |
| 1261 |
|
| 1262 |
/* vim: set sts=3 sw=3 et: */ |