using System;
using System.Runtime.CompilerServices;
namespace BansheeEngine
{
///
/// A two dimensional texture.
///
public sealed class Texture2D : Texture
{
///
/// Constructor for the internal use by the runtime.
///
private Texture2D()
{ }
///
/// Creates a new blank 2D texture.
///
/// Width of the texture in pixels.
/// Height of the texture in pixels.
/// Format of the pixels.
/// Describes planned texture use.
/// If higher than 1, texture containing multiple samples per pixel is created.
/// Should the texture allocate memory for the entire mip-map chain or only the top level.
///
/// If true the texture data is assumed to have be gamma corrected and will be
/// converted back to linear space when sampled on GPU, and converted to gamma space
/// before being written by the GPU.
public Texture2D(int width, int height, PixelFormat format = PixelFormat.R8G8B8A8, TextureUsage usage = TextureUsage.Default,
int numSamples = 1, bool hasMipmaps = false, bool gammaCorrection = false)
{
Internal_CreateInstance(this, format, width, height, usage, numSamples, hasMipmaps, gammaCorrection);
}
///
/// Returns pixels for the specified mip level. Pixels will be read from system memory, which means the texture has
/// to be created with . If the texture was updated from the GPU the pixels
/// retrieved from this method will not reflect that, and you should use instead.
///
/// Mip level to retrieve pixels for. Top level (0) is the highest quality.
/// A set of pixels for the specified mip level.
public PixelData GetPixels(int mipLevel = 0)
{
return Internal_GetPixels(mCachedPtr, mipLevel);
}
///
/// Sets pixels for the specified mip level.
///
/// Pixels to assign to the specified mip level. Pixel data must match the mip level size
/// and texture pixel format.
/// Mip level to set pixels for. Top level (0) is the highest quality.
public void SetPixels(PixelData data, int mipLevel = 0)
{
Internal_SetPixels(mCachedPtr, data, mipLevel);
}
///
/// Sets pixels for the specified mip level.
///
/// Pixels to assign to the specified mip level. Size of the array must match texture width
/// multiplied by height. Data is expected to be laid out row by row. Pixels will be
/// automatically converted to the valid pixel format.
/// Mip level to set pixels for. Top level (0) is the highest quality.
public void SetPixels(Color[] data, int mipLevel = 0)
{
if (data == null || data.Length != (Width*Height))
{
int size = data == null ? 0 : data.Length;
Debug.LogError("SetPixels called with incorrect size: " + size);
}
Internal_SetPixelsArray(mCachedPtr, data, mipLevel);
}
///
/// Reads texture pixels directly from the GPU. This is similar to but the texture doesn't
/// need to be created with , and the data will contain any updates performed by
/// the GPU. This method can be potentially slow as it introduces a CPU-GPU synchronization point. Additionally
/// this method is asynchronous which means the data is not available immediately.
///
/// Mip level to retrieve pixels for. Top level (0) is the highest quality.
/// object that will contain object when the operation
/// completes.
public AsyncOp GetGPUPixels(int mipLevel = 0)
{
return Internal_GetGPUPixels(mCachedPtr, mipLevel);
}
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_CreateInstance(Texture2D instance, PixelFormat format, int width,
int height, TextureUsage usage, int numSamples, bool hasMipmaps, bool gammaCorrection);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern PixelData Internal_GetPixels(IntPtr thisPtr, int mipLevel);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern AsyncOp Internal_GetGPUPixels(IntPtr thisPtr, int mipLevel);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetPixels(IntPtr thisPtr, PixelData data, int mipLevel);
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void Internal_SetPixelsArray(IntPtr thisPtr, Color[] data, int mipLevel);
}
}