Cycles OSL: refactoring to remove all dependencies on builtin OSL closures.

These were removed in new OSL versions. We only used these as base classes,
not using them at all simplifies the code a bit.
This commit is contained in:
Brecht Van Lommel 2013-11-29 02:09:24 +01:00
parent aa3933b411
commit 746628e0d0
12 changed files with 80 additions and 202 deletions

View File

@ -53,11 +53,6 @@ ccl_device float3 volume_transparent_eval_phase(const ShaderClosure *sc, const f
ccl_device float3 volume_eval_phase(KernelGlobals *kg, const ShaderClosure *sc, const float3 omega_in, const float3 omega_out)
{
#ifdef __OSL__
if(kg->osl && sc->prim)
return OSLShader::volume_eval_phase(sc, omega_in, omega_out);
#endif
float3 eval;
switch(sc->type) {

View File

@ -764,11 +764,6 @@ ccl_device float3 shader_bssrdf_sum(ShaderData *sd, float3 *N_, float *texture_b
ccl_device float3 emissive_eval(KernelGlobals *kg, ShaderData *sd, ShaderClosure *sc)
{
#ifdef __OSL__
if(kg->osl && sc->prim)
return OSLShader::emissive_eval(sd, sc);
#endif
return emissive_simple_eval(sd->Ng, sd->I);
}

View File

@ -46,14 +46,9 @@ using namespace OSL;
/// to return a color in background shaders. No methods,
/// only the weight is taking into account
///
class GenericBackgroundClosure : public OSL::BackgroundClosure {
class GenericBackgroundClosure : public CClosurePrimitive {
public:
GenericBackgroundClosure() {}
void setup() {};
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "background"; }
void print_on(std::ostream &out) const { out << name() << " ()"; }
GenericBackgroundClosure() : CClosurePrimitive(Background) {}
};
/// Holdout closure
@ -63,14 +58,9 @@ public:
/// point. No parameters, only the weight will be
/// used
///
class HoldoutClosure : ClosurePrimitive {
class HoldoutClosure : CClosurePrimitive {
public:
HoldoutClosure () : ClosurePrimitive(Holdout) {}
void setup() {};
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "holdout"; }
void print_on(std::ostream &out) const { out << name() << " ()"; }
HoldoutClosure () : CClosurePrimitive(Holdout) {}
};
/// ambient occlusion closure
@ -79,14 +69,9 @@ public:
/// to return a color in ambient occlusion shaders. No methods,
/// only the weight is taking into account
///
class AmbientOcclusionClosure : public ClosurePrimitive {
class AmbientOcclusionClosure : public CClosurePrimitive {
public:
AmbientOcclusionClosure () : ClosurePrimitive((ClosurePrimitive::Category)AmbientOcclusion) {}
void setup() {};
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "ambient_occlusion"; }
void print_on(std::ostream &out) const { out << name() << " ()"; }
AmbientOcclusionClosure () : CClosurePrimitive(AmbientOcclusion) {}
};
ClosureParam *closure_background_params()
@ -98,7 +83,7 @@ ClosureParam *closure_background_params()
return params;
}
CLOSURE_PREPARE(closure_background_prepare, GenericBackgroundClosure)
CCLOSURE_PREPARE(closure_background_prepare, GenericBackgroundClosure)
ClosureParam *closure_holdout_params()
{
@ -108,7 +93,7 @@ ClosureParam *closure_holdout_params()
return params;
}
CLOSURE_PREPARE(closure_holdout_prepare, HoldoutClosure)
CCLOSURE_PREPARE(closure_holdout_prepare, HoldoutClosure)
ClosureParam *closure_ambient_occlusion_params()
{
@ -119,7 +104,7 @@ ClosureParam *closure_ambient_occlusion_params()
return params;
}
CLOSURE_PREPARE(closure_ambient_occlusion_prepare, AmbientOcclusionClosure)
CCLOSURE_PREPARE(closure_ambient_occlusion_prepare, AmbientOcclusionClosure)
CCL_NAMESPACE_END

View File

@ -46,14 +46,10 @@ using namespace OSL;
class DiffuseRampClosure : public CBSDFClosure {
public:
DiffuseRampClosure() : CBSDFClosure(LABEL_DIFFUSE) {}
Color3 colors[8];
float3 fcolors[8];
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "diffuse_ramp"; }
void setup()
DiffuseRampClosure() : CBSDFClosure(LABEL_DIFFUSE)
{
sc.prim = this;
m_shaderdata_flag = bsdf_diffuse_ramp_setup(&sc);
@ -62,21 +58,11 @@ public:
fcolors[i] = TO_FLOAT3(colors[i]);
}
bool mergeable(const ClosurePrimitive *other) const
{
return false;
}
void blur(float roughness)
{
bsdf_diffuse_ramp_blur(&sc, roughness);
}
void print_on(std::ostream &out) const
{
out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))";
}
float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const
{
return bsdf_diffuse_ramp_eval_reflect(&sc, fcolors, omega_out, omega_in, &pdf);
@ -109,7 +95,7 @@ ClosureParam *closure_bsdf_diffuse_ramp_params()
return params;
}
CLOSURE_PREPARE(closure_bsdf_diffuse_ramp_prepare, DiffuseRampClosure)
CCLOSURE_PREPARE(closure_bsdf_diffuse_ramp_prepare, DiffuseRampClosure)
CCL_NAMESPACE_END

View File

@ -45,14 +45,10 @@ using namespace OSL;
class PhongRampClosure : public CBSDFClosure {
public:
PhongRampClosure() : CBSDFClosure(LABEL_GLOSSY) {}
Color3 colors[8];
float3 fcolors[8];
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "phong_ramp"; }
void setup()
PhongRampClosure() : CBSDFClosure(LABEL_GLOSSY)
{
sc.prim = this;
m_shaderdata_flag = bsdf_phong_ramp_setup(&sc);
@ -61,21 +57,11 @@ public:
fcolors[i] = TO_FLOAT3(colors[i]);
}
bool mergeable(const ClosurePrimitive *other) const
{
return false;
}
void blur(float roughness)
{
bsdf_phong_ramp_blur(&sc, roughness);
}
void print_on(std::ostream &out) const
{
out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))";
}
float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const
{
return bsdf_phong_ramp_eval_reflect(&sc, fcolors, omega_out, omega_in, &pdf);
@ -109,7 +95,7 @@ ClosureParam *closure_bsdf_phong_ramp_params()
return params;
}
CLOSURE_PREPARE(closure_bsdf_phong_ramp_prepare, PhongRampClosure)
CCLOSURE_PREPARE(closure_bsdf_phong_ramp_prepare, PhongRampClosure)
CCL_NAMESPACE_END

