Texture2D.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// A two dimensional texture.
  7. /// </summary>
  8. public sealed class Texture2D : Texture
  9. {
  10. /// <summary>
  11. /// Constructor for the internal use by the runtime.
  12. /// </summary>
  13. private Texture2D()
  14. { }
  15. /// <summary>
  16. /// Creates a new blank 2D texture.
  17. /// </summary>
  18. /// <param name="width">Width of the texture in pixels.</param>
  19. /// <param name="height">Height of the texture in pixels.</param>
  20. /// <param name="format">Format of the pixels.</param>
  21. /// <param name="usage">Describes planned texture use.</param>
  22. /// <param name="numSamples">If higher than 1, texture containing multiple samples per pixel is created.</param>
  23. /// <param name="hasMipmaps">Should the texture allocate memory for the entire mip-map chain or only the top level.
  24. /// </param>
  25. /// <param name="gammaCorrection">If true the texture data is assumed to have be gamma corrected and will be
  26. /// converted back to linear space when sampled on GPU, and converted to gamma space
  27. /// before being written by the GPU.</param>
  28. public Texture2D(int width, int height, PixelFormat format = PixelFormat.R8G8B8A8, TextureUsage usage = TextureUsage.Default,
  29. int numSamples = 1, bool hasMipmaps = false, bool gammaCorrection = false)
  30. {
  31. Internal_CreateInstance(this, format, width, height, usage, numSamples, hasMipmaps, gammaCorrection);
  32. }
  33. /// <summary>
  34. /// Returns pixels for the specified mip level. Pixels will be read from system memory, which means the texture has
  35. /// to be created with <see cref="TextureUsage.CPUCached"/>. If the texture was updated from the GPU the pixels
  36. /// retrieved from this method will not reflect that, and you should use <see cref="GetGPUPixels"/> instead.
  37. /// </summary>
  38. /// <param name="mipLevel">Mip level to retrieve pixels for. Top level (0) is the highest quality.</param>
  39. /// <returns>A set of pixels for the specified mip level.</returns>
  40. public PixelData GetPixels(int mipLevel = 0)
  41. {
  42. return Internal_GetPixels(mCachedPtr, mipLevel);
  43. }
  44. /// <summary>
  45. /// Sets pixels for the specified mip level.
  46. /// </summary>
  47. /// <param name="data">Pixels to assign to the specified mip level. Pixel data must match the mip level size
  48. /// and texture pixel format.</param>
  49. /// <param name="mipLevel">Mip level to set pixels for. Top level (0) is the highest quality.</param>
  50. public void SetPixels(PixelData data, int mipLevel = 0)
  51. {
  52. Internal_SetPixels(mCachedPtr, data, mipLevel);
  53. }
  54. /// <summary>
  55. /// Sets pixels for the specified mip level.
  56. /// </summary>
  57. /// <param name="data">Pixels to assign to the specified mip level. Size of the array must match texture width
  58. /// multiplied by height. Data is expected to be laid out row by row. Pixels will be
  59. /// automatically converted to the valid pixel format.</param>
  60. /// <param name="mipLevel">Mip level to set pixels for. Top level (0) is the highest quality.</param>
  61. public void SetPixels(Color[] data, int mipLevel = 0)
  62. {
  63. if (data == null || data.Length != (Width*Height))
  64. {
  65. int size = data == null ? 0 : data.Length;
  66. Debug.LogError("SetPixels called with incorrect size: " + size);
  67. }
  68. Internal_SetPixelsArray(mCachedPtr, data, mipLevel);
  69. }
  70. /// <summary>
  71. /// Reads texture pixels directly from the GPU. This is similar to <see cref="GetPixels"/> but the texture doesn't
  72. /// need to be created with <see cref="TextureUsage.CPUCached"/>, and the data will contain any updates performed by
  73. /// the GPU. This method can be potentially slow as it introduces a CPU-GPU synchronization point. Additionally
  74. /// this method is asynchronous which means the data is not available immediately.
  75. /// </summary>
  76. /// <param name="mipLevel">Mip level to retrieve pixels for. Top level (0) is the highest quality.</param>
  77. /// <returns><see cref="AsyncOp"/> object that will contain <see cref="PixelData"/> object when the operation
  78. /// completes.</returns>
  79. public AsyncOp GetGPUPixels(int mipLevel = 0)
  80. {
  81. return Internal_GetGPUPixels(mCachedPtr, mipLevel);
  82. }
  83. [MethodImpl(MethodImplOptions.InternalCall)]
  84. private static extern void Internal_CreateInstance(Texture2D instance, PixelFormat format, int width,
  85. int height, TextureUsage usage, int numSamples, bool hasMipmaps, bool gammaCorrection);
  86. [MethodImpl(MethodImplOptions.InternalCall)]
  87. private static extern PixelData Internal_GetPixels(IntPtr thisPtr, int mipLevel);
  88. [MethodImpl(MethodImplOptions.InternalCall)]
  89. private static extern AsyncOp Internal_GetGPUPixels(IntPtr thisPtr, int mipLevel);
  90. [MethodImpl(MethodImplOptions.InternalCall)]
  91. private static extern void Internal_SetPixels(IntPtr thisPtr, PixelData data, int mipLevel);
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern void Internal_SetPixelsArray(IntPtr thisPtr, Color[] data, int mipLevel);
  94. }
  95. }