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