Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Technique::Pass Class Reference

#include <technique.h>

Classes

struct  ShaderParam
 
struct  TextureUnit
 

Public Types

enum  Tristate { False =0, True =1, Auto =2 }
 
enum  Type { FixedPass, ShaderPass }
 
enum  BlendMode {
  Default, Add, Multiply, AlphaBlend,
  Decal, PremultAlphaBlend, MultiAlphaBlend
}
 
enum  DepthFunction {
  Less, LEqual, Greater, GEqual,
  Equal, Always, Never
}
 
enum  Face {
  None, Front, Back, FrontAndBack,
  DefaultFace
}
 
enum  PolyMode { Point, Line, Fill }
 

Public Member Functions

 Pass ()
 
 ~Pass ()
 
void setProgram (const std::string &vertex, const std::string &fragment)
 
int getCompiledProgram () const
 
void addTextureUnit (const std::string &source, int target, const std::string &deflt, const std::string &paramName, Technique::Pass::TextureUnit::Kind texKind)
 
void addShaderParam (const std::string &name, float value[4], bool optional)
 
void addShaderParam (const std::string &name, ShaderParam::Semantic semantic, bool optional)
 
size_t getNumTextureUnits () const
 
const TextureUnitgetTextureUnit (int index) const
 
TextureUnitgetTextureUnit (int index)
 
size_t getNumShaderParams () const
 
const ShaderParamgetShaderParam (int index) const
 
ShaderParamgetShaderParam (int index)
 
void compile ()
 
bool isCompiled () const
 
bool isCompiled (int programVersion) const
 

Public Attributes

Type type
 
bool sRGBAware
 
bool colorWrite
 
Tristate zWrite
 
unsigned int perLightIteration
 
unsigned int maxIterations
 
BlendMode blendMode
 
DepthFunction depthFunction
 
Face cullMode
 
PolyMode polyMode
 
float offsetFactor
 
float offsetUnits
 
float lineWidth
 
int sequence
 

Detailed Description

Definition at line 22 of file technique.h.

Member Enumeration Documentation

Enumerator
Default 

Whatever the mesh specifies as blend mode

Add 

Blend function ONE ONE

Multiply 

Blend function SRCCOLOR ZERO

AlphaBlend 

Blend function ALPHA INVSRCALPHA

Decal 

Blend function ONE ZERO

PremultAlphaBlend 

Blend function ONE INVSRCALPHA

MultiAlphaBlend 

Blend function ALPHA INVSRCALPHA for the first pass, ALPHA ONE for subsequent passes

Definition at line 62 of file technique.h.

63  {
65  Default,
66 
68  Add,
69 
71  Multiply,
72 
74  AlphaBlend,
75 
77  Decal,
78 
81 
84  };
Enumerator
Less 
LEqual 
Greater 
GEqual 
Equal 
Always 
Never 

Definition at line 86 of file technique.h.

87  {
88  Less,
89  LEqual,
90  Greater,
91  GEqual,
92  Equal,
93  Always,
94  Never
95  };
Enumerator
None 
Front 
Back 
FrontAndBack 
DefaultFace 

Definition at line 97 of file technique.h.

98  {
99  None,
100  Front,
101  Back,
102  FrontAndBack,
104  };
Enumerator
Point 
Line 
Fill 

Definition at line 106 of file technique.h.

107  {
108  Point,
109  Line,
110  Fill
111  };
Enumerator
False 
True 
Auto 

Definition at line 37 of file technique.h.

38  {
39  False=0,
40  True =1,
41  Auto =2
42  };
Enumerator
FixedPass 

A fixed pass is one that uses up to 4 optional fixed-function texture targets: 0: base diffuse 1: specmap 2: damage diffuse 3: glow

It is rendered using the fixed function pipeline.

ShaderPass 

A shader pass is a configurable pass that uses shaders to do everything. Vertex and fragment programs must be specified.

Definition at line 44 of file technique.h.

45  {
54  FixedPass,
55 
60  };

Constructor & Destructor Documentation

Technique::Pass::Pass ( )

Definition at line 261 of file technique.cpp.

262  : program( 0 )
263  , type( FixedPass )
264  , colorWrite( true )
265  , zWrite( True )
266  , perLightIteration( 0 )
267  , maxIterations( 0 )
268  , blendMode( Default )
269  , depthFunction( LEqual )
270  , cullMode( DefaultFace )
271  , polyMode( Fill )
272  , offsetFactor( 0 )
273  , offsetUnits( 0 )
274  , lineWidth( 1 )
275  , sequence( 0 )
276 {}
Technique::Pass::~Pass ( )

