Exemplo n.º 1
0
        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Form.FormClosing" /> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.FormClosingEventArgs" /> that contains the event data.</param>
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);

            GorgonApplication.IdleMethod = null;

            _sobel?.Dispose();
            _sobelShader?.Dispose();
            _outputTexture?.Dispose();
            _sourceTexture?.Texture?.Dispose();
            _renderer?.Dispose();
            _graphics?.Dispose();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Defines the entry point of the application.
        /// </summary>
        private static void Main()
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("For this example, we'll upload two sets of values to the GPU and the compute engine will perform a simple mathematical");
                Console.WriteLine("operation on each sets of values to produce another set of values stored as a structured buffer.\n");

                Console.WriteLine("To do this, we first create a graphics interface so we can access the GPU from the compute engine, then we create the");
                Console.WriteLine("compute engine itself and bind the buffers containing the input values and the output buffer. Then, finally, we execute");
                Console.WriteLine("the compute shader and retrieve the result values from the output buffer into a buffer that the CPU can read and test");
                Console.WriteLine("them for accuracy.\n");
                Console.ResetColor();

                // Initialize the required objects.
                Console.WriteLine("Initializing...");
                Initialize();

                // Initialize our input and output data structures.
                Console.WriteLine("Creating data...");
                BuildInputOutputBuffers();

                // Now create the engine and execute the shader.
                Console.WriteLine("Executing...");
                OutputData[] results = Execute();

                // Now test the results to ensure we the engine did what we wanted.
                IReadOnlyList <(int Index, OutputData Expected, OutputData Actual)> errors = TestResults(results);

                if (errors.Count != 0)
                {
                    foreach ((int Index, OutputData Expected, OutputData Actual) in errors)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"Failure at element {Index}.");

                        if (Expected.Sum != Actual.Sum)
                        {
                            Console.WriteLine($"Sum should be: {Expected.Sum}, Got: {Actual.Sum}");
                        }

                        if (!Expected.Product.EqualsEpsilon(Actual.Product))
                        {
                            Console.WriteLine($"Product should be: {Expected.Product}, Got: {Actual.Product}");
                        }

                        Console.ResetColor();
                    }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Success. All compute engine output values match expected values.");
                    Console.ResetColor();
                }
            }
            catch (Exception ex)
            {
                // And, of course, if we have errors, then we should obviously handle them.
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine($"There was an error running the application:\n{ex.Message}\n\nStack trace:\n{ex.StackTrace}");
                Console.ResetColor();
            }
            finally
            {
                _floatBuffer?.Dispose();
                _intBuffer?.Dispose();
                _outputBuffer?.Dispose();
                _computeShader?.Dispose();
                _graphics?.Dispose();
            }

            // Wait for the user to exit.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }