X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=gl_textures.c;h=29d325ed68763bc7c0792b3e32fc80b443caab2c;hb=dd4ca2a5d4848141aefcca6987e71e1734a50856;hp=57c257f27d85d1ecfff4e5909a8e8a426d8c4ef9;hpb=e3ca095035958bf6ca16e8a1dcfa506e3fd23884;p=xonotic%2Fdarkplaces.git diff --git a/gl_textures.c b/gl_textures.c index 57c257f2..29d325ed 100644 --- a/gl_textures.c +++ b/gl_textures.c @@ -81,6 +81,11 @@ typedef struct textypeinfo_s textypeinfo_t; #ifdef USE_GLES2 + +// we use these internally even if we never deliver such data to the driver +#define GL_BGR 0x80E0 +#define GL_BGRA 0x80E1 + // framebuffer texture formats // GLES2 devices rarely support depth textures, so we actually use a renderbuffer there static textypeinfo_t textype_shadowmap16_comp = {"shadowmap16_comp", TEXTYPE_SHADOWMAP16_COMP , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT}; @@ -91,8 +96,8 @@ static textypeinfo_t textype_depth16 = {"depth16", static textypeinfo_t textype_depth24 = {"depth24", TEXTYPE_DEPTHBUFFER24 , 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT}; static textypeinfo_t textype_depth24stencil8 = {"depth24stencil8", TEXTYPE_DEPTHBUFFER24STENCIL8, 2, 2, 2.0f, GL_DEPTH_COMPONENT16 , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT}; static textypeinfo_t textype_colorbuffer = {"colorbuffer", TEXTYPE_COLORBUFFER , 2, 2, 2.0f, GL_RGB565 , GL_RGBA , GL_UNSIGNED_SHORT_5_6_5}; -static textypeinfo_t textype_colorbuffer16f = {"colorbuffer16f", TEXTYPE_COLORBUFFER16F , 2, 2, 2.0f, GL_RGB565 , GL_RGBA , GL_UNSIGNED_SHORT_5_6_5}; -static textypeinfo_t textype_colorbuffer32f = {"colorbuffer32f", TEXTYPE_COLORBUFFER32F , 2, 2, 2.0f, GL_RGB565 , GL_RGBA , GL_UNSIGNED_SHORT_5_6_5}; +static textypeinfo_t textype_colorbuffer16f = {"colorbuffer16f", TEXTYPE_COLORBUFFER16F , 2, 2, 2.0f, GL_RGBA16F , GL_RGBA , GL_HALF_FLOAT_ARB}; +static textypeinfo_t textype_colorbuffer32f = {"colorbuffer32f", TEXTYPE_COLORBUFFER32F , 2, 2, 2.0f, GL_RGBA32F , GL_RGBA , GL_FLOAT}; // image formats: static textypeinfo_t textype_alpha = {"alpha", TEXTYPE_ALPHA , 1, 4, 4.0f, GL_ALPHA , GL_ALPHA , GL_UNSIGNED_BYTE }; @@ -102,6 +107,9 @@ static textypeinfo_t textype_rgba = {"rgba", static textypeinfo_t textype_rgba_alpha = {"rgba_alpha", TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , GL_RGBA , GL_UNSIGNED_BYTE }; static textypeinfo_t textype_bgra = {"bgra", TEXTYPE_BGRA , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE }; static textypeinfo_t textype_bgra_alpha = {"bgra_alpha", TEXTYPE_BGRA , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE }; +#ifdef __ANDROID__ +static textypeinfo_t textype_etc1 = {"etc1", TEXTYPE_ETC1 , 1, 3, 0.5f, GL_ETC1_RGB8_OES , 0 , 0 }; +#endif #else // framebuffer texture formats static textypeinfo_t textype_shadowmap16_comp = {"shadowmap16_comp", TEXTYPE_SHADOWMAP16_COMP , 2, 2, 2.0f, GL_DEPTH_COMPONENT16_ARB , GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT}; @@ -112,7 +120,7 @@ static textypeinfo_t textype_depth16 = {"depth16", static textypeinfo_t textype_depth24 = {"depth24", TEXTYPE_DEPTHBUFFER24 , 4, 4, 4.0f, GL_DEPTH_COMPONENT24_ARB , GL_DEPTH_COMPONENT, GL_UNSIGNED_INT }; static textypeinfo_t textype_depth24stencil8 = {"depth24stencil8", TEXTYPE_DEPTHBUFFER24STENCIL8, 4, 4, 4.0f, GL_DEPTH24_STENCIL8_EXT , GL_DEPTH_STENCIL_EXT, GL_UNSIGNED_INT_24_8_EXT}; static textypeinfo_t textype_colorbuffer = {"colorbuffer", TEXTYPE_COLORBUFFER , 4, 4, 4.0f, GL_RGBA , GL_BGRA , GL_UNSIGNED_BYTE }; -static textypeinfo_t textype_colorbuffer16f = {"colorbuffer16f", TEXTYPE_COLORBUFFER16F , 8, 8, 8.0f, GL_RGBA16F_ARB , GL_RGBA , GL_FLOAT }; +static textypeinfo_t textype_colorbuffer16f = {"colorbuffer16f", TEXTYPE_COLORBUFFER16F , 8, 8, 8.0f, GL_RGBA16F_ARB , GL_RGBA , GL_HALF_FLOAT_ARB}; static textypeinfo_t textype_colorbuffer32f = {"colorbuffer32f", TEXTYPE_COLORBUFFER32F , 16, 16, 16.0f, GL_RGBA32F_ARB , GL_RGBA , GL_FLOAT }; // image formats: @@ -157,7 +165,9 @@ typedef enum gltexturetype_e gltexturetype_t; static int gltexturetypeenums[GLTEXTURETYPE_TOTAL] = {GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP}; +#ifdef GL_TEXTURE_WRAP_R static int gltexturetypedimensions[GLTEXTURETYPE_TOTAL] = {2, 3, 2}; +#endif static int cubemapside[6] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, @@ -199,7 +209,7 @@ typedef struct gltexture_s // dynamic texture stuff [11/22/2007 Black] updatecallback_t updatecallback; - void *updatacallback_data; + void *updatecallback_data; // --- [11/22/2007 Black] // stores backup copy of texture for deferred texture updates (gl_nopartialtextureupdates cvar) @@ -268,6 +278,25 @@ static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags) { switch(textype) { +#ifdef USE_GLES2 + case TEXTYPE_PALETTE: return (flags & TEXF_ALPHA) ? &textype_palette_alpha : &textype_palette; + case TEXTYPE_RGBA: return ((flags & TEXF_ALPHA) ? &textype_rgba_alpha : &textype_rgba); + case TEXTYPE_BGRA: return ((flags & TEXF_ALPHA) ? &textype_bgra_alpha : &textype_bgra); +#ifdef __ANDROID__ + case TEXTYPE_ETC1: return &textype_etc1; +#endif + case TEXTYPE_ALPHA: return &textype_alpha; + case TEXTYPE_COLORBUFFER: return &textype_colorbuffer; + case TEXTYPE_COLORBUFFER16F: return &textype_colorbuffer16f; + case TEXTYPE_COLORBUFFER32F: return &textype_colorbuffer32f; + case TEXTYPE_DEPTHBUFFER16: return &textype_depth16; + case TEXTYPE_DEPTHBUFFER24: return &textype_depth24; + case TEXTYPE_DEPTHBUFFER24STENCIL8: return &textype_depth24stencil8; + case TEXTYPE_SHADOWMAP16_COMP: return &textype_shadowmap16_comp; + case TEXTYPE_SHADOWMAP16_RAW: return &textype_shadowmap16_raw; + case TEXTYPE_SHADOWMAP24_COMP: return &textype_shadowmap24_comp; + case TEXTYPE_SHADOWMAP24_RAW: return &textype_shadowmap24_raw; +#else case TEXTYPE_DXT1: return &textype_dxt1; case TEXTYPE_DXT1A: return &textype_dxt1a; case TEXTYPE_DXT3: return &textype_dxt3; @@ -293,6 +322,7 @@ static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags) case TEXTYPE_SRGB_PALETTE: return (flags & TEXF_ALPHA) ? &textype_sRGB_palette_alpha : &textype_sRGB_palette; 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); 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); +#endif default: Host_Error("R_GetTexTypeInfo: unknown texture format"); break; @@ -323,13 +353,13 @@ void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void glt->flags |= GLTEXF_DYNAMIC; glt->updatecallback = updatecallback; - glt->updatacallback_data = data; + glt->updatecallback_data = data; } static void R_UpdateDynamicTexture(gltexture_t *glt) { glt->dirty = false; if( glt->updatecallback ) { - glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data ); + glt->updatecallback( (rtexture_t*) glt, glt->updatecallback_data ); } } @@ -825,7 +855,7 @@ static void r_textures_devicelost(void) { int i, endindex; gltexture_t *glt; - endindex = Mem_ExpandableArray_IndexRange(&texturearray); + endindex = (int)Mem_ExpandableArray_IndexRange(&texturearray); for (i = 0;i < endindex;i++) { glt = (gltexture_t *) Mem_ExpandableArray_RecordAtIndex(&texturearray, i); @@ -869,7 +899,7 @@ static void r_textures_devicerestored(void) { int i, endindex; gltexture_t *glt; - endindex = Mem_ExpandableArray_IndexRange(&texturearray); + endindex = (int)Mem_ExpandableArray_IndexRange(&texturearray); for (i = 0;i < endindex;i++) { glt = (gltexture_t *) Mem_ExpandableArray_RecordAtIndex(&texturearray, i); @@ -964,7 +994,10 @@ void R_Textures_Init (void) void R_Textures_Frame (void) { +#ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT static int old_aniso = 0; + static qboolean first_time_aniso = true; +#endif // could do procedural texture animation here, if we keep track of which // textures were accessed this frame... @@ -982,6 +1015,7 @@ void R_Textures_Frame (void) colorconvertbuffer = NULL; } +#ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT if (old_aniso != gl_texture_anisotropy.integer) { gltexture_t *glt; @@ -999,6 +1033,12 @@ void R_Textures_Frame (void) case RENDERPATH_GL20: case RENDERPATH_GLES1: case RENDERPATH_GLES2: + // ignore the first difference, any textures loaded by now probably had the same aniso value + if (first_time_aniso) + { + first_time_aniso = false; + break; + } CHECKGLERROR GL_ActiveTexture(0); for (pool = gltexturepoolchain;pool;pool = pool->next) @@ -1025,6 +1065,7 @@ void R_Textures_Frame (void) break; } } +#endif } static void R_MakeResizeBufferBigger(int size) @@ -1050,6 +1091,7 @@ static void GL_SetupTextureParameters(int flags, textype_t textype, int texturet CHECKGLERROR +#ifdef GL_TEXTURE_MAX_ANISOTROPY_EXT if (vid.support.ext_texture_filter_anisotropic && (flags & TEXF_MIPMAP)) { int aniso = bound(1, gl_texture_anisotropy.integer, (int)vid.max_anisotropy); @@ -1057,6 +1099,7 @@ static void GL_SetupTextureParameters(int flags, textype_t textype, int texturet Cvar_SetValueQuick(&gl_texture_anisotropy, aniso); qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR } +#endif qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR #ifdef GL_TEXTURE_WRAP_R @@ -1111,6 +1154,7 @@ static void GL_SetupTextureParameters(int flags, textype_t textype, int texturet qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR } +#ifdef GL_TEXTURE_COMPARE_MODE_ARB switch(textype) { case TEXTYPE_SHADOWMAP16_COMP: @@ -1128,6 +1172,7 @@ static void GL_SetupTextureParameters(int flags, textype_t textype, int texturet default: break; } +#endif CHECKGLERROR } @@ -1218,13 +1263,13 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) for (width = glt->tilewidth;width < glt->inputwidth ;width <<= 1); for (height = glt->tileheight;height < glt->inputheight;height <<= 1); for (depth = glt->tiledepth;depth < glt->inputdepth ;depth <<= 1); - R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); if (prevbuffer == NULL) { width = glt->tilewidth; height = glt->tileheight; depth = glt->tiledepth; +// R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); // memset(resizebuffer, 0, width * height * depth * glt->sides * glt->bytesperpixel); // prevbuffer = resizebuffer; } @@ -1233,6 +1278,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) if (glt->textype->textype == TEXTYPE_PALETTE) { // promote paletted to BGRA, so we only have to worry about BGRA in the rest of this code + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_Copy8bitBGRA(prevbuffer, colorconvertbuffer, glt->inputwidth * glt->inputheight * glt->inputdepth * glt->sides, glt->palette); prevbuffer = colorconvertbuffer; } @@ -1240,6 +1286,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { // multiply RGB channels by A channel before uploading int alpha; + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); for (i = 0;i < glt->inputwidth*glt->inputheight*glt->inputdepth*4;i += 4) { alpha = prevbuffer[i+3]; @@ -1253,12 +1300,14 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) // scale up to a power of 2 size (if appropriate) if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer); prevbuffer = resizebuffer; } // apply mipmap reduction algorithm to get down to picmip/max_size while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth); prevbuffer = resizebuffer; } @@ -1281,6 +1330,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[glt->texturetype]); qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR +#ifndef USE_GLES2 #ifdef GL_TEXTURE_COMPRESSION_HINT_ARB if (qglGetCompressedTexImageARB) { @@ -1290,6 +1340,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST); CHECKGLERROR } +#endif #endif switch(glt->texturetype) { @@ -1299,6 +1350,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { while (width > 1 || height > 1 || depth > 1) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1); prevbuffer = resizebuffer; qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR @@ -1312,6 +1364,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { while (width > 1 || height > 1 || depth > 1) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1); prevbuffer = resizebuffer; qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR @@ -1329,12 +1382,14 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel; if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer); prevbuffer = resizebuffer; } // picmip/max_size while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth); prevbuffer = resizebuffer; } @@ -1344,6 +1399,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { while (width > 1 || height > 1 || depth > 1) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1); prevbuffer = resizebuffer; qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR @@ -1378,6 +1434,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { while (width > 1 || height > 1 || depth > 1) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1); prevbuffer = resizebuffer; if (IDirect3DTexture9_LockRect((IDirect3DTexture9*)glt->d3dtexture, mip, &d3dlockedrect, NULL, 0) == D3D_OK && d3dlockedrect.pBits) @@ -1401,6 +1458,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { while (width > 1 || height > 1 || depth > 1) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1); prevbuffer = resizebuffer; if (IDirect3DVolumeTexture9_LockBox((IDirect3DVolumeTexture9*)glt->d3dtexture, mip, &d3dlockedbox, NULL, 0) == D3D_OK && d3dlockedbox.pBits) @@ -1423,12 +1481,14 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel; if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer); prevbuffer = resizebuffer; } // picmip/max_size while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth); prevbuffer = resizebuffer; } @@ -1443,6 +1503,7 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) { while (width > 1 || height > 1 || depth > 1) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1); prevbuffer = resizebuffer; if (IDirect3DCubeTexture9_LockRect((IDirect3DCubeTexture9*)glt->d3dtexture, (D3DCUBEMAP_FACES)i, mip, &d3dlockedrect, NULL, 0) == D3D_OK && d3dlockedrect.pBits) @@ -1530,12 +1591,14 @@ static void R_UploadFullTexture(gltexture_t *glt, const unsigned char *data) texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel; if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer); prevbuffer = resizebuffer; } // picmip/max_size while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth) { + R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel); Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth); prevbuffer = resizebuffer; } @@ -1624,7 +1687,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden case TEXTYPE_SRGB_DXT1A: textype = TEXTYPE_DXT1A ;convertsRGB = true;break; case TEXTYPE_SRGB_DXT3: textype = TEXTYPE_DXT3 ;convertsRGB = true;break; case TEXTYPE_SRGB_DXT5: textype = TEXTYPE_DXT5 ;convertsRGB = true;break; - case TEXTYPE_SRGB_PALETTE: textype = TEXTYPE_PALETTE;convertsRGB = true;break; + case TEXTYPE_SRGB_PALETTE: textype = TEXTYPE_PALETTE;/*convertsRGB = true;*/break; case TEXTYPE_SRGB_RGBA: textype = TEXTYPE_RGBA ;convertsRGB = true;break; case TEXTYPE_SRGB_BGRA: textype = TEXTYPE_BGRA ;convertsRGB = true;break; default: @@ -1763,7 +1826,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden glt->gltexturetypeenum = gltexturetypeenums[glt->texturetype]; // init the dynamic texture attributes, too [11/22/2007 Black] glt->updatecallback = NULL; - glt->updatacallback_data = NULL; + glt->updatecallback_data = NULL; GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->miplevel, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth, &glt->miplevels); @@ -1931,11 +1994,11 @@ rtexture_t *R_LoadTextureRenderBuffer(rtexturepool_t *rtexturepool, const char * glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1; glt->texnum = 0; glt->dirty = false; - glt->glisdepthstencil = glt->texturetype == TEXTYPE_DEPTHBUFFER24STENCIL8; - glt->gltexturetypeenum = gltexturetypeenums[glt->texturetype]; + glt->glisdepthstencil = textype == TEXTYPE_DEPTHBUFFER24STENCIL8; + glt->gltexturetypeenum = GL_TEXTURE_2D; // init the dynamic texture attributes, too [11/22/2007 Black] glt->updatecallback = NULL; - glt->updatacallback_data = NULL; + glt->updatecallback_data = NULL; GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->miplevel, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth, &glt->miplevels); @@ -2148,6 +2211,11 @@ int R_SaveTextureDDSFile(rtexture_t *rt, const char *filename, qboolean skipunco #endif } +#ifdef __ANDROID__ +// ELUAN: FIXME: separate this code +#include "ktx10/include/ktx.h" +#endif + 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 { int i, size, dds_format_flags, dds_miplevels, dds_width, dds_height; @@ -2168,10 +2236,154 @@ rtexture_t *R_LoadTextureDDSFile(rtexturepool_t *rtexturepool, const char *filen fs_offset_t ddsfilesize; unsigned int ddssize; qboolean force_swdecode, npothack; +#ifdef __ANDROID__ + // ELUAN: FIXME: separate this code + char vabuf[1024]; + char vabuf2[1024]; + int strsize; + KTX_dimensions sizes; +#endif if (cls.state == ca_dedicated) return NULL; +#ifdef __ANDROID__ + // ELUAN: FIXME: separate this code + if (vid.renderpath != RENDERPATH_GLES2) + { + Con_DPrintf("KTX texture format is only supported on the GLES2 renderpath\n"); + return NULL; + } + + // some textures are specified with extensions, so it becomes .tga.dds + FS_StripExtension (filename, vabuf2, sizeof(vabuf2)); + FS_StripExtension (vabuf2, vabuf, sizeof(vabuf)); + FS_DefaultExtension (vabuf, ".ktx", sizeof(vabuf)); + strsize = strlen(vabuf); + if (strsize > 5) + for (i = 0; i <= strsize - 4; i++) // copy null termination + vabuf[i] = vabuf[i + 4]; + + Con_DPrintf("Loading %s...\n", vabuf); + dds = FS_LoadFile(vabuf, tempmempool, true, &ddsfilesize); + ddssize = ddsfilesize; + + if (!dds) + { + Con_DPrintf("Not found!\n"); + return NULL; // not found + } + Con_DPrintf("Found!\n"); + + if (flags & TEXF_ALPHA) + { + Con_DPrintf("KTX texture with alpha not supported yet, disabling\n"); + flags &= ~TEXF_ALPHA; + } + + { + GLenum target; + GLenum glerror; + GLboolean isMipmapped; + KTX_error_code ktxerror; + + glt = (gltexture_t *)Mem_ExpandableArray_AllocRecord(&texturearray); + + // texture uploading can take a while, so make sure we're sending keepalives + CL_KeepaliveMessage(false); + + // create the texture object + CHECKGLERROR + GL_ActiveTexture(0); + oldbindtexnum = R_Mesh_TexBound(0, gltexturetypeenums[GLTEXTURETYPE_2D]); + qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR + qglBindTexture(gltexturetypeenums[GLTEXTURETYPE_2D], glt->texnum);CHECKGLERROR + + // upload the texture + // we need to restore the texture binding after finishing the upload + + // NOTE: some drivers fail with ETC1 NPOT (only PowerVR?). This may make the driver crash later. + ktxerror = ktxLoadTextureM(dds, ddssize, &glt->texnum, &target, &sizes, &isMipmapped, &glerror, + 0, NULL);// can't CHECKGLERROR, the lib catches it + + // FIXME: delete texture if we fail here + if (target != GL_TEXTURE_2D) + { + qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR + Mem_Free(dds); + Con_DPrintf("%s target != GL_TEXTURE_2D, target == %x\n", vabuf, target); + return NULL; // FIXME: delete the texture from memory + } + + if (KTX_SUCCESS == ktxerror) + { + textype = TEXTYPE_ETC1; + flags &= ~TEXF_COMPRESS; // don't let the textype be wrong + + // return whether this texture is transparent + if (hasalphaflag) + *hasalphaflag = (flags & TEXF_ALPHA) != 0; + + // TODO: apply gl_picmip + // TODO: avgcolor + // TODO: srgb + // TODO: only load mipmaps if requested + + if (isMipmapped) + flags |= TEXF_MIPMAP; + else + flags &= ~TEXF_MIPMAP; + + texinfo = R_GetTexTypeInfo(textype, flags); + + strlcpy (glt->identifier, vabuf, sizeof(glt->identifier)); + glt->pool = pool; + glt->chain = pool->gltchain; + pool->gltchain = glt; + glt->inputwidth = sizes.width; + glt->inputheight = sizes.height; + glt->inputdepth = 1; + glt->flags = flags; + glt->textype = texinfo; + glt->texturetype = GLTEXTURETYPE_2D; + glt->inputdatasize = ddssize; + glt->glinternalformat = texinfo->glinternalformat; + glt->glformat = texinfo->glformat; + glt->gltype = texinfo->gltype; + glt->bytesperpixel = texinfo->internalbytesperpixel; + glt->sides = 1; + glt->gltexturetypeenum = gltexturetypeenums[glt->texturetype]; + glt->tilewidth = sizes.width; + glt->tileheight = sizes.height; + glt->tiledepth = 1; + glt->miplevels = isMipmapped ? 1 : 0; // FIXME + + // after upload we have to set some parameters... +#ifdef GL_TEXTURE_MAX_LEVEL + /* FIXME + if (dds_miplevels >= 1 && !mipcomplete) + { + // need to set GL_TEXTURE_MAX_LEVEL + qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_LEVEL, dds_miplevels - 1);CHECKGLERROR + } + */ +#endif + GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype); + + qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR + Mem_Free(dds); + return (rtexture_t *)glt; + } + else + { + qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR + Mem_Free(dds); + Con_DPrintf("KTX texture %s failed to load: %x\n", vabuf, ktxerror); + return NULL; + } + } +#endif // __ANDROID__ + dds = FS_LoadFile(filename, tempmempool, true, &ddsfilesize); ddssize = ddsfilesize;