Page MenuHome

Buffer Protocol for bgl.Buffer

Authored by Germano Cavalcante (mano-wii) on Jul 3 2017, 9:15 PM.



I needed to transmit the data of the rendered image by socket. Therefore I used numpy to convert the bgl.Buffer into a numpy array.

The function numpy.asarray(bufferObject) will access the buffer protocol if the given object implements that. Without the buffer protocol, the to_list() function will get triggered.

Using the to_list() function I had a performance of 0.5fps since an humongous array needs to get copied. With the buffer protocol, I have a speed up by at least 60x (>30fps).

I tried to stick to the conventions used for the IDProperty which implements the buffer protocl as well.

Diff Detail

Event Timeline

Markus Kohout (ricorodriguez) created this object with edit policy "Administrators".
Markus Kohout (ricorodriguez) changed the edit policy from "Administrators" to "All Users".

Actually I don't have tested it, I also don't have experience with the buffer protocol and if there are no other issues with it.
Did you have tested the buffer also with some of the other bgl function calls like glTexImage2D()?
Does slicing the buffer still work?


Use C style comment here.
/* Number of entries in the buffer */


Seems that the newline at end of file is missing.
I think changing this is not necessary.

Germano Cavalcante (mano-wii) updated this revision to Diff 9289.

It really makes sense bgl.Buffer to have a Buffer Protocol.
I made some changes to the code.

Germano Cavalcante (mano-wii) marked an inline comment as done.Sep 20 2017, 4:33 AM

I added Tristan Porteries (panzergame), Ulysse Martin (youle) from the UPBGE team. I think the have more experience in this kind of things.

I undo some of my changes. It may be best to leave the original Buffer structure untouched.

Hello, I tested with most of my files using bgl and it worked fine for me. I had only 1 file to test with bge.texture and bgl however. But this sounds ok to me.

Strides are needed when working with multidimensional array.

  • added strides and ndim;
  • Missed MEM_freeN(strides) if there is an error in PyBuffer_FillInfo;
  • flags are not really being used;