Pyrogenesis  trunk
Classes | Enumerations | Functions | Variables
ogl_tex.cpp File Reference
#include "precompiled.h"
#include "ogl_tex.h"
#include <cstdio>
#include "lib/app_hooks.h"
#include "lib/ogl.h"
#include "lib/bits.h"
#include "lib/sysdep/gfx.h"
#include "lib/tex/tex.h"
#include "lib/res/h_mgr.h"
#include "lib/fnv_hash.h"
Include dependency graph for ogl_tex.cpp:

Classes

struct  OglTexState
 
struct  OglTex
 
struct  UploadParams
 

Enumerations

enum  OglTexFlags { OT_IS_UPLOADED = 1, OT_TEX_VALID = 2, OT_NEED_AUTO_UPLOAD = 4, OT_ALL_FLAGS = OT_IS_UPLOADED|OT_TEX_VALID|OT_NEED_AUTO_UPLOAD }
 

Functions

static bool filter_valid (GLint filter)
 
static bool wrap_valid (GLint wrap)
 
static bool are_mipmaps_needed (size_t width, size_t height, GLint filter)
 
static bool fmt_is_s3tc (GLenum fmt)
 
static GLint choose_fmt (size_t bpp, size_t flags)
 
static bool q_flags_valid (int q_flags)
 
void ogl_tex_set_defaults (int q_flags, GLint filter)
 Change default settings - these affect performance vs. More...
 
static GLint choose_int_fmt (GLenum fmt, int q_flags)
 
static void state_set_to_defaults (OglTexState *ots)
 
static void state_latch (OglTexState *ots)
 
 H_TYPE_DEFINE (OglTex)
 
static void OglTex_init (OglTex *ot, va_list args)
 
static void OglTex_dtor (OglTex *ot)
 
static Status OglTex_reload (OglTex *ot, const PIVFS &vfs, const VfsPath &pathname, Handle h)
 
static Status OglTex_validate (const OglTex *ot)
 
static Status OglTex_to_string (const OglTex *ot, wchar_t *buf)
 
Handle ogl_tex_load (const PIVFS &vfs, const VfsPath &pathname, size_t flags)
 Load and return a handle to the texture. More...
 
Handle ogl_tex_find (const VfsPath &pathname)
 Find and return an existing texture object, if it has already been loaded and is still in memory. More...
 
Handle ogl_tex_wrap (Tex *t, const PIVFS &vfs, const VfsPath &pathname, size_t flags)
 Make the Tex object ready for use as an OpenGL texture and return a handle to it. More...
 
Status ogl_tex_free (Handle &ht)
 Release this texture reference. More...
 
static void warn_if_uploaded (Handle ht, const OglTex *ot)
 
Status ogl_tex_set_filter (Handle ht, GLint filter)
 Override default filter (see ogl_tex_set_defaults) for this texture. More...
 
Status ogl_tex_set_wrap (Handle ht, GLint wrap_s, GLint wrap_t)
 Override default wrap mode (GL_REPEAT) for this texture. More...
 
Status ogl_tex_set_anisotropy (Handle ht, GLfloat anisotropy)
 Override default maximum anisotropic filtering for this texture. More...
 
void ogl_tex_override (OglTexOverrides what, OglTexAllow allow)
 Override the default decision and force/disallow use of the given feature. More...
 
static void detect_gl_upload_caps ()
 
static Status get_mipmaps (Tex *t, GLint filter, int q_flags, int *plevels_to_skip)
 
static void upload_level (size_t level, size_t level_w, size_t level_h, const u8 *RESTRICT level_data, size_t level_data_size, void *RESTRICT cbData)
 
static void upload_compressed_level (size_t level, size_t level_w, size_t level_h, const u8 *RESTRICT level_data, size_t level_data_size, void *RESTRICT cbData)
 
static void upload_impl (Tex *t, GLenum fmt, GLint int_fmt, int levels_to_skip, u32 *uploaded_size)
 
