Пример #1
0
        ///<summary>
        ///Enqueues a command to map a part of a buffer into the host address space.
        ///</summary>
        ///<param name = "buffer"> The buffer to map. </param>
        ///<param name = "blocking"> The mode of operation of this call. </param>
        ///<param name = "flags"> A list of properties for the mapping mode. </param>
        ///<param name = "offset"> The <paramref name = "buffer"/> element position where mapping starts.. </param>
        ///<param name = "region"> The region of elements to map. </param>
        ///<paramref name = "events"/> is not <c> null </c> or read-only a new <param name = "events"> A collection of events that need to complete before this command   <see cref = "ComputeEvent"/> identifying this command is created and attached to the end of the collection. </param>
        ///<remarks> If <paramref name = "blocking"/> is <c> false </c> is <c> true </c> this method will not return until the command completes.   > this method will return immediately after the command is enqueued. </remarks>
        public IntPtr Map <T>(ComputeBufferBase <T> buffer, bool blocking, ComputeMemoryMappingFlags flags, long offset, long region, ICollection <ComputeEventBase> events) where T : struct
        {
            int sizeofT = Marshal.SizeOf(typeof(T));

            int eventWaitListSize;

            CLEventHandle[] eventHandles   = ComputeTools.ExtractHandles(events, out eventWaitListSize);
            bool            eventsWritable = (events != null && !events.IsReadOnly);

            CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null;

            IntPtr mappedPtr = IntPtr.Zero;

            ComputeErrorCode error = ComputeErrorCode.Success;

            mappedPtr = CL12.EnqueueMapBuffer(Handle, buffer.Handle, blocking, flags, new IntPtr(offset * sizeofT), new IntPtr(region * sizeofT), eventWaitListSize, eventHandles, newEventHandle, out error);
            ComputeException.ThrowOnError(error);

            if (eventsWritable)
            {
                events.Add(new ComputeEvent(newEventHandle[0], this));
            }

            return(mappedPtr);
        }
Пример #2
0
        private ReadOnlyCollection <byte[]> GetBinaries()
        {
            IntPtr[] binaryLengths = GetArrayInfo <CLProgramHandle, ComputeProgramInfo, IntPtr>(Handle, ComputeProgramInfo.BinarySizes, CL12.GetProgramInfo);

            GCHandle[]     binariesGCHandles    = new GCHandle[binaryLengths.Length];
            IntPtr[]       binariesPtrs         = new IntPtr[binaryLengths.Length];
            IList <byte[]> binaries             = new List <byte[]>();
            GCHandle       binariesPtrsGCHandle = GCHandle.Alloc(binariesPtrs, GCHandleType.Pinned);

            try
            {
                for (int i = 0; i < binaryLengths.Length; i++)
                {
                    byte[] binary = new byte[binaryLengths[i].ToInt64()];
                    binariesGCHandles[i] = GCHandle.Alloc(binary, GCHandleType.Pinned);
                    binariesPtrs[i]      = binariesGCHandles[i].AddrOfPinnedObject();
                    binaries.Add(binary);
                }

                IntPtr           sizeRet;
                ComputeErrorCode error = CL12.GetProgramInfo(Handle, ComputeProgramInfo.Binaries, new IntPtr(binariesPtrs.Length * IntPtr.Size), binariesPtrsGCHandle.AddrOfPinnedObject(), out sizeRet);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                for (int i = 0; i < binaryLengths.Length; i++)
                {
                    binariesGCHandles[i].Free();
                }
                binariesPtrsGCHandle.Free();
            }

            return(new ReadOnlyCollection <byte[]>(binaries));
        }
