我正在尝试实现一个就地落沙算法,假设我有一个 3D 纹理,我希望所有不为零的点在该点为空 (0) 时向下移动一个点。这很容易做到:
Let's say 'from' is xyz, 'to' is xyz + (0, -1, 0)
uint toValueWas;
InterlockedCompareExchange(_texture[to], 0, _texture[from], toValueWas);
if (toValueWas == 0)
{
_texture[from] = toValueWas;
}
这很完美,但是如果我需要比一个单位更多的存储空间怎么办?假设我想要另一个纹理 b,它将给我额外的存储空间,并且我希望哪些移动与原始纹理保持同步。我已经尝试了各种方法来做到这一点,但两个纹理/缓冲区总是不同步。例如,这不起作用:
uint toValueWas;
InterlockedCompareExchange(_texture[to], 0, _texture[from], toValueWas);
if (toValueWas == 0)
{
_texture[from] = toValueWas;
// Goes out of sync with _texture:
InterlockedExchange(_textureB[to], _textureB[from], _textureB[from]);
}
问题归结为:有没有办法在超过 32 位的情况下有效地进行原子交换?
我曾期望找到任何方法来自动交换超过 32 位的数据,但找不到它。