Rework cl_video to use dyntextures.
[divverent/darkplaces.git] / gl_textures.c
1
2 #include "quakedef.h"
3 #include "image.h"
4 #include "jpeg.h"
5 #include "image_png.h"
6
7 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048", "maximum allowed texture size, can be used to reduce video memory usage, note: this is automatically reduced to match video card capabilities (such as 256 on 3Dfx cards before Voodoo4/5)"};
8 cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0", "reduces resolution of textures by powers of 2, for example 1 will halve width/height, reducing texture memory usage by 75%"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1", "bilinear filters images when scaling them up to power of 2 size (mode 1), looks better than glquake (mode 0)"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1", "0 = never upload textures until used, 1 = upload most textures before use (exceptions: rarely used skin colormap layers), 2 = upload all textures before use (can increase texture memory usage significantly)"};
11 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1", "anisotropic filtering quality (if supported by hardware), 1 sample (no anisotropy) and 8 sample (8 tap anisotropy) are recommended values"};
12 cvar_t gl_texturecompression = {CVAR_SAVE, "gl_texturecompression", "0", "whether to compress textures, a value of 0 disables compression (even if the individual cvars are 1), 1 enables fast (low quality) compression at startup, 2 enables slow (high quality) compression at startup"};
13 cvar_t gl_texturecompression_color = {CVAR_SAVE, "gl_texturecompression_color", "1", "whether to compress colormap (diffuse) textures"};
14 cvar_t gl_texturecompression_normal = {CVAR_SAVE, "gl_texturecompression_normal", "1", "whether to compress normalmap (normalmap) textures"};
15 cvar_t gl_texturecompression_gloss = {CVAR_SAVE, "gl_texturecompression_gloss", "1", "whether to compress glossmap (specular) textures"};
16 cvar_t gl_texturecompression_glow = {CVAR_SAVE, "gl_texturecompression_glow", "1", "whether to compress glowmap (luma) textures"};
17 cvar_t gl_texturecompression_2d = {CVAR_SAVE, "gl_texturecompression_2d", "1", "whether to compress 2d (hud/menu) textures other than the font"};
18 cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "1", "whether to compress lightmaps in q3bsp format levels"};
19 cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "1", "whether to compress deluxemaps in q3bsp format levels (only levels compiled with q3map2 -deluxe have these)"};
20 cvar_t gl_texturecompression_sky = {CVAR_SAVE, "gl_texturecompression_sky", "0", "whether to compress sky textures"};
21 cvar_t gl_texturecompression_lightcubemaps = {CVAR_SAVE, "gl_texturecompression_lightcubemaps", "1", "whether to compress light cubemaps (spotlights and other light projection images)"};
22
23 int             gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
24 int             gl_filter_mag = GL_LINEAR;
25
26
27 static mempool_t *texturemempool;
28
29 // note: this must not conflict with TEXF_ flags in r_textures.h
30 // cleared when a texture is uploaded
31 #define GLTEXF_UPLOAD           0x00010000
32 // bitmask for mismatch checking
33 #define GLTEXF_IMPORTANTBITS (0)
34 // set when image is uploaded and freed
35 #define GLTEXF_DESTROYED        0x00040000
36 // dynamic texture (treat texnum == 0 differently)
37 #define GLTEXF_DYNAMIC          0x00080000
38
39 typedef struct textypeinfo_s
40 {
41         int textype;
42         int inputbytesperpixel;
43         int internalbytesperpixel;
44         float glinternalbytesperpixel;
45         int glformat;
46         int glinternalformat;
47 }
48 textypeinfo_t;
49
50 static textypeinfo_t textype_palette                = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA   , 3};
51 static textypeinfo_t textype_rgba                   = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 3};
52 static textypeinfo_t textype_bgra                   = {TEXTYPE_BGRA   , 4, 4, 4.0f, GL_BGRA   , 3};
53 static textypeinfo_t textype_palette_alpha          = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA   , 4};
54 static textypeinfo_t textype_rgba_alpha             = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 4};
55 static textypeinfo_t textype_bgra_alpha             = {TEXTYPE_BGRA   , 4, 4, 4.0f, GL_BGRA   , 4};
56 static textypeinfo_t textype_palette_compress       = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB};
57 static textypeinfo_t textype_rgba_compress          = {TEXTYPE_RGBA   , 4, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB};
58 static textypeinfo_t textype_bgra_compress          = {TEXTYPE_BGRA   , 4, 4, 0.5f, GL_BGRA   , GL_COMPRESSED_RGB_ARB};
59 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB};
60 static textypeinfo_t textype_rgba_alpha_compress    = {TEXTYPE_RGBA   , 4, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB};
61 static textypeinfo_t textype_bgra_alpha_compress    = {TEXTYPE_BGRA   , 4, 4, 1.0f, GL_BGRA   , GL_COMPRESSED_RGBA_ARB};
62
63 #define GLTEXTURETYPE_1D 0
64 #define GLTEXTURETYPE_2D 1
65 #define GLTEXTURETYPE_3D 2
66 #define GLTEXTURETYPE_CUBEMAP 3
67
68 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
69 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
70 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
71 static int cubemapside[6] =
72 {
73         GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
74         GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
75         GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
76         GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
77         GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
78         GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
79 };
80
81 typedef struct gltexture_s
82 {
83         // this field is exposed to the R_GetTexture macro, for speed reasons
84         // (must be identical in rtexture_t)
85         int texnum; // GL texture slot number
86
87         // dynamic texture stuff [11/22/2007 Black]
88         // used to hold the texture number of dirty textures   
89         int dirtytexnum;
90         updatecallback_t updatecallback;
91         void *updatacallback_data;
92         // --- [11/22/2007 Black]
93
94         // pointer to texturepool (check this to see if the texture is allocated)
95         struct gltexturepool_s *pool;
96         // pointer to next texture in texturepool chain
97         struct gltexture_s *chain;
98         // name of the texture (this might be removed someday), no duplicates
99         char identifier[MAX_QPATH + 32];
100         // original data size in *inputtexels
101         int inputwidth, inputheight, inputdepth;
102         // copy of the original texture(s) supplied to the upload function, for
103         // delayed uploads (non-precached)
104         unsigned char *inputtexels;
105         // original data size in *inputtexels
106         int inputdatasize;
107         // flags supplied to the LoadTexture function
108         // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
109         int flags;
110         // pointer to one of the textype_ structs
111         textypeinfo_t *textype;
112         // one of the GLTEXTURETYPE_ values
113         int texturetype;
114         // palette if the texture is TEXTYPE_PALETTE
115         const unsigned int *palette;
116         // actual stored texture size after gl_picmip and gl_max_size are applied
117         // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
118         int tilewidth, tileheight, tiledepth;
119         // 1 or 6 depending on texturetype
120         int sides;
121         // bytes per pixel
122         int bytesperpixel;
123         // GL_RGB or GL_RGBA
124         int glformat;
125         // 3 or 4
126         int glinternalformat;
127 }
128 gltexture_t;
129
130 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
131
132 typedef struct gltexturepool_s
133 {
134         unsigned int sentinel;
135         struct gltexture_s *gltchain;
136         struct gltexturepool_s *next;
137 }
138 gltexturepool_t;
139
140 static gltexturepool_t *gltexturepoolchain = NULL;
141
142 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
143 static int resizebuffersize = 0;
144 static unsigned char *texturebuffer;
145 static int texturebuffersize = 0;
146
147 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
148 {
149         if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
150         {
151                 if (flags & TEXF_ALPHA)
152                 {
153                         switch(textype)
154                         {
155                         case TEXTYPE_PALETTE:
156                                 return &textype_palette_alpha_compress;
157                         case TEXTYPE_RGBA:
158                                 return &textype_rgba_alpha_compress;
159                         case TEXTYPE_BGRA:
160                                 return &textype_bgra_alpha_compress;
161                         default:
162                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
163                                 return NULL;
164                         }
165                 }
166                 else
167                 {
168                         switch(textype)
169                         {
170                         case TEXTYPE_PALETTE:
171                                 return &textype_palette_compress;
172                         case TEXTYPE_RGBA:
173                                 return &textype_rgba_compress;
174                         case TEXTYPE_BGRA:
175                                 return &textype_bgra_compress;
176                         default:
177                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
178                                 return NULL;
179                         }
180                 }
181         }
182         else
183         {
184                 if (flags & TEXF_ALPHA)
185                 {
186                         switch(textype)
187                         {
188                         case TEXTYPE_PALETTE:
189                                 return &textype_palette_alpha;
190                         case TEXTYPE_RGBA:
191                                 return &textype_rgba_alpha;
192                         case TEXTYPE_BGRA:
193                                 return &textype_bgra_alpha;
194                         default:
195                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
196                                 return NULL;
197                         }
198                 }
199                 else
200                 {
201                         switch(textype)
202                         {
203                         case TEXTYPE_PALETTE:
204                                 return &textype_palette;
205                         case TEXTYPE_RGBA:
206                                 return &textype_rgba;
207                         case TEXTYPE_BGRA:
208                                 return &textype_bgra;
209                         default:
210                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
211                                 return NULL;
212                         }
213                 }
214         }
215         return NULL; // this line only to hush compiler warnings
216 }
217
218 // dynamic texture code [11/22/2007 Black]
219 void R_MarkDirtyTexture(rtexture_t *rt) {
220         gltexture_t *glt = (gltexture_t*) rt;
221         if( !glt ) {
222                 return;
223         }
224
225         // dont do anything if the texture is already dirty (and make sure this *is* a dynamic texture after all!)
226         if( !glt->dirtytexnum && glt->flags & GLTEXF_DYNAMIC ) {
227                 glt->dirtytexnum = glt->texnum;
228                 // mark it as dirty, so R_RealGetTexture gets called
229                 glt->texnum = 0;
230         }
231 }
232
233 void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void *data) {
234         gltexture_t *glt = (gltexture_t*) rt;
235         if( !glt ) {
236                 return;
237         }
238
239         glt->flags |= GLTEXF_DYNAMIC;
240         glt->updatecallback = updatecallback;
241         glt->updatacallback_data = data;
242 }
243
244 static void R_UpdateDynamicTexture(gltexture_t *glt) {
245         glt->texnum = glt->dirtytexnum;
246         // reset dirtytexnum again (not dirty anymore)
247         glt->dirtytexnum = 0;
248         // TODO: now assert that t->texnum != 0 ?
249         if( glt->updatecallback ) {
250                 glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data );
251         }
252 }
253
254 static void R_UploadTexture(gltexture_t *t);
255
256 static void R_PrecacheTexture(gltexture_t *glt)
257 {
258         int precache;
259         precache = false;
260         if (glt->flags & TEXF_ALWAYSPRECACHE)
261                 precache = true;
262         else if (r_precachetextures.integer >= 2)
263                 precache = true;
264         else if (r_precachetextures.integer >= 1)
265                 if (glt->flags & TEXF_PRECACHE)
266                         precache = true;
267
268         if (precache)
269                 R_UploadTexture(glt);
270 }
271
272 int R_RealGetTexture(rtexture_t *rt)
273 {
274         if (rt)
275         {
276                 gltexture_t *glt;
277                 glt = (gltexture_t *)rt;
278                 if (glt->flags & GLTEXF_DYNAMIC)
279                         R_UpdateDynamicTexture(glt);
280                 if (glt->flags & GLTEXF_UPLOAD)
281                         R_UploadTexture(glt);
282
283                 return glt->texnum;
284         }
285         else
286                 return 0;
287 }
288
289 void R_FreeTexture(rtexture_t *rt)
290 {
291         gltexture_t *glt, **gltpointer;
292
293         glt = (gltexture_t *)rt;
294         if (glt == NULL)
295                 Host_Error("R_FreeTexture: texture == NULL");
296
297         for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
298         if (*gltpointer == glt)
299                 *gltpointer = glt->chain;
300         else
301                 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
302
303         if (glt->texnum)
304         {
305                 CHECKGLERROR
306                 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
307         }
308
309         if (glt->inputtexels)
310                 Mem_Free(glt->inputtexels);
311         Mem_Free(glt);
312 }
313
314 rtexturepool_t *R_AllocTexturePool(void)
315 {
316         gltexturepool_t *pool;
317         if (texturemempool == NULL)
318                 return NULL;
319         pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
320         if (pool == NULL)
321                 return NULL;
322         pool->next = gltexturepoolchain;
323         gltexturepoolchain = pool;
324         pool->sentinel = TEXTUREPOOL_SENTINEL;
325         return (rtexturepool_t *)pool;
326 }
327
328 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
329 {
330         gltexturepool_t *pool, **poolpointer;
331         if (rtexturepool == NULL)
332                 return;
333         if (*rtexturepool == NULL)
334                 return;
335         pool = (gltexturepool_t *)(*rtexturepool);
336         *rtexturepool = NULL;
337         if (pool->sentinel != TEXTUREPOOL_SENTINEL)
338                 Host_Error("R_FreeTexturePool: pool already freed");
339         for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
340         if (*poolpointer == pool)
341                 *poolpointer = pool->next;
342         else
343                 Host_Error("R_FreeTexturePool: pool not linked");
344         while (pool->gltchain)
345                 R_FreeTexture((rtexture_t *)pool->gltchain);
346         Mem_Free(pool);
347 }
348
349
350 typedef struct glmode_s
351 {
352         char *name;
353         int minification, magnification;
354 }
355 glmode_t;
356
357 static glmode_t modes[6] =
358 {
359         {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
360         {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
361         {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
362         {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
363         {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
364         {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
365 };
366
367 static void GL_TextureMode_f (void)
368 {
369         int i;
370         GLint oldbindtexnum;
371         gltexture_t *glt;
372         gltexturepool_t *pool;
373
374         if (Cmd_Argc() == 1)
375         {
376                 for (i = 0;i < 6;i++)
377                 {
378                         if (gl_filter_min == modes[i].minification)
379                         {
380                                 Con_Printf("%s\n", modes[i].name);
381                                 return;
382                         }
383                 }
384                 Con_Print("current filter is unknown???\n");
385                 return;
386         }
387
388         for (i = 0;i < 6;i++)
389                 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
390                         break;
391         if (i == 6)
392         {
393                 Con_Print("bad filter name\n");
394                 return;
395         }
396
397         gl_filter_min = modes[i].minification;
398         gl_filter_mag = modes[i].magnification;
399
400         // change all the existing mipmap texture objects
401         // FIXME: force renderer(/client/something?) restart instead?
402         CHECKGLERROR
403         for (pool = gltexturepoolchain;pool;pool = pool->next)
404         {
405                 for (glt = pool->gltchain;glt;glt = glt->chain)
406                 {
407                         // only update already uploaded images
408                         if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
409                         {
410                                 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
411                                 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
412                                 if (glt->flags & TEXF_MIPMAP)
413                                 {
414                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
415                                 }
416                                 else
417                                 {
418                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
419                                 }
420                                 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
421                                 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
422                         }
423                 }
424         }
425 }
426
427 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
428 {
429         int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
430
431         if (gl_max_size.integer > gl_max_texture_size)
432                 Cvar_SetValue("gl_max_size", gl_max_texture_size);
433
434         switch (texturetype)
435         {
436         default:
437         case GLTEXTURETYPE_1D:
438         case GLTEXTURETYPE_2D:
439                 maxsize = gl_max_texture_size;
440                 break;
441         case GLTEXTURETYPE_3D:
442                 maxsize = gl_max_3d_texture_size;
443                 break;
444         case GLTEXTURETYPE_CUBEMAP:
445                 maxsize = gl_max_cube_map_texture_size;
446                 break;
447         }
448
449         if (flags & TEXF_PICMIP)
450         {
451                 maxsize = min(maxsize, gl_max_size.integer);
452                 picmip = gl_picmip.integer;
453         }
454
455         if (outwidth)
456         {
457                 if (gl_support_arb_texture_non_power_of_two)
458                         width2 = min(inwidth >> picmip, maxsize);
459                 else
460                 {
461                         for (width2 = 1;width2 < inwidth;width2 <<= 1);
462                         for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
463                 }
464                 *outwidth = max(1, width2);
465         }
466         if (outheight)
467         {
468                 if (gl_support_arb_texture_non_power_of_two)
469                         height2 = min(inheight >> picmip, maxsize);
470                 else
471                 {
472                         for (height2 = 1;height2 < inheight;height2 <<= 1);
473                         for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
474                 }
475                 *outheight = max(1, height2);
476         }
477         if (outdepth)
478         {
479                 if (gl_support_arb_texture_non_power_of_two)
480                         depth2 = min(indepth >> picmip, maxsize);
481                 else
482                 {
483                         for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
484                         for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
485                 }
486                 *outdepth = max(1, depth2);
487         }
488 }
489
490
491 static int R_CalcTexelDataSize (gltexture_t *glt)
492 {
493         int width2, height2, depth2, size;
494
495         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
496
497         size = width2 * height2 * depth2;
498
499         if (glt->flags & TEXF_MIPMAP)
500         {
501                 while (width2 > 1 || height2 > 1 || depth2 > 1)
502                 {
503                         if (width2 > 1)
504                                 width2 >>= 1;
505                         if (height2 > 1)
506                                 height2 >>= 1;
507                         if (depth2 > 1)
508                                 depth2 >>= 1;
509                         size += width2 * height2 * depth2;
510                 }
511         }
512
513         return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
514 }
515
516 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
517 {
518         int glsize;
519         int isloaded;
520         int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
521         int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
522         gltexture_t *glt;
523         gltexturepool_t *pool;
524         if (printeach)
525                 Con_Print("glsize input loaded mip alpha name\n");
526         for (pool = gltexturepoolchain;pool;pool = pool->next)
527         {
528                 pooltotal = 0;
529                 pooltotalt = 0;
530                 pooltotalp = 0;
531                 poolloaded = 0;
532                 poolloadedt = 0;
533                 poolloadedp = 0;
534                 for (glt = pool->gltchain;glt;glt = glt->chain)
535                 {
536                         glsize = R_CalcTexelDataSize(glt);
537                         isloaded = !(glt->flags & GLTEXF_UPLOAD);
538                         pooltotal++;
539                         pooltotalt += glsize;
540                         pooltotalp += glt->inputdatasize;
541                         if (isloaded)
542                         {
543                                 poolloaded++;
544                                 poolloadedt += glsize;
545                                 poolloadedp += glt->inputdatasize;
546                         }
547                         if (printeach)
548                                 Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', isloaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier);
549                 }
550                 if (printpool)
551                         Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", pool, pooltotal, pooltotalt / 1048576.0, pooltotalp / 1048576.0, poolloaded, poolloadedt / 1048576.0, poolloadedp / 1048576.0, pooltotal - poolloaded, (pooltotalt - poolloadedt) / 1048576.0, (pooltotalp - poolloadedp) / 1048576.0);
552                 sumtotal += pooltotal;
553                 sumtotalt += pooltotalt;
554                 sumtotalp += pooltotalp;
555                 sumloaded += poolloaded;
556                 sumloadedt += poolloadedt;
557                 sumloadedp += poolloadedp;
558         }
559         if (printtotal)
560                 Con_Printf("textures total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", sumtotal, sumtotalt / 1048576.0, sumtotalp / 1048576.0, sumloaded, sumloadedt / 1048576.0, sumloadedp / 1048576.0, sumtotal - sumloaded, (sumtotalt - sumloadedt) / 1048576.0, (sumtotalp - sumloadedp) / 1048576.0);
561 }
562
563 static void R_TextureStats_f(void)
564 {
565         R_TextureStats_Print(true, true, true);
566 }
567
568 static void r_textures_start(void)
569 {
570         // LordHavoc: allow any alignment
571         CHECKGLERROR
572         qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
573         qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
574
575         texturemempool = Mem_AllocPool("texture management", 0, NULL);
576
577         // Disable JPEG screenshots if the DLL isn't loaded
578         if (! JPEG_OpenLibrary ())
579                 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
580         // TODO: support png screenshots?
581         PNG_OpenLibrary ();
582 }
583
584 static void r_textures_shutdown(void)
585 {
586         rtexturepool_t *temp;
587
588         JPEG_CloseLibrary ();
589
590         while(gltexturepoolchain)
591         {
592                 temp = (rtexturepool_t *) gltexturepoolchain;
593                 R_FreeTexturePool(&temp);
594         }
595
596         resizebuffersize = 0;
597         texturebuffersize = 0;
598         resizebuffer = NULL;
599         colorconvertbuffer = NULL;
600         texturebuffer = NULL;
601         Mem_FreePool(&texturemempool);
602 }
603
604 static void r_textures_newmap(void)
605 {
606 }
607
608 void R_Textures_Init (void)
609 {
610         Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
611         Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
612         Cvar_RegisterVariable (&gl_max_size);
613         Cvar_RegisterVariable (&gl_picmip);
614         Cvar_RegisterVariable (&r_lerpimages);
615         Cvar_RegisterVariable (&r_precachetextures);
616         Cvar_RegisterVariable (&gl_texture_anisotropy);
617         Cvar_RegisterVariable (&gl_texturecompression);
618         Cvar_RegisterVariable (&gl_texturecompression_color);
619         Cvar_RegisterVariable (&gl_texturecompression_normal);
620         Cvar_RegisterVariable (&gl_texturecompression_gloss);
621         Cvar_RegisterVariable (&gl_texturecompression_glow);
622         Cvar_RegisterVariable (&gl_texturecompression_2d);
623         Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
624         Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
625         Cvar_RegisterVariable (&gl_texturecompression_sky);
626         Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
627
628         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
629 }
630
631 void R_Textures_Frame (void)
632 {
633         static int old_aniso = 0;
634
635         // could do procedural texture animation here, if we keep track of which
636         // textures were accessed this frame...
637
638         // free the resize buffers
639         resizebuffersize = 0;
640         if (resizebuffer)
641         {
642                 Mem_Free(resizebuffer);
643                 resizebuffer = NULL;
644         }
645         if (colorconvertbuffer)
646         {
647                 Mem_Free(colorconvertbuffer);
648                 colorconvertbuffer = NULL;
649         }
650
651         if (old_aniso != gl_texture_anisotropy.integer)
652         {
653                 gltexture_t *glt;
654                 gltexturepool_t *pool;
655                 GLint oldbindtexnum;
656
657                 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
658
659                 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
660
661                 CHECKGLERROR
662                 for (pool = gltexturepoolchain;pool;pool = pool->next)
663                 {
664                         for (glt = pool->gltchain;glt;glt = glt->chain)
665                         {
666                                 // only update already uploaded images
667                                 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
668                                 {
669                                         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
670
671                                         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
672                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
673
674                                         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
675                                 }
676                         }
677                 }
678         }
679 }
680
681 void R_MakeResizeBufferBigger(int size)
682 {
683         if (resizebuffersize < size)
684         {
685                 resizebuffersize = size;
686                 if (resizebuffer)
687                         Mem_Free(resizebuffer);
688                 if (colorconvertbuffer)
689                         Mem_Free(colorconvertbuffer);
690                 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
691                 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
692                 if (!resizebuffer || !colorconvertbuffer)
693                         Host_Error("R_Upload: out of memory");
694         }
695 }
696
697 static void GL_SetupTextureParameters(int flags, int texturetype)
698 {
699         int textureenum = gltexturetypeenums[texturetype];
700         int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
701
702         CHECKGLERROR
703
704         if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
705         {
706                 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
707                 if (gl_texture_anisotropy.integer != aniso)
708                         Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
709                 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
710         }
711         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
712         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
713         if (gltexturetypedimensions[texturetype] >= 3)
714         {
715                 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
716         }
717
718         CHECKGLERROR
719         if (flags & TEXF_FORCENEAREST)
720         {
721                 if (flags & TEXF_MIPMAP)
722                 {
723                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
724                 }
725                 else
726                 {
727                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
728                 }
729                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
730         }
731         else if (flags & TEXF_FORCELINEAR)
732         {
733                 if (flags & TEXF_MIPMAP)
734                 {
735                         if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
736                         {
737                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
738                         }
739                         else
740                         {
741                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
742                         }
743                 }
744                 else
745                 {
746                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
747                 }
748                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
749         }
750         else
751         {
752                 if (flags & TEXF_MIPMAP)
753                 {
754                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
755                 }
756                 else
757                 {
758                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
759                 }
760                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
761         }
762
763         CHECKGLERROR
764 }
765
766 static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
767 {
768         int i, mip, width, height, depth;
769         GLint oldbindtexnum;
770         const unsigned char *prevbuffer;
771         prevbuffer = data;
772
773         CHECKGLERROR
774
775         // we need to restore the texture binding after finishing the upload
776         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
777         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
778
779         // these are rounded up versions of the size to do better resampling
780         if (gl_support_arb_texture_non_power_of_two)
781         {
782                 width = glt->inputwidth;
783                 height = glt->inputheight;
784                 depth = glt->inputdepth;
785         }
786         else
787         {
788                 for (width  = 1;width  < glt->inputwidth ;width  <<= 1);
789                 for (height = 1;height < glt->inputheight;height <<= 1);
790                 for (depth  = 1;depth  < glt->inputdepth ;depth  <<= 1);
791         }
792
793         R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
794         R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
795
796         if (prevbuffer == NULL)
797         {
798                 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
799                 prevbuffer = resizebuffer;
800         }
801         else if (glt->textype->textype == TEXTYPE_PALETTE)
802         {
803                 // promote paletted to RGBA, so we only have to worry about RGB and
804                 // RGBA in the rest of this code
805                 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
806                 prevbuffer = colorconvertbuffer;
807         }
808
809         if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
810         {
811                 // update a portion of the image
812                 switch(glt->texturetype)
813                 {
814                 case GLTEXTURETYPE_1D:
815                         qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
816                         break;
817                 case GLTEXTURETYPE_2D:
818                         qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
819                         break;
820                 case GLTEXTURETYPE_3D:
821                         qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
822                         break;
823                 default:
824                         Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
825                         break;
826                 }
827         }
828         else
829         {
830                 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
831                         Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
832
833                 // upload the image for the first time
834                 glt->flags &= ~GLTEXF_UPLOAD;
835
836                 // cubemaps contain multiple images and thus get processed a bit differently
837                 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
838                 {
839                         if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
840                         {
841                                 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
842                                 prevbuffer = resizebuffer;
843                         }
844                         // picmip/max_size
845                         while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
846                         {
847                                 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
848                                 prevbuffer = resizebuffer;
849                         }
850                 }
851                 mip = 0;
852                 if (gl_support_texture_compression)
853                 {
854                         if (gl_texturecompression.integer >= 2)
855                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
856                         else
857                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
858                         CHECKGLERROR
859                 }
860                 switch(glt->texturetype)
861                 {
862                 case GLTEXTURETYPE_1D:
863                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
864                         if (glt->flags & TEXF_MIPMAP)
865                         {
866                                 while (width > 1 || height > 1 || depth > 1)
867                                 {
868                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
869                                         prevbuffer = resizebuffer;
870                                         qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
871                                 }
872                         }
873                         break;
874                 case GLTEXTURETYPE_2D:
875                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
876                         if (glt->flags & TEXF_MIPMAP)
877                         {
878                                 while (width > 1 || height > 1 || depth > 1)
879                                 {
880                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
881                                         prevbuffer = resizebuffer;
882                                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
883                                 }
884                         }
885                         break;
886                 case GLTEXTURETYPE_3D:
887                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
888                         if (glt->flags & TEXF_MIPMAP)
889                         {
890                                 while (width > 1 || height > 1 || depth > 1)
891                                 {
892                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
893                                         prevbuffer = resizebuffer;
894                                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
895                                 }
896                         }
897                         break;
898                 case GLTEXTURETYPE_CUBEMAP:
899                         // convert and upload each side in turn,
900                         // from a continuous block of input texels
901                         texturebuffer = prevbuffer;
902                         for (i = 0;i < 6;i++)
903                         {
904                                 prevbuffer = texturebuffer;
905                                 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
906                                 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
907                                 {
908                                         Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
909                                         prevbuffer = resizebuffer;
910                                 }
911                                 // picmip/max_size
912                                 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
913                                 {
914                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
915                                         prevbuffer = resizebuffer;
916                                 }
917                                 mip = 0;
918                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
919                                 if (glt->flags & TEXF_MIPMAP)
920                                 {
921                                         while (width > 1 || height > 1 || depth > 1)
922                                         {
923                                                 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
924                                                 prevbuffer = resizebuffer;
925                                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
926                                         }
927                                 }
928                         }
929                         break;
930                 }
931                 GL_SetupTextureParameters(glt->flags, glt->texturetype);
932         }
933         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
934 }
935
936 static void R_UploadTexture (gltexture_t *glt)
937 {
938         if (!(glt->flags & GLTEXF_UPLOAD))
939                 return;
940
941         CHECKGLERROR
942         qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
943         R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
944         if (glt->inputtexels)
945         {
946                 Mem_Free(glt->inputtexels);
947                 glt->inputtexels = NULL;
948                 glt->flags |= GLTEXF_DESTROYED;
949         }
950         else if (glt->flags & GLTEXF_DESTROYED)
951                 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
952 }
953
954 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, int textype, int texturetype, const unsigned char *data, const unsigned int *palette)
955 {
956         int i, size;
957         gltexture_t *glt;
958         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
959         textypeinfo_t *texinfo;
960
961         if (cls.state == ca_dedicated)
962                 return NULL;
963
964         if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
965         {
966                 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
967                 return NULL;
968         }
969         if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
970         {
971                 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
972                 return NULL;
973         }
974
975         texinfo = R_GetTexTypeInfo(textype, flags);
976         size = width * height * depth * sides * texinfo->inputbytesperpixel;
977         if (size < 1)
978         {
979                 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
980                 return NULL;
981         }
982
983         // clear the alpha flag if the texture has no transparent pixels
984         switch(textype)
985         {
986         case TEXTYPE_PALETTE:
987                 if (flags & TEXF_ALPHA)
988                 {
989                         flags &= ~TEXF_ALPHA;
990                         if (data)
991                         {
992                                 for (i = 0;i < size;i++)
993                                 {
994                                         if (((unsigned char *)&palette[data[i]])[3] < 255)
995                                         {
996                                                 flags |= TEXF_ALPHA;
997                                                 break;
998                                         }
999                                 }
1000                         }
1001                 }
1002                 break;
1003         case TEXTYPE_RGBA:
1004         case TEXTYPE_BGRA:
1005                 if (flags & TEXF_ALPHA)
1006                 {
1007                         flags &= ~TEXF_ALPHA;
1008                         if (data)
1009                         {
1010                                 for (i = 3;i < size;i += 4)
1011                                 {
1012                                         if (data[i] < 255)
1013                                         {
1014                                                 flags |= TEXF_ALPHA;
1015                                                 break;
1016                                         }
1017                                 }
1018                         }
1019                 }
1020                 break;
1021         default:
1022                 Host_Error("R_LoadTexture: unknown texture type");
1023         }
1024
1025         glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1026         if (identifier)
1027                 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1028         glt->pool = pool;
1029         glt->chain = pool->gltchain;
1030         pool->gltchain = glt;
1031         glt->inputwidth = width;
1032         glt->inputheight = height;
1033         glt->inputdepth = depth;
1034         glt->flags = flags | GLTEXF_UPLOAD;
1035         glt->textype = texinfo;
1036         glt->texturetype = texturetype;
1037         glt->inputdatasize = size;
1038         glt->palette = palette;
1039         glt->glinternalformat = texinfo->glinternalformat;
1040         glt->glformat = texinfo->glformat;
1041         glt->bytesperpixel = texinfo->internalbytesperpixel;
1042         glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1043         glt->texnum = -1;
1044         // init the dynamic texture attributes, too [11/22/2007 Black]
1045         glt->dirtytexnum = 0;
1046         glt->updatecallback = NULL;
1047         glt->updatacallback_data = NULL;
1048
1049         if (data)
1050         {
1051                 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1052                 memcpy(glt->inputtexels, data, size);
1053         }
1054         else
1055                 glt->inputtexels = NULL;
1056
1057         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1058         R_PrecacheTexture(glt);
1059
1060         return (rtexture_t *)glt;
1061 }
1062
1063 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1064 {
1065         return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1066 }
1067
1068 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1069 {
1070         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1071 }
1072
1073 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1074 {
1075         return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1076 }
1077
1078 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1079 {
1080         return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1081 }
1082
1083 int R_TextureHasAlpha(rtexture_t *rt)
1084 {
1085         return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1086 }
1087
1088 int R_TextureWidth(rtexture_t *rt)
1089 {
1090         return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1091 }
1092
1093 int R_TextureHeight(rtexture_t *rt)
1094 {
1095         return rt ? ((gltexture_t *)rt)->inputheight : 0;
1096 }
1097
1098 void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, int width, int height)
1099 {
1100         gltexture_t *glt;
1101         if (rt == NULL)
1102                 Host_Error("R_UpdateTexture: no texture supplied");
1103         if (data == NULL)
1104                 Host_Error("R_UpdateTexture: no data supplied");
1105         glt = (gltexture_t *)rt;
1106
1107         // we need it to be uploaded before we can update a part of it
1108         if (glt->flags & GLTEXF_UPLOAD)
1109                 R_UploadTexture(glt);
1110
1111         // update part of the texture
1112         R_Upload(glt, data, x, y, 0, width, height, 1);
1113 }
1114