Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Macros | Functions | Variables
shader.cpp File Reference
#include <boost/algorithm/clamp.hpp>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <memory>
#include "SDL_opengl.h"
#include "inexor/engine/glemu.hpp"
#include "inexor/engine/glexts.hpp"
#include "inexor/engine/renderbackground.hpp"
#include "inexor/engine/rendergl.hpp"
#include "inexor/engine/rendertarget.hpp"
#include "inexor/engine/shader.hpp"
#include "inexor/io/Error.hpp"
#include "inexor/io/Logging.hpp"
#include "inexor/shared/command.hpp"
#include "inexor/shared/cube_formatting.hpp"
#include "inexor/shared/cube_hash.hpp"
#include "inexor/shared/cube_unicode.hpp"
#include "inexor/shared/tools.hpp"
#include "inexor/texture/slot.hpp"
#include "inexor/texture/texture.hpp"
#include "inexor/ui/legacy/menus.hpp"
#include "inexor/ui/screen/ScreenManager.hpp"
#include "inexor/util/legacy_time.hpp"

Classes

struct  postfxtex
 
struct  postfxpass
 

Macros

#define UNIFORMTEX(name, tmu)
 
#define SETSLOTPARAM(l, mask, i, val)
 
#define SETSLOTPARAMS(slotparams)
 
#define SETDEFAULTPARAMS
 
#define GENSHADER(cond, body)
 
#define NUMPOSTFXBINDS   10
 

Functions

 VAR (reservevpparams, 1, 16, 0)
 
 VAR (maxvsuniforms, 1, 0, 0)
 
 VAR (maxfsuniforms, 1, 0, 0)
 
 VAR (maxvaryings, 1, 0, 0)
 
 VAR (dbgshader, 0, 0, 2)
 
void loadshaders ()
 
Shaderlookupshaderbyname (const char *name)
 
Shadergenerateshader (const char *name, const char *fmt,...)
 
static void showglslinfo (GLenum type, GLuint obj, const char *name, const char **parts=nullptr, int numparts=0)
 
static void compileglslshader (GLenum type, GLuint &obj, const char *def, const char *name, bool msg=true)
 
 VAR (dbgubo, 0, 0, 1)
 
static void bindglsluniform (Shader &s, UniformLoc &u)
 
static void linkglslprogram (Shader &s, bool msg=true)
 
int getlocalparam (const char *name)
 
static int addlocalparam (Shader &s, const char *name, int loc, int size, GLenum format)
 
GlobalShaderParamStategetglobalparam (const char *name)
 
static GlobalShaderParamUseaddglobalparam (Shader &s, GlobalShaderParamState *param, int loc, int size, GLenum format)
 
static void setglsluniformformat (Shader &s, const char *name, GLenum format, int size)
 
static void allocglslactiveuniforms (Shader &s)
 
static float * findslotparam (Slot &s, const char *name, float *noval=nullptr)
 
static float * findslotparam (VSlot &s, const char *name, float *noval=nullptr)
 
static void setslotparam (SlotShaderParamState &l, const float *val)
 
 VARF (shaderprecision, 0, 0, 2, initwarning("shader quality"))
 
static void genattriblocs (Shader &s, const char *vs, const char *ps, Shader *reusevs, Shader *reuseps)
 
static void genuniformlocs (Shader &s, const char *vs, const char *ps, Shader *reusevs, Shader *reuseps)
 
Shadernewshader (int type, const char *name, const char *vs, const char *ps, Shader *variant=nullptr, int row=0)
 
void setupshaders ()
 
static const char * findglslmain (const char *s)
 
static void gengenericvariant (Shader &s, const char *sname, const char *vs, const char *ps, int row=0)
 
static bool genwatervariant (Shader &s, const char *sname, const char *vs, const char *ps, int row=2)
 
bool minimizedynlighttcusage ()
 
static void gendynlightvariant (Shader &s, const char *sname, const char *vs, const char *ps, int row=0)
 
static void genshadowmapvariant (Shader &s, const char *sname, const char *vs, const char *ps, int row=1)
 
static void genfogshader (vector< char > &vsbuf, vector< char > &psbuf, const char *vs, const char *ps)
 
static void genuniformdefs (vector< char > &vsbuf, vector< char > &psbuf, const char *vs, const char *ps, Shader *variant=nullptr)
 
 VAR (defershaders, 0, 1, 1)
 
