// Copyright (C) 2012-2014 ChaosForge Ltd // This file is part of NV Libraries. // For conditions of distribution and use, see copyright notice in nv.hh #include "nv/gl/gl_context.hh" #include "nv/gl/gl_enum.hh" #include "nv/lib/gl.hh" #include "nv/gl/gl_device.hh" using namespace nv; nv::vertex_array nv::gl_context::create_vertex_array() { vertex_array result = m_vertex_arrays.create(); vertex_array_info* info = m_vertex_arrays.get( result ); info->count = 0; info->index = buffer(); info->index_owner = false; info->index_type = USHORT; return result; } nv::framebuffer nv::gl_context::create_framebuffer() { if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) && is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_BLIT ) ) { unsigned glid = 0; glGenFramebuffers( 1, &glid ); framebuffer result = m_framebuffers.create(); gl_framebuffer_info* info = m_framebuffers.get( result ); info->glid = glid; info->depth_rb_glid = 0; info->color_attachment_count = 0; return result; } else return framebuffer(); } void nv::gl_context::release( vertex_array va ) { vertex_array_info* info = m_vertex_arrays.get( va ); if ( info ) { for ( uint32 i = 0; i < info->count; ++i ) { if ( info->attr[i].owner ) m_device->release( info->attr[i].vbuffer ); } if ( info->index.is_valid() && info->index_owner) m_device->release( info->index ); m_vertex_arrays.destroy( va ); } } void nv::gl_context::release( framebuffer f ) { gl_framebuffer_info* info = m_framebuffers.get( f ); if ( info ) { // TODO: release textures? glBindFramebuffer( GL_FRAMEBUFFER, 0 ); glBindRenderbuffer( GL_RENDERBUFFER, 0 ); if ( info->depth_rb_glid == 0 ) glDeleteRenderbuffers( 1, &info->depth_rb_glid ); glDeleteFramebuffers( 1, &info->glid ); m_framebuffers.destroy( f ); } } const vertex_array_info* nv::gl_context::get_vertex_array_info( vertex_array va ) const { return m_vertex_arrays.get( va ); } const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const { return m_framebuffers.get( f ); } vertex_array_info* nv::gl_context::get_vertex_array_info_mutable( vertex_array va ) { return m_vertex_arrays.get( va ); } void nv::gl_context::attach( framebuffer f, output_slot slot, texture t ) { // TODO: framebuffer variable, so no re-binding? // TODO: support 1d, 3d textures, cubemaps or layers? // TODO: support GL_READ_FRAMEBUFFER? const gl_framebuffer_info* info = m_framebuffers.get( f ); const gl_texture_info* tinfo = (gl_texture_info*)m_device->get_texture_info( t ); if ( info ) { glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); unsigned gl_type = texture_type_to_enum( tinfo->type ); if ( tinfo ) { // if ( tinfo->size.y == 0 ) // glFramebufferTexture1D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, GL_TEXTURE_1D, tinfo->glid, 0 ); glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, gl_type, tinfo->glid, 0 ); } else { glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, gl_type, 0, 0 ); } } } void nv::gl_context::attach( framebuffer f, texture depth ) { // TODO: framebuffer variable, so no re-binding? // TODO: support GL_READ_FRAMEBUFFER? const gl_framebuffer_info* info = m_framebuffers.get( f ); const gl_texture_info* tinfo = (gl_texture_info*)m_device->get_texture_info( depth ); if ( info ) { glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); unsigned gl_type = texture_type_to_enum( tinfo->type ); if ( tinfo ) { glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, tinfo->glid, 0 ); } else { glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, 0, 0 ); } } } void nv::gl_context::attach( framebuffer f, ivec2 size ) { // TODO: framebuffer variable, so no re-binding? // TODO: support GL_READ_FRAMEBUFFER? gl_framebuffer_info* info = m_framebuffers.get( f ); if ( info ) { glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); if ( info->depth_rb_glid ) glDeleteRenderbuffers( 1, &(info->depth_rb_glid) ); glGenRenderbuffers( 1, &(info->depth_rb_glid) ); glBindRenderbuffer( GL_RENDERBUFFER, info->depth_rb_glid ); glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.x, size.y ); glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, info->depth_rb_glid ); glBindRenderbuffer( GL_RENDERBUFFER, 0 ); } } void nv::gl_context::blit( framebuffer f, clear_state::buffers_type mask, ivec2 src1, ivec2 src2, ivec2 dst1, ivec2 dst2 ) { gl_framebuffer_info* info = m_framebuffers.get( f ); if ( info ) { glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST; glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter ); } } bool nv::gl_context::check( framebuffer_slot ft ) { if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) && is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_BLIT ) ) { unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) ); if ( result == GL_FRAMEBUFFER_COMPLETE ) return true; switch ( result ) { case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete attachment!" ); break; case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer missing attachment!" ); break; case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete dimensions!" ); break; case GL_FRAMEBUFFER_INCOMPLETE_FORMATS : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete formats!" ); break; case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete draw buffer!" ); break; case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete read buffer!" ); break; case GL_FRAMEBUFFER_UNSUPPORTED : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer format combination unsupported!" ); break; default : NV_LOG( LOG_ERROR, "gl_context::check : Unknown Framebuffer error! (" << result << ")" ); break; } } else { NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer extensions not loaded!" ); } return false; } void nv::gl_context::bind( framebuffer f, framebuffer_slot ft /*= FRAMEBUFFER_BOTH */ ) { const gl_framebuffer_info* info = m_framebuffers.get( f ); if ( info ) { glBindFramebuffer( framebuffer_slot_to_enum(ft), info->glid ); } else { glBindFramebuffer( framebuffer_slot_to_enum(ft), 0 ); } } void gl_context::bind( texture t, texture_slot slot ) { const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) ); if ( info ) { glActiveTexture( GL_TEXTURE0 + static_cast< GLenum >( slot ) ); glBindTexture( texture_type_to_enum( info->type ), info->glid ); } } void nv::gl_context::bind( program p ) { gl_program_info* info = ((gl_device*)m_device)->m_programs.get( p ); if ( info ) { glUseProgram( info->glid ); ((gl_device*)m_device)->update_uniforms( info ); } } // void nv::gl_context::bind( buffer b ) // { // const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) ); // if ( info ) // { // glBindBuffer( buffer_type_to_enum( info->type ), info->glid ); // } // } void nv::gl_context::bind( vertex_array va ) { const vertex_array_info* info = m_vertex_arrays.get( va ); if ( info ) { for ( uint32 i = 0; i < info->count; ++i ) { const vertex_buffer_attribute& vba = info->attr[i]; uint32 location = static_cast( vba.location ); glEnableVertexAttribArray( location ); const gl_buffer_info* vinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( vba.vbuffer ) ); if ( vinfo && vinfo->type == VERTEX_BUFFER ) glBindBuffer( GL_ARRAY_BUFFER, vinfo->glid ); else { // TODO: report error } glVertexAttribPointer( location, static_cast( vba.components ), nv::datatype_to_gl_enum( vba.dtype ), GL_FALSE, static_cast( vba.stride ), (void*)vba.offset ); } glBindBuffer( GL_ARRAY_BUFFER, 0 ); if ( info->index.is_valid() ) { const gl_buffer_info* iinfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( info->index ) ); if ( iinfo && iinfo->type == INDEX_BUFFER ) { glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, iinfo->glid ); } else { // TODO: report error } } } } void nv::gl_context::unbind( program ) { glUseProgram( 0 ); } // void nv::gl_context::unbind( buffer b ) // { // const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) ); // if ( info ) // { // glBindBuffer( buffer_type_to_enum( info->type ), 0 ); // } // } void nv::gl_context::unbind( vertex_array va ) { const vertex_array_info* info = m_vertex_arrays.get( va ); if ( info ) { if ( info->index.is_valid() ) { glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } for ( uint32 i = 0; i < info->count; ++i ) { glDisableVertexAttribArray( static_cast( info->attr[i].location ) ); } } } void nv::gl_context::unbind( framebuffer f ) { // this way we are sure that the extension is loaded const gl_framebuffer_info* info = m_framebuffers.get( f ); if ( info ) { glBindFramebuffer( GL_FRAMEBUFFER, 0 ); } glDrawBuffer( GL_BACK ); glReadBuffer( GL_BACK ); } void nv::gl_context::update( texture t, void* data ) { const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) ); if ( info ) { image_format format = info->format; ivec2 size = info->size; unsigned gl_type = texture_type_to_enum( info->type ); glBindTexture( gl_type, info->glid ); glTexImage2D( gl_type, 0, (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 ); } } void gl_context::update( buffer b, const void* data, size_t offset, size_t size ) { const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) ); if ( info ) { GLenum glenum = buffer_type_to_enum( info->type ); glBindBuffer( glenum, info->glid ); glBufferSubData( glenum, (GLintptr)offset, (GLsizeiptr)size, data ); } } void gl_context::clear( const clear_state& cs ) { // apply_framebuffer apply_scissor_test( cs.scissor_test ); apply_color_mask( cs.color_mask ); apply_depth_mask( cs.depth_mask ); // stencil_mask_separate if ( m_clear_color != cs.color ) { glClearColor( cs.color.r, cs.color.g, cs.color.b, cs.color.a ); m_clear_color = cs.color; } if ( m_clear_depth != cs.depth ) { glClearDepth( cs.depth ); m_clear_depth = cs.depth; } if ( m_clear_stencil != cs.stencil ) { glClearStencil( cs.stencil ); m_clear_stencil = cs.stencil; } glClear( clear_state_buffers_to_mask( cs.buffers ) ); } const ivec4& gl_context::get_viewport() { return m_viewport; } void gl_context::set_viewport( const ivec4& viewport ) { if ( viewport.z < 0 || viewport.w < 0 ) { NV_THROW( logic_error, "viewport width and height must be greater than zero!"); } m_viewport = viewport; glViewport( viewport.x, viewport.y, viewport.z, viewport.w ); } void gl_context::enable( unsigned int what, bool value ) { if ( value ) { glEnable( what ); } else { glDisable( what ); } } void gl_context::apply_stencil_test( const stencil_test& stencil ) { if ( m_render_state.stencil_test.enabled != stencil.enabled ) { enable( GL_STENCIL_TEST, stencil.enabled ); m_render_state.stencil_test.enabled = stencil.enabled; } if ( stencil.enabled ) { apply_stencil_face( GL_FRONT, m_render_state.stencil_test.front_face, stencil.front_face ); apply_stencil_face( GL_BACK, m_render_state.stencil_test.back_face, stencil.back_face ); } } void gl_context::apply_stencil_face( unsigned face, stencil_test_face& stencil, const stencil_test_face& new_stencil ) { if (( stencil.op_fail != new_stencil.op_fail ) || ( stencil.op_depth_fail != new_stencil.op_depth_fail ) || ( stencil.op_depth_pass != new_stencil.op_depth_pass ) ) { glStencilOpSeparate( face, stencil_operation_to_enum( new_stencil.op_fail ), stencil_operation_to_enum( new_stencil.op_depth_fail ), stencil_operation_to_enum( new_stencil.op_depth_pass ) ); stencil.op_fail = new_stencil.op_fail; stencil.op_depth_fail = new_stencil.op_depth_fail; stencil.op_depth_pass = new_stencil.op_depth_pass; } if (( stencil.function != new_stencil.function ) || ( stencil.ref_value != new_stencil.ref_value ) || ( stencil.mask != new_stencil.mask )) { glStencilFuncSeparate( face, stencil_function_to_enum( new_stencil.function ), new_stencil.ref_value, new_stencil.mask ); stencil.function = new_stencil.function; stencil.ref_value = new_stencil.ref_value; stencil.mask = new_stencil.mask; } } void gl_context::apply_scissor_test( const scissor_test& scissor ) { if ( m_render_state.scissor_test.enabled != scissor.enabled ) { enable( GL_SCISSOR_TEST, scissor.enabled ); m_render_state.scissor_test.enabled = scissor.enabled; } if ( scissor.dim.x < 0 || scissor.dim.y < 0 ) { NV_THROW( logic_error, "scissor_test.rect width and height must be greater than zero!" ); } if ( scissor.enabled ) { if ( m_render_state.scissor_test.dim != scissor.dim || m_render_state.scissor_test.pos != scissor.pos ) { glScissor( scissor.pos.x, scissor.pos.y, scissor.dim.x, scissor.dim.y ); m_render_state.scissor_test.dim = scissor.dim; m_render_state.scissor_test.pos = scissor.pos; } } } void gl_context::apply_depth_test( const depth_test& depth ) { if ( m_render_state.depth_test.enabled != depth.enabled ) { enable( GL_DEPTH_TEST, depth.enabled ); m_render_state.depth_test.enabled = depth.enabled; } if ( depth.enabled ) { if ( m_render_state.depth_test.function != depth.function ) { glDepthFunc( depth_state_function_to_enum( depth.function ) ); m_render_state.depth_test.function = depth.function; } } } void gl_context::apply_depth_mask( bool mask ) { if ( m_render_state.depth_mask != mask ) { glDepthMask( mask ); m_render_state.depth_mask = mask; } } void gl_context::apply_polygon_mode( const polygon_mode& mode ) { if ( m_render_state.polygon_mode.fill != mode.fill ) { glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( mode.fill ) ); m_render_state.polygon_mode.fill = mode.fill; } } void gl_context::apply_depth_range( const depth_range& range ) { if ( range.near < 0.0 || range.near > 1.0 ) { NV_THROW( logic_error, "render_state.depth_range.near must be between zero and one!"); } if ( range.far < 0.0 || range.far > 1.0 ) { NV_THROW( logic_error, "render_state.depth_range.far must be between zero and one!"); } if ((m_render_state.depth_range.far != range.far) || (m_render_state.depth_range.near != range.near)) { glDepthRange( range.near, range.far ); m_render_state.depth_range.far = range.far; m_render_state.depth_range.near = range.near; } } void gl_context::apply_color_mask( const color_mask& mask ) { if ( m_render_state.color_mask != mask ) { glColorMask( mask.red, mask.green, mask.blue, mask.alpha ); m_render_state.color_mask = mask; } } void gl_context::apply_blending( const blending& blend ) { if ( m_render_state.blending.enabled != blend.enabled ) { enable( GL_BLEND, blend.enabled ); m_render_state.blending.enabled = blend.enabled; } if ( blend.enabled ) { if ((m_render_state.blending.src_rgb_factor != blend.src_rgb_factor ) || (m_render_state.blending.dst_rgb_factor != blend.dst_rgb_factor ) || (m_render_state.blending.src_alpha_factor != blend.src_alpha_factor ) || (m_render_state.blending.dst_alpha_factor != blend.dst_alpha_factor )) { glBlendFuncSeparate( blending_factor_to_enum( blend.src_rgb_factor ), blending_factor_to_enum( blend.dst_rgb_factor ), blending_factor_to_enum( blend.src_alpha_factor ), blending_factor_to_enum( blend.dst_alpha_factor ) ); m_render_state.blending.src_rgb_factor = blend.src_rgb_factor; m_render_state.blending.dst_rgb_factor = blend.dst_rgb_factor; m_render_state.blending.src_alpha_factor = blend.src_alpha_factor; m_render_state.blending.dst_alpha_factor = blend.dst_alpha_factor; } if ((m_render_state.blending.rgb_equation != blend.rgb_equation ) || (m_render_state.blending.alpha_equation != blend.alpha_equation )) { glBlendEquationSeparate( blending_equation_to_enum( blend.rgb_equation ), blending_equation_to_enum( blend.alpha_equation ) ); m_render_state.blending.rgb_equation = blend.rgb_equation; m_render_state.blending.alpha_equation = blend.alpha_equation; } if (( m_render_state.blending.color != blend.color )) { glBlendColor( blend.color.r, blend.color.g, blend.color.b, blend.color.a ); m_render_state.blending.color = blend.color; } } } void gl_context::apply_culling( const culling& cull ) { if ( m_render_state.culling.enabled != cull.enabled ) { enable( GL_CULL_FACE, cull.enabled ); m_render_state.culling.enabled = cull.enabled; } if ( cull.enabled ) { if ( m_render_state.culling.face != cull.face ) { glCullFace( culling_face_type_to_enum(cull.face) ); m_render_state.culling.face = cull.face; } if ( m_render_state.culling.order != cull.order ) { glFrontFace( culling_order_type_to_enum( cull.order ) ); m_render_state.culling.order = cull.order; } } } void gl_context::force_apply_render_state( const render_state& state ) { enable( GL_CULL_FACE, state.culling.enabled ); glCullFace( culling_face_type_to_enum( state.culling.face ) ); glFrontFace( culling_order_type_to_enum( state.culling.order ) ); enable( GL_SCISSOR_TEST, state.scissor_test.enabled ); glScissor( state.scissor_test.pos.x, state.scissor_test.pos.y, state.scissor_test.dim.x, state.scissor_test.dim.y ); enable( GL_STENCIL_TEST, state.stencil_test.enabled ); force_apply_stencil_face( GL_FRONT, state.stencil_test.front_face ); force_apply_stencil_face( GL_BACK, state.stencil_test.back_face ); enable( GL_DEPTH_TEST, state.depth_test.enabled ); glDepthFunc( depth_state_function_to_enum( state.depth_test.function ) ); glDepthRange( state.depth_range.near, state.depth_range.far ); enable( GL_BLEND, state.blending.enabled ); glBlendFuncSeparate( blending_factor_to_enum( state.blending.src_rgb_factor ), blending_factor_to_enum( state.blending.dst_rgb_factor ), blending_factor_to_enum( state.blending.src_alpha_factor ), blending_factor_to_enum( state.blending.dst_alpha_factor ) ); glBlendEquationSeparate( blending_equation_to_enum( state.blending.rgb_equation ), blending_equation_to_enum( state.blending.alpha_equation ) ); glBlendColor( state.blending.color.r, state.blending.color.g, state.blending.color.b, state.blending.color.a ); glDepthMask( state.depth_mask ); glColorMask( state.color_mask.red, state.color_mask.green, state.color_mask.blue, state.color_mask.alpha ); glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( state.polygon_mode.fill ) ); } void gl_context::force_apply_stencil_face( unsigned face, const stencil_test_face& stencil ) { glStencilOpSeparate( face, stencil_operation_to_enum( stencil.op_fail ), stencil_operation_to_enum( stencil.op_depth_fail ), stencil_operation_to_enum( stencil.op_depth_pass ) ); glStencilFuncSeparate( face, stencil_function_to_enum( stencil.function ), stencil.ref_value, stencil.mask ); } void gl_context::apply_render_state( const render_state& state ) { // apply_primitive_restart apply_culling( state.culling ); // apply_program_point_size // apply_rasterization_mode apply_scissor_test( state.scissor_test ); apply_stencil_test( state.stencil_test ); apply_depth_test( state.depth_test ); apply_depth_range( state.depth_range ); apply_blending( state.blending ); apply_color_mask( state.color_mask ); apply_depth_mask( state.depth_mask ); apply_polygon_mode( state.polygon_mode ); } gl_context::gl_context( device* a_device, void* a_handle ) : context( a_device ), m_handle( a_handle ) { // TODO: configurable: load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT ); force_apply_render_state( m_render_state ); } nv::gl_context::~gl_context() { while ( m_framebuffers.size() > 0 ) release( m_framebuffers.get_handle(0) ); while ( m_vertex_arrays.size() > 0 ) release( m_vertex_arrays.get_handle(0) ); } void nv::gl_context::apply_engine_uniforms( program p, const scene_state& s ) { gl_program_info* info = ((gl_device*)m_device)->m_programs.get( p ); if ( info ) { for ( auto u : info->m_engine_uniforms ) { u->set( this, &s ); } } } void nv::gl_context::set_draw_buffers( uint32 count, const output_slot* slots ) { if ( count == 0 ) return; if ( count == 1 ) { set_draw_buffer( slots[0] ); return; } unsigned int buffers[8]; count = glm::min( count, 8 ); for ( uint32 i = 0; i < count; ++i ) { buffers[i] = output_slot_to_enum( slots[i] ); if ( slots[i] > OUTPUT_7 ) buffers[i] = 0; } glDrawBuffers( count, buffers ); } void nv::gl_context::set_draw_buffer( output_slot slot ) { glDrawBuffer( output_slot_to_enum(slot) ); } void nv::gl_context::set_read_buffer( output_slot slot ) { glReadBuffer( output_slot_to_enum(slot) ); } void gl_context::draw( primitive prim, const render_state& rs, program p, vertex_array va, size_t count ) { apply_render_state( rs ); const vertex_array_info* info = m_vertex_arrays.get( va ); if ( count > 0 && info ) { bind( p ); bind( va ); if ( info->index.is_valid() ) { glDrawElements( primitive_to_enum(prim), static_cast( count ), datatype_to_gl_enum( info->index_type ), 0 ); } else { glDrawArrays( primitive_to_enum(prim), 0, static_cast( count ) ); } unbind( va ); //unbind( p ); } }