Texture3D.cs 4.8 KB

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