Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / python / gpu / gpu_py_element.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file \ingroup bpygpu
18  *
19  * - Use ``bpygpu_`` for local API.
20  * - Use ``BPyGPU`` for public API.
21  */
22
23 #include <Python.h>
24
25 #include "GPU_element.h"
26
27 #include "BLI_math.h"
28
29 #include "MEM_guardedalloc.h"
30
31 #include "../generic/py_capi_utils.h"
32 #include "../generic/python_utildefines.h"
33
34 #include "gpu_py_api.h"
35 #include "gpu_py_element.h" /* own include */
36
37
38 /* -------------------------------------------------------------------- */
39 /** \name IndexBuf Type
40  * \{ */
41
42 static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
43 {
44         BPYGPU_IS_INIT_OR_ERROR_OBJ;
45
46         const char *error_prefix = "IndexBuf.__new__";
47         bool ok = true;
48
49         struct {
50                 GPUPrimType type_id;
51                 PyObject *seq;
52         } params;
53
54         uint verts_per_prim;
55         uint index_len;
56         GPUIndexBufBuilder builder;
57
58         static const char *_keywords[] = {"type", "seq", NULL};
59         static _PyArg_Parser _parser = {"$O&O:IndexBuf.__new__", _keywords, 0};
60         if (!_PyArg_ParseTupleAndKeywordsFast(
61                 args, kwds, &_parser,
62                 bpygpu_ParsePrimType, &params.type_id,
63                 &params.seq))
64         {
65                 return NULL;
66         }
67
68         verts_per_prim = GPU_indexbuf_primitive_len(params.type_id);
69         if (verts_per_prim == -1) {
70                 PyErr_Format(PyExc_ValueError,
71                              "The argument 'type' must be "
72                              "'POINTS', 'LINES', 'TRIS' or 'LINES_ADJ'");
73                 return NULL;
74         }
75
76         if (PyObject_CheckBuffer(params.seq)) {
77                 Py_buffer pybuffer;
78
79                 if (PyObject_GetBuffer(params.seq, &pybuffer, PyBUF_FORMAT | PyBUF_ND) == -1) {
80                         /* PyObject_GetBuffer already handles error messages. */
81                         return NULL;
82                 }
83
84                 if (pybuffer.ndim != 1 && pybuffer.shape[1] != verts_per_prim) {
85                         PyErr_Format(PyExc_ValueError,
86                                      "Each primitive must exactly %d indices",
87                                      verts_per_prim);
88                         return NULL;
89                 }
90
91                 if (pybuffer.itemsize != 4 ||
92                     PyC_StructFmt_type_is_float_any(PyC_StructFmt_type_from_str(pybuffer.format)))
93                 {
94                         PyErr_Format(PyExc_ValueError,
95                                      "Each index must be an 4-bytes integer value");
96                         return NULL;
97                 }
98
99                 index_len = pybuffer.shape[0];
100                 if (pybuffer.ndim != 1) {
101                         index_len *= pybuffer.shape[1];
102                 }
103
104                 /* The `vertex_len` parameter is only used for asserts in the Debug build. */
105                 /* Not very useful in python since scripts are often tested in Release build. */
106                 /* Use `INT_MAX` instead of the actual number of vertices. */
107                 GPU_indexbuf_init(
108                         &builder, params.type_id, index_len, INT_MAX);
109
110 #if 0
111                 uint *buf = pybuffer.buf;
112                 for (uint i = index_len; i--; buf++) {
113                         GPU_indexbuf_add_generic_vert(&builder, *buf);
114                 }
115 #else
116                 memcpy(builder.data, pybuffer.buf, index_len * sizeof(*builder.data));
117                 builder.index_len = index_len;
118 #endif
119                 PyBuffer_Release(&pybuffer);
120         }
121         else {
122                 PyObject *seq_fast = PySequence_Fast(params.seq, error_prefix);
123
124                 if (seq_fast == NULL) {
125                         return false;
126                 }
127
128                 const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast);
129
130                 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
131
132                 index_len = seq_len * verts_per_prim;
133
134                 /* The `vertex_len` parameter is only used for asserts in the Debug build. */
135                 /* Not very useful in python since scripts are often tested in Release build. */
136                 /* Use `INT_MAX` instead of the actual number of vertices. */
137                 GPU_indexbuf_init(
138                         &builder, params.type_id, index_len, INT_MAX);
139
140                 if (verts_per_prim == 1) {
141                         for (uint i = 0; i < seq_len; i++) {
142                                 GPU_indexbuf_add_generic_vert(
143                                         &builder, PyC_Long_AsU32(seq_items[i]));
144                         }
145                 }
146                 else {
147                         int values[4];
148                         for (uint i = 0; i < seq_len; i++) {
149                                 PyObject *seq_fast_item = PySequence_Fast(seq_items[i], error_prefix);
150                                 if (seq_fast_item == NULL) {
151                                         PyErr_Format(PyExc_TypeError,
152                                                      "%s: expected a sequence, got %s",
153                                                      error_prefix, Py_TYPE(seq_items[i])->tp_name);
154                                         ok = false;
155                                         goto finally;
156                                 }
157
158                                 ok = PyC_AsArray_FAST(
159                                         values, seq_fast_item, verts_per_prim,
160                                         &PyLong_Type, false, error_prefix) == 0;
161
162                                 if (ok) {
163                                         for (uint j = 0; j < verts_per_prim; j++) {
164                                                 GPU_indexbuf_add_generic_vert(&builder, values[j]);
165                                         }
166                                 }
167                                 Py_DECREF(seq_fast_item);
168                         }
169                 }
170
171                 if (PyErr_Occurred()) {
172                         ok = false;
173                 }
174
175 finally:
176
177                 Py_DECREF(seq_fast);
178         }
179
180         if (ok == false) {
181                 MEM_freeN(builder.data);
182                 return NULL;
183         }
184
185         return BPyGPUIndexBuf_CreatePyObject(GPU_indexbuf_build(&builder));
186 }
187
188 static void bpygpu_IndexBuf_dealloc(BPyGPUIndexBuf *self)
189 {
190         GPU_indexbuf_discard(self->elem);
191         Py_TYPE(self)->tp_free(self);
192 }
193
194 PyDoc_STRVAR(py_gpu_element_doc,
195 ".. class:: GPUIndexBuf(type, seq)\n"
196 "\n"
197 "   Contains an index buffer.\n"
198 "\n"
199 "   :param type: One of these primitive types: {\n"
200 "      `POINTS`,\n"
201 "      `LINES`,\n"
202 "      `TRIS`,\n"
203 "      `LINE_STRIP_ADJ` }\n"
204 "   :type type: `str`\n"
205 "   :param seq: Indices this index buffer will contain.\n"
206 "      Whether a 1D or 2D sequence is required depends on the type.\n"
207 "      Optionally the sequence can support the buffer protocol.\n"
208 "   :type seq: 1D or 2D sequence\n"
209 );
210 PyTypeObject BPyGPUIndexBuf_Type = {
211         PyVarObject_HEAD_INIT(NULL, 0)
212         .tp_name = "GPUIndexBuf",
213         .tp_basicsize = sizeof(BPyGPUIndexBuf),
214         .tp_dealloc = (destructor)bpygpu_IndexBuf_dealloc,
215         .tp_flags = Py_TPFLAGS_DEFAULT,
216         .tp_doc = py_gpu_element_doc,
217         .tp_new = bpygpu_IndexBuf_new,
218 };
219
220 /** \} */
221
222
223 /* -------------------------------------------------------------------- */
224 /** \name Public API
225  * \{ */
226
227 PyObject *BPyGPUIndexBuf_CreatePyObject(GPUIndexBuf *elem)
228 {
229         BPyGPUIndexBuf *self;
230
231         self = PyObject_New(BPyGPUIndexBuf, &BPyGPUIndexBuf_Type);
232         self->elem = elem;
233
234         return (PyObject *)self;
235 }
236
237 /** \} */