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

This commit is contained in:
Brecht Van Lommel 2016-05-08 01:54:35 +02:00
parent 93e4ae84ad
commit 08670d3b81
13 changed files with 106 additions and 166 deletions

View File

@ -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;
}

View File

@ -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];

View File

@ -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,

View File

@ -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];

View File

@ -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,

View File

@ -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.

View File

@ -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 += table_size;
}
ccl_device void svm_node_vector_curves(KernelGlobals *kg, ShaderData *sd, float *stack, uint4 node, int *offset)
@ -108,6 +113,8 @@ ccl_device void svm_node_vector_curves(KernelGlobals *kg, ShaderData *sd, float
&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);
@ -116,14 +123,14 @@ ccl_device void svm_node_vector_curves(KernelGlobals *kg, ShaderData *sd, float
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 += table_size;
}
CCL_NAMESPACE_END

View File

@ -4322,6 +4322,9 @@ RGBCurvesNode::RGBCurvesNode()
void RGBCurvesNode::compile(SVMCompiler& compiler)
{
if (curves.size() == 0)
return;
ShaderInput *fac_in = input("Fac");
ShaderInput *color_in = input("Color");
ShaderOutput *color_out = output("Color");
@ -4332,20 +4335,18 @@ void RGBCurvesNode::compile(SVMCompiler& compiler)
compiler.stack_assign(color_out)),
__float_as_int(min_x),
__float_as_int(max_x));
compiler.add_array(curves, RAMP_TABLE_SIZE);
compiler.add_node(curves.size());
for(int i = 0; i < curves.size(); i++)
compiler.add_node(float3_to_float4(curves[i]));
}
void RGBCurvesNode::compile(OSLCompiler& compiler)
{
float ramp[RAMP_TABLE_SIZE][3];
if (curves.size() == 0)
return;
for(int i = 0; i < RAMP_TABLE_SIZE; ++i) {
ramp[i][0] = curves[i].x;
ramp[i][1] = curves[i].y;
ramp[i][2] = curves[i].z;
}
compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
compiler.parameter_color_array("ramp", curves);
compiler.parameter("min_x", min_x);
compiler.parameter("max_x", max_x);
compiler.add(this, "node_rgb_curves");
@ -4366,6 +4367,9 @@ VectorCurvesNode::VectorCurvesNode()
void VectorCurvesNode::compile(SVMCompiler& compiler)
{
if (curves.size() == 0)
return;
ShaderInput *fac_in = input("Fac");
ShaderInput *vector_in = input("Vector");
ShaderOutput *vector_out = output("Vector");
@ -4376,20 +4380,18 @@ void VectorCurvesNode::compile(SVMCompiler& compiler)
compiler.stack_assign(vector_out)),
__float_as_int(min_x),
__float_as_int(max_x));
compiler.add_array(curves, RAMP_TABLE_SIZE);
compiler.add_node(curves.size());
for(int i = 0; i < curves.size(); i++)
compiler.add_node(float3_to_float4(curves[i]));
}
void VectorCurvesNode::compile(OSLCompiler& compiler)
{
float ramp[RAMP_TABLE_SIZE][3];
if (curves.size() == 0)
return;
for(int i = 0; i < RAMP_TABLE_SIZE; ++i) {
ramp[i][0] = curves[i].x;
ramp[i][1] = curves[i].y;
ramp[i][2] = curves[i].z;
}
compiler.parameter_color_array("ramp", ramp, RAMP_TABLE_SIZE);
compiler.parameter_color_array("ramp", curves);
compiler.parameter("min_x", min_x);
compiler.parameter("max_x", max_x);
compiler.add(this, "node_vector_curves");
@ -4409,6 +4411,9 @@ RGBRampNode::RGBRampNode()
void RGBRampNode::compile(SVMCompiler& compiler)
{
if (ramp.size() == 0 || ramp.size() != ramp_alpha.size())
return;
ShaderInput *fac_in = input("Fac");
ShaderOutput *color_out = output("Color");
ShaderOutput *alpha_out = output("Alpha");
@ -4419,25 +4424,19 @@ void RGBRampNode::compile(SVMCompiler& compiler)
compiler.stack_assign_if_linked(color_out),
compiler.stack_assign_if_linked(alpha_out)),
interpolate);
compiler.add_array(ramp, RAMP_TABLE_SIZE);
compiler.add_node(ramp.size());
for(int i = 0; i < ramp.size(); i++)
compiler.add_node(make_float4(ramp[i].x, ramp[i].y, ramp[i].z, ramp_alpha[i]));
}
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];
if (ramp.size() == 0 || ramp.size() != ramp_alpha.size())
return;
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.parameter_color_array("ramp_color", ramp);
compiler.parameter_array("ramp_alpha", ramp_alpha.data(), ramp_alpha.size());
compiler.parameter("ramp_interpolate", interpolate);
compiler.add(this, "node_rgb_ramp");

