diff --git a/examples/osgtessellationshaders/osgtessellationshaders.cpp b/examples/osgtessellationshaders/osgtessellationshaders.cpp index d0670ba0fdb..9ca266f98df 100644 --- a/examples/osgtessellationshaders/osgtessellationshaders.cpp +++ b/examples/osgtessellationshaders/osgtessellationshaders.cpp @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -175,7 +176,6 @@ osg::ref_ptr 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 @@ class KeyboardEventHandler : public osgGA::GUIEventHandler { 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 diff --git a/include/osg/PatchParameter b/include/osg/PatchParameter new file mode 100644 index 00000000000..8e9d4712f57 --- /dev/null +++ b/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 +#include +#include +#include + +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 diff --git a/include/osg/Program b/include/osg/Program index d31fbc8b1e7..4d5991ac6b9 100644 --- a/include/osg/Program +++ b/include/osg/Program @@ -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; diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index e8c155d42ed..977561f4ccc 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -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.*/ diff --git a/src/osg/CMakeLists.txt b/src/osg/CMakeLists.txt index 94db33d2299..7a7e6ce13c4 100644 --- a/src/osg/CMakeLists.txt +++ b/src/osg/CMakeLists.txt @@ -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 diff --git a/src/osg/PatchParameter.cpp b/src/osg/PatchParameter.cpp new file mode 100644 index 00000000000..8955960e05b --- /dev/null +++ b/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 +#include +#include +#include + +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() ); + } +} diff --git a/src/osg/Program.cpp b/src/osg/Program.cpp index 612c34ad61d..012ee153096 100644 --- a/src/osg/Program.cpp +++ b/src/osg/Program.cpp @@ -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(); - 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: + OSG_WARN << "Program::setParameter invalid param " << GL_PATCH_VERTICES << ", use osg::PatchParameter when setting GL_PATCH_VERTICES."<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 ) { diff --git a/src/osgWrappers/serializers/osg/PatchParameter.cpp b/src/osgWrappers/serializers/osg/PatchParameter.cpp new file mode 100644 index 00000000000..77ace10e029 --- /dev/null +++ b/src/osgWrappers/serializers/osg/PatchParameter.cpp @@ -0,0 +1,14 @@ +#include +#include +#include +#include + +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)); +}