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