Blender V5.3
gpu_py_element.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_index_buffer.hh"
15
16#include "MEM_guardedalloc.h"
17
19#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
20
21#include "gpu_py.hh"
22#include "gpu_py_element.hh" /* own include */
23
24namespace blender {
25
26/* -------------------------------------------------------------------- */
29
30static PyObject *pygpu_IndexBuf__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
31{
33
34 const char *error_prefix = "IndexBuf.__new__";
35 bool ok = true;
36
38 PyObject *seq;
39
40 uint verts_per_prim;
41 uint index_len;
42 GPUIndexBufBuilder builder;
43
44 static const char *_keywords[] = {"type", "seq", nullptr};
45 static _PyArg_Parser _parser = {
46 "$" /* Keyword only arguments. */
47 "O&" /* `type` */
48 "O" /* `seq` */
49 ":IndexBuf.__new__",
50 _keywords,
51 nullptr,
52 };
53 if (!_PyArg_ParseTupleAndKeywordsFast(
54 args, kwds, &_parser, PyC_ParseStringEnum, &prim_type, &seq))
55 {
56 return nullptr;
57 }
58
59 verts_per_prim = GPU_indexbuf_primitive_len(GPUPrimType(prim_type.value_found));
60 if (verts_per_prim == -1) {
61 PyErr_SetString(PyExc_ValueError,
62 "The argument 'type' must be "
63 "'POINTS', 'LINES', 'TRIS', 'LINES_ADJ' or 'TRIS_ADJ'");
64 return nullptr;
65 }
66
67 if (PyObject_CheckBuffer(seq)) {
68 Py_buffer pybuffer;
69
70 if (PyObject_GetBuffer(seq, &pybuffer, PyBUF_FORMAT | PyBUF_ND) == -1) {
71 /* PyObject_GetBuffer already handles error messages. */
72 return nullptr;
73 }
74
75 if (pybuffer.ndim != 1 && pybuffer.shape[1] != verts_per_prim) {
76 PyErr_Format(PyExc_ValueError, "Each primitive must exactly %d indices", verts_per_prim);
77 PyBuffer_Release(&pybuffer);
78 return nullptr;
79 }
80
81 if (pybuffer.itemsize != 4 ||
83 {
84 PyErr_SetString(PyExc_ValueError, "Each index must be an 4-bytes integer value");
85 PyBuffer_Release(&pybuffer);
86 return nullptr;
87 }
88
89 index_len = pybuffer.shape[0];
90 if (pybuffer.ndim != 1) {
91 index_len *= pybuffer.shape[1];
92 }
93
94 /* The `vertex_len` parameter is only used for asserts in the Debug build. */
95 /* Not very useful in python since scripts are often tested in Release build. */
96 /* Use `INT_MAX` instead of the actual number of vertices. */
97 GPU_indexbuf_init(&builder, GPUPrimType(prim_type.value_found), index_len, INT_MAX);
98
99 uint *buf = static_cast<uint *>(pybuffer.buf);
100 for (uint i = index_len; i--; buf++) {
101 GPU_indexbuf_add_generic_vert(&builder, *buf);
102 }
103
104 PyBuffer_Release(&pybuffer);
105 }
106 else {
107 PyObject *seq_fast = PySequence_Fast(seq, error_prefix);
108
109 if (seq_fast == nullptr) {
110 return nullptr;
111 }
112
113 const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast);
114
115 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
116
117 index_len = seq_len * verts_per_prim;
118
119 /* The `vertex_len` parameter is only used for asserts in the Debug build. */
120 /* Not very useful in python since scripts are often tested in Release build. */
121 /* Use `INT_MAX` instead of the actual number of vertices. */
122 GPU_indexbuf_init(&builder, GPUPrimType(prim_type.value_found), index_len, INT_MAX);
123
124 if (verts_per_prim == 1) {
125 for (uint i = 0; i < seq_len; i++) {
126 GPU_indexbuf_add_generic_vert(&builder, PyC_Long_AsU32(seq_items[i]));
127 }
128 }
129 else {
130 int values[4];
131 for (uint i = 0; i < seq_len; i++) {
132 PyObject *seq_fast_item = PySequence_Fast(seq_items[i], error_prefix);
133 if (seq_fast_item == nullptr) {
134 PyErr_Format(PyExc_TypeError,
135 "%s: expected a sequence, got %s",
136 error_prefix,
137 Py_TYPE(seq_items[i])->tp_name);
138 ok = false;
139 goto finally;
140 }
141
142 ok = PyC_AsArray_FAST(values,
143 sizeof(*values),
144 seq_fast_item,
145 verts_per_prim,
146 &PyLong_Type,
147 error_prefix) == 0;
148
149 if (ok) {
150 for (uint j = 0; j < verts_per_prim; j++) {
151 GPU_indexbuf_add_generic_vert(&builder, values[j]);
152 }
153 }
154 Py_DECREF(seq_fast_item);
155 }
156 }
157
158 if (PyErr_Occurred()) {
159 ok = false;
160 }
161
162 finally:
163
164 Py_DECREF(seq_fast);
165 }
166
167 if (ok == false) {
168 MEM_delete(builder.data);
169 return nullptr;
170 }
171
173}
174
176{
178 Py_TYPE(self)->tp_free(self);
179}
180
182 /* Wrap. */
183 pygpu_IndexBuf__tp_doc,
184 ".. class:: GPUIndexBuf\n"
185 "\n"
186 " Contains an index buffer.\n"
187 "\n"
188 " .. method:: __init__(type, seq)\n"
189 "\n"
190 " :param type: The primitive type this index buffer is composed of.\n"
191 " :type type: Literal['POINTS', 'LINES', 'TRIS', 'LINES_ADJ', 'TRIS_ADJ']\n"
192 " :param seq: Indices this index buffer will contain.\n"
193 " Whether a 1D or 2D sequence is required depends on the type.\n"
194 " Optionally the sequence can support the buffer protocol.\n"
195 " :type seq: Buffer | Sequence[int] | Sequence[Sequence[int]]\n");
196PyTypeObject BPyGPUIndexBuf_Type = {
197 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
198 /*tp_name*/ "GPUIndexBuf",
199 /*tp_basicsize*/ sizeof(BPyGPUIndexBuf),
200 /*tp_itemsize*/ 0,
201 /*tp_dealloc*/ reinterpret_cast<destructor>(pygpu_IndexBuf__tp_dealloc),
202 /*tp_vectorcall_offset*/ 0,
203 /*tp_getattr*/ nullptr,
204 /*tp_setattr*/ nullptr,
205 /*tp_as_async*/ nullptr,
206 /*tp_repr*/ nullptr,
207 /*tp_as_number*/ nullptr,
208 /*tp_as_sequence*/ nullptr,
209 /*tp_as_mapping*/ nullptr,
210 /*tp_hash*/ nullptr,
211 /*tp_call*/ nullptr,
212 /*tp_str*/ nullptr,
213 /*tp_getattro*/ nullptr,
214 /*tp_setattro*/ nullptr,
215 /*tp_as_buffer*/ nullptr,
216 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
217 /*tp_doc*/ pygpu_IndexBuf__tp_doc,
218 /*tp_traverse*/ nullptr,
219 /*tp_clear*/ nullptr,
220 /*tp_richcompare*/ nullptr,
221 /*tp_weaklistoffset*/ 0,
222 /*tp_iter*/ nullptr,
223 /*tp_iternext*/ nullptr,
224 /*tp_methods*/ nullptr,
225 /*tp_members*/ nullptr,
226 /*tp_getset*/ nullptr,
227 /*tp_base*/ nullptr,
228 /*tp_dict*/ nullptr,
229 /*tp_descr_get*/ nullptr,
230 /*tp_descr_set*/ nullptr,
231 /*tp_dictoffset*/ 0,
232 /*tp_init*/ nullptr,
233 /*tp_alloc*/ nullptr,
234 /*tp_new*/ pygpu_IndexBuf__tp_new,
235 /*tp_free*/ nullptr,
236 /*tp_is_gc*/ nullptr,
237 /*tp_bases*/ nullptr,
238 /*tp_mro*/ nullptr,
239 /*tp_cache*/ nullptr,
240 /*tp_subclasses*/ nullptr,
241 /*tp_weaklist*/ nullptr,
242 /*tp_del*/ nullptr,
243 /*tp_version_tag*/ 0,
244 /*tp_finalize*/ nullptr,
245 /*tp_vectorcall*/ nullptr,
246};
247
249
250/* -------------------------------------------------------------------- */
253
255{
256 BPyGPUIndexBuf *self;
257
258 self = PyObject_New(BPyGPUIndexBuf, &BPyGPUIndexBuf_Type);
259 self->elem = elem;
260
261 return reinterpret_cast<PyObject *>(self);
262}
263
265
266} // namespace blender
Read Blender memory allocation and freeing.
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
#define INT_MAX
BMesh Iterators.
int PyC_AsArray_FAST(void *array, const size_t array_item_size, PyObject *value_fast, const Py_ssize_t length, const PyTypeObject *type, const char *error_prefix)
static PyObject * pygpu_IndexBuf__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
unsigned int uint
Definition BKE_text.h:55
char PyC_StructFmt_type_from_str(const char *typestr)
PyObject * BPyGPUIndexBuf_CreatePyObject(gpu::IndexBuf *elem)
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")
PyTypeObject BPyGPUIndexBuf_Type
gpu::IndexBuf * GPU_indexbuf_build(GPUIndexBufBuilder *)
static void pygpu_IndexBuf__tp_dealloc(BPyGPUIndexBuf *self)
uint32_t PyC_Long_AsU32(PyObject *value)
PyC_StringEnumItems bpygpu_primtype_items[]
Definition gpu_py.cc:28
bool PyC_StructFmt_type_is_float_any(char format)
void GPU_indexbuf_discard(gpu::IndexBuf *elem)
void GPU_indexbuf_add_generic_vert(GPUIndexBufBuilder *, uint v)
int PyC_ParseStringEnum(PyObject *o, void *p)
int GPU_indexbuf_primitive_len(GPUPrimType prim_type)
header-only compatibility defines.
PyObject_HEAD gpu::IndexBuf * elem