Example #1
0
        static void Ispc_SimpleExample()
        {
            //from: ispc-14-dev-windows\examples\simple
            string module = "simple";

            //TODO: check if we need to rebuild or not
            bool rebuild = NeedRebuildIspc(module);

            if (rebuild)
            {
                IspcBuilder ispcBuilder = new IspcBuilder();
                ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
                ispcBuilder.IspcFilename      = module + ".ispc";
                ispcBuilder.AutoCsTargetFile  = $"..\\..\\AutoGenBinders\\{module}.cs";
                ispcBuilder.RebuildLibraryAndAPI();
            }

            string dllName = module + ".dll";
            IntPtr dllPtr  = LoadLibrary(dllName);

            if (dllPtr == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }

            IntPtr funct = GetProcAddress(dllPtr, "my_simple"); //test with raw name

            if (funct == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }


            //test1
            int[] inputData = new int[]
            {
                1 << 16, 2 << 16, 3 << 16, 4 << 16,
                    5 << 16, 6 << 16, 7 << 16, 8 << 16,
                    9 << 16, 10 << 16, 11 << 16, 12 << 16,
            };
            //test2
            int[] outputData = new int[inputData.Length];
            unsafe
            {
                fixed(int *output_h = &outputData[0])
                fixed(int *h = &inputData[0])
                {
                    simple_ispc.NativeMethods.flipY_and_swap(h, output_h, 4, 3);
                }
            }

            unsafe
            {
                fixed(int *h = &inputData[0])
                {
                    simple_ispc.NativeMethods.clear(h, 0, inputData.Length);
                }
            }
        }
Example #2
0
        static void Ispc_SortExample()
        {
            //from: ispc-14-dev-windows\examples\sort

            string module = "sort";
            //TODO: check if we need to rebuild or not
            bool rebuild = NeedRebuildIspc(module);

            if (rebuild)
            {
                IspcBuilder ispcBuilder = new IspcBuilder();
                ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
                ispcBuilder.IspcFilename      = module + ".ispc";
                ispcBuilder.AutoCsTargetFile  = $"..\\..\\AutoGenBinders\\{module}.cs";

                string currentDir = Directory.GetCurrentDirectory();
                ispcBuilder.AdditionalInputItems = new string[]
                {
                    currentDir + "\\tasksys.cpp"
                };
                ispcBuilder.RebuildLibraryAndAPI();
            }

            string dllName = module + ".dll";
            IntPtr dllPtr  = LoadLibrary(dllName);

            if (dllPtr == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }


            int    m_round    = 20;
            int    elem_count = 10000;
            Random rand       = new Random(20);

            uint[] code = new uint[elem_count];
            for (int round = 0; round < m_round; round++)
            {
                for (int i = 0; i < elem_count; i++)
                {
                    code[i] = (uint)rand.Next(0, elem_count);
                }
                unsafe
                {
                    int[] ordered_output = new int[elem_count];
                    fixed(uint *code_ptr = &code[0])
                    fixed(int *ordered_output_ptr = &ordered_output[0])
                    {
                        sort_ispc.NativeMethods.sort_ispc(elem_count, code_ptr, ordered_output_ptr, 0);
                    }
                }
            }
        }
Example #3
0
        static void Ispc_MandlebrotTaskExample()
        {
            //from: ispc-14-dev-windows\examples\mandelbrot

            //TODO: check if we need to rebuild or not
            string module  = "mandelbrot_task";
            bool   rebuild = NeedRebuildIspc(module);

            if (rebuild)
            {
                IspcBuilder ispcBuilder = new IspcBuilder();
                ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
                ispcBuilder.IspcFilename      = module + ".ispc";
                ispcBuilder.AutoCsTargetFile  = $"..\\..\\AutoGenBinders\\{module}.cs";

                string currentDir = Directory.GetCurrentDirectory();
                ispcBuilder.AdditionalInputItems = new string[]
                {
                    currentDir + "\\tasksys.cpp"
                };
                ispcBuilder.RebuildLibraryAndAPI();
            }

            string dllName = module + ".dll";
            IntPtr dllPtr  = LoadLibrary(dllName);

            if (dllPtr == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }

            int width  = 768;
            int height = 512;
            //
            float x0 = -2;
            float x1 = 1;
            float y0 = -1;
            float y1 = 1;

            int maxIterations = 256;

            int[] buffer = new int[width * height];
            unsafe
            {
                fixed(int *output_h = &buffer[0])
                {
                    mandelbrot_task_ispc.NativeMethods.mandelbrot_ispc(x0, y0, x1, y1, width, height, maxIterations, output_h);
                }
            }

            SaveManelbrotImage(buffer, width, height, "test_mandelbrot_task.png");
        }
