matplotlib Matplolib和Arcade计算着色器:默认主上下文冲突

e0bqpujr  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(89)

我正在尝试改编一个用python编写的大型辐射传输代码,以使用GPU的能力,因为我执行了很多次相同的计算,这些计算可以并行完成。我是一个着色器新手,但发现this似乎提供了我想要的。它使用Arcade python模块,运行良好,因为是,在我的机器上。我开始在我的辐射传输代码中实现这个,但是得到了一个错误,我在互联网上找不到任何地方:

(python:20832): GLib-GIO-CRITICAL **: 14:10:15.559: g_application_run() cannot acquire the default main context because it is already acquired by another thread!

(python:20832): Gdk-WARNING **: 14:10:15.563: gdk_gl_context_make_current() failed

我相信这与在同一个程序中使用matplotlib有关。我可以通过在主脚本中导入matplotlib.pyplot并尝试绘制一个虚拟图来在来自arcade的演示代码(their github)中重现这个错误:

import matplotlib.pyplot as plt

import arcade
from arcade.gl import BufferDescription

###
Rest of the main.py file
###

if __name__ == '__main__':
    f = plt.figure()
    plt.plot(range(10), range(10))
    plt.show()
    
    app = MyWindow()
    arcade.run()

我读了很多论坛和手册,都提到了线程和上下文,但是没有找到任何提到这个错误的地方,也没有找到解决这个问题的方法。我可以为matplotlib和计算着色器设置两个单独的上下文吗?在我的代码中,我会在计算着色器之前或之后使用matplotlib,但不会同时使用。而且着色器只运行一次,而不是像演示示例中的每一帧,我知道这可能是一个愚蠢的问题,因为我并不真正理解我在做什么。然而,我找到的所有关于这个主题的资源都不是面向初学者的,所以我很乐意学习初学者的tuto,如果你知道一个好的,或者更适合在python中使用计算着色器的方法。
提前感谢您的帮助

z9smfwbn

z9smfwbn1#

在这篇文章下面进行了大量的搜索和等待奇迹之后,我想我找到了一个更好的方法来完成我正在尝试做的事情。首先,我找到了modernGL python模块,它似乎更适合我的目的,因为它不是一个游戏引擎,并且专注于着色器。然后,使用他们在github link上的计算着色器示例,我尝试了一下,发现在文件的最后,如果我先释放modernGL上下文,就可以使用matplotlib,如下所示:

# Begining of the example file
...
import matplotlib.pyplot as plt
...    
# First plot with matplotlib
plt.plot(range(10), range(10), 'r')
...
# Creation of the modernGL context
context = moderngl.create_standalone_context(require=430)
# Compute shader usage
...
# End of the example file, we can release the context and come back to the previous one to allow plotting with matplotlib
context.release()

# Plotting a second graph and displaying them.
plt.plot(range(10), range(10) + 1, 'b')
plt.show()

我希望有一天这能帮助一些迷失的灵魂;)

相关问题