Texture3D.cs 5.1 KB

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