Ejemplo n.º 1
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));
        }
Ejemplo n.º 2
0
        internal ComputeSubBuffer(ComputeContext context, CLMemoryHandle handle, ComputeMemoryFlags flags)
            : base(context, flags)
        {
            Handle = handle;

            Init();
        }
Ejemplo n.º 3
0
        private ComputeImage3D(CLMemoryHandle handle, ComputeContext context, ComputeMemoryFlags flags)
            : base(context, flags)
        {
            Handle = handle;

            Init();
        }
Ejemplo n.º 4
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));
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a new ComputeBuffer from external memory handles.
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static ComputeBuffer <T> From(IntPtr handle, ComputeContext context)
        {
            var memoryHandle = new CLMemoryHandle(handle);

            var flags = (ComputeMemoryFlags)GetInfo <CLMemoryHandle, ComputeMemoryInfo, long>(memoryHandle, ComputeMemoryInfo.Flags, CL12.GetMemObjectInfo);

            return(new ComputeBuffer <T>(memoryHandle, context, flags));
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        internal ComputeUserEvent(ComputeContext context, CLEventHandle handle, ComputeCommandType type)
        {
            Handle = handle;

            SetID(Handle.Value);

            Type = type;

            Context = context;
        }
Ejemplo n.º 10
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));
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates a new <see cref="ComputeUserEvent"/>.
        /// </summary>
        /// <param name="context"> The <see cref="ComputeContext"/> in which the <see cref="ComputeUserEvent"/> is created. </param>
        /// <remarks> Requires OpenCL 1.1. </remarks>
        public ComputeUserEvent(ComputeContext context)
        {
            Handle = CL11.CreateUserEvent(context.Handle, out var error);
            ComputeException.ThrowOnError(error);

            SetID(Handle.Value);

            Type    = (ComputeCommandType)GetInfo <CLEventHandle, ComputeEventInfo, uint>(Handle, ComputeEventInfo.CommandType, CL12.GetEventInfo);
            Context = context;
            //HookNotifier();

            //Debug.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information");
        }
Ejemplo n.º 12
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;
        }
Ejemplo n.º 13
0
        public GenericArrayMemory(ComputeContext context, ComputeMemoryFlags flags, object obj) : base(context, flags)
        {
            Array array = (Array)obj;

            if (array.Length == 0)
            {
                return;
            }

            int size               = Marshal.SizeOf(array.GetValue(0).GetType()) * array.Length;
            var datagch            = GCHandle.Alloc(obj, GCHandleType.Pinned);
            ComputeErrorCode error = ComputeErrorCode.Success;
            var handle             = CL12.CreateBuffer(context.Handle, ComputeMemoryFlags.ReadWrite | ComputeMemoryFlags.CopyHostPointer, new IntPtr(size), datagch.AddrOfPinnedObject(), out error);

            this.Size   = size;
            this.Handle = handle;
        }
Ejemplo n.º 14
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="data"> The data for the <see cref="ComputeBuffer{T}"/>. </param>
        /// <remarks> Note, that <paramref name="data"/> cannot be an "immediate" parameter, i.e.: <c>new T[100]</c>, because it could be quickly collected by the GC causing Amplifier.OpenCL.Cloo to send and invalid reference to OpenCL. </remarks>
        public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, T[] data)
            : base(context, flags)
        {
            var size = ComputeTools.SizeOf <T>() * data.Length;

            GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned);

            try
            {
                Handle = CL12.CreateBuffer(context.Handle, flags, new IntPtr(size), dataPtr.AddrOfPinnedObject(), out var error);
                ComputeException.ThrowOnError(error);
            }
            finally
            {
                dataPtr.Free();
            }

            Init(size, data.Length);
        }
Ejemplo n.º 15
0
        public GenericArrayMemory(ComputeContext context, ComputeMemoryFlags flags, Array array) : base(context, flags)
        {
            if (array.Length == 0)
            {
                return;
            }

            int size    = Marshal.SizeOf(array.GetValue(0).GetType()) * array.Length;
            var hostPtr = IntPtr.Zero;

            if ((flags & (ComputeMemoryFlags.CopyHostPointer | ComputeMemoryFlags.UseHostPointer)) != ComputeMemoryFlags.None)
            {
                var datagch = GCHandle.Alloc(array, GCHandleType.Pinned);
                hostPtr = datagch.AddrOfPinnedObject();
            }

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

            this.Size   = size;
            this.Handle = handle;
        }
Ejemplo n.º 16
0
 public GenericArrayMemory(ComputeContext context, ComputeMemoryFlags flags) : base(context, flags)
 {
 }
Ejemplo n.º 17
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <param name="flags"></param>
 protected ComputeBufferBase(ComputeContext context, ComputeMemoryFlags flags)
     : base(context, flags)
 {
 }
Ejemplo n.º 18
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="context"></param>
 /// <param name="flags"></param>
 protected ComputeImage(ComputeContext context, ComputeMemoryFlags flags)
     : base(context, flags)
 {
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Gets a collection of supported <see cref="ComputeImage3D"/> <see cref="ComputeImageFormat"/>s in a <see cref="ComputeContext"/>.
 /// </summary>
 /// <param name="context"> The <see cref="ComputeContext"/> for which the collection of <see cref="ComputeImageFormat"/>s is queried. </param>
 /// <param name="flags"> The <c>ComputeMemoryFlags</c> for which the collection of <see cref="ComputeImageFormat"/>s is queried. </param>
 /// <returns> The collection of the required <see cref="ComputeImageFormat"/>s. </returns>
 public static ICollection <ComputeImageFormat> GetSupportedFormats(ComputeContext context, ComputeMemoryFlags flags)
 {
     return(GetSupportedFormats(context, flags, ComputeMemoryType.Image3D));
 }
Ejemplo n.º 20
0
 private ComputeBuffer(CLMemoryHandle handle, ComputeContext context, ComputeMemoryFlags flags, long size, long count)
     : base(context, flags)
 {
     Handle = handle;
     Init(size, count);
 }
Ejemplo n.º 21
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>
 public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, long count)
     : this(context, flags, count, IntPtr.Zero)
 {
 }