// Copyright (C) 2012-2017 ChaosForge Ltd // http://chaosforge.org/ // // This file is part of Nova libraries. // For conditions of distribution and use, see copying.txt file in root folder. #include "nv/gl/gl_device.hh" #include "nv/gl/gl_window.hh" #include "nv/core/logging.hh" #include "nv/lib/sdl_image.hh" #include "nv/gl/gl_enum.hh" #include "nv/lib/gl.hh" using namespace nv; gl_device::gl_device() { m_shader_header.append( "#version 330 core\n" ); for ( auto& i : get_uniform_factory() ) m_shader_header.append( "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ i.first +";\n" ); for ( auto& i : get_link_uniform_factory() ) m_shader_header.append( "uniform sampler2D "+i.first +";\n" ); } // this is a temporary function that will be removed once we find a way to // pass binary file data around image_data* gl_device::create_image_data( string_view filename ) { load_sdl_image_library(); SDL_Surface* image = IMG_Load( filename.data() ); if (!image) { NV_LOG_ERROR( "Image file ", filename, " not found!" ); return nullptr; } // TODO: BGR vs RGB, single channel pixel_format pformat = RGBA8; switch ( image->format->BytesPerPixel ) { case 4: pformat = RGBA8; break; case 3: pformat = RGB8; break; case 1: pformat = R8; break; default: NV_ASSERT( false, "BytesPerPixel != 4,3 or 1!" ); } image_data* data = new image_data( pformat, ivec2( image->w, image->h ), static_cast( image->pixels ) ); return data; } // this is a temporary function that will be removed once we find a way to // pass binary file data around image_data* gl_device::create_image_data( const uint8* data, uint32 size ) { load_sdl_image_library(); SDL_Surface* image = IMG_LoadTyped_RW( SDL_RWFromMem( const_cast( data ), int( size ) ), 1, "png" ); if ( !image ) { NV_LOG_ERROR( "Image binary data cannot be loaded found!" ); return nullptr; } // TODO: BGR vs RGB, single channel NV_ASSERT( image->format->BytesPerPixel > 2, "bytes per pixel > 2!" ); pixel_format format( image->format->BytesPerPixel == 3 ? RGB8 : RGBA8 ); image_data* idata = new image_data( format, ivec2( image->w, image->h ), static_cast( image->pixels ) ); return idata; } gl_device::~gl_device() { for ( auto& t : m_textures ) release( &t ); for ( auto& b : m_buffers ) release( &b ); for ( auto& p : m_programs ) release( &p ); for ( auto& s : m_shaders ) release( &s ); } nv::shader nv::gl_device::create_shader( shader_type type, string_view sh_source ) { uint32 glid = 0; if ( !compile( shader_type_to_enum( type ), sh_source, glid ) ) return shader(); shader result = m_shaders.create(); gl_shader_info* info = m_shaders.get( result ); info->type = type; info->glid = glid; info->ref = 0; return result; } nv::program nv::gl_device::create_program( shader vs, shader fs ) { program result = m_programs.create(); gl_program_info* info = m_programs.get( result ); info->m_attribute_map = new attribute_map; info->m_engine_uniforms = new engine_uniform_list; info->m_uniform_map = new uniform_map; info->glid = glCreateProgram(); info->validated = false; compile( result, vs, fs ); prepare_program( result ); return result; } nv::texture nv::gl_device::create_texture( texture_type type, pixel_format format ) { unsigned glid = 0; glGenTextures( 1, &glid ); texture result = m_textures.create(); gl_texture_info* info = m_textures.get( result ); info->type = type; info->format = format; info->tsampler = sampler(); info->size = ivec3( 1, 1, 1 ); info->glid = glid; return result; } void nv::gl_device::release( texture t ) { if ( auto info = m_textures.get( t ) ) { release( info ); m_textures.destroy( t ); } } void nv::gl_device::release( gl_shader_info* s ) { if ( s && s->ref == 0 && s->glid != 0 ) glDeleteShader( s->glid ); } void nv::gl_device::release( gl_program_info* p ) { if ( p && p->glid != 0 ) { for ( auto& i : *p->m_uniform_map ) delete i.second; gl_shader_info* vi = m_shaders.get( p->vertex ); gl_shader_info* fi = m_shaders.get( p->fragment ); if ( vi ) { glDetachShader( p->glid, vi->glid ); vi->ref--; release( vi ); } if ( fi ) { glDetachShader( p->glid, fi->glid ); fi->ref--; release( fi ); } glDeleteProgram( p->glid ); delete p->m_attribute_map; delete p->m_engine_uniforms; delete p->m_uniform_map; } } void nv::gl_device::release( gl_texture_info* t ) { if ( t && t->glid != 0 ) glDeleteTextures( 1, &( t->glid ) ); } void nv::gl_device::release( gl_buffer_info* b ) { if ( b && b->glid != 0 ) glDeleteBuffers( 1, &( b->glid ) ); } void nv::gl_device::release( buffer b ) { if ( auto info = m_buffers.get( b ) ) { release( info ); m_buffers.destroy( b ); } } const texture_info* nv::gl_device::get_texture_info( texture t ) const { return m_textures.get( t ); } nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint ) { unsigned glid = 0; glGenBuffers( 1, &glid ); buffer result = m_buffers.create(); gl_buffer_info* info = m_buffers.get( result ); info->type = type; info->hint = hint; info->size = 0; info->glid = glid; return result; } void nv::gl_device::attach( program p, shader s ) { gl_program_info* pinfo = m_programs.get( p ); gl_shader_info* sinfo = m_shaders.get( s ); if ( sinfo && pinfo ) { glAttachShader( pinfo->glid, sinfo->glid ); sinfo->ref++; } } void nv::gl_device::detach( program p, shader s ) { gl_program_info* pinfo = m_programs.get( p ); gl_shader_info* sinfo = m_shaders.get( s ); if ( sinfo && pinfo ) { glDetachShader( pinfo->glid, sinfo->glid ); sinfo->ref--; } } const buffer_info* nv::gl_device::get_buffer_info( buffer t ) const { return m_buffers.get( t ); } void nv::gl_device::release( program p ) { if ( auto info = m_programs.get( p ) ) { release( info ); m_programs.destroy( p ); } } void nv::gl_device::release( shader s ) { if ( auto info = m_shaders.get( s ) ) { release( info ); m_shaders.destroy( s ); } } nv::gl_texture_info* nv::gl_device::get_full_texture_info( texture t ) { return m_textures.get( t ); } nv::gl_buffer_info* nv::gl_device::get_full_buffer_info( buffer t ) { return m_buffers.get( t ); } void nv::gl_device::prepare_program( program p ) { gl_program_info* info = m_programs.get( p ); if ( info ) { auto& map = get_uniform_factory(); auto& lmap = get_link_uniform_factory(); for ( auto& i : *info->m_uniform_map ) { auto j = lmap.find( i.first ); if ( j != lmap.end() ) { j->second->set( i.second ); } auto k = map.find( i.first ); if ( k != map.end() ) { info->m_engine_uniforms->push_back( k->second->create( i.second ) ); } } } } uniform_base* nv::gl_device::get_uniform( program p, const string_view& name, bool fatal /*= true */ ) const { const gl_program_info* info = m_programs.get( p ); { nv::uniform_map::const_iterator i = info->m_uniform_map->find( name ); if ( i != info->m_uniform_map->end() ) { return i->second; } if ( fatal ) { NV_LOG_CRITICAL( "gl_device : uniform '", name, "' not found in program!" ); NV_ABORT( "gl_device : uniform not found!" ); } } return nullptr; } int nv::gl_device::get_attribute_location( program p, const string_view& name, bool fatal /*= true */ ) const { const gl_program_info* info = m_programs.get( p ); if ( info ) { attribute_map::const_iterator i = info->m_attribute_map->find( name ); if ( i != info->m_attribute_map->end() ) { return i->second.location; } if ( fatal ) { NV_LOG_CRITICAL( "gl_device : attribute '", name, "' not found in program!" ); NV_ABORT( "gl_device : attribute not found!" ); } } return -1; } bool nv::gl_device::bind_block( program p, const string_view& name, uint32 index ) { const gl_program_info* info = m_programs.get( p ); if ( info ) { int id = get_block_location( p, name, false ); if ( id < 0 ) return false; glUniformBlockBinding( info->glid, unsigned( id ), index ); return true; } return false; } int nv::gl_device::get_block_location( program p, const string_view& name, bool fatal /*= true */ ) const { const gl_program_info* info = m_programs.get( p ); if ( info ) { GLuint result = glGetUniformBlockIndex( info->glid, name.data() ); if ( result != GL_INVALID_INDEX ) return static_cast( result ); if ( fatal ) { NV_LOG_CRITICAL( "gl_device : block '", name, "' not found in program!" ); NV_ABORT( "gl_device : block not found!" ); } } return -1; } bool nv::gl_device::compile( program pp, shader vp, shader fp ) { gl_program_info* p = m_programs.get( pp ); p->vertex = vp; p->fragment = fp; glBindAttribLocation( p->glid, static_cast( slot::POSITION ), "nv_position" ); glBindAttribLocation( p->glid, static_cast( slot::TEXCOORD ), "nv_texcoord" ); glBindAttribLocation( p->glid, static_cast( slot::NORMAL ), "nv_normal" ); glBindAttribLocation( p->glid, static_cast( slot::COLOR ), "nv_color" ); glBindAttribLocation( p->glid, static_cast( slot::TANGENT ), "nv_tangent" ); glBindAttribLocation( p->glid, static_cast( slot::BONEINDEX ), "nv_boneindex" ); glBindAttribLocation( p->glid, static_cast( slot::BONEWEIGHT ), "nv_boneweight"); attach( pp, p->vertex ); attach( pp, p->fragment ); glLinkProgram( p->glid ); const uint32 buffer_size = 2048; char buffer[ buffer_size ] = { 0 }; int length; int status; glGetProgramiv( p->glid, GL_LINK_STATUS, &status ); glGetProgramInfoLog( p->glid, buffer_size, &length, buffer ); NV_LOG_INFO( "Program #", p->glid, (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") ); if ( length > 0 ) { NV_LOG_INFO( "Program #", p->glid, " log: ", string_view( buffer, size_t( length ) ) ); } if ( status == GL_FALSE ) { return false; } load_attributes( p ); load_uniforms( p ); return true; } void nv::gl_device::update_uniforms( gl_program_info* p ) { for ( auto& i : *p->m_uniform_map ) { uniform_base* ubase = i.second; if ( ubase->is_dirty() ) { GLint uloc = ubase->get_location(); GLsizei size = static_cast( ubase->get_length() ); switch( ubase->get_type() ) { case FLOAT : glUniform1fv( uloc, size, static_cast< uniform< enum_to_type< FLOAT >::type >*>( ubase )->get_value() ); break; case INT : glUniform1iv( uloc, size, static_cast< uniform< enum_to_type< INT >::type >*>( ubase )->get_value() ); break; case FLOAT_VECTOR_2 : glUniform2fv( uloc, size, reinterpret_cast( static_cast< uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*>( ubase )->get_value())); break; case FLOAT_VECTOR_3 : glUniform3fv( uloc, size, reinterpret_cast( static_cast< uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*>( ubase )->get_value())); break; case FLOAT_VECTOR_4 : glUniform4fv( uloc, size, reinterpret_cast( static_cast< uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*>( ubase )->get_value())); break; case INT_VECTOR_2 : glUniform2iv( uloc, size, reinterpret_cast( static_cast< uniform< enum_to_type< INT_VECTOR_2 >::type >*>( ubase )->get_value())); break; case INT_VECTOR_3 : glUniform3iv( uloc, size, reinterpret_cast( static_cast< uniform< enum_to_type< INT_VECTOR_3 >::type >*>( ubase )->get_value())); break; case INT_VECTOR_4 : glUniform4iv( uloc, size, reinterpret_cast( static_cast< uniform< enum_to_type< INT_VECTOR_4 >::type >*>( ubase )->get_value())); break; case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, size, GL_FALSE, reinterpret_cast( static_cast< uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*>( ubase )->get_value())); break; case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, size, GL_FALSE, reinterpret_cast( static_cast< uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*>( ubase )->get_value())); break; case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, size, GL_FALSE, reinterpret_cast( static_cast< uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*>( ubase )->get_value())); break; default : break; // error? } ubase->clean(); } } } void nv::gl_device::load_attributes( gl_program_info* p ) { int params; glGetProgramiv( p->glid, GL_ACTIVE_ATTRIBUTES, ¶ms ); for ( unsigned i = 0; i < unsigned( params ); ++i ) { int attr_nlen; int attr_len; unsigned attr_type; char name_buffer[128]; glGetActiveAttrib( p->glid, i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer ); string_view name( name_buffer, size_t( attr_nlen ) ); // skip built-ins if ( name.substr(0,3) == "gl_" ) continue; int attr_loc = glGetAttribLocation( p->glid, name.data() ); attribute& attr = (*p->m_attribute_map)[ name ]; attr.location = attr_loc; attr.type = gl_enum_to_datatype( attr_type ); attr.length = attr_len; } } void nv::gl_device::load_uniforms( gl_program_info* p ) { int params; int bparams; glGetProgramiv( p->glid, GL_ACTIVE_UNIFORMS, ¶ms ); glGetProgramiv( p->glid, GL_ACTIVE_UNIFORM_BLOCKS, &bparams ); for ( unsigned i = 0; i < unsigned( params ); ++i ) { int uni_nlen; int uni_len; unsigned uni_type; char name_buffer[128]; glGetActiveUniform( p->glid, i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer ); string_view name( name_buffer, uint32( uni_nlen ) ); // skip built-ins if ( name.substr(0,3) == "gl_" ) continue; int uni_loc = glGetUniformLocation( p->glid, name.data() ); datatype utype = gl_enum_to_datatype( uni_type ); // check for array uint32 arrchar = name.find( '[' ); if ( arrchar != string_view::npos ) { name = name.substr( 0, arrchar ); } uniform_base* u = uniform_base::create( utype, uni_loc, size_t( uni_len ) ); NV_ASSERT( u, "Unknown uniform type!" ); (*p->m_uniform_map)[ name ] = u; //NV_LOG_DEBUG( "Uniform : ", name, " - ", utype, "/", uni_len ); } for ( unsigned i = 0; i < unsigned( bparams ); ++i ) { int uni_len; char name_buffer[128]; glGetActiveUniformBlockName( p->glid, i, 128, &uni_len, name_buffer ); NV_LOG_INFO( string_view( name_buffer, size_t( uni_len ) ) ); } } bool nv::gl_device::compile( uint32 sh_type, string_view shader_code, unsigned& glid ) { glid = glCreateShader( sh_type ); const char* pc = shader_code.data(); int l = int( shader_code.length() ); glShaderSource( glid, 1, &pc, &l ); glCompileShader( glid ); const uint32 buffer_size = 1024; char buffer[ buffer_size ] = { 0 }; int length; int compile_ok = GL_FALSE; glGetShaderiv(glid, GL_COMPILE_STATUS, &compile_ok); glGetShaderInfoLog( glid, buffer_size, &length, buffer ); if ( length > 0 ) { if ( compile_ok == 0 ) { NV_LOG_ERROR( "Shader #", glid, " error: ", string_view( buffer, size_t( length ) ) ); } else { NV_LOG_INFO( "Shader #", glid, " compiled successfully: ", string_view( buffer, size_t( length ) ) ); } } else { NV_LOG_INFO( "Shader #", glid, " compiled successfully." ); } return compile_ok != 0; }