Cycles: Experiment with making previews more interactive

There were two major problems with the interactivity of material previews:

- Beckmann tables were re-generated on every material tweak.
  This is because preview scene is not set to be persistent, so re-triggering
  the render leads to the full scene re-sync.

- Images could take rather noticeable time to load with OIIO from the disk
  on every tweak.

This patch addressed this two issues in the following way:

- Beckmann tables are now static on CPU memory.

  They're couple of hundred kilobytes only, so wouldn't expect this to be
  an issue. And they're needed for almost every render anyway.

  This actually also makes blackbody table to be static, but it's even smaller
  than beckmann table.

  Not totally happy with this approach, but others seems to complicate things
  quite a bit with all this render engine life time and so..

- For preview rendering all images are considered to be built-in. This means
  instead of OIIO which re-loads images on every re-render they're coming
  from ImBuf cache which is fully manageable from blender side and unused
  images gets freed later.

  This would make it impossible to have mipmapping with OSL for now, but we'll
  be working on that later anyway and don't think mipmaps are really so crucial
  for the material preview.

  This seems to be a better alternative to making preview scene persistent,
  because of much optimal memory control from blender side.

Reviewers: brecht, juicyfruit, campbellbarton, dingto

Subscribers: eyecandy, venomgfx

Differential Revision: https://developer.blender.org/D1132
This commit is contained in:
Sergey Sharybin 2015-02-21 21:55:24 +05:00
parent 36f352a4e4
commit b5f58c1ad9
4 changed files with 70 additions and 18 deletions

View File

