Ejemplo n.º 1
0
        static D3D11Buffer CreateAndCopyToDebugBuf(D3D11Device pDevice, D3D11DeviceContext pd3dImmediateContext, D3D11Buffer pBuffer)
        {
            var desc = pBuffer.Description;

            desc.CpuAccessOptions = D3D11CpuAccessOptions.Read;
            desc.Usage            = D3D11Usage.Staging;
            desc.BindOptions      = D3D11BindOptions.None;
            desc.MiscOptions      = D3D11ResourceMiscOptions.None;

            D3D11Buffer debugbuf = pDevice.CreateBuffer(desc);

#if DEBUG
            debugbuf.SetDebugName("Debug");
#endif

            pd3dImmediateContext.CopyResource(debugbuf, pBuffer);

            return(debugbuf);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                Console.Write("Creating device...");
                CreateComputeDevice();
                Console.WriteLine("done");

                Console.Write("Creating Compute Shader...");
                CreateComputeShader();
                Console.WriteLine("done");

                Console.Write("Creating buffers and filling them with initial data...");

                for (int i = 0; i < NUM_ELEMENTS; ++i)
                {
                    g_vBuf0[i].i = i;
                    g_vBuf0[i].f = (float)i;
#if TEST_DOUBLE
                    g_vBuf0[i].d = (double)i;
#endif

                    g_vBuf1[i].i = i;
                    g_vBuf1[i].f = (float)i;
#if TEST_DOUBLE
                    g_vBuf1[i].d = (double)i;
#endif
                }

#if USE_STRUCTURED_BUFFERS
                g_pBuf0      = CreateStructuredBuffer(deviceResources.D3DDevice, g_vBuf0);
                g_pBuf1      = CreateStructuredBuffer(deviceResources.D3DDevice, g_vBuf1);
                g_pBufResult = CreateStructuredBuffer(deviceResources.D3DDevice, new BufType[NUM_ELEMENTS]);
#else
                g_pBuf0      = CreateRawBuffer(deviceResources.D3DDevice, g_vBuf0);
                g_pBuf1      = CreateRawBuffer(deviceResources.D3DDevice, g_vBuf1);
                g_pBufResult = CreateRawBuffer(deviceResources.D3DDevice, new BufType[NUM_ELEMENTS]);
#endif

#if DEBUG
                g_pBuf0?.SetDebugName("Buffer0");
                g_pBuf1?.SetDebugName("Buffer1");
                g_pBufResult?.SetDebugName("Result");
#endif

                Console.WriteLine("done");

                Console.Write("Creating buffer views...");
                g_pBuf0SRV      = CreateBufferSRV(deviceResources.D3DDevice, g_pBuf0);
                g_pBuf1SRV      = CreateBufferSRV(deviceResources.D3DDevice, g_pBuf1);
                g_pBufResultUAV = CreateBufferUAV(deviceResources.D3DDevice, g_pBufResult);

#if DEBUG
                g_pBuf0SRV?.SetDebugName("Buffer0 SRV");
                g_pBuf1SRV?.SetDebugName("Buffer1 SRV");
                g_pBufResultUAV?.SetDebugName("Result UAV");
#endif

                Console.WriteLine("done");

                Console.Write("Running Compute Shader...");
                RunComputeShader(deviceResources.D3DContext, g_pCS, new[] { g_pBuf0SRV, g_pBuf1SRV }, g_pBufResultUAV, NUM_ELEMENTS, 1, 1);
                Console.WriteLine("done");

                // Read back the result from GPU, verify its correctness against result computed by CPU
                D3D11Buffer debugbuf = CreateAndCopyToDebugBuf(deviceResources.D3DDevice, deviceResources.D3DContext, g_pBufResult);

                try
                {
                    D3D11MappedSubResource mappedResource = deviceResources.D3DContext.Map(debugbuf, 0, D3D11MapCpuPermission.Read, D3D11MapOptions.None);

                    try
                    {
                        // Set a break point here and put down the expression "p, 1024" in your watch window to see what has been written out by our CS
                        // This is also a common trick to debug CS programs.

                        // Verify that if Compute Shader has done right
                        Console.Write("Verifying against CPU result...");
                        bool bSuccess = true;

                        for (int i = 0; i < NUM_ELEMENTS; ++i)
                        {
                            BufType p = Marshal.PtrToStructure <BufType>(mappedResource.Data + i * (int)BufType.Size);

                            if ((p.i != g_vBuf0[i].i + g_vBuf1[i].i) ||
                                (p.f != g_vBuf0[i].f + g_vBuf1[i].f)
#if TEST_DOUBLE
                                || (p.d != g_vBuf0[i].d + g_vBuf1[i].d)
#endif
                                )
                            {
                                Console.WriteLine("failure");
                                bSuccess = false;
                                break;
                            }
                        }

                        if (bSuccess)
                        {
                            Console.WriteLine("succeeded");
                        }
                    }
                    finally
                    {
                        deviceResources.D3DContext.Unmap(debugbuf, 0);
                    }
                }
                finally
                {
                    D3D11Utils.ReleaseAndNull(ref debugbuf);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                Console.Write("Cleaning up...");
                CleanupResources();
                Console.WriteLine("done");
            }

            Console.ReadKey(false);
        }