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_NonCopyable.h>
18 #include <UT/UT_SharedPtr.h>
19 #include <UT/UT_StringHolder.h>
20 #include <UT/UT_StringMap.h>
21 
22 class RE_Render;
23 class RE_Shader;
24 
26 
27 /// A Shader Atlas is a collection of related shaders identified by tags.
28 /// It has its own simple file format which associates .prog shader files with
29 /// a set of tags. To retrieve that shader, supply the list of tags.
31 {
32 public:
33  RE_ShaderAtlas(const UT_StringRef &shader_atlas_file);
34  virtual ~RE_ShaderAtlas();
35 
37 
38  // Load an atlas file. Does not load all the shaders,
39  // just their definitions.
40  bool loadAtlas(const char *global_defines = "");
41 
42  // Return the bit corresponding to a string tag, needed for getShader().
43  // These should be cached to avoid string ops, often right after loadAtlas.
44  exint getBit(const UT_StringRef &tag) const;
45 
46  // Return the shader matching the tags (exact matches only)
47  RE_Shader *getShader(RE_Render *r, exint shbits) const;
48 
49  const UT_StringHolder& getFilePath() const { return myAtlasFile; }
50 
51  // This is mostly for debug (glslcompile)
52  const UT_Map<exint, RE_ShaderHandlePtr> &shaders() const { return myMap; }
53  const UT_StringMap<exint> &bits() const { return myBits; }
54 
55  void printTags(exint tags);
56 protected:
57  void applyDefsToMatchingShaders(const UT_StringArray &tag_stack,
58  UT_WorkBuffer &defines);
59  void addShader(const UT_StringArray &tag_stack,
60  const UT_StringArray &tokens,
61  const UT_StringHolder &program,
62  UT_WorkBuffer &defines);
63  bool addShader(exint key,
64  const UT_StringHolder &program,
65  const UT_StringHolder &defines,
66  bool check_for_abs_path);
67  virtual
68  RE_ShaderHandlePtr createShaderImpl(const char *program,
69  const char *defines);
70  exint registerTokens(const UT_StringArray &tokens);
71 
74  int myMaxBit;
75 
80 };
81 
82 // sample.patl:
83 
84 // // This is a sample program atlas file.
85 // shaded
86 // {
87 // . shaded_geo.prog
88 // wire shaded_wire.prog
89 // flat
90 // {
91 // . flat_shaded_geo.prog
92 // wire flat_shaded_wire.prog
93 // }
94 // vertnorm shaded_geo.prog : "#define VERTEX_NORMALS"
95 // }
96 // wire wireframe.prog
97 // wire hidden hidden_line_wire.prog
98 // tangent APPLYTO vertnorm : "#define USE_TANGENTS"
99 
100 // atlas->getShader(ren, atlas->getBit("shaded") | atlas->getBit("wire"));
101 // returns the shader program shaded_wire.prog.
102 // atlas->getShader(ren, atlas->getBit("flat") | atlas->getBit("wire"));
103 // returns null, no such shader
104 
105 // NOTES:
106 // - putting a tag before {} adds it to all programs in that group.
107 // - multiple tags can be applied on the line the program is specified on.
108 // - '.' acts as an empty tag (no tag specified)
109 // - '@' in place of a shader filename will use the last entry's filename
110 // - It is an error to have 2 or more shaders with the exact same set of tags.
111 // - A shader can be referenced by more than one set of tags, a unique instance
112 // will be returned for each set.
113 // - : "" can be specified after a program file to inject the text within ""
114 // to the top of all shader files' text. This can be used to further vary
115 // the shader code by use of #ifdef's in the shader text.
116 // - DUPLICATE will apply the preceding tag(s) and define to all existing
117 // shaders that are tagged with the following matching tag(s). The matching
118 // tags may be zero, in which case all existing shaders are affected.
119 // "Existing shaders" means every shader previously created at that point
120 // in the file. In the example above, all shaders with the 'vertnorm' tag
121 // are duplicated and the 'tangent' and "#define USE_TANGENTS" tag added to
122 // the new copies.
123 // - if the DUPLICATE matching tag list includes '^' then tags after the '^'
124 // will be treated as an exclude list. The DUPLICATE will only apply to
125 // shaders that do not have any tags from the exclude list
126 
127 #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
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
UT_Map< exint, RE_ShaderHandlePtr > myMap
GLboolean r
Definition: glcorearb.h:1222
GLbitfield GLuint program
Definition: glcorearb.h:1931