Abstract program objects, vertex, and pixel programs can now be created using the driver, so the user can now feed shaders to it.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 11 years ago
parent 41e84066fb
commit 9dc91a6ed1

@ -104,6 +104,9 @@ struct EBadDisplay : public NLMISC::Exception
// ****************************************************************************
typedef void (*emptyProc)(void);
class IProgram;
class IProgramObject;
// ****************************************************************************
// *** IMPORTANT ********************
// *** IF YOU MODIFY THE STRUCTURE OF THIS CLASS, PLEASE INCREMENT IDriver::InterfaceVersion TO INVALIDATE OLD DRIVER DLL
@ -464,7 +467,6 @@ public:
*/
virtual bool activeVertexBuffer(CVertexBuffer& VB)=0;
/** active a current IB, for future render().
*
* Don't change the index buffer format/size after having activated it.
@ -1021,6 +1023,19 @@ public:
*/
virtual bool activeVertexProgram (CVertexProgram *program) =0;
/// Activates the specified GPU program object
virtual bool activeProgramObject( IProgramObject *program ) = 0;
/// Creates a new GPU program object
virtual IProgramObject* createProgramObject() const = 0;
/// Creates a new Vertex program
virtual IProgram* createVertexProgram() const = 0;
/// Creates a new Pixel program
virtual IProgram* createPixelProgram() const = 0;
/**
* Setup constant values.
*/

@ -0,0 +1,83 @@
// 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 I_PROGRAM_H
#define I_PROGRAM_H
#include <string>
namespace NL3D
{
/// Interface class for all NEL GPU ( vertex, pixel, geometry, etc ) programs
class IProgram
{
public:
enum EProgramType
{
VERTEX_PROGRAM,
PIXEL_PROGRAM,
GEOMETRY_PROGRAM,
UNKNOWN_PROGRAM
};
IProgram(){
shaderId = 0;
compiled = false;
type = UNKNOWN_PROGRAM;
}
virtual ~IProgram(){}
virtual void shaderSource( const char *source ) = 0;
virtual bool compile( std::string &log ) = 0;
unsigned int getShaderId() const{ return shaderId; }
bool isCompiled() const{ return compiled; }
bool isVertexProgram() const{
if( type == VERTEX_PROGRAM )
return true;
else
return false;
}
bool isPixelProgram() const{
if( type == PIXEL_PROGRAM )
return true;
else
return false;
}
bool isGeometryProgram() const{
if( type == GEOMETRY_PROGRAM )
return true;
else
return false;
}
protected:
unsigned int shaderId;
bool compiled;
EProgramType type;
};
}
#endif

@ -0,0 +1,54 @@
// 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 I_PROGRAM_OBJECT_H
#define I_PROGRAM_OBJECT_H
namespace NL3D
{
class IProgram;
class IProgramObject
{
public:
IProgramObject(){}
virtual ~IProgramObject(){}
virtual bool attachVertexProgram( IProgram *shader ) = 0;
virtual bool attachPixelProgram( IProgram *shader ) = 0;
virtual bool detachVertexProgram( IProgram *shader ) = 0;
virtual bool detachPixelProgram( IProgram *shader ) = 0;
unsigned int getProgramId() const{ return programId; }
virtual bool link( std::string &log ) = 0;
bool isLinked() const{ return linked; }
protected:
unsigned int programId;
bool linked;
};
}
#endif

@ -136,6 +136,8 @@ SOURCE_GROUP(Driver FILES
../../include/nel/3d/dru.h
event_mouse_listener.cpp
../../include/nel/3d/event_mouse_listener.h
../../include/nel/3d/i_program.h
../../include/nel/3d/i_program_object.h
index_buffer.cpp
../../include/nel/3d/index_buffer.h
init_3d.cpp

@ -23,6 +23,7 @@ namespace NL3D
{
CGLSLPixelProgram::CGLSLPixelProgram()
{
type = PIXEL_PROGRAM;
shaderId = nglCreateShader( GL_FRAGMENT_SHADER );
nlassert( shaderId != 0 );
}

@ -27,9 +27,6 @@ namespace NL3D
public:
CGLSLPixelProgram();
~CGLSLPixelProgram();
bool isVertexProgram() const{ return false; }
bool isPixelProgram() const{ return true; }
};
}

