/// <summary> /// Loads a texture from a stream. /// </summary> /// <param name="device">The <see cref="Direct3D11.Device"/>.</param> /// <param name="stream">The stream to load the texture from.</param> /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param> /// <param name="usage">Usage of the resource. Default is <see cref="ResourceUsage.Immutable"/> </param> /// <returns>A texture</returns> public static Texture Load(Direct3D11.Device device, Stream stream, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable) { stream.Position = 0; var image = Image.Load(stream); if (image == null) { stream.Position = 0; return(null); } try { switch (image.Description.Dimension) { case TextureDimension.Texture1D: return(Texture1D.New(device, image, flags, usage)); case TextureDimension.Texture2D: return(Texture2D.New(device, image, flags, usage)); case TextureDimension.Texture3D: return(Texture3D.New(device, image, flags, usage)); case TextureDimension.TextureCube: return(TextureCube.New(device, image, flags, usage)); } } finally { image.Dispose(); stream.Position = 0; } throw new InvalidOperationException("Dimension not supported"); }
/// <summary> /// Creates a new texture with the specified generic texture description. /// </summary> /// <param name="graphicsDevice">The graphics device.</param> /// <param name="description">The description.</param> /// <returns>A Texture instance, either a RenderTarget or DepthStencilBuffer or Texture, depending on Binding flags.</returns> public static Texture New(Direct3D11.Device graphicsDevice, TextureDescription description) { if (graphicsDevice == null) { throw new ArgumentNullException("graphicsDevice"); } if ((description.BindFlags & BindFlags.RenderTarget) != 0) { throw new NotSupportedException("RenderTarget is not supported."); } else if ((description.BindFlags & BindFlags.DepthStencil) != 0) { throw new NotSupportedException("DepthStencil is not supported."); } else { switch (description.Dimension) { case TextureDimension.Texture1D: return(Texture1D.New(graphicsDevice, description)); case TextureDimension.Texture2D: return(Texture2D.New(graphicsDevice, description)); case TextureDimension.Texture3D: return(Texture3D.New(graphicsDevice, description)); case TextureDimension.TextureCube: return(TextureCube.New(graphicsDevice, description)); } } return(null); }
/// <summary> /// Creates a new <see cref="TextureCube" /> from a initial data.. /// </summary> /// <typeparam name="T">Type of a pixel data</typeparam> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="size">The size (in pixels) of the top-level faces of the cube texture.</param> /// <param name="format">Describes the format to use.</param> /// <param name="usage">The usage.</param> /// <param name="flags">Sets the texture flags (for unordered access...etc.)</param> /// <param name="textureData">an array of 6 textures. See remarks</param> /// <returns>A new instance of <see cref="TextureCube" /> class.</returns> /// <msdn-id>ff476521</msdn-id> /// <unmanaged>HRESULT ID3D11Device::CreateTexture2D([In] const D3D11_TEXTURE2D_DESC* pDesc,[In, Buffer, Optional] const D3D11_SUBRESOURCE_DATA* pInitialData,[Out, Fast] ID3D11Texture2D** ppTexture2D)</unmanaged> /// <unmanaged-short>ID3D11Device::CreateTexture2D</unmanaged-short> /// <remarks> /// The first dimension of mipMapTextures describes the number of array (TextureCube Array), the second is the texture data for a particular cube face. /// </remarks> public unsafe static TextureCube New <T>(GraphicsDevice device, int size, PixelFormat format, T[][] textureData, TextureFlags flags = TextureFlags.ShaderResource, ResourceUsage usage = ResourceUsage.Immutable) where T : struct { if (textureData.Length != 6) { throw new ArgumentException("Invalid texture data. First dimension must be equal to 6", "textureData"); } TextureCube cube = null; Utilities.Pin(textureData[0], ptr1 => { var dataBox1 = GetDataBox(format, size, size, 1, textureData[0], ptr1); Utilities.Pin(textureData[1], ptr2 => { var dataBox2 = GetDataBox(format, size, size, 1, textureData[0], ptr2); Utilities.Pin(textureData[2], ptr3 => { var dataBox3 = GetDataBox(format, size, size, 1, textureData[0], ptr3); Utilities.Pin(textureData[3], ptr4 => { var dataBox4 = GetDataBox(format, size, size, 1, textureData[0], ptr4); Utilities.Pin(textureData[4], ptr5 => { var dataBox5 = GetDataBox(format, size, size, 1, textureData[0], ptr5); Utilities.Pin(textureData[5], ptr6 => { var dataBox6 = GetDataBox(format, size, size, 1, textureData[0], ptr6); cube = new TextureCube(device, NewTextureCubeDescription(size, format, flags | TextureFlags.ShaderResource, 1, usage), dataBox1, dataBox2, dataBox3, dataBox4, dataBox5, dataBox6); }); }); }); }); }); }); return(cube); }