diff options
author | Sergey Sharybin <sergey.vfx@gmail.com> | 2014-09-01 16:03:03 +0400 |
---|---|---|
committer | Sergey Sharybin <sergey.vfx@gmail.com> | 2014-09-01 16:05:10 +0400 |
commit | 6212b7302cce8f6e2b5f9a0297b9595cf2ad69a8 (patch) | |
tree | f1e8daed921fc33243e7452d5cbd8a00bc2a2d9f /intern/cycles | |
parent | e8f3fa99de8196a36736381b03015689492137bd (diff) |
Cycles: Rebuild BVH from scratch if loading cache failed
Before this Cycles used to try using the cache even so it knew for the
fact that reading it from the disk failed. This change doesn't make it
more stable if someone will try to trick Cycles and give malformed data
but it solves general cases when Blender crashed during the cache write
and will preserve rendering from crashing when trying to use that partial
cache.
Diffstat (limited to 'intern/cycles')
-rw-r--r-- | intern/cycles/bvh/bvh.cpp | 36 | ||||
-rw-r--r-- | intern/cycles/util/util_cache.h | 42 |
2 files changed, 53 insertions, 25 deletions
diff --git a/intern/cycles/bvh/bvh.cpp b/intern/cycles/bvh/bvh.cpp index 3c0c5c021c8..15bd814b8d5 100644 --- a/intern/cycles/bvh/bvh.cpp +++ b/intern/cycles/bvh/bvh.cpp @@ -103,18 +103,30 @@ bool BVH::cache_read(CacheData& key) 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); - value.read(pack.tri_woop); - value.read(pack.prim_type); - value.read(pack.prim_visibility); - value.read(pack.prim_index); - value.read(pack.prim_object); - value.read(pack.is_leaf); - + if(!(value.read(pack.root_index) && + value.read(pack.SAH) && + value.read(pack.nodes) && + value.read(pack.object_node) && + value.read(pack.tri_woop) && + value.read(pack.prim_type) && + value.read(pack.prim_visibility) && + value.read(pack.prim_index) && + value.read(pack.prim_object) && + value.read(pack.is_leaf))) + { + /* Clear the pack if load failed. */ + pack.root_index = 0; + pack.SAH = 0.0f; + pack.nodes.clear(); + pack.object_node.clear(); + pack.tri_woop.clear(); + pack.prim_type.clear(); + pack.prim_visibility.clear(); + pack.prim_index.clear(); + pack.prim_object.clear(); + pack.is_leaf.clear(); + return false; + } return true; } diff --git a/intern/cycles/util/util_cache.h b/intern/cycles/util/util_cache.h index c6752d942f1..bfb2877a22b 100644 --- a/intern/cycles/util/util_cache.h +++ b/intern/cycles/util/util_cache.h @@ -96,54 +96,70 @@ public: buffers.push_back(buffer); } - template<typename T> void read(array<T>& data) + template<typename T> bool read(array<T>& data) { size_t size; if(!fread(&size, sizeof(size), 1, f)) { fprintf(stderr, "Failed to read vector size from cache.\n"); - return; + return false; } if(!size) - return; + return false; data.resize(size/sizeof(T)); if(!fread(&data[0], size, 1, f)) { fprintf(stderr, "Failed to read vector data from cache (%lu).\n", (unsigned long)size); - return; + return false; } + return true; } - void read(int& data) + bool read(int& data) { size_t size; - if(!fread(&size, sizeof(size), 1, f)) + if(!fread(&size, sizeof(size), 1, f)) { fprintf(stderr, "Failed to read int size from cache.\n"); - if(!fread(&data, sizeof(data), 1, f)) + return false; + } + if(!fread(&data, sizeof(data), 1, f)) { fprintf(stderr, "Failed to read int from cache.\n"); + return false; + } + return true; } - void read(float& data) + bool read(float& data) { size_t size; - if(!fread(&size, sizeof(size), 1, f)) + if(!fread(&size, sizeof(size), 1, f)) { fprintf(stderr, "Failed to read float size from cache.\n"); - if(!fread(&data, sizeof(data), 1, f)) + return false; + } + if(!fread(&data, sizeof(data), 1, f)) { fprintf(stderr, "Failed to read float from cache.\n"); + return false; + } + return true; } - void read(size_t& data) + bool read(size_t& data) { size_t size; - if(!fread(&size, sizeof(size), 1, f)) + 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)) + return false; + } + if(!fread(&data, sizeof(data), 1, f)) { fprintf(stderr, "Failed to read size_t from cache.\n"); + return false; + } + return true; } }; |