pyopengl
pyopengl copied to clipboard
Support read-only numpy arrays
When using opengl-accelerate, many operations will fail if passed a numpy.array which is contiguous, but is not writeable, e.g.:
print(value.flags)
gl.glBufferData(gl.GL_ARRAY_BUFFER,
value.nbytes,
value,
gl.GL_STATIC_DRAW)
results in:
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : False
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
Traceback (most recent call last):
...
File ".../program.py", line 313, in setAtt
gl.GL_STATIC_DRAW)
File "latebind.pyx", line 44, in OpenGL_accelerate.latebind.Curry.__call__ (src/latebind.c:1201)
File ".../OpenGL/GL/VERSION/GL_1_5.py", line 86, in glBufferData
data = ArrayDatatype.asArray( data )
File "arraydatatype.pyx", line 176, in OpenGL_accelerate.arraydatatype.ArrayDatatype.asArray (src/arraydatatype.c:4253)
File "numpy_formathandler.pyx", line 112, in OpenGL_accelerate.numpy_formathandler.NumpyHandler.c_asArray (src/numpy_formathandler.c:2689)
File "numpy_formathandler.pyx", line 132, in OpenGL_accelerate.numpy_formathandler.NumpyHandler.contiguous (src/numpy_formathandler.c:3045)
OpenGL.error.CopyError: ('Non-contiguous array passed', array([0.00093461, 0.00061148, 0.00495276, ..., 0.00381922, 0.00935766,
0.00224066], dtype=float32))
This is because accelerate/src/numpy_formathandler.pyx uses the PyArray_ISCARRAY function to test whether an array is contiguous. However, PyArray_ISCARRAY also tests whether an array is writeable.
This PR simply changes numpy_formathandler.pyx so it uses PyArray_ISCARRAY_RO rather than PyArray_ISCARRAY, so that read-only arrays are accepted.
This issue has arisen in my work due to a recent change in numpy which causes data loaded via np.frombuffer to be set as read-only.
The change to numpy_formathandler.pyx seems to work fine for me, although I am not sure whether some circumstances do actually require arrays to be writeable. If this is the case, please disregard this PR.
It appears that equivalent changes have been made to the codebase, so I think this PR should be closed.