CmTexture.h 14 KB

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