コード例 #1
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
        /// <summary>
        ///   Loads a texture from an image in memory.
        /// </summary>
        /// <param name = "device">The device used to load the texture.</param>
        /// <param name = "memory">Array of memory containing the image data to load.</param>
        /// <param name = "loadInfo">Specifies information used to load the texture.</param>
        /// <returns>The loaded texture object.</returns>
        public static Resource FromMemory(Device device, byte[] memory, ImageLoadInformation loadInfo)
        {
            unsafe
            {
                System.Diagnostics.Debug.Assert(memory != null);
                System.Diagnostics.Debug.Assert(memory.Length > 0);
                IntPtr temp;
                Result resultOut;

                fixed(void *pBuffer = &memory[0])
                D3DX11.CreateTextureFromMemory(device, (IntPtr)pBuffer, memory.Length, loadInfo, IntPtr.Zero,
                                               out temp, out resultOut);

                var resource = new Resource(temp);
                try
                {
                    switch (resource.Dimension)
                    {
                    case ResourceDimension.Texture1D: return(FromPointer <Texture1D>(temp));

                    case ResourceDimension.Texture2D: return(FromPointer <Texture2D>(temp));

                    case ResourceDimension.Texture3D: return(FromPointer <Texture3D>(temp));
                    }
                }
                finally
                {
                    resource.NativePointer = IntPtr.Zero;
                }
                return(null);
            }
        }
コード例 #2
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
        /// <summary>
        ///   Loads a texture from an image in memory.
        /// </summary>
        /// <param name = "device">The device used to load the texture.</param>
        /// <param name = "pointer">Pointer to unmanaged memory containing the image data to load.</param>
        /// <param name = "loadInfo">Specifies information used to load the texture.</param>
        /// <returns>The loaded texture object.</returns>
        public static Resource FromMemory(Device device, DataPointer pointer, ImageLoadInformation loadInfo)
        {
            System.Diagnostics.Debug.Assert(pointer.Pointer != IntPtr.Zero);
            System.Diagnostics.Debug.Assert(pointer.Size > 0);

            IntPtr temp;
            Result resultOut;

            D3DX11.CreateTextureFromMemory(device, pointer.Pointer, pointer.Size, loadInfo, IntPtr.Zero, out temp, out resultOut);

            var resource = new Resource(temp);

            try
            {
                switch (resource.Dimension)
                {
                case ResourceDimension.Texture1D: return(FromPointer <Texture1D>(temp));

                case ResourceDimension.Texture2D: return(FromPointer <Texture2D>(temp));

                case ResourceDimension.Texture3D: return(FromPointer <Texture3D>(temp));
                }
            }
            finally
            {
                resource.NativePointer = IntPtr.Zero;
            }

            return(null);
        }
コード例 #3
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
        /// <summary>
        ///   Loads a texture from an image file.
        /// </summary>
        /// <param name = "device">The device used to load the texture.</param>
        /// <param name = "fileName">Path to the file on disk.</param>
        /// <param name = "loadInfo">Specifies information used to load the texture.</param>
        /// <returns>The loaded texture object.</returns>
        public static Resource FromFile(Device device, string fileName, ImageLoadInformation loadInfo)
        {
            IntPtr temp;
            Result resultOut;

            D3DX11.CreateTextureFromFile(device, fileName, loadInfo, IntPtr.Zero, out temp, out resultOut);

            var resource = new Resource(temp);

            try
            {
                switch (resource.Dimension)
                {
                case ResourceDimension.Texture1D:
                    return(FromPointer <Texture1D>(temp));

                case ResourceDimension.Texture2D:
                    return(FromPointer <Texture2D>(temp));

                case ResourceDimension.Texture3D:
                    return(FromPointer <Texture3D>(temp));
                }
            }
            finally
            {
                resource.NativePointer = IntPtr.Zero;
            }

            return(null);
        }
