Blender  V2.93
gpu_py_vertex_format.c
Go to the documentation of this file.
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 
24 #include <Python.h>
25 
26 #include "BLI_math.h"
27 
28 #include "MEM_guardedalloc.h"
29 
30 #include "../generic/py_capi_utils.h"
31 #include "../generic/python_utildefines.h"
32 
33 #include "gpu_py_vertex_format.h" /* own include */
34 
35 /* -------------------------------------------------------------------- */
42  {GPU_COMP_I8, "I8"},
43  {GPU_COMP_U8, "U8"},
44  {GPU_COMP_I16, "I16"},
45  {GPU_COMP_U16, "U16"},
46  {GPU_COMP_I32, "I32"},
47  {GPU_COMP_U32, "U32"},
48  {GPU_COMP_F32, "F32"},
49  {GPU_COMP_I10, "I10"},
50  {0, NULL},
51 };
52 
54  {GPU_FETCH_FLOAT, "FLOAT"},
55  {GPU_FETCH_INT, "INT"},
56  {GPU_FETCH_INT_TO_FLOAT_UNIT, "INT_TO_FLOAT_UNIT"},
57  {GPU_FETCH_INT_TO_FLOAT, "INT_TO_FLOAT"},
58  {0, NULL},
59 };
60 
63 /* -------------------------------------------------------------------- */
67 static PyObject *pygpu_vertformat__tp_new(PyTypeObject *UNUSED(type),
68  PyObject *args,
69  PyObject *kwds)
70 {
71  if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) {
72  PyErr_SetString(PyExc_ValueError, "This function takes no arguments");
73  return NULL;
74  }
76 }
77 
79  pygpu_vertformat_attr_add_doc,
80  ".. method:: attr_add(id, comp_type, len, fetch_mode)\n"
81  "\n"
82  " Add a new attribute to the format.\n"
83  "\n"
84  " :param id: Name the attribute. Often `position`, `normal`, ...\n"
85  " :type id: str\n"
86  " :param comp_type: The data type that will be used store the value in memory.\n"
87  " Possible values are `I8`, `U8`, `I16`, `U16`, `I32`, `U32`, `F32` and `I10`.\n"
88  " :type comp_type: str\n"
89  " :param len: How many individual values the attribute consists of\n"
90  " (e.g. 2 for uv coordinates).\n"
91  " :type len: int\n"
92  " :param fetch_mode: How values from memory will be converted when used in the shader.\n"
93  " This is mainly useful for memory optimizations when you want to store values with\n"
94  " reduced precision. E.g. you can store a float in only 1 byte but it will be\n"
95  " converted to a normal 4 byte float when used.\n"
96  " Possible values are `FLOAT`, `INT`, `INT_TO_FLOAT_UNIT` and `INT_TO_FLOAT`.\n"
97  " :type fetch_mode: str\n");
98 static PyObject *pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
99 {
100  const char *id;
101  uint len;
104 
105  if (self->fmt.attr_len == GPU_VERT_ATTR_MAX_LEN) {
106  PyErr_SetString(PyExc_ValueError, "Maximum attr reached " STRINGIFY(GPU_VERT_ATTR_MAX_LEN));
107  return NULL;
108  }
109 
110  static const char *_keywords[] = {"id", "comp_type", "len", "fetch_mode", NULL};
111  static _PyArg_Parser _parser = {"$sO&IO&:attr_add", _keywords, 0};
112  if (!_PyArg_ParseTupleAndKeywordsFast(args,
113  kwds,
114  &_parser,
115  &id,
117  &comp_type,
118  &len,
120  &fetch_mode)) {
121  return NULL;
122  }
123 
125  &self->fmt, id, comp_type.value_found, len, fetch_mode.value_found);
126  return PyLong_FromLong(attr_id);
127 }
128 
129 static struct PyMethodDef pygpu_vertformat__tp_methods[] = {
130  {"attr_add",
131  (PyCFunction)pygpu_vertformat_attr_add,
132  METH_VARARGS | METH_KEYWORDS,
133  pygpu_vertformat_attr_add_doc},
134  {NULL, NULL, 0, NULL},
135 };
136 
138 {
139  Py_TYPE(self)->tp_free(self);
140 }
141 
142 PyDoc_STRVAR(pygpu_vertformat__tp_doc,
143  ".. class:: GPUVertFormat()\n"
144  "\n"
145  " This object contains information about the structure of a vertex buffer.\n");
146 PyTypeObject BPyGPUVertFormat_Type = {
147  PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUVertFormat",
148  .tp_basicsize = sizeof(BPyGPUVertFormat),
149  .tp_dealloc = (destructor)pygpu_vertformat__tp_dealloc,
150  .tp_flags = Py_TPFLAGS_DEFAULT,
151  .tp_doc = pygpu_vertformat__tp_doc,
152  .tp_methods = pygpu_vertformat__tp_methods,
153  .tp_new = pygpu_vertformat__tp_new,
154 };
155 
158 /* -------------------------------------------------------------------- */
163 {
164  BPyGPUVertFormat *self;
165 
166  self = PyObject_New(BPyGPUVertFormat, &BPyGPUVertFormat_Type);
167  if (fmt) {
168  self->fmt = *fmt;
169  }
170  else {
171  memset(&self->fmt, 0, sizeof(self->fmt));
172  }
173 
174  return (PyObject *)self;
175 }
176 
unsigned int uint
Definition: BLI_sys_types.h:83
#define STRINGIFY(x)
#define UNUSED(x)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT_TO_FLOAT_UNIT
@ GPU_FETCH_INT
@ GPU_FETCH_INT_TO_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
#define GPU_VERT_ATTR_MAX_LEN
@ GPU_COMP_U16
@ GPU_COMP_I10
@ GPU_COMP_F32
@ GPU_COMP_I32
@ GPU_COMP_I8
@ GPU_COMP_U32
@ GPU_COMP_I16
@ GPU_COMP_U8
Read Guarded memory(de)allocation.
PyObject * self
Definition: bpy_driver.c:185
struct @612::@615 attr_id
PyTypeObject BPyGPUVertFormat_Type
static struct PyC_StringEnumItems pygpu_vertfetchmode_items[]
PyObject * BPyGPUVertFormat_CreatePyObject(GPUVertFormat *fmt)
static PyObject * pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
static struct PyC_StringEnumItems pygpu_vertcomptype_items[]
PyDoc_STRVAR(pygpu_vertformat_attr_add_doc, ".. method:: attr_add(id, comp_type, len, fetch_mode)\n" "\n" " Add a new attribute to the format.\n" "\n" " :param id: Name the attribute. Often `position`, `normal`, ...\n" " :type id: str\n" " :param comp_type: The data type that will be used store the value in memory.\n" " Possible values are `I8`, `U8`, `I16`, `U16`, `I32`, `U32`, `F32` and `I10`.\n" " :type comp_type: str\n" " :param len: How many individual values the attribute consists of\n" " (e.g. 2 for uv coordinates).\n" " :type len: int\n" " :param fetch_mode: How values from memory will be converted when used in the shader.\n" " 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" " Possible values are `FLOAT`, `INT`, `INT_TO_FLOAT_UNIT` and `INT_TO_FLOAT`.\n" " :type fetch_mode: str\n")
static void pygpu_vertformat__tp_dealloc(BPyGPUVertFormat *self)
static PyObject * pygpu_vertformat__tp_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
static struct PyMethodDef pygpu_vertformat__tp_methods[]
struct BPyGPUVertFormat BPyGPUVertFormat
int PyC_ParseStringEnum(PyObject *o, void *p)
uint len