Cycles: Fix denoising passes being written when they're not actually generated
[blender-staging.git] / intern / cycles / blender / blender_session.cpp
index dfd6dc8db348ac0e9e975e2a5176643f096bee9a..2b5dd5eadea636141a6276bd9ba3082fabf2257f 100644 (file)
@@ -299,12 +299,13 @@ static BL::RenderResult begin_render_result(BL::RenderEngine& b_engine,
 static void end_render_result(BL::RenderEngine& b_engine,
                               BL::RenderResult& b_rr,
                               bool cancel,
+                              bool highlight,
                               bool do_merge_results)
 {
-       b_engine.end_result(b_rr, (int)cancel, (int)do_merge_results);
+       b_engine.end_result(b_rr, (int)cancel, (int) highlight, (int)do_merge_results);
 }
 
-void BlenderSession::do_write_update_render_tile(RenderTile& rtile, bool do_update_only)
+void BlenderSession::do_write_update_render_tile(RenderTile& rtile, bool do_update_only, bool highlight)
 {
        BufferParams& params = rtile.buffers->params;
        int x = params.full_x - session->tile_manager.params.full_x;
@@ -340,37 +341,37 @@ void BlenderSession::do_write_update_render_tile(RenderTile& rtile, bool do_upda
                        update_render_result(b_rr, b_rlay, rtile);
                }
 
-               end_render_result(b_engine, b_rr, true, true);
+               end_render_result(b_engine, b_rr, true, highlight, true);
        }
        else {
                /* write result */
                write_render_result(b_rr, b_rlay, rtile);
-               end_render_result(b_engine, b_rr, false, true);
+               end_render_result(b_engine, b_rr, false, false, true);
        }
 }
 
 void BlenderSession::write_render_tile(RenderTile& rtile)
 {
-       do_write_update_render_tile(rtile, false);
+       do_write_update_render_tile(rtile, false, false);
 }
 
-void BlenderSession::update_render_tile(RenderTile& rtile)
+void BlenderSession::update_render_tile(RenderTile& rtile, bool highlight)
 {
        /* use final write for preview renders, otherwise render result wouldn't be
         * be updated in blender side
         * would need to be investigated a bit further, but for now shall be fine
         */
        if(!b_engine.is_preview())
-               do_write_update_render_tile(rtile, true);
+               do_write_update_render_tile(rtile, true, highlight);
        else
-               do_write_update_render_tile(rtile, false);
+               do_write_update_render_tile(rtile, false, false);
 }
 
 void BlenderSession::render()
 {
        /* set callback to write out render results */
        session->write_render_tile_cb = function_bind(&BlenderSession::write_render_tile, this, _1);
-       session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1);
+       session->update_render_tile_cb = function_bind(&BlenderSession::update_render_tile, this, _1, _2);
 
        /* get buffer parameters */
        SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
@@ -391,22 +392,38 @@ void BlenderSession::render()
 
                /* layer will be missing if it was disabled in the UI */
                if(b_single_rlay == b_rr.layers.end()) {
-                       end_render_result(b_engine, b_rr, true, false);
+                       end_render_result(b_engine, b_rr, true, true, false);
                        continue;
                }
 
                BL::RenderLayer b_rlay = *b_single_rlay;
 
                /* add passes */
-               array<Pass> passes;
-               if(session_params.device.advanced_shading) {
-                       passes = sync->sync_render_passes(b_rlay, *b_layer_iter);
-               }
-               else {
-                       Pass::add(PASS_COMBINED, passes);
-               }
-
+               array<Pass> passes = sync->sync_render_passes(b_rlay, *b_layer_iter, session_params);
                buffer_params.passes = passes;
