Shader::Shader( GLenum type, std::string const& source ) { GLuint id = glCreateShader( type ); setGLId( id ); GLint length = dp::checked_cast<GLint>(source.length()); GLchar const* src = source.c_str(); glShaderSource( id, 1, &src, &length ); glCompileShader( id ); #if !defined( NDEBUG ) GLint result; glGetShaderiv( id, GL_COMPILE_STATUS, &result ); if ( ! result ) { GLint errorLen; glGetShaderiv( id, GL_INFO_LOG_LENGTH, &errorLen ); std::string buffer; buffer.resize( errorLen, 0 ); glGetShaderInfoLog( id, errorLen, NULL, &buffer[0] ); DP_ASSERT( false ); } #endif }
void Renderbuffer::init( int width, int height ) { GLuint id; glGenRenderbuffers( 1, &id ); setGLId( id ); resize( width, height ); }
void RenderBuffer::onCreate(const RenderBuffer *source) { if(GlobalSystemState == Startup) return; setGLId( Window::registerGLObject( boost::bind(&RenderBuffer::handleGL, RenderBufferMTUncountedPtr(this), _1, _2, _3, _4), &RenderBuffer::handleDestroyGL)); }
void GeoMultiPropertyData::onCreate(const GeoMultiPropertyData *) { if(GlobalSystemState == Startup) return; setGLId( Window::registerGLObject( boost::bind(&GeoMultiPropertyData::handleGL, GeoMultiPropertyDataMTUncountedPtr(this), _1, _2, _3, _4), &GeoMultiPropertyData::handleDestroyGL)); }
void FragmentProgramChunk::onCreate(const FragmentProgramChunk *chunk) { Inherited::onCreate(chunk); if(GlobalSystemState == Startup) return; setGLId(Window::registerGLObject( boost::bind(&FragmentProgramChunk::handleGL, FragmentProgramChunkMTUncountedPtr(this), _1, _2, _3, _4), &FragmentProgramChunk::handleDestroyGL)); }
void GeoVectorBufferProperty::onCreate(const GeoVectorBufferProperty *source) { Inherited::onCreate(source); if(GlobalSystemState == Startup) return; setGLId( Window::registerGLObject( boost::bind(&Self::handleGL, GeoPropertyMTUncountedPtr(this), _1, _2, _3, _4), &GeoProperty::handleDestroyGL)); }
void CubeTextureObjChunk::onCreate(const CubeTextureObjChunk *source) { // skip TextureObjChunk::onCreate TextureBaseChunk::onCreate(source); if(GlobalSystemState == Startup) return; setGLId( Window::registerGLObject( boost::bind(&CubeTextureObjChunk::handleGL, CubeTextureObjChunkMTUncountedPtr(this), _1, _2, _3, _4), &CubeTextureObjChunk::handleDestroyGL)); }
void ShaderExecutableChunk::onCreate(const ShaderExecutableChunk *source) { Inherited::onCreate(source); // ignore prototypes. if(GlobalSystemState == Startup) return; setGLId( Window::registerGLObject( boost::bind(&ShaderExecutableChunk::handleGL, ShaderExecutableChunkMTUncountedPtr(this), _1, _2, _3, _4), &ShaderExecutableChunk::handleDestroyGL)); _uiChunkId = _uiChunkCounter++; }
void ShaderProgram::onCreate(const ShaderProgram *source) { Inherited::onCreate(source); // ignore prototypes. if(GlobalSystemState == Startup) { if(_pProgIdPool == NULL) { _pProgIdPool = new ProgramIdPool; } return; } setGLId( Window::registerGLObject( boost::bind(&ShaderProgram::handleGL, ShaderProgramMTUncountedPtr(this), _1, _2, _3, _4), &ShaderProgram::handleDestroyGL)); _uiProgId = _pProgIdPool->create(); }
Program::Program( std::vector<SmartShader> const& shaders, Parameters const& parameters ) : m_activeAttributesCount(0) , m_activeAttributesMask(0) , m_shaders( shaders ) { #if !defined(NDEBUG) std::set<GLenum> types; for ( std::vector<SmartShader>::const_iterator it = m_shaders.begin() ; it != m_shaders.end() ; ++it ) { DP_ASSERT( *it && types.insert( (*it)->getType() ).second ); } #endif GLuint id = glCreateProgram(); setGLId( id ); DP_ASSERT( glIsProgram( id ) && "failed to create program" ); glProgramParameteri( id, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, parameters.m_binaryRetrievableHint ); glProgramParameteri( id, GL_PROGRAM_SEPARABLE, parameters.m_separable ); for ( std::vector<SmartShader>::const_iterator it = m_shaders.begin() ; it != m_shaders.end() ; ++it ) { glAttachShader( id, (*it)->getGLId() ); } glLinkProgram( id ); GLint result; glGetProgramiv( id, GL_LINK_STATUS, &result ); if ( !result ) { GLint errorLen; glGetProgramiv( id, GL_INFO_LOG_LENGTH, &errorLen ); std::string buffer; buffer.resize( errorLen, 0 ); glGetProgramInfoLog( id, errorLen, NULL, &buffer[0] ); std::ostringstream error; error << "failed to link program: " << std::endl << buffer << std::endl; throw std::runtime_error(error.str().c_str()); } GLint activeUniformMaxLength = 0; glGetProgramiv( id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &activeUniformMaxLength ); std::vector<char> name( activeUniformMaxLength ); GLint activeUniformsCount = 0; glGetProgramiv( id, GL_ACTIVE_UNIFORMS, &activeUniformsCount ); m_uniforms.resize( activeUniformsCount ); glUseProgram( id ); for ( GLint i=0 ; i<activeUniformsCount ; i++ ) { glGetActiveUniform( id, i, activeUniformMaxLength, nullptr, &m_uniforms[i].size, &m_uniforms[i].type, name.data() ); m_uniforms[i].name = name.data(); m_uniforms[i].location = glGetUniformLocation( id, m_uniforms[i].name.data() ); if ( isSamplerType( m_uniforms[i].type ) ) { DP_ASSERT( m_uniforms[i].size == 1 ); glUniform1i( m_uniforms[i].location, dp::util::checked_cast<GLint>(m_samplerUniforms.size() ) ); m_samplerUniforms.push_back( i ); } else if ( isImageType( m_uniforms[i].type ) ) { DP_ASSERT( m_uniforms[i].size == 1 ); glUniform1i( m_uniforms[i].location, dp::util::checked_cast<GLint>(m_imageUniforms.size()) ); m_imageUniforms.push_back( ImageData() ); m_imageUniforms.back().index = i; } } glUseProgram( 0 ); // build mask of active vertex attributes GLint activeAttributeMaxLength; glGetProgramiv( id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &activeAttributeMaxLength ); name.resize( activeAttributeMaxLength ); GLint activeAttributesCount; glGetProgramiv( id, GL_ACTIVE_ATTRIBUTES, &activeAttributesCount ); // the driver reports 1 attribute while activeAttributeMaxLength is 0. // since attributes always have names? something strange is going on. if ( activeAttributeMaxLength == 0 ) { //DP_ASSERT( false ); activeAttributesCount = 0; } m_activeAttributesCount = activeAttributesCount; for ( GLint i=0 ; i<activeAttributesCount ; i++ ) { GLint size; GLenum type; glGetActiveAttrib( id, i, activeAttributeMaxLength, nullptr, &size, &type, &name[0] ); m_activeAttributesMask |= 1 << glGetAttribLocation( id, &name[0] ); } }