ITexture.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. #pragma once
  5. #include "IReferenceCounted.h"
  6. #include "IImage.h"
  7. #include "dimension2d.h"
  8. #include "EDriverTypes.h"
  9. #include "path.h"
  10. #include "matrix4.h"
  11. namespace irr
  12. {
  13. namespace video
  14. {
  15. //! Enumeration flags used to tell the video driver with setTextureCreationFlag in which format textures should be created.
  16. enum E_TEXTURE_CREATION_FLAG
  17. {
  18. /** Forces the driver to create 16 bit textures always, independent of
  19. which format the file on disk has. When choosing this you may lose
  20. some color detail, but gain much speed and memory. 16 bit textures can
  21. be transferred twice as fast as 32 bit textures and only use half of
  22. the space in memory.
  23. When using this flag, it does not make sense to use the flags
  24. ETCF_ALWAYS_32_BIT, ETCF_OPTIMIZED_FOR_QUALITY, or
  25. ETCF_OPTIMIZED_FOR_SPEED at the same time.
  26. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  27. ETCF_ALWAYS_16_BIT = 0x00000001,
  28. /** Forces the driver to create 32 bit textures always, independent of
  29. which format the file on disk has. Please note that some drivers (like
  30. the software device) will ignore this, because they are only able to
  31. create and use 16 bit textures.
  32. Default is true.
  33. When using this flag, it does not make sense to use the flags
  34. ETCF_ALWAYS_16_BIT, ETCF_OPTIMIZED_FOR_QUALITY, or
  35. ETCF_OPTIMIZED_FOR_SPEED at the same time.
  36. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  37. ETCF_ALWAYS_32_BIT = 0x00000002,
  38. /** Lets the driver decide in which format the textures are created and
  39. tries to make the textures look as good as possible. Usually it simply
  40. chooses the format in which the texture was stored on disk.
  41. When using this flag, it does not make sense to use the flags
  42. ETCF_ALWAYS_16_BIT, ETCF_ALWAYS_32_BIT, or ETCF_OPTIMIZED_FOR_SPEED at
  43. the same time.
  44. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  45. ETCF_OPTIMIZED_FOR_QUALITY = 0x00000004,
  46. /** Lets the driver decide in which format the textures are created and
  47. tries to create them maximizing render speed.
  48. When using this flag, it does not make sense to use the flags
  49. ETCF_ALWAYS_16_BIT, ETCF_ALWAYS_32_BIT, or ETCF_OPTIMIZED_FOR_QUALITY,
  50. at the same time.
  51. Not all texture formats are affected (usually those up to ECF_A8R8G8B8). */
  52. ETCF_OPTIMIZED_FOR_SPEED = 0x00000008,
  53. /** Creates textures with mipmap levels.
  54. If disabled textures can not have mipmaps.
  55. Default is true. */
  56. ETCF_CREATE_MIP_MAPS = 0x00000010,
  57. /** Discard any alpha layer and use non-alpha color format.
  58. Warning: This may lead to getting 24-bit texture formats which
  59. are often badly supported by drivers. So it's generally
  60. not recommended to enable this flag. */
  61. ETCF_NO_ALPHA_CHANNEL = 0x00000020,
  62. //! Allow the Driver to use Non-Power-2-Textures
  63. /** BurningVideo can handle Non-Power-2 Textures in 2D (GUI), but not in 3D. */
  64. ETCF_ALLOW_NON_POWER_2 = 0x00000040,
  65. //! Allow the driver to keep a copy of the texture in memory
  66. /** Enabling this makes calls to ITexture::lock a lot faster, but costs main memory.
  67. Currently only used in combination with OpenGL drivers.
  68. NOTE: Disabling this does not yet work correctly with alpha-textures.
  69. So the default is on for now (but might change with Irrlicht 1.9 if we get the alpha-troubles fixed).
  70. */
  71. ETCF_ALLOW_MEMORY_COPY = 0x00000080,
  72. //! Enable automatic updating mip maps when the base texture changes.
  73. /** Default is true.
  74. This flag is only used when ETCF_CREATE_MIP_MAPS is also enabled and if the driver supports it.
  75. Please note:
  76. - On D3D (and maybe older OGL?) you can no longer manually set mipmap data when enabled
  77. (for example mips from image loading will be ignored).
  78. - On D3D (and maybe older OGL?) texture locking for mipmap levels usually won't work anymore.
  79. - On new OGL this flag is ignored.
  80. - When disabled you do _not_ get hardware mipmaps on D3D, so mipmap generation can be slower.
  81. - When disabled you can still update your mipmaps when the texture changed by manually calling regenerateMipMapLevels.
  82. - You can still call regenerateMipMapLevels when this flag is enabled (it will be a hint on d3d to update mips immediately)
  83. */
  84. ETCF_AUTO_GENERATE_MIP_MAPS = 0x00000100,
  85. /** This flag is never used, it only forces the compiler to compile
  86. these enumeration values to 32 bit. */
  87. ETCF_FORCE_32_BIT_DO_NOT_USE = 0x7fffffff
  88. };
  89. //! Enum for the mode for texture locking. Read-Only, write-only or read/write.
  90. enum E_TEXTURE_LOCK_MODE
  91. {
  92. //! The default mode. Texture can be read and written to.
  93. ETLM_READ_WRITE = 0,
  94. //! Read only. The texture is downloaded, but not uploaded again.
  95. /** Often used to read back shader generated textures. */
  96. ETLM_READ_ONLY,
  97. //! Write only. The texture is not downloaded and might be uninitialized.
  98. /** The updated texture is uploaded to the GPU.
  99. Used for initializing the shader from the CPU. */
  100. ETLM_WRITE_ONLY
  101. };
  102. //! Additional bitflags for ITexture::lock() call
  103. enum E_TEXTURE_LOCK_FLAGS
  104. {
  105. ETLF_NONE = 0,
  106. //! Flip left-bottom origin rendertarget textures upside-down
  107. /** Irrlicht usually has all textures with left-top as origin.
  108. And for drivers with a left-bottom origin coordinate system (OpenGL)
  109. Irrlicht modifies the texture-matrix in the fixed function pipeline to make
  110. the textures show up correctly (shader coders have to handle upside down
  111. textures themselves).
  112. But rendertarget textures (RTT's) are written by drivers the way the
  113. coordinate system of that driver works. So on OpenGL images tend to look
  114. upside down (aka Y coordinate going up) on lock() when this flag isn't set.
  115. When the flag is set it will flip such textures on lock() to make them look
  116. like non-rtt textures (origin left-top). Note that this also means the texture
  117. will be uploaded flipped on unlock. So mostly you want to have this flag set
  118. when you want to look at the texture or save it, but unset if you want to
  119. upload it again to the card.
  120. If you disable this flag you get the memory just as it is on the graphic card.
  121. For backward compatibility reasons this flag is enabled by default. */
  122. ETLF_FLIP_Y_UP_RTT = 1
  123. };
  124. //! Where did the last IVideoDriver::getTexture call find this texture
  125. enum E_TEXTURE_SOURCE
  126. {
  127. //! IVideoDriver::getTexture was never called (texture created otherwise)
  128. ETS_UNKNOWN,
  129. //! Texture has been found in cache
  130. ETS_FROM_CACHE,
  131. //! Texture had to be loaded
  132. ETS_FROM_FILE
  133. };
  134. //! Enumeration describing the type of ITexture.
  135. enum E_TEXTURE_TYPE
  136. {
  137. //! 2D texture.
  138. ETT_2D,
  139. //! Cubemap texture.
  140. ETT_CUBEMAP
  141. };
  142. //! Interface of a Video Driver dependent Texture.
  143. /** An ITexture is created by an IVideoDriver by using IVideoDriver::addTexture
  144. or IVideoDriver::getTexture. After that, the texture may only be used by this
  145. VideoDriver. As you can imagine, textures of the DirectX and the OpenGL device
  146. will, e.g., not be compatible. An exception is the Software device and the
  147. NULL device, their textures are compatible. If you try to use a texture
  148. created by one device with an other device, the device will refuse to do that
  149. and write a warning or an error message to the output buffer.
  150. */
  151. class ITexture : public virtual IReferenceCounted
  152. {
  153. public:
  154. //! constructor
  155. ITexture(const io::path &name, E_TEXTURE_TYPE type) :
  156. 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. case ECF_A8R8G8B8:
  251. case ECF_A1R5G5B5:
  252. case ECF_A16B16G16R16F:
  253. case ECF_A32B32G32R32F:
  254. status = true;
  255. break;
  256. default:
  257. break;
  258. }
  259. return status;
  260. }
  261. //! Returns the type of texture
  262. E_TEXTURE_TYPE getType() const { return Type; }
  263. protected:
  264. //! Helper function, helps to get the desired texture creation format from the flags.
  265. /** \return Either ETCF_ALWAYS_32_BIT, ETCF_ALWAYS_16_BIT,
  266. ETCF_OPTIMIZED_FOR_QUALITY, or ETCF_OPTIMIZED_FOR_SPEED. */
  267. inline E_TEXTURE_CREATION_FLAG getTextureFormatFromFlags(u32 flags)
  268. {
  269. if (flags & ETCF_OPTIMIZED_FOR_SPEED)
  270. return ETCF_OPTIMIZED_FOR_SPEED;
  271. if (flags & ETCF_ALWAYS_16_BIT)
  272. return ETCF_ALWAYS_16_BIT;
  273. if (flags & ETCF_ALWAYS_32_BIT)
  274. return ETCF_ALWAYS_32_BIT;
  275. if (flags & ETCF_OPTIMIZED_FOR_QUALITY)
  276. return ETCF_OPTIMIZED_FOR_QUALITY;
  277. return ETCF_OPTIMIZED_FOR_SPEED;
  278. }
  279. io::SNamedPath NamedPath;
  280. core::dimension2d<u32> OriginalSize;
  281. core::dimension2d<u32> Size;
  282. E_DRIVER_TYPE DriverType;
  283. ECOLOR_FORMAT OriginalColorFormat;
  284. ECOLOR_FORMAT ColorFormat;
  285. u32 Pitch;
  286. bool HasMipMaps;
  287. bool IsRenderTarget;
  288. E_TEXTURE_SOURCE Source;
  289. E_TEXTURE_TYPE Type;
  290. };
  291. } // end namespace video
  292. } // end namespace irr