コード例 #4
0
ファイル: ShaderResourceView.cs プロジェクト: Nezz/SharpDX
 /// <summary>	
 /// Create a shader-resource view from a file.	
 /// </summary>	
 /// <param name="device">A reference to the device (see <see cref="SharpDX.Direct3D11.Device"/>) that will use the resource. </param>
 /// <param name="fileName">Name of the file that contains the shader-resource view.</param>
 /// <param name="loadInformation">Identifies the characteristics of a texture (see <see cref="SharpDX.Direct3D11.ImageLoadInformation"/>) when the data processor is created. </param>
 /// <returns>Returns a reference to the shader-resource view (see <see cref="SharpDX.Direct3D11.ShaderResourceView"/>). </returns>
 /// <unmanaged>HRESULT D3DX11CreateShaderResourceViewFromFileW([None] ID3D10Device* pDevice,[None] const wchar_t* pSrcFile,[In, Optional] D3DX11_IMAGE_LOAD_INFO* pLoadInfo,[None] ID3DX11ThreadPump* pPump,[None] ID3D10ShaderResourceView** ppShaderResourceView,[None] HRESULT* pHResult)</unmanaged>
 public static ShaderResourceView FromFile(Device device, string fileName, ImageLoadInformation loadInformation)
 {
     ShaderResourceView temp;
     Result hResult;
     D3DX11.CreateShaderResourceViewFromFile(device, fileName, loadInformation, IntPtr.Zero, out temp, out hResult);
     // TODO test hResult?
     return temp;
 }
コード例 #5
0
ファイル: ShaderResourceView.cs プロジェクト: wpwen/SharpDX
        /// <summary>
        /// Create a shader-resource view from a file.
        /// </summary>
        /// <param name="device">A reference to the device (see <see cref="SharpDX.Direct3D11.Device"/>) that will use the resource. </param>
        /// <param name="fileName">Name of the file that contains the shader-resource view.</param>
        /// <param name="loadInformation">Identifies the characteristics of a texture (see <see cref="SharpDX.Direct3D11.ImageLoadInformation"/>) when the data processor is created. </param>
        /// <returns>Returns a reference to the shader-resource view (see <see cref="SharpDX.Direct3D11.ShaderResourceView"/>). </returns>
        /// <unmanaged>HRESULT D3DX11CreateShaderResourceViewFromFileW([None] ID3D10Device* pDevice,[None] const wchar_t* pSrcFile,[In, Optional] D3DX11_IMAGE_LOAD_INFO* pLoadInfo,[None] ID3DX11ThreadPump* pPump,[None] ID3D10ShaderResourceView** ppShaderResourceView,[None] HRESULT* pHResult)</unmanaged>
        public static ShaderResourceView FromFile(Device device, string fileName, ImageLoadInformation loadInformation)
        {
            ShaderResourceView temp;
            Result             hResult;

            D3DX11.CreateShaderResourceViewFromFile(device, fileName, loadInformation, IntPtr.Zero, out temp, out hResult);
            // TODO test hResult?
            return(temp);
        }
コード例 #6
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
        /// <summary>
        ///   Loads a texture from an image file.
        /// </summary>
        /// <param name = "device">The device used to load the texture.</param>
        /// <param name = "fileName">Path to the file on disk.</param>
        /// <param name = "loadInfo">Specifies information used to load the texture.</param>
        /// <returns>The loaded texture object.</returns>
        public static T FromFile <T>(Device device, string fileName, ImageLoadInformation loadInfo) where T : Resource
        {
            System.Diagnostics.Debug.Assert(typeof(T) == typeof(Texture1D) || typeof(T) == typeof(Texture2D) ||
                                            typeof(T) == typeof(Texture3D));

            IntPtr temp;
            Result resultOut;

            D3DX11.CreateTextureFromFile(device, fileName, loadInfo, IntPtr.Zero, out temp, out resultOut);
            return(FromPointer <T>(temp));
        }
