[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [51952] trunk/blender/intern/cycles: Cycles OSL: support for the trace(point pos, vector dir, ...) function, to trace

Brecht Van Lommel brechtvanlommel at pandora.be
Tue Nov 6 20:59:11 CET 2012


Revision: 51952
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=51952
Author:   blendix
Date:     2012-11-06 19:59:10 +0000 (Tue, 06 Nov 2012)
Log Message:
-----------
Cycles OSL: support for the trace(point pos, vector dir, ...) function, to trace
rays from the OSL shader. The "shade" parameter is not supported currently, but
attributes can be retrieved from the object that was hit using the
getmessage("trace", ..) function.

As mentioned in the OSL specification, this function can't be used instead of
lighting, the main purpose is to allow shaders to "probe" nearby geometry, for
example to apply a projected texture that can be blocked by geometry, apply
more ?\226?\128?\156wear?\226?\128?\157 to exposed geometry, or make other ambient occlusion-like effects.

http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/OSL#Trace

Example .blend and render:
http://www.pasteall.org/blend/17347
http://www.pasteall.org/pic/show.php?id=40066

Modified Paths:
--------------
    trunk/blender/intern/cycles/kernel/osl/osl_globals.h
    trunk/blender/intern/cycles/kernel/osl/osl_services.cpp
    trunk/blender/intern/cycles/kernel/osl/osl_services.h
    trunk/blender/intern/cycles/kernel/osl/osl_shader.cpp
    trunk/blender/intern/cycles/render/mesh.cpp

Modified: trunk/blender/intern/cycles/kernel/osl/osl_globals.h
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/osl_globals.h	2012-11-06 19:59:07 UTC (rev 51951)
+++ trunk/blender/intern/cycles/kernel/osl/osl_globals.h	2012-11-06 19:59:10 UTC (rev 51952)
@@ -59,6 +59,7 @@
 
 	vector<AttributeMap> attribute_map;
 	ObjectNameMap object_name_map;
+	vector<ustring> object_names;
 
 	/* thread key for thread specific data lookup */
 	struct ThreadData {

Modified: trunk/blender/intern/cycles/kernel/osl/osl_services.cpp
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/osl_services.cpp	2012-11-06 19:59:07 UTC (rev 51951)
+++ trunk/blender/intern/cycles/kernel/osl/osl_services.cpp	2012-11-06 19:59:10 UTC (rev 51952)
@@ -22,6 +22,7 @@
 #include "object.h"
 #include "scene.h"
 
+#include "osl_closures.h"
 #include "osl_services.h"
 #include "osl_shader.h"
 
@@ -30,8 +31,14 @@
 
 #include "kernel_compat_cpu.h"
 #include "kernel_globals.h"
+#include "kernel_montecarlo.h"
+#include "kernel_projection.h"
+#include "kernel_differential.h"
 #include "kernel_object.h"
+#include "kernel_bvh.h"
 #include "kernel_triangle.h"
+#include "kernel_accumulate.h"
+#include "kernel_shader.h"
 
 CCL_NAMESPACE_BEGIN
 
@@ -282,7 +289,7 @@
 	return false;
 }
 
-static void set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val)
+static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val)
 {
 	if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
 	    type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
@@ -302,8 +309,10 @@
 			fval[7] = f[2].y;
 			fval[8] = f[2].z;
 		}
+
+		return true;
 	}
-	else {
+	else if(type == TypeDesc::TypeFloat) {
 		float *fval = (float *)val;
 		fval[0] = average(f[0]);
 
@@ -311,11 +320,26 @@
 			fval[1] = average(f[1]);
 			fval[2] = average(f[2]);
 		}
+
+		return true;
 	}
+
+	return false;
 }
 
-static void set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val)
+static bool set_attribute_float3(float3 f, TypeDesc type, bool derivatives, void *val)
 {
+	float3 fv[3];
+
+	fv[0] = f;
+	fv[1] = make_float3(0.0f, 0.0f, 0.0f);
+	fv[2] = make_float3(0.0f, 0.0f, 0.0f);
+
+	return set_attribute_float3(fv, type, derivatives, val);
+}
+
+static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val)
+{
 	if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
 	    type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor)
 	{
@@ -333,8 +357,10 @@
 			fval[7] = f[2];
 			fval[8] = f[2];
 		}
+
+		return true;
 	}
-	else {
+	else if(type == TypeDesc::TypeFloat) {
 		float *fval = (float *)val;
 		fval[0] = f[0];
 
@@ -342,9 +368,24 @@
 			fval[1] = f[1];
 			fval[2] = f[2];
 		}
+
+		return true;
 	}
+
+	return false;
 }
 
+static bool set_attribute_float(float f, TypeDesc type, bool derivatives, void *val)
+{
+	float fv[3];
+
+	fv[0] = f;
+	fv[1] = 0.0f;
+	fv[2] = 0.0f;
+
+	return set_attribute_float(fv, type, derivatives, val);
+}
+
 static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val)
 {
 	if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
@@ -362,6 +403,23 @@
 	return false;
 }
 
