[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.
|
---|
[37] | 6 |
|
---|
| 7 | #include "nv/gl/gl_context.hh"
|
---|
| 8 |
|
---|
| 9 | #include "nv/gl/gl_enum.hh"
|
---|
| 10 | #include "nv/lib/gl.hh"
|
---|
[301] | 11 | #include "nv/gl/gl_device.hh"
|
---|
[365] | 12 | #include "nv/core/logger.hh"
|
---|
[37] | 13 |
|
---|
| 14 | using namespace nv;
|
---|
| 15 |
|
---|
[491] | 16 | nv::vertex_array nv::gl_context::create_vertex_array( const vertex_array_desc& desc )
|
---|
[313] | 17 | {
|
---|
| 18 | vertex_array result = m_vertex_arrays.create();
|
---|
[491] | 19 | gl_vertex_array_info* info = m_vertex_arrays.get( result );
|
---|
| 20 |
|
---|
| 21 | glGenVertexArrays( 1, &info->glid );
|
---|
| 22 | info->count = desc.count;
|
---|
| 23 | info->index = desc.index;
|
---|
| 24 | info->index_owner = desc.index_owner;
|
---|
| 25 | info->index_type = desc.index_type;
|
---|
| 26 |
|
---|
| 27 | for ( uint32 i = 0; i < desc.count; ++i )
|
---|
| 28 | info->attr[i] = desc.attr[i];
|
---|
| 29 |
|
---|
| 30 | glBindVertexArray( info->glid );
|
---|
| 31 |
|
---|
| 32 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
| 33 | {
|
---|
| 34 | const vertex_buffer_attribute& vba = info->attr[i];
|
---|
| 35 | uint32 location = static_cast<uint32>( vba.location );
|
---|
| 36 | glEnableVertexAttribArray( location );
|
---|
| 37 | const gl_buffer_info* vinfo = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( vba.vbuffer ) );
|
---|
| 38 | if ( vinfo && vinfo->type == VERTEX_BUFFER )
|
---|
| 39 | glBindBuffer( GL_ARRAY_BUFFER, vinfo->glid );
|
---|
| 40 | else
|
---|
| 41 | {
|
---|
| 42 | // TODO: report error
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | glVertexAttribPointer(
|
---|
| 46 | location,
|
---|
| 47 | static_cast<GLint>( vba.components ),
|
---|
| 48 | nv::datatype_to_gl_enum( vba.dtype ),
|
---|
| 49 | GL_FALSE,
|
---|
| 50 | static_cast<GLsizei>( vba.stride ),
|
---|
| 51 | reinterpret_cast<void*>( vba.offset )
|
---|
| 52 | );
|
---|
| 53 | }
|
---|
| 54 | glBindBuffer( GL_ARRAY_BUFFER, 0 );
|
---|
| 55 |
|
---|
| 56 | if ( info->index.is_valid() )
|
---|
| 57 | {
|
---|
| 58 | const gl_buffer_info* iinfo = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( info->index ) );
|
---|
| 59 | if ( iinfo && iinfo->type == INDEX_BUFFER )
|
---|
| 60 | {
|
---|
| 61 | glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iinfo->glid );
|
---|
| 62 | }
|
---|
| 63 | else
|
---|
| 64 | {
|
---|
| 65 | // TODO: report error
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | glBindVertexArray( 0 );
|
---|
| 69 |
|
---|
| 70 | if ( info->index.is_valid() )
|
---|
| 71 | {
|
---|
| 72 | glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[492] | 75 | // for ( uint32 i = 0; i < info->count; ++i )
|
---|
| 76 | // {
|
---|
| 77 | // glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
|
---|
| 78 | // }
|
---|
[491] | 79 |
|
---|
| 80 |
|
---|
[313] | 81 | return result;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[492] | 84 | nv::framebuffer nv::gl_context::create_framebuffer( uint32 temp_samples )
|
---|
[313] | 85 | {
|
---|
[491] | 86 | unsigned glid = 0;
|
---|
| 87 | glGenFramebuffers( 1, &glid );
|
---|
| 88 | framebuffer result = m_framebuffers.create();
|
---|
| 89 | gl_framebuffer_info* info = m_framebuffers.get( result );
|
---|
| 90 | info->glid = glid;
|
---|
| 91 | info->depth_rb_glid = 0;
|
---|
| 92 | info->color_attachment_count = 0;
|
---|
[492] | 93 | info->sample_count = temp_samples;
|
---|
[491] | 94 | return result;
|
---|
[313] | 95 | }
|
---|
| 96 |
|
---|
| 97 | void nv::gl_context::release( vertex_array va )
|
---|
| 98 | {
|
---|
[491] | 99 | gl_vertex_array_info* info = m_vertex_arrays.get( va );
|
---|
[313] | 100 | if ( info )
|
---|
| 101 | {
|
---|
| 102 | for ( uint32 i = 0; i < info->count; ++i )
|
---|
| 103 | {
|
---|
[469] | 104 | if ( info->attr[i].owner )
|
---|
[501] | 105 | release( info->attr[i].vbuffer );
|
---|
[313] | 106 | }
|
---|
[501] | 107 | if ( info->index.is_valid() && info->index_owner) release( info->index );
|
---|
[491] | 108 | glDeleteVertexArrays( 1, &info->glid );
|
---|
[313] | 109 | m_vertex_arrays.destroy( va );
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void nv::gl_context::release( framebuffer f )
|
---|
| 114 | {
|
---|
| 115 | gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
| 116 | if ( info )
|
---|
| 117 | {
|
---|
| 118 | // TODO: release textures?
|
---|
[331] | 119 | glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
---|
| 120 | glBindRenderbuffer( GL_RENDERBUFFER, 0 );
|
---|
| 121 | if ( info->depth_rb_glid == 0 )
|
---|
| 122 | glDeleteRenderbuffers( 1, &info->depth_rb_glid );
|
---|
| 123 | glDeleteFramebuffers( 1, &info->glid );
|
---|
[335] | 124 | m_framebuffers.destroy( f );
|
---|
[313] | 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const
|
---|
| 129 | {
|
---|
| 130 | return m_framebuffers.get( f );
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[500] | 133 | void nv::gl_context::attach( framebuffer f, output_slot slot, texture t, int layer /* = -1*/ )
|
---|
[331] | 134 | {
|
---|
| 135 | // TODO: framebuffer variable, so no re-binding?
|
---|
| 136 | // TODO: support 1d, 3d textures, cubemaps or layers?
|
---|
| 137 | // TODO: support GL_READ_FRAMEBUFFER?
|
---|
| 138 | const gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
[406] | 139 | const gl_texture_info* tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
|
---|
[331] | 140 | if ( info )
|
---|
| 141 | {
|
---|
| 142 | glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
|
---|
| 143 | unsigned gl_type = texture_type_to_enum( tinfo->type );
|
---|
[500] | 144 | unsigned tglid = tinfo ? tinfo->glid : 0;
|
---|
[313] | 145 |
|
---|
[500] | 146 | if ( layer >= 0 )
|
---|
[331] | 147 | {
|
---|
[500] | 148 | glFramebufferTextureLayer( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + unsigned( slot ), tglid, 0, layer );
|
---|
[331] | 149 | }
|
---|
| 150 | else
|
---|
| 151 | {
|
---|
[500] | 152 | glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + unsigned( slot ), gl_type, tglid, 0 );
|
---|
[331] | 153 | }
|
---|
| 154 |
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[463] | 158 | void nv::gl_context::attach( framebuffer f, texture depth, int layer /*= -1*/ )
|
---|
[331] | 159 | {
|
---|
| 160 | // TODO: framebuffer variable, so no re-binding?
|
---|
| 161 | // TODO: support GL_READ_FRAMEBUFFER?
|
---|
| 162 | const gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
[406] | 163 | const gl_texture_info* tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( depth ) );
|
---|
[331] | 164 | if ( info )
|
---|
| 165 | {
|
---|
| 166 | glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
|
---|
| 167 | unsigned gl_type = texture_type_to_enum( tinfo->type );
|
---|
[463] | 168 | unsigned glid = ( tinfo ? tinfo->glid : 0 );
|
---|
| 169 | if ( layer >= 0 )
|
---|
[331] | 170 | {
|
---|
[500] | 171 | glFramebufferTextureLayer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, glid, 0, layer );
|
---|
[331] | 172 | }
|
---|
| 173 | else
|
---|
| 174 | {
|
---|
[463] | 175 | glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, glid, 0 );
|
---|
[331] | 176 | }
|
---|
| 177 | }
|
---|
[463] | 178 |
|
---|
[331] | 179 | }
|
---|
| 180 |
|
---|
| 181 | void nv::gl_context::attach( framebuffer f, ivec2 size )
|
---|
| 182 | {
|
---|
| 183 | // TODO: framebuffer variable, so no re-binding?
|
---|
| 184 | // TODO: support GL_READ_FRAMEBUFFER?
|
---|
| 185 | gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
| 186 | if ( info )
|
---|
| 187 | {
|
---|
| 188 | glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
|
---|
[462] | 189 | if ( info->depth_rb_glid )
|
---|
| 190 | glDeleteRenderbuffers( 1, &(info->depth_rb_glid) );
|
---|
[331] | 191 | glGenRenderbuffers( 1, &(info->depth_rb_glid) );
|
---|
| 192 | glBindRenderbuffer( GL_RENDERBUFFER, info->depth_rb_glid );
|
---|
[492] | 193 | if ( info->sample_count > 1 )
|
---|
| 194 | glRenderbufferStorageMultisample( GL_RENDERBUFFER, info->sample_count, GL_DEPTH_COMPONENT16, size.x, size.y );
|
---|
| 195 | else
|
---|
| 196 | glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.x, size.y );
|
---|
[331] | 197 | glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, info->depth_rb_glid );
|
---|
| 198 | glBindRenderbuffer( GL_RENDERBUFFER, 0 );
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | void nv::gl_context::blit( framebuffer f, clear_state::buffers_type mask, ivec2 src1, ivec2 src2, ivec2 dst1, ivec2 dst2 )
|
---|
| 204 | {
|
---|
| 205 | gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
| 206 | if ( info )
|
---|
| 207 | {
|
---|
| 208 | glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
|
---|
| 209 | unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST;
|
---|
| 210 | glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter );
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 |
|
---|
[492] | 215 | void nv::gl_context::blit( framebuffer from, framebuffer to, clear_state::buffers_type mask, ivec2 src1, ivec2 src2, ivec2 dst1, ivec2 dst2 )
|
---|
| 216 | {
|
---|
| 217 | gl_framebuffer_info* finfo = m_framebuffers.get( from );
|
---|
| 218 | gl_framebuffer_info* tinfo = m_framebuffers.get( to );
|
---|
| 219 | if ( finfo )
|
---|
| 220 | {
|
---|
| 221 | glBindFramebuffer( GL_READ_FRAMEBUFFER, finfo->glid );
|
---|
| 222 | glBindFramebuffer( GL_DRAW_FRAMEBUFFER, tinfo ? tinfo->glid : 0 );
|
---|
| 223 | unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST;
|
---|
| 224 | glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter );
|
---|
| 225 | glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
|
---|
| 226 | if ( tinfo ) glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 );
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[331] | 230 | bool nv::gl_context::check( framebuffer_slot ft )
|
---|
| 231 | {
|
---|
[462] | 232 | glDrawBuffer( 0 );
|
---|
| 233 | glReadBuffer( 0 );
|
---|
[491] | 234 |
|
---|
| 235 | unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) );
|
---|
| 236 | if ( result == GL_FRAMEBUFFER_COMPLETE ) return true;
|
---|
| 237 | switch ( result )
|
---|
[331] | 238 | {
|
---|
[492] | 239 | case GL_FRAMEBUFFER_UNDEFINED : NV_LOG_ERROR( "gl_context::check : Framebuffer undefined!" ); break;
|
---|
| 240 | case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE : NV_LOG_ERROR( "gl_context::check : Incomplete multisample!" ); break;
|
---|
| 241 | case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS : NV_LOG_ERROR( "gl_context::check : Incomplete layer targets!" ); break;
|
---|
[491] | 242 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete attachment!" ); break;
|
---|
| 243 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : NV_LOG_ERROR( "gl_context::check : Framebuffer missing attachment!" ); break;
|
---|
[466] | 244 | // case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete dimensions!" ); break;
|
---|
| 245 | // case GL_FRAMEBUFFER_INCOMPLETE_FORMATS : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete formats!" ); break;
|
---|
[491] | 246 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete draw buffer!" ); break;
|
---|
| 247 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete read buffer!" ); break;
|
---|
| 248 | case GL_FRAMEBUFFER_UNSUPPORTED : NV_LOG_ERROR( "gl_context::check : Framebuffer format combination unsupported!" ); break;
|
---|
| 249 | default: NV_LOG_ERROR( "gl_context::check : Unknown Framebuffer error! (", result, ")" ); break;
|
---|
[331] | 250 | }
|
---|
[462] | 251 | glDrawBuffer( GL_BACK );
|
---|
| 252 | glReadBuffer( GL_BACK );
|
---|
[331] | 253 | return false;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void nv::gl_context::bind( framebuffer f, framebuffer_slot ft /*= FRAMEBUFFER_BOTH */ )
|
---|
| 257 | {
|
---|
| 258 | const gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
| 259 | if ( info )
|
---|
| 260 | {
|
---|
| 261 | glBindFramebuffer( framebuffer_slot_to_enum(ft), info->glid );
|
---|
| 262 | }
|
---|
| 263 | else
|
---|
| 264 | {
|
---|
| 265 | glBindFramebuffer( framebuffer_slot_to_enum(ft), 0 );
|
---|
| 266 | }
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[501] | 269 |
|
---|
[491] | 270 | void nv::gl_context::bind( buffer b, texture t )
|
---|
| 271 | {
|
---|
| 272 | const gl_buffer_info* binfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
|
---|
| 273 | const gl_texture_info* tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
|
---|
| 274 | NV_ASSERT( binfo && tinfo, "Bad buffer or texture passed to create_texture" );
|
---|
| 275 | if ( binfo && tinfo )
|
---|
| 276 | {
|
---|
| 277 | NV_ASSERT_ALWAYS( binfo->type == TEXTURE_BUFFER && tinfo->type == TEXTURE_1D_BUFFER, "bad texture or buffer type!" );
|
---|
[492] | 278 | bind( t, TEXTURE_0 );
|
---|
[491] | 279 | glTexBuffer( GL_TEXTURE_BUFFER, image_format_to_internal_enum( tinfo->format.format ), binfo->glid );
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[473] | 283 | void nv::gl_context::bind( buffer b, uint32 index, size_t offset /*= 0*/, size_t size /*= 0 */ )
|
---|
| 284 | {
|
---|
| 285 | const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
|
---|
| 286 | if ( info )
|
---|
| 287 | {
|
---|
| 288 | if ( size == 0 )
|
---|
| 289 | glBindBufferBase( buffer_type_to_enum( info->type ), index, info->glid );
|
---|
| 290 | else
|
---|
[487] | 291 | glBindBufferRange( buffer_type_to_enum( info->type ), index, info->glid, static_cast<GLintptr>( offset ), static_cast<GLsizeiptr>( size ) );
|
---|
[473] | 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[301] | 295 | void gl_context::bind( texture t, texture_slot slot )
|
---|
[299] | 296 | {
|
---|
[492] | 297 | if ( m_bound_textures[ slot ] != t )
|
---|
[301] | 298 | {
|
---|
[492] | 299 | const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
|
---|
| 300 | if ( info )
|
---|
| 301 | {
|
---|
| 302 | set_active_texture( slot );
|
---|
| 303 | glBindTexture( texture_type_to_enum( info->type ), info->glid );
|
---|
[501] | 304 | m_bound_textures[slot] = t;
|
---|
[492] | 305 | }
|
---|
[301] | 306 | }
|
---|
[299] | 307 | }
|
---|
| 308 |
|
---|
[303] | 309 | void nv::gl_context::bind( program p )
|
---|
[299] | 310 | {
|
---|
[406] | 311 | gl_device* gdevice = static_cast<gl_device*>( m_device );
|
---|
| 312 | gl_program_info* info = gdevice->m_programs.get( p );
|
---|
[303] | 313 | if ( info )
|
---|
| 314 | {
|
---|
[501] | 315 | if ( m_bound_program != p )
|
---|
| 316 | glUseProgram( info->glid );
|
---|
[406] | 317 | gdevice->update_uniforms( info );
|
---|
[493] | 318 | if ( !info->validated )
|
---|
| 319 | {
|
---|
| 320 | validate_program( p );
|
---|
| 321 | }
|
---|
[501] | 322 | m_bound_program = p;
|
---|
[303] | 323 | }
|
---|
[299] | 324 | }
|
---|
| 325 |
|
---|
[493] | 326 | bool nv::gl_context::validate_program( program p )
|
---|
| 327 | {
|
---|
| 328 | gl_device* gdevice = static_cast<gl_device*>( m_device );
|
---|
| 329 | gl_program_info* info = gdevice->m_programs.get( p );
|
---|
| 330 | if ( info )
|
---|
| 331 | {
|
---|
| 332 | info->validated = true;
|
---|
| 333 | const uint32 buffer_size = 1024;
|
---|
| 334 | char buffer[buffer_size] = { 0 };
|
---|
| 335 | int length;
|
---|
| 336 | int status;
|
---|
| 337 | glValidateProgram( info->glid );
|
---|
| 338 | glGetProgramiv( info->glid, GL_VALIDATE_STATUS, &status );
|
---|
| 339 |
|
---|
| 340 | if ( status == GL_FALSE )
|
---|
| 341 | {
|
---|
| 342 | glGetProgramInfoLog( info->glid, buffer_size, &length, buffer );
|
---|
| 343 | NV_LOG_ERROR( "Program #", info->glid, " validation error : ", buffer );
|
---|
| 344 | return false;
|
---|
| 345 | }
|
---|
| 346 | return true;
|
---|
| 347 | }
|
---|
| 348 | return false;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 |
|
---|
[313] | 352 | // void nv::gl_context::bind( buffer b )
|
---|
| 353 | // {
|
---|
| 354 | // const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
|
---|
| 355 | // if ( info )
|
---|
| 356 | // {
|
---|
| 357 | // glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
|
---|
| 358 | // }
|
---|
| 359 | // }
|
---|
[299] | 360 |
|
---|
[302] | 361 | void nv::gl_context::bind( vertex_array va )
|
---|
[299] | 362 | {
|
---|
[491] | 363 | gl_vertex_array_info* info = m_vertex_arrays.get( va );
|
---|
[302] | 364 | if ( info )
|
---|
[299] | 365 | {
|
---|
[491] | 366 | glBindVertexArray( info->glid );
|
---|
[299] | 367 | }
|
---|
| 368 | }
|
---|
| 369 |
|
---|
[313] | 370 | void nv::gl_context::unbind( program )
|
---|
| 371 | {
|
---|
[501] | 372 | if ( m_bound_program )
|
---|
| 373 | glUseProgram( 0 );
|
---|
| 374 | m_bound_program = program();
|
---|
[313] | 375 | }
|
---|
| 376 |
|
---|
[492] | 377 | void nv::gl_context::set_active_texture( texture_slot slot )
|
---|
| 378 | {
|
---|
| 379 | if ( slot != m_active_slot )
|
---|
[501] | 380 | {
|
---|
[492] | 381 | glActiveTexture( GL_TEXTURE0 + static_cast<GLenum>( slot ) );
|
---|
[501] | 382 | m_active_slot = slot;
|
---|
| 383 | }
|
---|
[492] | 384 | }
|
---|
| 385 |
|
---|
[313] | 386 | // void nv::gl_context::unbind( buffer b )
|
---|
| 387 | // {
|
---|
| 388 | // const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
|
---|
| 389 | // if ( info )
|
---|
| 390 | // {
|
---|
| 391 | // glBindBuffer( buffer_type_to_enum( info->type ), 0 );
|
---|
| 392 | // }
|
---|
| 393 | // }
|
---|
| 394 |
|
---|
[302] | 395 | void nv::gl_context::unbind( vertex_array va )
|
---|
[299] | 396 | {
|
---|
[313] | 397 | const vertex_array_info* info = m_vertex_arrays.get( va );
|
---|
[302] | 398 | if ( info )
|
---|
[299] | 399 | {
|
---|
[491] | 400 | glBindVertexArray( 0 );
|
---|
[299] | 401 | }
|
---|
| 402 | }
|
---|
| 403 |
|
---|
[313] | 404 | void nv::gl_context::unbind( framebuffer f )
|
---|
| 405 | {
|
---|
| 406 | // this way we are sure that the extension is loaded
|
---|
| 407 | const gl_framebuffer_info* info = m_framebuffers.get( f );
|
---|
| 408 | if ( info )
|
---|
| 409 | {
|
---|
| 410 | glBindFramebuffer( GL_FRAMEBUFFER, 0 );
|
---|
| 411 | }
|
---|
[331] | 412 | glDrawBuffer( GL_BACK );
|
---|
| 413 | glReadBuffer( GL_BACK );
|
---|
[313] | 414 | }
|
---|
| 415 |
|
---|
[406] | 416 | void nv::gl_context::update( texture t, const void* data )
|
---|
[299] | 417 | {
|
---|
[301] | 418 | const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
|
---|
[491] | 419 | NV_ASSERT_ALWAYS( info->type != TEXTURE_1D_BUFFER, "Buffer texture passed to update!" );
|
---|
[492] | 420 | NV_ASSERT_ALWAYS( info->type != TEXTURE_2D_MULTISAMPLE, "Multisample texture passed to update!" );
|
---|
[301] | 421 | if ( info )
|
---|
| 422 | {
|
---|
[331] | 423 | image_format format = info->format;
|
---|
[463] | 424 | ivec3 size = info->size;
|
---|
[331] | 425 | unsigned gl_type = texture_type_to_enum( info->type );
|
---|
[299] | 426 |
|
---|
[501] | 427 | bind( t, texture_slot::TEXTURE_0 );
|
---|
[463] | 428 | if ( info->type == TEXTURE_3D || info->type == TEXTURE_2D_ARRAY )
|
---|
[501] | 429 | // glTexImage3D( gl_type, 0, static_cast<GLint>( nv::image_format_to_internal_enum( format.format ) ), size.x, size.y, size.z, 0, nv::image_format_to_enum( format.format ), nv::datatype_to_gl_enum( format.type ), data );
|
---|
| 430 | glTexSubImage3D( gl_type, 0, 0, 0, 0, size.x, size.y, size.z, nv::image_format_to_enum( format.format ), nv::datatype_to_gl_enum( format.type ), data );
|
---|
[463] | 431 | else
|
---|
[501] | 432 | // glTexImage2D( gl_type, 0, static_cast<GLint>( nv::image_format_to_internal_enum(format.format) ), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data );
|
---|
| 433 | glTexSubImage2D( gl_type, 0, 0, 0, size.x, size.y, nv::image_format_to_enum( format.format ), nv::datatype_to_gl_enum( format.type ), data );
|
---|
[301] | 434 | }
|
---|
[299] | 435 | }
|
---|
| 436 |
|
---|
[499] | 437 | void* nv::gl_context::map_buffer( buffer b, buffer_access ba, size_t offset, size_t length )
|
---|
| 438 | {
|
---|
| 439 | const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
|
---|
| 440 | unsigned int glenum = buffer_type_to_enum( info->type );
|
---|
| 441 | glBindBuffer( glenum, info->glid );
|
---|
| 442 | void* result = glMapBufferRange( glenum, GLintptr( offset ), GLsizeiptr( length ), buffer_access_to_bitfield( ba ) );
|
---|
| 443 | if ( result == nullptr )
|
---|
| 444 | {
|
---|
| 445 | while ( GLenum err = glGetError() )
|
---|
| 446 | switch ( err )
|
---|
| 447 | {
|
---|
| 448 | case GL_INVALID_VALUE : NV_LOG_ERROR( "map_buffer failed : GL_INVALID_VALUE" ) break;
|
---|
| 449 | case GL_INVALID_OPERATION : NV_LOG_ERROR( "map_buffer failed : GL_INVALID_OPERATION " ) break;
|
---|
| 450 | case GL_OUT_OF_MEMORY : NV_LOG_ERROR( "map_buffer failed : GL_OUT_OF_MEMORY " ) break;
|
---|
[500] | 451 | default:
|
---|
[499] | 452 | break;
|
---|
| 453 | }
|
---|
| 454 | }
|
---|
| 455 | return result;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | void nv::gl_context::unmap_buffer( buffer b )
|
---|
| 459 | {
|
---|
| 460 | const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
|
---|
| 461 | glUnmapBuffer( buffer_type_to_enum( info->type ) );
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[485] | 464 | // void nv::gl_context::update( buffer b, uint32 index, const void* data, size_t offset, size_t size )
|
---|
| 465 | // {
|
---|
| 466 | // const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
|
---|
| 467 | // if ( info )
|
---|
| 468 | // {
|
---|
| 469 | // GLenum glenum = buffer_type_to_enum( info->type );
|
---|
| 470 | // if ( size == 0 )
|
---|
| 471 | // glBindBufferBase( glenum, index, info->glid );
|
---|
| 472 | // else
|
---|
| 473 | // glBindBufferRange( glenum, index, info->glid, offset, size );
|
---|
| 474 | // glBufferSubData( glenum, GLintptr( offset ), GLsizeiptr( size ), data );
|
---|
| 475 | // }
|
---|
| 476 | // }
|
---|
[473] | 477 |
|
---|
[376] | 478 | void gl_context::update( buffer b, const void* data, nv::size_t offset, nv::size_t size )
|
---|
[299] | 479 | {
|
---|
[302] | 480 | const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
|
---|
| 481 | if ( info )
|
---|
| 482 | {
|
---|
[313] | 483 | GLenum glenum = buffer_type_to_enum( info->type );
|
---|
| 484 | glBindBuffer( glenum, info->glid );
|
---|
[406] | 485 | glBufferSubData( glenum, GLintptr( offset ), GLsizeiptr( size ), data );
|
---|
[302] | 486 | }
|
---|
[299] | 487 | }
|
---|
| 488 |
|
---|
[37] | 489 | void gl_context::clear( const clear_state& cs )
|
---|
| 490 | {
|
---|
| 491 | // apply_framebuffer
|
---|
[44] | 492 |
|
---|
[37] | 493 | apply_scissor_test( cs.scissor_test );
|
---|
| 494 | apply_color_mask( cs.color_mask );
|
---|
| 495 | apply_depth_mask( cs.depth_mask );
|
---|
| 496 | // stencil_mask_separate
|
---|
| 497 |
|
---|
| 498 | if ( m_clear_color != cs.color )
|
---|
| 499 | {
|
---|
| 500 | glClearColor( cs.color.r, cs.color.g, cs.color.b, cs.color.a );
|
---|
| 501 | m_clear_color = cs.color;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | if ( m_clear_depth != cs.depth )
|
---|
| 505 | {
|
---|
| 506 | glClearDepth( cs.depth );
|
---|
| 507 | m_clear_depth = cs.depth;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | if ( m_clear_stencil != cs.stencil )
|
---|
| 511 | {
|
---|
| 512 | glClearStencil( cs.stencil );
|
---|
| 513 | m_clear_stencil = cs.stencil;
|
---|
| 514 | }
|
---|
[44] | 515 |
|
---|
| 516 | glClear( clear_state_buffers_to_mask( cs.buffers ) );
|
---|
[37] | 517 | }
|
---|
| 518 |
|
---|
| 519 | const ivec4& gl_context::get_viewport()
|
---|
| 520 | {
|
---|
| 521 | return m_viewport;
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | void gl_context::set_viewport( const ivec4& viewport )
|
---|
| 525 | {
|
---|
[501] | 526 | if ( m_viewport != viewport )
|
---|
| 527 | {
|
---|
| 528 | NV_ASSERT_ALWAYS( viewport.z > 0 && viewport.w > 0, "viewport dimensions must be greater than zero!" );
|
---|
| 529 | m_viewport = viewport;
|
---|
| 530 | glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
|
---|
| 531 | }
|
---|
[37] | 532 | }
|
---|
| 533 |
|
---|
| 534 | void gl_context::enable( unsigned int what, bool value )
|
---|
| 535 | {
|
---|
| 536 | if ( value )
|
---|
| 537 | {
|
---|
| 538 | glEnable( what );
|
---|
| 539 | }
|
---|
| 540 | else
|
---|
| 541 | {
|
---|
| 542 | glDisable( what );
|
---|
| 543 | }
|
---|
| 544 | }
|
---|
| 545 |
|
---|
| 546 | void gl_context::apply_stencil_test( const stencil_test& stencil )
|
---|
| 547 | {
|
---|
| 548 | if ( m_render_state.stencil_test.enabled != stencil.enabled )
|
---|
| 549 | {
|
---|
| 550 | enable( GL_STENCIL_TEST, stencil.enabled );
|
---|
| 551 | m_render_state.stencil_test.enabled = stencil.enabled;
|
---|
| 552 | }
|
---|
[331] | 553 |
|
---|
[37] | 554 | if ( stencil.enabled )
|
---|
| 555 | {
|
---|
| 556 | apply_stencil_face( GL_FRONT, m_render_state.stencil_test.front_face, stencil.front_face );
|
---|
| 557 | apply_stencil_face( GL_BACK, m_render_state.stencil_test.back_face, stencil.back_face );
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[121] | 561 | void gl_context::apply_stencil_face( unsigned face, stencil_test_face& stencil, const stencil_test_face& new_stencil )
|
---|
[37] | 562 | {
|
---|
| 563 | if (( stencil.op_fail != new_stencil.op_fail ) ||
|
---|
| 564 | ( stencil.op_depth_fail != new_stencil.op_depth_fail ) ||
|
---|
| 565 | ( stencil.op_depth_pass != new_stencil.op_depth_pass ) )
|
---|
| 566 | {
|
---|
| 567 | glStencilOpSeparate( face,
|
---|
| 568 | stencil_operation_to_enum( new_stencil.op_fail ),
|
---|
| 569 | stencil_operation_to_enum( new_stencil.op_depth_fail ),
|
---|
| 570 | stencil_operation_to_enum( new_stencil.op_depth_pass )
|
---|
| 571 | );
|
---|
| 572 |
|
---|
| 573 | stencil.op_fail = new_stencil.op_fail;
|
---|
| 574 | stencil.op_depth_fail = new_stencil.op_depth_fail;
|
---|
| 575 | stencil.op_depth_pass = new_stencil.op_depth_pass;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | if (( stencil.function != new_stencil.function ) ||
|
---|
| 579 | ( stencil.ref_value != new_stencil.ref_value ) ||
|
---|
| 580 | ( stencil.mask != new_stencil.mask ))
|
---|
| 581 | {
|
---|
| 582 | glStencilFuncSeparate( face,
|
---|
| 583 | stencil_function_to_enum( new_stencil.function ),
|
---|
| 584 | new_stencil.ref_value,
|
---|
| 585 | new_stencil.mask
|
---|
| 586 | );
|
---|
| 587 |
|
---|
| 588 | stencil.function = new_stencil.function;
|
---|
| 589 | stencil.ref_value = new_stencil.ref_value;
|
---|
| 590 | stencil.mask = new_stencil.mask;
|
---|
| 591 | }
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | void gl_context::apply_scissor_test( const scissor_test& scissor )
|
---|
| 595 | {
|
---|
| 596 | if ( m_render_state.scissor_test.enabled != scissor.enabled )
|
---|
| 597 | {
|
---|
| 598 | enable( GL_SCISSOR_TEST, scissor.enabled );
|
---|
| 599 | m_render_state.scissor_test.enabled = scissor.enabled;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 |
|
---|
| 603 | if ( scissor.enabled )
|
---|
| 604 | {
|
---|
[403] | 605 | NV_ASSERT_ALWAYS( scissor.dim.x > 0 && scissor.dim.y > 0, "scissor_test.rect dimension equal to zero!" );
|
---|
| 606 |
|
---|
[37] | 607 | if ( m_render_state.scissor_test.dim != scissor.dim || m_render_state.scissor_test.pos != scissor.pos )
|
---|
| 608 | {
|
---|
| 609 | glScissor(
|
---|
| 610 | scissor.pos.x, scissor.pos.y,
|
---|
| 611 | scissor.dim.x, scissor.dim.y
|
---|
| 612 | );
|
---|
| 613 | m_render_state.scissor_test.dim = scissor.dim;
|
---|
| 614 | m_render_state.scissor_test.pos = scissor.pos;
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | void gl_context::apply_depth_test( const depth_test& depth )
|
---|
| 620 | {
|
---|
| 621 | if ( m_render_state.depth_test.enabled != depth.enabled )
|
---|
| 622 | {
|
---|
| 623 | enable( GL_DEPTH_TEST, depth.enabled );
|
---|
| 624 | m_render_state.depth_test.enabled = depth.enabled;
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | if ( depth.enabled )
|
---|
| 628 | {
|
---|
| 629 | if ( m_render_state.depth_test.function != depth.function )
|
---|
| 630 | {
|
---|
| 631 | glDepthFunc( depth_state_function_to_enum( depth.function ) );
|
---|
| 632 | m_render_state.depth_test.function = depth.function;
|
---|
| 633 | }
|
---|
| 634 | }
|
---|
| 635 | }
|
---|
| 636 |
|
---|
| 637 | void gl_context::apply_depth_mask( bool mask )
|
---|
| 638 | {
|
---|
| 639 | if ( m_render_state.depth_mask != mask )
|
---|
| 640 | {
|
---|
| 641 | glDepthMask( mask );
|
---|
| 642 | m_render_state.depth_mask = mask;
|
---|
| 643 | }
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[492] | 646 | void gl_context::apply_multisample( bool multisample )
|
---|
| 647 | {
|
---|
| 648 | if ( m_render_state.multisample != multisample )
|
---|
| 649 | {
|
---|
| 650 | glDepthMask( multisample );
|
---|
| 651 | m_render_state.multisample = multisample;
|
---|
| 652 | }
|
---|
| 653 | }
|
---|
| 654 |
|
---|
[233] | 655 | void gl_context::apply_polygon_mode( const polygon_mode& mode )
|
---|
| 656 | {
|
---|
| 657 | if ( m_render_state.polygon_mode.fill != mode.fill )
|
---|
| 658 | {
|
---|
| 659 | glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( mode.fill ) );
|
---|
| 660 | m_render_state.polygon_mode.fill = mode.fill;
|
---|
| 661 | }
|
---|
| 662 | }
|
---|
| 663 |
|
---|
| 664 |
|
---|
[37] | 665 | void gl_context::apply_depth_range( const depth_range& range )
|
---|
| 666 | {
|
---|
[403] | 667 | NV_ASSERT_ALWAYS( range.near >= 0.0 && range.near <= 1.0, "render_state.depth_range.near must be between zero and one!" );
|
---|
| 668 | NV_ASSERT_ALWAYS( range.far >= 0.0 && range.far <= 1.0, "render_state.depth_range.far must be between zero and one!" );
|
---|
[37] | 669 |
|
---|
| 670 | if ((m_render_state.depth_range.far != range.far) ||
|
---|
| 671 | (m_render_state.depth_range.near != range.near))
|
---|
| 672 | {
|
---|
| 673 | glDepthRange( range.near, range.far );
|
---|
| 674 |
|
---|
| 675 | m_render_state.depth_range.far = range.far;
|
---|
| 676 | m_render_state.depth_range.near = range.near;
|
---|
| 677 | }
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | void gl_context::apply_color_mask( const color_mask& mask )
|
---|
| 681 | {
|
---|
| 682 | if ( m_render_state.color_mask != mask )
|
---|
| 683 | {
|
---|
| 684 | glColorMask( mask.red, mask.green, mask.blue, mask.alpha );
|
---|
| 685 | m_render_state.color_mask = mask;
|
---|
| 686 | }
|
---|
| 687 | }
|
---|
| 688 |
|
---|
| 689 | void gl_context::apply_blending( const blending& blend )
|
---|
| 690 | {
|
---|
| 691 | if ( m_render_state.blending.enabled != blend.enabled )
|
---|
| 692 | {
|
---|
| 693 | enable( GL_BLEND, blend.enabled );
|
---|
| 694 | m_render_state.blending.enabled = blend.enabled;
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | if ( blend.enabled )
|
---|
| 698 | {
|
---|
| 699 | if ((m_render_state.blending.src_rgb_factor != blend.src_rgb_factor ) ||
|
---|
| 700 | (m_render_state.blending.dst_rgb_factor != blend.dst_rgb_factor ) ||
|
---|
| 701 | (m_render_state.blending.src_alpha_factor != blend.src_alpha_factor ) ||
|
---|
| 702 | (m_render_state.blending.dst_alpha_factor != blend.dst_alpha_factor ))
|
---|
| 703 | {
|
---|
| 704 | glBlendFuncSeparate(
|
---|
| 705 | blending_factor_to_enum( blend.src_rgb_factor ),
|
---|
| 706 | blending_factor_to_enum( blend.dst_rgb_factor ),
|
---|
| 707 | blending_factor_to_enum( blend.src_alpha_factor ),
|
---|
| 708 | blending_factor_to_enum( blend.dst_alpha_factor )
|
---|
| 709 | );
|
---|
| 710 |
|
---|
| 711 | m_render_state.blending.src_rgb_factor = blend.src_rgb_factor;
|
---|
| 712 | m_render_state.blending.dst_rgb_factor = blend.dst_rgb_factor;
|
---|
| 713 | m_render_state.blending.src_alpha_factor = blend.src_alpha_factor;
|
---|
| 714 | m_render_state.blending.dst_alpha_factor = blend.dst_alpha_factor;
|
---|
| 715 | }
|
---|
| 716 |
|
---|
| 717 | if ((m_render_state.blending.rgb_equation != blend.rgb_equation ) ||
|
---|
| 718 | (m_render_state.blending.alpha_equation != blend.alpha_equation ))
|
---|
| 719 | {
|
---|
| 720 | glBlendEquationSeparate(
|
---|
| 721 | blending_equation_to_enum( blend.rgb_equation ),
|
---|
| 722 | blending_equation_to_enum( blend.alpha_equation )
|
---|
| 723 | );
|
---|
| 724 |
|
---|
| 725 | m_render_state.blending.rgb_equation = blend.rgb_equation;
|
---|
| 726 | m_render_state.blending.alpha_equation = blend.alpha_equation;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | if (( m_render_state.blending.color != blend.color ))
|
---|
| 730 | {
|
---|
| 731 | glBlendColor( blend.color.r, blend.color.g, blend.color.b, blend.color.a );
|
---|
| 732 | m_render_state.blending.color = blend.color;
|
---|
| 733 | }
|
---|
| 734 | }
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 |
|
---|
| 738 | void gl_context::apply_culling( const culling& cull )
|
---|
| 739 | {
|
---|
| 740 | if ( m_render_state.culling.enabled != cull.enabled )
|
---|
| 741 | {
|
---|
| 742 | enable( GL_CULL_FACE, cull.enabled );
|
---|
| 743 | m_render_state.culling.enabled = cull.enabled;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
| 746 | if ( cull.enabled )
|
---|
| 747 | {
|
---|
| 748 | if ( m_render_state.culling.face != cull.face )
|
---|
| 749 | {
|
---|
[462] | 750 | glCullFace( culling_face_type_to_enum( cull.face ) );
|
---|
[37] | 751 | m_render_state.culling.face = cull.face;
|
---|
| 752 | }
|
---|
[462] | 753 | }
|
---|
[37] | 754 |
|
---|
[462] | 755 | if ( m_render_state.culling.order != cull.order )
|
---|
| 756 | {
|
---|
| 757 | glFrontFace( culling_order_type_to_enum( cull.order ) );
|
---|
| 758 | m_render_state.culling.order = cull.order;
|
---|
[37] | 759 | }
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 |
|
---|
| 763 | void gl_context::force_apply_render_state( const render_state& state )
|
---|
| 764 | {
|
---|
| 765 | enable( GL_CULL_FACE, state.culling.enabled );
|
---|
| 766 | glCullFace( culling_face_type_to_enum( state.culling.face ) );
|
---|
| 767 | glFrontFace( culling_order_type_to_enum( state.culling.order ) );
|
---|
| 768 |
|
---|
| 769 | enable( GL_SCISSOR_TEST, state.scissor_test.enabled );
|
---|
| 770 | glScissor(
|
---|
| 771 | state.scissor_test.pos.x, state.scissor_test.pos.y,
|
---|
| 772 | state.scissor_test.dim.x, state.scissor_test.dim.y
|
---|
| 773 | );
|
---|
| 774 |
|
---|
| 775 | enable( GL_STENCIL_TEST, state.stencil_test.enabled );
|
---|
| 776 | force_apply_stencil_face( GL_FRONT, state.stencil_test.front_face );
|
---|
| 777 | force_apply_stencil_face( GL_BACK, state.stencil_test.back_face );
|
---|
| 778 |
|
---|
| 779 | enable( GL_DEPTH_TEST, state.depth_test.enabled );
|
---|
| 780 | glDepthFunc( depth_state_function_to_enum( state.depth_test.function ) );
|
---|
| 781 | glDepthRange( state.depth_range.near, state.depth_range.far );
|
---|
| 782 |
|
---|
| 783 | enable( GL_BLEND, state.blending.enabled );
|
---|
| 784 | glBlendFuncSeparate(
|
---|
| 785 | blending_factor_to_enum( state.blending.src_rgb_factor ),
|
---|
| 786 | blending_factor_to_enum( state.blending.dst_rgb_factor ),
|
---|
| 787 | blending_factor_to_enum( state.blending.src_alpha_factor ),
|
---|
| 788 | blending_factor_to_enum( state.blending.dst_alpha_factor )
|
---|
| 789 | );
|
---|
| 790 | glBlendEquationSeparate(
|
---|
| 791 | blending_equation_to_enum( state.blending.rgb_equation ),
|
---|
| 792 | blending_equation_to_enum( state.blending.alpha_equation )
|
---|
| 793 | );
|
---|
| 794 | glBlendColor(
|
---|
| 795 | state.blending.color.r, state.blending.color.g,
|
---|
| 796 | state.blending.color.b, state.blending.color.a
|
---|
| 797 | );
|
---|
| 798 |
|
---|
| 799 | glDepthMask( state.depth_mask );
|
---|
| 800 | glColorMask(
|
---|
| 801 | state.color_mask.red, state.color_mask.green,
|
---|
| 802 | state.color_mask.blue, state.color_mask.alpha
|
---|
| 803 | );
|
---|
[233] | 804 | glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( state.polygon_mode.fill ) );
|
---|
[37] | 805 | }
|
---|
| 806 |
|
---|
[121] | 807 | void gl_context::force_apply_stencil_face( unsigned face, const stencil_test_face& stencil )
|
---|
[37] | 808 | {
|
---|
| 809 | glStencilOpSeparate( face,
|
---|
| 810 | stencil_operation_to_enum( stencil.op_fail ),
|
---|
| 811 | stencil_operation_to_enum( stencil.op_depth_fail ),
|
---|
| 812 | stencil_operation_to_enum( stencil.op_depth_pass )
|
---|
| 813 | );
|
---|
| 814 |
|
---|
| 815 | glStencilFuncSeparate( face,
|
---|
| 816 | stencil_function_to_enum( stencil.function ),
|
---|
| 817 | stencil.ref_value,
|
---|
| 818 | stencil.mask
|
---|
| 819 | );
|
---|
| 820 | }
|
---|
| 821 |
|
---|
| 822 |
|
---|
| 823 | void gl_context::apply_render_state( const render_state& state )
|
---|
| 824 | {
|
---|
| 825 | // apply_primitive_restart
|
---|
| 826 | apply_culling( state.culling );
|
---|
| 827 | // apply_program_point_size
|
---|
| 828 | // apply_rasterization_mode
|
---|
| 829 | apply_scissor_test( state.scissor_test );
|
---|
| 830 | apply_stencil_test( state.stencil_test );
|
---|
| 831 | apply_depth_test( state.depth_test );
|
---|
| 832 | apply_depth_range( state.depth_range );
|
---|
| 833 | apply_blending( state.blending );
|
---|
| 834 | apply_color_mask( state.color_mask );
|
---|
| 835 | apply_depth_mask( state.depth_mask );
|
---|
[492] | 836 | apply_multisample( state.multisample );
|
---|
[233] | 837 | apply_polygon_mode( state.polygon_mode );
|
---|
[37] | 838 | }
|
---|
| 839 |
|
---|
[44] | 840 |
|
---|
[326] | 841 | gl_context::gl_context( device* a_device, void* a_handle )
|
---|
| 842 | : context( a_device ), m_handle( a_handle )
|
---|
[44] | 843 | {
|
---|
[331] | 844 | // TODO: configurable:
|
---|
[491] | 845 | // load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT | GL_EXT_TEXTURE_ARRAY );
|
---|
[492] | 846 | m_active_slot = texture_slot( -1 );
|
---|
[326] | 847 | force_apply_render_state( m_render_state );
|
---|
[245] | 848 | }
|
---|
| 849 |
|
---|
| 850 |
|
---|
| 851 | nv::gl_context::~gl_context()
|
---|
| 852 | {
|
---|
[313] | 853 | while ( m_framebuffers.size() > 0 )
|
---|
| 854 | release( m_framebuffers.get_handle(0) );
|
---|
| 855 | while ( m_vertex_arrays.size() > 0 )
|
---|
| 856 | release( m_vertex_arrays.get_handle(0) );
|
---|
[245] | 857 | }
|
---|
| 858 |
|
---|
[303] | 859 | void nv::gl_context::apply_engine_uniforms( program p, const scene_state& s )
|
---|
[245] | 860 | {
|
---|
[406] | 861 | gl_program_info* info = static_cast<gl_device*>( m_device )->m_programs.get( p );
|
---|
[303] | 862 | if ( info )
|
---|
| 863 | {
|
---|
[392] | 864 | for ( auto& u : *info->m_engine_uniforms )
|
---|
[303] | 865 | {
|
---|
| 866 | u->set( this, &s );
|
---|
| 867 | }
|
---|
| 868 | }
|
---|
| 869 | }
|
---|
| 870 |
|
---|
[501] | 871 | nv::texture nv::gl_context::create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
| 872 | {
|
---|
| 873 | texture result = create_texture( type, aformat.format );
|
---|
| 874 | gl_texture_info* info = static_cast<gl_device*>( m_device )->get_full_texture_info( result );
|
---|
| 875 | bind( result, texture_slot::TEXTURE_0 );
|
---|
| 876 | unsigned glid = info->glid;
|
---|
| 877 | unsigned gl_type = texture_type_to_enum( type );
|
---|
| 878 | GLenum gl_internal = GLenum( image_format_to_internal_enum( aformat.format ) );
|
---|
| 879 | unsigned gl_enum = image_format_to_enum( aformat.format );
|
---|
| 880 |
|
---|
| 881 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 882 |
|
---|
| 883 | // Detect if mipmapping was requested
|
---|
| 884 | // if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
| 885 | // {
|
---|
| 886 | // // TODO: This should not be done if we use framebuffers!
|
---|
| 887 | // glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
| 888 | // }
|
---|
| 889 |
|
---|
| 890 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
| 891 | {
|
---|
| 892 | asampler.filter_max = sampler::LINEAR;
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE )
|
---|
| 896 | {
|
---|
| 897 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 898 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 | if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE && gl_enum != GL_RED_INTEGER )
|
---|
| 902 | {
|
---|
| 903 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
| 904 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
| 905 | }
|
---|
| 906 |
|
---|
| 907 | if ( is_depth )
|
---|
| 908 | {
|
---|
| 909 | // glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 910 | // glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
| 911 |
|
---|
| 912 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 913 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 914 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | // #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
|
---|
| 918 | // #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
|
---|
| 919 | //
|
---|
| 920 | // float aniso = 0.0f;
|
---|
| 921 | // glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso );
|
---|
| 922 | // NV_LOG_INFO( "Anisotropy at ", aniso, " (", int( aniso ), " ) " );
|
---|
| 923 | // glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso );
|
---|
| 924 |
|
---|
| 925 | if ( gl_type != GL_TEXTURE_2D_MULTISAMPLE )
|
---|
| 926 | glTexImage2D( gl_type, 0, gl_internal, size.x, size.y, 0, gl_enum, nv::datatype_to_gl_enum( aformat.type ), data );
|
---|
| 927 | else
|
---|
| 928 | glTexImage2DMultisample( gl_type, 4, gl_internal, size.x, size.y, 1 );
|
---|
| 929 |
|
---|
| 930 | if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
| 931 | {
|
---|
| 932 | // TODO: This should not be done if we use framebuffers!
|
---|
| 933 | glGenerateMipmap( gl_type );
|
---|
| 934 | }
|
---|
| 935 |
|
---|
| 936 | bind( texture(), texture_slot::TEXTURE_0 );
|
---|
| 937 |
|
---|
| 938 | info->type = type;
|
---|
| 939 | info->format = aformat;
|
---|
| 940 | info->tsampler = asampler;
|
---|
| 941 | info->size = ivec3( size.x, size.y, 1 );
|
---|
| 942 | info->glid = glid;
|
---|
| 943 |
|
---|
| 944 | return result;
|
---|
| 945 | }
|
---|
| 946 |
|
---|
| 947 | nv::texture nv::gl_context::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
| 948 | {
|
---|
| 949 | texture result = create_texture( type, aformat.format );
|
---|
| 950 | gl_texture_info* info = static_cast<gl_device*>( m_device )->get_full_texture_info( result );
|
---|
| 951 | bind( result, texture_slot::TEXTURE_0 );
|
---|
| 952 | unsigned glid = info->glid;
|
---|
| 953 |
|
---|
| 954 | unsigned gl_type = texture_type_to_enum( type );
|
---|
| 955 |
|
---|
| 956 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
| 957 |
|
---|
| 958 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
| 959 | {
|
---|
| 960 | asampler.filter_max = sampler::LINEAR;
|
---|
| 961 | }
|
---|
| 962 |
|
---|
| 963 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
| 964 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
| 965 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
| 966 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
| 967 |
|
---|
| 968 | if ( is_depth )
|
---|
| 969 | {
|
---|
| 970 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
| 971 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
| 972 |
|
---|
| 973 | // This is to allow usage of shadow2DProj function in the shader
|
---|
| 974 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
| 975 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | //glTexStorage3D( GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount );
|
---|
| 979 | 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 );
|
---|
| 980 |
|
---|
| 981 | bind( texture(), texture_slot::TEXTURE_0 );
|
---|
| 982 |
|
---|
| 983 | info->type = type;
|
---|
| 984 | info->format = aformat;
|
---|
| 985 | info->tsampler = asampler;
|
---|
| 986 | info->size = size;
|
---|
| 987 | info->glid = glid;
|
---|
| 988 |
|
---|
| 989 | return result;
|
---|
| 990 | }
|
---|
| 991 |
|
---|
| 992 | nv::buffer nv::gl_context::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
| 993 | {
|
---|
| 994 | buffer result = static_cast<gl_device*>( m_device )->create_buffer( type, hint );
|
---|
| 995 | if ( size > 0 ) create_buffer( result, size, source );
|
---|
| 996 | return result;
|
---|
| 997 | }
|
---|
| 998 |
|
---|
| 999 | void nv::gl_context::create_buffer( buffer b, size_t size, const void* source /*= nullptr */ )
|
---|
| 1000 | {
|
---|
| 1001 | gl_buffer_info* info = static_cast<gl_device*>( m_device )->get_full_buffer_info( b );
|
---|
| 1002 | if ( info )
|
---|
| 1003 | {
|
---|
| 1004 | unsigned glenum = buffer_type_to_enum( info->type );
|
---|
| 1005 | glBindBuffer( glenum, info->glid );
|
---|
| 1006 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( info->hint ) );
|
---|
| 1007 | glBindBuffer( glenum, 0 );
|
---|
| 1008 | }
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
[342] | 1011 | void nv::gl_context::set_draw_buffers( uint32 count, const output_slot* slots )
|
---|
[331] | 1012 | {
|
---|
[342] | 1013 | if ( count == 0 ) return;
|
---|
| 1014 | if ( count == 1 )
|
---|
| 1015 | {
|
---|
| 1016 | set_draw_buffer( slots[0] );
|
---|
| 1017 | return;
|
---|
| 1018 | }
|
---|
[331] | 1019 | unsigned int buffers[8];
|
---|
[398] | 1020 | count = nv::min<uint32>( count, 8 );
|
---|
[331] | 1021 | for ( uint32 i = 0; i < count; ++i )
|
---|
| 1022 | {
|
---|
| 1023 | buffers[i] = output_slot_to_enum( slots[i] );
|
---|
| 1024 | if ( slots[i] > OUTPUT_7 ) buffers[i] = 0;
|
---|
| 1025 | }
|
---|
[406] | 1026 | glDrawBuffers( GLsizei( count ), buffers );
|
---|
[331] | 1027 | }
|
---|
| 1028 |
|
---|
| 1029 | void nv::gl_context::set_draw_buffer( output_slot slot )
|
---|
| 1030 | {
|
---|
| 1031 | glDrawBuffer( output_slot_to_enum(slot) );
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
| 1034 | void nv::gl_context::set_read_buffer( output_slot slot )
|
---|
| 1035 | {
|
---|
| 1036 | glReadBuffer( output_slot_to_enum(slot) );
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
[473] | 1039 | void gl_context::draw( primitive prim, const render_state& rs, program p, vertex_array va, nv::size_t count, nv::size_t first )
|
---|
[303] | 1040 | {
|
---|
[245] | 1041 | apply_render_state( rs );
|
---|
[313] | 1042 | const vertex_array_info* info = m_vertex_arrays.get( va );
|
---|
[302] | 1043 | if ( count > 0 && info )
|
---|
[245] | 1044 | {
|
---|
[299] | 1045 | bind( p );
|
---|
| 1046 | bind( va );
|
---|
[302] | 1047 | if ( info->index.is_valid() )
|
---|
[245] | 1048 | {
|
---|
[473] | 1049 | glDrawElements( primitive_to_enum(prim), static_cast<GLsizei>( count ), datatype_to_gl_enum( info->index_type ), reinterpret_cast< const void* >( get_datatype_info( info->index_type ).size * first ) );
|
---|
[245] | 1050 | }
|
---|
| 1051 | else
|
---|
| 1052 | {
|
---|
[487] | 1053 | glDrawArrays( primitive_to_enum(prim), static_cast<GLint>( first ), static_cast<GLsizei>( count ) );
|
---|
[245] | 1054 | }
|
---|
[299] | 1055 | unbind( va );
|
---|
| 1056 | //unbind( p );
|
---|
[245] | 1057 | }
|
---|
| 1058 | }
|
---|