Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrecht Van Lommel <brechtvanlommel@pandora.be>2012-01-16 17:13:37 +0400
committerBrecht Van Lommel <brechtvanlommel@pandora.be>2012-01-16 17:13:37 +0400
commit92764260d7050800572c9d5b8a80f954572e9a0f (patch)
treefe2f6821b5dd93e0cb49cc2ae1f78c4e04235df6 /intern/cycles/util
parent3123ad12a395031a1bf5a964582762a3831e9b1c (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')
-rw-r--r--intern/cycles/util/util_cache.cpp63
-rw-r--r--intern/cycles/util/util_cache.h49
2 files changed, 88 insertions, 24 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
diff --git a/intern/cycles/util/util_cache.h b/intern/cycles/util/util_cache.h
index 6e3c7c47e39..e8f111a5397 100644
--- a/intern/cycles/util/util_cache.h
+++ b/intern/cycles/util/util_cache.h
@@ -32,6 +32,7 @@
* different scenes where it may be hard to detect duplicate work.
*/
+#include "util_set.h"
#include "util_string.h"
#include "util_vector.h"
@@ -50,25 +51,25 @@ class CacheData {
public:
vector<CacheBuffer> buffers;
string name;
+ string filename;
+ bool have_filename;
FILE *f;
CacheData(const string& name = "");
~CacheData();
+ const string& get_filename();
+
template<typename T> void add(const vector<T>& data)
{
- if(data.size()) {
- CacheBuffer buffer(&data[0], data.size()*sizeof(T));
- buffers.push_back(buffer);
- }
+ CacheBuffer buffer(data.size()? &data[0]: NULL, data.size()*sizeof(T));
+ buffers.push_back(buffer);
}
template<typename T> void add(const array<T>& data)
{
- if(data.size()) {
- CacheBuffer buffer(&data[0], data.size()*sizeof(T));
- buffers.push_back(buffer);
- }
+ CacheBuffer buffer(data.size()? &data[0]: NULL, data.size()*sizeof(T));
+ buffers.push_back(buffer);
}
void add(void *data, size_t size)
@@ -85,6 +86,12 @@ public:
buffers.push_back(buffer);
}
+ void add(float& data)
+ {
+ CacheBuffer buffer(&data, sizeof(float));
+ buffers.push_back(buffer);
+ }
+
void add(size_t& data)
{
CacheBuffer buffer(&data, sizeof(size_t));
@@ -113,12 +120,30 @@ public:
void read(int& data)
{
+ size_t size;
+
+ if(!fread(&size, sizeof(size), 1, f))
+ fprintf(stderr, "Failed to read int size from cache.\n");
if(!fread(&data, sizeof(data), 1, f))
fprintf(stderr, "Failed to read int from cache.\n");
}
+ void read(float& data)
+ {
+ size_t size;
+
+ if(!fread(&size, sizeof(size), 1, f))
+ fprintf(stderr, "Failed to read float size from cache.\n");
+ if(!fread(&data, sizeof(data), 1, f))
+ fprintf(stderr, "Failed to read float from cache.\n");
+ }
+
void read(size_t& data)
{
+ size_t size;
+
+ if(!fread(&size, sizeof(size), 1, f))
+ fprintf(stderr, "Failed to read size_t size from cache.\n");
if(!fread(&data, sizeof(data), 1, f))
fprintf(stderr, "Failed to read size_t from cache.\n");
}
@@ -128,11 +153,13 @@ class Cache {
public:
static Cache global;
- void insert(const CacheData& key, const CacheData& value);
- bool lookup(const CacheData& key, CacheData& value);
+ void insert(CacheData& key, CacheData& value);
+ bool lookup(CacheData& key, CacheData& value);
+
+ void clear_except(const string& name, const set<string>& except);
protected:
- string data_filename(const CacheData& key);
+ string data_filename(CacheData& key);
};
CCL_NAMESPACE_END