/// <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); }
/// <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); } }
/// <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> /// <returns>The loaded texture object.</returns> public static Resource FromMemory(Device device, DataPointer pointer) { 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, null, 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); }
/// <summary> /// Saves a texture to file. /// </summary> /// <param name = "context">The device used to save the texture.</param> /// <param name = "texture">The texture to save.</param> /// <param name = "format">The format the texture will be saved as.</param> /// <param name = "fileName">Name of the destination output file where the texture will be saved.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> public static void ToFile(DeviceContext context, Resource texture, ImageFileFormat format, string fileName) { //System.Diagnostics.Debug.Assert(typeof(T) == typeof(Texture1D) || typeof(T) == typeof(Texture2D) || // typeof(T) == typeof(Texture3D)); D3DX11.SaveTextureToFile(context, texture, format, fileName); }
/// <summary> /// Create a shader-resource view from a file. Read the characteristics of a texture when the texture is loaded. /// </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> /// <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) { ShaderResourceView temp; Result hResult; D3DX11.CreateShaderResourceViewFromFile(device, fileName, null, IntPtr.Zero, out temp, out hResult); // TODO test hResult? return(temp); }
/// <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); }
/// <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)); }
/// <summary> /// Retrieves information about a given image file. /// </summary> /// <param name="fileName">File name of image to retrieve information about.</param> /// <returns>If the function succeeds, returns a <see cref="SharpDX.Direct3D11.ImageInformation"/> filled with the description of the data in the source file. else returns null </returns> /// <unmanaged>HRESULT D3DX11GetImageInfoFromFileW([None] const wchar_t* pSrcFile,[None] ID3DX11ThreadPump* pPump,[None] D3DX11_IMAGE_INFO* pSrcInfo,[None] HRESULT* pHResult)</unmanaged> public static ImageInformation?FromFile(string fileName) { try { var info = new ImageInformation(); Result hresult; D3DX11.GetImageInfoFromFile(fileName, IntPtr.Zero, ref info, out hresult); return(info); } catch (SharpDXException) { } return(null); }
/// <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> /// <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) { unsafe { ShaderResourceView temp; Result hResult; fixed(void *pMemory = &memory[0]) D3DX11.CreateShaderResourceViewFromMemory(device, new IntPtr(pMemory), memory.Length, null, IntPtr.Zero, out temp, out hResult); // TODO test hResult? return(temp); } }
/// <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> /// <returns>The loaded texture object.</returns> public static T FromMemory <T>(Device device, DataPointer pointer) 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, null, IntPtr.Zero, out temp, out resultOut); return(FromPointer <T>(temp)); }
/// <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)); } }
/// <summary> /// Retrieves information about a given image file from a memory location. /// </summary> /// <param name="memory">an array to the image in memory</param> /// <returns>If the function succeeds, returns a <see cref="SharpDX.Direct3D11.ImageInformation"/> filled with the description of the data from the image memory. else returns null </returns> /// <unmanaged>HRESULT D3DX11GetImageInfoFromFileW([None] const wchar_t* pSrcFile,[None] ID3DX11ThreadPump* pPump,[None] D3DX11_IMAGE_INFO* pSrcInfo,[None] HRESULT* pHResult)</unmanaged> public static ImageInformation?FromMemory(byte[] memory) { unsafe { try { var info = new ImageInformation(); Result hresult; fixed(void *pMemory = &memory[0]) D3DX11.GetImageInfoFromMemory((IntPtr)pMemory, memory.Length, IntPtr.Zero, ref info, out hresult); // TODO test hResult? return(info); } catch (SharpDXException) { } return(null); } }
/// <summary> /// Saves a texture to a stream. /// </summary> /// <param name = "context">The device used to save the texture.</param> /// <param name = "texture">The texture to save.</param> /// <param name = "format">The format the texture will be saved as.</param> /// <param name = "stream">Destination memory stream where the image will be saved.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> public static void ToStream <T>(DeviceContext context, T texture, ImageFileFormat format, Stream stream) where T : Resource { System.Diagnostics.Debug.Assert(typeof(T) == typeof(Texture1D) || typeof(T) == typeof(Texture2D) || typeof(T) == typeof(Texture3D)); Blob blob; D3DX11.SaveTextureToMemory(context, texture, format, out blob, 0); IntPtr bufferPtr = blob.BufferPointer; int blobSize = blob.BufferSize; // Write byte-by-byte to avoid allocating a managed byte[] that will wastefully persist. for (int byteIndex = 0; byteIndex < blobSize; ++byteIndex) { stream.WriteByte(Marshal.ReadByte(bufferPtr, byteIndex)); } blob.Dispose(); }
/// <summary> /// Projects a function represented in a cube map into spherical harmonics. /// </summary> /// <param name="context"><para>A reference to an <see cref="SharpDX.Direct3D11.DeviceContext"/> object.</para></param> /// <param name="cubeMap"><para>A reference to an <see cref="SharpDX.Direct3D11.Texture2D"/> that represents a cubemap that is going to be projected into spherical harmonics.</para></param> /// <param name="order"><para>Order of the SH evaluation, generates Order^2 coefficients whose degree is Order-1. Valid range is between 2 and 6.</para></param> /// <returns>An array of SH Vector for red, green and blue components with a length Order^2.</returns> /// <unmanaged>HRESULT D3DX11SHProjectCubeMap([In] ID3D11DeviceContext* pContext,[In] unsigned int Order,[In] ID3D11Texture2D* pCubeMap,[Out, Buffer] float* pROut,[Out, Buffer, Optional] float* pGOut,[Out, Buffer, Optional] float* pBOut)</unmanaged> public static RawColor3[] SHProjectCubeMap(DeviceContext context, Texture2D cubeMap, int order) { if (order < 2 || order > 6) { throw new ArgumentException("Invalid range for SH order. Must be in the range [2,6]"); } int length = order * order; var redSH = new float[length]; var greenSH = new float[length]; var blueSH = new float[length]; D3DX11.SHProjectCubeMap(context, order, cubeMap, redSH, greenSH, blueSH); var result = new RawColor3[length]; for (int i = 0; i < result.Length; i++) { result[i].R = redSH[i]; result[i].G = greenSH[i]; result[i].B = blueSH[i]; } return(result); }
/// <summary> /// Converts a height map into a normal map. The (x,y,z) components of each normal are mapped to the (r,g,b) channels of the output texture. /// </summary> /// <param name = "context">The device used to create the normal map.</param> /// <param name = "source">The source height map texture.</param> /// <param name = "destination">The destination texture.</param> /// <param name = "flags">One or more flags that control generation of normal maps.</param> /// <param name = "channel">One or more flag specifying the source of height information.</param> /// <param name = "amplitude">Constant value multiplier that increases (or decreases) the values in the normal map. Higher values usually make bumps more visible, lower values usually make bumps less visible.</param> /// <returns>A <see cref = "T:SharpDX.Result" /> object describing the result of the operation.</returns> public static void ComputeNormalMap(DeviceContext context, Texture2D source, Texture2D destination, NormalMapFlags flags, Channel channel, float amplitude) { D3DX11.ComputeNormalMap(context, source, flags, channel, amplitude, destination); }
/// <summary> /// Load a texture from a texture. /// </summary> /// <param name="context">A reference to a valid <see cref="DeviceContext"/></param> /// <param name="source">Pointer to the source texture. See <see cref="SharpDX.Direct3D11.Resource"/>. </param> /// <param name="destination">Pointer to the destination texture. See <see cref="SharpDX.Direct3D11.Resource"/>. </param> /// <param name="loadInformation">Pointer to texture loading parameters. See <see cref="SharpDX.Direct3D11.TextureLoadInformation"/>. </param> /// <returns>The return value is one of the values listed in {{Direct3D 10 Return Codes}}. </returns> /// <unmanaged>HRESULT D3DX10LoadTextureFromTexture([None] ID3D10Resource* pSrcTexture,[None] D3DX10_TEXTURE_LOAD_INFO* pLoadInfo,[None] ID3D10Resource* pDstTexture)</unmanaged> public static void LoadTextureFromTexture(DeviceContext context, Resource source, Resource destination, TextureLoadInformation loadInformation) { D3DX11.LoadTextureFromTexture(context, source, loadInformation, destination); }
/// <summary> /// Generates mipmap chain using a particular texture filter for this texture instance. /// </summary> /// <param name="deviceContext"><para>A reference to an <see cref="SharpDX.Direct3D11.DeviceContext"/> object.</para></param> /// <param name="sourceLevel"><para>The mipmap level whose data is used to generate the rest of the mipmap chain.</para></param> /// <param name="mipFilter"><para>Flags controlling how each miplevel is filtered (or D3DX11_DEFAULT for <see cref="SharpDX.Direct3D11.FilterFlags.Linear"/>). See <see cref="SharpDX.Direct3D11.FilterFlags"/>.</para></param> /// <returns>The return value is one of the values listed in Direct3D 11 Return Codes.</returns> /// <unmanaged>HRESULT D3DX11FilterTexture([In] ID3D11DeviceContext* pContext,[In] ID3D11Resource* pTexture,[In] unsigned int SrcLevel,[In] unsigned int MipFilter)</unmanaged> public void FilterTexture(DeviceContext deviceContext, int sourceLevel, FilterFlags mipFilter) { D3DX11.FilterTexture(deviceContext, this, sourceLevel, (int)mipFilter); }