Started working on the shader generator.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 11 years ago
parent d67198580a
commit 46363f1909

@ -0,0 +1,128 @@
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "sstream"
#include "driver_glsl_shader_generator.h"
#include "nel/3d/vertex_buffer.h"
namespace
{
inline bool hasFlag( uint32 data, uint32 flag )
{
if( ( data & flag ) != 0 )
return true;
else
return false;
}
}
namespace NL3D
{
uint16 vertexFlags[ CVertexBuffer::NumValue ] =
{
CVertexBuffer::PositionFlag,
CVertexBuffer::WeightFlag,
CVertexBuffer::NormalFlag,
CVertexBuffer::PrimaryColorFlag,
CVertexBuffer::SecondaryColorFlag,
CVertexBuffer::FogFlag,
CVertexBuffer::PaletteSkinFlag,
0,
CVertexBuffer::TexCoord0Flag,
CVertexBuffer::TexCoord1Flag,
CVertexBuffer::TexCoord2Flag,
CVertexBuffer::TexCoord3Flag,
CVertexBuffer::TexCoord4Flag,
CVertexBuffer::TexCoord5Flag,
CVertexBuffer::TexCoord6Flag,
CVertexBuffer::TexCoord7Flag
};
const char *attribNames[ CVertexBuffer::NumValue ] =
{
"position",
"weight",
"normal",
"color",
"color2",
"fog",
"paletteSkin",
"none",
"texCoord0",
"texCoord1",
"texCoord2",
"texCoord3",
"texCoord4",
"texCoord5",
"texCoord6",
"texCoord7"
};
CGLSLShaderGenerator::CGLSLShaderGenerator()
{
reset();
}
CGLSLShaderGenerator::~CGLSLShaderGenerator()
{
}
void CGLSLShaderGenerator::reset()
{
material = NULL;
vbFormat = 0;
}
void CGLSLShaderGenerator::generateVS( std::string &vs )
{
std::stringstream ss;
ss << "#version 330" << std::endl;
ss << "uniform mat4 mvpMatrix;" << std::endl;
ss << std::endl;
for( int i = 0; i < CVertexBuffer::NumValue; i++ )
{
if( hasFlag( vbFormat, vertexFlags[ i ] ) )
{
ss << "layout ( location = ";
ss << i;
ss << " ) ";
ss << "in vec4 ";
ss << attribNames[ i ];
ss << ";";
ss << std::endl;
}
}
ss << std::endl;
ss << "void main( void )" << std::endl;
ss << "{" << std::endl;
ss << "gl_Position = mvp * " << attribNames[ 0 ] << ";" << std::endl;
ss << "}" << std::endl;
vs.append( ss.str() );
}
void CGLSLShaderGenerator::generatePS( std::string &ps )
{
}
}

@ -0,0 +1,48 @@
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef GLSL_SHADER_GENERATOR
#define GLSL_SHADER_GENERATOR
#include <string>
namespace NL3D
{
class CMaterial;
class CGLSLShaderGenerator
{
public:
CGLSLShaderGenerator();
~CGLSLShaderGenerator();
void reset();
void generateVS( std::string &vs );
void generatePS( std::string &ps );
void setMaterial( CMaterial *mat ){ material = mat; }
void setVBFormat( uint16 format ){ vbFormat = format; }
private:
uint16 vbFormat;
CMaterial const *material;
};
}
#endif

