__del__函数中PyOpenGL.glDeleteBuffers的奇怪行为?

时间:2019-03-03 14:34:12

标签: python glfw pyopengl del

我发现了一些我不了解的del行为,希望您能为我提供一些见识。我正在尝试使用PyOpenGL和glfw实现OpenGL的hello_triangle。关闭OpenGL窗口后,应该清理程序,但是glDeleteBuffers会引发TypeError,但仅在__del__函数内部被调用时才会出现:

class Scene:
    def __init__ (self):
        # ...
        self.buffer = glGenBuffers(1)
        # ...
    def __del__ (self):
        # ...
        glDeleteBuffers(1, [self.buffer]) # TypeError: ('No array-type handler for type builtins.type (value: [1]) registered', <OpenGL.converters.CallFuncPyConverter object at ...>)
        # ...

# ...
scene = Scene()
while not glfwWindowShouldClose(window):
    scene.render()
    glfwSwapBuffers(window)
    glfwPollEvents()

del scene

如果相反,我会这样实现

class Scene:
    def __init__ (self):
        # ...
        self.buffer = glGenBuffers(1)
        # ...
    def delete (self): # Renamed __del__ to delete
        # ...
        glDeleteBuffers(1, [self.buffer]) # No error
        # ...

# ...
scene = Scene()
while not glfwWindowShouldClose(window):
    scene.render()
    glfwSwapBuffers(window)
    glfwPollEvents()

scene.delete() # Swapped del scene for scene.delete()

glDeleteBuffers突然起作用并且没有引发任何错误。 这是为什么?如果您想自己尝试,请参见完整代码:

import ctypes
import sys

# OpenGL + GLFW
import glfw
from glfw.GLFW import *
from OpenGL.GL import *


glfw.ERROR_REPORTING = False # Catch errors by return values

class obj: pass # Object to assign arbitrary properties to



def main (args):
    # Initialize GLFW + create window
    if glfwInit() == GL_TRUE:
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4)
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5)
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE)

        window = glfwCreateWindow(800, 600, "Title", None, None)
        if window:
            glfwMakeContextCurrent(window)

            window_size_callback(window, 800, 600)
            glfwSetWindowSizeCallback(window, window_size_callback)

            # Render stuff
            scene = Scene()

            while not glfwWindowShouldClose(window):
                scene.render()
                glfwSwapBuffers(window)

                glfwPollEvents()

            # Clean up
            del scene # TypeError
            # scene.delete() # no TypeError

        else:
            print("Failed to create GLFW window!")

        glfwTerminate()
    else:
        print("Failed to initialize GLFW!")



def window_size_callback (window, width, height):
    glViewport(0, 0, width, height)



class Scene:
    _instances = []

    class vertex (ctypes.Structure):
        _fields_ = [
            ("x", GLfloat),
            ("y", GLfloat)
        ]

    def __static_init__ ():
        # Create rendering pipeline program
        vertex_shader = glCreateShader(GL_VERTEX_SHADER)
        glShaderSource(vertex_shader, """#version 450 core
        in vec4 pos;

        void main () {
            gl_Position = vec4(pos.xy, 0.0, 1.0);
        }""")
        glCompileShader(vertex_shader)

        fragment_shader = glCreateShader(GL_FRAGMENT_SHADER)
        glShaderSource(fragment_shader, """#version 450 core
        out vec4 frag_color;

        void main () {
            frag_color = vec4(1.0, 1.0, 1.0, 1.0);
        }""")
        glCompileShader(fragment_shader)

        Scene._program = glCreateProgram()
        glAttachShader(Scene._program, vertex_shader)
        glAttachShader(Scene._program, fragment_shader)
        glLinkProgram(Scene._program)

        glDeleteShader(vertex_shader)
        glDeleteShader(fragment_shader)

        # Create VAO
        Scene._vertex_array = glGenVertexArrays(1)
        glBindVertexArray(Scene._vertex_array)

        Scene._attrib_pos = glGetAttribLocation(Scene._program, "pos")
        glVertexAttribFormat(Scene._attrib_pos, 2, GL_FLOAT, GL_FALSE, 0)
        glEnableVertexAttribArray(Scene._attrib_pos)
        glVertexAttribBinding(Scene._attrib_pos, Scene._attrib_pos)

    def __static_del__ ():
        glDeleteVertexArrays(1, [Scene._vertex_array]) # Alsa raises a TypeError, if glDeleteBuffers' error is catched before
        glDeleteProgram(Scene._program)

    def __init__ (self):
        if len(Scene._instances) == 0:
            Scene.__static_init__()

        Scene._instances.append(self)

        # Create VBO
        vertex_buffer_data = (Scene.vertex * 3)(
            Scene.vertex(-0.5, 0.5),
            Scene.vertex(0.5, 0.5),
            Scene.vertex(0.5, -0.5)
        )

        self._vertex_buffer = obj()
        self._vertex_buffer.buffer = glGenBuffers(1)
        self._vertex_buffer.length = len(vertex_buffer_data)
        self._vertex_buffer.offset = Scene.vertex.x.offset
        self._vertex_buffer.stride = ctypes.sizeof(Scene.vertex)

        glBindBuffer(GL_ARRAY_BUFFER, self._vertex_buffer.buffer)
        glBufferData(GL_ARRAY_BUFFER, vertex_buffer_data, GL_STATIC_DRAW)

    def __del__ (self): # Rename to delete
        glDeleteBuffers(1, [self._vertex_buffer.buffer]) # TypeError, if executed in __del__(), but not when executeed in delete()

        Scene._instances.remove(self)
        if len(Scene._instances) == 0:
            Scene.__static_del__()

    def render (self):
        glClearColor(0.0, 0.1, 0.2, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        # Draw
        glUseProgram(Scene._program)
        glBindVertexArray(Scene._vertex_array)

        glBindVertexBuffer(Scene._attrib_pos, self._vertex_buffer.buffer, self._vertex_buffer.offset, self._vertex_buffer.stride)

        glDrawArrays(GL_TRIANGLES, 0, self._vertex_buffer.length)



if __name__ == "__main__":
    main(sys.argv[1:])

1 个答案:

答案 0 :(得分:1)

  

glDeleteBuffers引发TypeError,但只有在__del__函数内部调用时才会出现:

由于在调用析构函数之前OpenGL上下文已被破坏而导致错误。
与其他任何OpenGL指令一样,对于glDeleteBuffers,需要有效的当前OpenGL上下文。

如果

scene.delete()

被调用,然后delete()并由此glDeleteBuffers被立即调用。此时,OpenGL上下文是最新的,无论如何该操作都将成功。

但是当你这样做

del scene

然后不能保证立即销毁析构函数。

请参见Python- 3.3.1. Data model - Basic customization

  

请注意,del x不会直接调用x.__del__()-前者将x的引用计数减一,而后者仅在x的引用计数达到零时才调用。

何时调用析构函数取决于垃圾回收。 Python不提供任何保证,关于析构函数何时被调用,发生在所有引用被删除之后,因此它不一定在紧随其后发生。

这导致在OpenGL竞赛被销毁后(glfwTerminate()之后)调用了destructoris,操作失败。

一种安全的方法是直接调用析构函数:

例如

Scene.__del__(scene)