/// <summary> /// Calculates the mip map count from a requested level. /// </summary> /// <param name="requestedLevel">The requested level.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="depth">The depth.</param> /// <returns>The resulting mipmap count (clamp to [1, maxMipMapCount] for this texture)</returns> internal static int CalculateMipMapCount(MipMapCount requestedLevel, int width, int height = 0, int depth = 0) { int size = Math.Max(Math.Max(width, height), depth); int maxMipMap = 1 + (int)Math.Ceiling(Math.Log(size) / Math.Log(2.0)); return(requestedLevel == 0 ? maxMipMap : Math.Min(requestedLevel, maxMipMap)); }
/// <summary> /// Creates a new 2D <see cref="Texture" />. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="format">Describes the format to use.</param> /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="textureData">Texture datas through an array of <see cref="DataBox"/> </param> /// <param name="textureFlags">true if the texture needs to support unordered read write.</param> /// <param name="arraySize">Size of the texture 2D array, default to 1.</param> /// <param name="usage">The usage.</param> /// <returns>A new instance of 2D <see cref="Texture" /> class.</returns> public static Texture New2D( GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(new Texture(device).InitializeFrom(TextureDescription.New2D(width, height, mipCount, format, textureFlags, arraySize, usage), textureData)); }
/// <summary> /// Calculates the number of miplevels for a Texture 2D. /// </summary> /// <param name="width">The width of the texture.</param> /// <param name="height">The height of the texture.</param> /// <param name="mipLevels">A <see cref="MipMapCount"/>, set to true to calculates all mipmaps, to false to calculate only 1 miplevel, or > 1 to calculate a specific amount of levels.</param> /// <returns>The number of miplevels.</returns> public static int CalculateMipLevels(int width, int height, MipMapCount mipLevels) { if (mipLevels > 1) { int maxMips = CountMips(width, height); if (mipLevels > maxMips) throw new InvalidOperationException(String.Format("MipLevels must be <= {0}", maxMips)); } else if (mipLevels == 0) { mipLevels = CountMips(width, height); } else { mipLevels = 1; } return mipLevels; }
/// <summary> /// Calculates the number of miplevels for a Texture 2D. /// </summary> /// <param name="width">The width of the texture.</param> /// <param name="height">The height of the texture.</param> /// <param name="depth">The depth of the texture.</param> /// <param name="mipLevels">A <see cref="MipMapCount"/>, set to true to calculates all mipmaps, to false to calculate only 1 miplevel, or > 1 to calculate a specific amount of levels.</param> /// <returns>The number of miplevels.</returns> public static int CalculateMipLevels(int width, int height, int depth, MipMapCount mipLevels) { if (mipLevels > 1) { if (!IsPow2(width) || !IsPow2(height) || !IsPow2(depth)) throw new InvalidOperationException("Width/Height/Depth must be power of 2"); int maxMips = CountMips(width, height, depth); if (mipLevels > maxMips) throw new InvalidOperationException(String.Format("MipLevels must be <= {0}", maxMips)); } else if (mipLevels == 0) { if (!IsPow2(width) || !IsPow2(height) || !IsPow2(depth)) throw new InvalidOperationException("Width/Height/Depth must be power of 2"); mipLevels = CountMips(width, height, depth); } else { mipLevels = 1; } return mipLevels; }
/// <summary> /// Creates a new instance of a Cube <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <param name="dataPointer">Pointer to an existing buffer.</param> /// <returns>A new image.</returns> public static Image NewCube(int width, MipMapCount mipMapCount, PixelFormat format, IntPtr dataPointer) { return new Image(CreateDescription(TextureDimension.TextureCube, width, width, 1, mipMapCount, format, 6), dataPointer, 0, null, false); }
/// <summary> /// Creates a new 3D <see cref="Texture"/>. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="depth">The depth.</param> /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="format">Describes the format to use.</param> /// <param name="usage">The usage.</param> /// <param name="textureFlags">true if the texture needs to support unordered read write.</param> /// <returns> /// A new instance of 3D <see cref="Texture"/> class. /// </returns> public static Texture New3D(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(new Texture(device).InitializeFrom(TextureDescription.New3D(width, height, depth, mipCount, format, textureFlags, usage))); }
/// <summary> /// Creates a new instance of a 3D <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="depth">The depth.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <param name="dataPointer">Pointer to an existing buffer.</param> /// <returns>A new image.</returns> public static Image New3D(int width, int height, int depth, MipMapCount mipMapCount, PixelFormat format, IntPtr dataPointer) { return new Image(CreateDescription(TextureDimension.Texture3D, width, width, depth, mipMapCount, format, 1), dataPointer, 0, null, false); }
/// <summary> /// Creates a new instance of a Cube <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <returns>A new image.</returns> public static Image NewCube(int width, MipMapCount mipMapCount, PixelFormat format) { return NewCube(width, mipMapCount, format, IntPtr.Zero); }
/// <summary> /// Creates a new instance of a 2D <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <param name="arraySize">Size of the array.</param> /// <param name="dataPointer">Pointer to an existing buffer.</param> /// <param name="rowStride">Specify a specific rowStride, only valid when mipMapCount == 1 and pixel format is not compressed.</param> /// <returns>A new image.</returns> public static Image New2D(int width, int height, MipMapCount mipMapCount, PixelFormat format, int arraySize, IntPtr dataPointer, int rowStride = 0) { return new Image(CreateDescription(TextureDimension.Texture2D, width, height, 1, mipMapCount, format, arraySize), dataPointer, 0, null, false, PitchFlags.None, rowStride); }
/// <summary> /// Creates a new instance of a 3D <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="depth">The depth.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <returns>A new image.</returns> public static Image New3D(int width, int height, int depth, MipMapCount mipMapCount, PixelFormat format) { return New3D(width, height, depth, mipMapCount, format, IntPtr.Zero); }
/// <summary> /// Creates a new instance of a 1D <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <param name="arraySize">Size of the array.</param> /// <param name="dataPointer">Pointer to an existing buffer.</param> /// <returns>A new image.</returns> public static Image New1D(int width, MipMapCount mipMapCount, PixelFormat format, int arraySize, IntPtr dataPointer) { return new Image(CreateDescription(TextureDimension.Texture1D, width, 1, 1, mipMapCount, format, arraySize), dataPointer, 0, null, false); }
/// <summary> /// Creates a new instance of a 2D <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <param name="arraySize">Size of the array.</param> /// <returns>A new image.</returns> public static Image New2D(int width, int height, MipMapCount mipMapCount, PixelFormat format, int arraySize = 1, int rowStride = 0) { return New2D(width, height, mipMapCount, format, arraySize, IntPtr.Zero, rowStride); }
/// <summary> /// Creates a new <see cref="TextureCube"/>. /// </summary> /// <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="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="format">Describes the format to use.</param> /// <param name="usage">The usage.</param> /// <param name="isUnorderedReadWrite">true if the texture needs to support unordered read write.</param> /// <returns> /// A new instance of <see cref="Texture2D"/> class. /// </returns> public static TextureCube New(GraphicsDevice device, int size, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(new TextureCube(device, NewTextureCubeDescription(size, format, textureFlags, mipCount, usage))); }
/// <summary> /// Creates a new instance of a 1D <see cref="Image"/>. /// </summary> /// <param name="width">The width.</param> /// <param name="mipMapCount">The mip map count.</param> /// <param name="format">The format.</param> /// <param name="arraySize">Size of the array.</param> /// <returns>A new image.</returns> public static Image New1D(int width, MipMapCount mipMapCount, PixelFormat format, int arraySize = 1) { return New1D(width, mipMapCount, format, arraySize, IntPtr.Zero); }
/// <summary> /// Creates a new Cube <see cref="Texture" />. /// </summary> /// <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="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="format">Describes the format to use.</param> /// <param name="textureFlags">The texture flags.</param> /// <param name="usage">The usage.</param> /// <returns>A new instance of 2D <see cref="Texture" /> class.</returns> public static Texture NewCube(GraphicsDevice device, int size, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(new Texture(device).InitializeFrom(TextureDescription.NewCube(size, mipCount, format, textureFlags, usage))); }
private static ImageDescription CreateDescription(TextureDimension dimension, int width, int height, int depth, MipMapCount mipMapCount, PixelFormat format, int arraySize) { return new ImageDescription() { Width = width, Height = height, Depth = depth, ArraySize = arraySize, Dimension = dimension, Format = format, MipLevels = mipMapCount, }; }
/// <summary> /// Creates a new <see cref="Texture2D" />. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="format">Describes the format to use.</param> /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="textureFlags">true if the texture needs to support unordered read write.</param> /// <param name="arraySize">Size of the texture 2D array, default to 1.</param> /// <param name="usage">The usage.</param> /// <returns>A new instance of <see cref="Texture2D" /> class.</returns> public static Texture2D New(GraphicsDevice device, int width, int height, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(new Texture2D(device, NewDescription(width, height, format, textureFlags, mipCount, arraySize, usage))); }
/// <summary> /// Creates a new 1D <see cref="Texture"/>. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="width">The width.</param> /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="format">Describes the format to use.</param> /// <param name="usage">The usage.</param> /// <param name="textureFlags">true if the texture needs to support unordered read write.</param> /// <param name="arraySize">Size of the texture 2D array, default to 1.</param> /// <returns> /// A new instance of 1D <see cref="Texture"/> class. /// </returns> public static Texture New1D(GraphicsDevice device, int width, MipMapCount mipCount, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, int arraySize = 1, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { return(New(device, TextureDescription.New1D(width, mipCount, format, textureFlags, arraySize, usage))); }
/// <summary> /// Creates a new <see cref="Texture3D"/>. /// </summary> /// <param name="device">The <see cref="GraphicsDevice"/>.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="depth">The depth.</param> /// <param name="mipCount">Number of mipmaps, set to true to have all mipmaps, set to an int >=1 for a particular mipmap count.</param> /// <param name="format">Describes the format to use.</param> /// <param name="usage">The usage.</param> /// <param name="textureFlags">true if the texture needs to support unordered read write.</param> /// <returns> /// A new instance of <see cref="Texture3D"/> class. /// </returns> public static Texture3D New(GraphicsDevice device, int width, int height, int depth, MipMapCount mipCount, PixelFormat format, DataBox[] textureData, TextureFlags textureFlags = TextureFlags.ShaderResource, GraphicsResourceUsage usage = GraphicsResourceUsage.Default) { // TODO Add check for number of texture datas according to width/height/depth/mipCount. return(new Texture3D(device, NewDescription(width, height, depth, format, textureFlags, mipCount, usage), textureData)); }