CmTexture.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #ifndef _Texture_H__
  25. #define _Texture_H__
  26. #include "CmPrerequisites.h"
  27. #include "CmGpuResource.h"
  28. #include "CmHardwareBuffer.h"
  29. #include "CmPixelUtil.h"
  30. #include "CmTextureView.h"
  31. namespace CamelotFramework {
  32. /** \addtogroup Core
  33. * @{
  34. */
  35. /** \addtogroup Resources
  36. * @{
  37. */
  38. /** Enum identifying the texture usage
  39. */
  40. enum TextureUsage
  41. {
  42. /// @copydoc HardwareBuffer::Usage
  43. TU_STATIC = GBU_STATIC, // Optimal setting if texture is read by the GPU often, and very rarely written by CPU
  44. TU_DYNAMIC = GBU_DYNAMIC, // Optimal if the texture is updated by CPU often (e.g. every frame)
  45. TU_RENDERTARGET = 0x200, // Used for rendering by the GPU
  46. TU_DEPTHSTENCIL = 0x400,
  47. TU_DEFAULT = TU_STATIC
  48. };
  49. /** Enum identifying the texture type
  50. */
  51. enum TextureType
  52. {
  53. /// 1D texture, used in combination with 1D texture coordinates
  54. TEX_TYPE_1D = 1,
  55. /// 2D texture, used in combination with 2D texture coordinates (default)
  56. TEX_TYPE_2D = 2,
  57. /// 3D volume texture, used in combination with 3D texture coordinates
  58. TEX_TYPE_3D = 3,
  59. /// 3D cube map, used in combination with 3D texture coordinates
  60. TEX_TYPE_CUBE_MAP = 4
  61. };
  62. /** Enum identifying special mipmap numbers
  63. */
  64. enum TextureMipmap
  65. {
  66. /// Generate mipmaps up to 1x1
  67. MIP_UNLIMITED = 0x7FFFFFFF
  68. };
  69. /** Abstract class representing a Texture resource.
  70. @remarks
  71. The actual concrete subclass which will exist for a texture
  72. is dependent on the rendering system in use (Direct3D, OpenGL etc).
  73. This class represents the commonalities, and is the one 'used'
  74. by programmers even though the real implementation could be
  75. different in reality. Texture objects are created through
  76. the 'create' method of the TextureManager concrete subclass.
  77. */
  78. class CM_EXPORT Texture : public GpuResource
  79. {
  80. public:
  81. /** Gets the type of texture
  82. */
  83. virtual TextureType getTextureType(void) const { return mTextureType; }
  84. /** Gets the number of mipmaps to be used for this texture. This number excludes the top level
  85. * map (which is always assumed to be present).
  86. */
  87. virtual UINT32 getNumMipmaps(void) const {return mNumMipmaps;}
  88. /** Gets whether this texture will be set up so that on sampling it,
  89. hardware gamma correction is applied.
  90. */
  91. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  92. /** Get the level of multisample AA to be used if this texture is a
  93. rendertarget.
  94. */
  95. virtual UINT32 getFSAA() const { return mFSAA; }
  96. /** Get the multisample AA hint if this texture is a rendertarget.
  97. */
  98. virtual const String& getFSAAHint() const { return mFSAAHint; }
  99. /** Returns the height of the texture.
  100. */
  101. virtual UINT32 getHeight() const { return mHeight; }
  102. /** Returns the width of the texture.
  103. */
  104. virtual UINT32 getWidth() const { return mWidth; }
  105. /** Returns the depth of the texture (only applicable for 3D textures).
  106. */
  107. virtual UINT32 getDepth() const { return mDepth; }
  108. /** Returns the TextureUsage indentifier for this Texture
  109. */
  110. virtual int getUsage() const { return mUsage; }
  111. /** Returns the pixel format for the texture surface. */
  112. virtual PixelFormat getFormat() const { return mFormat; }
  113. /** Returns true if the texture has an alpha layer. */
  114. virtual bool hasAlpha() const;
  115. /** Return the number of faces this texture has. This will be 6 for a cubemap
  116. texture and 1 for a 1D, 2D or 3D one.
  117. */
  118. virtual UINT32 getNumFaces() const;
  119. /**
  120. * @brief Returns true if the texture can be bound to a shader.
  121. *
  122. * @note This is only false for some rare special cases. (e.g. AA render texture in DX9)
  123. */
  124. virtual bool isBindableAsShaderResource() const { return true; }
  125. /**
  126. * @copydoc GpuResource::writeSubresource
  127. */
  128. virtual void writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data);
  129. /**
  130. * @copydoc GpuResource::readSubresource
  131. */
  132. virtual void readSubresource(UINT32 subresourceIdx, GpuResourceData& data);
  133. /**
  134. * @brief Allocates a buffer you may use for storage when reading a subresource. You
  135. * need to allocate such a buffer if you are calling "readSubresource".
  136. *
  137. * @note This method is thread safe.
  138. */
  139. PixelDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  140. /**
  141. * @brief Maps a subresource index to an exact face and mip level. Subresource indexes
  142. * are used when reading or writing to the resource.
  143. *
  144. * @note Subresource index is only valid for the instance it was created on. You cannot use a subresource
  145. * index from a different texture and expect to get valid result. Modifying the resource so the number
  146. * of subresources changes, invalidates all subresource indexes.
  147. */
  148. void mapFromSubresourceIdx(UINT32 subresourceIdx, UINT32& face, UINT32& mip) const;
  149. /**
  150. * @brief Map a face and a mip level to a subresource index you can use for updating or reading
  151. * a specific sub-resource.
  152. *
  153. * @note Generated subresource index is only valid for the instance it was created on. Modifying the resource so the number
  154. * of subresources changes, invalidates all subresource indexes.
  155. */
  156. UINT32 mapToSubresourceIdx(UINT32 face, UINT32 mip) const;
  157. PixelData lock(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  158. void unlock();
  159. /** Copies the contents of this texture to
  160. another texture. */
  161. void copy(TexturePtr& target);
  162. /**
  163. * @brief Returns a dummy 2x2 texture. Don't modify the returned texture.
  164. */
  165. static HTexture dummy();
  166. /************************************************************************/
  167. /* TEXTURE VIEW */
  168. /************************************************************************/
  169. static TextureViewPtr requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  170. static void releaseView(TextureViewPtr view);
  171. protected:
  172. virtual TextureViewPtr createView();
  173. void clearBufferViews();
  174. struct TextureViewReference
  175. {
  176. TextureViewReference(TextureViewPtr _view)
  177. :view(_view), refCount(0)
  178. { }
  179. TextureViewPtr view;
  180. UINT32 refCount;
  181. };
  182. UnorderedMap<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction>::type mTextureViews;
  183. protected:
  184. friend class TextureManager;
  185. UINT32 mHeight;
  186. UINT32 mWidth;
  187. UINT32 mDepth;
  188. UINT32 mNumMipmaps;
  189. bool mHwGamma;
  190. UINT32 mFSAA;
  191. String mFSAAHint;
  192. TextureType mTextureType;
  193. PixelFormat mFormat;
  194. int mUsage; // Bit field, so this can't be TextureUsage
  195. Texture();
  196. /**
  197. * @brief Initializes the texture. This must be called right after the texture is constructed. Called by TextureManager
  198. * upon texture creation, so usually you don't want to call this manually.
  199. *
  200. * @note Initialization is not done immediately, and is instead just scheduled on the
  201. * core thread. Unless called from core thread, in which case it is initialized
  202. * right away.
  203. */
  204. void initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  205. PixelFormat format, int usage, bool hwGamma, UINT32 fsaa, const String& fsaaHint);
  206. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  207. virtual void unlockImpl() = 0;
  208. virtual void copyImpl(TexturePtr& target) = 0;
  209. /// @copydoc Resource::calculateSize
  210. UINT32 calculateSize(void) const;
  211. /************************************************************************/
  212. /* SERIALIZATION */
  213. /************************************************************************/
  214. public:
  215. friend class TextureRTTI;
  216. static RTTITypeBase* getRTTIStatic();
  217. virtual RTTITypeBase* getRTTI() const;
  218. /************************************************************************/
  219. /* STATICS */
  220. /************************************************************************/
  221. public:
  222. static HTexture create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  223. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  224. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  225. static HTexture create(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  226. PixelFormat format, int usage = TU_DEFAULT,
  227. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  228. };
  229. /** @} */
  230. }
  231. #endif