Skip to content

Commit

Permalink
Refactor: Relocated GL_BindTexture() to gl_main
Browse files Browse the repository at this point in the history
Logic for the binding of a texture to a GL texture unit does not
belong within the GL texture manager.
  • Loading branch information
danij-deng committed Jan 17, 2013
1 parent 18e04a6 commit 3990936
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 93 deletions.
16 changes: 12 additions & 4 deletions doomsday/engine/include/gl/gl_main.h
@@ -1,5 +1,4 @@
/**
* @file gl_main.h GL-Graphics Subsystem
/** @file gl_main.h GL-Graphics Subsystem.
* @ingroup gl
*
* @author Copyright &copy; 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
Expand Down Expand Up @@ -29,10 +28,10 @@

#include "render/r_main.h"

struct colorpalette_s;
struct material_s;
struct colorpalette_s;
struct ColorRawf_s;
struct material_s;
struct texturevariant_s;

#define MAX_TEX_UNITS 2 // More aren't currently used.

Expand Down Expand Up @@ -180,6 +179,15 @@ void GL_SetPSprite(struct material_s *mat, int tclass, int tmap);

void GL_SetRawImage(lumpnum_t lumpNum, int wrapS, int wrapT);

/**
* Bind this texture to the currently active texture unit.
* The bind process may result in modification of the GL texture state
* according to the specification used to define this variant.
*
* @param tex Texture::Variant object which represents the GL texture to be bound.
*/
void GL_BindTexture(struct texturevariant_s *tex);

void GL_BindTextureUnmanaged(DGLuint texname, int magMode);

void GL_SetNoTexture(void);
Expand Down
56 changes: 28 additions & 28 deletions doomsday/engine/include/gl/gl_texmanager.h
Expand Up @@ -32,19 +32,17 @@
* time. After clearing they must be manually reloaded.
*/

#ifndef LIBDENG_GLTEXTURE_MANAGER_H
#define LIBDENG_GLTEXTURE_MANAGER_H
#ifndef LIBDENG_GL_TEXMANAGER_H
#define LIBDENG_GL_TEXMANAGER_H

#include "sys_opengl.h"

#include "filehandle.h"
#include "resource/r_data.h" // For flaretexid_t, lightingtexid_t, etc...
#include "resource/rawtexture.h"
#include "resource/texture.h"
#include "resource/texturevariantspec.h"

#ifdef __cplusplus
extern "C" {
#endif

struct image_s;
struct texturecontent_s;
struct texturevariant_s;
Expand All @@ -53,18 +51,22 @@ struct texturevariant_s;
#define MINTEXWIDTH 8
#define MINTEXHEIGHT 8

extern int ratioLimit;
extern int mipmapping, filterUI, texQuality, filterSprites;
extern int texMagMode, texAniso;
extern int useSmartFilter;
extern int texMagMode;
extern int monochrome, upscaleAndSharpenPatches;
extern int glmode[6];
extern boolean fillOutlines;
extern boolean noHighResTex;
extern boolean noHighResPatches;
extern boolean highResWithPWAD;
extern byte loadExtAlways;
DENG_EXTERN_C int ratioLimit;
DENG_EXTERN_C int mipmapping, filterUI, texQuality, filterSprites;
DENG_EXTERN_C int texMagMode, texAniso;
DENG_EXTERN_C int useSmartFilter;
DENG_EXTERN_C int texMagMode;
DENG_EXTERN_C int monochrome, upscaleAndSharpenPatches;
DENG_EXTERN_C int glmode[6];
DENG_EXTERN_C boolean fillOutlines;
DENG_EXTERN_C boolean noHighResTex;
DENG_EXTERN_C boolean noHighResPatches;
DENG_EXTERN_C boolean highResWithPWAD;
DENG_EXTERN_C byte loadExtAlways;

#ifdef __cplusplus
extern "C" {
#endif

void GL_TexRegister(void);

Expand Down Expand Up @@ -195,6 +197,10 @@ uint8_t *GL_LoadImageStr(struct image_s *img, ddstring_t const *filePath);

TexSource GL_LoadExtTexture(struct image_s *image, char const *name, gfxmode_t mode);

GLint GL_MinFilterForVariantSpec(variantspecification_t const *spec);
GLint GL_MagFilterForVariantSpec(variantspecification_t const *spec);
int GL_LogicalAnisoLevelForVariantSpec(variantspecification_t const *spec);

/**
* Compare the given TextureVariantSpecifications and determine whether they can
* be considered equal (dependent on current engine state and the available features
Expand Down Expand Up @@ -271,15 +277,6 @@ DGLuint GL_PrepareTexture(struct texture_s *tex, texturevariantspecification_t *
struct texturevariant_s *GL_PrepareTextureVariant2(struct texture_s *tex, texturevariantspecification_t *spec, preparetextureresult_t *returnOutcome);
struct texturevariant_s *GL_PrepareTextureVariant(struct texture_s *tex, texturevariantspecification_t *spec/*, returnOutcome = 0 */);

/**
* Bind this texture to the currently active texture unit.
* The bind process may result in modification of the GL texture state
* according to the specification used to define this variant.
*
* @param tex Texture::Variant object which represents the GL texture to be bound.
*/
void GL_BindTexture(struct texturevariant_s *tex);

/**
* Dump the pixel data of @a img to an ARGB32 at @a filePath.
*
Expand Down Expand Up @@ -331,6 +328,9 @@ DGLuint GL_NewTextureWithParams2(dgltexformat_t format, int width, int height, u

#ifdef __cplusplus
} // extern "C"

bool GL_LoadImageAndPrepareVariant(de::Texture &tex, texturevariantspecification_t &spec,
de::Texture::Variant **variant);
#endif

#endif /* LIBDENG_GLTEXTURE_MANAGER_H */
#endif /* LIBDENG_GL_TEXMANAGER_H */
49 changes: 49 additions & 0 deletions doomsday/engine/src/gl/gl_main.cpp
Expand Up @@ -781,6 +781,55 @@ void GL_SetRawImage(lumpnum_t lumpNum, int wrapS, int wrapT)
}
}

