glMapBufferRange()在Android中使用安全/ Java的 [英] Safe usage of glMapBufferRange() on Android/Java

查看:657
本文介绍了glMapBufferRange()在Android中使用安全/ Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用工作code glMapBufferRange()从的OpenGL-ES 3.0的Andr​​oid上看起来是这样的:

I have working code using glMapBufferRange() from OpenGL-ES 3.0 on Android that looks like this:

  glBindBuffer(GL_ARRAY_BUFFER, myVertexBufferName);
  glBufferData(GL_ARRAY_BUFFER, myVertexBufferSize, null, GL_STATIC_DRAW);
  ByteBuffer mappedBuffer = (ByteBuffer)glMapBufferRange(
    GL_ARRAY_BUFFER,
    0, myVertexBufferSize,
    GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

  // [fill buffer...]

  glUnmapBuffer(GL_ARRAY_BUFFER);

我的问题是关于向下转换 glMapBufferRange()的结果的ByteBuffer 第三行。 glMapBufferRange()是<一个href="http://developer.android.com/reference/android/opengl/GLES30.html#glMapBufferRange(int,%20int,%20int,%20int)">declared返回缓存

My question is about downcasting the result of glMapBufferRange() to ByteBuffer on the third line. glMapBufferRange() is declared to return a Buffer:

公共静态缓冲glMapBufferRange(INT目标,诠释抵消,诠释长度,INT接入)

在我的测试平台,该函数返回的ByteBuffer 子类,所以剧组工作,但做这种假设适用于所有平台还是Android版本支持OpenGL-ES 3+没有按'牛逼看起来很安全的。尽管这似乎是合理的,我还没有找到,保证它的任何文件,如果它得到了保障,好像功能应该被声明为返回的ByteBuffer

On my test platform the function returns a subclass of ByteBuffer so the cast works, but making this assumption for all platforms or Android versions supporting OpenGL-ES 3+ doesn't seem very safe. Although it seems reasonable, I haven't found any documentation that guarantees it, and if it were guaranteed it seems like the function should be declared as returning ByteBuffer.

什么是正确的方式使用的缓存 glMapBufferRange返回()

What is the correct way (preferably supported by documentation) of using the Buffer returned by glMapBufferRange()?

推荐答案

正如你已经发现,文档缺乏。但仍然有相当确凿的参考:OpenGL的Java绑定的实现是公共Android源$ C ​​$ C部分

As you already found, the documentation is lacking. But there is still a fairly conclusive reference: The implementation of the OpenGL Java bindings is part of the public Android source code.

如果你看一下JNI包装的实施 glMapBufferRange(),它是文件<一中href="http://androidxref.com/5.1.0_r1/xref/frameworks/native/opengl/tools/glgen/stubs/gles11/glMapBufferRange.cpp">glMapBufferRange.cpp,你可以看到,缓冲区通过调用一个名为函数分配 NewDirectByteBuffer()。在此基础上,似乎可以假定缓冲区确实是一个的ByteBuffer

If you look at the implementation of the JNI wrapper for glMapBufferRange(), which is in the file glMapBufferRange.cpp, you can see that the buffer is allocated by calling a function named NewDirectByteBuffer(). Based on this, it seems safe to assume that the buffer is indeed a ByteBuffer.

虽然厂商可以改变安卓code,这似乎不太可能有人会改变Java绑定的行为(可能除了修复bug)。如果您担心的实施可能会改变在以后的And​​r​​oid版本,你当然可以使用标准的Java类型检查:

While vendors can change the Android code, it seems very unlikely that anybody would change the behavior of the Java bindings (except maybe to fix bugs). If you are concerned that the implementation could change in later Android versions, you can certainly use a standard Java type check:

Buffer buf = glMapBufferRange(...);
ByteBuffer byteBuf = null;
if (buf instanceof ByteBuffer) {
    byteBuf = (ByteBuffer)buf;
}

或者你可以使用更复杂的反思,开始调用的getClass()在返回的缓冲区。接下来的问题当然是你做什么,如果返回的缓冲区不是的ByteBuffer 。它就是这么对我来说很有意义的唯一类型。

Or you could use more elaborate reflection, starting with calling getClass() on the returned buffer. The next question is of course what you do if the returned buffer is not a ByteBuffer. It's really the only type that makes sense to me.

这篇关于glMapBufferRange()在Android中使用安全/ Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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