Skip to content

Commit 0ce3d60

Browse files
authored
fix: enhance ImGuiImplGl3 OpenGL compatibility checks for sampler objects (#320)
1 parent 30bd1d5 commit 0ce3d60

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

imgui-lwjgl3/src/main/java/imgui/gl3/ImGuiImplGl3.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import imgui.flag.ImGuiConfigFlags;
1212
import imgui.flag.ImGuiViewportFlags;
1313
import imgui.type.ImInt;
14+
import org.lwjgl.opengl.GL;
15+
import org.lwjgl.opengl.GLCapabilities;
1416

1517
import java.nio.ByteBuffer;
1618
import java.util.regex.Matcher;
@@ -157,6 +159,7 @@ protected static class Data {
157159
// protected boolean glProfileIsES3;
158160
protected boolean glProfileIsCompat;
159161
protected int glProfileMask;
162+
protected GLCapabilities glCapabilities = null;
160163
protected String glslVersion = "";
161164
protected int fontTexture = 0;
162165
protected int shaderHandle = 0;
@@ -273,6 +276,15 @@ public boolean init(final String glslVersion) {
273276
data.glVersion = major * 100 + minor * 10;
274277
data.glProfileMask = glGetInteger(GL_CONTEXT_PROFILE_MASK);
275278
data.glProfileIsCompat = (data.glProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0;
279+
if (data.glVersion < 330) { // Ignore in higher GL versions since they support sampler objects anyway
280+
try {
281+
data.glCapabilities = GL.getCapabilities();
282+
} catch (IllegalStateException ignored) {
283+
// IllegalStateException – if setCapabilities has never been called in the current thread or was last called with a null value
284+
// This exception can be safely ignored as it does not impact the initialization process.
285+
// GL_ARB_sampler_objects will be unavailable (and therefore not supported) if the GL version is less than 3.3.
286+
}
287+
}
276288
}
277289

278290
// We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
@@ -381,7 +393,7 @@ protected void setupRenderState(final ImDrawData drawData, final int fbWidth, fi
381393
glUniform1i(data.attribLocationTex, 0);
382394
glUniformMatrix4fv(data.attribLocationProjMtx, false, props.orthoProjMatrix);
383395

384-
if (data.glVersion >= 330) {
396+
if (data.glVersion >= 330 || (data.glCapabilities != null && data.glCapabilities.GL_ARB_sampler_objects)) {
385397
glBindSampler(0, 0);
386398
}
387399

@@ -421,7 +433,7 @@ public void renderDrawData(final ImDrawData drawData) {
421433
glActiveTexture(GL_TEXTURE0);
422434
glGetIntegerv(GL_CURRENT_PROGRAM, props.lastProgram);
423435
glGetIntegerv(GL_TEXTURE_BINDING_2D, props.lastTexture);
424-
if (data.glVersion >= 330) {
436+
if (data.glVersion >= 330 || (data.glCapabilities != null && data.glCapabilities.GL_ARB_sampler_objects)) {
425437
glGetIntegerv(GL_SAMPLER_BINDING, props.lastSampler);
426438
}
427439
glGetIntegerv(GL_ARRAY_BUFFER_BINDING, props.lastArrayBuffer);
@@ -525,7 +537,7 @@ public void renderDrawData(final ImDrawData drawData) {
525537
glUseProgram(props.lastProgram[0]);
526538
}
527539
glBindTexture(GL_TEXTURE_2D, props.lastTexture[0]);
528-
if (data.glVersion >= 330) {
540+
if (data.glVersion >= 330 || (data.glCapabilities != null && data.glCapabilities.GL_ARB_sampler_objects)) {
529541
glBindSampler(0, props.lastSampler[0]);
530542
}
531543
glActiveTexture(props.lastActiveTexture[0]);

0 commit comments

Comments
 (0)