[Bf-extensions-cvs] [7c0dd68b] master: snap_context module: replace functions that access the objects internals through ctypes

Germano noreply at git.blender.org
Wed Sep 27 21:21:34 CEST 2017


Commit: 7c0dd68b60184e2ff4265b98e120d503a32acfa0
Author: Germano
Date:   Wed Sep 27 16:21:26 2017 -0300
Branches: master
https://developer.blender.org/rBA7c0dd68b60184e2ff4265b98e120d503a32acfa0

snap_context module: replace functions that access the objects internals through ctypes

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

M	modules/snap_context/__init__.py
D	modules/snap_context/bgl_ext.py
M	modules/snap_context/mesh_drawing.py
M	modules/snap_context/utils_shader.py

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

diff --git a/modules/snap_context/__init__.py b/modules/snap_context/__init__.py
index 403520d0..35fe13f8 100644
--- a/modules/snap_context/__init__.py
+++ b/modules/snap_context/__init__.py
@@ -67,7 +67,7 @@ class SnapContext():
 
     def __init__(self, region, space):
         import gpu
-        from .bgl_ext import VoidBufValue
+        import ctypes
 
         self.freed = False
         self.snap_objects = []
@@ -92,8 +92,8 @@ class SnapContext():
         self._texture = self._offscreen.color_texture
         bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._texture)
 
-        NULL = VoidBufValue(0)
-        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, self.region.width, self.region.height, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL.buf)
+        NULL = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int * 1).from_address(0))
+        bgl.glTexImage2D(bgl.GL_TEXTURE_2D, 0, bgl.GL_R32UI, self.region.width, self.region.height, 0, bgl.GL_RED_INTEGER, bgl.GL_UNSIGNED_INT, NULL)
         del NULL
 
         bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST)
diff --git a/modules/snap_context/bgl_ext.py b/modules/snap_context/bgl_ext.py
deleted file mode 100644
index 61415b07..00000000
--- a/modules/snap_context/bgl_ext.py
+++ /dev/null
@@ -1,110 +0,0 @@
-# ##### BEGIN GPL LICENSE BLOCK #####
-#
-#  This program is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU General Public License
-#  as published by the Free Software Foundation; either version 3
-#  of the License, or (at your option) any later version.
-#
-#  This program is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#  GNU General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#
-# ##### END GPL LICENSE BLOCK #####
-
-
-import bgl
-import ctypes
-import numpy as np
-
-
-# figure out size of _Py_ssize_t
-if hasattr(ctypes.pythonapi, 'Py_InitModule4_64'):
-    _Py_ssize_t = ctypes.c_int64
-else:
-    _Py_ssize_t = ctypes.c_int
-
-class _PyObject(ctypes.Structure):
-    pass
-
-_PyObject._fields_ = [
-    ('ob_refcnt', _Py_ssize_t),
-    ('ob_type', ctypes.POINTER(_PyObject)),
-]
-
-if object.__basicsize__ != ctypes.sizeof(_PyObject):
-    # python with trace
-    class _PyObject(ctypes.Structure):
-        _fields_ = [
-            ('_ob_next', ctypes.POINTER(_PyObject)),
-            ('_ob_prev', ctypes.POINTER(_PyObject)),
-            ('ob_refcnt', _Py_ssize_t),
-            ('ob_type', ctypes.POINTER(_PyObject)),
-        ]
-
-class _PyVarObject(_PyObject):
-    _fields_ = [
-        ('ob_size', _Py_ssize_t),
-    ]
-
-class C_Buffer(_PyVarObject):
-    _fields_ = [
-        ("parent", ctypes.py_object),
-        ("type", ctypes.c_int),
-        ("ndimensions", ctypes.c_int),
-        ("dimensions", ctypes.POINTER(ctypes.c_int)),
-        ("buf", ctypes.c_void_p),
-    ]
-
-assert ctypes.sizeof(C_Buffer) == bgl.Buffer.__basicsize__
-
-class VoidBufValue():
-    def __init__(self, value):
-        self.buf = bgl.Buffer(bgl.GL_BYTE, 1)
-        self.c_buf = C_Buffer.from_address(id(self.buf))
-        self._allocated_buf = self.c_buf.buf
-        self.c_buf.buf = value
-        self.c_buf.type = 0 # allows repr
-    def __del__(self):
-        self.c_buf.buf = self._allocated_buf
-        #del self._allocated_buf
-        del self.buf
-
-
-def np_array_as_bgl_Buffer(array):
-    type = array.dtype
-    if type == np.int8:
-        type = bgl.GL_BYTE
-    elif type == np.int16:
-        type = bgl.GL_SHORT
-    elif type == np.int32:
-        type = bgl.GL_INT
-    elif type == np.float32:
-        type = bgl.GL_FLOAT
-    elif type == np.float64:
-        type = bgl.GL_DOUBLE
-    else:
-        raise
-
-    _decref = ctypes.pythonapi.Py_DecRef
-    _incref = ctypes.pythonapi.Py_IncRef
-
-    _decref.argtypes = _incref.argtypes = [ctypes.py_object]
-    _decref.restype = _incref.restype = None
-
-    buf = bgl.Buffer(bgl.GL_BYTE, ((1,) * (len(array.shape) + 1)))[0]
-    c_buf = C_Buffer.from_address(id(buf))
-
-    _decref(c_buf.parent)
-    _incref(array)
-
-    c_buf.parent = array # Prevents MEM_freeN
-    c_buf.type = type
-    for i, n in enumerate(array.shape):
-        c_buf.dimensions[i] = n
-    c_buf.buf = array.ctypes.data
-
-    return buf
diff --git a/modules/snap_context/mesh_drawing.py b/modules/snap_context/mesh_drawing.py
index 2aa7e167..82d87937 100644
--- a/modules/snap_context/mesh_drawing.py
+++ b/modules/snap_context/mesh_drawing.py
@@ -21,7 +21,6 @@ import bmesh
 import numpy as np
 from mathutils import Matrix
 
-from .bgl_ext import VoidBufValue, np_array_as_bgl_Buffer
 from .utils_shader import Shader
 
 
@@ -30,6 +29,10 @@ def load_shader(shadername):
     with open(path.join(path.dirname(__file__), 'resources', shadername), 'r') as f:
         return f.read()
 
+def gl_buffer_void_as_long(value):
+    import ctypes
+    a = (ctypes.c_byte * 1).from_address(value)
+    return bgl.Buffer(bgl.GL_BYTE, 1, a)
 
 def get_mesh_vert_co_array(me):
     tot_vco = len(me.vertices)
@@ -180,7 +183,7 @@ class GPU_Indices_Mesh():
     vert_co = bgl.Buffer(bgl.GL_FLOAT, 3)
 
     def __init__(self, obj, draw_tris, draw_edges, draw_verts):
-        self._NULL = VoidBufValue(0)
+        self._NULL = gl_buffer_void_as_long(0)
 
         self.MVP = bgl.Buffer(bgl.GL_FLOAT, (4, 4))
 
@@ -215,7 +218,8 @@ class GPU_Indices_Mesh():
             self.vbo = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.vbo_len * 12, np_array_as_bgl_Buffer(mesh_arrays.verts_co), bgl.GL_STATIC_DRAW)
+            verts_co = bgl.Buffer(bgl.GL_FLOAT, mesh_arrays.verts_co.shape, mesh_arrays.verts_co)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.vbo_len * 12, verts_co, bgl.GL_STATIC_DRAW)
 
         ## Create VBO for Tris ##
         if mesh_arrays.tri_verts is not None:
@@ -223,17 +227,19 @@ class GPU_Indices_Mesh():
             self.num_tris = len(self.tri_verts)
 
             np_tris_co = mesh_arrays.verts_co[mesh_arrays.tri_verts]
+            np_tris_co = bgl.Buffer(bgl.GL_FLOAT, np_tris_co.shape, np_tris_co)
             self.vbo_tris = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo_tris)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 36, np_array_as_bgl_Buffer(np_tris_co), bgl.GL_STATIC_DRAW)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 36, np_tris_co, bgl.GL_STATIC_DRAW)
             del np_tris_co
 
             tri_indices = np.repeat(np.arange(self.num_tris, dtype = 'f4'), 3)
+            tri_indices = bgl.Buffer(bgl.GL_FLOAT, tri_indices.shape, tri_indices)
             self.vbo_tri_indices = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo_tri_indices)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 12, np_array_as_bgl_Buffer(tri_indices), bgl.GL_STATIC_DRAW)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_tris * 12, tri_indices, bgl.GL_STATIC_DRAW)
             del tri_indices
 
         else:
@@ -246,17 +252,19 @@ class GPU_Indices_Mesh():
             self.num_edges = len(self.edge_verts)
 
             np_edges_co = mesh_arrays.verts_co[mesh_arrays.edge_verts]
+            np_edges_co = bgl.Buffer(bgl.GL_FLOAT, np_edges_co.shape, np_edges_co)
             self.vbo_edges = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo_edges)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 24, np_array_as_bgl_Buffer(np_edges_co), bgl.GL_STATIC_DRAW)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 24, np_edges_co, bgl.GL_STATIC_DRAW)
             del np_edges_co
 
             edge_indices = np.repeat(np.arange(self.num_edges, dtype = 'f4'), 2)
+            edge_indices = bgl.Buffer(bgl.GL_FLOAT, edge_indices.shape, edge_indices)
             self.vbo_edge_indices = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo_edge_indices)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 8, np_array_as_bgl_Buffer(edge_indices), bgl.GL_STATIC_DRAW)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_edges * 8, edge_indices, bgl.GL_STATIC_DRAW)
             del edge_indices
         else:
             self.num_edges = 0
@@ -268,17 +276,19 @@ class GPU_Indices_Mesh():
             self.num_verts = len(mesh_arrays.looseverts)
 
             np_lverts_co = mesh_arrays.verts_co[mesh_arrays.looseverts]
+            np_lverts_co = bgl.Buffer(bgl.GL_FLOAT, np_lverts_co.shape, np_lverts_co)
             self.vbo_verts = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo_verts)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 12, np_array_as_bgl_Buffer(np_lverts_co), bgl.GL_STATIC_DRAW)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 12, np_lverts_co, bgl.GL_STATIC_DRAW)
             del np_lverts_co
 
             looseverts_indices = np.arange(self.num_verts, dtype = 'f4')
+            looseverts_indices = bgl.Buffer(bgl.GL_FLOAT, looseverts_indices.shape, looseverts_indices)
             self.vbo_looseverts_indices = bgl.Buffer(bgl.GL_INT, 1)
             bgl.glGenBuffers(1, self.vbo_looseverts_indices)
             bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_looseverts_indices[0])
-            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 4, np_array_as_bgl_Buffer(looseverts_indices), bgl.GL_STATIC_DRAW)
+            bgl.glBufferData(bgl.GL_ARRAY_BUFFER, self.num_verts * 4, looseverts_indices, bgl.GL_STATI

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list