From 92764260d7050800572c9d5b8a80f954572e9a0f Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Mon, 16 Jan 2012 13:13:37 +0000 Subject: 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. --- intern/cycles/bvh/bvh.cpp | 31 +++++++++++++++++++++++++++++++ intern/cycles/bvh/bvh.h | 4 ++++ intern/cycles/bvh/bvh_params.h | 11 +++++++---- 3 files changed, 42 insertions(+), 4 deletions(-) (limited to 'intern/cycles/bvh') diff --git a/intern/cycles/bvh/bvh.cpp b/intern/cycles/bvh/bvh.cpp index cd3ad709812..c9bfa964332 100644 --- a/intern/cycles/bvh/bvh.cpp +++ b/intern/cycles/bvh/bvh.cpp @@ -75,12 +75,18 @@ bool BVH::cache_read(CacheData& key) foreach(Object *ob, objects) { key.add(ob->mesh->verts); key.add(ob->mesh->triangles); + key.add(&ob->bounds, sizeof(ob->bounds)); + key.add(&ob->visibility, sizeof(ob->visibility)); + key.add(&ob->mesh->transform_applied, sizeof(bool)); } CacheData value; if(Cache::global.lookup(key, value)) { + cache_filename = key.get_filename(); + value.read(pack.root_index); + value.read(pack.SAH); value.read(pack.nodes); value.read(pack.object_node); @@ -101,6 +107,7 @@ void BVH::cache_write(CacheData& key) CacheData value; value.add(pack.root_index); + value.add(pack.SAH); value.add(pack.nodes); value.add(pack.object_node); @@ -111,6 +118,26 @@ void BVH::cache_write(CacheData& key) value.add(pack.is_leaf); Cache::global.insert(key, value); + + cache_filename = key.get_filename(); +} + +void BVH::clear_cache_except() +{ + set except; + + if(!cache_filename.empty()) + except.insert(cache_filename); + + foreach(Object *ob, objects) { + Mesh *mesh = ob->mesh; + BVH *bvh = mesh->bvh; + + if(bvh && !bvh->cache_filename.empty()) + except.insert(bvh->cache_filename); + } + + Cache::global.clear_except("bvh", except); } /* Building */ @@ -177,6 +204,10 @@ void BVH::build(Progress& progress) if(params.use_cache) { progress.set_substatus("Writing BVH cache"); cache_write(key); + + /* clear other bvh files from cache */ + if(params.top_level) + clear_cache_except(); } } diff --git a/intern/cycles/bvh/bvh.h b/intern/cycles/bvh/bvh.h index e502af72335..30ae7dac106 100644 --- a/intern/cycles/bvh/bvh.h +++ b/intern/cycles/bvh/bvh.h @@ -20,6 +20,7 @@ #include "bvh_params.h" +#include "util_string.h" #include "util_types.h" #include "util_vector.h" @@ -83,6 +84,7 @@ public: PackedBVH pack; BVHParams params; vector objects; + string cache_filename; static BVH *create(const BVHParams& params, const vector& objects); virtual ~BVH() {} @@ -90,6 +92,8 @@ public: void build(Progress& progress); void refit(Progress& progress); + void clear_cache_except(); + protected: BVH(const BVHParams& params, const vector& objects); diff --git a/intern/cycles/bvh/bvh_params.h b/intern/cycles/bvh/bvh_params.h index b38e40cfbda..38093438500 100644 --- a/intern/cycles/bvh/bvh_params.h +++ b/intern/cycles/bvh/bvh_params.h @@ -26,7 +26,7 @@ class BVHParams { public: /* spatial split area threshold */ - bool use_spatial_split; + int use_spatial_split; float spatial_split_alpha; /* SAH costs */ @@ -38,13 +38,15 @@ public: int max_leaf_size; /* object or mesh level bvh */ - bool top_level; + int top_level; /* disk cache */ - bool use_cache; + int use_cache; /* QBVH */ - bool use_qbvh; + int use_qbvh; + + int pad; /* fixed parameters */ enum { @@ -67,6 +69,7 @@ public: top_level = false; use_cache = false; use_qbvh = false; + pad = false; } /* SAH costs */ -- cgit v1.2.3