[Bf-blender-cvs] [dd9c53b] master: Freestyle: View map caching.

Tamito Kajiyama noreply at git.blender.org
Thu Oct 2 10:52:36 CEST 2014


Commit: dd9c53b312a62f1cc87e26cab3fde0c568c14844
Author: Tamito Kajiyama
Date:   Tue Jan 28 23:24:59 2014 +0900
Branches: master
https://developer.blender.org/rBdd9c53b312a62f1cc87e26cab3fde0c568c14844

Freestyle: View map caching.

New render layer option named "View map cache" is added to reuse a
previously computed view map for subsequent rendering.  The cache is
automatically updated when the mesh geometry of the input 3D scene has
been changed.

This functionality offers a major performance boost for Freestyle
animation rendering when camera-space mesh geometry is static, as well
as for repeated still renders with updates of line stylization options.

Although the "View map cache" toggle is a render layer option, the cache
memory is shared by all render layers and scenes.  This means that if
Freestyle is used for two or more render layers (possibly in different
scenes through the compositor), then the cached view map for one render
layer is replaced by a new view map for another render layer and hence
no performance gain is expected.

===================================================================

M	release/scripts/startup/bl_ui/properties_freestyle.py
M	source/blender/freestyle/CMakeLists.txt
M	source/blender/freestyle/FRS_freestyle.h
M	source/blender/freestyle/intern/application/Controller.cpp
M	source/blender/freestyle/intern/application/Controller.h
M	source/blender/freestyle/intern/blender_interface/FRS_freestyle.cpp
A	source/blender/freestyle/intern/scene_graph/SceneHash.cpp
A	source/blender/freestyle/intern/scene_graph/SceneHash.h
M	source/blender/makesdna/DNA_freestyle_types.h
M	source/blender/makesrna/intern/CMakeLists.txt
M	source/blender/makesrna/intern/SConscript
M	source/blender/makesrna/intern/rna_scene.c

===================================================================

diff --git a/release/scripts/startup/bl_ui/properties_freestyle.py b/release/scripts/startup/bl_ui/properties_freestyle.py
index d9bfd89..6abd6f4 100644
--- a/release/scripts/startup/bl_ui/properties_freestyle.py
+++ b/release/scripts/startup/bl_ui/properties_freestyle.py
@@ -122,7 +122,9 @@ class RENDERLAYER_PT_freestyle(RenderLayerFreestyleButtonsPanel, Panel):
 
         layout.active = rl.use_freestyle
 
+        row = layout.row()
         layout.prop(freestyle, "mode", text="Control mode")
+        layout.prop(freestyle, "use_view_map_cache", text="View Map Cache")
         layout.label(text="Edge Detection Options:")
 
         split = layout.split()
diff --git a/source/blender/freestyle/CMakeLists.txt b/source/blender/freestyle/CMakeLists.txt
index c94a5ac..bfd1c17 100644
--- a/source/blender/freestyle/CMakeLists.txt
+++ b/source/blender/freestyle/CMakeLists.txt
@@ -412,6 +412,8 @@ set(SRC
 	intern/scene_graph/OrientedLineRep.h
 	intern/scene_graph/Rep.cpp
 	intern/scene_graph/Rep.h
+	intern/scene_graph/SceneHash.cpp
+	intern/scene_graph/SceneHash.h
 	intern/scene_graph/ScenePrettyPrinter.cpp
 	intern/scene_graph/ScenePrettyPrinter.h
 	intern/scene_graph/SceneVisitor.cpp
diff --git a/source/blender/freestyle/FRS_freestyle.h b/source/blender/freestyle/FRS_freestyle.h
index fc9fc35..1815883 100644
--- a/source/blender/freestyle/FRS_freestyle.h
+++ b/source/blender/freestyle/FRS_freestyle.h
@@ -48,6 +48,7 @@ int FRS_is_freestyle_enabled(struct SceneRenderLayer *srl);
 void FRS_init_stroke_rendering(struct Render *re);
 struct Render *FRS_do_stroke_rendering(struct Render *re, struct SceneRenderLayer *srl, int render);
 void FRS_finish_stroke_rendering(struct Render *re);
+void FRS_free_view_map_cache();
 void FRS_composite_result(struct Render *re, struct SceneRenderLayer *srl, struct Render *freestyle_render);
 void FRS_exit(void);
 
diff --git a/source/blender/freestyle/intern/application/Controller.cpp b/source/blender/freestyle/intern/application/Controller.cpp
index 1761996..a7f936f 100644
--- a/source/blender/freestyle/intern/application/Controller.cpp
+++ b/source/blender/freestyle/intern/application/Controller.cpp
@@ -118,6 +118,7 @@ Controller::Controller()
 	_Canvas = new AppCanvas;
 
 	_inter = new PythonInterpreter();
+	_EnableViewMapCache = false;
 	_EnableQI = true;
 	_EnableFaceSmoothness = false;
 	_ComputeRidges = true;
@@ -212,6 +213,19 @@ void Controller::setContext(bContext *C)
 	py_inter->setContext(C);
 }
 