Example #4
0
        static void Ispc_DeferredShading()
        {
            //from ispc-14-dev-windows\examples\deferred\kernels.ispc

            //TODO: check if we need to rebuild or not
            string module  = "kernels";
            bool   rebuild = NeedRebuildIspc(module);

            if (rebuild)
            {
                IspcBuilder ispcBuilder = new IspcBuilder();
                ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
                ispcBuilder.IspcFilename      = module + ".ispc";
                ispcBuilder.AutoCsTargetFile  = $"..\\..\\AutoGenBinders\\{module}.cs";

                string currentDir = Directory.GetCurrentDirectory();
                ispcBuilder.AdditionalInputItems = new string[]
                {
                    currentDir + "\\tasksys.cpp",
                    currentDir + "\\deferred.h"
                };
                ispcBuilder.RebuildLibraryAndAPI();
            }

            string dllName = module + ".dll";
            IntPtr dllPtr  = LoadLibrary(dllName);

            if (dllPtr == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }

            unsafe
            {
                byte[] data_file = File.ReadAllBytes("Data/pp1280x720.bin");
                kernels_ispc.NativeMethods.InputDataArrays array = new kernels_ispc.NativeMethods.InputDataArrays();
                //TODO:
                //port ispc-14-dev-windows\examples\deferred\main.cpp
            }
        }
Example #5
0
        static void Ispc_AoBench()
        {
            string module  = "ao";
            bool   rebuild = NeedRebuildIspc(module);

            if (rebuild)
            {
                IspcBuilder ispcBuilder = new IspcBuilder();
                ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
                ispcBuilder.IspcFilename      = module + ".ispc";
                ispcBuilder.AutoCsTargetFile  = $"..\\..\\AutoGenBinders\\{module}.cs";

                string currentDir = Directory.GetCurrentDirectory();
                ispcBuilder.AdditionalInputItems = new string[]
                {
                    currentDir + "\\tasksys.cpp"
                };

                ispcBuilder.RebuildLibraryAndAPI();
            }

            string dllName = module + ".dll";
            IntPtr dllPtr  = LoadLibrary(dllName);

            if (dllPtr == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

            unsafe
            {
                sw.Reset();
                sw.Start();
                int     w         = 800;
                int     h         = 600;
                float[] imgBuffer = new float[w * h * 3];//3 channel
                fixed(float *img_h = &imgBuffer[0])
                {
                    ao_ispc.NativeMethods.ao_ispc(w, h, 2, img_h);
                }

                sw.Stop();
                System.Diagnostics.Debug.WriteLine("ao_ms:" + sw.ElapsedMilliseconds.ToString());

                //conver float[] to img
                ConvertToBitmapAndSave(imgBuffer, w, h, "ao1.png");
            }
            unsafe
            {
                sw.Reset();
                sw.Start();
                int     w         = 800;
                int     h         = 600;
                float[] imgBuffer = new float[w * h * 3];//3 channel
                fixed(float *img_h = &imgBuffer[0])
                {
                    ao_ispc.NativeMethods.ao_ispc_tasks(w, h, 2, img_h);
                }

                sw.Stop();
                System.Diagnostics.Debug.WriteLine("ao_task_ms:" + sw.ElapsedMilliseconds.ToString());
                //conver float[] to img
                ConvertToBitmapAndSave(imgBuffer, w, h, "ao2.png");
            }
        }
Example #6
0
        static void Ispc_TestCallback()
        {
            //read more about callback from ispc
            //on  https://ispc.github.io/ispc.html => section "Interoperability with The Application"

            string module  = "callback_test";
            bool   rebuild = NeedRebuildIspc(module);

            if (rebuild)
            {
                IspcBuilder ispcBuilder = new IspcBuilder();
                ispcBuilder.ProjectConfigKind = BridgeBuilder.Vcx.ProjectConfigKind.Debug;
                ispcBuilder.IspcFilename      = module + ".ispc";
                ispcBuilder.AutoCsTargetFile  = $"..\\..\\AutoGenBinders\\{module}.cs";

                string currentDir = Directory.GetCurrentDirectory();
                ispcBuilder.AdditionalInputItems = new string[]
                {
                    currentDir + "\\callback_test1.cpp"
                };
                ispcBuilder.RebuildLibraryAndAPI();
            }

            string dllName = module + ".dll";
            IntPtr dllPtr  = LoadLibrary(dllName);

            if (dllPtr == IntPtr.Zero)
            {
                throw new NotSupportedException();
            }

            GetManagedDelegate(dllPtr, "set_managed_callback", out s_setManagedCallback);
            if (s_setManagedCallback == null)
            {
                throw new NotSupportedException();
            }

            //-----------
            m_callback = (int a) =>
            {
#if DEBUG
                //System.Diagnostics.Debugger.Break();
                System.Diagnostics.Debug.WriteLine("callback from ispc");
#endif
            };
            m_callback_ptr = Marshal.GetFunctionPointerForDelegate(m_callback);
            s_setManagedCallback(m_callback_ptr);

            //-----------
            //test call to ispc
            //test1
            int[] inputData = new int[]
            {
                1 << 16, 2 << 16, 3 << 16, 4 << 16,
                    5 << 16, 6 << 16, 7 << 16, 8 << 16,
                    9 << 16, 10 << 16, 11 << 16, 12 << 16,
            };
            unsafe
            {
                fixed(int *h = &inputData[0])
                {
                    callback_test_ispc.NativeMethods.clear(h, 0, inputData.Length);
                }
            }
        }
Example #7
0
 static bool NeedRebuildIspc(string moduleName) => IspcBuilder.NeedRebuildIspc(moduleName);
Example #8
0
        static void dbugParseHeader(string filename)
        {
            IspcBuilder builder = new IspcBuilder();

            builder.ParseAutoGenHeaderFromFile(filename);
        }