From 7c0dd68b60184e2ff4265b98e120d503a32acfa0 Mon Sep 17 00:00:00 2001 From: Germano Date: Wed, 27 Sep 2017 16:21:26 -0300 Subject: snap_context module: replace functions that access the objects internals through ctypes --- modules/snap_context/__init__.py | 6 +- modules/snap_context/bgl_ext.py | 110 ----------------------------------- modules/snap_context/mesh_drawing.py | 40 ++++++++----- modules/snap_context/utils_shader.py | 8 +-- 4 files changed, 32 insertions(+), 132 deletions(-) delete mode 100644 modules/snap_context/bgl_ext.py (limited to 'modules') 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 . -# -# ##### 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_STATIC_DRAW) del looseverts_indices else: self.num_verts = 0 @@ -328,11 +338,11 @@ class GPU_Indices_Mesh(): bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tris[0]) bgl.glEnableVertexAttribArray(self.attr_pos) - bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) + bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_tri_indices[0]) bgl.glEnableVertexAttribArray(self.attr_primitive_id) - bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) + bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glDrawArrays(bgl.GL_TRIANGLES, 0, self.num_tris * 3) @@ -343,11 +353,11 @@ class GPU_Indices_Mesh(): bgl.glUniform1f(self.unif_offset, float(index_offset)) #TODO: use glUniform1ui bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edges[0]) - bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) + bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_edge_indices[0]) - bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) + bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glDrawArrays(bgl.GL_LINES, 0, self.num_edges * 2) @@ -358,11 +368,11 @@ class GPU_Indices_Mesh(): bgl.glUniform1f(self.unif_offset, float(index_offset)) #TODO: use glUniform1ui bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_verts[0]) - bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) + bgl.glVertexAttribPointer(self.attr_pos, 3, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_pos) bgl.glBindBuffer(bgl.GL_ARRAY_BUFFER, self.vbo_looseverts_indices[0]) - bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL.buf) + bgl.glVertexAttribPointer(self.attr_primitive_id, 1, bgl.GL_FLOAT, bgl.GL_FALSE, 0, self._NULL) bgl.glEnableVertexAttribArray(self.attr_primitive_id) bgl.glDrawArrays(bgl.GL_POINTS, 0, self.num_verts) diff --git a/modules/snap_context/utils_shader.py b/modules/snap_context/utils_shader.py index ef47dd09..769c6dbb 100644 --- a/modules/snap_context/utils_shader.py +++ b/modules/snap_context/utils_shader.py @@ -28,15 +28,15 @@ def check_shaderError(shader, flag, isProgram, errorMessage): if success[0] == bgl.GL_FALSE: import numpy as np - from .bgl_ext import VoidBufValue + import ctypes - offset = VoidBufValue(None) + offset = bgl.Buffer(bgl.GL_INT, 1, (ctypes.c_int * 1).from_address(0)) error = bgl.Buffer(bgl.GL_BYTE, 1024) if isProgram: - bgl.glGetProgramInfoLog(shader, 1024, offset.buf, error) + bgl.glGetProgramInfoLog(shader, 1024, offset, error) print(errorMessage, np.bytes_(error).decode("utf-8")) else: - bgl.glGetShaderInfoLog(shader, 1024, offset.buf, error) + bgl.glGetShaderInfoLog(shader, 1024, offset, error) print(errorMessage, np.bytes_(error).decode("utf-8")) del offset -- cgit v1.2.3