Exemplo n.º 1
0
        public void CreateImageTests()
        {
            ErrorCode error;

            if (Cl.GetDeviceInfo(_device, DeviceInfo.ImageSupport, out error).CastTo <Bool>() == Bool.False)
            {
                Console.WriteLine("No image support");
                return;
            }

            {
                var  image2DData = new float[200 * 200 * sizeof(float)];
                IMem image2D     = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, new ImageFormat(ChannelOrder.RGBA, ChannelType.Float),
                                                    (IntPtr)200, (IntPtr)200, (IntPtr)0, image2DData, out error);
                Assert.AreEqual(error, ErrorCode.Success);

                Assert.AreEqual(Cl.GetImageInfo(image2D, ImageInfo.Width, out error).CastTo <uint>(), 200);
                Assert.AreEqual(Cl.GetImageInfo(image2D, ImageInfo.Height, out error).CastTo <uint>(), 200);

                image2D.Dispose();
            }

            {
                var  image3DData = new float[200 * 200 * 200 * sizeof(float)];
                IMem image3D     = Cl.CreateImage3D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, new ImageFormat(ChannelOrder.RGBA, ChannelType.Float),
                                                    (IntPtr)200, (IntPtr)200, (IntPtr)200, IntPtr.Zero, IntPtr.Zero, image3DData, out error);
                Assert.AreEqual(error, ErrorCode.Success);

                Assert.AreEqual(Cl.GetImageInfo(image3D, ImageInfo.Width, out error).CastTo <uint>(), 200);
                Assert.AreEqual(Cl.GetImageInfo(image3D, ImageInfo.Height, out error).CastTo <uint>(), 200);
                Assert.AreEqual(Cl.GetImageInfo(image3D, ImageInfo.Depth, out error).CastTo <uint>(), 200);

                image3D.Dispose();
            }
        }
Exemplo n.º 2
0
        public Image2D(ComputeProvider provider, Operations operations, bool hostAccessible, int width, int height, int rowPitch = -1) // Create, no data
        {
            ErrorCode error;

            _image = Cl.CreateImage2D(provider.Context, (MemFlags)operations | (hostAccessible ? MemFlags.AllocHostPtr : 0),
                                      new ImageFormat(_imageFormat.ChannelOrder, _imageFormat.ChannelType.ChannelType), (IntPtr)width, (IntPtr)height,
                                      rowPitch == -1 ? (IntPtr)(width * _imageFormat.ComponentCount * _imageFormat.ChannelType.Size) : (IntPtr)rowPitch,
                                      null, out error);

            if (error != ErrorCode.Success)
            {
                throw new CLException(error);
            }

            _width    = width;
            _height   = height;
            _rowPitch = rowPitch;
            if ((MemFlags)operations == MemFlags.ReadOnly)
            {
                _modifier = true;
            }
            else
            {
                _modifier = false;
            }
        }
Exemplo n.º 3
0
        public Image2D(ComputeProvider provider, Operations operations, Memory memory, int width, int height, T[] data, int rowPitch = -1) // Create and copy/use data from host
        {
            ErrorCode error;

            _image = Cl.CreateImage2D(provider.Context, (MemFlags)operations | (memory == Memory.Host ? MemFlags.UseHostPtr : (MemFlags)memory | MemFlags.CopyHostPtr),
                                      new ImageFormat(_imageFormat.ChannelOrder, _imageFormat.ChannelType.ChannelType), (IntPtr)width, (IntPtr)height,
                                      rowPitch == -1 ? (IntPtr)(width * _imageFormat.ComponentCount * _imageFormat.ChannelType.Size) : (IntPtr)rowPitch,
                                      data, out error);

            if (error != ErrorCode.Success)
            {
                throw new CLException(error);
            }

            _width    = width;
            _height   = height;
            _rowPitch = rowPitch;
            if ((MemFlags)operations == MemFlags.ReadOnly)
            {
                _modifier = true;
            }
            else
            {
                _modifier = false;
            }
        }
Exemplo n.º 4
0
        public void singlePass(Kernel kernel, FloatMap inMap, FloatMap outMap)
        {
            var clInImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.Luminance, ChannelType.Float);

            IMem inputMapBuffer = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, clInImageFormat,
                                                   (IntPtr)inMap.W, (IntPtr)inMap.H, new IntPtr(inMap.Stride * sizeof(float)),
                                                   inMap._buf, out err);

            assert(err, "input img creation");


            IMem outputMapBuffer = Cl.CreateImage2D(_context, MemFlags.WriteOnly, clInImageFormat,
                                                    (IntPtr)outMap.W, (IntPtr)outMap.H, new IntPtr(outMap.Stride * sizeof(float)),
                                                    outMap._buf, out err);

            assert(err, "output img creation");


            // Set memory objects as parameters to kernel
            err = Cl.SetKernelArg(kernel, 0, intPtrSize, inputMapBuffer);
            assert(err, "input map setKernelArg");

            err = Cl.SetKernelArg(kernel, 1, intPtrSize, outputMapBuffer);
            assert(err, "output map setKernelArg");


            // write actual data into memory object
            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };               //x, y, z
            IntPtr[] inRegionPtr      = new IntPtr[] { (IntPtr)inMap.W, (IntPtr)inMap.H, (IntPtr)1 };   //x, y, z
            IntPtr[] outRegionPtr     = new IntPtr[] { (IntPtr)outMap.W, (IntPtr)outMap.H, (IntPtr)1 }; //x, y, z
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)outMap.W, (IntPtr)outMap.H, (IntPtr)1 };
            Event    clevent;

            //err = Cl.EnqueueWriteImage(_commandsQueue, inputMapBuffer, Bool.True, originPtr, inRegionPtr, (IntPtr)0, (IntPtr)0, inMap._buf, 0, null, out clevent);
            //clevent.Dispose();
            //assert(err, "write input img");

            // execute
            err = Cl.EnqueueNDRangeKernel(_commandsQueue, kernel, 2,
                                          originPtr,
                                          workGroupSizePtr,
                                          null, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "Cl.EnqueueNDRangeKernel");

            // sync
            Cl.Finish(_commandsQueue);

            // read from output memory object into actual buffer
            err = Cl.EnqueueReadImage(_commandsQueue, outputMapBuffer, Bool.True, originPtr, outRegionPtr, new IntPtr(outMap.Stride * sizeof(float)), (IntPtr)0, outMap._buf, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "read output buffer");

            Cl.ReleaseMemObject(inputMapBuffer);
            Cl.ReleaseMemObject(outputMapBuffer);
        }
Exemplo n.º 5
0
 public KernelImage(int index, MemFlags flags, byte[] bytes, int width, int height, ImageFormat pixelFormat)
     : this(index, flags, width, height, pixelFormat)
 {
     Source    = bytes;
     GpuBuffer = Cl.CreateImage2D(EasyClCompiler.Context, (OpenCL.Net.MemFlags)Flags, ImageFormat, (IntPtr)Width, (IntPtr)Height, (IntPtr)0, Source, out ErrorCode error);
     if (error != ErrorCode.Success)
     {
         throw new GPUException("SetImage2D", error.ToString());
     }
 }
Exemplo n.º 6
0
        public static ComputeImage2D CreateImage2D(int w, int h, bool write, bool read)
        {
            ImageFormat fmt = new ImageFormat(ChannelOrder.RGB, ChannelType.Unsigned_Int8);
            var         img = Cl.CreateImage2D(_context, (write && read ? MemFlags.ReadWrite : 0) | (write && !read ? MemFlags.WriteOnly : 0) | (!write && read ? MemFlags.ReadOnly : 0), fmt, (IntPtr)w, (IntPtr)h, IntPtr.Zero, null, out var err);

            ComputeImage2D cimg = new ComputeImage2D()
            {
                img    = img,
                Width  = w,
                Height = h
            };

            return(cimg);
        }
