iliak's picture

[PBO] How to use GL.MapBuffer() ?

How can I access pixel data in a PBO with this method :

IntPtr ptr = GL.MapBuffer(BufferTarget.PixelUnpackBuffer, BufferAccess.ReadWrite);

How can I convert from a IntPtr to a byte[] ? I can allocate memory pointed to by IntPtr with Marshal.AllocHGlobal
and copy data to it with Marshal.Copy, but I loose the advantage of the PBO.


Comment viewing options

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

GL.MapBuffer returns a pointer to unmanaged memory. You cannot move this over to managed memory (byte[]) without copying data.

However, you can use unsafe code to cast the IntPtr to byte* and access the data like that. Alternatively, you can use Marshal.ReadByte() and Marshal.WriteByte and avoid unsafe code altogether.

iliak's picture

Logic would dictate that we should not copy the data because it loses the benefit of the extension. What would be the best way... ?

the Fiddler's picture

Either use an unsafe pointer or Marshal.Read/WriteByte(). "Best" here depends on your requirements - for example, you may wish to avoid unsafe code, or you may be using a language which doesn't support pointers (e.g. VB.Net) so you are left with the Marshal class. C# can use both approaches.

Inertia's picture

There's a code snippet how to use unsafe code with GL.MapBuffer in the documentation.

If you do not require random access to the buffer, use GL.BufferSubData and GL.GetBufferSubData to perform copies.