1 | // Copyright (C) 2014-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/engine/program_manager.hh"
|
---|
8 | #include "nv/stl/range.hh"
|
---|
9 | #include "nv/core/logging.hh"
|
---|
10 | #include "nv/lua/lua_nova.hh"
|
---|
11 |
|
---|
12 |
|
---|
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 | {
|
---|
21 | NV_LOG_DEBUG( table.get_string("id") );
|
---|
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 |
|
---|
39 | nv::program program = m_context->get_device()->create_program( string_view( vsource ), string_view( fsource ) );
|
---|
40 | return add( program );
|
---|
41 | }
|
---|
42 |
|
---|
43 | void nv::program_manager::release( program p )
|
---|
44 | {
|
---|
45 | m_context->get_device()->release( p );
|
---|
46 | }
|
---|
47 |
|
---|
48 | void nv::program_manager::load_source( lua::table_guard& table, std::string& out, const std::string& append )
|
---|
49 | {
|
---|
50 | out = append;
|
---|
51 | if ( table.is_string( "files" ) )
|
---|
52 | {
|
---|
53 | out += nv::slurp( table.get_std_string( "files" ) );
|
---|
54 | }
|
---|
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 | {
|
---|
61 | std::string include( inctable.get<std::string,uint32>(i) );
|
---|
62 | if ( i == count ) out += "#line 1\n";
|
---|
63 | out += nv::slurp( include );
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if ( table.is_string( "file" ) )
|
---|
68 | {
|
---|
69 | out += "#line 1\n" + nv::slurp( table.get_std_string( "file" ) );
|
---|
70 | }
|
---|
71 |
|
---|
72 | if ( table.is_string( "source" ) )
|
---|
73 | {
|
---|
74 | out += table.get_std_string( "source" );
|
---|
75 | }
|
---|
76 | }
|
---|