View File

@ -917,7 +917,7 @@ public:
virtual int get_group() { return NODE_GROUP_LEVEL_3; }
virtual bool equals(const ShaderNode * /*other*/) { return false; }
float4 curves[RAMP_TABLE_SIZE];
array<float3> curves;
float min_x, max_x;
};
@ -928,14 +928,15 @@ public:
virtual int get_group() { return NODE_GROUP_LEVEL_3; }
virtual bool equals(const ShaderNode * /*other*/) { return false; }
float4 curves[RAMP_TABLE_SIZE];
array<float3> curves;
float min_x, max_x;
};
class RGBRampNode : public ShaderNode {
public:
SHADER_NODE_CLASS(RGBRampNode)
float4 ramp[RAMP_TABLE_SIZE];
array<float3> ramp;
array<float> ramp_alpha;
bool interpolate;
virtual int get_group() { return NODE_GROUP_LEVEL_1; }
virtual bool equals(const ShaderNode * /*other*/) { return false; }

View File

@ -668,60 +668,21 @@ void OSLCompiler::parameter_array(const char *name, const float f[], int arrayle
ss->Parameter(name, type, f);
}
void OSLCompiler::parameter_color_array(const char *name, const float f[][3], int arraylen)
void OSLCompiler::parameter_color_array(const char *name, const array<float3>& f)
{
/* NB: cycles float3 type is actually 4 floats! need to use an explicit array */
array<float[3]> table(f.size());
for(int i = 0; i < f.size(); ++i) {
table[i][0] = f[i].x;
table[i][1] = f[i].y;
table[i][2] = f[i].z;
}
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);
type.arraylen = table.size();
ss->Parameter(name, type, table.data());
}
void OSLCompiler::find_dependencies(ShaderNodeSet& dependencies, ShaderInput *input)
@ -947,31 +908,7 @@ void OSLCompiler::parameter_array(const char * /*name*/, const float /*f*/[], in
{
}
void OSLCompiler::parameter_color_array(const char * /*name*/, const float /*f*/[][3], int /*arraylen*/)
{
}
void OSLCompiler::parameter_vector_array(const char * /*name*/, const float /*f*/[][3], int /*arraylen*/)
{
}
void OSLCompiler::parameter_normal_array(const char * /*name*/, const float /*f*/[][3], int /*arraylen*/)
{
}
void OSLCompiler::parameter_point_array(const char * /*name*/, const float /*f*/[][3], int /*arraylen*/)
{
}
void OSLCompiler::parameter_array(const char * /*name*/, const int /*f*/[], int /*arraylen*/)
{
}
void OSLCompiler::parameter_array(const char * /*name*/, const char * const /*s*/[], int /*arraylen*/)
{
}
void OSLCompiler::parameter_array(const char * /*name*/, const Transform /*tfm*/[], int /*arraylen*/)
void OSLCompiler::parameter_color_array(const char *name, const array<float3>& f)
{
}

View File

@ -129,13 +129,7 @@ public:
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);
void parameter_color_array(const char *name, const array<float3>& f);
ShaderType output_type() { return current_type; }

View File

@ -338,12 +338,6 @@ void SVMCompiler::add_node(const float4& f)
__float_as_int(f.w)));
}
void SVMCompiler::add_array(float4 *f, int num)
{
for(int i = 0; i < num; i++)
add_node(f[i]);
}
uint SVMCompiler::attribute(ustring name)
{
return shader_manager->get_attribute_id(name);

View File

@ -107,7 +107,6 @@ public:
void add_node(int a = 0, int b = 0, int c = 0, int d = 0);
void add_node(NodeType type, const float3& f);
void add_node(const float4& f);
void add_array(float4 *f, int num);
uint attribute(ustring name);
uint attribute(AttributeStandard std);
uint encode_uchar4(uint x, uint y = 0, uint z = 0, uint w = 0);