Пример #3
0
        static ComputePlatform()
        {
            lock (typeof(ComputePlatform))
            {
                try
                {
                    if (platforms != null)
                    {
                        return;
                    }

                    ComputeErrorCode error = CL12.GetPlatformIDs(0, null, out var handlesLength);
                    ComputeException.ThrowOnError(error);
                    var handles = new CLPlatformHandle[handlesLength];

                    error = CL12.GetPlatformIDs(handlesLength, handles, out handlesLength);
                    ComputeException.ThrowOnError(error);

                    List <ComputePlatform> platformList = new List <ComputePlatform>(handlesLength);
                    foreach (CLPlatformHandle handle in handles)
                    {
                        platformList.Add(new ComputePlatform(handle));
                    }

                    platforms = platformList.AsReadOnly();
                }
                catch (DllNotFoundException)
                {
                    platforms = new List <ComputePlatform>().AsReadOnly();
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Creates a new <see cref="ComputeBuffer{T}"/> from an existing OpenGL buffer object.
        /// </summary>
        /// <typeparam name="TDataType"> The type of the elements of the <see cref="ComputeBuffer{T}"/>. <typeparamref name="T"/> should match the type of the elements in the OpenGL buffer. </typeparam>
        /// <param name="context"> A <see cref="ComputeContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="ComputeBuffer{T}"/>. Only <see cref="ComputeMemoryFlags.ReadOnly"/>, <see cref="ComputeMemoryFlags.WriteOnly"/> and <see cref="ComputeMemoryFlags.ReadWrite"/> are allowed. </param>
        /// <param name="bufferId"> The OpenGL buffer object id to use for the creation of the <see cref="ComputeBuffer{T}"/>. </param>
        /// <returns> The created <see cref="ComputeBuffer{T}"/>. </returns>
        public static ComputeBuffer <TDataType> CreateFromGLBuffer <TDataType>(ComputeContext context, ComputeMemoryFlags flags, int bufferId) where TDataType : struct
        {
            CLMemoryHandle handle = CL12.CreateFromGLBuffer(context.Handle, flags, bufferId, out var error);

            ComputeException.ThrowOnError(error);
            return(new ComputeBuffer <TDataType>(handle, context, flags));
        }
        /// <summary>
        /// Builds (compiles and links) a program executable from the program source or binary for all or some of the <see cref="ComputeProgram.Devices"/>.
        /// </summary>
        /// <param name="devices"> A subset or all of <see cref="ComputeProgram.Devices"/>. If <paramref name="devices"/> is <c>null</c>, the executable is built for every item of <see cref="ComputeProgram.Devices"/> for which a source or a binary has been loaded. </param>
        /// <param name="options"> A set of options for the OpenCL compiler. </param>
        /// <param name="notify"> A delegate instance that represents a reference to a notification routine. This routine is a callback function that an application can register and which will be called when the program executable has been built (successfully or unsuccessfully). If <paramref name="notify"/> is not <c>null</c>, <see cref="ComputeProgram.Build"/> does not need to wait for the build to complete and can return immediately. If <paramref name="notify"/> is <c>null</c>, <see cref="ComputeProgram.Build"/> does not return until the build has completed. The callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe and that the delegate instance doesn't get collected by the Garbage Collector until the build operation triggers the callback. </param>
        /// <param name="notifyDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param>
        public void Build(ICollection <ComputeDevice> devices, string options, ComputeProgramBuildNotifier notify, IntPtr notifyDataPtr)
        {
            int handleCount;

            CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out handleCount);
            buildOptions = (options != null) ? options : "";
            buildNotify  = notify;

            ComputeErrorCode error = CL12.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr);

#if DEBUG
            if (error != ComputeErrorCode.Success)
            {
                string str  = String.Empty;
                var    ptr  = Marshal.StringToHGlobalAnsi(str);
                IntPtr size = IntPtr.Zero;

                var errorCode = CL12.GetProgramBuildInfo(Handle, deviceHandles[0], ComputeProgramBuildInfo.BuildLog,
                                                         new IntPtr(4096), ptr,
                                                         out size);
                if (errorCode == ComputeErrorCode.Success)
                {
                    Debug.WriteLine($"Build Error:{Marshal.PtrToStringAnsi(ptr, (int) size)}");
                }
            }
#endif

            ComputeException.ThrowOnError(error);
        }
Пример #6
0
        /// <summary>
        /// Waits on the host thread for the specified events to complete.
        /// </summary>
        /// <param name="events"> The events to be waited for completition. </param>
        public static void Wait(ICollection <ComputeEventBase> events)
        {
            CLEventHandle[]  eventHandles = ComputeTools.ExtractHandles(events, out var eventWaitListSize);
            ComputeErrorCode error        = CL12.WaitForEvents(eventWaitListSize, eventHandles);

            ComputeException.ThrowOnError(error);
        }
Пример #7
0
        ///<summary>
        ///Enqueues a marker.
        ///</summary>
        public ComputeEvent AddMarker()
        {
            CLEventHandle    newEventHandle;
            ComputeErrorCode error = CL12.EnqueueMarker(Handle, out newEventHandle);

            ComputeException.ThrowOnError(error);
            return(new ComputeEvent(newEventHandle, this));
        }
Пример #8
0
        public ComputeImage3D(ComputeContext context, ComputeMemoryFlags flags, ComputeImageFormat format, int width, int height, int depth, long rowPitch, long slicePitch, IntPtr data)
            : base(context, flags)
        {
            Handle = CL12.CreateImage3D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(depth), new IntPtr(rowPitch), new IntPtr(slicePitch), data, out var error);
            ComputeException.ThrowOnError(error);

            Init();
        }
