Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/gpu/gpu_py_shader.c
| Show All 20 Lines | |||||
| * - Use ``BPyGPU`` for public API. | * - Use ``BPyGPU`` for public API. | ||||
| */ | */ | ||||
| #include <Python.h> | #include <Python.h> | ||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "GPU_shader.h" | #include "GPU_shader.h" | ||||
| #include "GPU_texture.h" | |||||
| #include "GPU_uniform_buffer.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_api.h" | #include "gpu_py_api.h" | ||||
| #include "gpu_py_shader.h" /* own include */ | #include "gpu_py_texture.h" | ||||
| #include "gpu_py_uniformbuffer.h" | |||||
| #include "gpu_py_vertex_format.h" | #include "gpu_py_vertex_format.h" | ||||
| #include "gpu_py_shader.h" /* own include */ | |||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name Enum Conversion. | /** \name Enum Conversion. | ||||
| * \{ */ | * \{ */ | ||||
| static const struct PyC_StringEnumItems pygpu_bultinshader_items[] = { | static const struct PyC_StringEnumItems pygpu_bultinshader_items[] = { | ||||
| {GPU_SHADER_2D_UNIFORM_COLOR, "2D_UNIFORM_COLOR"}, | {GPU_SHADER_2D_UNIFORM_COLOR, "2D_UNIFORM_COLOR"}, | ||||
| {GPU_SHADER_2D_FLAT_COLOR, "2D_FLAT_COLOR"}, | {GPU_SHADER_2D_FLAT_COLOR, "2D_FLAT_COLOR"}, | ||||
| {GPU_SHADER_2D_SMOOTH_COLOR, "2D_SMOOTH_COLOR"}, | {GPU_SHADER_2D_SMOOTH_COLOR, "2D_SMOOTH_COLOR"}, | ||||
| ▲ Show 20 Lines • Show All 407 Lines • ▼ Show 20 Lines | if (location == -1) { | ||||
| return NULL; | return NULL; | ||||
| } | } | ||||
| GPU_shader_uniform_vector_int(self->shader, location, length, 1, values); | GPU_shader_uniform_vector_int(self->shader, location, length, 1, values); | ||||
| Py_RETURN_NONE; | Py_RETURN_NONE; | ||||
| } | } | ||||
| PyDoc_STRVAR(py_shader_uniform_texture_doc, | |||||
| ".. method:: uniform_texture(name, texture)\n" | |||||
| "\n" | |||||
| " Specify the value of a texture uniform variable for the current GPUShader.\n" | |||||
fclem: texture > sampler | |||||
| "\n" | |||||
| " :param name: name of the uniform variable whose texture is to be specified.\n" | |||||
| " :type name: str\n" | |||||
| " :param texture: Texture to attach.\n" | |||||
| " :type texture: :class:`gpu.types.GPUTexture`\n"); | |||||
| static PyObject *py_shader_uniform_texture(BPyGPUShader *self, PyObject *args) | |||||
| { | |||||
| const char *name; | |||||
| BPyGPUTexture *py_texture; | |||||
| if (!PyArg_ParseTuple( | |||||
| args, "sO!:GPUShader.uniform_texture", &name, &BPyGPUTexture_Type, &py_texture)) { | |||||
| return NULL; | |||||
| } | |||||
| int slot = GPU_shader_get_texture_binding(self->shader, name); | |||||
| GPU_texture_bind(py_texture->tex, slot); | |||||
| GPU_shader_uniform_1i(self->shader, name, slot); | |||||
| Py_RETURN_NONE; | |||||
| } | |||||
| PyDoc_STRVAR( | |||||
| py_shader_uniform_buffer_doc, | |||||
| ".. method:: uniform_buffer(name, ubo)\n" | |||||
| "\n" | |||||
| " Specify the value of an uniform buffer object variable for the current GPUShader.\n" | |||||
| "\n" | |||||
| " :param name: name of the uniform variable whose UBO is to be specified.\n" | |||||
| " :type name: str\n" | |||||
| " :param ubo: Uniform Buffer to attach.\n" | |||||
| " :type texture: :class:`gpu.types.GPUUniformBuf`\n"); | |||||
| static PyObject *py_shader_uniform_buffer(BPyGPUShader *self, PyObject *args) | |||||
| { | |||||
| const char *name; | |||||
| BPyGPUUniformBuf *py_ubo; | |||||
| if (!PyArg_ParseTuple( | |||||
| args, "sO!:GPUShader.uniform_buffer", &name, &BPyGPUUniformBuf_Type, &py_ubo)) { | |||||
| return NULL; | |||||
| } | |||||
| int slot = GPU_shader_get_uniform_block(self->shader, name); | |||||
Done Inline ActionsYou should check for slot correctness here. It can be -1 and lead to problems if used on GPU_uniformbuf_bind. I can fix GLUniformBuf::bind to avoid the crash but you should definitely check for that error. fclem: You should check for slot correctness here. It can be -1 and lead to problems if used on… | |||||
| GPU_uniformbuf_bind(py_ubo->ubo, slot); | |||||
| GPU_shader_uniform_1i(self->shader, name, slot); | |||||
| Py_RETURN_NONE; | |||||
| } | |||||
| PyDoc_STRVAR( | PyDoc_STRVAR( | ||||
| py_shader_attr_from_name_doc, | py_shader_attr_from_name_doc, | ||||
| ".. method:: attr_from_name(name)\n" | ".. method:: attr_from_name(name)\n" | ||||
| "\n" | "\n" | ||||
| " Get attribute location by name.\n" | " Get attribute location by name.\n" | ||||
| "\n" | "\n" | ||||
| " :param name: The name of the attribute variable whose location is to be queried.\n" | " :param name: The name of the attribute variable whose location is to be queried.\n" | ||||
| " :type name: str\n" | " :type name: str\n" | ||||
| ▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | static struct PyMethodDef py_shader_methods[] = { | ||||
| (PyCFunction)py_shader_uniform_bool, | (PyCFunction)py_shader_uniform_bool, | ||||
| METH_VARARGS, | METH_VARARGS, | ||||
| py_shader_uniform_bool_doc}, | py_shader_uniform_bool_doc}, | ||||
| {"uniform_float", | {"uniform_float", | ||||
| (PyCFunction)py_shader_uniform_float, | (PyCFunction)py_shader_uniform_float, | ||||
| METH_VARARGS, | METH_VARARGS, | ||||
| py_shader_uniform_float_doc}, | py_shader_uniform_float_doc}, | ||||
| {"uniform_int", (PyCFunction)py_shader_uniform_int, METH_VARARGS, py_shader_uniform_int_doc}, | {"uniform_int", (PyCFunction)py_shader_uniform_int, METH_VARARGS, py_shader_uniform_int_doc}, | ||||
| {"uniform_texture", | |||||
| (PyCFunction)py_shader_uniform_texture, | |||||
| METH_VARARGS, | |||||
| py_shader_uniform_texture_doc}, | |||||
| {"uniform_buffer", | |||||
| (PyCFunction)py_shader_uniform_buffer, | |||||
| METH_VARARGS, | |||||
| py_shader_uniform_buffer_doc}, | |||||
| {"attr_from_name", | {"attr_from_name", | ||||
| (PyCFunction)py_shader_attr_from_name, | (PyCFunction)py_shader_attr_from_name, | ||||
| METH_O, | METH_O, | ||||
| py_shader_attr_from_name_doc}, | py_shader_attr_from_name_doc}, | ||||
| {"format_calc", (PyCFunction)py_shader_calc_format, METH_NOARGS, py_shader_calc_format_doc}, | {"format_calc", (PyCFunction)py_shader_calc_format, METH_NOARGS, py_shader_calc_format_doc}, | ||||
| {NULL, NULL, 0, NULL}, | {NULL, NULL, 0, NULL}, | ||||
| }; | }; | ||||
| ▲ Show 20 Lines • Show All 194 Lines • ▼ Show 20 Lines | PyDoc_STRVAR(py_shader_module_doc, | ||||
| " :Attributes: vec3 pos\n" | " :Attributes: vec3 pos\n" | ||||
| " :Uniforms: vec4 color\n" | " :Uniforms: vec4 color\n" | ||||
| "3D_FLAT_COLOR\n" | "3D_FLAT_COLOR\n" | ||||
| " :Attributes: vec3 pos, vec4 color\n" | " :Attributes: vec3 pos, vec4 color\n" | ||||
| " :Uniforms: none\n" | " :Uniforms: none\n" | ||||
| "3D_SMOOTH_COLOR\n" | "3D_SMOOTH_COLOR\n" | ||||
| " :Attributes: vec3 pos, vec4 color\n" | " :Attributes: vec3 pos, vec4 color\n" | ||||
| " :Uniforms: none\n"); | " :Uniforms: none\n"); | ||||
| static PyModuleDef BPyGPU_shader_module_def = { | static PyModuleDef py_shader_module_def = { | ||||
| PyModuleDef_HEAD_INIT, | PyModuleDef_HEAD_INIT, | ||||
| .m_name = "gpu.shader", | .m_name = "gpu.shader", | ||||
| .m_doc = py_shader_module_doc, | .m_doc = py_shader_module_doc, | ||||
| .m_methods = py_shader_module_methods, | .m_methods = py_shader_module_methods, | ||||
| }; | }; | ||||
| /** \} */ | /** \} */ | ||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name Public API | /** \name Public API | ||||
| * \{ */ | * \{ */ | ||||
| PyObject *BPyGPUShader_CreatePyObject(GPUShader *shader, bool is_builtin) | PyObject *BPyGPUShader_CreatePyObject(GPUShader *shader, bool is_builtin) | ||||
| { | { | ||||
| BPyGPUShader *self; | BPyGPUShader *self; | ||||
| self = PyObject_New(BPyGPUShader, &BPyGPUShader_Type); | self = PyObject_New(BPyGPUShader, &BPyGPUShader_Type); | ||||
| self->shader = shader; | self->shader = shader; | ||||
| self->is_builtin = is_builtin; | self->is_builtin = is_builtin; | ||||
| return (PyObject *)self; | return (PyObject *)self; | ||||
| } | } | ||||
| PyObject *BPyInit_gpu_shader(void) | PyObject *bpygpu_shader_init(void) | ||||
| { | { | ||||
| PyObject *submodule; | PyObject *submodule; | ||||
| submodule = PyModule_Create(&BPyGPU_shader_module_def); | submodule = PyModule_Create(&py_shader_module_def); | ||||
| return submodule; | return submodule; | ||||
| } | } | ||||
| /** \} */ | /** \} */ | ||||
texture > sampler