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