@ -16,6 +16,7 @@
#include "driver_glsl_program.h"
#include "nel/3d/i_program_object.h"
#include "driver_glsl_shader_base.h"
#include "stdopengl.h"
#include "driver_opengl_extension.h"
@ -24,7 +25,8 @@
namespace NL3D
{
CGLSLProgram::CGLSLProgram()
CGLSLProgram::CGLSLProgram() :
IProgramObject()
{
programId = nglCreateProgram();
nlassert( programId != 0 );
@ -39,7 +41,7 @@ namespace NL3D
programId = 0;
}
bool CGLSLProgram::attachVertexProgram( CGLSLShaderBase *shader )
bool CGLSLProgram::attachVertexProgram( IProgram *shader )
{
if( !shader->isVertexProgram() )
return false;
@ -47,7 +49,7 @@ namespace NL3D
if( !shader->isCompiled() )
return false;
std::vector< CGLSLShaderBase* >::const_iterator itr =
std::vector< IProgram* >::const_iterator itr =
std::find( vertexPrograms.begin(), vertexPrograms.end(), shader );
if( itr != vertexPrograms.end() )
return false;
@ -63,7 +65,7 @@ namespace NL3D
return true;
}
bool CGLSLProgram::attachPixelProgram( CGLSLShaderBase *shader )
bool CGLSLProgram::attachPixelProgram( IProgram *shader )
{
if( !shader->isPixelProgram() )
return false;
@ -71,7 +73,7 @@ namespace NL3D
if( !shader->isCompiled() )
return false;
std::vector< CGLSLShaderBase* >::const_iterator itr =
std::vector< IProgram* >::const_iterator itr =
std::find( pixelPrograms.begin(), pixelPrograms.end(), shader );
if( itr != pixelPrograms.end() )
return false;
@ -87,12 +89,12 @@ namespace NL3D
return true;
}
bool CGLSLProgram::detachVertexProgram( CGLSLShaderBase *shader )
bool CGLSLProgram::detachVertexProgram( IProgram *shader )
{
if( !shader->isVertexProgram() )
return false;
std::vector< CGLSLShaderBase* >::iterator itr =
std::vector< IProgram* >::iterator itr =
std::find( vertexPrograms.begin(), vertexPrograms.end(), shader );
if( itr == vertexPrograms.end() )
return false;
@ -109,12 +111,12 @@ namespace NL3D
}
bool CGLSLProgram::detachPixelProgram( CGLSLShaderBase *shader )
bool CGLSLProgram::detachPixelProgram( IProgram *shader )
{
if( !shader->isPixelProgram() )
return false;
std::vector< CGLSLShaderBase* >::iterator itr =
std::vector< IProgram* >::iterator itr =
std::find( pixelPrograms.begin(), pixelPrograms.end(), shader );
if( itr == pixelPrograms.end() )
return false;
@ -155,7 +157,7 @@ namespace NL3D
void CGLSLProgram::deleteShaders()
{
std::vector< CGLSLShaderBase* >::iterator itr;
std::vector< IProgram* >::iterator itr;
itr = vertexPrograms.begin();
while( itr != vertexPrograms.end() )

@ -20,37 +20,30 @@
#include <vector>
#include <string>
#include "nel/3d/i_program_object.h"
namespace NL3D
{
class CGLSLShaderBase;
/// Wrapper class for OpenGL shader program object
class CGLSLProgram
class CGLSLProgram : public IProgramObject
{
public:
CGLSLProgram();
~CGLSLProgram();
bool attachVertexProgram( CGLSLShaderBase *shader );
bool attachPixelProgram( CGLSLShaderBase *shader );
bool attachVertexProgram( IProgram *shader );
bool attachPixelProgram( IProgram *shader );
bool detachVertexProgram( CGLSLShaderBase *shader );
bool detachPixelProgram( CGLSLShaderBase *shader );
bool detachVertexProgram( IProgram *shader );
bool detachPixelProgram( IProgram *shader );
bool link( std::string &log );
unsigned int getProgramId() const{ return programId; }
bool isLinked() const{ return linked; }
private:
void deleteShaders();
unsigned int programId;
bool linked;
std::vector< CGLSLShaderBase* > vertexPrograms;
std::vector< CGLSLShaderBase* > pixelPrograms;
std::vector< IProgram* > vertexPrograms;
std::vector< IProgram* > pixelPrograms;
};
}

@ -18,33 +18,20 @@
#ifndef GLSL_SHADER_BASE_H
#define GLSL_SHADER_BASE_H
#include "nel/3d/i_program.h"
namespace NL3D
{
/// Base class for OpenGL shader objects
class CGLSLShaderBase
class CGLSLShaderBase : public IProgram
{
public:
CGLSLShaderBase(){
shaderId = 0;
compiled = false;
}
CGLSLShaderBase() : IProgram(){}
virtual ~CGLSLShaderBase(){}
~CGLSLShaderBase(){}
void shaderSource( const char *source );
bool compile( std::string &log );
virtual bool isVertexProgram() const = 0;
virtual bool isPixelProgram() const = 0;
unsigned int getShaderId() const{ return shaderId; }
bool isCompiled() const{ return compiled; }
protected:
unsigned int shaderId;
private:
bool compiled;
};
}

@ -24,6 +24,7 @@ namespace NL3D
CGLSLVertexProgram::CGLSLVertexProgram() :
CGLSLShaderBase()
{
type = VERTEX_PROGRAM;
shaderId = nglCreateShader( GL_VERTEX_SHADER );
nlassert( shaderId != 0 );
}

@ -28,10 +28,6 @@ namespace NL3D
public:
CGLSLVertexProgram();
~CGLSLVertexProgram();
bool isVertexProgram() const{ return true; }
bool isPixelProgram() const{ return false; }
};
}

@ -102,7 +102,6 @@ class CDriverGL3;
class IVertexArrayRange;
class IVertexBufferHardGL;
class COcclusionQueryGL3;
class CGLSLProgram;
void displayGLError(GLenum error);
@ -1258,6 +1257,11 @@ private:
bool isVertexProgramSupported () const{ return true; }
bool isVertexProgramEmulated () const{ return false; }
bool activeVertexProgram (CVertexProgram *program);
bool activeProgramObject( IProgramObject *program );
IProgramObject* createProgramObject() const;
IProgram* createVertexProgram() const;
IProgram* createPixelProgram() const;
void setConstant (uint index, float, float, float, float);
void setConstant (uint index, double, double, double, double);
void setConstant (uint indexStart, const NLMISC::CVector& value);
@ -1277,7 +1281,6 @@ private:
/// \name Vertex program implementation
// @{
bool activeARBVertexProgram (CVertexProgram *program);
bool activeGLSLProgram( CGLSLProgram *program );
//@}

@ -2,10 +2,11 @@
#include "driver_glsl_program.h"
#include "driver_glsl_vertex_program.h"
#include "driver_glsl_pixel_program.h"
#include "nel/3d/i_program_object.h"
namespace NL3D
{
bool CDriverGL3::activeGLSLProgram( CGLSLProgram *program )
bool CDriverGL3::activeProgramObject( IProgramObject *program )
{
if( !program )
{
@ -35,64 +36,25 @@ namespace NL3D
}
const char *vs =
"#version 330\n"
"in vec4 vertex;\n"
"void main( void )\n"
"{\n"
"gl_Position = vertex;\n"
"}\n";
const char *fs =
"#version 330\n"
"out vec4 color;\n"
"void main( void )\n"
"{\n"
"color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
"}\n";
bool CDriverGL3::renderRawTriangles2( CMaterial &mat, uint32 startIndex, uint32 numTris )
IProgramObject* CDriverGL3::createProgramObject() const
{
std::string log;
CGLSLProgram program;
CGLSLVertexProgram *vp = new CGLSLVertexProgram();
CGLSLPixelProgram *pp = new CGLSLPixelProgram();
vp->shaderSource( vs );
if( !vp->compile( log ) )
{
nlinfo( "%s", log.c_str() );
return false;
}
pp->shaderSource( fs );
if( !pp->compile( log ) )
{
nlinfo( "%s", log.c_str() );
return false;
}
if( !program.attachVertexProgram( vp ) )
return false;
return new CGLSLProgram();
}
if( !program.attachPixelProgram( pp ) )
return false;
IProgram* CDriverGL3::createVertexProgram() const
{
return new CGLSLVertexProgram();
}
if( !program.link( log ) )
{
nlinfo( "%s", log.c_str() );
return false;
}
IProgram* CDriverGL3::createPixelProgram() const
{
return new CGLSLPixelProgram();
}
if( !activeGLSLProgram( &program ) )
return false;
bool CDriverGL3::renderRawTriangles2( CMaterial &mat, uint32 startIndex, uint32 numTris )
{
glDrawArrays( GL_TRIANGLES, startIndex * 3, numTris * 3 );
activeGLSLProgram( NULL );
return true;
}

@ -25,6 +25,8 @@
#include "nel/3d/u_camera.h"
#include "nel/3d/u_instance.h"
#include "nel/3d/u_3d_mouse_listener.h"
#include "nel/3d/i_program.h"
#include "nel/3d/i_program_object.h"
#include "nel/misc/i_xml.h"
#include "nel/misc/o_xml.h"
#include "nel/misc/file.h"
@ -445,14 +447,24 @@ namespace MaterialEditor
return loadShape( "primitives/teapot.shape" );
}
const char *vs =
"#version 330\n"
"in vec4 vertex;\n"
"void main( void )\n"
"{\n"
"gl_Position = vertex;\n"
"}\n";
const char *ps =
"#version 330\n"
"out vec4 color;\n"
"void main( void )\n"
"{\n"
"color = vec4( 1.0, 0.0, 0.0, 1.0 );\n"
"}\n";
void CNel3DInterface::drawTriangle()
{
driver->clearBuffers( NLMISC::CRGBA::Black );
NLMISC::CMatrix f;
f.identity();
driver->setFrustumMatrix( f );
NL3D::CDriverUser *d = dynamic_cast< NL3D::CDriverUser* >( driver );
if( d != NULL )
{
@ -484,11 +496,73 @@ namespace MaterialEditor
id->activeIndexBuffer( ib );
NL3D::CMaterial mat;
mat.setColor( NLMISC::CRGBA::White );
NL3D::IProgramObject *po = id->createProgramObject();
NL3D::IProgram *vp = id->createVertexProgram();
NL3D::IProgram *pp = id->createPixelProgram();
vp->shaderSource( vs );
pp->shaderSource( ps );
std::string log;
bool ok = false;
ok = vp->compile( log );
if( !ok )
{
delete po;
delete vp;
delete pp;
return;
}
ok = pp->compile( log );
if( !ok )
{
delete po;
delete vp;
delete pp;
return;
}
ok = po->attachVertexProgram( vp );
if( !ok )
{
delete po;
return;
}
ok = po->attachPixelProgram( pp );
if( !ok )
{
delete po;
return;
}
ok = po->link( log );
if( !ok )
{
delete po;
return;
}
ok = id->activeProgramObject( po );
if( !ok )
{
delete po;
return;
}
driver->clearBuffers( NLMISC::CRGBA::Black );
NLMISC::CMatrix f;
f.identity();
driver->setFrustumMatrix( f );
id->renderRawTriangles2( mat, 0, 1 );
driver->swapBuffers();
id->activeProgramObject( NULL );
delete po;
}
driver->swapBuffers();
}

Loading…
Cancel
Save