CmTexture.h 12 KB

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