Commit 4258d7b5 authored by Jan Möbius's avatar Jan Möbius
Browse files

Preliminary work on the geometry shader inclusion

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@16272 383ad7c9-94d9-4d36-a494-682f7c89f535
parent ca371b12
......@@ -523,6 +523,12 @@ QString IRenderer::dumpCurrentRenderObjectsToString(ACG::RenderObject** _list, b
outStrm << progGen.getVertexShaderCode()[i] << "\n";
outStrm << "\n---------------------end-vertex-shader--------------------\n\n";
outStrm << "\n---------------------geometry-shader--------------------\n\n";
for (int i = 0; i < progGen.getGeometryShaderCode().size(); ++i)
outStrm << progGen.getGeometryShaderCode()[i] << "\n";
outStrm << "\n---------------------end-geometry-shader--------------------\n\n";
outStrm << "\n---------------------fragment-shader--------------------\n\n";
for (int i = 0; i < progGen.getFragmentShaderCode().size(); ++i)
outStrm << progGen.getFragmentShaderCode()[i] << "\n";
......@@ -537,15 +543,20 @@ QString IRenderer::dumpCurrentRenderObjectsToString(ACG::RenderObject** _list, b
outStrm << "\n";
outStrm << _list[i]->shaderDesc.toString();
outStrm << renderObjects_[i].shaderDesc.toString();
ShaderProgGenerator progGen(&(_list[i]->shaderDesc));
ShaderProgGenerator progGen(&(renderObjects_[i].shaderDesc));
outStrm << "\n---------------------vertex-shader--------------------\n\n";
for (int i = 0; i < progGen.getVertexShaderCode().size(); ++i)
outStrm << progGen.getVertexShaderCode()[i] << "\n";
outStrm << "\n---------------------end-vertex-shader--------------------\n\n";
outStrm << "\n---------------------geometry-shader--------------------\n\n";
for (int i = 0; i < progGen.getGeometryShaderCode().size(); ++i)
outStrm << progGen.getGeometryShaderCode()[i] << "\n";
outStrm << "\n---------------------end-geometry-shader--------------------\n\n";
outStrm << "\n---------------------fragment-shader--------------------\n\n";
for (int i = 0; i < progGen.getFragmentShaderCode().size(); ++i)
outStrm << progGen.getFragmentShaderCode()[i] << "\n";
......
......@@ -133,9 +133,14 @@ GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc, unsigne
for (int i = 0; i < progGen.getVertexShaderCode().size(); ++i)
outStrm << progGen.getVertexShaderCode()[i] << "\n";
outStrm << "\n---------------------geometry-shader--------------------\n\n";
for (int i = 0; i < progGen.getGeometryShaderCode().size(); ++i)
outStrm << progGen.getVertexShaderCode()[i] << "\n";
outStrm << "\n---------------------fragment-shader--------------------\n\n";
for (int i = 0; i < progGen.getFragmentShaderCode().size(); ++i)
for (int i = 0; i < progGen.getGeometryShaderCode().size(); ++i)
outStrm << progGen.getFragmentShaderCode()[i] << "\n";
......@@ -144,21 +149,26 @@ GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc, unsigne
}
#endif
// TODO: Geometry shader handling if supported!
// TODO: Build geometry shader only if supported!
GLSL::FragmentShader* fragShader = new GLSL::FragmentShader();
GLSL::VertexShader* vertShader = new GLSL::VertexShader();
// GLSL::GeometryShader* geomShader = new GLSL::GeometryShader();
GLSL::VertexShader* vertShader = new GLSL::VertexShader();
vertShader->setSource(progGen.getVertexShaderCode());
progGen.getGeometryShaderCode();
//geomShader->setSource(progGen.getGeometryShaderCode());
fragShader->setSource(progGen.getFragmentShaderCode());
vertShader->compile();
//geomShader->compile();
fragShader->compile();
GLSL::Program* prog = new GLSL::Program();
prog->attach(vertShader);
//prog->attach(geomShader);
prog->attach(fragShader);
prog->link();
......
......@@ -59,8 +59,6 @@ namespace ACG
int ShaderProgGenerator::numModifiers_ = 0;
ShaderModifier* ShaderProgGenerator::modifiers_[32] = {0};
......@@ -117,6 +115,25 @@ void ShaderGenerator::initVertexShaderIO(const ShaderGenDesc* _desc)
addOutput(strColorOut.c_str());
}
void ShaderGenerator::initGeometryShaderIO(const ShaderGenDesc* _desc) {
/// TODO
// TODO: Add shader description point to define geometry shader input
addStringToList("layout(triangles)", &inputs_, "", " in;");
// TODO: Add shader description point to define geometry shader output
addStringToList("layout(triangle_strip, max_vertices = 6)", &outputs_, "", " out;");
// TODO: Correctly pass information about the available data
// addStringToList("VertexData {", &inputs_, "in ", "");
// addStringToList("vec2 texCoord;", &inputs_, "", "");
// addStringToList("vec3 normal;", &inputs_, "", "");
// addStringToList("} VertexIn[3];", &inputs_, "", "");
std::cerr << "TODO : initGeometryShaderIO" << std::endl;
}
void ShaderGenerator::initFragmentShaderIO(const ShaderGenDesc* _desc)
......@@ -199,9 +216,6 @@ void ShaderGenerator::addLight(int lightIndex_, ShaderGenLightType _light)
void ShaderGenerator::addStringToList(QString _str,
QStringList* _arr,
const char* _prefix,
......@@ -357,7 +371,7 @@ QStringList ShaderProgGenerator::lightingCode_;
ShaderProgGenerator::ShaderProgGenerator(const ShaderGenDesc* _desc,
unsigned int _usage)
: vertex_(0), fragment_(0), usage_(_usage)
: vertex_(0), geometry_(0), fragment_(0), usage_(_usage)
{
if (shaderDir_.isEmpty())
std::cout << "error: call ShaderProgGenerator::setShaderDir() first!" << std::endl;
......@@ -642,6 +656,150 @@ int ShaderProgGenerator::checkForIncludes(QString _str, ShaderGenerator* _gen, Q
return 0;
}
void ShaderProgGenerator::buildGeometryShader()
{
std::cerr << "TODO : buildGeometryShader" << std::endl;
delete geometry_;
geometry_ = new ShaderGenerator();
geometry_->initGeometryShaderIO(&desc_);
//
// geometry_->initDefaultUniforms();
//
// apply i/o modifiers
for (int i = 0; i < numModifiers_; ++i)
{
if (usage_ & (1 << i))
modifiers_[i]->modifyGeometryIO(fragment_);
}
// initGenDefines(fragment_);
//
//
//
// // io
//
// // when to use fragment lights
// if (desc_.shadeMode == SG_SHADE_PHONG)
// {
// for (int i = 0; i < desc_.numLights; ++i)
// fragment_->addLight(i, desc_.lightTypes[i]);
// }
//
//
// assemble main function
QStringList mainCode;
// addLightingFunctions(&mainCode);
//
//
if (!fragmentTemplate_.size())
{
mainCode.push_back("void main()");
mainCode.push_back("{");
addGeometryBeginCode(&mainCode);
addGeometryEndCode(&mainCode);
mainCode.push_back("}");
}
else
{
// interpret loaded shader template:
// import #includes and replace SG_GEOMETRY_BEGIN/END markers
QString it;
foreach(it,geometryTemplate_)
{
if (!checkForIncludes(it, geometry_, getPathName(geometryShaderFile_)))
{
// str line is no include directive
// check for SG_ markers
if (it.contains("SG_GEOMETRY_BEGIN"))
addFragmentBeginCode(&mainCode);
else
{
if (it.contains("SG_GEOMETRY_END"))
addGeometryEndCode(&mainCode);
else
{
// no SG marker
mainCode.push_back(it);
}
}
}
}
}
geometry_->buildShaderCode(&mainCode);
}
void ShaderProgGenerator::addGeometryBeginCode(QStringList* _code)
{
std::cerr << "TODO : addGeometryBeginCode" << std::endl;
// // support for projective texture mapping
// _code->push_back("vec2 sg_vScreenPos = outPosCS.xy / outPosCS.w * 0.5 + vec2(0.5, 0.5);");
//
// _code->push_back("vec4 sg_cColor = vec4(g_cEmissive, ALPHA);");
//
// if (desc_.shadeMode == SG_SHADE_GOURAUD ||
// desc_.shadeMode == SG_SHADE_FLAT ||
// desc_.vertexColors)
// _code->push_back("sg_cColor = outColor;");
//
//
// if (desc_.shadeMode == SG_SHADE_PHONG)
// {
// _code->push_back("vec4 sg_vPosVS = outPosVS;");
// _code->push_back("vec3 sg_vNormalVS = outNormal;");
//
// addLightingCode(_code);
// }
//
// if (desc_.textured)
// {
// _code->push_back("vec4 sg_cTex = texture(g_Texture0, outTexCoord);");
// _code->push_back("sg_cColor *= sg_cTex;");
// }
//
//
// // apply modifiers
// for (int i = 0; i < numModifiers_; ++i)
// {
// if (usage_ & (1 << i))
// modifiers_[i]->modifyFragmentBeginCode(_code);
// }
}
void ShaderProgGenerator::addGeometryEndCode(QStringList* _code)
{
std::cerr << "TODO : addGeometryEndCode" << std::endl;
// _code->push_back("outFragment = sg_cColor;");
//
// // apply modifiers
// for (int i = 0; i < numModifiers_; ++i)
// {
// if (usage_ & (1 << i))
// modifiers_[i]->modifyFragmentEndCode(_code);
// }
}
void ShaderProgGenerator::buildFragmentShader()
{
delete fragment_;
......@@ -831,6 +989,7 @@ void ShaderProgGenerator::generateShaders()
{
loadShaderTemplateFromFile();
buildVertexShader();
buildGeometryShader();
buildFragmentShader();
}
......@@ -840,6 +999,11 @@ const QStringList& ShaderProgGenerator::getVertexShaderCode()
return vertex_->getShaderCode();
}
const QStringList& ShaderProgGenerator::getGeometryShaderCode()
{
return geometry_->getShaderCode();
}
const QStringList& ShaderProgGenerator::getFragmentShaderCode()
{
return fragment_->getShaderCode();
......@@ -851,6 +1015,11 @@ void ShaderProgGenerator::saveVertexShToFile(const char* _fileName)
vertex_->saveToFile(_fileName);
}
void ShaderProgGenerator::saveGeometryShToFile(const char* _fileName)
{
geometry_->saveToFile(_fileName);
}
void ShaderProgGenerator::saveFragmentShToFile(const char* _fileName)
{
fragment_->saveToFile(_fileName);
......
......@@ -316,6 +316,10 @@ public:
*/
void initVertexShaderIO(const ShaderGenDesc* _desc);
/** \brief Adds fitting vertex shader io for a given description
*/
void initGeometryShaderIO(const ShaderGenDesc* _desc);
/** \brief Adds fitting fragment shader io for a given description
*/
void initFragmentShaderIO(const ShaderGenDesc* _desc);
......@@ -478,6 +482,46 @@ public:
*/
virtual void modifyVertexEndCode(QStringList* _code) {}
/** \brief Add your own inputs/outputs to the geometry shader.
*
* your implementation may look like this:
*
* \code
* _shader->addInput("sampler2D depthSampler");
* _shader->addUniform("vec4 shaderParam");
* \endcode
*
* @param _shader shader interface
*/
virtual void modifyGeometryIO(ShaderGenerator* _shader) {}
/** \brief Append code the the geometry shader.
*
* Refer to the generation structure (\ref ShaderGenerator_page ) to see where
* your code is added and which variables you can modify.
* Use
* \code
* _code->push_back("...");
* \endcode
* to insert your code here
*
* @param _code string list of shader code.
*/
virtual void modifyGeometryBeginCode(QStringList* _code) {}
/** \brief Append code the the geometry shader.
*
* Refer to the generation structure (\ref ShaderGenerator_page )
* to see where your code is added and which variables you can modify.
* Use
* \code
* _code->push_back("...");
* \endcode
* to insert your code here.
* @param _code string list of shader code.
*/
virtual void modifyGeometryEndCode(QStringList* _code) {}
/** \brief Add your own inputs/outputs to the fragment shader.
*
......@@ -506,7 +550,7 @@ public:
*/
virtual void modifyFragmentBeginCode(QStringList* _code) {}
/** \brief Append code the the vertex shader.
/** \brief Append code the the fragment shader.
*
* Refer to the generation structure (\ref ShaderGenerator_page )
* to see where your code is added and which variables you can modify.
......@@ -554,12 +598,17 @@ public:
void saveVertexShToFile(const char* _fileName);
void saveGeometryShToFile(const char* _fileName);
void saveFragmentShToFile(const char* _fileName);
/** \brief Returns generated vertex shader code
*/
const QStringList& getVertexShaderCode();
/** \brief Returns generated geometry shader code
*/
const QStringList& getGeometryShaderCode();
/** \brief Returns generated fragment shader code
*/
const QStringList& getFragmentShaderCode();
......@@ -581,13 +630,16 @@ private:
void generateShaders();
void buildVertexShader();
void buildGeometryShader();
void buildFragmentShader();
void addVertexBeginCode(QStringList* _code);
void addVertexEndCode(QStringList* _code);
void addGeometryBeginCode(QStringList* _code);
void addGeometryEndCode(QStringList* _code);
void addFragmentBeginCode(QStringList* _code);
void addFragmentEndCode(QStringList* _code);
......@@ -616,9 +668,11 @@ private:
static void loadStringListFromFile(QString _fileName, QStringList* _out);
ShaderGenerator* vertex_;
ShaderGenerator* geometry_;
ShaderGenerator* fragment_;
QStringList vertexTemplate_;
QStringList geometryTemplate_;
QStringList fragmentTemplate_;
ShaderGenDesc desc_;
......@@ -631,6 +685,7 @@ private:
/// path + filename to shader templates
QString vertexShaderFile_;
QString geometryShaderFile_;
QString fragmentShaderFile_;
......
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