Skip to content

Commit 18cbe1c

Browse files
Guard OpenGL functions with availability checks
Replace strict LIME_GLES3_API compile-time guards with checks that allow desktop GL (or GLES3) and verify function pointer availability before calling. Adds nullptr checks around calls like glBlitFramebuffer, glClearBuffer* variants, glCopyTexSubImage3D, glDrawBuffers and glReadBuffer (including HL_* wrappers) to avoid invoking missing entry points. Also scopes the draw_buffers allocation/loop inside the availability guard. This makes the OpenGL bindings safer across platforms and when certain GL functions are not present. We should probably come up with a more idomatic solution here but this is a good temporary workaround. These functions are available on desktop gl, not just gles3.
1 parent e345364 commit 18cbe1c

1 file changed

Lines changed: 70 additions & 38 deletions

File tree

project/src/graphics/opengl/OpenGLBindings.cpp

Lines changed: 70 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,21 @@ namespace lime {
563563

564564
void lime_gl_blit_framebuffer (int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) {
565565

566-
#ifdef LIME_GLES3_API
567-
glBlitFramebuffer (srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
566+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
567+
if (glBlitFramebuffer) {
568+
glBlitFramebuffer (srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
569+
}
568570
#endif
569571

570572
}
571573

572574

573575
HL_PRIM void HL_NAME(hl_gl_blit_framebuffer) (int srcX0, int srcY0, int srcX1, int srcY1, int dstX0, int dstY0, int dstX1, int dstY1, int mask, int filter) {
574576

575-
#ifdef LIME_GLES3_API
576-
glBlitFramebuffer (srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
577+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
578+
if (glBlitFramebuffer) {
579+
glBlitFramebuffer (srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter);
580+
}
577581
#endif
578582

579583
}
@@ -639,71 +643,87 @@ namespace lime {
639643

640644
void lime_gl_clear_bufferfi (int buffer, int drawBuffer, float depth, int stencil) {
641645

642-
#ifdef LIME_GLES3_API
643-
glClearBufferfi (buffer, drawBuffer, depth, stencil);
646+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
647+
if (glClearBufferfi) {
648+
glClearBufferfi (buffer, drawBuffer, depth, stencil);
649+
}
644650
#endif
645651

646652
}
647653

648654

649655
HL_PRIM void HL_NAME(hl_gl_clear_bufferfi) (int buffer, int drawBuffer, float depth, int stencil) {
650656

651-
#ifdef LIME_GLES3_API
652-
glClearBufferfi (buffer, drawBuffer, depth, stencil);
657+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
658+
if (glClearBufferfi) {
659+
glClearBufferfi (buffer, drawBuffer, depth, stencil);
660+
}
653661
#endif
654662

655663
}
656664

657665

658666
void lime_gl_clear_bufferfv (int buffer, int drawBuffer, double data) {
659667

660-
#ifdef LIME_GLES3_API
661-
glClearBufferfv (buffer, drawBuffer, (GLfloat*)(uintptr_t)data);
668+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
669+
if (glClearBufferfv) {
670+
glClearBufferfv (buffer, drawBuffer, (GLfloat*)(uintptr_t)data);
671+
}
662672
#endif
663673

664674
}
665675

666676

667677
HL_PRIM void HL_NAME(hl_gl_clear_bufferfv) (int buffer, int drawBuffer, double data) {
668678

669-
#ifdef LIME_GLES3_API
670-
glClearBufferfv (buffer, drawBuffer, (GLfloat*)(uintptr_t)data);
679+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
680+
if (glClearBufferfv) {
681+
glClearBufferfv (buffer, drawBuffer, (GLfloat*)(uintptr_t)data);
682+
}
671683
#endif
672684

673685
}
674686

675687

676688
void lime_gl_clear_bufferiv (int buffer, int drawBuffer, double data) {
677689

678-
#ifdef LIME_GLES3_API
679-
glClearBufferiv (buffer, drawBuffer, (GLint*)(uintptr_t)data);
690+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
691+
if (glClearBufferiv) {
692+
glClearBufferiv (buffer, drawBuffer, (GLint*)(uintptr_t)data);
693+
}
680694
#endif
681695

682696
}
683697

684698

685699
HL_PRIM void HL_NAME(hl_gl_clear_bufferiv) (int buffer, int drawBuffer, double data) {
686700

687-
#ifdef LIME_GLES3_API
688-
glClearBufferiv (buffer, drawBuffer, (GLint*)(uintptr_t)data);
701+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
702+
if (glClearBufferiv) {
703+
glClearBufferiv (buffer, drawBuffer, (GLint*)(uintptr_t)data);
704+
}
689705
#endif
690706

691707
}
692708

693709

694710
void lime_gl_clear_bufferuiv (int buffer, int drawBuffer, double data) {
695711

696-
#ifdef LIME_GLES3_API
697-
glClearBufferuiv (buffer, drawBuffer, (GLuint*)(uintptr_t)data);
712+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
713+
if (glClearBufferuiv) {
714+
glClearBufferuiv (buffer, drawBuffer, (GLuint*)(uintptr_t)data);
715+
}
698716
#endif
699717

700718
}
701719

702720

703721
HL_PRIM void HL_NAME(hl_gl_clear_bufferuiv) (int buffer, int drawBuffer, double data) {
704722

705-
#ifdef LIME_GLES3_API
706-
glClearBufferuiv (buffer, drawBuffer, (GLuint*)(uintptr_t)data);
723+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
724+
if (glClearBufferuiv) {
725+
glClearBufferuiv (buffer, drawBuffer, (GLuint*)(uintptr_t)data);
726+
}
707727
#endif
708728

709729
}
@@ -923,17 +943,21 @@ namespace lime {
923943

924944
void lime_gl_copy_tex_sub_image_3d (int target, int level, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height) {
925945

926-
#ifdef LIME_GLES3_API
927-
glCopyTexSubImage3D (target, level, xoffset, yoffset, zoffset, x, y, width, height);
946+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
947+
if (glCopyTexSubImage3D) {
948+
glCopyTexSubImage3D (target, level, xoffset, yoffset, zoffset, x, y, width, height);
949+
}
928950
#endif
929951

930952
}
931953

932954

933955
HL_PRIM void HL_NAME(hl_gl_copy_tex_sub_image_3d) (int target, int level, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height) {
934956

935-
#ifdef LIME_GLES3_API
936-
glCopyTexSubImage3D (target, level, xoffset, yoffset, zoffset, x, y, width, height);
957+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
958+
if (glCopyTexSubImage3D) {
959+
glCopyTexSubImage3D (target, level, xoffset, yoffset, zoffset, x, y, width, height);
960+
}
937961
#endif
938962

939963
}
@@ -1443,27 +1467,31 @@ namespace lime {
14431467

14441468
void lime_gl_draw_buffers (value buffers) {
14451469

1446-
#ifdef LIME_GLES3_API
1447-
GLsizei size = val_array_size (buffers);
1448-
GLenum *_buffers = (GLenum*)alloca (size * sizeof(GLenum));
1470+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
1471+
if (glDrawBuffers) {
1472+
GLsizei size = val_array_size (buffers);
1473+
GLenum *_buffers = (GLenum*)alloca (size * sizeof(GLenum));
14491474

1450-
for (int i = 0; i < size; i++) {
1475+
for (int i = 0; i < size; i++) {
14511476

1452-
_buffers[i] = val_int (val_array_i (buffers, i));
1477+
_buffers[i] = val_int (val_array_i (buffers, i));
14531478

1454-
}
1479+
}
14551480

1456-
glDrawBuffers (size, _buffers);
1481+
glDrawBuffers (size, _buffers);
1482+
}
14571483
#endif
14581484

14591485
}
14601486

14611487

14621488
HL_PRIM void HL_NAME(hl_gl_draw_buffers) (hl_varray* buffers) {
14631489

1464-
#ifdef LIME_GLES3_API
1465-
GLsizei size = buffers->size;
1466-
glDrawBuffers (size, (GLenum*)hl_aptr (buffers, int));
1490+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
1491+
if (glDrawBuffers) {
1492+
GLsizei size = buffers->size;
1493+
glDrawBuffers (size, (GLenum*)hl_aptr (buffers, int));
1494+
}
14671495
#endif
14681496

14691497
}
@@ -4029,17 +4057,21 @@ namespace lime {
40294057

40304058
void lime_gl_read_buffer (int src) {
40314059

4032-
#ifdef LIME_GLES3_API
4033-
glReadBuffer (src);
4060+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
4061+
if (glReadBuffer) {
4062+
glReadBuffer (src);
4063+
}
40344064
#endif
40354065

40364066
}
40374067

40384068

40394069
HL_PRIM void HL_NAME(hl_gl_read_buffer) (int src) {
40404070

4041-
#ifdef LIME_GLES3_API
4042-
glReadBuffer (src);
4071+
#if defined (LIME_GLES3_API) || !defined (LIME_GLES)
4072+
if (glReadBuffer) {
4073+
glReadBuffer (src);
4074+
}
40434075
#endif
40444076

40454077
}

0 commit comments

Comments
 (0)