Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unbound Shader Program Error When Using ImGui #300

Open
G-ALDN opened this issue Dec 6, 2024 · 3 comments
Open

Unbound Shader Program Error When Using ImGui #300

G-ALDN opened this issue Dec 6, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@G-ALDN
Copy link

G-ALDN commented Dec 6, 2024

Version

1.87.6

What happened?

I'm encountering an error related to shader program binding while using ImGui with LWJGL and OpenGL. The error message reads:

GL_INVALID_OPERATION error generated. No active program.
GL_INVALID_OPERATION error generated. Invalid VAO/VBO/pointer usage.
GL_INVALID_OPERATION error generated. Target buffer must be bound.

When running the application with ImGui rendering enabled, the OpenGL shaders seem to be unbound, and the program fails to render properly. The error does not occur when ImGui is disabled.

This code was pretty much taken from the GamesWithGabe lwjgl imgui tutorial linked in the main page. If I am just implementing this incorrectly, please let me know.

Heres the code:

import imgui.ImFontConfig;
import imgui.ImGui;
import imgui.ImGuiIO;
import imgui.flag.ImGuiConfigFlags;
import imgui.gl3.ImGuiImplGl3;
import imgui.glfw.ImGuiImplGlfw;
import imgui.type.ImInt;
import org.lwjgl.Version;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL43;
import org.lwjgl.opengl.GLDebugMessageCallback;

import static org.lwjgl.glfw.GLFW.;
import static org.lwjgl.opengl.GL11.
;
import static org.lwjgl.opengl.GL20C.glUseProgram;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.system.MemoryUtil.NULL;

public class Window {
private final ImGuiImplGlfw imGuiGlfw = new ImGuiImplGlfw();
private final ImGuiImplGl3 imGuiGl3 = new ImGuiImplGl3();

private String glslVersion = null;
private long windowPtr;
private ImGuiLayer imguiLayer;

public Window(ImGuiLayer layer) {
    imguiLayer = layer;
}

public void init() {
    initWindow();
    initImGui();
    imGuiGlfw.init(windowPtr, true);
    ImGui.getIO().getFonts().getTexDataAsRGBA32(new ImInt(255), new ImInt(255), new ImInt(255));
    imGuiGl3.init(glslVersion);
}

public void destroy() {
    imGuiGl3.shutdown();
    imGuiGlfw.shutdown();
    ImGui.destroyContext();
    Callbacks.glfwFreeCallbacks(windowPtr);
    glfwDestroyWindow(windowPtr);
    glfwTerminate();
}

private void initWindow() {
    GLFWErrorCallback.createPrint(System.err).set();

    // Initialize GLFW. Most GLFW functions will not work before doing this.
    if ( !glfwInit() ) {
        System.out.println("Unable to initialize GLFW");
        System.exit(-1);
    }

    glslVersion = "#version 460";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    windowPtr = glfwCreateWindow(1920, 1080, "My Window", NULL, NULL);

    if (windowPtr == NULL) {
        System.out.println("Unable to create window");
        System.exit(-1);
    }

    glfwMakeContextCurrent(windowPtr);
    glfwSwapInterval(1);
    glfwShowWindow(windowPtr);

    GL.createCapabilities();

    GL43.glEnable(GL43.GL_DEBUG_OUTPUT);
    GL43.glDebugMessageCallback(
            GLDebugMessageCallback.create((source, type, id, severity, length, message, userParam) -> {
                System.err.println("GL CALLBACK: " + severity + " - " + GLDebugMessageCallback.getMessage(length, message));
            }),
            0
    );
    System.out.println("OpenGL version: " + glGetString(GL_VERSION));
}

private void initImGui() {
    ImGui.createContext();
    ImGuiIO io = ImGui.getIO();
    io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable);

    // Explicitly build font atlas
    io.getFonts().setLocked(false);
    final ImFontConfig fontConfig = new ImFontConfig();
    fontConfig.setMergeMode(true);
    fontConfig.setFontDataOwnedByAtlas(false);
    fontConfig.setGlyphRanges(io.getFonts().getGlyphRangesDefault());
    io.getFonts().addFontDefault();
    io.getFonts().build();
    fontConfig.destroy();

    // Initialize GLFW and OpenGL backends
    imGuiGlfw.init(windowPtr, true);

    imGuiGl3.init(glslVersion);
}

public void run() {
    while (!glfwWindowShouldClose(windowPtr)) {
        glClearColor(0.1f, 0.09f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        imGuiGlfw.newFrame();
        ImGui.newFrame();
        imguiLayer.imgui(); // Your ImGui content

        ImGui.render();
        imGuiGl3.renderDrawData(ImGui.getDrawData());

        // Optionally reset OpenGL state
        glUseProgram(0); // Unbind any program
        glBindVertexArray(0); // Unbind VAO

        if (ImGui.getIO().hasConfigFlags(ImGuiConfigFlags.ViewportsEnable)) {
            final long backupWindowPtr = GLFW.glfwGetCurrentContext();
            ImGui.updatePlatformWindows();
            ImGui.renderPlatformWindowsDefault();
            GLFW.glfwMakeContextCurrent(backupWindowPtr);
        }

        GLFW.glfwSwapBuffers(windowPtr);
        GLFW.glfwPollEvents();
    }
}

}
hs_err_pid12376.log

Reproduction

Initialize GLFW window and OpenGL context.
Initialize ImGui with LWJGL backend (ImGuiImplGlfw, ImGuiImplGl3).
Call imGuiGlfw.newFrame() and ImGui.newFrame() in the main render loop.
Render ImGui content using ImGui.render() and imGuiGl3.renderDrawData(ImGui.getDrawData()).
Observe the OpenGL errors related to unbound shader programs.

Relevant log output

No response

@G-ALDN G-ALDN added the bug Something isn't working label Dec 6, 2024
@SpaiR
Copy link
Owner

SpaiR commented Dec 6, 2024

Please try the Application module to check if Dear ImGui does work on your system in general. This will help to understand if there is a problem on the binding side.

@G-ALDN
Copy link
Author

G-ALDN commented Dec 6, 2024

The Application module does indeed work.

@SpaiR
Copy link
Owner

SpaiR commented Dec 6, 2024

In that case the problems you've met are rely in a sphere of your local code. I recommend to follow configuration steps from the Application class.

Also, looking through your code I see

        // Optionally reset OpenGL state
        glUseProgram(0); // Unbind any program
        glBindVertexArray(0); // Unbind VAO

This looks very odd to me and may be partly the reason of your errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants