diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-01-16 17:13:37 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-01-16 17:13:37 +0400 |
commit | 92764260d7050800572c9d5b8a80f954572e9a0f (patch) | |
tree | fe2f6821b5dd93e0cb49cc2ae1f78c4e04235df6 /intern/cycles/util/util_cache.cpp | |
parent | 3123ad12a395031a1bf5a964582762a3831e9b1c (diff) |
Cycles: add option to cache BVH's between subsequent renders, storing the BVH on
disk to be reused by the next render.
This is useful for rendering animations where only the camera or materials change.
Note that saving the BVH to disk only to be removed for the next frame is slower
if this is not the case and the meshes do actually change.
For a render, it will save bvh files to the cache user directory, and remove all
cache files from other renders. The files are named using a MD5 hash based on the
mesh, to verify if the meshes are still the same.
Diffstat (limited to 'intern/cycles/util/util_cache.cpp')
-rw-r--r-- | intern/cycles/util/util_cache.cpp | 63 |
1 files changed, 50 insertions, 13 deletions
diff --git a/intern/cycles/util/util_cache.cpp b/intern/cycles/util/util_cache.cpp index 49a0f62cae8..44d784ba741 100644 --- a/intern/cycles/util/util_cache.cpp +++ b/intern/cycles/util/util_cache.cpp @@ -19,11 +19,18 @@ #include <stdio.h> #include "util_cache.h" +#include "util_debug.h" #include "util_foreach.h" +#include "util_map.h" #include "util_md5.h" #include "util_path.h" #include "util_types.h" +#define BOOST_FILESYSTEM_VERSION 2 + +#include <boost/filesystem.hpp> +#include <boost/algorithm/string.hpp> + CCL_NAMESPACE_BEGIN /* CacheData */ @@ -32,6 +39,7 @@ CacheData::CacheData(const string& name_) { name = name_; f = NULL; + have_filename = false; } CacheData::~CacheData() @@ -40,24 +48,35 @@ CacheData::~CacheData() fclose(f); } +const string& CacheData::get_filename() +{ + if(!have_filename) { + MD5Hash hash; + + foreach(const CacheBuffer& buffer, buffers) + if(buffer.size) + hash.append((uint8_t*)buffer.data, buffer.size); + + filename = name + "_" + hash.get_hex(); + have_filename = true; + } + + return filename; +} + /* Cache */ Cache Cache::global; -string Cache::data_filename(const CacheData& key) +string Cache::data_filename(CacheData& key) { - MD5Hash hash; - - foreach(const CacheBuffer& buffer, key.buffers) - hash.append((uint8_t*)buffer.data, buffer.size); - - string fname = key.name + "_" + hash.get_hex(); - return path_get("cache/" + fname); + return path_user_get(path_join("cache", key.get_filename())); } -void Cache::insert(const CacheData& key, const CacheData& value) +void Cache::insert(CacheData& key, CacheData& value) { string filename = data_filename(key); + path_create_directories(filename); FILE *f = fopen(filename.c_str(), "wb"); if(!f) { @@ -65,17 +84,18 @@ void Cache::insert(const CacheData& key, const CacheData& value) return; } - foreach(const CacheBuffer& buffer, value.buffers) { + foreach(CacheBuffer& buffer, value.buffers) { if(!fwrite(&buffer.size, sizeof(buffer.size), 1, f)) fprintf(stderr, "Failed to write to file %s.\n", filename.c_str()); - if(!fwrite(buffer.data, buffer.size, 1, f)) - fprintf(stderr, "Failed to write to file %s.\n", filename.c_str()); + if(buffer.size) + if(!fwrite(buffer.data, buffer.size, 1, f)) + fprintf(stderr, "Failed to write to file %s.\n", filename.c_str()); } fclose(f); } -bool Cache::lookup(const CacheData& key, CacheData& value) +bool Cache::lookup(CacheData& key, CacheData& value) { string filename = data_filename(key); FILE *f = fopen(filename.c_str(), "rb"); @@ -89,5 +109,22 @@ bool Cache::lookup(const CacheData& key, CacheData& value) return true; } +void Cache::clear_except(const string& name, const set<string>& except) +{ + string dir = path_user_get("cache"); + + if(boost::filesystem::exists(dir)) { + boost::filesystem::directory_iterator it(dir), it_end; + + for(; it != it_end; it++) { + string filename = it->path().filename(); + + if(boost::starts_with(filename, name)) + if(except.find(filename) == except.end()) + boost::filesystem::remove(it->path()); + } + } +} + CCL_NAMESPACE_END |