DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

Image.h (14412B)


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