@ -33,6 +33,7 @@
#include "nel/misc/hierarchical_timer.h" #include "nel/misc/hierarchical_timer.h"
#include "nel/misc/dynloadlib.h" #include "nel/misc/dynloadlib.h"
#include "driver_opengl_vertex_buffer_hard.h" #include "driver_opengl_vertex_buffer_hard.h"
#include "driver_glsl_shader_generator.h"
using namespace std; using namespace std;
@ -333,14 +334,25 @@ CDriverGL3::CDriverGL3()
_TextureTargetUpload = false; _TextureTargetUpload = false;
currentProgram = NULL; currentProgram = NULL;
shaderGenerator = new CGLSLShaderGenerator();
} }
// *************************************************************************** // ***************************************************************************
CDriverGL3::~CDriverGL3() CDriverGL3::~CDriverGL3()
{ {
H_AUTO_OGL(CDriverGL3_CDriverGLDtor) H_AUTO_OGL(CDriverGL3_CDriverGLDtor)
if( currentProgram != NULL )
{
delete currentProgram;
currentProgram = NULL;
}
release(); release();
delete shaderGenerator;
shaderGenerator = NULL;
#if defined(NL_OS_MAC) #if defined(NL_OS_MAC)
[_autoreleasePool release]; [_autoreleasePool release];
#endif #endif

@ -277,6 +277,8 @@ public:
}; };
class CGLSLShaderGenerator;
// *************************************************************************** // ***************************************************************************
class CDriverGL3 : public IDriver class CDriverGL3 : public IDriver
@ -374,6 +376,7 @@ public:
static inline void setupCausticsSecondTex(uint stage);*/ static inline void setupCausticsSecondTex(uint stage);*/
virtual bool setupMaterial(CMaterial& mat); virtual bool setupMaterial(CMaterial& mat);
bool setupProgram(CMaterial& mat);
virtual void startSpecularBatch(); virtual void startSpecularBatch();
virtual void endSpecularBatch(); virtual void endSpecularBatch();
@ -431,6 +434,7 @@ public:
virtual bool renderLines(CMaterial& mat, uint32 firstIndex, uint32 nlines); virtual bool renderLines(CMaterial& mat, uint32 firstIndex, uint32 nlines);
virtual bool renderTriangles(CMaterial& Mat, uint32 firstIndex, uint32 ntris); virtual bool renderTriangles(CMaterial& Mat, uint32 firstIndex, uint32 ntris);
bool renderTriangles2(CMaterial& Mat, uint32 firstIndex, uint32 ntris );
virtual bool renderSimpleTriangles(uint32 firstTri, uint32 ntris); virtual bool renderSimpleTriangles(uint32 firstTri, uint32 ntris);
virtual bool renderRawPoints(CMaterial& Mat, uint32 startIndex, uint32 numPoints); virtual bool renderRawPoints(CMaterial& Mat, uint32 startIndex, uint32 numPoints);
virtual bool renderRawLines(CMaterial& Mat, uint32 startIndex, uint32 numLines); virtual bool renderRawLines(CMaterial& Mat, uint32 startIndex, uint32 numLines);
@ -1351,6 +1355,8 @@ private:
private: private:
CGLSLShaderGenerator *shaderGenerator;
IProgramObject *currentProgram; IProgramObject *currentProgram;
bool setupARBVertexProgram (const CVPParser::TProgram &parsedProgram, GLuint id, bool &specularWritten); bool setupARBVertexProgram (const CVPParser::TProgram &parsedProgram, GLuint id, bool &specularWritten);

@ -2,6 +2,8 @@
#include "driver_glsl_program.h" #include "driver_glsl_program.h"
#include "driver_glsl_vertex_program.h" #include "driver_glsl_vertex_program.h"
#include "driver_glsl_pixel_program.h" #include "driver_glsl_pixel_program.h"
#include "driver_glsl_shader_generator.h"
#include "driver_opengl_vertex_buffer_hard.h"
#include "nel/3d/i_program_object.h" #include "nel/3d/i_program_object.h"
namespace NL3D namespace NL3D
@ -114,6 +116,29 @@ namespace NL3D
return true; return true;
} }
bool CDriverGL3::renderTriangles2( CMaterial &mat, uint32 startIndex, uint32 numTris )
{
if( !setupProgram( mat ) )
return false;
glDrawArrays( GL_TRIANGLES, startIndex * 3, numTris * 3 );
return true;
}
bool CDriverGL3::setupProgram( CMaterial &mat )
{
std::string vs;
std::string ps;
shaderGenerator->reset();
shaderGenerator->setMaterial( &mat );
shaderGenerator->setVBFormat( _CurrentVertexBufferHard->VB->getVertexFormat() );
shaderGenerator->generateVS( vs );
shaderGenerator->generatePS( ps );
return true;
}
} }

@ -265,6 +265,8 @@ bool CDriverGL3::renderTriangles(CMaterial& mat, uint32 firstIndex, uint32 ntris
if ( !setupMaterial(mat) || _LastIB._Values == NULL ) if ( !setupMaterial(mat) || _LastIB._Values == NULL )
return false; return false;
setupProgram( mat );
refreshTexMatrices(); refreshTexMatrices();
if (_CurrentVertexBufferHard && _CurrentVertexBufferHard->isInvalid()) return true; if (_CurrentVertexBufferHard && _CurrentVertexBufferHard->isInvalid()) return true;

Loading…
Cancel
Save