CmTexture.h 15 KB

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