+bool Controller::hitViewMapCache()
+{
+	if (!_EnableViewMapCache) {
+		return false;
+	}
+	real hashCode = sceneHashFunc.getValue();
+	if (prevSceneHash == hashCode) {
+		return (NULL != _ViewMap);
+	}
+	prevSceneHash = hashCode;
+	return false;
+}
+
 int Controller::LoadMesh(Render *re, SceneRenderLayer *srl)
 {
 	BlenderFileLoader loader(re, srl);
@@ -242,6 +256,7 @@ int Controller::LoadMesh(Render *re, SceneRenderLayer *srl)
 	if (G.debug & G_DEBUG_FREESTYLE) {
 		cout << "Scene loaded" << endl;
 		printf("Mesh cleaning    : %lf\n", duration);
+		printf("View map cache   : %s\n", _EnableViewMapCache ? "enabled" : "disabled");
 	}
 	_SceneNumFaces += loader.numFacesRead();
 
@@ -263,6 +278,22 @@ int Controller::LoadMesh(Render *re, SceneRenderLayer *srl)
 	if (_pRenderMonitor->testBreak())
 		return 0;
 
+	if (_EnableViewMapCache) {
+		sceneHashFunc.reset();
+		blenderScene->accept(sceneHashFunc);
+		if (G.debug & G_DEBUG_FREESTYLE) {
+			printf("Scene hash       : %.16e\n", sceneHashFunc.getValue());
+		}
+		if (hitViewMapCache()) {
+			ClearRootNode();
+			return 0;
+		}
+		else {
+			delete _ViewMap;
+			_ViewMap = NULL;
+		}
+	}
+
 	_Chrono.start();
 
 	WXEdgeBuilder wx_builder;
@@ -357,7 +388,7 @@ void Controller::DeleteWingedEdge()
 	_minEdgeSize = DBL_MAX;
 }
 
