2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2003-01-05 04:45:53 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/GLExtensions>
|
|
|
|
#include <osg/VertexProgram>
|
2003-04-10 20:11:40 +08:00
|
|
|
#include <osg/State>
|
2003-07-16 17:52:43 +08:00
|
|
|
#include <osg/Timer>
|
|
|
|
|
|
|
|
#include <list>
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2003-04-11 17:54:27 +08:00
|
|
|
// static cache of deleted vertex programs which can only
|
|
|
|
// by completely deleted once the appropriate OpenGL context
|
|
|
|
// is set.
|
2003-07-16 17:52:43 +08:00
|
|
|
typedef std::list<GLuint> VertexProgramObjectList;
|
|
|
|
typedef std::map<unsigned int,VertexProgramObjectList> DeletedVertexProgramObjectCache;
|
2003-04-11 17:54:27 +08:00
|
|
|
static DeletedVertexProgramObjectCache s_deletedVertexProgramObjectCache;
|
|
|
|
|
|
|
|
void VertexProgram::deleteVertexProgramObject(unsigned int contextID,GLuint handle)
|
|
|
|
{
|
|
|
|
if (handle!=0)
|
|
|
|
{
|
|
|
|
// insert the handle into the cache for the appropriate context.
|
|
|
|
s_deletedVertexProgramObjectCache[contextID].push_back(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-16 17:52:43 +08:00
|
|
|
void VertexProgram::flushDeletedVertexProgramObjects(unsigned int contextID,double /*currentTime*/, double& availableTime)
|
2003-04-11 17:54:27 +08:00
|
|
|
{
|
2003-07-16 17:52:43 +08:00
|
|
|
// if no time available don't try to flush objects.
|
|
|
|
if (availableTime<=0.0) return;
|
2003-04-11 17:54:27 +08:00
|
|
|
|
2003-07-16 17:52:43 +08:00
|
|
|
const osg::Timer& timer = *osg::Timer::instance();
|
|
|
|
osg::Timer_t start_tick = timer.tick();
|
|
|
|
double elapsedTime = 0.0;
|
2003-04-11 17:54:27 +08:00
|
|
|
|
|
|
|
DeletedVertexProgramObjectCache::iterator citr = s_deletedVertexProgramObjectCache.find(contextID);
|
|
|
|
if (citr!=s_deletedVertexProgramObjectCache.end())
|
|
|
|
{
|
2003-07-16 17:52:43 +08:00
|
|
|
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
2003-06-30 05:41:57 +08:00
|
|
|
|
2003-07-16 17:52:43 +08:00
|
|
|
VertexProgramObjectList& vpol = citr->second;
|
|
|
|
|
|
|
|
for(VertexProgramObjectList::iterator titr=vpol.begin();
|
|
|
|
titr!=vpol.end() && elapsedTime<availableTime;
|
|
|
|
)
|
2003-04-11 17:54:27 +08:00
|
|
|
{
|
|
|
|
extensions->glDeletePrograms( 1L, &(*titr ) );
|
2003-07-16 17:52:43 +08:00
|
|
|
titr = vpol.erase(titr);
|
|
|
|
elapsedTime = timer.delta_s(start_tick,timer.tick());
|
2003-04-11 17:54:27 +08:00
|
|
|
}
|
|
|
|
}
|
2003-07-16 17:52:43 +08:00
|
|
|
|
|
|
|
availableTime -= elapsedTime;
|
2003-04-11 17:54:27 +08:00
|
|
|
}
|
|
|
|
|
2003-01-05 04:45:53 +08:00
|
|
|
|
2003-04-10 20:11:40 +08:00
|
|
|
VertexProgram::VertexProgram()
|
2003-01-05 04:45:53 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VertexProgram::VertexProgram(const VertexProgram& vp,const CopyOp& copyop):
|
2003-04-10 20:11:40 +08:00
|
|
|
osg::StateAttribute(vp,copyop)
|
2003-04-11 17:54:27 +08:00
|
|
|
{
|
|
|
|
_vertexProgram = vp._vertexProgram;
|
|
|
|
|
|
|
|
for( LocalParamList::const_iterator itr = vp._programLocalParameters.begin();
|
|
|
|
itr != vp._programLocalParameters.end(); ++itr )
|
|
|
|
{
|
|
|
|
_programLocalParameters[itr->first] = itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( MatrixList::const_iterator mitr = vp._matrixList.begin();
|
|
|
|
mitr != vp._matrixList.end(); ++mitr )
|
|
|
|
{
|
|
|
|
_matrixList[mitr->first] = mitr->second;
|
|
|
|
}
|
|
|
|
}
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
// virtual
|
|
|
|
VertexProgram::~VertexProgram()
|
2003-04-11 17:54:27 +08:00
|
|
|
{
|
|
|
|
dirtyVertexProgramObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::dirtyVertexProgramObject()
|
|
|
|
{
|
|
|
|
for(unsigned int i=0;i<_vertexProgramIDList.size();++i)
|
|
|
|
{
|
|
|
|
if (_vertexProgramIDList[i] != 0)
|
|
|
|
{
|
|
|
|
VertexProgram::deleteVertexProgramObject(i,_vertexProgramIDList[i]);
|
|
|
|
_vertexProgramIDList[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
void VertexProgram::apply(State& state) const
|
|
|
|
{
|
2003-04-11 03:32:32 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
2003-01-05 04:45:53 +08:00
|
|
|
|
2003-04-11 03:32:32 +08:00
|
|
|
if (!extensions->isVertexProgramSupported())
|
|
|
|
return;
|
2003-01-05 04:45:53 +08:00
|
|
|
|
2003-04-10 20:11:40 +08:00
|
|
|
|
|
|
|
GLuint& vertexProgramId=getVertexProgramID(state.getContextID());
|
|
|
|
|
2003-01-05 04:45:53 +08:00
|
|
|
// Vertex Program
|
2003-04-10 20:11:40 +08:00
|
|
|
if (vertexProgramId != 0)
|
2003-01-05 04:45:53 +08:00
|
|
|
{
|
2003-04-11 03:32:32 +08:00
|
|
|
extensions->glBindProgram( GL_VERTEX_PROGRAM_ARB, vertexProgramId );
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
else if (!_vertexProgram.empty())
|
|
|
|
{
|
2003-05-07 21:13:13 +08:00
|
|
|
glGetError(); // Reset Error flags.
|
2003-04-11 03:32:32 +08:00
|
|
|
extensions->glGenPrograms( 1, &vertexProgramId );
|
|
|
|
extensions->glBindProgram( GL_VERTEX_PROGRAM_ARB, vertexProgramId );
|
|
|
|
extensions->glProgramString( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
|
|
|
|
_vertexProgram.length(), _vertexProgram.c_str());
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
// Check for errors
|
2003-02-10 22:23:31 +08:00
|
|
|
GLint errorposition;
|
2003-05-07 21:13:13 +08:00
|
|
|
glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorposition);
|
2003-01-05 04:45:53 +08:00
|
|
|
if (errorposition != -1)
|
|
|
|
{
|
2003-05-07 21:13:13 +08:00
|
|
|
notify(osg::FATAL) << "VertexProgram: " << glGetString(GL_PROGRAM_ERROR_STRING_ARB) << std::endl;
|
2003-01-05 04:45:53 +08:00
|
|
|
|
|
|
|
std::string::size_type start = _vertexProgram.rfind('\n', errorposition);
|
|
|
|
std::string::size_type stop = _vertexProgram.find('\n', errorposition);
|
|
|
|
if (start!=std::string::npos && stop!=std::string::npos)
|
|
|
|
{
|
|
|
|
notify(osg::FATAL) << " : " << _vertexProgram.substr(start+1, stop-start-2) << std::endl;
|
|
|
|
std::string pointAtproblem(errorposition-(start+1), ' ');
|
|
|
|
notify(osg::FATAL) << " : " << pointAtproblem << '^' << std::endl;
|
|
|
|
}
|
2003-05-01 16:51:03 +08:00
|
|
|
return;
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update local program parameters
|
|
|
|
{
|
|
|
|
for(LocalParamList::const_iterator itr=_programLocalParameters.begin();
|
|
|
|
itr!=_programLocalParameters.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2003-04-11 03:32:32 +08:00
|
|
|
extensions->glProgramLocalParameter4fv(GL_VERTEX_PROGRAM_ARB, (*itr).first, (*itr).second.ptr());
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update matrix
|
|
|
|
if (!_matrixList.empty())
|
|
|
|
{
|
|
|
|
for(MatrixList::const_iterator itr = _matrixList.begin();
|
|
|
|
itr!=_matrixList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
2003-05-07 21:13:13 +08:00
|
|
|
glMatrixMode((*itr).first);
|
2003-09-03 01:19:18 +08:00
|
|
|
(*itr).second.glLoadMatrix();
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
2003-05-07 21:13:13 +08:00
|
|
|
glMatrixMode(GL_MODELVIEW); // restore matrix mode
|
2003-01-05 04:45:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-11 03:32:32 +08:00
|
|
|
typedef buffered_value< ref_ptr<VertexProgram::Extensions> > BufferedExtensions;
|
|
|
|
static BufferedExtensions s_extensions;
|
|
|
|
|
|
|
|
VertexProgram::Extensions* VertexProgram::getExtensions(unsigned int contextID,bool createIfNotInitalized)
|
|
|
|
{
|
|
|
|
if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions;
|
|
|
|
return s_extensions[contextID].get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::setExtensions(unsigned int contextID,Extensions* extensions)
|
|
|
|
{
|
|
|
|
s_extensions[contextID] = extensions;
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexProgram::Extensions::Extensions()
|
|
|
|
{
|
|
|
|
setupGLExtenions();
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexProgram::Extensions::Extensions(const Extensions& rhs):
|
|
|
|
Referenced()
|
|
|
|
{
|
|
|
|
_isVertexProgramSupported = rhs._isVertexProgramSupported;
|
|
|
|
_glBindProgram = rhs._glBindProgram;
|
|
|
|
_glGenPrograms = rhs._glGenPrograms;
|
2003-04-11 17:54:27 +08:00
|
|
|
_glDeletePrograms = rhs._glDeletePrograms;
|
2003-04-11 03:32:32 +08:00
|
|
|
_glProgramString = rhs._glProgramString;
|
|
|
|
_glProgramLocalParameter4fv = rhs._glProgramLocalParameter4fv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::lowestCommonDenominator(const Extensions& rhs)
|
|
|
|
{
|
|
|
|
if (!rhs._isVertexProgramSupported) _isVertexProgramSupported = false;
|
|
|
|
|
|
|
|
if (!rhs._glBindProgram) _glBindProgram = 0;
|
|
|
|
if (!rhs._glGenPrograms) _glGenPrograms = 0;
|
2003-04-11 17:54:27 +08:00
|
|
|
if (!rhs._glDeletePrograms) _glDeletePrograms = 0;
|
2003-04-11 03:32:32 +08:00
|
|
|
if (!rhs._glProgramString) _glProgramString = 0;
|
|
|
|
if (!rhs._glProgramLocalParameter4fv) _glProgramLocalParameter4fv = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::setupGLExtenions()
|
|
|
|
{
|
|
|
|
_isVertexProgramSupported = isGLExtensionSupported("GL_ARB_vertex_program");
|
|
|
|
|
|
|
|
_glBindProgram = osg::getGLExtensionFuncPtr("glBindProgramARB");
|
|
|
|
_glGenPrograms = osg::getGLExtensionFuncPtr("glGenProgramsARB");
|
2003-04-11 17:54:27 +08:00
|
|
|
_glDeletePrograms = osg::getGLExtensionFuncPtr("glDeleteProgramsARB");
|
2003-04-11 03:32:32 +08:00
|
|
|
_glProgramString = osg::getGLExtensionFuncPtr("glProgramStringARB");
|
|
|
|
_glProgramLocalParameter4fv = osg::getGLExtensionFuncPtr("glProgramLocalParameter4fvARB");
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::glBindProgram(GLenum target, GLuint id) const
|
|
|
|
{
|
|
|
|
if (_glBindProgram)
|
|
|
|
{
|
|
|
|
typedef void (APIENTRY * BindProgramProc) (GLenum target, GLuint id);
|
|
|
|
((BindProgramProc)_glBindProgram)(target,id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify(WARN)<<"Error: glBindProgram not supported by OpenGL driver"<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::glGenPrograms(GLsizei n, GLuint *programs) const
|
|
|
|
{
|
|
|
|
if (_glGenPrograms)
|
|
|
|
{
|
|
|
|
typedef void (APIENTRY * GenProgramsProc) (GLsizei n, GLuint *programs);
|
|
|
|
((GenProgramsProc)_glGenPrograms)(n,programs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify(WARN)<<"Error: glGenPrograms not supported by OpenGL driver"<<std::endl;
|
2003-04-11 17:54:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::glDeletePrograms(GLsizei n, GLuint *programs) const
|
|
|
|
{
|
|
|
|
if (_glDeletePrograms)
|
|
|
|
{
|
|
|
|
typedef void (APIENTRY * DeleteProgramsProc) (GLsizei n, GLuint *programs);
|
|
|
|
((DeleteProgramsProc)_glDeletePrograms)(n,programs);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify(WARN)<<"Error: glDeletePrograms not supported by OpenGL driver"<<std::endl;
|
2003-04-11 03:32:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::glProgramString(GLenum target, GLenum format, GLsizei len, const void *string) const
|
|
|
|
{
|
|
|
|
if (_glProgramString)
|
|
|
|
{
|
|
|
|
typedef void (APIENTRY * ProgramStringProc) (GLenum target, GLenum format, GLsizei len, const void *string);
|
|
|
|
((ProgramStringProc)_glProgramString)(target,format, len, string);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify(WARN)<<"Error: glProgramString not supported by OpenGL driver"<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexProgram::Extensions::glProgramLocalParameter4fv(GLenum target, GLuint index, const GLfloat *params) const
|
|
|
|
{
|
|
|
|
if (_glProgramLocalParameter4fv)
|
|
|
|
{
|
|
|
|
typedef void (APIENTRY * ProgramLocalParameter4fvProc) (GLenum target, GLuint index, const GLfloat *params);
|
|
|
|
((ProgramLocalParameter4fvProc)_glProgramLocalParameter4fv)(target, index, params);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify(WARN)<<"Error: glProgramLocalParameter4fv not supported by OpenGL driver"<<std::endl;
|
|
|
|
}
|
|
|
|
}
|