Exemplo n.º 7
0
        public KernelImage(int index, MemFlags flags, Bitmap bmp) : this(index, flags, bmp.Width, bmp.Height, bmp.PixelFormat)
        {
            Rectangle bmpRect = new Rectangle(Point.Empty, bmp.Size);

            SystemGDI.BitmapData bmpData = bmp.LockBits(bmpRect, SystemGDI.ImageLockMode.ReadWrite, bmp.PixelFormat);
            IntPtr bmpPtr    = bmpData.Scan0;
            int    byteCount = bmpData.Stride * Height;

            Source = new byte[byteCount];
            Marshal.Copy(bmpPtr, Source, 0, byteCount);
            GpuBuffer = Cl.CreateImage2D(EasyClCompiler.Context, (OpenCL.Net.MemFlags)flags, ImageFormat, (IntPtr)Width, (IntPtr)Height, (IntPtr)0, Source, out ErrorCode error);
            if (error != ErrorCode.Success)
            {
                throw new GPUException("SetImage2D", error.ToString());
            }
            bmp.UnlockBits(bmpData);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Writes an image to GPU memeory
        /// </summary>
        /// <param name="image">the image to write</param>
        /// <param name="memFlags">the memory flags</param>
        public void WriteImage(Bitmap image, MemFlags memFlags)
        {
            ErrorCode error;

            _image = Cl.CreateImage2D(_handle.Context, memFlags, new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unorm_Int8),
                                      new IntPtr(image.Width), new IntPtr(image.Height), new IntPtr(0), new IntPtr(0), out error);
            CLException.CheckException(error);

            byte[]   data        = BmpToBytes(image);
            IntPtr[] originArray = new IntPtr[] { new IntPtr(0), new IntPtr(0), new IntPtr(0) };
            IntPtr[] sizeArray   = new IntPtr[] { new IntPtr(image.Width), new IntPtr(image.Height), new IntPtr(1) };

            Event e;

            error = Cl.EnqueueWriteImage(_handle.Queue, _image, Bool.True,
                                         originArray, sizeArray, new IntPtr(0), new IntPtr(0),
                                         data, 0, null, out e);
            CLException.CheckException(error);
        }
        public override Bitmap Plot()
        {
            ErrorCode error;

            using (Kernel kernel = CompileKernel("mandelbrot"))
            {
                Bitmap plotImg = new Bitmap(Width, Height);

                int intPtrSize = Marshal.SizeOf(typeof(IntPtr));
                int uint4size  = Marshal.SizeOf(typeof(uint4));

                // Buffer do OpenCL para manter os dados da imagem
                OpenCL.Net.ImageFormat clImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unsigned_Int8);

                //// Obtém o buffer de pixels
                //BitmapData data = plotImg.LockBits(new Rectangle(0, 0, plotImg.Width, plotImg.Height), ImageLockMode.WriteOnly, plotImg.PixelFormat);
                //int depth = Bitmap.GetPixelFormatSize(data.PixelFormat) / 8; // Tamanho de cada pixel em memória, em bytes

                int    depth  = Bitmap.GetPixelFormatSize(PixelFormat.Format32bppArgb) / 8;
                int    stride = 4 * ((Width * depth + 3) / 4);
                byte[] buffer = new byte[Height * stride]; // Cria o buffer para se trabalhar na imagem
                //Marshal.Copy(data.Scan0, buffer, 0, buffer.Length); // Copia as informações da imagem no buffer

                // Cria o buffer do OpenCL para a imagem
                Mem image2dbuffer = (Mem)Cl.CreateImage2D(context, MemFlags.CopyHostPtr | MemFlags.WriteOnly, clImageFormat,
                                                          (IntPtr)Width, (IntPtr)Height,
                                                          (IntPtr)0, buffer, out error);
                CheckErr(error, "Cl.CreateImage2D");

                // Passa os parametros para o kernel
                error = Cl.SetKernelArg(kernel, 0, (IntPtr)intPtrSize, image2dbuffer);
                CheckErr(error, "Cl.SetKernelArg imageBuffer");

                uint4 startColorUi = new uint4(Colors.StartColor.B, Colors.StartColor.G, Colors.StartColor.R, Colors.StartColor.A);
                error = Cl.SetKernelArg(kernel, 1, (IntPtr)uint4size, startColorUi);
                CheckErr(error, "Cl.SetKernelArg startColor");

                uint4 endColorUi = new uint4(Colors.EndColor.B, Colors.EndColor.G, Colors.EndColor.R, Colors.EndColor.A);
                error = Cl.SetKernelArg(kernel, 2, (IntPtr)uint4size, endColorUi);
                CheckErr(error, "Cl.SetKernelArg endColor");

                error = Cl.SetKernelArg(kernel, 3, (IntPtr)sizeof(int), Iterations);
                CheckErr(error, "Cl.SetKernelArg iterations");


                // Cria uma fila de comandos, com todos os comandos a serem executados pelo kernel
                CommandQueue cmdQueue = Cl.CreateCommandQueue(context, device, 0, out error);
                CheckErr(error, "Cl.CreateCommandQueue");

                // Copia a imagem para a GPU
                Event    clevent;
                IntPtr[] imgOriginPtr = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };          //x, y, z
                IntPtr[] imgRegionPtr = new IntPtr[] { (IntPtr)Width, (IntPtr)Height, (IntPtr)1 }; //x, y, z
                error = Cl.EnqueueWriteImage(cmdQueue, image2dbuffer, Bool.True, imgOriginPtr, imgRegionPtr, (IntPtr)0, (IntPtr)0, buffer, 0, null, out clevent);
                CheckErr(error, "Cl.EnqueueWriteImage");

                // Executa o Kernel carregado pelo OpenCL (com múltiplos processadores :D)
                IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)Width, (IntPtr)Height, (IntPtr)1 }; // x, y, z
                error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
                CheckErr(error, "Cl.EnqueueNDRangeKernel");

                // Espera terminar a execução
                error = Cl.Finish(cmdQueue);
                CheckErr(error, "Cl.Finish");

                // Lê a imagem processada pela GPU e coloca novamente no buffer
                error = Cl.EnqueueReadImage(cmdQueue, image2dbuffer, Bool.True, imgOriginPtr, imgRegionPtr,
                                            (IntPtr)0, (IntPtr)0, buffer, 0, null, out clevent);
                CheckErr(error, "Cl.clEnqueueReadImage");

                // Limpa a memória
                Cl.ReleaseKernel(kernel);
                Cl.ReleaseCommandQueue(cmdQueue);
                Cl.ReleaseMemObject(image2dbuffer);

                // Get a pointer to our unmanaged output byte[] array
                //GCHandle pinnedBuffer = GCHandle.Alloc(buffer, GCHandleType.Pinned);
                //IntPtr bmpPointer = pinnedBuffer.AddrOfPinnedObject();

                BitmapData data = plotImg.LockBits(new Rectangle(0, 0, plotImg.Width, plotImg.Height), ImageLockMode.WriteOnly, plotImg.PixelFormat);
                Marshal.Copy(buffer, 0, data.Scan0, buffer.Length); // Copia as informações no buffer de volta à imagem
                plotImg.UnlockBits(data);                           // Libera a imagem

                //pinnedBuffer.Free();

                return(plotImg);
            }
        }