+static bool set_attribute_string(ustring str, TypeDesc type, bool derivatives, void *val)
+{
+	if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) {
+		ustring *sval = (ustring *)val;
+		sval[0] = str;
+
+		if (derivatives) {
+			sval[1] = OSLRenderServices::u_empty;
+			sval[2] = OSLRenderServices::u_empty;
+		}
+
+		return true;
+	}
+
+	return false;
+}
+
 static bool set_attribute_float3_3(float3 P[3], TypeDesc type, bool derivatives, void *val)
 {
 	if(type.vecsemantics == TypeDesc::POINT && type.arraylen >= 3) {
@@ -394,20 +452,18 @@
                                const TypeDesc& type, bool derivatives, void *val)
 {
 	if (attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector ||
-	         attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor)
+	    attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor)
 	{
 		float3 fval[3];
 		fval[0] = triangle_attribute_float3(kg, sd, attr.elem, attr.offset,
 		                                    (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
-		set_attribute_float3(fval, type, derivatives, val);
-		return true;
+		return set_attribute_float3(fval, type, derivatives, val);
 	}
 	else if (attr.type == TypeDesc::TypeFloat) {
 		float fval[3];
 		fval[0] = triangle_attribute_float(kg, sd, attr.elem, attr.offset,
 		                                   (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL);
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		return set_attribute_float(fval, type, derivatives, val);
 	}
 	else {
 		return false;
@@ -426,118 +482,76 @@
 static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name,
                                           TypeDesc type, bool derivatives, void *val)
 {
-	/* todo: turn this into hash table + callback once */
+	/* todo: turn this into hash table returning int, which can be used in switch */
 
 	/* Object Attributes */
 	if (name == "object:location") {
-		float3 fval[3];
-		fval[0] = object_location(kg, sd);
-		fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0);	/* derivates set to 0 */
-		set_attribute_float3(fval, type, derivatives, val);
-		return true;
+		float3 f = object_location(kg, sd);
+		return set_attribute_float3(f, type, derivatives, val);
 	}
 	else if (name == "object:index") {
-		float fval[3];
-		fval[0] = object_pass_id(kg, sd->object);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f = object_pass_id(kg, sd->object);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 	else if (name == "geom:dupli_generated") {
-		float3 fval[3];
-		fval[0] = object_dupli_generated(kg, sd->object);
-		fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0);	/* derivates set to 0 */
-		set_attribute_float3(fval, type, derivatives, val);
-		return true;
+		float3 f = object_dupli_generated(kg, sd->object);
+		return set_attribute_float3(f, type, derivatives, val);
 	}
 	else if (name == "geom:dupli_uv") {
-		float3 fval[3];
-		fval[0] = object_dupli_uv(kg, sd->object);
-		fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0);	/* derivates set to 0 */
-		set_attribute_float3(fval, type, derivatives, val);
-		return true;
+		float3 f = object_dupli_uv(kg, sd->object);
+		return set_attribute_float3(f, type, derivatives, val);
 	}
 	else if (name == "material:index") {
-		float fval[3];
-		fval[0] = shader_pass_id(kg, sd);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f = shader_pass_id(kg, sd);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 	else if (name == "object:random") {
-		float fval[3];
-		fval[0] = object_random_number(kg, sd->object);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f = object_random_number(kg, sd->object);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 
 	/* Particle Attributes */
 	else if (name == "particle:index") {
-		float fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_index(kg, particle_id);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f = particle_index(kg, particle_id);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 	else if (name == "particle:age") {
-		float fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_age(kg, particle_id);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f = particle_age(kg, particle_id);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 	else if (name == "particle:lifetime") {
-		float fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_lifetime(kg, particle_id);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f= particle_lifetime(kg, particle_id);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 	else if (name == "particle:location") {
-		float3 fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_location(kg, particle_id);
-		fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0);	/* derivates set to 0 */
-		set_attribute_float3(fval, type, derivatives, val);
-		return true;
+		float3 f = particle_location(kg, particle_id);
+		return set_attribute_float3(f, type, derivatives, val);
 	}
 #if 0	/* unsupported */
 	else if (name == "particle:rotation") {
-		float4 fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_rotation(kg, particle_id);
-		fval[1] = fval[2] = make_float4(0.0, 0.0, 0.0, 0.0);	/* derivates set to 0 */
-		set_attribute_float4(fval, type, derivatives, val);
-		return true;
+		float4 f = particle_rotation(kg, particle_id);
+		return set_attribute_float4(f, type, derivatives, val);
 	}
 #endif
 	else if (name == "particle:size") {
-		float fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_size(kg, particle_id);
-		fval[1] = fval[2] = 0.0;	/* derivates set to 0 */
-		set_attribute_float(fval, type, derivatives, val);
-		return true;
+		float f = particle_size(kg, particle_id);
+		return set_attribute_float(f, type, derivatives, val);
 	}
 	else if (name == "particle:velocity") {
-		float3 fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);
-		fval[0] = particle_velocity(kg, particle_id);
-		fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0);	/* derivates set to 0 */
-		set_attribute_float3(fval, type, derivatives, val);
-		return true;
+		float3 f = particle_velocity(kg, particle_id);
+		return set_attribute_float3(f, type, derivatives, val);
 	}
 	else if (name == "particle:angular_velocity") {
-		float3 fval[3];
 		uint particle_id = object_particle_id(kg, sd->object);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list