[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"
|
---|
[438] | 11 | #include "nv/io/c_file_system.hh"
|
---|
[316] | 12 |
|
---|
| 13 | nv::program_manager::program_manager( context* a_context ) : m_context( a_context )
|
---|
| 14 | {
|
---|
[438] | 15 | m_shader_head = a_context->get_device()->get_shader_header();
|
---|
[316] | 16 | }
|
---|
| 17 |
|
---|
[477] | 18 | bool nv::program_manager::load_resource( lua::table_guard& table, shash64 id )
|
---|
[316] | 19 | {
|
---|
[539] | 20 | NV_LOG_DEBUG( table["id"].get_string() );
|
---|
[438] | 21 | string_buffer vsource;
|
---|
| 22 | string_buffer fsource;
|
---|
| 23 | string_buffer header( m_shader_head );
|
---|
[539] | 24 | if ( table["common"].is_table() )
|
---|
[316] | 25 | {
|
---|
| 26 | lua::table_guard common( table, "common" );
|
---|
[438] | 27 | header.append( "\n" + load_source( common, "" ) + "\n" );
|
---|
[316] | 28 | }
|
---|
| 29 | {
|
---|
| 30 | lua::table_guard vtable( table, "vertex" );
|
---|
[438] | 31 | vsource = load_source( vtable, header );
|
---|
[316] | 32 | }
|
---|
| 33 | {
|
---|
| 34 | lua::table_guard ftable( table, "fragment" );
|
---|
[438] | 35 | fsource = load_source( ftable, header );
|
---|
[316] | 36 | }
|
---|
| 37 |
|
---|
[501] | 38 | add( id, m_context->create_program( vsource, fsource ) );
|
---|
[477] | 39 | return true;
|
---|
[316] | 40 | }
|
---|
| 41 |
|
---|
[477] | 42 | void nv::program_manager::release( program p )
|
---|
[316] | 43 | {
|
---|
[501] | 44 | m_context->release( p );
|
---|
[316] | 45 | }
|
---|
| 46 |
|
---|
[505] | 47 | nv::const_string nv::program_manager::file_to_string( const string_view& path )
|
---|
| 48 | {
|
---|
| 49 | c_file_system fs;
|
---|
| 50 | stream* fstream = open_stream( fs, path );
|
---|
| 51 | if ( !fstream ) return const_string();
|
---|
[534] | 52 | uint32 size = static_cast< uint32 >( fstream->size() );
|
---|
[505] | 53 | const_string result( nullptr, size );
|
---|
| 54 | fstream->read( const_cast<char*>( result.data() ), size, 1 );
|
---|
| 55 | delete fstream;
|
---|
| 56 | return result;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[438] | 59 | nv::string_buffer nv::program_manager::load_source( lua::table_guard& table, const string_view& append )
|
---|
[316] | 60 | {
|
---|
[438] | 61 | c_file_system fs;
|
---|
| 62 | string_buffer out( append );
|
---|
[539] | 63 | if ( table["files"].is_string() )
|
---|
[316] | 64 | {
|
---|
[539] | 65 | out.append( file_to_string( table["files"].get_string() ) );
|
---|
[316] | 66 | }
|
---|
[539] | 67 | else if ( table["files"].is_table() )
|
---|
[317] | 68 | {
|
---|
| 69 | lua::table_guard inctable( table, "files" );
|
---|
[540] | 70 | uint32 count = inctable.size();
|
---|
[317] | 71 | for ( uint32 i = 1; i <= count; ++i )
|
---|
| 72 | {
|
---|
[438] | 73 | const_string include( inctable.get<const_string,uint32>(i) );
|
---|
| 74 | if ( i == count ) out.append( "#line 1\n" );
|
---|
[505] | 75 | out.append( file_to_string( include ) );
|
---|
[317] | 76 | }
|
---|
| 77 | }
|
---|
[316] | 78 |
|
---|
[539] | 79 | if ( table["file"].is_string() )
|
---|
[316] | 80 | {
|
---|
[539] | 81 | const_string data = file_to_string( table["file"].get_string() );
|
---|
[438] | 82 | out.append( "#line 1\n" + data );
|
---|
[316] | 83 | }
|
---|
[317] | 84 |
|
---|
[539] | 85 | if ( table["source"].is_string() )
|
---|
[316] | 86 | {
|
---|
[539] | 87 | out.append( table["source"].get_string() );
|
---|
[316] | 88 | }
|
---|
[438] | 89 | return out;
|
---|
[316] | 90 | }
|
---|