[Bf-blender-cvs] [9bd65528e32] blender2.8: GPU Python: optionally init the vertexformat in the vertexbuffer itself.
mano-wii
noreply at git.blender.org
Fri Oct 5 22:22:19 CEST 2018
Commit: 9bd65528e3206012a6c842bea289cecb17b309b8
Author: mano-wii
Date: Fri Oct 5 17:21:10 2018 -0300
Branches: blender2.8
https://developer.blender.org/rB9bd65528e3206012a6c842bea289cecb17b309b8
GPU Python: optionally init the vertexformat in the vertexbuffer itself.
===================================================================
M source/blender/python/gpu/gpu_py_vertex_buffer.c
M source/blender/python/gpu/gpu_py_vertex_format.c
M source/blender/python/gpu/gpu_py_vertex_format.h
===================================================================
diff --git a/source/blender/python/gpu/gpu_py_vertex_buffer.c b/source/blender/python/gpu/gpu_py_vertex_buffer.c
index fd4ee64c996..0282154d681 100644
--- a/source/blender/python/gpu/gpu_py_vertex_buffer.c
+++ b/source/blender/python/gpu/gpu_py_vertex_buffer.c
@@ -98,7 +98,7 @@ static bool bpygpu_vertbuf_fill_impl(
GPUVertBuf *vbo,
uint data_id, PyObject *seq)
{
- const char *exc_str_size_mismatch = "Expected a %s of size %d, got %d";
+ const char *exc_str_size_mismatch = "Expected a %s of size %d, got %u";
bool ok = true;
const GPUVertAttr *attr = &vbo->format.attribs[data_id];
@@ -111,7 +111,7 @@ static bool bpygpu_vertbuf_fill_impl(
return false;
}
- int comp_len = pybuffer.ndim == 1 ? 1 : pybuffer.shape[1];
+ uint comp_len = pybuffer.ndim == 1 ? 1 : (uint)pybuffer.shape[1];
if (pybuffer.shape[0] != vbo->vertex_len) {
PyErr_Format(PyExc_ValueError, exc_str_size_mismatch,
@@ -221,22 +221,43 @@ static int bpygpu_fill_attribute(GPUVertBuf *buf, int id, PyObject *py_seq_data)
static PyObject *bpygpu_VertBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
{
+ const char *error_prefix = "GPUVertBuf.__new__";
+
struct {
- BPyGPUVertFormat *py_fmt;
+ PyObject *py_fmt;
uint len;
} params;
static const char *_keywords[] = {"format", "len", NULL};
- static _PyArg_Parser _parser = {"O!I:GPUVertBuf.__new__", _keywords, 0};
+ static _PyArg_Parser _parser = {"OI:GPUVertBuf.__new__", _keywords, 0};
if (!_PyArg_ParseTupleAndKeywordsFast(
args, kwds, &_parser,
- &BPyGPUVertFormat_Type, ¶ms.py_fmt,
+ ¶ms.py_fmt,
¶ms.len))
{
return NULL;
}
- struct GPUVertBuf *vbo = GPU_vertbuf_create_with_format(¶ms.py_fmt->fmt);
+ GPUVertFormat *fmt, fmt_stack;
+
+ if (BPyGPUVertFormat_Check(params.py_fmt)) {
+ fmt = &((BPyGPUVertFormat *)params.py_fmt)->fmt;
+ }
+ else if (PyList_Check(params.py_fmt)) {
+ fmt = &fmt_stack;
+ GPU_vertformat_clear(fmt);
+ if (!bpygpu_vertformat_from_PyList(
+ (PyListObject *)params.py_fmt, error_prefix, fmt))
+ {
+ return NULL;
+ }
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError, "format not understood");
+ return NULL;
+ }
+
+ struct GPUVertBuf *vbo = GPU_vertbuf_create_with_format(fmt);
GPU_vertbuf_data_alloc(vbo, params.len);
diff --git a/source/blender/python/gpu/gpu_py_vertex_format.c b/source/blender/python/gpu/gpu_py_vertex_format.c
index 0568bc2a9ae..f05e7bd29d0 100644
--- a/source/blender/python/gpu/gpu_py_vertex_format.c
+++ b/source/blender/python/gpu/gpu_py_vertex_format.c
@@ -193,25 +193,9 @@ static int add_attribute_from_tuple(GPUVertFormat *format, PyObject *data)
return add_attribute_simple(format, name, comp_type, length);
}
-static int insert_attributes_from_list(GPUVertFormat *format, PyObject *list)
-{
- Py_ssize_t amount = PyList_Size(list);
-
- for (Py_ssize_t i = 0; i < amount; i++) {
- PyObject *element = PyList_GET_ITEM(list, i);
- if (!PyTuple_Check(element)) {
- PyErr_SetString(PyExc_TypeError, "expected a list of tuples");
- return 0;
- }
- if (!add_attribute_from_tuple(format, element)) return 0;
- }
-
- return 1;
-}
-
static PyObject *bpygpu_VertFormat_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds)
{
- PyObject *format_list;
+ PyListObject *format_list;
static const char *_keywords[] = {"format", NULL};
static _PyArg_Parser _parser = {"O!:VertFormat.__new__", _keywords, 0};
@@ -224,8 +208,8 @@ static PyObject *bpygpu_VertFormat_new(PyTypeObject *UNUSED(type), PyObject *arg
BPyGPUVertFormat *ret = (BPyGPUVertFormat *)BPyGPUVertFormat_CreatePyObject(NULL);
- if (!insert_attributes_from_list(&ret->fmt, format_list)) {
- Py_DecRef((PyObject *)ret);
+ if (!bpygpu_vertformat_from_PyList(format_list, "VertFormat.__new__", &ret->fmt)) {
+ Py_DECREF(ret);
return NULL;
}
@@ -310,4 +294,27 @@ PyObject *BPyGPUVertFormat_CreatePyObject(GPUVertFormat *fmt)
return (PyObject *)self;
}
+bool bpygpu_vertformat_from_PyList(
+ const PyListObject *list, const char *error_prefix, GPUVertFormat *r_fmt)
+{
+ BLI_assert(PyList_Check(list));
+
+ Py_ssize_t amount = Py_SIZE(list);
+
+ for (Py_ssize_t i = 0; i < amount; i++) {
+ PyObject *element = PyList_GET_ITEM(list, i);
+ if (!PyTuple_Check(element)) {
+ PyErr_Format(PyExc_TypeError,
+ "%.200s expected a list of tuples", error_prefix);
+
+ return false;
+ }
+ if (!add_attribute_from_tuple(r_fmt, element)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
/** \} */
diff --git a/source/blender/python/gpu/gpu_py_vertex_format.h b/source/blender/python/gpu/gpu_py_vertex_format.h
index 5b0bf1addc2..f1f1961b0ad 100644
--- a/source/blender/python/gpu/gpu_py_vertex_format.h
+++ b/source/blender/python/gpu/gpu_py_vertex_format.h
@@ -37,5 +37,7 @@ typedef struct BPyGPUVertFormat {
} BPyGPUVertFormat;
PyObject *BPyGPUVertFormat_CreatePyObject(struct GPUVertFormat *fmt);
+bool bpygpu_vertformat_from_PyList(
+ const PyListObject *list, const char *error_prefix, GPUVertFormat *r_fmt);
#endif /* __GPU_PY_VERTEX_FORMAT_H__ */
More information about the Bf-blender-cvs
mailing list