From 21964e65380131324dcb50567e913cf6e4a29102 Mon Sep 17 00:00:00 2001 From: Lukas Toenne Date: Sat, 15 Sep 2012 15:41:37 +0000 Subject: 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. --- intern/cycles/kernel/osl/nodes/CMakeLists.txt | 1 + intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl | 44 ++++++++++++++++ intern/cycles/kernel/osl/nodes/oslutil.h | 3 ++ intern/cycles/render/nodes.cpp | 13 +++++ intern/cycles/render/osl.cpp | 64 ++++++++++++++++++++++++ intern/cycles/render/osl.h | 9 ++++ 6 files changed, 134 insertions(+) create mode 100644 intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl diff --git a/intern/cycles/kernel/osl/nodes/CMakeLists.txt b/intern/cycles/kernel/osl/nodes/CMakeLists.txt index 729d4dae1c9..541076f84c9 100644 --- a/intern/cycles/kernel/osl/nodes/CMakeLists.txt +++ b/intern/cycles/kernel/osl/nodes/CMakeLists.txt @@ -43,6 +43,7 @@ set(SRC_OSL 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 diff --git a/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl b/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl new file mode 100644 index 00000000000..2bc10f31cb3 --- /dev/null +++ b/intern/cycles/kernel/osl/nodes/node_rgb_ramp.osl @@ -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]; + } +} + diff --git a/intern/cycles/kernel/osl/nodes/oslutil.h b/intern/cycles/kernel/osl/nodes/oslutil.h index 08611187a3c..6c91684d9a6 100644 --- a/intern/cycles/kernel/osl/nodes/oslutil.h +++ b/intern/cycles/kernel/osl/nodes/oslutil.h @@ -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. diff --git a/intern/cycles/render/nodes.cpp b/intern/cycles/render/nodes.cpp index 1f03ab4e347..1acb6795aca 100644 --- a/intern/cycles/render/nodes.cpp +++ b/intern/cycles/render/nodes.cpp @@ -2768,6 +2768,19 @@ void RGBRampNode::compile(SVMCompiler& compiler) 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"); } diff --git a/intern/cycles/render/osl.cpp b/intern/cycles/render/osl.cpp index dbd6b0ac9d9..75a10d38c5d 100644 --- a/intern/cycles/render/osl.cpp +++ b/intern/cycles/render/osl.cpp @@ -309,6 +309,70 @@ void OSLCompiler::parameter(const char *name, const Transform& tfm) 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& dependencies, ShaderInput *input) { ShaderNode *node = (input->link)? input->link->parent: NULL; diff --git a/intern/cycles/render/osl.h b/intern/cycles/render/osl.h index 329cf9a4828..2da951ab3d2 100644 --- a/intern/cycles/render/osl.h +++ b/intern/cycles/render/osl.h @@ -79,6 +79,15 @@ public: 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; -- cgit v1.2.3