public void TestBufferReadWriteRaw() { checkSetup(); CommandQueue queue = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable); Buffer buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16); Float4 vec = new Float4(); vec.X = 1.0f; vec.Y = 2.0f; vec.Z = 3.0f; vec.W = 4.0f; IntPtr vecBuff = Marshal.AllocHGlobal(16); try{ Marshal.StructureToPtr(vec, vecBuff, false); unsafe { Event writeEvent; buffer.EnqueueWriteRaw(queue, (IntPtr)0, (IntPtr)16, vecBuff, out writeEvent); writeEvent.Wait(); float v0, v1, v2, v3; buffer.ReadRaw(queue, (IntPtr)0, (IntPtr)4, (IntPtr)(&v0)); buffer.ReadRaw(queue, (IntPtr)4, (IntPtr)4, (IntPtr)(&v1)); buffer.ReadRaw(queue, (IntPtr)8, (IntPtr)4, (IntPtr)(&v2)); buffer.ReadRaw(queue, (IntPtr)12, (IntPtr)4, (IntPtr)(&v3)); Assert.AreEqual(1.0f, v0); Assert.AreEqual(2.0f, v1); Assert.AreEqual(3.0f, v2); Assert.AreEqual(4.0f, v3); float k = 9.0f; buffer.WriteRaw(queue, (IntPtr)4, (IntPtr)4, (IntPtr)(&k)); buffer.ReadRaw(queue, (IntPtr)4, (IntPtr)4, (IntPtr)(&v1)); Assert.AreEqual(9.0f, v1); } } finally{ Marshal.FreeHGlobal(vecBuff); buffer.Dispose(); queue.Dispose(); } }
public void Write <T>(CommandQueue queue, T[] values) where T : struct { if (values == null) { throw new ArgumentException("values array is null", "values"); } int elemSize = Marshal.SizeOf(typeof(T)); IntPtr arrSize = (IntPtr)(values.Length * elemSize); if ((ulong)arrSize > (ulong)this.Size) { throw new OpenCLBufferSizeException("Buffer is too small for array."); } IntPtr buf = this.Map(queue, MapFlags.Write, (IntPtr)0, arrSize); try{ copyArrayToPtr(values, buf); } finally{ this.Unmap(queue, ref buf); } }
public IntPtr EnqueueMap(CommandQueue queue, MapFlags flags, Offset2D origin, Dim2D region, out IntPtr pitch, Event[] waitList, out Event evt) { Offset3D origin_buf = new Offset3D(origin.X, origin.Y, (IntPtr)0); Dim3D region_buf = new Dim3D(region.X, region.Y, (IntPtr)1); IntPtr tempPitch = IntPtr.Zero; uint waitCount = (waitList == null ? 0 : (uint)waitList.Length); IntPtr[] wlh = CLObject.GetHandles(waitList); IntPtr evtHandle = IntPtr.Zero; ErrorCode errorCode; IntPtr result = IntPtr.Zero; unsafe { result = Native.EnqueueMapImage(queue.Handle, this.Handle, true, flags, &origin_buf, ®ion_buf, &tempPitch, null, waitCount, wlh, &evtHandle, &errorCode); } if (errorCode != ErrorCode.Success) { throw new OpenCLCallFailedException(errorCode); } pitch = tempPitch; evt = new Event(evtHandle); return(result); }
public void TestBufferMap() { checkSetup(); CommandQueue queue = CommandQueue.Create(context, device, CommandQueueFlags.ProfilingEnable); Buffer buffer = Buffer.Create(context, MemFlags.ReadWrite, (IntPtr)16); IntPtr writePtr = buffer.Map(queue, MapFlags.Write, (IntPtr)0, (IntPtr)16); try{ unsafe { float *arr = (float *)writePtr; arr[0] = 2.0f; arr[1] = 4.0f; arr[2] = 6.0f; arr[3] = 8.0f; } } finally{ buffer.Unmap(queue, ref writePtr); } Assert.AreEqual(IntPtr.Zero, writePtr); float sum = 0.0f; IntPtr readPtr = buffer.Map(queue, MapFlags.Read, (IntPtr)0, (IntPtr)16); try{ unsafe { float *arr = (float *)readPtr; sum = arr[0] + arr[1] + arr[2] + arr[3]; } } finally{ buffer.Unmap(queue, ref readPtr); } Assert.AreEqual(IntPtr.Zero, readPtr); Assert.AreEqual(20.0f, sum); }
public void CopyTo(Image2D dstImage, CommandQueue queue, int srcOriginX, int srcOriginY, int dstOriginX, int dstOriginY, int width, int height) { IntPtr evtHandle = IntPtr.Zero; IntPtr[] srcOrg = new IntPtr[] { (IntPtr)srcOriginX, (IntPtr)srcOriginY, (IntPtr)0 }; IntPtr[] dstOrg = new IntPtr[] { (IntPtr)dstOriginX, (IntPtr)dstOriginY, (IntPtr)0 }; IntPtr[] region = new IntPtr[] { (IntPtr)width, (IntPtr)height, (IntPtr)1 }; unsafe { fixed(IntPtr *p_srcOrg = srcOrg, p_dstOrg = dstOrg, p_region = region) { Native.Call(Native.EnqueueCopyImage( queue.Handle, this.Handle, dstImage.Handle, p_srcOrg, p_dstOrg, p_region, 0, null, &evtHandle)); } } Event evt = new Event(evtHandle); evt.Wait(); evt.Dispose(); }
public unsafe void WriteRaw(CommandQueue queue, IntPtr offset, IntPtr size, IntPtr srcPtr) { Native.Call(Native.EnqueueWriteBuffer(queue.Handle, this.handle, true, offset, size, srcPtr, 0, null, null)); }
public unsafe void ReadRaw(CommandQueue queue, IntPtr offset, IntPtr size, IntPtr destPtr) { Native.Call(Native.EnqueueReadBuffer(queue.Handle, this.handle, true, offset, size, destPtr, 0, null, null)); }
/// <summary> /// Enqueue a three dimensional kernel for launch. /// </summary> public void EnqueueLaunch(CommandQueue queue, int globalWidth, int globalHeight, int globalDepth, int localWidth, int localHeight, int localDepth, out Event evt) { this.EnqueueLaunch(queue, new Dim3D(globalWidth, globalHeight, globalDepth), new Dim3D(localWidth, localHeight, localDepth), out evt); }
/// <summary> /// Enqueue a one dimensional kernel for launch. /// </summary> public void EnqueueLaunch(CommandQueue queue, int globalWorkSize, int localWorkSize, Event[] waitList, out Event evt) { this.EnqueueLaunch(queue, (IntPtr)globalWorkSize, (IntPtr)localWorkSize, waitList, out evt); }