CmTexture.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #pragma once
  25. #include "CmPrerequisites.h"
  26. #include "CmGpuResource.h"
  27. #include "CmHardwareBuffer.h"
  28. #include "CmPixelUtil.h"
  29. #include "CmTextureView.h"
  30. namespace CamelotFramework
  31. {
  32. /** Enum identifying the texture usage
  33. */
  34. enum TextureUsage
  35. {
  36. /// @copydoc HardwareBuffer::Usage
  37. TU_STATIC = GBU_STATIC, // Optimal setting if texture is read by the GPU often, and very rarely written by CPU
  38. TU_DYNAMIC = GBU_DYNAMIC, // Optimal if the texture is updated by CPU often (e.g. every frame)
  39. TU_RENDERTARGET = 0x200, // Used for rendering by the GPU
  40. TU_DEPTHSTENCIL = 0x400,
  41. TU_DEFAULT = TU_STATIC
  42. };
  43. /** Enum identifying the texture type
  44. */
  45. enum TextureType
  46. {
  47. /// 1D texture, used in combination with 1D texture coordinates
  48. TEX_TYPE_1D = 1,
  49. /// 2D texture, used in combination with 2D texture coordinates (default)
  50. TEX_TYPE_2D = 2,
  51. /// 3D volume texture, used in combination with 3D texture coordinates
  52. TEX_TYPE_3D = 3,
  53. /// 3D cube map, used in combination with 3D texture coordinates
  54. TEX_TYPE_CUBE_MAP = 4
  55. };
  56. /** Enum identifying special mipmap numbers
  57. */
  58. enum TextureMipmap
  59. {
  60. /// Generate mipmaps up to 1x1
  61. MIP_UNLIMITED = 0x7FFFFFFF
  62. };
  63. /** Abstract class representing a Texture resource.
  64. @remarks
  65. The actual concrete subclass which will exist for a texture
  66. is dependent on the rendering system in use (Direct3D, OpenGL etc).
  67. This class represents the commonalities, and is the one 'used'
  68. by programmers even though the real implementation could be
  69. different in reality. Texture objects are created through
  70. the 'create' method of the TextureManager concrete subclass.
  71. */
  72. class CM_EXPORT Texture : public GpuResource
  73. {
  74. public:
  75. /** Gets the type of texture
  76. */
  77. virtual TextureType getTextureType(void) const { return mTextureType; }
  78. /** Gets the number of mipmaps to be used for this texture. This number excludes the top level
  79. * map (which is always assumed to be present).
  80. */
  81. virtual UINT32 getNumMipmaps(void) const {return mNumMipmaps;}
  82. /** Gets whether this texture will be set up so that on sampling it,
  83. hardware gamma correction is applied.
  84. */
  85. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  86. /** Get the level of multisample AA to be used if this texture is a
  87. rendertarget.
  88. */
  89. virtual UINT32 getFSAA() const { return mFSAA; }
  90. /** Get the multisample AA hint if this texture is a rendertarget.
  91. */
  92. virtual const String& getFSAAHint() const { return mFSAAHint; }
  93. /** Returns the height of the texture.
  94. */
  95. virtual UINT32 getHeight() const { return mHeight; }
  96. /** Returns the width of the texture.
  97. */
  98. virtual UINT32 getWidth() const { return mWidth; }
  99. /** Returns the depth of the texture (only applicable for 3D textures).
  100. */
  101. virtual UINT32 getDepth() const { return mDepth; }
  102. /** Returns the TextureUsage indentifier for this Texture
  103. */
  104. virtual int getUsage() const { return mUsage; }
  105. /** Returns the pixel format for the texture surface. */
  106. virtual PixelFormat getFormat() const { return mFormat; }
  107. /** Returns true if the texture has an alpha layer. */
  108. virtual bool hasAlpha() const;
  109. /** Return the number of faces this texture has. This will be 6 for a cubemap
  110. texture and 1 for a 1D, 2D or 3D one.
  111. */
  112. virtual UINT32 getNumFaces() const;
  113. /**
  114. * @brief Returns true if the texture can be bound to a shader.
  115. *
  116. * @note This is only false for some rare special cases. (e.g. AA render texture in DX9)
  117. */
  118. virtual bool isBindableAsShaderResource() const { return true; }
  119. /**
  120. * @copydoc GpuResource::writeSubresource
  121. */
  122. virtual void writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  123. /**
  124. * @copydoc GpuResource::readSubresource
  125. */
  126. virtual void readSubresource(UINT32 subresourceIdx, GpuResourceData& data);
  127. /**
  128. * @brief Allocates a buffer you may use for storage when reading a subresource. You
  129. * need to allocate such a buffer if you are calling "readSubresource".
  130. *
  131. * @note This method is thread safe.
  132. */
  133. PixelDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  134. /**
  135. * @brief Maps a subresource index to an exact face and mip level. Subresource indexes
  136. * are used when reading or writing to the resource.
  137. *
  138. * @note Subresource index is only valid for the instance it was created on. You cannot use a subresource
  139. * index from a different texture and expect to get valid result. Modifying the resource so the number
  140. * of subresources changes, invalidates all subresource indexes.
  141. */
  142. void mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const;
  143. /**
  144. * @brief Map a face and a mip level to a subresource index you can use for updating or reading
  145. * a specific sub-resource.
  146. *
  147. * @note Generated subresource index is only valid for the instance it was created on. Modifying the resource so the number
  148. * of subresources changes, invalidates all subresource indexes.
  149. */
  150. UINT32 mapToSubresourceIdx(UINT32 face, UINT32 mip) const;
  151. /**
  152. * @brief Locks the buffer for reading or writing.
  153. *
  154. * @param options Options for controlling the operation.
  155. * @param mipLevel (optional) the mip level.
  156. * @param face (optional) the face.
  157. *
  158. * @return Pointer to the buffer data. Only valid until you call unlock.
  159. *
  160. * @note If you are just reading or writing one block of data use
  161. * readData/writeData methods as they can be must faster in certain situations.
  162. */
  163. PixelData lock(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  164. /**
  165. * @brief Unlocks a previously locked buffer. After the buffer is unlocked,
  166. * any data returned by lock becomes invalid.
  167. */
  168. void unlock();
  169. /**
  170. * @brief Copies the contents of this texture to another texture. Texture format
  171. * and size must match.
  172. */
  173. void copy(TexturePtr& target);
  174. /**
  175. * @brief Reads data from the texture buffer into the provided buffer.
  176. */
  177. virtual void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  178. /**
  179. * @brief Writes data from the provided buffer into the texture buffer.
  180. */
  181. virtual void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) = 0;
  182. /**
  183. * @brief Returns a dummy 2x2 texture. Don't modify the returned texture.
  184. */
  185. static const HTexture& dummy();
  186. /************************************************************************/
  187. /* TEXTURE VIEW */
  188. /************************************************************************/
  189. static TextureViewPtr requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  190. static void releaseView(TextureViewPtr view);
  191. protected:
  192. virtual TextureViewPtr createView();
  193. void clearBufferViews();
  194. struct TextureViewReference
  195. {
  196. TextureViewReference(TextureViewPtr _view)
  197. :view(_view), refCount(0)
  198. { }
  199. TextureViewPtr view;
  200. UINT32 refCount;
  201. };
  202. UnorderedMap<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction>::type mTextureViews;
  203. protected:
  204. friend class TextureManager;
  205. UINT32 mHeight; // Immutable
  206. UINT32 mWidth; // Immutable
  207. UINT32 mDepth; // Immutable
  208. UINT32 mNumMipmaps; // Immutable
  209. bool mHwGamma; // Immutable
  210. UINT32 mFSAA; // Immutable
  211. String mFSAAHint; // Immutable
  212. TextureType mTextureType; // Immutable
  213. PixelFormat mFormat; // Immutable
  214. int mUsage; // Immutable
  215. Texture();
  216. /**
  217. * @brief Initializes the texture. This must be called right after the texture is constructed. Called by TextureManager
  218. * upon texture creation, so usually you don't want to call this manually.
  219. *
  220. * @note Initialization is not done immediately, and is instead just scheduled on the
  221. * core thread. Unless called from core thread, in which case it is initialized
  222. * right away.
  223. */
  224. void initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  225. PixelFormat format, int usage, bool hwGamma, UINT32 fsaa, const String& fsaaHint);
  226. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  227. virtual void unlockImpl() = 0;
  228. virtual void copyImpl(TexturePtr& target) = 0;
  229. /// @copydoc Resource::calculateSize
  230. UINT32 calculateSize(void) const;
  231. /************************************************************************/
  232. /* SERIALIZATION */
  233. /************************************************************************/
  234. public:
  235. friend class TextureRTTI;
  236. static RTTITypeBase* getRTTIStatic();
  237. virtual RTTITypeBase* getRTTI() const;
  238. /************************************************************************/
  239. /* STATICS */
  240. /************************************************************************/
  241. public:
  242. static HTexture create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  243. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  244. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  245. static HTexture create(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  246. PixelFormat format, int usage = TU_DEFAULT,
  247. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  248. };
  249. }