void GL_BindTexture(texturevariant_s *_tex)
{
Texture::Variant *tex = reinterpret_cast<Texture::Variant *>(_tex);

if(BusyMode_InWorkerThread()) return;

texturevariantspecification_t *spec = 0;
if(tex)
{
spec = &tex->spec();
// Ensure we've prepared this.
if(!tex->isPrepared())
{
Texture::Variant **hndl = &tex;
if(!GL_LoadImageAndPrepareVariant(tex->generalCase(), *spec, hndl))
{
tex = 0;
}
}
}

// Bind our chosen texture.
if(!tex)
{
GL_SetNoTexture();
return;
}

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glBindTexture(GL_TEXTURE_2D, tex->glName());
Sys_GLCheckError();

// Apply dynamic adjustments to the GL texture state according to our spec.
if(spec->type == TST_GENERAL)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, TS_GENERAL(*spec)->wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, TS_GENERAL(*spec)->wrapT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_MagFilterForVariantSpec(TS_GENERAL(*spec)));
if(GL_state.features.texFilterAniso)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
GL_GetTexAnisoMul(GL_LogicalAnisoLevelForVariantSpec(TS_GENERAL(*spec))));
}
}
}

void GL_BindTextureUnmanaged(DGLuint glName, int magMode)
{
LIBDENG_ASSERT_IN_MAIN_THREAD();
Expand Down
73 changes: 12 additions & 61 deletions doomsday/engine/src/gl/gl_texmanager.cpp
Expand Up @@ -191,7 +191,7 @@ void GL_TexRegister()
Textures::consoleRegister();
}