コード例 #7
0
ファイル: ShaderResourceView.cs プロジェクト: wpwen/SharpDX
        /// <summary>
        /// Create a shader-resource view from a file in memory.
        /// </summary>
        /// <param name="device">A reference to the device (see <see cref="SharpDX.Direct3D11.Device"/>) that will use the resource. </param>
        /// <param name="memory">Pointer to a memory location that contains the shader-resource view. </param>
        /// <param name="loadInformation">Identifies the characteristics of a texture (see <see cref="SharpDX.Direct3D11.ImageLoadInformation"/>) when the data processor is created. </param>
        /// <returns>Returns a reference to the shader-resource view (see <see cref="SharpDX.Direct3D11.ShaderResourceView"/>). </returns>
        /// <unmanaged>HRESULT D3DX11CreateShaderResourceViewFromMemory([None] ID3D10Device* pDevice,[None] const void* pSrcData,[None] SIZE_T SrcDataSize,[In, Optional] D3DX11_IMAGE_LOAD_INFO* pLoadInfo,[None] ID3DX11ThreadPump* pPump,[None] ID3D10ShaderResourceView** ppShaderResourceView,[None] HRESULT* pHResult)</unmanaged>
        public static ShaderResourceView FromMemory(Device device, byte[] memory, ImageLoadInformation loadInformation)
        {
            unsafe
            {
                ShaderResourceView temp;
                Result             hResult;

                fixed(void *pMemory = &memory[0])
                D3DX11.CreateShaderResourceViewFromMemory(device, new IntPtr(pMemory), memory.Length, loadInformation, IntPtr.Zero, out temp, out hResult);

                // TODO test hResult?
                return(temp);
            }
        }
コード例 #8
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
        /// <summary>
        ///   Loads a texture from an image in memory.
        /// </summary>
        /// <param name = "device">The device used to load the texture.</param>
        /// <param name = "pointer">Pointer to unmanaged memory containing the image data to load.</param>
        /// <param name = "loadInfo">Specifies information used to load the texture.</param>
        /// <returns>The loaded texture object.</returns>
        public static T FromMemory <T>(Device device, DataPointer pointer, ImageLoadInformation loadInfo) where T : Resource
        {
            System.Diagnostics.Debug.Assert(typeof(T) == typeof(Texture1D) || typeof(T) == typeof(Texture2D) ||
                                            typeof(T) == typeof(Texture3D));

            System.Diagnostics.Debug.Assert(pointer.Pointer != IntPtr.Zero);
            System.Diagnostics.Debug.Assert(pointer.Size > 0);
            IntPtr temp;
            Result resultOut;

            D3DX11.CreateTextureFromMemory(device, pointer.Pointer, pointer.Size, loadInfo, IntPtr.Zero,
                                           out temp, out resultOut);
            return(FromPointer <T>(temp));
        }
コード例 #9
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
        /// <summary>
        ///   Loads a texture from an image in memory.
        /// </summary>
        /// <param name = "device">The device used to load the texture.</param>
        /// <param name = "memory">Array of memory containing the image data to load.</param>
        /// <param name = "loadInfo">Specifies information used to load the texture.</param>
        /// <returns>The loaded texture object.</returns>
        public static T FromMemory <T>(Device device, byte[] memory, ImageLoadInformation loadInfo) where T : Resource
        {
            System.Diagnostics.Debug.Assert(typeof(T) == typeof(Texture1D) || typeof(T) == typeof(Texture2D) ||
                                            typeof(T) == typeof(Texture3D));

            unsafe
            {
                System.Diagnostics.Debug.Assert(memory != null);
                System.Diagnostics.Debug.Assert(memory.Length > 0);
                IntPtr temp;
                Result resultOut;

                fixed(void *pBuffer = &memory[0])
                D3DX11.CreateTextureFromMemory(device, (IntPtr)pBuffer, memory.Length, loadInfo, IntPtr.Zero,
                                               out temp, out resultOut);

                return(FromPointer <T>(temp));
            }
        }
