[395] | 1 | // Copyright (C) 2014-2015 ChaosForge Ltd
|
---|
[316] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[316] | 6 |
|
---|
| 7 | #include "nv/engine/program_manager.hh"
|
---|
[368] | 8 | #include "nv/stl/range.hh"
|
---|
[365] | 9 | #include "nv/core/logging.hh"
|
---|
[316] | 10 | #include "nv/lua/lua_nova.hh"
|
---|
| 11 |
|
---|
[365] | 12 |
|
---|
[316] | 13 | nv::program_manager::program_manager( context* a_context ) : m_context( a_context )
|
---|
| 14 | {
|
---|
| 15 | m_vertex_head = a_context->get_device()->get_shader_header();
|
---|
| 16 | m_fragment_head = a_context->get_device()->get_shader_header();
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | nv::resource_id nv::program_manager::load_resource( lua::table_guard& table )
|
---|
| 20 | {
|
---|
[365] | 21 | NV_LOG_DEBUG( table.get_string("id") );
|
---|
[316] | 22 | std::string vsource;
|
---|
| 23 | std::string fsource;
|
---|
| 24 | std::string csource;
|
---|
| 25 | if ( table.is_table("common") )
|
---|
| 26 | {
|
---|
| 27 | lua::table_guard common( table, "common" );
|
---|
| 28 | load_source( common, csource, "" );
|
---|
| 29 | }
|
---|
| 30 | {
|
---|
| 31 | lua::table_guard vtable( table, "vertex" );
|
---|
| 32 | load_source( vtable, vsource, m_vertex_head+"\n"+csource+"\n");
|
---|
| 33 | }
|
---|
| 34 | {
|
---|
| 35 | lua::table_guard ftable( table, "fragment" );
|
---|
| 36 | load_source( ftable, fsource, m_fragment_head+"\n"+csource+"\n" );
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[433] | 39 | nv::program program = m_context->get_device()->create_program( string_view( vsource.c_str(), vsource.size() ), string_view( fsource.c_str(), fsource.size() ) );
|
---|
[316] | 40 | return add( program );
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | void nv::program_manager::release( program p )
|
---|
| 44 | {
|
---|
| 45 | m_context->get_device()->release( p );
|
---|
| 46 | }
|
---|
| 47 |
|
---|
[380] | 48 | void nv::program_manager::load_source( lua::table_guard& table, std::string& out, const std::string& append )
|
---|
[316] | 49 | {
|
---|
| 50 | out = append;
|
---|
[317] | 51 | if ( table.is_string( "files" ) )
|
---|
[316] | 52 | {
|
---|
[361] | 53 | out += nv::slurp( table.get_std_string( "files" ) );
|
---|
[316] | 54 | }
|
---|
[317] | 55 | else if ( table.is_table( "files" ) )
|
---|
| 56 | {
|
---|
| 57 | lua::table_guard inctable( table, "files" );
|
---|
| 58 | uint32 count = inctable.get_size();
|
---|
| 59 | for ( uint32 i = 1; i <= count; ++i )
|
---|
| 60 | {
|
---|
[323] | 61 | std::string include( inctable.get<std::string,uint32>(i) );
|
---|
[317] | 62 | if ( i == count ) out += "#line 1\n";
|
---|
| 63 | out += nv::slurp( include );
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
[316] | 66 |
|
---|
| 67 | if ( table.is_string( "file" ) )
|
---|
| 68 | {
|
---|
[361] | 69 | out += "#line 1\n" + nv::slurp( table.get_std_string( "file" ) );
|
---|
[316] | 70 | }
|
---|
[317] | 71 |
|
---|
| 72 | if ( table.is_string( "source" ) )
|
---|
[316] | 73 | {
|
---|
[361] | 74 | out += table.get_std_string( "source" );
|
---|
[316] | 75 | }
|
---|
| 76 | }
|
---|