BsTexture.h 14 KB

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