コード例 #10
0
ファイル: ShaderResourceView.cs プロジェクト: Nezz/SharpDX
 /// <summary>	
 /// Create a shader-resource view from a file in memory.	
 /// </summary>	
 /// <param name="device">A reference to the device (see <see cref="SharpDX.Direct3D11.Device"/>) that will use the resource. </param>
 /// <param name="memory">Pointer to a memory location that contains the shader-resource view. </param>
 /// <param name="loadInformation">Identifies the characteristics of a texture (see <see cref="SharpDX.Direct3D11.ImageLoadInformation"/>) when the data processor is created. </param>
 /// <returns>Returns a reference to the shader-resource view (see <see cref="SharpDX.Direct3D11.ShaderResourceView"/>). </returns>
 /// <unmanaged>HRESULT D3DX11CreateShaderResourceViewFromMemory([None] ID3D10Device* pDevice,[None] const void* pSrcData,[None] SIZE_T SrcDataSize,[In, Optional] D3DX11_IMAGE_LOAD_INFO* pLoadInfo,[None] ID3DX11ThreadPump* pPump,[None] ID3D10ShaderResourceView** ppShaderResourceView,[None] HRESULT* pHResult)</unmanaged>
 public static ShaderResourceView FromMemory(Device device, byte[] memory, ImageLoadInformation loadInformation)
 {
     unsafe
     {
         ShaderResourceView temp;
         Result hResult;
         fixed (void* pMemory = &memory[0])
             D3DX11.CreateShaderResourceViewFromMemory(device, new IntPtr(pMemory), memory.Length, loadInformation, IntPtr.Zero, out temp, out hResult);
         // TODO test hResult?
         return temp;
     }
 }
コード例 #11
0
        public static Texture11 FromBitmap(Device device, Bitmap bmp)
        {
            var ms = new MemoryStream();

            bmp.Save(ms, ImageFormat.Png);

            ms.Seek(0, SeekOrigin.Begin);

            if (IsPowerOf2((uint)bmp.Width) && IsPowerOf2((uint)bmp.Height))
            {
                var loadInfo = new ImageLoadInformation();
                loadInfo.BindFlags = BindFlags.ShaderResource;
                loadInfo.CpuAccessFlags = CpuAccessFlags.None;
                loadInfo.Depth = -1;
                loadInfo.Format = RenderContext11.DefaultTextureFormat;
                loadInfo.Filter = FilterFlags.Box;
                loadInfo.FirstMipLevel = 0;
                loadInfo.Height = -1;
                loadInfo.MipFilter = FilterFlags.Linear;
                loadInfo.MipLevels = 0;
                loadInfo.OptionFlags = ResourceOptionFlags.None;
                loadInfo.Usage = ResourceUsage.Default;
                loadInfo.Width = -1;
                if (loadInfo.Format == Format.R8G8B8A8_UNorm_SRgb)
                {
                    loadInfo.Filter |= FilterFlags.SRgb;
                }

                var texture = Texture2D.FromStream<Texture2D>(device, ms, (int)ms.Length, loadInfo);

                ms.Dispose();
                return new Texture11(texture);
            }
            else
            {
                ms.Seek(0, SeekOrigin.Begin);
                var ili = new ImageLoadInformation()
                {
                    BindFlags = BindFlags.ShaderResource,
                    CpuAccessFlags = CpuAccessFlags.None,
                    Depth = -1,
                    Filter = FilterFlags.Box,
                    FirstMipLevel = 0,
                    Format = RenderContext11.DefaultTextureFormat,
                    Height = -1,
                    MipFilter = FilterFlags.None,
                    MipLevels = 1,
                    OptionFlags = ResourceOptionFlags.None,
                    Usage = ResourceUsage.Default,
                    Width = -1
                };
                if (ili.Format == Format.R8G8B8A8_UNorm_SRgb)
                {
                    ili.Filter |= FilterFlags.SRgb;
                }

                var texture = Texture2D.FromStream<Texture2D>(device, ms, (int)ms.Length, ili);
                ms.Dispose();
                return new Texture11(texture);
            }
        }
コード例 #12
0
        public static Texture11 FromStream(Device device, Stream stream)
        {
            try
            {
                var loadInfo = new ImageLoadInformation();
                loadInfo.BindFlags = BindFlags.ShaderResource;
                loadInfo.CpuAccessFlags = CpuAccessFlags.None;
                loadInfo.Depth = -1;
                loadInfo.Format = RenderContext11.DefaultTextureFormat;
                loadInfo.Filter = FilterFlags.Box;
                loadInfo.FirstMipLevel = 0;
                loadInfo.Height = -1;
                loadInfo.MipFilter = FilterFlags.Linear;
                loadInfo.MipLevels = 0;
                loadInfo.OptionFlags = ResourceOptionFlags.None;
                loadInfo.Usage = ResourceUsage.Default;
                loadInfo.Width = -1;
                if (loadInfo.Format == Format.R8G8B8A8_UNorm_SRgb)
                {
                    loadInfo.Filter |= FilterFlags.SRgb;
                }

                var texture = Texture2D.FromStream<Texture2D>(device, stream, (int)stream.Length, loadInfo);

                return new Texture11(texture);
            }
            catch
            {
                return null;
            }
        }
