[Bf-blender-cvs] [8cfca8e1bd8] master: PyGPU: only use 3D shaders and rename string enums

Germano Cavalcante noreply at git.blender.org
Fri Sep 2 14:39:28 CEST 2022


Commit: 8cfca8e1bd85a300732d92b30741d5c243797a31
Author: Germano Cavalcante
Date:   Fri Sep 2 09:33:29 2022 -0300
Branches: master
https://developer.blender.org/rB8cfca8e1bd85a300732d92b30741d5c243797a31

PyGPU: only use 3D shaders and rename string enums

Since rB6269d66da, creating formats no longer depends solely on the
shader, but now depends on the dimensions used to fill the VBOs.

This allows 3D shaders to work flawlessly when assigned dimensions are
2D.

So there's no real benefit to us having shaders that are limited to 2D
use anymore.

This limitation makes it difficult to implement other builtin shaders
as they indirectly require a 2D version.

So this commit removes the 2D versions of the builtin sahders used in
Python , renames the string enums but keeps the old enums working for
backward compatibility.

(This brings parts of the changes reviewed in D15836).

===================================================================

M	release/scripts/modules/bpy_types.py
M	release/scripts/modules/gpu_extras/presets.py
M	release/scripts/templates_py/operator_modal_draw.py
M	source/blender/python/gpu/gpu_py_shader.c

===================================================================

diff --git a/release/scripts/modules/bpy_types.py b/release/scripts/modules/bpy_types.py
index 87b32eb1f40..b2f4d71ed92 100644
--- a/release/scripts/modules/bpy_types.py
+++ b/release/scripts/modules/bpy_types.py
@@ -793,7 +793,7 @@ class Gizmo(StructRNA):
         vbo = GPUVertBuf(len=len(verts), format=fmt)
         vbo.attr_fill(id=pos_id, data=verts)
         batch = GPUBatch(type=type, buf=vbo)
-        shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR' if dims == 3 else '2D_UNIFORM_COLOR')
+        shader = gpu.shader.from_builtin('UNIFORM_COLOR')
         batch.program_set(shader)
         return (batch, shader)
 
diff --git a/release/scripts/modules/gpu_extras/presets.py b/release/scripts/modules/gpu_extras/presets.py
index eba8d6c161d..f68824d76c8 100644
--- a/release/scripts/modules/gpu_extras/presets.py
+++ b/release/scripts/modules/gpu_extras/presets.py
@@ -42,7 +42,7 @@ def draw_circle_2d(position, color, radius, *, segments=None):
         vbo = GPUVertBuf(len=len(verts), format=fmt)
         vbo.attr_fill(id=pos_id, data=verts)
         batch = GPUBatch(type='LINE_STRIP', buf=vbo)
-        shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
+        shader = gpu.shader.from_builtin('UNIFORM_COLOR')
         batch.program_set(shader)
         shader.uniform_float("color", color)
         batch.draw()
@@ -67,7 +67,7 @@ def draw_texture_2d(texture, position, width, height):
 
     coords = ((0, 0), (1, 0), (1, 1), (0, 1))
 
