BsTexture.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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, const PixelDataPtr& initData);
  145. virtual ~TextureCore() {}
  146. /**
  147. * @copydoc CoreObjectCore::initialize
  148. */
  149. virtual void initialize() override;
  150. /**
  151. * @brief Updates a part of the texture with the provided data.
  152. *
  153. * @param subresourceIdx Index of the subresource to update, if the texture has more than one.
  154. * @param data Data to update the texture with.
  155. * @param discardEntireBuffer When true the existing contents of the resource you are updating will be discarded. This can make the
  156. * operation faster. Resources with certain buffer types might require this flag to be in a specific state
  157. * otherwise the operation will fail.
  158. */
  159. virtual void writeSubresource(UINT32 subresourceIdx, const PixelData& data, bool discardEntireBuffer);
  160. /**
  161. * @brief Reads a part of the current resource into the provided "data" parameter.
  162. * Data buffer needs to be pre-allocated.
  163. *
  164. * @param subresourceIdx Index of the subresource to update, if the texture has more than one.
  165. * @param data Buffer that will receive the data. Should be allocated with "allocateSubresourceBuffer"
  166. * to ensure it is of valid type and size.
  167. */
  168. virtual void readSubresource(UINT32 subresourceIdx, PixelData& data);
  169. /**
  170. * @brief Locks the buffer for reading or writing.
  171. *
  172. * @param options Options for controlling what you may do with the locked data.
  173. * @param mipLevel (optional) Mipmap level to lock.
  174. * @param face (optional) Texture face to lock.
  175. *
  176. * @return Pointer to the buffer data. Only valid until you call unlock.
  177. *
  178. * @note If you are just reading or writing one block of data use
  179. * readData/writeData methods as they can be much faster in certain situations.
  180. */
  181. PixelData lock(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  182. /**
  183. * @brief Unlocks a previously locked buffer. After the buffer is unlocked,
  184. * any data returned by lock becomes invalid.
  185. */
  186. void unlock();
  187. /**
  188. * @brief Copies the contents a subresource in this texture to another texture.
  189. * Texture format and size of the subresource must match.
  190. *
  191. * You are allowed to copy from a multisampled to non-multisampled
  192. * surface, which will resolve the multisampled surface before copying.
  193. *
  194. * @param srcSubresourceIdx Index of the subresource to copy from.
  195. * @param destSubresourceIdx Index of the subresource to copy to.
  196. * @param target Texture that contains the destination subresource.
  197. */
  198. void copy(UINT32 srcSubresourceIdx, UINT32 destSubresourceIdx, const SPtr<TextureCore>& target);
  199. /**
  200. * @brief Reads data from the texture buffer into the provided buffer.
  201. *
  202. * @param dest Previously allocated buffer to read data into.
  203. * @param mipLevel (optional) Mipmap level to read from.
  204. * @param face (optional) Texture face to read from.
  205. */
  206. virtual void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  207. /**
  208. * @brief Writes data from the provided buffer into the texture buffer.
  209. *
  210. * @param dest Buffer to retrieve the data from.
  211. * @param mipLevel (optional) Mipmap level to write into.
  212. * @param face (optional) Texture face to write into.
  213. * @param discardWholeBuffer (optional) If true any existing texture data will be discard. This can
  214. * improve performance of the write operation.
  215. */
  216. virtual void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) = 0;
  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. * @brief Returns properties that contain information about the texture.
  226. */
  227. const TextureProperties& getProperties() const { return mProperties; }
  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. * @note Core thread only.
  237. */
  238. static TextureViewPtr requestView(const SPtr<TextureCore>& texture, UINT32 mostDetailMip, UINT32 numMips,
  239. UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  240. /**
  241. * @brief Releases the view. View won't actually get destroyed until all references to it are released.
  242. *
  243. * @note Core thread only.
  244. */
  245. static void releaseView(const TextureViewPtr& view);
  246. protected:
  247. /**
  248. * @copydoc lock
  249. */
  250. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  251. /**
  252. * @copydoc unlock
  253. */
  254. virtual void unlockImpl() = 0;
  255. /**
  256. * @copydoc copy
  257. */
  258. virtual void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target) = 0;
  259. /************************************************************************/
  260. /* TEXTURE VIEW */
  261. /************************************************************************/
  262. /**
  263. * @brief Creates a new empty/undefined texture view.
  264. */
  265. virtual TextureViewPtr createView(const SPtr<TextureCore>& texture, const TEXTURE_VIEW_DESC& desc);
  266. /**
  267. * @brief Releases all internal texture view references. Views won't get destroyed if there are external references still held.
  268. */
  269. void clearBufferViews();
  270. /**
  271. * @brief Holds a single texture view with a usage reference count.
  272. */
  273. struct TextureViewReference
  274. {
  275. TextureViewReference(TextureViewPtr _view)
  276. :view(_view), refCount(0)
  277. { }
  278. TextureViewPtr view;
  279. UINT32 refCount;
  280. };
  281. UnorderedMap<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction> mTextureViews;
  282. TextureProperties mProperties;
  283. PixelDataPtr mInitData;
  284. };
  285. /**
  286. * @brief Abstract class representing a texture. Specific render systems have their
  287. * own Texture implementations. Internally represented as one or more surfaces
  288. * with pixels in a certain number of dimensions, backed by a hardware buffer.
  289. *
  290. * @note Sim thread.
  291. */
  292. class BS_CORE_EXPORT Texture : public Resource
  293. {
  294. public:
  295. /**
  296. * @brief Updates the texture with new data. The actual write will be queued for later execution on the core thread.
  297. * Provided data buffer will be locked until the operation completes.
  298. *
  299. * @param accessor Accessor to queue the operation on.
  300. *
  301. * @return Async operation object you can use to track operation completion.
  302. *
  303. * @see TextureCore::writeSubresource
  304. */
  305. AsyncOp writeSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const PixelDataPtr& data, bool discardEntireBuffer);
  306. /**
  307. * @brief Reads internal texture data to the provided previously allocated buffer. The read is queued for execution
  308. * on the core thread and not executed immediately. Provided data buffer will be locked until the
  309. * operation completes.
  310. *
  311. * @param accessor Accessor to queue the operation on.
  312. *
  313. * @return Async operation object you can use to track operation completion.
  314. *
  315. * @see TextureCore::readSubresource
  316. */
  317. AsyncOp readSubresource(CoreAccessor& accessor, UINT32 subresourceIdx, const PixelDataPtr& data);
  318. /**
  319. * @brief Reads data from the cached system memory texture buffer into the provided buffer.
  320. *
  321. * @param dest Previously allocated buffer to read data into.
  322. * @param mipLevel (optional) Mipmap level to read from.
  323. * @param face (optional) Texture face to read from.
  324. *
  325. * @note The data read is the cached texture data. Any data written to the texture from the GPU
  326. * or core thread will not be reflected in this data. Use "readSubresource" if you require
  327. * those changes.
  328. *
  329. * The texture must have been created with TU_CPUCACHED usage otherwise this method
  330. * will not return any data.
  331. */
  332. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0);
  333. /**
  334. * @brief Returns properties that contain information about the texture.
  335. */
  336. const TextureProperties& getProperties() const { return mProperties; }
  337. /**
  338. * @brief Retrieves a core implementation of a texture usable only from the
  339. * core thread.
  340. */
  341. SPtr<TextureCore> getCore() const;
  342. /************************************************************************/
  343. /* STATICS */
  344. /************************************************************************/
  345. /**
  346. * @brief Creates a new empty texture.
  347. *
  348. * @param texType Type of the texture.
  349. * @param width Width of the texture in pixels.
  350. * @param height Height of the texture in pixels.
  351. * @param depth Depth of the texture in pixels (Must be 1 for 2D textures).
  352. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  353. * @param format Format of the pixels.
  354. * @param usage Describes how we plan on using the texture in the pipeline.
  355. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  356. * converted back to linear space when sampled on GPU.
  357. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  358. */
  359. static HTexture create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  360. int numMips, PixelFormat format, int usage = TU_DEFAULT,
  361. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  362. /**
  363. * @brief Creates a new empty texture.
  364. *
  365. * @param texType Type of the texture.
  366. * @param width Width of the texture in pixels.
  367. * @param height Height of the texture in pixels.
  368. * @param numMips Number of mip-maps the texture has. This number excludes the full resolution map.
  369. * @param format Format of the pixels.
  370. * @param usage Describes planned texture use.
  371. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  372. * converted back to linear space when sampled on GPU.
  373. * @param multisampleCount If higher than 1, texture containing multiple samples per pixel is created.
  374. */
  375. static HTexture create(TextureType texType, UINT32 width, UINT32 height, int numMips,
  376. PixelFormat format, int usage = TU_DEFAULT,
  377. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  378. /**
  379. * @brief Creates a new 2D or 3D texture initialized using the provided pixel data. Texture will not have any mipmaps.
  380. *
  381. * @param pixelData Data to initialize the texture width.
  382. * @param usage Describes planned texture use.
  383. * @param hwGammaCorrection If true the texture data is assumed to have been gamma corrected and will be
  384. * converted back to linear space when sampled on GPU.
  385. */
  386. static HTexture create(const PixelDataPtr& pixelData, int usage = TU_DEFAULT, bool hwGammaCorrection = false);
  387. /**
  388. * @copydoc create(TextureType, UINT32, 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, UINT32 depth,
  393. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  394. bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  395. /**
  396. * @copydoc create(TextureType, UINT32, UINT32, int, PixelFormat, int, bool, UINT32)
  397. *
  398. * @note Internal method. Creates a texture pointer without a handle. Use "create" for normal usage.
  399. */
  400. static TexturePtr _createPtr(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  401. PixelFormat format, int usage = TU_DEFAULT, bool hwGammaCorrection = false, UINT32 multisampleCount = 0);
  402. protected:
  403. friend class TextureManager;
  404. Texture(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  405. PixelFormat format, int usage, bool hwGamma, UINT32 multisampleCount);
  406. Texture(const PixelDataPtr& pixelData, int usage, bool hwGamma);
  407. /**
  408. * @copydoc Resource::initialize
  409. */
  410. void initialize() override;
  411. /**
  412. * @copydoc CoreObject::createCore
  413. */
  414. SPtr<CoreObjectCore> createCore() const override;
  415. /**
  416. * @copydoc Resource::calculateSize
  417. */
  418. UINT32 calculateSize() const;
  419. /**
  420. * @brief Creates buffers used for caching of CPU texture data.
  421. *
  422. * @note Make sure to initialize all texture properties before calling this.
  423. */
  424. void createCPUBuffers();
  425. /**
  426. * @brief Updates the cached CPU buffers with new data.
  427. */
  428. void updateCPUBuffers(UINT32 subresourceIdx, const PixelData& data);
  429. protected:
  430. Vector<PixelDataPtr> mCPUSubresourceData;
  431. TextureProperties mProperties;
  432. mutable PixelDataPtr mInitData;
  433. /************************************************************************/
  434. /* SERIALIZATION */
  435. /************************************************************************/
  436. public:
  437. Texture(); // Serialization only
  438. friend class TextureRTTI;
  439. static RTTITypeBase* getRTTIStatic();
  440. virtual RTTITypeBase* getRTTI() const override;
  441. };
  442. }