Exemplo n.º 10
0
        public ClImage2D(Bitmap bitmap, Context context, IOMode iOMode, ChannelType channelType, out ErrorCode error)
        {
            switch (channelType)
            {
            case ChannelType.RGBA32bpp:
                clImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, Net.ChannelType.Unsigned_Int8);
                pixelFormat   = PixelFormat.Format32bppArgb;
                break;

            case ChannelType.Single8bpp:
                clImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.R, Net.ChannelType.Unsigned_Int8);
                pixelFormat   = PixelFormat.Format8bppIndexed;
                break;
            }

            ready = false;

            original    = bitmap;
            this.Width  = bitmap.Width;
            this.Height = bitmap.Height;
            this.iOMode = iOMode;

            //Lock bitmap
            switch (iOMode)
            {
            case IOMode.ReadOnly:
                bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height),
                                             ImageLockMode.ReadOnly, pixelFormat);
                break;

            case IOMode.WriteOnly:
                bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height),
                                             ImageLockMode.WriteOnly, pixelFormat);
                break;

            case IOMode.ReadWrite:
                bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height),
                                             ImageLockMode.ReadWrite, pixelFormat);
                break;
            }

            //Get image properties
            this.Stride = bitmapData.Stride;
            this.Size   = Stride * Height;

            //Crate cl memory object
            switch (iOMode)
            {
            default:
            case IOMode.ReadOnly:
                clMem = Cl.CreateImage2D(context, MemFlags.UseHostPtr | MemFlags.ReadOnly, clImageFormat,
                                         (IntPtr)Width, (IntPtr)Height, (IntPtr)0,
                                         bitmapData.Scan0, out error);
                break;

            case IOMode.WriteOnly:
                clMem = Cl.CreateImage2D(context, MemFlags.AllocHostPtr | MemFlags.WriteOnly, clImageFormat,
                                         (IntPtr)Width, (IntPtr)Height, (IntPtr)0,
                                         null, out error);
                break;

            case IOMode.ReadWrite:
                clMem = Cl.CreateImage2D(context, MemFlags.UseHostPtr | MemFlags.ReadWrite, clImageFormat,
                                         (IntPtr)Width, (IntPtr)Height, (IntPtr)0,
                                         bitmapData.Scan0, out error);
                break;
            }

            //Check error
            if (error == ErrorCode.Success)
            {
                ready = true;
            }
            else
            {
                UnlockBits();
            }
        }
Exemplo n.º 11
0
        public HTTPResponse GetResponse(HTTPRequest request)
        {
            HTTPResponse  response = new HTTPResponse(200);
            StringBuilder sb       = new StringBuilder();
            ErrorCode     error;

            if (!_isInit)
            {
                init();
                _isInit = true;
            }

            if (request.Method == HTTPRequest.METHOD_GET)
            {
                // Input form, this can be place by any HTML page
                sb.Append("<html><body>");
                sb.Append(GenUploadForm());
                sb.Append("</body></html>");
                response.Body = Encoding.UTF8.GetBytes(sb.ToString());
                return(response);
            }
            else if (request.Method == HTTPRequest.METHOD_POST)
            {
                // Get remote image from URL
                string url = Uri.UnescapeDataString(request.GetRequestByKey("imageUploadUrl"));
                byte[] data;
                try {
                    data = DownloadImageFromUrl(url);
                } catch (Exception) {
                    return(new HTTPResponse(400));
                }
                // https://www.codeproject.com/Articles/502829/GPGPU-image-processing-basics-using-OpenCL-NET
                // Convert image to bitmap binary
                Image inputImage = Image.FromStream(new MemoryStream(data));
                if (inputImage == null)
                {
                    return(new HTTPResponse(500));
                }
                int imagewidth  = inputImage.Width;
                int imageHeight = inputImage.Height;

                Bitmap     bmpImage           = new Bitmap(inputImage);
                BitmapData bitmapData         = bmpImage.LockBits(new Rectangle(0, 0, bmpImage.Width, bmpImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
                int        inputImageByteSize = bitmapData.Stride * bitmapData.Height;
                byte[]     inputByteArray     = new byte[inputImageByteSize];
                Marshal.Copy(bitmapData.Scan0, inputByteArray, 0, inputImageByteSize);

                // Load kernel source code
                string programPath = System.Environment.CurrentDirectory + "/Kernel.cl";
                if (!System.IO.File.Exists(programPath))
                {
                    return(new HTTPResponse(404));
                }

                string programSource = System.IO.File.ReadAllText(programPath);
                using (OpenCL.Net.Program program = Cl.CreateProgramWithSource(_context, 1, new[] { programSource }, null, out error)) {
                    // Create kernel
                    LogError(error, "Cl.CreateProgramWithSource");
                    error = Cl.BuildProgram(program, 1, new[] { _device }, string.Empty, null, IntPtr.Zero);
                    LogError(error, "Cl.BuildProgram");
                    if (Cl.GetProgramBuildInfo(program, _device, ProgramBuildInfo.Status, out error).CastTo <OpenCL.Net.BuildStatus>()
                        != BuildStatus.Success)
                    {
                        LogError(error, "Cl.GetProgramBuildInfo");
                        return(new HTTPResponse(404));
                    }
                    Kernel kernel = Cl.CreateKernel(program, _parameters["KernelFunction"], out error);
                    LogError(error, "Cl.CreateKernel");

                    // Create image memory objects
                    OpenCL.Net.ImageFormat clImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unsigned_Int8);
                    IMem inputImage2DBuffer = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly,
                                                               clImageFormat, (IntPtr)bitmapData.Width, (IntPtr)bitmapData.Height,
                                                               (IntPtr)0, inputByteArray, out error);
                    LogError(error, "CreateImage2D input");
                    byte[] outputByteArray     = new byte[inputImageByteSize];
                    IMem   outputImage2DBuffer = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.WriteOnly,
                                                                  clImageFormat, (IntPtr)bitmapData.Width, (IntPtr)bitmapData.Height,
                                                                  (IntPtr)0, outputByteArray, out error);
                    LogError(error, "CreateImage2D output");

                    // Set arguments
                    int IntPtrSize = Marshal.SizeOf(typeof(IntPtr));
                    error  = Cl.SetKernelArg(kernel, 0, (IntPtr)IntPtrSize, inputImage2DBuffer);
                    error |= Cl.SetKernelArg(kernel, 1, (IntPtr)IntPtrSize, outputImage2DBuffer);
                    LogError(error, "Cl.SetKernelArg");

                    // Create command queue
                    CommandQueue cmdQueue = Cl.CreateCommandQueue(_context, _device, (CommandQueueProperties)0, out error);
                    LogError(error, "Cl.CreateCommandQueue");
                    Event clevent;

                    // Copy input image from the host to the GPU
                    IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
                    IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)imagewidth, (IntPtr)imageHeight, (IntPtr)1 };
                    IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)imagewidth, (IntPtr)imageHeight, (IntPtr)1 };
                    error = Cl.EnqueueWriteImage(cmdQueue, inputImage2DBuffer, Bool.True, originPtr, regionPtr, (IntPtr)0,
                                                 (IntPtr)0, inputByteArray, 0, null, out clevent);
                    LogError(error, "Cl.EnqueueWriteImage");

                    // Run the kernel
                    error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
                    LogError(error, "Cl.EnqueueNDRangeKernel");

                    // Wait for finish event
                    error = Cl.Finish(cmdQueue);
                    LogError(error, "Cl.Finish");

                    // Read the output image back from GPU
                    error = Cl.EnqueueReadImage(cmdQueue, outputImage2DBuffer, Bool.True, originPtr, regionPtr, (IntPtr)0,
                                                (IntPtr)0, outputByteArray, 0, null, out clevent);
                    LogError(error, "Cl.EnqueueReadImage");
                    error = Cl.Finish(cmdQueue);
                    LogError(error, "Cl.Finih");

                    // Release memory
                    Cl.ReleaseKernel(kernel);
                    Cl.ReleaseCommandQueue(cmdQueue);
                    Cl.ReleaseMemObject(inputImage2DBuffer);
                    Cl.ReleaseMemObject(outputImage2DBuffer);

                    // Convert binary bitmap to JPEG image and return as response
                    GCHandle     pinnedOutputArray = GCHandle.Alloc(outputByteArray, GCHandleType.Pinned);
                    IntPtr       outputBmpPointer  = pinnedOutputArray.AddrOfPinnedObject();
                    Bitmap       outputBitmap      = new Bitmap(imagewidth, imageHeight, bitmapData.Stride, PixelFormat.Format32bppArgb, outputBmpPointer);
                    MemoryStream msOutput          = new MemoryStream();
                    outputBitmap.Save(msOutput, System.Drawing.Imaging.ImageFormat.Jpeg);
                    response.Body = msOutput.ToArray();
                    response.Type = "image/jpeg";
                    return(response);
                }
            }
            return(new HTTPResponse(501));
        }