コード例 #13
0
        public static Texture11 FromFile(Device device, string fileName, LoadOptions options = LoadOptions.AssumeSRgb)
        {
            try
            {
                var loadInfo = new ImageLoadInformation();
                loadInfo.BindFlags = BindFlags.ShaderResource;
                loadInfo.CpuAccessFlags = CpuAccessFlags.None;
                loadInfo.Depth = -1;
                loadInfo.Filter = FilterFlags.Box;
                loadInfo.FirstMipLevel = 0;
                loadInfo.Format = Format.R8G8B8A8_UNorm;

                loadInfo.Height = -1;
                loadInfo.MipLevels = -1;
                loadInfo.OptionFlags = ResourceOptionFlags.None;
                loadInfo.Usage = ResourceUsage.Default;
                loadInfo.Width = -1;

                var shouldPromoteSRgb = RenderContext11.sRGB && (options & LoadOptions.AssumeSRgb) == LoadOptions.AssumeSRgb;

                if (fileName.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) ||
                    fileName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase) ||
                    fileName.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase))
                {
                    loadInfo.Filter = FilterFlags.Box;
                    if (shouldPromoteSRgb)
                    {
                        loadInfo.Format = promoteFormatToSRGB(loadInfo.Format);
                    }
                    if (isSRGBFormat(loadInfo.Format))
                    {
                        loadInfo.Filter |= FilterFlags.SRgb;
                    }
                }
                else
                {
                    // Promote image format to sRGB
                    var info = ImageInformation.FromFile(fileName);
                    if (info.HasValue && shouldPromoteSRgb)
                    {
                        loadInfo.Format = promoteFormatToSRGB(info.Value.Format);
                    }
                    if (isSRGBFormat(loadInfo.Format) )
                    {
                        loadInfo.Filter |= FilterFlags.SRgb;
                    }
                }

                var texture = Texture2D.FromFile<Texture2D>(device, fileName, loadInfo);

                return new Texture11(texture);
            }
            catch (Exception e)
            {
                try
                {
                    var ili = new ImageLoadInformation()
                                {
                                    BindFlags = BindFlags.ShaderResource,
                                    CpuAccessFlags = CpuAccessFlags.None,
                                    Depth = -1,
                                    Filter = FilterFlags.Box,
                                    FirstMipLevel = 0,
                                    Format = RenderContext11.DefaultTextureFormat,
                                    Height = -1,
                                    MipFilter = FilterFlags.None,
                                    MipLevels = 1,
                                    OptionFlags = ResourceOptionFlags.None,
                                    Usage = ResourceUsage.Default,
                                    Width = -1
                                };
                    if (ili.Format == Format.R8G8B8A8_UNorm_SRgb)
                    {
                        ili.Filter |= FilterFlags.SRgb;
                    }

                    var texture = Texture2D.FromFile<Texture2D>(device, fileName, ili);
                    return new Texture11(texture);

                }
                catch
                {
                    return null;
                }
            }
        }
