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 | #include "nv/io/c_file_system.hh"
|
---|
12 |
|
---|
13 | nv::program_manager::program_manager( context* a_context ) : m_context( a_context )
|
---|
14 | {
|
---|
15 | m_shader_head = a_context->get_device()->get_shader_header();
|
---|
16 | }
|
---|
17 |
|
---|
18 | nv::res_id nv::program_manager::load_resource( lua::table_guard& table )
|
---|
19 | {
|
---|
20 | NV_LOG_DEBUG( table.get_string("id") );
|
---|
21 | string_buffer vsource;
|
---|
22 | string_buffer fsource;
|
---|
23 | string_buffer header( m_shader_head );
|
---|
24 | if ( table.is_table("common") )
|
---|
25 | {
|
---|
26 | lua::table_guard common( table, "common" );
|
---|
27 | header.append( "\n" + load_source( common, "" ) + "\n" );
|
---|
28 | }
|
---|
29 | {
|
---|
30 | lua::table_guard vtable( table, "vertex" );
|
---|
31 | vsource = load_source( vtable, header );
|
---|
32 | }
|
---|
33 | {
|
---|
34 | lua::table_guard ftable( table, "fragment" );
|
---|
35 | fsource = load_source( ftable, header );
|
---|
36 | }
|
---|
37 |
|
---|
38 | nv::program* program = new nv::program( m_context->get_device()->create_program( vsource, fsource ) );
|
---|
39 | return add( program );
|
---|
40 | }
|
---|
41 |
|
---|
42 | void nv::program_manager::release( program* p )
|
---|
43 | {
|
---|
44 | m_context->get_device()->release( *p );
|
---|
45 | delete p;
|
---|
46 | }
|
---|
47 |
|
---|
48 | nv::string_buffer nv::program_manager::load_source( lua::table_guard& table, const string_view& append )
|
---|
49 | {
|
---|
50 | c_file_system fs;
|
---|
51 | string_buffer out( append );
|
---|
52 | if ( table.is_string( "files" ) )
|
---|
53 | {
|
---|
54 | out.append( fs.slurp( table.get_string( "files" ) ) );
|
---|
55 | }
|
---|
56 | else if ( table.is_table( "files" ) )
|
---|
57 | {
|
---|
58 | lua::table_guard inctable( table, "files" );
|
---|
59 | uint32 count = inctable.get_size();
|
---|
60 | for ( uint32 i = 1; i <= count; ++i )
|
---|
61 | {
|
---|
62 | const_string include( inctable.get<const_string,uint32>(i) );
|
---|
63 | if ( i == count ) out.append( "#line 1\n" );
|
---|
64 | out.append( fs.slurp( include ) );
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | if ( table.is_string( "file" ) )
|
---|
69 | {
|
---|
70 | const_string data = fs.slurp( table.get_string( "file" ) );
|
---|
71 | out.append( "#line 1\n" + data );
|
---|
72 | }
|
---|
73 |
|
---|
74 | if ( table.is_string( "source" ) )
|
---|
75 | {
|
---|
76 | out.append( table.get_string( "source" ) );
|
---|
77 | }
|
---|
78 | return out;
|
---|
79 | }
|
---|