使用SharpDX运行DX11计算着色器 - 无法获得结果

3
我正在尝试使用SharpDX运行计算着色器并获取生成的纹理。
据我所知,我需要: 1. 创建一个纹理作为着色器的输出。 2. 将上述纹理设置为无序访问视图,以便可以对其进行写入。 3. 运行着色器 4. 将UAV纹理复制到分段纹理中,以便CPU可以访问它 5. 从分段纹理读取到位图
问题在于,无论我做什么,结果都是黑色的位图。我不认为问题在于Texture2D-> Bitmap转换代码,因为直接从分段纹理打印第一个像素也会得到0。
这是我的着色器代码:
RWTexture2D<float4> Output : register(u0);

[numthreads(32, 32, 1)]
void main(uint3 id : SV_DispatchThreadID) {
    Output[id.xy] = float4(0, 1.0, 0, 1.0);
}

利用微软DX11文档和博客,我拼凑出了这段代码以运行纹理:

public class GPUScreenColor {
    private int adapterIndex = 0;

    private Adapter1 gpu;
    private Device device;
    private ComputeShader computeShader;

    private Texture2D texture;
    private Texture2D stagingTexture;
    private UnorderedAccessView view;

    public GPUScreenColor() {
        initializeDirectX();
    }

    private void initializeDirectX() {
        using (var factory = new Factory1()) {
            gpu = factory.GetAdapter1(adapterIndex);
        }

        device = new Device(gpu, DeviceCreationFlags.Debug, FeatureLevel.Level_11_1);

        var compilationResult = ShaderBytecode.CompileFromFile("test.hlsl", "main", "cs_5_0", ShaderFlags.Debug);
        computeShader = new ComputeShader(device, compilationResult.Bytecode);

        texture = new Texture2D(device, new Texture2DDescription() {
            BindFlags = BindFlags.UnorderedAccess | BindFlags.ShaderResource,
            Format = Format.R8G8B8A8_UNorm,
            Width = 1024,
            Height = 1024,
            OptionFlags = ResourceOptionFlags.None,
            MipLevels = 1,
            ArraySize = 1,
            SampleDescription = { Count = 1, Quality = 0 }
        });

        UnorderedAccessView view = new UnorderedAccessView(device, texture, new UnorderedAccessViewDescription() {
            Format = Format.R8G8B8A8_UNorm,
            Dimension = UnorderedAccessViewDimension.Texture2D,
            Texture2D = { MipSlice = 0 }
        });

        stagingTexture = new Texture2D(device, new Texture2DDescription {
            CpuAccessFlags = CpuAccessFlags.Read,
            BindFlags = BindFlags.None,
            Format = Format.R8G8B8A8_UNorm,
            Width = 1024,
            Height = 1024,
            OptionFlags = ResourceOptionFlags.None,
            MipLevels = 1,
            ArraySize = 1,
            SampleDescription = { Count = 1, Quality = 0 },
            Usage = ResourceUsage.Staging
        });
    }

    public Bitmap getBitmap() {
        device.ImmediateContext.ComputeShader.Set(computeShader);
        device.ImmediateContext.ComputeShader.SetUnorderedAccessView(0, view);

        device.ImmediateContext.Dispatch(32, 32, 1);
        device.ImmediateContext.CopyResource(texture, stagingTexture);
        var mapSource = device.ImmediateContext.MapSubresource(stagingTexture, 0, MapMode.Read, MapFlags.None);

        Console.WriteLine(Marshal.ReadInt32(IntPtr.Add(mapSource.DataPointer, 0)));

        try {
            // Copy pixels from screen capture Texture to GDI bitmap
            Bitmap bitmap = new Bitmap(1024, 1024, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
            BitmapData mapDest = bitmap.LockBits(new Rectangle(0, 0, 1024, 1024), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            try {
                var sourcePtr = mapSource.DataPointer;
                var destPtr = mapDest.Scan0;
                for (int y = 0; y < 1024; y++) {
                    // Copy a single line
                    Utilities.CopyMemory(destPtr, sourcePtr, 1024 * 4);

                    // Advance pointers
                    sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch);
                    destPtr = IntPtr.Add(destPtr, mapDest.Stride);
                }

                return bitmap;
            } finally {
                bitmap.UnlockBits(mapDest);
            }
        } finally {
            device.ImmediateContext.UnmapSubresource(stagingTexture, 0);
        }
    }
}

我对着色器还比较陌生,所以可能有一些显而易见的东西...


你是否使用D3D11调试(并启用不安全调试,并检查调试VS输出窗口)来检查流程中是否有任何问题?你尝试过图形调试器吗?(例如Renderdoc) - xoofx
1个回答

3

首先,你需要在本地创建你的无人机:

UnorderedAccessView view = new UnorderedAccessView(....

所以该字段现在为空,被替换为

view = new UnorderedAccessView(....

首先,将解决第一个问题。

其次,运行时很可能会抱怨类型(调试将为您提供类似以下内容的信息:

在着色器代码中声明的组件 0 的资源返回类型(FLOAT)与计算着色器单元的无序访问视图插槽 0 绑定的资源类型(UNORM)不兼容。

某些显卡可能会做一些事情(默默地修复它),有些可能什么也不做,有些可能会崩溃 :)

问题在于 RWTexture2D 不匹配 UNORM 格式(因为您在此处指定了浮点格式)。

您需要强制将 RWTexture 明确设置为 unorm 格式,例如(是的,运行时可以如此挑剔):

RWTexture2D<unorm float4> Output : register(u0);

那么你的整个设置应该可以工作(PS:我没有检查位图代码,但我仔细检查了着色器是否正常运行,并且第一个像素匹配)


我确信这是我错过的一些愚蠢的东西。谢谢!当我回家(本周末)时,我会再次检查以确保解决了问题,并标记答案。同时,我在一个新项目中让它工作了,但没有发现问题出在哪里。 - stormbreaker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接