Пример #9
0
        /// <summary>
        /// Creates a new <see cref="ComputeImage2D"/> from an OpenGL renderbuffer object.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/> with enabled CL/GL sharing. </param>
        /// <param name="flags"> A bit-field that is used to specify usage information about the <see cref="ComputeImage2D"/>. Only <c>ComputeMemoryFlags.ReadOnly</c>, <c>ComputeMemoryFlags.WriteOnly</c> and <c>ComputeMemoryFlags.ReadWrite</c> are allowed. </param>
        /// <param name="renderbufferId"> The OpenGL renderbuffer object id to use. </param>
        /// <returns> The created <see cref="ComputeImage2D"/>. </returns>
        public static ComputeImage2D CreateFromGLRenderbuffer(ComputeContext context, ComputeMemoryFlags flags, int renderbufferId)
        {
            CLMemoryHandle image = CL12.CreateFromGLRenderbuffer(context.Handle, flags, renderbufferId, out var error);

            ComputeException.ThrowOnError(error);

            return(new ComputeImage2D(image, context, flags));
        }
Пример #10
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Releases the associated OpenCL object. </summary>
        ///
        /// <remarks>
        /// <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the
        /// application.
        /// </remarks>
        ///
        /// <param name="manual">   Specifies the operation mode of this method. </param>
        ///
        /// <seealso cref="M:Cloo.ComputeResource.Dispose(bool)"/>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        protected override void Dispose(bool manual)
        {
            if (Handle.IsValid)
            {
                CL12.ReleaseKernel(Handle);
                Handle.Invalidate();
            }
        }
Пример #11
0
        public static ComputeImage3D CreateFromGLTexture3D(ComputeContext context, ComputeMemoryFlags flags, int textureTarget, int mipLevel, int textureId)
        {
            var image = CL12.CreateFromGLTexture3D(context.Handle, flags, textureTarget, mipLevel, textureId, out var error);

            ComputeException.ThrowOnError(error);

            return(new ComputeImage3D(image, context, flags));
        }
Пример #12
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Releases the associated OpenCL object. </summary>
        ///
        /// <remarks>
        /// <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the
        /// application.
        /// </remarks>
        ///
        /// <param name="manual">   Specifies the operation mode of this method. </param>
        ///
        /// <seealso cref="M:Cloo.ComputeResource.Dispose(bool)"/>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        protected override void Dispose(bool manual)
        {
            if (Handle.IsValid)
            {
                RILogManager.Default?.SendTrace(string.Intern("Dispose ") + this + string.Intern(" in Thread(") + Thread.CurrentThread.ManagedThreadId + string.Intern(")."), string.Intern("Information"));
                CL12.ReleaseSampler(Handle);
                Handle.Invalidate();
            }
        }
Пример #13
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Creates a new <see cref="ComputeBuffer{T}"/>. </summary>
        ///
        /// <param name="context">  A <see cref="ComputeContext"/> used to create the
        ///                         <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="flags">    A bit-field that is used to specify allocation and usage information
        ///                         about the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="count">    The number of elements of the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="dataPtr">  A pointer to the data for the <see cref="ComputeBuffer{T}"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, long count, IntPtr dataPtr)
            : base(context, flags)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateBuffer(context.Handle, flags, new IntPtr(Marshal.SizeOf(typeof(T)) * count), dataPtr, out error);
            ComputeException.ThrowOnError(error);
            Init();
        }