Status ogl_tex_upload (const Handle ht, GLenum fmt_ovr, int q_flags_ovr, GLint int_fmt_ovr)
 Upload texture to OpenGL. More...
 
Status ogl_tex_get_size (Handle ht, size_t *w, size_t *h, size_t *bpp)
 Retrieve dimensions and bit depth of the texture. More...
 
Status ogl_tex_get_format (Handle ht, size_t *flags, GLenum *fmt)
 Retrieve pixel format of the texture. More...
 
Status ogl_tex_get_data (Handle ht, u8 **p)
 Retrieve pixel data of the texture. More...
 
Status ogl_tex_get_uploaded_size (Handle ht, size_t *size)
 Retrieve number of bytes uploaded for the texture, including mipmaps. More...
 
Status ogl_tex_get_average_color (Handle ht, u32 *p)
 Retrieve ARGB value of 1x1 mipmap level of the texture, i.e. More...
 
Status ogl_tex_bind (Handle ht, size_t unit)
 Bind texture to the specified unit in preparation for using it in rendering. More...
 
Status ogl_tex_get_texture_id (Handle ht, GLuint *id)
 Return the GL handle of the loaded texture in *id, or 0 on failure. More...
 
Status ogl_tex_transform (Handle ht, size_t transforms)
 (partially) Transform pixel format of the texture. More...
 
Status ogl_tex_transform_to (Handle ht, size_t new_flags)
 Transform pixel format of the texture. More...
 
bool ogl_tex_has_s3tc ()
 Return whether native S3TC texture compression support is available. More...
 
bool ogl_tex_has_anisotropy ()
 Return whether anisotropic filtering support is available. More...
 

Variables

static GLint default_filter = GL_LINEAR
 
static int default_q_flags = OGL_TEX_FULL_QUALITY
 
static int have_auto_mipmap_gen = -1
 
static int have_s3tc = -1
 
static int have_anistropy = -1
 

Enumeration Type Documentation

Enumerator
OT_IS_UPLOADED 
OT_TEX_VALID 
OT_NEED_AUTO_UPLOAD 
OT_ALL_FLAGS 

Function Documentation

static bool are_mipmaps_needed ( size_t  width,
size_t  height,
GLint  filter 
)
static
static GLint choose_fmt ( size_t  bpp,
size_t  flags 
)
static
static GLint choose_int_fmt ( GLenum  fmt,
int  q_flags 
)
static
static void detect_gl_upload_caps ( )
static
static bool filter_valid ( GLint  filter)
static
static bool fmt_is_s3tc ( GLenum  fmt)
static
static Status get_mipmaps ( Tex t,
GLint  filter,
int  q_flags,
int *  plevels_to_skip 
)
static
H_TYPE_DEFINE ( OglTex  )
Status ogl_tex_bind ( Handle  ht,
size_t  unit = 0 
)

Bind texture to the specified unit in preparation for using it in rendering.

Parameters
htTexture handle. If 0, texturing is disabled on this unit.
unitTexture Mapping Unit number, typically 0 for the first.
Returns
Status

Side Effects:

  • changes the active texture unit;
  • (if successful) texturing was enabled/disabled on that unit.

Notes:

  • assumes multitexturing is available.
  • not necessary before calling ogl_tex_upload!
  • on error, the unit's texture state is unchanged; see implementation.
Handle ogl_tex_find ( const VfsPath pathname)

Find and return an existing texture object, if it has already been loaded and is still in memory.

Parameters
pathnamefn VFS filename of texture.
Returns
Handle to texture or negative Status
Status ogl_tex_free ( Handle ht)

Release this texture reference.

When the count reaches zero, all of its associated resources are freed and further use made impossible.

Parameters
htTexture handle.
Returns
Status
Status ogl_tex_get_average_color ( Handle  ht,
u32 p 
)

Retrieve ARGB value of 1x1 mipmap level of the texture, i.e.

the average color of the whole texture.

Parameters
htTexture handle
pwill be filled with ARGB value (or 0 if texture does not have mipmaps)
Returns
Status

