Radium Engine  1.5.0
ShaderConfiguration.cpp
1 #include <Core/Resources/Resources.hpp>
2 #include <Engine/Data/ShaderConfiguration.hpp>
3 #include <Engine/Data/ShaderProgramManager.hpp>
4 #include <glbinding/Version.h>
5 
9 static const std::string defaultVertexShader {
11  "[[Default resources path not found]]" ) +
12  "Shaders/Materials/Plain/Plain.vert.glsl" };
13 static const std::string defaultFragmentShader {
15  "[[Default resources path not found]]" ) +
16  "Shaders/Materials/Plain/Plain.frag.glsl" };
17 
18 namespace Ra {
19 namespace Engine {
20 namespace Data {
21 
22 std::ostream& operator<<( std::ostream& stream, const ShaderConfiguration& config ) {
23 
24  stream << " -- shader configuration [" << config.m_name << "]\n";
25 
26  for ( const auto& s : config.m_shaders ) {
27  stream << " shaders [" << s.first << " " << s.second << " ]\n";
28  }
29 
30  for ( const auto& s : config.m_properties ) {
31  stream << "props [" << s << "]\n";
32  }
33 
34  for ( const auto& s : config.m_includes ) {
35  stream << "inc [" << s.first << "]\n";
36  }
37  for ( const auto& s : config.m_named_strings ) {
38  stream << "props [" << s.first << "]\n";
39  }
40  return stream;
41 }
42 
43 ShaderConfiguration ShaderConfiguration::m_defaultShaderConfig( "Default Program",
44  defaultVertexShader,
45  defaultFragmentShader );
46 
47 ShaderConfiguration::ShaderConfiguration( const std::string& name ) :
48  m_name { name }, m_version { "#version " + s_glslVersion } {}
49 
50 ShaderConfiguration::ShaderConfiguration( const std::string& name,
51  const std::string& vertexShader,
52  const std::string& fragmentShader ) :
53  m_name { name }, m_version { "#version " + s_glslVersion } {
54  m_shaders[ShaderType_VERTEX] = { vertexShader, true };
55  m_shaders[ShaderType_FRAGMENT] = { fragmentShader, true };
56 }
57 
58 void ShaderConfiguration::addShader( ShaderType type, const std::string& name ) {
59  m_shaders[type] = { name, true };
60 }
61 
62 void ShaderConfiguration::addShaderSource( ShaderType type, const std::string& source ) {
63  m_shaders[type] = { source, false };
64 }
65 
66 void ShaderConfiguration::addProperty( const std::string& prop ) {
67  m_properties.insert( "#define " + prop );
68 }
69 
70 void ShaderConfiguration::addProperties( const std::list<std::string>& props ) {
71  for ( const auto& prop : props ) {
72  m_properties.insert( "#define " + prop );
73  }
74 }
75 
76 void ShaderConfiguration::removeProperty( const std::string& prop ) {
77  m_properties.erase( "#define " + prop );
78 }
79 
80 void ShaderConfiguration::addInclude( const std::string& incl, ShaderType type ) {
81  m_includes.emplace_back( "#include " + incl, type );
82 }
83 
84 void ShaderConfiguration::addIncludes( const std::list<std::string>& incls, ShaderType type ) {
85  for ( const auto& incl : incls ) {
86  m_includes.emplace_back( "#include " + incl, type );
87  }
88 }
89 
90 void ShaderConfiguration::removeInclude( const std::string& incl, ShaderType type ) {
91  CORE_UNUSED( type );
92  m_properties.erase( "#include " + incl );
93 }
94 
95 void ShaderConfiguration::addNamedString( const std::string& includepath,
96  const std::string& realfile ) {
97  m_named_strings.emplace_back( includepath, realfile );
98 }
99 
101  return ( ( !m_shaders[ShaderType_VERTEX].first.empty() ) &&
102  ( !m_shaders[ShaderType_FRAGMENT].first.empty() ) ) ||
103  !m_shaders[ShaderType_COMPUTE].first.empty();
104 }
105 
106 bool ShaderConfiguration::operator<( const ShaderConfiguration& o ) const {
107  bool res = false;
108 
109  for ( size_t i = 0; i < ShaderType_COUNT; ++i ) {
110  if ( m_shaders[i] != o.m_shaders[i] ) { return m_shaders[i] < o.m_shaders[i]; }
111  }
112 
113  if ( m_properties.size() == o.m_properties.size() ) {
114  if ( m_properties.empty() ) {
115  if ( m_includes.size() == o.m_includes.size() ) {
116  if ( m_includes.empty() ) { res = false; }
117  else {
118  auto lit = m_includes.begin();
119  auto rit = o.m_includes.begin();
120 
121  for ( ; ( lit != m_includes.end() ) && ( *lit == *rit ); ++lit, ++rit )
122  ;
123 
124  if ( lit == m_includes.end() ) { res = false; }
125  else { res = *lit < *rit; }
126  }
127  }
128  }
129  else {
130  auto lit = m_properties.begin();
131  auto rit = o.m_properties.begin();
132 
133  for ( ; ( lit != m_properties.end() ) && ( *lit == *rit ); ++lit, ++rit )
134  ;
135 
136  if ( lit == m_properties.end() ) { res = false; }
137  else { res = *lit < *rit; }
138  }
139  }
140  else { res = m_properties.size() < o.m_properties.size(); }
141 
142  return res;
143 }
144 
145 std::set<std::string> ShaderConfiguration::getProperties() const {
146  return m_properties;
147 }
148 
149 const std::vector<std::pair<std::string, ShaderType>>& ShaderConfiguration::getIncludes() const {
150  return m_includes;
151 }
152 
153 const std::vector<std::pair<std::string, std::string>>&
154 ShaderConfiguration::getNamedStrings() const {
155  return m_named_strings;
156 }
157 
158 std::string ShaderConfiguration::s_glslVersion { "410" };
159 
160 void ShaderConfiguration::setOpenGLVersion( const glbinding::Version& version ) {
161  std::map<std::string, std::string> openGLToGLSL { { "2.0", "110" },
162  { "2.1", "120" },
163  { "3.0", "130" },
164  { "3.1", "140" },
165  { "3.2", "150" },
166  { "3.3", "330" },
167  { "4.0", "400" },
168  { "4.1", "410" },
169  { "4.2", "420" },
170  { "4.3", "430" },
171  { "4.4", "440" },
172  { "4.5", "450" },
173  { "4.6", "460" } };
174  auto it = openGLToGLSL.find( version.toString() );
175  if ( it != openGLToGLSL.end() ) { s_glslVersion = it->second; }
176  else { s_glslVersion = "410"; }
177 }
179  return s_glslVersion;
180 }
181 
182 } // namespace Data
183 } // namespace Engine
184 } // namespace Ra
static void setOpenGLVersion(const glbinding::Version &version)
set the OpenGL version to use in the generated header for shaders.
void addShader(ShaderType type, const std::string &name)
void addInclude(const std::string &incl, ShaderType type=ShaderType_FRAGMENT)
void addNamedString(const std::string &includepath, const std::string &realfile)
Manage named strings (see ShaderProgramManager::addNamedString)
void addShaderSource(ShaderType type, const std::string &source)
void addProperty(const std::string &prop)
static std::string getGLSLVersion()
get the OpenGL version used in the generated header for shaders.
optional< std::string > getRadiumResourcesPath()
Get the path of Radium internal resources.
Definition: Resources.cpp:35
Definition: Cage.cpp:3