在LWJGL 3.0中给GL15.glBufferData()一个浮点数组或FloatBuffer有什么区别吗? [英] Is there any difference between giving GL15.glBufferData() a float array or a FloatBuffer in LWJGL 3.0?

查看:45
本文介绍了在LWJGL 3.0中给GL15.glBufferData()一个浮点数组或FloatBuffer有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在观看有关使用LWJGL用Java编写OpenGL游戏的教程.在其中,讲述人/作者首先将float数组从 float [] 转换为 FloatBuffer ,然后再将其提供给 GL15.glBufferData().最初我对此并不怎么想,但是我无意中给了 GL15.glBufferData()浮点数组,而不是将其转换为 FloatBuffer ,代码最终反正工作.我查找了文档,并且只说"Array version of:BufferData";作为评论,这仍然让我不确定.

I was recently watching a tutorial on writing an OpenGL game in Java with LWJGL. In it, the narrator/author first converts the float array from float[] to a FloatBuffer before giving it toGL15.glBufferData(). Initially I didn't think much of this, but I accidentally gave GL15.glBufferData() the float array directly instead of converting it to a FloatBuffer, and the code ended up working anyway. I looked up the documentation and it only says "Array version of: BufferData" as comment, which still leaves me uncertain.

这使我想知道,在调用 GL15.glBufferData()时,是否有任何努力将 float [] 转换为 FloatBuffer 在具有Java 15的LWJGL 3.0中,假设通过使用 FloatBuffer :: put() FloatBuffer 赋予了它的值,因此包含相同的数据吗?

This leads me to wonder, is there any point of going through the effort to convert float[] to a FloatBuffer when calling GL15.glBufferData() in LWJGL 3.0 with Java 15, assuming the FloatBuffer was given its value by using FloatBuffer::put() and thus contains the same data?

推荐答案

direct NIO缓冲区和float数组之间的区别在于,将float数组交给LWJGL时,首先需要使用JNI调用将数组固定到Java运行时中,这将为LWJGL的本机代码提供一个虚拟内存地址,该地址可以移交给实际的本机函数.然后,需要再次取消固定float数组,使其再次有资格进行垃圾回收.

The difference between a direct NIO Buffer and a float array is that when you hand a float array to LWJGL, it will first need to pin the array using a JNI call into the Java runtime, which will give LWJGL's native code a virtual memory address it can hand over to the actual called native function. Then, the float array needs to be unpinned again, making it eligible again for garbage collection.

使用 direct NIO缓冲区,这不需要发生,因为 direct NIO缓冲区已经由本机虚拟内存地址支持.因此,使用 direct NIO缓冲区要快得多(在性能方面).当然,当您首先创建一个float数组然后将其复制到NIO Buffer中时,所有这些性能都会有所损失.

With a direct NIO Buffer, this does not need to happen, because direct NIO Buffers are already backed by a native virtual memory address. So using direct NIO Buffers is considerably faster (performance-wise). Of course, all this performance is somewhat lost when you first create a float array and then copy it into the NIO Buffer.

您应该只是使用浮点数组,而只能填充 direct NIO缓冲区.

You are expected to simply not use a float array to being with, but only ever populate your direct NIO Buffer.

这篇关于在LWJGL 3.0中给GL15.glBufferData()一个浮点数组或FloatBuffer有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