| 1 |
/* ______ ___ ___ |
| 2 |
* /\ _ \ /\_ \ /\_ \ |
| 3 |
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___ |
| 4 |
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\ |
| 5 |
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \ |
| 6 |
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/ |
| 7 |
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/ |
| 8 |
* /\____/ |
| 9 |
* \_/__/ |
| 10 |
* |
| 11 |
* OpenGL framebuffer objects. |
| 12 |
* |
| 13 |
* See LICENSE.txt for copyright information. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <float.h> |
| 17 |
|
| 18 |
#include "allegro5/allegro.h" |
| 19 |
#include "allegro5/allegro_opengl.h" |
| 20 |
#include "allegro5/internal/aintern.h" |
| 21 |
#include "allegro5/internal/aintern_opengl.h" |
| 22 |
#include "allegro5/internal/aintern_pixels.h" |
| 23 |
|
| 24 |
#ifdef ALLEGRO_ANDROID |
| 25 |
#include <android/api-level.h> |
| 26 |
#include "allegro5/internal/aintern_android.h" |
| 27 |
#elif defined ALLEGRO_IPHONE |
| 28 |
#include "allegro5/internal/aintern_iphone.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "ogl_helpers.h" |
| 32 |
|
| 33 |
ALLEGRO_DEBUG_CHANNEL("opengl") |
| 34 |
|
| 35 |
|
| 36 |
/* forward declarations */ |
| 37 |
static void setup_fbo_backbuffer(ALLEGRO_DISPLAY *display, |
| 38 |
ALLEGRO_BITMAP *bitmap); |
| 39 |
static void use_fbo_for_bitmap(ALLEGRO_DISPLAY *display, |
| 40 |
ALLEGRO_BITMAP *bitmap, ALLEGRO_FBO_INFO *info); |
| 41 |
|
| 42 |
|
| 43 |
/* glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT..) not supported on some Androids. |
| 44 |
* We keep track of it manually. |
| 45 |
*/ |
| 46 |
#ifdef ALLEGRO_ANDROID |
| 47 |
|
| 48 |
static GLint _al_gl_curr_fbo = 0; |
| 49 |
|
| 50 |
GLint _al_android_get_curr_fbo(void) |
| 51 |
{ |
| 52 |
return _al_gl_curr_fbo; |
| 53 |
} |
| 54 |
|
| 55 |
void _al_android_set_curr_fbo(GLint fbo) |
| 56 |
{ |
| 57 |
_al_gl_curr_fbo = fbo; |
| 58 |
} |
| 59 |
|
| 60 |
GLint _al_ogl_bind_framebuffer(GLint fbo) |
| 61 |
{ |
| 62 |
GLint old_fbo = _al_android_get_curr_fbo(); |
| 63 |
GLint e; |
| 64 |
|
| 65 |
if (ANDROID_PROGRAMMABLE_PIPELINE(al_get_current_display())) { |
| 66 |
glBindFramebuffer(GL_FRAMEBUFFER, fbo); |
| 67 |
} |
| 68 |
else { |
| 69 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); |
| 70 |
} |
| 71 |
e = glGetError(); |
| 72 |
if (e) { |
| 73 |
ALLEGRO_DEBUG("glBindFramebufferEXT failed (%s)", |
| 74 |
_al_gl_error_string(e)); |
| 75 |
} |
| 76 |
_al_android_set_curr_fbo(fbo); |
| 77 |
return old_fbo; |
| 78 |
} |
| 79 |
|
| 80 |
#else /* !ALLEGRO_ANDROID */ |
| 81 |
|
| 82 |
GLint _al_ogl_bind_framebuffer(GLint fbo) |
| 83 |
{ |
| 84 |
GLint old_fbo; |
| 85 |
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &old_fbo); |
| 86 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); |
| 87 |
return old_fbo; |
| 88 |
} |
| 89 |
|
| 90 |
#endif /* !ALLEGRO_ANDROID */ |
| 91 |
|
| 92 |
|
| 93 |
void _al_ogl_reset_fbo_info(ALLEGRO_FBO_INFO *info) |
| 94 |
{ |
| 95 |
info->fbo_state = FBO_INFO_UNUSED; |
| 96 |
info->fbo = 0; |
| 97 |
info->buffers.depth_buffer = 0; |
| 98 |
info->buffers.multisample_buffer = 0; |
| 99 |
info->buffers.dw = 0; |
| 100 |
info->buffers.dh = 0; |
| 101 |
info->buffers.mw = 0; |
| 102 |
info->buffers.mh = 0; |
| 103 |
info->owner = NULL; |
| 104 |
info->last_use_time = 0.0; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
#if (!defined ALLEGRO_CFG_OPENGLES) || defined ALLEGRO_CFG_OPENGLES3 |
| 109 |
static void check_gl_error(void) |
| 110 |
{ |
| 111 |
GLint e = glGetError(); |
| 112 |
if (e) { |
| 113 |
ALLEGRO_ERROR("OpenGL call failed! (%s)\n", |
| 114 |
_al_gl_error_string(e)); |
| 115 |
} |
| 116 |
} |
| 117 |
#endif |
| 118 |
|
| 119 |
|
| 120 |
static void detach_depth_buffer(ALLEGRO_FBO_INFO *info) |
| 121 |
{ |
| 122 |
#ifndef ALLEGRO_RASPBERRYPI |
| 123 |
if (info->buffers.depth_buffer == 0) |
| 124 |
return; |
| 125 |
ALLEGRO_DEBUG("Deleting depth render buffer: %u\n", |
| 126 |
info->buffers.depth_buffer); |
| 127 |
glDeleteRenderbuffersEXT(1, &info->buffers.depth_buffer); |
| 128 |
info->buffers.depth_buffer = 0; |
| 129 |
info->buffers.dw = 0; |
| 130 |
info->buffers.dh = 0; |
| 131 |
info->buffers.depth = 0; |
| 132 |
#endif |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static void detach_multisample_buffer(ALLEGRO_FBO_INFO *info) |
| 137 |
{ |
| 138 |
#ifndef ALLEGRO_RASPBERRYPI |
| 139 |
if (info->buffers.multisample_buffer == 0) |
| 140 |
return; |
| 141 |
ALLEGRO_DEBUG("Deleting multisample render buffer: %u\n", |
| 142 |
info->buffers.depth_buffer); |
| 143 |
glDeleteRenderbuffersEXT(1, &info->buffers.multisample_buffer); |
| 144 |
info->buffers.multisample_buffer = 0; |
| 145 |
info->buffers.mw = 0; |
| 146 |
info->buffers.mh = 0; |
| 147 |
info->buffers.samples = 0; |
| 148 |
#endif |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static void attach_depth_buffer(ALLEGRO_FBO_INFO *info) |
| 154 |
{ |
| 155 |
#if !defined ALLEGRO_RASPBERRYPI |
| 156 |
GLuint rb; |
| 157 |
GLenum gldepth = GL_DEPTH_COMPONENT16; |
| 158 |
|
| 159 |
ALLEGRO_BITMAP *b = info->owner; |
| 160 |
int bits = al_get_bitmap_depth(b); |
| 161 |
|
| 162 |
if (info->buffers.depth_buffer != 0) { |
| 163 |
|
| 164 |
if (info->buffers.depth != bits || |
| 165 |
info->buffers.dw != al_get_bitmap_width(b) || |
| 166 |
info->buffers.dh != al_get_bitmap_height(b)) { |
| 167 |
detach_depth_buffer(info); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if (!bits) |
| 172 |
return; |
| 173 |
|
| 174 |
if (info->buffers.depth_buffer == 0) { |
| 175 |
ALLEGRO_DISPLAY *display = _al_get_bitmap_display(info->owner); |
| 176 |
int w = al_get_bitmap_width(info->owner); |
| 177 |
int h = al_get_bitmap_height(info->owner); |
| 178 |
|
| 179 |
#if !defined ALLEGRO_CFG_OPENGLES || defined ALLEGRO_CFG_OPENGLES3 |
| 180 |
if (bits == 24) gldepth = GL_DEPTH_COMPONENT24; |
| 181 |
#endif |
| 182 |
|
| 183 |
glGenRenderbuffersEXT(1, &rb); |
| 184 |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb); |
| 185 |
|
| 186 |
int samples = al_get_bitmap_samples(info->owner); |
| 187 |
|
| 188 |
bool extension_supported; |
| 189 |
#ifdef ALLEGRO_CFG_OPENGLES |
| 190 |
(void)display; |
| 191 |
extension_supported = al_have_opengl_extension("EXT_multisampled_render_to_texture"); |
| 192 |
#else |
| 193 |
extension_supported = display->ogl_extras->extension_list->ALLEGRO_GL_EXT_framebuffer_multisample; |
| 194 |
#endif |
| 195 |
|
| 196 |
if (samples == 0 || !extension_supported) |
| 197 |
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, gldepth, w, h); |
| 198 |
#if !defined ALLEGRO_CFG_OPENGLES || defined ALLEGRO_CFG_OPENGLES3 |
| 199 |
else |
| 200 |
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, |
| 201 |
samples, gldepth, w, h); |
| 202 |
#else |
| 203 |
else { |
| 204 |
return; |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
info->buffers.depth_buffer = rb; |
| 209 |
info->buffers.dw = w; |
| 210 |
info->buffers.dh = h; |
| 211 |
info->buffers.depth = bits; |
| 212 |
GLint e = glGetError(); |
| 213 |
if (e) { |
| 214 |
ALLEGRO_ERROR("glRenderbufferStorage failed! bits=%d w=%d h=%d (%s)\n", |
| 215 |
bits, w, h, _al_gl_error_string(e)); |
| 216 |
} |
| 217 |
else { |
| 218 |
ALLEGRO_DEBUG("Depth render buffer created: %u\n", |
| 219 |
info->buffers.depth_buffer); |
| 220 |
} |
| 221 |
|
| 222 |
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, |
| 223 |
GL_RENDERBUFFER_EXT, rb); |
| 224 |
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 225 |
ALLEGRO_ERROR("attaching depth renderbuffer failed\n"); |
| 226 |
} |
| 227 |
|
| 228 |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); |
| 229 |
} |
| 230 |
#endif |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
static void attach_multisample_buffer(ALLEGRO_FBO_INFO *info) |
| 235 |
{ |
| 236 |
#if !defined ALLEGRO_CFG_OPENGLES || defined ALLEGRO_CFG_OPENGLES3 |
| 237 |
ALLEGRO_BITMAP *b = info->owner; |
| 238 |
int samples = al_get_bitmap_samples(b); |
| 239 |
|
| 240 |
if (info->buffers.multisample_buffer != 0) { |
| 241 |
|
| 242 |
if (info->buffers.samples != samples || |
| 243 |
info->buffers.mw != al_get_bitmap_width(b) || |
| 244 |
info->buffers.mh != al_get_bitmap_height(b)) { |
| 245 |
detach_multisample_buffer(info); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
if (!samples) |
| 250 |
return; |
| 251 |
ALLEGRO_DISPLAY *display = _al_get_bitmap_display(info->owner); |
| 252 |
if (!display->ogl_extras->extension_list->ALLEGRO_GL_EXT_framebuffer_multisample) |
| 253 |
return; |
| 254 |
|
| 255 |
#ifdef ALLEGRO_CFG_OPENGLES |
| 256 |
(void)display; |
| 257 |
#else |
| 258 |
|
| 259 |
if (info->buffers.multisample_buffer == 0) { |
| 260 |
GLuint rb; |
| 261 |
GLint e; |
| 262 |
int w = al_get_bitmap_width(info->owner); |
| 263 |
int h = al_get_bitmap_height(info->owner); |
| 264 |
|
| 265 |
glGenRenderbuffersEXT(1, &rb); |
| 266 |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, rb); |
| 267 |
check_gl_error(); |
| 268 |
|
| 269 |
glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, |
| 270 |
samples, _al_ogl_get_glformat( |
| 271 |
al_get_bitmap_format(info->owner), 0), w, h); |
| 272 |
info->buffers.multisample_buffer = rb; |
| 273 |
info->buffers.mw = w; |
| 274 |
info->buffers.mh = h; |
| 275 |
info->buffers.samples = samples; |
| 276 |
e = glGetError(); |
| 277 |
if (e) { |
| 278 |
ALLEGRO_ERROR("glRenderbufferStorage failed! samples=%d w=%d h=%d (%s)\n", |
| 279 |
samples, w, h, _al_gl_error_string(e)); |
| 280 |
} |
| 281 |
else { |
| 282 |
ALLEGRO_DEBUG("Multisample render buffer created: %u\n", |
| 283 |
info->buffers.multisample_buffer); |
| 284 |
} |
| 285 |
|
| 286 |
glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, |
| 287 |
GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, rb); |
| 288 |
|
| 289 |
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT) { |
| 290 |
ALLEGRO_ERROR("attaching multisample renderbuffer failed\n"); |
| 291 |
} |
| 292 |
|
| 293 |
glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); |
| 294 |
} |
| 295 |
#endif |
| 296 |
#else |
| 297 |
(void)info; |
| 298 |
#endif |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
bool _al_ogl_create_persistent_fbo(ALLEGRO_BITMAP *bitmap) |
| 303 |
{ |
| 304 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap; |
| 305 |
ALLEGRO_FBO_INFO *info; |
| 306 |
GLint old_fbo, e; |
| 307 |
|
| 308 |
if (bitmap->parent) |
| 309 |
bitmap = bitmap->parent; |
| 310 |
ogl_bitmap = bitmap->extra; |
| 311 |
|
| 312 |
/* Don't continue if the bitmap does not belong to the current display. */ |
| 313 |
if (_al_get_bitmap_display(bitmap)->ogl_extras->is_shared == false && |
| 314 |
_al_get_bitmap_display(bitmap) != al_get_current_display()) { |
| 315 |
return false; |
| 316 |
} |
| 317 |
|
| 318 |
if (ogl_bitmap->is_backbuffer) { |
| 319 |
return false; |
| 320 |
} |
| 321 |
|
| 322 |
ASSERT(!ogl_bitmap->fbo_info); |
| 323 |
|
| 324 |
info = al_malloc(sizeof(ALLEGRO_FBO_INFO)); |
| 325 |
info->owner = bitmap; |
| 326 |
if (ANDROID_PROGRAMMABLE_PIPELINE(al_get_current_display())) { |
| 327 |
glGenFramebuffers(1, &info->fbo); |
| 328 |
} |
| 329 |
else { |
| 330 |
glGenFramebuffersEXT(1, &info->fbo); |
| 331 |
} |
| 332 |
if (info->fbo == 0) { |
| 333 |
al_free(info); |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
old_fbo = _al_ogl_bind_framebuffer(info->fbo); |
| 338 |
|
| 339 |
if (ANDROID_PROGRAMMABLE_PIPELINE(al_get_current_display())) { |
| 340 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 341 |
GL_TEXTURE_2D, ogl_bitmap->texture, 0); |
| 342 |
} |
| 343 |
else { |
| 344 |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 345 |
GL_TEXTURE_2D, ogl_bitmap->texture, 0); |
| 346 |
} |
| 347 |
|
| 348 |
e = glGetError(); |
| 349 |
if (e) { |
| 350 |
ALLEGRO_DEBUG("glFrameBufferTexture2DEXT failed! fbo=%d texture=%d (%s)\n", |
| 351 |
info->fbo, ogl_bitmap->texture, _al_gl_error_string(e)); |
| 352 |
} |
| 353 |
|
| 354 |
attach_depth_buffer(info); |
| 355 |
|
| 356 |
/* You'll see this a couple times in this file: some ES 1.1 functions aren't |
| 357 |
* implemented on Android. This is an ugly workaround. |
| 358 |
*/ |
| 359 |
if (UNLESS_ANDROID_OR_RPI( |
| 360 |
glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)) |
| 361 |
{ |
| 362 |
ALLEGRO_ERROR("FBO incomplete.\n"); |
| 363 |
_al_ogl_bind_framebuffer(old_fbo); |
| 364 |
glDeleteFramebuffersEXT(1, &info->fbo); |
| 365 |
al_free(info); |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
_al_ogl_bind_framebuffer(old_fbo); |
| 370 |
|
| 371 |
info->fbo_state = FBO_INFO_PERSISTENT; |
| 372 |
info->last_use_time = al_get_time(); |
| 373 |
ogl_bitmap->fbo_info = info; |
| 374 |
ALLEGRO_DEBUG("Persistent FBO: %u\n", info->fbo); |
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
ALLEGRO_FBO_INFO *_al_ogl_persist_fbo(ALLEGRO_DISPLAY *display, |
| 380 |
ALLEGRO_FBO_INFO *transient_fbo_info) |
| 381 |
{ |
| 382 |
ALLEGRO_OGL_EXTRAS *extras = display->ogl_extras; |
| 383 |
int i; |
| 384 |
ASSERT(transient_fbo_info->fbo_state == FBO_INFO_TRANSIENT); |
| 385 |
|
| 386 |
for (i = 0; i < ALLEGRO_MAX_OPENGL_FBOS; i++) { |
| 387 |
if (transient_fbo_info == &extras->fbos[i]) { |
| 388 |
ALLEGRO_FBO_INFO *new_info = al_malloc(sizeof(ALLEGRO_FBO_INFO)); |
| 389 |
*new_info = *transient_fbo_info; |
| 390 |
new_info->fbo_state = FBO_INFO_PERSISTENT; |
| 391 |
_al_ogl_reset_fbo_info(transient_fbo_info); |
| 392 |
ALLEGRO_DEBUG("Persistent FBO: %u\n", new_info->fbo); |
| 393 |
return new_info; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
ALLEGRO_ERROR("Could not find FBO %u in pool\n", transient_fbo_info->fbo); |
| 398 |
return transient_fbo_info; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
static ALLEGRO_FBO_INFO *ogl_find_unused_fbo(ALLEGRO_DISPLAY *display) |
| 403 |
{ |
| 404 |
ALLEGRO_OGL_EXTRAS *extras = display->ogl_extras; |
| 405 |
double min_time = DBL_MAX; |
| 406 |
int min_time_index = -1; |
| 407 |
int i; |
| 408 |
|
| 409 |
for (i = 0; i < ALLEGRO_MAX_OPENGL_FBOS; i++) { |
| 410 |
if (extras->fbos[i].fbo_state == FBO_INFO_UNUSED) |
| 411 |
return &extras->fbos[i]; |
| 412 |
if (extras->fbos[i].last_use_time < min_time) { |
| 413 |
min_time = extras->fbos[i].last_use_time; |
| 414 |
min_time_index = i; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
return &extras->fbos[min_time_index]; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
void _al_ogl_del_fbo(ALLEGRO_FBO_INFO *info) |
| 423 |
{ |
| 424 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra = info->owner->extra; |
| 425 |
extra->fbo_info = NULL; |
| 426 |
ALLEGRO_DEBUG("Deleting FBO: %u\n", info->fbo); |
| 427 |
if (ANDROID_PROGRAMMABLE_PIPELINE(al_get_current_display())) { |
| 428 |
glDeleteFramebuffers(1, &info->fbo); |
| 429 |
} |
| 430 |
else { |
| 431 |
glDeleteFramebuffersEXT(1, &info->fbo); |
| 432 |
} |
| 433 |
|
| 434 |
detach_depth_buffer(info); |
| 435 |
detach_multisample_buffer(info); |
| 436 |
|
| 437 |
info->fbo = 0; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
static ALLEGRO_FBO_INFO *ogl_new_fbo(ALLEGRO_DISPLAY *display) |
| 442 |
{ |
| 443 |
ALLEGRO_FBO_INFO *info; |
| 444 |
GLint e; |
| 445 |
|
| 446 |
info = ogl_find_unused_fbo(display); |
| 447 |
ASSERT(info->fbo_state != FBO_INFO_PERSISTENT); |
| 448 |
|
| 449 |
if (info->fbo_state == FBO_INFO_TRANSIENT) { |
| 450 |
_al_ogl_del_fbo(info); |
| 451 |
_al_ogl_reset_fbo_info(info); |
| 452 |
} |
| 453 |
else { |
| 454 |
/* FBO_INFO_UNUSED */ |
| 455 |
} |
| 456 |
|
| 457 |
if (ANDROID_PROGRAMMABLE_PIPELINE(al_get_current_display())) { |
| 458 |
glGenFramebuffers(1, &info->fbo); |
| 459 |
} |
| 460 |
else { |
| 461 |
glGenFramebuffersEXT(1, &info->fbo); |
| 462 |
} |
| 463 |
e = glGetError(); |
| 464 |
if (e) { |
| 465 |
ALLEGRO_ERROR("glGenFramebuffersEXT failed\n"); |
| 466 |
_al_ogl_reset_fbo_info(info); |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
|
| 470 |
ALLEGRO_DEBUG("Created FBO: %u\n", info->fbo); |
| 471 |
return info; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
void _al_ogl_setup_fbo(ALLEGRO_DISPLAY *display, ALLEGRO_BITMAP *bitmap) |
| 476 |
{ |
| 477 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap; |
| 478 |
|
| 479 |
if (bitmap->parent) |
| 480 |
bitmap = bitmap->parent; |
| 481 |
ogl_bitmap = bitmap->extra; |
| 482 |
|
| 483 |
/* We can't return here. Target's FBO can be taken away by locking |
| 484 |
* a lot of bitmaps consecutively. |
| 485 |
* Also affects ex_multiwin; resizing one window affects the other. |
| 486 |
*/ |
| 487 |
if (false && display->ogl_extras->opengl_target == bitmap) |
| 488 |
return; |
| 489 |
|
| 490 |
_al_ogl_unset_target_bitmap(display, display->ogl_extras->opengl_target); |
| 491 |
|
| 492 |
if (ogl_bitmap->is_backbuffer) |
| 493 |
setup_fbo_backbuffer(display, bitmap); |
| 494 |
else |
| 495 |
_al_ogl_setup_fbo_non_backbuffer(display, bitmap); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
/* With the framebuffer_multisample extension, the absolutely one and |
| 500 |
* only way to ever access the multisample buffer is with the |
| 501 |
* framebuffer_blit extension. [1] |
| 502 |
* |
| 503 |
* This is what we do in this function - if there is a multisample |
| 504 |
* buffer, downsample it back into the texture. |
| 505 |
* |
| 506 |
* [1] https://www.opengl.org/registry/specs/EXT/framebuffer_multisample.txt |
| 507 |
*/ |
| 508 |
void _al_ogl_finalize_fbo(ALLEGRO_DISPLAY *display, |
| 509 |
ALLEGRO_BITMAP *bitmap) |
| 510 |
{ |
| 511 |
ALLEGRO_BITMAP_EXTRA_OPENGL *extra = bitmap->extra; |
| 512 |
if (!extra) |
| 513 |
return; |
| 514 |
ALLEGRO_FBO_INFO *info = extra->fbo_info; |
| 515 |
(void)display; |
| 516 |
if (!info) |
| 517 |
return; |
| 518 |
if (!info->buffers.multisample_buffer) |
| 519 |
return; |
| 520 |
#ifndef ALLEGRO_CFG_OPENGLES |
| 521 |
int w = al_get_bitmap_width(bitmap); |
| 522 |
int h = al_get_bitmap_height(bitmap); |
| 523 |
|
| 524 |
GLuint blit_fbo; |
| 525 |
glGenFramebuffersEXT(1, &blit_fbo); |
| 526 |
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, blit_fbo); |
| 527 |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, |
| 528 |
GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, extra->texture, 0); |
| 529 |
|
| 530 |
glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, info->fbo); |
| 531 |
glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, blit_fbo); |
| 532 |
glBlitFramebufferEXT(0, 0, w, h, 0, 0, w, h, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 533 |
check_gl_error(); |
| 534 |
|
| 535 |
glDeleteFramebuffersEXT(1, &blit_fbo); |
| 536 |
#else |
| 537 |
(void)bitmap; |
| 538 |
#endif |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
static void setup_fbo_backbuffer(ALLEGRO_DISPLAY *display, |
| 543 |
ALLEGRO_BITMAP *bitmap) |
| 544 |
{ |
| 545 |
display->ogl_extras->opengl_target = bitmap; |
| 546 |
|
| 547 |
// The IS_OPENGLES part is a hack. |
| 548 |
if (IS_OPENGLES || |
| 549 |
display->ogl_extras->extension_list->ALLEGRO_GL_EXT_framebuffer_object || |
| 550 |
display->ogl_extras->extension_list->ALLEGRO_GL_OES_framebuffer_object) |
| 551 |
{ |
| 552 |
_al_ogl_bind_framebuffer(0); |
| 553 |
} |
| 554 |
|
| 555 |
#ifdef ALLEGRO_IPHONE |
| 556 |
_al_iphone_setup_opengl_view(display, false); |
| 557 |
#endif |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
bool _al_ogl_setup_fbo_non_backbuffer(ALLEGRO_DISPLAY *display, |
| 562 |
ALLEGRO_BITMAP *bitmap) |
| 563 |
{ |
| 564 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = bitmap->extra; |
| 565 |
ALLEGRO_FBO_INFO *info; |
| 566 |
|
| 567 |
ASSERT(bitmap->parent == NULL); |
| 568 |
|
| 569 |
if (ogl_bitmap->fbo_failed) |
| 570 |
return false; |
| 571 |
|
| 572 |
/* When a bitmap is set as target bitmap, we try to create an FBO for it. */ |
| 573 |
info = ogl_bitmap->fbo_info; |
| 574 |
if (!info) { |
| 575 |
/* FIXME The IS_OPENGLES part is quite a hack but I don't know how the |
| 576 |
* Allegro extension manager works to fix this properly (getting |
| 577 |
* extensions properly reported on iphone). All iOS devices support |
| 578 |
* FBOs though (currently.) |
| 579 |
*/ |
| 580 |
if (IS_OPENGLES || |
| 581 |
al_get_opengl_extension_list()->ALLEGRO_GL_EXT_framebuffer_object || |
| 582 |
al_get_opengl_extension_list()->ALLEGRO_GL_OES_framebuffer_object) |
| 583 |
{ |
| 584 |
info = ogl_new_fbo(display); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (!info || info->fbo == 0) { |
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
use_fbo_for_bitmap(display, bitmap, info); |
| 593 |
return true; /* state changed */ |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
static void use_fbo_for_bitmap(ALLEGRO_DISPLAY *display, |
| 598 |
ALLEGRO_BITMAP *bitmap, ALLEGRO_FBO_INFO *info) |
| 599 |
{ |
| 600 |
ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap = bitmap->extra; |
| 601 |
GLint e; |
| 602 |
|
| 603 |
if (info->fbo_state == FBO_INFO_UNUSED) |
| 604 |
info->fbo_state = FBO_INFO_TRANSIENT; |
| 605 |
info->owner = bitmap; |
| 606 |
info->last_use_time = al_get_time(); |
| 607 |
ogl_bitmap->fbo_info = info; |
| 608 |
|
| 609 |
/* Bind to the FBO. */ |
| 610 |
_al_ogl_bind_framebuffer(info->fbo); |
| 611 |
|
| 612 |
attach_multisample_buffer(info); |
| 613 |
attach_depth_buffer(info); |
| 614 |
|
| 615 |
/* If we have a multisample renderbuffer, we can only syncronize |
| 616 |
* it back to the texture once we stop drawing into it - i.e. |
| 617 |
* when the target bitmap is changed to something else. |
| 618 |
*/ |
| 619 |
if (!info->buffers.multisample_buffer) { |
| 620 |
|
| 621 |
/* Attach the texture. */ |
| 622 |
#ifdef ALLEGRO_CFG_OPENGLES |
| 623 |
if (ANDROID_PROGRAMMABLE_PIPELINE(al_get_current_display())) { |
| 624 |
if (al_get_bitmap_samples(bitmap) == 0 || !al_have_opengl_extension("EXT_multisampled_render_to_texture")) { |
| 625 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, |
| 626 |
GL_TEXTURE_2D, ogl_bitmap->texture, 0); |
| 627 |
} |
| 628 |
#if ((!defined ALLEGRO_CFG_OPENGLES || defined ALLEGRO_CFG_OPENGLES3) && !defined ALLEGRO_IPHONE) |
| 629 |
#if (!defined ALLEGRO_ANDROID) || (__ANDROID_API__ >= 28) /* Android: glFramebufferTexture2DMultisampleEXT exists in newer libGLESv[23].so */ |
| 630 |
else { |
| 631 |
glFramebufferTexture2DMultisampleEXT(GL_FRAMEBUFFER, |
| 632 |
GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ogl_bitmap->texture, |
| 633 |
0, al_get_bitmap_samples(bitmap)); |
| 634 |
|
| 635 |
} |
| 636 |
#endif |
| 637 |
#endif |
| 638 |
} |
| 639 |
else |
| 640 |
#endif |
| 641 |
{ |
| 642 |
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, |
| 643 |
GL_TEXTURE_2D, ogl_bitmap->texture, 0); |
| 644 |
} |
| 645 |
|
| 646 |
e = glGetError(); |
| 647 |
if (e) { |
| 648 |
ALLEGRO_DEBUG("glFrameBufferTexture2DEXT failed! fbo=%d texture=%d (%s)\n", |
| 649 |
info->fbo, ogl_bitmap->texture, _al_gl_error_string(e)); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
/* See comment about unimplemented functions on Android above */ |
| 654 |
if (UNLESS_ANDROID_OR_RPI( |
| 655 |
glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)) |
| 656 |
{ |
| 657 |
/* For some reason, we cannot use the FBO with this |
| 658 |
* texture. So no reason to keep re-trying, output a log |
| 659 |
* message and switch to (extremely slow) software mode. |
| 660 |
*/ |
| 661 |
ALLEGRO_ERROR("Could not use FBO for bitmap with format %s.\n", |
| 662 |
_al_pixel_format_name(al_get_bitmap_format(bitmap))); |
| 663 |
ALLEGRO_ERROR("*** SWITCHING TO SOFTWARE MODE ***\n"); |
| 664 |
_al_ogl_bind_framebuffer(0); |
| 665 |
glDeleteFramebuffersEXT(1, &info->fbo); |
| 666 |
_al_ogl_reset_fbo_info(info); |
| 667 |
ogl_bitmap->fbo_info = NULL; |
| 668 |
ogl_bitmap->fbo_failed = true; |
| 669 |
} |
| 670 |
else { |
| 671 |
display->ogl_extras->opengl_target = bitmap; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
/* vim: set sts=3 sw=3 et: */ |