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

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