View File

@ -50,18 +50,9 @@ using namespace OSL;
/// outer_angle limit. It can also behave as a lambertian emitter
/// if the provided angles are PI/2, which is the default
///
class GenericEmissiveClosure : public EmissiveClosure {
class GenericEmissiveClosure : public CClosurePrimitive {
public:
GenericEmissiveClosure() { }
void setup() {}
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "emission"; }
void print_on(std::ostream &out) const
{
out << name() << "()";
}
GenericEmissiveClosure() : CClosurePrimitive(Emissive) { }
Color3 eval(const Vec3 &Ng, const Vec3 &omega_out) const
{
@ -92,7 +83,7 @@ ClosureParam *closure_emission_params()
return params;
}
CLOSURE_PREPARE(closure_emission_prepare, GenericEmissiveClosure)
CCLOSURE_PREPARE(closure_emission_prepare, GenericEmissiveClosure)
CCL_NAMESPACE_END

View File

@ -51,24 +51,11 @@ using namespace OSL;
class CubicBSSRDFClosure : public CBSSRDFClosure {
public:
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "bssrdf_cubic"; }
void setup()
CubicBSSRDFClosure()
{
sc.type = CLOSURE_BSSRDF_CUBIC_ID;
sc.data0 = fabsf(average(radius));
}
bool mergeable(const ClosurePrimitive *other) const
{
return false;
}
void print_on(std::ostream &out) const
{
out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))";
}
};
ClosureParam *closure_bssrdf_cubic_params()
@ -96,30 +83,17 @@ ClosureParam *closure_bssrdf_cubic_extended_params()
return params;
}
CLOSURE_PREPARE(closure_bssrdf_cubic_prepare, CubicBSSRDFClosure)
CCLOSURE_PREPARE(closure_bssrdf_cubic_prepare, CubicBSSRDFClosure)
/* Gaussian */
class GaussianBSSRDFClosure : public CBSSRDFClosure {
public:
size_t memsize() const { return sizeof(*this); }
const char *name() const { return "bssrdf_gaussian"; }
void setup()
GaussianBSSRDFClosure()
{
sc.type = CLOSURE_BSSRDF_GAUSSIAN_ID;
sc.data0 = fabsf(average(radius));
}
bool mergeable(const ClosurePrimitive *other) const
{
return false;
}
void print_on(std::ostream &out) const
{
out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))";
}
};
ClosureParam *closure_bssrdf_gaussian_params()
@ -146,7 +120,7 @@ ClosureParam *closure_bssrdf_gaussian_extended_params()
return params;
}
CLOSURE_PREPARE(closure_bssrdf_gaussian_prepare, GaussianBSSRDFClosure)
CCLOSURE_PREPARE(closure_bssrdf_gaussian_prepare, GaussianBSSRDFClosure)
CCL_NAMESPACE_END

