// Copyright (C) 2014 ChaosForge Ltd // http://chaosforge.org/ // // This file is part of NV Libraries. // For conditions of distribution and use, see copyright notice in nv.hh #include "nv/engine/program_manager.hh" #include "nv/range.hh" #include "nv/lua/lua_nova.hh" nv::program_manager::program_manager( context* a_context ) : m_context( a_context ) { m_vertex_head = a_context->get_device()->get_shader_header(); m_fragment_head = a_context->get_device()->get_shader_header(); } nv::resource_id nv::program_manager::load_resource( lua::table_guard& table ) { NV_LOG( LOG_DEBUG, table.get_string("id") ); std::string vsource; std::string fsource; std::string csource; if ( table.is_table("common") ) { lua::table_guard common( table, "common" ); load_source( common, csource, "" ); } { lua::table_guard vtable( table, "vertex" ); load_source( vtable, vsource, m_vertex_head+"\n"+csource+"\n"); } { lua::table_guard ftable( table, "fragment" ); load_source( ftable, fsource, m_fragment_head+"\n"+csource+"\n" ); } nv::program program = m_context->get_device()->create_program( vsource, fsource ); return add( program ); } void nv::program_manager::release( program p ) { m_context->get_device()->release( p ); } void nv::program_manager::load_source( lua::table_guard& table, string& out, const string& append ) { out = append; std::string include; if ( table.is_string( "include" ) ) { out += nv::slurp( table.get_string( "include" ) ); } if ( table.is_string( "file" ) ) { out += "#line 1\n"+nv::slurp( table.get_string( "file" ) ); } else if ( table.is_string( "source" ) ) { out += table.get_string( "source" ); } }