BsTexture.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. #include "BsHardwareBuffer.h"
  5. #include "BsPixelUtil.h"
  6. #include "BsTextureView.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Flags that describe how is a 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 that can be rendered to 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, /**< Ensures 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 Contains various information about a texture
  41. */
  42. class BS_CORE_EXPORT TextureProperties
  43. {
  44. public:
  45. TextureProperties();
  46. TextureProperties(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  47. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount);
  48. /**
  49. * @brief Gets the type of texture.
  50. */
  51. 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. UINT32 getNumMipmaps() const {return mNumMipmaps;}
  57. /**
  58. * @brief Gets whether this texture will be set up so that on sampling it,
  59. * hardware gamma correction is applied.
  60. */
  61. bool isHardwareGammaEnabled() const { return mHwGamma; }
  62. /**
  63. * @brief Gets the number of samples used for multisampling.
  64. * (0 if multisampling is not used).
  65. */
  66. UINT32 getMultisampleCount() const { return mMultisampleCount; }
  67. /**
  68. * @brief Returns the height of the texture.
  69. */
  70. UINT32 getHeight() const { return mHeight; }
  71. /**
  72. * @brief Returns the width of the texture.
  73. */
  74. UINT32 getWidth() const { return mWidth; }
  75. /**
  76. * @brief Returns the depth of the texture (only applicable for 3D textures).
  77. */
  78. UINT32 getDepth() const { return mDepth; }
  79. /**
  80. * @brief Returns texture usage (TextureUsage) of this texture.
  81. */
  82. int getUsage() const { return mUsage; }
  83. /**
  84. * @brief Returns the pixel format for the texture surface.
  85. */
  86. PixelFormat getFormat() const { return mFormat; }
  87. /**
  88. * @brief Returns true if the texture has an alpha layer.
  89. */
  90. bool hasAlpha() const;
  91. /**
  92. * @brief Return the number of faces this texture has.
  93. */
  94. UINT32 getNumFaces() const;
  95. /**
  96. * @brief Maps a sub-resource index to an exact face and mip level. Sub-resource indexes
  97. * are used when reading or writing to the resource.
  98. *
  99. * @note Sub-resource index is only valid for the instance it was created on. You cannot use a sub-resource
  100. * index from a different texture and expect to get valid result. Modifying the resource so the number
  101. * of sub-resources changes invalidates all sub-resource indexes.
  102. */
  103. void mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const;
  104. /**
  105. * @brief Map a face and a mip level to a sub-resource index you can use for updating or reading
  106. * a specific sub-resource.
  107. *
  108. * @note Generated sub-resource index is only valid for the instance it was created on. Modifying the resource so the number
  109. * of sub-resources changes, invalidates all sub-resource indexes.
  110. */
  111. UINT32 mapToSubresourceIdx(UINT32 face, UINT32 mip) const;
  112. /**
  113. * @brief Allocates a buffer you may use for storage when reading or writing a sub-resource. You
  114. * need to allocate such a buffer if you are calling "readSubresource".
  115. *
  116. * You can retrieve a sub-resource index by calling "mapToSubresourceIdx".
  117. *
  118. * @note Thread safe.
  119. */
  120. PixelDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  121. protected:
  122. friend class TextureRTTI;
  123. UINT32 mHeight;
  124. UINT32 mWidth;
  125. UINT32 mDepth;
  126. UINT32 mNumMipmaps;
  127. bool mHwGamma;
  128. UINT32 mMultisampleCount;
  129. TextureType mTextureType;
  130. PixelFormat mFormat;
  131. int mUsage;
  132. };
  133. /**
  134. * @brief Core thread version of a Texture.
  135. *
  136. * @see Texture
  137. *
  138. * @note Core thread.
  139. */
  140. class BS_CORE_EXPORT TextureCore : public CoreObjectCore
  141. {
  142. public:
  143. TextureCore(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  144. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount);
  145. virtual ~TextureCore() {}
  146. /**
  147. * @brief Updates a part of the texture with the provided data.
  148. *
  149. * @param subresourceIdx Index of the subresource to update, if the texture has more than one.
  150. * @param data Data to update the texture with.
  151. * @param discardEntireBuffer When true the existing contents of the resource you are updating will be discarded. This can make the
  152. * operation faster. Resources with certain buffer types might require this flag to be in a specific state
  153. * otherwise the operation will fail.
  154. */
  155. virtual void writeSubresource(UINT32 subresourceIdx, const PixelData& data, bool discardEntireBuffer);
  156. /**
  157. * @brief Reads a part of the current resource into the provided "data" parameter.
  158. * Data buffer needs to be pre-allocated.
  159. *
  160. * @param subresourceIdx Index of the subresource to update, if the texture has more than one.
  161. * @param data Buffer that will receive the data. Should be allocated with "allocateSubresourceBuffer"
  162. * to ensure it is of valid type and size.
  163. */
  164. virtual void readSubresource(UINT32 subresourceIdx, PixelData& data);
  165. /**
  166. * @brief Locks the buffer for reading or writing.
  167. *
  168. * @param options Options for controlling what you may do with the locked data.
  169. * @param mipLevel (optional) Mipmap level to lock.
  170. * @param face (optional) Texture face to lock.
  171. *
  172. * @return Pointer to the buffer data. Only valid until you call unlock.
  173. *
  174. * @note If you are just reading or writing one block of data use
  175. * readData/writeData methods as they can be much faster in certain situations.
  176. */
  177. PixelData lock(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  178. /**
  179. * @brief Unlocks a previously locked buffer. After the buffer is unlocked,
  180. * any data returned by lock becomes invalid.
  181. */
  182. void unlock();
  183. /**
  184. * @brief Copies the contents a subresource in this texture to another texture.
  185. * Texture format and size of the subresource must match.
  186. *
  187. * You are allowed to copy from a multisampled to non-multisampled
  188. * surface, which will resolve the multisampled surface before copying.
  189. *
  190. * @param srcSubresourceIdx Index of the subresource to copy from.
  191. * @param destSubresourceIdx Index of the subresource to copy to.
  192. * @param target Texture that contains the destination subresource.
  193. */
  194. void copy(UINT32 srcSubresourceIdx, UINT32 destSubresourceIdx, const SPtr<TextureCore>& target);
  195. /**
  196. * @brief Reads data from the texture buffer into the provided buffer.
  197. *
  198. * @param dest Previously allocated buffer to read data into.
  199. * @param mipLevel (optional) Mipmap level to read from.
  200. * @param face (optional) Texture face to read from.
  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. virtual void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) = 0;
  213. /**
  214. * @brief Returns true if the texture can be bound to a shader.
  215. *
  216. * @note This is only false for some rare special cases. (e.g. AA render texture in DX9)
  217. * Internal method.
  218. */
  219. virtual bool isBindableAsShaderResource() const { return true; }
  220. /**
  221. * @brief Returns properties that contain information about the texture.
  222. */
  223. const TextureProperties& getProperties() const { return mProperties; }
  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. * @note Core thread only.
  233. */
  234. static TextureViewPtr requestView(const SPtr<TextureCore>& texture, UINT32 mostDetailMip, UINT32 numMips,
  235. UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  236. /**
  237. * @brief Releases the view. View won't actually get destroyed until all references to it are released.
  238. *
  239. * @note Core thread only.
  240. */
  241. static void releaseView(const TextureViewPtr& view);
  242. protected:
  243. /**
  244. * @copydoc lock
  245. */
  246. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  247. /**
  248. * @copydoc unlock
  249. */
  250. virtual void unlockImpl() = 0;
  251. /**
  252. * @copydoc copy
  253. */
  254. virtual void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target) = 0;
  255. /************************************************************************/
  256. /* TEXTURE VIEW */
  257. /************************************************************************/
  258. /**
  259. * @brief Creates a new empty/undefined texture view.
  260. */
  261. virtual TextureViewPtr createView(const SPtr<TextureCore>& texture, const TEXTURE_VIEW_DESC& desc);
  262. /**
  263. * @brief Releases all internal texture view references. Views won't get destroyed if there are external references still held.
  264. */
  265. void clearBufferViews();
  266. /**
  267. * @brief Holds a single texture view with a usage reference count.
  268. */
  269. struct TextureViewReference
  270. {
  271. TextureViewReference(TextureViewPtr _view)
  272. :view(_view), refCount(0)
  273. { }
  274. TextureViewPtr view;
  275. UINT32 refCount;
  276. };
  277. UnorderedMap<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction> mTextureViews;
  278. TextureProperties mProperties;
  279. };
  280. /**
  281. * @brief Abstract class representing a texture. Specific render systems have their
  282. * own Texture implementations. Internally represented as one or more surfaces
  283. * with pixels in a certain number of dimensions, backed by a hardware buffer.
  284. *
  285. * @note Sim thread.
  286. */
  287. class BS_CORE_EXPORT Texture : public Resource
  288. {
  289. public:
  290. /**
  291. * @brief Updates the texture with new data. The actual write will be queued for later execution on the core thread.
  292. * Provided data buffer will be locked until the operation completes.
  293. *
  294. * @param accessor Accessor to queue the operation on.
  295. *
  296. * @return Async operation object you can use to track operation completion.
  297. *
  298. * @see TextureCore::writeSubresource
  299. */
  300. AsyncOp writeSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const PixelDataPtr& data, bool discardEntireBuffer);
  301. /**
  302. * @brief Reads internal texture data to the provided previously allocated buffer. The read is queued for execution
  303. * on the core thread and not executed immediately. Provided data buffer will be locked until the
  304. * operation completes.
  305. *
  306. * @param accessor Accessor to queue the operation on.
  307. *
  308. * @return Async operation object you can use to track operation completion.
  309. *
  310. * @see TextureCore::readSubresource
  311. */
  312. AsyncOp readSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const PixelDataPtr& data);
  313. /**
  314. * @brief Reads data from the cached system memory texture buffer into the provided buffer.
  315. *
  316. * @param dest Previously allocated buffer to read data into.
  317. * @param mipLevel (optional) Mipmap level to read from.
  318. * @param face (optional) Texture face to read from.
  319. *
  320. * @note The data read is the cached texture data. Any data written to the texture from the GPU
  321. * or core thread will not be reflected in this data. Use "readSubresource" if you require
  322. * those changes.
  323. *
  324. * The texture must have been created with TU_CPUCACHED usage otherwise this method
  325. * will not return any data.
  326. */
  327. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0);
  328. /**
  329. * @brief Returns properties that contain information about the texture.
  330. */
  331. const TextureProperties& getProperties() const { return mProperties; }
  332. /**
  333. * @brief Retrieves a core implementation of a texture usable only from the
  334. * core thread.
  335. */
  336. SPtr<TextureCore> getCore() const;
  337. /**
  338. * @brief Returns a dummy 2x2 texture. Don't modify the returned texture.
  339. *
  340. * @note Thread safe.
  341. */
  342. static const HTexture& dummy();
  343. /************************************************************************/
  344. /* STATICS */
  345. /************************************************************************/
  346. /**
  347. * @brief Creates a new empty texture.
  348. *
  349. * @param texType Type of the texture.
  350. * @param width Width of the texture in pixels.
  351. * @param height Height of the texture in pixels.
  352. * @param depth Depth of the texture in pixels (Must be 1 for 2D textures).
  353. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  354. * @param format Format of the pixels.
  355. * @param usage Describes how we plan on using the texture in the pipeline.
  356. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  357. * converted back to linear space when sampled on GPU.
  358. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  359. */
  360. static HTexture create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  361. int numMips, PixelFormat format, int usage = TU_DEFAULT,
  362. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  363. /**
  364. * @brief Creates a new empty texture.
  365. *
  366. * @param texType Type of the texture.
  367. * @param width Width of the texture in pixels.
  368. * @param height Height of the texture in pixels.
  369. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  370. * @param format Format of the pixels.
  371. * @param usage Describes planned texture use.
  372. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  373. * converted back to linear space when sampled on GPU.
  374. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  375. */
  376. static HTexture create(TextureType texType, UINT32 width, UINT32 height, int numMips,
  377. PixelFormat format, int usage = TU_DEFAULT,
  378. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  379. /**
  380. * @copydoc create(TextureType, UINT32, UINT32, UINT32, int, PixelFormat, int, bool, UINT32)
  381. *
  382. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  383. */
  384. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  385. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  386. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  387. /**
  388. * @copydoc create(TextureType, UINT32, UINT32, int, PixelFormat, int, bool, UINT32)
  389. *
  390. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  391. */
  392. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  393. PixelFormat format, int usage = TU_DEFAULT, bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  394. protected:
  395. friend class TextureManager;
  396. Texture(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  397. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount);
  398. /**
  399. * @copydoc Resource::initialize
  400. */
  401. void initialize() override;
  402. /**
  403. * @copydoc CoreObject::createCore
  404. */
  405. SPtr<CoreObjectCore> createCore() const override;
  406. /**
  407. * @copydoc Resource::calculateSize
  408. */
  409. UINT32 calculateSize() const;
  410. /**
  411. * @brief Creates buffers used for caching of CPU texture data.
  412. *
  413. * @note Make sure to initialize all texture properties before calling this.
  414. */
  415. void createCPUBuffers();
  416. /**
  417. * @brief Updates the cached CPU buffers with new data.
  418. */
  419. void updateCPUBuffers(UINT32 subresourceIdx, const PixelData& data);
  420. protected:
  421. Vector<PixelDataPtr> mCPUSubresourceData;
  422. TextureProperties mProperties;
  423. /************************************************************************/
  424. /* SERIALIZATION */
  425. /************************************************************************/
  426. public:
  427. Texture(); // Serialization only
  428. friend class TextureRTTI;
  429. static RTTITypeBase* getRTTIStatic();
  430. virtual RTTITypeBase* getRTTI() const override;
  431. };
  432. }