// note: this must not conflict with TEXF_ flags in r_textures.h
// cleared when a texture is uploaded
#define GLTEXF_UPLOAD 0x00010000
-// texture generated by code, also causes permanent GLTEXF_UPLOAD effect
-#define GLTEXF_PROCEDURAL 0x00020000
// bitmask for mismatch checking
-#define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL)
+#define GLTEXF_IMPORTANTBITS (0)
// set when image is uploaded and freed
#define GLTEXF_DESTROYED 0x00040000
textypeinfo_t;
static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
-static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 3};
+static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 1};
static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3, 1};
static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4, 1};
typedef struct gltexture_s
{
+ // this field is exposed to the R_GetTexture macro, for speed reasons
+ // (must be identical in rtexture_t)
+ int texnum; // GL texture slot number
+
// pointer to texturepool (check this to see if the texture is allocated)
struct gltexturepool_s *pool;
// pointer to next texture in texturepool chain
qbyte *inputtexels;
// to identify cache mismatchs (this might be removed someday)
int crc;
- // flags supplied to the LoadTexture/ProceduralTexture functions
+ // flags supplied to the LoadTexture function
// (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
int flags;
- // procedural texture generation function, called once per frame if the texture is used
- int (*generate)(qbyte *buffer, int width, int height, void *parameterdata, int parameterdatasize);
- // data provided to generate, persistent from call to call
- qbyte *proceduraldata;
- // size of data
- int proceduraldatasize;
- // used only to avoid updating the texture more than once per frame
- int proceduralframecount;
// pointer to one of the textype_ structs
textypeinfo_t *textype;
}
R_UploadTexture(glt);
}
-int R_GetTexture(rtexture_t *rt)
+int R_RealGetTexture(rtexture_t *rt)
{
- gltexture_t *glt;
- if (!rt)
- return 0;
- glt = (gltexture_t *)rt;
- if (glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL))
+ if (rt)
{
- if (glt->flags & GLTEXF_PROCEDURAL)
- {
- if (glt->proceduralframecount != r_framecount)
- {
- glt->proceduralframecount = r_framecount;
- R_UploadTexture(glt);
- }
- }
- else
+ gltexture_t *glt;
+ glt = (gltexture_t *)rt;
+ if (glt->flags & GLTEXF_UPLOAD)
R_UploadTexture(glt);
+ glt->texnum = glt->image->texnum;
+ return glt->image->texnum;
}
- return glt->image->texnum;
+ else
+ return 0;
}
void R_FreeTexture(rtexture_t *rt)
Mem_Free(glt->identifier);
if (glt->inputtexels)
Mem_Free(glt->inputtexels);
- if (glt->proceduraldata)
- Mem_Free(glt->proceduraldata);
Mem_Free(glt);
}
for (glt = pool->gltchain;glt;glt = glt->chain)
{
loaded = !(glt->flags & GLTEXF_UPLOAD);
- if (glt->flags & GLTEXF_PROCEDURAL)
- Con_Printf("%c%4i%c %4i PROC %s %s %s %s\n" , loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
- else
- Con_Printf("%c%4i%c%c%4i%c %04X %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->crc, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
+ Con_Printf("%c%4i%c%c%4i%c %04X %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->crc, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
}
Con_Printf("pool %10p\n", pool);
}
// deal with size limits of various drivers (3dfx in particular)
qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
CHECKGLERROR
+ // LordHavoc: allow any alignment
+ qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
+ CHECKGLERROR
// use the largest scrap texture size we can (not sure if this is really a good idea)
for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
}
+void R_Textures_Frame (void)
+{
+ // could do procedural texture animation here, if we keep track of which
+ // textures were accessed this frame...
+
+ // free the resize buffers
+ resizebuffersize = 0;
+ if (resizebuffer)
+ {
+ Mem_Free(resizebuffer);
+ resizebuffer = NULL;
+ }
+ if (colorconvertbuffer)
+ {
+ Mem_Free(colorconvertbuffer);
+ colorconvertbuffer = NULL;
+ }
+}
+
+void R_MakeResizeBufferBigger(int size)
+{
+ if (resizebuffersize < size)
+ {
+ resizebuffersize = size;
+ if (resizebuffer)
+ Mem_Free(resizebuffer);
+ if (colorconvertbuffer)
+ Mem_Free(colorconvertbuffer);
+ resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
+ colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
+ if (!resizebuffer || !colorconvertbuffer)
+ Host_Error("R_Upload: out of memory\n");
+ }
+}
+
static void R_Upload(gltexture_t *glt, qbyte *data)
{
int mip, width, height, internalformat;
if (glt->flags & TEXF_FRAGMENT)
{
- if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
- {
- resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
- if (resizebuffer)
- Mem_Free(resizebuffer);
- if (colorconvertbuffer)
- Mem_Free(colorconvertbuffer);
- resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
- colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
- if (!resizebuffer || !colorconvertbuffer)
- Host_Error("R_Upload: out of memory\n");
- }
-
if (glt->image->flags & GLTEXF_UPLOAD)
{
Con_DPrintf("uploaded new fragments image\n");
+ R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->bytesperpixel);
glt->image->flags &= ~GLTEXF_UPLOAD;
memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
qglTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
if (prevbuffer == NULL)
{
+ R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->bytesperpixel);
memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
prevbuffer = resizebuffer;
}
{
// promote paletted to RGBA, so we only have to worry about RGB and
// RGBA in the rest of this code
+ R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->bytesperpixel);
Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
prevbuffer = colorconvertbuffer;
}
qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
CHECKGLERROR
+ glt->texnum = glt->image->texnum;
return;
}
for (width = 1;width < glt->width;width <<= 1);
for (height = 1;height < glt->height;height <<= 1);
- if (resizebuffersize < width * height * glt->image->bytesperpixel)
- {
- resizebuffersize = width * height * glt->image->bytesperpixel;
- if (resizebuffer)
- Mem_Free(resizebuffer);
- if (colorconvertbuffer)
- Mem_Free(colorconvertbuffer);
- resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
- colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
- if (!resizebuffer || !colorconvertbuffer)
- Host_Error("R_Upload: out of memory\n");
- }
+ R_MakeResizeBufferBigger(width * height * glt->image->bytesperpixel);
if (prevbuffer == NULL)
{
qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
CHECKGLERROR
}
+ glt->texnum = glt->image->texnum;
}
static void R_FindImageForTexture(gltexture_t *glt)
texinfo = glt->textype;
pool = glt->pool;
+ // remains -1 until uploaded
+ glt->texnum = -1;
+
x = 0;
y = 0;
w = glt->width;
// note: R_FindImageForTexture must be called before this
static void R_UploadTexture (gltexture_t *glt)
{
- if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
+ if (!(glt->flags & GLTEXF_UPLOAD))
return;
- if (glt->flags & GLTEXF_PROCEDURAL)
- {
- if (glt->generate)
- {
- if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
- {
- if (texturebuffer)
- Mem_Free(texturebuffer);
- texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
- texturebuffer = Mem_Alloc(textureprocessingmempool, texturebuffersize);
- }
-
- glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
- }
- }
- else
+ R_Upload(glt, glt->inputtexels);
+ if (glt->inputtexels)
{
- R_Upload(glt, glt->inputtexels);
- if (glt->inputtexels)
- {
- Mem_Free(glt->inputtexels);
- glt->inputtexels = NULL;
- glt->flags |= GLTEXF_DESTROYED;
- }
- else if (glt->flags & GLTEXF_DESTROYED)
- Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
+ Mem_Free(glt->inputtexels);
+ glt->inputtexels = NULL;
+ glt->flags |= GLTEXF_DESTROYED;
}
+ else if (glt->flags & GLTEXF_DESTROYED)
+ Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
}
-static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data, int (*generate)(qbyte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
+static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data)
{
gltexture_t *glt;
glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
else
glt->inputtexels = NULL;
- glt->generate = generate;
- glt->proceduraldatasize = proceduraldatasize;
- if (proceduraldatasize)
- {
- glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
- if (glt->proceduraldata == NULL)
- Sys_Error("R_SetupTexture: out of memory\n");
- }
- else
- glt->proceduraldata = NULL;
-
R_FindImageForTexture(glt);
R_PrecacheTexture(glt);
R_FreeTexture((rtexture_t *)glt);
}
- return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
-}
-
-rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, int textype, int flags, int (*generate)(qbyte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
-{
- gltexture_t *glt;
- gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
- textypeinfo_t *texinfo;
-
- if (cls.state == ca_dedicated)
- return NULL;
-
- texinfo = R_GetTexTypeInfo(textype, flags);
-
- if (flags & TEXF_FRAGMENT)
- if ((width * texinfo->internalbytesperpixel) & 3)
- Host_Error("R_ProceduralTexture: incompatible width for fragment");
-
- // see if the texture is already present
- if (identifier && (glt = R_FindTexture(pool, identifier)))
- {
- if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
- {
- Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
- return (rtexture_t *)glt; // exact match, use existing
- }
- Con_Printf("R_LoadTexture: cache mismatch, replacing old texture\n");
- R_FreeTexture((rtexture_t *)glt);
- }
-
- return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
+ return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data);
}
int R_TextureHasAlpha(rtexture_t *rt)