Пример #1
0
        /// <summary>
        /// create host visible linear image without command from data pointed by IntPtr pointer containing full image file (jpg, png,...)
        /// </summary>
        public static Image Load(Device dev,
                                 IntPtr bitmap, ulong bitmapByteCount, VkImageUsageFlags usage = VkImageUsageFlags.TransferSrc,
                                 VkFormat format = VkFormat.Undefined,
                                 VkMemoryPropertyFlags memoryProps = VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent,
                                 VkImageTiling tiling  = VkImageTiling.Linear, bool generateMipmaps = false,
                                 VkImageType imageType = VkImageType.Image2D)
        {
            if (format == VkFormat.Undefined)
            {
                format = DefaultTextureFormat;
            }
            if (generateMipmaps)
            {
                usage |= (VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst);
            }

            using (StbImage stbi = new StbImage(bitmap, bitmapByteCount)) {
                uint mipLevels = generateMipmaps ? ComputeMipLevels(stbi.Width, stbi.Height) : 1;

                Image img = new Image(dev, format, usage, memoryProps, (uint)stbi.Width, (uint)stbi.Height, imageType,
                                      VkSampleCountFlags.SampleCount1, tiling, mipLevels);

                img.Map();
                stbi.CoptyTo(img.MappedData);
                img.Unmap();

                return(img);
            }
        }
Пример #2
0
        /// <summary>
        /// Load bitmap into Image with stagging and mipmap generation if necessary
        /// and usage.
        /// </summary>
        public static Image Load(Device dev, Queue staggingQ, CommandPool staggingCmdPool,
                                 string path, VkFormat format      = VkFormat.Undefined,
                                 VkMemoryPropertyFlags memoryProps = VkMemoryPropertyFlags.DeviceLocal,
                                 VkImageTiling tiling    = VkImageTiling.Optimal, bool generateMipmaps = true,
                                 VkImageType imageType   = VkImageType.Image2D,
                                 VkImageUsageFlags usage = VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst)
        {
            if (format == VkFormat.Undefined)
            {
                format = DefaultTextureFormat;
            }
            if (tiling == VkImageTiling.Optimal)
            {
                usage |= VkImageUsageFlags.TransferDst;
            }
            if (generateMipmaps)
            {
                usage |= (VkImageUsageFlags.TransferSrc | VkImageUsageFlags.TransferDst);
            }

            using (StbImage stbi = new StbImage(path)) {
                uint mipLevels = generateMipmaps ? ComputeMipLevels(stbi.Width, stbi.Height) : 1;

                Image img = new Image(dev, format, usage, memoryProps, (uint)stbi.Width, (uint)stbi.Height, imageType,
                                      VkSampleCountFlags.SampleCount1, tiling, mipLevels);

                img.load(staggingQ, staggingCmdPool, stbi.Handle, generateMipmaps);

                return(img);
            }
        }
Пример #3
0
        public void Dispose()
        {
#if STB_SHARP
            gcHandle.Free();
#else
            StbImage.FreeImage(Handle);
#endif
        }
Пример #4
0
        /// <summary>
        /// Open image with STBI library
        /// </summary>
        /// <param name="bitmap">raw bitmap datas</param>
        /// <param name="requestedChannels">Force returned channels count, set 0 for original count</param>
        public StbImage(Memory <byte> bitmap, int requestedChannels = 4)
        {
#if STB_SHARP
            StbImageSharp.ImageResult stbi =
                StbImageSharp.ImageResult.FromMemory(bitmap.ToArray(), (StbImageSharp.ColorComponents)requestedChannels);
            Width    = stbi.Width;
            Height   = stbi.Height;
            Channels = (int)stbi.Comp;
            gcHandle = GCHandle.Alloc(stbi.Data, GCHandleType.Pinned);
#else
            Handle = StbImage.Load(ref MemoryMarshal.GetReference(bitmap.Span), bitmap.Length, out Width, out Height, out Channels, requestedChannels);
            if (Handle == IntPtr.Zero)
            {
                throw new Exception($"STBI image loading error.");
            }
#endif
            if (requestedChannels > 0)
            {
                Channels = requestedChannels;
            }
        }
Пример #5
0
        /// <summary>
        /// Open image with STBI library
        /// </summary>
        /// <param name="path">file path</param>
        /// <param name="requestedChannels">Force returned channels count, set 0 for original count</param>
        public StbImage(string path, int requestedChannels = 4)
        {
#if STB_SHARP
            using (Stream stream = new FileStream(path, FileMode.Open)) {
                StbImageSharp.ImageResult stbi =
                    StbImageSharp.ImageResult.FromStream(stream, (StbImageSharp.ColorComponents)requestedChannels);
                Width    = stbi.Width;
                Height   = stbi.Height;
                Channels = (int)stbi.Comp;
                gcHandle = GCHandle.Alloc(stbi.Data, GCHandleType.Pinned);
            }
#else
            Handle = StbImage.Load(path, out Width, out Height, out Channels, requestedChannels);
            if (Handle == IntPtr.Zero)
            {
                throw new Exception($"STBI image loading error.");
            }
#endif
            if (requestedChannels > 0)
            {
                Channels = requestedChannels;
            }
        }
Пример #6
0
        /// <summary>
        /// create host visible linear image without command from path
        /// </summary>
        public static Image Load(Device dev,
                                 string path, VkImageUsageFlags usage = VkImageUsageFlags.Sampled, bool reserveSpaceForMipmaps = true, VkFormat format = VkFormat.Undefined,
                                 VkMemoryPropertyFlags memoryProps    = VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent,
                                 VkImageTiling tiling = VkImageTiling.Linear, VkImageType imageType = VkImageType.Image2D)
        {
            if (format == VkFormat.Undefined)
            {
                format = DefaultTextureFormat;
            }

            using (StbImage stbi = new StbImage(path)) {
                uint mipLevels = reserveSpaceForMipmaps ? ComputeMipLevels(stbi.Width, stbi.Height) : 1;

                Image img = new Image(dev, format, usage, memoryProps, (uint)stbi.Width, (uint)stbi.Height, imageType,
                                      VkSampleCountFlags.SampleCount1, tiling, mipLevels);

                img.Map();
                stbi.CoptyTo(img.MappedData);
                img.Unmap();

                return(img);
            }
        }
Пример #7
0
        /// <summary>
        /// Open image with STBI library
        /// </summary>
        /// <param name="bitmap">raw bitmap datas</param>
        /// <param name="bitmapByteCount">Bitmap byte count.</param>
        /// <param name="requestedChannels">Force returned channels count, set 0 for original count</param>
        public StbImage(IntPtr bitmap, ulong bitmapByteCount, int requestedChannels = 4)
        {
#if STB_SHARP
            unsafe {
                Span <byte> byteArray          = new Span <byte> (bitmap.ToPointer(), (int)bitmapByteCount);
                StbImageSharp.ImageResult stbi =
                    StbImageSharp.ImageResult.FromMemory(byteArray.ToArray(), (StbImageSharp.ColorComponents)requestedChannels);
                Width    = stbi.Width;
                Height   = stbi.Height;
                Channels = (int)stbi.Comp;
                gcHandle = GCHandle.Alloc(stbi.Data, GCHandleType.Pinned);
            }
#else
            Handle = StbImage.Load(bitmap, (int)bitmapByteCount, out Width, out Height, out Channels, requestedChannels);
            if (Handle == IntPtr.Zero)
            {
                throw new Exception($"STBI image loading error.");
            }
#endif
            if (requestedChannels > 0)
            {
                Channels = requestedChannels;
            }
        }