Must be called before uploading (raises a warning if called afterwards).

Status ogl_tex_get_data ( Handle  ht,
u8 **  p 
)

Retrieve pixel data of the texture.

Parameters
htTexture handle
pwill be filled with pointer to texels.
Returns
Status

Note: this memory is freed after a successful ogl_tex_upload for this texture. After that, the pointer we retrieve is NULL but the function doesn't fail (negative return value) by design. If you still need to get at the data, add a reference before uploading it or read directly from OpenGL (discouraged).

Status ogl_tex_get_format ( Handle  ht,
size_t *  flags,
GLenum *  fmt 
)

Retrieve pixel format of the texture.

Parameters
htTexture handle
flagsoptional; will be filled with TexFlags
fmtoptional; will be filled with GL format (it is determined during ogl_tex_upload and 0 before then)
Returns
Status
Status ogl_tex_get_size ( Handle  ht,
size_t *  w,
size_t *  h,
size_t *  bpp 
)

Retrieve dimensions and bit depth of the texture.

Parameters
htTexture handle
woptional; will be filled with width
hoptional; will be filled with height
bppoptional; will be filled with bits per pixel
Returns
Status
Status ogl_tex_get_texture_id ( Handle  ht,
GLuint *  id 
)

Return the GL handle of the loaded texture in *id, or 0 on failure.

Status ogl_tex_get_uploaded_size ( Handle  ht,
size_t *  size 
)

Retrieve number of bytes uploaded for the texture, including mipmaps.

size will be 0 if the texture has not been uploaded yet.

Parameters
htTexture handle
sizeWill be filled with size in bytes
Returns
Status
bool ogl_tex_has_anisotropy ( )

Return whether anisotropic filtering support is available.

(The anisotropy might still be disabled or overridden by the driver configuration.)

Returns
true if anisotropic filtering supported.

ogl_tex_upload must be called at least once before this.

bool ogl_tex_has_s3tc ( )

Return whether native S3TC texture compression support is available.

If not, textures will be decompressed automatically, hurting performance.

Returns
true if native S3TC supported.

ogl_tex_upload must be called at least once before this.

Handle ogl_tex_load ( const PIVFS vfs,
const VfsPath pathname,
size_t  flags = 0 
)

Load and return a handle to the texture.

Parameters
vfs
pathname
flagsh_alloc flags.
Returns
Handle to texture or negative Status for a list of supported formats, see tex.h's tex_load.
void ogl_tex_override ( OglTexOverrides  what,
OglTexAllow  allow 
)

Override the default decision and force/disallow use of the given feature.

Typically called from ah_override_gl_upload_caps.

Parameters
whatFeature to influence.
allowDisable/enable flag.
Status ogl_tex_set_anisotropy ( Handle  ht,
GLfloat  anisotropy 
)

Override default maximum anisotropic filtering for this texture.

Parameters
htTexture handle
anisotropyAnisotropy value (must not be less than 1.0; should usually be a power of two)
Returns
Status

Must be called before uploading (raises a warning if called afterwards).

void ogl_tex_set_defaults ( int  q_flags,
GLint  filter 
)

Change default settings - these affect performance vs.

quality. May be overridden for individual textures via parameter to ogl_tex_upload or ogl_tex_set_filter, respectively.

Parameters
q_flagsquality flags. Pass 0 to keep the current setting (initially OGL_TEX_FULL_QUALITY), or any combination of OglTexQualityFlags.
filtermag/minification filter. Pass 0 to keep the current setting (initially GL_LINEAR), or any valid OpenGL minification filter.
Status ogl_tex_set_filter ( Handle  ht,
GLint  filter 
)

Override default filter (see ogl_tex_set_defaults) for this texture.

Parameters
htTexture handle
filterOpenGL minification and magnification filter (rationale: see OglTexState)
Returns
Status

Must be called before uploading (raises a warning if called afterwards).

Status ogl_tex_set_wrap ( Handle  ht,
GLint  wrap_s,
GLint  wrap_t 
)

