Privacy
An open-source, flexible 3D physical simulation framework
shader-function.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 #ifndef OSG_MATERIAL_MANAGER_SHADER_FUNC_H
23 #define OSG_MATERIAL_MANAGER_SHADER_FUNC_H
24 
25 #include <cstdio>
26 #include <set>
27 #include <list>
28 #include <map>
29 #include <string>
30 #include <vector>
31 #include "shader-types.h"
32 
33 namespace osg_material_manager {
34 
35  class ShaderFunc {
36  public:
37  ShaderFunc(std::string name, std::vector<std::string> args, unsigned int priority=0) {
38  funcs.push_back(FunctionCall(name, args, priority, 0));
39  this->name = name;
40  // minimum gl version
41  minVersion = 120;
42  shaderCode = code();
43  }
44 
46  this->name = "";
47  // minimum gl version
48  minVersion = 120;
49  shaderCode = code();
50  }
51 
53  if (minVersion>this->minVersion)
54  this->minVersion = minVersion;
55  }
56 
57  int getMinVersion() {
58  return minVersion;
59  }
60 
61  void addDependencyCode(std::string codeId, std::string code) {
62  deps[codeId] = code;
63  }
64 
65  std::vector< std::pair<std::string,std::string> > getDeps() const {
66  std::vector< std::pair<std::string,std::string> > v;
67  v.reserve(deps.size());
68  v.insert(v.begin(), deps.begin(), deps.end());
69  return v;
70  }
71 
72  void addVarying(GLSLVarying varying) {
73  varyings.insert(varying);
74  }
75  const std::set<GLSLVarying>& getVaryings() const {
76  return varyings;
77  }
78 
79  void addUniform(GLSLUniform uniform) {
80  uniforms.insert(uniform);
81  }
82  const std::set<GLSLUniform>& getUniforms() const {
83  return uniforms;
84  }
85 
86  void addConstant(GLSLConstant constant) {
87  constants.insert(constant);
88  }
89  const std::set<GLSLConstant>& getConstants() const {
90  return constants;
91  }
92 
94  attributes.insert(att);
95  }
96  const std::set<GLSLAttribute>& getAttributes() const {
97  return attributes;
98  }
99 
100  void enableExtension(std::string extensionName) {
101  enabledExtensions.insert(extensionName);
102  }
103  const std::set<std::string>& getEnabledExtensions() const {
104  return enabledExtensions;
105  }
106 
107  void disableExtension(std::string extensionName) {
108  disabledExtensions.insert(extensionName);
109  }
110  const std::set<std::string>& getDisabledExtensions() const {
111  return disabledExtensions;
112  }
113 
114  void addMainVar(GLSLVariable var, int priority=0) {
115  mainVars.push_back(MainVar(var.name, var.type, var.value, priority, mainVars.size()));
116  addMainVarDec((GLSLAttribute) {var.type, var.name});
117  }
118  const std::list<MainVar>& getMainVars() const {
119  return mainVars;
120  }
121 
123  std::set<GLSLAttribute>::const_iterator it = mainVarDecs.begin();
124  mainVarDecs.insert(att);
125  }
126 
127  const std::set<GLSLAttribute>& getMainVarDecs() const {
128  return mainVarDecs;
129  }
130 
131  void addSuffix(GLSLSuffix suffix) {
132  suffixes.insert(suffix);
133  }
134  const std::set<GLSLSuffix>& getSuffixes() const {
135  return suffixes;
136  }
137 
139  exports.push_back(e);
140  }
141  const std::vector<GLSLExport>& getExports() const {
142  return exports;
143  }
144 
145  void addSnippet(std::string line, int priority=0) {
146  snippets.push_back(PrioritizedLine(line, priority, snippets.size()));
147  }
148 
149  const std::vector<PrioritizedLine>& getSnippets() const {
150  return snippets;
151  }
152 
153  const std::vector<FunctionCall>& getFunctionCalls() const {
154  return funcs;
155  }
156 
157  std::string generateFunctionCode() {
158  return code() + "\n" + shaderCode;
159  }
160 
161  virtual std::string code() const {
162  return "";
163  }
164 
165  void merge(ShaderFunc *u);
166 
167  std::vector<std::string> generateFunctionCall();
168 
169  protected:
170  std::vector<FunctionCall> funcs;
171  std::string shaderCode;
172  std::string name;
173  // user variables
174  std::set<GLSLUniform> uniforms;
175  //
176  std::set<GLSLConstant> constants;
177  // passes calculations from vertex to fragment shader
178  std::set<GLSLVarying> varyings;
179  // per vertex attributes
180  std::set<GLSLAttribute> attributes;
181  // needed functions (tuple of name and code)
182  std::map<std::string,std::string> deps;
183  std::list<MainVar> mainVars;
184  std::set<GLSLAttribute> mainVarDecs;
185  std::vector<GLSLExport> exports;
186  std::set<GLSLSuffix> suffixes;
187  std::set<std::string> enabledExtensions;
188  std::set<std::string> disabledExtensions;
189  std::vector<PrioritizedLine> snippets;
190  // minimum gl version
192 
193  private:
194  static bool mainVarDecs_unique_pred(GLSLAttribute &first, GLSLAttribute &second) {
195  return first.name == second.name;
196  }
197 
198  }; // end of class ShaderFunc
199 
200 } // end of namespace osg_material_manager
201 
202 #endif /* OSG_MATERIAL_MANAGER_SHADER_FUNC_H */
203 
const std::list< MainVar > & getMainVars() const
void addAttribute(GLSLAttribute att)
std::vector< GLSLExport > exports
std::set< GLSLConstant > constants
virtual std::string code() const
void addSuffix(GLSLSuffix suffix)
const std::set< std::string > & getDisabledExtensions() const
const std::set< GLSLSuffix > & getSuffixes() const
void addVarying(GLSLVarying varying)
const std::set< GLSLAttribute > & getAttributes() const
void addMainVar(GLSLVariable var, int priority=0)
std::vector< std::pair< std::string, std::string > > getDeps() const
const std::vector< PrioritizedLine > & getSnippets() const
std::map< std::string, std::string > deps
std::vector< PrioritizedLine > snippets
const std::set< std::string > & getEnabledExtensions() const
void enableExtension(std::string extensionName)
const std::set< GLSLUniform > & getUniforms() const
void addUniform(GLSLUniform uniform)
std::set< std::string > disabledExtensions
void setMinVersion(int minVersion)
ShaderFunc(std::string name, std::vector< std::string > args, unsigned int priority=0)
std::set< GLSLUniform > uniforms
void addMainVarDec(GLSLAttribute att)
std::vector< std::string > generateFunctionCall()
void disableExtension(std::string extensionName)
const std::set< GLSLAttribute > & getMainVarDecs() const
std::set< GLSLVarying > varyings
osg_material_manager::FunctionCall FunctionCall
static bool mainVarDecs_unique_pred(GLSLAttribute &first, GLSLAttribute &second)
void addConstant(GLSLConstant constant)
std::vector< FunctionCall > funcs
osg_material_manager::MainVar MainVar
std::set< GLSLAttribute > mainVarDecs
const std::set< GLSLVarying > & getVaryings() const
const std::set< GLSLConstant > & getConstants() const
const std::vector< GLSLExport > & getExports() const
void addSnippet(std::string line, int priority=0)
void addDependencyCode(std::string codeId, std::string code)
const std::vector< FunctionCall > & getFunctionCalls() const
std::set< GLSLAttribute > attributes
std::set< GLSLSuffix > suffixes
std::set< std::string > enabledExtensions