Introduced new osg::PatchParameter StateAttribute class to wrap up glPatchParameter associated state.

Note, osg::Program::setParameter(GL_PATCH_VERTICES,num); is nolonger support and should be replaced by using the new PatchParameter class.
This commit is contained in:
Robert Osfield
2013-06-11 10:52:37 +00:00
parent 4623c251bb
commit 1a7f2fcb3e
8 changed files with 155 additions and 58 deletions

View File

@@ -4,6 +4,7 @@
*/
#include <osg/Program>
#include <osg/PatchParameter>
#include <osg/ShapeDrawable>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
@@ -175,7 +176,6 @@ osg::ref_ptr<osg::Program> createProgram(){
program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 3);
program->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
program->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);
program->setParameter(GL_PATCH_VERTICES,3);
return program;
}
@@ -213,6 +213,7 @@ public:
return osgGA::GUIEventHandler::handle(ea,gaa);
}
};
int main(int argc, char* argv[])
{
osgViewer::Viewer viewer;
@@ -226,6 +227,7 @@ int main(int argc, char* argv[])
state->addUniform(new osg::Uniform("LightPosition",osg::Vec3(0.25f, 0.25f, 1.0f)));
state->addUniform(tessInnerU.get());
state->addUniform(tessOuterU.get());
state->setAttribute(new osg::PatchParameter(3));
state->setAttribute(program.get());
// switch on the uniforms that track the modelview and projection matrices

View File

@@ -0,0 +1,88 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
*/
#ifndef OSG_PATCHPARAMETER
#define OSG_PATCHPARAMETER 1
#include <osg/Vec2>
#include <osg/Vec4>
#include <osg/StateAttribute>
#include <osg/GL2Extensions>
namespace osg {
/** Class which encapsulates glPatchParameter(..).
*/
class OSG_EXPORT PatchParameter : public StateAttribute
{
public :
PatchParameter(GLint vertices=3);
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
PatchParameter(const PatchParameter& rhs,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
StateAttribute(rhs,copyop),
_vertices(rhs._vertices),
_patchDefaultInnerLevel(rhs._patchDefaultInnerLevel),
_patchDefaultOuterLevel(rhs._patchDefaultOuterLevel) {}
META_StateAttribute(osg, PatchParameter, PATCH_PARAMETER);
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
virtual int compare(const StateAttribute& sa) const
{
// check the types are equal and then create the rhs variable
// used by the COMPARE_StateAttribute_Parameter macros below.
COMPARE_StateAttribute_Types(PatchParameter,sa)
// compare each parameter in turn against the rhs.
COMPARE_StateAttribute_Parameter(_vertices)
COMPARE_StateAttribute_Parameter(_patchDefaultInnerLevel)
COMPARE_StateAttribute_Parameter(_patchDefaultOuterLevel)
return 0; // passed all the above comparison macros, must be equal.
}
/** Set GL_PATCH_VERTICES parameter.*/
void setVertices(GLint vertices) { _vertices = vertices; }
/** Get GL_PATCH_VERTICES parameter.*/
GLint getVertices() const { return _vertices; }
/** Set GL_PATCH_DEFAULT_INNER_LEVEL parameter.*/
void setPatchDefaultInnerLevel(const osg::Vec2& level) { _patchDefaultInnerLevel = level; }
/** Get GL_PATCH_DEFAULT_INNER_LEVEL parameter.*/
const osg::Vec2& getPatchDefaultInnerLevel() const { return _patchDefaultInnerLevel; }
/** Set GL_PATCH_DEFAULT_OUTER_LEVEL parameter.*/
void setPatchDefaultOuterLevel(const osg::Vec4& level) { _patchDefaultOuterLevel = level; }
/** Get GL_PATCH_DEFAULT_INNER_LEVEL parameter.*/
const osg::Vec4& getPatchDefaultOuterLevel() const { return _patchDefaultOuterLevel; }
virtual void apply(State& state) const;
protected:
virtual ~PatchParameter();
GLint _vertices;
osg::Vec2 _patchDefaultInnerLevel;
osg::Vec4 _patchDefaultOuterLevel;
};
}
#endif

View File

@@ -99,9 +99,6 @@ class OSG_EXPORT Program : public osg::StateAttribute
void setParameter( GLenum pname, GLint value );
GLint getParameter( GLenum pname ) const;
void setParameterfv( GLenum pname, const GLfloat* value );
const GLfloat* getParameterfv( GLenum pname ) const;
/** Set/get compute shader work groups */
void setComputeGroups( GLint numGroupsX, GLint numGroupsY, GLint numGroupsZ );
void getComputeGroups( GLint& numGroupsX, GLint& numGroupsY, GLint& numGroupsZ ) const;
@@ -385,13 +382,6 @@ class OSG_EXPORT Program : public osg::StateAttribute
GLint _geometryInputType;
GLint _geometryOutputType;
/** Parameter maintained with glPatchParameteri */
GLint _patchVertices;
/** Parameter maintained with glPatchParameterfv */
// todo add tessellation default level
//GLfloat _patchDefaultInnerLevel[2];
//GLfloat _patchDefaultOuterLevel[4];
/** Parameter maintained with glDispatchCompute */
GLint _numGroupsX;

View File

@@ -190,7 +190,9 @@ class OSG_EXPORT StateAttribute : public Object
UNIFORMBUFFERBINDING,
TRANSFORMFEEDBACKBUFFERBINDING,
ATOMICCOUNTERBUFFERBINDING
ATOMICCOUNTERBUFFERBINDING,
PATCH_PARAMETER
};
/** Simple pairing between an attribute type and the member within that attribute type group.*/

View File

@@ -117,6 +117,7 @@ SET(TARGET_H
${HEADER_PATH}/OccluderNode
${HEADER_PATH}/OcclusionQueryNode
${HEADER_PATH}/OperationThread
${HEADER_PATH}/PatchParameter
${HEADER_PATH}/PagedLOD
${HEADER_PATH}/Plane
${HEADER_PATH}/Point
@@ -301,6 +302,7 @@ SET(TARGET_SRC
OccluderNode.cpp
OcclusionQueryNode.cpp
OperationThread.cpp
PatchParameter.cpp
PagedLOD.cpp
Point.cpp
PointSprite.cpp

View File

@@ -0,0 +1,42 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
*/
#include <osg/PatchParameter>
#include <osg/State>
#include <osg/Notify>
#include <osg/io_utils>
using namespace osg;
PatchParameter::PatchParameter(GLint vertices):
_vertices(vertices),
_patchDefaultInnerLevel(1.0f,1.0f),
_patchDefaultOuterLevel(1.0f,1.0f,1.0f,1.0f)
{
}
PatchParameter::~PatchParameter()
{
}
void PatchParameter::apply(State& state) const
{
GL2Extensions* extensions = GL2Extensions::Get( state.getContextID(), true );
if (extensions->areTessellationShadersSupported() )
{
extensions->glPatchParameteri( GL_PATCH_VERTICES, _vertices );
extensions->glPatchParameterfv( GL_PATCH_DEFAULT_INNER_LEVEL, _patchDefaultInnerLevel.ptr() );
extensions->glPatchParameterfv( GL_PATCH_DEFAULT_OUTER_LEVEL, _patchDefaultOuterLevel.ptr() );
}
}

View File

@@ -137,7 +137,6 @@ void Program::ProgramBinary::assign(unsigned int size, const unsigned char* data
Program::Program() :
_geometryVerticesOut(1), _geometryInputType(GL_TRIANGLES),
_geometryOutputType(GL_TRIANGLE_STRIP),
_patchVertices(3),
_numGroupsX(0), _numGroupsY(0), _numGroupsZ(0)
{
}
@@ -167,8 +166,6 @@ Program::Program(const Program& rhs, const osg::CopyOp& copyop):
_geometryInputType = rhs._geometryInputType;
_geometryOutputType = rhs._geometryOutputType;
_patchVertices = rhs._patchVertices;
_numGroupsX = rhs._numGroupsX;
_numGroupsY = rhs._numGroupsY;
_numGroupsZ = rhs._numGroupsZ;
@@ -206,9 +203,6 @@ int Program::compare(const osg::StateAttribute& sa) const
if( _geometryOutputType < rhs._geometryOutputType ) return -1;
if( rhs._geometryOutputType < _geometryOutputType ) return 1;
if( _patchVertices < rhs._patchVertices ) return -1;
if( rhs._patchVertices < _patchVertices ) return 1;
if( _numGroupsX < rhs._numGroupsX ) return -1;
if( rhs._numGroupsX < _numGroupsX ) return 1;
@@ -360,44 +354,14 @@ void Program::setParameter( GLenum pname, GLint value )
//dirtyProgram(); // needed?
break;
case GL_PATCH_VERTICES:
_patchVertices = value;
dirtyProgram();
OSG_WARN << "Program::setParameter invalid param " << GL_PATCH_VERTICES << ", use osg::PatchParameter when setting GL_PATCH_VERTICES."<<std::endl;
break;
default:
OSG_WARN << "setParameter invalid param " << pname << std::endl;
OSG_WARN << "Program::setParameter invalid param " << pname << std::endl;
break;
}
}
void Program::setParameterfv( GLenum pname, const GLfloat* /*value*/ )
{
switch( pname )
{
// todo tessellation default level
case GL_PATCH_DEFAULT_INNER_LEVEL:
break;
case GL_PATCH_DEFAULT_OUTER_LEVEL:
break;
default:
OSG_WARN << "setParameter invalid param " << pname << std::endl;
break;
}
}
const GLfloat* Program::getParameterfv( GLenum pname ) const
{
/*switch( pname )
{
;
// todo tessellation default level
// case GL_PATCH_DEFAULT_INNER_LEVEL: return _patchDefaultInnerLevel;
// case GL_PATCH_DEFAULT_OUTER_LEVEL: return _patchDefaultOuterLevel;
}*/
OSG_WARN << "getParameter invalid param " << pname << std::endl;
return 0;
}
GLint Program::getParameter( GLenum pname ) const
{
switch( pname )
@@ -405,7 +369,6 @@ GLint Program::getParameter( GLenum pname ) const
case GL_GEOMETRY_VERTICES_OUT_EXT: return _geometryVerticesOut;
case GL_GEOMETRY_INPUT_TYPE_EXT: return _geometryInputType;
case GL_GEOMETRY_OUTPUT_TYPE_EXT: return _geometryOutputType;
case GL_PATCH_VERTICES: return _patchVertices;
}
OSG_WARN << "getParameter invalid param " << pname << std::endl;
return 0;
@@ -604,13 +567,7 @@ void Program::PerContextProgram::linkProgram(osg::State& state)
_extensions->glProgramParameteri( _glProgramHandle, GL_GEOMETRY_INPUT_TYPE_EXT, _program->_geometryInputType );
_extensions->glProgramParameteri( _glProgramHandle, GL_GEOMETRY_OUTPUT_TYPE_EXT, _program->_geometryOutputType );
}
if (_extensions->areTessellationShadersSupported() )
{
_extensions->glPatchParameteri( GL_PATCH_VERTICES, _program->_patchVertices );
// todo: add default tessellation level
}
// Detach removed shaders
for( unsigned int i=0; i < _shadersToDetach.size(); ++i )
{

View File

@@ -0,0 +1,14 @@
#include <osg/PatchParameter>
#include <osgDB/ObjectWrapper>
#include <osgDB/InputStream>
#include <osgDB/OutputStream>
REGISTER_OBJECT_WRAPPER( PatchParameter,
new osg::PatchParameter,
osg::PatchParameter,
"osg::Object osg::StateAttribute osg::PatchParameter" )
{
ADD_INT_SERIALIZER( Vertices, 3);
ADD_VEC2_SERIALIZER( PatchDefaultInnerLevel, osg::Vec2(1.0f,1.0f));
ADD_VEC4_SERIALIZER( PatchDefaultOuterLevel, osg::Vec4(1.0f,1.0f,1.0f,1.0f));
}