[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [50428] trunk/blender/intern/cycles: Fix for attribute lookup in OSL.

Lukas Toenne lukas.toenne at googlemail.com
Wed Sep 5 19:08:57 CEST 2012


Revision: 50428
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=50428
Author:   lukastoenne
Date:     2012-09-05 17:08:56 +0000 (Wed, 05 Sep 2012)
Log Message:
-----------
Fix for attribute lookup in OSL. This uses a map in the OSL globals instead of the device texture.

Modified Paths:
--------------
    trunk/blender/intern/cycles/kernel/kernel_triangle.h
    trunk/blender/intern/cycles/kernel/kernel_types.h
    trunk/blender/intern/cycles/render/attribute.cpp
    trunk/blender/intern/cycles/render/attribute.h
    trunk/blender/intern/cycles/render/mesh.cpp

Modified: trunk/blender/intern/cycles/kernel/kernel_triangle.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_triangle.h	2012-09-05 16:29:16 UTC (rev 50427)
+++ trunk/blender/intern/cycles/kernel/kernel_triangle.h	2012-09-05 17:08:56 UTC (rev 50428)
@@ -15,9 +15,11 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
- 
+
 #include "kernel_projection.h"
 
+#include "util_param.h"
+
 CCL_NAMESPACE_BEGIN
 
 /* Point on triangle for Moller-Trumbore triangles */
@@ -183,17 +185,39 @@
 
 /* motion */
 
+/* note: declared in kernel.h, have to add it here because kernel.h is not available */
+bool kernel_osl_use(KernelGlobals *kg);
+
 __device int triangle_find_attribute(KernelGlobals *kg, ShaderData *sd, uint id)
 {
-	/* find attribute by unique id */
-	uint attr_offset = sd->object*kernel_data.bvh.attributes_map_stride;
-	uint4 attr_map = kernel_tex_fetch(__attributes_map, attr_offset);
 
-	while(attr_map.x != id)
-		attr_map = kernel_tex_fetch(__attributes_map, ++attr_offset);
-
-	/* return result */
-	return (attr_map.y == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : attr_map.z;
+#ifdef __OSL__
+	if (kernel_osl_use(kg)) {
+		/* for OSL, a hash map is used to lookup the attribute by name. */
+		OSLGlobals::AttributeMap &attr_map = kg->osl.attribute_map[sd->object];
+		ustring stdname = ustring(std::string("std::") + attribute_standard_name((AttributeStandard)id).c_str());
+		OSLGlobals::AttributeMap::const_iterator it = attr_map.find(stdname);
+		if (it != attr_map.end()) {
+			const OSLGlobals::Attribute &osl_attr = it->second;
+			/* return result */
+			return (osl_attr.elem == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : osl_attr.offset;
+		}
+		else
+			return (int)ATTR_STD_NOT_FOUND;
+	}
+	else
+#endif
+	{
+		/* for SVM, find attribute by unique id */
+		uint attr_offset = sd->object*kernel_data.bvh.attributes_map_stride;
+		uint4 attr_map = kernel_tex_fetch(__attributes_map, attr_offset);
+		
+		while(attr_map.x != id)
+			attr_map = kernel_tex_fetch(__attributes_map, ++attr_offset);
+		
+		/* return result */
+		return (attr_map.y == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND : attr_map.z;
+	}
 }
 
 __device float4 triangle_motion_vector(KernelGlobals *kg, ShaderData *sd)

Modified: trunk/blender/intern/cycles/kernel/kernel_types.h
===================================================================
--- trunk/blender/intern/cycles/kernel/kernel_types.h	2012-09-05 16:29:16 UTC (rev 50427)
+++ trunk/blender/intern/cycles/kernel/kernel_types.h	2012-09-05 17:08:56 UTC (rev 50428)
@@ -22,6 +22,8 @@
 #include "kernel_math.h"
 #include "svm/svm_types.h"
 
+#include "util_param.h"
+
 #ifndef __KERNEL_GPU__
 #define __KERNEL_CPU__
 #endif
@@ -368,6 +370,30 @@
 	ATTR_STD_NOT_FOUND = ~0
 } AttributeStandard;
 
+__device ustring attribute_standard_name(AttributeStandard std)
+{
+	if(std == ATTR_STD_VERTEX_NORMAL)
+		return ustring("N");
+	else if(std == ATTR_STD_FACE_NORMAL)
+		return ustring("Ng");
+	else if(std == ATTR_STD_UV)
+		return ustring("uv");
+	else if(std == ATTR_STD_GENERATED)
+		return ustring("generated");
+	else if(std == ATTR_STD_POSITION_UNDEFORMED)
+		return ustring("undeformed");
+	else if(std == ATTR_STD_POSITION_UNDISPLACED)
+		return ustring("undisplaced");
+	else if(std == ATTR_STD_MOTION_PRE)
+		return ustring("motion_pre");
+	else if(std == ATTR_STD_MOTION_POST)
+		return ustring("motion_post");
+	else if(std == ATTR_STD_PARTICLE)
+		return ustring("particle");
+	
+	return ustring();
+}
+
 /* Closure data */
 
 #define MAX_CLOSURE 8

Modified: trunk/blender/intern/cycles/render/attribute.cpp
===================================================================
--- trunk/blender/intern/cycles/render/attribute.cpp	2012-09-05 16:29:16 UTC (rev 50427)
+++ trunk/blender/intern/cycles/render/attribute.cpp	2012-09-05 17:08:56 UTC (rev 50428)
@@ -84,30 +84,6 @@
 	return false;
 }
 
-ustring Attribute::standard_name(AttributeStandard std)
-{
-	if(std == ATTR_STD_VERTEX_NORMAL)
-		return ustring("N");
-	else if(std == ATTR_STD_FACE_NORMAL)
-		return ustring("Ng");
-	else if(std == ATTR_STD_UV)
-		return ustring("uv");
-	else if(std == ATTR_STD_GENERATED)
-		return ustring("generated");
-	else if(std == ATTR_STD_POSITION_UNDEFORMED)
-		return ustring("undeformed");
-	else if(std == ATTR_STD_POSITION_UNDISPLACED)
-		return ustring("undisplaced");
-	else if(std == ATTR_STD_MOTION_PRE)
-		return ustring("motion_pre");
-	else if(std == ATTR_STD_MOTION_POST)
-		return ustring("motion_post");
-	else if(std == ATTR_STD_PARTICLE)
-		return ustring("particle");
-
-	return ustring();
-}
-
 /* Attribute Set */
 
 AttributeSet::AttributeSet()
@@ -178,7 +154,7 @@
 	Attribute *attr = NULL;
 
 	if(name == ustring())
-		name = Attribute::standard_name(std);
+		name = attribute_standard_name(std);
 
 	if(std == ATTR_STD_VERTEX_NORMAL)
 		attr = add(name, TypeDesc::TypeNormal, Attribute::VERTEX);

Modified: trunk/blender/intern/cycles/render/attribute.h
===================================================================
--- trunk/blender/intern/cycles/render/attribute.h	2012-09-05 16:29:16 UTC (rev 50427)
+++ trunk/blender/intern/cycles/render/attribute.h	2012-09-05 17:08:56 UTC (rev 50428)
@@ -71,7 +71,6 @@
 	const float *data_float() const { return (float*)data(); }
 
 	static bool same_storage(TypeDesc a, TypeDesc b);
-	static ustring standard_name(AttributeStandard std);
 };
 
 /* Attribute Set

Modified: trunk/blender/intern/cycles/render/mesh.cpp
===================================================================
--- trunk/blender/intern/cycles/render/mesh.cpp	2012-09-05 16:29:16 UTC (rev 50427)
+++ trunk/blender/intern/cycles/render/mesh.cpp	2012-09-05 17:08:56 UTC (rev 50428)
@@ -366,7 +366,7 @@
 
 			if(req.std != ATTR_STD_NONE) {
 				/* if standard attribute, add lookup by std:: name convention */
-				ustring stdname = ustring(string("std::") + Attribute::standard_name(req.std).c_str());
+				ustring stdname = ustring(string("std::") + attribute_standard_name(req.std).c_str());
 				og->attribute_map[i][stdname] = osl_attr;
 			}
 			else if(req.name != ustring()) {




More information about the Bf-blender-cvs mailing list