HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RE_ShaderHandle.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Render Library (C++)
7  *
8  * COMMENTS:
9  * Provides a handle to a shader and convenient methods for constructing
10  * shaders and testing for errors.
11  *
12  * Program files (.prog) are supported, with a limited syntax for
13  * constructing shaders. They contain shader files to be linked, in version
14  * blocks. A program file may have multiple version blocks. The highest
15  * supported GLSL version block is used.
16  *
17  * The directives are:
18  *
19  * #name <string> The name of the shader is "string"
20  * #version <ver> Specifies a base GLSL version (ie 110, 330, 410)
21  * #extension <ext> Extension ext must be supported (ie GL_ARB_shadows)
22  * #output <name> <num> Fragment output <name> is mapped to buffer <num>
23  *
24  * If either the #version check or any #extension check within the block
25  * fails, the entire block is discarded. A new block begins with #version.
26  *
27  * #name is used to specify an english name for the shader, otherwise
28  * the filename of the program is used. It resides outside of any version
29  * block.
30  *
31  * #output is used to connect fragment shader outputs to draw buffer
32  * indices. It is only used for GLSL versions >= 130.
33  *
34  * Comments are supported if the line begins with //. Comments are not
35  * supported elsewhere. All other non-blank lines are expected to be
36  * shader filenames. Indentation is supported but ignored.
37  *
38  * #name Blended Matte Shader
39  * // fallback shader
40  * #version 110
41  * basic/GL20/pos_transform.vert
42  * basic/GL20/const_color.frag
43  *
44  * #version 130
45  * #extension GL_ARB_draw_buffers
46  * #output fgcolor 0
47  * #output bgcolor 1
48  * basic/GL30/pos_transform.vert
49  * basic/GL30/blend_color.frag
50  *
51  * #version 400
52  * #output fgcolor 0
53  * #output bgcolor 1
54  * basic/GL40/pos_transform.vert
55  * basic/GL40/blend_color.frag
56  *
57  * If GLSL 4.0 is supported, the 400 block shaders (GL40) will be used.
58  * If GLSL 1.30 to 3.3 is supported along with the GL_ARB_draw_buffers
59  * extension, then 130 block shaders (GL30) are used.
60  * Otherwise, the 1.10 block's shaders will be used.
61  *
62  * You can determine the shader that was loaded via getCodeVersion().
63  */
64 #ifndef RE_ShaderHandle_h
65 #define RE_ShaderHandle_h
66 
67 #include "RE_Types.h"
68 #include <iosfwd>
69 #include <UT/UT_Lock.h>
70 #include <UT/UT_String.h>
71 
72 class RE_Render;
73 class RE_Shader;
74 
75 /// Simple interface to building a shader from a .prog file.
77 {
78 public:
79  /// Create a shader handle using a .prog file.
80  RE_ShaderHandle(const char *program_file,
81  bool register_shader = true,
82  const char *defines = NULL);
83 
84  /// Create a shader handle with source files. All source files must share
85  /// the same GLSL version (100,110,120,130,140,150,330,400,410...) and be
86  /// whitespace separated in the string.
87  RE_ShaderHandle(const char *program_name,
88  int code_version,
89  const char *files,
90  bool register_shader = true,
91  const char *defines = NULL);
92 
93  RE_ShaderHandle(const RE_ShaderHandle &handle);
94  ~RE_ShaderHandle();
95 
97  {
98  UT_ASSERT(myShader);
99  return myShader;
100  }
101 
102  /// Set the defines for the program, overwriting existing ones.
103  void setProgramDefines(const char *defines)
104  { myDefines.harden(defines); }
105  /// Add more defines to the defines list
106  void appendProgramDefines(const char *defines)
107  { myDefines +="\n"; myDefines += defines; }
108 
109  /// Default language is GLSL.
110  void setShaderLanguage(RE_ShaderLanguage lang);
111 
112  /// Returns true if the shader was initialized. The shader still may be
113  /// invalid (compiled improperly).
114  bool isInitialized() const { return myInitFlag; }
115 
116  /// Returns true if the shader successfully compiled. This may force a
117  /// compile.
119  {
120  if(!myInitFlag)
121  return compile(r);
122  return (myShader != NULL);
123  }
124 
125  /// Explicitly compile the shader. Has no effect if the shader is already
126  /// compiled. Returns true if the shader compiled successfully or was
127  /// already compiled, or false if the compile failed or previously failed.
128  bool compile(RE_Render *r);
129 
130  /// Returns the GLSL or Cg version that the shader was compiled with.
131  int getCodeVersion() const { return myCodeVersion; }
132 
133  /// Returns the shader for this handle. Will return NULL if the shader
134  /// did not compile.
135  RE_Shader *getShader() const { return myShader; }
136 
137  /// Prints this shader's errors, warnings and messages.
138  void printErrors(std::ostream &os) const;
139 
140  /// Returns a NULL-terminated string of errors. Empty string if no errors
141  /// exist, or if the shader has not been compiled yet.
142  const char *getErrors() const { return myErrors.nonNullBuffer(); }
143 
144  /// Name of the shader (if any) or the .prog pathname
145  const char *getName() const;
146 
147  const char *getSourceFiles() const { return mySourceFiles; }
148  const char *getDefines() const { return myDefines; }
149 
150  /// Returns a list of #defines for platform, vendor, driver version.
151  static const char *getSystemDefines(RE_Render *r)
152  {
153  if(!theVendorDefines.isstring())
154  initializeDriverInformation(r);
155  return theVendorDefines;
156  }
157 private:
158  static void initializeDriverInformation(RE_Render *r);
159 
160  UT_String myName;
161  RE_ShaderLanguage myShaderLanguage;
162  int myCodeVersion;
163  UT_String mySourceFiles;
164  UT_String myDefines;
165  RE_Shader *myShader;
166  bool myInitFlag;
167  UT_String myErrors;
168  UT_Lock myLock;
169  bool myRegisterFlag;
170  static UT_String theVendorDefines;
171 };
172 
173 #endif
#define RE_API
Definition: RE_API.h:10
const char * getErrors() const
const char * getDefines() const
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
void appendProgramDefines(const char *defines)
Add more defines to the defines list.
RE_Shader * operator->()
bool isInitialized() const
void setProgramDefines(const char *defines)
Set the defines for the program, overwriting existing ones.
static const char * getSystemDefines(RE_Render *r)
Returns a list of #defines for platform, vendor, driver version.
const char * getSourceFiles() const
RE_Shader * getShader() const
int getCodeVersion() const
Returns the GLSL or Cg version that the shader was compiled with.
GLboolean r
Definition: glcorearb.h:1221
RE_ShaderLanguage
Definition: RE_Types.h:240
bool isValid(RE_Render *r)
Simple interface to building a shader from a .prog file.