Blender V5.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
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
20namespace blender {
21
22/* -------------------------------------------------------------------- */
27
29 {GPU_COMP_I8, "I8"},
30 {GPU_COMP_U8, "U8"},
31 {GPU_COMP_I16, "I16"},
32 {GPU_COMP_U16, "U16"},
33 {GPU_COMP_I32, "I32"},
34 {GPU_COMP_U32, "U32"},
35 {GPU_COMP_F32, "F32"},
36 {GPU_COMP_I10, "I10"},
37 {0, nullptr},
38};
39
41 {GPU_FETCH_FLOAT, "FLOAT"},
42 {GPU_FETCH_INT, "INT"},
43 {GPU_FETCH_INT_TO_FLOAT_UNIT, "INT_TO_FLOAT_UNIT"},
44 {0, nullptr},
45};
46
48
49/* -------------------------------------------------------------------- */
52
53static PyObject *pygpu_vertformat__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
54{
56
57 if (PyTuple_GET_SIZE(args) || (kwds && PyDict_Size(kwds))) {
58 PyErr_SetString(PyExc_ValueError, "This function takes no arguments");
59 return nullptr;
60 }
61 return BPyGPUVertFormat_CreatePyObject(nullptr);
62}
63
65{
66 if (type == GPU_COMP_I10) {
67 return 4; /* Always packed as 10_10_10_2. */
68 }
69 BLI_assert(type <= GPU_COMP_F32); /* Other types have irregular sizes (not bytes). */
70 const uint sizes[] = {1, 1, 2, 2, 4, 4, 4};
71 return len * sizes[type];
72}
73
75 /* Wrap. */
76 pygpu_vertformat_attr_add_doc,
77 ".. method:: attr_add(id, comp_type, len, fetch_mode)\n"
78 "\n"
79 " Add a new attribute to the format.\n"
80 "\n"
81 " :param id: Name of the attribute. Often ``position``, ``normal``, ...\n"
82 " :type id: str\n"
83 " :param comp_type: The data type that will be used to store the value in memory.\n"
84 " :type comp_type: Literal['I8', 'U8', 'I16', 'U16', 'I32', 'U32', 'F32', 'I10']\n"
85 " :param len: How many individual values the attribute consists of\n"
86 " (e.g. 2 for uv coordinates).\n"
87 " :type len: int\n"
88 " :param 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 " :type fetch_mode: Literal['FLOAT', 'INT', 'INT_TO_FLOAT_UNIT']\n");
93static PyObject *pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
94{
95 const char *id;
96 uint len;
99
100 if (self->fmt.attr_len == GPU_VERT_ATTR_MAX_LEN) {
101 PyErr_SetString(PyExc_ValueError, "Maximum attr reached " STRINGIFY(GPU_VERT_ATTR_MAX_LEN));
102 return nullptr;
103 }
104
105 static const char *_keywords[] = {"id", "comp_type", "len", "fetch_mode", nullptr};
106 static _PyArg_Parser _parser = {
107 "$" /* Keyword only arguments. */
108 "s" /* `id` */
109 "O&" /* `comp_type` */
110 "I" /* `len` */
111 "O&" /* `fetch_mode` */
112 ":attr_add",
113 _keywords,
114 nullptr,
115 };
116 if (!_PyArg_ParseTupleAndKeywordsFast(args,
117 kwds,
118 &_parser,
119 &id,
121 &comp_type,
122 &len,
124 &fetch_mode))
125 {
126 return nullptr;
127 }
128
129 GPUVertCompType comp_type_enum = GPUVertCompType(comp_type.value_found);
130 GPUVertFetchMode fetch_mode_enum = GPUVertFetchMode(fetch_mode.value_found);
131
132 if (len > 4) {
133 PyErr_WarnEx(
134 PyExc_DeprecationWarning,
135 "Using GPUVertFormat.attr_add(...) with component count greater than 4 is deprecated. "
136 "Use several attributes for each matrix columns instead.",
137 1);
138 }
139
140 if (attr_size(comp_type_enum, len) % 4 != 0) {
141 PyErr_WarnEx(PyExc_DeprecationWarning,
142 "Using GPUVertFormat.attr_add(...) with a format that is not 4 bytes aligned is "
143 "deprecated. Add padding components and/or higher precision integers.",
144 1);
145 }
146
148 &self->fmt, id, comp_type_enum, len, fetch_mode_enum);
149
150 return PyLong_FromLong(attr_id);
151}
152
153#ifdef __GNUC__
154# ifdef __clang__
155# pragma clang diagnostic push
156# pragma clang diagnostic ignored "-Wcast-function-type"
157# else
158# pragma GCC diagnostic push
159# pragma GCC diagnostic ignored "-Wcast-function-type"
160# endif
161#endif
162
163static PyMethodDef pygpu_vertformat__tp_methods[] = {
164 {"attr_add",
165 reinterpret_cast<PyCFunction>(pygpu_vertformat_attr_add),
166 METH_VARARGS | METH_KEYWORDS,
167 pygpu_vertformat_attr_add_doc},
168 {nullptr, nullptr, 0, nullptr},
169};
170
171#ifdef __GNUC__
172# ifdef __clang__
173# pragma clang diagnostic pop
174# else
175# pragma GCC diagnostic pop
176# endif
177#endif
178
180{
181 Py_TYPE(self)->tp_free(self);
182}
183
185 /* Wrap. */
186 pygpu_vertformat__tp_doc,
187 ".. class:: GPUVertFormat()\n"
188 "\n"
189 " This object contains information about the structure of a vertex buffer.\n");
190PyTypeObject BPyGPUVertFormat_Type = {
191 /*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
192 /*tp_name*/ "GPUVertFormat",
193 /*tp_basicsize*/ sizeof(BPyGPUVertFormat),
194 /*tp_itemsize*/ 0,
195 /*tp_dealloc*/ reinterpret_cast<destructor>(pygpu_vertformat__tp_dealloc),
196 /*tp_vectorcall_offset*/ 0,
197 /*tp_getattr*/ nullptr,
198 /*tp_setattr*/ nullptr,
199 /*tp_as_async*/ nullptr,
200 /*tp_repr*/ nullptr,
201 /*tp_as_number*/ nullptr,
202 /*tp_as_sequence*/ nullptr,
203 /*tp_as_mapping*/ nullptr,
204 /*tp_hash*/ nullptr,
205 /*tp_call*/ nullptr,
206 /*tp_str*/ nullptr,
207 /*tp_getattro*/ nullptr,
208 /*tp_setattro*/ nullptr,
209 /*tp_as_buffer*/ nullptr,
210 /*tp_flags*/ Py_TPFLAGS_DEFAULT,
211 /*tp_doc*/ pygpu_vertformat__tp_doc,
212 /*tp_traverse*/ nullptr,
213 /*tp_clear*/ nullptr,
214 /*tp_richcompare*/ nullptr,
215 /*tp_weaklistoffset*/ 0,
216 /*tp_iter*/ nullptr,
217 /*tp_iternext*/ nullptr,
218 /*tp_methods*/ pygpu_vertformat__tp_methods,
219 /*tp_members*/ nullptr,
220 /*tp_getset*/ nullptr,
221 /*tp_base*/ nullptr,
222 /*tp_dict*/ nullptr,
223 /*tp_descr_get*/ nullptr,
224 /*tp_descr_set*/ nullptr,
225 /*tp_dictoffset*/ 0,
226 /*tp_init*/ nullptr,
227 /*tp_alloc*/ nullptr,
228 /*tp_new*/ pygpu_vertformat__tp_new,
229 /*tp_free*/ nullptr,
230 /*tp_is_gc*/ nullptr,
231 /*tp_bases*/ nullptr,
232 /*tp_mro*/ nullptr,
233 /*tp_cache*/ nullptr,
234 /*tp_subclasses*/ nullptr,
235 /*tp_weaklist*/ nullptr,
236 /*tp_del*/ nullptr,
237 /*tp_version_tag*/ 0,
238 /*tp_finalize*/ nullptr,
239 /*tp_vectorcall*/ nullptr,
240};
241
243
244/* -------------------------------------------------------------------- */
247
249{
250 BPyGPUVertFormat *self;
251
252 self = PyObject_New(BPyGPUVertFormat, &BPyGPUVertFormat_Type);
253 if (fmt) {
254 self->fmt = *fmt;
255 }
256 else {
257 memset(&self->fmt, 0, sizeof(self->fmt));
258 }
259
260 return reinterpret_cast<PyObject *>(self);
261}
262
264
265} // namespace blender
#define STRINGIFY(x)
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
Definition gpu_py.hh:45
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
PyTypeObject BPyGPUVertFormat_Type
static PyC_StringEnumItems pygpu_vertcomptype_items[]
unsigned int uint
Definition BKE_text.h:55
static uint attr_size(GPUVertCompType type, int len)
PyDoc_STRVAR(bpy_bm_new_doc, ".. function:: new(*, use_operators=True)\n" "\n" " :param use_operators: Support calling operators in :mod:`bmesh.ops` (uses some " "extra memory per vert/edge/face).\n" " :type use_operators: bool\n" " :return: Return a new, empty BMesh.\n" " :rtype: :class:`bmesh.types.BMesh`\n")
@ GPU_FETCH_INT_TO_FLOAT_UNIT
PyObject * BPyGPUVertFormat_CreatePyObject(GPUVertFormat *fmt)
static PyC_StringEnumItems pygpu_vertfetchmode_items[]
uint GPU_vertformat_attr_add_legacy(GPUVertFormat *, StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
static PyObject * pygpu_vertformat__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
int PyC_ParseStringEnum(PyObject *o, void *p)
static constexpr int GPU_VERT_ATTR_MAX_LEN
static PyMethodDef pygpu_vertformat__tp_methods[]
static PyObject * pygpu_vertformat_attr_add(BPyGPUVertFormat *self, PyObject *args, PyObject *kwds)
static void pygpu_vertformat__tp_dealloc(BPyGPUVertFormat *self)
header-only compatibility defines.
PyObject_HEAD GPUVertFormat fmt