[Bf-blender-cvs] [40453db] compositor-2016: Code refactor: use dynamic shader node array lengths now that OSL supports them.

Brecht Van Lommel noreply at git.blender.org
Wed Jun 8 21:47:32 CEST 2016


Commit: 40453dbca1ee07dc1ecc4a8ad148b8c20e622270
Author: Brecht Van Lommel
Date:   Sun May 8 01:54:35 2016 +0200
Branches: compositor-2016
https://developer.blender.org/rB40453dbca1ee07dc1ecc4a8ad148b8c20e622270

Code refactor: use dynamic shader node array lengths now that OSL supports them.

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

M	intern/cycles/blender/blender_shader.cpp
M	intern/cycles/blender/blender_util.h
M	intern/cycles/kernel/shaders/node_rgb_curves.osl
M	intern/cycles/kernel/shaders/node_rgb_ramp.osl
M	intern/cycles/kernel/shaders/node_vector_curves.osl
M	intern/cycles/kernel/shaders/oslutil.h
M	intern/cycles/kernel/svm/svm_ramp.h
M	intern/cycles/render/nodes.cpp
M	intern/cycles/render/nodes.h
M	intern/cycles/render/osl.cpp
M	intern/cycles/render/osl.h
M	intern/cycles/render/svm.cpp
M	intern/cycles/render/svm.h

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

diff --git a/intern/cycles/blender/blender_shader.cpp b/intern/cycles/blender/blender_shader.cpp
index 6e4ca66..04d8b14 100644
--- a/intern/cycles/blender/blender_shader.cpp
+++ b/intern/cycles/blender/blender_shader.cpp
@@ -291,7 +291,7 @@ static ShaderNode *add_node(Scene *scene,
 		RGBRampNode *ramp = new RGBRampNode();
 		BL::ShaderNodeValToRGB b_ramp_node(b_node);
 		BL::ColorRamp b_color_ramp(b_ramp_node.color_ramp());
-		colorramp_to_array(b_color_ramp, ramp->ramp, RAMP_TABLE_SIZE);
+		colorramp_to_array(b_color_ramp, ramp->ramp, ramp->ramp_alpha, RAMP_TABLE_SIZE);
 		ramp->interpolate = b_color_ramp.interpolation() != BL::ColorRamp::interpolation_CONSTANT;
 		node = ramp;
 	}
diff --git a/intern/cycles/blender/blender_util.h b/intern/cycles/blender/blender_util.h
index cefc01b..d856982 100644
--- a/intern/cycles/blender/blender_util.h
+++ b/intern/cycles/blender/blender_util.h
@@ -58,14 +58,19 @@ static inline BL::Mesh object_to_mesh(BL::BlendData& data,
 }
 
 static inline void colorramp_to_array(BL::ColorRamp& ramp,
-                                      float4 *data,
+                                      array<float3>& ramp_color,
+                                      array<float>& ramp_alpha,
                                       int size)
 {
+	ramp_color.resize(size);
+	ramp_alpha.resize(size);
+
 	for(int i = 0; i < size; i++) {
 		float color[4];
 
 		ramp.evaluate((float)i/(float)(size-1), color);
-		data[i] = make_float4(color[0], color[1], color[2], color[3]);
+		ramp_color[i] = make_float3(color[0], color[1], color[2]);
+		ramp_alpha[i] = color[3];
 	}
 }
 