Пример #14
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Releases the associated OpenCL object. </summary>
        ///
        /// <remarks>
        /// <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the
        /// application.
        /// </remarks>
        ///
        /// <param name="manual">   Specifies the operation mode of this method. </param>
        ///
        /// <seealso cref="M:Cloo.ComputeResource.Dispose(bool)"/>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        protected override void Dispose(bool manual)
        {
            if (Handle.IsValid)
            {
                RILogManager.Default?.SendTrace("Dispose " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
                CL12.ReleaseProgram(Handle);
                Handle.Invalidate();
            }
        }
Пример #15
0
        ///<summary>
        ///Creates a new <see cref = "ComputeImage 2 D"/> from an OpenGL 2D texture object.
        ///</summary>
        ///<param name = "context"> A <see cref = "ComputeContext"/> with enabled CL / GL sharing. </param>
        ///Only <c> ComputeMemoryFlags.ReadOnly </c>, <c> ComputeMemoryFlags.WriteOnly </c>, where <param name = "flags"> A bit-field that is used to specify usage information about the <   / c> and <c> ComputeMemoryFlags.ReadWrite </c> are allowed.. </param>
        ///<Param name = "textureTarget"> One of the following values:.. GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, or GL_TEXTURE_RECTANGLE Using GL_TEXTURE_RECTANGLE for texture_target requires OpenGL 3.1 Alternatively, GL_TEXTURE_RECTANGLE_ARB may be specified if the OpenGL extension GL_ARB_texture_rectangle   is supported. </param>
        ///<param name = "mipLevel"> The mipmap level of the OpenGL 2D texture object to be used. </param>
        ///<param name = "textureId"> The OpenGL 2D texture object id to use. </param>
        ///<returns> The created <see cref = "ComputeImage2D"/>. </returns>
        public static ComputeImage2D CreateFromGLTexture2D(ComputeContext context, ComputeMemoryFlags flags, int textureTarget, int mipLevel, int textureId)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;
            CLMemoryHandle   image = CL12.CreateFromGLTexture2D(context.Handle, flags, textureTarget, mipLevel, textureId, out error);

            ComputeException.ThrowOnError(error);

            return(new ComputeImage2D(image, context, flags));
        }