-void Controller::DeleteViewMap()
+void Controller::DeleteViewMap(bool freeCache)
 {
 	_pView->DetachSilhouette();
 	if (NULL != _SilhouetteNode) {
@@ -387,14 +418,15 @@ void Controller::DeleteViewMap()
 
 	_pView->DetachDebug();
 	if (NULL != _DebugNode) {
-	int ref = _DebugNode->destroy();
+		int ref = _DebugNode->destroy();
 		if (0 == ref)
 			_DebugNode->addRef();
 	}
 
-	if (NULL != _ViewMap) {
+	if ((freeCache || !_EnableViewMapCache) && NULL != _ViewMap) {
 		delete _ViewMap;
 		_ViewMap = NULL;
+		prevSceneHash = -1.0;
 	}
 }
 
@@ -403,40 +435,7 @@ void Controller::ComputeViewMap()
 	if (!_ListOfModels.size())
 		return;
 
-	if (NULL != _ViewMap) {
-		delete _ViewMap;
-		_ViewMap = NULL;
-	}
-
-	_pView->DetachDebug();
-	if (NULL != _DebugNode) {
-		int ref = _DebugNode->destroy();
-		if (0 == ref)
-			_DebugNode->addRef();
-	}
-
-	_pView->DetachSilhouette();
-	if (NULL != _SilhouetteNode) {
-		int ref = _SilhouetteNode->destroy();
-		if (0 == ref)
-			delete _SilhouetteNode;
-	}
-
-#if 0
-	if (NULL != _ProjectedSilhouette) {
-		int ref = _ProjectedSilhouette->destroy();
-		if (0 == ref)
-			delete _ProjectedSilhouette;
-	}
-
-	if (NULL != _VisibleProjectedSilhouette) {
-		int ref = _VisibleProjectedSilhouette->destroy();
-		if (0 == ref) {
-			delete _VisibleProjectedSilhouette;
-			_VisibleProjectedSilhouette = NULL;
-		}
-	}
-#endif
+	DeleteViewMap(true);
 
 	// retrieve the 3D viewpoint and transformations information
 	//----------------------------------------------------------
@@ -763,6 +762,16 @@ int Controller::getVisibilityAlgo()
 	return FREESTYLE_ALGO_ADAPTIVE_TRADITIONAL;
 }
 
+void Controller::setViewMapCache(bool iBool)
+{
+	_EnableViewMapCache = iBool;
+}
+
+bool Controller::getViewMapCache() const
+{
+	return _EnableViewMapCache;
+}
+
 void Controller::setQuantitativeInvisibility(bool iBool)
 {
 	_EnableQI = iBool;
diff --git a/source/blender/freestyle/intern/application/Controller.h b/source/blender/freestyle/intern/application/Controller.h
index f5e5034..84ee338 100644
--- a/source/blender/freestyle/intern/application/Controller.h
+++ b/source/blender/freestyle/intern/application/Controller.h
@@ -32,6 +32,7 @@
 
 //#include "ConfigIO.h"
 #include "../geometry/FastGrid.h"
+#include "../scene_graph/SceneHash.h"
 #include "../system/Interpreter.h"
 #include "../system/ProgressBar.h"
 #include "../system/Precision.h"
@@ -96,7 +97,7 @@ public:
 	void Clear();
 	void ClearRootNode();
 	void DeleteWingedEdge();
-	void DeleteViewMap();
+	void DeleteViewMap(bool freeCache = false);
 	void toggleLayer(unsigned index, bool iDisplay);
 	void setModified(unsigned index, bool iMod);
 	void resetModified(bool iMod=false);
@@ -118,6 +119,8 @@ public:
 	void setVisibilityAlgo(int algo);
 	int getVisibilityAlgo();
 
+	void setViewMapCache(bool iBool);
+	bool getViewMapCache() const;
 	void setQuantitativeInvisibility(bool iBool); // if true, we compute quantitativeInvisibility
 	bool getQuantitativeInvisibility() const;
 	void setFaceSmoothness(bool iBool);
@@ -144,6 +147,8 @@ public:
 	void setModulesDir(const string& dir);
 	string getModulesDir() const;
 
+	bool hitViewMapCache();
+
 	void resetInterpreter();
 
 public:
@@ -231,6 +236,7 @@ private:
 	string _help_index;
 	string _browser_cmd;
 
+	bool _EnableViewMapCache;
 	bool _EnableQI;
 	bool _EnableFaceSmoothness;
 	bool _ComputeRidges;
@@ -244,6 +250,9 @@ private:
 
 	FEdgeXDetector edgeDetector;
 
+	SceneHash sceneHashFunc;
+	real prevSceneHash = -1.0;
+
 #ifdef WITH_CXX_GUARDEDALLOC
 	MEM_CXX_CLASS_ALLOC_FUNCS("Freestyle:Controller")
 #endif
diff --git a/source/blender/freestyle/intern/blender_interface/FRS_freestyle.cpp b/source/blender/freestyle/intern/blender_interface/FRS_freestyle.cpp
index 32f49d4..7443da7 100644
--- a/source/blender/freestyle/intern/blender_interface/FRS_freestyle.cpp
+++ b/source/blender/freestyle/intern/blender_interface/FRS_freestyle.cpp
@@ -474,6 +474,9 @@ static void prepare(Main *bmain, Render *re, SceneRenderLayer *srl)
 		cout << "  Z = " << (z ? "enabled" : "disabled") << endl;
 	}
 
+	if (controller->hitViewMapCache())
+		return;
+
 	// compute view map
 	re->i.infostr = "Freestyle: View map creation";
 	re->stats_draw(re->sdh, &re->i);
@@ -589,6 +592,7 @@ Render *FRS_do_stroke_rendering(Render *re, SceneRenderLayer *srl, int render)
 
 	RenderMonitor monitor(re);
 	controller->setRenderMonitor(&monitor);
+	controller->setViewMapCache((srl->freestyleConfig.flags & FREESTYLE_VIEW_MAP_CACHE) ? true : false);
 
 	if (G.debug & G_DEBUG_FREESTYLE) {
 		cout << endl;
@@ -647,6 +651,17 @@ void FRS_finish_stroke_rendering(Render *re)
 	controller->Clear();
 }
 
+void FRS_free_view_map_cache()
+{
+	// free cache
+	controller->DeleteViewMap(true);
+#if 0
+	if (G.debug & G_DEBUG_FREESTYLE) {
+		printf("View map cache freed\n");
+	}
+#endif
+}
+
 //=======================================================
 //   Freestyle Panel Configuration
 //=======================================================
diff --git a/source/blender/freestyle/intern/scene_graph/SceneHash.cpp b/source/blender/freestyle/intern/scene_graph/SceneHash.cpp
new file mode 100644
index 0000000..6e8856f
--- /dev/null
+++ b/source/blender/freestyle/intern/scene_graph/SceneHash.cpp
@@ -0,0 +1,39 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/freestyle/intern/scene_graph/SceneHash.cpp
+ *  \ingroup freestyle
+ */
+
+#include "SceneHash.h"
+
+namespace Freestyle {
+
+void SceneHash::visitIndexedFaceSet(IndexedFaceSet& ifs)
+{
+	const real *v = ifs.vertices();
+	const unsigned n = ifs.vsize();
+
+	for (unsigned i = 0; i < n; i++) {
+		_hashcode += v[i];
+	}
+}
+
+} /* namespace Freestyle */
diff --git a/source

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list