CmTexture.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. namespace CamelotEngine {
  31. /** \addtogroup Core
  32. * @{
  33. */
  34. /** \addtogroup Resources
  35. * @{
  36. */
  37. /** Enum identifying the texture usage
  38. */
  39. enum TextureUsage
  40. {
  41. /// @copydoc HardwareBuffer::Usage
  42. TU_STATIC = HardwareBuffer::HBU_STATIC,
  43. TU_DYNAMIC = HardwareBuffer::HBU_DYNAMIC,
  44. TU_WRITE_ONLY = HardwareBuffer::HBU_WRITE_ONLY,
  45. TU_STATIC_WRITE_ONLY = HardwareBuffer::HBU_STATIC_WRITE_ONLY,
  46. TU_DYNAMIC_WRITE_ONLY = HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY,
  47. TU_DYNAMIC_WRITE_ONLY_DISCARDABLE = HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE,
  48. /// this texture will be a render target, i.e. used as a target for render to texture
  49. /// setting this flag will ignore all other texture usages
  50. TU_RENDERTARGET = 0x200,
  51. /// default to automatic mipmap generation static textures
  52. TU_DEFAULT = TU_STATIC_WRITE_ONLY
  53. };
  54. /** Enum identifying the texture type
  55. */
  56. enum TextureType
  57. {
  58. /// 1D texture, used in combination with 1D texture coordinates
  59. TEX_TYPE_1D = 1,
  60. /// 2D texture, used in combination with 2D texture coordinates (default)
  61. TEX_TYPE_2D = 2,
  62. /// 3D volume texture, used in combination with 3D texture coordinates
  63. TEX_TYPE_3D = 3,
  64. /// 3D cube map, used in combination with 3D texture coordinates
  65. TEX_TYPE_CUBE_MAP = 4,
  66. // Array of 2D textures,
  67. TEX_TYPE_2D_ARRAY = 5
  68. };
  69. /** Enum identifying special mipmap numbers
  70. */
  71. enum TextureMipmap
  72. {
  73. /// Generate mipmaps up to 1x1
  74. MIP_UNLIMITED = 0x7FFFFFFF
  75. };
  76. /** Abstract class representing a Texture resource.
  77. @remarks
  78. The actual concrete subclass which will exist for a texture
  79. is dependent on the rendering system in use (Direct3D, OpenGL etc).
  80. This class represents the commonalities, and is the one 'used'
  81. by programmers even though the real implementation could be
  82. different in reality. Texture objects are created through
  83. the 'create' method of the TextureManager concrete subclass.
  84. */
  85. class CM_EXPORT Texture : public Resource
  86. {
  87. public:
  88. /** Gets the type of texture
  89. */
  90. virtual TextureType getTextureType(void) const { return mTextureType; }
  91. /** Gets the number of mipmaps to be used for this texture.
  92. */
  93. virtual UINT32 getNumMipmaps(void) const {return mNumMipmaps;}
  94. /** Gets whether this texture will be set up so that on sampling it,
  95. hardware gamma correction is applied.
  96. */
  97. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  98. /** Get the level of multisample AA to be used if this texture is a
  99. rendertarget.
  100. */
  101. virtual UINT32 getFSAA() const { return mFSAA; }
  102. /** Get the multisample AA hint if this texture is a rendertarget.
  103. */
  104. virtual const String& getFSAAHint() const { return mFSAAHint; }
  105. /** Returns the height of the texture.
  106. */
  107. virtual UINT32 getHeight(void) const { return mHeight; }
  108. /** Returns the width of the texture.
  109. */
  110. virtual UINT32 getWidth(void) const { return mWidth; }
  111. /** Returns the depth of the texture (only applicable for 3D textures).
  112. */
  113. virtual UINT32 getDepth(void) const { return mDepth; }
  114. /** Returns the TextureUsage indentifier for this Texture
  115. */
  116. virtual int getUsage() const { return mUsage; }
  117. /** Returns the pixel format for the texture surface. */
  118. virtual PixelFormat getFormat() const { return mFormat; }
  119. /** Returns true if the texture has an alpha layer. */
  120. virtual bool hasAlpha(void) const;
  121. /** Return the number of faces this texture has. This will be 6 for a cubemap
  122. texture and 1 for a 1D, 2D or 3D one.
  123. */
  124. virtual UINT32 getNumFaces() const;
  125. /** Return hardware pixel buffer for a surface. This buffer can then
  126. be used to copy data from and to a particular level of the texture.
  127. @param face Face number, in case of a cubemap texture. Must be 0
  128. for other types of textures.
  129. For cubemaps, this is one of
  130. +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
  131. @param mipmap Mipmap level. This goes from 0 for the first, largest
  132. mipmap level to getNumMipmaps()-1 for the smallest.
  133. @returns A shared pointer to a hardware pixel buffer
  134. @remarks The buffer is invalidated when the resource is unloaded or destroyed.
  135. Do not use it after the lifetime of the containing texture.
  136. */
  137. virtual HardwarePixelBufferPtr getBuffer_internal(UINT32 face=0, UINT32 mipmap=0) = 0;
  138. /** Retrieve a platform or API-specific piece of information from this texture.
  139. This method of retrieving information should only be used if you know what you're doing.
  140. @param name The name of the attribute to retrieve
  141. @param pData Pointer to memory matching the type of data you want to retrieve.
  142. */
  143. virtual void getCustomAttribute_internal(const String& name, void* pData);
  144. /**
  145. * @brief Sets raw texture pixels for the specified mip level and texture face. Pixel format
  146. * must match the format of the texture.
  147. *
  148. * @note Not-async. This operation will block the current thread until the render thread
  149. * executes the command.
  150. */
  151. void setRawPixels(const PixelData& data, UINT32 face = 0, UINT32 mip = 0);
  152. /**
  153. * @brief Sets raw texture pixels for the specified mip level and texture face. Pixel format
  154. * must match the format of the texture. Returns immediately
  155. * but the texture won't be updated until the command
  156. * executes on the render thread.
  157. *
  158. * @see Texture::setRawPixels
  159. */
  160. void setRawPixels_async(const PixelData& data, UINT32 face = 0, UINT32 mip = 0);
  161. /**
  162. * @brief Internal version of Texture::setRawPixels. Only callable
  163. * from the render thread.
  164. *
  165. * @see Texture::setRawPixels
  166. */
  167. void setRawPixels_internal(const PixelData& data, UINT32 face = 0, UINT32 mip = 0);
  168. /**
  169. * @brief Gets raw pixels from the texture. This is a slow operation
  170. * as it will read data from the GPU. If the texture is compressed
  171. * the returned data will be contain compressed pixels as well.
  172. *
  173. * @note Not-async. This operation will block the current thread until the render thread
  174. * executes the command.
  175. */
  176. PixelDataPtr getRawPixels(UINT32 face = 0, UINT32 mip = 0);
  177. /**
  178. * @brief Async version of Texture::getRawPixels. Returns immediately
  179. * but you won't have access to the pixel data until the command
  180. * executes on the render thread.
  181. *
  182. * @see Texture::getRawPixels
  183. */
  184. AsyncOp getRawPixels_async(UINT32 face = 0, UINT32 mip = 0);
  185. /**
  186. * @brief Internal version of Texture::getRawPixels. Only callable
  187. * from the render thread.
  188. *
  189. * @see Texture::getRawPixels
  190. */
  191. void getRawPixels_internal(UINT32 face, UINT32 mip, AsyncOp& op);
  192. /** Copies (and maybe scales to fit) the contents of this texture to
  193. another texture. */
  194. virtual void copy_internal( TexturePtr& target );
  195. protected:
  196. friend class TextureManager;
  197. UINT32 mHeight;
  198. UINT32 mWidth;
  199. UINT32 mDepth;
  200. UINT32 mNumMipmaps;
  201. bool mHwGamma;
  202. UINT32 mFSAA;
  203. String mFSAAHint;
  204. TextureType mTextureType;
  205. PixelFormat mFormat;
  206. int mUsage; // Bit field, so this can't be TextureUsage
  207. Texture();
  208. /**
  209. * @brief Initializes the texture. This must be called right after the texture is constructed. Called by TextureManager
  210. * upon texture creation, so usually you don't want to call this manually.
  211. *
  212. * @note Initialization is not done immediately, and is instead just scheduled on the render thread.
  213. */
  214. void initialize(TextureType textureType, UINT32 width, UINT32 height, UINT32 depth, UINT32 numMipmaps,
  215. PixelFormat format, int usage, bool hwGamma, UINT32 fsaa, const String& fsaaHint);
  216. /**
  217. * @brief Performs GpuProgram initialization. Only callable from the render thread.
  218. */
  219. virtual void initialize_internal() = 0;
  220. /// @copydoc Resource::calculateSize
  221. UINT32 calculateSize(void) const;
  222. /** Creates the internal texture resources for this texture.
  223. @remarks
  224. This method creates the internal texture resources (pixel buffers,
  225. texture surfaces etc) required to begin using this texture. You do
  226. not need to call this method directly unless you are manually creating
  227. a texture, in which case something must call it, after having set the
  228. size and format of the texture (e.g. the ManualResourceLoader might
  229. be the best one to call it). If you are not defining a manual texture,
  230. or if you use one of the self-contained load...() methods, then it will be
  231. called for you.
  232. */
  233. virtual void createInternalResources(void);
  234. /** Frees internal texture resources for this texture.
  235. */
  236. virtual void freeInternalResources(void);
  237. /** Implementation of creating internal texture resources
  238. */
  239. virtual void createInternalResourcesImpl(void) = 0;
  240. /** Implementation of freeing internal texture resources
  241. */
  242. virtual void freeInternalResourcesImpl(void) = 0;
  243. /** Default implementation of unload which calls freeInternalResources */
  244. void unloadImpl(void);
  245. void throwIfNotRenderThread() const;
  246. /************************************************************************/
  247. /* SERIALIZATION */
  248. /************************************************************************/
  249. public:
  250. friend class TextureRTTI;
  251. static RTTITypeBase* getRTTIStatic();
  252. virtual RTTITypeBase* getRTTI() const;
  253. /************************************************************************/
  254. /* STATICS */
  255. /************************************************************************/
  256. public:
  257. static TexturePtr create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  258. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  259. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  260. static TexturePtr create(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  261. PixelFormat format, int usage = TU_DEFAULT,
  262. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  263. };
  264. /** @} */
  265. }
  266. #endif