新的Texture2D和Texture.SetData在大多数情况下都可以工作,但它会导致事情挂起。



这是monogame 中的错误,还是我做错了什么?

在任务/线程中运行时。这将导致OpenGL挂起/冻结。(在 DirectX 中工作正常)。虽然这不会一直发生。我们使用类似的代码从程序另一部分中的原始图像数据创建纹理。它在那里工作正常,它也在任务/线程中。

任务创建者:

List<Task> tasks = new List<Task>
{
Task.Run(() => Data.TryAdd(SectionName, Dataobj.Create(new Rectangle(0, 500, (int)Size.X, 124))))
}

挂在SetDatanew Texture2D上。通过将新的 texture2d 移动到主线程进行测试。

Texture2D localdot = new Texture2D(Memory.graphics.GraphicsDevice, 4, 4);
Color[] tmp = new Color[localdot.Height * localdot.Width];
for (int i = 0; i < tmp.Length; i++)
tmp[i] = Color.White;
localdot.SetData(tmp);

我正在等待完成的部分。

if (!Task.WaitAll(tasks.ToArray(), 10000))
throw new TimeoutException("Task took too long!");

在生成其他线程之前,在主线程中创建纹理和数组。任务(线程)应该只设置数据数组,完成后,可以在主线程中调用SetData()

Texture2D构造函数 norSetData()是线程安全的,可能会导致争用条件;由于Texture2D连接到GraphicsDevice.

(DirectX-OpenGL)差异很可能是由于SharpDX(DirectX)实现中的锁定机制,这在OpenGL版本中不会发生。

通过采用上述策略,可以在"DirectX"版本中提高性能。

最新更新