Texture3D.cs 5.1 KB

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