Override default wrap mode (GL_REPEAT) for this texture.

Parameters
htTexture handle
wrap_sOpenGL wrap mode for S coordinates
wrap_tOpenGL wrap mode for T coordinates
Returns
Status

Must be called before uploading (raises a warning if called afterwards).

Status ogl_tex_transform ( Handle  ht,
size_t  flags 
)

(partially) Transform pixel format of the texture.

Parameters
htTexture handle.
flagsthe TexFlags that are to be changed.
Returns
Status
See also
tex_transform

Must be called before uploading (raises a warning if called afterwards).

Status ogl_tex_transform_to ( Handle  ht,
size_t  new_flags 
)

Transform pixel format of the texture.

Parameters
htTexture handle.
new_flagsFlags desired new TexFlags indicating pixel format.
Returns
Status
See also
tex_transform

Must be called before uploading (raises a warning if called afterwards).

Note: this is equivalent to ogl_tex_transform(ht, ht_flags^new_flags).

Status ogl_tex_upload ( const Handle  ht,
GLenum  fmt_ovr = 0,
int  q_flags_ovr = 0,
GLint  int_fmt_ovr = 0 
)

Upload texture to OpenGL.

Parameters
htTexture handle
fmt_ovroptional override for OpenGL format (e.g. GL_RGB), which is decided from bpp / Tex flags
q_flags_ovroptional override for global default OglTexQualityFlags
int_fmt_ovroptional override for OpenGL internal format (e.g. GL_RGB8), which is decided from fmt / q_flags.
Returns
Status.

Side Effects:

  • enables texturing on TMU 0 and binds the texture to it;
  • frees the texel data! see ogl_tex_get_data.
Handle ogl_tex_wrap ( Tex t,
const PIVFS vfs,
const VfsPath pathname,
size_t  flags = 0 
)

Make the Tex object ready for use as an OpenGL texture and return a handle to it.

This will be as if its contents had been loaded by ogl_tex_load.

Parameters
tTexture object.
vfs
pathnamefilename or description of texture. not strictly needed, but would allow h_filename to return meaningful info for purposes of debugging.
flags
Returns
Handle to texture or negative Status

note: because we cannot guarantee that callers will pass distinct "filenames", caching is disabled for the created object. this avoids mistakenly reusing previous objects that share the same comment.

we need only add bookkeeping information and "wrap" it in a resource object (accessed via Handle), hence the name.

static void OglTex_dtor ( OglTex ot)
static
static void OglTex_init ( OglTex ot,
va_list  args 
)
static
static Status OglTex_reload ( OglTex ot,
const PIVFS vfs,
const VfsPath pathname,
Handle  h 
)
static
static Status OglTex_to_string ( const OglTex ot,
wchar_t buf 
)
static
static Status OglTex_validate ( const OglTex ot)
static
static bool q_flags_valid ( int  q_flags)
static
static void state_latch ( OglTexState ots)
static
static void state_set_to_defaults ( OglTexState ots)
static
static void upload_compressed_level ( size_t  level,
size_t  level_w,
size_t  level_h,
const u8 *RESTRICT  level_data,
size_t  level_data_size,
void *RESTRICT  cbData 
)
static
static void upload_impl ( Tex t,
GLenum  fmt,
GLint  int_fmt,
int  levels_to_skip,
u32 uploaded_size 
)
static
static void upload_level ( size_t  level,
size_t  level_w,
size_t  level_h,
const u8 *RESTRICT  level_data,
size_t  level_data_size,
void *RESTRICT  cbData 
)
static
static void warn_if_uploaded ( Handle  ht,
const OglTex ot 
)
static
static bool wrap_valid ( GLint  wrap)
static

Variable Documentation

GLint default_filter = GL_LINEAR
static
int default_q_flags = OGL_TEX_FULL_QUALITY
static
int have_anistropy = -1
static
int have_auto_mipmap_gen = -1
static
int have_s3tc = -1
static