[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
|
---|
[498] | 111 | // if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
| 112 | // {
|
---|
| 113 | // // TODO: This should not be done if we use framebuffers!
|
---|
| 114 | // glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
| 115 | // }
|
---|
[301] | 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 |
|
---|
[498] | 157 | if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
| 158 | {
|
---|
| 159 | // TODO: This should not be done if we use framebuffers!
|
---|
| 160 | glGenerateMipmap( gl_type );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
[331] | 164 | glBindTexture( gl_type, 0 );
|
---|
| 165 |
|
---|
[301] | 166 | texture result = m_textures.create();
|
---|
| 167 | gl_texture_info* info = m_textures.get( result );
|
---|
[331] | 168 | info->type = type;
|
---|
[323] | 169 | info->format = aformat;
|
---|
| 170 | info->tsampler = asampler;
|
---|
[463] | 171 | info->size = ivec3( size.x, size.y, 1 );
|
---|
[323] | 172 | info->glid = glid;
|
---|
[301] | 173 | return result;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
[491] | 176 |
|
---|
| 177 | nv::texture nv::gl_device::create_texture( texture_type type, pixel_format format )
|
---|
| 178 | {
|
---|
[492] | 179 | NV_ASSERT_ALWAYS( type == TEXTURE_1D_BUFFER, "create_texture not texture buffer!" );
|
---|
[491] | 180 | unsigned glid = 0;
|
---|
| 181 |
|
---|
| 182 | glGenTextures( 1, &glid );
|
---|
| 183 |
|
---|
| 184 | texture result = m_textures.create();
|
---|
| 185 | gl_texture_info* info = m_textures.get( result );
|
---|
| 186 | info->type = type;
|
---|
| 187 | info->format = format;
|
---|
| 188 | info->tsampler = sampler();
|
---|
| 189 | info->size = ivec3( 1, 1, 1 );
|
---|
| 190 | info->glid = glid;
|
---|
| 191 |
|
---|
| 192 | return result;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[463] | 195 | nv::texture nv::gl_device::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
| 196 | {
|
---|
| 197 | NV_ASSERT_ALWAYS( type == TEXTURE_3D || type == TEXTURE_2D_ARRAY, "3D texture type expected!" );
|
---|
| 198 | unsigned glid = 0;
|
---|
| 199 | unsigned gl_type = texture_type_to_enum( type );
|
---|
| 200 |
|
---|
| 201 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 202 |
|
---|
| 203 | glGenTextures( 1, &glid );
|
---|
| 204 | glBindTexture( gl_type, glid );
|
---|
| 205 |
|
---|
| 206 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
| 207 | {
|
---|
| 208 | asampler.filter_max = sampler::LINEAR;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 212 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 213 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
| 214 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
| 215 |
|
---|
[466] | 216 | if ( is_depth )
|
---|
| 217 | {
|
---|
| 218 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 219 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
| 220 |
|
---|
| 221 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 222 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 223 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
| 224 | }
|
---|
| 225 |
|
---|
[463] | 226 | //glTexStorage3D( GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount );
|
---|
| 227 | 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 );
|
---|
| 228 | glBindTexture( gl_type, 0 );
|
---|
| 229 |
|
---|
| 230 | texture result = m_textures.create();
|
---|
| 231 | gl_texture_info* info = m_textures.get( result );
|
---|
| 232 | info->type = type;
|
---|
| 233 | info->format = aformat;
|
---|
| 234 | info->tsampler = asampler;
|
---|
| 235 | info->size = size;
|
---|
| 236 | info->glid = glid;
|
---|
| 237 | return result;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 |
|
---|
[302] | 242 | void nv::gl_device::release( texture t )
|
---|
[301] | 243 | {
|
---|
| 244 | gl_texture_info* info = m_textures.get( t );
|
---|
| 245 | if ( info )
|
---|
| 246 | {
|
---|
[302] | 247 | if ( info->glid != 0 )
|
---|
| 248 | {
|
---|
| 249 | glDeleteTextures( 1, &(info->glid) );
|
---|
| 250 | }
|
---|
[301] | 251 | m_textures.destroy( t );
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[302] | 255 | void nv::gl_device::release( buffer b )
|
---|
| 256 | {
|
---|
| 257 | gl_buffer_info* info = m_buffers.get( b );
|
---|
| 258 | if ( info )
|
---|
| 259 | {
|
---|
| 260 | if ( info->glid != 0 )
|
---|
| 261 | {
|
---|
| 262 | glDeleteBuffers( 1, &(info->glid) );
|
---|
| 263 | }
|
---|
| 264 | m_buffers.destroy( b );
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[303] | 268 | const texture_info* nv::gl_device::get_texture_info( texture t ) const
|
---|
[301] | 269 | {
|
---|
| 270 | return m_textures.get( t );
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[302] | 273 | nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
| 274 | {
|
---|
[491] | 275 | unsigned glid = 0;
|
---|
[302] | 276 | unsigned glenum = buffer_type_to_enum( type );
|
---|
| 277 | glGenBuffers( 1, &glid );
|
---|
| 278 |
|
---|
[491] | 279 | if ( size > 0 )
|
---|
| 280 | {
|
---|
| 281 | glBindBuffer( glenum, glid );
|
---|
| 282 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( hint ) );
|
---|
| 283 | glBindBuffer( glenum, 0 );
|
---|
| 284 | }
|
---|
[302] | 285 |
|
---|
| 286 | buffer result = m_buffers.create();
|
---|
| 287 | gl_buffer_info* info = m_buffers.get( result );
|
---|
| 288 | info->type = type;
|
---|
| 289 | info->hint = hint;
|
---|
| 290 | info->size = size;
|
---|
| 291 | info->glid = glid;
|
---|
| 292 | return result;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[491] | 295 | void nv::gl_device::create_buffer( buffer b, size_t size, const void* source )
|
---|
| 296 | {
|
---|
| 297 | gl_buffer_info* info = m_buffers.get( b );
|
---|
| 298 | if ( info )
|
---|
| 299 | {
|
---|
| 300 | unsigned glenum = buffer_type_to_enum( info->type );
|
---|
| 301 | glBindBuffer( glenum, info->glid );
|
---|
| 302 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( info->hint ) );
|
---|
| 303 | glBindBuffer( glenum, 0 );
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
[303] | 307 | const buffer_info* nv::gl_device::get_buffer_info( buffer t ) const
|
---|
[302] | 308 | {
|
---|
| 309 | return m_buffers.get( t );
|
---|
| 310 | }
|
---|
[303] | 311 |
|
---|
| 312 | void nv::gl_device::release( program p )
|
---|
| 313 | {
|
---|
| 314 | gl_program_info* info = m_programs.get( p );
|
---|
| 315 | if ( info )
|
---|
| 316 | {
|
---|
[392] | 317 | for ( auto& i : *info->m_uniform_map )
|
---|
[303] | 318 | delete i.second;
|
---|
| 319 |
|
---|
| 320 | glDetachShader( info->glid, info->glidv );
|
---|
| 321 | glDetachShader( info->glid, info->glidf );
|
---|
| 322 | glDeleteShader( info->glidv );
|
---|
| 323 | glDeleteShader( info->glidf );
|
---|
| 324 | glDeleteProgram( info->glid );
|
---|
| 325 |
|
---|
[392] | 326 | delete info->m_attribute_map;
|
---|
| 327 | delete info->m_engine_uniforms;
|
---|
| 328 | delete info->m_uniform_map;
|
---|
| 329 |
|
---|
[303] | 330 | m_programs.destroy( p );
|
---|
| 331 | }
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | void nv::gl_device::prepare_program( program p )
|
---|
| 335 | {
|
---|
| 336 | gl_program_info* info = m_programs.get( p );
|
---|
| 337 | if ( info )
|
---|
| 338 | {
|
---|
| 339 | auto& map = get_uniform_factory();
|
---|
| 340 | auto& lmap = get_link_uniform_factory();
|
---|
| 341 |
|
---|
[392] | 342 | for ( auto& i : *info->m_uniform_map )
|
---|
[303] | 343 | {
|
---|
[439] | 344 | auto j = lmap.find( i.first );
|
---|
[303] | 345 | if ( j != lmap.end() )
|
---|
| 346 | {
|
---|
| 347 | j->second->set( i.second );
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[439] | 350 | auto k = map.find( i.first );
|
---|
[303] | 351 | if ( k != map.end() )
|
---|
| 352 | {
|
---|
[392] | 353 | info->m_engine_uniforms->push_back( k->second->create( i.second ) );
|
---|
[303] | 354 | }
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[439] | 359 | uniform_base* nv::gl_device::get_uniform( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
[303] | 360 | {
|
---|
| 361 | const gl_program_info* info = m_programs.get( p );
|
---|
| 362 | {
|
---|
[392] | 363 | nv::uniform_map::const_iterator i = info->m_uniform_map->find( name );
|
---|
| 364 | if ( i != info->m_uniform_map->end() )
|
---|
[303] | 365 | {
|
---|
| 366 | return i->second;
|
---|
| 367 | }
|
---|
| 368 | if ( fatal )
|
---|
| 369 | {
|
---|
[439] | 370 | NV_LOG_CRITICAL( "gl_device : uniform '", name, "' not found in program!" );
|
---|
[403] | 371 | NV_ABORT( "gl_device : uniform not found!" );
|
---|
[303] | 372 | }
|
---|
| 373 | }
|
---|
| 374 | return nullptr;
|
---|
| 375 | }
|
---|
| 376 |
|
---|
[439] | 377 | int nv::gl_device::get_attribute_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
[303] | 378 | {
|
---|
| 379 | const gl_program_info* info = m_programs.get( p );
|
---|
| 380 | if ( info )
|
---|
| 381 | {
|
---|
[392] | 382 | attribute_map::const_iterator i = info->m_attribute_map->find( name );
|
---|
| 383 | if ( i != info->m_attribute_map->end() )
|
---|
[303] | 384 | {
|
---|
| 385 | return i->second.location;
|
---|
| 386 | }
|
---|
| 387 | if ( fatal )
|
---|
| 388 | {
|
---|
[439] | 389 | NV_LOG_CRITICAL( "gl_device : attribute '", name, "' not found in program!" );
|
---|
[403] | 390 | NV_ABORT( "gl_device : attribute not found!" );
|
---|
[303] | 391 | }
|
---|
| 392 | }
|
---|
| 393 | return -1;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[485] | 396 | bool nv::gl_device::bind_block( program p, const string_view& name, uint32 index )
|
---|
| 397 | {
|
---|
| 398 | const gl_program_info* info = m_programs.get( p );
|
---|
| 399 | if ( info )
|
---|
| 400 | {
|
---|
| 401 | int id = get_block_location( p, name, false );
|
---|
| 402 | if ( id < 0 ) return false;
|
---|
| 403 | glUniformBlockBinding( info->glid, unsigned( id ), index );
|
---|
| 404 | return true;
|
---|
| 405 | }
|
---|
| 406 | return false;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[473] | 409 | int nv::gl_device::get_block_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
| 410 | {
|
---|
[485] | 411 | const gl_program_info* info = m_programs.get( p );
|
---|
| 412 | if ( info )
|
---|
| 413 | {
|
---|
[487] | 414 | GLuint result = glGetUniformBlockIndex( info->glid, name.data() );
|
---|
| 415 | if ( result != GL_INVALID_INDEX ) return static_cast<int>( result );
|
---|
[485] | 416 | if ( fatal )
|
---|
| 417 | {
|
---|
| 418 | NV_LOG_CRITICAL( "gl_device : block '", name, "' not found in program!" );
|
---|
| 419 | NV_ABORT( "gl_device : block not found!" );
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
[473] | 422 | return -1;
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[399] | 425 | bool nv::gl_device::compile( gl_program_info* p, string_view vertex_program, string_view fragment_program )
|
---|
[303] | 426 | {
|
---|
| 427 | if (!compile( GL_VERTEX_SHADER, vertex_program, p->glidv )) { return false; }
|
---|
| 428 | if (!compile( GL_FRAGMENT_SHADER, fragment_program, p->glidf )) { return false; }
|
---|
| 429 |
|
---|
| 430 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::POSITION ), "nv_position" );
|
---|
| 431 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TEXCOORD ), "nv_texcoord" );
|
---|
| 432 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::NORMAL ), "nv_normal" );
|
---|
| 433 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::COLOR ), "nv_color" );
|
---|
| 434 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TANGENT ), "nv_tangent" );
|
---|
| 435 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEINDEX ), "nv_boneindex" );
|
---|
| 436 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEWEIGHT ), "nv_boneweight");
|
---|
| 437 |
|
---|
| 438 | glAttachShader( p->glid, p->glidf );
|
---|
| 439 | glAttachShader( p->glid, p->glidv );
|
---|
| 440 | glLinkProgram( p->glid );
|
---|
| 441 |
|
---|
| 442 | const uint32 buffer_size = 2048;
|
---|
| 443 | char buffer[ buffer_size ] = { 0 };
|
---|
| 444 | int length;
|
---|
| 445 | int status;
|
---|
| 446 |
|
---|
| 447 | glGetProgramiv( p->glid, GL_LINK_STATUS, &status );
|
---|
| 448 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
| 449 |
|
---|
[365] | 450 | NV_LOG_INFO( "Program #", p->glid, (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
|
---|
[303] | 451 |
|
---|
| 452 | if ( length > 0 )
|
---|
| 453 | {
|
---|
[466] | 454 | NV_LOG_INFO( "Program #", p->glid, " log: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 455 | }
|
---|
| 456 |
|
---|
| 457 | if ( status == GL_FALSE )
|
---|
| 458 | {
|
---|
| 459 | return false;
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | load_attributes( p );
|
---|
| 463 | load_uniforms( p );
|
---|
| 464 | return true;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | void nv::gl_device::update_uniforms( gl_program_info* p )
|
---|
| 468 | {
|
---|
[392] | 469 | for ( auto& i : *p->m_uniform_map )
|
---|
[303] | 470 | {
|
---|
[392] | 471 | uniform_base* ubase = i.second;
|
---|
[303] | 472 | if ( ubase->is_dirty() )
|
---|
| 473 | {
|
---|
[487] | 474 | GLint uloc = ubase->get_location();
|
---|
| 475 | GLsizei size = static_cast<GLsizei>( ubase->get_length() );
|
---|
[303] | 476 | switch( ubase->get_type() )
|
---|
| 477 | {
|
---|
[487] | 478 | case FLOAT : glUniform1fv( uloc, size, static_cast< uniform< enum_to_type< FLOAT >::type >*>( ubase )->get_value() ); break;
|
---|
| 479 | case INT : glUniform1iv( uloc, size, static_cast< uniform< enum_to_type< INT >::type >*>( ubase )->get_value() ); break;
|
---|
| 480 | 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;
|
---|
| 481 | 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;
|
---|
| 482 | 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;
|
---|
| 483 | 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;
|
---|
| 484 | 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;
|
---|
| 485 | 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;
|
---|
| 486 | 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;
|
---|
| 487 | 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;
|
---|
| 488 | 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] | 489 | default : break; // error?
|
---|
| 490 | }
|
---|
| 491 | ubase->clean();
|
---|
| 492 | }
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 |
|
---|
| 496 | void nv::gl_device::load_attributes( gl_program_info* p )
|
---|
| 497 | {
|
---|
| 498 | int params;
|
---|
| 499 | glGetProgramiv( p->glid, GL_ACTIVE_ATTRIBUTES, ¶ms );
|
---|
| 500 |
|
---|
[406] | 501 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
[303] | 502 | {
|
---|
| 503 | int attr_nlen;
|
---|
| 504 | int attr_len;
|
---|
| 505 | unsigned attr_type;
|
---|
| 506 | char name_buffer[128];
|
---|
| 507 |
|
---|
| 508 | glGetActiveAttrib( p->glid, i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
|
---|
| 509 |
|
---|
[439] | 510 | string_view name( name_buffer, size_t( attr_nlen ) );
|
---|
[303] | 511 |
|
---|
| 512 | // skip built-ins
|
---|
| 513 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
| 514 |
|
---|
[439] | 515 | int attr_loc = glGetAttribLocation( p->glid, name.data() );
|
---|
[303] | 516 |
|
---|
[392] | 517 | attribute& attr = (*p->m_attribute_map)[ name ];
|
---|
[303] | 518 | attr.location = attr_loc;
|
---|
| 519 | attr.type = gl_enum_to_datatype( attr_type );
|
---|
| 520 | attr.length = attr_len;
|
---|
| 521 | }
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | void nv::gl_device::load_uniforms( gl_program_info* p )
|
---|
| 525 | {
|
---|
| 526 | int params;
|
---|
[473] | 527 | int bparams;
|
---|
[303] | 528 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORMS, ¶ms );
|
---|
[473] | 529 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORM_BLOCKS, &bparams );
|
---|
[303] | 530 |
|
---|
[406] | 531 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
[303] | 532 | {
|
---|
| 533 | int uni_nlen;
|
---|
| 534 | int uni_len;
|
---|
| 535 | unsigned uni_type;
|
---|
| 536 | char name_buffer[128];
|
---|
| 537 |
|
---|
| 538 | glGetActiveUniform( p->glid, i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
|
---|
| 539 |
|
---|
[439] | 540 | string_view name( name_buffer, size_t( uni_nlen ) );
|
---|
[303] | 541 |
|
---|
| 542 | // skip built-ins
|
---|
| 543 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
| 544 |
|
---|
[439] | 545 | int uni_loc = glGetUniformLocation( p->glid, name.data() );
|
---|
[303] | 546 | datatype utype = gl_enum_to_datatype( uni_type );
|
---|
| 547 |
|
---|
| 548 | // check for array
|
---|
[439] | 549 | size_t arrchar = name.find( '[' );
|
---|
| 550 | if ( arrchar != string_view::npos )
|
---|
[303] | 551 | {
|
---|
| 552 | name = name.substr( 0, arrchar );
|
---|
| 553 | }
|
---|
| 554 |
|
---|
[487] | 555 | uniform_base* u = uniform_base::create( utype, uni_loc, size_t( uni_len ) );
|
---|
[303] | 556 | NV_ASSERT( u, "Unknown uniform type!" );
|
---|
[392] | 557 | (*p->m_uniform_map)[ name ] = u;
|
---|
[492] | 558 | //NV_LOG_DEBUG( "Uniform : ", name, " - ", utype, "/", uni_len );
|
---|
| 559 |
|
---|
[303] | 560 | }
|
---|
[473] | 561 |
|
---|
| 562 | for ( unsigned i = 0; i < unsigned( bparams ); ++i )
|
---|
| 563 | {
|
---|
| 564 | int uni_len;
|
---|
| 565 | char name_buffer[128];
|
---|
| 566 |
|
---|
| 567 | glGetActiveUniformBlockName( p->glid, i, 128, &uni_len, name_buffer );
|
---|
| 568 | NV_LOG_INFO( string_view( name_buffer, size_t( uni_len ) ) );
|
---|
| 569 | }
|
---|
[303] | 570 | }
|
---|
| 571 |
|
---|
[399] | 572 | bool nv::gl_device::compile( uint32 sh_type, string_view shader_code, unsigned& glid )
|
---|
[303] | 573 | {
|
---|
| 574 | glid = glCreateShader( sh_type );
|
---|
| 575 |
|
---|
[361] | 576 | const char* pc = shader_code.data();
|
---|
[406] | 577 | int l = int( shader_code.length() );
|
---|
[303] | 578 |
|
---|
[361] | 579 | glShaderSource( glid, 1, &pc, &l );
|
---|
[303] | 580 | glCompileShader( glid );
|
---|
| 581 |
|
---|
| 582 | const uint32 buffer_size = 1024;
|
---|
| 583 | char buffer[ buffer_size ] = { 0 };
|
---|
| 584 | int length;
|
---|
| 585 | int compile_ok = GL_FALSE;
|
---|
| 586 | glGetShaderiv(glid, GL_COMPILE_STATUS, &compile_ok);
|
---|
| 587 | glGetShaderInfoLog( glid, buffer_size, &length, buffer );
|
---|
| 588 |
|
---|
| 589 | if ( length > 0 )
|
---|
| 590 | {
|
---|
| 591 | if ( compile_ok == 0 )
|
---|
| 592 | {
|
---|
[406] | 593 | NV_LOG_ERROR( "Shader #", glid, " error: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 594 | }
|
---|
| 595 | else
|
---|
| 596 | {
|
---|
[406] | 597 | NV_LOG_INFO( "Shader #", glid, " compiled successfully: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 598 | }
|
---|
| 599 | }
|
---|
| 600 | else
|
---|
| 601 | {
|
---|
[365] | 602 | NV_LOG_INFO( "Shader #", glid, " compiled successfully." );
|
---|
[303] | 603 | }
|
---|
| 604 | return compile_ok != 0;
|
---|
| 605 |
|
---|
| 606 | }
|
---|
| 607 |
|
---|