Skip to content

Commit 9b36d6a

Browse files
SiegeLordExSiegeLord
authored andcommitted
Restore FBO/GLContext after we're done with the current context.
Previously, we would restore it before we were done with the current context, causing issues as described in #981. Fixes #981.
1 parent adaedb6 commit 9b36d6a

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed

src/opengl/ogl_lock.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static bool ogl_lock_region_nonbb_writeonly(
8282
int x, int gl_y, int w, int h, int format);
8383
static bool ogl_lock_region_nonbb_readwrite(
8484
ALLEGRO_BITMAP *bitmap, ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap,
85-
int x, int gl_y, int w, int h, int format);
85+
int x, int gl_y, int w, int h, int format, bool* restore_fbo);
8686
static bool ogl_lock_region_nonbb_readwrite_fbo(
8787
ALLEGRO_BITMAP *bitmap, ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap,
8888
int x, int gl_y, int w, int h, int format);
@@ -98,8 +98,10 @@ ALLEGRO_LOCKED_REGION *_al_ogl_lock_region_new(ALLEGRO_BITMAP *bitmap,
9898
const GLint gl_y = bitmap->h - y - h;
9999
ALLEGRO_DISPLAY *disp;
100100
ALLEGRO_DISPLAY *old_disp = NULL;
101+
ALLEGRO_BITMAP *old_target = al_get_target_bitmap();
101102
GLenum e;
102103
bool ok;
104+
bool restore_fbo = false;
103105

104106
if (format == ALLEGRO_PIXEL_FORMAT_ANY) {
105107
/* Never pick compressed formats with ANY, as it interacts weirdly with
@@ -160,12 +162,29 @@ ALLEGRO_LOCKED_REGION *_al_ogl_lock_region_new(ALLEGRO_BITMAP *bitmap,
160162
else {
161163
ALLEGRO_DEBUG("Locking non-backbuffer READWRITE\n");
162164
ok = ogl_lock_region_nonbb_readwrite(bitmap, ogl_bitmap,
163-
x, gl_y, w, h, format);
165+
x, gl_y, w, h, format, &restore_fbo);
164166
}
165167
}
166168

167169
glPopClientAttrib();
168170

171+
/* Restore state after switching FBO. */
172+
if (restore_fbo) {
173+
if (!old_target) {
174+
/* Old target was NULL; release the context. */
175+
_al_set_current_display_only(NULL);
176+
}
177+
else if (!_al_get_bitmap_display(old_target)) {
178+
/* Old target was memory bitmap; leave the current display alone. */
179+
}
180+
else if (old_target != bitmap) {
181+
/* Old target was another OpenGL bitmap. */
182+
_al_ogl_setup_fbo(_al_get_bitmap_display(old_target), old_target);
183+
}
184+
}
185+
186+
ASSERT(al_get_target_bitmap() == old_target);
187+
169188
if (old_disp != NULL) {
170189
_al_set_current_display_only(old_disp);
171190
}
@@ -240,19 +259,16 @@ static bool ogl_lock_region_nonbb_writeonly(
240259

241260
static bool ogl_lock_region_nonbb_readwrite(
242261
ALLEGRO_BITMAP *bitmap, ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap,
243-
int x, int gl_y, int w, int h, int format)
262+
int x, int gl_y, int w, int h, int format, bool* restore_fbo)
244263
{
245-
ALLEGRO_BITMAP *old_target;
246-
bool fbo_was_set;
247264
bool ok;
248265

249266
ASSERT(bitmap->parent == NULL);
250267
ASSERT(bitmap->locked == false);
251268
ASSERT(_al_get_bitmap_display(bitmap) == al_get_current_display());
252269

253270
/* Try to create an FBO if there isn't one. */
254-
old_target = al_get_target_bitmap();
255-
fbo_was_set =
271+
*restore_fbo =
256272
_al_ogl_setup_fbo_non_backbuffer(_al_get_bitmap_display(bitmap), bitmap);
257273

258274
if (ogl_bitmap->fbo_info) {
@@ -266,23 +282,6 @@ static bool ogl_lock_region_nonbb_readwrite(
266282
x, gl_y, w, h, format);
267283
}
268284

269-
/* Restore state after switching FBO. */
270-
if (fbo_was_set) {
271-
if (!old_target) {
272-
/* Old target was NULL; release the context. */
273-
_al_set_current_display_only(NULL);
274-
}
275-
else if (!_al_get_bitmap_display(old_target)) {
276-
/* Old target was memory bitmap; leave the current display alone. */
277-
}
278-
else if (old_target != bitmap) {
279-
/* Old target was another OpenGL bitmap. */
280-
_al_ogl_setup_fbo(_al_get_bitmap_display(old_target), old_target);
281-
}
282-
}
283-
284-
ASSERT(al_get_target_bitmap() == old_target);
285-
286285
return ok;
287286
}
288287

src/opengl/ogl_lock_es.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static bool ogl_lock_region_nonbb_writeonly(
8282
int x, int gl_y, int w, int h, int real_format);
8383
static bool ogl_lock_region_nonbb_readwrite(
8484
ALLEGRO_BITMAP *bitmap, ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap,
85-
int x, int gl_y, int w, int h, int real_format);
85+
int x, int gl_y, int w, int h, int real_format, bool* restore_fbo);
8686
static bool ogl_lock_region_nonbb_readwrite_fbo(
8787
ALLEGRO_BITMAP *bitmap, ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap,
8888
int x, int gl_y, int w, int h, int real_format);
@@ -257,7 +257,9 @@ static ALLEGRO_LOCKED_REGION *ogl_lock_region_nonbb(ALLEGRO_BITMAP *bitmap,
257257
const int gl_y = bitmap->h - y - h;
258258
ALLEGRO_DISPLAY *disp;
259259
ALLEGRO_DISPLAY *old_disp = NULL;
260+
ALLEGRO_BITMAP *old_target = al_get_target_bitmap();
260261
bool ok;
262+
bool restore_fbo = false;
261263

262264
disp = al_get_current_display();
263265

@@ -300,10 +302,27 @@ static ALLEGRO_LOCKED_REGION *ogl_lock_region_nonbb(ALLEGRO_BITMAP *bitmap,
300302
ALLEGRO_DEBUG("Locking non-backbuffer %s\n",
301303
(flags & ALLEGRO_LOCK_READONLY) ? "READONLY" : "READWRITE");
302304
ok = ogl_lock_region_nonbb_readwrite(bitmap, ogl_bitmap,
303-
x, gl_y, w, h, real_format);
305+
x, gl_y, w, h, real_format, &restore_fbo);
306+
}
307+
}
308+
309+
/* Restore state after switching FBO. */
310+
if (restore_fbo) {
311+
if (!old_target) {
312+
/* Old target was NULL; release the context. */
313+
_al_set_current_display_only(NULL);
314+
}
315+
else if (!_al_get_bitmap_display(old_target)) {
316+
/* Old target was memory bitmap; leave the current display alone. */
317+
}
318+
else if (old_target != bitmap) {
319+
/* Old target was another OpenGL bitmap. */
320+
_al_ogl_setup_fbo(_al_get_bitmap_display(old_target), old_target);
304321
}
305322
}
306323

