Commit 5f72759e authored by Christian Schulte zu Berge's avatar Christian Schulte zu Berge
Browse files

Extended tgt::ShaderManager to support both global GLSL version and per-shader version setting.

parent 08793132
...@@ -104,7 +104,7 @@ namespace campvis { ...@@ -104,7 +104,7 @@ namespace campvis {
tgt::GLContextScopedLock lock(_localContext); tgt::GLContextScopedLock lock(_localContext);
tgt::initGL(featureset); tgt::initGL(featureset);
ShdrMgr.setGlobalHeader("#version 330\n"); ShdrMgr.setDefaultGlslVersion("330");
LGL_ERROR; LGL_ERROR;
// ensure matching OpenGL specs // ensure matching OpenGL specs
......
...@@ -135,9 +135,20 @@ void ShaderPreprocessor::parse() { ...@@ -135,9 +135,20 @@ void ShaderPreprocessor::parse() {
result_.clear(); result_.clear();
includedFilenames_.clear(); includedFilenames_.clear();
if (! shd_->customGlslVersion_.empty()) {
parsePart("#version " + shd_->customGlslVersion_ + "\n", "Custom per-shader version");
}
else {
if (! ShdrMgr.getDefaultGlslVersion().empty()) {
parsePart("#version " + ShdrMgr.getDefaultGlslVersion() + "\n", "Global version");
}
}
if (! ShdrMgr.getGlobalHeader().empty()) {
outputComment("BEGIN GLOBAL HEADER"); outputComment("BEGIN GLOBAL HEADER");
parsePart(ShdrMgr.getGlobalHeader(), "GLOBAL HEADER"); parsePart(ShdrMgr.getGlobalHeader(), "GLOBAL HEADER");
outputComment("END GLOBAL HEADER"); outputComment("END GLOBAL HEADER");
}
if (!shd_->header_.empty()) { if (!shd_->header_.empty()) {
outputComment("BEGIN HEADER"); outputComment("BEGIN HEADER");
...@@ -600,7 +611,7 @@ void Shader::load(const string& filename, const string& customHeader) throw (Exc ...@@ -600,7 +611,7 @@ void Shader::load(const string& filename, const string& customHeader) throw (Exc
} }
void Shader::loadSeparate(const string& vert_filename, const string& geom_filename, void Shader::loadSeparate(const string& vert_filename, const string& geom_filename,
const string& frag_filename, const string& customHeader) const string& frag_filename, const string& customHeader, const std::string& customGlslVersion)
throw (Exception) throw (Exception)
{ {
ShaderObject* frag = 0; ShaderObject* frag = 0;
...@@ -613,6 +624,9 @@ void Shader::loadSeparate(const string& vert_filename, const string& geom_filena ...@@ -613,6 +624,9 @@ void Shader::loadSeparate(const string& vert_filename, const string& geom_filena
if (!customHeader.empty()) { if (!customHeader.empty()) {
vert->setHeader(customHeader); vert->setHeader(customHeader);
} }
if (!customGlslVersion.empty()) {
vert->setCustomGlslVersion(customGlslVersion);
}
try { try {
vert->loadSourceFromFile(vert_filename); vert->loadSourceFromFile(vert_filename);
...@@ -639,6 +653,9 @@ void Shader::loadSeparate(const string& vert_filename, const string& geom_filena ...@@ -639,6 +653,9 @@ void Shader::loadSeparate(const string& vert_filename, const string& geom_filena
if (!customHeader.empty()) { if (!customHeader.empty()) {
geom->setHeader(customHeader); geom->setHeader(customHeader);
} }
if (!customGlslVersion.empty()) {
geom->setCustomGlslVersion(customGlslVersion);
}
try { try {
geom->loadSourceFromFile(geom_filename); geom->loadSourceFromFile(geom_filename);
...@@ -666,6 +683,9 @@ void Shader::loadSeparate(const string& vert_filename, const string& geom_filena ...@@ -666,6 +683,9 @@ void Shader::loadSeparate(const string& vert_filename, const string& geom_filena
if (!customHeader.empty()) { if (!customHeader.empty()) {
frag->setHeader(customHeader); frag->setHeader(customHeader);
} }
if (!customGlslVersion.empty()) {
frag->setCustomGlslVersion(customGlslVersion);
}
try { try {
frag->loadSourceFromFile(frag_filename); frag->loadSourceFromFile(frag_filename);
...@@ -1289,22 +1309,22 @@ ShaderManager::ShaderManager() ...@@ -1289,22 +1309,22 @@ ShaderManager::ShaderManager()
{} {}
Shader* ShaderManager::load(const string& filename, const string& customHeader, Shader* ShaderManager::load(const string& filename, const string& customHeader,
bool activate) bool activate, const std::string& customGlslVersion /*= ""*/)
throw (Exception) throw (Exception)
{ {
return loadSeparate(filename + ".vert", filename + ".frag", customHeader, activate); return loadSeparate(filename + ".vert", filename + ".frag", customHeader, activate, customGlslVersion);
} }
Shader* ShaderManager::loadSeparate(const string& vert_filename, const string& frag_filename, Shader* ShaderManager::loadSeparate(const string& vert_filename, const string& frag_filename,
const string& customHeader, bool activate) const string& customHeader, bool activate, const std::string& customGlslVersion /*= ""*/)
throw (Exception) throw (Exception)
{ {
return loadSeparate(vert_filename, "", frag_filename, customHeader, activate); return loadSeparate(vert_filename, "", frag_filename, customHeader, activate, customGlslVersion);
} }
Shader* ShaderManager::loadSeparate(const string& vert_filename, const string& geom_filename, Shader* ShaderManager::loadSeparate(const string& vert_filename, const string& geom_filename,
const string& frag_filename, const string& frag_filename,
const string& customHeader, bool activate) const string& customHeader, bool activate, const std::string& customGlslVersion /*= ""*/)
throw (Exception) throw (Exception)
{ {
LDEBUG("Loading files " << vert_filename << " and " << frag_filename); LDEBUG("Loading files " << vert_filename << " and " << frag_filename);
......
...@@ -118,6 +118,10 @@ public: ...@@ -118,6 +118,10 @@ public:
*/ */
void setHeader(const std::string& h); void setHeader(const std::string& h);
void setCustomGlslVersion(const std::string& version) {
customGlslVersion_ = version;
}
ShaderType getType() { return shaderType_; } ShaderType getType() { return shaderType_; }
void setSource(std::string source) { void setSource(std::string source) {
...@@ -160,6 +164,7 @@ protected: ...@@ -160,6 +164,7 @@ protected:
std::string source_; std::string source_;
std::string unparsedSource_; std::string unparsedSource_;
std::string header_; std::string header_;
std::string customGlslVersion_; ///< Optional custom GLSL version, will override global GLSL version if set
bool isCompiled_; bool isCompiled_;
GLint inputType_; GLint inputType_;
GLint outputType_; GLint outputType_;
...@@ -385,7 +390,7 @@ protected: ...@@ -385,7 +390,7 @@ protected:
* @throw Exception if loading failed * @throw Exception if loading failed
*/ */
void loadSeparate(const std::string& vertFilename, const std::string& geomFilename, void loadSeparate(const std::string& vertFilename, const std::string& geomFilename,
const std::string& fragFilename, const std::string& customHeader = "") const std::string& fragFilename, const std::string& customHeader = "", const std::string& customGlslVersion = "")
throw (Exception); throw (Exception);
typedef std::list<ShaderObject*> ShaderObjects; typedef std::list<ShaderObject*> ShaderObjects;
...@@ -430,7 +435,7 @@ public: ...@@ -430,7 +435,7 @@ public:
* @throw Exception if loading failed * @throw Exception if loading failed
*/ */
Shader* load(const std::string& filename, const std::string& customHeader = "", Shader* load(const std::string& filename, const std::string& customHeader = "",
bool activate = true) bool activate = true, const std::string& customGlslVersion = "")
throw (Exception); throw (Exception);
/** /**
...@@ -447,7 +452,7 @@ public: ...@@ -447,7 +452,7 @@ public:
* @throw Exception if loading failed * @throw Exception if loading failed
*/ */
Shader* loadSeparate(const std::string& vertFilename, const std::string& fragFilename, Shader* loadSeparate(const std::string& vertFilename, const std::string& fragFilename,
const std::string& customHeader = "", bool activate = true) const std::string& customHeader = "", bool activate = true, const std::string& customGlslVersion = "")
throw (Exception); throw (Exception);
/** /**
...@@ -465,7 +470,7 @@ public: ...@@ -465,7 +470,7 @@ public:
*/ */
Shader* loadSeparate(const std::string& vertFilename, const std::string& geomFilename, Shader* loadSeparate(const std::string& vertFilename, const std::string& geomFilename,
const std::string& fragFilename, const std::string& fragFilename,
const std::string& customHeader, bool activate = true) const std::string& customHeader, bool activate = true, const std::string& customGlslVersion = "")
throw(Exception); throw(Exception);
bool rebuildAllShadersFromFile(); bool rebuildAllShadersFromFile();
...@@ -486,7 +491,25 @@ public: ...@@ -486,7 +491,25 @@ public:
return globalHeader_; return globalHeader_;
} }
/**
* Sets the default GLSL version string, will be added to the '#version' pragma
* at the beginning of each shader.
* \param version Default GLSL version string.
*/
void setDefaultGlslVersion(const std::string& version) {
defaultGlslVersion_ = version;
}
/**
* Gets the default GLSL version string, will be added to the '#version' pragma
* at the beginning of each shader.
*/
const std::string& getDefaultGlslVersion() const {
return defaultGlslVersion_;
}
protected: protected:
std::string defaultGlslVersion_; ///< Default GLSL version string, will be added to the '#version' pragma at the beginning of each shader
std::string globalHeader_; ///< Global header that will be added to all shaders. std::string globalHeader_; ///< Global header that will be added to all shaders.
static const std::string loggerCat_; static const std::string loggerCat_;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment