I am starting this thread to continue a discussion on the use of Buffers with JOGL that have arisen as a side-issue on this thread:
http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=jogl;action=display;num=1106324571
Ken Russell wrote:
[quote] Direct buffer usage, at least FloatBuffer and other types, should be very fast wth HotSpot. ByteBuffer is currently more of a problem (fixes for this problem are coming), but if you cast your direct ByteBuffers to MappedByteBuffer you should see very high speed with those buffer types as well. I would think that you could store your data in a large FloatBuffer and still get efficient access for collision detection and other purposes. Take a look at the source code for the Grand Canyon demo (an update of that demo to use JOGL rather than GL4Java is presently underway); it performs collision detection against the ground, the data for which is contained in a direct ShortBuffer (16-bit ground heights).
[/quote]
Ken - I should probably start with a lengthy aside:
[aside]
These were the problems that I saw when trying to move to Buffers and that my alternate approach which is to put my vertex,normal and texcoord data in an interleaved float array is every bit as fast as using Buffers, for the following reasons:
- My V,N,TC data is generated programmatically into the float[]
- The float[] is used to create a static draw VBO which involves a single lock-access-unlock of the float array
- All subsequent rendering is done by binding and using the VBO, and hence is fast.
So I don’t think there is a benefit to moving to use Buffers. ( There is one possible exception to this in that I don’t properly understand the memory issues associated with what an OpenGL driver does with VBOs and whether there might be an extra copy of the data around for a VBO created from a Buffer vs. from a float[], and whether this even depends on whether it’s a GL_STATIC_DRAW_ARB VBO or not - comments on this are welcome )
[/aside]
Having said that I’m interested in understanding if my issues with Buffers could have been resolved, so let’s continue
Your comment about the performance of FloatBuffers is very interesting - my code is now much better structured to allow different data representations of the vertex data, so I might have a another go at encoding this representation at some point. Thanks for the suggestion.
Next, you suggest ‘casting’ a ByteBuffer to MappedByteBuffer. A couple of questions:
-
Did you mean ‘cast’ or did you just mean ‘use a MappedByteBuffer’ ? (I don’t see how casting is going to achieve anything other than maybe a ClassCastException :-))
-
Are you suggesting that the file underlying the MappedByteBuffer is the source of the vertex data ? I’m struggling a bit with why introducing a file underlying my vertex data would improve performance, as it would seem like an extra overhead ?
[quote] I agree that the issue of direct buffer alignment on certain OSs is nasty. I added (Sun-internal) comments to bug 4820023. If memory consumption for small direct buffers were more reasonable, would it be easier for you to use them in your application?
[/quote]
Yes, certainly. However, as I said in my [aside] this isn’t currently causing me an issue as I adopted a different approach. It would be great to have the option of using Buffers though, and this was the drop-dead issue for me.
Rob