From 2ce8b01c597c232d165ef2c004e3cd8d9db22cbf Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Mon, 19 Sep 2022 09:39:48 -0300 Subject: PyGPU: call 'GPU_shader_bind' in 'GPUShader.uniform_' methods This simplifies python code. When we call a method like shader.uniform_float("color", (1,1,1,1)), we expect the shader's uniform to be updated regardless of whether the shader is bound or not. And `batch.draw()` already calls `GPU_shader_bind` inside. Differential Revision: https://developer.blender.org/D15929 --- doc/python_api/examples/gpu.1.py | 1 - doc/python_api/examples/gpu.10.py | 1 - doc/python_api/examples/gpu.2.py | 1 - doc/python_api/examples/gpu.3.py | 1 - doc/python_api/examples/gpu.5.py | 1 - doc/python_api/examples/gpu.6.py | 1 - doc/python_api/examples/gpu.7.py | 1 - release/scripts/modules/bpy_types.py | 1 - release/scripts/modules/gpu_extras/presets.py | 1 - release/scripts/templates_py/operator_modal_draw.py | 1 - source/blender/python/gpu/gpu_py_shader.c | 7 +++++++ 11 files changed, 7 insertions(+), 10 deletions(-) diff --git a/doc/python_api/examples/gpu.1.py b/doc/python_api/examples/gpu.1.py index a014e69c2d2..c44e79a77aa 100644 --- a/doc/python_api/examples/gpu.1.py +++ b/doc/python_api/examples/gpu.1.py @@ -134,7 +134,6 @@ batch = batch_for_shader(shader, 'LINES', {"pos": coords}) def draw(): - shader.bind() shader.uniform_float("color", (1, 1, 0, 1)) batch.draw(shader) diff --git a/doc/python_api/examples/gpu.10.py b/doc/python_api/examples/gpu.10.py index b47ff732e2b..6c438bd396e 100644 --- a/doc/python_api/examples/gpu.10.py +++ b/doc/python_api/examples/gpu.10.py @@ -58,7 +58,6 @@ batch = batch_for_shader( def draw(): - shader.bind() matrix = bpy.context.region_data.perspective_matrix shader.uniform_float("u_ViewProjectionMatrix", matrix) shader.uniform_float("u_Scale", 10) diff --git a/doc/python_api/examples/gpu.2.py b/doc/python_api/examples/gpu.2.py index 2a46e833752..e308ce7f78e 100644 --- a/doc/python_api/examples/gpu.2.py +++ b/doc/python_api/examples/gpu.2.py @@ -41,7 +41,6 @@ batch = batch_for_shader(shader, 'TRIS', {"position": coords}) def draw(): - shader.bind() matrix = bpy.context.region_data.perspective_matrix shader.uniform_float("viewProjectionMatrix", matrix) shader.uniform_float("brightness", 0.5) diff --git a/doc/python_api/examples/gpu.3.py b/doc/python_api/examples/gpu.3.py index 0c86b52bcf5..9e8f762d9c9 100644 --- a/doc/python_api/examples/gpu.3.py +++ b/doc/python_api/examples/gpu.3.py @@ -22,7 +22,6 @@ batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices) def draw(): - shader.bind() shader.uniform_float("color", (1, 0, 0, 1)) batch.draw(shader) diff --git a/doc/python_api/examples/gpu.5.py b/doc/python_api/examples/gpu.5.py index 2edde46a364..983372706c1 100644 --- a/doc/python_api/examples/gpu.5.py +++ b/doc/python_api/examples/gpu.5.py @@ -18,7 +18,6 @@ batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices) def draw(): - shader.bind() shader.uniform_float("color", (0, 0.5, 0.5, 1.0)) batch.draw(shader) diff --git a/doc/python_api/examples/gpu.6.py b/doc/python_api/examples/gpu.6.py index 5576b2d0bfe..96decf571ee 100644 --- a/doc/python_api/examples/gpu.6.py +++ b/doc/python_api/examples/gpu.6.py @@ -56,7 +56,6 @@ batch = batch_for_shader( def draw(): - shader.bind() shader.uniform_sampler("image", texture) batch.draw(shader) diff --git a/doc/python_api/examples/gpu.7.py b/doc/python_api/examples/gpu.7.py index e3bfbd14e34..5d25b42728d 100644 --- a/doc/python_api/examples/gpu.7.py +++ b/doc/python_api/examples/gpu.7.py @@ -76,7 +76,6 @@ batch = batch_for_shader( def draw(): - shader.bind() shader.uniform_float("modelMatrix", Matrix.Translation((1, 2, 3)) @ Matrix.Scale(3, 4)) shader.uniform_float("viewProjectionMatrix", bpy.context.region_data.perspective_matrix) shader.uniform_sampler("image", offscreen.texture_color) diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py index b2f4d71ed92..d8d6a9123f2 100644 --- a/release/scripts/modules/bpy_types.py +++ b/release/scripts/modules/bpy_types.py @@ -742,7 +742,6 @@ class Gizmo(StructRNA): matrix = self.matrix_world batch, shader = shape - shader.bind() if select_id is not None: gpu.select.load_id(select_id) diff --git a/release/scripts/modules/gpu_extras/presets.py b/release/scripts/modules/gpu_extras/presets.py index f68824d76c8..f0d80e855c1 100644 --- a/release/scripts/modules/gpu_extras/presets.py +++ b/release/scripts/modules/gpu_extras/presets.py @@ -78,7 +78,6 @@ def draw_texture_2d(texture, position, width, height): gpu.matrix.scale((width, height)) shader = gpu.shader.from_builtin('IMAGE') - shader.bind() if isinstance(texture, int): # Call the legacy bgl to not break the existing API diff --git a/release/scripts/templates_py/operator_modal_draw.py b/release/scripts/templates_py/operator_modal_draw.py index 78edf874791..77371a75605 100644 --- a/release/scripts/templates_py/operator_modal_draw.py +++ b/release/scripts/templates_py/operator_modal_draw.py @@ -19,7 +19,6 @@ def draw_callback_px(self, context): gpu.state.blend_set('ALPHA') gpu.state.line_width_set(2.0) batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": self.mouse_path}) - shader.bind() shader.uniform_float("color", (0.0, 0.0, 0.0, 0.5)) batch.draw(shader) diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c index 9bac23fd7e7..43b50dbbef0 100644 --- a/source/blender/python/gpu/gpu_py_shader.c +++ b/source/blender/python/gpu/gpu_py_shader.c @@ -270,6 +270,7 @@ static PyObject *pygpu_shader_uniform_vector_float(BPyGPUShader *self, PyObject return NULL; } + GPU_shader_bind(self->shader); GPU_shader_uniform_vector(self->shader, location, length, count, pybuffer.buf); PyBuffer_Release(&pybuffer); @@ -292,6 +293,7 @@ static PyObject *pygpu_shader_uniform_vector_int(BPyGPUShader *self, PyObject *a return NULL; } + GPU_shader_bind(self->shader); GPU_shader_uniform_vector_int(self->shader, location, length, count, pybuffer.buf); PyBuffer_Release(&pybuffer); @@ -359,6 +361,7 @@ static PyObject *pygpu_shader_uniform_bool(BPyGPUShader *self, PyObject *args) return NULL; } + GPU_shader_bind(self->shader); GPU_shader_uniform_vector_int(self->shader, location, length, 1, values); Py_RETURN_NONE; @@ -428,6 +431,7 @@ static PyObject *pygpu_shader_uniform_float(BPyGPUShader *self, PyObject *args) return NULL; } + GPU_shader_bind(self->shader); GPU_shader_uniform_vector(self->shader, location, length, 1, values); Py_RETURN_NONE; @@ -499,6 +503,7 @@ static PyObject *pygpu_shader_uniform_int(BPyGPUShader *self, PyObject *args) return NULL; } + GPU_shader_bind(self->shader); GPU_shader_uniform_vector_int(self->shader, location, length, 1, values); Py_RETURN_NONE; @@ -522,6 +527,7 @@ static PyObject *pygpu_shader_uniform_sampler(BPyGPUShader *self, PyObject *args return NULL; } + GPU_shader_bind(self->shader); 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); @@ -556,6 +562,7 @@ static PyObject *pygpu_shader_uniform_block(BPyGPUShader *self, PyObject *args) return NULL; } + GPU_shader_bind(self->shader); GPU_uniformbuf_bind(py_ubo->ubo, binding); Py_RETURN_NONE; -- cgit v1.2.3