musmuris's picture

GL.BufferData takes an InPtr?

Hi,
Just taking a look at OpenTK to try and do some 3D stuff in C# and trying to learn modern OpenGL as well.

I noticed that GL.BufferData takes an IntPtr - but the actual calls seem to just multiply ints together to pass in. Is this just an artefact of how it's bound, or was there another reason it can't just take an int?


Comments

Comment viewing options

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

This is how GL.BufferData is defined in the OpenGL registry:

        <command>
            <proto>void <name>glBufferData</name></proto>
            <param group="BufferTargetARB"><ptype>GLenum</ptype> <name>target</name></param>
            <param group="BufferSize"><ptype>GLsizeiptr</ptype> <name>size</name></param>
            <param len="size">const void *<name>data</name></param>
            <param group="BufferUsageARB"><ptype>GLenum</ptype> <name>usage</name></param>
        </command>

The C headers translate this to:

typedef ptrdiff_t GLsizeiptr;
void glBufferData(GLenum target, GLsizeiptr size, const void *data, GLenum usage);

In OpenTK, this is mapped to the following signatures:

void BufferData(BufferTarget target, IntPtr size, IntPtr data, BufferUsageHint usage);
void BufferData<T>(BufferTarget target, IntPtr size, ref T data, BufferUsageHint usage);
void BufferData<T>(BufferTarget target, IntPtr size, T[] data, BufferUsageHint usage);
void BufferData<T>(BufferTarget target, IntPtr size, T[,] data, BufferUsageHint usage);
void BufferData<T>(BufferTarget target, IntPtr size, T[,,] data, BufferUsageHint usage);

which match the specification exactly.

An int overload for size would be helpful, but it would have to be implemented manually in "Source/OpenTK/Graphics/[API]/GLHelper.cs". There are 6 relevant APIs (OpenGL, OpenGL4, ES10, ES11, ES20, ES30), so 30 overloads in total. I would happily accept a PR against https://github.com/opentk/opentk that implements this.

musmuris's picture

Ah - That explains it. In C++ using ptrdiff_t doesn't matter too much as that probably is typedef'd to uint or uint64 depending on what it is. Sadly although it's strictly correct to use IntPtr in C# (though nearly all of the time I bet an int would suffice) it just makes a big ugly :(
Fair point about a PR - though I'd like to get a lot more familiar with OpenGL and OpenTK first!

Thanks for the answer.