CmTexture.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. /// mipmaps will be automatically generated for this texture
  49. TU_AUTOMIPMAP = 0x100,
  50. /// this texture will be a render target, i.e. used as a target for render to texture
  51. /// setting this flag will ignore all other texture usages except TU_AUTOMIPMAP
  52. TU_RENDERTARGET = 0x200,
  53. /// default to automatic mipmap generation static textures
  54. TU_DEFAULT = TU_AUTOMIPMAP | TU_STATIC_WRITE_ONLY
  55. };
  56. /** Enum identifying the texture type
  57. */
  58. enum TextureType
  59. {
  60. /// 1D texture, used in combination with 1D texture coordinates
  61. TEX_TYPE_1D = 1,
  62. /// 2D texture, used in combination with 2D texture coordinates (default)
  63. TEX_TYPE_2D = 2,
  64. /// 3D volume texture, used in combination with 3D texture coordinates
  65. TEX_TYPE_3D = 3,
  66. /// 3D cube map, used in combination with 3D texture coordinates
  67. TEX_TYPE_CUBE_MAP = 4
  68. };
  69. /** Enum identifying special mipmap numbers
  70. */
  71. enum TextureMipmap
  72. {
  73. /// Generate mipmaps up to 1x1
  74. MIP_UNLIMITED = 0x7FFFFFFF,
  75. /// Use TextureManager default
  76. MIP_DEFAULT = -1
  77. };
  78. /** Abstract class representing a Texture resource.
  79. @remarks
  80. The actual concrete subclass which will exist for a texture
  81. is dependent on the rendering system in use (Direct3D, OpenGL etc).
  82. This class represents the commonalities, and is the one 'used'
  83. by programmers even though the real implementation could be
  84. different in reality. Texture objects are created through
  85. the 'create' method of the TextureManager concrete subclass.
  86. */
  87. class CM_EXPORT Texture : public Resource
  88. {
  89. public:
  90. Texture();
  91. /** Sets the type of texture; can only be changed before load()
  92. */
  93. virtual void setTextureType(TextureType ttype ) { mTextureType = ttype; }
  94. /** Gets the type of texture
  95. */
  96. virtual TextureType getTextureType(void) const { return mTextureType; }
  97. /** Gets the number of mipmaps to be used for this texture.
  98. */
  99. virtual size_t getNumMipmaps(void) const {return mNumMipmaps;}
  100. /** Sets the number of mipmaps to be used for this texture.
  101. @note
  102. Must be set before calling any 'load' method.
  103. */
  104. virtual void setNumMipmaps(size_t num) {mNumRequestedMipmaps = mNumMipmaps = num;}
  105. /** Are mipmaps hardware generated?
  106. @remarks
  107. Will only be accurate after texture load, or createInternalResources
  108. */
  109. virtual bool getMipmapsHardwareGenerated(void) const { return mMipmapsHardwareGenerated; }
  110. /** Returns the gamma adjustment factor applied to this texture on loading.
  111. */
  112. virtual float getGamma(void) const { return mGamma; }
  113. /** Sets the gamma adjustment factor applied to this texture on loading the
  114. data.
  115. @note
  116. Must be called before any 'load' method. This gamma factor will
  117. be premultiplied in and may reduce the precision of your textures.
  118. You can use setHardwareGamma if supported to apply gamma on
  119. sampling the texture instead.
  120. */
  121. virtual void setGamma(float g) { mGamma = g; }
  122. /** Sets whether this texture will be set up so that on sampling it,
  123. hardware gamma correction is applied.
  124. @remarks
  125. 24-bit textures are often saved in gamma colour space; this preserves
  126. precision in the 'darks'. However, if you're performing blending on
  127. the sampled colours, you really want to be doing it in linear space.
  128. One way is to apply a gamma correction value on loading (see setGamma),
  129. but this means you lose precision in those dark colours. An alternative
  130. is to get the hardware to do the gamma correction when reading the
  131. texture and converting it to a floating point value for the rest of
  132. the pipeline. This option allows you to do that; it's only supported
  133. in relatively recent hardware (others will ignore it) but can improve
  134. the quality of colour reproduction.
  135. @note
  136. Must be called before any 'load' method since it may affect the
  137. construction of the underlying hardware resources.
  138. Also note this only useful on textures using 8-bit colour channels.
  139. */
  140. virtual void setHardwareGammaEnabled(bool enabled) { mHwGamma = enabled; }
  141. /** Gets whether this texture will be set up so that on sampling it,
  142. hardware gamma correction is applied.
  143. */
  144. virtual bool isHardwareGammaEnabled() const { return mHwGamma; }
  145. /** Set the level of multisample AA to be used if this texture is a
  146. rendertarget.
  147. @note This option will be ignored if TU_RENDERTARGET is not part of the
  148. usage options on this texture, or if the hardware does not support it.
  149. @param fsaa The number of samples
  150. @param fsaaHint Any hinting text (@see Root::createRenderWindow)
  151. */
  152. virtual void setFSAA(UINT32 fsaa, const String& fsaaHint) { mFSAA = fsaa; mFSAAHint = fsaaHint; }
  153. /** Get the level of multisample AA to be used if this texture is a
  154. rendertarget.
  155. */
  156. virtual UINT32 getFSAA() const { return mFSAA; }
  157. /** Get the multisample AA hint if this texture is a rendertarget.
  158. */
  159. virtual const String& getFSAAHint() const { return mFSAAHint; }
  160. /** Returns the height of the texture.
  161. */
  162. virtual size_t getHeight(void) const { return mHeight; }
  163. /** Returns the width of the texture.
  164. */
  165. virtual size_t getWidth(void) const { return mWidth; }
  166. /** Returns the depth of the texture (only applicable for 3D textures).
  167. */
  168. virtual size_t getDepth(void) const { return mDepth; }
  169. /** Returns the height of the original input texture (may differ due to hardware requirements).
  170. */
  171. virtual size_t getSrcHeight(void) const { return mSrcHeight; }
  172. /** Returns the width of the original input texture (may differ due to hardware requirements).
  173. */
  174. virtual size_t getSrcWidth(void) const { return mSrcWidth; }
  175. /** Returns the original depth of the input texture (only applicable for 3D textures).
  176. */
  177. virtual size_t getSrcDepth(void) const { return mSrcDepth; }
  178. /** Set the height of the texture; can only do this before load();
  179. */
  180. virtual void setHeight(size_t h) { mHeight = mSrcHeight = h; }
  181. /** Set the width of the texture; can only do this before load();
  182. */
  183. virtual void setWidth(size_t w) { mWidth = mSrcWidth = w; }
  184. /** Set the depth of the texture (only applicable for 3D textures);
  185. ; can only do this before load();
  186. */
  187. virtual void setDepth(size_t d) { mDepth = mSrcDepth = d; }
  188. /** Returns the TextureUsage indentifier for this Texture
  189. */
  190. virtual int getUsage() const
  191. {
  192. return mUsage;
  193. }
  194. /** Sets the TextureUsage indentifier for this Texture; only useful before load()
  195. @param u is a combination of TU_STATIC, TU_DYNAMIC, TU_WRITE_ONLY
  196. TU_AUTOMIPMAP and TU_RENDERTARGET (see TextureUsage enum). You are
  197. strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to
  198. update regularly, consider HBU_DYNAMIC_WRITE_ONLY.
  199. */
  200. virtual void setUsage(int u) { mUsage = u; }
  201. /** Creates the internal texture resources for this texture.
  202. @remarks
  203. This method creates the internal texture resources (pixel buffers,
  204. texture surfaces etc) required to begin using this texture. You do
  205. not need to call this method directly unless you are manually creating
  206. a texture, in which case something must call it, after having set the
  207. size and format of the texture (e.g. the ManualResourceLoader might
  208. be the best one to call it). If you are not defining a manual texture,
  209. or if you use one of the self-contained load...() methods, then it will be
  210. called for you.
  211. */
  212. virtual void createInternalResources(void);
  213. /** Frees internal texture resources for this texture.
  214. */
  215. virtual void freeInternalResources(void);
  216. /** Copies (and maybe scales to fit) the contents of this texture to
  217. another texture. */
  218. virtual void copyToTexture( TexturePtr& target );
  219. /** Returns the pixel format for the texture surface. */
  220. virtual PixelFormat getFormat() const
  221. {
  222. return mFormat;
  223. }
  224. /** Returns the desired pixel format for the texture surface. */
  225. virtual PixelFormat getDesiredFormat(void) const
  226. {
  227. return mDesiredFormat;
  228. }
  229. /** Returns the pixel format of the original input texture (may differ due to
  230. hardware requirements and pixel format convertion).
  231. */
  232. virtual PixelFormat getSrcFormat(void) const
  233. {
  234. return mSrcFormat;
  235. }
  236. /** Sets the pixel format for the texture surface; can only be set before load(). */
  237. virtual void setFormat(PixelFormat pf);
  238. /** Returns true if the texture has an alpha layer. */
  239. virtual bool hasAlpha(void) const;
  240. /** Sets desired bit depth for integer pixel format textures.
  241. @note
  242. Available values: 0, 16 and 32, where 0 (the default) means keep original format
  243. as it is. This value is number of bits for the pixel.
  244. */
  245. virtual void setDesiredIntegerBitDepth(UINT16 bits);
  246. /** gets desired bit depth for integer pixel format textures.
  247. */
  248. virtual UINT16 getDesiredIntegerBitDepth(void) const;
  249. /** Sets desired bit depth for float pixel format textures.
  250. @note
  251. Available values: 0, 16 and 32, where 0 (the default) means keep original format
  252. as it is. This value is number of bits for a channel of the pixel.
  253. */
  254. virtual void setDesiredFloatBitDepth(UINT16 bits);
  255. /** gets desired bit depth for float pixel format textures.
  256. */
  257. virtual UINT16 getDesiredFloatBitDepth(void) const;
  258. /** Sets desired bit depth for integer and float pixel format.
  259. */
  260. virtual void setDesiredBitDepths(UINT16 integerBits, UINT16 floatBits);
  261. /** Return the number of faces this texture has. This will be 6 for a cubemap
  262. texture and 1 for a 1D, 2D or 3D one.
  263. */
  264. virtual size_t getNumFaces() const;
  265. /** Return hardware pixel buffer for a surface. This buffer can then
  266. be used to copy data from and to a particular level of the texture.
  267. @param face Face number, in case of a cubemap texture. Must be 0
  268. for other types of textures.
  269. For cubemaps, this is one of
  270. +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
  271. @param mipmap Mipmap level. This goes from 0 for the first, largest
  272. mipmap level to getNumMipmaps()-1 for the smallest.
  273. @returns A shared pointer to a hardware pixel buffer
  274. @remarks The buffer is invalidated when the resource is unloaded or destroyed.
  275. Do not use it after the lifetime of the containing texture.
  276. */
  277. virtual HardwarePixelBufferPtr getBuffer(size_t face=0, size_t mipmap=0) = 0;
  278. /** Retrieve a platform or API-specific piece of information from this texture.
  279. This method of retrieving information should only be used if you know what you're doing.
  280. @param name The name of the attribute to retrieve
  281. @param pData Pointer to memory matching the type of data you want to retrieve.
  282. */
  283. virtual void getCustomAttribute(const String& name, void* pData) {}
  284. /**
  285. * @brief Retrieves the texture data from the GPU, loads it into system memory
  286. * and returns it in the form of TextureData for the specified.
  287. *
  288. * @return Texture data for the wanted face.
  289. */
  290. TextureDataPtr getTextureData(UINT32 face);
  291. /**
  292. * @brief Sets the texture data that will be used for initializing the texture.
  293. * You must call loadFromTextureData after setting the data for all faces.
  294. * Texture data array will be cleared after the texture is fully loaded.
  295. *
  296. * @param face The face index. Cubemaps have six faces in this order:
  297. * +X (0), -X (1), +Y (2), -Y (3), +Z (4), -Z (5)
  298. * @param textureData Texture data for the face.
  299. */
  300. void setTextureData(UINT32 face, TextureDataPtr textureData);
  301. protected:
  302. size_t mHeight;
  303. size_t mWidth;
  304. size_t mDepth;
  305. size_t mNumRequestedMipmaps;
  306. size_t mNumMipmaps;
  307. bool mMipmapsHardwareGenerated;
  308. float mGamma;
  309. bool mHwGamma;
  310. UINT32 mFSAA;
  311. String mFSAAHint;
  312. TextureType mTextureType;
  313. PixelFormat mFormat;
  314. int mUsage; // Bit field, so this can't be TextureUsage
  315. PixelFormat mSrcFormat;
  316. size_t mSrcWidth, mSrcHeight, mSrcDepth;
  317. PixelFormat mDesiredFormat;
  318. unsigned short mDesiredIntegerBitDepth;
  319. unsigned short mDesiredFloatBitDepth;
  320. bool mInternalResourcesCreated;
  321. public:
  322. vector<TextureDataPtr>::type mTextureData;
  323. protected:
  324. /// @copydoc Resource::calculateSize
  325. size_t calculateSize(void) const;
  326. /** Implementation of creating internal texture resources
  327. */
  328. virtual void createInternalResourcesImpl(void) = 0;
  329. /** Implementation of freeing internal texture resources
  330. */
  331. virtual void freeInternalResourcesImpl(void) = 0;
  332. /**
  333. * @brief Initializes the texture from texture data array that was previously populated using
  334. * setTextureData.
  335. *
  336. * @param textureData Array with texture data for each face of the texture.
  337. */
  338. virtual void initializeFromTextureData();
  339. /** Default implementation of unload which calls freeInternalResources */
  340. void unloadImpl(void);
  341. /************************************************************************/
  342. /* SERIALIZATION */
  343. /************************************************************************/
  344. public:
  345. friend class TextureRTTI;
  346. static RTTITypeBase* getRTTIStatic();
  347. virtual RTTITypeBase* getRTTI() const;
  348. };
  349. /** @} */
  350. }
  351. #endif