无法创建暂存 Texture3D

Can't create staging Texture3D

我想创建暂存 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通道纹理可能不会工作或崩溃你的 driver)

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(带有读取标志)访问您的纹理数据。