Пример #16
0
        /// <summary>
        /// Creates a new <see cref="ComputeBuffer{T}"/>.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/> used to create the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="flags"> A bit-field that is used to specify allocation and usage information about the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="count"> The number of elements of the <see cref="ComputeBuffer{T}"/>. </param>
        /// <param name="dataPtr"> A pointer to the data for the <see cref="ComputeBuffer{T}"/>. </param>
        public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, long count, IntPtr dataPtr)
            : base(context, flags)
        {
            var size = ComputeTools.SizeOf <T>() * count;

            Handle = CL12.CreateBuffer(context.Handle, flags, new IntPtr(size), dataPtr, out var error);
            ComputeException.ThrowOnError(error);
            Init(size, count);
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Releases the associated OpenCL object. </summary>
        ///
        /// <remarks>
        /// <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the
        /// application.
        /// </remarks>
        ///
        /// <param name="manual">   Specifies the operation mode of this method. </param>
        ///
        /// <seealso cref="M:Cloo.ComputeResource.Dispose(bool)"/>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        protected override void Dispose(bool manual)
        {
            if (Handle.IsValid)
            {
                Trace.WriteLine("Dispose " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
                CL12.ReleaseKernel(Handle);
                Handle.Invalidate();
            }
        }
Пример #18
0
        /*
         * ///<summary>
         * /// Creates a new <see cref="ComputeImage2D"/> from a <c>Bitmap</c>.
         * ///</summary>
         * ///<para name = "context"> A valid <see cref = "ComputeContext"/> in which the <see cref = "ComputeImage 2 D"/> is created. </param>
         * ///<para name = "flags"> A bit-field that is used to specify allocation and usage information about the <see cref = "ComputeImage 2 D"/>. </param>
         * /// <param name="bitmap"> The bitmap to use. </param>
         * /// <remarks> Note that only bitmaps with <c>Format32bppArgb</c> pixel format are currently supported. </remarks>
         * public ComputeImage2D(ComputeContext context, ComputeMemoryFlags flags, Bitmap bitmap)
         *  :base(context, flags)
         * {
         *  unsafe
         *  {
         *      if(bitmap.PixelFormat != PixelFormat.Format32bppArgb)
         *          throw new ArgumentException("Pixel format not supported.");
         *
         *      //ComputeImageFormat format = Tools.ConvertImageFormat(bitmap.PixelFormat);
         *      ComputeImageFormat format = new ComputeImageFormat(ComputeImageChannelOrder.Bgra, ComputeImageChannelType.UnsignedInt8);
         *      BitmapData bitmapData = bitmap.LockBits(new Rectangle(new Point(), bitmap.Size), ImageLockMode.ReadOnly, bitmap.PixelFormat);
         *
         *      try
         *      {
         *          ComputeErrorCode error = ComputeErrorCode.Success;
         *          Handle = CL12.CreateImage2D(
         *              context.Handle,
         *              flags,
         *              &format,
         *              new IntPtr(bitmap.Width),
         *              new IntPtr(bitmap.Height),
         *              new IntPtr(bitmapData.Stride),
         *              bitmapData.Scan0,
         *              &error);
         *          ComputeException.ThrowOnError(error);
         *      }
         *      finally
         *      {
         *          bitmap.UnlockBits(bitmapData);
         *      }
         *
         *      Init();
         *  }
         * }*/

        ///<summary>
        ///Creates a new <see cref = "ComputeImage 2 D"/>.
        ///</summary>
        ///<para name = "context"> A valid <see cref = "ComputeContext"/> in which the <see cref = "ComputeImage 2 D"/> is created. </param>
        ///<para name = "flags"> A bit-field that is used to specify allocation and usage information about the <see cref = "ComputeImage 2 D"/>. </param>
        ///<param name = "format"> A structure that describes the format properties of the <see cref = "ComputeImage 2 D"/>. </param>
        ///<param name = "width"> The width of the <see cref = "ComputeImage 2 D"/> in pixels. </param>
        ///<param name = "height"> The height of the <see cref = "ComputeImage 2 D"/> in pixels. </param>
        ///<para name = "rowPitch">> ​​The size in bytes of each row of elements of the <see cref = "ComputeImage2D"/>. If <paramref name = "rowPitch"/> is zero, OpenCL will compute the proper value based on   <see cref = "ComputeImage.Width"/> and <see cref = "ComputeImage.ElementSize"/>. </param>
        ///<param name = "data"> The data to initialize the <see cref = "ComputeImage 2 D"/>. Can be <c> IntPtr.Zero </c>. </param>
        public ComputeImage2D(ComputeContext context, ComputeMemoryFlags flags, ComputeImageFormat format, int width, int height, long rowPitch, IntPtr data)
            : base(context, flags)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateImage2D(context.Handle, flags, ref format, new IntPtr(width), new IntPtr(height), new IntPtr(rowPitch), data, out error);
            ComputeException.ThrowOnError(error);

            Init();
        }
Пример #19
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="flags"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        protected static ICollection <ComputeImageFormat> GetSupportedFormats(ComputeContext context, ComputeMemoryFlags flags, ComputeMemoryType type)
        {
            ComputeErrorCode error = CL12.GetSupportedImageFormats(context.Handle, flags, type, 0, null, out var formatCountRet);

            ComputeException.ThrowOnError(error);

            ComputeImageFormat[] formats = new ComputeImageFormat[formatCountRet];
            error = CL12.GetSupportedImageFormats(context.Handle, flags, type, formatCountRet, formats, out formatCountRet);
            ComputeException.ThrowOnError(error);

            return(new Collection <ComputeImageFormat>(formats));
        }
Пример #20
0
        /// <summary>
        /// Builds (compiles and links) a program executable from the program source or binary for all or some of the <see cref="ComputeProgram.Devices"/>.
        /// </summary>
        /// <param name="devices"> A subset or all of <see cref="ComputeProgram.Devices"/>. If <paramref name="devices"/> is <c>null</c>, the executable is built for every item of <see cref="ComputeProgram.Devices"/> for which a source or a binary has been loaded. </param>
        /// <param name="options"> A set of options for the OpenCL compiler. </param>
        /// <param name="notify"> A delegate instance that represents a reference to a notification routine. This routine is a callback function that an application can register and which will be called when the program executable has been built (successfully or unsuccessfully). If <paramref name="notify"/> is not <c>null</c>, <see cref="ComputeProgram.Build"/> does not need to wait for the build to complete and can return immediately. If <paramref name="notify"/> is <c>null</c>, <see cref="ComputeProgram.Build"/> does not return until the build has completed. The callback function may be called asynchronously by the OpenCL implementation. It is the application's responsibility to ensure that the callback function is thread-safe and that the delegate instance doesn't get collected by the Garbage Collector until the build operation triggers the callback. </param>
        /// <param name="notifyDataPtr"> Optional user data that will be passed to <paramref name="notify"/>. </param>
        public void Build(ICollection <ComputeDevice> devices, string options, ComputeProgramBuildNotifier notify, IntPtr notifyDataPtr)
        {
            int handleCount;

            CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out handleCount);
            buildOptions = (options != null) ? options : "";
            buildNotify  = notify;

            ComputeErrorCode error = CL12.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr);

            ComputeException.ThrowOnError(error);
        }
