shader preprocessing is working
This commit is contained in:
parent
5305fdeac2
commit
5a52b19cfe
@ -1,15 +1,31 @@
|
|||||||
#include "shadersource.h"
|
#include "shadersource.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <sstream>
|
||||||
#include "shader.h"
|
#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)
|
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)
|
if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -18,8 +34,7 @@ Shader* ShaderSource::compile(int nbDefines = 0, const char** defines = NULL)
|
|||||||
{
|
{
|
||||||
if(sources[i] == NULL)
|
if(sources[i] == NULL)
|
||||||
continue;
|
continue;
|
||||||
compiledSources[i] = std::string(sources[i]);
|
compiledSources[i] = preprocess(*(sources[i]), nbDefines, defines);
|
||||||
preprocess(compiledSources[i], nbDefines, defines);
|
|
||||||
}
|
}
|
||||||
if(sources[GEOMETRY] != NULL)
|
if(sources[GEOMETRY] != NULL)
|
||||||
return new Shader(compiledSources[VERTEX], compiledSources[GEOMETRY], compiledSources[FRAGMENT]);
|
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]);
|
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");
|
for(int i=0; i<nbDefines; ++i)
|
||||||
std::string line;
|
{
|
||||||
while (std::getline(f, line)) {
|
if(str.compare(defines[i]) == 0)
|
||||||
if(line.at(0) == '#')
|
return true;
|
||||||
{
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
@ -17,15 +17,16 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
ShaderSource();
|
ShaderSource();
|
||||||
|
~ShaderSource();
|
||||||
|
|
||||||
void addSource(const char *source, SourceType type);
|
void addSource(const char *source, SourceType type);
|
||||||
|
|
||||||
Shader* compile(int nbDefines = 0, const char** defines = NULL);
|
Shader* compile(int nbDefines = 0, const char** defines = NULL);
|
||||||
|
|
||||||
private:
|
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
|
#endif // SHADERSOURCE_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user