[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [50637] trunk/blender/intern/cycles: OSL implementation of RGB ramp node.

Lukas Toenne lukas.toenne at googlemail.com
Sat Sep 15 17:41:37 CEST 2012


Revision: 50637
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=50637
Author:   lukastoenne
Date:     2012-09-15 15:41:37 +0000 (Sat, 15 Sep 2012)
Log Message:
-----------
OSL implementation of RGB ramp node.

The sampled color ramp data is passed to OSL as a color array. This has to be done as actual float[3] array though, since the Cycles float3 type actually contains 4 floats, leading to shifting color components in the array.

Additional parameter set functions for arrays have been added to the Cycles OSL interface for this purpose.

Modified Paths:
--------------
    trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt
    trunk/blender/intern/cycles/kernel/osl/nodes/oslutil.h
    trunk/blender/intern/cycles/render/nodes.cpp
    trunk/blender/intern/cycles/render/osl.cpp
    trunk/blender/intern/cycles/render/osl.h

Added Paths:
-----------
    trunk/blender/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt	2012-09-15 15:17:51 UTC (rev 50636)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/CMakeLists.txt	2012-09-15 15:41:37 UTC (rev 50637)
@@ -43,6 +43,7 @@
 	node_output_surface.osl
 	node_output_volume.osl
 	node_particle_info.osl
+	node_rgb_ramp.osl
 	node_separate_rgb.osl
 	node_sky_texture.osl
 	node_texture_coordinate.osl

Added: trunk/blender/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl	                        (rev 0)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl	2012-09-15 15:41:37 UTC (rev 50637)
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2011, Blender Foundation.
+ *
+ * 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.
+ */
+
+#include "stdosl.h"
+#include "oslutil.h"
+
+shader node_rgb_ramp(
+    color ramp_color[RAMP_TABLE_SIZE] = {0.0},
+    float ramp_alpha[RAMP_TABLE_SIZE] = {0.0},
+
+    float Fac = 0.0,
+    output color Color = color(0.0, 0.0, 0.0),
+    output float Alpha = 1.0
+    )
+{
+    float f = clamp(Fac, 0.0, 1.0)*(RAMP_TABLE_SIZE-1);
+
+    int i = (int)f;
+    float t = f - (float)i;
+
+    Color = ramp_color[i];
+    Alpha = ramp_alpha[i];
+
+    if (t > 0.0) {
+        Color = (1.0 - t)*Color + t*ramp_color[i+1];
+        Alpha = (1.0 - t)*Alpha + t*ramp_alpha[i+1];
+    }
+}
+

Modified: trunk/blender/intern/cycles/kernel/osl/nodes/oslutil.h
===================================================================
--- trunk/blender/intern/cycles/kernel/osl/nodes/oslutil.h	2012-09-15 15:17:51 UTC (rev 50636)
+++ trunk/blender/intern/cycles/kernel/osl/nodes/oslutil.h	2012-09-15 15:41:37 UTC (rev 50637)
@@ -29,6 +29,9 @@
 #ifndef CCL_OSLUTIL_H
 #define CCL_OSLUTIL_H
 
+/* NB: must match the value in kernel_types.h */
+#define RAMP_TABLE_SIZE 256
+
 // Return wireframe opacity factor [0, 1] given a geometry type in
 // ("triangles", "polygons" or "patches"), and a line_width in raster
 // or world space depending on the last (raster) boolean argument.

Modified: trunk/blender/intern/cycles/render/nodes.cpp
===================================================================
--- trunk/blender/intern/cycles/render/nodes.cpp	2012-09-15 15:17:51 UTC (rev 50636)
+++ trunk/blender/intern/cycles/render/nodes.cpp	2012-09-15 15:41:37 UTC (rev 50637)
@@ -2768,6 +2768,19 @@
 
 void RGBRampNode::compile(OSLCompiler& compiler)
 {
+	/* OSL shader only takes separate RGB and A array, split the RGBA base array */
+	/* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
+	float ramp_color[RAMP_TABLE_SIZE][3];
+	float ramp_alpha[RAMP_TABLE_SIZE];
+	for (int i = 0; i < RAMP_TABLE_SIZE; ++i) {
+		ramp_color[i][0] = ramp[i].x;
+		ramp_color[i][1] = ramp[i].y;
+		ramp_color[i][2] = ramp[i].z;
+		ramp_alpha[i] = ramp[i].w;
+	}
+	compiler.parameter_color_array("ramp_color", ramp_color, RAMP_TABLE_SIZE);
+	compiler.parameter_array("ramp_alpha", ramp_alpha, RAMP_TABLE_SIZE);
+	
 	compiler.add(this, "node_rgb_ramp");
 }
 

Modified: trunk/blender/intern/cycles/render/osl.cpp
===================================================================
--- trunk/blender/intern/cycles/render/osl.cpp	2012-09-15 15:17:51 UTC (rev 50636)
+++ trunk/blender/intern/cycles/render/osl.cpp	2012-09-15 15:41:37 UTC (rev 50637)
@@ -309,6 +309,70 @@
 	ss->Parameter(name, TypeDesc::TypeMatrix, (float*)&tfm);
 }
 
+void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeFloat;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, f);
+}
+
+void OSLCompiler::parameter_color_array(const char *name, const float f[][3], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeColor;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, f);
+}
+
+void OSLCompiler::parameter_vector_array(const char *name, const float f[][3], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeVector;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, f);
+}
+
+void OSLCompiler::parameter_normal_array(const char *name, const float f[][3], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeNormal;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, f);
+}
+
+void OSLCompiler::parameter_point_array(const char *name, const float f[][3], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypePoint;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, f);
+}
+
+void OSLCompiler::parameter_array(const char *name, const int f[], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeInt;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, f);
+}
+
+void OSLCompiler::parameter_array(const char *name, const char * const s[], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeString;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, s);
+}
+
+void OSLCompiler::parameter_array(const char *name, const Transform tfm[], int arraylen)
+{
+	OSL::ShadingSystem *ss = (OSL::ShadingSystem*)shadingsys;
+	TypeDesc type = TypeDesc::TypeMatrix;
+	type.arraylen = arraylen;
+	ss->Parameter(name, type, (const float *)tfm);
+}
+
 void OSLCompiler::find_dependencies(set<ShaderNode*>& dependencies, ShaderInput *input)
 {
 	ShaderNode *node = (input->link)? input->link->parent: NULL;

Modified: trunk/blender/intern/cycles/render/osl.h
===================================================================
--- trunk/blender/intern/cycles/render/osl.h	2012-09-15 15:17:51 UTC (rev 50636)
+++ trunk/blender/intern/cycles/render/osl.h	2012-09-15 15:41:37 UTC (rev 50637)
@@ -79,6 +79,15 @@
 	void parameter(const char *name, ustring str);
 	void parameter(const char *name, const Transform& tfm);
 
+	void parameter_array(const char *name, const float f[], int arraylen);
+	void parameter_color_array(const char *name, const float f[][3], int arraylen);
+	void parameter_vector_array(const char *name, const float f[][3], int arraylen);
+	void parameter_normal_array(const char *name, const float f[][3], int arraylen);
+	void parameter_point_array(const char *name, const float f[][3], int arraylen);
+	void parameter_array(const char *name, const int f[], int arraylen);
+	void parameter_array(const char *name, const char * const s[], int arraylen);
+	void parameter_array(const char *name, const Transform tfm[], int arraylen);
+
 	ShaderType output_type() { return current_type; }
 
 	bool background;




More information about the Bf-blender-cvs mailing list