[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [50528] trunk/blender/intern/cycles: Performance fix for Cycles: Don' t wait in the main UI thread when resetting devices.

Lukas Toenne lukas.toenne at googlemail.com
Tue Sep 11 13:41:52 CEST 2012


Revision: 50528
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=50528
Author:   lukastoenne
Date:     2012-09-11 11:41:51 +0000 (Tue, 11 Sep 2012)
Log Message:
-----------
Performance fix for Cycles: Don't wait in the main UI thread when resetting devices.

When the scene is updated Cycles resets the renderer device, cancelling
all existing tasks. The main thread would wait for all running tasks to
finish before continuing. This is ok when tasks can actually cancel in a
timely fashion. For OSL however, this does not work, since the OSL
shader group optimization takes quite a bit of time and can not be
easily be cancelled once running (on my crappy machine in full debug
mode: ~0.12 seconds for simple node trees). This would lead to very
laggy UI behavior and make it difficult to accurately control elements
such as sliders.

This patch removes the wait condition from the device->task_cancel
method. Instead it just sets the do_cancel flag and returns. To avoid
backlog in the task pool of the device it will return early from the
BlenderSession::sync function while the reset is going on (tested in
Session::resetting). Once all existing tasks have finished the do_cancel
flag is finally cleared again (checked in TaskPool::num_decrease).

Care has to be taken to avoid race conditions on the do_cancel flag,
since it can now be modified outside the TaskPool::cancel function
itself. For this purpose the scope of the TaskPool::num_mutex locks has
been extended, in most cases the mutex is now locked by the TaskPool
itself before calling TaskScheduler methods, instead of only locking
inside the num_increase/num_decrease functions themselves. The only
occurrence of a lock outside of the TaskPool methods is in
TaskScheduler::thread_run.

This patch is most useful in combination with the OSL renderer mode, so
it can probably wait until after the 2.64 release. SVM tasks tend to be
cancelled quickly, so the effect is less noticeable.

Modified Paths:
--------------
    trunk/blender/intern/cycles/blender/blender_session.cpp
    trunk/blender/intern/cycles/device/device.h
    trunk/blender/intern/cycles/device/device_cpu.cpp
    trunk/blender/intern/cycles/device/device_cuda.cpp
    trunk/blender/intern/cycles/device/device_multi.cpp
    trunk/blender/intern/cycles/device/device_opencl.cpp
    trunk/blender/intern/cycles/render/session.cpp
    trunk/blender/intern/cycles/render/session.h
    trunk/blender/intern/cycles/util/util_task.cpp

Modified: trunk/blender/intern/cycles/blender/blender_session.cpp
===================================================================
--- trunk/blender/intern/cycles/blender/blender_session.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/blender/blender_session.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -372,6 +372,12 @@
 		return;
 	}
 
+	/* if the session is still resetting the device come back later */
+	if(session->resetting()) {
+		tag_update();
+		return;
+	}
+
 	/* increase samples, but never decrease */
 	session->set_samples(session_params.samples);
 	session->set_pause(BlenderSync::get_session_pause(b_scene, background));

Modified: trunk/blender/intern/cycles/device/device.h
===================================================================
--- trunk/blender/intern/cycles/device/device.h	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/device/device.h	2012-09-11 11:41:51 UTC (rev 50528)
@@ -115,6 +115,7 @@
 	virtual void task_add(DeviceTask& task) = 0;
 	virtual void task_wait() = 0;
 	virtual void task_cancel() = 0;
+	virtual bool task_cancelled() = 0;
 	
 	/* opengl drawing */
 	virtual void draw_pixels(device_memory& mem, int y, int w, int h,

Modified: trunk/blender/intern/cycles/device/device_cpu.cpp
===================================================================
--- trunk/blender/intern/cycles/device/device_cpu.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/device/device_cpu.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -273,6 +273,11 @@
 	{
 		task_pool.cancel();
 	}
+
+	bool task_cancelled()
+	{
+		return task_pool.cancelled();
+	}
 };
 
 Device *device_cpu_create(DeviceInfo& info, int threads)

Modified: trunk/blender/intern/cycles/device/device_cuda.cpp
===================================================================
--- trunk/blender/intern/cycles/device/device_cuda.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/device/device_cuda.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -892,6 +892,11 @@
 	{
 		task_pool.cancel();
 	}
+
+	bool task_cancelled()
+	{
+		return task_pool.cancelled();
+	}
 };
 
 Device *device_cuda_create(DeviceInfo& info, bool background)

Modified: trunk/blender/intern/cycles/device/device_multi.cpp
===================================================================
--- trunk/blender/intern/cycles/device/device_multi.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/device/device_multi.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -312,6 +312,14 @@
 		foreach(SubDevice& sub, devices)
 			sub.device->task_cancel();
 	}
