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