コード例 #14
0
ファイル: ShaderResourceView.cs プロジェクト: wpwen/SharpDX
 /// <summary>
 /// Create a shader-resource view from a file in a stream..
 /// </summary>
 /// <param name="device">A reference to the device (see <see cref="SharpDX.Direct3D11.Device"/>) that will use the resource. </param>
 /// <param name="stream">Pointer to the file in memory that contains the shader-resource view. </param>
 /// <param name="sizeInBytes">Size of the file to read from the stream</param>
 /// <param name="loadInformation">Identifies the characteristics of a texture (see <see cref="SharpDX.Direct3D11.ImageLoadInformation"/>) when the data processor is created. </param>
 /// <returns>Returns a reference to the shader-resource view (see <see cref="SharpDX.Direct3D11.ShaderResourceView"/>). </returns>
 /// <unmanaged>HRESULT D3DX11CreateShaderResourceViewFromMemory([None] ID3D10Device* pDevice,[None] const void* pSrcData,[None] SIZE_T SrcDataSize,[In, Optional] D3DX11_IMAGE_LOAD_INFO* pLoadInfo,[None] ID3DX11ThreadPump* pPump,[None] ID3D10ShaderResourceView** ppShaderResourceView,[None] HRESULT* pHResult)</unmanaged>
 public static ShaderResourceView FromStream(Device device, Stream stream, int sizeInBytes, ImageLoadInformation loadInformation)
 {
     byte[] memory = Utilities.ReadStream(stream, ref sizeInBytes);
     return(FromMemory(device, memory, loadInformation));
 }
コード例 #15
0
ファイル: ResourceProxy.cs プロジェクト: TomCrypto/Insight
 /// <summary>
 /// Adds a resource to the proxy, with specific loading information.
 /// </summary>
 /// <param name="name">The resource name.</param>
 /// <param name="loadInformation">Image loading information.</param>
 public void AddResource(String name, ImageLoadInformation loadInformation)
 {
     references.Add(name, new ResourceReference(Device, Resource.FromFile(Device, name, loadInformation)));
 }
コード例 #16
0
ファイル: ShaderResourceView.cs プロジェクト: Nezz/SharpDX
 /// <summary>	
 /// Create a shader-resource view from a file in a stream..	
 /// </summary>	
 /// <param name="device">A reference to the device (see <see cref="SharpDX.Direct3D11.Device"/>) that will use the resource. </param>
 /// <param name="stream">Pointer to the file in memory that contains the shader-resource view. </param>
 /// <param name="sizeInBytes">Size of the file to read from the stream</param>
 /// <param name="loadInformation">Identifies the characteristics of a texture (see <see cref="SharpDX.Direct3D11.ImageLoadInformation"/>) when the data processor is created. </param>
 /// <returns>Returns a reference to the shader-resource view (see <see cref="SharpDX.Direct3D11.ShaderResourceView"/>). </returns>
 /// <unmanaged>HRESULT D3DX11CreateShaderResourceViewFromMemory([None] ID3D10Device* pDevice,[None] const void* pSrcData,[None] SIZE_T SrcDataSize,[In, Optional] D3DX11_IMAGE_LOAD_INFO* pLoadInfo,[None] ID3DX11ThreadPump* pPump,[None] ID3D10ShaderResourceView** ppShaderResourceView,[None] HRESULT* pHResult)</unmanaged>
 public static ShaderResourceView FromStream(Device device, Stream stream, int sizeInBytes, ImageLoadInformation loadInformation)
 {
     byte[] memory = Utilities.ReadStream(stream, ref sizeInBytes);
     return FromMemory(device, memory, loadInformation);
 }
コード例 #17
0
ファイル: Resource.cs プロジェクト: oeoen/SharpDX
 /// <summary>
 ///   Loads a texture from a stream of data.
 /// </summary>
 /// <param name = "device">The device used to load the texture.</param>
 /// <param name = "stream">A stream containing the image data to load.</param>
 /// <param name = "sizeInBytes">Size of the image to load.</param>
 /// <param name = "loadInfo">Specifies information used to load the texture.</param>
 /// <returns>The loaded texture object.</returns>
 public static Resource FromStream(Device device, Stream stream, int sizeInBytes, ImageLoadInformation loadInfo)
 {
     byte[] buffer = Utilities.ReadStream(stream, ref sizeInBytes);
     return(FromMemory(device, buffer, loadInfo));
 }
