CmTexture.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 "CmResource.h"
  28. #include "CmHardwareBuffer.h"
  29. #include "CmPixelUtil.h"
  30. #include "CmTextureView.h"
  31. namespace CamelotEngine {
  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 Resource
  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.
  85. */
  86. virtual UINT32 getNumMipmaps(void) const {return mNumMipmaps;}
  87. /** Gets whether this texture will be set up so that on sampling it,
  88. hardware gamma correction is applied.
  89. */
  90. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  91. /** Get the level of multisample AA to be used if this texture is a
  92. rendertarget.
  93. */
  94. virtual UINT32 getFSAA() const { return mFSAA; }
  95. /** Get the multisample AA hint if this texture is a rendertarget.
  96. */
  97. virtual const String& getFSAAHint() const { return mFSAAHint; }
  98. /** Returns the height of the texture.
  99. */
  100. virtual UINT32 getHeight(void) const { return mHeight; }
  101. /** Returns the width of the texture.
  102. */
  103. virtual UINT32 getWidth(void) const { return mWidth; }
  104. /** Returns the depth of the texture (only applicable for 3D textures).
  105. */
  106. virtual UINT32 getDepth(void) const { return mDepth; }
  107. /** Returns the TextureUsage indentifier for this Texture
  108. */
  109. virtual int getUsage() const { return mUsage; }
  110. /** Returns the pixel format for the texture surface. */
  111. virtual PixelFormat getFormat() const { return mFormat; }
  112. /** Returns true if the texture has an alpha layer. */
  113. virtual bool hasAlpha(void) const;
  114. /** Return the number of faces this texture has. This will be 6 for a cubemap
  115. texture and 1 for a 1D, 2D or 3D one.
  116. */
  117. virtual UINT32 getNumFaces() const;
  118. /**
  119. * @brief Sets raw texture pixels for the specified mip level and texture face. Pixel format
  120. * must match the format of the texture.
  121. *
  122. * @note Not-async. This operation will block the current thread until the render thread
  123. * executes the command.
  124. */
  125. void setRawPixels(const PixelData& data, UINT32 face = 0, UINT32 mip = 0);
  126. /**
  127. * @brief Sets raw texture pixels for the specified mip level and texture face. Pixel format
  128. * must match the format of the texture. Returns immediately
  129. * but the texture won't be updated until the command
  130. * executes on the render thread.
  131. *
  132. * @see Texture::setRawPixels
  133. */
  134. void setRawPixels_async(const PixelData& data, UINT32 face = 0, UINT32 mip = 0);
  135. /**
  136. * @brief Internal version of Texture::setRawPixels. Only callable
  137. * from the render thread.
  138. *
  139. * @see Texture::setRawPixels
  140. */
  141. virtual void setRawPixels_internal(const PixelData& data, UINT32 face = 0, UINT32 mip = 0);
  142. /**
  143. * @brief Gets raw pixels from the texture. This is a slow operation
  144. * as it will read data from the GPU. If the texture is compressed
  145. * the returned data will be contain compressed pixels as well.
  146. *
  147. * @note Not-async. This operation will block the current thread until the render thread
  148. * executes the command.
  149. */
  150. PixelDataPtr getRawPixels(UINT32 face = 0, UINT32 mip = 0);
  151. /**
  152. * @brief Async version of Texture::getRawPixels. Returns immediately
  153. * but you won't have access to the pixel data until the command
  154. * executes on the render thread.
  155. *
  156. * @see Texture::getRawPixels
  157. */
  158. AsyncOp getRawPixels_async(UINT32 face = 0, UINT32 mip = 0);
  159. /**
  160. * @brief Internal version of Texture::getRawPixels. Only callable
  161. * from the render thread.
  162. *
  163. * @see Texture::getRawPixels
  164. */
  165. virtual void getRawPixels_internal(UINT32 face, UINT32 mip, AsyncOp& op);
  166. PixelData lock(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  167. void unlock();
  168. /** Copies the contents of this texture to
  169. another texture. */
  170. void copy(TexturePtr& target);
  171. /************************************************************************/
  172. /* TEXTURE VIEW */
  173. /************************************************************************/
  174. static TextureViewPtr requestView(TexturePtr texture, UINT32 mostDetailMip, UINT32 numMips, UINT32 firstArraySlice, UINT32 numArraySlices, GpuViewUsage usage);
  175. static void releaseView(TextureViewPtr view);
  176. protected:
  177. virtual TextureViewPtr createView();
  178. void clearBufferViews();
  179. struct TextureViewReference
  180. {
  181. TextureViewReference(TextureViewPtr _view)
  182. :view(_view), refCount(0)
  183. { }
  184. TextureViewPtr view;
  185. UINT32 refCount;
  186. };
  187. std::unordered_map<TEXTURE_VIEW_DESC, TextureViewReference*, TextureView::HashFunction, TextureView::EqualFunction> mTextureViews;
  188. protected:
  189. friend class TextureManager;
  190. UINT32 mHeight;
  191. UINT32 mWidth;
  192. UINT32 mDepth;
  193. UINT32 mNumMipmaps;
  194. bool mHwGamma;
  195. UINT32 mFSAA;
  196. String mFSAAHint;
  197. TextureType mTextureType;
  198. PixelFormat mFormat;
  199. int mUsage; // Bit field, so this can't be TextureUsage
  200. Texture();
  201. /**
  202. * @brief Initializes the texture. This must be called right after the texture is constructed. Called by TextureManager
  203. * upon texture creation, so usually you don't want to call this manually.
  204. *
  205. * @note Initialization is not done immediately, and is instead just scheduled on the
  206. * render thread. Unless called from render thread, in which case it is initialized
  207. * right away.
  208. */
  209. void initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  210. PixelFormat format, int usage, bool hwGamma, UINT32 fsaa, const String& fsaaHint);
  211. virtual PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) = 0;
  212. virtual void unlockImpl() = 0;
  213. virtual void copyImpl(TexturePtr& target) = 0;
  214. /// @copydoc Resource::calculateSize
  215. UINT32 calculateSize(void) const;
  216. void throwIfNotRenderThread() const;
  217. /************************************************************************/
  218. /* SERIALIZATION */
  219. /************************************************************************/
  220. public:
  221. friend class TextureRTTI;
  222. static RTTITypeBase* getRTTIStatic();
  223. virtual RTTITypeBase* getRTTI() const;
  224. /************************************************************************/
  225. /* STATICS */
  226. /************************************************************************/
  227. public:
  228. static TextureHandle create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  229. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  230. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  231. static TextureHandle create(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  232. PixelFormat format, int usage = TU_DEFAULT,
  233. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  234. };
  235. /** @} */
  236. }
  237. #endif