Cycles material preview: fix for generated/packed/movie files
authorSergey Sharybin <sergey.vfx@gmail.com>
Wed, 30 Jan 2013 13:42:12 +0000 (13:42 +0000)
committerSergey Sharybin <sergey.vfx@gmail.com>
Wed, 30 Jan 2013 13:42:12 +0000 (13:42 +0000)
Issue was caused by cycles trying to find builtin images in a main
database and in case of preview render images are not in database,
they're just referenced by shader node tree.

Now builtin images in cycles have got void* pointer to store data
needed to load builtin images.

In case ob blender session, this pointer will store pointer from
PointerRNA for image datablock and used later to construct Image
class based on this pointer.

This also saves database lookup for final render which is nice :)

Reviewed by Brecht.

intern/cycles/blender/blender_session.cpp
intern/cycles/blender/blender_session.h
intern/cycles/blender/blender_shader.cpp
intern/cycles/render/image.cpp
intern/cycles/render/image.h
intern/cycles/render/nodes.cpp
intern/cycles/render/nodes.h

index 650d3d3..c390c13 100644 (file)
@@ -111,9 +111,9 @@ void BlenderSession::create_session()
        b_engine.use_highlight_tiles(session_params.progressive_refine == false);
 
        /* setup callbacks for builtin image support */
-       scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5);
-       scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2);
-       scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2);
+       scene->image_manager->builtin_image_info_cb = function_bind(&BlenderSession::builtin_image_info, this, _1, _2, _3, _4, _5, _6);
+       scene->image_manager->builtin_image_pixels_cb = function_bind(&BlenderSession::builtin_image_pixels, this, _1, _2, _3);
+       scene->image_manager->builtin_image_float_pixels_cb = function_bind(&BlenderSession::builtin_image_float_pixels, this, _1, _2, _3);
 }
 
 void BlenderSession::reset_session(BL::BlendData b_data_, BL::Scene b_scene_)
@@ -622,23 +622,19 @@ void BlenderSession::test_cancel()
 /* builtin image file name is actually an image datablock name with
  * absolute sequence frame number concatenated via '@' character
  *
- * this function splits image id name and frame number from a
- * builtin image name
+ * this function splits frame from builtin name
  */
-void BlenderSession::builtin_name_split(const string &builtin_name, string &name, int &frame)
+int BlenderSession::builtin_image_frame(const string &builtin_name)
 {
        int last = builtin_name.find_last_of('@');
-       name = builtin_name.substr(0, last);
-       frame = atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
+       return atoi(builtin_name.substr(last + 1, builtin_name.size() - last - 1).c_str());
 }
 