@ -179,7 +179,13 @@ static bool is_output_node(BL::Node b_node)
|| b_node.is_a(&RNA_ShaderNodeOutputLamp));
}
static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree, BL::ShaderNode b_node)
static ShaderNode *add_node(Scene *scene,
BL::RenderEngine b_engine,
BL::BlendData b_data,
BL::Scene b_scene,
ShaderGraph *graph,
BL::ShaderNodeTree b_ntree,
BL::ShaderNode b_node)
{
ShaderNode *node = NULL;
@ -558,7 +564,8 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
*/
bool is_builtin = b_image.packed_file() ||
b_image.source() == BL::Image::source_GENERATED ||
b_image.source() == BL::Image::source_MOVIE;
b_image.source() == BL::Image::source_MOVIE ||
b_engine.is_preview();
if(is_builtin) {
/* for builtin images we're using image datablock name to find an image to
@ -601,7 +608,8 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
if(b_image) {
bool is_builtin = b_image.packed_file() ||
b_image.source() == BL::Image::source_GENERATED ||
b_image.source() == BL::Image::source_MOVIE;
b_image.source() == BL::Image::source_MOVIE ||
b_engine.is_preview();
if(is_builtin) {
int scene_frame = b_scene.frame_current();
@ -805,8 +813,14 @@ static ShaderOutput *node_find_output_by_name(ShaderNode *node, BL::Node b_node,
return node->output(name.c_str());
}
static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree,
const ProxyMap &proxy_input_map, const ProxyMap &proxy_output_map)
static void add_nodes(Scene *scene,
BL::RenderEngine b_engine,
BL::BlendData b_data,
BL::Scene b_scene,
ShaderGraph *graph,
BL::ShaderNodeTree b_ntree,
const ProxyMap &proxy_input_map,
const ProxyMap &proxy_output_map)
{
/* add nodes */
BL::ShaderNodeTree::nodes_iterator b_node;
@ -883,8 +897,16 @@ static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, Sha
output_map[b_output->ptr.data] = proxy->outputs[0];
}
if(b_group_ntree)
add_nodes(scene, b_data, b_scene, graph, b_group_ntree, group_proxy_input_map, group_proxy_output_map);
if (b_group_ntree) {
add_nodes(scene,
b_engine,
b_data,
b_scene,
graph,
b_group_ntree,
group_proxy_input_map,
group_proxy_output_map);
}
}
else if(b_node->is_a(&RNA_NodeGroupInput)) {
/* map each socket to a proxy node */
@ -923,7 +945,13 @@ static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, Sha
}
}
else {
node = add_node(scene, b_data, b_scene, graph, b_ntree, BL::ShaderNode(*b_node));
node = add_node(scene,
b_engine,
b_data,
b_scene,
graph,
b_ntree,
BL::ShaderNode(*b_node));
}
if(node) {
@ -979,10 +1007,22 @@ static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, Sha
}
}
static void add_nodes(Scene *scene, BL::BlendData b_data, BL::Scene b_scene, ShaderGraph *graph, BL::ShaderNodeTree b_ntree)
static void add_nodes(Scene *scene,
BL::RenderEngine b_engine,
BL::BlendData b_data,
BL::Scene b_scene,
ShaderGraph *graph,
BL::ShaderNodeTree b_ntree)
{
static const ProxyMap empty_proxy_map;
add_nodes(scene, b_data, b_scene, graph, b_ntree, empty_proxy_map, empty_proxy_map);
add_nodes(scene,
b_engine,
b_data,
b_scene,
graph,
b_ntree,
empty_proxy_map,
empty_proxy_map);
}
/* Sync Materials */
@ -1008,7 +1048,7 @@ void BlenderSync::sync_materials(bool update_all)
if(b_mat->use_nodes() && b_mat->node_tree()) {
BL::ShaderNodeTree b_ntree(b_mat->node_tree());
add_nodes(scene, b_data, b_scene, graph, b_ntree);
add_nodes(scene, b_engine, b_data, b_scene, graph, b_ntree);
}
else {
ShaderNode *closure, *out;
@ -1051,7 +1091,7 @@ void BlenderSync::sync_world(bool update_all)
if(b_world && b_world.use_nodes() && b_world.node_tree()) {
BL::ShaderNodeTree b_ntree(b_world.node_tree());
add_nodes(scene, b_data, b_scene, graph, b_ntree);
add_nodes(scene, b_engine, b_data, b_scene, graph, b_ntree);
/* volume */
PointerRNA cworld = RNA_pointer_get(&b_world.ptr, "cycles");
@ -1137,7 +1177,7 @@ void BlenderSync::sync_lamps(bool update_all)
BL::ShaderNodeTree b_ntree(b_lamp->node_tree());
add_nodes(scene, b_data, b_scene, graph, b_ntree);
add_nodes(scene, b_engine, b_data, b_scene, graph, b_ntree);
}
else {
ShaderNode *closure, *out;

View File

@ -385,7 +385,8 @@ void BlenderSync::sync_images()
*/
const bool is_builtin = b_image->packed_file() ||
b_image->source() == BL::Image::source_GENERATED ||
b_image->source() == BL::Image::source_MOVIE;
b_image->source() == BL::Image::source_MOVIE ||
b_engine.is_preview();
if(is_builtin == false) {
b_image->buffers_free();
}

View File

@ -32,6 +32,9 @@
CCL_NAMESPACE_BEGIN
vector<float> ShaderManager::blackbody_table;
vector<float> ShaderManager::beckmann_table;
/* Beckmann sampling precomputed table, see bsdf_microfacet.h */
/* 2D slope distribution (alpha = 1.0) */
@ -394,7 +397,10 @@ void ShaderManager::device_update_common(Device *device,
if(has_converter_blackbody && blackbody_table_offset == TABLE_OFFSET_INVALID) {
if(blackbody_table.size() == 0) {
blackbody_table = blackbody_table_build();
thread_scoped_lock lock(lookup_table_mutex);
if(blackbody_table.size() == 0) {
blackbody_table = blackbody_table_build();
}
}
blackbody_table_offset = scene->lookup_tables->add_table(dscene, blackbody_table);
@ -408,7 +414,10 @@ void ShaderManager::device_update_common(Device *device,
/* beckmann lookup table */
if(beckmann_table_offset == TABLE_OFFSET_INVALID) {
if(beckmann_table.size() == 0) {
beckmann_table_build(beckmann_table);
thread_scoped_lock lock(lookup_table_mutex);
if(beckmann_table.size() == 0) {
beckmann_table_build(beckmann_table);
}
}
beckmann_table_offset = scene->lookup_tables->add_table(dscene, beckmann_table);
ktables->beckmann_offset = (int)beckmann_table_offset;

View File

@ -36,6 +36,7 @@
#include "util_map.h"
#include "util_param.h"
#include "util_string.h"
#include "util_thread.h"
#include "util_types.h"
CCL_NAMESPACE_BEGIN
@ -171,8 +172,9 @@ protected:
typedef unordered_map<ustring, uint, ustringHash> AttributeIDMap;
AttributeIDMap unique_attribute_id;
vector<float> blackbody_table;
vector<float> beckmann_table;
thread_mutex lookup_table_mutex;
static vector<float> blackbody_table;
static vector<float> beckmann_table;
size_t blackbody_table_offset;
size_t beckmann_table_offset;