Definition at line 278 of file technique.cpp.

279 {
280  //Should deallocate the program... but... GFX doesn't have that API.
281 }

Member Function Documentation

void Technique::Pass::addShaderParam ( const std::string &  name,
float  value[4],
bool  optional 
)

Add a constant shader param

Parameters
nameA string defining the shader's parameter name.
valueAn array of 4 floats with the parameter's value.
optionalOptional parameters are allowed to be left unresolved.
Remarks
Parameters with less than 4 values will simply ignore the other values and should be fed with any dummy value (like 0).
void Technique::Pass::addShaderParam ( const std::string &  name,
ShaderParam::Semantic  semantic,
bool  optional 
)

Add an automatic shader param

Parameters
nameA string defining the shader's parameter name.
semanticThe kind of value that will be automatically put in the variable.
optionalOptional parameters are allowed to be left unresolved.
void Technique::Pass::addTextureUnit ( const std::string &  source,
int  target,
const std::string &  deflt,
const std::string &  paramName,
Technique::Pass::TextureUnit::Kind  texKind 
)

Add a texture unit

Parameters
sourceA string of the form [type]:[path or index] that specifies the texture unit's data source.
targetThe target texturing unit
defltA string of the form [type]:[path or index] that specifies the texture unit's default data source if the mesh lacks the specified decal in 'source'
paramNameThe shader parameter name (for a shader pass) that is to be bound to this texture unit.
Remarks
[type] may be either Decal or File, mapping to equally named SourceType s

Definition at line 290 of file technique.cpp.

References Technique::Pass::TextureUnit::defaultIndex, Technique::Pass::TextureUnit::defaultPath, Technique::Pass::TextureUnit::defaultType, Technique::Pass::TextureUnit::origTargetIndex, __impl::parseSourceType(), Technique::Pass::TextureUnit::sourceIndex, Technique::Pass::TextureUnit::sourcePath, Technique::Pass::TextureUnit::sourceType, Technique::Pass::TextureUnit::targetIndex, Technique::Pass::TextureUnit::targetParamId, Technique::Pass::TextureUnit::targetParamName, and Technique::Pass::TextureUnit::texKind.

295 {
296  textureUnits.resize( textureUnits.size()+1 );
297  TextureUnit &newTU = textureUnits.back();
298 
299  string::size_type ssep = string::npos, dsep = string::npos;
300  newTU.sourceType = parseSourceType( source, ssep );
301  newTU.defaultType = parseSourceType( deflt, dsep );
302  newTU.targetIndex =
303  newTU.origTargetIndex = target;
304  newTU.targetParamName = paramName;
305  newTU.targetParamId = -1;
306  newTU.texKind = texKind;
307  switch (newTU.sourceType)
308  {
309  case TextureUnit::Decal:
310  case TextureUnit::Detail:
311  if (ssep == string::npos) throw InvalidParameters( "Decal/Detail reference missing source index" );
312  newTU.sourceIndex = atoi( source.c_str()+ssep+1 );
313  break;
314  case TextureUnit::File:
315  if (ssep == string::npos) throw InvalidParameters( "File reference missing path" );
316  newTU.sourcePath.assign( source, ssep+1, string::npos );
317  break;
319  break;
320  default: throw InvalidParameters( "Missing source" );
321  }
322  switch (newTU.defaultType)
323  {
324  case TextureUnit::Decal:
325  case TextureUnit::Detail:
326  if (dsep == string::npos) throw InvalidParameters( "Decal/Detail reference missing source index" );
327  newTU.defaultIndex = atoi( deflt.c_str()+dsep+1 );
328  break;
329  case TextureUnit::File:
330  if (dsep == string::npos) throw InvalidParameters( "File reference missing path" );
331  newTU.defaultPath.assign( deflt, dsep+1, string::npos );
332  break;
333  case TextureUnit::None: //FIXME added by chuck_starchaser; please verify correctness
334  case TextureUnit::Environment: //FIXME added by chuck_starchaser; please verify correctness
335  default: //FIXME added by chuck_starchaser; please verify correctness
336  break; //FIXME added by chuck_starchaser; please verify correctness
337  }
338 }
void Technique::Pass::compile ( )

