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_device.hh"
|
---|
8 |
|
---|
9 | #include "nv/gl/gl_window.hh"
|
---|
10 | #include "nv/core/logging.hh"
|
---|
11 | #include "nv/lib/sdl_image.hh"
|
---|
12 | #include "nv/gl/gl_enum.hh"
|
---|
13 | #include "nv/lib/gl.hh"
|
---|
14 |
|
---|
15 | using namespace nv;
|
---|
16 |
|
---|
17 | gl_device::gl_device()
|
---|
18 | {
|
---|
19 | m_shader_header.append( "#version 330 core\n" );
|
---|
20 | for ( auto& i : get_uniform_factory() )
|
---|
21 | m_shader_header.append( "uniform "+datatype_to_glsl_type( i.second->get_datatype() )+" "+ i.first +";\n" );
|
---|
22 | for ( auto& i : get_link_uniform_factory() )
|
---|
23 | m_shader_header.append( "uniform sampler2D "+i.first +";\n" );
|
---|
24 | }
|
---|
25 |
|
---|
26 | program gl_device::create_program( string_view vs_source, string_view fs_source )
|
---|
27 | {
|
---|
28 | program result = m_programs.create();
|
---|
29 | gl_program_info* info = m_programs.get( result );
|
---|
30 |
|
---|
31 | info->m_attribute_map = new attribute_map;
|
---|
32 | info->m_engine_uniforms = new engine_uniform_list;
|
---|
33 | info->m_uniform_map = new uniform_map;
|
---|
34 |
|
---|
35 | info->glid = glCreateProgram();
|
---|
36 | compile( info, vs_source, fs_source );
|
---|
37 | prepare_program( result );
|
---|
38 | return result;
|
---|
39 | }
|
---|
40 |
|
---|
41 | // this is a temporary function that will be removed once we find a way to
|
---|
42 | // pass binary file data around
|
---|
43 | image_data* gl_device::create_image_data( string_view filename )
|
---|
44 | {
|
---|
45 | load_sdl_image_library();
|
---|
46 | SDL_Surface* image = IMG_Load( filename.data() );
|
---|
47 | if (!image)
|
---|
48 | {
|
---|
49 | NV_LOG_ERROR( "Image file ", filename, " not found!" );
|
---|
50 | return nullptr;
|
---|
51 | }
|
---|
52 | // TODO: BGR vs RGB, single channel
|
---|
53 | pixel_format pformat = RGBA;
|
---|
54 | switch ( image->format->BytesPerPixel )
|
---|
55 | {
|
---|
56 | case 4: pformat = RGBA; break;
|
---|
57 | case 3: pformat = RGB; break;
|
---|
58 | case 1: pformat = RED; break;
|
---|
59 | default: NV_ASSERT( false, "BytesPerPixel != 4,3 or 1!" );
|
---|
60 | }
|
---|
61 | image_format format( pformat, UBYTE );
|
---|
62 | image_data* data = new image_data( format, ivec2( image->w, image->h ), static_cast<nv::uint8*>( image->pixels ) );
|
---|
63 | return data;
|
---|
64 | }
|
---|
65 |
|
---|
66 | // this is a temporary function that will be removed once we find a way to
|
---|
67 | // pass binary file data around
|
---|
68 | image_data* gl_device::create_image_data( const uint8* data, uint32 size )
|
---|
69 | {
|
---|
70 | load_sdl_image_library();
|
---|
71 | SDL_Surface* image = IMG_LoadTyped_RW( SDL_RWFromMem( const_cast<uint8*>( data ), int( size ) ), 1, "png" );
|
---|
72 | if ( !image )
|
---|
73 | {
|
---|
74 | NV_LOG_ERROR( "Image binary data cannot be loaded found!" );
|
---|
75 | return nullptr;
|
---|
76 | }
|
---|
77 | // TODO: BGR vs RGB, single channel
|
---|
78 | NV_ASSERT( image->format->BytesPerPixel > 2, "bytes per pixel > 2!" );
|
---|
79 | image_format format( image->format->BytesPerPixel == 3 ? RGB : RGBA, UBYTE );
|
---|
80 | image_data* idata = new image_data( format, ivec2( image->w, image->h ), static_cast<nv::uint8*>( image->pixels ) );
|
---|
81 | return idata;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | gl_device::~gl_device()
|
---|
86 | {
|
---|
87 | while ( m_textures.size() > 0 )
|
---|
88 | release( m_textures.get_handle(0) );
|
---|
89 | while ( m_buffers.size() > 0 )
|
---|
90 | release( m_buffers.get_handle(0) );
|
---|
91 | while ( m_programs.size() > 0 )
|
---|
92 | release( m_programs.get_handle(0) );
|
---|
93 | }
|
---|
94 |
|
---|
95 | nv::texture nv::gl_device::create_texture( texture_type type, ivec2 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
96 | {
|
---|
97 | NV_ASSERT_ALWAYS( type != TEXTURE_1D_BUFFER && type != TEXTURE_3D && type != TEXTURE_2D_ARRAY, "2D texture type expected!" );
|
---|
98 | unsigned glid = 0;
|
---|
99 | unsigned gl_type = texture_type_to_enum( type );
|
---|
100 | GLint gl_internal = GLint( nv::image_format_to_internal_enum( aformat.format ) );
|
---|
101 | unsigned gl_enum = nv::image_format_to_enum( aformat.format );
|
---|
102 |
|
---|
103 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
104 |
|
---|
105 | glGenTextures( 1, &glid );
|
---|
106 |
|
---|
107 | glBindTexture( gl_type, glid );
|
---|
108 |
|
---|
109 | // Detect if mipmapping was requested
|
---|
110 | if ( gl_type == GL_TEXTURE_2D && gl_enum != GL_RED_INTEGER && asampler.filter_min != sampler::LINEAR && asampler.filter_min != sampler::NEAREST )
|
---|
111 | {
|
---|
112 | // TODO: This should not be done if we use framebuffers!
|
---|
113 | glTexParameteri( gl_type, GL_GENERATE_MIPMAP, GL_TRUE);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
117 | {
|
---|
118 | asampler.filter_max = sampler::LINEAR;
|
---|
119 | }
|
---|
120 |
|
---|
121 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
122 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
123 |
|
---|
124 | if ( gl_enum != GL_RED_INTEGER )
|
---|
125 | {
|
---|
126 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
127 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
128 | }
|
---|
129 |
|
---|
130 | if ( is_depth )
|
---|
131 | {
|
---|
132 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
133 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
134 |
|
---|
135 | // This is to allow usage of shadow2DProj function in the shader
|
---|
136 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
137 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
138 | }
|
---|
139 |
|
---|
140 | // #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
|
---|
141 | // #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
|
---|
142 | //
|
---|
143 | // float aniso = 0.0f;
|
---|
144 | // glGetFloatv( GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso );
|
---|
145 | // NV_LOG_INFO( "Anisotropy at ", aniso, " (", int( aniso ), " ) " );
|
---|
146 | // glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso );
|
---|
147 |
|
---|
148 | glTexImage2D( gl_type, 0, gl_internal, size.x, size.y, 0, gl_enum, nv::datatype_to_gl_enum(aformat.type), data );
|
---|
149 |
|
---|
150 | glBindTexture( gl_type, 0 );
|
---|
151 |
|
---|
152 | texture result = m_textures.create();
|
---|
153 | gl_texture_info* info = m_textures.get( result );
|
---|
154 | info->type = type;
|
---|
155 | info->format = aformat;
|
---|
156 | info->tsampler = asampler;
|
---|
157 | info->size = ivec3( size.x, size.y, 1 );
|
---|
158 | info->glid = glid;
|
---|
159 | return result;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | nv::texture nv::gl_device::create_texture( texture_type type, pixel_format format )
|
---|
164 | {
|
---|
165 | NV_ASSERT_ALWAYS( type == TEXTURE_1D_BUFFER );
|
---|
166 | unsigned glid = 0;
|
---|
167 | unsigned gl_type = texture_type_to_enum( type );
|
---|
168 |
|
---|
169 | glGenTextures( 1, &glid );
|
---|
170 |
|
---|
171 | texture result = m_textures.create();
|
---|
172 | gl_texture_info* info = m_textures.get( result );
|
---|
173 | info->type = type;
|
---|
174 | info->format = format;
|
---|
175 | info->tsampler = sampler();
|
---|
176 | info->size = ivec3( 1, 1, 1 );
|
---|
177 | info->glid = glid;
|
---|
178 |
|
---|
179 | return result;
|
---|
180 | }
|
---|
181 |
|
---|
182 | nv::texture nv::gl_device::create_texture( texture_type type, ivec3 size, image_format aformat, sampler asampler, const void* data /*= nullptr */ )
|
---|
183 | {
|
---|
184 | NV_ASSERT_ALWAYS( type == TEXTURE_3D || type == TEXTURE_2D_ARRAY, "3D texture type expected!" );
|
---|
185 | unsigned glid = 0;
|
---|
186 | unsigned gl_type = texture_type_to_enum( type );
|
---|
187 |
|
---|
188 | bool is_depth = aformat.format == DEPTH16 || aformat.format == DEPTH24 || aformat.format == DEPTH32;
|
---|
189 |
|
---|
190 | glGenTextures( 1, &glid );
|
---|
191 | glBindTexture( gl_type, glid );
|
---|
192 |
|
---|
193 | if ( asampler.filter_max != sampler::NEAREST )
|
---|
194 | {
|
---|
195 | asampler.filter_max = sampler::LINEAR;
|
---|
196 | }
|
---|
197 |
|
---|
198 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_min ) ) );
|
---|
199 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GLint( nv::sampler_filter_to_enum( asampler.filter_max ) ) );
|
---|
200 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_S, GLint( nv::sampler_wrap_to_enum( asampler.wrap_s ) ) );
|
---|
201 | glTexParameteri( gl_type, GL_TEXTURE_WRAP_T, GLint( nv::sampler_wrap_to_enum( asampler.wrap_t ) ) );
|
---|
202 |
|
---|
203 | if ( is_depth )
|
---|
204 | {
|
---|
205 | glTexParameteri( gl_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
|
---|
206 | glTexParameteri( gl_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
---|
207 |
|
---|
208 | // This is to allow usage of shadow2DProj function in the shader
|
---|
209 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE );
|
---|
210 | glTexParameteri( gl_type, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL );
|
---|
211 | }
|
---|
212 |
|
---|
213 | //glTexStorage3D( GL_TEXTURE_2D_ARRAY, mipLevelCount, GL_RGBA8, width, height, layerCount );
|
---|
214 | glTexImage3D( gl_type, 0, GLint( nv::image_format_to_internal_enum( aformat.format ) ), size.x, size.y, size.z, 0, nv::image_format_to_enum( aformat.format ), nv::datatype_to_gl_enum( aformat.type ), data );
|
---|
215 | glBindTexture( gl_type, 0 );
|
---|
216 |
|
---|
217 | texture result = m_textures.create();
|
---|
218 | gl_texture_info* info = m_textures.get( result );
|
---|
219 | info->type = type;
|
---|
220 | info->format = aformat;
|
---|
221 | info->tsampler = asampler;
|
---|
222 | info->size = size;
|
---|
223 | info->glid = glid;
|
---|
224 | return result;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 |
|
---|
229 | void nv::gl_device::release( texture t )
|
---|
230 | {
|
---|
231 | gl_texture_info* info = m_textures.get( t );
|
---|
232 | if ( info )
|
---|
233 | {
|
---|
234 | if ( info->glid != 0 )
|
---|
235 | {
|
---|
236 | glDeleteTextures( 1, &(info->glid) );
|
---|
237 | }
|
---|
238 | m_textures.destroy( t );
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | void nv::gl_device::release( buffer b )
|
---|
243 | {
|
---|
244 | gl_buffer_info* info = m_buffers.get( b );
|
---|
245 | if ( info )
|
---|
246 | {
|
---|
247 | if ( info->glid != 0 )
|
---|
248 | {
|
---|
249 | glDeleteBuffers( 1, &(info->glid) );
|
---|
250 | }
|
---|
251 | m_buffers.destroy( b );
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | const texture_info* nv::gl_device::get_texture_info( texture t ) const
|
---|
256 | {
|
---|
257 | return m_textures.get( t );
|
---|
258 | }
|
---|
259 |
|
---|
260 | nv::buffer nv::gl_device::create_buffer( buffer_type type, buffer_hint hint, size_t size, const void* source /*= nullptr */ )
|
---|
261 | {
|
---|
262 | unsigned glid = 0;
|
---|
263 | unsigned glenum = buffer_type_to_enum( type );
|
---|
264 | glGenBuffers( 1, &glid );
|
---|
265 |
|
---|
266 | if ( size > 0 )
|
---|
267 | {
|
---|
268 | glBindBuffer( glenum, glid );
|
---|
269 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( hint ) );
|
---|
270 | glBindBuffer( glenum, 0 );
|
---|
271 | }
|
---|
272 |
|
---|
273 | buffer result = m_buffers.create();
|
---|
274 | gl_buffer_info* info = m_buffers.get( result );
|
---|
275 | info->type = type;
|
---|
276 | info->hint = hint;
|
---|
277 | info->size = size;
|
---|
278 | info->glid = glid;
|
---|
279 | return result;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void nv::gl_device::create_buffer( buffer b, size_t size, const void* source )
|
---|
283 | {
|
---|
284 | gl_buffer_info* info = m_buffers.get( b );
|
---|
285 | if ( info )
|
---|
286 | {
|
---|
287 | unsigned glenum = buffer_type_to_enum( info->type );
|
---|
288 | glBindBuffer( glenum, info->glid );
|
---|
289 | glBufferData( glenum, GLsizeiptr( size ), source, buffer_hint_to_enum( info->hint ) );
|
---|
290 | glBindBuffer( glenum, 0 );
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | const buffer_info* nv::gl_device::get_buffer_info( buffer t ) const
|
---|
295 | {
|
---|
296 | return m_buffers.get( t );
|
---|
297 | }
|
---|
298 |
|
---|
299 | void nv::gl_device::release( program p )
|
---|
300 | {
|
---|
301 | gl_program_info* info = m_programs.get( p );
|
---|
302 | if ( info )
|
---|
303 | {
|
---|
304 | for ( auto& i : *info->m_uniform_map )
|
---|
305 | delete i.second;
|
---|
306 |
|
---|
307 | glDetachShader( info->glid, info->glidv );
|
---|
308 | glDetachShader( info->glid, info->glidf );
|
---|
309 | glDeleteShader( info->glidv );
|
---|
310 | glDeleteShader( info->glidf );
|
---|
311 | glDeleteProgram( info->glid );
|
---|
312 |
|
---|
313 | delete info->m_attribute_map;
|
---|
314 | delete info->m_engine_uniforms;
|
---|
315 | delete info->m_uniform_map;
|
---|
316 |
|
---|
317 | m_programs.destroy( p );
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | void nv::gl_device::prepare_program( program p )
|
---|
322 | {
|
---|
323 | gl_program_info* info = m_programs.get( p );
|
---|
324 | if ( info )
|
---|
325 | {
|
---|
326 | auto& map = get_uniform_factory();
|
---|
327 | auto& lmap = get_link_uniform_factory();
|
---|
328 |
|
---|
329 | for ( auto& i : *info->m_uniform_map )
|
---|
330 | {
|
---|
331 | auto j = lmap.find( i.first );
|
---|
332 | if ( j != lmap.end() )
|
---|
333 | {
|
---|
334 | j->second->set( i.second );
|
---|
335 | }
|
---|
336 |
|
---|
337 | auto k = map.find( i.first );
|
---|
338 | if ( k != map.end() )
|
---|
339 | {
|
---|
340 | info->m_engine_uniforms->push_back( k->second->create( i.second ) );
|
---|
341 | }
|
---|
342 | }
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | uniform_base* nv::gl_device::get_uniform( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
347 | {
|
---|
348 | const gl_program_info* info = m_programs.get( p );
|
---|
349 | {
|
---|
350 | nv::uniform_map::const_iterator i = info->m_uniform_map->find( name );
|
---|
351 | if ( i != info->m_uniform_map->end() )
|
---|
352 | {
|
---|
353 | return i->second;
|
---|
354 | }
|
---|
355 | if ( fatal )
|
---|
356 | {
|
---|
357 | NV_LOG_CRITICAL( "gl_device : uniform '", name, "' not found in program!" );
|
---|
358 | NV_ABORT( "gl_device : uniform not found!" );
|
---|
359 | }
|
---|
360 | }
|
---|
361 | return nullptr;
|
---|
362 | }
|
---|
363 |
|
---|
364 | int nv::gl_device::get_attribute_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
365 | {
|
---|
366 | const gl_program_info* info = m_programs.get( p );
|
---|
367 | if ( info )
|
---|
368 | {
|
---|
369 | attribute_map::const_iterator i = info->m_attribute_map->find( name );
|
---|
370 | if ( i != info->m_attribute_map->end() )
|
---|
371 | {
|
---|
372 | return i->second.location;
|
---|
373 | }
|
---|
374 | if ( fatal )
|
---|
375 | {
|
---|
376 | NV_LOG_CRITICAL( "gl_device : attribute '", name, "' not found in program!" );
|
---|
377 | NV_ABORT( "gl_device : attribute not found!" );
|
---|
378 | }
|
---|
379 | }
|
---|
380 | return -1;
|
---|
381 | }
|
---|
382 |
|
---|
383 | bool nv::gl_device::bind_block( program p, const string_view& name, uint32 index )
|
---|
384 | {
|
---|
385 | const gl_program_info* info = m_programs.get( p );
|
---|
386 | if ( info )
|
---|
387 | {
|
---|
388 | int id = get_block_location( p, name, false );
|
---|
389 | if ( id < 0 ) return false;
|
---|
390 | glUniformBlockBinding( info->glid, unsigned( id ), index );
|
---|
391 | return true;
|
---|
392 | }
|
---|
393 | return false;
|
---|
394 | }
|
---|
395 |
|
---|
396 | int nv::gl_device::get_block_location( program p, const string_view& name, bool fatal /*= true */ ) const
|
---|
397 | {
|
---|
398 | const gl_program_info* info = m_programs.get( p );
|
---|
399 | if ( info )
|
---|
400 | {
|
---|
401 | GLuint result = glGetUniformBlockIndex( info->glid, name.data() );
|
---|
402 | if ( result != GL_INVALID_INDEX ) return static_cast<int>( result );
|
---|
403 | if ( fatal )
|
---|
404 | {
|
---|
405 | NV_LOG_CRITICAL( "gl_device : block '", name, "' not found in program!" );
|
---|
406 | NV_ABORT( "gl_device : block not found!" );
|
---|
407 | }
|
---|
408 | }
|
---|
409 | return -1;
|
---|
410 | }
|
---|
411 |
|
---|
412 | bool nv::gl_device::compile( gl_program_info* p, string_view vertex_program, string_view fragment_program )
|
---|
413 | {
|
---|
414 | if (!compile( GL_VERTEX_SHADER, vertex_program, p->glidv )) { return false; }
|
---|
415 | if (!compile( GL_FRAGMENT_SHADER, fragment_program, p->glidf )) { return false; }
|
---|
416 |
|
---|
417 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::POSITION ), "nv_position" );
|
---|
418 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TEXCOORD ), "nv_texcoord" );
|
---|
419 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::NORMAL ), "nv_normal" );
|
---|
420 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::COLOR ), "nv_color" );
|
---|
421 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::TANGENT ), "nv_tangent" );
|
---|
422 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEINDEX ), "nv_boneindex" );
|
---|
423 | glBindAttribLocation( p->glid, static_cast<GLuint>( slot::BONEWEIGHT ), "nv_boneweight");
|
---|
424 |
|
---|
425 | glAttachShader( p->glid, p->glidf );
|
---|
426 | glAttachShader( p->glid, p->glidv );
|
---|
427 | glLinkProgram( p->glid );
|
---|
428 |
|
---|
429 | const uint32 buffer_size = 2048;
|
---|
430 | char buffer[ buffer_size ] = { 0 };
|
---|
431 | int length;
|
---|
432 | int status;
|
---|
433 |
|
---|
434 | glGetProgramiv( p->glid, GL_LINK_STATUS, &status );
|
---|
435 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
436 |
|
---|
437 | NV_LOG_INFO( "Program #", p->glid, (status == GL_FALSE ? " failed to compile!" : " compiled successfully.") );
|
---|
438 |
|
---|
439 | if ( length > 0 )
|
---|
440 | {
|
---|
441 | NV_LOG_INFO( "Program #", p->glid, " log: ", string_view( buffer, size_t( length ) ) );
|
---|
442 | }
|
---|
443 |
|
---|
444 | if ( status == GL_FALSE )
|
---|
445 | {
|
---|
446 | return false;
|
---|
447 | }
|
---|
448 |
|
---|
449 | glValidateProgram( p->glid );
|
---|
450 | glGetProgramiv( p->glid, GL_VALIDATE_STATUS, &status );
|
---|
451 |
|
---|
452 | if ( status == GL_FALSE )
|
---|
453 | {
|
---|
454 | glGetProgramInfoLog( p->glid, buffer_size, &length, buffer );
|
---|
455 | NV_LOG_ERROR( "Program #", p->glid, " validation error : ", buffer );
|
---|
456 | //return false;
|
---|
457 | }
|
---|
458 | load_attributes( p );
|
---|
459 | load_uniforms( p );
|
---|
460 | return true;
|
---|
461 | }
|
---|
462 |
|
---|
463 | void nv::gl_device::update_uniforms( gl_program_info* p )
|
---|
464 | {
|
---|
465 | for ( auto& i : *p->m_uniform_map )
|
---|
466 | {
|
---|
467 | uniform_base* ubase = i.second;
|
---|
468 | if ( ubase->is_dirty() )
|
---|
469 | {
|
---|
470 | GLint uloc = ubase->get_location();
|
---|
471 | GLsizei size = static_cast<GLsizei>( ubase->get_length() );
|
---|
472 | switch( ubase->get_type() )
|
---|
473 | {
|
---|
474 | case FLOAT : glUniform1fv( uloc, size, static_cast< uniform< enum_to_type< FLOAT >::type >*>( ubase )->get_value() ); break;
|
---|
475 | case INT : glUniform1iv( uloc, size, static_cast< uniform< enum_to_type< INT >::type >*>( ubase )->get_value() ); break;
|
---|
476 | case FLOAT_VECTOR_2 : glUniform2fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
|
---|
477 | case FLOAT_VECTOR_3 : glUniform3fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
|
---|
478 | case FLOAT_VECTOR_4 : glUniform4fv( uloc, size, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
|
---|
479 | case INT_VECTOR_2 : glUniform2iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_2 >::type >*>( ubase )->get_value())); break;
|
---|
480 | case INT_VECTOR_3 : glUniform3iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_3 >::type >*>( ubase )->get_value())); break;
|
---|
481 | case INT_VECTOR_4 : glUniform4iv( uloc, size, reinterpret_cast<const GLint*>( static_cast< uniform< enum_to_type< INT_VECTOR_4 >::type >*>( ubase )->get_value())); break;
|
---|
482 | case FLOAT_MATRIX_2 : glUniformMatrix2fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_2 >::type >*>( ubase )->get_value())); break;
|
---|
483 | case FLOAT_MATRIX_3 : glUniformMatrix3fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_3 >::type >*>( ubase )->get_value())); break;
|
---|
484 | case FLOAT_MATRIX_4 : glUniformMatrix4fv( uloc, size, GL_FALSE, reinterpret_cast<const GLfloat*>( static_cast< uniform< enum_to_type< FLOAT_MATRIX_4 >::type >*>( ubase )->get_value())); break;
|
---|
485 | default : break; // error?
|
---|
486 | }
|
---|
487 | ubase->clean();
|
---|
488 | }
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | void nv::gl_device::load_attributes( gl_program_info* p )
|
---|
493 | {
|
---|
494 | int params;
|
---|
495 | glGetProgramiv( p->glid, GL_ACTIVE_ATTRIBUTES, ¶ms );
|
---|
496 |
|
---|
497 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
498 | {
|
---|
499 | int attr_nlen;
|
---|
500 | int attr_len;
|
---|
501 | unsigned attr_type;
|
---|
502 | char name_buffer[128];
|
---|
503 |
|
---|
504 | glGetActiveAttrib( p->glid, i, 128, &attr_nlen, &attr_len, &attr_type, name_buffer );
|
---|
505 |
|
---|
506 | string_view name( name_buffer, size_t( attr_nlen ) );
|
---|
507 |
|
---|
508 | // skip built-ins
|
---|
509 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
510 |
|
---|
511 | int attr_loc = glGetAttribLocation( p->glid, name.data() );
|
---|
512 |
|
---|
513 | attribute& attr = (*p->m_attribute_map)[ name ];
|
---|
514 | attr.location = attr_loc;
|
---|
515 | attr.type = gl_enum_to_datatype( attr_type );
|
---|
516 | attr.length = attr_len;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | void nv::gl_device::load_uniforms( gl_program_info* p )
|
---|
521 | {
|
---|
522 | int params;
|
---|
523 | int bparams;
|
---|
524 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORMS, ¶ms );
|
---|
525 | glGetProgramiv( p->glid, GL_ACTIVE_UNIFORM_BLOCKS, &bparams );
|
---|
526 |
|
---|
527 | for ( unsigned i = 0; i < unsigned( params ); ++i )
|
---|
528 | {
|
---|
529 | int uni_nlen;
|
---|
530 | int uni_len;
|
---|
531 | unsigned uni_type;
|
---|
532 | char name_buffer[128];
|
---|
533 |
|
---|
534 | glGetActiveUniform( p->glid, i, 128, &uni_nlen, &uni_len, &uni_type, name_buffer );
|
---|
535 |
|
---|
536 | string_view name( name_buffer, size_t( uni_nlen ) );
|
---|
537 |
|
---|
538 | // skip built-ins
|
---|
539 | if ( name.substr(0,3) == "gl_" ) continue;
|
---|
540 |
|
---|
541 | int uni_loc = glGetUniformLocation( p->glid, name.data() );
|
---|
542 | datatype utype = gl_enum_to_datatype( uni_type );
|
---|
543 |
|
---|
544 | // check for array
|
---|
545 | size_t arrchar = name.find( '[' );
|
---|
546 | if ( arrchar != string_view::npos )
|
---|
547 | {
|
---|
548 | name = name.substr( 0, arrchar );
|
---|
549 | }
|
---|
550 |
|
---|
551 | uniform_base* u = uniform_base::create( utype, uni_loc, size_t( uni_len ) );
|
---|
552 | NV_ASSERT( u, "Unknown uniform type!" );
|
---|
553 | (*p->m_uniform_map)[ name ] = u;
|
---|
554 | }
|
---|
555 |
|
---|
556 | for ( unsigned i = 0; i < unsigned( bparams ); ++i )
|
---|
557 | {
|
---|
558 | int uni_len;
|
---|
559 | char name_buffer[128];
|
---|
560 |
|
---|
561 | glGetActiveUniformBlockName( p->glid, i, 128, &uni_len, name_buffer );
|
---|
562 | NV_LOG_INFO( string_view( name_buffer, size_t( uni_len ) ) );
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | bool nv::gl_device::compile( uint32 sh_type, string_view shader_code, unsigned& glid )
|
---|
567 | {
|
---|
568 | glid = glCreateShader( sh_type );
|
---|
569 |
|
---|
570 | const char* pc = shader_code.data();
|
---|
571 | int l = int( shader_code.length() );
|
---|
572 |
|
---|
573 | glShaderSource( glid, 1, &pc, &l );
|
---|
574 | glCompileShader( glid );
|
---|
575 |
|
---|
576 | const uint32 buffer_size = 1024;
|
---|
577 | char buffer[ buffer_size ] = { 0 };
|
---|
578 | int length;
|
---|
579 | int compile_ok = GL_FALSE;
|
---|
580 | glGetShaderiv(glid, GL_COMPILE_STATUS, &compile_ok);
|
---|
581 | glGetShaderInfoLog( glid, buffer_size, &length, buffer );
|
---|
582 |
|
---|
583 | if ( length > 0 )
|
---|
584 | {
|
---|
585 | if ( compile_ok == 0 )
|
---|
586 | {
|
---|
587 | NV_LOG_ERROR( "Shader #", glid, " error: ", string_view( buffer, size_t( length ) ) );
|
---|
588 | }
|
---|
589 | else
|
---|
590 | {
|
---|
591 | NV_LOG_INFO( "Shader #", glid, " compiled successfully: ", string_view( buffer, size_t( length ) ) );
|
---|
592 | }
|
---|
593 | }
|
---|
594 | else
|
---|
595 | {
|
---|
596 | NV_LOG_INFO( "Shader #", glid, " compiled successfully." );
|
---|
597 | }
|
---|
598 | return compile_ok != 0;
|
---|
599 |
|
---|
600 | }
|
---|
601 |
|
---|