324+
ASSERT(al_get_target_bitmap() == old_target);
325+
307326
if (old_disp != NULL) {
308327
_al_set_current_display_only(old_disp);
309328
}
@@ -348,23 +367,20 @@ static bool ogl_lock_region_nonbb_writeonly(
348367

349368
static bool ogl_lock_region_nonbb_readwrite(
350369
ALLEGRO_BITMAP *bitmap, ALLEGRO_BITMAP_EXTRA_OPENGL *ogl_bitmap,
351-
int x, int gl_y, int w, int h, int real_format)
370+
int x, int gl_y, int w, int h, int real_format, bool* restore_fbo)
352371
{
353-
ALLEGRO_BITMAP *old_target;
354-
bool fbo_was_set;
355372
bool ok;
356373

357374
ASSERT(bitmap->parent == NULL);
358375
ASSERT(bitmap->locked == false);
359376
ASSERT(_al_get_bitmap_display(bitmap) == al_get_current_display());
360377

361378
/* Try to create an FBO if there isn't one. */
362-
old_target = al_get_target_bitmap();
363-
fbo_was_set =
379+
*restore_fbo =
364380
_al_ogl_setup_fbo_non_backbuffer(_al_get_bitmap_display(bitmap), bitmap);
365381

366382
/* Unlike in desktop GL, there seems to be nothing we can do without an FBO. */
367-
if (fbo_was_set && ogl_bitmap->fbo_info) {
383+
if (*restore_fbo && ogl_bitmap->fbo_info) {
368384
ok = ogl_lock_region_nonbb_readwrite_fbo(bitmap, ogl_bitmap,
369385
x, gl_y, w, h, real_format);
370386
}
@@ -373,23 +389,6 @@ static bool ogl_lock_region_nonbb_readwrite(
373389
ok = false;
374390
}
375391

376-
/* Restore state after switching FBO. */
377-
if (fbo_was_set) {
378-
if (!old_target) {
379-
/* Old target was NULL; release the context. */
380-
_al_set_current_display_only(NULL);
381-
}
382-
else if (!_al_get_bitmap_display(old_target)) {
383-
/* Old target was memory bitmap; leave the current display alone. */
384-
}
385-
else if (old_target != bitmap) {
386-
/* Old target was another OpenGL bitmap. */
387-
_al_ogl_setup_fbo(_al_get_bitmap_display(old_target), old_target);
388-
}
389-
}
390-
391-
ASSERT(al_get_target_bitmap() == old_target);
392-
393392
return ok;
394393
}
395394

0 commit comments

Comments
 (0)