[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
| 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
| 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[38] | 6 |
|
---|
| 7 | #include "nv/gl/gl_device.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/gl/gl_window.hh"
|
---|
[319] | 10 | #include "nv/core/logging.hh"
|
---|
[90] | 11 | #include "nv/lib/sdl_image.hh"
|
---|
[301] | 12 | #include "nv/gl/gl_enum.hh"
|
---|
| 13 | #include "nv/lib/gl.hh"
|
---|
[38] | 14 |
|
---|
| 15 | using namespace nv;
|
---|
| 16 |
|
---|
| 17 | gl_device::gl_device()
|
---|
| 18 | {
|
---|
[491] | 19 | m_shader_header.append( "#version 330 core\n" );
|
---|
[316] | 20 | for ( auto& i : get_uniform_factory() )
|
---|
[438] | 21 | m_shader_header.append( "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ i.first +";\n" );
|
---|
[316] | 22 | for ( auto& i : get_link_uniform_factory() )
|
---|
[438] | 23 | m_shader_header.append( "uniform sampler2D "+i.first +";\n" );
|
---|
[38] | 24 | }
|
---|
| 25 |
|
---|
[399] | 26 | program gl_device::create_program( string_view vs_source, string_view fs_source )
|
---|
[38] | 27 | {
|
---|
[303] | 28 | program result = m_programs.create();
|
---|
| 29 | gl_program_info* info = m_programs.get( result );
|
---|
| 30 |
|
---|
[392] | 31 | info->m_attribute_map = new attribute_map;
|
---|
| 32 | info->m_engine_uniforms = new engine_uniform_list;
|
---|
| 33 | info->m_uniform_map = new uniform_map;
|
---|
| 34 |
|
---|
[303] | 35 | info->glid = glCreateProgram();
|
---|
| 36 | compile( info, vs_source, fs_source );
|
---|
| 37 | prepare_program( result );
|
---|
| 38 | return result;
|
---|
[38] | 39 | }
|
---|
| 40 |
|
---|
[90] | 41 | // this is a temporary function that will be removed once we find a way to
|
---|
| 42 | // pass binary file data around
|
---|
[399] | 43 | image_data* gl_device::create_image_data( string_view filename )
|
---|
[90] | 44 | {
|
---|
| 45 | load_sdl_image_library();
|
---|
[361] | 46 | SDL_Surface* image = IMG_Load( filename.data() );
|
---|
[120] | 47 | if (!image)
|
---|
| 48 | {
|
---|
[365] | 49 | NV_LOG_ERROR( "Image file ", filename, " not found!" );
|
---|
[120] | 50 | return nullptr;
|
---|
| 51 | }
|
---|
[292] | 52 | // TODO: BGR vs RGB, single channel
|
---|
[461] | 53 | pixel_format pformat = RGBA;
|
---|
| 54 | switch ( image->format->BytesPerPixel )
|
---|
| 55 | {
|
---|
| 56 | case 4: pformat = RGBA; break;
|
---|
| 57 | case 3: pformat = RGB; break;
|
---|
| 58 | case 1: pformat = RED; break;
|
---|
| 59 | default: NV_ASSERT( false, "BytesPerPixel != 4,3 or 1!" );
|
---|
| 60 | }
|
---|
| 61 | image_format format( pformat, UBYTE );
|
---|
[406] | 62 | image_data* data = new image_data( format, ivec2( image->w, image->h ), static_cast<nv::uint8*>( image->pixels ) );
|
---|
[90] | 63 | return data;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[350] | 66 | // this is a temporary function that will be removed once we find a way to
|
---|
| 67 | // pass binary file data around
|
---|
| 68 | image_data* gl_device::create_image_data( const uint8* data, uint32 size )
|
---|
| 69 | {
|
---|
| 70 | load_sdl_image_library();
|
---|
[406] | 71 | SDL_Surface* image = IMG_LoadTyped_RW( SDL_RWFromMem( const_cast<uint8*>( data ), int( size ) ), 1, "png" );
|
---|
[350] | 72 | if ( !image )
|
---|
| 73 | {
|
---|
[365] | 74 | NV_LOG_ERROR( "Image binary data cannot be loaded found!" );
|
---|
[350] | 75 | return nullptr;
|
---|
| 76 | }
|
---|
| 77 | // TODO: BGR vs RGB, single channel
|
---|
[471] | 78 | NV_ASSERT( image->format->BytesPerPixel > 2, "bytes per pixel > 2!" );
|
---|
[350] | 79 | image_format format( image->format->BytesPerPixel == 3 ? RGB : RGBA, UBYTE );
|
---|
[406] | 80 | image_data* idata = new image_data( format, ivec2( image->w, image->h ), static_cast<nv::uint8*>( image->pixels ) );
|
---|
[350] | 81 | return idata;
|
---|
| 82 | }
|
---|
[92] | 83 |
|
---|
[350] | 84 |
|
---|
[38] | 85 | gl_device::~gl_device()
|
---|
| 86 | {
|
---|
[302] | 87 | while ( m_textures.size() > 0 )
|
---|
| 88 | release( m_textures.get_handle(0) );
|
---|
| 89 | while ( m_buffers.size() > 0 )
|
---|
| 90 | release( m_buffers.get_handle(0) );
|
---|
[303] | 91 | while ( m_programs.size() > 0 )
|
---|
| 92 | release( m_programs.get_handle(0) );
|
---|
[38] | 93 | }
|
---|
[301] | 94 |
|
---|
[406] | 95 | nv::texture nv::gl_device::create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
[301] | 96 | {
|
---|
[491] | 97 | NV_ASSERT_ALWAYS( type != TEXTURE_1D_BUFFER && type != TEXTURE_3D && type != TEXTURE_2D_ARRAY, "2D texture type expected!" );
|
---|
[301] | 98 | unsigned glid = 0;
|
---|
[331] | 99 | unsigned gl_type = texture_type_to_enum( type );
|
---|
[492] | 100 | GLenum gl_internal = GLenum( nv::image_format_to_internal_enum( aformat.format ) );
|
---|
[491] | 101 | unsigned gl_enum = nv::image_format_to_enum( aformat.format );
|
---|
[463] | 102 |
|
---|
| 103 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 104 |
|
---|
[301] | 105 | glGenTextures( 1, &glid );
|
---|
| 106 |
|
---|
[331] | 107 | glBindTexture( gl_type, glid );
|
---|
[301] | 108 |
|
---|
| 109 | // Detect if mipmapping was requested
|
---|
[491] | 110 | if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
[301] | 111 | {
|
---|
[331] | 112 | // TODO: This should not be done if we use framebuffers!
|
---|
| 113 | glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
[301] | 114 | }
|
---|
| 115 |
|
---|
[331] | 116 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
[301] | 117 | {
|
---|
[331] | 118 | asampler.filter_max = sampler::LINEAR;
|
---|
[301] | 119 | }
|
---|
| 120 |
|
---|
[492] | 121 | if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE )
|
---|
| 122 | {
|
---|
| 123 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 124 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 125 | }
|
---|
[491] | 126 |
|
---|
[492] | 127 | if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE && gl_enum != GL_RED_INTEGER )
|
---|
[491] | 128 | {
|
---|
| 129 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
| 130 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
| 131 | }
|
---|
[301] | 132 |
|
---|
[463] | 133 | if ( is_depth )
|
---|
| 134 | {
|
---|
[492] | 135 | // glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 136 | // glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
[463] | 137 |
|
---|
[466] | 138 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 139 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 140 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
[463] | 141 | }
|
---|
| 142 |
|
---|
| 143 | // #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
|
---|
| 144 | // #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
|
---|
| 145 | //
|
---|
| 146 | // float aniso = 0.0f;
|
---|
| 147 | // glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso );
|
---|
| 148 | // NV_LOG_INFO( "Anisotropy at ", aniso, " (", int( aniso ), " ) " );
|
---|
| 149 | // glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso );
|
---|
| 150 |
|
---|
[492] | 151 | if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE )
|
---|
| 152 | glTexImage2D( gl_type, 0, gl_internal, size.x, size.y, 0, gl_enum, nv::datatype_to_gl_enum(aformat.type), data );
|
---|
| 153 | else
|
---|
| 154 | glTexImage2DMultisample( gl_type, 4, gl_internal, size.x, size.y, 1 );
|
---|
[331] | 155 |
|
---|
| 156 | glBindTexture( gl_type, 0 );
|
---|
| 157 |
|
---|
[301] | 158 | texture result = m_textures.create();
|
---|
| 159 | gl_texture_info* info = m_textures.get( result );
|
---|
[331] | 160 | info->type = type;
|
---|
[323] | 161 | info->format = aformat;
|
---|
| 162 | info->tsampler = asampler;
|
---|
[463] | 163 | info->size = ivec3( size.x, size.y, 1 );
|
---|
[323] | 164 | info->glid = glid;
|
---|
[301] | 165 | return result;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[491] | 168 |
|
---|
| 169 | nv::texture nv::gl_device::create_texture( texture_type type, pixel_format format )
|
---|
| 170 | {
|
---|
[492] | 171 | NV_ASSERT_ALWAYS( type == TEXTURE_1D_BUFFER, "create_texture not texture buffer!" );
|
---|
[491] | 172 | unsigned glid = 0;
|
---|
| 173 |
|
---|
| 174 | glGenTextures( 1, &glid );
|
---|
| 175 |
|
---|
| 176 | texture result = m_textures.create();
|
---|
| 177 | gl_texture_info* info = m_textures.get( result );
|
---|
| 178 | info->type = type;
|
---|
| 179 | info->format = format;
|
---|
| 180 | info->tsampler = sampler();
|
---|
| 181 | info->size = ivec3( 1, 1, 1 );
|
---|
| 182 | info->glid = glid;
|
---|
| 183 |
|
---|
| 184 | return result;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[463] | 187 | nv::texture nv::gl_device::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
| 188 | {
|
---|
| 189 | NV_ASSERT_ALWAYS( type == TEXTURE_3D || type == TEXTURE_2D_ARRAY, "3D texture type expected!" );
|
---|
| 190 | unsigned glid = 0;
|
---|
| 191 | unsigned gl_type = texture_type_to_enum( type );
|
---|
| 192 |
|
---|
| 193 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 194 |
|
---|
| 195 | glGenTextures( 1, &glid );
|
---|
| 196 | glBindTexture( gl_type, glid );
|
---|
| 197 |
|
---|
| 198 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
| 199 | {
|
---|
| 200 | asampler.filter_max = sampler::LINEAR;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 204 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 205 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
| 206 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
| 207 |
|
---|
[466] | 208 | if ( is_depth )
|
---|
| 209 | {
|
---|
| 210 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 211 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
| 212 |
|
---|
| 213 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 214 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 215 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[463] | 218 | //glTexStorage3D( GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount );
|
---|
| 219 | glTexImage3D( gl_type, 0, GLint( nv::image_format_to_internal_enum( aformat.format ) ), size.x, size.y, size.z, 0, nv::image_format_to_enum( aformat.format ), nv::datatype_to_gl_enum( aformat.type ), data );
|
---|
| 220 | glBindTexture( gl_type, 0 );
|
---|
| 221 |
|
---|
| 222 | texture result = m_textures.create();
|
---|
| 223 | gl_texture_info* info = m_textures.get( result );
|
---|
| 224 | info->type = type;
|
---|
| 225 | info->format = aformat;
|
---|
| 226 | info->tsampler = asampler;
|
---|
| 227 | info->size = size;
|
---|
| 228 | info->glid = glid;
|
---|
| 229 | return result;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 |
|
---|
[302] | 234 | void nv::gl_device::release( texture t )
|
---|
[301] | 235 | {
|
---|
| 236 | gl_texture_info* info = m_textures.get( t );
|
---|
| 237 | if ( info )
|
---|
| 238 | {
|
---|
[302] | 239 | if ( info->glid != 0 )
|
---|
| 240 | {
|
---|
| 241 | glDeleteTextures( 1, &(info->glid) );
|
---|
| 242 | }
|
---|
[301] | 243 | m_textures.destroy( t );
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[302] | 247 | void nv::gl_device::release( buffer b )
|
---|
| 248 | {
|
---|
| 249 | gl_buffer_info* info = m_buffers.get( b );
|
---|
| 250 | if ( info )
|
---|
| 251 | {
|
---|
| 252 | if ( info->glid != 0 )
|
---|
| 253 | {
|
---|
| 254 | glDeleteBuffers( 1, &(info->glid) );
|
---|
| 255 | }
|
---|
| 256 | m_buffers.destroy( b );
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[303] | 260 | const texture_info* nv::gl_device::get_texture_info( texture t ) const
|
---|
[301] | 261 | {
|
---|
| 262 | return m_textures.get( t );
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[302] | 265 | nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
| 266 | {
|
---|
[491] | 267 | unsigned glid = 0;
|
---|
[302] | 268 | unsigned glenum = buffer_type_to_enum( type );
|
---|
| 269 | glGenBuffers( 1, &glid );
|
---|
| 270 |
|
---|
[491] | 271 | if ( size > 0 )
|
---|
| 272 | {
|
---|
| 273 | glBindBuffer( glenum, glid );
|
---|
| 274 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( hint ) );
|
---|
| 275 | glBindBuffer( glenum, 0 );
|
---|
| 276 | }
|
---|
[302] | 277 |
|
---|
| 278 | buffer result = m_buffers.create();
|
---|
| 279 | gl_buffer_info* info = m_buffers.get( result );
|
---|
| 280 | info->type = type;
|
---|
| 281 | info->hint = hint;
|
---|
| 282 | info->size = size;
|
---|
| 283 | info->glid = glid;
|
---|
| 284 | return result;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[491] | 287 | void nv::gl_device::create_buffer( buffer b, size_t size, const void* source )
|
---|
| 288 | {
|
---|
| 289 | gl_buffer_info* info = m_buffers.get( b );
|
---|
| 290 | if ( info )
|
---|
| 291 | {
|
---|
| 292 | unsigned glenum = buffer_type_to_enum( info->type );
|
---|
| 293 | glBindBuffer( glenum, info->glid );
|
---|
| 294 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( info->hint ) );
|
---|
| 295 | glBindBuffer( glenum, 0 );
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[303] | 299 | const buffer_info* nv::gl_device::get_buffer_info( buffer t ) const
|
---|
[302] | 300 | {
|
---|
| 301 | return m_buffers.get( t );
|
---|
| 302 | }
|
---|
[303] | 303 |
|
---|
| 304 | void nv::gl_device::release( program p )
|
---|
| 305 | {
|
---|
| 306 | gl_program_info* info = m_programs.get( p );
|
---|
| 307 | if ( info )
|
---|
| 308 | {
|
---|
[392] | 309 | for ( auto& i : *info->m_uniform_map )
|
---|
[303] | 310 | delete i.second;
|
---|
| 311 |
|
---|
| 312 | glDetachShader( info->glid, info->glidv );
|
---|
| 313 | glDetachShader( info->glid, info->glidf );
|
---|
| 314 | glDeleteShader( info->glidv );
|
---|
| 315 | glDeleteShader( info->glidf );
|
---|
| 316 | glDeleteProgram( info->glid );
|
---|
| 317 |
|
---|
[392] | 318 | delete info->m_attribute_map;
|
---|
| 319 | delete info->m_engine_uniforms;
|
---|
| 320 | delete info->m_uniform_map;
|
---|
| 321 |
|
---|
[303] | 322 | m_programs.destroy( p );
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | void nv::gl_device::prepare_program( program p )
|
---|
| 327 | {
|
---|
| 328 | gl_program_info* info = m_programs.get( p );
|
---|
| 329 | if ( info )
|
---|
| 330 | {
|
---|
| 331 | auto& map = get_uniform_factory();
|
---|
| 332 | auto& lmap = get_link_uniform_factory();
|
---|
| 333 |
|
---|
[392] | 334 | for ( auto& i : *info->m_uniform_map )
|
---|
[303] | 335 | {
|
---|
[439] | 336 | auto j = lmap.find( i.first );
|
---|
[303] | 337 | if ( j != lmap.end() )
|
---|
| 338 | {
|
---|
| 339 | j->second->set( i.second );
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[439] | 342 | auto k = map.find( i.first );
|
---|
[303] | 343 | if ( k != map.end() )
|
---|
| 344 | {
|
---|
[392] | 345 | info->m_engine_uniforms->push_back( k->second->create( i.second ) );
|
---|
[303] | 346 | }
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 | }
|
---|
| 350 |
|
---|
[439] | 351 | uniform_base* nv::gl_device::get_uniform( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
[303] | 352 | {
|
---|
| 353 | const gl_program_info* info = m_programs.get( p );
|
---|
| 354 | {
|
---|
[392] | 355 | nv::uniform_map::const_iterator i = info->m_uniform_map->find( name );
|
---|
| 356 | if ( i != info->m_uniform_map->end() )
|
---|
[303] | 357 | {
|
---|
| 358 | return i->second;
|
---|
| 359 | }
|
---|
| 360 | if ( fatal )
|
---|
| 361 | {
|
---|
[439] | 362 | NV_LOG_CRITICAL( "gl_device : uniform '", name, "' not found in program!" );
|
---|
[403] | 363 | NV_ABORT( "gl_device : uniform not found!" );
|
---|
[303] | 364 | }
|
---|
| 365 | }
|
---|
| 366 | return nullptr;
|
---|
| 367 | }
|
---|
| 368 |
|
---|
[439] | 369 | int nv::gl_device::get_attribute_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
[303] | 370 | {
|
---|
| 371 | const gl_program_info* info = m_programs.get( p );
|
---|
| 372 | if ( info )
|
---|
| 373 | {
|
---|
[392] | 374 | attribute_map::const_iterator i = info->m_attribute_map->find( name );
|
---|
| 375 | if ( i != info->m_attribute_map->end() )
|
---|
[303] | 376 | {
|
---|
| 377 | return i->second.location;
|
---|
| 378 | }
|
---|
| 379 | if ( fatal )
|
---|
| 380 | {
|
---|
[439] | 381 | NV_LOG_CRITICAL( "gl_device : attribute '", name, "' not found in program!" );
|
---|
[403] | 382 | NV_ABORT( "gl_device : attribute not found!" );
|
---|
[303] | 383 | }
|
---|
| 384 | }
|
---|
| 385 | return -1;
|
---|
| 386 | }
|
---|
| 387 |
|
---|
[485] | 388 | bool nv::gl_device::bind_block( program p, const string_view& name, uint32 index )
|
---|
| 389 | {
|
---|
| 390 | const gl_program_info* info = m_programs.get( p );
|
---|
| 391 | if ( info )
|
---|
| 392 | {
|
---|
| 393 | int id = get_block_location( p, name, false );
|
---|
| 394 | if ( id < 0 ) return false;
|
---|
| 395 | glUniformBlockBinding( info->glid, unsigned( id ), index );
|
---|
| 396 | return true;
|
---|
| 397 | }
|
---|
| 398 | return false;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[473] | 401 | int nv::gl_device::get_block_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
| 402 | {
|
---|
[485] | 403 | const gl_program_info* info = m_programs.get( p );
|
---|
| 404 | if ( info )
|
---|
| 405 | {
|
---|
[487] | 406 | GLuint result = glGetUniformBlockIndex( info->glid, name.data() );
|
---|
| 407 | if ( result != GL_INVALID_INDEX ) return static_cast<int>( result );
|
---|
[485] | 408 | if ( fatal )
|
---|
| 409 | {
|
---|
| 410 | NV_LOG_CRITICAL( "gl_device : block '", name, "' not found in program!" );
|
---|
| 411 | NV_ABORT( "gl_device : block not found!" );
|
---|
| 412 | }
|
---|
| 413 | }
|
---|
[473] | 414 | return -1;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[399] | 417 | bool nv::gl_device::compile( gl_program_info* p, string_view vertex_program, string_view fragment_program )
|
---|
[303] | 418 | {
|
---|
| 419 | if (!compile( GL_VERTEX_SHADER, vertex_program, p->glidv )) { return false; }
|
---|
| 420 | if (!compile( GL_FRAGMENT_SHADER, fragment_program, p->glidf )) { return false; }
|
---|
| 421 |
|
---|
| 422 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::POSITION ), "nv_position" );
|
---|
| 423 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TEXCOORD ), "nv_texcoord" );
|
---|
| 424 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::NORMAL ), "nv_normal" );
|
---|
| 425 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::COLOR ), "nv_color" );
|
---|
| 426 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TANGENT ), "nv_tangent" );
|
---|
| 427 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEINDEX ), "nv_boneindex" );
|
---|
| 428 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEWEIGHT ), "nv_boneweight");
|
---|
| 429 |
|
---|
| 430 | glAttachShader( p->glid, p->glidf );
|
---|
| 431 | glAttachShader( p->glid, p->glidv );
|
---|
| 432 | glLinkProgram( p->glid );
|
---|
| 433 |
|
---|
| 434 | const uint32 buffer_size = 2048;
|
---|
| 435 | char buffer[ buffer_size ] = { 0 };
|
---|
| 436 | int length;
|
---|
| 437 | int status;
|
---|
| 438 |
|
---|
| 439 | glGetProgramiv( p->glid, GL_LINK_STATUS, &status );
|
---|
| 440 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
| 441 |
|
---|
[365] | 442 | NV_LOG_INFO( "Program #", p->glid, (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
|
---|
[303] | 443 |
|
---|
| 444 | if ( length > 0 )
|
---|
| 445 | {
|
---|
[466] | 446 | NV_LOG_INFO( "Program #", p->glid, " log: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 447 | }
|
---|
| 448 |
|
---|
| 449 | if ( status == GL_FALSE )
|
---|
| 450 | {
|
---|
| 451 | return false;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | glValidateProgram( p->glid );
|
---|
| 455 | glGetProgramiv( p->glid, GL_VALIDATE_STATUS, &status );
|
---|
| 456 |
|
---|
| 457 | if ( status == GL_FALSE )
|
---|
| 458 | {
|
---|
| 459 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
[365] | 460 | NV_LOG_ERROR( "Program #", p->glid, " validation error : ", buffer );
|
---|
[477] | 461 | //return false;
|
---|
[303] | 462 | }
|
---|
| 463 | load_attributes( p );
|
---|
| 464 | load_uniforms( p );
|
---|
| 465 | return true;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | void nv::gl_device::update_uniforms( gl_program_info* p )
|
---|
| 469 | {
|
---|
[392] | 470 | for ( auto& i : *p->m_uniform_map )
|
---|
[303] | 471 | {
|
---|
[392] | 472 | uniform_base* ubase = i.second;
|
---|
[303] | 473 | if ( ubase->is_dirty() )
|
---|
| 474 | {
|
---|
[487] | 475 | GLint uloc = ubase->get_location();
|
---|
| 476 | GLsizei size = static_cast<GLsizei>( ubase->get_length() );
|
---|
[303] | 477 | switch( ubase->get_type() )
|
---|
| 478 | {
|
---|
[487] | 479 | case FLOAT : glUniform1fv( uloc, size, static_cast< uniform< enum_to_type< FLOAT >::type >*>( ubase )->get_value() ); break;
|
---|
| 480 | case INT : glUniform1iv( uloc, size, static_cast< uniform< enum_to_type< INT >::type >*>( ubase )->get_value() ); break;
|
---|
| 481 | case FLOAT_VECTOR_2 : glUniform2fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
|
---|
| 482 | case FLOAT_VECTOR_3 : glUniform3fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
|
---|
| 483 | case FLOAT_VECTOR_4 : glUniform4fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
|
---|
| 484 | case INT_VECTOR_2 : glUniform2iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
|
---|
| 485 | case INT_VECTOR_3 : glUniform3iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
|
---|
| 486 | case INT_VECTOR_4 : glUniform4iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
|
---|
| 487 | case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*>( ubase )->get_value())); break;
|
---|
| 488 | case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*>( ubase )->get_value())); break;
|
---|
| 489 | case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*>( ubase )->get_value())); break;
|
---|
[303] | 490 | default : break; // error?
|
---|
| 491 | }
|
---|
| 492 | ubase->clean();
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | void nv::gl_device::load_attributes( gl_program_info* p )
|
---|
| 498 | {
|
---|
| 499 | int params;
|
---|
| 500 | glGetProgramiv( p->glid, GL_ACTIVE_ATTRIBUTES, ¶ms );
|
---|
| 501 |
|
---|
[406] | 502 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
[303] | 503 | {
|
---|
| 504 | int attr_nlen;
|
---|
| 505 | int attr_len;
|
---|
| 506 | unsigned attr_type;
|
---|
| 507 | char name_buffer[128];
|
---|
| 508 |
|
---|
| 509 | glGetActiveAttrib( p->glid, i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
|
---|
| 510 |
|
---|
[439] | 511 | string_view name( name_buffer, size_t( attr_nlen ) );
|
---|
[303] | 512 |
|
---|
| 513 | // skip built-ins
|
---|
| 514 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
| 515 |
|
---|
[439] | 516 | int attr_loc = glGetAttribLocation( p->glid, name.data() );
|
---|
[303] | 517 |
|
---|
[392] | 518 | attribute& attr = (*p->m_attribute_map)[ name ];
|
---|
[303] | 519 | attr.location = attr_loc;
|
---|
| 520 | attr.type = gl_enum_to_datatype( attr_type );
|
---|
| 521 | attr.length = attr_len;
|
---|
| 522 | }
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | void nv::gl_device::load_uniforms( gl_program_info* p )
|
---|
| 526 | {
|
---|
| 527 | int params;
|
---|
[473] | 528 | int bparams;
|
---|
[303] | 529 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORMS, ¶ms );
|
---|
[473] | 530 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORM_BLOCKS, &bparams );
|
---|
[303] | 531 |
|
---|
[406] | 532 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
[303] | 533 | {
|
---|
| 534 | int uni_nlen;
|
---|
| 535 | int uni_len;
|
---|
| 536 | unsigned uni_type;
|
---|
| 537 | char name_buffer[128];
|
---|
| 538 |
|
---|
| 539 | glGetActiveUniform( p->glid, i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
|
---|
| 540 |
|
---|
[439] | 541 | string_view name( name_buffer, size_t( uni_nlen ) );
|
---|
[303] | 542 |
|
---|
| 543 | // skip built-ins
|
---|
| 544 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
| 545 |
|
---|
[439] | 546 | int uni_loc = glGetUniformLocation( p->glid, name.data() );
|
---|
[303] | 547 | datatype utype = gl_enum_to_datatype( uni_type );
|
---|
| 548 |
|
---|
| 549 | // check for array
|
---|
[439] | 550 | size_t arrchar = name.find( '[' );
|
---|
| 551 | if ( arrchar != string_view::npos )
|
---|
[303] | 552 | {
|
---|
| 553 | name = name.substr( 0, arrchar );
|
---|
| 554 | }
|
---|
| 555 |
|
---|
[487] | 556 | uniform_base* u = uniform_base::create( utype, uni_loc, size_t( uni_len ) );
|
---|
[303] | 557 | NV_ASSERT( u, "Unknown uniform type!" );
|
---|
[392] | 558 | (*p->m_uniform_map)[ name ] = u;
|
---|
[492] | 559 | //NV_LOG_DEBUG( "Uniform : ", name, " - ", utype, "/", uni_len );
|
---|
| 560 |
|
---|
[303] | 561 | }
|
---|
[473] | 562 |
|
---|
| 563 | for ( unsigned i = 0; i < unsigned( bparams ); ++i )
|
---|
| 564 | {
|
---|
| 565 | int uni_len;
|
---|
| 566 | char name_buffer[128];
|
---|
| 567 |
|
---|
| 568 | glGetActiveUniformBlockName( p->glid, i, 128, &uni_len, name_buffer );
|
---|
| 569 | NV_LOG_INFO( string_view( name_buffer, size_t( uni_len ) ) );
|
---|
| 570 | }
|
---|
[303] | 571 | }
|
---|
| 572 |
|
---|
[399] | 573 | bool nv::gl_device::compile( uint32 sh_type, string_view shader_code, unsigned& glid )
|
---|
[303] | 574 | {
|
---|
| 575 | glid = glCreateShader( sh_type );
|
---|
| 576 |
|
---|
[361] | 577 | const char* pc = shader_code.data();
|
---|
[406] | 578 | int l = int( shader_code.length() );
|
---|
[303] | 579 |
|
---|
[361] | 580 | glShaderSource( glid, 1, &pc, &l );
|
---|
[303] | 581 | glCompileShader( glid );
|
---|
| 582 |
|
---|
| 583 | const uint32 buffer_size = 1024;
|
---|
| 584 | char buffer[ buffer_size ] = { 0 };
|
---|
| 585 | int length;
|
---|
| 586 | int compile_ok = GL_FALSE;
|
---|
| 587 | glGetShaderiv(glid, GL_COMPILE_STATUS, &compile_ok);
|
---|
| 588 | glGetShaderInfoLog( glid, buffer_size, &length, buffer );
|
---|
| 589 |
|
---|
| 590 | if ( length > 0 )
|
---|
| 591 | {
|
---|
| 592 | if ( compile_ok == 0 )
|
---|
| 593 | {
|
---|
[406] | 594 | NV_LOG_ERROR( "Shader #", glid, " error: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 595 | }
|
---|
| 596 | else
|
---|
| 597 | {
|
---|
[406] | 598 | NV_LOG_INFO( "Shader #", glid, " compiled successfully: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 599 | }
|
---|
| 600 | }
|
---|
| 601 | else
|
---|
| 602 | {
|
---|
[365] | 603 | NV_LOG_INFO( "Shader #", glid, " compiled successfully." );
|
---|
[303] | 604 | }
|
---|
| 605 | return compile_ok != 0;
|
---|
| 606 |
|
---|
| 607 | }
|
---|
| 608 |
|
---|