コード例 #1
0
ファイル: Program.cs プロジェクト: yonglehou/SharpDX-Samples
        public void Run()
        {
            // --------------------------------------------------------------------------------------
            // Init Direct3D11
            // --------------------------------------------------------------------------------------

            // Create Device and SwapChain
            var device  = new Device(DriverType.Hardware, DeviceCreationFlags.Debug);
            var context = device.ImmediateContext;

            const int Width  = 1024;
            const int Height = 1024;
            const int Count  = 1000;

            Console.WriteLine("Texture Size: ({0},{1}) - Count: {2}", Width, Height, Width * Height);
            Console.WriteLine();

            // Create random buffer
            var   random        = new Random();
            float maxScale      = (float)((byte)random.Next());
            var   randbomBuffer = new DataStream(sizeof(float) * Width * Height, true, true);

            for (int i = 0; i < Width * Height; i++)
            {
                var value = (float)random.NextDouble();
                if (value < 0.1 || value > 0.9)
                {
                    value = value * (float)random.NextDouble() * maxScale * ((value < 0.1) ? maxScale : 1.0f);
                }
                randbomBuffer.Write(value);
            }

            // Create random 2D texture
            var texture = new Texture2D(
                device,
                new Texture2DDescription
            {
                ArraySize         = 1,
                BindFlags         = BindFlags.ShaderResource,
                CpuAccessFlags    = CpuAccessFlags.None,
                Format            = Format.R32_Float,
                Width             = Width,
                Height            = Height,
                MipLevels         = 1,
                OptionFlags       = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = ResourceUsage.Immutable
            }, new DataRectangle(randbomBuffer.DataPointer, sizeof(float) * Width));
            var textureView = new ShaderResourceView(device, texture);

            var gpuProfiler = new GPUProfiler();

            gpuProfiler.Initialize(device);
            double elapsedTime = 0.0f;

            Console.WriteLine("Compiling Shaders...");
            Console.WriteLine();

            var pixelShaderMinMax = new MipMapMinMax();

            pixelShaderMinMax.Size = new Size(Width, Height);
            pixelShaderMinMax.Initialize(device);

            var testRunner = new Action <IMinMaxProcessor>((processor) =>
            {
                gpuProfiler.Begin(context);
                for (int i = 0; i < Count; i++)
                {
                    processor.Reduce(context, textureView);
                }
                gpuProfiler.End(context);

                context.Flush();
                float newMin;
                float newMax;
                processor.GetResults(context, out newMin, out newMax);

                elapsedTime = gpuProfiler.GetElapsedMilliseconds(context);
                Console.WriteLine("GPU {0}: {1} / {2} in {3}ms", processor, newMin, newMax, elapsedTime);
            });

            Console.WriteLine("Running Tests...");
            Console.WriteLine();

            var clock = new Stopwatch();
            var min   = float.MaxValue;
            var max   = float.MinValue;

            unsafe
            {
                var buffer = (float *)randbomBuffer.DataPointer;
                clock.Start();
                for (int j = 0; j < Count; j++)
                //for (int j = 0; j < 1; j++)
                {
                    min = float.MaxValue;
                    max = float.MinValue;
                    for (int i = 0; i < Width * Height; i++)
                    {
                        var value = buffer[i];
                        if (value < min)
                        {
                            min = value;
                        }
                        if (value > max)
                        {
                            max = value;
                        }
                    }
                }
                clock.Stop();
            }
            Console.WriteLine("CPU MinMax: {0} / {1} {2}ms", min, max, clock.ElapsedMilliseconds);


            Console.WriteLine();
            for (int i = 1; i < 4; i++)
            {
                pixelShaderMinMax.ReduceFactor = i;
                testRunner(pixelShaderMinMax);
            }

            Console.WriteLine();
        }
コード例 #2
0
        public void Run()
        {
            // --------------------------------------------------------------------------------------
            // Init Direct3D11
            // --------------------------------------------------------------------------------------

            // Create Device and SwapChain
            var device = new Device(DriverType.Hardware, DeviceCreationFlags.None);
            var context = device.ImmediateContext;

            const int Width = 1024;
            const int Height = 1024;
            const int Count = 1000;

            Console.WriteLine("Texture Size: ({0},{1}) - Count: {2}", Width, Height, Width * Height);
            Console.WriteLine();

            // Create random buffer
            var random = new Random();
            float maxScale = (float)((byte)random.Next());
            var randbomBuffer = new DataStream(sizeof(float) * Width * Height, true, true);
            for (int i = 0; i < Width * Height; i++)
            {
                var value = (float)random.NextDouble();
                if (value < 0.1 || value > 0.9)
                    value = value * (float)random.NextDouble() * maxScale * ((value < 0.1) ? maxScale : 1.0f);
                randbomBuffer.Write(value);
            }

            // Create random 2D texture
            var texture = new Texture2D(
                device,
                new Texture2DDescription
                {
                    ArraySize = 1,
                    BindFlags = BindFlags.ShaderResource,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Format = Format.R32_Float,
                    Width = Width,
                    Height = Height,
                    MipLevels = 1,
                    OptionFlags = ResourceOptionFlags.None,
                    SampleDescription = new SampleDescription(1, 0),
                    Usage = ResourceUsage.Immutable
                }, new DataRectangle(randbomBuffer.DataPointer, sizeof(float) * Width));
            var textureView = new ShaderResourceView(device, texture);

            var gpuProfiler = new GPUProfiler();
            gpuProfiler.Initialize(device);
            double elapsedTime = 0.0f;

            Console.WriteLine("Compiling Shaders...");
            Console.WriteLine();

            var pixelShaderMinMax = new MipMapMinMax();
            pixelShaderMinMax.Size = new Size(Width, Height);
            pixelShaderMinMax.Initialize(device);

            var testRunner = new Action<IMinMaxProcessor>( (processor) =>
                    {

                        gpuProfiler.Begin(context);
                        for (int i = 0; i < Count; i++)
                        {
                            processor.Reduce(context, textureView);
                        }
                        gpuProfiler.End(context);

                        context.Flush();
                        float newMin;
                        float newMax;
                        processor.GetResults(context, out newMin, out newMax);

                        elapsedTime = gpuProfiler.GetElapsedMilliseconds(context);
                        Console.WriteLine("GPU {0}: {1} / {2} in {3}ms", processor, newMin, newMax, elapsedTime);
                    });

            Console.WriteLine("Running Tests...");
            Console.WriteLine();

            var clock = new Stopwatch();
            var min = float.MaxValue;
            var max = float.MinValue;
            unsafe
            {
                var buffer = (float*)randbomBuffer.DataPointer;
                clock.Start();
                for (int j = 0; j < Count; j++)
                //for (int j = 0; j < 1; j++)
                {
                    min = float.MaxValue;
                    max = float.MinValue;
                    for (int i = 0; i < Width * Height; i++)
                    {
                        var value = buffer[i];
                        if (value < min) min = value;
                        if (value > max) max = value;
                    }
                }
                clock.Stop();
            }
            Console.WriteLine("CPU MinMax: {0} / {1} {2}ms", min, max, clock.ElapsedMilliseconds);

            Console.WriteLine();
            for (int i = 1; i < 4; i++)
            {
                pixelShaderMinMax.ReduceFactor = i;
                testRunner(pixelShaderMinMax);
            }

            Console.WriteLine();
        }