Exemplo n.º 12
0
        public static Image CreateImage2D(Context context, MemoryFlags flags,
                                          ChannelOrder order, ChannelType type, ulong width, ulong height,
                                          ulong pitch, IntPtr hostPtr)
        {
            if (context == Context.Null)
            {
                throw new ArgumentNullException("context");
            }

            if (flags.HasFlag(MemoryFlags.WriteOnly) & flags.HasFlag(MemoryFlags.ReadOnly))
            {
                throw new ArgumentException("MemoryFlags.WriteOnly and MemoryFlags.ReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostReadOnly))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostReadOnly are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostWriteOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostWriteOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }
            if (flags.HasFlag(MemoryFlags.HostReadOnly) & flags.HasFlag(MemoryFlags.HostNoAccess))
            {
                throw new ArgumentException("MemoryFlags.HostReadOnly and MemoryFlags.HostNoAccess are mutually exclusive.");
            }

            if (width == 0)
            {
                throw new ArgumentOutOfRangeException("width", width, "width is 0.");
            }
            if (height == 0)
            {
                throw new ArgumentOutOfRangeException("height", height, "height is 0.");
            }

            if (hostPtr == IntPtr.Zero)
            {
                if (flags.HasFlag(MemoryFlags.UseHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr is not valid.");
                }
                if (flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.CopyHostPtr is not valid.");
                }

                if (pitch != 0)
                {
                    throw new ArgumentOutOfRangeException("pitch", pitch, "pitch is not 0.");
                }
            }
            else
            {
                if (!flags.HasFlag(MemoryFlags.UseHostPtr) & !flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr or MemoryFlags.CopyHostPtr is required.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.CopyHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.CopyHostPtr are mutually exclusive.");
                }
                if (flags.HasFlag(MemoryFlags.UseHostPtr) & flags.HasFlag(MemoryFlags.AllocHostPtr))
                {
                    throw new ArgumentException("MemoryFlags.UseHostPtr and MemoryFlags.AllocHostPtr are mutually exclusive.");
                }


                if (pitch != 0 && pitch < width)
                {
                    throw new ArgumentOutOfRangeException("pitch", pitch, "pitch is not 0 and is less than width.");
                }
            }

            unsafe
            {
                Cl.image_format image_format = new Cl.image_format()
                {
                    image_channel_order     = (uint)order,
                    image_channel_data_type = (uint)type,
                };

                int error;
                var handle = Cl.CreateImage2D(
                    context.Handle, (ulong)flags,
                    &image_format,
                    new UIntPtr(width), new UIntPtr(height),
                    new UIntPtr(pitch), hostPtr.ToPointer(), &error);
                ClHelper.GetError(error);

                return(new Image(handle));
            }
        }
Exemplo n.º 13
0
        public float[][] calc_grad_rho_c(SimpleImage I0, SimpleImage I1d, FlowArray Flow)

        {
            float[][] arrays = new float[4][];

            arrays[0] = new float[I0.ImageHeight * I0.ImageWidth];
            arrays[1] = new float[I0.ImageHeight * I0.ImageWidth];
            arrays[2] = new float[I0.ImageHeight * I0.ImageWidth];
            arrays[3] = new float[I0.ImageHeight * I0.ImageWidth];


            Mem leftImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)I0.ImageWidth, (IntPtr)I0.ImageHeight, (IntPtr)0, I0.ByteArray, out error);

            Mem rightImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)I1d.ImageWidth, (IntPtr)I1d.ImageHeight, (IntPtr)0, I1d.ByteArray, out error);

            IMem <float> uInputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.ReadOnly, Flow.Width * Flow.Height * sizeof(float), out error);

            IMem <float> vInputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.ReadOnly, Flow.Width * Flow.Height * sizeof(float), out error);

            IMem <float> gradXBuf  = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, Flow.Height * Flow.Width * sizeof(float), out error);
            IMem <float> gradYBuf  = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, Flow.Height * Flow.Width * sizeof(float), out error);
            IMem <float> grad_2Buf = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, Flow.Height * Flow.Width * sizeof(float), out error);
            IMem <float> rho_cBuf  = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, Flow.Height * Flow.Width * sizeof(float), out error);

            Kernel _Kernel = Cl.CreateKernel(program, "gradRho", out error);

            error |= Cl.SetKernelArg(_Kernel, 0, leftImageMemObject);
            error |= Cl.SetKernelArg(_Kernel, 1, rightImageMemObject);
            error |= Cl.SetKernelArg <float>(_Kernel, 2, uInputFlowMemObject);
            error |= Cl.SetKernelArg <float>(_Kernel, 3, vInputFlowMemObject);
            error |= Cl.SetKernelArg <float>(_Kernel, 4, gradXBuf);
            error |= Cl.SetKernelArg <float>(_Kernel, 5, gradYBuf);
            error |= Cl.SetKernelArg <float>(_Kernel, 6, grad_2Buf);
            error |= Cl.SetKernelArg <float>(_Kernel, 7, rho_cBuf);
            error |= Cl.SetKernelArg(_Kernel, 8, I0.ImageWidth);
            error |= Cl.SetKernelArg(_Kernel, 9, I0.ImageHeight);

            Event _event;

            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
            IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)I0.ImageWidth, (IntPtr)I0.ImageHeight, (IntPtr)1 };
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)(Flow.Height * Flow.Width) };


            error = Cl.EnqueueWriteImage(commandQueue, leftImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, I0.ByteArray, 0, null, out _event);

            error = Cl.EnqueueWriteImage(commandQueue, rightImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, I1d.ByteArray, 0, null, out _event);



            error = Cl.EnqueueWriteBuffer <float>(commandQueue, uInputFlowMemObject, Bool.True, Flow.Array[0], 0, null, out _event);

            error = Cl.EnqueueWriteBuffer <float>(commandQueue, vInputFlowMemObject, Bool.True, Flow.Array[1], 0, null, out _event);

            error = Cl.EnqueueNDRangeKernel(commandQueue, _Kernel, 1, null, workGroupSizePtr, null, 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, gradXBuf, Bool.True, 0, (Flow.Width * Flow.Height), arrays[0], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, gradYBuf, Bool.True, 0, (Flow.Width * Flow.Height), arrays[1], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, grad_2Buf, Bool.True, 0, (Flow.Width * Flow.Height), arrays[2], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, rho_cBuf, Bool.True, 0, (Flow.Width * Flow.Height), arrays[3], 0, null, out _event);
            error = Cl.Finish(commandQueue);


            Cl.ReleaseMemObject(uInputFlowMemObject);
            Cl.ReleaseMemObject(vInputFlowMemObject);
            Cl.ReleaseMemObject(leftImageMemObject);
            Cl.ReleaseMemObject(rightImageMemObject);
            Cl.ReleaseMemObject(gradXBuf);
            Cl.ReleaseMemObject(gradYBuf);
            Cl.ReleaseMemObject(grad_2Buf);
            Cl.ReleaseMemObject(rho_cBuf);
            Cl.ReleaseKernel(_Kernel);


            return(arrays);
        }
