Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java glfwSwapBuffers速度很慢_Java_Opengl_Lwjgl_Glfw - Fatal编程技术网

Java glfwSwapBuffers速度很慢

Java glfwSwapBuffers速度很慢,java,opengl,lwjgl,glfw,Java,Opengl,Lwjgl,Glfw,我已经将我的LWJGL应用程序从LWJGL 2迁移到LWJGL 3,调用glfwSwapBuffers()非常慢(每次调用最多20毫秒),即使在游戏循环中没有一个渲染调用。当我尝试使用LWJGL 2的Display.update()时,我已经遇到了这个问题,这个方法也很慢。我已经用计算机测量了执行时间。我的操作系统是Mac OS X 10.13.3,图形卡为“英特尔高清图形500” 有关守则 执行时间过长的可能原因是什么?这是v-sync。调用glfwSwapInterval(0)以避免出现这种

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

有关守则
执行时间过长的可能原因是什么?

这是v-sync。调用
glfwSwapInterval(0)
以避免出现这种情况。(除非在图形驱动程序设置中强制执行)谢谢,通过禁用v-sync,我可以将
glSwapBuffers
的执行时间减少到6毫秒左右,但无需调用
glDraw…
。您知道其他一些对
glSwapBuffers
性能有很大影响的
glSwapBuffers
方法吗?它不是
glSwapBuffers
-它不是OpenGL函数。它是
glfwSwapBuffers
。最重要的是,你在循环中做了一些事情。调用
glClear()
也不便宜(尤其是在低端图形适配器上,具体取决于您的帧缓冲区尺寸)。除了时间测量代码和
gfwSwapBuffers()
@DasElias之外,我会从您的循环中删除所有内容:您不应该禁用v-sync
glfwSwapBuffers
不是“慢”——它故意等待监视器同步以防止撕裂。您向用户显示的帧数不能超过显示器的刷新率,刷新率可能为60Hz,帧间间隔约为17ms。无论如何,您都不能使用您的方法进行概要分析,因为OpenGL是流水线的,并且是异步执行的。出于分析目的,请使用
GL\u ARB\u timer\u query
查询GPU时间。@DasElias这实际上是预期的行为。OpenGL驱动程序倾向于在后台捆绑API调用,然后将它们发送到GPU以成批执行。这实际上减少了CPU和GPU之间的一些通信开销。OpenGL中的每个调用都是异步的,除了帧缓冲区交换,它会暂停调用线程,直到所有提交的函数都执行完毕。因此,glfwSwapBuffers似乎需要这么长时间的原因是v-sync。调用
glfwSwapInterval(0)
以避免出现这种情况。(除非在图形驱动程序设置中强制执行)谢谢,通过禁用v-sync,我可以将
glSwapBuffers
的执行时间减少到6毫秒左右,但无需调用
glDraw…
。您知道其他一些对
glSwapBuffers
性能有很大影响的
glSwapBuffers
方法吗?它不是
glSwapBuffers
-它不是OpenGL函数。它是
glfwSwapBuffers
。最重要的是,你在循环中做了一些事情。调用
glClear()
也不便宜(尤其是在低端图形适配器上,具体取决于您的帧缓冲区尺寸)。除了时间测量代码和
gfwSwapBuffers()
@DasElias之外,我会从您的循环中删除所有内容:您不应该禁用v-sync
glfwSwapBuffers
不是“慢”——它故意等待监视器同步以防止撕裂。您向用户显示的帧数不能超过显示器的刷新率,刷新率可能为60Hz,帧间间隔约为17ms。无论如何,您都不能使用您的方法进行概要分析,因为OpenGL是流水线的,并且是异步执行的。出于分析目的,请使用
GL\u ARB\u timer\u query
查询GPU时间。@DasElias这实际上是预期的行为。OpenGL驱动程序倾向于在后台捆绑API调用,然后将它们发送到GPU以成批执行。这实际上减少了CPU和GPU之间的一些通信开销。OpenGL中的每个调用都是异步的,除了帧缓冲区交换,它会暂停调用线程,直到所有提交的函数都执行完毕。因此,glfwSwapBuffers似乎需要很长时间。
 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();
            
        }
    }