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