TextureCube.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 cube texture. Cube texture consists of six two dimensional texture faces that can be accessed separately.
  12. /// </summary>
  13. public sealed class TextureCube : Texture
  14. {
  15. /// <summary>
  16. /// Constructor for internal use by the runtime.
  17. /// </summary>
  18. private TextureCube()
  19. { }
  20. /// <summary>
  21. /// Creates a new blank cube texture.
  22. /// </summary>
  23. /// <param name="width">Width of a single face of the texture in pixels.</param>
  24. /// <param name="height">Height of a single face 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 TextureCube(PixelFormat format, int width, int height, 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 of the specified face. Pixels will be read from system memory,
  40. /// which means the texture has to be created with <see cref="TextureUsage.CPUCached"/>. If the texture was updated
  41. /// from the GPU the pixels retrieved from this method will not reflect that, and you should use
  42. /// <see cref="GetGPUPixels"/> instead.
  43. /// </summary>
  44. /// <param name="face">Face of the cube to access.</param>
  45. /// <param name="mipLevel">Mip level to retrieve pixels for. Top level (0) is the highest quality.</param>
  46. /// <returns>A set of pixels for the specified mip level.</returns>
  47. public PixelData GetPixels(CubeFace face = CubeFace.PositiveX, int mipLevel = 0)
  48. {
  49. return Internal_GetPixels(mCachedPtr, face, mipLevel);
  50. }
  51. /// <summary>
  52. /// Sets pixels for the specified mip level of the specified face.
  53. /// </summary>
  54. /// <param name="data">Pixels to assign to the specified mip level. Pixel data must match the mip level size
  55. /// and texture pixel format.</param>
  56. /// <param name="face">Face of the cube to access.</param>
  57. /// <param name="mipLevel">Mip level to set pixels for. Top level (0) is the highest quality.</param>
  58. public void SetPixels(PixelData data, CubeFace face = CubeFace.PositiveX, int mipLevel = 0)
  59. {
  60. Internal_SetPixels(mCachedPtr, data, face, mipLevel);
  61. }
  62. /// <summary>
  63. /// Reads texture pixels directly from the GPU. This is similar to <see cref="GetPixels"/> but the texture doesn't
  64. /// need to be created with <see cref="TextureUsage.CPUCached"/>, and the data will contain any updates performed by
  65. /// the GPU. This method can be potentially slow as it introduces a CPU-GPU synchronization point. Additionally
  66. /// this method is asynchronous which means the data is not available immediately.
  67. /// </summary>
  68. /// <param name="face">Face of the cube to access.</param>
  69. /// <param name="mipLevel">Mip level to retrieve pixels for. Top level (0) is the highest quality.</param>
  70. /// <returns><see cref="AsyncOp"/> object that will contain <see cref="PixelData"/> object when the operation
  71. /// completes.</returns>
  72. public AsyncOp GetGPUPixels(CubeFace face = CubeFace.PositiveX, int mipLevel = 0)
  73. {
  74. return Internal_GetGPUPixels(mCachedPtr, face, mipLevel);
  75. }
  76. [MethodImpl(MethodImplOptions.InternalCall)]
  77. private static extern void Internal_CreateInstance(TextureCube instance, PixelFormat format, int width,
  78. int height, TextureUsage usage, int numSamples, bool hasMipmaps, bool gammaCorrection);
  79. [MethodImpl(MethodImplOptions.InternalCall)]
  80. private static extern PixelData Internal_GetPixels(IntPtr thisPtr, CubeFace face, int mipLevel);
  81. [MethodImpl(MethodImplOptions.InternalCall)]
  82. private static extern AsyncOp Internal_GetGPUPixels(IntPtr thisPtr, CubeFace face, int mipLevel);
  83. [MethodImpl(MethodImplOptions.InternalCall)]
  84. private static extern void Internal_SetPixels(IntPtr thisPtr, PixelData data, CubeFace face, int mipLevel);
  85. }
  86. /// <summary>
  87. /// Indices for the faces of a cube texture.
  88. /// </summary>
  89. public enum CubeFace
  90. {
  91. PositiveX = 0,
  92. NegativeX = 1,
  93. PositiveY = 2,
  94. NegativeY = 3,
  95. PositiveZ = 4,
  96. NegativeZ = 5,
  97. }
  98. /** @} */
  99. }