BsTexture.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. #include "BsGpuResource.h"
  7. #include "BsHardwareBuffer.h"
  8. #include "BsPixelUtil.h"
  9. #include "BsTextureView.h"
  10. namespace BansheeEngine
  11. {
  12. /**
  13. * @brief Properties that describe how is the texture used.
  14. */
  15. enum TextureUsage
  16. {
  17. TU_STATIC = GBU_STATIC, /**< A regular texture that is not often or ever updated from the CPU. */
  18. TU_DYNAMIC = GBU_DYNAMIC, /**< A regular texture that is often updated by the CPU. */
  19. TU_RENDERTARGET = 0x200, /**< Texture used for rendering by the GPU. */
  20. TU_DEPTHSTENCIL = 0x400, /**< Texture used as a depth/stencil buffer by the GPU. */
  21. TU_DEFAULT = TU_STATIC
  22. };
  23. /**
  24. * @brief Different texture types.
  25. */
  26. enum TextureType
  27. {
  28. TEX_TYPE_1D = 1, /**< One dimensional texture. Just a row of pixels. */
  29. TEX_TYPE_2D = 2, /**< Two dimensional texture. */
  30. TEX_TYPE_3D = 3, /**< Three dimensional texture. */
  31. TEX_TYPE_CUBE_MAP = 4 /**< Texture consisting out of six 2D textures describing an inside of a cube. Allows special sampling. */
  32. };
  33. /**
  34. * @brief Mipmap options.
  35. */
  36. enum TextureMipmap
  37. {
  38. MIP_UNLIMITED = 0x7FFFFFFF /**< Create all mip maps down to 1x1. */
  39. };
  40. /**
  41. * @brief Abstract class representing a texture. Specific render systems have their
  42. * own Texture implementations. Internally represented as one or more surfaces
  43. * with pixels in a certain number of dimensions, backed by a hardware buffer.
  44. *
  45. * @note Core thread unless specified otherwise.
  46. */
  47. class BS_CORE_EXPORT Texture : public GpuResource
  48. {
  49. public:
  50. /**
  51. * @brief Gets the type of texture.
  52. *
  53. * @note Thread safe.
  54. */
  55. virtual TextureType getTextureType() const { return mTextureType; }
  56. /**
  57. * @brief Gets the number of mipmaps to be used for this texture. This number excludes the top level
  58. * map (which is always assumed to be present).
  59. *
  60. * @note Thread safe.
  61. */
  62. virtual UINT32 getNumMipmaps() const {return mNumMipmaps;}
  63. /**
  64. * @brief Gets whether this texture will be set up so that on sampling it,
  65. * hardware gamma correction is applied.
  66. *
  67. * @note Thread safe.
  68. */
  69. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  70. /**
  71. * @brief Gets the number of samples used for multisampling.
  72. * (0 if multisampling is not used).
  73. *
  74. * @note Thread safe.
  75. */
  76. virtual UINT32 getMultisampleCount() const { return mMultisampleCount; }
  77. /**
  78. * @brief Get a render-system specific hint used for determining
  79. * multisampling type.
  80. *
  81. * @note Thread safe.
  82. */
  83. virtual const String& getMultisampleHint() const { return mMultisampleHint; }
  84. /**
  85. * @brief Returns the height of the texture.
  86. *
  87. * @note Thread safe.
  88. */
  89. virtual UINT32 getHeight() const { return mHeight; }
  90. /**
  91. * @brief Returns the width of the texture.
  92. *
  93. * @note Thread safe.
  94. */
  95. virtual UINT32 getWidth() const { return mWidth; }
  96. /**
  97. * @brief Returns the depth of the texture (only applicable for 3D textures).
  98. *
  99. * @note Thread safe.
  100. */
  101. virtual UINT32 getDepth() const { return mDepth; }
  102. /**
  103. * @brief Returns texture usage (TextureUsage) of this texture.
  104. *
  105. * @note Thread safe.
  106. */
  107. virtual int getUsage() const { return mUsage; }
  108. /**
  109. * @brief Returns the pixel format for the texture surface.
  110. *
  111. * @note Thread safe.
  112. */
  113. virtual PixelFormat getFormat() const { return mFormat; }
  114. /**
  115. * @brief Returns true if the texture has an alpha layer.
  116. *
  117. * @note Thread safe.
  118. */
  119. virtual bool hasAlpha() const;
  120. /**
  121. * @brief Return the number of faces this texture has.
  122. *
  123. * @note Thread safe.
  124. */
  125. virtual UINT32 getNumFaces() const;
  126. /**
  127. * @copydoc GpuResource::writeSubresource
  128. */
  129. virtual void writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  130. /**
  131. * @copydoc GpuResource::readSubresource
  132. */
  133. virtual void readSubresource(UINT32 subresourceIdx, GpuResourceData& data);
  134. /**
  135. * @brief Allocates a buffer you may use for storage when reading or writing a sub-resource. You
  136. * need to allocate such a buffer if you are calling "readSubresource".
  137. *
  138. * You can retrieve a sub-resource index by calling "mapToSubresourceIdx".
  139. *
  140. * @note Thread safe.
  141. */
  142. PixelDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  143. /**
  144. * @brief Maps a sub-resource index to an exact face and mip level. Sub-resource indexes
  145. * are used when reading or writing to the resource.
  146. *
  147. * @note Sub-resource index is only valid for the instance it was created on. You cannot use a sub-resource
  148. * index from a different texture and expect to get valid result. Modifying the resource so the number
  149. * of sub-resources changes invalidates all sub-resource indexes.
  150. *
  151. * Thread safe.
  152. */
  153. void mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const;
  154. /**
  155. * @brief Map a face and a mip level to a sub-resource index you can use for updating or reading
  156. * a specific sub-resource.
  157. *
  158. * @note Generated sub-resource index is only valid for the instance it was created on. Modifying the resource so the number
  159. * of sub-resources changes, invalidates all sub-resource indexes.
  160. *
  161. * Thread safe.
  162. */
  163. UINT32 mapToSubresourceIdx(UINT32 face, UINT32 mip) const;
  164. /**
  165. * @brief Locks the buffer for reading or writing.
  166. *
  167. * @param options Options for controlling what you may do with the locked data.
  168. * @param mipLevel (optional) Mipmap level to lock.
  169. * @param face (optional) Texture face to lock.
  170. *
  171. * @return Pointer to the buffer data. Only valid until you call unlock.
  172. *
  173. * @note If you are just reading or writing one block of data use
  174. * readData/writeData methods as they can be much faster in certain situations.
  175. *
  176. * Core thread only.
  177. */
  178. PixelData lock(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  179. /**
  180. * @brief Unlocks a previously locked buffer. After the buffer is unlocked,
  181. * any data returned by lock becomes invalid.
  182. *
  183. * @note Core thread only.
  184. */
  185. void unlock();
  186. /**
  187. * @brief Copies the contents of this texture to another texture. Texture format
  188. * and size must match.
  189. *
  190. * @note Core thread only.
  191. */
  192. void copy(TexturePtr& target);
  193. /**
  194. * @brief Reads data from the texture buffer into the provided buffer.
  195. *
  196. * @param dest Previously allocated buffer to read data into.
  197. * @param mipLevel (optional) Mipmap level to read from.
  198. * @param face (optional) Texture face to read from.
  199. *
  200. * @note Core thread only.
  201. */
  202. virtual void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  203. /**
  204. * @brief Writes data from the provided buffer into the texture buffer.
  205. *
  206. * @param dest Buffer to retrieve the data from.
  207. * @param mipLevel (optional) Mipmap level to write into.
  208. * @param face (optional) Texture face to write into.
  209. * @param discardWholeBuffer (optional) If true any existing texture data will be discard. This can
  210. * improve performance of the write operation.
  211. *
  212. * @note Core thread only.
  213. */
  214. virtual void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) = 0;
  215. /**
  216. * @brief Returns a dummy 2x2 texture. Don't modify the returned texture.
  217. *
  218. * @note Thread safe.
  219. */
  220. static const HTexture& dummy();
  221. /**
  222. * @brief Returns true if the texture can be bound to a shader.
  223. *
  224. * @note This is only false for some rare special cases. (e.g. AA render texture in DX9)
  225. * Internal method.
  226. */
  227. virtual bool isBindableAsShaderResource() const { return true; }
  228. /************************************************************************/
  229. /* TEXTURE VIEW */
  230. /************************************************************************/
  231. /**
  232. * @brief Requests a texture view for the specified mip and array ranges. Returns an existing view of one for
  233. * the specified ranges already exists, otherwise creates a new one. You must release all views
  234. * by calling "releaseView" when done.
  235. */
  236. static TextureViewPtr requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  237. /**
  238. * @brief Releases the view. View won't actually get destroyed until all references to it are released.
  239. */
  240. static void releaseView(TextureViewPtr view);
  241. /************************************************************************/
  242. /* STATICS */
  243. /************************************************************************/
  244. /**
  245. * @brief Creates a new empty texture.
  246. *
  247. * @param texType Type of the texture.
  248. * @param width Width of the texture in pixels.
  249. * @param height Height of the texture in pixels.
  250. * @param depth Depth of the texture in pixels (Must be 1 for 2D textures).
  251. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  252. * @param format Format of the pixels.
  253. * @param usage Describes how we plan on using the texture in the pipeline.
  254. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  255. * converted back to linear space when sampled on GPU.
  256. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  257. * @param multisampleHint Hint about what kind of multisampling to use. Render system specific.
  258. */
  259. static HTexture create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  260. int numMips, PixelFormat format, int usage = TU_DEFAULT,
  261. bool hwGammaCorrection = false, UINT32 multisampleCount = 0, const String& multisampleHint = StringUtil::BLANK);
  262. /**
  263. * @brief Creates a new empty texture.
  264. *
  265. * @param texType Type of the texture.
  266. * @param width Width of the texture in pixels.
  267. * @param height Height of the texture in pixels.
  268. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  269. * @param format Format of the pixels.
  270. * @param usage Describes how we plan on using the texture in the pipeline.
  271. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  272. * converted back to linear space when sampled on GPU.
  273. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  274. * @param multisampleHint Hint about what kind of multisampling to use. Render system specific.
  275. */
  276. static HTexture create(TextureType texType, UINT32 width, UINT32 height, int numMips,
  277. PixelFormat format, int usage = TU_DEFAULT,
  278. bool hwGammaCorrection = false, UINT32 multisampleCount = 0, const String& multisampleHint = StringUtil::BLANK);
  279. /**
  280. * @copydoc create(TextureType, UINT32, UINT32, UINT32, int, PixelFormat, int, bool, UINT32, const String&)
  281. *
  282. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  283. */
  284. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  285. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  286. bool hwGammaCorrection = false, UINT32 multisampleCount = 0, const String& multisampleHint = StringUtil::BLANK);
  287. /**
  288. * @copydoc create(TextureType, UINT32, UINT32, int, PixelFormat, int, bool, UINT32, const String&)
  289. *
  290. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  291. */
  292. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  293. PixelFormat format, int usage = TU_DEFAULT,
  294. bool hwGammaCorrection = false, UINT32 multisampleCount = 0, const String& multisampleHint = StringUtil::BLANK);
  295. protected:
  296. /************************************************************************/
  297. /* TEXTURE VIEW */
  298. /************************************************************************/
  299. /**
  300. * @brief Creates a new empty/undefined texture view.
  301. */
  302. virtual TextureViewPtr createView();
  303. /**
  304. * @brief Releases all internal texture view references. Views won't get destroyed if there are external references still held.
  305. */
  306. void clearBufferViews();
  307. /**
  308. * @brief Holds a single texture view with a usage reference count.
  309. */
  310. struct TextureViewReference
  311. {
  312. TextureViewReference(TextureViewPtr _view)
  313. :view(_view), refCount(0)
  314. { }
  315. TextureViewPtr view;
  316. UINT32 refCount;
  317. };
  318. UnorderedMap<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction> mTextureViews;
  319. protected:
  320. friend class TextureManager;
  321. Texture();
  322. /**
  323. * @copydoc GpuResource::initialize
  324. */
  325. void initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  326. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount, const String& multisampleHint);
  327. /**
  328. * @copydoc lock
  329. */
  330. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  331. /**
  332. * @copydoc unlock
  333. */
  334. virtual void unlockImpl() = 0;
  335. /**
  336. * @copydoc copy
  337. */
  338. virtual void copyImpl(TexturePtr& target) = 0;
  339. /**
  340. * @copydoc Resource::calculateSize
  341. */
  342. UINT32 calculateSize() const;
  343. protected:
  344. UINT32 mHeight; // Immutable
  345. UINT32 mWidth; // Immutable
  346. UINT32 mDepth; // Immutable
  347. UINT32 mNumMipmaps; // Immutable
  348. bool mHwGamma; // Immutable
  349. UINT32 mMultisampleCount; // Immutable
  350. String mMultisampleHint; // Immutable
  351. TextureType mTextureType; // Immutable
  352. PixelFormat mFormat; // Immutable
  353. int mUsage; // Immutable
  354. /************************************************************************/
  355. /* SERIALIZATION */
  356. /************************************************************************/
  357. public:
  358. friend class TextureRTTI;
  359. static RTTITypeBase* getRTTIStatic();
  360. virtual RTTITypeBase* getRTTI() const;
  361. };
  362. }