Blender V5.3
gpu_py_uniformbuffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
13
14#include <Python.h>
15
16#include "BLI_string_utf8.hh"
17
18#include "GPU_context.hh"
19#include "GPU_uniform_buffer.hh"
20
21#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
22
23#include "gpu_py.hh"
24#include "gpu_py_uniformbuffer.hh" /* own include */
25
26namespace blender {
27
28/* -------------------------------------------------------------------- */
31
33{
34 if (bpygpu_ub->ubo == nullptr) [[unlikely]] {
35 PyErr_SetString(PyExc_ReferenceError,
37 "GPU uniform buffer was freed, no further access is valid");
38#else
39
40 "GPU uniform buffer: internal error");
41#endif
42 return -1;
43 }
44 return 0;
45}
46
47#define BPYGPU_UNIFORMBUF_CHECK_OBJ(bpygpu) \
48 { \
49 if (pygpu_uniformbuffer_valid_check(bpygpu) == -1) [[unlikely]] { \
50 return nullptr; \
51 } \
52 } \
53 ((void)0)
54
56
57/* -------------------------------------------------------------------- */
60
61static PyObject *pygpu_uniformbuffer__tp_new(PyTypeObject * /*self*/,
62 PyObject *args,
63 PyObject *kwds)
64{
66
67 gpu::UniformBuf *ubo = nullptr;
68 PyObject *pybuffer_obj;
69 char err_out[256] = "unknown error. See console";
70
71 static const char *_keywords[] = {"data", nullptr};
72 static _PyArg_Parser _parser = {
73 "O" /* `data` */
74 ":GPUUniformBuf.__new__",
75 _keywords,
76 nullptr,
77 };
78 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &pybuffer_obj)) {
79 return nullptr;
80 }
81
83 STRNCPY_UTF8(err_out, "No active GPU context found");
84 }
85 else {
86 Py_buffer pybuffer;
87 if (PyObject_GetBuffer(pybuffer_obj, &pybuffer, PyBUF_SIMPLE) == -1) {
88 /* PyObject_GetBuffer raise a PyExc_BufferError */
89 return nullptr;
90 }
91
92 if ((pybuffer.len % 16) != 0) {
93 STRNCPY_UTF8(err_out, "UBO is not padded to size of vec4");
94 }
95 else {
96 ubo = GPU_uniformbuf_create_ex(pybuffer.len, pybuffer.buf, "python_uniformbuffer");
97 }
98 PyBuffer_Release(&pybuffer);
99 }
100
101 if (ubo == nullptr) {
102 PyErr_Format(PyExc_RuntimeError, "GPUUniformBuf.__new__(...) failed with '%s'", err_out);
103 return nullptr;
104 }
105
107}
108
110 /* Wrap. */
111 pygpu_uniformbuffer_update_doc,
112 ".. method:: update(data)\n"
113 "\n"
114 " Update the data of the uniform buffer object.\n"
115 "\n"
116 " :param data: Data to fill the buffer.\n"
117 " :type data: Buffer\n");
118static PyObject *pygpu_uniformbuffer_update(BPyGPUUniformBuf *self, PyObject *obj)
119{
121
122 Py_buffer pybuffer;
123 if (PyObject_GetBuffer(obj, &pybuffer, PyBUF_SIMPLE) == -1) {
124 /* PyObject_GetBuffer raise a PyExc_BufferError */
125 return nullptr;
126 }
127
128 GPU_uniformbuf_update(self->ubo, pybuffer.buf);
129 PyBuffer_Release(&pybuffer);
130 Py_RETURN_NONE;
131}
132
133#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
135 /* Wrap. */
136 pygpu_uniformbuffer_free_doc,
137 ".. method:: free()\n"
138 "\n"
139 " Free the uniform buffer object.\n"
140 " The uniform buffer object will no longer be accessible.\n");
141static PyObject *pygpu_uniformbuffer_free(BPyGPUUniformBuf *self)
142{
144
145 GPU_uniformbuf_free(self->ubo);
146 self->ubo = nullptr;
147 Py_RETURN_NONE;
148}
149#endif
150
152{
153 if (self->ubo) {
155 }
156 Py_TYPE(self)->tp_free(reinterpret_cast<PyObject *>(self));
157}
158
159static PyGetSetDef pygpu_uniformbuffer__tp_getseters[] = {
160 {nullptr, nullptr, nullptr, nullptr, nullptr} /* Sentinel */
161};
162
163static PyMethodDef pygpu_uniformbuffer__tp_methods[] = {
164 {"update",
165 reinterpret_cast<PyCFunction>(pygpu_uniformbuffer_update),
166 METH_O,
167 pygpu_uniformbuffer_update_doc},
168#ifdef BPYGPU_USE_GPUOBJ_FREE_METHOD
169 {"free", (PyCFunction)pygpu_uniformbuffer_free, METH_NOARGS, pygpu_uniformbuffer_free_doc},
170#endif
171 {nullptr, nullptr, 0, nullptr},
172};
173
175 /* Wrap. */
176 pygpu_uniformbuffer__tp_doc,
177 ".. class:: GPUUniformBuf\n"
178 "\n"
179 " This object gives access to uniform buffers.\n"
180 "\n"
181 " .. method:: __init__(data)\n"
182 "\n"
183 " :param data: Data to fill the buffer.\n"
184 " :type data: Buffer\n");
185PyTypeObject BPyGPUUniformBuf_Type = {
186 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
187 /*tp_name*/ "GPUUniformBuf",
188 /*tp_basicsize*/ sizeof(BPyGPUUniformBuf),
189 /*tp_itemsize*/ 0,
190 /*tp_dealloc*/ reinterpret_cast<destructor>(BPyGPUUniformBuf__tp_dealloc),
191 /*tp_vectorcall_offset*/ 0,
192 /*tp_getattr*/ nullptr,
193 /*tp_setattr*/ nullptr,
194 /*tp_as_async*/ nullptr,
195 /*tp_repr*/ nullptr,
196 /*tp_as_number*/ nullptr,
197 /*tp_as_sequence*/ nullptr,
198 /*tp_as_mapping*/ nullptr,
199 /*tp_hash*/ nullptr,
200 /*tp_call*/ nullptr,
201 /*tp_str*/ nullptr,
202 /*tp_getattro*/ nullptr,
203 /*tp_setattro*/ nullptr,
204 /*tp_as_buffer*/ nullptr,
205 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
206 /*tp_doc*/ pygpu_uniformbuffer__tp_doc,
207 /*tp_traverse*/ nullptr,
208 /*tp_clear*/ nullptr,
209 /*tp_richcompare*/ nullptr,
210 /*tp_weaklistoffset*/ 0,
211 /*tp_iter*/ nullptr,
212 /*tp_iternext*/ nullptr,
213 /*tp_methods*/ pygpu_uniformbuffer__tp_methods,
214 /*tp_members*/ nullptr,
216 /*tp_base*/ nullptr,
217 /*tp_dict*/ nullptr,
218 /*tp_descr_get*/ nullptr,
219 /*tp_descr_set*/ nullptr,
220 /*tp_dictoffset*/ 0,
221 /*tp_init*/ nullptr,
222 /*tp_alloc*/ nullptr,
224 /*tp_free*/ nullptr,
225 /*tp_is_gc*/ nullptr,
226 /*tp_bases*/ nullptr,
227 /*tp_mro*/ nullptr,
228 /*tp_cache*/ nullptr,
229 /*tp_subclasses*/ nullptr,
230 /*tp_weaklist*/ nullptr,
231 /*tp_del*/ nullptr,
232 /*tp_version_tag*/ 0,
233 /*tp_finalize*/ nullptr,
234 /*tp_vectorcall*/ nullptr,
235};
236
238
239/* -------------------------------------------------------------------- */
242
244{
245 BPyGPUUniformBuf *self;
246
247 self = PyObject_New(BPyGPUUniformBuf, &BPyGPUUniformBuf_Type);
248 self->ubo = ubo;
249
250 return reinterpret_cast<PyObject *>(self);
251}
252
254
255#undef BPYGPU_UNIFORMBUF_CHECK_OBJ
256
257} // 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")
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
#define BPYGPU_USE_GPUOBJ_FREE_METHOD
#define BPYGPU_UNIFORMBUF_CHECK_OBJ(bpygpu)
BMesh Iterators.
PyObject * BPyGPUUniformBuf_CreatePyObject(gpu::UniformBuf *ubo)
void GPU_uniformbuf_free(gpu::UniformBuf *ubo)
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 PyMethodDef pygpu_uniformbuffer__tp_methods[]
static int pygpu_uniformbuffer_valid_check(BPyGPUUniformBuf *bpygpu_ub)
static void BPyGPUUniformBuf__tp_dealloc(BPyGPUUniformBuf *self)
gpu::UniformBuf * GPU_uniformbuf_create_ex(size_t size, const void *data, const char *name)
PyTypeObject BPyGPUUniformBuf_Type
void GPU_uniformbuf_update(gpu::UniformBuf *ubo, const void *data)
static PyGetSetDef pygpu_uniformbuffer__tp_getseters[]
static PyObject * pygpu_uniformbuffer_update(BPyGPUUniformBuf *self, PyObject *obj)
static PyObject * pygpu_uniformbuffer__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
GPUContext * GPU_context_active_get()
header-only compatibility defines.
PyObject_HEAD gpu::UniformBuf * ubo