[Bf-extensions-cvs] [f01d08b7] master: Snap Utilities Line: Replace the uses of the bgl module with the gpu equivalent

Germano Cavalcante noreply at git.blender.org
Wed Feb 17 16:57:57 CET 2021


Commit: f01d08b7c5f7873e0ce8a77b84788e2f6b1ad716
Author: Germano Cavalcante
Date:   Wed Feb 17 12:45:52 2021 -0300
Branches: master
https://developer.blender.org/rBAf01d08b7c5f7873e0ce8a77b84788e2f6b1ad716

Snap Utilities Line: Replace the uses of the bgl module with the gpu equivalent

Also a little style update due to automatic updates.

Ref T80730, T85675

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

M	mesh_snap_utilities_line/__init__.py
M	mesh_snap_utilities_line/drawing_utilities.py
M	mesh_snap_utilities_line/op_line.py
M	mesh_snap_utilities_line/snap_context_l/__init__.py
M	mesh_snap_utilities_line/snap_context_l/mesh_drawing.py

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

diff --git a/mesh_snap_utilities_line/__init__.py b/mesh_snap_utilities_line/__init__.py
index 5e4d2f96..ee6503aa 100644
--- a/mesh_snap_utilities_line/__init__.py
+++ b/mesh_snap_utilities_line/__init__.py
@@ -21,8 +21,8 @@
 bl_info = {
     "name": "Snap_Utilities_Line",
     "author": "Germano Cavalcante",
-    "version": (5, 9, 19),
-    "blender": (2, 80, 0),
+    "version": (6, 9, 20),
+    "blender": (2, 93, 0),
     "location": "View3D > TOOLS > Line Tool",
     "description": "Extends Blender Snap controls",
     "doc_url" : "https://blenderartists.org/t/cad-snap-utilities",
diff --git a/mesh_snap_utilities_line/drawing_utilities.py b/mesh_snap_utilities_line/drawing_utilities.py
index 0488f0f3..cf67db4b 100644
--- a/mesh_snap_utilities_line/drawing_utilities.py
+++ b/mesh_snap_utilities_line/drawing_utilities.py
@@ -14,13 +14,12 @@
 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 # ##### END GPL LICENSE BLOCK #####
-
-import bgl
+import gpu
 from mathutils import Vector
 
 
 class SnapDrawn():
-    __slots__ = (
+    __slots__ = (\
         'out_color',
         'face_color',
         'edge_color',
@@ -33,11 +32,9 @@ class SnapDrawn():
         'axis_z_color',
         '_format_pos',
         '_format_pos_and_color',
-        '_is_point_size_enabled',
         '_program_unif_col',
         '_program_smooth_col',
-        '_batch_point',
-    )
+        '_batch_point',)
 
     def __init__(self, out_color, face_color,
                  edge_color, vert_color, center_color,
@@ -72,17 +69,11 @@ class SnapDrawn():
 
 
     def _gl_state_push(self):
-        self._is_point_size_enabled = bgl.glIsEnabled(bgl.GL_PROGRAM_POINT_SIZE)
-        if self._is_point_size_enabled:
-            bgl.glDisable(bgl.GL_PROGRAM_POINT_SIZE)
-
-        # draw 3d point OpenGL in the 3D View
-        bgl.glEnable(bgl.GL_BLEND)
+        gpu.state.program_point_size_set(False)
+        gpu.state.blend_set('ALPHA')
 
     def _gl_state_restore(self):
-        bgl.glDisable(bgl.GL_BLEND)
-        if self._is_point_size_enabled:
-            bgl.glEnable(bgl.GL_PROGRAM_POINT_SIZE)
+        gpu.state.blend_set('NONE')
 
     def batch_line_strip_create(self, coords):
         from gpu.types import (
@@ -138,22 +129,29 @@ class SnapDrawn():
 
         if list_verts_co:
             # draw 3d line OpenGL in the 3D View
-            bgl.glDepthRange(0, 0.9999)
-            bgl.glLineWidth(3.0)
+            winmat = gpu.matrix.get_projection_matrix()
+            winmat[3][2] -= 0.0001
+            gpu.matrix.push_projection()
+            gpu.matrix.load_projection_matrix(winmat)
+            gpu.state.line_width_set(3.0)
 
             batch = self.batch_line_strip_create([v.to_tuple() for v in list_verts_co] + [location.to_tuple()])
 
+            self._program_unif_col.bind()
             self._program_unif_col.uniform_float("color", (1.0, 0.8, 0.0, 0.5))
             batch.draw(self._program_unif_col)
+            gpu.matrix.pop_projection()
             del batch
 
-        bgl.glDisable(bgl.GL_DEPTH_TEST)
+        gpu.state.depth_test_set('NONE')
 
         point_batch = self.batch_point_get()
         if vector_constrain:
             if prevloc:
-                bgl.glPointSize(5.0)
+                gpu.state.point_size_set(5.0)
                 gpu.matrix.translate(prevloc)
+
+                self._program_unif_col.bind()
                 self._program_unif_col.uniform_float("color", (1.0, 1.0, 1.0, 0.5))
                 point_batch.draw(self._program_unif_col)
                 gpu.matrix.translate(-prevloc)
@@ -182,17 +180,17 @@ class SnapDrawn():
             else: # type == None
                 Color4f = self.out_color
 
-        bgl.glPointSize(10.0)
+        gpu.state.point_size_set(10.0)
 
         gpu.matrix.translate(location)
+        self._program_unif_col.bind()
         self._program_unif_col.uniform_float("color", Color4f)
         point_batch.draw(self._program_unif_col)
 
         # restore opengl defaults
-        bgl.glDepthRange(0.0, 1.0)
-        bgl.glPointSize(1.0)
-        bgl.glLineWidth(1.0)
-        bgl.glEnable(bgl.GL_DEPTH_TEST)
+        gpu.state.point_size_set(1.0)
+        gpu.state.line_width_set(1.0)
+        gpu.state.depth_test_set('LESS_EQUAL')
 
         gpu.matrix.pop()
         self._gl_state_restore()
@@ -208,7 +206,7 @@ class SnapDrawn():
 
         with gpu.matrix.push_pop():
             self._gl_state_push()
-            bgl.glDisable(bgl.GL_DEPTH_TEST)
+            gpu.state.depth_test_set('NONE')
 
             gpu.matrix.multiply_matrix(snap_obj.mat)
 
@@ -225,24 +223,26 @@ class SnapDrawn():
                     edges.shape = -1
 
                     self._program_smooth_col.bind()
-                    bgl.glLineWidth(3.0)
+                    gpu.state.line_width_set(3.0)
                     batch = self.batch_lines_smooth_color_create(edges["pos"], edges["color"])
                     batch.draw(self._program_smooth_col)
-                    bgl.glLineWidth(1.0)
+                    gpu.state.line_width_set(1.0)
             else:
                 self._program_unif_col.bind()
 
                 if isinstance(elem, BMEdge):
+                    self._program_unif_col.bind()
                     self._program_unif_col.uniform_float("color", self.edge_color)
 
-                    bgl.glLineWidth(3.0)
+                    gpu.state.line_width_set(3.0)
                     batch = self.batch_line_strip_create([v.co for v in elem.verts])
                     batch.draw(self._program_unif_col)
-                    bgl.glLineWidth(1.0)
+                    gpu.state.line_width_set(1.0)
 
                 elif isinstance(elem, BMFace):
                     if len(snap_obj.data) == 2:
                         face_color = self.face_color[0], self.face_color[1], self.face_color[2], self.face_color[3] * 0.2
+                        self._program_unif_col.bind()
                         self._program_unif_col.uniform_float("color", face_color)
 
                         tris = snap_obj.data[1].get_loop_tri_co_by_bmface(bm, elem)
@@ -251,6 +251,6 @@ class SnapDrawn():
                         batch.draw(self._program_unif_col)
 
             # restore opengl defaults
-            bgl.glEnable(bgl.GL_DEPTH_TEST)
+            gpu.state.depth_test_set('LESS_EQUAL')
 
         self._gl_state_restore()
diff --git a/mesh_snap_utilities_line/op_line.py b/mesh_snap_utilities_line/op_line.py
index 685ebca4..f63eb219 100644
--- a/mesh_snap_utilities_line/op_line.py
+++ b/mesh_snap_utilities_line/op_line.py
@@ -408,7 +408,6 @@ class SnapUtilitiesLine(SnapUtilities, bpy.types.Operator):
             #Store values from 3d view context
             self.rv3d = context.region_data
             self.rotMat = self.rv3d.view_matrix.copy()
-            # self.obj_glmatrix = bgl.Buffer(bgl.GL_FLOAT, [4, 4],
             # self.obj_matrix.transposed())
 
             #modals
diff --git a/mesh_snap_utilities_line/snap_context_l/__init__.py b/mesh_snap_utilities_line/snap_context_l/__init__.py
index 3c09e537..74c85499 100644
--- a/mesh_snap_utilities_line/snap_context_l/__init__.py
+++ b/mesh_snap_utilities_line/snap_context_l/__init__.py
@@ -14,25 +14,15 @@
 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 # ##### END GPL LICENSE BLOCK #####
+__all__ = ("SnapContext",)
 
-__all__ = (
-    "SnapContext",
-    )
-
-import bgl
+import gpu
 from mathutils import Vector
 
 VERT = 1
 EDGE = 2
 FACE = 4
 
-
-def check_gl_error():
-    error = bgl.glGetError()
-    if error != bgl.GL_NO_ERROR:
-        raise Exception(error)
-
-
 class _Internal:
     global_snap_context = None
 
@@ -46,7 +36,6 @@ class _Internal:
         gpu_Indices_enable_state,
         gpu_Indices_restore_state,
         gpu_Indices_use_clip_planes,
-        gpu_Indices_set_ProjectionMatrix,
         gpu_Indices_mesh_cache_clear,
         )
 
@@ -70,119 +59,41 @@ class _SnapObjectData():
 class _SnapOffscreen():
     bound = None
     def __init__(self, width, height):
-        self.freed = False
-        self.is_bound = False
+        self._fbo = None
 
         self.width = width
         self.height = height
 
-        self.fbo = bgl.Buffer(bgl.GL_INT, 1)
-        self.buf_color = bgl.Buffer(bgl.GL_INT, 1)
-        self.buf_depth = bgl.Buffer(bgl.GL_INT, 1)
-
-        self.cur_fbo = bgl.Buffer(bgl.GL_INT, 1)
-        self.cur_viewport = bgl.Buffer(bgl.GL_INT, 4)
-
-        bgl.glGenRenderbuffers(1, self.buf_depth)
-        bgl.glGenTextures(1, self.buf_color)
-
-        self._config_textures()
-
-        bgl.glGetIntegerv(bgl.GL_FRAMEBUFFER_BINDING, self.cur_fbo)
-
-        bgl.glGenFramebuffers(1, self.fbo)
-        bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, self.fbo[0])
-
-        bgl.glFramebufferRenderbuffer(
-                bgl.GL_FRAMEBUFFER, bgl.GL_DEPTH_ATTACHMENT,
-                bgl.GL_RENDERBUFFER, self.buf_depth[0])
-
-        bgl.glFramebufferTexture(bgl.GL_FRAMEBUFFER, bgl.GL_COLOR_ATTACHMENT0, self.buf_color[0], 0)
+        self._framebuffer_config()
 
-        bgl.glDrawBuffers(1, bgl.Buffer(bgl.GL_INT, 1, [bgl.GL_COLOR_ATTACHMENT0]))
+    def _framebuffer_config(self):
+        self._framebuffer_free()
 
-        status = bgl.glCheckFramebufferStatus(bgl.GL_FRAMEBUFFER)
-        if status != bgl.GL_FRAMEBUFFER_COMPLETE:
-            print("Framebuffer Invalid", status)
+        self._tex_color = gpu.types.GPUTexture((self.width, self.height), format='R32UI')
+        self._tex_depth = gpu.types.GPUTexture((self.width, self.height), format='DEPTH_COMPONENT32F')
+        self._fbo = gpu.types.GPUFrameBuffer(depth_slot=self._tex_depth, color_slots=self._tex_color)
 
-        bgl.glBindFramebuffer(bgl.GL_FRAMEBUFFER, self.cur_fbo[0])
-
-    def _config_textures(self):
-        import ctypes
-
-        bgl.glBindRenderbuffer(bgl.GL_RENDERBUFFER, self.buf_depth[0])
-        bgl.glRenderbufferStorage(
-                bgl.GL_RENDERBUFFER, bgl.GL_DEPTH_COMPONENT, self.width, self.height)
-
-        NULL = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int32 * 1).from_address(0))
-        bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.buf_color[0])
-        bgl.glTexImage2D(
-                bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list