static inline GLint glMinFilterForVariantSpec(variantspecification_t const *spec)
GLint GL_MinFilterForVariantSpec(variantspecification_t const *spec)
{
DENG_ASSERT(spec);
if(spec->minFilter >= 0) // Constant logical value.
Expand All @@ -202,7 +202,7 @@ static inline GLint glMinFilterForVariantSpec(variantspecification_t const *spec
return spec->mipmapped? glmode[mipmapping] : GL_LINEAR;
}

static inline GLint glMagFilterForVariantSpec(variantspecification_t const *spec)
GLint GL_MagFilterForVariantSpec(variantspecification_t const *spec)
{
DENG_ASSERT(spec);
if(spec->magFilter >= 0) // Constant logical value.
Expand All @@ -222,7 +222,7 @@ static inline GLint glMagFilterForVariantSpec(variantspecification_t const *spec
}
}

static inline int logicalAnisoLevelForVariantSpec(variantspecification_t const *spec)
int GL_LogicalAnisoLevelForVariantSpec(variantspecification_t const *spec)
{
DENG_ASSERT(spec);
return spec->anisoFilter < 0? texAniso : spec->anisoFilter;
Expand Down Expand Up @@ -994,9 +994,9 @@ static uploadcontentmethod_t prepareVariantFromImage(de::Texture::Variant *tex,
}
}

minFilter = glMinFilterForVariantSpec(spec);
magFilter = glMagFilterForVariantSpec(spec);
anisoFilter = logicalAnisoLevelForVariantSpec(spec);
minFilter = GL_MinFilterForVariantSpec(spec);
magFilter = GL_MagFilterForVariantSpec(spec);
anisoFilter = GL_LogicalAnisoLevelForVariantSpec(spec);

/**
* Calculate texture coordinates based on the image dimensions. The
Expand Down Expand Up @@ -3043,11 +3043,11 @@ static void performImageAnalyses(de::Texture &tex, image_t const *image,
}
}

static bool tryLoadImageAndPrepareVariant(de::Texture &tex,
texturevariantspecification_t &spec, de::Texture::Variant **variant)
bool GL_LoadImageAndPrepareVariant(Texture &tex, texturevariantspecification_t &spec,
Texture::Variant **variant)
{
DENG_ASSERT(initedOk);
LOG_AS("tryLoadImageAndPrepareVariant");
LOG_AS("GL_LoadImageAndPrepareVariant");

// Load the source image data.
image_t image;
Expand Down Expand Up @@ -3076,7 +3076,7 @@ static bool tryLoadImageAndPrepareVariant(de::Texture &tex,
if(!*variant)
{
DGLuint newGLName = GL_GetReservedTextureName();
*variant = new de::Texture::Variant(tex, spec, source);
*variant = new Texture::Variant(tex, spec, source);
(*variant)->setGLName(newGLName);
tex.addVariant(**variant);
}
Expand All @@ -3095,7 +3095,7 @@ static bool tryLoadImageAndPrepareVariant(de::Texture &tex,
case TST_GENERAL: uploadMethod = prepareVariantFromImage(*variant, &image); break;
case TST_DETAIL: uploadMethod = prepareDetailVariantFromImage(*variant, &image); break;
default:
Con_Error("tryLoadImageAndPrepareVariant: Invalid spec type %i.", spec.type);
Con_Error("GL_LoadImageAndPrepareVariant: Invalid spec type %i.", spec.type);
exit(1); // Unreachable.
}

Expand Down Expand Up @@ -3155,7 +3155,7 @@ texturevariant_s *GL_PrepareTextureVariant2(texture_s *_tex, texturevariantspeci
}

// Suffer the cache miss and attempt to (re)prepare a variant.
bool loadedOk = tryLoadImageAndPrepareVariant(tex, *spec, &variant);
bool loadedOk = GL_LoadImageAndPrepareVariant(tex, *spec, &variant);
if(outcome)
{
if(loadedOk)
Expand Down Expand Up @@ -3197,55 +3197,6 @@ DGLuint GL_PrepareTexture(struct texture_s *tex, texturevariantspecification_t *
return GL_PrepareTexture2(tex, spec, 0);
}

void GL_BindTexture(texturevariant_s *_tex)
{
texturevariantspecification_t *spec = 0;

if(BusyMode_InWorkerThread()) return;

de::Texture::Variant *tex = reinterpret_cast<de::Texture::Variant *>(_tex);
if(tex)
{
spec = &tex->spec();
// Ensure we've prepared this.
if(!tex->isPrepared())
{
de::Texture::Variant **hndl = &tex;
if(!tryLoadImageAndPrepareVariant(tex->generalCase(), *spec, hndl))
{
tex = 0;
}
}
}

// Bind our chosen texture.
if(!tex)
{
GL_SetNoTexture();
return;
}

LIBDENG_ASSERT_IN_MAIN_THREAD();
LIBDENG_ASSERT_GL_CONTEXT_ACTIVE();

glBindTexture(GL_TEXTURE_2D, tex->glName());
Sys_GLCheckError();

// Apply dynamic adjustments to the GL texture state according to our spec.
if(spec->type == TST_GENERAL)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, TS_GENERAL(*spec)->wrapS);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, TS_GENERAL(*spec)->wrapT);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, glMagFilterForVariantSpec(TS_GENERAL(*spec)));
if(GL_state.features.texFilterAniso)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT,
GL_GetTexAnisoMul(logicalAnisoLevelForVariantSpec(TS_GENERAL(*spec))));
}
}
}

void GL_ReleaseGLTexturesByTexture(texture_s *tex)
{
if(!tex) return;
Expand Down

0 comments on commit 3990936

Please sign in to comment.