glfwSwapBuffers很慢

时间:2019-04-09 17:40:10

标签: java opengl lwjgl glfw

我已经将我的LWJGL应用程序从LWJGL 2迁移到LWJGL 3,即使游戏循环中没有一个渲染调用,glfwSwapBuffers()的调用也非常慢(每次调用最多20毫秒) 。当我尝试对LWJGL 2使用Display.update()时,我已经遇到了这个问题,这种方法也很慢。我用JETM测量了执行时间。我的操作系统是Mac OS X 10.13.3,带有“ Intel HD Graphics 500”作为图形卡。

相关代码

 public static void main(String[] args) {
        new MainLoop_I();
    }

    private static final int START_WIDTH = 500;
    private static final int START_HEIGHT = 300;

    private long window;
    private GLFWErrorCallback errorCallback;


    private static GLCapabilities capabilities;

    public MainLoop_I() {        
        init();

        // configure time execution measurement library (JETM)
        BasicEtmConfigurator.configure();
        final EtmMonitor monitor = EtmManager.getEtmMonitor();
        monitor.start();

        loop();

        // print out results of JETM
        monitor.render(new SimpleTextRenderer());
        monitor.stop();

        glfwFreeCallbacks(window);
        glfwDestroyWindow(window);

        glfwTerminate();
        glfwSetErrorCallback(null).free();

        System.exit(0);
    }

    private void init() {
        glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
        System.out.println(glfwGetVersionString());
        if ( !glfwInit() ) throw new IllegalStateException("Unable to initialize GLFW");

        glfwDefaultWindowHints();
        glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
        glfwWindowHint(GLFW_DEPTH_BITS, 24);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

        window = glfwCreateWindow(START_WIDTH, START_HEIGHT, "Game", NULL, NULL);
        if ( window == NULL ) throw new RuntimeException("Failed to create the GLFW window");



        glfwMakeContextCurrent(window);
        glfwShowWindow(window);
        glfwFocusWindow(window);

        capabilities = GL.createCapabilities();
    }

    private void loop() {
        long lastFrameTime = System.currentTimeMillis();


        glClearColor(0, 0, 0, 1);

        while(! glfwWindowShouldClose(window)) {
            glfwPollEvents();

            long thisFrameTime = System.currentTimeMillis();
            float deltaSeconds = (thisFrameTime - lastFrameTime) / 1000f;
            lastFrameTime = thisFrameTime;


            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


            EtmPoint point = EtmManager.getEtmMonitor().createPoint("MainLoop_I:loop");
            glfwSwapBuffers(window);
            point.collect();

        }
    }

如此长的执行时间可能是什么原因?

0 个答案:

没有答案