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

Last change on this file since 493 was 493, checked in by epyon, 9 years ago
  • program validation moved to first use - fixes validation errors on Intel and AMD cards
File size: 27.3 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( const vertex_array_desc& desc )
17{
18        vertex_array result = m_vertex_arrays.create();
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
76//      for ( uint32 i = 0; i < info->count; ++i )
77//      {
78//              glDisableVertexAttribArray( static_cast<uint32>( info->attr[i].location ) );
79//      }
80
81
82        return result;
83}
84
85nv::framebuffer nv::gl_context::create_framebuffer( uint32 temp_samples )
86{
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;
94        info->sample_count = temp_samples;
95        return result;
96}
97
98void nv::gl_context::release( vertex_array va )
99{
100        gl_vertex_array_info* info = m_vertex_arrays.get( va );
101        if ( info )
102        {
103                for ( uint32 i = 0; i < info->count; ++i )
104                {
105                        if ( info->attr[i].owner )
106                                m_device->release( info->attr[i].vbuffer );
107                }
108                if ( info->index.is_valid() && info->index_owner) m_device->release( info->index );
109                glDeleteVertexArrays( 1, &info->glid );
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?
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 );
125                m_framebuffers.destroy( f );
126        }
127}
128
129const framebuffer_info* nv::gl_context::get_framebuffer_info( framebuffer f ) const
130{
131        return m_framebuffers.get( f );
132}
133
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 );
140        const gl_texture_info*     tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
141        if ( info )
142        {
143                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
144                unsigned gl_type = texture_type_to_enum( tinfo->type );
145
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 );
150                        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+unsigned( slot ), gl_type, tinfo->glid, 0 );
151                }
152                else
153                {
154                        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+unsigned( slot ), gl_type, 0, 0 );
155                }
156
157        }
158}
159
160void nv::gl_context::attach( framebuffer f, texture depth, int layer /*= -1*/ )
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 );
165        const gl_texture_info*     tinfo = static_cast< const gl_texture_info* >( m_device->get_texture_info( depth ) );
166        if ( info )
167        {
168                glBindFramebuffer( GL_FRAMEBUFFER, info->glid );
169                unsigned gl_type = texture_type_to_enum( tinfo->type );
170                unsigned glid = ( tinfo ? tinfo->glid : 0 );
171                if ( layer >= 0 )
172                {
173                        glFramebufferTextureLayer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, tinfo->glid, 0, layer );
174                }
175                else
176                {
177                        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, glid, 0 );
178                }
179        }
180
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 );
191                if ( info->depth_rb_glid )
192                        glDeleteRenderbuffers( 1, &(info->depth_rb_glid) );
193                glGenRenderbuffers( 1, &(info->depth_rb_glid) );
194                glBindRenderbuffer( GL_RENDERBUFFER, info->depth_rb_glid );
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 );
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
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
234bool nv::gl_context::check( framebuffer_slot ft )
235{
236        glDrawBuffer( 0 );
237        glReadBuffer( 0 );
238
239        unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) );
240        if ( result == GL_FRAMEBUFFER_COMPLETE ) return true;
241        switch ( result )
242        {
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;
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;
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;
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;
254        }
255        glDrawBuffer( GL_BACK );
256        glReadBuffer( GL_BACK );
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
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!" );
281                bind( t, TEXTURE_0 );
282                glTexBuffer( GL_TEXTURE_BUFFER, image_format_to_internal_enum( tinfo->format.format ), binfo->glid );
283        }
284}
285
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
294                        glBindBufferRange( buffer_type_to_enum( info->type ), index, info->glid, static_cast<GLintptr>( offset ), static_cast<GLsizeiptr>( size ) );
295        }
296}
297
298void gl_context::bind( texture t, texture_slot slot )
299{
300        if ( m_bound_textures[ slot ] != t )
301        {
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                }
308        }
309}
310
311void nv::gl_context::bind( program p )
312{
313        gl_device* gdevice    = static_cast<gl_device*>( m_device );
314        gl_program_info* info = gdevice->m_programs.get( p );
315        if ( info )
316        {
317                glUseProgram( info->glid );
318                gdevice->update_uniforms( info );
319                if ( !info->validated )
320                {
321                        validate_program( p );
322                }
323        }
324}
325
326bool 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
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// }
360
361void nv::gl_context::bind( vertex_array va )
362{
363        gl_vertex_array_info* info = m_vertex_arrays.get( va );
364        if ( info )
365        {
366                glBindVertexArray( info->glid );
367        }
368}
369
370void nv::gl_context::unbind( program )
371{
372        glUseProgram( 0 );
373}
374
375void nv::gl_context::set_active_texture( texture_slot slot )
376{
377        if ( slot != m_active_slot )
378                glActiveTexture( GL_TEXTURE0 + static_cast<GLenum>( slot ) );
379}
380
381// void nv::gl_context::unbind( buffer b )
382// {
383//      const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
384//      if ( info )
385//      {
386//              glBindBuffer( buffer_type_to_enum( info->type ), 0 );
387//      }
388// }
389
390void nv::gl_context::unbind( vertex_array va )
391{
392        const vertex_array_info* info = m_vertex_arrays.get( va );
393        if ( info )
394        {
395                glBindVertexArray( 0 );
396        }
397}
398
399void nv::gl_context::unbind( framebuffer f )
400{
401        // this way we are sure that the extension is loaded
402        const gl_framebuffer_info* info = m_framebuffers.get( f );
403        if ( info )
404        {
405                glBindFramebuffer( GL_FRAMEBUFFER, 0 );
406        }
407        glDrawBuffer( GL_BACK );
408        glReadBuffer( GL_BACK );
409}
410
411void nv::gl_context::update( texture t, const void* data )
412{
413        const gl_texture_info* info = static_cast< const gl_texture_info* >( m_device->get_texture_info( t ) );
414        NV_ASSERT_ALWAYS( info->type != TEXTURE_1D_BUFFER, "Buffer texture passed to update!" );
415        NV_ASSERT_ALWAYS( info->type != TEXTURE_2D_MULTISAMPLE, "Multisample texture passed to update!" );
416        if ( info )
417        {
418                image_format format  = info->format;
419                ivec3        size    = info->size;
420                unsigned     gl_type = texture_type_to_enum( info->type );
421
422                if ( m_bound_textures[ m_active_slot ] != t )
423                        glBindTexture( gl_type, info->glid );
424                int this_should_be_subTexImage;
425                if ( info->type == TEXTURE_3D || info->type == TEXTURE_2D_ARRAY )
426                        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 );
427                else
428                        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 );
429        }
430}
431
432// void nv::gl_context::update( buffer b, uint32 index, const void* data, size_t offset, size_t size )
433// {
434//      const gl_buffer_info* info = static_cast<const gl_buffer_info*>( m_device->get_buffer_info( b ) );
435//      if ( info )
436//      {
437//              GLenum glenum = buffer_type_to_enum( info->type );
438//              if ( size == 0 )
439//                      glBindBufferBase( glenum, index, info->glid );
440//              else
441//                      glBindBufferRange( glenum, index, info->glid, offset, size );
442//              glBufferSubData( glenum, GLintptr( offset ), GLsizeiptr( size ), data );
443//      }
444// }
445
446void gl_context::update( buffer b, const void* data, nv::size_t offset, nv::size_t size )
447{
448        const gl_buffer_info* info = static_cast< const gl_buffer_info* >( m_device->get_buffer_info( b ) );
449        if ( info )
450        {
451                GLenum glenum = buffer_type_to_enum( info->type );
452                glBindBuffer( glenum, info->glid );
453                glBufferSubData( glenum, GLintptr( offset ), GLsizeiptr( size ), data );
454        }
455}
456
457void gl_context::clear( const clear_state& cs )
458{
459        // apply_framebuffer
460       
461        apply_scissor_test( cs.scissor_test );
462        apply_color_mask( cs.color_mask );
463        apply_depth_mask( cs.depth_mask );
464        // stencil_mask_separate
465
466        if ( m_clear_color != cs.color )
467        {
468                glClearColor( cs.color.r, cs.color.g, cs.color.b, cs.color.a );
469                m_clear_color = cs.color;
470        }
471
472        if ( m_clear_depth != cs.depth )
473        {
474                glClearDepth( cs.depth );
475                m_clear_depth = cs.depth;
476        }
477
478        if ( m_clear_stencil != cs.stencil )
479        {
480                glClearStencil( cs.stencil );
481                m_clear_stencil = cs.stencil;
482        }
483       
484        glClear( clear_state_buffers_to_mask( cs.buffers ) );
485}
486
487const ivec4& gl_context::get_viewport()
488{
489        return m_viewport;
490}
491
492void gl_context::set_viewport( const ivec4& viewport )
493{
494        NV_ASSERT_ALWAYS( viewport.z > 0 && viewport.w > 0, "viewport dimensions must be greater than zero!" );
495        m_viewport = viewport;
496        glViewport( viewport.x, viewport.y, viewport.z, viewport.w );
497}
498
499void gl_context::enable( unsigned int what, bool value )
500{
501        if ( value )
502        {
503                glEnable( what );
504        }
505        else
506        {
507                glDisable( what );
508        }
509}
510
511void gl_context::apply_stencil_test( const stencil_test& stencil )
512{
513        if ( m_render_state.stencil_test.enabled != stencil.enabled )
514        {
515                enable( GL_STENCIL_TEST, stencil.enabled );
516                m_render_state.stencil_test.enabled = stencil.enabled;
517        }
518       
519        if ( stencil.enabled )
520        {
521                apply_stencil_face( GL_FRONT, m_render_state.stencil_test.front_face, stencil.front_face );
522                apply_stencil_face( GL_BACK,  m_render_state.stencil_test.back_face,  stencil.back_face );
523        }
524}
525
526void gl_context::apply_stencil_face( unsigned face, stencil_test_face& stencil, const stencil_test_face& new_stencil )
527{
528        if (( stencil.op_fail       != new_stencil.op_fail       ) ||
529                ( stencil.op_depth_fail != new_stencil.op_depth_fail ) ||
530                ( stencil.op_depth_pass != new_stencil.op_depth_pass ) )
531        {
532                glStencilOpSeparate( face,
533                        stencil_operation_to_enum( new_stencil.op_fail ),
534                        stencil_operation_to_enum( new_stencil.op_depth_fail ),
535                        stencil_operation_to_enum( new_stencil.op_depth_pass )
536                );
537
538                stencil.op_fail       = new_stencil.op_fail;
539                stencil.op_depth_fail = new_stencil.op_depth_fail;
540                stencil.op_depth_pass = new_stencil.op_depth_pass;
541        }
542
543        if (( stencil.function  != new_stencil.function ) ||
544                ( stencil.ref_value != new_stencil.ref_value ) ||
545                ( stencil.mask      != new_stencil.mask ))
546        {
547                glStencilFuncSeparate( face,
548                        stencil_function_to_enum( new_stencil.function ),
549                        new_stencil.ref_value,
550                        new_stencil.mask
551                );
552
553                stencil.function  = new_stencil.function;
554                stencil.ref_value = new_stencil.ref_value;
555                stencil.mask      = new_stencil.mask;
556        }
557}
558
559void gl_context::apply_scissor_test( const scissor_test& scissor )
560{
561        if ( m_render_state.scissor_test.enabled != scissor.enabled )
562        {
563                enable( GL_SCISSOR_TEST, scissor.enabled );
564                m_render_state.scissor_test.enabled = scissor.enabled;
565        }
566
567
568        if ( scissor.enabled )
569        {
570                NV_ASSERT_ALWAYS( scissor.dim.x > 0 && scissor.dim.y > 0, "scissor_test.rect dimension equal to zero!" );
571
572                if ( m_render_state.scissor_test.dim != scissor.dim || m_render_state.scissor_test.pos != scissor.pos )
573                {
574                        glScissor(
575                                scissor.pos.x, scissor.pos.y,
576                                scissor.dim.x, scissor.dim.y
577                        );
578                        m_render_state.scissor_test.dim = scissor.dim;
579                        m_render_state.scissor_test.pos = scissor.pos;
580                }
581        }
582}
583
584void gl_context::apply_depth_test( const depth_test& depth )
585{
586        if ( m_render_state.depth_test.enabled != depth.enabled )
587        {
588                enable( GL_DEPTH_TEST, depth.enabled );
589                m_render_state.depth_test.enabled = depth.enabled;
590        }
591
592        if ( depth.enabled )
593        {
594                if ( m_render_state.depth_test.function != depth.function )
595                {
596                        glDepthFunc( depth_state_function_to_enum( depth.function ) );
597                        m_render_state.depth_test.function = depth.function;
598                }
599        }
600}
601
602void gl_context::apply_depth_mask( bool mask )
603{
604        if ( m_render_state.depth_mask != mask )
605        {
606                glDepthMask( mask );
607                m_render_state.depth_mask = mask;
608        }
609}
610
611void gl_context::apply_multisample( bool multisample )
612{
613        if ( m_render_state.multisample != multisample )
614        {
615                glDepthMask( multisample );
616                m_render_state.multisample = multisample;
617        }
618}
619
620void gl_context::apply_polygon_mode( const polygon_mode& mode )
621{
622        if ( m_render_state.polygon_mode.fill != mode.fill )
623        {
624                glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( mode.fill ) );
625                m_render_state.polygon_mode.fill = mode.fill;
626        }
627}
628
629
630void gl_context::apply_depth_range( const depth_range& range )
631{
632        NV_ASSERT_ALWAYS( range.near >= 0.0 && range.near <= 1.0, "render_state.depth_range.near must be between zero and one!" );
633        NV_ASSERT_ALWAYS( range.far  >= 0.0 && range.far  <= 1.0, "render_state.depth_range.far must be between zero and one!" );
634
635        if ((m_render_state.depth_range.far  != range.far) ||
636                (m_render_state.depth_range.near != range.near))
637        {
638                glDepthRange( range.near, range.far );
639
640                m_render_state.depth_range.far  = range.far;
641                m_render_state.depth_range.near = range.near;
642        }
643}
644
645void gl_context::apply_color_mask( const color_mask& mask )
646{
647        if ( m_render_state.color_mask != mask )
648        {
649                glColorMask( mask.red, mask.green, mask.blue, mask.alpha );
650                m_render_state.color_mask = mask;
651        }
652}
653
654void gl_context::apply_blending( const blending& blend )
655{
656        if ( m_render_state.blending.enabled != blend.enabled )
657        {
658                enable( GL_BLEND, blend.enabled );
659                m_render_state.blending.enabled = blend.enabled;
660        }
661
662        if ( blend.enabled )
663        {
664                if ((m_render_state.blending.src_rgb_factor   != blend.src_rgb_factor ) ||
665                        (m_render_state.blending.dst_rgb_factor   != blend.dst_rgb_factor ) ||
666                        (m_render_state.blending.src_alpha_factor != blend.src_alpha_factor ) ||
667                        (m_render_state.blending.dst_alpha_factor != blend.dst_alpha_factor ))
668                {
669                        glBlendFuncSeparate(
670                                blending_factor_to_enum( blend.src_rgb_factor ),
671                                blending_factor_to_enum( blend.dst_rgb_factor ),
672                                blending_factor_to_enum( blend.src_alpha_factor ),
673                                blending_factor_to_enum( blend.dst_alpha_factor )
674                        );
675
676                        m_render_state.blending.src_rgb_factor   = blend.src_rgb_factor;
677                        m_render_state.blending.dst_rgb_factor   = blend.dst_rgb_factor;
678                        m_render_state.blending.src_alpha_factor = blend.src_alpha_factor;
679                        m_render_state.blending.dst_alpha_factor = blend.dst_alpha_factor;
680                }
681
682                if ((m_render_state.blending.rgb_equation   != blend.rgb_equation ) ||
683                        (m_render_state.blending.alpha_equation != blend.alpha_equation ))
684                {
685                        glBlendEquationSeparate(
686                                blending_equation_to_enum( blend.rgb_equation ),
687                                blending_equation_to_enum( blend.alpha_equation )
688                        );
689
690                        m_render_state.blending.rgb_equation   = blend.rgb_equation;
691                        m_render_state.blending.alpha_equation = blend.alpha_equation;
692                }
693
694                if (( m_render_state.blending.color != blend.color ))
695                {
696                        glBlendColor( blend.color.r, blend.color.g, blend.color.b, blend.color.a );
697                        m_render_state.blending.color = blend.color;
698                }
699        }
700}
701
702
703void gl_context::apply_culling( const culling& cull )
704{
705        if ( m_render_state.culling.enabled != cull.enabled )
706        {
707                enable( GL_CULL_FACE, cull.enabled );
708                m_render_state.culling.enabled = cull.enabled;
709        }
710
711        if ( cull.enabled )
712        {
713                if ( m_render_state.culling.face != cull.face )
714                {
715                        glCullFace( culling_face_type_to_enum( cull.face ) );
716                        m_render_state.culling.face = cull.face;
717                }
718        }
719
720        if ( m_render_state.culling.order != cull.order )
721        {
722                glFrontFace( culling_order_type_to_enum( cull.order ) );
723                m_render_state.culling.order = cull.order;
724        }
725}
726
727
728void gl_context::force_apply_render_state( const render_state& state )
729{
730        enable( GL_CULL_FACE, state.culling.enabled );
731        glCullFace( culling_face_type_to_enum( state.culling.face ) );
732        glFrontFace( culling_order_type_to_enum( state.culling.order ) );
733
734        enable( GL_SCISSOR_TEST, state.scissor_test.enabled );
735        glScissor(
736                state.scissor_test.pos.x, state.scissor_test.pos.y,
737                state.scissor_test.dim.x, state.scissor_test.dim.y
738        );
739
740        enable( GL_STENCIL_TEST, state.stencil_test.enabled );
741        force_apply_stencil_face( GL_FRONT, state.stencil_test.front_face );
742        force_apply_stencil_face( GL_BACK,  state.stencil_test.back_face  );
743
744        enable( GL_DEPTH_TEST, state.depth_test.enabled );
745        glDepthFunc( depth_state_function_to_enum( state.depth_test.function ) );
746        glDepthRange( state.depth_range.near, state.depth_range.far );
747
748        enable( GL_BLEND, state.blending.enabled );
749        glBlendFuncSeparate(
750                blending_factor_to_enum( state.blending.src_rgb_factor ),
751                blending_factor_to_enum( state.blending.dst_rgb_factor ),
752                blending_factor_to_enum( state.blending.src_alpha_factor ),
753                blending_factor_to_enum( state.blending.dst_alpha_factor )
754        );
755        glBlendEquationSeparate(
756                blending_equation_to_enum( state.blending.rgb_equation ),
757                blending_equation_to_enum( state.blending.alpha_equation )
758        );
759        glBlendColor(
760                state.blending.color.r, state.blending.color.g,
761                state.blending.color.b, state.blending.color.a
762        );
763
764        glDepthMask( state.depth_mask );
765        glColorMask(
766                state.color_mask.red, state.color_mask.green,
767                state.color_mask.blue, state.color_mask.alpha
768        );
769        glPolygonMode( GL_FRONT_AND_BACK, polygon_mode_fill_to_enum( state.polygon_mode.fill ) );
770}
771
772void gl_context::force_apply_stencil_face( unsigned face, const stencil_test_face& stencil )
773{
774        glStencilOpSeparate( face,
775                stencil_operation_to_enum( stencil.op_fail ),
776                stencil_operation_to_enum( stencil.op_depth_fail ),
777                stencil_operation_to_enum( stencil.op_depth_pass )
778        );
779
780        glStencilFuncSeparate( face,
781                stencil_function_to_enum( stencil.function ),
782                stencil.ref_value,
783                stencil.mask
784        );
785}
786
787
788void gl_context::apply_render_state( const render_state& state )
789{
790        // apply_primitive_restart
791        apply_culling( state.culling );
792        // apply_program_point_size
793        // apply_rasterization_mode
794        apply_scissor_test( state.scissor_test );
795        apply_stencil_test( state.stencil_test );
796        apply_depth_test( state.depth_test );
797        apply_depth_range( state.depth_range );
798        apply_blending( state.blending );
799        apply_color_mask( state.color_mask );
800        apply_depth_mask( state.depth_mask );
801        apply_multisample( state.multisample );
802        apply_polygon_mode( state.polygon_mode );
803}
804
805
806gl_context::gl_context( device* a_device, void* a_handle )
807        : context( a_device ), m_handle( a_handle )
808{
809        // TODO: configurable:
810//      load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT | GL_EXT_TEXTURE_ARRAY );
811        m_active_slot = texture_slot( -1 );
812        force_apply_render_state( m_render_state );
813}
814
815
816nv::gl_context::~gl_context()
817{
818        while ( m_framebuffers.size() > 0 )
819                release( m_framebuffers.get_handle(0) );
820        while ( m_vertex_arrays.size() > 0 )
821                release( m_vertex_arrays.get_handle(0) );
822}
823
824void nv::gl_context::apply_engine_uniforms( program p, const scene_state& s )
825{
826        gl_program_info* info = static_cast<gl_device*>( m_device )->m_programs.get( p );
827        if ( info )
828        {
829                for ( auto& u : *info->m_engine_uniforms )
830                {
831                        u->set( this, &s );
832                }
833        }
834}
835
836void nv::gl_context::set_draw_buffers( uint32 count, const output_slot* slots )
837{
838        if ( count == 0 ) return;
839        if ( count == 1 )
840        {
841                set_draw_buffer( slots[0] );
842                return;
843        }
844        unsigned int buffers[8];
845        count = nv::min<uint32>( count, 8 );
846        for ( uint32 i = 0; i < count; ++i )
847        {
848                buffers[i] = output_slot_to_enum( slots[i] );
849                if ( slots[i] > OUTPUT_7 ) buffers[i] = 0;
850        }
851        glDrawBuffers( GLsizei( count ), buffers );
852}
853
854void nv::gl_context::set_draw_buffer( output_slot slot )
855{
856        glDrawBuffer( output_slot_to_enum(slot) );
857}
858
859void nv::gl_context::set_read_buffer( output_slot slot )
860{
861        glReadBuffer( output_slot_to_enum(slot) );
862}
863
864void gl_context::draw( primitive prim, const render_state& rs, program p, vertex_array va, nv::size_t count, nv::size_t first )
865{
866        apply_render_state( rs );
867        const vertex_array_info* info = m_vertex_arrays.get( va );
868        if ( count > 0 && info )
869        {
870                bind( p );
871                bind( va );
872                if ( info->index.is_valid() )
873                {
874                        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 ) );
875                }
876                else
877                {
878                        glDrawArrays( primitive_to_enum(prim), static_cast<GLint>( first ), static_cast<GLsizei>( count ) );
879                }
880                unbind( va );
881                //unbind( p );
882        }
883}
Note: See TracBrowser for help on using the repository browser.