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