Compile the pass (shaders, fetch shader params, etc...)

Definition at line 365 of file technique.cpp.

References gl_options_t::ext_srgb_framebuffer, GFXCreateProgram(), GFXDestroyProgram(), GFXGetProgramVersion(), GFXNamedShaderConstant(), gl_options, max(), gl_options_t::nv_fp2, BaseUtil::Texture(), and VSFileSystem::vs_dprintf().

366 {
367  if (type == ShaderPass) {
368  int prog = program; // BEGIN TRANSACTION
369 
370  if (prog != 0 && programVersion != GFXGetProgramVersion()) {
371  GFXDestroyProgram(program);
372  prog = 0;
373  }
374 
375  if (prog == 0) {
376  std::string defines;
377 
378  // Automatic defines
379  if (sRGBAware) {
381  defines += "#define SRGB_FRAMEBUFFER 1\n";
382  else
383  defines += "#define SRGB_FRAMEBUFFER 0\n";
384  }
385  if (gl_options.nv_fp2)
386  defines += "#define VGL_NV_fragment_program2 1\n";
387 
388  // Compile program
389  prog = GFXCreateProgram( vertexProgram.c_str(), fragmentProgram.c_str(),
390  (defines.empty() ? NULL : defines.c_str()) );
391  if (prog == 0)
392  throw ProgramCompileError(
393  "Error compiling program vp:\""+vertexProgram
394  +"\" fp:\""+fragmentProgram+"\"" );
395  else
396  VSFileSystem::vs_dprintf( 1, "Successfully compiled and linked program \"%s+%s\"\n",
397  vertexProgram.c_str(), fragmentProgram.c_str() );
398  }
399 
400  for (ShaderParamList::iterator it = shaderParams.begin(); it != shaderParams.end(); ++it) {
401  it->id = GFXNamedShaderConstant( prog, it->name.c_str() );
402  if (it->id < 0) {
403  if (!it->optional)
404  throw ProgramCompileError( "Cannot resolve shader constant \""+it->name+"\"" );
405  else
406  VSFileSystem::vs_dprintf( 1, "Cannot resolve <<optional>> shader constant \"%s\" in program \"%s+%s\"\n",
407  it->name.c_str(), vertexProgram.c_str(), fragmentProgram.c_str() );
408  }
409  }
410  int lastTU = -1;
411  for (TextureUnitList::iterator tit = textureUnits.begin(); tit != textureUnits.end(); ++tit) {
412  if (tit->sourceType == TextureUnit::File) {
413  // Yep, we don't want to reload textures
414  if (tit->texture.get() == 0) {
415  tit->texture.reset( new Texture( tit->sourcePath.c_str() ) );
416  if ( !tit->texture->LoadSuccess() ) throw InvalidParameters(
417  "Cannot load texture file \""+tit->sourcePath+"\"" );
418  }
419  } else if (tit->defaultType == TextureUnit::File) {
420  // Yep, we don't want to reload textures
421  if (tit->texture.get() == 0) {
422  tit->texture.reset( new Texture( tit->defaultPath.c_str() ) );
423  if ( !tit->texture->LoadSuccess() ) throw InvalidParameters(
424  "Cannot load texture file \""+tit->defaultPath+"\"" );
425  }
426  }
427  if (!tit->targetParamName.empty()) {
428  tit->targetParamId = GFXNamedShaderConstant( prog, tit->targetParamName.c_str() );
429  if (tit->targetParamId < 0) {
430  if (tit->origTargetIndex >= 0)
431  throw ProgramCompileError(
432  "Cannot resolve shader constant \""+tit->targetParamName+"\"" );
433  else
434  tit->targetIndex = -1;
435  } else {
436  if (tit->origTargetIndex < 0)
437  tit->targetIndex = lastTU+1;
438  lastTU = std::max( tit->targetIndex, lastTU );
439  }
440  }
441  }
442 
443  // COMMIT ;-)
444  program = prog;
446  }
447 }
int Technique::Pass::getCompiledProgram ( ) const
inline

Get the vertex and fragment programs, compiled. 0 for fixed-function

Definition at line 301 of file technique.h.

302  {
303  return program;
304  }
size_t Technique::Pass::getNumShaderParams ( ) const
inline

Get the number of shader params in this pass

Definition at line 358 of file technique.h.

Referenced by GameUnit< UnitType >::applyTechniqueOverrides().

