/// <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); }