Texture2D.cs 5.9 KB

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