void defershader (int *type, const char *name, const char *contents)
 
void fixshaderdetail ()
 
 VARFP (shaderdetail, 0, MAXSHADERDETAIL, MAXSHADERDETAIL, fixshaderdetail())
 
Shaderuseshaderbyname (const char *name)
 
void shader (int *type, char *name, char *vs, char *ps)
 
void variantshader (int *type, char *name, int *row, char *vs, char *ps)
 
void setshader (char *name)
 
void resetslotshader ()
 
void setslotshader (Slot &s)
 
static void linkslotshaderparams (vector< SlotShaderParam > &params, Shader *sh, bool load)
 
void linkslotshader (Slot &s, bool load)
 
void linkvslotshader (VSlot &s, bool load)
 
void altshader (char *origname, char *altname)
 
void fastshader (char *nice, char *fast, int *detail)
 
 COMMAND (shader,"isss")
 
 COMMAND (variantshader,"isiss")
 
 COMMAND (setshader,"s")
 
 COMMAND (altshader,"ss")
 
 COMMAND (fastshader,"ssi")
 
 COMMAND (defershader,"iss")
 
 ICOMMAND (forceshader,"s",(const char *name), useshaderbyname(name))
 
 ICOMMAND (isshaderdefined,"s",(char *name), intret(lookupshaderbyname(name)?1:0))
 
const char * getshaderparamname (const char *name, bool insert)
 
void addslotparam (const char *name, float x, float y, float z, float w)
 
 ICOMMAND (defuniformparam,"sffff",(char *name, float *x, float *y, float *z, float *w), addslotparam(name,*x,*y,*z,*w))
 
 ICOMMAND (setshaderparam,"sffff",(char *name, float *x, float *y, float *z, float *w), addslotparam(name,*x,*y,*z,*w))
 
 ICOMMAND (setuniformparam,"sffff",(char *name, float *x, float *y, float *z, float *w), addslotparam(name,*x,*y,*z,*w))
 
static int allocatepostfxtex (int scale)
 
void cleanuppostfx (bool fullclean)
 
void renderpostfx ()
 
static bool addpostfx (const char *name, int outputbind, int outputscale, uint inputs, uint freeinputs, const vec4 &params)
 
void clearpostfx ()
 
 COMMAND (clearpostfx,"")
 
 ICOMMAND (addpostfx,"siisffff",(char *name, int *bind, int *scale, char *inputs, float *x, float *y, float *z, float *w),{int inputmask=inputs[0]?0:1;int freemask=inputs[0]?0:1;bool freeinputs=true;for(;*inputs;inputs++) if(isdigit(*inputs)){inputmask|=1<<(*inputs-'0');if(freeinputs) freemask|=1<<(*inputs-'0');}else if(*inputs=='+') freeinputs=false;else if(*inputs=='-') freeinputs=true;inputmask &=(1<< NUMPOSTFXBINDS)-1;freemask &=(1<< NUMPOSTFXBINDS)-1;addpostfx(name, clamp(*bind, 0, NUMPOSTFXBINDS-1), max(*scale, 0), inputmask, freemask, vec4(*x,*y,*z,*w));})
 
 ICOMMAND (setpostfx,"sffff",(char *name, float *x, float *y, float *z, float *w),{clearpostfx();if(name[0]) addpostfx(name, 0, 0, 1, 1, vec4(*x,*y,*z,*w));})
 
void cleanupshaders ()
 
void reloadshaders ()
 
void setupblurkernel (int radius, float sigma, float *weights, float *offsets)
 
void setblurshader (int pass, int size, int radius, float *weights, float *offsets)
 

Variables

Shadernullshader = nullptr
 
Shaderhudshader = nullptr
 
Shaderhudnotextureshader = nullptr
 
Shadertextureshader = nullptr
 
Shadernotextureshader = nullptr
 
Shadernocolorshader = nullptr
 
Shaderfoggedshader = nullptr
 
Shaderfoggednotextureshader = nullptr
 
Shaderstdworldshader = nullptr
 
static hashnameset
< GlobalShaderParamState
globalparams (256)
 
static hashtable< const char
*, int > 
localparams (256)
 
static hashnameset< Shadershaders (256)
 