View File

@ -37,20 +37,20 @@
#include <OSL/oslexec.h>
#include <OSL/genclosure.h>
#include "osl_closures.h"
#include "kernel_types.h"
#include "util_types.h"
CCL_NAMESPACE_BEGIN
class CBSSRDFClosure : public OSL::ClosurePrimitive {
class CBSSRDFClosure : public CClosurePrimitive {
public:
ShaderClosure sc;
float3 radius;
CBSSRDFClosure() : OSL::ClosurePrimitive(BSSRDF) { memset(&sc, 0, sizeof(sc)); }
~CBSSRDFClosure() { }
CBSSRDFClosure() : CClosurePrimitive(BSSRDF) { }
int scattering() const { return LABEL_DIFFUSE; }
};

View File

@ -171,25 +171,16 @@ BSDF_CLOSURE_CLASS_END(HairTransmission, hair_transmission)
/* Registration */
static void generic_closure_setup(OSL::RendererServices *, int id, void *data)
{
assert(data);
OSL::ClosurePrimitive *prim = (OSL::ClosurePrimitive *)data;
prim->setup();
}
static bool generic_closure_compare(int id, const void *dataA, const void *dataB)
{
assert(dataA && dataB);
OSL::ClosurePrimitive *primA = (OSL::ClosurePrimitive *)dataA;
OSL::ClosurePrimitive *primB = (OSL::ClosurePrimitive *)dataB;
return primA->mergeable(primB);
}
static void register_closure(OSL::ShadingSystem *ss, const char *name, int id, OSL::ClosureParam *params, OSL::PrepareClosureFunc prepare)
{
ss->register_closure(name, id, params, prepare, generic_closure_setup, generic_closure_compare);
/* optimization: it's possible to not use a prepare function at all and
* only initialize the actual class when accessing the closure component
* data, but then we need to map the id to the class somehow */
#ifdef CLOSURE_PREPARE
ss->register_closure(name, id, params, prepare, NULL, NULL);
#else
ss->register_closure(name, id, params, prepare, NULL);
#endif
}
void OSLShader::register_closures(OSLShadingSystem *ss_)

View File

@ -66,36 +66,51 @@ void closure_westin_sheen_prepare(OSL::RendererServices *, int id, void *data);
void closure_bssrdf_cubic_prepare(OSL::RendererServices *, int id, void *data);
void closure_bssrdf_gaussian_prepare(OSL::RendererServices *, int id, void *data);
enum {
AmbientOcclusion = 100
};
#define CLOSURE_PREPARE(name, classname) \
#define CCLOSURE_PREPARE(name, classname) \
void name(RendererServices *, int id, void *data) \
{ \
memset(data, 0, sizeof(classname)); \
new (data) classname(); \
}
#define CLOSURE_PREPARE_STATIC(name, classname) static CLOSURE_PREPARE(name, classname)
#define CCLOSURE_PREPARE_STATIC(name, classname) static CCLOSURE_PREPARE(name, classname)
#define CLOSURE_FLOAT3_PARAM(st, fld) \
{ TypeDesc::TypeVector, reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) }
{ TypeDesc::TypeVector, reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) }
#define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
#define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
#define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
/* Closure */
class CClosurePrimitive {
public:
enum Category {
BSDF, ///< Reflective and/or transmissive surface
BSSRDF, ///< Sub-surface light transfer
Emissive, ///< Light emission
Background, ///< Background emission
Volume, ///< Volume scattering
Holdout, ///< Holdout from alpha
AmbientOcclusion, ///< Ambient occlusion
};
CClosurePrimitive (Category category_) : category (category_) { }
virtual ~CClosurePrimitive() {}
Category category;
};
/* BSDF */
class CBSDFClosure : public OSL::ClosurePrimitive {
class CBSDFClosure : public CClosurePrimitive {
public:
ShaderClosure sc;
CBSDFClosure(int scattering) : OSL::ClosurePrimitive(BSDF),
CBSDFClosure(int scattering) : CClosurePrimitive(BSDF),
m_scattering_label(scattering), m_shaderdata_flag(0)
{ memset(&sc, 0, sizeof(sc)); }
~CBSDFClosure() { }
{ }
int scattering() const { return m_scattering_label; }
int shaderdata_flag() const { return m_shaderdata_flag; }
@ -119,30 +134,16 @@ protected:
\
class Upper##Closure : public CBSDFClosure { \
public: \
Upper##Closure() : CBSDFClosure(TYPE) {} \
size_t memsize() const { return sizeof(*this); } \
const char *name() const { return #lower; } \
\
void setup() \
Upper##Closure() : CBSDFClosure(TYPE) \
{ \
sc.prim = NULL; \
m_shaderdata_flag = bsdf_##lower##_setup(&sc); \
} \
\
bool mergeable(const ClosurePrimitive *other) const \
{ \
return false; \
} \
\
void blur(float roughness) \
{ \
bsdf_##svmlower##_blur(&sc, roughness); \
} \
\
void print_on(std::ostream &out) const \
{ \
out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))"; \
} \
\
float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
{ \
@ -178,7 +179,7 @@ static ClosureParam *bsdf_##lower##_params() \
return params; \
} \
\
CLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
CCL_NAMESPACE_END

View File

@ -146,7 +146,7 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
if (closure->type == OSL::ClosureColor::COMPONENT) {
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
OSL::ClosurePrimitive *prim = (OSL::ClosurePrimitive *)comp->data();
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
if (prim) {
ShaderClosure sc;
@ -156,8 +156,8 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
sc.weight = weight;
#endif
switch (prim->category()) {
case OSL::ClosurePrimitive::BSDF: {
switch (prim->category) {
case CClosurePrimitive::BSDF: {
CBSDFClosure *bsdf = (CBSDFClosure *)prim;
int scattering = bsdf->scattering();
@ -191,7 +191,7 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
}
break;
}
case OSL::ClosurePrimitive::Emissive: {
case CClosurePrimitive::Emissive: {
/* sample weight */
float sample_weight = fabsf(average(weight));
@ -208,7 +208,7 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
}
break;
}
case AmbientOcclusion: {
case CClosurePrimitive::AmbientOcclusion: {
/* sample weight */
float sample_weight = fabsf(average(weight));
@ -224,7 +224,7 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
}
break;
}
case OSL::ClosurePrimitive::Holdout: {
case CClosurePrimitive::Holdout: {
sc.sample_weight = 0.0f;
sc.type = CLOSURE_HOLDOUT_ID;
sc.data0 = 0.0f;
@ -237,7 +237,7 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
}
break;
}
case OSL::ClosurePrimitive::BSSRDF: {
case CClosurePrimitive::BSSRDF: {
CBSSRDFClosure *bssrdf = (CBSSRDFClosure *)prim;
float sample_weight = fabsf(average(weight));
@ -280,10 +280,8 @@ static void flatten_surface_closure_tree(ShaderData *sd, int path_flag,
}
break;
}
case OSL::ClosurePrimitive::Debug:
break; /* not implemented */
case OSL::ClosurePrimitive::Background:
case OSL::ClosurePrimitive::Volume:
case CClosurePrimitive::Background:
case CClosurePrimitive::Volume:
break; /* not relevant */
}
}
@ -332,9 +330,9 @@ static float3 flatten_background_closure_tree(const OSL::ClosureColor *closure)
if (closure->type == OSL::ClosureColor::COMPONENT) {
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
OSL::ClosurePrimitive *prim = (OSL::ClosurePrimitive *)comp->data();
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
if (prim && prim->category() == OSL::ClosurePrimitive::Background)
if (prim && prim->category == CClosurePrimitive::Background)
#ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS
return TO_FLOAT3(comp->w);
#else
@ -387,7 +385,7 @@ static void flatten_volume_closure_tree(ShaderData *sd,
if (closure->type == OSL::ClosureColor::COMPONENT) {
OSL::ClosureComponent *comp = (OSL::ClosureComponent *)closure;
OSL::ClosurePrimitive *prim = (OSL::ClosurePrimitive *)comp->data();
CClosurePrimitive *prim = (CClosurePrimitive *)comp->data();
if (prim) {
ShaderClosure sc;
@ -397,8 +395,8 @@ static void flatten_volume_closure_tree(ShaderData *sd,
sc.weight = weight;
#endif
switch (prim->category()) {
case OSL::ClosurePrimitive::Volume: {
switch (prim->category) {
case CClosurePrimitive::Volume: {
/* sample weight */
float sample_weight = fabsf(average(weight));
@ -413,13 +411,13 @@ static void flatten_volume_closure_tree(ShaderData *sd,
sd->closure[sd->num_closure++] = sc;
break;
}
case OSL::ClosurePrimitive::Holdout:
case OSL::ClosurePrimitive::Debug:
case CClosurePrimitive::Holdout:
break; /* not implemented */
case OSL::ClosurePrimitive::Background:
case OSL::ClosurePrimitive::BSDF:
case OSL::ClosurePrimitive::Emissive:
case OSL::ClosurePrimitive::BSSRDF:
case CClosurePrimitive::Background:
case CClosurePrimitive::BSDF:
case CClosurePrimitive::Emissive:
case CClosurePrimitive::BSSRDF:
case CClosurePrimitive::AmbientOcclusion:
break; /* not relevant */
}
}
@ -509,25 +507,6 @@ void OSLShader::bsdf_blur(ShaderClosure *sc, float roughness)
bsdf->blur(roughness);
}
/* Emissive Closure */
float3 OSLShader::emissive_eval(const ShaderData *sd, const ShaderClosure *sc)
{
OSL::EmissiveClosure *emissive = (OSL::EmissiveClosure *)sc->prim;
OSL::Color3 emissive_eval = emissive->eval(TO_VEC3(sd->Ng), TO_VEC3(sd->I));
return TO_FLOAT3(emissive_eval);
}
/* Volume Closure */
float3 OSLShader::volume_eval_phase(const ShaderClosure *sc, const float3 omega_in, const float3 omega_out)
{
OSL::VolumeClosure *volume = (OSL::VolumeClosure *)sc->prim;
OSL::Color3 volume_eval = volume->eval_phase(TO_VEC3(omega_in), TO_VEC3(omega_out));
return TO_FLOAT3(volume_eval) * sc->weight;
}
/* Attributes */
int OSLShader::find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id, AttributeElement *elem)

View File

@ -66,11 +66,6 @@ public:
const float3& omega_in, float& pdf);
static void bsdf_blur(ShaderClosure *sc, float roughness);
static float3 emissive_eval(const ShaderData *sd, const ShaderClosure *sc);
static float3 volume_eval_phase(const ShaderClosure *sc,
const float3 omega_in, const float3 omega_out);
/* attributes */
static int find_attribute(KernelGlobals *kg, const ShaderData *sd, uint id, AttributeElement *elem);
};