@@ -105,7 +110,7 @@ static inline void curvemapping_to_array(BL::CurveMapping& cumap,
 }
 
 static inline void curvemapping_color_to_array(BL::CurveMapping& cumap,
-                                               float4 *data,
+                                               array<float3>& data,
                                                int size,
                                                bool rgb_curve)
 {
@@ -132,6 +137,8 @@ static inline void curvemapping_color_to_array(BL::CurveMapping& cumap,
 	BL::CurveMap mapG = cumap.curves[1];
 	BL::CurveMap mapB = cumap.curves[2];
 
+	data.resize(size);
+
 	if(rgb_curve) {
 		BL::CurveMap mapI = cumap.curves[3];
 
diff --git a/intern/cycles/kernel/shaders/node_rgb_curves.osl b/intern/cycles/kernel/shaders/node_rgb_curves.osl
index fc93dbd..8e208e8 100644
--- a/intern/cycles/kernel/shaders/node_rgb_curves.osl
+++ b/intern/cycles/kernel/shaders/node_rgb_curves.osl
@@ -17,8 +17,10 @@
 #include "stdosl.h"
 #include "oslutil.h"
 
-float ramp_lookup(color ramp[RAMP_TABLE_SIZE], float at, int component)
+float ramp_lookup(color ramp[], float at, int component)
 {
+	int table_size = arraylength(ramp);
+
 	if (at < 0.0 || at > 1.0) {
 		float t0, dy;
 		if (at < 0.0) {
@@ -27,19 +29,19 @@ float ramp_lookup(color ramp[RAMP_TABLE_SIZE], float at, int component)
 			at = -at;
 		}
 		else {
-			t0 = ramp[RAMP_TABLE_SIZE - 1][component];
-			dy = t0 - ramp[RAMP_TABLE_SIZE - 2][component];
+			t0 = ramp[table_size - 1][component];
+			dy = t0 - ramp[table_size - 2][component];
 			at = at - 1.0;
 		}
-		return t0 + dy * at * (RAMP_TABLE_SIZE - 1);
+		return t0 + dy * at * (table_size - 1);
 	}
 
-	float f = clamp(at, 0.0, 1.0) * (RAMP_TABLE_SIZE - 1);
+	float f = clamp(at, 0.0, 1.0) * (table_size - 1);
 
 	/* clamp int as well in case of NaN */
 	int i = (int)f;
 	if (i < 0) i = 0;
-	if (i >= RAMP_TABLE_SIZE) i = RAMP_TABLE_SIZE - 1;
+	if (i >= table_size) i = table_size - 1;
 	float t = f - (float)i;
 
 	float result = ramp[i][component];
@@ -51,7 +53,7 @@ float ramp_lookup(color ramp[RAMP_TABLE_SIZE], float at, int component)
 }
 
 shader node_rgb_curves(
-	color ramp[RAMP_TABLE_SIZE] = {0.0},
+	color ramp[] = {0.0},
 	float min_x = 0.0,
 	float max_x = 1.0,
 
diff --git a/intern/cycles/kernel/shaders/node_rgb_ramp.osl b/intern/cycles/kernel/shaders/node_rgb_ramp.osl
index 0202ba0..2ab6b67 100644
--- a/intern/cycles/kernel/shaders/node_rgb_ramp.osl
+++ b/intern/cycles/kernel/shaders/node_rgb_ramp.osl
@@ -18,20 +18,21 @@
 #include "oslutil.h"
 
 shader node_rgb_ramp(
-	color ramp_color[RAMP_TABLE_SIZE] = {0.0},
-	float ramp_alpha[RAMP_TABLE_SIZE] = {0.0},
+	color ramp_color[] = {0.0},
+	float ramp_alpha[] = {0.0},
 	int ramp_interpolate = 1,
 
 	float Fac = 0.0,
 	output color Color = 0.0,
 	output float Alpha = 1.0)
 {
-	float f = clamp(Fac, 0.0, 1.0) * (RAMP_TABLE_SIZE - 1);
+	int table_size = arraylength(ramp_color);
+	float f = clamp(Fac, 0.0, 1.0) * (table_size - 1);
 
 	/* clamp int as well in case of NaN */
 	int i = (int)f;
 	if (i < 0) i = 0;
-	if (i >= RAMP_TABLE_SIZE) i = RAMP_TABLE_SIZE - 1;
+	if (i >= table_size) i = table_size - 1;
 	float t = f - (float)i;
 
 	Color = ramp_color[i];
diff --git a/intern/cycles/kernel/shaders/node_vector_curves.osl b/intern/cycles/kernel/shaders/node_vector_curves.osl
index 4d4c28b..cff4efe 100644
--- a/intern/cycles/kernel/shaders/node_vector_curves.osl
+++ b/intern/cycles/kernel/shaders/node_vector_curves.osl
@@ -17,8 +17,10 @@
 #include "stdosl.h"
 #include "oslutil.h"
 
-float ramp_lookup(color ramp[RAMP_TABLE_SIZE], float at, int component)
+float ramp_lookup(color ramp[], float at, int component)
 {
+	int table_size = arraylength(ramp);
+
 	if (at < 0.0 || at > 1.0) {
 		float t0, dy;
 		if (at < 0.0) {
@@ -27,19 +29,19 @@ float ramp_lookup(color ramp[RAMP_TABLE_SIZE], float at, int component)
 			at = -at;
 		}
 		else {
-			t0 = ramp[RAMP_TABLE_SIZE - 1][component];
-			dy = t0 - ramp[RAMP_TABLE_SIZE - 2][component];
+			t0 = ramp[table_size - 1][component];
+			dy = t0 - ramp[table_size - 2][component];
 			at = at - 1.0;
 		}
-		return t0 + dy * at * (RAMP_TABLE_SIZE - 1);
+		return t0 + dy * at * (table_size - 1);
 	}
 
-	float f = clamp(at, 0.0, 1.0) * (RAMP_TABLE_SIZE - 1);
+	float f = clamp(at, 0.0, 1.0) * (table_size - 1);
 
 	/* clamp int as well in case of NaN */
 	int i = (int)f;
 	if (i < 0) i = 0;
-	if (i >= RAMP_TABLE_SIZE) i = RAMP_TABLE_SIZE - 1;
+	if (i >= table_size) i = table_size - 1;
 	float t = f - (float)i;
 
 	float result = ramp[i][component];
@@ -51,7 +53,7 @@ float ramp_lookup(color ramp[RAMP_TABLE_SIZE], float at, int component)
 }
 
 shader node_vector_curves(
-	color ramp[RAMP_TABLE_SIZE] = {0.0},
+	color ramp[] = {0.0},
 	float min_x = 0.0,
 	float max_x = 1.0,
 
diff --git a/intern/cycles/kernel/shaders/oslutil.h b/intern/cycles/kernel/shaders/oslutil.h
index d90900b..141e5d2 100644
--- a/intern/cycles/kernel/shaders/oslutil.h
+++ b/intern/cycles/kernel/shaders/oslutil.h
@@ -33,9 +33,6 @@
 #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.
diff --git a/intern/cycles/kernel/svm/svm_ramp.h b/intern/cycles/kernel/svm/svm_ramp.h
index 7e4106e..59dec40 100644
--- a/intern/cycles/kernel/svm/svm_ramp.h
+++ b/intern/cycles/kernel/svm/svm_ramp.h
@@ -23,7 +23,8 @@ ccl_device float4 rgb_ramp_lookup(KernelGlobals *kg,
                                   int offset,
                                   float f,
                                   bool interpolate,
-                                  bool extrapolate)
+                                  bool extrapolate,
+                                  int table_size)
 {
 	if((f < 0.0f || f > 1.0f) && extrapolate) {
 		float4 t0, dy;
@@ -33,17 +34,17 @@ ccl_device float4 rgb_ramp_lookup(KernelGlobals *kg,
 			f = -f;
 		}
 		else {
-			t0 = fetch_node_float(kg, offset + RAMP_TABLE_SIZE - 1);
-			dy = t0 - fetch_node_float(kg, offset + RAMP_TABLE_SIZE - 2);
+			t0 = fetch_node_float(kg, offset + table_size - 1);
+			dy = t0 - fetch_node_float(kg, offset + table_size - 2);
 			f = f - 1.0f;
 		}
-		return t0 + dy * f * (RAMP_TABLE_SIZE-1);
+		return t0 + dy * f * (table_size-1);
 	}
 
-	f = saturate(f)*(RAMP_TABLE_SIZE-1);
+	f = saturate(f)*(table_size-1);
 
 	/* clamp int as well in case of NaN */
-	int i = clamp(float_to_int(f), 0, RAMP_TABLE_SIZE-1);
+	int i = clamp(float_to_int(f), 0, table_size-1);
 	float t = f - (float)i;
 
 	float4 a = fetch_node_float(kg, offset+i);
@@ -61,15 +62,17 @@ ccl_device void svm_node_rgb_ramp(KernelGlobals *kg, ShaderData *sd, float *stac
 
 	decode_node_uchar4(node.y, &fac_offset, &color_offset, &alpha_offset, NULL);
 
+	uint table_size = read_node(kg, offset).x;
+
 	float fac = stack_load_float(stack, fac_offset);
-	float4 color = rgb_ramp_lookup(kg, *offset, fac, interpolate, false);
+	float4 color = rgb_ramp_lookup(kg, *offset, fac, interpolate, false, table_size);
 
 	if(stack_valid(color_offset))
 		stack_store_float3(stack, color_offset, float4_to_float3(color));
 	if(stack_valid(alpha_offset))
 		stack_store_float(stack, alpha_offset, color.w);
 
-	*offset += RAMP_TABLE_SIZE;
+	*offset += table_size;
 }
 
 ccl_device void svm_node_rgb_curves(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
@@ -81,6 +84,8 @@ ccl_device void svm_node_rgb_curves(KernelGlobals *kg, ShaderData *sd, float *st
 	                   &out_offset,
 	                   NULL);
 
+	uint table_size = read_node(kg, offset).x;
+
 	float fac = stack_load_float(stack, fac_offset);
 	float3 color = stack_load_float3(stack, color_offset);
 
@@ -89,14 +94,14 @@ ccl_device void svm_node_rgb_curves(KernelGlobals *kg, ShaderData *sd, float *st
 	const float range_x = max_x - min_x;
 	color = (color - make_float3(min_x, min_x, min_x)) / range_x;
 
-	float r = rgb_ramp_lookup(kg, *offset, color.x, true, true).x;
-	float g = rgb_ramp_lookup(kg, *offset, color.y, true, true).y;
-	float b = rgb_ramp_lookup(kg, *offset, color.z, true, true).z;
+	float r = rgb_ramp_lookup(kg, *offset, color.x, true, true, table_size).x;
+	float g = rgb_ramp_lookup(kg, *offset, color.y, true, true, table_size).y;
+	float b = rgb_ramp_lookup(kg, *offset, color.z, true, true, table_size).z;
 
 	color = (1.0f - fac)*color + fac*make_float3(r, g, b);
 	stack_store_float3(stack, out_offset, color);
 
-	*offset += RAMP_TABLE_SIZE;
+	*offset

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list