Beispiel #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();
            }
        }
Beispiel #2
0
        public static Image CreateImage3D(Context context, MemoryFlags flags,
                                          ChannelOrder order, ChannelType type, ulong width, ulong height, ulong depth,
                                          ulong rowPitch, ulong slicePitch, 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 (depth == 0)
            {
                throw new ArgumentOutOfRangeException("depth", depth, "depth 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 (rowPitch != 0)
                {
                    throw new ArgumentOutOfRangeException("rowPitch", rowPitch, "rowPitch is not 0.");
                }
                if (slicePitch != 0)
                {
                    throw new ArgumentOutOfRangeException("slicePitch", slicePitch, "slicePitch 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 (rowPitch != 0 && rowPitch < width)
                {
                    throw new ArgumentOutOfRangeException("rowPitch", rowPitch, "rowPitch is not 0 and is less than width.");
                }
                if (slicePitch != 0 && slicePitch < height)
                {
                    throw new ArgumentOutOfRangeException("slicePitch", slicePitch, "slicePitch is not 0 and is less than height.");
                }
            }

            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.CreateImage3D(
                    context.Handle, (ulong)flags,
                    &image_format,
                    new UIntPtr(width), new UIntPtr(height), new UIntPtr(depth),
                    new UIntPtr(rowPitch), new UIntPtr(slicePitch), hostPtr.ToPointer(), &error);
                ClHelper.GetError(error);

                return(new Image(handle));
            }
        }