29#define PYGPU_AS_NATIVE_SWITCH(attr) \
30 switch (attr->type.comp_type()) { \
32 PY_AS_NATIVE(int8_t, PyC_Long_AsI8); \
36 PY_AS_NATIVE(uint8_t, PyC_Long_AsU8); \
39 case GPU_COMP_I16: { \
40 PY_AS_NATIVE(int16_t, PyC_Long_AsI16); \
43 case GPU_COMP_U16: { \
44 PY_AS_NATIVE(uint16_t, PyC_Long_AsU16); \
47 case GPU_COMP_I32: { \
48 PY_AS_NATIVE(int32_t, PyC_Long_AsI32); \
51 case GPU_COMP_U32: { \
52 PY_AS_NATIVE(uint32_t, PyC_Long_AsU32); \
55 case GPU_COMP_F32: { \
56 PY_AS_NATIVE(float, PyFloat_AsDouble); \
60 BLI_assert_unreachable(); \
68#define PY_AS_NATIVE(ty_dst, py_as_native) \
70 ty_dst *data_dst = static_cast<ty_dst *>(data_dst_void); \
71 *data_dst = py_as_native(py_src); \
82 PyObject *py_seq_fast,
86 PyObject **value_fast_items = PySequence_Fast_ITEMS(py_seq_fast);
91#define PY_AS_NATIVE(ty_dst, py_as_native) \
92 ty_dst *data_dst = static_cast<ty_dst *>(data_dst_void); \
93 for (uint i = 0; i < len; i++) { \
94 data_dst[i] = py_as_native(value_fast_items[i]); \
103#undef PYGPU_AS_NATIVE_SWITCH
104#undef WARN_TYPE_LIMIT_PUSH
105#undef WARN_TYPE_LIMIT_POP
110 const char *error_prefix)
112 const char *exc_str_size_mismatch =
"Expected a %s of size %d, got %u";
118 if (PyObject_CheckBuffer(
seq)) {
121 if (PyObject_GetBuffer(
seq, &pybuffer, PyBUF_STRIDES | PyBUF_ND) == -1) {
126 const uint comp_len = pybuffer.ndim == 1 ? 1 :
uint(pybuffer.shape[1]);
128 if (pybuffer.shape[0] != vert_len) {
130 PyExc_ValueError, exc_str_size_mismatch,
"sequence", vert_len, pybuffer.shape[0]);
135 PyExc_ValueError, exc_str_size_mismatch,
"component", attr->
type.
comp_len(), comp_len);
142 PyBuffer_Release(&pybuffer);
148 PyObject *seq_fast = PySequence_Fast(
seq,
"Vertex buffer fill");
149 if (seq_fast ==
nullptr) {
153 const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast);
155 if (seq_len != vert_len) {
156 PyErr_Format(PyExc_ValueError, exc_str_size_mismatch,
"sequence", vert_len, seq_len);
159 PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
162 for (
uint i = 0; i < seq_len; i++) {
164 PyObject *item = seq_items[i];
169 for (
uint i = 0; i < seq_len; i++) {
171 PyObject *seq_fast_item = PySequence_Fast(seq_items[i], error_prefix);
173 if (seq_fast_item ==
nullptr) {
177 if (PySequence_Fast_GET_SIZE(seq_fast_item) != attr->
type.
comp_len()) {
178 PyErr_Format(PyExc_ValueError,
179 exc_str_size_mismatch,
182 PySequence_Fast_GET_SIZE(seq_fast_item));
184 Py_DECREF(seq_fast_item);
190 Py_DECREF(seq_fast_item);
194 if (PyErr_Occurred()) {
207 PyObject *py_seq_data,
208 const char *error_prefix)
211 PyErr_Format(PyExc_ValueError,
"Format id %d out of range",
id);
215 if (buf->
data<
char>().data() ==
nullptr) {
216 PyErr_SetString(PyExc_ValueError,
"Can't fill, static buffer already in use");
242 static const char *_keywords[] = {
"format",
"len",
nullptr};
243 static _PyArg_Parser _parser = {
246 ":GPUVertBuf.__new__",
250 if (!_PyArg_ParseTupleAndKeywordsFast(
257 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(fmt);
266 pygpu_vertbuf_attr_fill_doc,
267 ".. method:: attr_fill(id, data)\n"
269 " Insert data into the buffer for a single attribute.\n"
271 " :param id: Either the name or the id of the attribute.\n"
272 " :type id: int | str\n"
273 " :param data: Buffer or sequence of data that should be stored in the buffer\n"
274 " :type data: collections.abc.Buffer | "
275 "Sequence[float] | Sequence[int] | Sequence[Sequence[float]] | Sequence[Sequence[int]]\n");
279 PyObject *identifier;
281 static const char *_keywords[] = {
"id",
"data",
nullptr};
282 static _PyArg_Parser _parser = {
289 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwds, &_parser, &identifier, &
data)) {
295 if (PyLong_Check(identifier)) {
296 id = PyLong_AsLong(identifier);
298 else if (PyUnicode_Check(identifier)) {
300 const char *name = PyUnicode_AsUTF8(identifier);
303 PyErr_Format(PyExc_ValueError,
"Unknown attribute '%s'", name);
308 PyErr_SetString(PyExc_TypeError,
"expected int or str type as identifier");
321# pragma clang diagnostic push
322# pragma clang diagnostic ignored "-Wcast-function-type"
324# pragma GCC diagnostic push
325# pragma GCC diagnostic ignored "-Wcast-function-type"
332 METH_VARARGS | METH_KEYWORDS,
333 pygpu_vertbuf_attr_fill_doc},
334 {
nullptr,
nullptr, 0,
nullptr},
339# pragma clang diagnostic pop
341# pragma GCC diagnostic pop
348 Py_TYPE(self)->tp_free(self);
353 pygpu_vertbuf__tp_doc,
354 ".. class:: GPUVertBuf\n"
358 " .. method:: __init__(format, len)\n"
360 " :param format: Vertex format.\n"
361 " :type format: :class:`gpu.types.GPUVertFormat`\n"
362 " :param len: Amount of vertices that will fit into this buffer.\n"
363 " :type len: int\n");
365 PyVarObject_HEAD_INIT(
nullptr, 0)
385 pygpu_vertbuf__tp_doc,
429 return reinterpret_cast<PyObject *
>(self);
#define BPYGPU_IS_INIT_OR_ERROR_OBJ
#define PYGPU_AS_NATIVE_SWITCH(attr)
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
PyTypeObject BPyGPUVertFormat_Type
static PyObject * pygpu_vertbuf_attr_fill(BPyGPUVertBuf *self, PyObject *args, PyObject *kwds)
static void pygpu_fill_format_sequence(void *data_dst_void, PyObject *py_seq_fast, const GPUVertAttr *attr)
PyTypeObject BPyGPUVertBuf_Type
int GPU_vertformat_attr_id_get(const GPUVertFormat *, StringRef name)
PyObject * BPyGPUVertBuf_CreatePyObject(gpu::VertBuf *buf)
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")
static PyMethodDef pygpu_vertbuf__tp_methods[]
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
static void pygpu_fill_format_elem(void *data_dst_void, PyObject *py_src, const GPUVertAttr *attr)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void GPU_vertbuf_attr_fill_stride(gpu::VertBuf *, uint a_idx, uint stride, const void *data)
static PyObject * pygpu_vertbuf__tp_new(PyTypeObject *, PyObject *args, PyObject *kwds)
BMesh const char void * data
static bool pygpu_vertbuf_fill_impl(gpu::VertBuf *vbo, uint data_id, PyObject *seq, const char *error_prefix)
void GPU_vertbuf_attr_get_raw_data(gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
static int pygpu_vertbuf_fill(gpu::VertBuf *buf, int id, PyObject *py_seq_data, const char *error_prefix)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
uint GPU_vertbuf_get_vertex_len(const gpu::VertBuf *verts)
void GPU_vertbuf_discard(gpu::VertBuf *)
static void pygpu_vertbuf__tp_dealloc(BPyGPUVertBuf *self)
header-only compatibility defines.
PyObject_HEAD gpu::VertBuf * buf
struct blender::GPUVertAttr::Type type