glGetBufferSubData pyopengl 随机结果和段错误

问题描述 投票:0回答:2

为了扩展我的库中的一些内容,我需要将数据从 VBO 读回 CPU 内存。这里不涉及转换反馈。

当我从缓冲区读取数据时,我要么得到“随机数据”要么“分段错误”“非法硬件指令”“malloc”。

以下是我遇到的不同错误:

malloc

python(678,0x7fff746f7000) malloc: *** error for object 0x7fa532e1d6b8: 
incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
[1]    678 abort      python -m glib.examples.transformation_domain

段错误

[1]    2448 segmentation fault  python -m demos.read_vbo

非法硬件指令

objc[2789]: Method cache corrupted. This may be a message to an invalid object, or a memory error somewhere else.
objc[2789]: receiver 0x7fcaee1efaf0, SEL 0x7fff8314a468, isa 0x7fff72e0cf18, cache 0x7fff72e0cf28, buckets 0x7fcaee1d0a10, mask 0x1f, occupied 0x10
objc[2789]: receiver 64 bytes, buckets 528 bytes
objc[2789]: selector 'key'
objc[2789]: isa '_CFXNotificationNameWildcardObjectRegistration'
objc[2789]: Method cache corrupted.
[1]    2789 illegal hardware instruction  python -m glib.examples.transformation_domain

因为我想知道我的应用程序中是否有某些内容可能导致此问题(可能是一个糟糕的状态,...)我隔离了该问题:

"""
this code demosntrates problems with glGetBufferSubData
@author Nicolas 'keksnicoh' Heimann
"""
from gllib.glfw import * 
from OpenGL.GL import * 
import numpy as np 

# spawn glfw stuff
if not glfwInit(): raise RuntimeError('glfw.Init() error')
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(500, 500)
glfwMakeContextCurrent(window)

# push data to vbo
data = np.array([1,2,3,4,5,6], dtype=np.float32)
vbo = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, 6*4, data, GL_STATIC_READ)
glBindBuffer(GL_ARRAY_BUFFER, 0)

# pullback
recv_data = np.empty_like(data)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glGetBufferSubData(GL_ARRAY_BUFFER, 0, 6*4, recv_data)
glBindBuffer(GL_ARRAY_BUFFER, 0)

print(recv_data)

此代码每次运行都会产生不同的结果,或者以上述错误之一结束。

    1 keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo                                 :(
[  0.00000000e+00   1.58456325e+29   0.00000000e+00   1.58456325e+29
   4.02037986e-33   1.40129846e-45]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[  0.00000000e+00  -4.65661287e-10   0.00000000e+00  -4.65661287e-10
   5.88545355e-44   0.00000000e+00]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[  0.00000000e+00   3.68934881e+19   0.00000000e+00   3.68934881e+19
   1.89559592e+28   1.40129846e-43]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[  0.00000000e+00   2.52435490e-29   0.00000000e+00   2.52435490e-29
   1.89559592e+28   1.40129846e-43]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[  0.00000000e+00   3.68934881e+19   0.00000000e+00   3.68934881e+19
   2.92698291e-36   1.40129846e-45]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[  0.00000000e+00  -3.68934881e+19   0.00000000e+00  -3.68934881e+19
   7.42639198e-31   1.40129846e-45]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[  0.00000000e+00  -4.65661287e-10   0.00000000e+00  -4.65661287e-10
   7.22223950e-33   1.40129846e-45]
keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo
[1]    4263 segmentation fault  python -m demos.read_vbo
139 keksnicoh@dhcp-172-21-66-45 ~/unih/bsc/opengl_plot_prototype (git)-[master] % python -m demos.read_vbo                               :(
[1]    4335 segmentation fault  python -m demos.read_vbo

看来这个glGetBufferSubData可能是从错误的内存空间读取的?或者也许指针recv_data有一些问题?

这是我的 OpenGL 设置:

[...] init GLFW
[...] load OPENGL_CORE_PROFILE 4.10
[...] initialize glfw window
  + Opengl version     410
  + GLSL Version       410
  + GLFW3              (3, 1, 2)

我使用 MacBookPro Retina 2013

Date/Time:             2015-06-22 21:30:53.795 +0200
OS Version:            Mac OS X 10.10.2 (14C109)
Report Version:        11
python opengl pyopengl
2个回答
1
投票

经过对这个问题的更多调查,我找到了解决方案。首先,我意识到 recv_data 参数可能会发生一些问题。导致无效操作异常,我可以看到 clGetBufferSubData 的 c 调用的内部跟踪

    OpenGL.error.GLError: GLError(
    err = 1281,
    description = 'invalid value',
    baseOperation = glGetBufferSubData,
    pyArgs = (
        GL_ARRAY_BUFFER,
        0,
        48,
        array([ 0.,  0.,  0.,  0.,  0.,  0.], dtype=float32),
    ),
    cArgs = (
        GL_ARRAY_BUFFER,
        0,
        48,
        array([0, 0, 0, 0, 0, 0], dtype=uint8),  *** < data is now unit8 instead of float32
    ),
    cArguments = (
        GL_ARRAY_BUFFER,
        0,
        48,
        array([0, 0, 0, 0, 0, 0], dtype=uint8),  
    )
)

可以看出,也许参数处理错误,因为它不应该是一个unit8?函数glGetBufferSubData返回一个unit8类型的numpy.ndarray。然后我通过转换它。 numpy 查看回到 float32 并最终可以读取数据。

# pullback
glBindBuffer(GL_ARRAY_BUFFER, vbo)
raw_unit8_data = glGetBufferSubData(GL_ARRAY_BUFFER, 0, 6*4)
glBindBuffer(GL_ARRAY_BUFFER, 0)
print(raw_unit8_data.view('<f4'))

感谢另一篇 stackoverflow 文章 的简短转换

由于这种行为对我来说看起来很奇怪,所以我在 pyopengl 票证系统上创建了一个 bug 票证


0
投票

我遇到了同样的问题,发现也可以通过传递

array.data
而不是
array
来解决:

...
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo)
array = np.empty(num_elements, dtype=np.float32)
glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, array.nbytes,array.data)

这是我的代码中的一个片段,它使用另一个缓冲区,但原理应该是相同的

© www.soinside.com 2019 - 2024. All rights reserved.