1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "application.h"
#include "ioptions.h"
#include "result.h"
#include "shaders.h"
#include "string_modifiers.h"
#include "system_functions.h"
#include <boost/filesystem/convenience.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/static_assert.hpp>
#include <iostream>
namespace k3d
{
/////////////////////////////////////////////////////////////////////////////
// compile_shader
bool compile_shader(const boost::filesystem::path& SourceFile, const std::string RenderEngineType, const std::string RenderEngine)
{
// Sanity checks ...
return_val_if_fail(boost::filesystem::exists(SourceFile), false);
return_val_if_fail(RenderEngineType.size(), false);
return_val_if_fail(RenderEngine.size(), false);
// Get the set of available render engines
const k3d::ioptions::render_engines_t engines = k3d::application().options().render_engines();
// For each engine ...
for(k3d::ioptions::render_engines_t::const_iterator engine = engines.begin(); engine != engines.end(); ++engine)
{
// If it's the wrong type, skip it ...
if(engine->type != RenderEngineType)
continue;
// If it's the wrong engine, skip it ...
if(engine->engine != RenderEngine)
continue;
// OK, first let's make sure the shader binary cache directory exists ...
boost::filesystem::create_directories(k3d::application().shader_cache_path());
return_val_if_fail(boost::filesystem::exists(k3d::application().shader_cache_path()), false);
// Figure out what the compiled shader filename will be ...
std::string shader_binary_file(engine->shader_binary);
k3d::formatted_replace(shader_binary_file, '%', "p", SourceFile.leaf().substr(0, SourceFile.leaf().rfind(".")));
// Figure out the full path to the source and compiled shaders ...
const boost::filesystem::path shader_source_path(SourceFile);
const boost::filesystem::path shader_binary_path(k3d::application().shader_cache_path() / boost::filesystem::path(shader_binary_file, boost::filesystem::native));
// Get the last modification time of the shader source ...
time_t source_modified = 0;
return_val_if_fail(k3d::system::file_modification_time(shader_source_path, source_modified), false);
// Get the last modification time of the shader binary (if it exists) ...
time_t binary_modified = 0;
k3d::system::file_modification_time(shader_binary_path, binary_modified);
// If the binary is up-to-date, we're done ...
if(source_modified <= binary_modified)
return true;
std::cerr << info << "Updating [" << shader_source_path.native_file_string() << "] to [" << shader_binary_path.native_file_string() << "]" << std::endl;
// Create a command line that can compile the shader ...
std::string commandline = engine->compile_shader;
k3d::formatted_replace(commandline, '%', "s", shader_source_path.native_file_string()); // %s = shader source file
k3d::formatted_replace(commandline, '%', "b", shader_binary_path.native_file_string()); // %b = shader binary file
k3d::formatted_replace(commandline, '%', "c", k3d::application().shader_cache_path().native_file_string()); // %c = shader cache directory
k3d::formatted_replace(commandline, '%', "d", shader_source_path.branch_path().native_file_string()); // %d = shader source directory
std::cerr << info << commandline << std::endl;
// Make it happen ...
std::stringstream buffer;
return_val_if_fail(k3d::system::run_process(commandline, buffer), false);
return true;
}
return false;
}
/////////////////////////////////////////////////////////////////////////////
// lookup_shader
std::auto_ptr<sdpsl::shader> lookup_shader(const std::string ShaderName)
{
const sdpsl::shaders_t shaders(k3d::application().shaders());
for(sdpsl::shaders_t::const_iterator shader = shaders.begin(); shader != shaders.end(); ++shader)
{
if(shader->name == ShaderName)
return std::auto_ptr<sdpsl::shader>(new sdpsl::shader(*shader));
}
return std::auto_ptr<sdpsl::shader>(0);
}
} // namespace k3d
|