Image.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. /*
  21. ====================================================================
  22. IMAGE
  23. idImage have a one to one correspondance with GL/DX/GCM textures.
  24. No texture is ever used that does not have a corresponding idImage.
  25. ====================================================================
  26. */
  27. static const int MAX_TEXTURE_LEVELS = 14;
  28. // How is this texture used? Determines the storage and color format
  29. typedef enum {
  30. TD_SPECULAR, // may be compressed, and always zeros the alpha channel
  31. TD_DIFFUSE, // may be compressed
  32. TD_DEFAULT, // generic RGBA texture (particles, etc...)
  33. TD_BUMP, // may be compressed with 8 bit lookup
  34. TD_FONT, // Font image
  35. TD_LIGHT, // Light image
  36. TD_LOOKUP_TABLE_MONO, // Mono lookup table (including alpha)
  37. TD_LOOKUP_TABLE_ALPHA, // Alpha lookup table with a white color channel
  38. TD_LOOKUP_TABLE_RGB1, // RGB lookup table with a solid white alpha
  39. TD_LOOKUP_TABLE_RGBA, // RGBA lookup table
  40. TD_COVERAGE, // coverage map for fill depth pass when YCoCG is used
  41. TD_DEPTH, // depth buffer copy for motion blur
  42. } textureUsage_t;
  43. typedef enum {
  44. CF_2D, // not a cube map
  45. CF_NATIVE, // _px, _nx, _py, etc, directly sent to GL
  46. CF_CAMERA // _forward, _back, etc, rotated and flipped as needed before sending to GL
  47. } cubeFiles_t;
  48. #include "ImageOpts.h"
  49. #include "BinaryImage.h"
  50. #define MAX_IMAGE_NAME 256
  51. class idImage {
  52. public:
  53. idImage( const char * name );
  54. const char * GetName() const { return imgName; }
  55. // Makes this image active on the current GL texture unit.
  56. // automatically enables or disables cube mapping
  57. // May perform file loading if the image was not preloaded.
  58. void Bind();
  59. // Should be called at least once
  60. void SetSamplerState( textureFilter_t tf, textureRepeat_t tr );
  61. // used by callback functions to specify the actual data
  62. // data goes from the bottom to the top line of the image, as OpenGL expects it
  63. // These perform an implicit Bind() on the current texture unit
  64. // FIXME: should we implement cinematics this way, instead of with explicit calls?
  65. void GenerateImage( const byte *pic, int width, int height,
  66. textureFilter_t filter, textureRepeat_t repeat, textureUsage_t usage );
  67. void GenerateCubeImage( const byte *pic[6], int size,
  68. textureFilter_t filter, textureUsage_t usage );
  69. void CopyFramebuffer( int x, int y, int width, int height );
  70. void CopyDepthbuffer( int x, int y, int width, int height );
  71. void UploadScratch( const byte *pic, int width, int height );
  72. // estimates size of the GL image based on dimensions and storage type
  73. int StorageSize() const;
  74. // print a one line summary of the image
  75. void Print() const;
  76. // check for changed timestamp on disk and reload if necessary
  77. void Reload( bool force );
  78. void AddReference() { refCount++; };
  79. void MakeDefault(); // fill with a grid pattern
  80. const idImageOpts & GetOpts() const { return opts; }
  81. int GetUploadWidth() const { return opts.width; }
  82. int GetUploadHeight() const { return opts.height; }
  83. void SetReferencedOutsideLevelLoad() { referencedOutsideLevelLoad = true; }
  84. void SetReferencedInsideLevelLoad() { levelLoadReferenced = true; }
  85. void ActuallyLoadImage( bool fromBackEnd );
  86. //---------------------------------------------
  87. // Platform specific implementations
  88. //---------------------------------------------
  89. void AllocImage( const idImageOpts &imgOpts, textureFilter_t filter, textureRepeat_t repeat );
  90. // Deletes the texture object, but leaves the structure so it can be reloaded
  91. // or resized.
  92. void PurgeImage();
  93. // z is 0 for 2D textures, 0 - 5 for cube maps, and 0 - uploadDepth for 3D textures. Only
  94. // one plane at a time of 3D textures can be uploaded. The data is assumed to be correct for
  95. // the format, either bytes, halfFloats, floats, or DXT compressed. The data is assumed to
  96. // be in OpenGL RGBA format, the consoles may have to reorganize. pixelPitch is only needed
  97. // when updating from a source subrect. Width, height, and dest* are always in pixels, so
  98. // they must be a multiple of four for dxt data.
  99. void SubImageUpload( int mipLevel, int destX, int destY, int destZ,
  100. int width, int height, const void * data,
  101. int pixelPitch = 0 ) const;
  102. // SetPixel is assumed to be a fast memory write on consoles, degenerating to a
  103. // SubImageUpload on PCs. Used to update the page mapping images.
  104. // We could remove this now, because the consoles don't use the intermediate page mapping
  105. // textures now that they can pack everything into the virtual page table images.
  106. void SetPixel( int mipLevel, int x, int y, const void * data, int dataSize );
  107. // some scratch images are dynamically resized based on the display window size. This
  108. // simply purges the image and recreates it if the sizes are different, so it should not be
  109. // done under any normal circumstances, and probably not at all on consoles.
  110. void Resize( int width, int height );
  111. bool IsCompressed() const { return ( opts.format == FMT_DXT1 || opts.format == FMT_DXT5 ); }
  112. void SetTexParameters(); // update aniso and trilinear
  113. bool IsLoaded() const { return texnum != TEXTURE_NOT_LOADED; }
  114. static void GetGeneratedName( idStr &_name, const textureUsage_t &_usage, const cubeFiles_t &_cube );
  115. private:
  116. friend class idImageManager;
  117. void AllocImage();
  118. void DeriveOpts();
  119. // parameters that define this image
  120. idStr imgName; // game path, including extension (except for cube maps), may be an image program
  121. cubeFiles_t cubeFiles; // If this is a cube map, and if so, what kind
  122. void (*generatorFunction)( idImage *image ); // NULL for files
  123. textureUsage_t usage; // Used to determine the type of compression to use
  124. idImageOpts opts; // Parameters that determine the storage method
  125. // Sampler settings
  126. textureFilter_t filter;
  127. textureRepeat_t repeat;
  128. bool referencedOutsideLevelLoad;
  129. bool levelLoadReferenced; // for determining if it needs to be purged
  130. bool defaulted; // true if the default image was generated because a file couldn't be loaded
  131. ID_TIME_T sourceFileTime; // the most recent of all images used in creation, for reloadImages command
  132. ID_TIME_T binaryFileTime; // the time stamp of the binary file
  133. int refCount; // overall ref count
  134. static const GLuint TEXTURE_NOT_LOADED = 0xFFFFFFFF;
  135. GLuint texnum; // gl texture binding
  136. // we could derive these in subImageUpload each time if necessary
  137. GLuint internalFormat;
  138. GLuint dataFormat;
  139. GLuint dataType;
  140. };
  141. ID_INLINE idImage::idImage( const char * name ) : imgName( name ) {
  142. texnum = TEXTURE_NOT_LOADED;
  143. internalFormat = 0;
  144. dataFormat = 0;
  145. dataType = 0;
  146. generatorFunction = NULL;
  147. filter = TF_DEFAULT;
  148. repeat = TR_REPEAT;
  149. usage = TD_DEFAULT;
  150. cubeFiles = CF_2D;
  151. referencedOutsideLevelLoad = false;
  152. levelLoadReferenced = false;
  153. defaulted = false;
  154. sourceFileTime = FILE_NOT_FOUND_TIMESTAMP;
  155. binaryFileTime = FILE_NOT_FOUND_TIMESTAMP;
  156. refCount = 0;
  157. }
  158. // data is RGBA
  159. void R_WriteTGA( const char *filename, const byte *data, int width, int height, bool flipVertical = false, const char * basePath = "fs_savepath" );
  160. // data is in top-to-bottom raster order unless flipVertical is set
  161. class idImageManager {
  162. public:
  163. idImageManager()
  164. {
  165. insideLevelLoad = false;
  166. preloadingMapImages = false;
  167. }
  168. void Init();
  169. void Shutdown();
  170. // If the exact combination of parameters has been asked for already, an existing
  171. // image will be returned, otherwise a new image will be created.
  172. // Be careful not to use the same image file with different filter / repeat / etc parameters
  173. // if possible, because it will cause a second copy to be loaded.
  174. // If the load fails for any reason, the image will be filled in with the default
  175. // grid pattern.
  176. // Will automatically execute image programs if needed.
  177. idImage * ImageFromFile( const char *name,
  178. textureFilter_t filter, textureRepeat_t repeat, textureUsage_t usage, cubeFiles_t cubeMap = CF_2D );
  179. // look for a loaded image, whatever the parameters
  180. idImage * GetImage( const char *name ) const;
  181. // look for a loaded image, whatever the parameters
  182. idImage * GetImageWithParameters( const char *name, textureFilter_t filter, textureRepeat_t repeat, textureUsage_t usage, cubeFiles_t cubeMap ) const;
  183. // The callback will be issued immediately, and later if images are reloaded or vid_restart
  184. // The callback function should call one of the idImage::Generate* functions to fill in the data
  185. idImage * ImageFromFunction( const char *name, void (*generatorFunction)( idImage *image ));
  186. // scratch images are for internal renderer use. ScratchImage names should always begin with an underscore
  187. idImage * ScratchImage( const char *name, idImageOpts *imgOpts, textureFilter_t filter, textureRepeat_t repeat, textureUsage_t usage );
  188. // purges all the images before a vid_restart
  189. void PurgeAllImages();
  190. // reloads all apropriate images after a vid_restart
  191. void ReloadImages( bool all );
  192. // unbind all textures from all texture units
  193. void UnbindAll();
  194. // disable the active texture unit
  195. void BindNull();
  196. // Called only by renderSystem::BeginLevelLoad
  197. void BeginLevelLoad();
  198. // Called only by renderSystem::EndLevelLoad
  199. void EndLevelLoad();
  200. void Preload( const idPreloadManifest &manifest, const bool & mapPreload );
  201. // Loads unloaded level images
  202. int LoadLevelImages( bool pacifier );
  203. // used to clear and then write the dds conversion batch file
  204. void StartBuild();
  205. void FinishBuild( bool removeDups = false );
  206. void PrintMemInfo( MemInfo_t *mi );
  207. // built-in images
  208. void CreateIntrinsicImages();
  209. idImage * defaultImage;
  210. idImage * flatNormalMap; // 128 128 255 in all pixels
  211. idImage * alphaNotchImage; // 2x1 texture with just 1110 and 1111 with point sampling
  212. idImage * whiteImage; // full of 0xff
  213. idImage * blackImage; // full of 0x00
  214. idImage * noFalloffImage; // all 255, but zero clamped
  215. idImage * fogImage; // increasing alpha is denser fog
  216. idImage * fogEnterImage; // adjust fogImage alpha based on terminator plane
  217. idImage * scratchImage;
  218. idImage * scratchImage2;
  219. idImage * accumImage;
  220. idImage * currentRenderImage; // for SS_POST_PROCESS shaders
  221. idImage * currentDepthImage; // for motion blur
  222. idImage * originalCurrentRenderImage; // currentRenderImage before any changes for stereo rendering
  223. idImage * loadingIconImage; // loading icon must exist always
  224. idImage * hellLoadingIconImage; // loading icon must exist always
  225. //--------------------------------------------------------
  226. idImage * AllocImage( const char *name );
  227. idImage * AllocStandaloneImage( const char *name );
  228. bool ExcludePreloadImage( const char *name );
  229. idList<idImage*, TAG_IDLIB_LIST_IMAGE> images;
  230. idHashIndex imageHash;
  231. bool insideLevelLoad; // don't actually load images now
  232. bool preloadingMapImages; // unless this is set
  233. };
  234. extern idImageManager *globalImages; // pointer to global list for the rest of the system
  235. int MakePowerOfTwo( int num );
  236. /*
  237. ====================================================================
  238. IMAGEPROCESS
  239. FIXME: make an "imageBlock" type to hold byte*,width,height?
  240. ====================================================================
  241. */
  242. byte *R_Dropsample( const byte *in, int inwidth, int inheight, int outwidth, int outheight );
  243. byte *R_ResampleTexture( const byte *in, int inwidth, int inheight, int outwidth, int outheight );
  244. byte *R_MipMapWithAlphaSpecularity( const byte *in, int width, int height );
  245. byte *R_MipMapWithGamma( const byte *in, int width, int height );
  246. byte *R_MipMap( const byte *in, int width, int height );
  247. // these operate in-place on the provided pixels
  248. void R_BlendOverTexture( byte *data, int pixelCount, const byte blend[4] );
  249. void R_HorizontalFlip( byte *data, int width, int height );
  250. void R_VerticalFlip( byte *data, int width, int height );
  251. void R_RotatePic( byte *data, int width );
  252. /*
  253. ====================================================================
  254. IMAGEFILES
  255. ====================================================================
  256. */
  257. void R_LoadImage( const char *name, byte **pic, int *width, int *height, ID_TIME_T *timestamp, bool makePowerOf2 );
  258. // pic is in top to bottom raster format
  259. bool R_LoadCubeImages( const char *cname, cubeFiles_t extensions, byte *pic[6], int *size, ID_TIME_T *timestamp );
  260. /*
  261. ====================================================================
  262. IMAGEPROGRAM
  263. ====================================================================
  264. */
  265. void R_LoadImageProgram( const char *name, byte **pic, int *width, int *height, ID_TIME_T *timestamp, textureUsage_t * usage = NULL );
  266. const char *R_ParsePastImageProgram( idLexer &src );