haymo's picture

Shouldn't InterleavedArrays be fixed?

My question is regarding the usage of InterleavedArray(InterleavedArrayFormat format,Int32 stride, [In, Out] object vertices): I cant think of a situation it would be needed?

I see, you are pinning the pointer to the interleaved vertex array (via GCHandle). I dont know so much about the very internals of OpenGL, but I dont think, the GL will copy the data from the [vertices] array? It rather simply stores the pointer, right? And as soon as the function returns, the same pointer will be used for methods like DrawElements etc. But this time the GCHandle will be released already! Well, it probably will work most the time anyway, but obviously mostly because
1) the vertices array will often be a large object - not beeing moved by the GC. (Very fragile!). Also,
2) subsequent methods (f.e. DrawElements) will probably be called soon after InterleavedArray. But one should not rely on the GC not cleaning up right after InterleavedArray and eventually moving the vertices array away, especially if 1 not holds. -> also fragile!

So for me the only valid method seem to be, to pin the vertices array in a fixed block and use the IntPtr overload of InterleavedArray. Am I missing something here?


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

No, you are correct: you should never, ever use opengl Vertex Arrays in .Net, unless you know what you are doing (*). Or any other technique with client-side storage. You are free to use Vertex Buffer Objects however, since these will safely copy the data into server-memory (note that both techniques use the same functions, but with different setups).

If you were to ask me, there's no reason to use VAs anymore. VBOs are supported since the Geforce1/2 era, and are better in all regards.

(*) This generally includes pinning memory beforehand, allocating unmanaged blobs through Marshal.AllocHGlobal and other unsavoury techniques. :)

objarni's picture

Is there a book page summing up these kind of considerations somewhere..?

the Fiddler's picture

No, not yet, but it's in the todo list. There are two rules of thumb: use server storage rather than client storage and try to minimize the number of OpenGL calls per frame. This generally means use display lists, yes VBOs, avoid immediate mode, avoid VAs.

Inertia's picture

The main problem with VA is that the GPU is not in sync with the CPU in more complicated scenes than drawing a simple quad.

The CPU might have release the pin required for VA to work by the time the GPU starts processing the GL.VertexPointer location in memory, leading to confusing exceptions (I've experienced some with an early version of the PQ torusknot app).

Actually I'd not recommend using VA unless you intend to wrap them into a DL at load time (and use GL.Finish to make sure CPU/GPU are in sync).