ITexture.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 telling the video driver 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. 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. 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. ETCF_ALWAYS_32_BIT = 0x00000002,
  36. /** Lets the driver decide in which format the textures are created and
  37. tries to make the textures look as good as possible. Usually it simply
  38. chooses the format in which the texture was stored on disk.
  39. When using this flag, it does not make sense to use the flags
  40. ETCF_ALWAYS_16_BIT, ETCF_ALWAYS_32_BIT, or ETCF_OPTIMIZED_FOR_SPEED at
  41. the same time. */
  42. ETCF_OPTIMIZED_FOR_QUALITY = 0x00000004,
  43. /** Lets the driver decide in which format the textures are created and
  44. tries to create them maximizing render speed.
  45. When using this flag, it does not make sense to use the flags
  46. ETCF_ALWAYS_16_BIT, ETCF_ALWAYS_32_BIT, or ETCF_OPTIMIZED_FOR_QUALITY,
  47. at the same time. */
  48. ETCF_OPTIMIZED_FOR_SPEED = 0x00000008,
  49. /** Automatically creates mip map levels for the textures. */
  50. ETCF_CREATE_MIP_MAPS = 0x00000010,
  51. /** Discard any alpha layer and use non-alpha color format. */
  52. ETCF_NO_ALPHA_CHANNEL = 0x00000020,
  53. //! Allow the Driver to use Non-Power-2-Textures
  54. /** BurningVideo can handle Non-Power-2 Textures in 2D (GUI), but not in 3D. */
  55. ETCF_ALLOW_NON_POWER_2 = 0x00000040,
  56. /** This flag is never used, it only forces the compiler to compile
  57. these enumeration values to 32 bit. */
  58. ETCF_FORCE_32_BIT_DO_NOT_USE = 0x7fffffff
  59. };
  60. //! Enum for the mode for texture locking. Read-Only, write-only or read/write.
  61. enum E_TEXTURE_LOCK_MODE
  62. {
  63. //! The default mode. Texture can be read and written to.
  64. ETLM_READ_WRITE = 0,
  65. //! Read only. The texture is downloaded, but not uploaded again.
  66. /** Often used to read back shader generated textures. */
  67. ETLM_READ_ONLY,
  68. //! Write only. The texture is not downloaded and might be uninitialised.
  69. /** The updated texture is uploaded to the GPU.
  70. Used for initialising the shader from the CPU. */
  71. ETLM_WRITE_ONLY
  72. };
  73. //! Interface of a Video Driver dependent Texture.
  74. /** An ITexture is created by an IVideoDriver by using IVideoDriver::addTexture
  75. or IVideoDriver::getTexture. After that, the texture may only be used by this
  76. VideoDriver. As you can imagine, textures of the DirectX and the OpenGL device
  77. will, e.g., not be compatible. An exception is the Software device and the
  78. NULL device, their textures are compatible. If you try to use a texture
  79. created by one device with an other device, the device will refuse to do that
  80. and write a warning or an error message to the output buffer.
  81. */
  82. class ITexture : public virtual IReferenceCounted
  83. {
  84. public:
  85. //! constructor
  86. ITexture(const io::path& name) : NamedPath(name)
  87. {
  88. }
  89. //! Lock function.
  90. /** Locks the Texture and returns a pointer to access the
  91. pixels. After lock() has been called and all operations on the pixels
  92. are done, you must call unlock().
  93. Locks are not accumulating, hence one unlock will do for an arbitrary
  94. number of previous locks. You should avoid locking different levels without
  95. unlocking inbetween, though, because only the last level locked will be
  96. unlocked.
  97. The size of the i-th mipmap level is defined as max(getSize().Width>>i,1)
  98. and max(getSize().Height>>i,1)
  99. \param mode Specifies what kind of changes to the locked texture are
  100. allowed. Unspecified behavior will arise if texture is written in read
  101. only mode or read from in write only mode.
  102. Support for this feature depends on the driver, so don't rely on the
  103. texture being write-protected when locking with read-only, etc.
  104. \param mipmapLevel Number of the mipmapLevel to lock. 0 is main texture.
  105. Non-existing levels will silently fail and return 0.
  106. \return Returns a pointer to the pixel data. The format of the pixel can
  107. be determined by using getColorFormat(). 0 is returned, if
  108. the texture cannot be locked. */
  109. virtual void* lock(E_TEXTURE_LOCK_MODE mode=ETLM_READ_WRITE, u32 mipmapLevel=0) = 0;
  110. //! Unlock function. Must be called after a lock() to the texture.
  111. /** One should avoid to call unlock more than once before another lock.
  112. The last locked mip level will be unlocked. */
  113. virtual void unlock() = 0;
  114. //! Get original size of the texture.
  115. /** The texture is usually scaled, if it was created with an unoptimal
  116. size. For example if the size was not a power of two. This method
  117. returns the size of the texture it had before it was scaled. Can be
  118. useful when drawing 2d images on the screen, which should have the
  119. exact size of the original texture. Use ITexture::getSize() if you want
  120. to know the real size it has now stored in the system.
  121. \return The original size of the texture. */
  122. virtual const core::dimension2d<u32>& getOriginalSize() const = 0;
  123. //! Get dimension (=size) of the texture.
  124. /** \return The size of the texture. */
  125. virtual const core::dimension2d<u32>& getSize() const = 0;
  126. //! Get driver type of texture.
  127. /** This is the driver, which created the texture. This method is used
  128. internally by the video devices, to check, if they may use a texture
  129. because textures may be incompatible between different devices.
  130. \return Driver type of texture. */
  131. virtual E_DRIVER_TYPE getDriverType() const = 0;
  132. //! Get the color format of texture.
  133. /** \return The color format of texture. */
  134. virtual ECOLOR_FORMAT getColorFormat() const = 0;
  135. //! Get pitch of the main texture (in bytes).
  136. /** The pitch is the amount of bytes used for a row of pixels in a
  137. texture.
  138. \return Pitch of texture in bytes. */
  139. virtual u32 getPitch() const = 0;
  140. //! Check whether the texture has MipMaps
  141. /** \return True if texture has MipMaps, else false. */
  142. virtual bool hasMipMaps() const { return false; }
  143. //! Returns if the texture has an alpha channel
  144. virtual bool hasAlpha() const {
  145. return getColorFormat () == video::ECF_A8R8G8B8 || getColorFormat () == video::ECF_A1R5G5B5;
  146. }
  147. //! Regenerates the mip map levels of the texture.
  148. /** Required after modifying the texture, usually after calling unlock().
  149. \param mipmapData Optional parameter to pass in image data which will be
  150. used instead of the previously stored or automatically generated mipmap
  151. data. The data has to be a continuous pixel data for all mipmaps until
  152. 1x1 pixel. Each mipmap has to be half the width and height of the previous
  153. level. At least one pixel will be always kept.*/
  154. virtual void regenerateMipMapLevels(void* mipmapData=0) = 0;
  155. //! Check whether the texture is a render target
  156. /** Render targets can be set as such in the video driver, in order to
  157. render a scene into the texture. Once unbound as render target, they can
  158. be used just as usual textures again.
  159. \return True if this is a render target, otherwise false. */
  160. virtual bool isRenderTarget() const { return false; }
  161. //! Get name of texture (in most cases this is the filename)
  162. const io::SNamedPath& getName() const { return NamedPath; }
  163. protected:
  164. //! Helper function, helps to get the desired texture creation format from the flags.
  165. /** \return Either ETCF_ALWAYS_32_BIT, ETCF_ALWAYS_16_BIT,
  166. ETCF_OPTIMIZED_FOR_QUALITY, or ETCF_OPTIMIZED_FOR_SPEED. */
  167. inline E_TEXTURE_CREATION_FLAG getTextureFormatFromFlags(u32 flags)
  168. {
  169. if (flags & ETCF_OPTIMIZED_FOR_SPEED)
  170. return ETCF_OPTIMIZED_FOR_SPEED;
  171. if (flags & ETCF_ALWAYS_16_BIT)
  172. return ETCF_ALWAYS_16_BIT;
  173. if (flags & ETCF_ALWAYS_32_BIT)
  174. return ETCF_ALWAYS_32_BIT;
  175. if (flags & ETCF_OPTIMIZED_FOR_QUALITY)
  176. return ETCF_OPTIMIZED_FOR_QUALITY;
  177. return ETCF_OPTIMIZED_FOR_SPEED;
  178. }
  179. io::SNamedPath NamedPath;
  180. };
  181. } // end namespace video
  182. } // end namespace irr
  183. #endif