コード例 #18
0
ファイル: DXT.cs プロジェクト: jzebedee/Armiger
        //bmp > tga > png > dds
        //public byte[] ReadFile(string file)
        //{
        //    try
        //    {
        //        byte[] fBytes = File.ReadAllBytes(file);

        //        if (fBytes.Length == 0)
        //        {
        //            switch (Path.GetExtension(file).ToLowerInvariant())
        //            {
        //                case "bmp":
        //                    return ReadFile(Path.ChangeExtension(file, "tga"));
        //                case "tga":
        //                    return ReadFile(Path.ChangeExtension(file, "png"));
        //                case "png":
        //                    return ReadFile(Path.ChangeExtension(file, "dds"));
        //                //case "dds":
        //                //default:
        //            }
        //        }
        //    }
        //    catch (IOException e)
        //    {
        //        Trace.TraceError(e.ToString());
        //    }

        //    return null;
        //}

        private JobResult _process(Job job, Recovery recovery, bool asMappable = true)
        {
            string file = job.Key;

            byte[] fBytes = job.Value;
            int fLen = fBytes.Length;

            Result code = Result.NoAction;
            byte[] output = null;

            using (var ms = new MemoryStream(fBytes))
                try
                {
                    var txFlags = asMappable ? SharpDX.Toolkit.Graphics.TextureFlags.RenderTarget : SharpDX.Toolkit.Graphics.TextureFlags.ShaderResource;

                    bool cube = file.Contains("\\cubemaps\\");

                    Texture tex;
                    if (cube)
                        tex = SharpDX.Toolkit.Graphics.TextureCube.Load(_device, ms, txFlags, ResourceUsage.Default);
                    else
                        tex = SharpDX.Toolkit.Graphics.Texture2D.Load(_device, ms, txFlags, ResourceUsage.Default);

                    using (tex)
                    {
                        if (tex.Description.BindFlags.HasFlag(BindFlags.RenderTarget | BindFlags.ShaderResource) && tex.Description.MipLevels > 1)
                        {
                            Trace.TraceInformation("Generating mipmaps...");
                            tex.GenerateMipMaps();

                            code |= Result.GeneratedMipmaps;
                        }

                        if (!tex.IsBlockCompressed)
                        {
                            Console.WriteLine(tex.Description.Format);
                            //var loadOpts = new ImageLoadInformation()
                            //{
                            //    //Format = SharpDX.DXGI.Format.bc3,
                            //};

                            var desc = tex.Description;
                            var loadOpts = new ImageLoadInformation
                            {
                                BindFlags = desc.BindFlags,
                                CpuAccessFlags = desc.CpuAccessFlags,
                                Depth = desc.Depth,
                                Filter = FilterFlags.None,
                                //FirstMipLevel = desc.MipLevels,// 0,
                                Format = desc.Format,//SharpDX.DXGI.Format.BC3_UNorm_SRgb,
                                Height = tex.Height,
                                Width = tex.Width,
                                MipFilter = FilterFlags.Box,
                                MipLevels = desc.MipLevels,
                                OptionFlags = desc.OptionFlags,
                                //PSrcInfo = new IntPtr(&imginf),
                                Usage = desc.Usage,
                            };

                            ms.Seek(0, SeekOrigin.Begin);
                            System.Diagnostics.Trace.Assert(tex.GetType() == typeof(SharpDX.Toolkit.Graphics.Texture2D));
                            System.Diagnostics.Trace.Assert(!cube);
                            //using (var newTex = SharpDX.Direct3D11.Texture2D.FromStream(_device, ms, fLen))//, loadOpts))
                            using (var newTex = SharpDX.Toolkit.Graphics.Texture2D.Load(_device, ms, txFlags, ResourceUsage.Default))//(_device, ms, fLen))//, loadOpts))
                            {
                                using (var msNew = new MemoryStream())
                                {
                                    SharpDX.Direct3D11.Texture2D.ToStream(_device, newTex, ImageFileFormat.Dds, msNew);

                                    File.Copy(file, recovery.GetBackupPath(Path.GetFileNameWithoutExtension(file) + "_orig.dds"));
                                    //recovery.Backup(file);
                                    output = msNew.ToArray();
                                }

                                Trace.TraceInformation(Path.GetFileNameWithoutExtension(file) + " converted to BC3");
                                code |= Result.CompressedBC3;
                            }
                        }
                    }
                }
                catch (SharpDXException sdx)
                {
                    if (asMappable)
                        return _process(job, recovery, false);
                    Trace.TraceError(sdx.ToString());
                    code = Result.Failed;
                }
                catch (NullReferenceException nre)
                {
                    Trace.TraceError(nre.ToString());
                    code = Result.Failed;
                }

            return new JobResult(recovery.GetBackupPath(file), code, output);
        }