-void BlenderSession::builtin_image_info(const string &builtin_name, bool &is_float, int &width, int &height, int &channels)
+void BlenderSession::builtin_image_info(const string &builtin_name, void *builtin_data, bool &is_float, int &width, int &height, int &channels)
 {
-       string name;
-       int frame;
-       builtin_name_split(builtin_name, name, frame);
-
-       BL::Image b_image = b_data.images[name];
+       PointerRNA ptr;
+       RNA_id_pointer_create((ID*)builtin_data, &ptr);
+       BL::Image b_image(ptr);
 
        if(b_image) {
                is_float = b_image.is_float();
@@ -654,13 +650,13 @@ void BlenderSession::builtin_image_info(const string &builtin_name, bool &is_flo
        }
 }
 
-bool BlenderSession::builtin_image_pixels(const string &builtin_name, unsigned char *pixels)
+bool BlenderSession::builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels)
 {
-       string name;
-       int frame;
-       builtin_name_split(builtin_name, name, frame);
+       int frame = builtin_image_frame(builtin_name);
 
-       BL::Image b_image = b_data.images[name];
+       PointerRNA ptr;
+       RNA_id_pointer_create((ID*)builtin_data, &ptr);
+       BL::Image b_image(ptr);
 
        if(b_image) {
                int width = b_image.size()[0];
@@ -696,13 +692,13 @@ bool BlenderSession::builtin_image_pixels(const string &builtin_name, unsigned c
        return false;
 }
 
-bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, float *pixels)
+bool BlenderSession::builtin_image_float_pixels(const string &builtin_name, void *builtin_data, float *pixels)
 {
-       string name;
-       int frame;
-       builtin_name_split(builtin_name, name, frame);
+       int frame = builtin_image_frame(builtin_name);
 
-       BL::Image b_image = b_data.images[name];
+       PointerRNA ptr;
+       RNA_id_pointer_create((ID*)builtin_data, &ptr);
+       BL::Image b_image(ptr);
 
        if(b_image) {
                int width = b_image.size()[0];
index 686ff3d..0210df9 100644 (file)
@@ -94,10 +94,10 @@ protected:
        void do_write_update_render_result(BL::RenderResult b_rr, BL::RenderLayer b_rlay, RenderTile& rtile, bool do_update_only);
        void do_write_update_render_tile(RenderTile& rtile, bool do_update_only);
 
-       void builtin_name_split(const string &builtin_name, string &name, int &frame);
-       void builtin_image_info(const string &builtin_name, bool &is_float, int &width, int &height, int &channels);
-       bool builtin_image_pixels(const string &builtin_name, unsigned char *pixels);
-       bool builtin_image_float_pixels(const string &builtin_name, float *pixels);
+       int builtin_image_frame(const string &builtin_name);
+       void builtin_image_info(const string &builtin_name, void *builtin_data, bool &is_float, int &width, int &height, int &channels);
+       bool builtin_image_pixels(const string &builtin_name, void *builtin_data, unsigned char *pixels);
+       bool builtin_image_float_pixels(const string &builtin_name, void *builtin_data, float *pixels);
 };
 
 CCL_NAMESPACE_END
index b1eaedb..0f7dc15 100644 (file)
@@ -529,11 +529,11 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                                        int scene_frame = b_scene.frame_current();
                                        int image_frame = image_user_frame_number(b_image_node.image_user(), scene_frame);
                                        image->filename = b_image.name() + "@" + string_printf("%d", image_frame);
-                                       image->is_builtin = true;
+                                       image->builtin_data = b_image.ptr.data;
                                }
                                else {
                                        image->filename = image_user_file_path(b_image_node.image_user(), b_image, b_scene.frame_current());
-                                       image->is_builtin = false;
+                                       image->builtin_data = NULL;
                                }
 
                                image->animated = b_image_node.image_user().use_auto_refresh();
@@ -558,11 +558,12 @@ static ShaderNode *add_node(Scene *scene, BL::BlendData b_data, BL::Scene b_scen
                                        int scene_frame = b_scene.frame_current();
                                        int image_frame = image_user_frame_number(b_env_node.image_user(), scene_frame);
                                        env->filename = b_image.name() + "@" + string_printf("%d", image_frame);
-                                       env->is_builtin = true;
+                                       env->builtin_data = b_image.ptr.data;
                                }
                                else {
                                        env->filename = image_user_file_path(b_env_node.image_user(), b_image, b_scene.frame_current());
                                        env->animated = b_env_node.image_user().use_auto_refresh();
+                                       env->builtin_data = NULL;
                                }
                        }
                        env->color_space = EnvironmentTextureNode::color_space_enum[(int)b_env_node.color_space()];
index e6f8ab4..6bfaf48 100644 (file)
@@ -85,14 +85,14 @@ bool ImageManager::set_animation_frame_update(int frame)
        return false;
 }
 
