Blender V4.5
gpu_py_vertex_buffer.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
11
12#include <Python.h>
13
14#include "GPU_vertex_buffer.hh"
15
17#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
18
19#include "gpu_py.hh"
20#include "gpu_py_vertex_buffer.hh" /* own include */
22
23/* -------------------------------------------------------------------- */
26
27#define PYGPU_AS_NATIVE_SWITCH(attr) \
28 switch (attr->comp_type) { \
29 case GPU_COMP_I8: { \
30 PY_AS_NATIVE(int8_t, PyC_Long_AsI8); \
31 break; \
32 } \
33 case GPU_COMP_U8: { \
34 PY_AS_NATIVE(uint8_t, PyC_Long_AsU8); \
35 break; \
36 } \
37 case GPU_COMP_I16: { \
38 PY_AS_NATIVE(int16_t, PyC_Long_AsI16); \
39 break; \
40 } \
41 case GPU_COMP_U16: { \
42 PY_AS_NATIVE(uint16_t, PyC_Long_AsU16); \
43 break; \
44 } \
45 case GPU_COMP_I32: { \
46 PY_AS_NATIVE(int32_t, PyC_Long_AsI32); \
47 break; \
48 } \
49 case GPU_COMP_U32: { \
50 PY_AS_NATIVE(uint32_t, PyC_Long_AsU32); \
51 break; \
52 } \
53 case GPU_COMP_F32: { \
54 if (attr->python_int_to_float) { \
55 PY_AS_NATIVE(float, PyC_Long_AsI32); \
56 } \
57 else { \
58 PY_AS_NATIVE(float, PyFloat_AsDouble); \
59 } \
60 break; \
61 } \
62 default: \
63 BLI_assert_unreachable(); \
64 break; \
65 } \
66 ((void)0)
67
68/* No error checking, callers must run PyErr_Occurred */
69static void pygpu_fill_format_elem(void *data_dst_void, PyObject *py_src, const GPUVertAttr *attr)
70{
71#define PY_AS_NATIVE(ty_dst, py_as_native) \
72 { \
73 ty_dst *data_dst = static_cast<ty_dst *>(data_dst_void); \
74 *data_dst = py_as_native(py_src); \
75 } \
76 ((void)0)
77
79
80#undef PY_AS_NATIVE
81}
82
83/* No error checking, callers must run PyErr_Occurred */
84static void pygpu_fill_format_sequence(void *data_dst_void,
85 PyObject *py_seq_fast,
86 const GPUVertAttr *attr)
87{
88 const uint len = attr->comp_len;
89 PyObject **value_fast_items = PySequence_Fast_ITEMS(py_seq_fast);
90
94#define PY_AS_NATIVE(ty_dst, py_as_native) \
95 ty_dst *data_dst = static_cast<ty_dst *>(data_dst_void); \
96 for (uint i = 0; i < len; i++) { \
97 data_dst[i] = py_as_native(value_fast_items[i]); \
98 } \
99 ((void)0)
100
102
103#undef PY_AS_NATIVE
104}
105
106#undef PYGPU_AS_NATIVE_SWITCH
107#undef WARN_TYPE_LIMIT_PUSH
108#undef WARN_TYPE_LIMIT_POP
109
111 uint data_id,
112 PyObject *seq,
113 const char *error_prefix)
114{
115 const char *exc_str_size_mismatch = "Expected a %s of size %d, got %u";
116
117 bool ok = true;
118 const GPUVertAttr *attr = &GPU_vertbuf_get_format(vbo)->attrs[data_id];
119 uint vert_len = GPU_vertbuf_get_vertex_len(vbo);
120
121 if (PyObject_CheckBuffer(seq)) {
122 Py_buffer pybuffer;
123
124 if (PyObject_GetBuffer(seq, &pybuffer, PyBUF_STRIDES | PyBUF_ND) == -1) {
125 /* PyObject_GetBuffer raise a PyExc_BufferError */
126 return false;
127 }
128
129 const uint comp_len = pybuffer.ndim == 1 ? 1 : uint(pybuffer.shape[1]);
130
131 if (pybuffer.shape[0] != vert_len) {
132 PyErr_Format(
133 PyExc_ValueError, exc_str_size_mismatch, "sequence", vert_len, pybuffer.shape[0]);
134 ok = false;
135 }
136 else if (comp_len != attr->comp_len) {
137 PyErr_Format(PyExc_ValueError, exc_str_size_mismatch, "component", attr->comp_len, comp_len);
138 ok = false;
139 }
140 else {
141 GPU_vertbuf_attr_fill_stride(vbo, data_id, pybuffer.strides[0], pybuffer.buf);
142 }
143
144 PyBuffer_Release(&pybuffer);
145 }
146 else {
147 GPUVertBufRaw data_step;
148 GPU_vertbuf_attr_get_raw_data(vbo, data_id, &data_step);
149
150 PyObject *seq_fast = PySequence_Fast(seq, "Vertex buffer fill");
151 if (seq_fast == nullptr) {
152 return false;
153 }
154
155 const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast);
156
157 if (seq_len != vert_len) {
158 PyErr_Format(PyExc_ValueError, exc_str_size_mismatch, "sequence", vert_len, seq_len);
159 }
160
161 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
162
163 if (attr->comp_len == 1) {
164 for (uint i = 0; i < seq_len; i++) {
165 uchar *data = (uchar *)GPU_vertbuf_raw_step(&data_step);
166 PyObject *item = seq_items[i];
167 pygpu_fill_format_elem(data, item, attr);
168 }
169 }
170 else {
171 for (uint i = 0; i < seq_len; i++) {
172 uchar *data = (uchar *)GPU_vertbuf_raw_step(&data_step);
173 PyObject *seq_fast_item = PySequence_Fast(seq_items[i], error_prefix);
174
175 if (seq_fast_item == nullptr) {
176 ok = false;
177 goto finally;
178 }
179 if (PySequence_Fast_GET_SIZE(seq_fast_item) != attr->comp_len) {
180 PyErr_Format(PyExc_ValueError,
181 exc_str_size_mismatch,
182 "sequence",
183 attr->comp_len,
184 PySequence_Fast_GET_SIZE(seq_fast_item));
185 ok = false;
186 Py_DECREF(seq_fast_item);
187 goto finally;
188 }
189
190 /* May trigger error, check below */
191 pygpu_fill_format_sequence(data, seq_fast_item, attr);
192 Py_DECREF(seq_fast_item);
193 }
194 }
195
196 if (PyErr_Occurred()) {
197 ok = false;
198 }
199
200 finally:
201
202 Py_DECREF(seq_fast);
203 }
204 return ok;
205}
206
208 int id,
209 PyObject *py_seq_data,
210 const char *error_prefix)
211{
212 if (id < 0 || id >= GPU_vertbuf_get_format(buf)->attr_len) {
213 PyErr_Format(PyExc_ValueError, "Format id %d out of range", id);
214 return 0;
215 }
216
217 if (buf->data<char>().data() == nullptr) {
218 PyErr_SetString(PyExc_ValueError, "Can't fill, static buffer already in use");
219 return 0;
220 }
221
222 if (!pygpu_vertbuf_fill_impl(buf, uint(id), py_seq_data, error_prefix)) {
223 return 0;
224 }
225
226 return 1;
227}
228
230
231/* -------------------------------------------------------------------- */
234
235static PyObject *pygpu_vertbuf__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
236{
238
239 struct {
240 PyObject *py_fmt;
241 uint len;
242 } params;
243
244 static const char *_keywords[] = {"format", "len", nullptr};
245 static _PyArg_Parser _parser = {
247 "O!" /* `format` */
248 "I" /* `len` */
249 ":GPUVertBuf.__new__",
250 _keywords,
251 nullptr,
252 };
253 if (!_PyArg_ParseTupleAndKeywordsFast(
254 args, kwds, &_parser, &BPyGPUVertFormat_Type, &params.py_fmt, &params.len))
255 {
256 return nullptr;
257 }
258
259 const GPUVertFormat &fmt = ((BPyGPUVertFormat *)params.py_fmt)->fmt;
261
263
265}
266
268 /* Wrap. */
269 pygpu_vertbuf_attr_fill_doc,
270 ".. method:: attr_fill(id, data)\n"
271 "\n"
272 " Insert data into the buffer for a single attribute.\n"
273 "\n"
274 " :arg id: Either the name or the id of the attribute.\n"
275 " :type id: int | str\n"
276 " :arg data: Buffer or sequence of data that should be stored in the buffer\n"
277 " :type data: Buffer | "
278 "Sequence[float] | Sequence[int] | Sequence[Sequence[float]] | Sequence[Sequence[int]]\n");
279static PyObject *pygpu_vertbuf_attr_fill(BPyGPUVertBuf *self, PyObject *args, PyObject *kwds)
280{
281 PyObject *data;
282 PyObject *identifier;
283
284 static const char *_keywords[] = {"id", "data", nullptr};
285 static _PyArg_Parser _parser = {
287 "O" /* `id` */
288 "O" /* `data` */
289 ":attr_fill",
290 _keywords,
291 nullptr,
292 };
293 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &identifier, &data)) {
294 return nullptr;
295 }
296
297 int id;
298
299 if (PyLong_Check(identifier)) {
300 id = PyLong_AsLong(identifier);
301 }
302 else if (PyUnicode_Check(identifier)) {
304 const char *name = PyUnicode_AsUTF8(identifier);
306 if (id == -1) {
307 PyErr_Format(PyExc_ValueError, "Unknown attribute '%s'", name);
308 return nullptr;
309 }
310 }
311 else {
312 PyErr_SetString(PyExc_TypeError, "expected int or str type as identifier");
313 return nullptr;
314 }
315
316 if (!pygpu_vertbuf_fill(self->buf, id, data, "GPUVertBuf.attr_fill")) {
317 return nullptr;
318 }
319
320 Py_RETURN_NONE;
321}
322
323#ifdef __GNUC__
324# ifdef __clang__
325# pragma clang diagnostic push
326# pragma clang diagnostic ignored "-Wcast-function-type"
327# else
328# pragma GCC diagnostic push
329# pragma GCC diagnostic ignored "-Wcast-function-type"
330# endif
331#endif
332
333static PyMethodDef pygpu_vertbuf__tp_methods[] = {
334 {"attr_fill",
335 (PyCFunction)pygpu_vertbuf_attr_fill,
336 METH_VARARGS | METH_KEYWORDS,
337 pygpu_vertbuf_attr_fill_doc},
338 {nullptr, nullptr, 0, nullptr},
339};
340
341#ifdef __GNUC__
342# ifdef __clang__
343# pragma clang diagnostic pop
344# else
345# pragma GCC diagnostic pop
346# endif
347#endif
348
350{
352 Py_TYPE(self)->tp_free(self);
353}
354
356 /* Wrap. */
357 pygpu_vertbuf__tp_doc,
358 ".. class:: GPUVertBuf(format, len)\n"
359 "\n"
360 " Contains a VBO.\n"
361 "\n"
362 " :arg format: Vertex format.\n"
363 " :type format: :class:`gpu.types.GPUVertFormat`\n"
364 " :arg len: Amount of vertices that will fit into this buffer.\n"
365 " :type len: int\n");
366PyTypeObject BPyGPUVertBuf_Type = {
367 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
368 /*tp_name*/ "GPUVertBuf",
369 /*tp_basicsize*/ sizeof(BPyGPUVertBuf),
370 /*tp_itemsize*/ 0,
371 /*tp_dealloc*/ (destructor)pygpu_vertbuf__tp_dealloc,
372 /*tp_vectorcall_offset*/ 0,
373 /*tp_getattr*/ nullptr,
374 /*tp_setattr*/ nullptr,
375 /*tp_as_async*/ nullptr,
376 /*tp_repr*/ nullptr,
377 /*tp_as_number*/ nullptr,
378 /*tp_as_sequence*/ nullptr,
379 /*tp_as_mapping*/ nullptr,
380 /*tp_hash*/ nullptr,
381 /*tp_call*/ nullptr,
382 /*tp_str*/ nullptr,
383 /*tp_getattro*/ nullptr,
384 /*tp_setattro*/ nullptr,
385 /*tp_as_buffer*/ nullptr,
386 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
387 /*tp_doc*/ pygpu_vertbuf__tp_doc,
388 /*tp_traverse*/ nullptr,
389 /*tp_clear*/ nullptr,
390 /*tp_richcompare*/ nullptr,
391 /*tp_weaklistoffset*/ 0,
392 /*tp_iter*/ nullptr,
393 /*tp_iternext*/ nullptr,
394 /*tp_methods*/ pygpu_vertbuf__tp_methods,
395 /*tp_members*/ nullptr,
396 /*tp_getset*/ nullptr,
397 /*tp_base*/ nullptr,
398 /*tp_dict*/ nullptr,
399 /*tp_descr_get*/ nullptr,
400 /*tp_descr_set*/ nullptr,
401 /*tp_dictoffset*/ 0,
402 /*tp_init*/ nullptr,
403 /*tp_alloc*/ nullptr,
404 /*tp_new*/ pygpu_vertbuf__tp_new,
405 /*tp_free*/ nullptr,
406 /*tp_is_gc*/ nullptr,
407 /*tp_bases*/ nullptr,
408 /*tp_mro*/ nullptr,
409 /*tp_cache*/ nullptr,
410 /*tp_subclasses*/ nullptr,
411 /*tp_weaklist*/ nullptr,
412 /*tp_del*/ nullptr,
413 /*tp_version_tag*/ 0,
414 /*tp_finalize*/ nullptr,
415 /*tp_vectorcall*/ nullptr,
416};
417
419
420/* -------------------------------------------------------------------- */
423
425{
427
428 self = PyObject_New(BPyGPUVertBuf, &BPyGPUVertBuf_Type);
429 self->buf = buf;
430
431 return (PyObject *)self;
432}
433
unsigned char uchar
unsigned int uint
void GPU_vertbuf_attr_get_raw_data(blender::gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void GPU_vertbuf_attr_fill_stride(blender::gpu::VertBuf *, uint a_idx, uint stride, const void *data)
#define GPU_vertbuf_create_with_format(format)
const GPUVertFormat * GPU_vertbuf_get_format(const blender::gpu::VertBuf *verts)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
uint GPU_vertbuf_get_vertex_len(const blender::gpu::VertBuf *verts)
void GPU_vertbuf_discard(blender::gpu::VertBuf *)
int GPU_vertformat_attr_id_get(const GPUVertFormat *, blender::StringRef name)
BMesh const char void * data
PyObject * self
MutableSpan< T > data()
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:20
PyTypeObject BPyGPUVertBuf_Type
PyObject * BPyGPUVertBuf_CreatePyObject(blender::gpu::VertBuf *buf)
static bool pygpu_vertbuf_fill_impl(blender::gpu::VertBuf *vbo, uint data_id, PyObject *seq, const char *error_prefix)
static PyObject * pygpu_vertbuf__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
#define PYGPU_AS_NATIVE_SWITCH(attr)
static void pygpu_fill_format_sequence(void *data_dst_void, PyObject *py_seq_fast, const GPUVertAttr *attr)
static PyObject * pygpu_vertbuf_attr_fill(BPyGPUVertBuf *self, PyObject *args, PyObject *kwds)
static void pygpu_fill_format_elem(void *data_dst_void, PyObject *py_src, const GPUVertAttr *attr)
static int pygpu_vertbuf_fill(blender::gpu::VertBuf *buf, int id, PyObject *py_seq_data, const char *error_prefix)
static PyMethodDef pygpu_vertbuf__tp_methods[]
PyDoc_STRVAR(pygpu_vertbuf_attr_fill_doc, ".. method:: attr_fill(id, data)\n" "\n" " Insert data into the buffer for a single attribute.\n" "\n" " :arg id: Either the name or the id of the attribute.\n" " :type id: int | str\n" " :arg data: Buffer or sequence of data that should be stored in the buffer\n" " :type data: Buffer | " "Sequence[float] | Sequence[int] | Sequence[Sequence[float]] | Sequence[Sequence[int]]\n")
static void pygpu_vertbuf__tp_dealloc(BPyGPUVertBuf *self)
PyTypeObject BPyGPUVertFormat_Type
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
format
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN]
i
Definition text_draw.cc:230
uint len