-    shader = gpu.shader.from_builtin('2D_IMAGE')
+    shader = gpu.shader.from_builtin('IMAGE')
     batch = batch_for_shader(
         shader, 'TRI_FAN',
         {"pos": coords, "texCoord": coords},
@@ -77,7 +77,7 @@ def draw_texture_2d(texture, position, width, height):
         gpu.matrix.translate(position)
         gpu.matrix.scale((width, height))
 
-        shader = gpu.shader.from_builtin('2D_IMAGE')
+        shader = gpu.shader.from_builtin('IMAGE')
         shader.bind()
 
         if isinstance(texture, int):
diff --git a/release/scripts/templates_py/operator_modal_draw.py b/release/scripts/templates_py/operator_modal_draw.py
index 20e888ad5f3..78edf874791 100644
--- a/release/scripts/templates_py/operator_modal_draw.py
+++ b/release/scripts/templates_py/operator_modal_draw.py
@@ -15,7 +15,7 @@ def draw_callback_px(self, context):
     blf.draw(font_id, "Hello Word " + str(len(self.mouse_path)))
 
     # 50% alpha, 2 pixel width line
-    shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
+    shader = gpu.shader.from_builtin('UNIFORM_COLOR')
     gpu.state.blend_set('ALPHA')
     gpu.state.line_width_set(2.0)
     batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": self.mouse_path})
diff --git a/source/blender/python/gpu/gpu_py_shader.c b/source/blender/python/gpu/gpu_py_shader.c
index ce250df1589..02fccedd820 100644
--- a/source/blender/python/gpu/gpu_py_shader.c
+++ b/source/blender/python/gpu/gpu_py_shader.c
@@ -31,52 +31,36 @@
  * \{ */
 
 #define PYDOC_BUILTIN_SHADER_DESCRIPTION \
-  "``2D_FLAT_COLOR``\n" \
-  "   :Attributes: vec2 pos, vec4 color\n" \
-  "   :Uniforms: none\n" \
-  "``2D_IMAGE``\n" \
-  "   :Attributes: vec2 pos, vec2 texCoord\n" \
-  "   :Uniforms: sampler2D image\n" \
-  "``2D_SMOOTH_COLOR``\n" \
-  "   :Attributes: vec2 pos, vec4 color\n" \
-  "   :Uniforms: none\n" \
-  "``2D_UNIFORM_COLOR``\n" \
-  "   :Attributes: vec2 pos\n" \
-  "   :Uniforms: vec4 color\n" \
-  "``3D_FLAT_COLOR``\n" \
+  "``FLAT_COLOR``\n" \
   "   :Attributes: vec3 pos, vec4 color\n" \
   "   :Uniforms: none\n" \
-  "``3D_IMAGE``\n" \
+  "``IMAGE``\n" \
   "   :Attributes: vec3 pos, vec2 texCoord\n" \
   "   :Uniforms: sampler2D image\n" \
-  "``3D_SMOOTH_COLOR``\n" \
+  "``SMOOTH_COLOR``\n" \
   "   :Attributes: vec3 pos, vec4 color\n" \
   "   :Uniforms: none\n" \
-  "``3D_UNIFORM_COLOR``\n" \
+  "``UNIFORM_COLOR``\n" \
   "   :Attributes: vec3 pos\n" \
   "   :Uniforms: vec4 color\n" \
-  "``3D_POLYLINE_FLAT_COLOR``\n" \
+  "``POLYLINE_FLAT_COLOR``\n" \
   "   :Attributes: vec3 pos, vec4 color\n" \
   "   :Uniforms: vec2 viewportSize, float lineWidth\n" \
-  "``3D_POLYLINE_SMOOTH_COLOR``\n" \
+  "``POLYLINE_SMOOTH_COLOR``\n" \
   "   :Attributes: vec3 pos, vec4 color\n" \
   "   :Uniforms: vec2 viewportSize, float lineWidth\n" \
-  "``3D_POLYLINE_UNIFORM_COLOR``\n" \
+  "``POLYLINE_UNIFORM_COLOR``\n" \
   "   :Attributes: vec3 pos\n" \
   "   :Uniforms: vec2 viewportSize, float lineWidth\n"
 
 static const struct PyC_StringEnumItems pygpu_shader_builtin_items[] = {
-    {GPU_SHADER_2D_FLAT_COLOR, "2D_FLAT_COLOR"},
-    {GPU_SHADER_2D_IMAGE, "2D_IMAGE"},
-    {GPU_SHADER_2D_SMOOTH_COLOR, "2D_SMOOTH_COLOR"},
-    {GPU_SHADER_2D_UNIFORM_COLOR, "2D_UNIFORM_COLOR"},
-    {GPU_SHADER_3D_FLAT_COLOR, "3D_FLAT_COLOR"},
-    {GPU_SHADER_3D_IMAGE, "3D_IMAGE"},
-    {GPU_SHADER_3D_SMOOTH_COLOR, "3D_SMOOTH_COLOR"},
-    {GPU_SHADER_3D_UNIFORM_COLOR, "3D_UNIFORM_COLOR"},
-    {GPU_SHADER_3D_POLYLINE_FLAT_COLOR, "3D_POLYLINE_FLAT_COLOR"},
-    {GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR, "3D_POLYLINE_SMOOTH_COLOR"},
-    {GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR, "3D_POLYLINE_UNIFORM_COLOR"},
+    {GPU_SHADER_3D_FLAT_COLOR, "FLAT_COLOR"},
+    {GPU_SHADER_3D_IMAGE, "IMAGE"},
+    {GPU_SHADER_3D_SMOOTH_COLOR, "SMOOTH_COLOR"},
+    {GPU_SHADER_3D_UNIFORM_COLOR, "UNIFORM_COLOR"},
+    {GPU_SHADER_3D_POLYLINE_FLAT_COLOR, "POLYLINE_FLAT_COLOR"},
+    {GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR, "POLYLINE_SMOOTH_COLOR"},
+    {GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR, "POLYLINE_UNIFORM_COLOR"},
     {0, NULL},
 };
 
@@ -784,6 +768,24 @@ PyTypeObject BPyGPUShader_Type = {
 /** \name gpu.shader Module API
  * \{ */
 
+static int pyc_parse_buitinshader_w_backward_compatibility(PyObject *o, void *p)
+{
+  struct PyC_StringEnum *e = p;
+  const char *value = PyUnicode_AsUTF8(o);
+  if (value && ELEM(value[0], u'2', u'3')) {
+    /* Deprecated enums that start with "3D_" or "2D_". */
+    value += 3;
+    for (int i = 0; e->items[i].id; i++) {
+      if (STREQ(e->items[i].id, value)) {
+        e->value_found = e->items[i].value;
+        return 1;
+      }
+    }
+  }
+
+  return PyC_ParseStringEnum(o, p);
+}
+
 PyDoc_STRVAR(pygpu_shader_unbind_doc,
              ".. function:: unbind()\n"
              "\n"
@@ -834,7 +836,7 @@ static PyObject *pygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *arg
   if (!_PyArg_ParseTupleAndKeywordsFast(args,
                                         kwds,
                                         &_parser,
-                                        PyC_ParseStringEnum,
+                                        pyc_parse_buitinshader_w_backward_compatibility,
                                         &pygpu_bultinshader,
                                         PyC_ParseStringEnum,
                                         &pygpu_config)) {



More information about the Bf-blender-cvs mailing list