migueltk's picture

Help with "GL.MapBuffer ()"

Hello, ....

In the following code I get an error when compiling:

      float[] array;
      array = (float[])GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly);

Error: Can not convert type 'System.IntPtr' to 'float []'

How I can do the assignment correctly?

Help, please ...

Regards, ...


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 a blob of unmanaged memory. AFAIK, you have two options:

  1. Use an unsafe region to cast the IntPtr to float*:
        float* array = (float*)GL.MapBuffer(...);
        // Read the value at offset 15:
        float val = *(array + 15);
        // Write a value at offset 0:
        *(array + 0) = 0.5f;
  2. Use Marshal.Copy() to copy the unmanaged buffer into a managed array and vice versa.

The first approach is likely to be faster.

migueltk's picture

Thank you very much ...

Preliminary results are strange, for dynamic update of vertices, using arrays of vertices is faster than using VBO. I suppose that by using arrays of vertices, the transfer by the graphics card is done by DMA (Direct Memory Access) and in the case of using VBO no DMA.

Simple Vertex Array: Average FPS = 113 Max.FPS = 102
VBO (BufferUsageHint.DynamicDraw): Average FPS = 55 Max.FPS = 101

Preliminary conclusion, using VBO is best when used with shader programs, otherwise use arrays of vertices.