+
+	bool task_cancelled()
+	{
+		foreach(SubDevice& sub, devices)
+			if (sub.device->task_cancelled())
+				return true;
+		return false;
+	}
 };
 
 Device *device_multi_create(DeviceInfo& info, bool background)

Modified: trunk/blender/intern/cycles/device/device_opencl.cpp
===================================================================
--- trunk/blender/intern/cycles/device/device_opencl.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/device/device_opencl.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -724,6 +724,11 @@
 	{
 		task_pool.cancel();
 	}
+
+	bool task_cancelled()
+	{
+		return task_pool.cancelled();
+	}
 };
 
 Device *device_opencl_create(DeviceInfo& info, bool background)

Modified: trunk/blender/intern/cycles/render/session.cpp
===================================================================
--- trunk/blender/intern/cycles/render/session.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/render/session.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -140,6 +140,12 @@
 	pause_cond.notify_all();
 }
 
+bool Session::resetting_gpu() const
+{
+	/* no need to wait for gpu device */
+	return false;
+}
+
 bool Session::draw_gpu(BufferParams& buffer_params)
 {
 	/* block for buffer access */
@@ -290,6 +296,11 @@
 	pause_cond.notify_all();
 }
 
+bool Session::resetting_cpu() const
+{
+	return device->task_cancelled();
+}
+
 bool Session::draw_cpu(BufferParams& buffer_params)
 {
 	thread_scoped_lock display_lock(display_mutex);
@@ -584,6 +595,14 @@
 		reset_cpu(buffer_params, samples);
 }
 
+bool Session::resetting() const
+{
+	if(device_use_gl)
+		return resetting_gpu();
+	else
+		return resetting_cpu();
+}
+
 void Session::set_samples(int samples)
 {
 	if(samples != params.samples) {

Modified: trunk/blender/intern/cycles/render/session.h
===================================================================
--- trunk/blender/intern/cycles/render/session.h	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/render/session.h	2012-09-11 11:41:51 UTC (rev 50528)
@@ -116,6 +116,7 @@
 
 	bool ready_to_reset();
 	void reset(BufferParams& params, int samples);
+	bool resetting() const;
 	void set_samples(int samples);
 	void set_pause(bool pause);
 
@@ -139,10 +140,12 @@
 	void run_cpu();
 	bool draw_cpu(BufferParams& params);
 	void reset_cpu(BufferParams& params, int samples);
+	bool resetting_cpu() const;
 
 	void run_gpu();
 	bool draw_gpu(BufferParams& params);
 	void reset_gpu(BufferParams& params, int samples);
+	bool resetting_gpu() const;
 
 	bool acquire_tile(Device *tile_device, RenderTile& tile);
 	void update_tile_sample(RenderTile& tile);

Modified: trunk/blender/intern/cycles/util/util_task.cpp
===================================================================
--- trunk/blender/intern/cycles/util/util_task.cpp	2012-09-11 10:34:19 UTC (rev 50527)
+++ trunk/blender/intern/cycles/util/util_task.cpp	2012-09-11 11:41:51 UTC (rev 50528)
@@ -38,6 +38,8 @@
 
 void TaskPool::push(Task *task, bool front)
 {
+	thread_scoped_lock num_lock(num_mutex);
+
 	TaskScheduler::Entry entry;
 
 	entry.task = task;
@@ -102,22 +104,17 @@
 
 void TaskPool::cancel()
 {
+	thread_scoped_lock num_lock(num_mutex);
+
 	do_cancel = true;
 
 	TaskScheduler::clear(this);
-	
-	{
-		thread_scoped_lock num_lock(num_mutex);
-
-		while(num)
-			num_cond.wait(num_lock);
-	}
-
-	do_cancel = false;
 }
 
 void TaskPool::stop()
 {
+	thread_scoped_lock num_lock(num_mutex);
+	
 	TaskScheduler::clear(this);
 
 	assert(num == 0);
@@ -130,20 +127,20 @@
 
 void TaskPool::num_decrease(int done)
 {
-	num_mutex.lock();
 	num -= done;
-
 	assert(num >= 0);
-	if(num == 0)
+	
+	if(num == 0) {
+		do_cancel = false;
+		
 		num_cond.notify_all();
-
-	num_mutex.unlock();
+	}
 }
 
 void TaskPool::num_increase()
 {
-	thread_scoped_lock num_lock(num_mutex);
 	num++;
+	
 	num_cond.notify_all();
 }
 
@@ -239,7 +236,11 @@
 		delete entry.task;
 
 		/* notify pool task was done */
-		entry.pool->num_decrease(1);
+		{
+			/* not called from TaskPool, have to explicitly lock the mutex here */
+			thread_scoped_lock num_lock(entry.pool->num_mutex);
+			entry.pool->num_decrease(1);
+		}
 	}
 }
 




More information about the Bf-blender-cvs mailing list