Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/gpu/gpu_py_shader.c
| Show All 32 Lines | |||||
| #include "GPU_shader_interface.h" | #include "GPU_shader_interface.h" | ||||
| #include "../generic/py_capi_utils.h" | #include "../generic/py_capi_utils.h" | ||||
| #include "../generic/python_utildefines.h" | #include "../generic/python_utildefines.h" | ||||
| #include "../mathutils/mathutils.h" | #include "../mathutils/mathutils.h" | ||||
| #include "gpu_py_shader.h" /* own include */ | #include "gpu_py_shader.h" /* own include */ | ||||
| #include "gpu_py_vertex_format.h" | #include "gpu_py_vertex_format.h" | ||||
| #include "gpu_py_vertex_buffer.h" | |||||
| #include "gpu_py_batch.h" | |||||
| #include "gpu_py_element.h" | |||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name Enum Conversion. | /** \name Enum Conversion. | ||||
| * \{ */ | * \{ */ | ||||
| static int bpygpu_ParseBultinShaderEnum(PyObject *o, void *p) | static int bpygpu_ParseBultinShaderEnum(PyObject *o, void *p) | ||||
| ▲ Show 20 Lines • Show All 532 Lines • ▼ Show 20 Lines | |||||
| ); | ); | ||||
| static PyObject *bpygpu_shader_calc_format(BPyGPUShader *self, PyObject *UNUSED(arg)) | static PyObject *bpygpu_shader_calc_format(BPyGPUShader *self, PyObject *UNUSED(arg)) | ||||
| { | { | ||||
| BPyGPUVertFormat *ret = (BPyGPUVertFormat *)BPyGPUVertFormat_CreatePyObject(NULL); | BPyGPUVertFormat *ret = (BPyGPUVertFormat *)BPyGPUVertFormat_CreatePyObject(NULL); | ||||
| GPU_vertformat_from_interface(&ret->fmt, GPU_shader_get_interface(self->shader)); | GPU_vertformat_from_interface(&ret->fmt, GPU_shader_get_interface(self->shader)); | ||||
| return (PyObject *)ret; | return (PyObject *)ret; | ||||
| } | } | ||||
| PyDoc_STRVAR(bpygpu_shader_new_batch_doc, | |||||
| ".. method:: new_batch(type, content, indices=None)\n" | |||||
| ); | |||||
| static PyObject *bpygpu_shader_new_batch(BPyGPUShader *self, PyObject *args, PyObject *kwds) | |||||
| { | |||||
| struct { | |||||
| PyObject *type; | |||||
| PyObject *content; | |||||
| PyObject *indices; | |||||
| } params = {0}; | |||||
| static const char *_keywords[] = { | |||||
| "type", "content", "indices", NULL}; | |||||
| static _PyArg_Parser _parser = {"O!O!|$O:GPUShader.new_batch", _keywords, 0}; | |||||
| if (!_PyArg_ParseTupleAndKeywordsFast( | |||||
| args, kwds, &_parser, | |||||
| &PyUnicode_Type, ¶ms.type, | |||||
| &PyDict_Type, ¶ms.content, | |||||
| ¶ms.indices)) | |||||
| { | |||||
| return NULL; | |||||
| } | |||||
| int attr_amount = PyDict_Size(params.content); | |||||
| if (attr_amount <= 0) { | |||||
| PyErr_SetString(PyExc_Exception, "expected at least one attribute"); | |||||
| return NULL; | |||||
| } | |||||
| PyListObject *content = (PyListObject *)PyDict_Items(params.content); | |||||
| int buffer_length = -1; | |||||
| for (int i = 0; i < attr_amount; i++) { | |||||
| int length = PyObject_Size(PyTuple_GetItem(content->ob_item[i], 1)); | |||||
| if (buffer_length == -1) buffer_length = length; | |||||
| else if (buffer_length != length) { | |||||
| PyErr_SetString(PyExc_Exception, "mismatching attribute sequence lengths"); | |||||
| Py_DECREF(content); | |||||
| return NULL; | |||||
| } | |||||
| } | |||||
| PyObject *format = bpygpu_shader_calc_format(self, NULL); | |||||
| PyObject *vbo_args = Py_BuildValue("OI", format, buffer_length); | |||||
| PyObject *vbo = bpygpu_VertBuf_new(&BPyGPUVertBuf_Type, vbo_args, NULL); | |||||
| Py_DECREF(vbo_args); | |||||
| Py_DECREF(format); | |||||
| for (int i = 0; i < attr_amount; i++) { | |||||
| PyObject *fill_args = content->ob_item[i]; | |||||
| if (bpygpu_VertBuf_attr_fill((BPyGPUVertBuf *)vbo, fill_args, NULL) == NULL) { | |||||
| Py_DECREF(content); | |||||
| Py_DECREF(vbo); | |||||
| return NULL; | |||||
| } | |||||
| } | |||||
| Py_DECREF(content); | |||||
| PyObject *batch = NULL; | |||||
| PyObject *batch_args = PyTuple_New(0); | |||||
| PyObject *batch_kwds = PyDict_New(); | |||||
| PyDict_SetItemString(batch_kwds, "type", params.type); | |||||
| PyDict_SetItemString(batch_kwds, "buf", vbo); | |||||
| if (params.indices == NULL) { | |||||
| batch = bpygpu_Batch_new(&BPyGPUBatch_Type, batch_args, batch_kwds); | |||||
| } | |||||
| else { | |||||
| PyObject *ibo_args = PyTuple_New(0); | |||||
| PyObject *ibo_kwds = PyDict_New(); | |||||
| PyObject *str_points = PyUnicode_FromString("POINTS"); | |||||
| PyDict_SetItemString(ibo_kwds, "type", str_points); | |||||
| PyDict_SetItemString(ibo_kwds, "seq", params.indices); | |||||
| Py_DECREF(str_points); | |||||
| PyObject *ibo = bpygpu_IndexBuf_new(&BPyGPUIndexBuf_Type, ibo_args, ibo_kwds); | |||||
| Py_DECREF(ibo_args); | |||||
| Py_DECREF(ibo_kwds); | |||||
| if (ibo != NULL) { | |||||
| PyDict_SetItemString(batch_kwds, "elem", ibo); | |||||
| batch = bpygpu_Batch_new(&BPyGPUBatch_Type, batch_args, batch_kwds); | |||||
| } | |||||
| } | |||||
| Py_DECREF(batch_kwds); | |||||
| Py_DECREF(batch_args); | |||||
| Py_DECREF(vbo); | |||||
| return batch; | |||||
| } | |||||
| static struct PyMethodDef bpygpu_shader_methods[] = { | static struct PyMethodDef bpygpu_shader_methods[] = { | ||||
| {"bind", (PyCFunction)bpygpu_shader_bind, | {"bind", (PyCFunction)bpygpu_shader_bind, | ||||
| METH_NOARGS, bpygpu_shader_bind_doc}, | METH_NOARGS, bpygpu_shader_bind_doc}, | ||||
| {"transform_feedback_enable", | {"transform_feedback_enable", | ||||
| (PyCFunction)bpygpu_shader_transform_feedback_enable, | (PyCFunction)bpygpu_shader_transform_feedback_enable, | ||||
| METH_O, bpygpu_shader_transform_feedback_enable_doc}, | METH_O, bpygpu_shader_transform_feedback_enable_doc}, | ||||
| {"transform_feedback_disable", | {"transform_feedback_disable", | ||||
| (PyCFunction)bpygpu_transform_feedback_disable, | (PyCFunction)bpygpu_transform_feedback_disable, | ||||
| Show All 20 Lines | static struct PyMethodDef bpygpu_shader_methods[] = { | ||||
| (PyCFunction)bpygpu_shader_uniform_int, | (PyCFunction)bpygpu_shader_uniform_int, | ||||
| METH_VARARGS, bpygpu_shader_uniform_int_doc}, | METH_VARARGS, bpygpu_shader_uniform_int_doc}, | ||||
| {"attr_from_name", | {"attr_from_name", | ||||
| (PyCFunction)bpygpu_shader_attr_from_name, | (PyCFunction)bpygpu_shader_attr_from_name, | ||||
| METH_O, bpygpu_shader_attr_from_name_doc}, | METH_O, bpygpu_shader_attr_from_name_doc}, | ||||
| {"format_calc", | {"format_calc", | ||||
| (PyCFunction)bpygpu_shader_calc_format, | (PyCFunction)bpygpu_shader_calc_format, | ||||
| METH_NOARGS, bpygpu_shader_calc_format_doc}, | METH_NOARGS, bpygpu_shader_calc_format_doc}, | ||||
| {"new_batch", | |||||
| (PyCFunction)bpygpu_shader_new_batch, | |||||
| METH_VARARGS | METH_KEYWORDS, bpygpu_shader_new_batch_doc}, | |||||
| {NULL, NULL, 0, NULL} | {NULL, NULL, 0, NULL} | ||||
| }; | }; | ||||
| PyDoc_STRVAR(bpygpu_shader_program_doc, | PyDoc_STRVAR(bpygpu_shader_program_doc, | ||||
| "The name of the program object for use by the OpenGL API (read-only).\n\n:type: int" | "The name of the program object for use by the OpenGL API (read-only).\n\n:type: int" | ||||
| ); | ); | ||||
| static PyObject *bpygpu_shader_program_get(BPyGPUShader *self, void *UNUSED(closure)) | static PyObject *bpygpu_shader_program_get(BPyGPUShader *self, void *UNUSED(closure)) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 218 Lines • Show Last 20 Lines | |||||