Skip to content

Commit

Permalink
Introduced new osg::PatchParameter StateAttribute class to wrap up gl…
Browse files Browse the repository at this point in the history
…PatchParameter associated state.

Note, osg::Program::setParameter(GL_PATCH_VERTICES,num); is nolonger support and should be replaced by using the new PatchParameter class.
  • Loading branch information
robertosfield committed Jun 11, 2013
1 parent 15ab621 commit eb09273
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 58 deletions.
4 changes: 3 additions & 1 deletion examples/osgtessellationshaders/osgtessellationshaders.cpp
Expand Up @@ -4,6 +4,7 @@
*/ */


#include <osg/Program> #include <osg/Program>
#include <osg/PatchParameter>
#include <osg/ShapeDrawable> #include <osg/ShapeDrawable>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
Expand Down Expand Up @@ -175,7 +176,6 @@ osg::ref_ptr<osg::Program> createProgram(){
program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 3); program->setParameter(GL_GEOMETRY_VERTICES_OUT_EXT, 3);
program->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES); program->setParameter(GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLES);
program->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP); program->setParameter(GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP);
program->setParameter(GL_PATCH_VERTICES,3);
return program; return program;
} }


Expand Down Expand Up @@ -213,6 +213,7 @@ class KeyboardEventHandler : public osgGA::GUIEventHandler {
return osgGA::GUIEventHandler::handle(ea,gaa); return osgGA::GUIEventHandler::handle(ea,gaa);
} }
}; };

int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
osgViewer::Viewer viewer; osgViewer::Viewer viewer;
Expand All @@ -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(new osg::Uniform("LightPosition",osg::Vec3(0.25f, 0.25f, 1.0f)));
state->addUniform(tessInnerU.get()); state->addUniform(tessInnerU.get());
state->addUniform(tessOuterU.get()); state->addUniform(tessOuterU.get());
state->setAttribute(new osg::PatchParameter(3));
state->setAttribute(program.get()); state->setAttribute(program.get());


// switch on the uniforms that track the modelview and projection matrices // switch on the uniforms that track the modelview and projection matrices
Expand Down
88 changes: 88 additions & 0 deletions include/osg/PatchParameter
@@ -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
10 changes: 0 additions & 10 deletions include/osg/Program
Expand Up @@ -99,9 +99,6 @@ class OSG_EXPORT Program : public osg::StateAttribute
void setParameter( GLenum pname, GLint value ); void setParameter( GLenum pname, GLint value );
GLint getParameter( GLenum pname ) const; GLint getParameter( GLenum pname ) const;


void setParameterfv( GLenum pname, const GLfloat* value );
const GLfloat* getParameterfv( GLenum pname ) const;

/** Set/get compute shader work groups */ /** Set/get compute shader work groups */
void setComputeGroups( GLint numGroupsX, GLint numGroupsY, GLint numGroupsZ ); void setComputeGroups( GLint numGroupsX, GLint numGroupsY, GLint numGroupsZ );
void getComputeGroups( GLint& numGroupsX, GLint& numGroupsY, GLint& numGroupsZ ) const; void getComputeGroups( GLint& numGroupsX, GLint& numGroupsY, GLint& numGroupsZ ) const;
Expand Down Expand Up @@ -385,13 +382,6 @@ class OSG_EXPORT Program : public osg::StateAttribute
GLint _geometryInputType; GLint _geometryInputType;
GLint _geometryOutputType; 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 */ /** Parameter maintained with glDispatchCompute */
GLint _numGroupsX; GLint _numGroupsX;
Expand Down
4 changes: 3 additions & 1 deletion include/osg/StateAttribute
Expand Up @@ -190,7 +190,9 @@ class OSG_EXPORT StateAttribute : public Object
UNIFORMBUFFERBINDING, UNIFORMBUFFERBINDING,
TRANSFORMFEEDBACKBUFFERBINDING, TRANSFORMFEEDBACKBUFFERBINDING,


ATOMICCOUNTERBUFFERBINDING ATOMICCOUNTERBUFFERBINDING,

PATCH_PARAMETER
}; };


