ITexture.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 IRR_I_TEXTURE_H_INCLUDED
  5. #define IRR_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. //! Enable support for vertex shader texture sampling on some drivers
  87. /** Default is false.
  88. This adds a small costs to all texture switches.
  89. Currently only affects D3D9.
  90. On OpenGL vertex shaders use the same texture unit as pixel shaders, so support there only depends on GL version and not on this flag
  91. */
  92. ETCF_SUPPORT_VERTEXT_TEXTURE = 0x00000200,
  93. /** This flag is never used, it only forces the compiler to compile
  94. these enumeration values to 32 bit. */
  95. ETCF_FORCE_32_BIT_DO_NOT_USE = 0x7fffffff
  96. };
  97. //! Enum for the mode for texture locking. Read-Only, write-only or read/write.
  98. enum E_TEXTURE_LOCK_MODE
  99. {
  100. //! The default mode. Texture can be read and written to.
  101. ETLM_READ_WRITE = 0,
  102. //! Read only. The texture is downloaded, but not uploaded again.
  103. /** Often used to read back shader generated textures. */
  104. ETLM_READ_ONLY,
  105. //! Write only. The texture is not downloaded and might be uninitialized.
  106. /** The updated texture is uploaded to the GPU.
  107. Used for initializing the shader from the CPU. */
  108. ETLM_WRITE_ONLY
  109. };
  110. //! Additional bitflags for ITexture::lock() call
  111. enum E_TEXTURE_LOCK_FLAGS
  112. {
  113. ETLF_NONE = 0,
  114. //! Flip left-bottom origin rendertarget textures upside-down
  115. /** Irrlicht usually has all textures with left-top as origin.
  116. And for drivers with a left-bottom origin coordinate system (OpenGL)
  117. Irrlicht modifies the texture-matrix in the fixed function pipeline to make
  118. the textures show up correctly (shader coders have to handle upside down
  119. textures themselves).
  120. But rendertarget textures (RTT's) are written by drivers the way the
  121. coordinate system of that driver works. So on OpenGL images tend to look
  122. upside down (aka Y coordinate going up) on lock() when this flag isn't set.
  123. When the flag is set it will flip such textures on lock() to make them look
  124. like non-rtt textures (origin left-top). Note that this also means the texture
  125. will be uploaded flipped on unlock. So mostly you want to have this flag set
  126. when you want to look at the texture or save it, but unset if you want to
  127. upload it again to the card.
  128. If you disable this flag you get the memory just as it is on the graphic card.
  129. For backward compatibility reasons this flag is enabled by default. */
  130. ETLF_FLIP_Y_UP_RTT = 1
  131. };
  132. //! Where did the last IVideoDriver::getTexture call find this texture
  133. enum E_TEXTURE_SOURCE
  134. {
  135. //! IVideoDriver::getTexture was never called (texture created otherwise)
  136. ETS_UNKNOWN,
  137. //! Texture has been found in cache
  138. ETS_FROM_CACHE,
  139. //! Texture had to be loaded
  140. ETS_FROM_FILE
  141. };
  142. //! Enumeration describing the type of ITexture.
  143. enum E_TEXTURE_TYPE
  144. {
  145. //! 2D texture.
  146. ETT_2D,
  147. //! Cubemap texture.
  148. ETT_CUBEMAP
  149. };
  150. //! Interface of a Video Driver dependent Texture.
  151. /** An ITexture is created by an IVideoDriver by using IVideoDriver::addTexture
  152. or IVideoDriver::getTexture. After that, the texture may only be used by this
  153. VideoDriver. As you can imagine, textures of the DirectX and the OpenGL device
  154. will, e.g., not be compatible. An exception is the Software device and the
  155. NULL device, their textures are compatible. If you try to use a texture
  156. created by one device with an other device, the device will refuse to do that
  157. and write a warning or an error message to the output buffer.
  158. */
  159. class ITexture : public virtual IReferenceCounted
  160. {
  161. public:
  162. //! constructor
  163. ITexture(const io::path& name, E_TEXTURE_TYPE type) : NamedPath(name), DriverType(EDT_NULL), OriginalColorFormat(ECF_UNKNOWN),
  164. ColorFormat(ECF_UNKNOWN), Pitch(0), HasMipMaps(false), IsRenderTarget(false), Source(ETS_UNKNOWN), Type(type)
  165. {
  166. }
  167. //! Lock function.
  168. /** Locks the Texture and returns a pointer to access the
  169. pixels. After lock() has been called and all operations on the pixels
  170. are done, you must call unlock(). Locks are not accumulating, hence one
  171. unlock will do for an arbitrary number of previous locks. You should avoid
  172. locking different levels without unlocking in between, because only the
  173. last level locked will be unlocked.
  174. The size of the i-th mipmap level is defined as max(getSize().Width>>i,1)
  175. and max(getSize().Height>>i,1).
  176. Except for textures of EDT_SOFTWARE driver which returns data for
  177. getOriginalSize(). Reason: Both original sized and modified sized textures are used
  178. in that driver depending on whether the texture is used in 2d or 3d.
  179. \param mode Specifies what kind of changes to the locked texture are
  180. allowed. Unspecified behavior will arise if texture is written in read
  181. only mode or read from in write only mode.
  182. Support for this feature depends on the driver, so don't rely on the
  183. texture being write-protected when locking with read-only, etc.
  184. \param mipmapLevel Number of the mipmapLevel to lock. 0 is main texture.
  185. Non-existing levels will silently fail and return 0.
  186. \param layer It determines which cubemap face or texture array layer should be locked.
  187. \param lockFlags See E_TEXTURE_LOCK_FLAGS documentation.
  188. \return Returns a pointer to the pixel data. The format of the pixel can
  189. be determined by using getColorFormat(). 0 is returned, if
  190. the texture cannot be locked. */
  191. 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;
  192. //! Unlock function. Must be called after a lock() to the texture.
  193. /** One should avoid to call unlock more than once before another lock.
  194. The last locked mip level will be unlocked.
  195. You may want to call regenerateMipMapLevels() after this when you changed any data. */
  196. virtual void unlock() = 0;
  197. //! Regenerates the mip map levels of the texture.
  198. /** Required after modifying the texture, usually after calling unlock().
  199. \param data Optional parameter to pass in image data which will be
  200. used instead of the previously stored or automatically generated mipmap
  201. data. The data has to be a continuous pixel data for all mipmaps until
  202. 1x1 pixel. Each mipmap has to be half the width and height of the previous
  203. level. At least one pixel will be always kept.
  204. \param layer It informs a texture about which cubemap or texture array layer
  205. needs mipmap regeneration. */
  206. virtual void regenerateMipMapLevels(void* data = 0, u32 layer = 0) = 0;
  207. //! Get original size of the texture.
  208. /** The texture is usually scaled, if it was created with an unoptimal
  209. size. For example if the size was not a power of two. This method
  210. returns the size of the texture it had before it was scaled. Can be
  211. useful when drawing 2d images on the screen, which should have the
  212. exact size of the original texture. Use ITexture::getSize() if you want
  213. to know the real size it has now stored in the system.
  214. \return The original size of the texture. */
  215. const core::dimension2d<u32>& getOriginalSize() const { return OriginalSize; };
  216. //! Get dimension (=size) of the texture.
  217. /** \return The size of the texture. */
  218. const core::dimension2d<u32>& getSize() const { return Size; };
  219. //! Get driver type of texture.
  220. /** This is the driver, which created the texture. This method is used
  221. internally by the video devices, to check, if they may use a texture
  222. because textures may be incompatible between different devices.
  223. \return Driver type of texture. */
  224. E_DRIVER_TYPE getDriverType() const { return DriverType; };
  225. //! Get the color format of texture.
  226. /** \return The color format of texture. */
  227. ECOLOR_FORMAT getColorFormat() const { return ColorFormat; };
  228. //! Get the original color format
  229. /** When create textures from image data we will often use different color formats.
  230. For example depending on driver TextureCreationFlag's.
  231. This can give you the original format which the image used to create the texture had */
  232. ECOLOR_FORMAT getOriginalColorFormat() const { return OriginalColorFormat; };
  233. //! Get pitch of the main texture (in bytes).
  234. /** The pitch is the amount of bytes used for a row of pixels in a
  235. texture.
  236. \return Pitch of texture in bytes. */
  237. u32 getPitch() const { return Pitch; };
  238. //! Check whether the texture has MipMaps
  239. /** \return True if texture has MipMaps, else false. */
  240. bool hasMipMaps() const { return HasMipMaps; }
  241. //! Check whether the texture is a render target
  242. /** Render targets can be set as such in the video driver, in order to
  243. render a scene into the texture. Once unbound as render target, they can
  244. be used just as usual textures again.
  245. \return True if this is a render target, otherwise false. */
  246. bool isRenderTarget() const { return IsRenderTarget; }
  247. //! Get name of texture (in most cases this is the filename)
  248. const io::SNamedPath& getName() const { return NamedPath; }
  249. //! Check where the last IVideoDriver::getTexture found this texture
  250. E_TEXTURE_SOURCE getSource() const { return Source; }
  251. //! Used internally by the engine to update Source status on IVideoDriver::getTexture calls.
  252. void updateSource(E_TEXTURE_SOURCE source) { Source = source; }
  253. //! Returns if the texture has an alpha channel
  254. bool hasAlpha() const
  255. {
  256. bool status = false;
  257. switch (ColorFormat)
  258. {
  259. case ECF_A8R8G8B8:
  260. case ECF_A1R5G5B5:
  261. case ECF_DXT1:
  262. case ECF_DXT2:
  263. case ECF_DXT3:
  264. case ECF_DXT4:
  265. case ECF_DXT5:
  266. case ECF_A16B16G16R16F:
  267. case ECF_A32B32G32R32F:
  268. status = true;
  269. break;
  270. default:
  271. break;
  272. }
  273. return status;
  274. }
  275. //! Returns the type of texture
  276. E_TEXTURE_TYPE getType() const { return Type; }
  277. protected:
  278. //! Helper function, helps to get the desired texture creation format from the flags.
  279. /** \return Either ETCF_ALWAYS_32_BIT, ETCF_ALWAYS_16_BIT,
  280. ETCF_OPTIMIZED_FOR_QUALITY, or ETCF_OPTIMIZED_FOR_SPEED. */
  281. inline E_TEXTURE_CREATION_FLAG getTextureFormatFromFlags(u32 flags)
  282. {
  283. if (flags & ETCF_OPTIMIZED_FOR_SPEED)
  284. return ETCF_OPTIMIZED_FOR_SPEED;
  285. if (flags & ETCF_ALWAYS_16_BIT)
  286. return ETCF_ALWAYS_16_BIT;
  287. if (flags & ETCF_ALWAYS_32_BIT)
  288. return ETCF_ALWAYS_32_BIT;
  289. if (flags & ETCF_OPTIMIZED_FOR_QUALITY)
  290. return ETCF_OPTIMIZED_FOR_QUALITY;
  291. return ETCF_OPTIMIZED_FOR_SPEED;
  292. }
  293. io::SNamedPath NamedPath;
  294. core::dimension2d<u32> OriginalSize;
  295. core::dimension2d<u32> Size;
  296. E_DRIVER_TYPE DriverType;
  297. ECOLOR_FORMAT OriginalColorFormat;
  298. ECOLOR_FORMAT ColorFormat;
  299. u32 Pitch;
  300. bool HasMipMaps;
  301. bool IsRenderTarget;
  302. E_TEXTURE_SOURCE Source;
  303. E_TEXTURE_TYPE Type;
  304. };
  305. } // end namespace video
  306. } // end namespace irr
  307. #endif