[Bf-blender-cvs] [af176760054] master: gpy.types.GPUUniformBuf: support any object exposing buffer interface

Germano Cavalcante noreply at git.blender.org
Wed Apr 13 03:57:33 CEST 2022


Commit: af176760054f2f3c17c524414d415dca9abb378e
Author: Germano Cavalcante
Date:   Tue Apr 12 22:44:57 2022 -0300
Branches: master
https://developer.blender.org/rBaf176760054f2f3c17c524414d415dca9abb378e

gpy.types.GPUUniformBuf: support any object exposing buffer interface

Since uniform buffers are generally structs and not sequences,
`BPyGPU_BufferType` objects are not suitable for filling the buffer.

===================================================================

M	source/blender/python/gpu/gpu_py_uniformbuffer.c

===================================================================

diff --git a/source/blender/python/gpu/gpu_py_uniformbuffer.c b/source/blender/python/gpu/gpu_py_uniformbuffer.c
index be3571d4f7c..f8f88d61cf6 100644
--- a/source/blender/python/gpu/gpu_py_uniformbuffer.c
+++ b/source/blender/python/gpu/gpu_py_uniformbuffer.c
@@ -64,17 +64,17 @@ static PyObject *pygpu_uniformbuffer__tp_new(PyTypeObject *UNUSED(self),
   BPYGPU_IS_INIT_OR_ERROR_OBJ;
 
   GPUUniformBuf *ubo = NULL;
-  BPyGPUBuffer *pybuffer_obj;
+  PyObject *pybuffer_obj;
   char err_out[256] = "unknown error. See console";
 
   static const char *_keywords[] = {"data", NULL};
   static _PyArg_Parser _parser = {
-      "O!" /* `data` */
+      "O" /* `data` */
       ":GPUUniformBuf.__new__",
       _keywords,
       0,
   };
-  if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &BPyGPU_BufferType, &pybuffer_obj)) {
+  if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &pybuffer_obj)) {
     return NULL;
   }
 
@@ -82,13 +82,19 @@ static PyObject *pygpu_uniformbuffer__tp_new(PyTypeObject *UNUSED(self),
     STRNCPY(err_out, "No active GPU context found");
   }
   else {
-    size_t size = bpygpu_Buffer_size(pybuffer_obj);
-    if ((size % 16) != 0) {
+    Py_buffer pybuffer;
+    if (PyObject_GetBuffer(pybuffer_obj, &pybuffer, PyBUF_SIMPLE) == -1) {
+      /* PyObject_GetBuffer raise a PyExc_BufferError */
+      return NULL;
+    }
+
+    if ((pybuffer.len % 16) != 0) {
       STRNCPY(err_out, "UBO is not padded to size of vec4");
     }
     else {
-      ubo = GPU_uniformbuf_create_ex(size, pybuffer_obj->buf.as_void, "python_uniformbuffer");
+      ubo = GPU_uniformbuf_create_ex(pybuffer.len, pybuffer.buf, "python_uniformbuffer");
     }
+    PyBuffer_Release(&pybuffer);
   }
 
   if (ubo == NULL) {
@@ -107,11 +113,14 @@ static PyObject *pygpu_uniformbuffer_update(BPyGPUUniformBuf *self, PyObject *ob
 {
   BPYGPU_UNIFORMBUF_CHECK_OBJ(self);
 
-  if (!BPyGPU_Buffer_Check(obj)) {
+  Py_buffer pybuffer;
+  if (PyObject_GetBuffer(obj, &pybuffer, PyBUF_SIMPLE) == -1) {
+    /* PyObject_GetBuffer raise a PyExc_BufferError */
     return NULL;
   }
 
-  GPU_uniformbuf_update(self->ubo, ((BPyGPUBuffer *)obj)->buf.as_void);
+  GPU_uniformbuf_update(self->ubo, pybuffer.buf);
+  PyBuffer_Release(&pybuffer);
   Py_RETURN_NONE;
 }
 
@@ -156,8 +165,8 @@ PyDoc_STRVAR(pygpu_uniformbuffer__tp_doc,
              "\n"
              "   This object gives access to off uniform buffers.\n"
              "\n"
-             "   :arg data: Buffer object.\n"
-             "   :type data: :class:`gpu.types.Buffer`\n");
+             "   :arg data: Data to fill the buffer.\n"
+             "   :type data: object exposing buffer interface\n");
 PyTypeObject BPyGPUUniformBuf_Type = {
     PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUUniformBuf",
     .tp_basicsize = sizeof(BPyGPUUniformBuf),



More information about the Bf-blender-cvs mailing list