| 
									
										
										
										
											2022-02-11 09:07:11 +11:00
										 |  |  | /* SPDX-License-Identifier: GPL-2.0-or-later */ | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup bpygpu | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-07-20 22:52:31 +10:00
										 |  |  |  * - Use `bpygpu_` for local API. | 
					
						
							|  |  |  |  * - Use `BPyGPU` for public API. | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							|  |  |  | #include "../generic/python_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_py_vertex_format.h" /* own include */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Enum Conversion
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use with PyArg_ParseTuple's "O&" formatting. | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  | static struct PyC_StringEnumItems pygpu_vertcomptype_items[] = { | 
					
						
							|  |  |  |     {GPU_COMP_I8, "I8"}, | 
					
						
							|  |  |  |     {GPU_COMP_U8, "U8"}, | 
					
						
							|  |  |  |     {GPU_COMP_I16, "I16"}, | 
					
						
							|  |  |  |     {GPU_COMP_U16, "U16"}, | 
					
						
							|  |  |  |     {GPU_COMP_I32, "I32"}, | 
					
						
							|  |  |  |     {GPU_COMP_U32, "U32"}, | 
					
						
							|  |  |  |     {GPU_COMP_F32, "F32"}, | 
					
						
							|  |  |  |     {GPU_COMP_I10, "I10"}, | 
					
						
							|  |  |  |     {0, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  | static struct PyC_StringEnumItems pygpu_vertfetchmode_items[] = { | 
					
						
							|  |  |  |     {GPU_FETCH_FLOAT, "FLOAT"}, | 
					
						
							|  |  |  |     {GPU_FETCH_INT, "INT"}, | 
					
						
							|  |  |  |     {GPU_FETCH_INT_TO_FLOAT_UNIT, "INT_TO_FLOAT_UNIT"}, | 
					
						
							|  |  |  |     {GPU_FETCH_INT_TO_FLOAT, "INT_TO_FLOAT"}, | 
					
						
							|  |  |  |     {0, NULL}, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name VertFormat Type
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_vertformat__tp_new(PyTypeObject *UNUSED(type), | 
					
						
							|  |  |  |                                           PyObject *args, | 
					
						
							|  |  |  |                                           PyObject *kwds) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-29 11:08:55 +11:00
										 |  |  |   if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) { | 
					
						
							|  |  |  |     PyErr_SetString(PyExc_ValueError, "This function takes no arguments"); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-10-29 11:08:55 +11:00
										 |  |  |   return BPyGPUVertFormat_CreatePyObject(NULL); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR( | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     pygpu_vertformat_attr_add_doc, | 
					
						
							| 
									
										
										
										
											2018-11-13 15:25:22 +01:00
										 |  |  |     ".. method:: attr_add(id, comp_type, len, fetch_mode)\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							|  |  |  |     "   Add a new attribute to the format.\n" | 
					
						
							|  |  |  |     "\n" | 
					
						
							| 
									
										
										
										
											2022-09-19 14:22:31 +10:00
										 |  |  |     "   :arg id: Name the attribute. Often `position`, `normal`, ...\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 15:25:22 +01:00
										 |  |  |     "   :type id: str\n" | 
					
						
							| 
									
										
										
										
											2022-09-19 14:22:31 +10:00
										 |  |  |     "   :arg comp_type: The data type that will be used store the value in memory.\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 15:25:22 +01:00
										 |  |  |     "      Possible values are `I8`, `U8`, `I16`, `U16`, `I32`, `U32`, `F32` and `I10`.\n" | 
					
						
							| 
									
										
										
										
											2021-03-16 12:48:00 -03:00
										 |  |  |     "   :type comp_type: str\n" | 
					
						
							| 
									
										
										
										
											2022-09-19 14:22:31 +10:00
										 |  |  |     "   :arg len: How many individual values the attribute consists of\n" | 
					
						
							| 
									
										
										
										
											2020-10-11 18:19:42 -04:00
										 |  |  |     "      (e.g. 2 for uv coordinates).\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 15:25:22 +01:00
										 |  |  |     "   :type len: int\n" | 
					
						
							| 
									
										
										
										
											2022-09-19 14:22:31 +10:00
										 |  |  |     "   :arg fetch_mode: How values from memory will be converted when used in the shader.\n" | 
					
						
							| 
									
										
										
										
											2020-10-11 18:19:42 -04:00
										 |  |  |     "      This is mainly useful for memory optimizations when you want to store values with\n" | 
					
						
							|  |  |  |     "      reduced precision. E.g. you can store a float in only 1 byte but it will be\n" | 
					
						
							|  |  |  |     "      converted to a normal 4 byte float when used.\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 15:25:22 +01:00
										 |  |  |     "      Possible values are `FLOAT`, `INT`, `INT_TO_FLOAT_UNIT` and `INT_TO_FLOAT`.\n" | 
					
						
							| 
									
										
										
										
											2021-03-16 12:48:00 -03:00
										 |  |  |     "   :type fetch_mode: str\n"); | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static PyObject *pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |   const char *id; | 
					
						
							|  |  |  |   uint len; | 
					
						
							|  |  |  |   struct PyC_StringEnum comp_type = {pygpu_vertcomptype_items, GPU_COMP_I8}; | 
					
						
							|  |  |  |   struct PyC_StringEnum fetch_mode = {pygpu_vertfetchmode_items, GPU_FETCH_FLOAT}; | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |   if (self->fmt.attr_len == GPU_VERT_ATTR_MAX_LEN) { | 
					
						
							| 
									
										
										
										
											2019-08-01 13:53:25 +10:00
										 |  |  |     PyErr_SetString(PyExc_ValueError, "Maximum attr reached " STRINGIFY(GPU_VERT_ATTR_MAX_LEN)); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 00:22:57 -03:00
										 |  |  |   static const char *_keywords[] = {"id", "comp_type", "len", "fetch_mode", NULL}; | 
					
						
							| 
									
										
										
										
											2022-04-08 09:41:28 +10:00
										 |  |  |   static _PyArg_Parser _parser = { | 
					
						
							|  |  |  |       "$"  /* Keyword only arguments. */ | 
					
						
							|  |  |  |       "s"  /* `id` */ | 
					
						
							|  |  |  |       "O&" /* `comp_type` */ | 
					
						
							|  |  |  |       "I"  /* `len` */ | 
					
						
							|  |  |  |       "O&" /* `fetch_mode` */ | 
					
						
							|  |  |  |       ":attr_add", | 
					
						
							|  |  |  |       _keywords, | 
					
						
							|  |  |  |       0, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2018-09-27 00:22:57 -03:00
										 |  |  |   if (!_PyArg_ParseTupleAndKeywordsFast(args, | 
					
						
							|  |  |  |                                         kwds, | 
					
						
							|  |  |  |                                         &_parser, | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |                                         &id, | 
					
						
							|  |  |  |                                         PyC_ParseStringEnum, | 
					
						
							|  |  |  |                                         &comp_type, | 
					
						
							|  |  |  |                                         &len, | 
					
						
							|  |  |  |                                         PyC_ParseStringEnum, | 
					
						
							|  |  |  |                                         &fetch_mode)) { | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |     return NULL; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2019-04-17 06:17:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |   uint attr_id = GPU_vertformat_attr_add( | 
					
						
							| 
									
										
										
										
											2021-02-22 08:26:45 -03:00
										 |  |  |       &self->fmt, id, comp_type.value_found, len, fetch_mode.value_found); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |   return PyLong_FromLong(attr_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static struct PyMethodDef pygpu_vertformat__tp_methods[] = { | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |     {"attr_add", | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |      (PyCFunction)pygpu_vertformat_attr_add, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |      METH_VARARGS | METH_KEYWORDS, | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |      pygpu_vertformat_attr_add_doc}, | 
					
						
							| 
									
										
										
										
											2019-02-03 14:01:45 +11:00
										 |  |  |     {NULL, NULL, 0, NULL}, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | static void pygpu_vertformat__tp_dealloc(BPyGPUVertFormat *self) | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | { | 
					
						
							|  |  |  |   Py_TYPE(self)->tp_free(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  | PyDoc_STRVAR(pygpu_vertformat__tp_doc, | 
					
						
							| 
									
										
										
										
											2018-11-13 15:25:22 +01:00
										 |  |  |              ".. class:: GPUVertFormat()\n" | 
					
						
							|  |  |  |              "\n" | 
					
						
							|  |  |  |              "   This object contains information about the structure of a vertex buffer.\n"); | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | PyTypeObject BPyGPUVertFormat_Type = { | 
					
						
							|  |  |  |     PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUVertFormat", | 
					
						
							|  |  |  |     .tp_basicsize = sizeof(BPyGPUVertFormat), | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     .tp_dealloc = (destructor)pygpu_vertformat__tp_dealloc, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  |     .tp_flags = Py_TPFLAGS_DEFAULT, | 
					
						
							| 
									
										
										
										
											2021-02-17 10:16:41 -03:00
										 |  |  |     .tp_doc = pygpu_vertformat__tp_doc, | 
					
						
							|  |  |  |     .tp_methods = pygpu_vertformat__tp_methods, | 
					
						
							|  |  |  |     .tp_new = pygpu_vertformat__tp_new, | 
					
						
							| 
									
										
										
										
											2018-09-05 21:10:42 -03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Public API
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *BPyGPUVertFormat_CreatePyObject(GPUVertFormat *fmt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   BPyGPUVertFormat *self; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   self = PyObject_New(BPyGPUVertFormat, &BPyGPUVertFormat_Type); | 
					
						
							|  |  |  |   if (fmt) { | 
					
						
							|  |  |  |     self->fmt = *fmt; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else { | 
					
						
							|  |  |  |     memset(&self->fmt, 0, sizeof(self->fmt)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (PyObject *)self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ |