shader preprocessing is working

This commit is contained in:
Anselme 2015-11-23 20:49:00 +01:00
parent 5305fdeac2
commit 5a52b19cfe
2 changed files with 56 additions and 15 deletions

View File

@ -1,15 +1,31 @@
#include "shadersource.h"
#include <string>
#include <sstream>
#include "shader.h"
ShaderSource::ShaderSource();
#include <iostream>
ShaderSource::ShaderSource()
{
for(int i=0; i<NB_TYPES; ++i)
sources[i] = NULL;
}
ShaderSource::~ShaderSource()
{
for(int i=0; i<NB_TYPES; ++i)
if(sources[i] != NULL)
delete(sources[i]);
}
void ShaderSource::addSource(const char *source, SourceType type)
{
if(sources[type] != NULL)
delete(sources[type]);
sources[type] = new std::string(source);
}
Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL)
Shader* ShaderSource::compile(int nbDefines, const char** defines)
{
if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL)
return NULL;
@ -18,8 +34,7 @@ Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL)
{
if(sources[i] == NULL)
continue;
compiledSources[i] = std::string(sources[i]);
preprocess(compiledSources[i], nbDefines, defines);
compiledSources[i] = preprocess(*(sources[i]), nbDefines, defines);
}
if(sources[GEOMETRY] != NULL)
return new Shader(compiledSources[VERTEX], compiledSources[GEOMETRY], compiledSources[FRAGMENT]);
@ -27,14 +42,39 @@ Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL)
return new Shader(compiledSources[VERTEX], compiledSources[FRAGMENT]);
}
void ShaderSource::preprocess(std::string &source, int nbDefines, const char** defines)
bool isDefined(const std::string &str, int nbDefines, const char** defines)
{
std::istringstream f("line1\nline2\nline3");
std::string line;
while (std::getline(f, line)) {
if(line.at(0) == '#')
for(int i=0; i<nbDefines; ++i)
{
// TODO
}
if(str.compare(defines[i]) == 0)
return true;
}
return false;
}
std::string ShaderSource::preprocess(std::string source, int nbDefines, const char** defines)
{
std::string compiled = "";
std::istringstream ss(source);
std::string line;
bool allowed = true;
while (std::getline(ss, line)) {
if(line.size() > 0 && line.at(0) == '#')
{
if(line.compare(0, 8, "#version") == 0)
compiled.append(line+'\n');
else if(line.compare(0, 7, "#ifdef ") == 0)
allowed = isDefined(line.substr(7), nbDefines, defines);
else if(line.compare(0, 8, "#ifndef ") == 0)
allowed = !isDefined(line.substr(8), nbDefines, defines);
else if(line.compare("#endif") == 0)
allowed = true;
else if(line.compare("#else") == 0)
allowed = !allowed;
}
else if(allowed)
compiled.append(line+'\n');
}
std::cout << compiled << std::endl;
return compiled;
}

View File

@ -17,15 +17,16 @@ public:
};
ShaderSource();
~ShaderSource();
void addSource(const char *source, SourceType type);
Shader* compile(int nbDefines = 0, const char** defines = NULL);
private:
char* sources[NB_TYPES];
std::string* sources[NB_TYPES];
void preprocess(std::string &source, int nbDefines, const char** defines);
std::string preprocess(std::string source, int nbDefines, const char** defines);
};
#endif // SHADERSOURCE_H