Texture2D.cs 5.8 KB

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