From Wang Rui, "In the attached files I've added the Compute Shader support for OSG, as well as serializer updates and a new osgcomputeshaders example. My submission also include a setComputeGroups() function in Program for setting compute-shader work groups, and a bindToImageUnit() function in Texture for binding textures as image variables in shaders.

All code are tested on Windows 7 + NVIDIA GFX 570 with the latest GeForce 310.70 Driver (BETA), which could support OpenGL 4.3.

Compute shader information can be found at "http://www.opengl.org/registry/specs/ARB/compute_shader.txt"
"
This commit is contained in:
Robert Osfield
2013-01-25 11:54:03 +00:00
parent f6450a1123
commit 26a8f63212
16 changed files with 397 additions and 4 deletions

View File

@@ -214,6 +214,9 @@ GL2Extensions::GL2Extensions(const GL2Extensions& rhs) : osg::Referenced()
// ARB_shader_atomic_counters
_glGetActiveAtomicCounterBufferiv = rhs._glGetActiveAtomicCounterBufferiv;
// ARB_compute_shader
_glDispatchCompute = rhs._glDispatchCompute;
}
@@ -399,6 +402,9 @@ void GL2Extensions::lowestCommonDenominator(const GL2Extensions& rhs)
// ARB_shader_atomic_counters
if(!rhs._glGetActiveAtomicCounterBufferiv) _glGetActiveAtomicCounterBufferiv = 0;
// ARB_compute_shder
if(!rhs._glDispatchCompute) _glDispatchCompute = 0;
}
@@ -589,6 +595,9 @@ void GL2Extensions::setupGL2Extensions(unsigned int contextID)
// ARB_shader_atomic_counters
_glGetActiveAtomicCounterBufferiv= 0;
// ARB_compute_shader
_glDispatchCompute= 0;
return;
}
@@ -794,6 +803,9 @@ void GL2Extensions::setupGL2Extensions(unsigned int contextID)
// ARB_shader_atomic_counters
setGLExtensionFuncPtr(_glGetActiveAtomicCounterBufferiv, "glGetActiveAtomicCounterBufferiv" );
// ARB_compute_shader
setGLExtensionFuncPtr(_glDispatchCompute, "glDispatchCompute" );
}
@@ -2826,6 +2838,18 @@ void GL2Extensions::glGetActiveAtomicCounterBufferiv( GLuint program, GLuint buf
}
}
void GL2Extensions::glDispatchCompute( GLuint numGroupsX, GLuint numGroupsY, GLuint numGroupsZ ) const
{
if (_glDispatchCompute)
{
_glDispatchCompute( numGroupsX, numGroupsY, numGroupsZ );
}
else
{
NotSupported( "glDispatchCompute" );
}
}
///////////////////////////////////////////////////////////////////////////
// C++-friendly convenience methods

View File

@@ -137,7 +137,8 @@ void Program::ProgramBinary::assign(unsigned int size, const unsigned char* data
Program::Program() :
_geometryVerticesOut(1), _geometryInputType(GL_TRIANGLES),
_geometryOutputType(GL_TRIANGLE_STRIP),
_patchVertices(3)
_patchVertices(3),
_numGroupsX(0), _numGroupsY(0), _numGroupsZ(0)
{
}
@@ -167,6 +168,10 @@ Program::Program(const Program& rhs, const osg::CopyOp& copyop):
_geometryOutputType = rhs._geometryOutputType;
_patchVertices = rhs._patchVertices;
_numGroupsX = rhs._numGroupsX;
_numGroupsY = rhs._numGroupsY;
_numGroupsZ = rhs._numGroupsZ;
}
@@ -203,6 +208,15 @@ int Program::compare(const osg::StateAttribute& sa) const
if( _patchVertices < rhs._patchVertices ) return -1;
if( rhs._patchVertices < _patchVertices ) return 1;
if( _numGroupsX < rhs._numGroupsX ) return -1;
if( rhs._numGroupsX < _numGroupsX ) return 1;
if( _numGroupsY < rhs._numGroupsY ) return -1;
if( rhs._numGroupsY < _numGroupsY ) return 1;
if( _numGroupsZ < rhs._numGroupsZ ) return -1;
if( rhs._numGroupsZ < _numGroupsZ ) return 1;
ShaderList::const_iterator litr=_shaderList.begin();
ShaderList::const_iterator ritr=rhs._shaderList.begin();
@@ -397,6 +411,19 @@ GLint Program::getParameter( GLenum pname ) const
return 0;
}
void Program::setComputeGroups( GLint numGroupsX, GLint numGroupsY, GLint numGroupsZ )
{
_numGroupsX = numGroupsX;
_numGroupsY = numGroupsY;
_numGroupsZ = numGroupsZ;
}
void Program::getComputeGroups( GLint& numGroupsX, GLint& numGroupsY, GLint& numGroupsZ ) const
{
numGroupsX = _numGroupsX;
numGroupsY = _numGroupsY;
numGroupsZ = _numGroupsZ;
}
void Program::addBindAttribLocation( const std::string& name, GLuint index )
{
@@ -930,4 +957,8 @@ Program::ProgramBinary* Program::PerContextProgram::compileProgramBinary(osg::St
void Program::PerContextProgram::useProgram() const
{
_extensions->glUseProgram( _glProgramHandle );
if ( _program->_numGroupsX>0 && _program->_numGroupsY>0 && _program->_numGroupsZ>0 )
{
_extensions->glDispatchCompute( _program->_numGroupsX, _program->_numGroupsY, _program->_numGroupsZ );
}
}

View File

@@ -330,6 +330,7 @@ const char* Shader::getTypename() const
case TESSEVALUATION: return "TESSEVALUATION";
case GEOMETRY: return "GEOMETRY";
case FRAGMENT: return "FRAGMENT";
case COMPUTE: return "COMPUTE";
default: return "UNDEFINED";
}
}
@@ -342,6 +343,7 @@ Shader::Type Shader::getTypeId( const std::string& tname )
if( tname == "TESSEVALUATION") return TESSEVALUATION;
if( tname == "GEOMETRY" ) return GEOMETRY;
if( tname == "FRAGMENT" ) return FRAGMENT;
if( tname == "COMPUTE" ) return COMPUTE;
return UNDEFINED;
}