+
+               PointerRNA crl = RNA_pointer_get(&b_layer_iter->ptr, "cycles");
+               bool use_denoising = !session_params.progressive_refine && get_boolean(crl, "use_denoising");
+               buffer_params.denoising_data_pass = use_denoising;
+               session->tile_manager.schedule_denoising = use_denoising;
+               session->params.use_denoising = use_denoising;
+               scene->film->denoising_data_pass = buffer_params.denoising_data_pass;
+               scene->film->denoising_flags = 0;
+               if(!get_boolean(crl, "denoising_diffuse_direct"))        scene->film->denoising_flags |= DENOISING_CLEAN_DIFFUSE_DIR;
+               if(!get_boolean(crl, "denoising_diffuse_indirect"))      scene->film->denoising_flags |= DENOISING_CLEAN_DIFFUSE_IND;
+               if(!get_boolean(crl, "denoising_glossy_direct"))         scene->film->denoising_flags |= DENOISING_CLEAN_GLOSSY_DIR;
+               if(!get_boolean(crl, "denoising_glossy_indirect"))       scene->film->denoising_flags |= DENOISING_CLEAN_GLOSSY_IND;
+               if(!get_boolean(crl, "denoising_transmission_direct"))   scene->film->denoising_flags |= DENOISING_CLEAN_TRANSMISSION_DIR;
+               if(!get_boolean(crl, "denoising_transmission_indirect")) scene->film->denoising_flags |= DENOISING_CLEAN_TRANSMISSION_IND;
+               if(!get_boolean(crl, "denoising_subsurface_direct"))     scene->film->denoising_flags |= DENOISING_CLEAN_SUBSURFACE_DIR;
+               if(!get_boolean(crl, "denoising_subsurface_indirect"))   scene->film->denoising_flags |= DENOISING_CLEAN_SUBSURFACE_IND;
+               scene->film->denoising_clean_pass = (scene->film->denoising_flags & DENOISING_CLEAN_ALL_PASSES);
+               buffer_params.denoising_clean_pass = scene->film->denoising_clean_pass;
+               session->params.denoising_radius = get_int(crl, "denoising_radius");
+               session->params.denoising_strength = get_float(crl, "denoising_strength");
+               session->params.denoising_feature_strength = get_float(crl, "denoising_feature_strength");
+               session->params.denoising_relative_pca = get_boolean(crl, "denoising_relative_pca");
+
                scene->film->pass_alpha_threshold = b_layer_iter->pass_alpha_threshold();
                scene->film->tag_passes_update(scene, passes);
                scene->film->tag_update(scene);
@@ -460,7 +477,7 @@ void BlenderSession::render()
                }
 
                /* free result without merging */
-               end_render_result(b_engine, b_rr, true, false);
+               end_render_result(b_engine, b_rr, true, true, false);
 
                if(session->progress.get_cancel())
                        break;
