Blender  V3.3
gpu_py_vertex_format.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
10 #include <Python.h>
11 
12 #include "../generic/py_capi_utils.h"
13 #include "../generic/python_utildefines.h"
14 
15 #include "gpu_py_vertex_format.h" /* own include */
16 
17 /* -------------------------------------------------------------------- */
24  {GPU_COMP_I8, "I8"},
25  {GPU_COMP_U8, "U8"},
26  {GPU_COMP_I16, "I16"},
27  {GPU_COMP_U16, "U16"},
28  {GPU_COMP_I32, "I32"},
29  {GPU_COMP_U32, "U32"},
30  {GPU_COMP_F32, "F32"},
31  {GPU_COMP_I10, "I10"},
32  {0, NULL},
33 };
34 
36  {GPU_FETCH_FLOAT, "FLOAT"},
37  {GPU_FETCH_INT, "INT"},
38  {GPU_FETCH_INT_TO_FLOAT_UNIT, "INT_TO_FLOAT_UNIT"},
39  {GPU_FETCH_INT_TO_FLOAT, "INT_TO_FLOAT"},
40  {0, NULL},
41 };
42 
45 /* -------------------------------------------------------------------- */
49 static PyObject *pygpu_vertformat__tp_new(PyTypeObject *UNUSED(type),
50  PyObject *args,
51  PyObject *kwds)
52 {
53  if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) {
54  PyErr_SetString(PyExc_ValueError, "This function takes no arguments");
55  return NULL;
56  }
58 }
59 
61  pygpu_vertformat_attr_add_doc,
62  ".. method:: attr_add(id, comp_type, len, fetch_mode)\n"
63  "\n"
64  " Add a new attribute to the format.\n"
65  "\n"
66  " :param id: Name the attribute. Often `position`, `normal`, ...\n"
67  " :type id: str\n"
68  " :param comp_type: The data type that will be used store the value in memory.\n"
69  " Possible values are `I8`, `U8`, `I16`, `U16`, `I32`, `U32`, `F32` and `I10`.\n"
70  " :type comp_type: str\n"
71  " :param len: How many individual values the attribute consists of\n"
72  " (e.g. 2 for uv coordinates).\n"
73  " :type len: int\n"
74  " :param fetch_mode: How values from memory will be converted when used in the shader.\n"
75  " This is mainly useful for memory optimizations when you want to store values with\n"
76  " reduced precision. E.g. you can store a float in only 1 byte but it will be\n"
77  " converted to a normal 4 byte float when used.\n"
78  " Possible values are `FLOAT`, `INT`, `INT_TO_FLOAT_UNIT` and `INT_TO_FLOAT`.\n"
79  " :type fetch_mode: str\n");
80 static PyObject *pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
81 {
82  const char *id;
83  uint len;
86 
87  if (self->fmt.attr_len == GPU_VERT_ATTR_MAX_LEN) {
88  PyErr_SetString(PyExc_ValueError, "Maximum attr reached " STRINGIFY(GPU_VERT_ATTR_MAX_LEN));
89  return NULL;
90  }
91 
92  static const char *_keywords[] = {"id", "comp_type", "len", "fetch_mode", NULL};
93  static _PyArg_Parser _parser = {
94  "$" /* Keyword only arguments. */
95  "s" /* `id` */
96  "O&" /* `comp_type` */
97  "I" /* `len` */
98  "O&" /* `fetch_mode` */
99  ":attr_add",
100  _keywords,
101  0,
102  };
103  if (!_PyArg_ParseTupleAndKeywordsFast(args,
104  kwds,
105  &_parser,
106  &id,
108  &comp_type,
109  &len,
111  &fetch_mode)) {
112  return NULL;
113  }
114 
116  &self->fmt, id, comp_type.value_found, len, fetch_mode.value_found);
117  return PyLong_FromLong(attr_id);
118 }
119 
120 static struct PyMethodDef pygpu_vertformat__tp_methods[] = {
121  {"attr_add",
122  (PyCFunction)pygpu_vertformat_attr_add,
123  METH_VARARGS | METH_KEYWORDS,
124  pygpu_vertformat_attr_add_doc},
125  {NULL, NULL, 0, NULL},
126 };
127 
129 {
130  Py_TYPE(self)->tp_free(self);
131 }
132 
133 PyDoc_STRVAR(pygpu_vertformat__tp_doc,
134  ".. class:: GPUVertFormat()\n"
135  "\n"
136  " This object contains information about the structure of a vertex buffer.\n");
137 PyTypeObject BPyGPUVertFormat_Type = {
138  PyVarObject_HEAD_INIT(NULL, 0).tp_name = "GPUVertFormat",
139  .tp_basicsize = sizeof(BPyGPUVertFormat),
140  .tp_dealloc = (destructor)pygpu_vertformat__tp_dealloc,
141  .tp_flags = Py_TPFLAGS_DEFAULT,
142  .tp_doc = pygpu_vertformat__tp_doc,
143  .tp_methods = pygpu_vertformat__tp_methods,
144  .tp_new = pygpu_vertformat__tp_new,
145 };
146 
149 /* -------------------------------------------------------------------- */
154 {
155  BPyGPUVertFormat *self;
156 
157  self = PyObject_New(BPyGPUVertFormat, &BPyGPUVertFormat_Type);
158  if (fmt) {
159  self->fmt = *fmt;
160  }
161  else {
162  memset(&self->fmt, 0, sizeof(self->fmt));
163  }
164 
165  return (PyObject *)self;
166 }
167 
unsigned int uint
Definition: BLI_sys_types.h:67
#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
PyObject * self
Definition: bpy_driver.c:165
int len
Definition: draw_manager.c:108
struct @653::@656 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)