-bool ImageManager::is_float_image(const string& filename, bool is_builtin)
+bool ImageManager::is_float_image(const string& filename, void *builtin_data)
 {
        bool is_float = false;
 
-       if(is_builtin) {
+       if(builtin_data) {
                if(builtin_image_info_cb) {
                        int width, height, channels;
-                       builtin_image_info_cb(filename, is_float, width, height, channels);
+                       builtin_image_info_cb(filename, builtin_data, is_float, width, height, channels);
                }
 
                return is_float;
@@ -123,13 +123,13 @@ bool ImageManager::is_float_image(const string& filename, bool is_builtin)
        return is_float;
 }
 
-int ImageManager::add_image(const string& filename, bool is_builtin, bool animated, bool& is_float)
+int ImageManager::add_image(const string& filename, void *builtin_data, bool animated, bool& is_float)
 {
        Image *img;
        size_t slot;
 
        /* load image info and find out if we need a float texture */
-       is_float = (pack_images)? false: is_float_image(filename, is_builtin);
+       is_float = (pack_images)? false: is_float_image(filename, builtin_data);
 
        if(is_float) {
                /* find existing image */
@@ -160,7 +160,7 @@ int ImageManager::add_image(const string& filename, bool is_builtin, bool animat
                /* add new image */
                img = new Image();
                img->filename = filename;
-               img->is_builtin = is_builtin;
+               img->builtin_data = builtin_data;
                img->need_load = true;
                img->animated = animated;
                img->users = 1;
@@ -195,7 +195,7 @@ int ImageManager::add_image(const string& filename, bool is_builtin, bool animat
                /* add new image */
                img = new Image();
                img->filename = filename;
-               img->is_builtin = is_builtin;
+               img->builtin_data = builtin_data;
                img->need_load = true;
                img->animated = animated;
                img->users = 1;
@@ -209,12 +209,12 @@ int ImageManager::add_image(const string& filename, bool is_builtin, bool animat
        return slot;
 }
 
-void ImageManager::remove_image(const string& filename, bool is_builtin)
+void ImageManager::remove_image(const string& filename, void *builtin_data)
 {
        size_t slot;
 
        for(slot = 0; slot < images.size(); slot++) {
-               if(images[slot] && images[slot]->filename == filename && images[slot]->is_builtin == is_builtin) {
+               if(images[slot] && images[slot]->filename == filename && images[slot]->builtin_data == builtin_data) {
                        /* decrement user count */
                        images[slot]->users--;
                        assert(images[slot]->users >= 0);
@@ -232,7 +232,7 @@ void ImageManager::remove_image(const string& filename, bool is_builtin)
        if(slot == images.size()) {
                /* see if it's in a float texture slot */
                for(slot = 0; slot < float_images.size(); slot++) {
-                       if(float_images[slot] && float_images[slot]->filename == filename && float_images[slot]->is_builtin == is_builtin) {
+                       if(float_images[slot] && float_images[slot]->filename == filename && float_images[slot]->builtin_data == builtin_data) {
                                /* decrement user count */
                                float_images[slot]->users--;
                                assert(float_images[slot]->users >= 0);
@@ -257,7 +257,7 @@ bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
        ImageInput *in = NULL;
        int width, height, components;
 
-       if(!img->is_builtin) {
+       if(!img->builtin_data) {
                /* load image from file through OIIO */
                in = ImageInput::create(img->filename);
 
@@ -281,7 +281,7 @@ bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
                        return false;
 
                bool is_float;
-               builtin_image_info_cb(img->filename, is_float, width, height, components);
+               builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, components);
        }
 
        /* we only handle certain number of components */
@@ -309,7 +309,7 @@ bool ImageManager::file_load_image(Image *img, device_vector<uchar4>& tex_img)
                delete in;
        }
        else {
-               builtin_image_pixels_cb(img->filename, pixels);
+               builtin_image_pixels_cb(img->filename, img->builtin_data, pixels);
        }
 
        if(components == 3) {
@@ -340,7 +340,7 @@ bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_
        ImageInput *in = NULL;
        int width, height, components;
 
-       if(!img->is_builtin) {
+       if(!img->builtin_data) {
                /* load image from file through OIIO */
                in = ImageInput::create(img->filename);
 
@@ -365,7 +365,7 @@ bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_
                        return false;
 
                bool is_float;
-               builtin_image_info_cb(img->filename, is_float, width, height, components);
+               builtin_image_info_cb(img->filename, img->builtin_data, is_float, width, height, components);
        }
 
        if(!(components == 1 || components == 3 || components == 4)) {
@@ -391,7 +391,7 @@ bool ImageManager::file_load_float_image(Image *img, device_vector<float4>& tex_
                delete in;
        }
        else {
-               builtin_image_float_pixels_cb(img->filename, pixels);
+               builtin_image_float_pixels_cb(img->filename, img->builtin_data, pixels);
        }
 
        if(components == 3) {
index e39ac14..464b87f 100644 (file)
@@ -51,9 +51,9 @@ public:
        ImageManager();
        ~ImageManager();
 
-       int add_image(const string& filename, bool is_builtin, bool animated, bool& is_float);
-       void remove_image(const string& filename, bool is_builtin);
-       bool is_float_image(const string& filename, bool is_builtin);
+       int add_image(const string& filename, void *builtin_data, bool animated, bool& is_float);
+       void remove_image(const string& filename, void *builtin_data);
+       bool is_float_image(const string& filename, void *builtin_data);
 
        void device_update(Device *device, DeviceScene *dscene, Progress& progress);
        void device_free(Device *device, DeviceScene *dscene);
@@ -65,9 +65,9 @@ public:
 
        bool need_update;
 
-       boost::function<void(const string &filename, bool &is_float, int &width, int &height, int &channels)> builtin_image_info_cb;
-       boost::function<bool(const string &filename, unsigned char *pixels)> builtin_image_pixels_cb;
-       boost::function<bool(const string &filename, float *pixels)> builtin_image_float_pixels_cb;
+       boost::function<void(const string &filename, void *data, bool &is_float, int &width, int &height, int &channels)> builtin_image_info_cb;
+       boost::function<bool(const string &filename, void *data, unsigned char *pixels)> builtin_image_pixels_cb;
+       boost::function<bool(const string &filename, void *data, float *pixels)> builtin_image_float_pixels_cb;
 private:
        int tex_num_images;
        int tex_num_float_images;
@@ -77,7 +77,7 @@ private:
 
        struct Image {
                string filename;
-               bool is_builtin;
+               void *builtin_data;
 
                bool need_load;
                bool animated;
index 5444299..a0a933e 100644 (file)
@@ -142,7 +142,7 @@ ImageTextureNode::ImageTextureNode()
        slot = -1;
        is_float = -1;
        filename = "";
-       is_builtin = false;
+       builtin_data = NULL;
        color_space = ustring("Color");
        projection = ustring("Flat");
        projection_blend = 0.0f;
@@ -156,7 +156,7 @@ ImageTextureNode::ImageTextureNode()
 ImageTextureNode::~ImageTextureNode()
 {
        if(image_manager)
-               image_manager->remove_image(filename, is_builtin);
+               image_manager->remove_image(filename, builtin_data);
 }
 
 ShaderNode *ImageTextureNode::clone() const
@@ -177,7 +177,7 @@ void ImageTextureNode::compile(SVMCompiler& compiler)
        image_manager = compiler.image_manager;
        if(is_float == -1) {
                bool is_float_bool;
-               slot = image_manager->add_image(filename, is_builtin, animated, is_float_bool);
+               slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool);
                is_float = (int)is_float_bool;
        }
 
@@ -238,7 +238,7 @@ void ImageTextureNode::compile(OSLCompiler& compiler)
        tex_mapping.compile(compiler);
 
        if(is_float == -1)
-               is_float = (int)image_manager->is_float_image(filename, false);
+               is_float = (int)image_manager->is_float_image(filename, NULL);
 
        compiler.parameter("filename", filename.c_str());
        if(is_float || color_space != "Color")
@@ -272,7 +272,7 @@ EnvironmentTextureNode::EnvironmentTextureNode()
        slot = -1;
        is_float = -1;
        filename = "";
-       is_builtin = false;
+       builtin_data = NULL;
        color_space = ustring("Color");
        projection = ustring("Equirectangular");
        animated = false;
@@ -285,7 +285,7 @@ EnvironmentTextureNode::EnvironmentTextureNode()
 EnvironmentTextureNode::~EnvironmentTextureNode()
 {
        if(image_manager)
-               image_manager->remove_image(filename, is_builtin);
+               image_manager->remove_image(filename, builtin_data);
 }
 
 ShaderNode *EnvironmentTextureNode::clone() const
@@ -306,7 +306,7 @@ void EnvironmentTextureNode::compile(SVMCompiler& compiler)
        image_manager = compiler.image_manager;
        if(slot == -1) {
                bool is_float_bool;
-               slot = image_manager->add_image(filename, is_builtin, animated, is_float_bool);
+               slot = image_manager->add_image(filename, builtin_data, animated, is_float_bool);
                is_float = (int)is_float_bool;
        }
 
@@ -356,7 +356,7 @@ void EnvironmentTextureNode::compile(OSLCompiler& compiler)
        tex_mapping.compile(compiler);
 
        if(is_float == -1)
-               is_float = (int)image_manager->is_float_image(filename, false);
+               is_float = (int)image_manager->is_float_image(filename, NULL);
 
        compiler.parameter("filename", filename.c_str());
        compiler.parameter("projection", projection);
index 8b2d6a0..66be919 100644 (file)
@@ -70,7 +70,7 @@ public:
        int slot;
        int is_float;
        string filename;
-       bool is_builtin;
+       void *builtin_data;
        ustring color_space;
        ustring projection;
        float projection_blend;
@@ -90,7 +90,7 @@ public:
        int slot;
        int is_float;
        string filename;
-       bool is_builtin;
+       void *builtin_data;
        ustring color_space;
        ustring projection;
        bool animated;