Merge branch 'master' into blender2.8
[blender.git] / source / blender / python / gawain / gwn_py_types.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/python/gawain/gwn_py_types.c
22  *  \ingroup pygawain
23  *
24  * - Use ``bpygwn_`` for local API.
25  * - Use ``BPyGwn_`` for public API.
26  */
27
28 #include <Python.h>
29
30 #include "gawain/gwn_batch.h"
31 #include "gawain/gwn_vertex_format.h"
32
33 #include "BLI_math.h"
34
35 #include "GPU_batch.h"
36
37 #include "MEM_guardedalloc.h"
38
39 #include "../generic/py_capi_utils.h"
40 #include "../generic/python_utildefines.h"
41
42 #include "gwn_py_types.h" /* own include */
43
44 #ifdef __BIG_ENDIAN__
45    /* big endian */
46 #  define MAKE_ID2(c, d)  ((c) << 8 | (d))
47 #  define MAKE_ID3(a, b, c) ( (int)(a) << 24 | (int)(b) << 16 | (c) << 8 )
48 #  define MAKE_ID4(a, b, c, d) ( (int)(a) << 24 | (int)(b) << 16 | (c) << 8 | (d) )
49 #else
50    /* little endian  */
51 #  define MAKE_ID2(c, d)  ((d) << 8 | (c))
52 #  define MAKE_ID3(a, b, c) ( (int)(c) << 16 | (b) << 8 | (a) )
53 #  define MAKE_ID4(a, b, c, d) ( (int)(d) << 24 | (int)(c) << 16 | (b) << 8 | (a) )
54 #endif
55
56 /* -------------------------------------------------------------------- */
57
58 /** \name Enum Conversion
59  *
60  * Use with PyArg_ParseTuple's "O&" formatting.
61  * \{ */
62
63 static int bpygwn_ParseVertCompType(PyObject *o, void *p)
64 {
65         Py_ssize_t comp_type_id_len;
66         const char *comp_type_id = _PyUnicode_AsStringAndSize(o, &comp_type_id_len);
67         if (comp_type_id == NULL) {
68                 PyErr_Format(PyExc_ValueError,
69                              "expected a string, got %s",
70                              Py_TYPE(o)->tp_name);
71                 return 0;
72         }
73
74         Gwn_VertCompType comp_type;
75         if (comp_type_id_len == 2) {
76                 switch (*((ushort *)comp_type_id)) {
77                         case MAKE_ID2('I', '8'): { comp_type = GWN_COMP_I8; goto success; }
78                         case MAKE_ID2('U', '8'): { comp_type = GWN_COMP_U8; goto success; }
79                 }
80         }
81         else if (comp_type_id_len == 3) {
82                 switch (*((uint *)comp_type_id)) {
83                         case MAKE_ID3('I', '1', '6'): { comp_type = GWN_COMP_I16; goto success; }
84                         case MAKE_ID3('U', '1', '6'): { comp_type = GWN_COMP_U16; goto success; }
85                         case MAKE_ID3('I', '3', '2'): { comp_type = GWN_COMP_I32; goto success; }
86                         case MAKE_ID3('U', '3', '2'): { comp_type = GWN_COMP_U32; goto success; }
87                         case MAKE_ID3('F', '3', '2'): { comp_type = GWN_COMP_F32; goto success; }
88                         case MAKE_ID3('I', '1', '0'): { comp_type = GWN_COMP_I10; goto success; }
89                 }
90         }
91
92         PyErr_Format(PyExc_ValueError,
93                      "unknown type literal: '%s'",
94                      comp_type_id);
95         return 0;
96
97 success:
98         *((Gwn_VertCompType *)p) = comp_type;
99         return 1;
100 }
101
102 static int bpygwn_ParseVertFetchMode(PyObject *o, void *p)
103 {
104         Py_ssize_t mode_id_len;
105         const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
106         if (mode_id == NULL) {
107                 PyErr_Format(PyExc_ValueError,
108                              "expected a string, got %s",
109                              Py_TYPE(o)->tp_name);
110                 return 0;
111         }
112 #define MATCH_ID(id) \
113         if (mode_id_len == strlen(STRINGIFY(id))) { \
114                 if (STREQ(mode_id, STRINGIFY(id))) { \
115                         mode = GWN_FETCH_##id; \
116                         goto success; \
117                 } \
118         } ((void)0)
119
120         Gwn_VertCompType mode;
121         MATCH_ID(FLOAT);
122         MATCH_ID(INT);
123         MATCH_ID(INT_TO_FLOAT_UNIT);
124         MATCH_ID(INT_TO_FLOAT);
125 #undef MATCH_ID
126         PyErr_Format(PyExc_ValueError,
127                      "unknown type literal: '%s'",
128                      mode_id);
129         return 0;
130
131 success:
132         (*(Gwn_VertFetchMode *)p) = mode;
133         return 1;
134 }
135
136 static int bpygwn_ParsePrimType(PyObject *o, void *p)
137 {
138         Py_ssize_t mode_id_len;
139         const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
140         if (mode_id == NULL) {
141                 PyErr_Format(PyExc_ValueError,
142                              "expected a string, got %s",
143                              Py_TYPE(o)->tp_name);
144                 return 0;
145         }
146 #define MATCH_ID(id) \
147         if (mode_id_len == strlen(STRINGIFY(id))) { \
148                 if (STREQ(mode_id, STRINGIFY(id))) { \
149                         mode = GWN_PRIM_##id; \
150                         goto success; \
151                 } \
152         } ((void)0)
153
154         Gwn_PrimType mode;
155         MATCH_ID(POINTS);
156         MATCH_ID(LINES);
157         MATCH_ID(TRIS);
158         MATCH_ID(LINE_STRIP);
159         MATCH_ID(LINE_LOOP);
160         MATCH_ID(TRI_STRIP);
161         MATCH_ID(TRI_FAN);
162         MATCH_ID(LINE_STRIP_ADJ);
163
164 #undef MATCH_ID
165         PyErr_Format(PyExc_ValueError,
166                      "unknown type literal: '%s'",
167                      mode_id);
168         return 0;
169
170 success:
171         (*(Gwn_VertFetchMode *)p) = mode;
172         return 1;
173 }
174
175 /** \} */
176
177
178 /* -------------------------------------------------------------------- */
179
180 /** \name Utility Functions
181  * \{ */
182
183 #define PY_AS_NATIVE_SWITCH(attr) \
184         switch (attr->comp_type) { \
185                 case GWN_COMP_I8:  { PY_AS_NATIVE(int8_t,   PyC_Long_AsI8); break; } \
186                 case GWN_COMP_U8:  { PY_AS_NATIVE(uint8_t,  PyC_Long_AsU8); break; } \
187                 case GWN_COMP_I16: { PY_AS_NATIVE(int16_t,  PyC_Long_AsI16); break; } \
188                 case GWN_COMP_U16: { PY_AS_NATIVE(uint16_t, PyC_Long_AsU16); break; } \
189                 case GWN_COMP_I32: { PY_AS_NATIVE(int32_t,  PyC_Long_AsI32); break; } \
190                 case GWN_COMP_U32: { PY_AS_NATIVE(uint32_t, PyC_Long_AsU32); break; } \
191                 case GWN_COMP_F32: { PY_AS_NATIVE(float, PyFloat_AsDouble); break; } \
192                 default: \
193                         BLI_assert(0); \
194         } ((void)0)
195
196 /* No error checking, callers must run PyErr_Occurred */
197 static void fill_format_elem(void *data_dst_void, PyObject *py_src, const Gwn_VertAttr *attr)
198 {
199 #define PY_AS_NATIVE(ty_dst, py_as_native) \
200 { \
201         ty_dst *data_dst = data_dst_void; \
202         *data_dst = py_as_native(py_src); \
203 } ((void)0)
204
205         PY_AS_NATIVE_SWITCH(attr);
206
207 #undef PY_AS_NATIVE
208 }
209
210 /* No error checking, callers must run PyErr_Occurred */
211 static void fill_format_tuple(void *data_dst_void, PyObject *py_src, const Gwn_VertAttr *attr)
212 {
213         const uint len = attr->comp_ct;
214
215 /**
216  * Args are constants, so range checks will be optimized out if they're nop's.
217  */
218 #define PY_AS_NATIVE(ty_dst, py_as_native) \
219         ty_dst *data_dst = data_dst_void; \
220         for (uint i = 0; i < len; i++) { \
221                 data_dst[i] = py_as_native(PyTuple_GET_ITEM(py_src, i)); \
222         } ((void)0)
223
224         PY_AS_NATIVE_SWITCH(attr);
225
226 #undef PY_AS_NATIVE
227 }
228
229 #undef PY_AS_NATIVE_SWITCH
230 #undef WARN_TYPE_LIMIT_PUSH
231 #undef WARN_TYPE_LIMIT_POP
232
233 static bool bpygwn_vertbuf_fill_impl(
234         Gwn_VertBuf *vbo,
235         uint data_id, PyObject *seq)
236 {
237         bool ok = true;
238         const Gwn_VertAttr *attr = &vbo->format.attribs[data_id];
239
240         Gwn_VertBufRaw data_step;
241         GWN_vertbuf_attr_get_raw_data(vbo, data_id, &data_step);
242
243         PyObject *seq_fast = PySequence_Fast(seq, "Vertex buffer fill");
244         if (seq_fast == NULL) {
245                 goto finally;
246         }
247
248         const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast);
249
250         if (seq_len != vbo->vertex_ct) {
251                 PyErr_Format(PyExc_ValueError,
252                              "Expected a sequence of size %d, got %d",
253                              vbo->vertex_ct, seq_len);
254         }
255
256         PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
257
258         if (attr->comp_ct == 1) {
259                 for (uint i = 0; i < seq_len; i++) {
260                         uchar *data = (uchar *)GWN_vertbuf_raw_step(&data_step);
261                         PyObject *item = seq_items[i];
262                         fill_format_elem(data, item, attr);
263                 }
264         }
265         else {
266                 for (uint i = 0; i < seq_len; i++) {
267                         uchar *data = (uchar *)GWN_vertbuf_raw_step(&data_step);
268                         PyObject *item = seq_items[i];
269                         if (!PyTuple_CheckExact(item)) {
270                                 PyErr_Format(PyExc_ValueError,
271                                              "expected a tuple, got %s",
272                                              Py_TYPE(item)->tp_name);
273                                 ok = false;
274                                 goto finally;
275                         }
276                         if (PyTuple_GET_SIZE(item) != attr->comp_ct) {
277                                 PyErr_Format(PyExc_ValueError,
278                                              "expected a tuple of size %d, got %d",
279                                              attr->comp_ct, PyTuple_GET_SIZE(item));
280                                 ok = false;
281                                 goto finally;
282                         }
283
284                         /* May trigger error, check below */
285                         fill_format_tuple(data, item, attr);
286                 }
287         }
288
289         if (PyErr_Occurred()) {
290                 ok = false;
291         }
292
293 finally:
294
295         Py_DECREF(seq_fast);
296         return ok;
297 }
298
299 /* handy, but not used just now */
300 #if 0
301 static int bpygwn_find_id(const Gwn_VertFormat *fmt, const char *id)
302 {
303         for (int i = 0; i < fmt->attrib_ct; i++) {
304                 for (uint j = 0; j < fmt->name_ct; j++) {
305                         if (STREQ(fmt->attribs[i].name[j], id)) {
306                                 return i;
307                         }
308                 }
309         }
310         return -1;
311 }
312 #endif
313
314 /** \} */
315
316
317 /* -------------------------------------------------------------------- */
318
319 /** \name VertFormat Type
320  * \{ */
321
322 static PyObject *bpygwn_VertFormat_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
323 {
324         if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) {
325                 PyErr_SetString(PyExc_TypeError,
326                                 "VertFormat(): takes no arguments");
327                 return NULL;
328         }
329
330         BPyGwn_VertFormat *ret = (BPyGwn_VertFormat *)BPyGwn_VertFormat_CreatePyObject(NULL);
331
332         return (PyObject *)ret;
333 }
334
335 PyDoc_STRVAR(bpygwn_VertFormat_attr_add_doc,
336 "TODO"
337 );
338 static PyObject *bpygwn_VertFormat_attr_add(BPyGwn_VertFormat *self, PyObject *args, PyObject *kwds)
339 {
340         static const char *kwlist[] = {"id", "comp_type", "len", "fetch_mode", NULL};
341
342         struct {
343                 const char *id;
344                 Gwn_VertCompType comp_type;
345                 uint len;
346                 Gwn_VertFetchMode fetch_mode;
347         } params;
348
349         if (self->fmt.attrib_ct == GWN_VERT_ATTR_MAX_LEN) {
350                 PyErr_SetString(PyExc_ValueError, "Maxumum attr reached " STRINGIFY(GWN_VERT_ATTR_MAX_LEN));
351                 return NULL;
352         }
353
354         if (!PyArg_ParseTupleAndKeywords(
355                 args, kwds, "$sO&IO&:attr_add", (char **)kwlist,
356                 &params.id,
357                 bpygwn_ParseVertCompType, &params.comp_type,
358                 &params.len,
359                 bpygwn_ParseVertFetchMode, &params.fetch_mode))
360         {
361                 return NULL;
362         }
363
364         uint attr_id = GWN_vertformat_attr_add(&self->fmt, params.id, params.comp_type, params.len, params.fetch_mode);
365         return PyLong_FromLong(attr_id);
366 }
367
368 static struct PyMethodDef bpygwn_VertFormat_methods[] = {
369         {"attr_add", (PyCFunction)bpygwn_VertFormat_attr_add,
370          METH_VARARGS | METH_KEYWORDS, bpygwn_VertFormat_attr_add_doc},
371         {NULL, NULL, 0, NULL}
372 };
373
374
375 static void bpygwn_VertFormat_dealloc(BPyGwn_VertFormat *self)
376 {
377         Py_TYPE(self)->tp_free(self);
378 }
379
380 PyTypeObject BPyGwn_VertFormat_Type = {
381         PyVarObject_HEAD_INIT(NULL, 0)
382         .tp_name = "Gwn_VertFormat",
383         .tp_basicsize = sizeof(BPyGwn_VertFormat),
384         .tp_dealloc = (destructor)bpygwn_VertFormat_dealloc,
385         .tp_flags = Py_TPFLAGS_DEFAULT,
386         .tp_methods = bpygwn_VertFormat_methods,
387         .tp_new = bpygwn_VertFormat_new,
388 };
389
390 /** \} */
391
392
393 /* -------------------------------------------------------------------- */
394
395 /** \name VertBuf Type
396  * \{ */
397
398 static PyObject *bpygwn_VertBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
399 {
400         const char * const keywords[] = {"len", "format", NULL};
401
402         struct {
403                 BPyGwn_VertFormat *py_fmt;
404                 uint len;
405         } params;
406
407         if (!PyArg_ParseTupleAndKeywords(
408                 args, kwds,
409                 "$IO!:Gwn_VertBuf.__new__", (char **)keywords,
410                 &params.len,
411                 &BPyGwn_VertFormat_Type, &params.py_fmt))
412         {
413                 return NULL;
414         }
415
416         struct Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&params.py_fmt->fmt);
417
418         GWN_vertbuf_data_alloc(vbo, params.len);
419
420         return BPyGwn_VertBuf_CreatePyObject(vbo);
421 }
422
423 PyDoc_STRVAR(bpygwn_VertBuf_fill_doc,
424 "TODO"
425 );
426 static PyObject *bpygwn_VertBuf_fill(BPyGwn_VertBuf *self, PyObject *args, PyObject *kwds)
427 {
428         static const char *kwlist[] = {"id", "data", NULL};
429
430         struct {
431                 uint id;
432                 PyObject *py_seq_data;
433         } params;
434
435         if (!PyArg_ParseTupleAndKeywords(
436                 args, kwds, "$IO:fill", (char **)kwlist,
437                 &params.id,
438                 &params.py_seq_data))
439         {
440                 return NULL;
441         }
442
443         if (params.id >= self->buf->format.attrib_ct) {
444                 PyErr_Format(PyExc_ValueError,
445                              "Format id %d out of range",
446                              params.id);
447                 return NULL;
448         }
449
450         if (self->buf->vbo_id != 0) {
451                 PyErr_SetString(PyExc_ValueError,
452                                 "Can't fill, buffer already in use");
453                 return NULL;
454         }
455
456         if (!bpygwn_vertbuf_fill_impl(self->buf, params.id, params.py_seq_data)) {
457                 return NULL;
458         }
459         Py_RETURN_NONE;
460 }
461
462 static struct PyMethodDef bpygwn_VertBuf_methods[] = {
463         {"fill", (PyCFunction) bpygwn_VertBuf_fill,
464          METH_VARARGS | METH_KEYWORDS, bpygwn_VertBuf_fill_doc},
465         {NULL, NULL, 0, NULL}
466 };
467
468 static void bpygwn_VertBuf_dealloc(BPyGwn_VertBuf *self)
469 {
470         GWN_vertbuf_discard(self->buf);
471         Py_TYPE(self)->tp_free(self);
472 }
473
474 PyTypeObject BPyGwn_VertBuf_Type = {
475         PyVarObject_HEAD_INIT(NULL, 0)
476         .tp_name = "Gwn_VertBuf",
477         .tp_basicsize = sizeof(BPyGwn_VertBuf),
478         .tp_dealloc = (destructor)bpygwn_VertBuf_dealloc,
479         .tp_flags = Py_TPFLAGS_DEFAULT,
480         .tp_methods = bpygwn_VertBuf_methods,
481         .tp_new = bpygwn_VertBuf_new,
482 };
483
484 /** \} */
485
486
487 /* -------------------------------------------------------------------- */
488
489 /** \name VertBatch Type
490  * \{ */
491
492 static PyObject *bpygwn_Batch_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
493 {
494         const char * const keywords[] = {"type", "buf", NULL};
495
496         struct {
497                 Gwn_PrimType type_id;
498                 BPyGwn_VertBuf *py_buf;
499         } params;
500
501         if (!PyArg_ParseTupleAndKeywords(
502                 args, kwds,
503                 "$O&O!:Gwn_Batch.__new__", (char **)keywords,
504                 bpygwn_ParsePrimType, &params.type_id,
505                 &BPyGwn_VertBuf_Type, &params.py_buf))
506         {
507                 return NULL;
508         }
509
510         Gwn_Batch *batch = GWN_batch_create(params.type_id, params.py_buf->buf, NULL);
511         BPyGwn_Batch *ret = (BPyGwn_Batch *)BPyGwn_Batch_CreatePyObject(batch);
512
513 #ifdef USE_GWN_PY_REFERENCES
514         ret->references = PyList_New(1);
515         PyList_SET_ITEM(ret->references, 0, (PyObject *)params.py_buf);
516         Py_INCREF(params.py_buf);
517         PyObject_GC_Track(ret);
518 #endif
519
520         return (PyObject *)ret;
521 }
522
523 PyDoc_STRVAR(bpygwn_VertBatch_vertbuf_add_doc,
524 "TODO"
525 );
526 static PyObject *bpygwn_VertBatch_vertbuf_add(BPyGwn_Batch *self, BPyGwn_VertBuf *py_buf)
527 {
528         if (!BPyGwn_VertBuf_Check(py_buf)) {
529                 PyErr_Format(PyExc_TypeError,
530                              "Expected a Gwn_VertBuf, got %s",
531                              Py_TYPE(py_buf)->tp_name);
532                 return NULL;
533         }
534
535         if (self->batch->verts[0]->vertex_ct != py_buf->buf->vertex_ct) {
536                 PyErr_Format(PyExc_TypeError,
537                              "Expected %d length, got %d",
538                              self->batch->verts[0]->vertex_ct, py_buf->buf->vertex_ct);
539                 return NULL;
540         }
541
542 #ifdef USE_GWN_PY_REFERENCES
543         /* Hold user */
544         PyList_Append(self->references, (PyObject *)py_buf);
545 #endif
546
547         GWN_batch_vertbuf_add(self->batch, py_buf->buf);
548         Py_RETURN_NONE;
549 }
550
551 /* Currently magic number from Py perspective. */
552 PyDoc_STRVAR(bpygwn_VertBatch_program_set_builtin_doc,
553 "TODO"
554 );
555 static PyObject *bpygwn_VertBatch_program_set_builtin(BPyGwn_Batch *self, PyObject *args, PyObject *kwds)
556 {
557         static const char *kwlist[] = {"id", NULL};
558
559         struct {
560                 const char *shader;
561         } params;
562
563         if (!PyArg_ParseTupleAndKeywords(
564                 args, kwds, "s:program_set_builtin", (char **)kwlist,
565                 &params.shader))
566         {
567                 return NULL;
568         }
569
570         GPUBuiltinShader shader;
571
572 #define MATCH_ID(id) \
573         if (STREQ(params.shader, STRINGIFY(id))) { \
574                 shader = GPU_SHADER_##id; \
575                 goto success; \
576         } ((void)0)
577
578         MATCH_ID(2D_FLAT_COLOR);
579         MATCH_ID(2D_SMOOTH_COLOR);
580         MATCH_ID(2D_UNIFORM_COLOR);
581
582         MATCH_ID(3D_FLAT_COLOR);
583         MATCH_ID(3D_SMOOTH_COLOR);
584         MATCH_ID(3D_UNIFORM_COLOR);
585
586 #undef MATCH_ID
587
588         PyErr_SetString(PyExc_ValueError,
589                         "shader name not known");
590         return NULL;
591
592 success:
593         GWN_batch_program_set_builtin(self->batch, shader);
594         Py_RETURN_NONE;
595 }
596
597 static PyObject *bpygwn_VertBatch_uniform_bool(BPyGwn_Batch *self, PyObject *args)
598 {
599         struct {
600                 const char *id;
601                 bool values[1];
602         } params;
603
604         if (!PyArg_ParseTuple(
605                 args, "sO&:uniform_bool",
606                 &params.id,
607                 PyC_ParseBool, &params.values[0]))
608         {
609                 return NULL;
610         }
611
612         GWN_batch_uniform_1b(self->batch, params.id, params.values[0]);
613         Py_RETURN_NONE;
614 }
615
616 static PyObject *bpygwn_VertBatch_uniform_i32(BPyGwn_Batch *self, PyObject *args)
617 {
618         struct {
619                 const char *id;
620                 int values[1];
621         } params;
622
623         if (!PyArg_ParseTuple(
624                 args, "si:uniform_i32",
625                 &params.id,
626                 &params.values[0]))
627         {
628                 return NULL;
629         }
630
631         GWN_batch_uniform_1i(self->batch, params.id, params.values[0]);
632         Py_RETURN_NONE;
633 }
634
635 static PyObject *bpygwn_VertBatch_uniform_f32(BPyGwn_Batch *self, PyObject *args)
636 {
637         struct {
638                 const char *id;
639                 float values[4];
640         } params;
641
642         if (!PyArg_ParseTuple(
643                 args, "sf|fff:uniform_f32",
644                 &params.id,
645                 &params.values[0], &params.values[1], &params.values[2], &params.values[3]))
646         {
647                 return NULL;
648         }
649
650         switch (PyTuple_GET_SIZE(args)) {
651                 case 2: GWN_batch_uniform_1f(self->batch, params.id, params.values[0]); break;
652                 case 3: GWN_batch_uniform_2f(self->batch, params.id, UNPACK2(params.values)); break;
653                 case 4: GWN_batch_uniform_3f(self->batch, params.id, UNPACK3(params.values)); break;
654                 case 5: GWN_batch_uniform_4f(self->batch, params.id, UNPACK4(params.values)); break;
655                 default:
656                         BLI_assert(0);
657         }
658         Py_RETURN_NONE;
659 }
660
661 PyDoc_STRVAR(bpygwn_VertBatch_draw_doc,
662 "TODO"
663 );
664 static PyObject *bpygwn_VertBatch_draw(BPyGwn_Batch *self)
665 {
666         if (!glIsProgram(self->batch->program)) {
667                 PyErr_SetString(PyExc_ValueError,
668                                 "batch program has not not set");
669         }
670         GWN_batch_draw(self->batch);
671         Py_RETURN_NONE;
672 }
673
674 static PyObject *bpygwn_VertBatch_program_use_begin(BPyGwn_Batch *self)
675 {
676         if (!glIsProgram(self->batch->program)) {
677                 PyErr_SetString(PyExc_ValueError,
678                                 "batch program has not not set");
679         }
680         GWN_batch_program_use_begin(self->batch);
681         Py_RETURN_NONE;
682 }
683
684 static PyObject *bpygwn_VertBatch_program_use_end(BPyGwn_Batch *self)
685 {
686         if (!glIsProgram(self->batch->program)) {
687                 PyErr_SetString(PyExc_ValueError,
688                                 "batch program has not not set");
689         }
690         GWN_batch_program_use_end(self->batch);
691         Py_RETURN_NONE;
692 }
693
694 static struct PyMethodDef bpygwn_VertBatch_methods[] = {
695         {"vertbuf_add", (PyCFunction)bpygwn_VertBatch_vertbuf_add,
696          METH_O, bpygwn_VertBatch_vertbuf_add_doc},
697         {"program_set_builtin", (PyCFunction)bpygwn_VertBatch_program_set_builtin,
698          METH_VARARGS | METH_KEYWORDS, bpygwn_VertBatch_program_set_builtin_doc},
699         {"uniform_bool", (PyCFunction)bpygwn_VertBatch_uniform_bool,
700          METH_VARARGS, NULL},
701         {"uniform_i32", (PyCFunction)bpygwn_VertBatch_uniform_i32,
702          METH_VARARGS, NULL},
703         {"uniform_f32", (PyCFunction)bpygwn_VertBatch_uniform_f32,
704           METH_VARARGS, NULL},
705         {"draw", (PyCFunction) bpygwn_VertBatch_draw,
706          METH_NOARGS, bpygwn_VertBatch_draw_doc},
707         {"program_use_begin", (PyCFunction)bpygwn_VertBatch_program_use_begin,
708          METH_NOARGS, ""},
709         {"program_use_end", (PyCFunction)bpygwn_VertBatch_program_use_end,
710          METH_NOARGS, ""},
711         {NULL, NULL, 0, NULL}
712 };
713
714 #ifdef USE_GWN_PY_REFERENCES
715
716 static int bpygwn_Batch_traverse(BPyGwn_Batch *self, visitproc visit, void *arg)
717 {
718         Py_VISIT(self->references);
719         return 0;
720 }
721
722 static int bpygwn_Batch_clear(BPyGwn_Batch *self)
723 {
724         Py_CLEAR(self->references);
725         return 0;
726 }
727
728 #endif
729
730 static void bpygwn_Batch_dealloc(BPyGwn_Batch *self)
731 {
732         GWN_batch_discard(self->batch);
733
734 #ifdef USE_GWN_PY_REFERENCES
735         if (self->references) {
736                 PyObject_GC_UnTrack(self);
737                 bpygwn_Batch_clear(self);
738                 Py_XDECREF(self->references);
739         }
740 #endif
741
742         Py_TYPE(self)->tp_free(self);
743 }
744
745 PyTypeObject BPyGwn_Batch_Type = {
746         PyVarObject_HEAD_INIT(NULL, 0)
747         .tp_name = "Gwn_Batch",
748         .tp_basicsize = sizeof(BPyGwn_Batch),
749         .tp_dealloc = (destructor)bpygwn_Batch_dealloc,
750 #ifdef USE_GWN_PY_REFERENCES
751         .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
752         .tp_traverse = (traverseproc)bpygwn_Batch_traverse,
753         .tp_clear = (inquiry)bpygwn_Batch_clear,
754 #else
755         .tp_flags = Py_TPFLAGS_DEFAULT,
756 #endif
757         .tp_methods = bpygwn_VertBatch_methods,
758         .tp_new = bpygwn_Batch_new,
759 };
760
761 /* -------------------------------------------------------------------- */
762
763
764 /** \name Gawain Types Module
765  * \{ */
766
767 static struct PyModuleDef BPy_BM_types_module_def = {
768         PyModuleDef_HEAD_INIT,
769         .m_name = "_gawain.types",
770 };
771
772 PyObject *BPyInit_gawain_types(void)
773 {
774         PyObject *submodule;
775
776         submodule = PyModule_Create(&BPy_BM_types_module_def);
777
778         if (PyType_Ready(&BPyGwn_VertFormat_Type) < 0)
779                 return NULL;
780         if (PyType_Ready(&BPyGwn_VertBuf_Type) < 0)
781                 return NULL;
782         if (PyType_Ready(&BPyGwn_Batch_Type) < 0)
783                 return NULL;
784
785 #define MODULE_TYPE_ADD(s, t) \
786         PyModule_AddObject(s, t.tp_name, (PyObject *)&t); Py_INCREF((PyObject *)&t)
787
788         MODULE_TYPE_ADD(submodule, BPyGwn_VertFormat_Type);
789         MODULE_TYPE_ADD(submodule, BPyGwn_VertBuf_Type);
790         MODULE_TYPE_ADD(submodule, BPyGwn_Batch_Type);
791
792 #undef MODULE_TYPE_ADD
793
794         return submodule;
795 }
796
797 /** \} */
798
799
800 /* -------------------------------------------------------------------- */
801
802 /** \name Public API
803  * \{ */
804
805 PyObject *BPyGwn_VertFormat_CreatePyObject(Gwn_VertFormat *fmt)
806 {
807         BPyGwn_VertFormat *self;
808
809         self = PyObject_New(BPyGwn_VertFormat, &BPyGwn_VertFormat_Type);
810         if (fmt) {
811                 self->fmt = *fmt;
812         }
813         else {
814                 memset(&self->fmt, 0, sizeof(self->fmt));
815         }
816
817         return (PyObject *)self;
818 }
819
820 PyObject *BPyGwn_VertBuf_CreatePyObject(Gwn_VertBuf *buf)
821 {
822         BPyGwn_VertBuf *self;
823
824         self = PyObject_New(BPyGwn_VertBuf, &BPyGwn_VertBuf_Type);
825         self->buf = buf;
826
827         return (PyObject *)self;
828 }
829
830
831 PyObject *BPyGwn_Batch_CreatePyObject(Gwn_Batch *batch)
832 {
833         BPyGwn_Batch *self;
834
835 #ifdef USE_GWN_PY_REFERENCES
836         self = (BPyGwn_Batch *)_PyObject_GC_New(&BPyGwn_Batch_Type);
837         self->references = NULL;
838 #else
839         self = PyObject_New(BPyGwn_Batch, &BPyGwn_Batch_Type);
840 #endif
841
842         self->batch = batch;
843
844         return (PyObject *)self;
845 }
846
847 /** \} */