Blender V4.3
gpu_py_vertex_format.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
12#include <Python.h>
13
16
17#include "gpu_py.hh"
18#include "gpu_py_vertex_format.hh" /* own include */
19
20/* -------------------------------------------------------------------- */
27 {GPU_COMP_I8, "I8"},
28 {GPU_COMP_U8, "U8"},
29 {GPU_COMP_I16, "I16"},
30 {GPU_COMP_U16, "U16"},
31 {GPU_COMP_I32, "I32"},
32 {GPU_COMP_U32, "U32"},
33 {GPU_COMP_F32, "F32"},
34 {GPU_COMP_I10, "I10"},
35 {0, nullptr},
36};
37
39 {GPU_FETCH_FLOAT, "FLOAT"},
40 {GPU_FETCH_INT, "INT"},
41 {GPU_FETCH_INT_TO_FLOAT_UNIT, "INT_TO_FLOAT_UNIT"},
42 {GPU_FETCH_INT_TO_FLOAT, "INT_TO_FLOAT"},
43 {0, nullptr},
44};
45
48/* -------------------------------------------------------------------- */
52static PyObject *pygpu_vertformat__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
53{
55
56 if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) {
57 PyErr_SetString(PyExc_ValueError, "This function takes no arguments");
58 return nullptr;
59 }
60 return BPyGPUVertFormat_CreatePyObject(nullptr);
61}
62
64 /* Wrap. */
65 pygpu_vertformat_attr_add_doc,
66 ".. method:: attr_add(id, comp_type, len, fetch_mode)\n"
67 "\n"
68 " Add a new attribute to the format.\n"
69 "\n"
70 " :arg id: Name the attribute. Often `position`, `normal`, ...\n"
71 " :type id: str\n"
72 " :arg comp_type: The data type that will be used store the value in memory.\n"
73 " Possible values are `I8`, `U8`, `I16`, `U16`, `I32`, `U32`, `F32` and `I10`.\n"
74 " :type comp_type: str\n"
75 " :arg len: How many individual values the attribute consists of\n"
76 " (e.g. 2 for uv coordinates).\n"
77 " :type len: int\n"
78 " :arg fetch_mode: How values from memory will be converted when used in the shader.\n"
79 " This is mainly useful for memory optimizations when you want to store values with\n"
80 " reduced precision. E.g. you can store a float in only 1 byte but it will be\n"
81 " converted to a normal 4 byte float when used.\n"
82 " Possible values are `FLOAT`, `INT`, `INT_TO_FLOAT_UNIT` and `INT_TO_FLOAT`.\n"
83 " :type fetch_mode: str\n");
84static PyObject *pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
85{
86 const char *id;
87 uint len;
90
91 if (self->fmt.attr_len == GPU_VERT_ATTR_MAX_LEN) {
92 PyErr_SetString(PyExc_ValueError, "Maximum attr reached " STRINGIFY(GPU_VERT_ATTR_MAX_LEN));
93 return nullptr;
94 }
95
96 static const char *_keywords[] = {"id", "comp_type", "len", "fetch_mode", nullptr};
97 static _PyArg_Parser _parser = {
99 "$" /* Keyword only arguments. */
100 "s" /* `id` */
101 "O&" /* `comp_type` */
102 "I" /* `len` */
103 "O&" /* `fetch_mode` */
104 ":attr_add",
105 _keywords,
106 nullptr,
107 };
108 if (!_PyArg_ParseTupleAndKeywordsFast(args,
109 kwds,
110 &_parser,
111 &id,
113 &comp_type,
114 &len,
116 &fetch_mode))
117 {
118 return nullptr;
119 }
120
122 id,
123 GPUVertCompType(comp_type.value_found),
124 len,
125 GPUVertFetchMode(fetch_mode.value_found));
126 return PyLong_FromLong(attr_id);
127}
128
129#if (defined(__GNUC__) && !defined(__clang__))
130# pragma GCC diagnostic push
131# pragma GCC diagnostic ignored "-Wcast-function-type"
132#endif
133
134static PyMethodDef pygpu_vertformat__tp_methods[] = {
135 {"attr_add",
136 (PyCFunction)pygpu_vertformat_attr_add,
137 METH_VARARGS | METH_KEYWORDS,
138 pygpu_vertformat_attr_add_doc},
139 {nullptr, nullptr, 0, nullptr},
140};
141
142#if (defined(__GNUC__) && !defined(__clang__))
143# pragma GCC diagnostic pop
144#endif
145
147{
148 Py_TYPE(self)->tp_free(self);
149}
150
152 /* Wrap. */
153 pygpu_vertformat__tp_doc,
154 ".. class:: GPUVertFormat()\n"
155 "\n"
156 " This object contains information about the structure of a vertex buffer.\n");
157PyTypeObject BPyGPUVertFormat_Type = {
158 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
159 /*tp_name*/ "GPUVertFormat",
160 /*tp_basicsize*/ sizeof(BPyGPUVertFormat),
161 /*tp_itemsize*/ 0,
162 /*tp_dealloc*/ (destructor)pygpu_vertformat__tp_dealloc,
163 /*tp_vectorcall_offset*/ 0,
164 /*tp_getattr*/ nullptr,
165 /*tp_setattr*/ nullptr,
166 /*tp_as_async*/ nullptr,
167 /*tp_repr*/ nullptr,
168 /*tp_as_number*/ nullptr,
169 /*tp_as_sequence*/ nullptr,
170 /*tp_as_mapping*/ nullptr,
171 /*tp_hash*/ nullptr,
172 /*tp_call*/ nullptr,
173 /*tp_str*/ nullptr,
174 /*tp_getattro*/ nullptr,
175 /*tp_setattro*/ nullptr,
176 /*tp_as_buffer*/ nullptr,
177 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
178 /*tp_doc*/ pygpu_vertformat__tp_doc,
179 /*tp_traverse*/ nullptr,
180 /*tp_clear*/ nullptr,
181 /*tp_richcompare*/ nullptr,
182 /*tp_weaklistoffset*/ 0,
183 /*tp_iter*/ nullptr,
184 /*tp_iternext*/ nullptr,
185 /*tp_methods*/ pygpu_vertformat__tp_methods,
186 /*tp_members*/ nullptr,
187 /*tp_getset*/ nullptr,
188 /*tp_base*/ nullptr,
189 /*tp_dict*/ nullptr,
190 /*tp_descr_get*/ nullptr,
191 /*tp_descr_set*/ nullptr,
192 /*tp_dictoffset*/ 0,
193 /*tp_init*/ nullptr,
194 /*tp_alloc*/ nullptr,
195 /*tp_new*/ pygpu_vertformat__tp_new,
196 /*tp_free*/ nullptr,
197 /*tp_is_gc*/ nullptr,
198 /*tp_bases*/ nullptr,
199 /*tp_mro*/ nullptr,
200 /*tp_cache*/ nullptr,
201 /*tp_subclasses*/ nullptr,
202 /*tp_weaklist*/ nullptr,
203 /*tp_del*/ nullptr,
204 /*tp_version_tag*/ 0,
205 /*tp_finalize*/ nullptr,
206 /*tp_vectorcall*/ nullptr,
207};
208
211/* -------------------------------------------------------------------- */
216{
218
220 if (fmt) {
221 self->fmt = *fmt;
222 }
223 else {
224 memset(&self->fmt, 0, sizeof(self->fmt));
225 }
226
227 return (PyObject *)self;
228}
229
unsigned int uint
#define STRINGIFY(x)
GPUVertFetchMode
@ 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
GPUVertCompType
@ 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
int len
struct @620::@623 attr_id
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:18
PyTypeObject BPyGPUVertFormat_Type
static PyObject * pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
static PyObject * pygpu_vertformat__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
PyObject * BPyGPUVertFormat_CreatePyObject(GPUVertFormat *fmt)
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" " :arg id: Name the attribute. Often `position`, `normal`, ...\n" " :type id: str\n" " :arg 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" " :arg len: How many individual values the attribute consists of\n" " (e.g. 2 for uv coordinates).\n" " :type len: int\n" " :arg 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 PyMethodDef pygpu_vertformat__tp_methods[]
static void pygpu_vertformat__tp_dealloc(BPyGPUVertFormat *self)
static PyC_StringEnumItems pygpu_vertcomptype_items[]
static PyC_StringEnumItems pygpu_vertfetchmode_items[]
int PyC_ParseStringEnum(PyObject *o, void *p)
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()