From 46363f19096010e98d09bcf2387172bb98227d2a Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Fri, 16 Aug 2013 04:23:51 +0200 Subject: [PATCH] Started working on the shader generator. --HG-- branch : gsoc2013-dfighter --- .../OpenGL3/driver_glsl_shader_generator.cpp | 128 ++++++++++++++++++ .../OpenGL3/driver_glsl_shader_generator.h | 48 +++++++ .../src/3d/driver/OpenGL3/driver_opengl.cpp | 12 ++ .../nel/src/3d/driver/OpenGL3/driver_opengl.h | 6 + .../driver/OpenGL3/driver_opengl_program.cpp | 25 ++++ .../driver/OpenGL3/driver_opengl_vertex.cpp | 2 + 6 files changed, 221 insertions(+) create mode 100644 code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp create mode 100644 code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h diff --git a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp new file mode 100644 index 000000000..80b56a24b --- /dev/null +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp @@ -0,0 +1,128 @@ +// NeL - MMORPG Framework +// 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 . + +#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 ) + { + } + +} + + diff --git a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h new file mode 100644 index 000000000..9246b6a5e --- /dev/null +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h @@ -0,0 +1,48 @@ +// NeL - MMORPG Framework +// 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 . + + +#ifndef GLSL_SHADER_GENERATOR +#define GLSL_SHADER_GENERATOR + +#include + +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 + + diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl.cpp b/code/nel/src/3d/driver/OpenGL3/driver_opengl.cpp index 79fa574b8..8212e5f7f 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl.cpp @@ -33,6 +33,7 @@ #include "nel/misc/hierarchical_timer.h" #include "nel/misc/dynloadlib.h" #include "driver_opengl_vertex_buffer_hard.h" +#include "driver_glsl_shader_generator.h" using namespace std; @@ -333,14 +334,25 @@ CDriverGL3::CDriverGL3() _TextureTargetUpload = false; currentProgram = NULL; + shaderGenerator = new CGLSLShaderGenerator(); } // *************************************************************************** CDriverGL3::~CDriverGL3() { H_AUTO_OGL(CDriverGL3_CDriverGLDtor) + + if( currentProgram != NULL ) + { + delete currentProgram; + currentProgram = NULL; + } + release(); + delete shaderGenerator; + shaderGenerator = NULL; + #if defined(NL_OS_MAC) [_autoreleasePool release]; #endif diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl.h b/code/nel/src/3d/driver/OpenGL3/driver_opengl.h index f90c655c9..c5f02b7c7 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl.h +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl.h @@ -277,6 +277,8 @@ public: }; +class CGLSLShaderGenerator; + // *************************************************************************** class CDriverGL3 : public IDriver @@ -374,6 +376,7 @@ public: static inline void setupCausticsSecondTex(uint stage);*/ virtual bool setupMaterial(CMaterial& mat); + bool setupProgram(CMaterial& mat); virtual void startSpecularBatch(); virtual void endSpecularBatch(); @@ -431,6 +434,7 @@ public: virtual bool renderLines(CMaterial& mat, uint32 firstIndex, uint32 nlines); 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 renderRawPoints(CMaterial& Mat, uint32 startIndex, uint32 numPoints); virtual bool renderRawLines(CMaterial& Mat, uint32 startIndex, uint32 numLines); @@ -1351,6 +1355,8 @@ private: private: + CGLSLShaderGenerator *shaderGenerator; + IProgramObject *currentProgram; bool setupARBVertexProgram (const CVPParser::TProgram &parsedProgram, GLuint id, bool &specularWritten); diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp index e087f1a2e..4e3ef9dfe 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp @@ -2,6 +2,8 @@ #include "driver_glsl_program.h" #include "driver_glsl_vertex_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" namespace NL3D @@ -114,6 +116,29 @@ namespace NL3D 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; + } } diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl_vertex.cpp b/code/nel/src/3d/driver/OpenGL3/driver_opengl_vertex.cpp index ef8c5d574..8d76c1e43 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_vertex.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_vertex.cpp @@ -265,6 +265,8 @@ bool CDriverGL3::renderTriangles(CMaterial& mat, uint32 firstIndex, uint32 ntris if ( !setupMaterial(mat) || _LastIB._Values == NULL ) return false; + setupProgram( mat ); + refreshTexMatrices(); if (_CurrentVertexBufferHard && _CurrentVertexBufferHard->isInvalid()) return true;