359  {
360  return shaderParams.size();
361  }
size_t Technique::Pass::getNumTextureUnits ( ) const
inline

Get the number of texture units in this pass

Definition at line 340 of file technique.h.

Referenced by OrigMeshContainer::operator<().

341  {
342  return textureUnits.size();
343  }
const ShaderParam& Technique::Pass::getShaderParam ( int  index) const
inline

Get the specified shader param

Definition at line 364 of file technique.h.

References index.

Referenced by GameUnit< UnitType >::applyTechniqueOverrides().

365  {
366  return shaderParams[index];
367  }
ShaderParam& Technique::Pass::getShaderParam ( int  index)
inline

Get the specified shader param

Definition at line 370 of file technique.h.

References index.

371  {
372  return shaderParams[index];
373  }
const TextureUnit& Technique::Pass::getTextureUnit ( int  index) const
inline

Get the specified texture unit

Definition at line 346 of file technique.h.

References index.

Referenced by OrigMeshContainer::operator<().

347  {
348  return textureUnits[index];
349  }
TextureUnit& Technique::Pass::getTextureUnit ( int  index)
inline

Get the specified texture unit

Definition at line 352 of file technique.h.

References index.

353  {
354  return textureUnits[index];
355  }
bool Technique::Pass::isCompiled ( ) const

Return whether the pass has been compiled or not

Definition at line 450 of file technique.cpp.

451 {
452  return (type != ShaderPass) || (program != 0);
453 }
bool Technique::Pass::isCompiled ( int  programVersion) const

Return whether the pass has been compiled with a matching program version

Return whether the pass has been compiled or not with a matching program version

Definition at line 456 of file technique.cpp.

457 {
458  return (type != ShaderPass) || (program != 0 && this->programVersion == programVersion);
459 }
void Technique::Pass::setProgram ( const std::string &  vertex,
const std::string &  fragment 
)

Set the vertex and fragment programs - throw on error

Definition at line 283 of file technique.cpp.

284 {
285  vertexProgram = vertex;
286  fragmentProgram = fragment;
287  program = 0;
288 }

Member Data Documentation

BlendMode Technique::Pass::blendMode

Blending mode - either default or an override

Definition at line 270 of file technique.h.

Referenced by setupGLState().

bool Technique::Pass::colorWrite

Whether to write the color buffer or not

Definition at line 258 of file technique.h.

Referenced by setupGLState().

Face Technique::Pass::cullMode

Polygon backface culling mode. Front will CULL Front faces, so standard is Back

Definition at line 276 of file technique.h.

Referenced by setupGLState().

DepthFunction Technique::Pass::depthFunction

Z-test function - far queue always overrides with Always

Definition at line 273 of file technique.h.

Referenced by setupGLState().

float Technique::Pass::lineWidth

Line width - for line-mode polygons

Definition at line 288 of file technique.h.

Referenced by setupGLState().

unsigned int Technique::Pass::maxIterations

The maximum number of iterations, 0 means infinite - nonzero helps keep performance acceptable

Definition at line 267 of file technique.h.

float Technique::Pass::offsetFactor

Polygon offset slope factor

Definition at line 282 of file technique.h.

Referenced by setupGLState().

float Technique::Pass::offsetUnits

Polygon offset units

Definition at line 285 of file technique.h.

Referenced by setupGLState().

unsigned int Technique::Pass::perLightIteration

The number of lights this pass can handle, 0 means infinite

Definition at line 264 of file technique.h.

PolyMode Technique::Pass::polyMode

Polygon rasterization mode

Definition at line 279 of file technique.h.

Referenced by setupGLState().

int Technique::Pass::sequence

Sequence number, the order in which passes are rendered

Definition at line 291 of file technique.h.

bool Technique::Pass::sRGBAware

Shaders are aware of SRGB frambuffers, so everything should be set up for sRGB blending if sRGB framebuffers are available. Shaders will be compiled with SRGB_FRAMEBUFFER set to 0 (not supported) or 1 (supported).

Definition at line 255 of file technique.h.

Referenced by setupGLState().

Type Technique::Pass::type

Type of pass - shader or fixed pipeline

Definition at line 249 of file technique.h.

Referenced by Mesh::ProcessDrawQueue().

Tristate Technique::Pass::zWrite

Whether to write the z-buffer or not - default or an override

Definition at line 261 of file technique.h.


The documentation for this class was generated from the following files: