6 #include "intoverflow.h"
9 #define GL_TEXTURE_3D 0x806F
12 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048", "maximum allowed texture size, can be used to reduce video memory usage, limited by hardware capabilities (typically 2048, 4096, or 8192)"};
13 cvar_t gl_max_lightmapsize = {CVAR_SAVE, "gl_max_lightmapsize", "1024", "maximum allowed texture size for lightmap textures, use larger values to improve rendering speed, as long as there is enough video memory available (setting it too high for the hardware will cause very bad performance)"};
14 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%"};
15 cvar_t gl_picmip_world = {CVAR_SAVE, "gl_picmip_world", "0", "extra picmip level for world textures (may be negative, which will then reduce gl_picmip for these)"};
16 cvar_t r_picmipworld = {CVAR_SAVE, "r_picmipworld", "1", "whether gl_picmip shall apply to world textures too (setting this to 0 is a shorthand for gl_picmip_world -9999999)"};
17 cvar_t gl_picmip_sprites = {CVAR_SAVE, "gl_picmip_sprites", "0", "extra picmip level for sprite textures (may be negative, which will then reduce gl_picmip for these)"};
18 cvar_t r_picmipsprites = {CVAR_SAVE, "r_picmipsprites", "1", "make gl_picmip affect sprites too (saves some graphics memory in sprite heavy games) (setting this to 0 is a shorthand for gl_picmip_sprites -9999999)"};
19 cvar_t gl_picmip_other = {CVAR_SAVE, "gl_picmip_other", "0", "extra picmip level for other textures (may be negative, which will then reduce gl_picmip for these)"};
20 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)"};
21 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"};
22 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"};
23 cvar_t gl_texturecompression_color = {CVAR_SAVE, "gl_texturecompression_color", "1", "whether to compress colormap (diffuse) textures"};
24 cvar_t gl_texturecompression_normal = {CVAR_SAVE, "gl_texturecompression_normal", "0", "whether to compress normalmap (normalmap) textures"};
25 cvar_t gl_texturecompression_gloss = {CVAR_SAVE, "gl_texturecompression_gloss", "1", "whether to compress glossmap (specular) textures"};
26 cvar_t gl_texturecompression_glow = {CVAR_SAVE, "gl_texturecompression_glow", "1", "whether to compress glowmap (luma) textures"};
27 cvar_t gl_texturecompression_2d = {CVAR_SAVE, "gl_texturecompression_2d", "0", "whether to compress 2d (hud/menu) textures other than the font"};
28 cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "0", "whether to compress lightmaps in q3bsp format levels"};
29 cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "0", "whether to compress deluxemaps in q3bsp format levels (only levels compiled with q3map2 -deluxe have these)"};
30 cvar_t gl_texturecompression_sky = {CVAR_SAVE, "gl_texturecompression_sky", "0", "whether to compress sky textures"};
31 cvar_t gl_texturecompression_lightcubemaps = {CVAR_SAVE, "gl_texturecompression_lightcubemaps", "1", "whether to compress light cubemaps (spotlights and other light projection images)"};
32 cvar_t gl_texturecompression_reflectmask = {CVAR_SAVE, "gl_texturecompression_reflectmask", "1", "whether to compress reflection cubemap masks (mask of which areas of the texture should reflect the generic shiny cubemap)"};
33 cvar_t gl_texturecompression_sprites = {CVAR_SAVE, "gl_texturecompression_sprites", "1", "whether to compress sprites"};
34 cvar_t gl_nopartialtextureupdates = {CVAR_SAVE, "gl_nopartialtextureupdates", "0", "use alternate path for dynamic lightmap updates that avoids a possibly slow code path in the driver"};
35 cvar_t r_texture_dds_load_alphamode = {0, "r_texture_dds_load_alphamode", "1", "0: trust DDPF_ALPHAPIXELS flag, 1: texture format and brute force search if ambiguous, 2: texture format only"};
36 cvar_t r_texture_dds_load_logfailure = {0, "r_texture_dds_load_logfailure", "0", "log missing DDS textures to ddstexturefailures.log, 0: done log, 1: log with no optional textures (_norm, glow etc.). 2: log all"};
37 cvar_t r_texture_dds_swdecode = {0, "r_texture_dds_swdecode", "0", "0: don't software decode DDS, 1: software decode DDS if unsupported, 2: always software decode DDS"};
39 qboolean gl_filter_force = false;
40 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
41 int gl_filter_mag = GL_LINEAR;
44 static mempool_t *texturemempool;
45 static memexpandablearray_t texturearray;
47 // note: this must not conflict with TEXF_ flags in r_textures.h
48 // bitmask for mismatch checking
49 #define GLTEXF_IMPORTANTBITS (0)
50 // dynamic texture (treat texnum == 0 differently)
51 #define GLTEXF_DYNAMIC 0x00080000
53 typedef struct textypeinfo_s
57 int inputbytesperpixel;
58 int internalbytesperpixel;
59 float glinternalbytesperpixel;
68 // we use these internally even if we never deliver such data to the driver
70 #define GL_BGRA 0x80E1
72 // framebuffer texture formats
73 // GLES2 devices rarely support depth textures, so we actually use a renderbuffer there
74 static textypeinfo_t textype_shadowmap16_comp = {"shadowmap16_comp", TEXTYPE_SHADOWMAP16_COMP , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
75 static textypeinfo_t textype_shadowmap16_raw = {"shadowmap16_raw", TEXTYPE_SHADOWMAP16_RAW , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
76 static textypeinfo_t textype_shadowmap24_comp = {"shadowmap24_comp", TEXTYPE_SHADOWMAP24_COMP , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
77 static textypeinfo_t textype_shadowmap24_raw = {"shadowmap24_raw", TEXTYPE_SHADOWMAP24_RAW , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
78 static textypeinfo_t textype_depth16 = {"depth16", TEXTYPE_DEPTHBUFFER16 , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
79 static textypeinfo_t textype_depth24 = {"depth24", TEXTYPE_DEPTHBUFFER24 , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
80 static textypeinfo_t textype_depth24stencil8 = {"depth24stencil8", TEXTYPE_DEPTHBUFFER24STENCIL8, 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
81 static textypeinfo_t textype_colorbuffer = {"colorbuffer", TEXTYPE_COLORBUFFER , 2, 2, 2.0f, GL_RGB565 , GL_RGBA , GL_UNSIGNED_SHORT_5_6_5};
82 static textypeinfo_t textype_colorbuffer16f = {"colorbuffer16f", TEXTYPE_COLORBUFFER16F , 2, 2, 2.0f, GL_RGBA16F , GL_RGBA , GL_HALF_FLOAT};
83 static textypeinfo_t textype_colorbuffer32f = {"colorbuffer32f", TEXTYPE_COLORBUFFER32F , 2, 2, 2.0f, GL_RGBA32F , GL_RGBA , GL_FLOAT};
86 static textypeinfo_t textype_alpha = {"alpha", TEXTYPE_ALPHA , 1, 4, 4.0f, GL_ALPHA , GL_ALPHA , GL_UNSIGNED_BYTE };
87 static textypeinfo_t textype_palette = {"palette", TEXTYPE_PALETTE , 1, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
88 static textypeinfo_t textype_palette_alpha = {"palette_alpha", TEXTYPE_PALETTE , 1, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
89 static textypeinfo_t textype_rgba = {"rgba", TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , GL_RGBA , GL_UNSIGNED_BYTE };
90 static textypeinfo_t textype_rgba_alpha = {"rgba_alpha", TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , GL_RGBA , GL_UNSIGNED_BYTE };
91 static textypeinfo_t textype_bgra = {"bgra", TEXTYPE_BGRA , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
92 static textypeinfo_t textype_bgra_alpha = {"bgra_alpha", TEXTYPE_BGRA , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
94 static textypeinfo_t textype_etc1 = {"etc1", TEXTYPE_ETC1 , 1, 3, 0.5f, GL_ETC1_RGB8_OES , 0 , 0 };
97 // framebuffer texture formats
98 static textypeinfo_t textype_shadowmap16_comp = {"shadowmap16_comp", TEXTYPE_SHADOWMAP16_COMP , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
99 static textypeinfo_t textype_shadowmap16_raw = {"shadowmap16_raw", TEXTYPE_SHADOWMAP16_RAW , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
100 static textypeinfo_t textype_shadowmap24_comp = {"shadowmap24_comp", TEXTYPE_SHADOWMAP24_COMP , 4, 4, 4.0f, GL_DEPTH_COMPONENT24 , GL_DEPTH_COMPONENT, GL_UNSIGNED_INT };
101 static textypeinfo_t textype_shadowmap24_raw = {"shadowmap24_raw", TEXTYPE_SHADOWMAP24_RAW , 4, 4, 4.0f, GL_DEPTH_COMPONENT24 , GL_DEPTH_COMPONENT, GL_UNSIGNED_INT };
102 static textypeinfo_t textype_depth16 = {"depth16", TEXTYPE_DEPTHBUFFER16 , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT};
103 static textypeinfo_t textype_depth24 = {"depth24", TEXTYPE_DEPTHBUFFER24 , 4, 4, 4.0f, GL_DEPTH_COMPONENT24 , GL_DEPTH_COMPONENT, GL_UNSIGNED_INT };
104 static textypeinfo_t textype_depth24stencil8 = {"depth24stencil8", TEXTYPE_DEPTHBUFFER24STENCIL8, 4, 4, 4.0f, GL_DEPTH24_STENCIL8 , GL_DEPTH_STENCIL , GL_UNSIGNED_INT_24_8};
105 static textypeinfo_t textype_colorbuffer = {"colorbuffer", TEXTYPE_COLORBUFFER , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
106 static textypeinfo_t textype_colorbuffer16f = {"colorbuffer16f", TEXTYPE_COLORBUFFER16F , 8, 8, 8.0f, GL_RGBA16F , GL_RGBA , GL_HALF_FLOAT };
107 static textypeinfo_t textype_colorbuffer32f = {"colorbuffer32f", TEXTYPE_COLORBUFFER32F , 16, 16, 16.0f, GL_RGBA32F , GL_RGBA , GL_FLOAT };
110 static textypeinfo_t textype_alpha = {"alpha", TEXTYPE_ALPHA , 1, 4, 4.0f, GL_ALPHA , GL_ALPHA , GL_UNSIGNED_BYTE };
111 static textypeinfo_t textype_palette = {"palette", TEXTYPE_PALETTE , 1, 4, 4.0f, GL_RGB , GL_BGRA , GL_UNSIGNED_BYTE };
112 static textypeinfo_t textype_palette_alpha = {"palette_alpha", TEXTYPE_PALETTE , 1, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
113 static textypeinfo_t textype_rgba = {"rgba", TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGB , GL_RGBA , GL_UNSIGNED_BYTE };
114 static textypeinfo_t textype_rgba_alpha = {"rgba_alpha", TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , GL_RGBA , GL_UNSIGNED_BYTE };
115 static textypeinfo_t textype_rgba_compress = {"rgba_compress", TEXTYPE_RGBA , 4, 4, 0.5f, GL_COMPRESSED_RGB_S3TC_DXT1_EXT , GL_RGBA , GL_UNSIGNED_BYTE };
116 static textypeinfo_t textype_rgba_alpha_compress = {"rgba_alpha_compress", TEXTYPE_RGBA , 4, 4, 1.0f, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT , GL_RGBA , GL_UNSIGNED_BYTE };
117 static textypeinfo_t textype_bgra = {"bgra", TEXTYPE_BGRA , 4, 4, 4.0f, GL_RGB , GL_BGRA , GL_UNSIGNED_BYTE };
118 static textypeinfo_t textype_bgra_alpha = {"bgra_alpha", TEXTYPE_BGRA , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE };
119 static textypeinfo_t textype_bgra_compress = {"bgra_compress", TEXTYPE_BGRA , 4, 4, 0.5f, GL_COMPRESSED_RGB_S3TC_DXT1_EXT , GL_BGRA , GL_UNSIGNED_BYTE };
120 static textypeinfo_t textype_bgra_alpha_compress = {"bgra_alpha_compress", TEXTYPE_BGRA , 4, 4, 1.0f, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT , GL_BGRA , GL_UNSIGNED_BYTE };
121 static textypeinfo_t textype_dxt1 = {"dxt1", TEXTYPE_DXT1 , 4, 0, 0.5f, GL_COMPRESSED_RGB_S3TC_DXT1_EXT , 0 , 0 };
122 static textypeinfo_t textype_dxt1a = {"dxt1a", TEXTYPE_DXT1A , 4, 0, 0.5f, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT , 0 , 0 };
123 static textypeinfo_t textype_dxt3 = {"dxt3", TEXTYPE_DXT3 , 4, 0, 1.0f, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT , 0 , 0 };
124 static textypeinfo_t textype_dxt5 = {"dxt5", TEXTYPE_DXT5 , 4, 0, 1.0f, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT , 0 , 0 };
125 static textypeinfo_t textype_sRGB_palette = {"sRGB_palette", TEXTYPE_PALETTE , 1, 4, 4.0f, GL_SRGB , GL_BGRA , GL_UNSIGNED_BYTE };
126 static textypeinfo_t textype_sRGB_palette_alpha = {"sRGB_palette_alpha", TEXTYPE_PALETTE , 1, 4, 4.0f, GL_SRGB_ALPHA , GL_BGRA , GL_UNSIGNED_BYTE };
127 static textypeinfo_t textype_sRGB_rgba = {"sRGB_rgba", TEXTYPE_RGBA , 4, 4, 4.0f, GL_SRGB , GL_RGBA , GL_UNSIGNED_BYTE };
128 static textypeinfo_t textype_sRGB_rgba_alpha = {"sRGB_rgba_alpha", TEXTYPE_RGBA , 4, 4, 4.0f, GL_SRGB_ALPHA , GL_RGBA , GL_UNSIGNED_BYTE };
129 static textypeinfo_t textype_sRGB_rgba_compress = {"sRGB_rgba_compress", TEXTYPE_RGBA , 4, 4, 0.5f, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT , GL_RGBA , GL_UNSIGNED_BYTE };
130 static textypeinfo_t textype_sRGB_rgba_alpha_compress = {"sRGB_rgba_alpha_compress", TEXTYPE_RGBA , 4, 4, 1.0f, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_RGBA , GL_UNSIGNED_BYTE };
131 static textypeinfo_t textype_sRGB_bgra = {"sRGB_bgra", TEXTYPE_BGRA , 4, 4, 4.0f, GL_SRGB , GL_BGRA , GL_UNSIGNED_BYTE };
132 static textypeinfo_t textype_sRGB_bgra_alpha = {"sRGB_bgra_alpha", TEXTYPE_BGRA , 4, 4, 4.0f, GL_SRGB_ALPHA , GL_BGRA , GL_UNSIGNED_BYTE };
133 static textypeinfo_t textype_sRGB_bgra_compress = {"sRGB_bgra_compress", TEXTYPE_BGRA , 4, 4, 0.5f, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT , GL_BGRA , GL_UNSIGNED_BYTE };
134 static textypeinfo_t textype_sRGB_bgra_alpha_compress = {"sRGB_bgra_alpha_compress", TEXTYPE_BGRA , 4, 4, 1.0f, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, GL_BGRA , GL_UNSIGNED_BYTE };
135 static textypeinfo_t textype_sRGB_dxt1 = {"sRGB_dxt1", TEXTYPE_DXT1 , 4, 0, 0.5f, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT , 0 , 0 };
136 static textypeinfo_t textype_sRGB_dxt1a = {"sRGB_dxt1a", TEXTYPE_DXT1A , 4, 0, 0.5f, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 0 , 0 };
137 static textypeinfo_t textype_sRGB_dxt3 = {"sRGB_dxt3", TEXTYPE_DXT3 , 4, 0, 1.0f, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 0 , 0 };
138 static textypeinfo_t textype_sRGB_dxt5 = {"sRGB_dxt5", TEXTYPE_DXT5 , 4, 0, 1.0f, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 0 , 0 };
141 typedef enum gltexturetype_e
145 GLTEXTURETYPE_CUBEMAP,
150 static int gltexturetypeenums[GLTEXTURETYPE_TOTAL] = {GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP};
151 #ifdef GL_TEXTURE_WRAP_R
152 static int gltexturetypedimensions[GLTEXTURETYPE_TOTAL] = {2, 3, 2};
154 static int cubemapside[6] =
156 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
157 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
158 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
159 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
160 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
161 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
164 typedef struct gltexture_s
166 // this portion of the struct is exposed to the R_GetTexture macro for
167 // speed reasons, must be identical in rtexture_t!
168 int texnum; // GL texture slot number
169 int renderbuffernum; // GL renderbuffer slot number
170 qboolean dirty; // indicates that R_RealGetTexture should be called
171 qboolean glisdepthstencil; // indicates that FBO attachment has to be GL_DEPTH_STENCIL_ATTACHMENT
172 int gltexturetypeenum; // used by R_Mesh_TexBind
174 // dynamic texture stuff [11/22/2007 Black]
175 updatecallback_t updatecallback;
176 void *updatecallback_data;
177 // --- [11/22/2007 Black]
179 // stores backup copy of texture for deferred texture updates (gl_nopartialtextureupdates cvar)
180 unsigned char *bufferpixels;
181 qboolean buffermodified;
183 // pointer to texturepool (check this to see if the texture is allocated)
184 struct gltexturepool_s *pool;
185 // pointer to next texture in texturepool chain
186 struct gltexture_s *chain;
187 // name of the texture (this might be removed someday), no duplicates
188 char identifier[MAX_QPATH + 32];
189 // original data size in *inputtexels
190 int inputwidth, inputheight, inputdepth;
191 // copy of the original texture(s) supplied to the upload function, for
192 // delayed uploads (non-precached)
193 unsigned char *inputtexels;
194 // original data size in *inputtexels
196 // flags supplied to the LoadTexture function
197 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
201 // pointer to one of the textype_ structs
202 textypeinfo_t *textype;
203 // one of the GLTEXTURETYPE_ values
205 // palette if the texture is TEXTYPE_PALETTE
206 const unsigned int *palette;
207 // actual stored texture size after gl_picmip and gl_max_size are applied
208 int tilewidth, tileheight, tiledepth;
209 // 1 or 6 depending on texturetype
211 // how many mipmap levels in this texture
215 // GL_RGB or GL_RGBA or GL_DEPTH_COMPONENT
218 int glinternalformat;
219 // GL_UNSIGNED_BYTE or GL_UNSIGNED_INT or GL_UNSIGNED_SHORT or GL_FLOAT
224 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
226 typedef struct gltexturepool_s
228 unsigned int sentinel;
229 struct gltexture_s *gltchain;
230 struct gltexturepool_s *next;
234 static gltexturepool_t *gltexturepoolchain = NULL;
236 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
237 static int resizebuffersize = 0;
238 static const unsigned char *texturebuffer;
240 static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags)
245 case TEXTYPE_PALETTE: return (flags & TEXF_ALPHA) ? &textype_palette_alpha : &textype_palette;
246 case TEXTYPE_RGBA: return ((flags & TEXF_ALPHA) ? &textype_rgba_alpha : &textype_rgba);
247 case TEXTYPE_BGRA: return ((flags & TEXF_ALPHA) ? &textype_bgra_alpha : &textype_bgra);
249 case TEXTYPE_ETC1: return &textype_etc1;
251 case TEXTYPE_ALPHA: return &textype_alpha;
252 case TEXTYPE_COLORBUFFER: return &textype_colorbuffer;
253 case TEXTYPE_COLORBUFFER16F: return &textype_colorbuffer16f;
254 case TEXTYPE_COLORBUFFER32F: return &textype_colorbuffer32f;
255 case TEXTYPE_DEPTHBUFFER16: return &textype_depth16;
256 case TEXTYPE_DEPTHBUFFER24: return &textype_depth24;
257 case TEXTYPE_DEPTHBUFFER24STENCIL8: return &textype_depth24stencil8;
258 case TEXTYPE_SHADOWMAP16_COMP: return &textype_shadowmap16_comp;
259 case TEXTYPE_SHADOWMAP16_RAW: return &textype_shadowmap16_raw;
260 case TEXTYPE_SHADOWMAP24_COMP: return &textype_shadowmap24_comp;
261 case TEXTYPE_SHADOWMAP24_RAW: return &textype_shadowmap24_raw;
263 case TEXTYPE_DXT1: return &textype_dxt1;
264 case TEXTYPE_DXT1A: return &textype_dxt1a;
265 case TEXTYPE_DXT3: return &textype_dxt3;
266 case TEXTYPE_DXT5: return &textype_dxt5;
267 case TEXTYPE_PALETTE: return (flags & TEXF_ALPHA) ? &textype_palette_alpha : &textype_palette;
268 case TEXTYPE_RGBA: return ((flags & TEXF_COMPRESS) && vid.support.ext_texture_compression_s3tc) ? ((flags & TEXF_ALPHA) ? &textype_rgba_alpha_compress : &textype_rgba_compress) : ((flags & TEXF_ALPHA) ? &textype_rgba_alpha : &textype_rgba);
269 case TEXTYPE_BGRA: return ((flags & TEXF_COMPRESS) && vid.support.ext_texture_compression_s3tc) ? ((flags & TEXF_ALPHA) ? &textype_bgra_alpha_compress : &textype_bgra_compress) : ((flags & TEXF_ALPHA) ? &textype_bgra_alpha : &textype_bgra);
270 case TEXTYPE_ALPHA: return &textype_alpha;
271 case TEXTYPE_COLORBUFFER: return &textype_colorbuffer;
272 case TEXTYPE_COLORBUFFER16F: return &textype_colorbuffer16f;
273 case TEXTYPE_COLORBUFFER32F: return &textype_colorbuffer32f;
274 case TEXTYPE_DEPTHBUFFER16: return &textype_depth16;
275 case TEXTYPE_DEPTHBUFFER24: return &textype_depth24;
276 case TEXTYPE_DEPTHBUFFER24STENCIL8: return &textype_depth24stencil8;
277 case TEXTYPE_SHADOWMAP16_COMP: return &textype_shadowmap16_comp;
278 case TEXTYPE_SHADOWMAP16_RAW: return &textype_shadowmap16_raw;
279 case TEXTYPE_SHADOWMAP24_COMP: return &textype_shadowmap24_comp;
280 case TEXTYPE_SHADOWMAP24_RAW: return &textype_shadowmap24_raw;
281 case TEXTYPE_SRGB_DXT1: return &textype_sRGB_dxt1;
282 case TEXTYPE_SRGB_DXT1A: return &textype_sRGB_dxt1a;
283 case TEXTYPE_SRGB_DXT3: return &textype_sRGB_dxt3;
284 case TEXTYPE_SRGB_DXT5: return &textype_sRGB_dxt5;
285 case TEXTYPE_SRGB_PALETTE: return (flags & TEXF_ALPHA) ? &textype_sRGB_palette_alpha : &textype_sRGB_palette;
286 case TEXTYPE_SRGB_RGBA: return ((flags & TEXF_COMPRESS) && vid.support.ext_texture_compression_s3tc) ? ((flags & TEXF_ALPHA) ? &textype_sRGB_rgba_alpha_compress : &textype_sRGB_rgba_compress) : ((flags & TEXF_ALPHA) ? &textype_sRGB_rgba_alpha : &textype_sRGB_rgba);
287 case TEXTYPE_SRGB_BGRA: return ((flags & TEXF_COMPRESS) && vid.support.ext_texture_compression_s3tc) ? ((flags & TEXF_ALPHA) ? &textype_sRGB_bgra_alpha_compress : &textype_sRGB_bgra_compress) : ((flags & TEXF_ALPHA) ? &textype_sRGB_bgra_alpha : &textype_sRGB_bgra);
290 Host_Error("R_GetTexTypeInfo: unknown texture format %i with flags %x", (int)textype, flags);
296 // dynamic texture code [11/22/2007 Black]
297 void R_MarkDirtyTexture(rtexture_t *rt) {
298 gltexture_t *glt = (gltexture_t*) rt;
303 // dont do anything if the texture is already dirty (and make sure this *is* a dynamic texture after all!)
304 if (glt->flags & GLTEXF_DYNAMIC)
306 // mark it as dirty, so R_RealGetTexture gets called
311 void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void *data) {
312 gltexture_t *glt = (gltexture_t*) rt;
317 glt->flags |= GLTEXF_DYNAMIC;
318 glt->updatecallback = updatecallback;
319 glt->updatecallback_data = data;
322 static void R_UpdateDynamicTexture(gltexture_t *glt) {
324 if( glt->updatecallback ) {
325 glt->updatecallback( (rtexture_t*) glt, glt->updatecallback_data );
329 void R_PurgeTexture(rtexture_t *rt)
331 if(rt && !(((gltexture_t*) rt)->flags & TEXF_PERSISTENT)) {
336 void R_FreeTexture(rtexture_t *rt)
338 gltexture_t *glt, **gltpointer;
340 glt = (gltexture_t *)rt;
342 Host_Error("R_FreeTexture: texture == NULL");
344 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
345 if (*gltpointer == glt)
346 *gltpointer = glt->chain;
348 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
350 R_Mesh_ClearBindingsForTexture(glt->texnum);
352 switch(vid.renderpath)
354 case RENDERPATH_GL32:
355 case RENDERPATH_GLES2:
359 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
361 if (glt->renderbuffernum)
364 qglDeleteRenderbuffers(1, (GLuint *)&glt->renderbuffernum);CHECKGLERROR
369 if (glt->inputtexels)
370 Mem_Free(glt->inputtexels);
371 Mem_ExpandableArray_FreeRecord(&texturearray, glt);
374 rtexturepool_t *R_AllocTexturePool(void)
376 gltexturepool_t *pool;
377 if (texturemempool == NULL)
379 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
382 pool->next = gltexturepoolchain;
383 gltexturepoolchain = pool;
384 pool->sentinel = TEXTUREPOOL_SENTINEL;
385 return (rtexturepool_t *)pool;
388 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
390 gltexturepool_t *pool, **poolpointer;
391 if (rtexturepool == NULL)
393 if (*rtexturepool == NULL)
395 pool = (gltexturepool_t *)(*rtexturepool);
396 *rtexturepool = NULL;
397 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
398 Host_Error("R_FreeTexturePool: pool already freed");
399 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
400 if (*poolpointer == pool)
401 *poolpointer = pool->next;
403 Host_Error("R_FreeTexturePool: pool not linked");
404 while (pool->gltchain)
405 R_FreeTexture((rtexture_t *)pool->gltchain);
410 typedef struct glmode_s
413 int minification, magnification;
417 static glmode_t modes[6] =
419 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
420 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
421 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
422 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
423 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
424 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
427 static void GL_TextureMode_f (void)
432 gltexturepool_t *pool;
436 Con_Printf("Texture mode is %sforced\n", gl_filter_force ? "" : "not ");
437 for (i = 0;i < 6;i++)
439 if (gl_filter_min == modes[i].minification)
441 Con_Printf("%s\n", modes[i].name);
445 Con_Print("current filter is unknown???\n");
449 for (i = 0;i < (int)(sizeof(modes)/sizeof(*modes));i++)
450 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
454 Con_Print("bad filter name\n");
458 gl_filter_min = modes[i].minification;
459 gl_filter_mag = modes[i].magnification;
460 gl_filter_force = ((Cmd_Argc() > 2) && !strcasecmp(Cmd_Argv(2), "force"));
462 switch(vid.renderpath)
464 case RENDERPATH_GL32:
465 case RENDERPATH_GLES2:
466 // change all the existing mipmap texture objects
467 // FIXME: force renderer(/client/something?) restart instead?
470 for (pool = gltexturepoolchain;pool;pool = pool->next)
472 for (glt = pool->gltchain;glt;glt = glt->chain)
474 // only update already uploaded images
475 if (glt->texnum && (gl_filter_force || !(glt->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR))))
477 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]);
478 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
479 if (glt->flags & TEXF_MIPMAP)
481 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
485 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
487 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
488 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
496 static void GL_Texture_CalcImageSize(int texturetype, int flags, int miplevel, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth, int *outmiplevels)
498 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1, miplevels = 1;
503 case GLTEXTURETYPE_2D:
504 maxsize = vid.maxtexturesize_2d;
505 if (flags & TEXF_PICMIP)
507 maxsize = bound(1, gl_max_size.integer, maxsize);
511 case GLTEXTURETYPE_3D:
512 maxsize = vid.maxtexturesize_3d;
514 case GLTEXTURETYPE_CUBEMAP:
515 maxsize = vid.maxtexturesize_cubemap;
519 width2 = min(inwidth >> picmip, maxsize);
520 height2 = min(inheight >> picmip, maxsize);
521 depth2 = min(indepth >> picmip, maxsize);
524 if (flags & TEXF_MIPMAP)
526 int extent = max(width2, max(height2, depth2));
532 *outwidth = max(1, width2);
534 *outheight = max(1, height2);
536 *outdepth = max(1, depth2);
538 *outmiplevels = miplevels;
542 static int R_CalcTexelDataSize (gltexture_t *glt)
544 int width2, height2, depth2, size;
546 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->miplevel, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2, NULL);
548 size = width2 * height2 * depth2;
550 if (glt->flags & TEXF_MIPMAP)
552 while (width2 > 1 || height2 > 1 || depth2 > 1)
560 size += width2 * height2 * depth2;
564 return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
567 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
571 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
572 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
574 gltexturepool_t *pool;
576 Con_Print("glsize input loaded mip alpha name\n");
577 for (pool = gltexturepoolchain;pool;pool = pool->next)
585 for (glt = pool->gltchain;glt;glt = glt->chain)
587 glsize = R_CalcTexelDataSize(glt);
588 isloaded = glt->texnum != 0 || glt->renderbuffernum != 0;
590 pooltotalt += glsize;
591 pooltotalp += glt->inputdatasize;
595 poolloadedt += glsize;
596 poolloadedp += glt->inputdatasize;
599 Con_Printf("%c%4i%c%c%4i%c %-24s %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->textype->name, isloaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier);
602 Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", (void *)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);
603 sumtotal += pooltotal;
604 sumtotalt += pooltotalt;
605 sumtotalp += pooltotalp;
606 sumloaded += poolloaded;
607 sumloadedt += poolloadedt;
608 sumloadedp += poolloadedp;
611 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);
614 static void R_TextureStats_f(void)
616 R_TextureStats_Print(true, true, true);
619 static void r_textures_start(void)
621 switch(vid.renderpath)
623 case RENDERPATH_GL32:
624 case RENDERPATH_GLES2:
625 // LordHavoc: allow any alignment
627 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
628 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
632 texturemempool = Mem_AllocPool("texture management", 0, NULL);
633 Mem_ExpandableArray_NewArray(&texturearray, texturemempool, sizeof(gltexture_t), 512);
635 // Disable JPEG screenshots if the DLL isn't loaded
636 if (! JPEG_OpenLibrary ())
637 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
638 if (! PNG_OpenLibrary ())
639 Cvar_SetValueQuick (&scr_screenshot_png, 0);
642 static void r_textures_shutdown(void)
644 rtexturepool_t *temp;
646 JPEG_CloseLibrary ();
648 while(gltexturepoolchain)
650 temp = (rtexturepool_t *) gltexturepoolchain;
651 R_FreeTexturePool(&temp);
654 resizebuffersize = 0;
656 colorconvertbuffer = NULL;
657 texturebuffer = NULL;
658 Mem_ExpandableArray_FreeArray(&texturearray);
659 Mem_FreePool(&texturemempool);
662 static void r_textures_newmap(void)
666 static void r_textures_devicelost(void)
670 endindex = (int)Mem_ExpandableArray_IndexRange(&texturearray);
671 for (i = 0;i < endindex;i++)
673 glt = (gltexture_t *) Mem_ExpandableArray_RecordAtIndex(&texturearray, i);
674 if (!glt || !(glt->flags & TEXF_RENDERTARGET))
676 switch(vid.renderpath)
678 case RENDERPATH_GL32:
679 case RENDERPATH_GLES2:
685 static void r_textures_devicerestored(void)
689 endindex = (int)Mem_ExpandableArray_IndexRange(&texturearray);
690 for (i = 0;i < endindex;i++)
692 glt = (gltexture_t *) Mem_ExpandableArray_RecordAtIndex(&texturearray, i);
693 if (!glt || !(glt->flags & TEXF_RENDERTARGET))
695 switch(vid.renderpath)
697 case RENDERPATH_GL32:
698 case RENDERPATH_GLES2:
705 void R_Textures_Init (void)
707 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc); an additional argument 'force' forces the texture mode even in cases where it may not be appropriate");
708 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
709 Cvar_RegisterVariable (&gl_max_size);
710 Cvar_RegisterVariable (&gl_picmip);
711 Cvar_RegisterVariable (&gl_picmip_world);
712 Cvar_RegisterVariable (&r_picmipworld);
713 Cvar_RegisterVariable (&gl_picmip_sprites);
714 Cvar_RegisterVariable (&r_picmipsprites);
715 Cvar_RegisterVariable (&gl_picmip_other);
716 Cvar_RegisterVariable (&gl_max_lightmapsize);
717 Cvar_RegisterVariable (&r_lerpimages);
718 Cvar_RegisterVariable (&gl_texture_anisotropy);
719 Cvar_RegisterVariable (&gl_texturecompression);
720 Cvar_RegisterVariable (&gl_texturecompression_color);
721 Cvar_RegisterVariable (&gl_texturecompression_normal);
722 Cvar_RegisterVariable (&gl_texturecompression_gloss);
723 Cvar_RegisterVariable (&gl_texturecompression_glow);
724 Cvar_RegisterVariable (&gl_texturecompression_2d);
725 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
726 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
727 Cvar_RegisterVariable (&gl_texturecompression_sky);
728 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
729 Cvar_RegisterVariable (&gl_texturecompression_reflectmask);
730 Cvar_RegisterVariable (&gl_texturecompression_sprites);
731 Cvar_RegisterVariable (&gl_nopartialtextureupdates);
732 Cvar_RegisterVariable (&r_texture_dds_load_alphamode);
733 Cvar_RegisterVariable (&r_texture_dds_load_logfailure);
734 Cvar_RegisterVariable (&r_texture_dds_swdecode);
736 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap, r_textures_devicelost, r_textures_devicerestored);
739 void R_Textures_Frame (void)
741 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
742 static int old_aniso = 0;
743 static qboolean first_time_aniso = true;
746 // could do procedural texture animation here, if we keep track of which
747 // textures were accessed this frame...
749 // free the resize buffers
750 resizebuffersize = 0;
753 Mem_Free(resizebuffer);
756 if (colorconvertbuffer)
758 Mem_Free(colorconvertbuffer);
759 colorconvertbuffer = NULL;
762 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
763 if (old_aniso != gl_texture_anisotropy.integer)
766 gltexturepool_t *pool;
769 old_aniso = bound(1, gl_texture_anisotropy.integer, (int)vid.max_anisotropy);
771 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
773 switch(vid.renderpath)
775 case RENDERPATH_GL32:
776 case RENDERPATH_GLES2:
777 // ignore the first difference, any textures loaded by now probably had the same aniso value
778 if (first_time_aniso)
780 first_time_aniso = false;
785 for (pool = gltexturepoolchain;pool;pool = pool->next)
787 for (glt = pool->gltchain;glt;glt = glt->chain)
789 // only update already uploaded images
790 if (glt->texnum && (glt->flags & TEXF_MIPMAP) == TEXF_MIPMAP)
792 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]);
794 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
795 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
797 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
807 static void R_MakeResizeBufferBigger(int size)
809 if (resizebuffersize < size)
811 resizebuffersize = size;
813 Mem_Free(resizebuffer);
814 if (colorconvertbuffer)
815 Mem_Free(colorconvertbuffer);
816 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
817 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
818 if (!resizebuffer || !colorconvertbuffer)
819 Host_Error("R_Upload: out of memory");
823 static void GL_SetupTextureParameters(int flags, textype_t textype, int texturetype)
825 int textureenum = gltexturetypeenums[texturetype];
826 int wrapmode = (flags & TEXF_CLAMP) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
830 #ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT
831 if (vid.support.ext_texture_filter_anisotropic && (flags & TEXF_MIPMAP))
833 int aniso = bound(1, gl_texture_anisotropy.integer, (int)vid.max_anisotropy);
834 if (gl_texture_anisotropy.integer != aniso)
835 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
836 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
839 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
840 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
841 #ifdef GL_TEXTURE_WRAP_R
842 if (gltexturetypedimensions[texturetype] >= 3)
844 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
849 if (!gl_filter_force && flags & TEXF_FORCENEAREST)
851 if (flags & TEXF_MIPMAP)
853 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
857 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
859 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
861 else if (!gl_filter_force && flags & TEXF_FORCELINEAR)
863 if (flags & TEXF_MIPMAP)
865 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
867 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
871 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
876 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
878 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
882 if (flags & TEXF_MIPMAP)
884 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
888 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
890 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
896 case TEXTYPE_SHADOWMAP16_COMP:
897 case TEXTYPE_SHADOWMAP24_COMP:
898 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);CHECKGLERROR
899 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);CHECKGLERROR
901 case TEXTYPE_SHADOWMAP16_RAW:
902 case TEXTYPE_SHADOWMAP24_RAW:
903 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE, GL_NONE);CHECKGLERROR
904 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);CHECKGLERROR
914 static void R_UploadPartialTexture(gltexture_t *glt, const unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
917 Sys_Error("R_UploadPartialTexture \"%s\": partial update with NULL pixels", glt->identifier);
919 if (glt->texturetype != GLTEXTURETYPE_2D)
920 Sys_Error("R_UploadPartialTexture \"%s\": partial update of type other than 2D", glt->identifier);
922 if (glt->textype->textype == TEXTYPE_PALETTE)
923 Sys_Error("R_UploadPartialTexture \"%s\": partial update of paletted texture", glt->identifier);
925 if (glt->flags & (TEXF_MIPMAP | TEXF_PICMIP))
926 Sys_Error("R_UploadPartialTexture \"%s\": partial update not supported with MIPMAP or PICMIP flags", glt->identifier);
928 if (glt->inputwidth != glt->tilewidth || glt->inputheight != glt->tileheight || glt->tiledepth != 1)
929 Sys_Error("R_UploadPartialTexture \"%s\": partial update not supported with stretched or special textures", glt->identifier);
931 // update a portion of the image
933 switch(vid.renderpath)
935 case RENDERPATH_GL32:
936 case RENDERPATH_GLES2:
940 // we need to restore the texture binding after finishing the upload
942 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]);
943 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
944 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, glt->gltype, data);CHECKGLERROR
945 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
951 static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data)
953 int i, mip = 0, width, height, depth;
954 GLint oldbindtexnum = 0;
955 const unsigned char *prevbuffer;
958 // error out if a stretch is needed on special texture types
959 if (glt->texturetype != GLTEXTURETYPE_2D && (glt->tilewidth != glt->inputwidth || glt->tileheight != glt->inputheight || glt->tiledepth != glt->inputdepth))
960 Sys_Error("R_UploadFullTexture \"%s\": stretch uploads allowed only on 2D textures\n", glt->identifier);
962 // when picmip or maxsize is applied, we scale up to a power of 2 multiple
963 // of the target size and then use the mipmap reduction function to get
964 // high quality supersampled results
965 for (width = glt->tilewidth;width < glt->inputwidth ;width <<= 1);
966 for (height = glt->tileheight;height < glt->inputheight;height <<= 1);
967 for (depth = glt->tiledepth;depth < glt->inputdepth ;depth <<= 1);
969 if (prevbuffer == NULL)
971 width = glt->tilewidth;
972 height = glt->tileheight;
973 depth = glt->tiledepth;
974 // R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
975 // memset(resizebuffer, 0, width * height * depth * glt->sides * glt->bytesperpixel);
976 // prevbuffer = resizebuffer;
980 if (glt->textype->textype == TEXTYPE_PALETTE)
982 // promote paletted to BGRA, so we only have to worry about BGRA in the rest of this code
983 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
984 Image_Copy8bitBGRA(prevbuffer, colorconvertbuffer, glt->inputwidth * glt->inputheight * glt->inputdepth * glt->sides, glt->palette);
985 prevbuffer = colorconvertbuffer;
987 if (glt->flags & TEXF_RGBMULTIPLYBYALPHA)
989 // multiply RGB channels by A channel before uploading
991 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
992 for (i = 0;i < glt->inputwidth*glt->inputheight*glt->inputdepth*4;i += 4)
994 alpha = prevbuffer[i+3];
995 colorconvertbuffer[i] = (prevbuffer[i] * alpha) >> 8;
996 colorconvertbuffer[i+1] = (prevbuffer[i+1] * alpha) >> 8;
997 colorconvertbuffer[i+2] = (prevbuffer[i+2] * alpha) >> 8;
998 colorconvertbuffer[i+3] = alpha;
1000 prevbuffer = colorconvertbuffer;
1002 // scale up to a power of 2 size (if appropriate)
1003 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
1005 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1006 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
1007 prevbuffer = resizebuffer;
1009 // apply mipmap reduction algorithm to get down to picmip/max_size
1010 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
1012 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1013 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
1014 prevbuffer = resizebuffer;
1018 // do the appropriate upload type...
1019 switch(vid.renderpath)
1021 case RENDERPATH_GL32:
1022 case RENDERPATH_GLES2:
1023 if (glt->texnum) // not renderbuffers
1027 // we need to restore the texture binding after finishing the upload
1028 GL_ActiveTexture(0);
1029 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]);
1030 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
1033 if (gl_texturecompression.integer >= 2)
1034 qglHint(GL_TEXTURE_COMPRESSION_HINT, GL_NICEST);
1036 qglHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST);
1039 switch(glt->texturetype)
1041 case GLTEXTURETYPE_2D:
1042 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
1043 if (glt->flags & TEXF_MIPMAP)
1045 while (width > 1 || height > 1 || depth > 1)
1047 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1048 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
1049 prevbuffer = resizebuffer;
1050 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
1054 case GLTEXTURETYPE_3D:
1056 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
1057 if (glt->flags & TEXF_MIPMAP)
1059 while (width > 1 || height > 1 || depth > 1)
1061 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1062 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
1063 prevbuffer = resizebuffer;
1064 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
1069 case GLTEXTURETYPE_CUBEMAP:
1070 // convert and upload each side in turn,
1071 // from a continuous block of input texels
1072 texturebuffer = (unsigned char *)prevbuffer;
1073 for (i = 0;i < 6;i++)
1075 prevbuffer = texturebuffer;
1076 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
1077 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
1079 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1080 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
1081 prevbuffer = resizebuffer;
1084 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
1086 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1087 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
1088 prevbuffer = resizebuffer;
1091 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
1092 if (glt->flags & TEXF_MIPMAP)
1094 while (width > 1 || height > 1 || depth > 1)
1096 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
1097 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
1098 prevbuffer = resizebuffer;
1099 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
1105 GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype);
1106 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
1112 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, int miplevel, textype_t textype, int texturetype, const unsigned char *data, const unsigned int *palette)
1116 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1117 textypeinfo_t *texinfo, *texinfo2;
1118 unsigned char *temppixels = NULL;
1121 if (cls.state == ca_dedicated)
1124 // see if we need to swap red and blue (BGRA <-> RGBA conversion)
1125 if (textype == TEXTYPE_PALETTE && vid.forcetextype == TEXTYPE_RGBA)
1127 int numpixels = width * height * depth * sides;
1128 size = numpixels * 4;
1129 temppixels = (unsigned char *)Mem_Alloc(tempmempool, size);
1132 const unsigned char *p;
1133 unsigned char *o = temppixels;
1134 for (i = 0;i < numpixels;i++, o += 4)
1136 p = (const unsigned char *)palette + 4*data[i];
1144 textype = TEXTYPE_RGBA;
1149 case TEXTYPE_RGBA: if (vid.forcetextype == TEXTYPE_BGRA) {swaprb = true;textype = TEXTYPE_BGRA;} break;
1150 case TEXTYPE_BGRA: if (vid.forcetextype == TEXTYPE_RGBA) {swaprb = true;textype = TEXTYPE_RGBA;} break;
1151 case TEXTYPE_SRGB_RGBA: if (vid.forcetextype == TEXTYPE_BGRA) {swaprb = true;textype = TEXTYPE_SRGB_BGRA;} break;
1152 case TEXTYPE_SRGB_BGRA: if (vid.forcetextype == TEXTYPE_RGBA) {swaprb = true;textype = TEXTYPE_SRGB_RGBA;} break;
1158 static int rgbaswapindices[4] = {2, 1, 0, 3};
1159 size = width * height * depth * sides * 4;
1160 temppixels = (unsigned char *)Mem_Alloc(tempmempool, size);
1162 Image_CopyMux(temppixels, data, width, height*depth*sides, false, false, false, 4, 4, rgbaswapindices);
1166 // if sRGB texture formats are not supported, convert input to linear and upload as normal types
1167 if (!vid.support.ext_texture_srgb)
1169 qboolean convertsRGB = false;
1172 case TEXTYPE_SRGB_DXT1: textype = TEXTYPE_DXT1 ;convertsRGB = true;break;
1173 case TEXTYPE_SRGB_DXT1A: textype = TEXTYPE_DXT1A ;convertsRGB = true;break;
1174 case TEXTYPE_SRGB_DXT3: textype = TEXTYPE_DXT3 ;convertsRGB = true;break;
1175 case TEXTYPE_SRGB_DXT5: textype = TEXTYPE_DXT5 ;convertsRGB = true;break;
1176 case TEXTYPE_SRGB_PALETTE: textype = TEXTYPE_PALETTE;/*convertsRGB = true;*/break;
1177 case TEXTYPE_SRGB_RGBA: textype = TEXTYPE_RGBA ;convertsRGB = true;break;
1178 case TEXTYPE_SRGB_BGRA: textype = TEXTYPE_BGRA ;convertsRGB = true;break;
1182 if (convertsRGB && data)
1184 size = width * height * depth * sides * 4;
1187 temppixels = (unsigned char *)Mem_Alloc(tempmempool, size);
1188 memcpy(temppixels, data, size);
1191 Image_MakeLinearColorsFromsRGB(temppixels, temppixels, width*height*depth*sides);
1195 texinfo = R_GetTexTypeInfo(textype, flags);
1196 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1199 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
1203 // clear the alpha flag if the texture has no transparent pixels
1206 case TEXTYPE_PALETTE:
1207 case TEXTYPE_SRGB_PALETTE:
1208 if (flags & TEXF_ALPHA)
1210 flags &= ~TEXF_ALPHA;
1213 for (i = 0;i < size;i++)
1215 if (((unsigned char *)&palette[data[i]])[3] < 255)
1217 flags |= TEXF_ALPHA;
1226 case TEXTYPE_SRGB_RGBA:
1227 case TEXTYPE_SRGB_BGRA:
1228 if (flags & TEXF_ALPHA)
1230 flags &= ~TEXF_ALPHA;
1233 for (i = 3;i < size;i += 4)
1237 flags |= TEXF_ALPHA;
1244 case TEXTYPE_SHADOWMAP16_COMP:
1245 case TEXTYPE_SHADOWMAP16_RAW:
1246 case TEXTYPE_SHADOWMAP24_COMP:
1247 case TEXTYPE_SHADOWMAP24_RAW:
1250 case TEXTYPE_SRGB_DXT1:
1253 case TEXTYPE_SRGB_DXT1A:
1255 case TEXTYPE_SRGB_DXT3:
1257 case TEXTYPE_SRGB_DXT5:
1258 flags |= TEXF_ALPHA;
1261 flags |= TEXF_ALPHA;
1263 case TEXTYPE_COLORBUFFER:
1264 case TEXTYPE_COLORBUFFER16F:
1265 case TEXTYPE_COLORBUFFER32F:
1266 flags |= TEXF_ALPHA;
1269 Sys_Error("R_LoadTexture: unknown texture type");
1272 texinfo2 = R_GetTexTypeInfo(textype, flags);
1273 if(size == width * height * depth * sides * texinfo->inputbytesperpixel)
1276 Con_Printf ("R_LoadTexture: input size changed after alpha fallback\n");
1278 glt = (gltexture_t *)Mem_ExpandableArray_AllocRecord(&texturearray);
1280 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1282 glt->chain = pool->gltchain;
1283 pool->gltchain = glt;
1284 glt->inputwidth = width;
1285 glt->inputheight = height;
1286 glt->inputdepth = depth;
1288 glt->miplevel = (miplevel < 0) ? R_PicmipForFlags(flags) : miplevel; // note: if miplevel is -1, we know the texture is in original size and we can picmip it normally
1289 glt->textype = texinfo;
1290 glt->texturetype = texturetype;
1291 glt->inputdatasize = size;
1292 glt->palette = palette;
1293 glt->glinternalformat = texinfo->glinternalformat;
1294 glt->glformat = texinfo->glformat;
1295 glt->gltype = texinfo->gltype;
1296 glt->bytesperpixel = texinfo->internalbytesperpixel;
1297 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1300 glt->glisdepthstencil = false;
1301 glt->gltexturetypeenum = gltexturetypeenums[glt->texturetype];
1302 // init the dynamic texture attributes, too [11/22/2007 Black]
1303 glt->updatecallback = NULL;
1304 glt->updatecallback_data = NULL;
1306 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->miplevel, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth, &glt->miplevels);
1308 // upload the texture
1309 // data may be NULL (blank texture for dynamic rendering)
1310 switch(vid.renderpath)
1312 case RENDERPATH_GL32:
1313 case RENDERPATH_GLES2:
1315 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
1319 R_UploadFullTexture(glt, data);
1320 if ((glt->flags & TEXF_ALLOWUPDATES) && gl_nopartialtextureupdates.integer)
1321 glt->bufferpixels = (unsigned char *)Mem_Alloc(texturemempool, glt->tilewidth*glt->tileheight*glt->tiledepth*glt->sides*glt->bytesperpixel);
1323 // free any temporary processing buffer we allocated...
1325 Mem_Free(temppixels);
1327 // texture converting and uploading can take a while, so make sure we're sending keepalives
1328 // FIXME: this causes rendering during R_Shadow_DrawLights
1329 // CL_KeepaliveMessage(false);
1331 return (rtexture_t *)glt;
1334 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const unsigned char *data, textype_t textype, int flags, int miplevel, const unsigned int *palette)
1336 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, miplevel, textype, GLTEXTURETYPE_2D, data, palette);
1339 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const unsigned char *data, textype_t textype, int flags, int miplevel, const unsigned int *palette)
1341 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, miplevel, textype, GLTEXTURETYPE_3D, data, palette);
1344 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, textype_t textype, int flags, int miplevel, const unsigned int *palette)
1346 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, miplevel, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1349 rtexture_t *R_LoadTextureShadowMap2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, textype_t textype, qboolean filter)
1351 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, TEXF_RENDERTARGET | TEXF_CLAMP | (filter ? TEXF_FORCELINEAR : TEXF_FORCENEAREST), -1, textype, GLTEXTURETYPE_2D, NULL, NULL);
1354 rtexture_t *R_LoadTextureRenderBuffer(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, textype_t textype)
1357 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1358 textypeinfo_t *texinfo;
1360 if (cls.state == ca_dedicated)
1363 texinfo = R_GetTexTypeInfo(textype, TEXF_RENDERTARGET | TEXF_CLAMP);
1365 glt = (gltexture_t *)Mem_ExpandableArray_AllocRecord(&texturearray);
1367 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1369 glt->chain = pool->gltchain;
1370 pool->gltchain = glt;
1371 glt->inputwidth = width;
1372 glt->inputheight = height;
1373 glt->inputdepth = 1;
1374 glt->flags = TEXF_RENDERTARGET | TEXF_CLAMP | TEXF_FORCENEAREST;
1376 glt->textype = texinfo;
1377 glt->texturetype = textype;
1378 glt->inputdatasize = width*height*texinfo->internalbytesperpixel;
1379 glt->palette = NULL;
1380 glt->glinternalformat = texinfo->glinternalformat;
1381 glt->glformat = texinfo->glformat;
1382 glt->gltype = texinfo->gltype;
1383 glt->bytesperpixel = texinfo->internalbytesperpixel;
1384 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1387 glt->glisdepthstencil = textype == TEXTYPE_DEPTHBUFFER24STENCIL8;
1388 glt->gltexturetypeenum = GL_TEXTURE_2D;
1389 // init the dynamic texture attributes, too [11/22/2007 Black]
1390 glt->updatecallback = NULL;
1391 glt->updatecallback_data = NULL;
1393 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->miplevel, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth, &glt->miplevels);
1395 // upload the texture
1396 // data may be NULL (blank texture for dynamic rendering)
1397 switch(vid.renderpath)
1399 case RENDERPATH_GL32:
1400 case RENDERPATH_GLES2:
1402 qglGenRenderbuffers(1, (GLuint *)&glt->renderbuffernum);CHECKGLERROR
1403 qglBindRenderbuffer(GL_RENDERBUFFER, glt->renderbuffernum);CHECKGLERROR
1404 qglRenderbufferStorage(GL_RENDERBUFFER, glt->glinternalformat, glt->tilewidth, glt->tileheight);CHECKGLERROR
1405 // note we can query the renderbuffer for info with glGetRenderbufferParameteriv for GL_WIDTH, GL_HEIGHt, GL_RED_SIZE, GL_GREEN_SIZE, GL_BLUE_SIZE, GL_GL_ALPHA_SIZE, GL_DEPTH_SIZE, GL_STENCIL_SIZE, GL_INTERNAL_FORMAT
1406 qglBindRenderbuffer(GL_RENDERBUFFER, 0);CHECKGLERROR
1410 return (rtexture_t *)glt;
1413 int R_SaveTextureDDSFile(rtexture_t *rt, const char *filename, qboolean skipuncompressed, qboolean hasalpha)
1416 return -1; // unsupported on this platform
1418 gltexture_t *glt = (gltexture_t *)rt;
1421 int bytesperpixel = 0;
1422 int bytesperblock = 0;
1424 int dds_format_flags;
1432 GLint internalformat;
1433 const char *ddsfourcc;
1435 return -1; // NULL pointer
1436 if (!strcmp(gl_version, "2.0.5885 WinXP Release"))
1437 return -2; // broken driver - crashes on reading internal format
1438 if (!qglGetTexLevelParameteriv)
1440 GL_ActiveTexture(0);
1441 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]);
1442 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
1443 qglGetTexLevelParameteriv(gltexturetypeenums[glt->texturetype], 0, GL_TEXTURE_INTERNAL_FORMAT, &internalformat);
1444 switch(internalformat)
1446 default: ddsfourcc = NULL;bytesperpixel = 4;break;
1447 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
1448 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: ddsfourcc = "DXT1";bytesperblock = 8;break;
1449 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: ddsfourcc = "DXT3";bytesperblock = 16;break;
1450 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: ddsfourcc = "DXT5";bytesperblock = 16;break;
1452 // if premultiplied alpha, say so in the DDS file
1453 if(glt->flags & TEXF_RGBMULTIPLYBYALPHA)
1455 switch(internalformat)
1457 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: ddsfourcc = "DXT2";break;
1458 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: ddsfourcc = "DXT4";break;
1461 if (!bytesperblock && skipuncompressed)
1462 return -3; // skipped
1463 memset(mipinfo, 0, sizeof(mipinfo));
1464 mipinfo[0][0] = glt->tilewidth;
1465 mipinfo[0][1] = glt->tileheight;
1467 if ((glt->flags & TEXF_MIPMAP) && !(glt->tilewidth == 1 && glt->tileheight == 1))
1469 for (mip = 1;mip < 16;mip++)
1471 mipinfo[mip][0] = mipinfo[mip-1][0] > 1 ? mipinfo[mip-1][0] >> 1 : 1;
1472 mipinfo[mip][1] = mipinfo[mip-1][1] > 1 ? mipinfo[mip-1][1] >> 1 : 1;
1473 if (mipinfo[mip][0] == 1 && mipinfo[mip][1] == 1)
1481 for (mip = 0;mip < mipmaps;mip++)
1483 mipinfo[mip][2] = bytesperblock ? ((mipinfo[mip][0]+3)/4)*((mipinfo[mip][1]+3)/4)*bytesperblock : mipinfo[mip][0]*mipinfo[mip][1]*bytesperpixel;
1484 mipinfo[mip][3] = ddssize;
1485 ddssize += mipinfo[mip][2];
1487 dds = (unsigned char *)Mem_Alloc(tempmempool, ddssize);
1490 dds_caps1 = 0x1000; // DDSCAPS_TEXTURE
1494 dds_flags = 0x81007; // DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_LINEARSIZE
1495 dds_format_flags = 0x4; // DDPF_FOURCC
1499 dds_flags = 0x100F; // DDSD_CAPS | DDSD_PIXELFORMAT | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PITCH
1500 dds_format_flags = 0x40; // DDPF_RGB
1504 dds_flags |= 0x20000; // DDSD_MIPMAPCOUNT
1505 dds_caps1 |= 0x400008; // DDSCAPS_MIPMAP | DDSCAPS_COMPLEX
1508 dds_format_flags |= 0x1; // DDPF_ALPHAPIXELS
1509 memcpy(dds, "DDS ", 4);
1510 StoreLittleLong(dds+4, 124); // http://msdn.microsoft.com/en-us/library/bb943982%28v=vs.85%29.aspx says so
1511 StoreLittleLong(dds+8, dds_flags);
1512 StoreLittleLong(dds+12, mipinfo[0][1]); // height
1513 StoreLittleLong(dds+16, mipinfo[0][0]); // width
1514 StoreLittleLong(dds+24, 0); // depth
1515 StoreLittleLong(dds+28, mipmaps); // mipmaps
1516 StoreLittleLong(dds+76, 32); // format size
1517 StoreLittleLong(dds+80, dds_format_flags);
1518 StoreLittleLong(dds+108, dds_caps1);
1519 StoreLittleLong(dds+112, dds_caps2);
1522 StoreLittleLong(dds+20, mipinfo[0][2]); // linear size
1523 memcpy(dds+84, ddsfourcc, 4);
1524 for (mip = 0;mip < mipmaps;mip++)
1526 qglGetCompressedTexImage(gltexturetypeenums[glt->texturetype], mip, dds + mipinfo[mip][3]);CHECKGLERROR
1531 StoreLittleLong(dds+20, mipinfo[0][0]*bytesperpixel); // pitch
1532 StoreLittleLong(dds+88, bytesperpixel*8); // bits per pixel
1533 dds[94] = dds[97] = dds[100] = dds[107] = 255; // bgra byte order masks
1534 for (mip = 0;mip < mipmaps;mip++)
1536 qglGetTexImage(gltexturetypeenums[glt->texturetype], mip, GL_BGRA, GL_UNSIGNED_BYTE, dds + mipinfo[mip][3]);CHECKGLERROR
1539 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
1540 ret = FS_WriteFile(filename, dds, ddssize);
1542 return ret ? ddssize : -5;
1547 // ELUAN: FIXME: separate this code
1548 #include "ktx10/include/ktx.h"
1551 rtexture_t *R_LoadTextureDDSFile(rtexturepool_t *rtexturepool, const char *filename, qboolean srgb, int flags, qboolean *hasalphaflag, float *avgcolor, int miplevel, qboolean optionaltexture) // DDS textures are opaque, so miplevel isn't a pointer but just seen as a hint
1553 int i, size, dds_format_flags, dds_miplevels, dds_width, dds_height;
1556 int bytesperblock, bytesperpixel;
1559 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1560 textypeinfo_t *texinfo;
1561 int mip, mipwidth, mipheight, mipsize, mipsize_total;
1562 unsigned int c, r, g, b;
1563 GLint oldbindtexnum = 0;
1564 unsigned char *mippixels;
1565 unsigned char *mippixels_start;
1566 unsigned char *ddspixels;
1568 fs_offset_t ddsfilesize;
1569 unsigned int ddssize;
1570 qboolean force_swdecode;
1572 // ELUAN: FIXME: separate this code
1576 KTX_dimensions sizes;
1579 if (cls.state == ca_dedicated)
1583 // ELUAN: FIXME: separate this code
1584 if (vid.renderpath != RENDERPATH_GLES2)
1586 Con_DPrintf("KTX texture format is only supported on the GLES2 renderpath\n");
1590 // some textures are specified with extensions, so it becomes .tga.dds
1591 FS_StripExtension (filename, vabuf2, sizeof(vabuf2));
1592 FS_StripExtension (vabuf2, vabuf, sizeof(vabuf));
1593 FS_DefaultExtension (vabuf, ".ktx", sizeof(vabuf));
1594 strsize = strlen(vabuf);
1596 for (i = 0; i <= strsize - 4; i++) // copy null termination
1597 vabuf[i] = vabuf[i + 4];
1599 Con_DPrintf("Loading %s...\n", vabuf);
1600 dds = FS_LoadFile(vabuf, tempmempool, true, &ddsfilesize);
1601 ddssize = ddsfilesize;
1605 Con_DPrintf("Not found!\n");
1606 return NULL; // not found
1608 Con_DPrintf("Found!\n");
1610 if (flags & TEXF_ALPHA)
1612 Con_DPrintf("KTX texture with alpha not supported yet, disabling\n");
1613 flags &= ~TEXF_ALPHA;
1619 GLboolean isMipmapped;
1620 KTX_error_code ktxerror;
1622 glt = (gltexture_t *)Mem_ExpandableArray_AllocRecord(&texturearray);
1624 // texture uploading can take a while, so make sure we're sending keepalives
1625 CL_KeepaliveMessage(false);
1627 // create the texture object
1629 GL_ActiveTexture(0);
1630 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[GLTEXTURETYPE_2D]);
1631 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
1632 qglBindTexture(gltexturetypeenums[GLTEXTURETYPE_2D], glt->texnum);CHECKGLERROR
1634 // upload the texture
1635 // we need to restore the texture binding after finishing the upload
1637 // NOTE: some drivers fail with ETC1 NPOT (only PowerVR?). This may make the driver crash later.
1638 ktxerror = ktxLoadTextureM(dds, ddssize, &glt->texnum, &target, &sizes, &isMipmapped, &glerror,
1639 0, NULL);// can't CHECKGLERROR, the lib catches it
1641 // FIXME: delete texture if we fail here
1642 if (target != GL_TEXTURE_2D)
1644 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
1646 Con_DPrintf("%s target != GL_TEXTURE_2D, target == %x\n", vabuf, target);
1647 return NULL; // FIXME: delete the texture from memory
1650 if (KTX_SUCCESS == ktxerror)
1652 textype = TEXTYPE_ETC1;
1653 flags &= ~TEXF_COMPRESS; // don't let the textype be wrong
1655 // return whether this texture is transparent
1657 *hasalphaflag = (flags & TEXF_ALPHA) != 0;
1659 // TODO: apply gl_picmip
1662 // TODO: only load mipmaps if requested
1665 flags |= TEXF_MIPMAP;
1667 flags &= ~TEXF_MIPMAP;
1669 texinfo = R_GetTexTypeInfo(textype, flags);
1671 strlcpy (glt->identifier, vabuf, sizeof(glt->identifier));
1673 glt->chain = pool->gltchain;
1674 pool->gltchain = glt;
1675 glt->inputwidth = sizes.width;
1676 glt->inputheight = sizes.height;
1677 glt->inputdepth = 1;
1679 glt->textype = texinfo;
1680 glt->texturetype = GLTEXTURETYPE_2D;
1681 glt->inputdatasize = ddssize;
1682 glt->glinternalformat = texinfo->glinternalformat;
1683 glt->glformat = texinfo->glformat;
1684 glt->gltype = texinfo->gltype;
1685 glt->bytesperpixel = texinfo->internalbytesperpixel;
1687 glt->gltexturetypeenum = gltexturetypeenums[glt->texturetype];
1688 glt->tilewidth = sizes.width;
1689 glt->tileheight = sizes.height;
1691 glt->miplevels = isMipmapped ? 1 : 0; // FIXME
1693 // after upload we have to set some parameters...
1694 #ifdef GL_TEXTURE_MAX_LEVEL
1696 if (dds_miplevels >= 1 && !mipcomplete)
1698 // need to set GL_TEXTURE_MAX_LEVEL
1699 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_LEVEL, dds_miplevels - 1);CHECKGLERROR
1703 GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype);
1705 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
1707 return (rtexture_t *)glt;
1711 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
1713 Con_DPrintf("KTX texture %s failed to load: %x\n", vabuf, ktxerror);
1717 #endif // __ANDROID__
1719 dds = FS_LoadFile(filename, tempmempool, true, &ddsfilesize);
1720 ddssize = ddsfilesize;
1724 if (r_texture_dds_load_logfailure.integer && (r_texture_dds_load_logfailure.integer >= 2 || !optionaltexture))
1725 Log_Printf("ddstexturefailures.log", "%s\n", filename);
1726 return NULL; // not found
1729 if (ddsfilesize <= 128 || memcmp(dds, "DDS ", 4) || ddssize < (unsigned int)BuffLittleLong(dds+4) || BuffLittleLong(dds+76) != 32)
1732 Con_Printf("^1%s: not a DDS image\n", filename);
1736 //dds_flags = BuffLittleLong(dds+8);
1737 dds_format_flags = BuffLittleLong(dds+80);
1738 dds_miplevels = (BuffLittleLong(dds+108) & 0x400000) ? BuffLittleLong(dds+28) : 1;
1739 dds_width = BuffLittleLong(dds+16);
1740 dds_height = BuffLittleLong(dds+12);
1741 ddspixels = dds + 128;
1743 if(r_texture_dds_load_alphamode.integer == 0)
1744 if(!(dds_format_flags & 0x1)) // DDPF_ALPHAPIXELS
1745 flags &= ~TEXF_ALPHA;
1747 //flags &= ~TEXF_ALPHA; // disabled, as we DISABLE TEXF_ALPHA in the alpha detection, not enable it!
1748 if ((dds_format_flags & 0x40) && BuffLittleLong(dds+88) == 32)
1750 // very sloppy BGRA 32bit identification
1751 textype = TEXTYPE_BGRA;
1752 flags &= ~TEXF_COMPRESS; // don't let the textype be wrong
1755 size = INTOVERFLOW_MUL(INTOVERFLOW_MUL(dds_width, dds_height), bytesperpixel);
1756 if(INTOVERFLOW_ADD(128, size) > INTOVERFLOW_NORMALIZE(ddsfilesize))
1759 Con_Printf("^1%s: invalid BGRA DDS image\n", filename);
1762 if((r_texture_dds_load_alphamode.integer == 1) && (flags & TEXF_ALPHA))
1765 for (i = 3;i < size;i += 4)
1766 if (ddspixels[i] < 255)
1769 flags &= ~TEXF_ALPHA;
1772 else if (!memcmp(dds+84, "DXT1", 4))
1774 // we need to find out if this is DXT1 (opaque) or DXT1A (transparent)
1775 // LordHavoc: it is my belief that this does not infringe on the
1776 // patent because it is not decoding pixels...
1777 textype = TEXTYPE_DXT1;
1780 //size = ((dds_width+3)/4)*((dds_height+3)/4)*bytesperblock;
1781 size = INTOVERFLOW_MUL(INTOVERFLOW_MUL(INTOVERFLOW_DIV(INTOVERFLOW_ADD(dds_width, 3), 4), INTOVERFLOW_DIV(INTOVERFLOW_ADD(dds_height, 3), 4)), bytesperblock);
1782 if(INTOVERFLOW_ADD(128, size) > INTOVERFLOW_NORMALIZE(ddsfilesize))
1785 Con_Printf("^1%s: invalid DXT1 DDS image\n", filename);
1788 if (flags & TEXF_ALPHA)
1790 if (r_texture_dds_load_alphamode.integer == 1)
1793 for (i = 0;i < size;i += bytesperblock)
1794 if (ddspixels[i+0] + ddspixels[i+1] * 256 <= ddspixels[i+2] + ddspixels[i+3] * 256)
1796 // NOTE: this assumes sizeof(unsigned int) == 4
1797 unsigned int data = * (unsigned int *) &(ddspixels[i+4]);
1798 // check if data, in base 4, contains a digit 3 (DXT1: transparent pixel)
1799 if(data & (data<<1) & 0xAAAAAAAA)//rgh
1803 textype = TEXTYPE_DXT1A;
1805 flags &= ~TEXF_ALPHA;
1807 else if (r_texture_dds_load_alphamode.integer == 0)
1808 textype = TEXTYPE_DXT1A;
1811 flags &= ~TEXF_ALPHA;
1815 else if (!memcmp(dds+84, "DXT3", 4) || !memcmp(dds+84, "DXT2", 4))
1817 if(!memcmp(dds+84, "DXT2", 4))
1819 if(!(flags & TEXF_RGBMULTIPLYBYALPHA))
1821 Con_Printf("^1%s: expecting DXT3 image without premultiplied alpha, got DXT2 image with premultiplied alpha\n", filename);
1826 if(flags & TEXF_RGBMULTIPLYBYALPHA)
1828 Con_Printf("^1%s: expecting DXT2 image without premultiplied alpha, got DXT3 image without premultiplied alpha\n", filename);
1831 textype = TEXTYPE_DXT3;
1834 size = INTOVERFLOW_MUL(INTOVERFLOW_MUL(INTOVERFLOW_DIV(INTOVERFLOW_ADD(dds_width, 3), 4), INTOVERFLOW_DIV(INTOVERFLOW_ADD(dds_height, 3), 4)), bytesperblock);
1835 if(INTOVERFLOW_ADD(128, size) > INTOVERFLOW_NORMALIZE(ddsfilesize))
1838 Con_Printf("^1%s: invalid DXT3 DDS image\n", filename);
1841 // we currently always assume alpha
1843 else if (!memcmp(dds+84, "DXT5", 4) || !memcmp(dds+84, "DXT4", 4))
1845 if(!memcmp(dds+84, "DXT4", 4))
1847 if(!(flags & TEXF_RGBMULTIPLYBYALPHA))
1849 Con_Printf("^1%s: expecting DXT5 image without premultiplied alpha, got DXT4 image with premultiplied alpha\n", filename);
1854 if(flags & TEXF_RGBMULTIPLYBYALPHA)
1856 Con_Printf("^1%s: expecting DXT4 image without premultiplied alpha, got DXT5 image without premultiplied alpha\n", filename);
1859 textype = TEXTYPE_DXT5;
1862 size = INTOVERFLOW_MUL(INTOVERFLOW_MUL(INTOVERFLOW_DIV(INTOVERFLOW_ADD(dds_width, 3), 4), INTOVERFLOW_DIV(INTOVERFLOW_ADD(dds_height, 3), 4)), bytesperblock);
1863 if(INTOVERFLOW_ADD(128, size) > INTOVERFLOW_NORMALIZE(ddsfilesize))
1866 Con_Printf("^1%s: invalid DXT5 DDS image\n", filename);
1869 // we currently always assume alpha
1874 Con_Printf("^1%s: unrecognized/unsupported DDS format\n", filename);
1878 // when requesting a non-alpha texture and we have DXT3/5, convert to DXT1
1879 if(!(flags & TEXF_ALPHA) && (textype == TEXTYPE_DXT3 || textype == TEXTYPE_DXT5))
1881 textype = TEXTYPE_DXT1;
1885 for (i = 0;i < (int)ddssize;i += bytesperblock)
1886 memcpy(&ddspixels[i], &ddspixels[(i<<1)+8], 8);
1890 force_swdecode = false;
1893 if(vid.support.ext_texture_compression_s3tc)
1895 if(r_texture_dds_swdecode.integer > 1)
1896 force_swdecode = true;
1900 if(r_texture_dds_swdecode.integer < 1)
1906 force_swdecode = true;
1910 // return whether this texture is transparent
1912 *hasalphaflag = (flags & TEXF_ALPHA) != 0;
1914 // if we SW decode, choose 2 sizes bigger
1917 // this is quarter res, so do not scale down more than we have to
1921 Con_DPrintf("WARNING: fake software decoding of compressed texture %s degraded quality\n", filename);
1924 // this is where we apply gl_picmip
1925 mippixels_start = ddspixels;
1926 mipwidth = dds_width;
1927 mipheight = dds_height;
1928 while(miplevel >= 1 && dds_miplevels >= 1)
1930 if (mipwidth <= 1 && mipheight <= 1)
1932 mipsize = bytesperblock ? ((mipwidth+3)/4)*((mipheight+3)/4)*bytesperblock : mipwidth*mipheight*bytesperpixel;
1933 mippixels_start += mipsize; // just skip
1941 mipsize_total = ddssize - 128 - (mippixels_start - ddspixels);
1942 mipsize = bytesperblock ? ((mipwidth+3)/4)*((mipheight+3)/4)*bytesperblock : mipwidth*mipheight*bytesperpixel;
1944 // from here on, we do not need the ddspixels and ddssize any more (apart from the statistics entry in glt)
1946 // fake decode S3TC if needed
1949 int mipsize_new = mipsize_total / bytesperblock * 4;
1950 unsigned char *mipnewpixels = (unsigned char *) Mem_Alloc(tempmempool, mipsize_new);
1951 unsigned char *p = mipnewpixels;
1952 for (i = bytesperblock == 16 ? 8 : 0;i < (int)mipsize_total;i += bytesperblock, p += 4)
1954 c = mippixels_start[i] + 256*mippixels_start[i+1] + 65536*mippixels_start[i+2] + 16777216*mippixels_start[i+3];
1955 p[2] = (((c >> 11) & 0x1F) + ((c >> 27) & 0x1F)) * (0.5f / 31.0f * 255.0f);
1956 p[1] = (((c >> 5) & 0x3F) + ((c >> 21) & 0x3F)) * (0.5f / 63.0f * 255.0f);
1957 p[0] = (((c ) & 0x1F) + ((c >> 16) & 0x1F)) * (0.5f / 31.0f * 255.0f);
1958 if(textype == TEXTYPE_DXT5)
1959 p[3] = (0.5 * mippixels_start[i-8] + 0.5 * mippixels_start[i-7]);
1960 else if(textype == TEXTYPE_DXT3)
1962 (mippixels_start[i-8] & 0x0F)
1963 + (mippixels_start[i-8] >> 4)
1964 + (mippixels_start[i-7] & 0x0F)
1965 + (mippixels_start[i-7] >> 4)
1966 + (mippixels_start[i-6] & 0x0F)
1967 + (mippixels_start[i-6] >> 4)
1968 + (mippixels_start[i-5] & 0x0F)
1969 + (mippixels_start[i-5] >> 4)
1970 ) * (0.125f / 15.0f * 255.0f);
1975 textype = TEXTYPE_BGRA;
1979 // as each block becomes a pixel, we must use pixel count for this
1980 mipwidth = (mipwidth + 3) / 4;
1981 mipheight = (mipheight + 3) / 4;
1982 mipsize = bytesperpixel * mipwidth * mipheight;
1983 mippixels_start = mipnewpixels;
1984 mipsize_total = mipsize_new;
1987 // start mip counting
1988 mippixels = mippixels_start;
1990 // calculate average color if requested
1994 Vector4Clear(avgcolor);
1997 for (i = bytesperblock == 16 ? 8 : 0;i < mipsize;i += bytesperblock)
1999 c = mippixels[i] + 256*mippixels[i+1] + 65536*mippixels[i+2] + 16777216*mippixels[i+3];
2000 avgcolor[0] += ((c >> 11) & 0x1F) + ((c >> 27) & 0x1F);
2001 avgcolor[1] += ((c >> 5) & 0x3F) + ((c >> 21) & 0x3F);
2002 avgcolor[2] += ((c ) & 0x1F) + ((c >> 16) & 0x1F);
2003 if(textype == TEXTYPE_DXT5)
2004 avgcolor[3] += (mippixels[i-8] + (int) mippixels[i-7]) * (0.5f / 255.0f);
2005 else if(textype == TEXTYPE_DXT3)
2007 (mippixels_start[i-8] & 0x0F)
2008 + (mippixels_start[i-8] >> 4)
2009 + (mippixels_start[i-7] & 0x0F)
2010 + (mippixels_start[i-7] >> 4)
2011 + (mippixels_start[i-6] & 0x0F)
2012 + (mippixels_start[i-6] >> 4)
2013 + (mippixels_start[i-5] & 0x0F)
2014 + (mippixels_start[i-5] >> 4)
2015 ) * (0.125f / 15.0f);
2017 avgcolor[3] += 1.0f;
2019 f = (float)bytesperblock / mipsize;
2020 avgcolor[0] *= (0.5f / 31.0f) * f;
2021 avgcolor[1] *= (0.5f / 63.0f) * f;
2022 avgcolor[2] *= (0.5f / 31.0f) * f;
2027 for (i = 0;i < mipsize;i += 4)
2029 avgcolor[0] += mippixels[i+2];
2030 avgcolor[1] += mippixels[i+1];
2031 avgcolor[2] += mippixels[i];
2032 avgcolor[3] += mippixels[i+3];
2034 f = (1.0f / 255.0f) * bytesperpixel / mipsize;
2042 // if we want sRGB, convert now
2045 if (vid.support.ext_texture_srgb)
2049 case TEXTYPE_DXT1: textype = TEXTYPE_SRGB_DXT1 ;break;
2050 case TEXTYPE_DXT1A: textype = TEXTYPE_SRGB_DXT1A ;break;
2051 case TEXTYPE_DXT3: textype = TEXTYPE_SRGB_DXT3 ;break;
2052 case TEXTYPE_DXT5: textype = TEXTYPE_SRGB_DXT5 ;break;
2053 case TEXTYPE_RGBA: textype = TEXTYPE_SRGB_RGBA ;break;
2067 for (i = bytesperblock == 16 ? 8 : 0;i < mipsize_total;i += bytesperblock)
2069 int c0, c1, c0new, c1new;
2070 c0 = mippixels_start[i] + 256*mippixels_start[i+1];
2071 r = ((c0 >> 11) & 0x1F);
2072 g = ((c0 >> 5) & 0x3F);
2074 r = floor(Image_LinearFloatFromsRGB(r * (255.0f / 31.0f)) * 31.0f + 0.5f); // these multiplications here get combined with multiplications in Image_LinearFloatFromsRGB
2075 g = floor(Image_LinearFloatFromsRGB(g * (255.0f / 63.0f)) * 63.0f + 0.5f); // these multiplications here get combined with multiplications in Image_LinearFloatFromsRGB
2076 b = floor(Image_LinearFloatFromsRGB(b * (255.0f / 31.0f)) * 31.0f + 0.5f); // these multiplications here get combined with multiplications in Image_LinearFloatFromsRGB
2077 c0new = (r << 11) | (g << 5) | b;
2078 c1 = mippixels_start[i+2] + 256*mippixels_start[i+3];
2079 r = ((c1 >> 11) & 0x1F);
2080 g = ((c1 >> 5) & 0x3F);
2082 r = floor(Image_LinearFloatFromsRGB(r * (255.0f / 31.0f)) * 31.0f + 0.5f); // these multiplications here get combined with multiplications in Image_LinearFloatFromsRGB
2083 g = floor(Image_LinearFloatFromsRGB(g * (255.0f / 63.0f)) * 63.0f + 0.5f); // these multiplications here get combined with multiplications in Image_LinearFloatFromsRGB
2084 b = floor(Image_LinearFloatFromsRGB(b * (255.0f / 31.0f)) * 31.0f + 0.5f); // these multiplications here get combined with multiplications in Image_LinearFloatFromsRGB
2085 c1new = (r << 11) | (g << 5) | b;
2086 // swap the colors if needed to fix order
2087 if(c0 > c1) // thirds
2095 mippixels_start[i+4] ^= 0x55;
2096 mippixels_start[i+5] ^= 0x55;
2097 mippixels_start[i+6] ^= 0x55;
2098 mippixels_start[i+7] ^= 0x55;
2100 else if(c0new == c1new)
2102 mippixels_start[i+4] = 0x00;
2103 mippixels_start[i+5] = 0x00;
2104 mippixels_start[i+6] = 0x00;
2105 mippixels_start[i+7] = 0x00;
2108 else // half + transparent
2115 mippixels_start[i+4] ^= (~mippixels_start[i+4] >> 1) & 0x55;
2116 mippixels_start[i+5] ^= (~mippixels_start[i+5] >> 1) & 0x55;
2117 mippixels_start[i+6] ^= (~mippixels_start[i+6] >> 1) & 0x55;
2118 mippixels_start[i+7] ^= (~mippixels_start[i+7] >> 1) & 0x55;
2121 mippixels_start[i] = c0new & 255;
2122 mippixels_start[i+1] = c0new >> 8;
2123 mippixels_start[i+2] = c1new & 255;
2124 mippixels_start[i+3] = c1new >> 8;
2129 Image_MakeLinearColorsFromsRGB(mippixels, mippixels, mipsize_total / bytesperblock);
2137 // when not requesting mipmaps, do not load them
2138 if(!(flags & TEXF_MIPMAP))
2141 if (dds_miplevels >= 1)
2142 flags |= TEXF_MIPMAP;
2144 flags &= ~TEXF_MIPMAP;
2146 texinfo = R_GetTexTypeInfo(textype, flags);
2148 glt = (gltexture_t *)Mem_ExpandableArray_AllocRecord(&texturearray);
2149 strlcpy (glt->identifier, filename, sizeof(glt->identifier));
2151 glt->chain = pool->gltchain;
2152 pool->gltchain = glt;
2153 glt->inputwidth = mipwidth;
2154 glt->inputheight = mipheight;
2155 glt->inputdepth = 1;
2157 glt->textype = texinfo;
2158 glt->texturetype = GLTEXTURETYPE_2D;
2159 glt->inputdatasize = ddssize;
2160 glt->glinternalformat = texinfo->glinternalformat;
2161 glt->glformat = texinfo->glformat;
2162 glt->gltype = texinfo->gltype;
2163 glt->bytesperpixel = texinfo->internalbytesperpixel;
2165 glt->gltexturetypeenum = gltexturetypeenums[glt->texturetype];
2166 glt->tilewidth = mipwidth;
2167 glt->tileheight = mipheight;
2169 glt->miplevels = dds_miplevels;
2171 // texture uploading can take a while, so make sure we're sending keepalives
2172 CL_KeepaliveMessage(false);
2174 // create the texture object
2175 switch(vid.renderpath)
2177 case RENDERPATH_GL32:
2178 case RENDERPATH_GLES2:
2180 GL_ActiveTexture(0);
2181 oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]);
2182 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
2183 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
2187 // upload the texture
2188 // we need to restore the texture binding after finishing the upload
2189 mipcomplete = false;
2191 for (mip = 0;mip <= dds_miplevels;mip++) // <= to include the not-counted "largest" miplevel
2193 unsigned char *upload_mippixels = mippixels;
2194 int upload_mipwidth = mipwidth;
2195 int upload_mipheight = mipheight;
2196 mipsize = bytesperblock ? ((mipwidth+3)/4)*((mipheight+3)/4)*bytesperblock : mipwidth*mipheight*bytesperpixel;
2197 if (mippixels + mipsize > mippixels_start + mipsize_total)
2199 switch(vid.renderpath)
2201 case RENDERPATH_GL32:
2202 case RENDERPATH_GLES2:
2205 qglCompressedTexImage2D(GL_TEXTURE_2D, mip, glt->glinternalformat, upload_mipwidth, upload_mipheight, 0, mipsize, upload_mippixels);CHECKGLERROR
2209 qglTexImage2D(GL_TEXTURE_2D, mip, glt->glinternalformat, upload_mipwidth, upload_mipheight, 0, glt->glformat, glt->gltype, upload_mippixels);CHECKGLERROR
2213 if(upload_mippixels != mippixels)
2214 Mem_Free(upload_mippixels);
2215 mippixels += mipsize;
2216 if (mipwidth <= 1 && mipheight <= 1)
2227 // after upload we have to set some parameters...
2228 switch(vid.renderpath)
2230 case RENDERPATH_GL32:
2231 case RENDERPATH_GLES2:
2232 #ifdef GL_TEXTURE_MAX_LEVEL
2233 if (dds_miplevels >= 1 && !mipcomplete)
2235 // need to set GL_TEXTURE_MAX_LEVEL
2236 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_LEVEL, dds_miplevels - 1);CHECKGLERROR
2239 GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype);
2240 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
2246 Mem_Free((unsigned char *) mippixels_start);
2247 return (rtexture_t *)glt;
2250 int R_TextureWidth(rtexture_t *rt)
2252 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
2255 int R_TextureHeight(rtexture_t *rt)
2257 return rt ? ((gltexture_t *)rt)->inputheight : 0;
2260 int R_TextureFlags(rtexture_t *rt)
2262 return rt ? ((gltexture_t *)rt)->flags : 0;
2265 void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, int z, int width, int height, int depth)
2267 gltexture_t *glt = (gltexture_t *)rt;
2269 Host_Error("R_UpdateTexture: no data supplied");
2271 Host_Error("R_UpdateTexture: no texture supplied");
2274 Con_DPrintf("R_UpdateTexture: texture %p \"%s\" in pool %p has not been uploaded yet\n", (void *)glt, glt->identifier, (void *)glt->pool);
2277 // update part of the texture
2278 if (glt->bufferpixels)
2281 int bpp = glt->bytesperpixel;
2282 int inputskip = width*bpp;
2283 int outputskip = glt->tilewidth*bpp;
2284 const unsigned char *input = data;
2285 unsigned char *output = glt->bufferpixels;
2286 if (glt->inputdepth != 1 || glt->sides != 1)
2287 Sys_Error("R_UpdateTexture on buffered texture that is not 2D\n");
2297 input -= y*inputskip;
2300 if (width > glt->tilewidth - x)
2301 width = glt->tilewidth - x;
2302 if (height > glt->tileheight - y)
2303 height = glt->tileheight - y;
2304 if (width < 1 || height < 1)
2307 glt->buffermodified = true;
2308 output += y*outputskip + x*bpp;
2309 for (j = 0;j < height;j++, output += outputskip, input += inputskip)
2310 memcpy(output, input, width*bpp);
2312 else if (x || y || z || width != glt->inputwidth || height != glt->inputheight || depth != glt->inputdepth)
2313 R_UploadPartialTexture(glt, data, x, y, z, width, height, depth);
2315 R_UploadFullTexture(glt, data);
2318 int R_RealGetTexture(rtexture_t *rt)
2323 glt = (gltexture_t *)rt;
2324 if (glt->flags & GLTEXF_DYNAMIC)
2325 R_UpdateDynamicTexture(glt);
2326 if (glt->buffermodified && glt->bufferpixels)
2328 glt->buffermodified = false;
2329 R_UploadFullTexture(glt, glt->bufferpixels);
2335 return r_texture_white->texnum;
2338 void R_ClearTexture (rtexture_t *rt)
2340 gltexture_t *glt = (gltexture_t *)rt;
2342 R_UploadFullTexture(glt, NULL);
2345 int R_PicmipForFlags(int flags)
2348 if(flags & TEXF_PICMIP)
2350 miplevel += gl_picmip.integer;
2351 if (flags & TEXF_ISWORLD)
2353 if (r_picmipworld.integer)
2354 miplevel += gl_picmip_world.integer;
2358 else if (flags & TEXF_ISSPRITE)
2360 if (r_picmipsprites.integer)
2361 miplevel += gl_picmip_sprites.integer;
2366 miplevel += gl_picmip_other.integer;
2368 return max(0, miplevel);