BsTexture.h 14 KB

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