Exemplo n.º 14
0
    public Bitmap GPUMandel(Bitmap inputBitmap)
    {
        ErrorCode error;
        //Load and compile kernel source code.
        string programPath = System.Environment.CurrentDirectory + "/../../program.cl";

        //The path to the source file may vary

        if (!System.IO.File.Exists(programPath))
        {
            Console.WriteLine("Program doesn't exist at path " + programPath);
            return(inputBitmap);
        }

        string programSource = System.IO.File.ReadAllText(programPath);

        using (Program program = Cl.CreateProgramWithSource(_context, 1, new[] { programSource }, null, out error))
        {
            CheckErr(error, "Cl.CreateProgramWithSource");
            //Compile kernel source
            error = Cl.BuildProgram(program, 1, new[] { _device }, string.Empty, null, IntPtr.Zero);
            CheckErr(error, "Cl.BuildProgram");
            //Check for any compilation errors
            if (Cl.GetProgramBuildInfo(program, _device, ProgramBuildInfo.Status, out error).CastTo <BuildStatus>()
                != BuildStatus.Success)
            {
                CheckErr(error, "Cl.GetProgramBuildInfo");
                Console.WriteLine("Cl.GetProgramBuildInfo != Success");
                Console.WriteLine(Cl.GetProgramBuildInfo(program, _device, ProgramBuildInfo.Log, out error));
                return(inputBitmap);
            }
            //Create the required kernel (entry function)
            Kernel kernel = Cl.CreateKernel(program, "Mandelbrot", out error);
            CheckErr(error, "Cl.CreateKernel");


            //Unmanaged output image's raw RGBA byte[] array
            BitmapData bitmapData        = inputBitmap.LockBits(new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            int        inputImgBytesSize = bitmapData.Stride * bitmapData.Height;
            int        inputImgWidth     = inputBitmap.Width;
            int        inputImgHeight    = inputBitmap.Height;
            int        intPtrSize        = Marshal.SizeOf(typeof(IntPtr));

            OpenCL.Net.ImageFormat clImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unsigned_Int8);
            //OpenCL.Net.ImageFormat clImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Float);


            byte[] outputByteArray = new byte[inputImgBytesSize];
            //Allocate OpenCL image memory buffer
            Mem outputImage2DBuffer = (Mem)Cl.CreateImage2D(_context, MemFlags.CopyHostPtr |
                                                            MemFlags.WriteOnly, clImageFormat, (IntPtr)inputImgWidth,
                                                            (IntPtr)inputImgHeight, (IntPtr)0, outputByteArray, out error);
            CheckErr(error, "Cl.CreateImage2D output");
            //Pass the memory buffers to our kernel function
            error = Cl.SetKernelArg(kernel, 0, (IntPtr)intPtrSize, outputImage2DBuffer);
            CheckErr(error, "Cl.SetKernelArg");

            //Create a command queue, where all of the commands for execution will be added
            CommandQueue cmdQueue = Cl.CreateCommandQueue(_context, _device, (CommandQueueProperties)0, out error);
            CheckErr(error, "Cl.CreateCommandQueue");
            Event clevent;
            //Copy input image from the host to the GPU.
            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };                          //x, y, z
            IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)inputImgWidth, (IntPtr)inputImgHeight, (IntPtr)1 }; //x, y, z
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)inputImgWidth, (IntPtr)inputImgHeight, (IntPtr)1 };

            error = Cl.EnqueueWriteImage(cmdQueue, outputImage2DBuffer, Bool.True,
                                         originPtr, regionPtr, (IntPtr)0, (IntPtr)0, outputByteArray, 0, null, out clevent);

            CheckErr(error, "Cl.EnqueueWriteImage");
            //Execute our kernel (OpenCL code)
            error = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
            CheckErr(error, "Cl.EnqueueNDRangeKernel");
            //Wait for completion of all calculations on the GPU.
            error = Cl.Finish(cmdQueue);
            CheckErr(error, "Cl.Finish");
            //Read the processed image from GPU to raw RGBA data byte[] array
            error = Cl.EnqueueReadImage(cmdQueue, outputImage2DBuffer, Bool.True, originPtr, regionPtr,
                                        (IntPtr)0, (IntPtr)0, outputByteArray, 0, null, out clevent);
            CheckErr(error, "Cl.clEnqueueReadImage");
            //Clean up memory
            Cl.ReleaseKernel(kernel);
            Cl.ReleaseCommandQueue(cmdQueue);

            //Cl.ReleaseMemObject(inputImage2DBuffer);
            Cl.ReleaseMemObject(outputImage2DBuffer);
            //Get a pointer to our unmanaged output byte[] array
            GCHandle pinnedOutputArray = GCHandle.Alloc(outputByteArray, GCHandleType.Pinned);
            IntPtr   outputBmpPointer  = pinnedOutputArray.AddrOfPinnedObject();
            //Create a new bitmap with processed data and save it to a file.

            Bitmap outputBitmap = new Bitmap(inputImgWidth, inputImgHeight,
                                             bitmapData.Stride, PixelFormat.Format32bppArgb, outputBmpPointer);

            return(outputBitmap);

            //outputBitmap.Save(outputImagePath, System.Drawing.Imaging.ImageFormat.Png);
            //pinnedOutputArray.Free();
        }
    }
Exemplo n.º 15
0
        // copypasted/modified from Map2Bmp
        public Bitmap Map2BmpFauxColors(FloatMap inMap, float k)
        {
            var kernel = _kernels["mapToFauxColorsBmp"];

            int w = inMap.W;
            int h = inMap.H;

            var clInImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.Luminance, ChannelType.Float);

            IMem inputImage2DBuffer = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, clInImageFormat,
                                                       (IntPtr)w, (IntPtr)h,
                                                       new IntPtr(inMap.Stride * sizeof(float)), inMap._buf, out err);

            assert(err, "input img creation");


            var clOutImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unsigned_Int8);

            IMem outputImage2DBuffer = Cl.CreateImage2D(_context, MemFlags.WriteOnly, clOutImageFormat,
                                                        (IntPtr)w, (IntPtr)h,
                                                        (IntPtr)0, (IntPtr)0, out err);

            assert(err, "output img creation");

            err = Cl.SetKernelArg(kernel, 0, intPtrSize, inputImage2DBuffer);
            assert(err, "input img setKernelArg");
            err = Cl.SetKernelArg(kernel, 1, intPtrSize, outputImage2DBuffer);
            assert(err, "output img setKernelArg");
            err = Cl.SetKernelArg(kernel, 2, floatSize, k);
            assert(err, "k setKernelArg");

            Event clevent;

            //x, y, z
            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
            IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)w, (IntPtr)h, (IntPtr)1 };
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)w, (IntPtr)h, (IntPtr)1 };

            //err = Cl.EnqueueWriteImage(_commandsQueue, inputImage2DBuffer, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, inputByteArray, 0, null, out clevent);
            //assert(err, "Cl.EnqueueWriteImage");

            //Execute  kernel
            err = Cl.EnqueueNDRangeKernel(_commandsQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "Cl.EnqueueNDRangeKernel");

            //Wait for completion
            err = Cl.Finish(_commandsQueue);
            assert(err, "Cl.Finish");

            // get data back
            var outputByteArray = new byte[w * h * 4];

            err = Cl.EnqueueReadImage(_commandsQueue, outputImage2DBuffer, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, outputByteArray, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "read output buffer");

            Cl.ReleaseMemObject(inputImage2DBuffer);
            Cl.ReleaseMemObject(outputImage2DBuffer);

            GCHandle pinnedOutputArray = GCHandle.Alloc(outputByteArray, GCHandleType.Pinned);
            IntPtr   outputBmpPointer  = pinnedOutputArray.AddrOfPinnedObject();

            var bmp = new Bitmap(w, h, w * 4, PixelFormat.Format32bppArgb, outputBmpPointer);

            pinnedOutputArray.Free();

            return(bmp);
        }