コード例 #19
0
        private void CompileButton_Click(object sender, RoutedEventArgs e)
        {
            //make the width and height flexible

            if (paths.Count > 2 && redIndex > -1 && greenIndex > -1 && blueIndex > -1)
            {

                Device d = new Device(SharpDX.Direct3D.DriverType.Hardware);

                ImageLoadInformation loadInfo = new ImageLoadInformation()
                {
                    BindFlags = BindFlags.None,
                    CpuAccessFlags = CpuAccessFlags.Read,
                    Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    OptionFlags = ResourceOptionFlags.None,
                    Usage = ResourceUsage.Staging
                };

                Texture2D red, green, blue,final;

                red = Texture2D.FromFile<Texture2D>(d,paths[redIndex],loadInfo);
                green = Texture2D.FromFile<Texture2D>(d, paths[greenIndex],loadInfo);
                blue = Texture2D.FromFile<Texture2D>(d, paths[blueIndex],loadInfo);

                final = new Texture2D(d, new Texture2DDescription()
                {
                    ArraySize = 1,
                    BindFlags = BindFlags.None,
                    CpuAccessFlags = CpuAccessFlags.Write,
                    Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                    Height = 3456,
                    MipLevels = 0,
                    OptionFlags = ResourceOptionFlags.None,
                    SampleDescription = new SharpDX.DXGI.SampleDescription(1,0),
                    Usage = ResourceUsage.Staging,
                    Width = 4608
                });

                DataBox redDataBox = d.ImmediateContext.MapSubresource(red,0,MapMode.Read,MapFlags.None);
                DataBox greenDataBox = d.ImmediateContext.MapSubresource(green, 0, MapMode.Read, MapFlags.None);
                DataBox blueDataBox = d.ImmediateContext.MapSubresource(blue, 0, MapMode.Read, MapFlags.None);
                DataBox finalDataBox = d.ImmediateContext.MapSubresource(final, 0, MapMode.Write, MapFlags.None);

                byte[]
                    redData = new byte[redDataBox.RowPitch * red.Description.Height],
                    greenData = new byte[greenDataBox.RowPitch * green.Description.Height],
                    blueData =new byte[blueDataBox.RowPitch * blue.Description.Height],
                    finalData = new byte[finalDataBox.RowPitch * final.Description.Height];

                Utilities.Read(redDataBox.DataPointer, redData, 0, redData.Length);
                Utilities.Read(greenDataBox.DataPointer, greenData, 0, greenData.Length);
                Utilities.Read(blueDataBox.DataPointer, blueData, 0, blueData.Length);

                for (int x = 0; x < final.Description.Width; x++)
                {
                    for (int y = 0; y < final.Description.Height; y++)
                    {
                        int index = (x*4) + (y * finalDataBox.RowPitch);

                        finalData[index]     = redData[index];
                        finalData[index + 1] = greenData[index];
                        finalData[index + 2] = blueData[index];
                        finalData[index + 3] = 255;//A

                    }
                }

                Utilities.Write(finalDataBox.DataPointer, finalData, 0, finalData.Length);

                d.ImmediateContext.UnmapSubresource(red, 0);
                d.ImmediateContext.UnmapSubresource(green, 0);
                d.ImmediateContext.UnmapSubresource(blue, 0);
                d.ImmediateContext.UnmapSubresource(final, 0);

                Texture2D.ToFile(d.ImmediateContext, final, ImageFileFormat.Png, "C:\\dan\\projectmedia\\BumpMap\\final.png");

                final.Dispose();
                red.Dispose();
                green.Dispose();
                blue.Dispose();
                d.Dispose();
            }
            else MessageBox.Show("must choose at least 3 files and select 3 to be the red green and blue channels");
        }