source: trunk/src/gl/gl_context.cc @ 492

Last change on this file since 492 was 492, checked in by epyon, 9 years ago
File size: 26.6 KB
RevLine 
[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
14using namespace nv;
15
[491]16nv::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        glBindBuffer( GL_ARRAY_BUFFER, 0 );
69        glBindVertexArray( 0 );
70
71        if ( info->index.is_valid() )
72        {
73                glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
74        }
75
[492]76//      for ( uint32 i = 0; i < info->count; ++i )
77//      {
78//              glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
79//      }
[491]80
81
[313]82        return result;
83}
84
[492]85nv::framebuffer nv::gl_context::create_framebuffer( uint32 temp_samples )
[313]86{
[491]87        unsigned glid   = 0;
88        glGenFramebuffers( 1, &glid );
89        framebuffer result = m_framebuffers.create();
90        gl_framebuffer_info* info = m_framebuffers.get( result );
91        info->glid = glid;
92        info->depth_rb_glid = 0;
93        info->color_attachment_count = 0;
[492]94        info->sample_count = temp_samples;
[491]95        return result;
[313]96}
97
98void nv::gl_context::release( vertex_array va )
99{
[491]100        gl_vertex_array_info* info = m_vertex_arrays.get( va );
[313]101        if ( info )
102        {
103                for ( uint32 i = 0; i < info->count; ++i )
104                {
[469]105                        if ( info->attr[i].owner )
106                                m_device->release( info->attr[i].vbuffer );
[313]107                }
108                if ( info->index.is_valid() && info->index_owner) m_device->release( info->index );
[491]109                glDeleteVertexArrays( 1, &info->glid );
[313]110                m_vertex_arrays.destroy( va );
111        }
112}
113
114void nv::gl_context::release( framebuffer f )
115{
116        gl_framebuffer_info* info = m_framebuffers.get( f );
117        if ( info )
118        {
119                // TODO: release textures?
[331]120                glBindFramebuffer( GL_FRAMEBUFFER, 0 );
121                glBindRenderbuffer( GL_RENDERBUFFER, 0 );
122                if ( info->depth_rb_glid == 0 )
123                        glDeleteRenderbuffers( 1, &info->depth_rb_glid );
124                glDeleteFramebuffers( 1, &info->glid );
[335]125                m_framebuffers.destroy( f );
[313]126        }
127}
128
129const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const
130{
131        return m_framebuffers.get( f );
132}
133
[331]134void nv::gl_context::attach( framebuffer f, output_slot slot, texture t )
135{
136        // TODO: framebuffer variable, so no re-binding?
137        // TODO: support 1d, 3d textures, cubemaps or layers?
138        // TODO: support GL_READ_FRAMEBUFFER?
139        const gl_framebuffer_info* info  = m_framebuffers.get( f );
[406]140        const gl_texture_info*     tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
[331]141        if ( info )
142        {
143                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
144                unsigned gl_type = texture_type_to_enum( tinfo->type );
[313]145
[331]146                if ( tinfo )
147                {
148                        //              if ( tinfo->size.y == 0 )
149                                //                      glFramebufferTexture1D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, GL_TEXTURE_1D, tinfo->glid, 0 );
[406]150                        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+unsigned( slot ), gl_type, tinfo->glid, 0 );
[331]151                }
152                else
153                {
[406]154                        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+unsigned( slot ), gl_type, 0, 0 );
[331]155                }
156
157        }
158}
159
[463]160void nv::gl_context::attach( framebuffer f, texture depth, int layer /*= -1*/ )
[331]161{
162        // TODO: framebuffer variable, so no re-binding?
163        // TODO: support GL_READ_FRAMEBUFFER?
164        const gl_framebuffer_info* info  = m_framebuffers.get( f );
[406]165        const gl_texture_info*     tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( depth ) );
[331]166        if ( info )
167        {
168                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
169                unsigned gl_type = texture_type_to_enum( tinfo->type );
[463]170                unsigned glid = ( tinfo ? tinfo->glid : 0 );
171                if ( layer >= 0 )
[331]172                {
[463]173                        glFramebufferTextureLayer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, tinfo->glid, 0, layer );
[331]174                }
175                else
176                {
[463]177                        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, glid, 0 );
[331]178                }
179        }
[463]180
[331]181}
182
183void nv::gl_context::attach( framebuffer f, ivec2 size )
184{
185        // TODO: framebuffer variable, so no re-binding?
186        // TODO: support GL_READ_FRAMEBUFFER?
187        gl_framebuffer_info* info  = m_framebuffers.get( f );
188        if ( info )
189        {
190                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
[462]191                if ( info->depth_rb_glid )
192                        glDeleteRenderbuffers( 1, &(info->depth_rb_glid) );
[331]193                glGenRenderbuffers( 1, &(info->depth_rb_glid) );
194                glBindRenderbuffer( GL_RENDERBUFFER, info->depth_rb_glid );
[492]195                if ( info->sample_count > 1 )
196                        glRenderbufferStorageMultisample( GL_RENDERBUFFER, info->sample_count, GL_DEPTH_COMPONENT16, size.x, size.y );
197                else
198                        glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.x, size.y );
[331]199                glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, info->depth_rb_glid );
200                glBindRenderbuffer( GL_RENDERBUFFER, 0 );
201        }
202
203}
204
205void nv::gl_context::blit( framebuffer f, clear_state::buffers_type mask, ivec2 src1, ivec2 src2, ivec2 dst1, ivec2 dst2 )
206{
207        gl_framebuffer_info* info  = m_framebuffers.get( f );
208        if ( info )
209        {
210                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
211                unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST;
212                glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter );
213        }
214}
215
216
[492]217void nv::gl_context::blit( framebuffer from, framebuffer to, clear_state::buffers_type mask, ivec2 src1, ivec2 src2, ivec2 dst1, ivec2 dst2 )
218{
219        gl_framebuffer_info* finfo = m_framebuffers.get( from );
220        gl_framebuffer_info* tinfo = m_framebuffers.get( to );
221        if ( finfo )
222        {
223                glBindFramebuffer( GL_READ_FRAMEBUFFER, finfo->glid );
224                glBindFramebuffer( GL_DRAW_FRAMEBUFFER, tinfo ? tinfo->glid : 0 );
225                unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST;
226                int remove_below;
227                filter = GL_NEAREST;
228                glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter );
229                glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 );
230                if ( tinfo ) glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 );
231        }
232}
233
[331]234bool nv::gl_context::check( framebuffer_slot ft )
235{
[462]236        glDrawBuffer( 0 );
237        glReadBuffer( 0 );
[491]238
239        unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) );
240        if ( result == GL_FRAMEBUFFER_COMPLETE ) return true;
241        switch ( result )
[331]242        {
[492]243        case GL_FRAMEBUFFER_UNDEFINED                     : NV_LOG_ERROR( "gl_context::check : Framebuffer undefined!" ); break;
244        case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE        : NV_LOG_ERROR( "gl_context::check : Incomplete multisample!" ); break;
245        case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS      : NV_LOG_ERROR( "gl_context::check : Incomplete layer targets!" ); break;
[491]246        case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT         : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete attachment!" ); break;
247        case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : NV_LOG_ERROR( "gl_context::check : Framebuffer missing attachment!" ); break;
[466]248//              case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS         : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete dimensions!" ); break;
249//              case GL_FRAMEBUFFER_INCOMPLETE_FORMATS            : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete formats!" ); break;
[491]250        case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER        : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete draw buffer!" ); break;
251        case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER        : NV_LOG_ERROR( "gl_context::check : Framebuffer incomplete read buffer!" ); break;
252        case GL_FRAMEBUFFER_UNSUPPORTED                   : NV_LOG_ERROR( "gl_context::check : Framebuffer format combination unsupported!" ); break;
253        default: NV_LOG_ERROR( "gl_context::check : Unknown Framebuffer error! (", result, ")" ); break;
[331]254        }
[462]255        glDrawBuffer( GL_BACK );
256        glReadBuffer( GL_BACK );
[331]257        return false;
258}
259
260void nv::gl_context::bind( framebuffer f, framebuffer_slot ft /*= FRAMEBUFFER_BOTH */ )
261{
262        const gl_framebuffer_info* info = m_framebuffers.get( f );
263        if ( info )
264        {
265                glBindFramebuffer( framebuffer_slot_to_enum(ft), info->glid );
266        }
267        else
268        {
269                glBindFramebuffer( framebuffer_slot_to_enum(ft), 0 );
270        }
271}
272
[491]273void nv::gl_context::bind( buffer b, texture t )
274{
275        const gl_buffer_info*  binfo = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
276        const gl_texture_info* tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
277        NV_ASSERT( binfo && tinfo, "Bad buffer or texture passed to create_texture" );
278        if ( binfo && tinfo )
279        {
280                NV_ASSERT_ALWAYS( binfo->type == TEXTURE_BUFFER && tinfo->type == TEXTURE_1D_BUFFER, "bad texture or buffer type!" );
[492]281                bind( t, TEXTURE_0 );
[491]282                glTexBuffer( GL_TEXTURE_BUFFER, image_format_to_internal_enum( tinfo->format.format ), binfo->glid );
283        }
284}
285
[473]286void nv::gl_context::bind( buffer b, uint32 index, size_t offset /*= 0*/, size_t size /*= 0 */ )
287{
288        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
289        if ( info )
290        {
291                if ( size == 0 )
292                        glBindBufferBase( buffer_type_to_enum( info->type ), index, info->glid );
293                else
[487]294                        glBindBufferRange( buffer_type_to_enum( info->type ), index, info->glid, static_cast<GLintptr>( offset ), static_cast<GLsizeiptr>( size ) );
[473]295        }
296}
297
[301]298void gl_context::bind( texture t, texture_slot slot )
[299]299{
[492]300        if ( m_bound_textures[ slot ] != t )
[301]301        {
[492]302                const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
303                if ( info )
304                {
305                        set_active_texture( slot );
306                        glBindTexture( texture_type_to_enum( info->type ), info->glid );
307                }
[301]308        }
[299]309}
310
[303]311void nv::gl_context::bind( program p )
[299]312{
[406]313        gl_device* gdevice    = static_cast<gl_device*>( m_device );
314        gl_program_info* info = gdevice->m_programs.get( p );
[303]315        if ( info )
316        {
317                glUseProgram( info->glid );
[406]318                gdevice->update_uniforms( info );
[303]319        }
[299]320}
321
[313]322// void nv::gl_context::bind( buffer b )
323// {
324//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
325//      if ( info )
326//      {
327//              glBindBuffer( buffer_type_to_enum( info->type ), info->glid );
328//      }
329// }
[299]330
[302]331void nv::gl_context::bind( vertex_array va )
[299]332{
[491]333        gl_vertex_array_info* info = m_vertex_arrays.get( va );
[302]334        if ( info )
[299]335        {
[491]336                glBindVertexArray( info->glid );
[299]337        }
338}
339
[313]340void nv::gl_context::unbind( program )
341{
342        glUseProgram( 0 );
343}
344
[492]345void nv::gl_context::set_active_texture( texture_slot slot )
346{
347        if ( slot != m_active_slot )
348                glActiveTexture( GL_TEXTURE0 + static_cast<GLenum>( slot ) );
349}
350
[313]351// void nv::gl_context::unbind( buffer b )
352// {
353//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
354//      if ( info )
355//      {
356//              glBindBuffer( buffer_type_to_enum( info->type ), 0 );
357//      }
358// }
359
[302]360void nv::gl_context::unbind( vertex_array va )
[299]361{
[313]362        const vertex_array_info* info = m_vertex_arrays.get( va );
[302]363        if ( info )
[299]364        {
[491]365                glBindVertexArray( 0 );
[299]366        }
367}
368
[313]369void nv::gl_context::unbind( framebuffer f )
370{
371        // this way we are sure that the extension is loaded
372        const gl_framebuffer_info* info = m_framebuffers.get( f );
373        if ( info )
374        {
375                glBindFramebuffer( GL_FRAMEBUFFER, 0 );
376        }
[331]377        glDrawBuffer( GL_BACK );
378        glReadBuffer( GL_BACK );
[313]379}
380
[406]381void nv::gl_context::update( texture t, const void* data )
[299]382{
[301]383        const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
[491]384        NV_ASSERT_ALWAYS( info->type != TEXTURE_1D_BUFFER, "Buffer texture passed to update!" );
[492]385        NV_ASSERT_ALWAYS( info->type != TEXTURE_2D_MULTISAMPLE, "Multisample texture passed to update!" );
[301]386        if ( info )
387        {
[331]388                image_format format  = info->format;
[463]389                ivec3        size    = info->size;
[331]390                unsigned     gl_type = texture_type_to_enum( info->type );
[299]391
[492]392                if ( m_bound_textures[ m_active_slot ] != t )
393                        glBindTexture( gl_type, info->glid );
[491]394                int this_should_be_subTexImage;
[463]395                if ( info->type == TEXTURE_3D || info->type == TEXTURE_2D_ARRAY )
396                        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 );
397                else
398                        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 );
[301]399        }
[299]400}
401
[485]402// void nv::gl_context::update( buffer b, uint32 index, const void* data, size_t offset, size_t size )
403// {
404//      const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
405//      if ( info )
406//      {
407//              GLenum glenum = buffer_type_to_enum( info->type );
408//              if ( size == 0 )
409//                      glBindBufferBase( glenum, index, info->glid );
410//              else
411//                      glBindBufferRange( glenum, index, info->glid, offset, size );
412//              glBufferSubData( glenum, GLintptr( offset ), GLsizeiptr( size ), data );
413//      }
414// }
[473]415
[376]416void gl_context::update( buffer b, const void* data, nv::size_t offset, nv::size_t size )
[299]417{
[302]418        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
419        if ( info )
420        {
[313]421                GLenum glenum = buffer_type_to_enum( info->type );
422                glBindBuffer( glenum, info->glid );
[406]423                glBufferSubData( glenum, GLintptr( offset ), GLsizeiptr( size ), data );
[302]424        }
[299]425}
426
[37]427void gl_context::clear( const clear_state& cs )
428{
429        // apply_framebuffer
[44]430       
[37]431        apply_scissor_test( cs.scissor_test );
432        apply_color_mask( cs.color_mask );
433        apply_depth_mask( cs.depth_mask );
434        // stencil_mask_separate
435
436        if ( m_clear_color != cs.color )
437        {
438                glClearColor( cs.color.r, cs.color.g, cs.color.b, cs.color.a );
439                m_clear_color = cs.color;
440        }
441
442        if ( m_clear_depth != cs.depth )
443        {
444                glClearDepth( cs.depth );
445                m_clear_depth = cs.depth;
446        }
447
448        if ( m_clear_stencil != cs.stencil )
449        {
450                glClearStencil( cs.stencil );
451                m_clear_stencil = cs.stencil;
452        }
[44]453       
454        glClear( clear_state_buffers_to_mask( cs.buffers ) );
[37]455}
456
457const ivec4& gl_context::get_viewport()
458{
459        return m_viewport;
460}
461
462void gl_context::set_viewport( const ivec4& viewport )
463{
[403]464        NV_ASSERT_ALWAYS( viewport.z > 0 && viewport.w > 0, "viewport dimensions must be greater than zero!" );
[37]465        m_viewport = viewport;
466        glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
467}
468
469void gl_context::enable( unsigned int what, bool value )
470{
471        if ( value )
472        {
473                glEnable( what );
474        }
475        else
476        {
477                glDisable( what );
478        }
479}
480
481void gl_context::apply_stencil_test( const stencil_test& stencil )
482{
483        if ( m_render_state.stencil_test.enabled != stencil.enabled )
484        {
485                enable( GL_STENCIL_TEST, stencil.enabled );
486                m_render_state.stencil_test.enabled = stencil.enabled;
487        }
[331]488       
[37]489        if ( stencil.enabled )
490        {
491                apply_stencil_face( GL_FRONT, m_render_state.stencil_test.front_face, stencil.front_face );
492                apply_stencil_face( GL_BACK,  m_render_state.stencil_test.back_face,  stencil.back_face );
493        }
494}
495
[121]496void gl_context::apply_stencil_face( unsigned face, stencil_test_face& stencil, const stencil_test_face& new_stencil )
[37]497{
498        if (( stencil.op_fail       != new_stencil.op_fail       ) ||
499                ( stencil.op_depth_fail != new_stencil.op_depth_fail ) ||
500                ( stencil.op_depth_pass != new_stencil.op_depth_pass ) )
501        {
502                glStencilOpSeparate( face,
503                        stencil_operation_to_enum( new_stencil.op_fail ),
504                        stencil_operation_to_enum( new_stencil.op_depth_fail ),
505                        stencil_operation_to_enum( new_stencil.op_depth_pass )
506                );
507
508                stencil.op_fail       = new_stencil.op_fail;
509                stencil.op_depth_fail = new_stencil.op_depth_fail;
510                stencil.op_depth_pass = new_stencil.op_depth_pass;
511        }
512
513        if (( stencil.function  != new_stencil.function ) ||
514                ( stencil.ref_value != new_stencil.ref_value ) ||
515                ( stencil.mask      != new_stencil.mask ))
516        {
517                glStencilFuncSeparate( face,
518                        stencil_function_to_enum( new_stencil.function ),
519                        new_stencil.ref_value,
520                        new_stencil.mask
521                );
522
523                stencil.function  = new_stencil.function;
524                stencil.ref_value = new_stencil.ref_value;
525                stencil.mask      = new_stencil.mask;
526        }
527}
528
529void gl_context::apply_scissor_test( const scissor_test& scissor )
530{
531        if ( m_render_state.scissor_test.enabled != scissor.enabled )
532        {
533                enable( GL_SCISSOR_TEST, scissor.enabled );
534                m_render_state.scissor_test.enabled = scissor.enabled;
535        }
536
537
538        if ( scissor.enabled )
539        {
[403]540                NV_ASSERT_ALWAYS( scissor.dim.x > 0 && scissor.dim.y > 0, "scissor_test.rect dimension equal to zero!" );
541
[37]542                if ( m_render_state.scissor_test.dim != scissor.dim || m_render_state.scissor_test.pos != scissor.pos )
543                {
544                        glScissor(
545                                scissor.pos.x, scissor.pos.y,
546                                scissor.dim.x, scissor.dim.y
547                        );
548                        m_render_state.scissor_test.dim = scissor.dim;
549                        m_render_state.scissor_test.pos = scissor.pos;
550                }
551        }
552}
553
554void gl_context::apply_depth_test( const depth_test& depth )
555{
556        if ( m_render_state.depth_test.enabled != depth.enabled )
557        {
558                enable( GL_DEPTH_TEST, depth.enabled );
559                m_render_state.depth_test.enabled = depth.enabled;
560        }
561
562        if ( depth.enabled )
563        {
564                if ( m_render_state.depth_test.function != depth.function )
565                {
566                        glDepthFunc( depth_state_function_to_enum( depth.function ) );
567                        m_render_state.depth_test.function = depth.function;
568                }
569        }
570}
571
572void gl_context::apply_depth_mask( bool mask )
573{
574        if ( m_render_state.depth_mask != mask )
575        {
576                glDepthMask( mask );
577                m_render_state.depth_mask = mask;
578        }
579}
580
[492]581void gl_context::apply_multisample( bool multisample )
582{
583        if ( m_render_state.multisample != multisample )
584        {
585                glDepthMask( multisample );
586                m_render_state.multisample = multisample;
587        }
588}
589
[233]590void gl_context::apply_polygon_mode( const polygon_mode& mode )
591{
592        if ( m_render_state.polygon_mode.fill != mode.fill )
593        {
594                glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( mode.fill ) );
595                m_render_state.polygon_mode.fill = mode.fill;
596        }
597}
598
599
[37]600void gl_context::apply_depth_range( const depth_range& range )
601{
[403]602        NV_ASSERT_ALWAYS( range.near >= 0.0 && range.near <= 1.0, "render_state.depth_range.near must be between zero and one!" );
603        NV_ASSERT_ALWAYS( range.far  >= 0.0 && range.far  <= 1.0, "render_state.depth_range.far must be between zero and one!" );
[37]604
605        if ((m_render_state.depth_range.far  != range.far) ||
606                (m_render_state.depth_range.near != range.near))
607        {
608                glDepthRange( range.near, range.far );
609
610                m_render_state.depth_range.far  = range.far;
611                m_render_state.depth_range.near = range.near;
612        }
613}
614
615void gl_context::apply_color_mask( const color_mask& mask )
616{
617        if ( m_render_state.color_mask != mask )
618        {
619                glColorMask( mask.red, mask.green, mask.blue, mask.alpha );
620                m_render_state.color_mask = mask;
621        }
622}
623
624void gl_context::apply_blending( const blending& blend )
625{
626        if ( m_render_state.blending.enabled != blend.enabled )
627        {
628                enable( GL_BLEND, blend.enabled );
629                m_render_state.blending.enabled = blend.enabled;
630        }
631
632        if ( blend.enabled )
633        {
634                if ((m_render_state.blending.src_rgb_factor   != blend.src_rgb_factor ) ||
635                        (m_render_state.blending.dst_rgb_factor   != blend.dst_rgb_factor ) ||
636                        (m_render_state.blending.src_alpha_factor != blend.src_alpha_factor ) ||
637                        (m_render_state.blending.dst_alpha_factor != blend.dst_alpha_factor ))
638                {
639                        glBlendFuncSeparate(
640                                blending_factor_to_enum( blend.src_rgb_factor ),
641                                blending_factor_to_enum( blend.dst_rgb_factor ),
642                                blending_factor_to_enum( blend.src_alpha_factor ),
643                                blending_factor_to_enum( blend.dst_alpha_factor )
644                        );
645
646                        m_render_state.blending.src_rgb_factor   = blend.src_rgb_factor;
647                        m_render_state.blending.dst_rgb_factor   = blend.dst_rgb_factor;
648                        m_render_state.blending.src_alpha_factor = blend.src_alpha_factor;
649                        m_render_state.blending.dst_alpha_factor = blend.dst_alpha_factor;
650                }
651
652                if ((m_render_state.blending.rgb_equation   != blend.rgb_equation ) ||
653                        (m_render_state.blending.alpha_equation != blend.alpha_equation ))
654                {
655                        glBlendEquationSeparate(
656                                blending_equation_to_enum( blend.rgb_equation ),
657                                blending_equation_to_enum( blend.alpha_equation )
658                        );
659
660                        m_render_state.blending.rgb_equation   = blend.rgb_equation;
661                        m_render_state.blending.alpha_equation = blend.alpha_equation;
662                }
663
664                if (( m_render_state.blending.color != blend.color ))
665                {
666                        glBlendColor( blend.color.r, blend.color.g, blend.color.b, blend.color.a );
667                        m_render_state.blending.color = blend.color;
668                }
669        }
670}
671
672
673void gl_context::apply_culling( const culling& cull )
674{
675        if ( m_render_state.culling.enabled != cull.enabled )
676        {
677                enable( GL_CULL_FACE, cull.enabled );
678                m_render_state.culling.enabled = cull.enabled;
679        }
680
681        if ( cull.enabled )
682        {
683                if ( m_render_state.culling.face != cull.face )
684                {
[462]685                        glCullFace( culling_face_type_to_enum( cull.face ) );
[37]686                        m_render_state.culling.face = cull.face;
687                }
[462]688        }
[37]689
[462]690        if ( m_render_state.culling.order != cull.order )
691        {
692                glFrontFace( culling_order_type_to_enum( cull.order ) );
693                m_render_state.culling.order = cull.order;
[37]694        }
695}
696
697
698void gl_context::force_apply_render_state( const render_state& state )
699{
700        enable( GL_CULL_FACE, state.culling.enabled );
701        glCullFace( culling_face_type_to_enum( state.culling.face ) );
702        glFrontFace( culling_order_type_to_enum( state.culling.order ) );
703
704        enable( GL_SCISSOR_TEST, state.scissor_test.enabled );
705        glScissor(
706                state.scissor_test.pos.x, state.scissor_test.pos.y,
707                state.scissor_test.dim.x, state.scissor_test.dim.y
708        );
709
710        enable( GL_STENCIL_TEST, state.stencil_test.enabled );
711        force_apply_stencil_face( GL_FRONT, state.stencil_test.front_face );
712        force_apply_stencil_face( GL_BACK,  state.stencil_test.back_face  );
713
714        enable( GL_DEPTH_TEST, state.depth_test.enabled );
715        glDepthFunc( depth_state_function_to_enum( state.depth_test.function ) );
716        glDepthRange( state.depth_range.near, state.depth_range.far );
717
718        enable( GL_BLEND, state.blending.enabled );
719        glBlendFuncSeparate(
720                blending_factor_to_enum( state.blending.src_rgb_factor ),
721                blending_factor_to_enum( state.blending.dst_rgb_factor ),
722                blending_factor_to_enum( state.blending.src_alpha_factor ),
723                blending_factor_to_enum( state.blending.dst_alpha_factor )
724        );
725        glBlendEquationSeparate(
726                blending_equation_to_enum( state.blending.rgb_equation ),
727                blending_equation_to_enum( state.blending.alpha_equation )
728        );
729        glBlendColor(
730                state.blending.color.r, state.blending.color.g,
731                state.blending.color.b, state.blending.color.a
732        );
733
734        glDepthMask( state.depth_mask );
735        glColorMask(
736                state.color_mask.red, state.color_mask.green,
737                state.color_mask.blue, state.color_mask.alpha
738        );
[233]739        glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( state.polygon_mode.fill ) );
[37]740}
741
[121]742void gl_context::force_apply_stencil_face( unsigned face, const stencil_test_face& stencil )
[37]743{
744        glStencilOpSeparate( face,
745                stencil_operation_to_enum( stencil.op_fail ),
746                stencil_operation_to_enum( stencil.op_depth_fail ),
747                stencil_operation_to_enum( stencil.op_depth_pass )
748        );
749
750        glStencilFuncSeparate( face,
751                stencil_function_to_enum( stencil.function ),
752                stencil.ref_value,
753                stencil.mask
754        );
755}
756
757
758void gl_context::apply_render_state( const render_state& state )
759{
760        // apply_primitive_restart
761        apply_culling( state.culling );
762        // apply_program_point_size
763        // apply_rasterization_mode
764        apply_scissor_test( state.scissor_test );
765        apply_stencil_test( state.stencil_test );
766        apply_depth_test( state.depth_test );
767        apply_depth_range( state.depth_range );
768        apply_blending( state.blending );
769        apply_color_mask( state.color_mask );
770        apply_depth_mask( state.depth_mask );
[492]771        apply_multisample( state.multisample );
[233]772        apply_polygon_mode( state.polygon_mode );
[37]773}
774
[44]775
[326]776gl_context::gl_context( device* a_device, void* a_handle )
777        : context( a_device ), m_handle( a_handle )
[44]778{
[331]779        // TODO: configurable:
[491]780//      load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT | GL_EXT_TEXTURE_ARRAY );
[492]781        m_active_slot = texture_slot( -1 );
[326]782        force_apply_render_state( m_render_state );
[245]783}
784
785
786nv::gl_context::~gl_context()
787{
[313]788        while ( m_framebuffers.size() > 0 )
789                release( m_framebuffers.get_handle(0) );
790        while ( m_vertex_arrays.size() > 0 )
791                release( m_vertex_arrays.get_handle(0) );
[245]792}
793
[303]794void nv::gl_context::apply_engine_uniforms( program p, const scene_state& s )
[245]795{
[406]796        gl_program_info* info = static_cast<gl_device*>( m_device )->m_programs.get( p );
[303]797        if ( info )
798        {
[392]799                for ( auto& u : *info->m_engine_uniforms )
[303]800                {
801                        u->set( this, &s );
802                }
803        }
804}
805
[342]806void nv::gl_context::set_draw_buffers( uint32 count, const output_slot* slots )
[331]807{
[342]808        if ( count == 0 ) return;
809        if ( count == 1 )
810        {
811                set_draw_buffer( slots[0] );
812                return;
813        }
[331]814        unsigned int buffers[8];
[398]815        count = nv::min<uint32>( count, 8 );
[331]816        for ( uint32 i = 0; i < count; ++i )
817        {
818                buffers[i] = output_slot_to_enum( slots[i] );
819                if ( slots[i] > OUTPUT_7 ) buffers[i] = 0;
820        }
[406]821        glDrawBuffers( GLsizei( count ), buffers );
[331]822}
823
824void nv::gl_context::set_draw_buffer( output_slot slot )
825{
826        glDrawBuffer( output_slot_to_enum(slot) );
827}
828
829void nv::gl_context::set_read_buffer( output_slot slot )
830{
831        glReadBuffer( output_slot_to_enum(slot) );
832}
833
[473]834void gl_context::draw( primitive prim, const render_state& rs, program p, vertex_array va, nv::size_t count, nv::size_t first )
[303]835{
[245]836        apply_render_state( rs );
[313]837        const vertex_array_info* info = m_vertex_arrays.get( va );
[302]838        if ( count > 0 && info )
[245]839        {
[299]840                bind( p );
841                bind( va );
[302]842                if ( info->index.is_valid() )
[245]843                {
[473]844                        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]845                }
846                else
847                {
[487]848                        glDrawArrays( primitive_to_enum(prim), static_cast<GLint>( first ), static_cast<GLsizei>( count ) );
[245]849                }
[299]850                unbind( va );
851                //unbind( p );
[245]852        }
853}
Note: See TracBrowser for help on using the repository browser.