Exemplo n.º 16
0
        public FloatMap Bmp2Map(Bitmap bmpImage)
        {
            var kernel = _kernels["getLumaImg"];

            int w = bmpImage.Width;
            int h = bmpImage.Height;

            BitmapData bitmapData        = bmpImage.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            int        inputImgStride    = bitmapData.Stride;
            int        inputImgBytesSize = bitmapData.Stride * bitmapData.Height;

            //Copy the raw bitmap data to an unmanaged byte[] array
            byte[] inputByteArray = new byte[inputImgBytesSize];
            Marshal.Copy(bitmapData.Scan0, inputByteArray, 0, inputImgBytesSize);

            var clInImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unsigned_Int8);

            //Allocate OpenCL image memory buffer
            IMem inputImage2DBuffer = Cl.CreateImage2D(_context, MemFlags.CopyHostPtr | MemFlags.ReadOnly, clInImageFormat,
                                                       (IntPtr)bitmapData.Width, (IntPtr)bitmapData.Height,
                                                       (IntPtr)0, inputByteArray, out err);

            assert(err, "input img creation");

            var      clOutImageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.Luminance, ChannelType.Float);
            FloatMap outMap           = new FloatMap(w, h);
            IMem     outputMapBuffer  = Cl.CreateImage2D(_context, MemFlags.WriteOnly, clOutImageFormat,
                                                         (IntPtr)outMap.W, (IntPtr)outMap.H, new IntPtr(outMap.Stride * sizeof(float)),
                                                         outMap._buf, out err);

            assert(err, "output img creation");

            err = Cl.SetKernelArg(kernel, 0, intPtrSize, inputImage2DBuffer);
            assert(err, "input img setKernelArg");
            err = Cl.SetKernelArg(kernel, 1, intPtrSize, outputMapBuffer);
            assert(err, "output img setKernelArg");

            Event clevent;

            //Copy input image from the host to the GPU.
            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 }; //x, y, z
            IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)w, (IntPtr)h, (IntPtr)1 }; //x, y, z
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)w, (IntPtr)h, (IntPtr)1 };

            //err = Cl.EnqueueWriteImage(_commandsQueue, inputImage2DBuffer, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, inputByteArray, 0, null, out clevent);
            //assert(err, "Cl.EnqueueWriteImage");

            //Execute our kernel (OpenCL code)
            err = Cl.EnqueueNDRangeKernel(_commandsQueue, kernel, 2, null, workGroupSizePtr, null, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "Cl.EnqueueNDRangeKernel");

            //Wait for completion of all calculations on the GPU.
            err = Cl.Finish(_commandsQueue);
            assert(err, "Cl.Finish");

            err = Cl.EnqueueReadImage(_commandsQueue, outputMapBuffer, Bool.True, originPtr, regionPtr, new IntPtr(outMap.Stride * sizeof(float)), (IntPtr)0, outMap._buf, 0, null, out clevent);
            clevent.Dispose();
            assert(err, "read output buffer");

            Cl.ReleaseMemObject(inputImage2DBuffer);
            Cl.ReleaseMemObject(outputMapBuffer);

            return(outMap);
        }
Exemplo n.º 17
0
        private FlowArray calcLocalGlobalOpticalFlow(SimpleImage Image0, SimpleImage Image1, FlowArray Flow)
        {
            FlowArray resultFlowArray = new FlowArray();

            resultFlowArray.Array    = new float[2][];
            resultFlowArray.Width    = Flow.Width;
            resultFlowArray.Height   = Flow.Height;
            resultFlowArray.Array[0] = new float[resultFlowArray.Width * resultFlowArray.Height];
            resultFlowArray.Array[1] = new float[resultFlowArray.Width * resultFlowArray.Height];



            Mem leftImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)Image0.ImageWidth, (IntPtr)Image0.ImageHeight, (IntPtr)0, Image0.ByteArray, out error);

            Mem rightImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)Image1.ImageWidth, (IntPtr)Image1.ImageHeight, (IntPtr)0, Image1.ByteArray, out error);

            IMem <float> uInputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.ReadOnly, Flow.Width * Flow.Height * sizeof(float), out error);

            IMem <float> vInputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.ReadOnly, Flow.Width * Flow.Height * sizeof(float), out error);

            IMem <float> OutFlow_U = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, Flow.Height * Flow.Width * sizeof(float), out error);
            IMem <float> OutFlow_V = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, Flow.Height * Flow.Width * sizeof(float), out error);

            Kernel currentKernel = Cl.CreateKernel(program, "localGlobalFlow1", out error);

            // Kernel arguiment declaration
            error  = Cl.SetKernelArg(currentKernel, 0, SimpleImage.intPtrSize, leftImageMemObject);
            error |= Cl.SetKernelArg(currentKernel, 1, SimpleImage.intPtrSize, rightImageMemObject);
            error |= Cl.SetKernelArg <float>(currentKernel, 2, uInputFlowMemObject);
            error |= Cl.SetKernelArg <float>(currentKernel, 3, vInputFlowMemObject);
            error |= Cl.SetKernelArg <float>(currentKernel, 4, OutFlow_U);
            error |= Cl.SetKernelArg <float>(currentKernel, 5, OutFlow_V);
            error |= Cl.SetKernelArg <float>(currentKernel, 6, alpha);

            error |= Cl.SetKernelArg <float>(currentKernel, 7, threshold);
            error |= Cl.SetKernelArg(currentKernel, 8, Flow.Width);
            error |= Cl.SetKernelArg(currentKernel, 9, Flow.Height);



            Event _event;

            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
            IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)Image0.ImageWidth, (IntPtr)Image0.ImageHeight, (IntPtr)1 };
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)(Flow.Height * Flow.Width) };


            error = Cl.EnqueueWriteImage(commandQueue, leftImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, Image0.ByteArray, 0, null, out _event);

            error = Cl.EnqueueWriteImage(commandQueue, rightImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, Image1.ByteArray, 0, null, out _event);

            error = Cl.EnqueueWriteBuffer <float>(commandQueue, uInputFlowMemObject, Bool.True, Flow.Array[1], 0, null, out _event);

            error = Cl.EnqueueWriteBuffer <float>(commandQueue, vInputFlowMemObject, Bool.True, Flow.Array[0], 0, null, out _event);

            error = Cl.EnqueueNDRangeKernel(commandQueue, currentKernel, 1, null, workGroupSizePtr, null, 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, OutFlow_U, Bool.True, 0, (resultFlowArray.Width * resultFlowArray.Height), resultFlowArray.Array[0], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, OutFlow_V, Bool.True, 0, (resultFlowArray.Width * resultFlowArray.Height), resultFlowArray.Array[1], 0, null, out _event);
            error = Cl.Finish(commandQueue);


            Cl.ReleaseMemObject(leftImageMemObject);
            Cl.ReleaseMemObject(rightImageMemObject);
            Cl.ReleaseMemObject(uInputFlowMemObject);
            Cl.ReleaseMemObject(vInputFlowMemObject);
            Cl.ReleaseMemObject(OutFlow_U);
            Cl.ReleaseMemObject(OutFlow_V);
            Cl.ReleaseKernel(currentKernel);


            return(resultFlowArray);
        }
