+#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__
+