static Shaderslotshader = nullptr
 
static vector< SlotShaderParamslotparams
 
static bool standardshaders = false
 
static bool forceshaders = true
 
static bool loadedshaders = false
 
static hashset< const char * > shaderparamnames (256)
 
vector< postfxtexpostfxtexs
 
int postfxbinds [NUMPOSTFXBINDS]
 
GLuint postfxfb = 0
 
int postfxw = 0
 
int postfxh = 0
 
vector< postfxpasspostfxpasses
 

Macro Definition Documentation

#define GENSHADER (   cond,
  body 
)
Value:
if(cond) \
{ \
if(vsbuf.length()) { vsbak.setsize(0); vsbak.put(vs, strlen(vs)+1); vs = vsbak.getbuf(); vsbuf.setsize(0); } \
if(psbuf.length()) { psbak.setsize(0); psbak.put(ps, strlen(ps)+1); ps = psbak.getbuf(); psbuf.setsize(0); } \
body; \
if(vsbuf.length()) vs = vsbuf.getbuf(); \
if(psbuf.length()) ps = psbuf.getbuf(); \
}
if(NOT DEFINED PROJECT_ARCH) message(FATAL_ERROR"You are generating from the wrong folder! \"Where is the Source
Definition: CMakeLists.txt:2

Referenced by shader(), and variantshader().

#define NUMPOSTFXBINDS   10

Referenced by renderpostfx().

#define SETDEFAULTPARAMS
Value:
loopv(defaultparams) \
{ \
SlotShaderParamState &l = defaultparams[i]; \
SETSLOTPARAM(l, unimask, i, l.val); \
}
#define SETSLOTPARAM(l, mask, i, val)
Definition: shader.cpp:442
#define loopv(v)
Definition: cube_loops.hpp:21

Referenced by Shader::setslotparams().

#define SETSLOTPARAM (   l,
  mask,
  i,
  val 
)
Value:
do { \
if(!(mask&(1<<i))) { \
mask |= 1<<i; \
setslotparam(l, val); \
} \
} while(0)
GLint GLint GLint GLint GLint GLint GLint GLbitfield mask
Definition: glexts.hpp:299
if(NOT DEFINED PROJECT_ARCH) message(FATAL_ERROR"You are generating from the wrong folder! \"Where is the Source
Definition: CMakeLists.txt:2
static void setslotparam(SlotShaderParamState &l, const float *val)
Definition: shader.cpp:423
#define SETSLOTPARAMS (   slotparams)
Value:
{ \
SlotShaderParam &p = slotparams[i]; \
if(!defaultparams.inrange(p.loc)) continue; \
SlotShaderParamState &l = defaultparams[p.loc]; \
SETSLOTPARAM(l, unimask, p.loc, p.val); \
}
if(NOT DEFINED PROJECT_ARCH) message(FATAL_ERROR"You are generating from the wrong folder! \"Where is the Source
Definition: CMakeLists.txt:2
#define SETSLOTPARAM(l, mask, i, val)
Definition: shader.cpp:442
#define loopv(v)
Definition: cube_loops.hpp:21
static vector< SlotShaderParam > slotparams
Definition: shader.cpp:39

Referenced by Shader::setslotparams().

#define UNIFORMTEX (   name,
  tmu 
)
Value:
{ \
loc = glGetUniformLocation_(program, name); \
int val = tmu; \
if(loc != -1) glUniform1i_(loc, val); \
}
PFNGLUNIFORM1IPROC glUniform1i_
Definition: rendergl.cpp:161
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation_
Definition: rendergl.cpp:152
if(NOT DEFINED PROJECT_ARCH) message(FATAL_ERROR"You are generating from the wrong folder! \"Where is the Source
Definition: CMakeLists.txt:2

Referenced by Shader::allocparams().

Function Documentation

static GlobalShaderParamUse* addglobalparam ( Shader s,
GlobalShaderParamState param,
int  loc,
int  size,
GLenum  format 
)
static
static int addlocalparam ( Shader s,
const char *  name,
int  loc,
int  size,
GLenum  format 
)
static
static bool addpostfx ( const char *  name,
int  outputbind,
int  outputscale,
uint  inputs,
uint  freeinputs,
const vec4 params 
)
static
void addslotparam ( const char *  name,
float  x,
float  y,
float  z,
float  w 
)
static int allocatepostfxtex ( int  scale)
static
static void allocglslactiveuniforms ( Shader s)
static
void altshader ( char *  origname,
char *  altname 
)
static void bindglsluniform ( Shader s,
UniformLoc u 
)
static
void cleanuppostfx ( bool  fullclean)
void cleanupshaders ( )
void clearpostfx ( )

References cleanuppostfx(), and postfxpasses.

COMMAND ( shader  ,
"isss"   
)
COMMAND ( variantshader  ,
"isiss"   
)
COMMAND ( setshader  ,
"s"   
)
COMMAND ( altshader  ,
"ss"   
)
COMMAND ( fastshader  ,
"ssi"   
)
COMMAND ( defershader  ,
"iss"   
)
COMMAND ( clearpostfx  ,
""   
)
static void compileglslshader ( GLenum  type,
GLuint obj,
const char *  def,
const char *  name,
bool  msg = true 
)
static
void defershader ( int *  type,
const char *  name,
const char *  contents 
)
void fastshader ( char *  nice,
char *  fast,
int *  detail 
)
static const char* findglslmain ( const char *  s)
static
static float* findslotparam ( Slot s,
const char *  name,
float *  noval = nullptr 
)
static
static float* findslotparam ( VSlot s,
const char *  name,
float *  noval = nullptr 
)
static
void fixshaderdetail ( )
static void genattriblocs ( Shader s,
const char *  vs,
const char *  ps,
Shader reusevs,
Shader reuseps 
)
static
static void gendynlightvariant ( Shader s,
const char *  sname,
const char *  vs,
const char *  ps,
int  row = 0 
)
static
Shader* generateshader ( const char *  name,
const char *  fmt,
  ... 
)
static void genfogshader ( vector< char > &  vsbuf,
vector< char > &  psbuf,
const char *  vs,
const char *  ps 
)
static
static void gengenericvariant ( Shader s,
const char *  sname,
const char *  vs,
const char *  ps,
int  row = 0 
)
static
static void genshadowmapvariant ( Shader s,
const char *  sname,
const char *  vs,
const char *  ps,
int  row = 1 
)
static
static void genuniformdefs ( vector< char > &  vsbuf,
vector< char > &  psbuf,
const char *  vs,
const char *  ps,
Shader variant = nullptr 
)
static
static void genuniformlocs ( Shader s,
const char *  vs,
const char *  ps,
Shader reusevs,
Shader reuseps 
)
static
static bool genwatervariant ( Shader s,
const char *  sname,
const char *  vs,
const char *  ps,
int  row = 2 
)
static
GlobalShaderParamState* getglobalparam ( const char *  name)
int getlocalparam ( const char *  name)

References localparams.

Referenced by addlocalparam(), and LocalShaderParam::resolve().

const char* getshaderparamname ( const char *  name,
bool  insert 
)
ICOMMAND ( forceshader  ,
"s"  ,
(const char *name)  ,
useshaderbyname(name)   
)
ICOMMAND ( isshaderdefined  ,
"s"  ,
(char *name)  ,
intret(lookupshaderbyname(name)?1:0)   
)
ICOMMAND ( defuniformparam  ,
"sffff"  ,
(char *name, float *x, float *y, float *z, float *w)  ,
addslotparam(name,*x,*y,*z,*w)   
)
ICOMMAND ( setshaderparam  ,
"sffff"  ,
(char *name, float *x, float *y, float *z, float *w)  ,
addslotparam(name,*x,*y,*z,*w)   
)
ICOMMAND ( setuniformparam  ,
"sffff"  ,
(char *name, float *x, float *y, float *z, float *w)  ,
addslotparam(name,*x,*y,*z,*w)   
)
ICOMMAND ( addpostfx  ,
"siisffff"  ,
(char *name, int *bind, int *scale, char *inputs, float *x, float *y, float *z, float *w)  ,
{int inputmask=inputs[0]?0:1;int freemask=inputs[0]?0:1;bool freeinputs=true;for(;*inputs;inputs++) if(isdigit(*inputs)){inputmask|=1<<(*inputs-'0');if(freeinputs) freemask|=1<<(*inputs-'0');}else if(*inputs=='+') freeinputs=false;else if(*inputs=='-') freeinputs=true;inputmask &=(1<< NUMPOSTFXBINDS)-1;freemask &=(1<< NUMPOSTFXBINDS)-1;addpostfx(name, clamp(*bind, 0, NUMPOSTFXBINDS-1), max(*scale, 0), inputmask, freemask, vec4(*x,*y,*z,*w));}   
)
ICOMMAND ( setpostfx  ,
"sffff"  ,
(char *name, float *x, float *y, float *z, float *w)  ,
{clearpostfx();if(name[0]) addpostfx(name, 0, 0, 1, 1, vec4(*x,*y,*z,*w));}   
)
static void linkglslprogram ( Shader s,
bool  msg = true 
)
static
void linkslotshader ( Slot s,
bool  load 
)
static void linkslotshaderparams ( vector< SlotShaderParam > &  params,
Shader sh,
bool  load 
)
static
void linkvslotshader ( VSlot s,
bool  load 
)
void loadshaders ( )
Shader* lookupshaderbyname ( const char *  name)
bool minimizedynlighttcusage ( )

Referenced by gendynlightvariant(), and setdynlights().

Shader* newshader ( int  type,
const char *  name,
const char *  vs,
const char *  ps,
Shader variant = nullptr,
int  row = 0 
)
void reloadshaders ( )
void renderpostfx ( )
void resetslotshader ( )
void setblurshader ( int  pass,
int  size,
int  radius,
float *  weights,
float *  offsets 
)
static void setglsluniformformat ( Shader s,
const char *  name,
GLenum  format,
int  size 
)
static
void setshader ( char *  name)
static void setslotparam ( SlotShaderParamState l,
const float *  val 
)
inlinestatic
void setslotshader ( Slot s)
void setupblurkernel ( int  radius,
float  sigma,
float *  weights,
float *  offsets 
)

References loopi(), and MAXBLURRADIUS.

Referenced by rendertarget::blur().

void setupshaders ( )
void shader ( int *  type,
char *  name,
char *  vs,
char *  ps 
)
static void showglslinfo ( GLenum  type,
GLuint  obj,
const char *  name,
const char **  parts = nullptr,
int  numparts = 0 
)
static
Shader* useshaderbyname ( const char *  name)
VAR ( reservevpparams  ,
,
16  ,
 
)
VAR ( maxvsuniforms  ,
,
,
 
)
VAR ( maxfsuniforms  ,
,
,
 
)
VAR ( maxvaryings  ,
,
,
 
)
VAR ( dbgshader  ,
,
,
 
)
VAR ( dbgubo  ,
,
,
 
)
VAR ( defershaders  ,
,
,
 
)
VARF ( shaderprecision  ,
,
,
,
initwarning("shader quality")   
)
VARFP ( shaderdetail  ,
,
MAXSHADERDETAIL  ,
MAXSHADERDETAIL  ,
fixshaderdetail()   
)
void variantshader ( int *  type,
char *  name,
int *  row,
char *  vs,
char *  ps 
)

Variable Documentation

Shader * foggednotextureshader = nullptr
Shader * foggedshader = nullptr
bool forceshaders = true
static

Referenced by Shader::force(), and newshader().

hashnameset<GlobalShaderParamState> globalparams(256)
static
Shader * hudnotextureshader = nullptr
Shader * hudshader = nullptr
bool loadedshaders = false
static

Referenced by generateshader(), and loadshaders().

hashtable<const char *, int> localparams(256)
static

Referenced by Shader::cleanup(), and getlocalparam().

Shader * nocolorshader = nullptr
Shader * notextureshader = nullptr
Shader* nullshader = nullptr
int postfxbinds[NUMPOSTFXBINDS]
GLuint postfxfb = 0

Referenced by cleanuppostfx(), and renderpostfx().

int postfxh = 0

Referenced by cleanuppostfx(), and renderpostfx().

vector<postfxpass> postfxpasses
vector<postfxtex> postfxtexs
int postfxw = 0

Referenced by cleanuppostfx(), and renderpostfx().

hashset<const char *> shaderparamnames(256)
static

Referenced by getshaderparamname().

hashnameset<Shader> shaders(256)
static
vector<SlotShaderParam> slotparams
static
Shader* slotshader = nullptr
static
bool standardshaders = false
static
Shader * stdworldshader = nullptr
Shader * textureshader = nullptr