/// <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> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="ComputeEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> If <paramref name="blocking"/> is <c>true</c> this method will not return until the command completes. If <paramref name="blocking"/> is <c>false</c> 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 = HDSPUtils.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 = CL10.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); }
/// <summary> /// /// </summary> protected void Init() { SetID(Handle.Value); Size = (long)GetInfo<CLMemoryHandle, ComputeMemoryInfo, IntPtr>(Handle, ComputeMemoryInfo.Size, CL10.GetMemObjectInfo); Count = Size / HDSPUtils.SizeOf(typeof(T)); //Console.WriteLine("Create " + this + " in Thread(" + Thread.CurrentThread.ManagedThreadId + ").", "Information"); }
/// <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 = CL10.CreateBuffer(context.Handle, flags, new IntPtr(HDSPUtils.SizeOf(typeof(T)) * count), dataPtr, out error); ComputeException.ThrowOnError(error); Init(); }
/// <summary> /// Creates a new <see cref="ComputeSubBuffer{T}"/> from a specified <see cref="ComputeBuffer{T}"/>. /// </summary> /// <param name="buffer"> The buffer to create the <see cref="ComputeSubBuffer{T}"/> from. </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="offset"> The index of the element of <paramref name="buffer"/>, where the <see cref="ComputeSubBuffer{T}"/> starts. </param> /// <param name="count"> The number of elements of <paramref name="buffer"/> to include in the <see cref="ComputeSubBuffer{T}"/>. </param> public ComputeSubBuffer(ComputeBuffer <T> buffer, ComputeMemoryFlags flags, long offset, long count) : base(buffer.Context, flags) { SysIntX2 region = new SysIntX2(offset * HDSPUtils.SizeOf(typeof(T)), count * HDSPUtils.SizeOf(typeof(T))); ComputeErrorCode error; CLMemoryHandle handle = CL11.CreateSubBuffer(Handle, flags, ComputeBufferCreateType.Region, ref region, out error); ComputeException.ThrowOnError(error); Init(); }
/// <summary> /// Sets a value argument of the <see cref="ComputeKernel"/>. /// </summary> /// <typeparam name="T"> The type of the argument. </typeparam> /// <param name="index"> The argument index. </param> /// <param name="data"> The data that is passed as the argument value. </param> /// <remarks> Arguments to the kernel are referred by indices that go from 0 for the leftmost argument to n-1, where n is the total number of arguments declared by the kernel. </remarks> public void SetValueArgument <T>(int index, T data) where T : struct { GCHandle gcHandle = GCHandle.Alloc(data, GCHandleType.Pinned); try { SetArgument( index, new IntPtr(HDSPUtils.SizeOf(typeof(T))), gcHandle.AddrOfPinnedObject()); } finally { gcHandle.Free(); } }
/// <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 Cloo to send and invalid reference to OpenCL. </remarks> public ComputeBuffer(ComputeContext context, ComputeMemoryFlags flags, T[] data) : base(context, flags) { GCHandle dataPtr = GCHandle.Alloc(data, GCHandleType.Pinned); try { ComputeErrorCode error = ComputeErrorCode.Success; Handle = CL10.CreateBuffer(context.Handle, flags, new IntPtr(HDSPUtils.SizeOf(typeof(T)) * data.Length), dataPtr.AddrOfPinnedObject(), out error); ComputeException.ThrowOnError(error); } finally { dataPtr.Free(); } Init(); }
/// <summary> /// /// </summary> /// <typeparam name="HandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <typeparam name="QueriedType"></typeparam> /// <param name="handle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected QueriedType GetInfo <HandleType, InfoType, QueriedType> (HandleType handle, InfoType paramName, GetInfoDelegate <HandleType, InfoType> getInfoDelegate) where QueriedType : struct { QueriedType result = new QueriedType(); GCHandle gcHandle = GCHandle.Alloc(result, GCHandleType.Pinned); try { IntPtr sizeRet; ComputeErrorCode error = getInfoDelegate(handle, paramName, (IntPtr)HDSPUtils.SizeOf(result.GetType()), gcHandle.AddrOfPinnedObject(), out sizeRet); ComputeException.ThrowOnError(error); } finally { result = (QueriedType)gcHandle.Target; gcHandle.Free(); } return(result); }
/// <summary> /// Added by Hybrid DSP /// </summary> /// <typeparam name="T"></typeparam> /// <param name="destinationHandle">The destination.</param> /// <param name="blocking">if set to <c>true</c> [blocking].</param> /// <param name="destinationOffset">The destination offset.</param> /// <param name="region">The region.</param> /// <param name="source">The source.</param> /// <param name="events">The events.</param> public void WriteEx <T>(CLMemoryHandle destinationHandle, bool blocking, long destinationOffset, long region, IntPtr source, ICollection <ComputeEventBase> events) where T : struct { int sizeofT = HDSPUtils.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; ComputeErrorCode error = CL10.EnqueueWriteBuffer(Handle, destinationHandle, blocking, new IntPtr(destinationOffset * sizeofT), new IntPtr(region * sizeofT), source, eventWaitListSize, eventHandles, newEventHandle); ComputeException.ThrowOnError(error); if (eventsWritable) { events.Add(new ComputeEvent(newEventHandle[0], this)); } }
/// <summary> /// Enqueues a command to copy data from <see cref="ComputeImage"/> to buffer. /// </summary> /// <param name="source"> The image to copy from. </param> /// <param name="destination"> The buffer to copy to. </param> /// <param name="sourceOffset"> The <paramref name="source"/> element position where reading starts. </param> /// <param name="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param> /// <param name="region"> The region of elements to copy. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="ComputeEvent"/> identifying this command is created and attached to the end of the collection. </param> public void Copy <T>(ComputeImage source, ComputeBufferBase <T> destination, SysIntX3 sourceOffset, long destinationOffset, SysIntX3 region, ICollection <ComputeEventBase> events) where T : struct { int sizeofT = HDSPUtils.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; ComputeErrorCode error = CL10.EnqueueCopyImageToBuffer(Handle, source.Handle, destination.Handle, ref sourceOffset, ref region, new IntPtr(destinationOffset * sizeofT), eventWaitListSize, eventHandles, newEventHandle); ComputeException.ThrowOnError(error); if (eventsWritable) { events.Add(new ComputeEvent(newEventHandle[0], this)); } }
/// <summary> /// /// </summary> /// <typeparam name="MainHandleType"></typeparam> /// <typeparam name="SecondHandleType"></typeparam> /// <typeparam name="InfoType"></typeparam> /// <typeparam name="QueriedType"></typeparam> /// <param name="mainHandle"></param> /// <param name="secondHandle"></param> /// <param name="paramName"></param> /// <param name="getInfoDelegate"></param> /// <returns></returns> protected QueriedType[] GetArrayInfo <MainHandleType, SecondHandleType, InfoType, QueriedType> (MainHandleType mainHandle, SecondHandleType secondHandle, InfoType paramName, GetInfoDelegateEx <MainHandleType, SecondHandleType, InfoType> getInfoDelegate) { ComputeErrorCode error; QueriedType[] buffer; IntPtr bufferSizeRet; error = getInfoDelegate(mainHandle, secondHandle, paramName, IntPtr.Zero, IntPtr.Zero, out bufferSizeRet); ComputeException.ThrowOnError(error); buffer = new QueriedType[bufferSizeRet.ToInt64() / HDSPUtils.SizeOf(typeof(QueriedType))]; GCHandle gcHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { error = getInfoDelegate(mainHandle, secondHandle, paramName, bufferSizeRet, gcHandle.AddrOfPinnedObject(), out bufferSizeRet); ComputeException.ThrowOnError(error); } finally { gcHandle.Free(); } return(buffer); }
/// <summary> /// Enqueues a command to write a 2D or 3D region of elements to a buffer. /// </summary> /// <typeparam name="T"> The type of the elements of the buffer. </typeparam> /// <param name="destination"> The buffer to write to. </param> /// <param name="blocking"> The mode of operation of this command. If <c>true</c> this call will not return until the command has finished execution. </param> /// <param name="destinationOffset"> The <paramref name="destination"/> element position where writing starts. </param> /// <param name="sourceOffset"> The <paramref name="source"/> element position where reading starts. </param> /// <param name="region"> The region of elements to copy. </param> /// <param name="destinationRowPitch"> The size of the destination buffer row in bytes. If set to zero then <paramref name="destinationRowPitch"/> equals <c>region.X * sizeof(T)</c>. </param> /// <param name="destinationSlicePitch"> The size of the destination buffer 2D slice in bytes. If set to zero then <paramref name="destinationSlicePitch"/> equals <c>region.Y * sizeof(T) * destinationRowPitch</c>. </param> /// <param name="sourceRowPitch"> The size of the memory area row in bytes. If set to zero then <paramref name="sourceRowPitch"/> equals <c>region.X * sizeof(T)</c>. </param> /// <param name="sourceSlicePitch"> The size of the memory area 2D slice in bytes. If set to zero then <paramref name="sourceSlicePitch"/> equals <c>region.Y * sizeof(T) * sourceRowPitch</c>. </param> /// <param name="source"> The data written to the buffer. </param> /// <param name="events"> A collection of events that need to complete before this particular command can be executed. If <paramref name="events"/> is not <c>null</c> or read-only a new <see cref="ComputeEvent"/> identifying this command is created and attached to the end of the collection. </param> /// <remarks> Requires OpenCL 1.1. </remarks> private void Write <T>(ComputeBufferBase <T> destination, bool blocking, SysIntX3 sourceOffset, SysIntX3 destinationOffset, SysIntX3 region, long destinationRowPitch, long destinationSlicePitch, long sourceRowPitch, long sourceSlicePitch, IntPtr source, ICollection <ComputeEventBase> events) where T : struct { int sizeofT = HDSPUtils.SizeOf(typeof(T)); sourceOffset.X = new IntPtr(sizeofT * sourceOffset.X.ToInt64()); destinationOffset.X = new IntPtr(sizeofT * destinationOffset.X.ToInt64()); region.X = new IntPtr(sizeofT * region.X.ToInt64()); int eventWaitListSize; CLEventHandle[] eventHandles = ComputeTools.ExtractHandles(events, out eventWaitListSize); bool eventsWritable = (events != null && !events.IsReadOnly); CLEventHandle[] newEventHandle = (eventsWritable) ? new CLEventHandle[1] : null; ComputeErrorCode error = CL11.EnqueueWriteBufferRect(this.Handle, destination.Handle, blocking, ref destinationOffset, ref sourceOffset, ref region, new IntPtr(destinationRowPitch), new IntPtr(destinationSlicePitch), new IntPtr(sourceRowPitch), new IntPtr(sourceSlicePitch), source, eventWaitListSize, eventHandles, newEventHandle); ComputeException.ThrowOnError(error); if (eventsWritable) { events.Add(new ComputeEvent(newEventHandle[0], this)); } }