Exemplo n.º 18
0
        private float[][] calc_J(SimpleImage I0, SimpleImage I1d)

        {
            float[][] J = new float[6][];

            J[0] = new float[I0.ImageHeight * I0.ImageWidth];
            J[1] = new float[I0.ImageHeight * I0.ImageWidth];
            J[2] = new float[I0.ImageHeight * I0.ImageWidth];
            J[3] = new float[I0.ImageHeight * I0.ImageWidth];
            J[4] = new float[I0.ImageHeight * I0.ImageWidth];
            J[5] = new float[I0.ImageHeight * I0.ImageWidth];


            Mem leftImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)I0.ImageWidth, (IntPtr)I0.ImageHeight, (IntPtr)0, I0.ByteArray, out error);

            Mem rightImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)I1d.ImageWidth, (IntPtr)I1d.ImageHeight, (IntPtr)0, I1d.ByteArray, out error);



            IMem <float> J1 = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, I0.ImageHeight * I0.ImageWidth * sizeof(float), out error);
            IMem <float> J2 = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, I0.ImageHeight * I0.ImageWidth * sizeof(float), out error);
            IMem <float> J3 = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, I0.ImageHeight * I0.ImageWidth * sizeof(float), out error);
            IMem <float> J4 = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, I0.ImageHeight * I0.ImageWidth * sizeof(float), out error);
            IMem <float> J5 = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, I0.ImageHeight * I0.ImageWidth * sizeof(float), out error);
            IMem <float> J6 = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, I0.ImageHeight * I0.ImageWidth * sizeof(float), out error);

            Kernel _Kernel = Cl.CreateKernel(program, "localGlobalFlow_J", out error);

            error |= Cl.SetKernelArg(_Kernel, 0, leftImageMemObject);
            error |= Cl.SetKernelArg(_Kernel, 1, rightImageMemObject);
            error |= Cl.SetKernelArg <float>(_Kernel, 2, J1);
            error |= Cl.SetKernelArg <float>(_Kernel, 3, J2);
            error |= Cl.SetKernelArg <float>(_Kernel, 4, J3);
            error |= Cl.SetKernelArg <float>(_Kernel, 5, J4);
            error |= Cl.SetKernelArg <float>(_Kernel, 6, J5);
            error |= Cl.SetKernelArg <float>(_Kernel, 7, J6);
            error |= Cl.SetKernelArg(_Kernel, 8, I0.ImageWidth);
            error |= Cl.SetKernelArg(_Kernel, 9, I0.ImageHeight);

            Event _event;

            IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
            IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)I0.ImageWidth, (IntPtr)I0.ImageHeight, (IntPtr)1 };
            IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)(I0.ImageWidth * I0.ImageHeight) };


            error = Cl.EnqueueWriteImage(commandQueue, leftImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, I0.ByteArray, 0, null, out _event);

            error = Cl.EnqueueWriteImage(commandQueue, rightImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, I1d.ByteArray, 0, null, out _event);

            error = Cl.EnqueueNDRangeKernel(commandQueue, _Kernel, 1, null, workGroupSizePtr, null, 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, J1, Bool.True, 0, (I0.ImageWidth * I0.ImageHeight), J[0], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, J2, Bool.True, 0, (I0.ImageWidth * I0.ImageHeight), J[1], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, J3, Bool.True, 0, (I0.ImageWidth * I0.ImageHeight), J[2], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, J4, Bool.True, 0, (I0.ImageWidth * I0.ImageHeight), J[3], 0, null, out _event);

            Cl.EnqueueReadBuffer <float>(commandQueue, J5, Bool.True, 0, (I0.ImageWidth * I0.ImageHeight), J[4], 0, null, out _event);
            error = Cl.Finish(commandQueue);

            Cl.EnqueueReadBuffer <float>(commandQueue, J6, Bool.True, 0, (I0.ImageWidth * I0.ImageHeight), J[5], 0, null, out _event);
            error = Cl.Finish(commandQueue);


            Cl.ReleaseMemObject(leftImageMemObject);
            Cl.ReleaseMemObject(rightImageMemObject);
            Cl.ReleaseMemObject(J1);
            Cl.ReleaseMemObject(J2);
            Cl.ReleaseMemObject(J3);
            Cl.ReleaseMemObject(J4);
            Cl.ReleaseMemObject(J5);
            Cl.ReleaseMemObject(J6);
            Cl.ReleaseKernel(_Kernel);


            return(J);
        }
Exemplo n.º 19
0
        /* Broken at the moment. */
        public void GetStatGPU(string file)
        {
            var stopwatch = new System.Diagnostics.Stopwatch();

            stopwatch.Start();
            stopwatch.Restart();
            ErrorCode err;


            using (OpenCL.Net.Program program = Cl.CreateProgramWithSource(clInfo._context, 1, new[] { clInfo.KernelSrc }, null, out err))
            {
                clInfo.CheckErr(err, "Cl.CreateProgramWithSource");

                //Compile Kernels & check errors.
                err = Cl.BuildProgram(program, 1, new[] { clInfo._device }, string.Empty, null, IntPtr.Zero);
                clInfo.CheckErr(err, "Cl.BuildProgram");
                if (Cl.GetProgramBuildInfo(program, clInfo._device, ProgramBuildInfo.Status, out err).CastTo <BuildStatus>() != BuildStatus.Success)
                {
                    clInfo.CheckErr(err, "Cl.GetProgramBuildInfo");
                    Console.WriteLine("Cl.GetProgramBuildInfo != Success");
                    Console.WriteLine(Cl.GetProgramBuildInfo(program, clInfo._device, ProgramBuildInfo.Log, out err));
                }

                //Specify the specific kernel for use
                Kernel kernel = Cl.CreateKernel(program, "satAvg", out err);
                clInfo.CheckErr(err, "Cl.CreateKernel");

                int intPtrSize = 0;
                intPtrSize = Marshal.SizeOf(typeof(IntPtr));

                IMem image2DBuffer;
                OpenCL.Net.ImageFormat imageFormat = new OpenCL.Net.ImageFormat(ChannelOrder.RGBA, ChannelType.Unsigned_Int8);
                int imageWidth, imageHeight, imageBytesSize, imageStride;

                using (FileStream imgStream = new FileStream(file, FileMode.Open))
                {
                    Image image = Image.FromStream(imgStream);

                    if (image == null)
                    {
                        Console.Error.WriteLine($"failed to open file {file}");
                        return;
                    }
                    imageWidth  = image.Width;
                    imageHeight = image.Height;

                    //Create a bitmap from the file with the correct channel settings.
                    Bitmap     inputBitmap = new Bitmap(image);
                    BitmapData bitmapDat   = inputBitmap.LockBits(new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height),
                                                                  ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                    imageStride    = bitmapDat.Stride;
                    imageBytesSize = imageStride * bitmapDat.Height;

                    byte[] inputByteArray = new byte[imageBytesSize];
                    Marshal.Copy(bitmapDat.Scan0, inputByteArray, 0, imageBytesSize);

                    //Create & populate memory buffer for use in Kernel
                    image2DBuffer = Cl.CreateImage2D(clInfo._context,
                                                     MemFlags.CopyHostPtr | MemFlags.ReadOnly, imageFormat,
                                                     (IntPtr)inputBitmap.Width,
                                                     (IntPtr)inputBitmap.Height,
                                                     (IntPtr)0, inputByteArray, out err);

                    clInfo.CheckErr(err, "Cl.CreateImage2D output");
                }
                //Create an output buffer for the average saturation of the image.
                float saturation = 0;
                IMem  sat = Cl.CreateBuffer(clInfo._context, MemFlags.CopyHostPtr | MemFlags.WriteOnly, (IntPtr)intPtrSize, (object)saturation, out err);
                clInfo.CheckErr(err, "Cl.CreateBuffer saturation");

                //Passing buffers to the CL kernel
                err = Cl.SetKernelArg(kernel, 0, (IntPtr)intPtrSize, image2DBuffer);
                //TODO: Figure out how to pass a float to the kernel
                //err |= Cl.SetKernelArg(kernel, 1, (IntPtr)intPtrSize, sat );
                clInfo.CheckErr(err, "Cl.SetKernelArg");

                //Create & queue commands
                CommandQueue cmdQueue = Cl.CreateCommandQueue(clInfo._context, clInfo._device, CommandQueueProperties.None, out err);
                clInfo.CheckErr(err, "Cl.CreateCommandQueue");

                Event @event;

                //Transfer image data to kernel buffers
                IntPtr[] originPtr = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
                IntPtr[] regionPtr = new IntPtr[] { (IntPtr)imageWidth, (IntPtr)imageHeight, (IntPtr)1 };
                IntPtr[] workGroupPtr = new IntPtr[] { (IntPtr)imageWidth, (IntPtr)imageHeight, (IntPtr)1 };

                err = Cl.EnqueueReadImage(cmdQueue, image2DBuffer, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, null, 0, null, out @event);
                clInfo.CheckErr(err, "Cl.EnqueueReadImage");

                //Execute the kerenl
                err = Cl.EnqueueNDRangeKernel(cmdQueue, kernel, 2, null, workGroupPtr, null, 0, null, out @event);
                clInfo.CheckErr(err, "Cl.EnqueueNDRangeKernel");

                err = Cl.Finish(cmdQueue);
                clInfo.CheckErr(err, "Cl.Finish");

                Cl.ReleaseKernel(kernel);
                Cl.ReleaseCommandQueue(cmdQueue);

                Cl.ReleaseMemObject(image2DBuffer);
            }
            Console.WriteLine(stopwatch.Elapsed);
            stopwatch.Stop();
        }