View File

@@ -52,6 +52,7 @@ osg::Program* ShaderComposer::getOrCreateProgram(const ShaderComponents& shaderC
Shaders tessEvaluationShaders;
Shaders geometryShaders;
Shaders fragmentShaders;
Shaders computeShaders;
OSG_NOTICE<<"ShaderComposer::getOrCreateProgram(shaderComponents.size()=="<<shaderComponents.size()<<std::endl;
@@ -81,6 +82,9 @@ osg::Program* ShaderComposer::getOrCreateProgram(const ShaderComponents& shaderC
case(Shader::FRAGMENT):
fragmentShaders.push_back(shader);
break;
case(Shader::COMPUTE):
computeShaders.push_back(shader);
break;
case(Shader::UNDEFINED):
OSG_WARN<<"Warning: ShaderCompose::getOrCreateProgam(ShaderComponts) encounterd invalid Shader::Type."<<std::endl;
break;
@@ -105,6 +109,11 @@ osg::Program* ShaderComposer::getOrCreateProgram(const ShaderComponents& shaderC
addShaderToProgram(program.get(), fragmentShaders);
}
if (!computeShaders.empty())
{
addShaderToProgram(program.get(), computeShaders);
}
// assign newly created program to map.
_programMap[shaderComponents] = program;

View File

@@ -1226,6 +1226,16 @@ void Texture::setMaxAnisotropy(float anis)
}
}
void Texture::bindToImageUnit(unsigned int unit, GLenum access, GLenum format, int level, bool layered, int layer)
{
_imageAttachment.unit = unit;
_imageAttachment.level = level;
_imageAttachment.layered = layered ? GL_TRUE : GL_FALSE;
_imageAttachment.layer = layer;
_imageAttachment.access = access;
_imageAttachment.format = format;
dirtyTextureParameters();
}
/** Force a recompile on next apply() of associated OpenGL texture objects.*/
void Texture::dirtyTextureObject()
@@ -1741,6 +1751,19 @@ void Texture::applyTexParameters(GLenum target, State& state) const
}
}
// Apply image load/store attributes
if (extensions->isBindImageTextureSupported() && _imageAttachment.access!=0)
{
TextureObject* tobj = getTextureObject(contextID);
if (tobj)
{
extensions->glBindImageTexture(
_imageAttachment.unit, tobj->id(), _imageAttachment.level,
_imageAttachment.layered, _imageAttachment.layer, _imageAttachment.access,
_imageAttachment.format!=0 ? _imageAttachment.format : _internalFormat);
}
}
getTextureParameterDirty(state.getContextID()) = false;
}
@@ -2590,6 +2613,8 @@ Texture::Extensions::Extensions(unsigned int contextID)
if (_glTexParameterIiv == NULL) setGLExtensionFuncPtr(_glTexParameterIiv, "glTexParameterIivEXT");
if (_glTexParameterIuiv == NULL) setGLExtensionFuncPtr(_glTexParameterIuiv, "glTexParameterIuivEXT");
setGLExtensionFuncPtr(_glBindImageTexture, "glBindImageTexture", "glBindImageTextureARB");
_isTextureMaxLevelSupported = ( getGLVersionNumber() >= 1.2f );
}