Пример #21
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Builds (compiles and links) a program executable from the program source or binary for all or
        /// some of the <see cref="ComputeProgram.Devices"/>.
        /// </summary>
        ///
        /// <param name="devices">          A subset or all of <see cref="ComputeProgram.Devices"/>. If
        ///                                 <paramref name="devices"/> is <c>null</c>, the executable is
        ///                                 built for every item of <see cref="ComputeProgram.Devices"/> for
        ///                                 which a source or a binary has been loaded. </param>
        /// <param name="options">          A set of options for the OpenCL compiler. </param>
        /// <param name="notify">           A delegate instance that represents a reference to a
        ///                                 notification routine. This routine is a callback function that an
        ///                                 application can register and which will be called when the
        ///                                 program executable has been built (successfully or
        ///                                 unsuccessfully). If <paramref name="notify"/> is not <c>null</c>,
        ///                                 <see cref="ComputeProgram.Build"/> does not need to wait for the
        ///                                 build to complete and can return immediately. If
        ///                                 <paramref name="notify"/> is <c>null</c>,
        ///                                 <see cref="ComputeProgram.Build"/> does not return until the
        ///                                 build has completed. The callback function may be called
        ///                                 asynchronously by the OpenCL implementation. It is the
        ///                                 application's responsibility to ensure that the callback function
        ///                                 is thread-safe and that the delegate instance doesn't get
        ///                                 collected by the Garbage Collector until the build operation
        ///                                 triggers the callback. </param>
        /// <param name="notifyDataPtr">    Optional user data that will be passed to
        ///                                 <paramref name="notify"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public void Build(ICollection <ComputeDevice> devices, string options, ComputeProgramBuildNotifier notify, IntPtr notifyDataPtr)
        {
            Ensure.Argument(devices).NotNull("devices is null");

            CLDeviceHandle[] deviceHandles = ComputeTools.ExtractHandles(devices, out var handleCount);
            buildOptions = options ?? "";
            buildNotify  = notify;

            ComputeErrorCode error = CL12.BuildProgram(Handle, handleCount, deviceHandles, options, buildNotify, notifyDataPtr);

            ComputeException.ThrowOnError(error);
        }
