SparrowRenderer/shadersource.cpp
2015-11-23 20:49:00 +01:00

81 lines
2.2 KiB
C++

#include "shadersource.h"
#include <string>
#include <sstream>
#include "shader.h"
#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, const char** defines)
{
if(sources[VERTEX] == NULL || sources[FRAGMENT] == NULL)
return NULL;
std::string compiledSources[NB_TYPES];
for(int i=0; i<NB_TYPES; ++i)
{
if(sources[i] == NULL)
continue;
compiledSources[i] = preprocess(*(sources[i]), nbDefines, defines);
}
if(sources[GEOMETRY] != NULL)
return new Shader(compiledSources[VERTEX], compiledSources[GEOMETRY], compiledSources[FRAGMENT]);
else
return new Shader(compiledSources[VERTEX], compiledSources[FRAGMENT]);
}
bool isDefined(const std::string &str, int nbDefines, const char** defines)
{
for(int i=0; i<nbDefines; ++i)
{
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;
}