@@ -541,8 +558,6 @@ void BlenderSession::bake(BL::Object& b_object,
                           float result[])
 {
        ShaderEvalType shader_type = get_shader_type(pass_type);
-       size_t object_index = OBJECT_NONE;
-       int tri_offset = 0;
 
        /* Set baking flag in advance, so kernel loading can check if we need
         * any baking capabilities.
@@ -552,9 +567,6 @@ void BlenderSession::bake(BL::Object& b_object,
        /* ensure kernels are loaded before we do any scene updates */
        session->load_kernels();
 
-       if(session->progress.get_cancel())
-               return;
-
        if(shader_type == SHADER_EVAL_UV) {
                /* force UV to be available */
                Pass::add(PASS_UV, scene->film->passes);
@@ -572,50 +584,61 @@ void BlenderSession::bake(BL::Object& b_object,
        scene->film->tag_update(scene);
        scene->integrator->tag_update(scene);
 
-       /* update scene */
-       BL::Object b_camera_override(b_engine.camera_override());
-       sync->sync_camera(b_render, b_camera_override, width, height, "");
-       sync->sync_data(b_render,
-                       b_v3d,
-                       b_camera_override,
-                       width, height,
-                       &python_thread_state,
-                       b_rlay_name.c_str());
+       if(!session->progress.get_cancel()) {
+               /* update scene */
+               BL::Object b_camera_override(b_engine.camera_override());
+               sync->sync_camera(b_render, b_camera_override, width, height, "");
+               sync->sync_data(b_render,
+                                               b_v3d,
+                                               b_camera_override,
+                                               width, height,
+                                               &python_thread_state,
+                                               b_rlay_name.c_str());
+       }
 
-       /* get buffer parameters */
-       SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
-       BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
+       BakeData *bake_data = NULL;
 
-       scene->bake_manager->set_shader_limit((size_t)b_engine.tile_x(), (size_t)b_engine.tile_y());
+       if(!session->progress.get_cancel()) {
+               /* get buffer parameters */
+               SessionParams session_params = BlenderSync::get_session_params(b_engine, b_userpref, b_scene, background);
+               BufferParams buffer_params = BlenderSync::get_buffer_params(b_render, b_v3d, b_rv3d, scene->camera, width, height);
 
-       /* set number of samples */
-       session->tile_manager.set_samples(session_params.samples);
-       session->reset(buffer_params, session_params.samples);
-       session->update_scene();
+               scene->bake_manager->set_shader_limit((size_t)b_engine.tile_x(), (size_t)b_engine.tile_y());
 
-       /* find object index. todo: is arbitrary - copied from mesh_displace.cpp */
-       for(size_t i = 0; i < scene->objects.size(); i++) {
-               if(strcmp(scene->objects[i]->name.c_str(), b_object.name().c_str()) == 0) {
-                       object_index = i;
-                       tri_offset = scene->objects[i]->mesh->tri_offset;
-                       break;
-               }
-       }
+               /* set number of samples */
+               session->tile_manager.set_samples(session_params.samples);
+               session->reset(buffer_params, session_params.samples);
+               session->update_scene();
 
-       int object = object_index;
+               /* find object index. todo: is arbitrary - copied from mesh_displace.cpp */
+               size_t object_index = OBJECT_NONE;
+               int tri_offset = 0;
 
-       BakeData *bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
+               for(size_t i = 0; i < scene->objects.size(); i++) {
+                       if(strcmp(scene->objects[i]->name.c_str(), b_object.name().c_str()) == 0) {
+                               object_index = i;
+                               tri_offset = scene->objects[i]->mesh->tri_offset;
+                               break;
+                       }
+               }
 
-       populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
+               int object = object_index;
 
-       /* set number of samples */
-       session->tile_manager.set_samples(session_params.samples);
-       session->reset(buffer_params, session_params.samples);
-       session->update_scene();
+               bake_data = scene->bake_manager->init(object, tri_offset, num_pixels);
+               populate_bake_data(bake_data, object_id, pixel_array, num_pixels);
 
-       session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
+               /* set number of samples */
+               session->tile_manager.set_samples(session_params.samples);
+               session->reset(buffer_params, session_params.samples);
+               session->update_scene();
 
-       scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
+               session->progress.set_update_callback(function_bind(&BlenderSession::update_bake_progress, this));
+       }
+
+       /* Perform bake. Check cancel to avoid crash with incomplete scene data. */
+       if(!session->progress.get_cancel()) {
+               scene->bake_manager->bake(scene->device, &scene->dscene, scene, session->progress, shader_type, bake_pass_filter, bake_data, result);
+       }
 
        /* free all memory used (host and device), so we wouldn't leave render
         * engine with extra memory allocated
@@ -666,6 +689,12 @@ void BlenderSession::do_write_update_render_result(BL::RenderResult& b_rr,
                                /* copy pixels */
                                read = buffers->get_pass_rect(pass_type, exposure, sample, components, &pixels[0]);
                        }
+                       else {
+                               int denoising_offset = BlenderSync::get_denoising_pass(b_pass);
+                               if(denoising_offset >= 0) {
+                                       read = buffers->get_denoising_pass_rect(denoising_offset, exposure, sample, components, &pixels[0]);
+                               }
+                       }
 
                        if(!read) {
                                memset(&pixels[0], 0, pixels.size()*sizeof(float));