/** Simple pairing between an attribute type and the member within that attribute type group.*/ /** Simple pairing between an attribute type and the member within that attribute type group.*/
Expand Down
2 changes: 2 additions & 0 deletions src/osg/CMakeLists.txt
Expand Up @@ -117,6 +117,7 @@ SET(TARGET_H
${HEADER_PATH}/OccluderNode ${HEADER_PATH}/OccluderNode
${HEADER_PATH}/OcclusionQueryNode ${HEADER_PATH}/OcclusionQueryNode
${HEADER_PATH}/OperationThread ${HEADER_PATH}/OperationThread
${HEADER_PATH}/PatchParameter
${HEADER_PATH}/PagedLOD ${HEADER_PATH}/PagedLOD
${HEADER_PATH}/Plane ${HEADER_PATH}/Plane
${HEADER_PATH}/Point ${HEADER_PATH}/Point
Expand Down Expand Up @@ -301,6 +302,7 @@ SET(TARGET_SRC
OccluderNode.cpp OccluderNode.cpp
OcclusionQueryNode.cpp OcclusionQueryNode.cpp
OperationThread.cpp OperationThread.cpp
PatchParameter.cpp
PagedLOD.cpp PagedLOD.cpp
Point.cpp Point.cpp
PointSprite.cpp PointSprite.cpp
Expand Down
42 changes: 42 additions & 0 deletions src/osg/PatchParameter.cpp
@@ -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() );
}
}
49 changes: 3 additions & 46 deletions src/osg/Program.cpp
Expand Up @@ -137,7 +137,6 @@ void Program::ProgramBinary::assign(unsigned int size, const unsigned char* data
Program::Program() : Program::Program() :
_geometryVerticesOut(1), _geometryInputType(GL_TRIANGLES), _geometryVerticesOut(1), _geometryInputType(GL_TRIANGLES),
_geometryOutputType(GL_TRIANGLE_STRIP), _geometryOutputType(GL_TRIANGLE_STRIP),
_patchVertices(3),
_numGroupsX(0), _numGroupsY(0), _numGroupsZ(0) _numGroupsX(0), _numGroupsY(0), _numGroupsZ(0)
{ {
} }
Expand Down Expand Up @@ -167,8 +166,6 @@ Program::Program(const Program& rhs, const osg::CopyOp& copyop):
_geometryInputType = rhs._geometryInputType; _geometryInputType = rhs._geometryInputType;
_geometryOutputType = rhs._geometryOutputType; _geometryOutputType = rhs._geometryOutputType;


_patchVertices = rhs._patchVertices;

_numGroupsX = rhs._numGroupsX; _numGroupsX = rhs._numGroupsX;
_numGroupsY = rhs._numGroupsY; _numGroupsY = rhs._numGroupsY;
_numGroupsZ = rhs._numGroupsZ; _numGroupsZ = rhs._numGroupsZ;
Expand Down Expand Up @@ -206,9 +203,6 @@ int Program::compare(const osg::StateAttribute& sa) const
if( _geometryOutputType < rhs._geometryOutputType ) return -1; if( _geometryOutputType < rhs._geometryOutputType ) return -1;
if( rhs._geometryOutputType < _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( _numGroupsX < rhs._numGroupsX ) return -1;
if( rhs._numGroupsX < _numGroupsX ) return 1; if( rhs._numGroupsX < _numGroupsX ) return 1;


Expand Down Expand Up @@ -360,52 +354,21 @@ void Program::setParameter( GLenum pname, GLint value )
//dirtyProgram(); // needed? //dirtyProgram(); // needed?
break; break;
case GL_PATCH_VERTICES: case GL_PATCH_VERTICES:
_patchVertices = value; OSG_WARN << "Program::setParameter invalid param " << GL_PATCH_VERTICES << ", use osg::PatchParameter when setting GL_PATCH_VERTICES."<<std::endl;
dirtyProgram();
break;
default:
OSG_WARN << "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; break;
default: default:
OSG_WARN << "setParameter invalid param " << pname << std::endl; OSG_WARN << "Program::setParameter invalid param " << pname << std::endl;
break; 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 GLint Program::getParameter( GLenum pname ) const
{ {
switch( pname ) switch( pname )
{ {
case GL_GEOMETRY_VERTICES_OUT_EXT: return _geometryVerticesOut; case GL_GEOMETRY_VERTICES_OUT_EXT: return _geometryVerticesOut;
case GL_GEOMETRY_INPUT_TYPE_EXT: return _geometryInputType; case GL_GEOMETRY_INPUT_TYPE_EXT: return _geometryInputType;
case GL_GEOMETRY_OUTPUT_TYPE_EXT: return _geometryOutputType; case GL_GEOMETRY_OUTPUT_TYPE_EXT: return _geometryOutputType;
case GL_PATCH_VERTICES: return _patchVertices;
} }
OSG_WARN << "getParameter invalid param " << pname << std::endl; OSG_WARN << "getParameter invalid param " << pname << std::endl;
return 0; return 0;
Expand Down Expand Up @@ -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_INPUT_TYPE_EXT, _program->_geometryInputType );
_extensions->glProgramParameteri( _glProgramHandle, GL_GEOMETRY_OUTPUT_TYPE_EXT, _program->_geometryOutputType ); _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 // Detach removed shaders
for( unsigned int i=0; i < _shadersToDetach.size(); ++i ) for( unsigned int i=0; i < _shadersToDetach.size(); ++i )
{ {
Expand Down
14 changes: 14 additions & 0 deletions src/osgWrappers/serializers/osg/PatchParameter.cpp
@@ -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));
}

0 comments on commit eb09273

Please sign in to comment.