HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RE_ShaderAtlas.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  * A collection of shader programs which can be retrieved with string tags.
10  */
11 
12 #ifndef RE_ShaderAtlas_h
13 #define RE_ShaderAtlas_h
14 
15 #include "RE_API.h"
16 #include "RE_ShaderHandle.h"
17 #include <UT/UT_StringMap.h>
18 #include <UT/UT_StringHolder.h>
19 #include <UT/UT_SharedPtr.h>
20 
21 class RE_Render;
22 class RE_Shader;
23 
25 
26 /// A Shader Atlas is a collection of related shaders identified by tags.
27 /// It has its own simple file format which associates .prog shader files with
28 /// a set of tags. To retrieve that shader, supply the list of tags.
30 {
31 public:
32  RE_ShaderAtlas(const UT_StringRef &shader_atlas_file);
33  virtual ~RE_ShaderAtlas();
34 
35  // Load an atlas file. Does not load all the shaders,
36  // just their definitions.
37  bool loadAtlas();
38 
39  // Return the bit corresponding to a string tag, needed for getShader().
40  // These should be cached to avoid string ops, often right after loadAtlas.
41  exint getBit(const UT_StringRef &tag) const;
42 
43  // Return the shader matching the tags (exact matches only)
44  RE_Shader *getShader(RE_Render *r, exint shbits) const;
45 
46  const UT_StringHolder& getFilePath() const { return myAtlasFile; }
47 
48  // This is mostly for debug (glslcompile)
49  const UT_Map<exint, RE_ShaderHandlePtr> &shaders() const { return myMap; }
50  const UT_StringMap<exint> &bits() const { return myBits; }
51 
52  void printTags(exint tags);
53 protected:
54  void applyDefsToMatchingShaders(const UT_StringArray &tag_stack,
55  UT_WorkBuffer &defines);
56  void addShader(const UT_StringArray &tag_stack,
57  const UT_StringArray &tokens,
58  const UT_StringHolder &program,
59  UT_WorkBuffer &defines);
60  bool addShader(exint key,
61  const UT_StringHolder &program,
62  const UT_StringHolder &defines,
63  bool check_for_abs_path);
64  virtual
65  RE_ShaderHandlePtr createShaderImpl(const char *program,
66  const char *defines);
67  exint registerTokens(const UT_StringArray &tokens);
68 
71  int myMaxBit;
72 
77 };
78 
79 // sample.patl:
80 
81 // // This is a sample program atlas file.
82 // shaded
83 // {
84 // . shaded_geo.prog
85 // wire shaded_wire.prog
86 // flat
87 // {
88 // . flat_shaded_geo.prog
89 // wire flat_shaded_wire.prog
90 // }
91 // vertnorm shaded_geo.prog : "#define VERTEX_NORMALS"
92 // }
93 // wire wireframe.prog
94 // wire hidden hidden_line_wire.prog
95 // tangent APPLYTO vertnorm : "#define USE_TANGENTS"
96 
97 // atlas->getShader(ren, atlas->getBit("shaded") | atlas->getBit("wire"));
98 // returns the shader program shaded_wire.prog.
99 // atlas->getShader(ren, atlas->getBit("flat") | atlas->getBit("wire"));
100 // returns null, no such shader
101 
102 // NOTES:
103 // - putting a tag before {} adds it to all programs in that group.
104 // - multiple tags can be applied on the line the program is specified on.
105 // - '.' acts as an empty tag (no tag specified)
106 // - It is an error to have 2 or more shaders with the exact same set of tags.
107 // - A shader can be referenced by more than one set of tags, a unique instance
108 // will be returned for each set.
109 // - : "" can be specified after a program file to inject the text within ""
110 // to the top of all shader files' text. This can be used to further vary
111 // the shader code by use of #ifdef's in the shader text.
112 // - DUPLICATE will apply the preceding tag(s) and define to all existing
113 // shaders that are tagged with the following matching tag(s). The matching
114 // tags may be zero, in which case all existing shaders are affected.
115 // "Existing shaders" means every shader previously created at that point
116 // in the file. In the example above, all shaders with the 'vertnorm' tag
117 // are duplicated and the 'tangent' and "#define USE_TANGENTS" tag added to
118 // the new copies.
119 
120 #endif
UT_StringHolder myAtlasDir
GR_API void loadAtlas(RV_Instance *inst, RV_ShaderAtlas &atlas, UT_Map< exint, RV_ShaderProgram * > &programmap)
#define RE_API
Definition: RE_API.h:10
UT_SharedPtr< RE_ShaderHandle > RE_ShaderHandlePtr
int64 exint
Definition: SYS_Types.h:125
UT_StringMap< exint > myBits
UT_StringHolder myAtlasFile
const UT_Map< exint, RE_ShaderHandlePtr > & shaders() const
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:36
const UT_StringHolder & getFilePath() const
const UT_StringMap< exint > & bits() const
UT_Map< exint, RE_ShaderHandlePtr > myMap
GLboolean r
Definition: glcorearb.h:1222
GLbitfield GLuint program
Definition: glcorearb.h:1931