BsTexture.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_LOADSTORE = 0x800, /**< Texture that allows load/store operations from the GPU program. */
  19. TU_CPUCACHED = 0x1000, /**< All texture data will also be cached in system memory. */
  20. TU_DEFAULT = TU_STATIC
  21. };
  22. /**
  23. * @brief Different texture types.
  24. */
  25. enum TextureType
  26. {
  27. TEX_TYPE_1D = 1, /**< One dimensional texture. Just a row of pixels. */
  28. TEX_TYPE_2D = 2, /**< Two dimensional texture. */
  29. TEX_TYPE_3D = 3, /**< Three dimensional texture. */
  30. TEX_TYPE_CUBE_MAP = 4 /**< Texture consisting out of six 2D textures describing an inside of a cube. Allows special sampling. */
  31. };
  32. /**
  33. * @brief Mipmap options.
  34. */
  35. enum TextureMipmap
  36. {
  37. MIP_UNLIMITED = 0x7FFFFFFF /**< Create all mip maps down to 1x1. */
  38. };
  39. /**
  40. * @brief Abstract class representing a texture. Specific render systems have their
  41. * own Texture implementations. Internally represented as one or more surfaces
  42. * with pixels in a certain number of dimensions, backed by a hardware buffer.
  43. *
  44. * @note Core thread unless specified otherwise.
  45. */
  46. class BS_CORE_EXPORT Texture : public GpuResource
  47. {
  48. public:
  49. /**
  50. * @brief Gets the type of texture.
  51. *
  52. * @note Thread safe.
  53. */
  54. virtual TextureType getTextureType() const { return mTextureType; }
  55. /**
  56. * @brief Gets the number of mipmaps to be used for this texture. This number excludes the top level
  57. * map (which is always assumed to be present).
  58. *
  59. * @note Thread safe.
  60. */
  61. virtual UINT32 getNumMipmaps() const {return mNumMipmaps;}
  62. /**
  63. * @brief Gets whether this texture will be set up so that on sampling it,
  64. * hardware gamma correction is applied.
  65. *
  66. * @note Thread safe.
  67. */
  68. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  69. /**
  70. * @brief Gets the number of samples used for multisampling.
  71. * (0 if multisampling is not used).
  72. *
  73. * @note Thread safe.
  74. */
  75. virtual UINT32 getMultisampleCount() const { return mMultisampleCount; }
  76. /**
  77. * @brief Returns the height of the texture.
  78. *
  79. * @note Thread safe.
  80. */
  81. virtual UINT32 getHeight() const { return mHeight; }
  82. /**
  83. * @brief Returns the width of the texture.
  84. *
  85. * @note Thread safe.
  86. */
  87. virtual UINT32 getWidth() const { return mWidth; }
  88. /**
  89. * @brief Returns the depth of the texture (only applicable for 3D textures).
  90. *
  91. * @note Thread safe.
  92. */
  93. virtual UINT32 getDepth() const { return mDepth; }
  94. /**
  95. * @brief Returns texture usage (TextureUsage) of this texture.
  96. *
  97. * @note Thread safe.
  98. */
  99. virtual int getUsage() const { return mUsage; }
  100. /**
  101. * @brief Returns the pixel format for the texture surface.
  102. *
  103. * @note Thread safe.
  104. */
  105. virtual PixelFormat getFormat() const { return mFormat; }
  106. /**
  107. * @brief Returns true if the texture has an alpha layer.
  108. *
  109. * @note Thread safe.
  110. */
  111. virtual bool hasAlpha() const;
  112. /**
  113. * @brief Return the number of faces this texture has.
  114. *
  115. * @note Thread safe.
  116. */
  117. virtual UINT32 getNumFaces() const;
  118. /**
  119. * @copydoc GpuResource::_writeSubresourceSim
  120. */
  121. virtual void _writeSubresourceSim(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  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 a subresource in this texture to another texture.
  184. * Texture format and size of the subresource must match.
  185. *
  186. * You are allowed to copy from a multisampled to non-multisampled
  187. * surface, which will resolve the multisampled surface before copying.
  188. *
  189. * @param srcSubresourceIdx Index of the subresource to copy from.
  190. * @param destSubresourceIdx Index of the subresource to copy to.
  191. * @param target Texture that contains the destination subresource.
  192. *
  193. * @note Core thread only.
  194. */
  195. void copy(UINT32 srcSubresourceIdx, UINT32 destSubresourceIdx, TexturePtr& target);
  196. /**
  197. * @brief Reads data from the cached system memory texture buffer into the provided buffer.
  198. *
  199. * @param dest Previously allocated buffer to read data into.
  200. * @param mipLevel (optional) Mipmap level to read from.
  201. * @param face (optional) Texture face to read from.
  202. *
  203. * @note Sim thread only.
  204. *
  205. * The data read is the cached texture data. Any data written to the texture from the GPU
  206. * or core thread will not be reflected in this data. Use "readData" if you require
  207. * those changes.
  208. *
  209. * The texture must have been created with TU_CPUCACHED usage otherwise this method
  210. * will not return any data.
  211. */
  212. virtual void readDataSim(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0);
  213. /**
  214. * @brief Reads data from the texture buffer into the provided buffer.
  215. *
  216. * @param dest Previously allocated buffer to read data into.
  217. * @param mipLevel (optional) Mipmap level to read from.
  218. * @param face (optional) Texture face to read from.
  219. *
  220. * @note Core thread only.
  221. */
  222. virtual void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  223. /**
  224. * @brief Writes data from the provided buffer into the texture buffer.
  225. *
  226. * @param dest Buffer to retrieve the data from.
  227. * @param mipLevel (optional) Mipmap level to write into.
  228. * @param face (optional) Texture face to write into.
  229. * @param discardWholeBuffer (optional) If true any existing texture data will be discard. This can
  230. * improve performance of the write operation.
  231. *
  232. * @note Core thread only.
  233. */
  234. virtual void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) = 0;
  235. /**
  236. * @brief Returns a dummy 2x2 texture. Don't modify the returned texture.
  237. *
  238. * @note Thread safe.
  239. */
  240. static const HTexture& dummy();
  241. /**
  242. * @brief Returns true if the texture can be bound to a shader.
  243. *
  244. * @note This is only false for some rare special cases. (e.g. AA render texture in DX9)
  245. * Internal method.
  246. */
  247. virtual bool isBindableAsShaderResource() const { return true; }
  248. /************************************************************************/
  249. /* TEXTURE VIEW */
  250. /************************************************************************/
  251. /**
  252. * @brief Requests a texture view for the specified mip and array ranges. Returns an existing view of one for
  253. * the specified ranges already exists, otherwise creates a new one. You must release all views
  254. * by calling "releaseView" when done.
  255. *
  256. * @note Core thread only.
  257. */
  258. static TextureViewPtr requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  259. /**
  260. * @brief Releases the view. View won't actually get destroyed until all references to it are released.
  261. *
  262. * @note Core thread only.
  263. */
  264. static void releaseView(TextureViewPtr view);
  265. /************************************************************************/
  266. /* STATICS */
  267. /************************************************************************/
  268. /**
  269. * @brief Creates a new empty texture.
  270. *
  271. * @param texType Type of the texture.
  272. * @param width Width of the texture in pixels.
  273. * @param height Height of the texture in pixels.
  274. * @param depth Depth of the texture in pixels (Must be 1 for 2D textures).
  275. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  276. * @param format Format of the pixels.
  277. * @param usage Describes how we plan on using the texture in the pipeline.
  278. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  279. * converted back to linear space when sampled on GPU.
  280. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  281. */
  282. static HTexture create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  283. int numMips, PixelFormat format, int usage = TU_DEFAULT,
  284. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  285. /**
  286. * @brief Creates a new empty texture.
  287. *
  288. * @param texType Type of the texture.
  289. * @param width Width of the texture in pixels.
  290. * @param height Height of the texture in pixels.
  291. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  292. * @param format Format of the pixels.
  293. * @param usage Describes how we plan on using the texture in the pipeline.
  294. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  295. * converted back to linear space when sampled on GPU.
  296. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  297. */
  298. static HTexture create(TextureType texType, UINT32 width, UINT32 height, int numMips,
  299. PixelFormat format, int usage = TU_DEFAULT,
  300. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  301. /**
  302. * @copydoc create(TextureType, UINT32, UINT32, UINT32, int, PixelFormat, int, bool, UINT32)
  303. *
  304. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  305. */
  306. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  307. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  308. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  309. /**
  310. * @copydoc create(TextureType, UINT32, UINT32, int, PixelFormat, int, bool, UINT32)
  311. *
  312. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  313. */
  314. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  315. PixelFormat format, int usage = TU_DEFAULT, bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  316. protected:
  317. /************************************************************************/
  318. /* TEXTURE VIEW */
  319. /************************************************************************/
  320. /**
  321. * @brief Creates a new empty/undefined texture view.
  322. */
  323. virtual TextureViewPtr createView();
  324. /**
  325. * @brief Releases all internal texture view references. Views won't get destroyed if there are external references still held.
  326. */
  327. void clearBufferViews();
  328. /**
  329. * @brief Holds a single texture view with a usage reference count.
  330. */
  331. struct TextureViewReference
  332. {
  333. TextureViewReference(TextureViewPtr _view)
  334. :view(_view), refCount(0)
  335. { }
  336. TextureViewPtr view;
  337. UINT32 refCount;
  338. };
  339. UnorderedMap<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction> mTextureViews;
  340. protected:
  341. friend class TextureManager;
  342. Texture();
  343. /**
  344. * @copydoc GpuResource::initialize
  345. */
  346. void initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  347. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount);
  348. /**
  349. * @copydoc lock
  350. */
  351. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  352. /**
  353. * @copydoc unlock
  354. */
  355. virtual void unlockImpl() = 0;
  356. /**
  357. * @copydoc copy
  358. */
  359. virtual void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, TexturePtr& target) = 0;
  360. /**
  361. * @copydoc Resource::calculateSize
  362. */
  363. UINT32 calculateSize() const;
  364. /**
  365. * @brief Creates buffers used for caching of CPU texture data.
  366. *
  367. * @note Make sure to initialize all texture properties before calling this.
  368. */
  369. void createCPUBuffers();
  370. protected:
  371. UINT32 mHeight; // Immutable
  372. UINT32 mWidth; // Immutable
  373. UINT32 mDepth; // Immutable
  374. UINT32 mNumMipmaps; // Immutable
  375. bool mHwGamma; // Immutable
  376. UINT32 mMultisampleCount; // Immutable
  377. TextureType mTextureType; // Immutable
  378. PixelFormat mFormat; // Immutable
  379. int mUsage; // Immutable
  380. Vector<PixelDataPtr> mCPUSubresourceData;
  381. /************************************************************************/
  382. /* SERIALIZATION */
  383. /************************************************************************/
  384. public:
  385. friend class TextureRTTI;
  386. static RTTITypeBase* getRTTIStatic();
  387. virtual RTTITypeBase* getRTTI() const;
  388. };
  389. }