Exemplo n.º 20
0
        private FlowArray HS_OpticalFlow(Bitmap input1, Bitmap input2, FlowArray inputFlow, float alpha)
        {
            SimpleImage left  = new SimpleImage(input1);
            SimpleImage right = new SimpleImage(input2);



            try
            {
                if (left.ImageHeight != right.ImageHeight || left.ImageWidth != right.ImageWidth)
                {
                    throw (new ArgumentException("The size of the left and right images are not the same!"));
                }

                ErrorCode error;


                FlowArray resultFlowArray = new FlowArray();
                resultFlowArray.Array    = new float[2][];
                resultFlowArray.Width    = input1.Width;
                resultFlowArray.Height   = input1.Height;
                resultFlowArray.Array[0] = new float[resultFlowArray.Width * resultFlowArray.Height];
                resultFlowArray.Array[1] = new float[resultFlowArray.Width * resultFlowArray.Height];

                Kernel flowKernel = Cl.CreateKernel(program, "hornSchunck", out error);

                Mem leftImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)left.ImageWidth, (IntPtr)left.ImageHeight, (IntPtr)0, left.ByteArray, out error);

                Mem rightImageMemObject = (Mem)Cl.CreateImage2D(context, MemFlags.ReadOnly | MemFlags.CopyHostPtr, SimpleImage.clImageFormat, (IntPtr)right.ImageWidth, (IntPtr)right.ImageHeight, (IntPtr)0, right.ByteArray, out error);



                IMem <float> uInputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.ReadOnly, inputFlow.Width * inputFlow.Height * sizeof(float), out error);

                IMem <float> vInputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.ReadOnly, inputFlow.Width * inputFlow.Height * sizeof(float), out error);

                IMem <float> uOutputFlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, resultFlowArray.Width * resultFlowArray.Height * sizeof(float), out error);

                IMem <float> vOutputlowMemObject = Cl.CreateBuffer <float>(context, MemFlags.WriteOnly, resultFlowArray.Width * resultFlowArray.Height * sizeof(float), out error);

                error  = Cl.SetKernelArg(flowKernel, 0, leftImageMemObject);
                error |= Cl.SetKernelArg(flowKernel, 1, rightImageMemObject);
                error |= Cl.SetKernelArg <float>(flowKernel, 2, uInputFlowMemObject);
                error |= Cl.SetKernelArg <float>(flowKernel, 3, vInputFlowMemObject);
                error |= Cl.SetKernelArg <float>(flowKernel, 4, uOutputFlowMemObject);
                error |= Cl.SetKernelArg <float>(flowKernel, 5, vOutputlowMemObject);
                error |= Cl.SetKernelArg <float>(flowKernel, 6, alpha);
                error |= Cl.SetKernelArg <float>(flowKernel, 7, threshold);
                error |= Cl.SetKernelArg(flowKernel, 8, inputFlow.Width);
                error |= Cl.SetKernelArg(flowKernel, 9, inputFlow.Height);


                Event clEvent;

                IntPtr[] originPtr        = new IntPtr[] { (IntPtr)0, (IntPtr)0, (IntPtr)0 };
                IntPtr[] regionPtr        = new IntPtr[] { (IntPtr)left.ImageWidth, (IntPtr)left.ImageHeight, (IntPtr)1 };
                IntPtr[] workGroupSizePtr = new IntPtr[] { (IntPtr)(inputFlow.Height * inputFlow.Width) };

                error = Cl.EnqueueWriteImage(commandQueue, leftImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, left.ByteArray, 0, null, out clEvent);

                error = Cl.EnqueueWriteImage(commandQueue, rightImageMemObject, Bool.True, originPtr, regionPtr, (IntPtr)0, (IntPtr)0, right.ByteArray, 0, null, out clEvent);


                error = Cl.EnqueueWriteBuffer <float>(commandQueue, uInputFlowMemObject, Bool.True, inputFlow.Array[0], 0, null, out clEvent);

                error = Cl.EnqueueWriteBuffer <float>(commandQueue, vInputFlowMemObject, Bool.True, inputFlow.Array[1], 0, null, out clEvent);


                // Enqueueing the kernel
                error = Cl.EnqueueNDRangeKernel(commandQueue, flowKernel, 1, null, workGroupSizePtr, null, 0, null, out clEvent);

                error = Cl.Finish(commandQueue);

                Cl.EnqueueReadBuffer <float>(commandQueue, uOutputFlowMemObject, Bool.True, 0, (resultFlowArray.Width * resultFlowArray.Height), resultFlowArray.Array[0], 0, null, out clEvent);
                error = Cl.Finish(commandQueue);

                Cl.EnqueueReadBuffer <float>(commandQueue, vOutputlowMemObject, Bool.True, 0, (resultFlowArray.Width * resultFlowArray.Height), resultFlowArray.Array[1], 0, null, out clEvent);
                error = Cl.Finish(commandQueue);


                Cl.ReleaseMemObject(leftImageMemObject);
                Cl.ReleaseMemObject(rightImageMemObject);
                Cl.ReleaseMemObject(uInputFlowMemObject);
                Cl.ReleaseMemObject(vInputFlowMemObject);
                Cl.ReleaseMemObject(uOutputFlowMemObject);
                Cl.ReleaseMemObject(vOutputlowMemObject);
                Cl.ReleaseKernel(flowKernel);


                return(resultFlowArray);
            }
            catch (Exception e)
            {
                throw (new Exception("Flow calculation error", e));
            }
        }