Пример #22
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Creates a new <see cref="ComputeProgram"/> from an array of source code strings.
        /// </summary>
        ///
        /// <remarks>
        /// The created <see cref="ComputeProgram"/> is associated with the
        /// <see cref="ComputeContext.Devices"/>.
        /// </remarks>
        ///
        /// <param name="context">  A <see cref="ComputeContext"/>. </param>
        /// <param name="source">   The source code lines for the <see cref="ComputeProgram"/>. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeProgram(ComputeContext context, string[] source)
        {
            Ensure.Argument(context).NotNull("context is null");

            Handle = CL12.CreateProgramWithSource(context.Handle, source.Length, source, null, out var error);
            ComputeException.ThrowOnError(error);

            this.context = context;
            devices      = context.Devices;
            this.source  = new ReadOnlyCollection <string>(source);

            RILogManager.Default?.SendTrace("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Пример #23
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Initializes a new instance of the Cloo.ComputeKernel class. </summary>
        ///
        /// <param name="functionName"> Gets the function name of the <see cref="ComputeKernel"/>. </param>
        /// <param name="program">      Gets the <see cref="ComputeProgram"/> that the
        ///                             <see cref="ComputeKernel"/> belongs to. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ComputeKernel(string functionName, ComputeProgram program)
        {
            Handle = CL12.CreateKernel(program.Handle, functionName, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            context           = program.Context;
            this.functionName = functionName;
            this.program      = program;

            RILogManager.Default?.SendTrace(string.Intern("Create ") + this + string.Intern(" in Thread(") + Thread.CurrentThread.ManagedThreadId + string.Intern(")."), string.Intern("Information"));
        }
Пример #24
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Creates a new <see cref="ComputeSampler"/>. </summary>
        ///
        /// <param name="context">          A <see cref="ComputeContext"/>. </param>
        /// <param name="normalizedCoords"> The usage state of normalized coordinates when accessing a
        ///                                 <see cref="ComputeImage"/> in a <see cref="ComputeKernel"/>. </param>
        /// <param name="addressing">       The <see cref="ComputeImageAddressing"/> mode of the
        ///                                 <see cref="ComputeSampler"/>. Specifies how out-of-range
        ///                                 image coordinates are handled while reading. </param>
        /// <param name="filtering">        The <see cref="ComputeImageFiltering"/> mode of the
        ///                                 <see cref="ComputeSampler"/>. Specifies the type of filter
        ///                                 that must be applied when reading data from an image. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        public ComputeSampler(ComputeContext context, bool normalizedCoords, ComputeImageAddressing addressing, ComputeImageFiltering filtering)
        {
            Handle = CL12.CreateSampler(context.Handle, normalizedCoords, addressing, filtering, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.addressing       = addressing;
            this.context          = context;
            this.filtering        = filtering;
            this.normalizedCoords = normalizedCoords;

            RILogManager.Default?.SendTrace(string.Intern("Create ") + this + string.Intern(" in Thread(") + Thread.CurrentThread.ManagedThreadId + string.Intern(")."), string.Intern("Information"));
        }
Пример #25
0
        /// <summary>
        /// Creates a new <see cref="ComputeProgram"/> from a specified list of binaries.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="binaries"> A list of binaries, one for each item in <paramref name="devices"/>. </param>
        /// <param name="devices"> A subset of the <see cref="ComputeContext.Devices"/>. If <paramref name="devices"/> is <c>null</c>, OpenCL will associate every binary from <see cref="ComputeProgram.Binaries"/> with a corresponding <see cref="ComputeDevice"/> from <see cref="ComputeContext.Devices"/>. </param>
        public ComputeProgram(ComputeContext context, IList <byte[]> binaries, IList <ComputeDevice> devices)
        {
            int count;

            CLDeviceHandle[] deviceHandles = (devices != null) ?
                                             ComputeTools.ExtractHandles(devices, out count) :
                                             ComputeTools.ExtractHandles(context.Devices, out count);

            IntPtr[]         binariesPtrs    = new IntPtr[count];
            IntPtr[]         binariesLengths = new IntPtr[count];
            int[]            binariesStats   = new int[count];
            ComputeErrorCode error           = ComputeErrorCode.Success;

            GCHandle[] binariesGCHandles = new GCHandle[count];

            try
            {
                for (int i = 0; i < count; i++)
                {
                    binariesGCHandles[i] = GCHandle.Alloc(binaries[i], GCHandleType.Pinned);
                    binariesPtrs[i]      = binariesGCHandles[i].AddrOfPinnedObject();
                    binariesLengths[i]   = new IntPtr(binaries[i].Length);
                }

                Handle = CL12.CreateProgramWithBinary(
                    context.Handle,
                    count,
                    deviceHandles,
                    binariesLengths,
                    binariesPtrs,
                    binariesStats,
                    out error);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                for (int i = 0; i < count; i++)
                {
                    binariesGCHandles[i].Free();
                }
            }


            this.binaries = new ReadOnlyCollection <byte[]>(binaries);
            this.context  = context;
            this.devices  = new ReadOnlyCollection <ComputeDevice>(
                (devices != null) ? devices : context.Devices);

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Пример #26
0
        /// <summary>
        /// Releases the associated OpenCL object.
        /// </summary>
        /// <param name="manual"> Specifies the operation mode of this method. </param>
        /// <remarks> <paramref name="manual"/> must be <c>true</c> if this method is invoked directly by the application. </remarks>
        protected override void Dispose(bool manual)
        {
            if (manual)
            {
                //free managed resources
            }

            // free native resources
            if (Handle.IsValid)
            {
                Trace.WriteLine("Dispose " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
                CL12.ReleaseContext(Handle);
                Handle.Invalidate();
            }
        }
        ////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>   Initializes a new instance of the Cloo.ComputeKernel class. </summary>
        ///
        /// <param name="functionName"> Gets the function name of the <see cref="ComputeKernel"/>. </param>
        /// <param name="program">      Gets the <see cref="ComputeProgram"/> that the
        ///                             <see cref="ComputeKernel"/> belongs to. </param>
        ////////////////////////////////////////////////////////////////////////////////////////////////////

        internal ComputeKernel(string functionName, ComputeProgram program)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateKernel(program.Handle, functionName, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            context           = program.Context;
            this.functionName = functionName;
            this.program      = program;

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Пример #28
0
        public XArrayMemory(ComputeContext context, ComputeMemoryFlags flags, XArray obj) : base(context, flags)
        {
            var hostPtr = IntPtr.Zero;

            if ((flags & (ComputeMemoryFlags.CopyHostPointer | ComputeMemoryFlags.UseHostPointer)) != ComputeMemoryFlags.None)
            {
                hostPtr = obj.NativePtr;
            }

            ComputeErrorCode error = ComputeErrorCode.Success;
            var handle             = CL12.CreateBuffer(context.Handle, flags, new IntPtr(obj.Count), hostPtr, out error);

            this.Size   = obj.Count;
            this.Handle = handle;
        }
Пример #29
0
        /// <summary>
        /// Creates a new <see cref="ComputeProgram"/> from a source code string.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="source"> The source code for the <see cref="ComputeProgram"/>. </param>
        /// <remarks> The created <see cref="ComputeProgram"/> is associated with the <see cref="ComputeContext.Devices"/>. </remarks>
        public ComputeProgram(ComputeContext context, string source)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateProgramWithSource(context.Handle, 1, new string[] { source }, null, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.context = context;
            this.devices = context.Devices;
            this.source  = new ReadOnlyCollection <string>(new string[] { source });

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Пример #30
0
        /// <summary>
        /// Creates a new <see cref="ComputeSampler"/>.
        /// </summary>
        /// <param name="context"> A <see cref="ComputeContext"/>. </param>
        /// <param name="normalizedCoords"> The usage state of normalized coordinates when accessing a <see cref="ComputeImage"/> in a <see cref="ComputeKernel"/>. </param>
        /// <param name="addressing"> The <see cref="ComputeImageAddressing"/> mode of the <see cref="ComputeSampler"/>. Specifies how out-of-range image coordinates are handled while reading. </param>
        /// <param name="filtering"> The <see cref="ComputeImageFiltering"/> mode of the <see cref="ComputeSampler"/>. Specifies the type of filter that must be applied when reading data from an image. </param>
        public ComputeSampler(ComputeContext context, bool normalizedCoords, ComputeImageAddressing addressing, ComputeImageFiltering filtering)
        {
            ComputeErrorCode error = ComputeErrorCode.Success;

            Handle = CL12.CreateSampler(context.Handle, normalizedCoords, addressing, filtering, out error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            this.addressing       = addressing;
            this.context          = context;
            this.filtering        = filtering;
            this.normalizedCoords = normalizedCoords;

            Trace.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }