Blender V5.3
gpu_py_storagebuffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include <Python.h>
15
16#include "BLI_math_base_c.hh"
17#include "BLI_string_utf8.hh"
18#include "BLI_vector.hh"
19
20#include "MEM_guardedalloc.h"
21
22#include "GPU_context.hh"
23#include "GPU_storage_buffer.hh"
24#include "GPU_texture.hh"
25
26#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
27
28#include "gpu_py.hh"
29#include "gpu_py_buffer.hh"
30#include "gpu_py_storagebuffer.hh" /* own include */
31
32namespace blender {
33
34/* -------------------------------------------------------------------- */
37
39{
40 if (bpygpu_ssbo->ssbo == nullptr) [[unlikely]] {
41 PyErr_SetString(PyExc_ReferenceError,
43 "GPU storage buffer was freed, no further access is valid");
44#else
45
46 "GPU storage buffer: internal error");
47#endif
48 return -1;
49 }
50 return 0;
51}
52
53#define BPYGPU_STORAGEBUF_CHECK_OBJ(bpygpu) \
54 { \
55 if (pygpu_storagebuffer_valid_check(bpygpu) == -1) [[unlikely]] { \
56 return nullptr; \
57 } \
58 } \
59 ((void)0)
60
62static size_t pygpu_storagebuffer_padded_size(const Py_buffer &pybuffer)
63{
64 return ceil_to_multiple_ul(size_t(pybuffer.len), 16);
65}
66
70static const void *pygpu_storagebuffer_data_padded(const Py_buffer &pybuffer,
71 Vector<uchar> &r_padded_data)
72{
73 const size_t size = size_t(pybuffer.len);
74 const size_t padded_size = pygpu_storagebuffer_padded_size(pybuffer);
75 if (size == padded_size) {
76 return pybuffer.buf;
77 }
78 r_padded_data.resize(padded_size, 0);
79 memcpy(r_padded_data.data(), pybuffer.buf, size);
80 return r_padded_data.data();
81}
82
84
85/* -------------------------------------------------------------------- */
88
89static PyObject *pygpu_storagebuffer__tp_new(PyTypeObject * /*self*/,
90 PyObject *args,
91 PyObject *kwds)
92{
94
95 gpu::StorageBuf *ssbo = nullptr;
96 PyObject *pybuffer_obj;
97 char err_out[256] = "unknown error. See console";
98
99 static const char *_keywords[] = {"data", nullptr};
100 static _PyArg_Parser _parser = {
101 "O" /* `data` */
102 ":GPUStorageBuf.__new__",
103 _keywords,
104 nullptr,
105 };
106 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &pybuffer_obj)) {
107 return nullptr;
108 }
109
110 if (!GPU_context_active_get()) {
111 STRNCPY_UTF8(err_out, "No active GPU context found");
112 }
113 else {
114 Py_buffer pybuffer;
115 if (PyObject_GetBuffer(pybuffer_obj, &pybuffer, PyBUF_SIMPLE) == -1) {
116 /* PyObject_GetBuffer raise a PyExc_BufferError */
117 return nullptr;
118 }
119
120 Vector<uchar> padded_data;
121 const void *data = pygpu_storagebuffer_data_padded(pybuffer, padded_data);
123 data,
125 "python_storagebuffer");
126 PyBuffer_Release(&pybuffer);
127 }
128
129 if (ssbo == nullptr) {
130 PyErr_Format(PyExc_RuntimeError, "GPUStorageBuf.__new__(...) failed with '%s'", err_out);
131 return nullptr;
132 }
133
135}
136
138 /* Wrap. */
139 pygpu_storagebuffer_update_doc,
140 ".. method:: update(data)\n"
141 "\n"
142 " Update the data of the storage buffer object.\n"
143 "\n"
144 " :param data: Data to fill the buffer.\n"
145 " :type data: Buffer\n");
146static PyObject *pygpu_storagebuffer_update(BPyGPUStorageBuf *self, PyObject *obj)
147{
149
150 Py_buffer pybuffer;
151 if (PyObject_GetBuffer(obj, &pybuffer, PyBUF_SIMPLE) == -1) {
152 /* PyObject_GetBuffer raise a PyExc_BufferError */
153 return nullptr;
154 }
155
156 /* The backends copy exactly the size the buffer was created with, regardless of how much
157 * data is actually passed in. Providing less would read past the end of `pybuffer`. */
158 const size_t size = GPU_storagebuf_usage_size_get(self->ssbo);
159 if (pygpu_storagebuffer_padded_size(pybuffer) != size) {
160 PyErr_Format(PyExc_ValueError,
161 "GPUStorageBuf.update(): expected a buffer of size %zu "
162 "(once padded to a multiple of 16 bytes), got %zu",
163 size,
164 size_t(pybuffer.len));
165 PyBuffer_Release(&pybuffer);
166 return nullptr;
167 }
168
169 Vector<uchar> padded_data;
170 GPU_storagebuf_update(self->ssbo, pygpu_storagebuffer_data_padded(pybuffer, padded_data));
171 PyBuffer_Release(&pybuffer);
172 Py_RETURN_NONE;
173}
174
176 /* Wrap. */
177 pygpu_storagebuffer_clear_to_zero_doc,
178 ".. method:: clear_to_zero()\n"
179 "\n"
180 " Clear the storage buffer data to zero.\n");
182{
184
186 Py_RETURN_NONE;
187}
188
190 /* Wrap. */
191 pygpu_storagebuffer_read_doc,
192 ".. method:: read()\n"
193 "\n"
194 " Read back the contents of the storage buffer.\n"
195 " This waits until all GPU operations are finished, performing the necessary "
196 "synchronization.\n"
197 "\n"
198 " :return: The Buffer with the read data.\n"
199 " :rtype: :class:`gpu.types.Buffer`\n");
201{
203
204 const size_t size = GPU_storagebuf_usage_size_get(self->ssbo);
205 void *buf = MEM_new_uninitialized(size, "python_storagebuffer_read");
206 GPU_storagebuf_read(self->ssbo, buf);
207
208 const Py_ssize_t shape = Py_ssize_t(size);
209 return reinterpret_cast<PyObject *>(
211}
212
213#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
215 /* Wrap. */
216 pygpu_storagebuffer_free_doc,
217 ".. method:: free()\n"
218 "\n"
219 " Free the storage buffer object.\n"
220 " The storage buffer object will no longer be accessible.\n");
221static PyObject *pygpu_storagebuffer_free(BPyGPUStorageBuf *self)
222{
224
225 GPU_storagebuf_free(self->ssbo);
226 self->ssbo = nullptr;
227 Py_RETURN_NONE;
228}
229#endif
230
232{
233 if (self->ssbo) {
235 }
236 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
237}
238
239static PyGetSetDef pygpu_storagebuffer__tp_getseters[] = {
240 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
241};
242
243static PyMethodDef pygpu_storagebuffer__tp_methods[] = {
244 {"update",
245 reinterpret_cast<PyCFunction>(pygpu_storagebuffer_update),
246 METH_O,
247 pygpu_storagebuffer_update_doc},
248 {"clear_to_zero",
249 reinterpret_cast<PyCFunction>(pygpu_storagebuffer_clear_to_zero),
250 METH_NOARGS,
251 pygpu_storagebuffer_clear_to_zero_doc},
252 {"read",
253 reinterpret_cast<PyCFunction>(pygpu_storagebuffer_read),
254 METH_NOARGS,
255 pygpu_storagebuffer_read_doc},
256#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
257 {"free", (PyCFunction)pygpu_storagebuffer_free, METH_NOARGS, pygpu_storagebuffer_free_doc},
258#endif
259 {nullptr, nullptr, 0, nullptr},
260};
261
263 /* Wrap. */
264 pygpu_storagebuffer__tp_doc,
265 ".. class:: GPUStorageBuf\n"
266 "\n"
267 " This object gives access to storage buffers.\n"
268 "\n"
269 " .. method:: __init__(data)\n"
270 "\n"
271 " :param data: Data to fill the buffer.\n"
272 " :type data: Buffer\n");
273PyTypeObject BPyGPUStorageBuf_Type = {
274 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
275 /*tp_name*/ "GPUStorageBuf",
276 /*tp_basicsize*/ sizeof(BPyGPUStorageBuf),
277 /*tp_itemsize*/ 0,
278 /*tp_dealloc*/ reinterpret_cast<destructor>(BPyGPUStorageBuf__tp_dealloc),
279 /*tp_vectorcall_offset*/ 0,
280 /*tp_getattr*/ nullptr,
281 /*tp_setattr*/ nullptr,
282 /*tp_as_async*/ nullptr,
283 /*tp_repr*/ nullptr,
284 /*tp_as_number*/ nullptr,
285 /*tp_as_sequence*/ nullptr,
286 /*tp_as_mapping*/ nullptr,
287 /*tp_hash*/ nullptr,
288 /*tp_call*/ nullptr,
289 /*tp_str*/ nullptr,
290 /*tp_getattro*/ nullptr,
291 /*tp_setattro*/ nullptr,
292 /*tp_as_buffer*/ nullptr,
293 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
294 /*tp_doc*/ pygpu_storagebuffer__tp_doc,
295 /*tp_traverse*/ nullptr,
296 /*tp_clear*/ nullptr,
297 /*tp_richcompare*/ nullptr,
298 /*tp_weaklistoffset*/ 0,
299 /*tp_iter*/ nullptr,
300 /*tp_iternext*/ nullptr,
301 /*tp_methods*/ pygpu_storagebuffer__tp_methods,
302 /*tp_members*/ nullptr,
304 /*tp_base*/ nullptr,
305 /*tp_dict*/ nullptr,
306 /*tp_descr_get*/ nullptr,
307 /*tp_descr_set*/ nullptr,
308 /*tp_dictoffset*/ 0,
309 /*tp_init*/ nullptr,
310 /*tp_alloc*/ nullptr,
312 /*tp_free*/ nullptr,
313 /*tp_is_gc*/ nullptr,
314 /*tp_bases*/ nullptr,
315 /*tp_mro*/ nullptr,
316 /*tp_cache*/ nullptr,
317 /*tp_subclasses*/ nullptr,
318 /*tp_weaklist*/ nullptr,
319 /*tp_del*/ nullptr,
320 /*tp_version_tag*/ 0,
321 /*tp_finalize*/ nullptr,
322 /*tp_vectorcall*/ nullptr,
323};
324
326
327/* -------------------------------------------------------------------- */
330
332{
333 BPyGPUStorageBuf *self;
334
335 self = PyObject_New(BPyGPUStorageBuf, &BPyGPUStorageBuf_Type);
336 self->ssbo = ssbo;
337
338 return reinterpret_cast<PyObject *>(self);
339}
340
342
343#undef BPYGPU_STORAGEBUF_CHECK_OBJ
344
345} // namespace blender
#define STRNCPY_UTF8(dst, src)
PyDoc_STRVAR(FalseBP1D___doc__, "Class hierarchy: :class:`freestyle.types.BinaryPredicate1D` > :class:`FalseBP1D`\n" "\n" ".. method:: __call__(inter1, inter2)\n" "\n" " Always returns false.\n" "\n" " :param inter1: The first Interface1D object.\n" " :type inter1: :class:`freestyle.types.Interface1D`\n" " :param inter2: The second Interface1D object.\n" " :type inter2: :class:`freestyle.types.Interface1D`\n" " :return: False.\n" " :rtype: bool\n")
Read Blender memory allocation and freeing.
void resize(const int64_t new_size)
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
#define BPYGPU_USE_GPUOBJ_FREE_METHOD
#define BPYGPU_STORAGEBUF_CHECK_OBJ(bpygpu)
BMesh Iterators.
void * MEM_new_uninitialized(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void GPU_storagebuf_update(gpu::StorageBuf *ssbo, const void *data)
void GPU_storagebuf_clear_to_zero(gpu::StorageBuf *ssbo)
static PyObject * pygpu_storagebuffer__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
gpu::StorageBuf * GPU_storagebuf_create_ex(size_t size, const void *data, GPUUsageType usage, const char *name)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
static int pygpu_storagebuffer_valid_check(BPyGPUStorageBuf *bpygpu_ssbo)
static size_t pygpu_storagebuffer_padded_size(const Py_buffer &pybuffer)
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
BMesh const char void * data
PyTypeObject BPyGPUStorageBuf_Type
size_t GPU_storagebuf_usage_size_get(const gpu::StorageBuf *ssbo)
static PyGetSetDef pygpu_storagebuffer__tp_getseters[]
static PyObject * pygpu_storagebuffer_update(BPyGPUStorageBuf *self, PyObject *obj)
BPyGPUBuffer * BPyGPU_Buffer_CreatePyObject(const int format, const Py_ssize_t *shape, const int shape_len, void *buffer)
static PyObject * pygpu_storagebuffer_clear_to_zero(BPyGPUStorageBuf *self)
static PyObject * pygpu_storagebuffer_read(BPyGPUStorageBuf *self)
static const void * pygpu_storagebuffer_data_padded(const Py_buffer &pybuffer, Vector< uchar > &r_padded_data)
static void BPyGPUStorageBuf__tp_dealloc(BPyGPUStorageBuf *self)
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
PyObject * BPyGPUStorageBuf_CreatePyObject(gpu::StorageBuf *ssbo)
static PyMethodDef pygpu_storagebuffer__tp_methods[]
GPUContext * GPU_context_active_get()
void GPU_storagebuf_read(gpu::StorageBuf *ssbo, void *data)
header-only compatibility defines.
PyObject_HEAD gpu::StorageBuf * ssbo