source: trunk/src/engine/program_manager.cc @ 473

Last change on this file since 473 was 438, checked in by epyon, 10 years ago
  • massive amount of std::string removal
  • removed slurp, use filesystem::slurp instead
  • lua_values const_string support
  • several bugfixes
  • program_manager and shader loading without std::string/std::stream
File size: 2.1 KB
RevLine 
[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
13nv::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
18nv::resource_id nv::program_manager::load_resource( lua::table_guard& table )
19{
[365]20        NV_LOG_DEBUG( table.get_string("id") );
[438]21        string_buffer vsource;
22        string_buffer fsource;
23        string_buffer header( m_shader_head );
[316]24        if ( table.is_table("common") )
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
[438]38        nv::program program = m_context->get_device()->create_program( vsource, fsource );
[316]39        return add( program );
40}
41
42void nv::program_manager::release( program p )
43{
44        m_context->get_device()->release( p );
45}
46
[438]47nv::string_buffer nv::program_manager::load_source( lua::table_guard& table, const string_view& append )
[316]48{
[438]49        c_file_system fs;
50        string_buffer out( append );
[317]51        if ( table.is_string( "files" ) )
[316]52        {
[438]53                out.append( fs.slurp( table.get_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                {
[438]61                        const_string include( inctable.get<const_string,uint32>(i) );
62                        if ( i == count ) out.append( "#line 1\n" );
63                        out.append( fs.slurp( include ) );
[317]64                }
65        }
[316]66
67        if ( table.is_string( "file" ) )
68        {
[438]69                const_string data = fs.slurp( table.get_string( "file" ) );
70                out.append( "#line 1\n" + data );
[316]71        }
[317]72
73        if ( table.is_string( "source" ) )
[316]74        {
[438]75                out.append( table.get_string( "source" ) );
[316]76        }
[438]77        return out;
[316]78}
Note: See TracBrowser for help on using the repository browser.