Changeset 331 for trunk/src/gl
- Timestamp:
- 09/09/14 20:08:33 (11 years ago)
- Location:
- trunk/src/gl
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gl/gl_context.cc
r326 r331 24 24 nv::framebuffer nv::gl_context::create_framebuffer() 25 25 { 26 if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) )26 if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) && is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_BLIT ) ) 27 27 { 28 28 unsigned glid = 0; … … 31 31 gl_framebuffer_info* info = m_framebuffers.get( result ); 32 32 info->glid = glid; 33 info->depth_rb_glid = 0; 33 34 info->color_attachment_count = 0; 34 35 return result; … … 57 58 { 58 59 // TODO: release textures? 59 glBindFramebuffer(GL_FRAMEBUFFER, 0); 60 glDeleteFramebuffers(1, &info->glid); 60 glBindFramebuffer( GL_FRAMEBUFFER, 0 ); 61 glBindRenderbuffer( GL_RENDERBUFFER, 0 ); 62 if ( info->depth_rb_glid == 0 ) 63 glDeleteRenderbuffers( 1, &info->depth_rb_glid ); 64 glDeleteFramebuffers( 1, &info->glid ); 61 65 } 62 66 } … … 77 81 } 78 82 83 void nv::gl_context::attach( framebuffer f, output_slot slot, texture t ) 84 { 85 // TODO: framebuffer variable, so no re-binding? 86 // TODO: support 1d, 3d textures, cubemaps or layers? 87 // TODO: support GL_READ_FRAMEBUFFER? 88 const gl_framebuffer_info* info = m_framebuffers.get( f ); 89 const gl_texture_info* tinfo = (gl_texture_info*)m_device->get_texture_info( t ); 90 if ( info ) 91 { 92 glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); 93 unsigned gl_type = texture_type_to_enum( tinfo->type ); 94 95 if ( tinfo ) 96 { 97 // if ( tinfo->size.y == 0 ) 98 // glFramebufferTexture1D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, GL_TEXTURE_1D, tinfo->glid, 0 ); 99 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, gl_type, tinfo->glid, 0 ); 100 } 101 else 102 { 103 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0+(unsigned)slot, gl_type, 0, 0 ); 104 } 105 106 } 107 } 108 109 void nv::gl_context::attach( framebuffer f, texture depth ) 110 { 111 // TODO: framebuffer variable, so no re-binding? 112 // TODO: support GL_READ_FRAMEBUFFER? 113 const gl_framebuffer_info* info = m_framebuffers.get( f ); 114 const gl_texture_info* tinfo = (gl_texture_info*)m_device->get_texture_info( depth ); 115 if ( info ) 116 { 117 glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); 118 unsigned gl_type = texture_type_to_enum( tinfo->type ); 119 120 if ( tinfo ) 121 { 122 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, tinfo->glid, 0 ); 123 } 124 else 125 { 126 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gl_type, 0, 0 ); 127 } 128 } 129 } 130 131 void nv::gl_context::attach( framebuffer f, ivec2 size ) 132 { 133 // TODO: framebuffer variable, so no re-binding? 134 // TODO: support GL_READ_FRAMEBUFFER? 135 gl_framebuffer_info* info = m_framebuffers.get( f ); 136 if ( info ) 137 { 138 glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); 139 if ( info->depth_rb_glid ) glDeleteRenderbuffers( 1, &(info->depth_rb_glid) ); 140 glGenRenderbuffers( 1, &(info->depth_rb_glid) ); 141 glBindRenderbuffer( GL_RENDERBUFFER, info->depth_rb_glid ); 142 glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size.x, size.y ); 143 glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, info->depth_rb_glid ); 144 glBindRenderbuffer( GL_RENDERBUFFER, 0 ); 145 } 146 147 } 148 149 void nv::gl_context::blit( framebuffer f, clear_state::buffers_type mask, ivec2 src1, ivec2 src2, ivec2 dst1, ivec2 dst2 ) 150 { 151 gl_framebuffer_info* info = m_framebuffers.get( f ); 152 if ( info ) 153 { 154 glBindFramebuffer( GL_FRAMEBUFFER, info->glid ); 155 unsigned filter = mask == clear_state::COLOR_BUFFER ? GL_LINEAR : GL_NEAREST; 156 glBlitFramebuffer( src1.x, src1.y, src2.x, src2.y, dst1.x, dst1.y, dst2.x, dst2.y, clear_state_buffers_to_mask( mask ), filter ); 157 } 158 } 159 160 161 bool nv::gl_context::check( framebuffer_slot ft ) 162 { 163 if ( is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_OBJECT ) && is_gl_extension_loaded( GL_EXT_FRAMEBUFFER_BLIT ) ) 164 { 165 unsigned result = glCheckFramebufferStatus( framebuffer_slot_to_enum(ft) ); 166 if ( result == GL_FRAMEBUFFER_COMPLETE ) return true; 167 switch ( result ) 168 { 169 case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete attachment!" ); break; 170 case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer missing attachment!" ); break; 171 case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete dimensions!" ); break; 172 case GL_FRAMEBUFFER_INCOMPLETE_FORMATS : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete formats!" ); break; 173 case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete draw buffer!" ); break; 174 case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer incomplete read buffer!" ); break; 175 case GL_FRAMEBUFFER_UNSUPPORTED : NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer format combination unsupported!" ); break; 176 default : NV_LOG( LOG_ERROR, "gl_context::check : Unknown Framebuffer error! (" << result << ")" ); break; 177 } 178 } 179 else 180 { 181 NV_LOG( LOG_ERROR, "gl_context::check : Framebuffer extensions not loaded!" ); 182 } 183 return false; 184 } 185 186 void nv::gl_context::bind( framebuffer f, framebuffer_slot ft /*= FRAMEBUFFER_BOTH */ ) 187 { 188 const gl_framebuffer_info* info = m_framebuffers.get( f ); 189 if ( info ) 190 { 191 glBindFramebuffer( framebuffer_slot_to_enum(ft), info->glid ); 192 } 193 else 194 { 195 glBindFramebuffer( framebuffer_slot_to_enum(ft), 0 ); 196 } 197 } 79 198 80 199 void gl_context::bind( texture t, texture_slot slot ) … … 84 203 { 85 204 glActiveTexture( GL_TEXTURE0 + static_cast< GLenum >( slot ) ); 86 glBindTexture( GL_TEXTURE_2D, info->glid );205 glBindTexture( texture_type_to_enum( info->type ), info->glid ); 87 206 } 88 207 } … … 151 270 } 152 271 153 void nv::gl_context::bind( framebuffer f )154 {155 const gl_framebuffer_info* info = m_framebuffers.get( f );156 if ( info )157 {158 glBindFramebuffer( GL_FRAMEBUFFER, info->glid );159 }160 }161 162 272 void nv::gl_context::unbind( program ) 163 273 { … … 199 309 glBindFramebuffer( GL_FRAMEBUFFER, 0 ); 200 310 } 311 glDrawBuffer( GL_BACK ); 312 glReadBuffer( GL_BACK ); 201 313 } 202 314 … … 206 318 if ( info ) 207 319 { 208 image_format format = info->format; 209 ivec2 size = info->size; 210 211 glBindTexture( GL_TEXTURE_2D, info->glid ); 212 glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(format.format), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data ); 320 image_format format = info->format; 321 ivec2 size = info->size; 322 unsigned gl_type = texture_type_to_enum( info->type ); 323 324 glBindTexture( gl_type, info->glid ); 325 glTexImage2D( gl_type, 0, (GLint)nv::image_format_to_internal_enum(format.format), size.x, size.y, 0, nv::image_format_to_enum(format.format), nv::datatype_to_gl_enum(format.type), data ); 213 326 } 214 327 } … … 290 403 m_render_state.stencil_test.enabled = stencil.enabled; 291 404 } 292 405 293 406 if ( stencil.enabled ) 294 407 { … … 579 692 : context( a_device ), m_handle( a_handle ) 580 693 { 694 // TODO: configurable: 695 load_gl_extensions( GL_EXT_FRAMEBUFFER_BLIT | GL_EXT_FRAMEBUFFER_OBJECT ); 581 696 force_apply_render_state( m_render_state ); 582 697 } … … 603 718 } 604 719 720 void nv::gl_context::set_draw_buffers( uint32 count, output_slot* slots ) 721 { 722 unsigned int buffers[8]; 723 count = glm::min<uint32>( count, 8 ); 724 for ( uint32 i = 0; i < count; ++i ) 725 { 726 buffers[i] = output_slot_to_enum( slots[i] ); 727 if ( slots[i] > OUTPUT_7 ) buffers[i] = 0; 728 } 729 glDrawBuffers( count, buffers ); 730 } 731 732 void nv::gl_context::set_draw_buffer( output_slot slot ) 733 { 734 glDrawBuffer( output_slot_to_enum(slot) ); 735 } 736 737 void nv::gl_context::set_read_buffer( output_slot slot ) 738 { 739 glReadBuffer( output_slot_to_enum(slot) ); 740 } 741 605 742 void gl_context::draw( primitive prim, const render_state& rs, program p, vertex_array va, size_t count ) 606 743 { -
trunk/src/gl/gl_device.cc
r326 r331 62 62 } 63 63 64 nv::texture nv::gl_device::create_texture( ivec2 size, image_format aformat, sampler asampler, void* data /*= nullptr */ )64 nv::texture nv::gl_device::create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, void* data /*= nullptr */ ) 65 65 { 66 66 unsigned glid = 0; 67 unsigned gl_type = texture_type_to_enum( type ); 67 68 glGenTextures( 1, &glid ); 68 69 69 glBindTexture( GL_TEXTURE_2D, glid );70 glBindTexture( gl_type, glid ); 70 71 71 72 // Detect if mipmapping was requested 72 if (( asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST ) || 73 ( asampler.filter_max != sampler::LINEAR && asampler.filter_max != sampler::NEAREST )) 74 { 75 glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); 76 } 77 78 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_min ) ); 79 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_max ) ); 80 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, (int)nv::sampler_wrap_to_enum( asampler.wrap_s) ); 81 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, (int)nv::sampler_wrap_to_enum( asampler.wrap_t) ); 82 83 if (data) 84 { 85 glTexImage2D( GL_TEXTURE_2D, 0, (GLint)nv::image_format_to_enum(aformat.format), size.x, size.y, 0, nv::image_format_to_enum(aformat.format), nv::datatype_to_gl_enum(aformat.type), data ); 86 } 87 88 glBindTexture( GL_TEXTURE_2D, 0 ); 73 if ( gl_type == GL_TEXTURE_2D && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST ) 74 { 75 // TODO: This should not be done if we use framebuffers! 76 glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE); 77 } 78 79 if ( asampler.filter_max != sampler::NEAREST ) 80 { 81 asampler.filter_max = sampler::LINEAR; 82 } 83 84 glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_min ) ); 85 glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, (int)nv::sampler_filter_to_enum( asampler.filter_max ) ); 86 glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, (int)nv::sampler_wrap_to_enum( asampler.wrap_s) ); 87 glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, (int)nv::sampler_wrap_to_enum( asampler.wrap_t) ); 88 89 glTexImage2D( gl_type, 0, (GLint)nv::image_format_to_internal_enum(aformat.format), size.x, size.y, 0, nv::image_format_to_enum(aformat.format), nv::datatype_to_gl_enum(aformat.type), data ); 90 91 glBindTexture( gl_type, 0 ); 89 92 90 93 texture result = m_textures.create(); 91 94 gl_texture_info* info = m_textures.get( result ); 95 info->type = type; 92 96 info->format = aformat; 93 97 info->tsampler = asampler; -
trunk/src/gl/gl_enum.cc
r323 r331 8 8 9 9 using namespace nv; 10 11 unsigned int nv::texture_type_to_enum( texture_type type ) 12 { 13 switch( type ) 14 { 15 case TEXTURE_1D : return GL_TEXTURE_1D; 16 case TEXTURE_2D : return GL_TEXTURE_2D; 17 case TEXTURE_RECT : return GL_TEXTURE_RECTANGLE; 18 case TEXTURE_3D : return GL_TEXTURE_3D; 19 case TEXTURE_CUBE : return GL_TEXTURE_CUBE_MAP; 20 NV_RETURN_COVERED_DEFAULT( 0 ); 21 } 22 } 10 23 11 24 unsigned int nv::clear_state_buffers_to_mask( clear_state::buffers_type type ) … … 167 180 } 168 181 182 unsigned int nv::image_format_to_internal_enum( pixel_format format ) 183 { 184 switch( format ) 185 { 186 case RGB : return GL_RGB8; 187 case RGBA : return GL_RGBA8; 188 NV_RETURN_COVERED_DEFAULT( 0 ); 189 } 190 } 191 192 169 193 unsigned int nv::sampler_filter_to_enum( sampler::filter filter ) 170 194 { … … 207 231 } 208 232 } 233 234 unsigned int nv::framebuffer_slot_to_enum( framebuffer_slot slot ) 235 { 236 switch( slot ) 237 { 238 case READ_FRAMEBUFFER : return GL_READ_FRAMEBUFFER; 239 case DRAW_FRAMEBUFFER : return GL_DRAW_FRAMEBUFFER; 240 case FRAMEBUFFER : return GL_FRAMEBUFFER; 241 NV_RETURN_COVERED_DEFAULT( 0 ); 242 } 243 } 244 245 246 unsigned int nv::output_slot_to_enum( output_slot slot ) 247 { 248 switch( slot ) 249 { 250 case OUTPUT_0 : return GL_COLOR_ATTACHMENT0; 251 case OUTPUT_1 : return GL_COLOR_ATTACHMENT1; 252 case OUTPUT_2 : return GL_COLOR_ATTACHMENT2; 253 case OUTPUT_3 : return GL_COLOR_ATTACHMENT3; 254 case OUTPUT_4 : return GL_COLOR_ATTACHMENT4; 255 case OUTPUT_5 : return GL_COLOR_ATTACHMENT5; 256 case OUTPUT_6 : return GL_COLOR_ATTACHMENT6; 257 case OUTPUT_7 : return GL_COLOR_ATTACHMENT7; 258 case OUTPUT_NONE : return 0; 259 case OUTPUT_FRONT : return GL_FRONT; 260 case OUTPUT_BACK : return GL_BACK; 261 NV_RETURN_COVERED_DEFAULT( 0 ); 262 } 263 } 264 209 265 210 266 unsigned int nv::datatype_to_gl_enum( datatype type ) … … 257 313 case GL_INT_VEC4 : return INT_VECTOR_4; 258 314 // TODO: separate types? 259 case GL_SAMPLER_1D : return INT; 260 case GL_SAMPLER_2D : return INT; 261 case GL_SAMPLER_3D : return INT; 262 case GL_SAMPLER_CUBE : return INT; 263 case GL_SAMPLER_1D_SHADOW : return INT; 264 case GL_SAMPLER_2D_SHADOW : return INT; 315 case GL_SAMPLER_1D : return INT; 316 case GL_SAMPLER_2D : return INT; 317 case GL_SAMPLER_3D : return INT; 318 case GL_SAMPLER_2D_RECT : return INT; 319 case GL_SAMPLER_2D_RECT_SHADOW : return INT; 320 case GL_SAMPLER_CUBE : return INT; 321 case GL_SAMPLER_1D_SHADOW : return INT; 322 case GL_SAMPLER_2D_SHADOW : return INT; 265 323 // TODO: implement? 266 324 // case GL_BOOL
Note: See TracChangeset
for help on using the changeset viewer.