Texture.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /// Base class for all textures. Contains a set of pixels of certain dimensions that can be used for rendering
  12. /// or read/written directly.
  13. /// </summary>
  14. public class Texture : Resource
  15. {
  16. /// <summary>
  17. /// Returns the pixel format for the texture surface.
  18. /// </summary>
  19. public PixelFormat PixelFormat
  20. {
  21. get
  22. {
  23. PixelFormat value;
  24. Internal_GetPixelFormat(mCachedPtr, out value);
  25. return value;
  26. }
  27. }
  28. /// <summary>
  29. /// Returns a value that signals the engine in what way is the texture expected to be used.
  30. /// </summary>
  31. public TextureUsage Usage
  32. {
  33. get
  34. {
  35. TextureUsage value;
  36. Internal_GetUsage(mCachedPtr, out value);
  37. return value;
  38. }
  39. }
  40. /// <summary>
  41. /// Width of the texture in pixels.
  42. /// </summary>
  43. public int Width
  44. {
  45. get
  46. {
  47. int value;
  48. Internal_GetWidth(mCachedPtr, out value);
  49. return value;
  50. }
  51. }
  52. /// <summary>
  53. /// Height of the texture in pixels.
  54. /// </summary>
  55. public int Height
  56. {
  57. get
  58. {
  59. int value;
  60. Internal_GetHeight(mCachedPtr, out value);
  61. return value;
  62. }
  63. }
  64. /// <summary>
  65. /// Determines does the texture contain gamma corrected data. If true then the GPU will automatically convert
  66. /// the pixels to linear space before reading from the texture, and convert them to gamma space when writing
  67. /// to the texture.
  68. /// </summary>
  69. public bool GammaCorrection
  70. {
  71. get
  72. {
  73. bool value;
  74. Internal_GetGammaCorrection(mCachedPtr, out value);
  75. return value;
  76. }
  77. }
  78. /// <summary>
  79. /// Number of samples per pixel. Zero or one mean no multisampling will be used.
  80. /// </summary>
  81. public int SampleCount
  82. {
  83. get
  84. {
  85. int value;
  86. Internal_GetSampleCount(mCachedPtr, out value);
  87. return value;
  88. }
  89. }
  90. /// <summary>
  91. /// Returns how many mipmap levels does the texture contain.
  92. /// </summary>
  93. public int MipmapCount
  94. {
  95. get
  96. {
  97. int value;
  98. Internal_GetMipmapCount(mCachedPtr, out value);
  99. return value;
  100. }
  101. }
  102. /// <summary>
  103. /// Constructor for the internal use by the runtime.
  104. /// </summary>
  105. private Texture()
  106. { }
  107. /// <summary>
  108. /// Creates a new blank 2D texture.
  109. /// </summary>
  110. /// <param name="width">Width of the texture in pixels.</param>
  111. /// <param name="height">Height of the texture in pixels.</param>
  112. /// <param name="format">Format of the pixels.</param>
  113. /// <param name="usage">Describes planned texture use.</param>
  114. /// <param name="numSamples">If higher than 1, texture containing multiple samples per pixel is created.</param>
  115. /// <param name="hasMipmaps">Should the texture allocate memory for the entire mip-map chain or only the top level.
  116. /// </param>
  117. /// <param name="gammaCorrection">If true the texture data is assumed to have be gamma corrected and will be
  118. /// converted back to linear space when sampled on GPU, and converted to gamma space
  119. /// before being written by the GPU.</param>
  120. public static Texture Create2D(int width, int height, PixelFormat format = PixelFormat.R8G8B8A8,
  121. TextureUsage usage = TextureUsage.Default, int numSamples = 1, bool hasMipmaps = false,
  122. bool gammaCorrection = false)
  123. {
  124. Texture texture = new Texture();
  125. Internal_CreateInstance(texture, format, width, height, 1, TextureType.Texture2D, usage, numSamples,
  126. hasMipmaps, gammaCorrection);
  127. return texture;
  128. }
  129. /// <summary>
  130. /// Creates a new blank 3D texture.
  131. /// </summary>
  132. /// <param name="width">Width of the texture in pixels.</param>
  133. /// <param name="height">Height of the texture in pixels.</param>
  134. /// <param name="depth">Depth of the texture in pixels.</param>
  135. /// <param name="format">Format of the pixels.</param>
  136. /// <param name="usage">Describes planned texture use.</param>
  137. /// <param name="hasMipmaps">Should the texture allocate memory for the entire mip-map chain or only the top level.
  138. /// </param>
  139. public static Texture Create3D(int width, int height, int depth, PixelFormat format = PixelFormat.R8G8B8A8,
  140. TextureUsage usage = TextureUsage.Default, bool hasMipmaps = false)
  141. {
  142. Texture texture = new Texture();
  143. Internal_CreateInstance(texture, format, width, height, depth, TextureType.Texture3D, usage, 1,
  144. hasMipmaps, false);
  145. return texture;
  146. }
  147. /// <summary>
  148. /// Creates a new blank cubemap texture.
  149. /// </summary>
  150. /// <param name="size">Width & height of a single cubemap face in pixels.</param>
  151. /// <param name="format">Format of the pixels.</param>
  152. /// <param name="usage">Describes planned texture use.</param>
  153. /// <param name="hasMipmaps">Should the texture allocate memory for the entire mip-map chain or only the top level.
  154. /// </param>
  155. /// <param name="gammaCorrection">If true the texture data is assumed to have be gamma corrected and will be
  156. /// converted back to linear space when sampled on GPU, and converted to gamma space
  157. /// before being written by the GPU.</param>
  158. public static Texture CreateCube(int size, PixelFormat format = PixelFormat.R8G8B8A8,
  159. TextureUsage usage = TextureUsage.Default, bool hasMipmaps = false, bool gammaCorrection = false)
  160. {
  161. Texture texture = new Texture();
  162. Internal_CreateInstance(texture, format, size, size, 1, TextureType.TextureCube, usage, 1,
  163. hasMipmaps, gammaCorrection);
  164. return texture;
  165. }
  166. /// <summary>
  167. /// Returns pixels for the specified mip level & face. Pixels will be read from system memory, which means the
  168. /// texture has to be created with <see cref="TextureUsage.CPUCached"/>. If the texture was updated from the GPU the
  169. /// pixels retrieved from this method will not reflect that, and you should use <see cref="GetGPUPixels"/> instead.
  170. /// </summary>
  171. /// <param name="mipLevel">Mip level to retrieve pixels for. Top level (0) is the highest quality.</param>
  172. /// <param name="face">Face to read the pixels from. Cubemap textures have six faces whose face indices are as
  173. /// specified in the <see cref="CubeFace"/> enum. Array textures can have an arbitrary number
  174. /// of faces (if it's a cubemap array it has to be a multiple of 6).</param>
  175. /// <returns>A set of pixels for the specified mip level.</returns>
  176. public PixelData GetPixels(int mipLevel = 0, int face = 0)
  177. {
  178. return Internal_GetPixels(mCachedPtr, face, mipLevel);
  179. }
  180. /// <summary>
  181. /// Sets pixels for the specified mip level and face.
  182. /// </summary>
  183. /// <param name="data">Pixels to assign to the specified mip level. Pixel data must match the mip level size
  184. /// and texture pixel format.</param>
  185. /// <param name="mipLevel">Mip level to set pixels for. Top level (0) is the highest quality.</param>
  186. /// <param name="face">Face to set pixels for. Cubemap textures have six faces whose face indices are as
  187. /// specified in the <see cref="CubeFace"/> enum. Array textures can have an arbitrary number
  188. /// of faces (if it's a cubemap array it has to be a multiple of 6).</param>
  189. public void SetPixels(PixelData data, int mipLevel = 0, int face = 0)
  190. {
  191. Internal_SetPixels(mCachedPtr, data, face, mipLevel);
  192. }
  193. /// <summary>
  194. /// Sets pixels for the specified mip level and face.
  195. /// </summary>
  196. /// <param name="data">Pixels to assign to the specified mip level. Size of the array must match the mip level
  197. /// dimensions. Data is expected to be laid out row by row. Pixels will be automatically
  198. /// converted to the valid pixel format.</param>
  199. /// <param name="mipLevel">Mip level to set pixels for. Top level (0) is the highest quality.</param>
  200. /// <param name="face">Face to set pixels for. Cubemap textures have six faces whose face indices are as
  201. /// specified in the <see cref="CubeFace"/> enum. Array textures can have an arbitrary number
  202. /// of faces (if it's a cubemap array it has to be a multiple of 6).</param>
  203. public void SetPixels(Color[] data, int mipLevel = 0, int face = 0)
  204. {
  205. Internal_SetPixelsArray(mCachedPtr, data, face, mipLevel);
  206. }
  207. /// <summary>
  208. /// Reads texture pixels directly from the GPU. This is similar to <see cref="GetPixels"/> but the texture doesn't
  209. /// need to be created with <see cref="TextureUsage.CPUCached"/>, and the data will contain any updates performed by
  210. /// the GPU. This method can be potentially slow as it introduces a CPU-GPU synchronization point. Additionally
  211. /// this method is asynchronous which means the data is not available immediately.
  212. /// </summary>
  213. /// <param name="mipLevel">Mip level to retrieve pixels for. Top level (0) is the highest quality.</param>
  214. /// <param name="face">Face to read the pixels from. Cubemap textures have six faces whose face indices are as
  215. /// specified in the <see cref="CubeFace"/> enum. Array textures can have an arbitrary number
  216. /// of faces (if it's a cubemap array it has to be a multiple of 6).</param>
  217. /// <returns><see cref="AsyncOp"/> object that will contain <see cref="PixelData"/> object when the operation
  218. /// completes.</returns>
  219. public AsyncOp GetGPUPixels(int mipLevel = 0, int face = 0)
  220. {
  221. return Internal_GetGPUPixels(mCachedPtr, face, mipLevel);
  222. }
  223. [MethodImpl(MethodImplOptions.InternalCall)]
  224. private static extern void Internal_GetPixelFormat(IntPtr thisPtr, out PixelFormat value);
  225. [MethodImpl(MethodImplOptions.InternalCall)]
  226. private static extern void Internal_GetUsage(IntPtr thisPtr, out TextureUsage value);
  227. [MethodImpl(MethodImplOptions.InternalCall)]
  228. private static extern void Internal_GetWidth(IntPtr thisPtr, out int value);
  229. [MethodImpl(MethodImplOptions.InternalCall)]
  230. private static extern void Internal_GetHeight(IntPtr thisPtr, out int value);
  231. [MethodImpl(MethodImplOptions.InternalCall)]
  232. private static extern void Internal_GetGammaCorrection(IntPtr thisPtr, out bool value);
  233. [MethodImpl(MethodImplOptions.InternalCall)]
  234. private static extern void Internal_GetSampleCount(IntPtr thisPtr, out int value);
  235. [MethodImpl(MethodImplOptions.InternalCall)]
  236. private static extern void Internal_GetMipmapCount(IntPtr thisPtr, out int value);
  237. [MethodImpl(MethodImplOptions.InternalCall)]
  238. private static extern void Internal_CreateInstance(Texture instance, PixelFormat format, int width,
  239. int height, int depth, TextureType texType, TextureUsage usage, int numSamples, bool hasMipmaps,
  240. bool gammaCorrection);
  241. [MethodImpl(MethodImplOptions.InternalCall)]
  242. private static extern PixelData Internal_GetPixels(IntPtr thisPtr, int face, int mipLevel);
  243. [MethodImpl(MethodImplOptions.InternalCall)]
  244. private static extern AsyncOp Internal_GetGPUPixels(IntPtr thisPtr, int face, int mipLevel);
  245. [MethodImpl(MethodImplOptions.InternalCall)]
  246. private static extern void Internal_SetPixels(IntPtr thisPtr, PixelData data, int face, int mipLevel);
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. private static extern void Internal_SetPixelsArray(IntPtr thisPtr, Color[] data, int face, int mipLevel);
  249. }
  250. /// <summary>
  251. /// Flags that describe how is a texture used.
  252. /// </summary>
  253. public enum TextureUsage // Note: Must match C++ enum TextureUsage
  254. {
  255. /// <summary>
  256. /// A regular texture that is not often or ever updated from the CPU.
  257. /// </summary>
  258. Default = 0x1,
  259. /// <summary>
  260. /// A regular texture that is often updated by the CPU.
  261. /// </summary>
  262. Dynamic = 0x2,
  263. /// <summary>
  264. /// Texture that can be rendered to by the GPU.
  265. /// </summary>
  266. Render = 0x200,
  267. /// <summary>
  268. /// Texture used as a depth/stencil buffer by the GPU.
  269. /// </summary>
  270. DepthStencil = 0x400,
  271. /// <summary>
  272. /// Texture that allows load/store operations from the GPU program.
  273. /// </summary>
  274. LoadStore = 0x800,
  275. /// <summary>
  276. /// All mesh data will also be cached in CPU memory, making it available for fast read access from the CPU.
  277. /// </summary>
  278. CPUCached = 0x1000,
  279. /// <summary>
  280. /// Allows the CPU to directly read the texture data buffers from the GPU.
  281. /// </summary>
  282. CPUReadable = 0x2000,
  283. }
  284. /// <summary>
  285. /// Indices for the faces of a cube texture.
  286. /// </summary>
  287. public enum CubeFace
  288. {
  289. PositiveX = 0,
  290. NegativeX = 1,
  291. PositiveY = 2,
  292. NegativeY = 3,
  293. PositiveZ = 4,
  294. NegativeZ = 5,
  295. }
  296. /// <summary>
  297. /// Supported texture types.
  298. /// </summary>
  299. internal enum TextureType // Note: Must match C++ enum TextureType
  300. {
  301. Texture2D = 2,
  302. Texture3D = 3,
  303. TextureCube = 4
  304. }
  305. /** @} */
  306. }