CmTextureManager.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 _TextureManager_H__
  25. #define _TextureManager_H__
  26. #include "CmPrerequisites.h"
  27. #include "CmTexture.h"
  28. #include "CmModule.h"
  29. namespace CamelotEngine {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Resources
  34. * @{
  35. */
  36. /** Class for loading & managing textures.
  37. @remarks
  38. Note that this class is abstract - the particular
  39. RenderSystem that is in use at the time will create
  40. a concrete subclass of this. Note that the concrete
  41. class will be available via the abstract singleton
  42. obtained from TextureManager::getSingleton(), but
  43. you should not assume that it is available until you
  44. have a) initialised Ogre (after selecting a RenderSystem
  45. and calling initialise from the Root object), and b)
  46. created at least one window - this may be done at the
  47. same time as part a if you allow Ogre to autocreate one.
  48. */
  49. class CM_EXPORT TextureManager : public Module<TextureManager>
  50. {
  51. protected:
  52. virtual Texture* createImpl() = 0;
  53. public:
  54. TextureManager(void);
  55. virtual ~TextureManager();
  56. /** Create a manual texture with specified width, height and depth (not loaded from a file).
  57. @param
  58. name The name to give the resulting texture
  59. @param
  60. group The name of the resource group to assign the texture to
  61. @param
  62. texType The type of texture to load/create, defaults to normal 2D textures
  63. @param
  64. width, height, depth The dimensions of the texture
  65. @param
  66. numMipmaps The number of pre-filtered mipmaps to generate. If left to MIP_DEFAULT then
  67. the TextureManager's default number of mipmaps will be used (see setDefaultNumMipmaps())
  68. If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
  69. level, 1x1x1.
  70. @param
  71. format The internal format you wish to request; the manager reserves
  72. the right to create a different format if the one you select is
  73. not available in this context.
  74. @param
  75. usage The kind of usage this texture is intended for. It
  76. is a combination of TU_STATIC, TU_DYNAMIC, TU_WRITE_ONLY,
  77. TU_AUTOMIPMAP and TU_RENDERTARGET (see TextureUsage enum). You are
  78. strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to
  79. update regularly, consider HBU_DYNAMIC_WRITE_ONLY.
  80. @param
  81. loader If you intend the contents of the manual texture to be
  82. regularly updated, to the extent that you don't need to recover
  83. the contents if the texture content is lost somehow, you can leave
  84. this parameter as 0. However, if you intend to populate the
  85. texture only once, then you should implement ManualResourceLoader
  86. and pass a pointer to it in this parameter; this means that if the
  87. manual texture ever needs to be reloaded, the ManualResourceLoader
  88. will be called to do it.
  89. @param hwGammaCorrection Pass 'true' to enable hardware gamma correction
  90. (sRGB) on this texture. The hardware will convert from gamma space
  91. to linear space when reading from this texture. Only applicable for
  92. 8-bits per channel textures, will be ignored for other types. Has the advantage
  93. over pre-applied gamma that the texture precision is maintained.
  94. @param fsaa The level of multisampling to use if this is a render target. Ignored
  95. if usage does not include TU_RENDERTARGET or if the device does
  96. not support it.
  97. */
  98. TexturePtr create(TextureType texType, UINT32 width, UINT32 height, UINT32 depth,
  99. int num_mips, PixelFormat format, int usage = TU_DEFAULT,
  100. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK);
  101. /** Create a manual texture with a depth of 1 (not loaded from a file).
  102. @param
  103. name The name to give the resulting texture
  104. @param
  105. group The name of the resource group to assign the texture to
  106. @param
  107. texType The type of texture to load/create, defaults to normal 2D textures
  108. @param
  109. width, height The dimensions of the texture
  110. @param
  111. numMipmaps The number of pre-filtered mipmaps to generate. If left to MIP_DEFAULT then
  112. the TextureManager's default number of mipmaps will be used (see setDefaultNumMipmaps()).
  113. If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
  114. level, 1x1x1.
  115. @param
  116. format The internal format you wish to request; the manager reserves
  117. the right to create a different format if the one you select is
  118. not available in this context.
  119. @param
  120. usage The kind of usage this texture is intended for. It
  121. is a combination of TU_STATIC, TU_DYNAMIC, TU_WRITE_ONLY,
  122. TU_AUTOMIPMAP and TU_RENDERTARGET (see TextureUsage enum). You are
  123. strongly advised to use HBU_STATIC_WRITE_ONLY wherever possible, if you need to
  124. update regularly, consider HBU_DYNAMIC_WRITE_ONLY.
  125. @param
  126. loader If you intend the contents of the manual texture to be
  127. regularly updated, to the extent that you don't need to recover
  128. the contents if the texture content is lost somehow, you can leave
  129. this parameter as 0. However, if you intend to populate the
  130. texture only once, then you should implement ManualResourceLoader
  131. and pass a pointer to it in this parameter; this means that if the
  132. manual texture ever needs to be reloaded, the ManualResourceLoader
  133. will be called to do it.
  134. @param hwGammaCorrection Pass 'true' to enable hardware gamma correction
  135. (sRGB) on this texture. The hardware will convert from gamma space
  136. to linear space when reading from this texture. Only applicable for
  137. 8-bits per channel textures, will be ignored for other types. Has the advantage
  138. over pre-applied gamma that the texture precision is maintained.
  139. @param fsaa The level of multisampling to use if this is a render target. Ignored
  140. if usage does not include TU_RENDERTARGET or if the device does
  141. not support it.
  142. */
  143. TexturePtr create(TextureType texType, UINT32 width, UINT32 height, int num_mips,
  144. PixelFormat format, int usage = TU_DEFAULT,
  145. bool hwGammaCorrection = false, UINT32 fsaa = 0, const String& fsaaHint = StringUtil::BLANK)
  146. {
  147. return create(texType, width, height, 1,
  148. num_mips, format, usage, hwGammaCorrection, fsaa, fsaaHint);
  149. }
  150. /** Sets preferred bit depth for integer pixel format textures.
  151. @param
  152. bits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
  153. original format as it is. This value is number of bits for the pixel.
  154. */
  155. virtual void setPreferredIntegerBitDepth(UINT16 bits);
  156. /** gets preferred bit depth for integer pixel format textures.
  157. */
  158. virtual UINT16 getPreferredIntegerBitDepth(void) const;
  159. /** Sets preferred bit depth for float pixel format textures.
  160. @param
  161. bits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
  162. original format as it is. This value is number of bits for a channel of the pixel.
  163. */
  164. virtual void setPreferredFloatBitDepth(UINT16 bits);
  165. /** gets preferred bit depth for float pixel format textures.
  166. */
  167. virtual UINT16 getPreferredFloatBitDepth(void) const;
  168. /** Sets preferred bit depth for integer and float pixel format.
  169. @param
  170. integerBits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
  171. original format as it is. This value is number of bits for the pixel.
  172. @param
  173. floatBits Number of bits. Available values: 0, 16 and 32, where 0 (the default) means keep
  174. original format as it is. This value is number of bits for a channel of the pixel.
  175. */
  176. virtual void setPreferredBitDepths(UINT16 integerBits, UINT16 floatBits);
  177. /** Returns whether this render system can natively support the precise texture
  178. format requested with the given usage options.
  179. @remarks
  180. You can still create textures with this format even if this method returns
  181. false; the texture format will just be altered to one which the device does
  182. support.
  183. @note
  184. Sometimes the device may just slightly change the format, such as reordering the
  185. channels or packing the channels differently, without it making and qualitative
  186. differences to the texture. If you want to just detect whether the quality of a
  187. given texture will be reduced, use isEquivalentFormatSupport instead.
  188. @param format The pixel format requested
  189. @param usage The kind of usage this texture is intended for, a combination of
  190. the TextureUsage flags.
  191. @returns true if the format is natively supported, false if a fallback would be used.
  192. */
  193. virtual bool isFormatSupported(TextureType ttype, PixelFormat format, int usage);
  194. /** Returns whether this render system can support the texture format requested
  195. with the given usage options, or another format with no quality reduction.
  196. */
  197. virtual bool isEquivalentFormatSupported(TextureType ttype, PixelFormat format, int usage);
  198. /** Gets the format which will be natively used for a requested format given the
  199. constraints of the current device.
  200. */
  201. virtual PixelFormat getNativeFormat(TextureType ttype, PixelFormat format, int usage) = 0;
  202. /** Returns whether this render system has hardware filtering supported for the
  203. texture format requested with the given usage options.
  204. @remarks
  205. Not all texture format are supports filtering by the hardware, i.e. some
  206. cards support floating point format, but it doesn't supports filtering on
  207. the floating point texture at all, or only a subset floating point formats
  208. have flitering supported.
  209. @par
  210. In the case you want to write shader to work with floating point texture, and
  211. you want to produce better visual quality, it's necessary to flitering the
  212. texture manually in shader (potential requires four or more texture fetch
  213. instructions, plus several arithmetic instructions) if filtering doesn't
  214. supported by hardware. But in case on the hardware that supports floating
  215. point filtering natively, it had better to adopt this capability for
  216. performance (because only one texture fetch instruction are required) and
  217. doesn't loss visual quality.
  218. @par
  219. This method allow you queries hardware texture filtering capability to deciding
  220. which verion of the shader to be used. Note it's up to you to write multi-version
  221. shaders for support various hardware, internal engine can't do that for you
  222. automatically.
  223. @note
  224. Under GL, texture filtering are always supported by driver, but if it's not
  225. supported by hardware natively, software simulation will be used, and you
  226. will end up with very slow speed (less than 0.1 fps for example). To slove
  227. this performance problem, you must disable filtering manually (by use
  228. <b>filtering none</b> in the material script's texture_unit section, or
  229. call TextureUnitState::setTextureFiltering with TFO_NONE if populate
  230. material in code).
  231. @param ttype The texture type requested
  232. @param format The pixel format requested
  233. @param usage The kind of usage this texture is intended for, a combination of
  234. the TextureUsage flags.
  235. @param preciseFormatOnly Whether precise or fallback format mode is used to detecting.
  236. In case the pixel format doesn't supported by device, false will be returned
  237. if in precise mode, and natively used pixel format will be actually use to
  238. check if in fallback mode.
  239. @returns true if the texture filtering is supported.
  240. */
  241. virtual bool isHardwareFilteringSupported(TextureType ttype, PixelFormat format, int usage,
  242. bool preciseFormatOnly = false) = 0;
  243. /** Sets the default number of mipmaps to be used for loaded textures, for when textures are
  244. loaded automatically (e.g. by Material class) or when 'load' is called with the default
  245. parameters by the application.
  246. If set to MIP_UNLIMITED mipmaps will be generated until the lowest possible
  247. level, 1x1x1.
  248. @note
  249. The default value is 0.
  250. */
  251. virtual void setDefaultNumMipmaps(size_t num);
  252. /** Gets the default number of mipmaps to be used for loaded textures.
  253. */
  254. virtual size_t getDefaultNumMipmaps()
  255. {
  256. return mDefaultNumMipmaps;
  257. }
  258. protected:
  259. UINT16 mPreferredIntegerBitDepth;
  260. UINT16 mPreferredFloatBitDepth;
  261. size_t mDefaultNumMipmaps;
  262. };
  263. /** @} */
  264. /** @} */
  265. }// Namespace
  266. #endif