ITexture.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #ifndef __I_TEXTURE_H_INCLUDED__
  5. #define __I_TEXTURE_H_INCLUDED__
  6. #include "IReferenceCounted.h"
  7. #include "IImage.h"
  8. #include "dimension2d.h"
  9. #include "EDriverTypes.h"
  10. #include "path.h"
  11. #include "matrix4.h"
  12. namespace irr
  13. {
  14. namespace video
  15. {
  16. //! Enumeration flags used to tell the video driver with setTextureCreationFlag in which format textures should be created.
  17. enum E_TEXTURE_CREATION_FLAG
  18. {
  19. /** Forces the driver to create 16 bit textures always, independent of
  20. which format the file on disk has. When choosing this you may lose
  21. some color detail, but gain much speed and memory. 16 bit textures can
  22. be transferred twice as fast as 32 bit textures and only use half of
  23. the space in memory.
  24. When using this flag, it does not make sense to use the flags
  25. ETCF_ALWAYS_32_BIT, ETCF_OPTIMIZED_FOR_QUALITY, or
  26. ETCF_OPTIMIZED_FOR_SPEED at the same time.
  27. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  28. ETCF_ALWAYS_16_BIT = 0x00000001,
  29. /** Forces the driver to create 32 bit textures always, independent of
  30. which format the file on disk has. Please note that some drivers (like
  31. the software device) will ignore this, because they are only able to
  32. create and use 16 bit textures.
  33. Default is true.
  34. When using this flag, it does not make sense to use the flags
  35. ETCF_ALWAYS_16_BIT, ETCF_OPTIMIZED_FOR_QUALITY, or
  36. ETCF_OPTIMIZED_FOR_SPEED at the same time.
  37. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  38. ETCF_ALWAYS_32_BIT = 0x00000002,
  39. /** Lets the driver decide in which format the textures are created and
  40. tries to make the textures look as good as possible. Usually it simply
  41. chooses the format in which the texture was stored on disk.
  42. When using this flag, it does not make sense to use the flags
  43. ETCF_ALWAYS_16_BIT, ETCF_ALWAYS_32_BIT, or ETCF_OPTIMIZED_FOR_SPEED at
  44. the same time.
  45. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  46. ETCF_OPTIMIZED_FOR_QUALITY = 0x00000004,
  47. /** Lets the driver decide in which format the textures are created and
  48. tries to create them maximizing render speed.
  49. When using this flag, it does not make sense to use the flags
  50. ETCF_ALWAYS_16_BIT, ETCF_ALWAYS_32_BIT, or ETCF_OPTIMIZED_FOR_QUALITY,
  51. at the same time.
  52. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  53. ETCF_OPTIMIZED_FOR_SPEED = 0x00000008,
  54. /** Creates textures with mipmap levels.
  55. If disabled textures can not have mipmaps.
  56. Default is true. */
  57. ETCF_CREATE_MIP_MAPS = 0x00000010,
  58. /** Discard any alpha layer and use non-alpha color format.
  59. Warning: This may lead to getting 24-bit texture formats which
  60. are often badly supported by drivers. So it's generally
  61. not recommended to enable this flag. */
  62. ETCF_NO_ALPHA_CHANNEL = 0x00000020,
  63. //! Allow the Driver to use Non-Power-2-Textures
  64. /** BurningVideo can handle Non-Power-2 Textures in 2D (GUI), but not in 3D. */
  65. ETCF_ALLOW_NON_POWER_2 = 0x00000040,
  66. //! Allow the driver to keep a copy of the texture in memory
  67. /** Enabling this makes calls to ITexture::lock a lot faster, but costs main memory.
  68. Currently only used in combination with OpenGL drivers.
  69. NOTE: Disabling this does not yet work correctly with alpha-textures.
  70. So the default is on for now (but might change with Irrlicht 1.9 if we get the alpha-troubles fixed).
  71. */
  72. ETCF_ALLOW_MEMORY_COPY = 0x00000080,
  73. //! Enable automatic updating mip maps when the base texture changes.
  74. /** Default is true.
  75. This flag is only used when ETCF_CREATE_MIP_MAPS is also enabled and if the driver supports it.
  76. Please note:
  77. - On D3D (and maybe older OGL?) you can no longer manually set mipmap data when enabled
  78. (for example mips from image loading will be ignored).
  79. - On D3D (and maybe older OGL?) texture locking for mipmap levels usually won't work anymore.
  80. - On new OGL this flag is ignored.
  81. - When disabled you do _not_ get hardware mipmaps on D3D, so mipmap generation can be slower.
  82. - When disabled you can still update your mipmaps when the texture changed by manually calling regenerateMipMapLevels.
  83. - You can still call regenerateMipMapLevels when this flag is enabled (it will be a hint on d3d to update mips immediately)
  84. */
  85. ETCF_AUTO_GENERATE_MIP_MAPS = 0x00000100,
  86. /** This flag is never used, it only forces the compiler to compile
  87. these enumeration values to 32 bit. */
  88. ETCF_FORCE_32_BIT_DO_NOT_USE = 0x7fffffff
  89. };
  90. //! Enum for the mode for texture locking. Read-Only, write-only or read/write.
  91. enum E_TEXTURE_LOCK_MODE
  92. {
  93. //! The default mode. Texture can be read and written to.
  94. ETLM_READ_WRITE = 0,
  95. //! Read only. The texture is downloaded, but not uploaded again.
  96. /** Often used to read back shader generated textures. */
  97. ETLM_READ_ONLY,
  98. //! Write only. The texture is not downloaded and might be uninitialized.
  99. /** The updated texture is uploaded to the GPU.
  100. Used for initializing the shader from the CPU. */
  101. ETLM_WRITE_ONLY
  102. };
  103. //! Additional bitflags for ITexture::lock() call
  104. enum E_TEXTURE_LOCK_FLAGS
  105. {
  106. ETLF_NONE = 0,
  107. //! Flip left-bottom origin rendertarget textures upside-down
  108. /** Irrlicht usually has all textures with left-top as origin.
  109. And for drivers with a left-bottom origin coordinate system (OpenGL)
  110. Irrlicht modifies the texture-matrix in the fixed function pipeline to make
  111. the textures show up correctly (shader coders have to handle upside down
  112. textures themselves).
  113. But rendertarget textures (RTT's) are written by drivers the way the
  114. coordinate system of that driver works. So on OpenGL images tend to look
  115. upside down (aka Y coordinate going up) on lock() when this flag isn't set.
  116. When the flag is set it will flip such textures on lock() to make them look
  117. like non-rtt textures (origin left-top). Note that this also means the texture
  118. will be uploaded flipped on unlock. So mostly you want to have this flag set
  119. when you want to look at the texture or save it, but unset if you want to
  120. upload it again to the card.
  121. If you disable this flag you get the memory just as it is on the graphic card.
  122. For backward compatibility reasons this flag is enabled by default. */
  123. ETLF_FLIP_Y_UP_RTT = 1
  124. };
  125. //! Where did the last IVideoDriver::getTexture call find this texture
  126. enum E_TEXTURE_SOURCE
  127. {
  128. //! IVideoDriver::getTexture was never called (texture created otherwise)
  129. ETS_UNKNOWN,
  130. //! Texture has been found in cache
  131. ETS_FROM_CACHE,
  132. //! Texture had to be loaded
  133. ETS_FROM_FILE
  134. };
  135. //! Enumeration describing the type of ITexture.
  136. enum E_TEXTURE_TYPE
  137. {
  138. //! 2D texture.
  139. ETT_2D,
  140. //! Cubemap texture.
  141. ETT_CUBEMAP
  142. };
  143. //! Interface of a Video Driver dependent Texture.
  144. /** An ITexture is created by an IVideoDriver by using IVideoDriver::addTexture
  145. or IVideoDriver::getTexture. After that, the texture may only be used by this
  146. VideoDriver. As you can imagine, textures of the DirectX and the OpenGL device
  147. will, e.g., not be compatible. An exception is the Software device and the
  148. NULL device, their textures are compatible. If you try to use a texture
  149. created by one device with an other device, the device will refuse to do that
  150. and write a warning or an error message to the output buffer.
  151. */
  152. class ITexture : public virtual IReferenceCounted
  153. {
  154. public:
  155. //! constructor
  156. ITexture(const io::path& name, E_TEXTURE_TYPE type) : NamedPath(name), DriverType(EDT_NULL), OriginalColorFormat(ECF_UNKNOWN),
  157. ColorFormat(ECF_UNKNOWN), Pitch(0), HasMipMaps(false), IsRenderTarget(false), Source(ETS_UNKNOWN), Type(type)
  158. {
  159. }
  160. //! Lock function.
  161. /** Locks the Texture and returns a pointer to access the
  162. pixels. After lock() has been called and all operations on the pixels
  163. are done, you must call unlock().
  164. Locks are not accumulating, hence one unlock will do for an arbitrary
  165. number of previous locks. You should avoid locking different levels without
  166. unlocking in between, though, because only the last level locked will be
  167. unlocked.
  168. The size of the i-th mipmap level is defined as max(getSize().Width>>i,1)
  169. and max(getSize().Height>>i,1)
  170. \param mode Specifies what kind of changes to the locked texture are
  171. allowed. Unspecified behavior will arise if texture is written in read
  172. only mode or read from in write only mode.
  173. Support for this feature depends on the driver, so don't rely on the
  174. texture being write-protected when locking with read-only, etc.
  175. \param mipmapLevel NOTE: Currently broken, sorry, we try if we can repair it for 1.9 release.
  176. Number of the mipmapLevel to lock. 0 is main texture.
  177. Non-existing levels will silently fail and return 0.
  178. \param layer It determines which cubemap face or texture array layer should be locked.
  179. \param lockFlags See E_TEXTURE_LOCK_FLAGS documentation.
  180. \return Returns a pointer to the pixel data. The format of the pixel can
  181. be determined by using getColorFormat(). 0 is returned, if
  182. the texture cannot be locked. */
  183. virtual void* lock(E_TEXTURE_LOCK_MODE mode = ETLM_READ_WRITE, u32 mipmapLevel=0, u32 layer = 0, E_TEXTURE_LOCK_FLAGS lockFlags = ETLF_FLIP_Y_UP_RTT) = 0;
  184. //! Unlock function. Must be called after a lock() to the texture.
  185. /** One should avoid to call unlock more than once before another lock.
  186. The last locked mip level will be unlocked.
  187. You may want to call regenerateMipMapLevels() after this when you changed any data. */
  188. virtual void unlock() = 0;
  189. //! Regenerates the mip map levels of the texture.
  190. /** Required after modifying the texture, usually after calling unlock().
  191. \param data Optional parameter to pass in image data which will be
  192. used instead of the previously stored or automatically generated mipmap
  193. data. The data has to be a continuous pixel data for all mipmaps until
  194. 1x1 pixel. Each mipmap has to be half the width and height of the previous
  195. level. At least one pixel will be always kept.
  196. \param layer It informs a texture about which cubemap or texture array layer
  197. needs mipmap regeneration. */
  198. virtual void regenerateMipMapLevels(void* data = 0, u32 layer = 0) = 0;
  199. //! Get original size of the texture.
  200. /** The texture is usually scaled, if it was created with an unoptimal
  201. size. For example if the size was not a power of two. This method
  202. returns the size of the texture it had before it was scaled. Can be
  203. useful when drawing 2d images on the screen, which should have the
  204. exact size of the original texture. Use ITexture::getSize() if you want
  205. to know the real size it has now stored in the system.
  206. \return The original size of the texture. */
  207. const core::dimension2d<u32>& getOriginalSize() const { return OriginalSize; };
  208. //! Get dimension (=size) of the texture.
  209. /** \return The size of the texture. */
  210. const core::dimension2d<u32>& getSize() const { return Size; };
  211. //! Get driver type of texture.
  212. /** This is the driver, which created the texture. This method is used
  213. internally by the video devices, to check, if they may use a texture
  214. because textures may be incompatible between different devices.
  215. \return Driver type of texture. */
  216. E_DRIVER_TYPE getDriverType() const { return DriverType; };
  217. //! Get the color format of texture.
  218. /** \return The color format of texture. */
  219. ECOLOR_FORMAT getColorFormat() const { return ColorFormat; };
  220. //! Get the original color format
  221. /** When create textures from image data we will often use different color formats.
  222. For example depending on driver TextureCreationFlag's.
  223. This can give you the original format which the image used to create the texture had */
  224. ECOLOR_FORMAT getOriginalColorFormat() const { return OriginalColorFormat; };
  225. //! Get pitch of the main texture (in bytes).
  226. /** The pitch is the amount of bytes used for a row of pixels in a
  227. texture.
  228. \return Pitch of texture in bytes. */
  229. u32 getPitch() const { return Pitch; };
  230. //! Check whether the texture has MipMaps
  231. /** \return True if texture has MipMaps, else false. */
  232. bool hasMipMaps() const { return HasMipMaps; }
  233. //! Check whether the texture is a render target
  234. /** Render targets can be set as such in the video driver, in order to
  235. render a scene into the texture. Once unbound as render target, they can
  236. be used just as usual textures again.
  237. \return True if this is a render target, otherwise false. */
  238. bool isRenderTarget() const { return IsRenderTarget; }
  239. //! Get name of texture (in most cases this is the filename)
  240. const io::SNamedPath& getName() const { return NamedPath; }
  241. //! Check where the last IVideoDriver::getTexture found this texture
  242. E_TEXTURE_SOURCE getSource() const { return Source; }
  243. //! Used internally by the engine to update Source status on IVideoDriver::getTexture calls.
  244. void updateSource(E_TEXTURE_SOURCE source) { Source = source; }
  245. //! Returns if the texture has an alpha channel
  246. bool hasAlpha() const
  247. {
  248. bool status = false;
  249. switch (ColorFormat)
  250. {
  251. case ECF_A8R8G8B8:
  252. case ECF_A1R5G5B5:
  253. case ECF_DXT1:
  254. case ECF_DXT2:
  255. case ECF_DXT3:
  256. case ECF_DXT4:
  257. case ECF_DXT5:
  258. case ECF_A16B16G16R16F:
  259. case ECF_A32B32G32R32F:
  260. status = true;
  261. break;
  262. default:
  263. break;
  264. }
  265. return status;
  266. }
  267. //! Returns the type of texture
  268. E_TEXTURE_TYPE getType() const { return Type; }
  269. protected:
  270. //! Helper function, helps to get the desired texture creation format from the flags.
  271. /** \return Either ETCF_ALWAYS_32_BIT, ETCF_ALWAYS_16_BIT,
  272. ETCF_OPTIMIZED_FOR_QUALITY, or ETCF_OPTIMIZED_FOR_SPEED. */
  273. inline E_TEXTURE_CREATION_FLAG getTextureFormatFromFlags(u32 flags)
  274. {
  275. if (flags & ETCF_OPTIMIZED_FOR_SPEED)
  276. return ETCF_OPTIMIZED_FOR_SPEED;
  277. if (flags & ETCF_ALWAYS_16_BIT)
  278. return ETCF_ALWAYS_16_BIT;
  279. if (flags & ETCF_ALWAYS_32_BIT)
  280. return ETCF_ALWAYS_32_BIT;
  281. if (flags & ETCF_OPTIMIZED_FOR_QUALITY)
  282. return ETCF_OPTIMIZED_FOR_QUALITY;
  283. return ETCF_OPTIMIZED_FOR_SPEED;
  284. }
  285. io::SNamedPath NamedPath;
  286. core::dimension2d<u32> OriginalSize;
  287. core::dimension2d<u32> Size;
  288. E_DRIVER_TYPE DriverType;
  289. ECOLOR_FORMAT OriginalColorFormat;
  290. ECOLOR_FORMAT ColorFormat;
  291. u32 Pitch;
  292. bool HasMipMaps;
  293. bool IsRenderTarget;
  294. E_TEXTURE_SOURCE Source;
  295. E_TEXTURE_TYPE Type;
  296. };
  297. } // end namespace video
  298. } // end namespace irr
  299. #endif