Allocate a buffer for non-RGBA to RGBA texture conversions.

UpdateTexture will I_Error (from AllocTextureBuffer) if the allocation fails.
This commit is contained in:
Jaime Ita Passos 2021-03-23 01:18:28 -03:00 committed by toaster
parent 4c9c54e44e
commit 5539341ebc

View file

@ -131,7 +131,6 @@ static const GLfloat byte2float[256] = {
// -----------------+ // -----------------+
// GL_DBG_Printf : Output debug messages to debug log if DEBUG_TO_FILE is defined, // GL_DBG_Printf : Output debug messages to debug log if DEBUG_TO_FILE is defined,
// : else do nothing // : else do nothing
// Returns :
// -----------------+ // -----------------+
#ifdef DEBUG_TO_FILE #ifdef DEBUG_TO_FILE
@ -159,8 +158,6 @@ FUNCPRINTF void GL_DBG_Printf(const char *format, ...)
// -----------------+ // -----------------+
// GL_MSG_Warning : Raises a warning. // GL_MSG_Warning : Raises a warning.
// :
// Returns :
// -----------------+ // -----------------+
static void GL_MSG_Warning(const char *format, ...) static void GL_MSG_Warning(const char *format, ...)
@ -184,8 +181,6 @@ static void GL_MSG_Warning(const char *format, ...)
// -----------------+ // -----------------+
// GL_MSG_Error : Raises an error. // GL_MSG_Error : Raises an error.
// :
// Returns :
// -----------------+ // -----------------+
static void GL_MSG_Error(const char *format, ...) static void GL_MSG_Error(const char *format, ...)
@ -207,6 +202,32 @@ static void GL_MSG_Error(const char *format, ...)
#endif #endif
} }
// ----------------------+
// GetTextureFormatName : Returns the corresponding texture format string from the texture format enumeration.
// ----------------------+
static const char *GetTextureFormatName(INT32 format)
{
static char num[12];
switch (format)
{
case GL_TEXFMT_P_8: return "GL_TEXFMT_P_8";
case GL_TEXFMT_AP_88: return "GL_TEXFMT_AP_88";
case GL_TEXFMT_RGBA: return "GL_TEXFMT_RGBA";
case GL_TEXFMT_ALPHA_8: return "GL_TEXFMT_ALPHA_8";
case GL_TEXFMT_INTENSITY_8: return "GL_TEXFMT_INTENSITY_8";
case GL_TEXFMT_ALPHA_INTENSITY_88: return "GL_TEXFMT_ALPHA_INTENSITY_88";
default: break;
}
// If the texture format is not known (due to it being invalid),
// return a string containing the format index instead.
format = INT32_MIN;
snprintf(num, sizeof(num), "%d", format);
return num;
}
#ifdef STATIC_OPENGL #ifdef STATIC_OPENGL
/* 1.0 functions */ /* 1.0 functions */
/* Miscellaneous */ /* Miscellaneous */
@ -1410,7 +1431,6 @@ INT32 isExtAvailable(const char *extension, const GLubyte *start)
// -----------------+ // -----------------+
// Init : Initialise the OpenGL interface API // Init : Initialise the OpenGL interface API
// Returns :
// -----------------+ // -----------------+
EXPORT boolean HWRAPI(Init) (void) EXPORT boolean HWRAPI(Init) (void)
{ {
@ -1770,38 +1790,60 @@ EXPORT void HWRAPI(SetBlend) (FBITFIELD PolyFlags)
CurrentPolyFlags = PolyFlags; CurrentPolyFlags = PolyFlags;
} }
// -------------------+
// AllocTextureBuffer : Allocates memory for converting a non-RGBA texture into an RGBA texture.
// -------------------+
static RGBA_t *AllocTextureBuffer(GLMipmap_t *pTexInfo)
{
size_t len = (pTexInfo->width * pTexInfo->height);
RGBA_t *tex = calloc(len, sizeof(RGBA_t));
if (tex == NULL)
I_Error("AllocTextureBuffer: out of memory allocating %s bytes for texture %d, format %s",
sizeu1(len * sizeof(RGBA_t)), pTexInfo->downloaded, GetTextureFormatName(pTexInfo->format));
return tex;
}
// ------------------+
// FreeTextureBuffer : Frees memory allocated by AllocTextureBuffer.
// ------------------+
static void FreeTextureBuffer(RGBA_t *tex)
{
if (tex)
free(tex);
}
// -----------------+ // -----------------+
// UpdateTexture : Updates the texture data. // UpdateTexture : Updates texture data.
// -----------------+ // -----------------+
EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo) EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
{ {
// Download a mipmap // Upload a texture
boolean updatemipmap = true; GLuint num = pTexInfo->downloaded;
static RGBA_t tex[2048*2048]; boolean update = true;
const GLvoid *ptex = tex;
INT32 w, h;
GLuint texnum = 0;
if (!pTexInfo->downloaded) INT32 w = pTexInfo->width, h = pTexInfo->height;
{
pglGenTextures(1, &texnum);
pTexInfo->downloaded = texnum;
updatemipmap = false;
}
else
texnum = pTexInfo->downloaded;
//GL_DBG_Printf ("DownloadMipmap %d %x\n",(INT32)texnum,pTexInfo->data);
w = pTexInfo->width;
h = pTexInfo->height;
if ((pTexInfo->format == GL_TEXFMT_P_8) ||
(pTexInfo->format == GL_TEXFMT_AP_88))
{
const GLubyte *pImgData = (const GLubyte *)pTexInfo->data;
INT32 i, j; INT32 i, j;
const GLubyte *pImgData = (const GLubyte *)pTexInfo->data;
const GLvoid *ptex = NULL;
RGBA_t *tex = NULL;
// Generate a new texture name.
if (!num)
{
pglGenTextures(1, &num);
pTexInfo->downloaded = num;
update = false;
}
//GL_DBG_Printf("UpdateTexture %d %x\n", (INT32)num, pImgData);
if ((pTexInfo->format == GL_TEXFMT_P_8) || (pTexInfo->format == GL_TEXFMT_AP_88))
{
ptex = tex = AllocTextureBuffer(pTexInfo);
for (j = 0; j < h; j++) for (j = 0; j < h; j++)
{ {
for (i = 0; i < w; i++) for (i = 0; i < w; i++)
@ -1831,20 +1873,17 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
tex[w*j+i].s.alpha = *pImgData; tex[w*j+i].s.alpha = *pImgData;
pImgData++; pImgData++;
} }
} }
} }
} }
else if (pTexInfo->format == GL_TEXFMT_RGBA) else if (pTexInfo->format == GL_TEXFMT_RGBA)
{ {
// corona test : passed as ARGB 8888, which is not in glide formats // Directly upload the texture data without any kind of conversion.
// Hurdler: not used for coronas anymore, just for dynamic lighting ptex = pImgData;
ptex = pTexInfo->data;
} }
else if (pTexInfo->format == GL_TEXFMT_ALPHA_INTENSITY_88) else if (pTexInfo->format == GL_TEXFMT_ALPHA_INTENSITY_88)
{ {
const GLubyte *pImgData = (const GLubyte *)pTexInfo->data; ptex = tex = AllocTextureBuffer(pTexInfo);
INT32 i, j;
for (j = 0; j < h; j++) for (j = 0; j < h; j++)
{ {
@ -1861,8 +1900,7 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
} }
else if (pTexInfo->format == GL_TEXFMT_ALPHA_8) // Used for fade masks else if (pTexInfo->format == GL_TEXFMT_ALPHA_8) // Used for fade masks
{ {
const GLubyte *pImgData = (const GLubyte *)pTexInfo->data; ptex = tex = AllocTextureBuffer(pTexInfo);
INT32 i, j;
for (j = 0; j < h; j++) for (j = 0; j < h; j++)
{ {
@ -1877,11 +1915,10 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
} }
} }
else else
GL_MSG_Warning ("SetTexture(bad format) %ld\n", pTexInfo->format); GL_MSG_Warning("UpdateTexture: bad format %d\n", pTexInfo->format);
// the texture number was already generated by pglGenTextures pglBindTexture(GL_TEXTURE_2D, num);
pglBindTexture(GL_TEXTURE_2D, texnum); tex_downloaded = num;
tex_downloaded = texnum;
// disable texture filtering on any texture that has holes so there's no dumb borders or blending issues // disable texture filtering on any texture that has holes so there's no dumb borders or blending issues
if (pTexInfo->flags & TF_TRANSPARENT) if (pTexInfo->flags & TF_TRANSPARENT)
@ -1910,7 +1947,7 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
} }
else else
{ {
if (updatemipmap) if (update)
pglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ptex); pglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ptex);
else else
pglTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptex); pglTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptex);
@ -1931,7 +1968,7 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
} }
else else
{ {
if (updatemipmap) if (update)
pglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ptex); pglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ptex);
else else
pglTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptex); pglTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptex);
@ -1951,13 +1988,16 @@ EXPORT void HWRAPI(UpdateTexture) (GLMipmap_t *pTexInfo)
} }
else else
{ {
if (updatemipmap) if (update)
pglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ptex); pglTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, ptex);
else else
pglTexImage2D(GL_TEXTURE_2D, 0, textureformatGL, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptex); pglTexImage2D(GL_TEXTURE_2D, 0, textureformatGL, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, ptex);
} }
} }
// Free the texture buffer
FreeTextureBuffer(tex);
if (pTexInfo->flags & TF_WRAPX) if (pTexInfo->flags & TF_WRAPX)
pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); pglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
else else