我遇到了这样一个事实,即在主流中调用 GL 时,没有发生错误,但是当我开始从 Task.Run 调用时,错误“尝试读取或写入受保护的内存。这通常表明其他内存腐败的。” 或者当我尝试在析构函数中调用纹理移除函数时发生错误
~Texture()
{
GL.DeleteTexture(handle);
}
我设法使用一个静态类来解决这个错误,在该类中我在析构函数中编写句柄,并且清理本身在主线程的每一帧中都被调用
public static class GarbageCollector
{
private static Stack<int> Textures { set; get; } = new Stack<int>();
public static void DeleteTexture(int handle)
{
Textures.Push(handle);
}
public static void ClearTextures()
{
while (Textures.TryPop(out int handle))
GL.DeleteTexture(handle);
}
}
析构函数现在看起来像这样:
~Texture()
{
GarbageCollector.DeleteTexture(Handle);
}
我调用的每一帧都清除记录的纹理以进行删除
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit|ClearBufferMask.DepthBufferBit);
SwapBuffers();
GarbageCollector.ClearTextures();
}
问题出现了如何从另一个线程调用 GL 类的方法,而不用手鼓跳舞?而如果这些函数不能从另一个线程调用,那么在主线程中,如果可能的话,应该只调用显卡的函数,而所有其他计算都应该通过这样的静态类链接到其他线程?
附言 一个最小可重现的示例,当您单击 Q 按钮时,我们会删除主线程中的缓冲区,而当您单击 W 按钮时,我们会删除 Task.Run 中的缓冲区
using OpenTK.Graphics.OpenGL4;
using OpenTK.Mathematics;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.Desktop;
using OpenTK.Windowing.GraphicsLibraryFramework;
using System;
using System.Threading.Tasks;
namespace TestOpenTk
{
class Program
{
static void Main(string[] args)
{
var nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Title = "My OpenTK Example Program"
};
using (var window = new GameWindowEngine(GameWindowSettings.Default, nativeWindowSettings))
{
window.Run();
}
}
}
public class GameWindowEngine : GameWindow
{
int VertexBufferObject;
public GameWindowEngine(GameWindowSettings gameWindowSettings,
NativeWindowSettings nativeWindowSettings)
: base(gameWindowSettings, nativeWindowSettings)
{
}
protected override void OnUpdateFrame(FrameEventArgs e)
{
base.OnUpdateFrame(e);
if (KeyboardState.IsKeyDown(Keys.Q))
{
GL.DeleteBuffer(VertexBufferObject);//Тут ошибки нет
}
if (KeyboardState.IsKeyDown(Keys.W))
{
Task.Run(() =>
{
GL.DeleteBuffer(VertexBufferObject);//тут ошибка есть
});
}
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
SwapBuffers();
}
protected override void OnLoad()
{
base.OnLoad();
GL.Enable(EnableCap.DepthTest);
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
VertexBufferObject = GL.GenBuffer();
}
}
}
我设法让它工作。
考虑到相互阻塞的存在,但这值得吗?你决定。