[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 | {
|
---|
[466] | 19 | m_shader_header.append( "#version 330\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 | {
|
---|
[463] | 97 | NV_ASSERT_ALWAYS( 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 );
|
---|
[463] | 100 |
|
---|
| 101 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 102 |
|
---|
[301] | 103 | glGenTextures( 1, &glid );
|
---|
| 104 |
|
---|
[331] | 105 | glBindTexture( gl_type, glid );
|
---|
[301] | 106 |
|
---|
| 107 | // Detect if mipmapping was requested
|
---|
[331] | 108 | if ( gl_type == GL_TEXTURE_2D && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
[301] | 109 | {
|
---|
[331] | 110 | // TODO: This should not be done if we use framebuffers!
|
---|
| 111 | glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
[301] | 112 | }
|
---|
| 113 |
|
---|
[331] | 114 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
[301] | 115 | {
|
---|
[331] | 116 | asampler.filter_max = sampler::LINEAR;
|
---|
[301] | 117 | }
|
---|
| 118 |
|
---|
[406] | 119 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 120 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 121 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s) ) );
|
---|
| 122 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t) ) );
|
---|
[301] | 123 |
|
---|
[463] | 124 | if ( is_depth )
|
---|
| 125 | {
|
---|
[466] | 126 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 127 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
[463] | 128 |
|
---|
[466] | 129 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 130 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 131 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
[463] | 132 | }
|
---|
| 133 |
|
---|
| 134 | // #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
|
---|
| 135 | // #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
|
---|
| 136 | //
|
---|
| 137 | // float aniso = 0.0f;
|
---|
| 138 | // glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso );
|
---|
| 139 | // NV_LOG_INFO( "Anisotropy at ", aniso, " (", int( aniso ), " ) " );
|
---|
| 140 | // glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso );
|
---|
| 141 |
|
---|
[406] | 142 | glTexImage2D( gl_type, 0, GLint( nv::image_format_to_internal_enum(aformat.format) ), size.x, size.y, 0, nv::image_format_to_enum(aformat.format), nv::datatype_to_gl_enum(aformat.type), data );
|
---|
[331] | 143 |
|
---|
| 144 | glBindTexture( gl_type, 0 );
|
---|
| 145 |
|
---|
[301] | 146 | texture result = m_textures.create();
|
---|
| 147 | gl_texture_info* info = m_textures.get( result );
|
---|
[331] | 148 | info->type = type;
|
---|
[323] | 149 | info->format = aformat;
|
---|
| 150 | info->tsampler = asampler;
|
---|
[463] | 151 | info->size = ivec3( size.x, size.y, 1 );
|
---|
[323] | 152 | info->glid = glid;
|
---|
[301] | 153 | return result;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[463] | 156 | nv::texture nv::gl_device::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
| 157 | {
|
---|
| 158 | NV_ASSERT_ALWAYS( type == TEXTURE_3D || type == TEXTURE_2D_ARRAY, "3D texture type expected!" );
|
---|
| 159 | unsigned glid = 0;
|
---|
| 160 | unsigned gl_type = texture_type_to_enum( type );
|
---|
| 161 |
|
---|
| 162 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 163 |
|
---|
| 164 | glGenTextures( 1, &glid );
|
---|
| 165 | glBindTexture( gl_type, glid );
|
---|
| 166 |
|
---|
| 167 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
| 168 | {
|
---|
| 169 | asampler.filter_max = sampler::LINEAR;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 173 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 174 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
| 175 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
| 176 |
|
---|
[466] | 177 | if ( is_depth )
|
---|
| 178 | {
|
---|
| 179 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 180 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
| 181 |
|
---|
| 182 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 183 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 184 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[463] | 187 | //glTexStorage3D( GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount );
|
---|
| 188 | 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 );
|
---|
| 189 | glBindTexture( gl_type, 0 );
|
---|
| 190 |
|
---|
| 191 | texture result = m_textures.create();
|
---|
| 192 | gl_texture_info* info = m_textures.get( result );
|
---|
| 193 | info->type = type;
|
---|
| 194 | info->format = aformat;
|
---|
| 195 | info->tsampler = asampler;
|
---|
| 196 | info->size = size;
|
---|
| 197 | info->glid = glid;
|
---|
| 198 | return result;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 |
|
---|
[302] | 203 | void nv::gl_device::release( texture t )
|
---|
[301] | 204 | {
|
---|
| 205 | gl_texture_info* info = m_textures.get( t );
|
---|
| 206 | if ( info )
|
---|
| 207 | {
|
---|
[302] | 208 | if ( info->glid != 0 )
|
---|
| 209 | {
|
---|
| 210 | glDeleteTextures( 1, &(info->glid) );
|
---|
| 211 | }
|
---|
[301] | 212 | m_textures.destroy( t );
|
---|
| 213 | }
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[302] | 216 | void nv::gl_device::release( buffer b )
|
---|
| 217 | {
|
---|
| 218 | gl_buffer_info* info = m_buffers.get( b );
|
---|
| 219 | if ( info )
|
---|
| 220 | {
|
---|
| 221 | if ( info->glid != 0 )
|
---|
| 222 | {
|
---|
| 223 | glDeleteBuffers( 1, &(info->glid) );
|
---|
| 224 | }
|
---|
| 225 | m_buffers.destroy( b );
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 |
|
---|
[303] | 229 | const texture_info* nv::gl_device::get_texture_info( texture t ) const
|
---|
[301] | 230 | {
|
---|
| 231 | return m_textures.get( t );
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[302] | 234 | nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
| 235 | {
|
---|
| 236 | unsigned glid = 0;
|
---|
| 237 | unsigned glenum = buffer_type_to_enum( type );
|
---|
| 238 | glGenBuffers( 1, &glid );
|
---|
| 239 |
|
---|
| 240 | glBindBuffer( glenum, glid );
|
---|
[406] | 241 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( hint ) );
|
---|
[302] | 242 | glBindBuffer( glenum, 0 );
|
---|
| 243 |
|
---|
| 244 | buffer result = m_buffers.create();
|
---|
| 245 | gl_buffer_info* info = m_buffers.get( result );
|
---|
| 246 | info->type = type;
|
---|
| 247 | info->hint = hint;
|
---|
| 248 | info->size = size;
|
---|
| 249 | info->glid = glid;
|
---|
| 250 | return result;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[303] | 253 | const buffer_info* nv::gl_device::get_buffer_info( buffer t ) const
|
---|
[302] | 254 | {
|
---|
| 255 | return m_buffers.get( t );
|
---|
| 256 | }
|
---|
[303] | 257 |
|
---|
| 258 | void nv::gl_device::release( program p )
|
---|
| 259 | {
|
---|
| 260 | gl_program_info* info = m_programs.get( p );
|
---|
| 261 | if ( info )
|
---|
| 262 | {
|
---|
[392] | 263 | for ( auto& i : *info->m_uniform_map )
|
---|
[303] | 264 | delete i.second;
|
---|
| 265 |
|
---|
| 266 | glDetachShader( info->glid, info->glidv );
|
---|
| 267 | glDetachShader( info->glid, info->glidf );
|
---|
| 268 | glDeleteShader( info->glidv );
|
---|
| 269 | glDeleteShader( info->glidf );
|
---|
| 270 | glDeleteProgram( info->glid );
|
---|
| 271 |
|
---|
[392] | 272 | delete info->m_attribute_map;
|
---|
| 273 | delete info->m_engine_uniforms;
|
---|
| 274 | delete info->m_uniform_map;
|
---|
| 275 |
|
---|
[303] | 276 | m_programs.destroy( p );
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | void nv::gl_device::prepare_program( program p )
|
---|
| 281 | {
|
---|
| 282 | gl_program_info* info = m_programs.get( p );
|
---|
| 283 | if ( info )
|
---|
| 284 | {
|
---|
| 285 | auto& map = get_uniform_factory();
|
---|
| 286 | auto& lmap = get_link_uniform_factory();
|
---|
| 287 |
|
---|
[392] | 288 | for ( auto& i : *info->m_uniform_map )
|
---|
[303] | 289 | {
|
---|
[439] | 290 | auto j = lmap.find( i.first );
|
---|
[303] | 291 | if ( j != lmap.end() )
|
---|
| 292 | {
|
---|
| 293 | j->second->set( i.second );
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[439] | 296 | auto k = map.find( i.first );
|
---|
[303] | 297 | if ( k != map.end() )
|
---|
| 298 | {
|
---|
[392] | 299 | info->m_engine_uniforms->push_back( k->second->create( i.second ) );
|
---|
[303] | 300 | }
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[439] | 305 | uniform_base* nv::gl_device::get_uniform( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
[303] | 306 | {
|
---|
| 307 | const gl_program_info* info = m_programs.get( p );
|
---|
| 308 | {
|
---|
[392] | 309 | nv::uniform_map::const_iterator i = info->m_uniform_map->find( name );
|
---|
| 310 | if ( i != info->m_uniform_map->end() )
|
---|
[303] | 311 | {
|
---|
| 312 | return i->second;
|
---|
| 313 | }
|
---|
| 314 | if ( fatal )
|
---|
| 315 | {
|
---|
[439] | 316 | NV_LOG_CRITICAL( "gl_device : uniform '", name, "' not found in program!" );
|
---|
[403] | 317 | NV_ABORT( "gl_device : uniform not found!" );
|
---|
[303] | 318 | }
|
---|
| 319 | }
|
---|
| 320 | return nullptr;
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[439] | 323 | int nv::gl_device::get_attribute_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
[303] | 324 | {
|
---|
| 325 | const gl_program_info* info = m_programs.get( p );
|
---|
| 326 | if ( info )
|
---|
| 327 | {
|
---|
[392] | 328 | attribute_map::const_iterator i = info->m_attribute_map->find( name );
|
---|
| 329 | if ( i != info->m_attribute_map->end() )
|
---|
[303] | 330 | {
|
---|
| 331 | return i->second.location;
|
---|
| 332 | }
|
---|
| 333 | if ( fatal )
|
---|
| 334 | {
|
---|
[439] | 335 | NV_LOG_CRITICAL( "gl_device : attribute '", name, "' not found in program!" );
|
---|
[403] | 336 | NV_ABORT( "gl_device : attribute not found!" );
|
---|
[303] | 337 | }
|
---|
| 338 | }
|
---|
| 339 | return -1;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[473] | 342 | int nv::gl_device::get_block_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
| 343 | {
|
---|
| 344 | return -1;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[399] | 347 | bool nv::gl_device::compile( gl_program_info* p, string_view vertex_program, string_view fragment_program )
|
---|
[303] | 348 | {
|
---|
| 349 | if (!compile( GL_VERTEX_SHADER, vertex_program, p->glidv )) { return false; }
|
---|
| 350 | if (!compile( GL_FRAGMENT_SHADER, fragment_program, p->glidf )) { return false; }
|
---|
| 351 |
|
---|
| 352 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::POSITION ), "nv_position" );
|
---|
| 353 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TEXCOORD ), "nv_texcoord" );
|
---|
| 354 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::NORMAL ), "nv_normal" );
|
---|
| 355 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::COLOR ), "nv_color" );
|
---|
| 356 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TANGENT ), "nv_tangent" );
|
---|
| 357 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEINDEX ), "nv_boneindex" );
|
---|
| 358 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEWEIGHT ), "nv_boneweight");
|
---|
| 359 |
|
---|
| 360 | glAttachShader( p->glid, p->glidf );
|
---|
| 361 | glAttachShader( p->glid, p->glidv );
|
---|
| 362 | glLinkProgram( p->glid );
|
---|
| 363 |
|
---|
| 364 | const uint32 buffer_size = 2048;
|
---|
| 365 | char buffer[ buffer_size ] = { 0 };
|
---|
| 366 | int length;
|
---|
| 367 | int status;
|
---|
| 368 |
|
---|
| 369 | glGetProgramiv( p->glid, GL_LINK_STATUS, &status );
|
---|
| 370 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
| 371 |
|
---|
[365] | 372 | NV_LOG_INFO( "Program #", p->glid, (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
|
---|
[303] | 373 |
|
---|
| 374 | if ( length > 0 )
|
---|
| 375 | {
|
---|
[466] | 376 | NV_LOG_INFO( "Program #", p->glid, " log: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 377 | }
|
---|
| 378 |
|
---|
| 379 | if ( status == GL_FALSE )
|
---|
| 380 | {
|
---|
| 381 | return false;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | glValidateProgram( p->glid );
|
---|
| 385 | glGetProgramiv( p->glid, GL_VALIDATE_STATUS, &status );
|
---|
| 386 |
|
---|
| 387 | if ( status == GL_FALSE )
|
---|
| 388 | {
|
---|
| 389 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
[365] | 390 | NV_LOG_ERROR( "Program #", p->glid, " validation error : ", buffer );
|
---|
[303] | 391 | return false;
|
---|
| 392 | }
|
---|
| 393 | load_attributes( p );
|
---|
| 394 | load_uniforms( p );
|
---|
| 395 | return true;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | void nv::gl_device::update_uniforms( gl_program_info* p )
|
---|
| 399 | {
|
---|
[392] | 400 | for ( auto& i : *p->m_uniform_map )
|
---|
[303] | 401 | {
|
---|
[392] | 402 | uniform_base* ubase = i.second;
|
---|
[303] | 403 | if ( ubase->is_dirty() )
|
---|
| 404 | {
|
---|
| 405 | int uloc = ubase->get_location();
|
---|
| 406 | switch( ubase->get_type() )
|
---|
| 407 | {
|
---|
[406] | 408 | case FLOAT : glUniform1fv( uloc, ubase->get_length(), static_cast< uniform< enum_to_type< FLOAT >::type >*>( ubase )->get_value() ); break;
|
---|
| 409 | case INT : glUniform1iv( uloc, ubase->get_length(), static_cast< uniform< enum_to_type< INT >::type >*>( ubase )->get_value() ); break;
|
---|
| 410 | case FLOAT_VECTOR_2 : glUniform2fv( uloc, ubase->get_length(), reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
|
---|
| 411 | case FLOAT_VECTOR_3 : glUniform3fv( uloc, ubase->get_length(), reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
|
---|
| 412 | case FLOAT_VECTOR_4 : glUniform4fv( uloc, ubase->get_length(), reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
|
---|
| 413 | case INT_VECTOR_2 : glUniform2iv( uloc, ubase->get_length(), reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
|
---|
| 414 | case INT_VECTOR_3 : glUniform3iv( uloc, ubase->get_length(), reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
|
---|
| 415 | case INT_VECTOR_4 : glUniform4iv( uloc, ubase->get_length(), reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
|
---|
| 416 | case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, ubase->get_length(), GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*>( ubase )->get_value())); break;
|
---|
| 417 | case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, ubase->get_length(), GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*>( ubase )->get_value())); break;
|
---|
| 418 | case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, ubase->get_length(), GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*>( ubase )->get_value())); break;
|
---|
[303] | 419 | default : break; // error?
|
---|
| 420 | }
|
---|
| 421 | ubase->clean();
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | }
|
---|
| 425 |
|
---|
| 426 | void nv::gl_device::load_attributes( gl_program_info* p )
|
---|
| 427 | {
|
---|
| 428 | int params;
|
---|
| 429 | glGetProgramiv( p->glid, GL_ACTIVE_ATTRIBUTES, ¶ms );
|
---|
| 430 |
|
---|
[406] | 431 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
[303] | 432 | {
|
---|
| 433 | int attr_nlen;
|
---|
| 434 | int attr_len;
|
---|
| 435 | unsigned attr_type;
|
---|
| 436 | char name_buffer[128];
|
---|
| 437 |
|
---|
| 438 | glGetActiveAttrib( p->glid, i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
|
---|
| 439 |
|
---|
[439] | 440 | string_view name( name_buffer, size_t( attr_nlen ) );
|
---|
[303] | 441 |
|
---|
| 442 | // skip built-ins
|
---|
| 443 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
| 444 |
|
---|
[439] | 445 | int attr_loc = glGetAttribLocation( p->glid, name.data() );
|
---|
[303] | 446 |
|
---|
[392] | 447 | attribute& attr = (*p->m_attribute_map)[ name ];
|
---|
[303] | 448 | attr.location = attr_loc;
|
---|
| 449 | attr.type = gl_enum_to_datatype( attr_type );
|
---|
| 450 | attr.length = attr_len;
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | void nv::gl_device::load_uniforms( gl_program_info* p )
|
---|
| 455 | {
|
---|
| 456 | int params;
|
---|
[473] | 457 | int bparams;
|
---|
[303] | 458 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORMS, ¶ms );
|
---|
[473] | 459 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORM_BLOCKS, &bparams );
|
---|
[303] | 460 |
|
---|
[406] | 461 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
[303] | 462 | {
|
---|
| 463 | int uni_nlen;
|
---|
| 464 | int uni_len;
|
---|
| 465 | unsigned uni_type;
|
---|
| 466 | char name_buffer[128];
|
---|
| 467 |
|
---|
| 468 | glGetActiveUniform( p->glid, i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
|
---|
| 469 |
|
---|
[439] | 470 | string_view name( name_buffer, size_t( uni_nlen ) );
|
---|
[303] | 471 |
|
---|
| 472 | // skip built-ins
|
---|
| 473 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
| 474 |
|
---|
[439] | 475 | int uni_loc = glGetUniformLocation( p->glid, name.data() );
|
---|
[303] | 476 | datatype utype = gl_enum_to_datatype( uni_type );
|
---|
| 477 |
|
---|
| 478 | // check for array
|
---|
[439] | 479 | size_t arrchar = name.find( '[' );
|
---|
| 480 | if ( arrchar != string_view::npos )
|
---|
[303] | 481 | {
|
---|
| 482 | name = name.substr( 0, arrchar );
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[439] | 485 | uniform_base* u = uniform_base::create( utype, uni_loc, uni_len );
|
---|
[303] | 486 | NV_ASSERT( u, "Unknown uniform type!" );
|
---|
[392] | 487 | (*p->m_uniform_map)[ name ] = u;
|
---|
[303] | 488 | }
|
---|
[473] | 489 |
|
---|
| 490 | for ( unsigned i = 0; i < unsigned( bparams ); ++i )
|
---|
| 491 | {
|
---|
| 492 | int uni_len;
|
---|
| 493 | char name_buffer[128];
|
---|
| 494 |
|
---|
| 495 | glGetActiveUniformBlockName( p->glid, i, 128, &uni_len, name_buffer );
|
---|
| 496 | NV_LOG_INFO( string_view( name_buffer, size_t( uni_len ) ) );
|
---|
| 497 | }
|
---|
[303] | 498 | }
|
---|
| 499 |
|
---|
[399] | 500 | bool nv::gl_device::compile( uint32 sh_type, string_view shader_code, unsigned& glid )
|
---|
[303] | 501 | {
|
---|
| 502 | glid = glCreateShader( sh_type );
|
---|
| 503 |
|
---|
[361] | 504 | const char* pc = shader_code.data();
|
---|
[406] | 505 | int l = int( shader_code.length() );
|
---|
[303] | 506 |
|
---|
[361] | 507 | glShaderSource( glid, 1, &pc, &l );
|
---|
[303] | 508 | glCompileShader( glid );
|
---|
| 509 |
|
---|
| 510 | const uint32 buffer_size = 1024;
|
---|
| 511 | char buffer[ buffer_size ] = { 0 };
|
---|
| 512 | int length;
|
---|
| 513 | int compile_ok = GL_FALSE;
|
---|
| 514 | glGetShaderiv(glid, GL_COMPILE_STATUS, &compile_ok);
|
---|
| 515 | glGetShaderInfoLog( glid, buffer_size, &length, buffer );
|
---|
| 516 |
|
---|
| 517 | if ( length > 0 )
|
---|
| 518 | {
|
---|
| 519 | if ( compile_ok == 0 )
|
---|
| 520 | {
|
---|
[406] | 521 | NV_LOG_ERROR( "Shader #", glid, " error: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 522 | }
|
---|
| 523 | else
|
---|
| 524 | {
|
---|
[406] | 525 | NV_LOG_INFO( "Shader #", glid, " compiled successfully: ", string_view( buffer, size_t( length ) ) );
|
---|
[303] | 526 | }
|
---|
| 527 | }
|
---|
| 528 | else
|
---|
| 529 | {
|
---|
[365] | 530 | NV_LOG_INFO( "Shader #", glid, " compiled successfully." );
|
---|
[303] | 531 | }
|
---|
| 532 | return compile_ok != 0;
|
---|
| 533 |
|
---|
| 534 | }
|
---|
| 535 |
|
---|