Blender V5.0
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
11
12#include <Python.h>
13
15#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
16
17#include "gpu_py.hh"
18#include "gpu_py_vertex_format.hh" /* own include */
19
20/* -------------------------------------------------------------------- */
25
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 {0, nullptr},
43};
44
46
47/* -------------------------------------------------------------------- */
50
51static PyObject *pygpu_vertformat__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
52{
54
55 if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) {
56 PyErr_SetString(PyExc_ValueError, "This function takes no arguments");
57 return nullptr;
58 }
59 return BPyGPUVertFormat_CreatePyObject(nullptr);
60}
61
63{
64 if (type == GPU_COMP_I10) {
65 return 4; /* Always packed as 10_10_10_2. */
66 }
67 BLI_assert(type <= GPU_COMP_F32); /* Other types have irregular sizes (not bytes). */
68 const uint sizes[] = {1, 1, 2, 2, 4, 4, 4};
69 return len * sizes[type];
70}
71
73 /* Wrap. */
74 pygpu_vertformat_attr_add_doc,
75 ".. method:: attr_add(id, comp_type, len, fetch_mode)\n"
76 "\n"
77 " Add a new attribute to the format.\n"
78 "\n"
79 " :arg id: Name the attribute. Often ``position``, ``normal``, ...\n"
80 " :type id: str\n"
81 " :arg comp_type: The data type that will be used store the value in memory.\n"
82 " Possible values are ``I8``, ``U8``, ``I16``, ``U16``, ``I32``, ``U32``, ``F32`` & "
83 "``I10``.\n"
84 " :type comp_type: str\n"
85 " :arg len: How many individual values the attribute consists of\n"
86 " (e.g. 2 for uv coordinates).\n"
87 " :type len: int\n"
88 " :arg fetch_mode: How values from memory will be converted when used in the shader.\n"
89 " This is mainly useful for memory optimizations when you want to store values with\n"
90 " reduced precision. E.g. you can store a float in only 1 byte but it will be\n"
91 " converted to a normal 4 byte float when used.\n"
92 " Possible values are ``FLOAT``, ``INT`` or ``INT_TO_FLOAT_UNIT``.\n"
93 " :type fetch_mode: str\n");
94static PyObject *pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
95{
96 const char *id;
97 uint len;
100
101 if (self->fmt.attr_len == GPU_VERT_ATTR_MAX_LEN) {
102 PyErr_SetString(PyExc_ValueError, "Maximum attr reached " STRINGIFY(GPU_VERT_ATTR_MAX_LEN));
103 return nullptr;
104 }
105
106 static const char *_keywords[] = {"id", "comp_type", "len", "fetch_mode", nullptr};
107 static _PyArg_Parser _parser = {
109 "$" /* Keyword only arguments. */
110 "s" /* `id` */
111 "O&" /* `comp_type` */
112 "I" /* `len` */
113 "O&" /* `fetch_mode` */
114 ":attr_add",
115 _keywords,
116 nullptr,
117 };
118 if (!_PyArg_ParseTupleAndKeywordsFast(args,
119 kwds,
120 &_parser,
121 &id,
123 &comp_type,
124 &len,
126 &fetch_mode))
127 {
128 return nullptr;
129 }
130
131 GPUVertCompType comp_type_enum = GPUVertCompType(comp_type.value_found);
132 GPUVertFetchMode fetch_mode_enum = GPUVertFetchMode(fetch_mode.value_found);
133
134 if (len > 4) {
135 PyErr_WarnEx(
136 PyExc_DeprecationWarning,
137 "Using GPUVertFormat.attr_add(...) with component count greater than 4 is deprecated. "
138 "Use several attributes for each matrix columns instead.",
139 1);
140 }
141
142 if (attr_size(comp_type_enum, len) % 4 != 0) {
143 PyErr_WarnEx(PyExc_DeprecationWarning,
144 "Using GPUVertFormat.attr_add(...) with a format that is not 4 bytes aligned is "
145 "deprecated. Add padding components and/or higher precision integers.",
146 1);
147 }
148
150 &self->fmt, id, comp_type_enum, len, fetch_mode_enum);
151
152 return PyLong_FromLong(attr_id);
153}
154
155#ifdef __GNUC__
156# ifdef __clang__
157# pragma clang diagnostic push
158# pragma clang diagnostic ignored "-Wcast-function-type"
159# else
160# pragma GCC diagnostic push
161# pragma GCC diagnostic ignored "-Wcast-function-type"
162# endif
163#endif
164
165static PyMethodDef pygpu_vertformat__tp_methods[] = {
166 {"attr_add",
167 (PyCFunction)pygpu_vertformat_attr_add,
168 METH_VARARGS | METH_KEYWORDS,
169 pygpu_vertformat_attr_add_doc},
170 {nullptr, nullptr, 0, nullptr},
171};
172
173#ifdef __GNUC__
174# ifdef __clang__
175# pragma clang diagnostic pop
176# else
177# pragma GCC diagnostic pop
178# endif
179#endif
180
182{
183 Py_TYPE(self)->tp_free(self);
184}
185
187 /* Wrap. */
188 pygpu_vertformat__tp_doc,
189 ".. class:: GPUVertFormat()\n"
190 "\n"
191 " This object contains information about the structure of a vertex buffer.\n");
192PyTypeObject BPyGPUVertFormat_Type = {
193 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
194 /*tp_name*/ "GPUVertFormat",
195 /*tp_basicsize*/ sizeof(BPyGPUVertFormat),
196 /*tp_itemsize*/ 0,
197 /*tp_dealloc*/ (destructor)pygpu_vertformat__tp_dealloc,
198 /*tp_vectorcall_offset*/ 0,
199 /*tp_getattr*/ nullptr,
200 /*tp_setattr*/ nullptr,
201 /*tp_as_async*/ nullptr,
202 /*tp_repr*/ nullptr,
203 /*tp_as_number*/ nullptr,
204 /*tp_as_sequence*/ nullptr,
205 /*tp_as_mapping*/ nullptr,
206 /*tp_hash*/ nullptr,
207 /*tp_call*/ nullptr,
208 /*tp_str*/ nullptr,
209 /*tp_getattro*/ nullptr,
210 /*tp_setattro*/ nullptr,
211 /*tp_as_buffer*/ nullptr,
212 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
213 /*tp_doc*/ pygpu_vertformat__tp_doc,
214 /*tp_traverse*/ nullptr,
215 /*tp_clear*/ nullptr,
216 /*tp_richcompare*/ nullptr,
217 /*tp_weaklistoffset*/ 0,
218 /*tp_iter*/ nullptr,
219 /*tp_iternext*/ nullptr,
220 /*tp_methods*/ pygpu_vertformat__tp_methods,
221 /*tp_members*/ nullptr,
222 /*tp_getset*/ nullptr,
223 /*tp_base*/ nullptr,
224 /*tp_dict*/ nullptr,
225 /*tp_descr_get*/ nullptr,
226 /*tp_descr_set*/ nullptr,
227 /*tp_dictoffset*/ 0,
228 /*tp_init*/ nullptr,
229 /*tp_alloc*/ nullptr,
230 /*tp_new*/ pygpu_vertformat__tp_new,
231 /*tp_free*/ nullptr,
232 /*tp_is_gc*/ nullptr,
233 /*tp_bases*/ nullptr,
234 /*tp_mro*/ nullptr,
235 /*tp_cache*/ nullptr,
236 /*tp_subclasses*/ nullptr,
237 /*tp_weaklist*/ nullptr,
238 /*tp_del*/ nullptr,
239 /*tp_version_tag*/ 0,
240 /*tp_finalize*/ nullptr,
241 /*tp_vectorcall*/ nullptr,
242};
243
245
246/* -------------------------------------------------------------------- */
249
251{
253
255 if (fmt) {
256 self->fmt = *fmt;
257 }
258 else {
259 memset(&self->fmt, 0, sizeof(self->fmt));
260 }
261
262 return (PyObject *)self;
263}
264
#define BLI_assert(a)
Definition BLI_assert.h:46
unsigned int uint
#define STRINGIFY(x)
static constexpr int GPU_VERT_ATTR_MAX_LEN
GPUVertFetchMode
@ GPU_FETCH_FLOAT
@ GPU_FETCH_INT_TO_FLOAT_UNIT
@ GPU_FETCH_INT
uint GPU_vertformat_attr_add_legacy(GPUVertFormat *, blender::StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
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
struct @021025263243242147216143265077100330027142264337::@240232116316110053135047106323056371161236243121 attr_id
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:20
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`` & " "``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`` or ``INT_TO_FLOAT_UNIT``.\n" " :type fetch_mode: str\n")
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)
static PyMethodDef pygpu_vertformat__tp_methods[]
static void pygpu_vertformat__tp_dealloc(BPyGPUVertFormat *self)
static PyC_StringEnumItems pygpu_vertcomptype_items[]
static uint attr_size(GPUVertCompType type, int len)
static PyC_StringEnumItems pygpu_vertfetchmode_items[]
int PyC_ParseStringEnum(PyObject *o, void *p)
header-only compatibility defines.
#define PY_ARG_PARSER_HEAD_COMPAT()
uint len