2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-09-27 00:53:45 -03:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bpygpu
|
2018-09-27 00:53:45 -03:00
|
|
|
*
|
2021-07-20 22:52:31 +10:00
|
|
|
* - Use `bpygpu_` for local API.
|
|
|
|
|
* - Use `BPyGPU` for public API.
|
2018-09-27 00:53:45 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <Python.h>
|
|
|
|
|
|
2024-03-23 01:24:18 +01:00
|
|
|
#include "GPU_index_buffer.hh"
|
2018-09-27 00:53:45 -03:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2024-09-24 15:25:36 +02:00
|
|
|
#include "../generic/py_capi_utils.hh"
|
2025-06-20 04:19:35 +00:00
|
|
|
#include "../generic/python_compat.hh" /* IWYU pragma: keep. */
|
2018-09-27 00:53:45 -03:00
|
|
|
|
2024-03-23 10:06:45 -04:00
|
|
|
#include "gpu_py.hh"
|
|
|
|
|
#include "gpu_py_element.hh" /* own include */
|
2018-09-27 00:53:45 -03:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name IndexBuf Type
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
static PyObject *pygpu_IndexBuf__tp_new(PyTypeObject * /*type*/, PyObject *args, PyObject *kwds)
|
2018-09-27 00:53:45 -03:00
|
|
|
{
|
2024-06-19 17:54:35 +02:00
|
|
|
BPYGPU_IS_INIT_OR_ERROR_OBJ;
|
|
|
|
|
|
2018-10-10 13:35:29 -03:00
|
|
|
const char *error_prefix = "IndexBuf.__new__";
|
2018-09-27 00:53:45 -03:00
|
|
|
bool ok = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-22 11:33:36 +10:00
|
|
|
PyC_StringEnum prim_type = {bpygpu_primtype_items, GPU_PRIM_NONE};
|
2021-02-22 08:26:45 -03:00
|
|
|
PyObject *seq;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
uint verts_per_prim;
|
|
|
|
|
uint index_len;
|
|
|
|
|
GPUIndexBufBuilder builder;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
static const char *_keywords[] = {"type", "seq", nullptr};
|
2022-04-08 09:41:28 +10:00
|
|
|
static _PyArg_Parser _parser = {
|
2023-08-30 14:05:32 +10:00
|
|
|
PY_ARG_PARSER_HEAD_COMPAT()
|
2022-04-08 09:41:28 +10:00
|
|
|
"$O" /* `type` */
|
|
|
|
|
"&O" /* `seq` */
|
|
|
|
|
":IndexBuf.__new__",
|
|
|
|
|
_keywords,
|
2023-08-03 19:14:53 +10:00
|
|
|
nullptr,
|
2022-04-08 09:41:28 +10:00
|
|
|
};
|
2019-01-03 01:08:26 +11:00
|
|
|
if (!_PyArg_ParseTupleAndKeywordsFast(
|
2024-01-02 18:12:54 +01:00
|
|
|
args, kwds, &_parser, PyC_ParseStringEnum, &prim_type, &seq))
|
|
|
|
|
{
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
verts_per_prim = GPU_indexbuf_primitive_len(GPUPrimType(prim_type.value_found));
|
2018-09-27 00:53:45 -03:00
|
|
|
if (verts_per_prim == -1) {
|
2025-08-20 16:10:00 +10:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
|
"The argument 'type' must be "
|
|
|
|
|
"'POINTS', 'LINES', 'TRIS', 'LINES_ADJ' or 'TRIS_ADJ'");
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-22 08:26:45 -03:00
|
|
|
if (PyObject_CheckBuffer(seq)) {
|
2018-09-27 00:53:45 -03:00
|
|
|
Py_buffer pybuffer;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-02-22 08:26:45 -03:00
|
|
|
if (PyObject_GetBuffer(seq, &pybuffer, PyBUF_FORMAT | PyBUF_ND) == -1) {
|
2018-09-27 00:53:45 -03:00
|
|
|
/* PyObject_GetBuffer already handles error messages. */
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
if (pybuffer.ndim != 1 && pybuffer.shape[1] != verts_per_prim) {
|
|
|
|
|
PyErr_Format(PyExc_ValueError, "Each primitive must exactly %d indices", verts_per_prim);
|
2022-04-12 22:16:09 -03:00
|
|
|
PyBuffer_Release(&pybuffer);
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-06 01:15:15 -03:00
|
|
|
if (pybuffer.itemsize != 4 ||
|
2018-10-08 08:37:32 +11:00
|
|
|
PyC_StructFmt_type_is_float_any(PyC_StructFmt_type_from_str(pybuffer.format)))
|
|
|
|
|
{
|
2025-08-20 16:10:00 +10:00
|
|
|
PyErr_SetString(PyExc_ValueError, "Each index must be an 4-bytes integer value");
|
2022-04-12 22:16:09 -03:00
|
|
|
PyBuffer_Release(&pybuffer);
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
index_len = pybuffer.shape[0];
|
|
|
|
|
if (pybuffer.ndim != 1) {
|
|
|
|
|
index_len *= pybuffer.shape[1];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 21:15:42 +10:00
|
|
|
/* The `vertex_len` parameter is only used for asserts in the Debug build. */
|
|
|
|
|
/* Not very useful in python since scripts are often tested in Release build. */
|
2018-09-27 00:53:45 -03:00
|
|
|
/* Use `INT_MAX` instead of the actual number of vertices. */
|
2023-07-21 19:41:03 +02:00
|
|
|
GPU_indexbuf_init(&builder, GPUPrimType(prim_type.value_found), index_len, INT_MAX);
|
2018-09-27 00:53:45 -03:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
uint *buf = static_cast<uint *>(pybuffer.buf);
|
2018-09-27 00:53:45 -03:00
|
|
|
for (uint i = index_len; i--; buf++) {
|
|
|
|
|
GPU_indexbuf_add_generic_vert(&builder, *buf);
|
|
|
|
|
}
|
2022-05-23 22:37:30 -03:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
PyBuffer_Release(&pybuffer);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-02-22 08:26:45 -03:00
|
|
|
PyObject *seq_fast = PySequence_Fast(seq, error_prefix);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-21 19:41:03 +02:00
|
|
|
if (seq_fast == nullptr) {
|
|
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
index_len = seq_len * verts_per_prim;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 21:15:42 +10:00
|
|
|
/* The `vertex_len` parameter is only used for asserts in the Debug build. */
|
|
|
|
|
/* Not very useful in python since scripts are often tested in Release build. */
|
2018-09-27 00:53:45 -03:00
|
|
|
/* Use `INT_MAX` instead of the actual number of vertices. */
|
2023-07-21 19:41:03 +02:00
|
|
|
GPU_indexbuf_init(&builder, GPUPrimType(prim_type.value_found), index_len, INT_MAX);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
if (verts_per_prim == 1) {
|
|
|
|
|
for (uint i = 0; i < seq_len; i++) {
|
|
|
|
|
GPU_indexbuf_add_generic_vert(&builder, PyC_Long_AsU32(seq_items[i]));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
|
|
|
|
else {
|
2018-10-10 13:35:29 -03:00
|
|
|
int values[4];
|
2018-09-27 00:53:45 -03:00
|
|
|
for (uint i = 0; i < seq_len; i++) {
|
2018-10-10 13:35:29 -03:00
|
|
|
PyObject *seq_fast_item = PySequence_Fast(seq_items[i], error_prefix);
|
2023-07-21 19:41:03 +02:00
|
|
|
if (seq_fast_item == nullptr) {
|
2018-10-10 13:35:29 -03:00
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
|
"%s: expected a sequence, got %s",
|
|
|
|
|
error_prefix,
|
|
|
|
|
Py_TYPE(seq_items[i])->tp_name);
|
2018-09-27 00:53:45 -03:00
|
|
|
ok = false;
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-07-27 22:26:33 +10:00
|
|
|
ok = PyC_AsArray_FAST(values,
|
|
|
|
|
sizeof(*values),
|
|
|
|
|
seq_fast_item,
|
|
|
|
|
verts_per_prim,
|
|
|
|
|
&PyLong_Type,
|
|
|
|
|
error_prefix) == 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-10-10 13:35:29 -03:00
|
|
|
if (ok) {
|
|
|
|
|
for (uint j = 0; j < verts_per_prim; j++) {
|
|
|
|
|
GPU_indexbuf_add_generic_vert(&builder, values[j]);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-10-10 13:35:29 -03:00
|
|
|
}
|
|
|
|
|
Py_DECREF(seq_fast_item);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
ok = false;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
finally:
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
Py_DECREF(seq_fast);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
if (ok == false) {
|
|
|
|
|
MEM_freeN(builder.data);
|
2023-07-21 19:41:03 +02:00
|
|
|
return nullptr;
|
2018-09-27 00:53:45 -03:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-09-27 00:53:45 -03:00
|
|
|
return BPyGPUIndexBuf_CreatePyObject(GPU_indexbuf_build(&builder));
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-17 10:16:41 -03:00
|
|
|
static void pygpu_IndexBuf__tp_dealloc(BPyGPUIndexBuf *self)
|
2018-09-27 00:53:45 -03:00
|
|
|
{
|
|
|
|
|
GPU_indexbuf_discard(self->elem);
|
|
|
|
|
Py_TYPE(self)->tp_free(self);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 10:22:16 +11:00
|
|
|
PyDoc_STRVAR(
|
|
|
|
|
/* Wrap. */
|
|
|
|
|
pygpu_IndexBuf__tp_doc,
|
|
|
|
|
".. class:: GPUIndexBuf(type, seq)\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" Contains an index buffer.\n"
|
|
|
|
|
"\n"
|
|
|
|
|
" :arg type: The primitive type this index buffer is composed of.\n"
|
2024-11-04 11:55:41 +11:00
|
|
|
" Possible values are [``POINTS``, ``LINES``, ``TRIS``, ``LINES_ADJ``, ``TRIS_ADJ``].\n"
|
2024-01-25 10:22:16 +11:00
|
|
|
" :type type: str\n"
|
|
|
|
|
" :arg seq: Indices this index buffer will contain.\n"
|
|
|
|
|
" Whether a 1D or 2D sequence is required depends on the type.\n"
|
|
|
|
|
" Optionally the sequence can support the buffer protocol.\n"
|
2024-11-03 15:42:19 +11:00
|
|
|
" :type seq: Buffer | Sequence[int] | Sequence[Sequence[int]]\n");
|
2018-09-27 00:53:45 -03:00
|
|
|
PyTypeObject BPyGPUIndexBuf_Type = {
|
2023-07-21 19:41:03 +02:00
|
|
|
/*ob_base*/ PyVarObject_HEAD_INIT(nullptr, 0)
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_name*/ "GPUIndexBuf",
|
|
|
|
|
/*tp_basicsize*/ sizeof(BPyGPUIndexBuf),
|
|
|
|
|
/*tp_itemsize*/ 0,
|
|
|
|
|
/*tp_dealloc*/ (destructor)pygpu_IndexBuf__tp_dealloc,
|
|
|
|
|
/*tp_vectorcall_offset*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_getattr*/ nullptr,
|
|
|
|
|
/*tp_setattr*/ nullptr,
|
|
|
|
|
/*tp_as_async*/ nullptr,
|
|
|
|
|
/*tp_repr*/ nullptr,
|
|
|
|
|
/*tp_as_number*/ nullptr,
|
|
|
|
|
/*tp_as_sequence*/ nullptr,
|
|
|
|
|
/*tp_as_mapping*/ nullptr,
|
|
|
|
|
/*tp_hash*/ nullptr,
|
|
|
|
|
/*tp_call*/ nullptr,
|
|
|
|
|
/*tp_str*/ nullptr,
|
|
|
|
|
/*tp_getattro*/ nullptr,
|
|
|
|
|
/*tp_setattro*/ nullptr,
|
|
|
|
|
/*tp_as_buffer*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_flags*/ Py_TPFLAGS_DEFAULT,
|
|
|
|
|
/*tp_doc*/ pygpu_IndexBuf__tp_doc,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_traverse*/ nullptr,
|
|
|
|
|
/*tp_clear*/ nullptr,
|
|
|
|
|
/*tp_richcompare*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_weaklistoffset*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_iter*/ nullptr,
|
|
|
|
|
/*tp_iternext*/ nullptr,
|
|
|
|
|
/*tp_methods*/ nullptr,
|
|
|
|
|
/*tp_members*/ nullptr,
|
|
|
|
|
/*tp_getset*/ nullptr,
|
|
|
|
|
/*tp_base*/ nullptr,
|
|
|
|
|
/*tp_dict*/ nullptr,
|
|
|
|
|
/*tp_descr_get*/ nullptr,
|
|
|
|
|
/*tp_descr_set*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_dictoffset*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_init*/ nullptr,
|
|
|
|
|
/*tp_alloc*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_new*/ pygpu_IndexBuf__tp_new,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_free*/ nullptr,
|
|
|
|
|
/*tp_is_gc*/ nullptr,
|
|
|
|
|
/*tp_bases*/ nullptr,
|
|
|
|
|
/*tp_mro*/ nullptr,
|
|
|
|
|
/*tp_cache*/ nullptr,
|
|
|
|
|
/*tp_subclasses*/ nullptr,
|
|
|
|
|
/*tp_weaklist*/ nullptr,
|
|
|
|
|
/*tp_del*/ nullptr,
|
2023-07-16 17:43:31 +10:00
|
|
|
/*tp_version_tag*/ 0,
|
2023-07-21 19:41:03 +02:00
|
|
|
/*tp_finalize*/ nullptr,
|
|
|
|
|
/*tp_vectorcall*/ nullptr,
|
2018-09-27 00:53:45 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Public API
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2024-03-24 16:38:30 +01:00
|
|
|
PyObject *BPyGPUIndexBuf_CreatePyObject(blender::gpu::IndexBuf *elem)
|
2018-09-27 00:53:45 -03:00
|
|
|
{
|
|
|
|
|
BPyGPUIndexBuf *self;
|
|
|
|
|
|
|
|
|
|
self = PyObject_New(BPyGPUIndexBuf, &BPyGPUIndexBuf_Type);
|
|
|
|
|
self->elem = elem;
|
|
|
|
|
|
|
|
|
|
return (PyObject *)self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|