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