我想创建暂存Texture3D并将其绑定到无序访问视图,以使用DirectCompute执行一些计算,然后使用CPU读取它们。不幸的是,使用以下描述创建Texture3D时出现错误:
Texture3DDescription desc = new Texture3DDescription()
{
BindFlags = BindFlags.UnorderedAccess,
CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write,
Depth = sunAngleIterations,
Format = SharpDX.DXGI.Format.R32G32B32_Float,
Height = viewAngleIterations,
MipLevels = 1,
OptionFlags = ResourceOptionFlags.None,
Usage = ResourceUsage.Staging,
Width = heightIterations
};
texture = new Texture3D(device, desc);
抛出的异常是:
{HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: Parameter jest niepoprawny.}
有什么主意吗?
最佳答案
暂存纹理无法绑定到着色器管道,因此您需要首先创建一个默认纹理(同样,并非所有卡都支持3个通道,因此我也更改了格式,尝试采样3个通道纹理可能不起作用或使驱动程序崩溃)
Texture3DDescription desc = new Texture3DDescription()
{
BindFlags = BindFlags.UnorderedAccess,
CpuAccessFlags = CpuAccessFlags.None,
Depth = sunAngleIterations,
Format = SharpDX.DXGI.Format.R32G32B32A32_Float,
Height = viewAngleIterations,
MipLevels = 1,
OptionFlags = ResourceOptionFlags.None,
Usage = ResourceUsage.Default,
Width = heightIterations
};
texture = new Texture3D(device, desc);
然后执行计算并使用暂存纹理来检索数据:
Texture3DDescription stagingdesc = new Texture3DDescription()
{
BindFlags = BindFlags.None,
CpuAccessFlags = CpuAccessFlags.Read,
Depth = sunAngleIterations,
Format = SharpDX.DXGI.Format.R32G32B32A32_Float,
Height = viewAngleIterations,
MipLevels = 1,
OptionFlags = ResourceOptionFlags.None,
Usage = ResourceUsage.Staging,
Width = heightIterations
};
stagingtexture = new Texture3D(device, stagingdesc);
然后需要使用deviceContext.CopyResource将默认rexture的内容复制到暂存纹理。
完成后,您可以使用deviceContext.MapSubresource(带有读取标志)来访问纹理数据。