
[PBO] How to use GL.MapBuffer() ?
Posted Friday, 6 November, 2009 - 08:15 by iliak inHow 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.


Comments
Re: [PBO] How to use GL.MapBuffer() ?
GL.MapBufferreturns 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
IntPtrtobyte*and access the data like that. Alternatively, you can useMarshal.ReadByte()andMarshal.WriteByteand avoid unsafe code altogether.Re: [PBO] How to use GL.MapBuffer() ?
Logic would dictate that we should not copy the data because it loses the benefit of the extension. What would be the best way... ?
Re: [PBO] How to use GL.MapBuffer() ?
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.Re: [PBO] How to use GL.MapBuffer() ?
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.