diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-11-06 23:59:10 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2012-11-06 23:59:10 +0400 |
commit | 3e9f2938f07feb7808186834f5975f7c2c211b15 (patch) | |
tree | d8aa84852f05b64a1c76e09f29f8b6916f6beb26 /intern/cycles/kernel/osl | |
parent | 209cd2574588d79edc8747bc94e329f6c81a3f99 (diff) |
Cycles OSL: support for the trace(point pos, vector dir, ...) function, to trace
rays from the OSL shader. The "shade" parameter is not supported currently, but
attributes can be retrieved from the object that was hit using the
getmessage("trace", ..) function.
As mentioned in the OSL specification, this function can't be used instead of
lighting, the main purpose is to allow shaders to "probe" nearby geometry, for
example to apply a projected texture that can be blocked by geometry, apply
more “wear” to exposed geometry, or make other ambient occlusion-like effects.
http://wiki.blender.org/index.php/Doc:2.6/Manual/Render/Cycles/Nodes/OSL#Trace
Example .blend and render:
http://www.pasteall.org/blend/17347
http://www.pasteall.org/pic/show.php?id=40066
Diffstat (limited to 'intern/cycles/kernel/osl')
-rw-r--r-- | intern/cycles/kernel/osl/osl_globals.h | 1 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_services.cpp | 288 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_services.h | 19 | ||||
-rw-r--r-- | intern/cycles/kernel/osl/osl_shader.cpp | 17 |
4 files changed, 238 insertions, 87 deletions
diff --git a/intern/cycles/kernel/osl/osl_globals.h b/intern/cycles/kernel/osl/osl_globals.h index 8cbbfc8dbb1..5d557ffcb07 100644 --- a/intern/cycles/kernel/osl/osl_globals.h +++ b/intern/cycles/kernel/osl/osl_globals.h @@ -59,6 +59,7 @@ struct OSLGlobals { vector<AttributeMap> attribute_map; ObjectNameMap object_name_map; + vector<ustring> object_names; /* thread key for thread specific data lookup */ struct ThreadData { diff --git a/intern/cycles/kernel/osl/osl_services.cpp b/intern/cycles/kernel/osl/osl_services.cpp index ac26fc1c18d..54cbf49704f 100644 --- a/intern/cycles/kernel/osl/osl_services.cpp +++ b/intern/cycles/kernel/osl/osl_services.cpp @@ -22,6 +22,7 @@ #include "object.h" #include "scene.h" +#include "osl_closures.h" #include "osl_services.h" #include "osl_shader.h" @@ -30,8 +31,14 @@ #include "kernel_compat_cpu.h" #include "kernel_globals.h" +#include "kernel_montecarlo.h" +#include "kernel_projection.h" +#include "kernel_differential.h" #include "kernel_object.h" +#include "kernel_bvh.h" #include "kernel_triangle.h" +#include "kernel_accumulate.h" +#include "kernel_shader.h" CCL_NAMESPACE_BEGIN @@ -282,7 +289,7 @@ bool OSLRenderServices::get_array_attribute(void *renderstate, bool derivatives, return false; } -static void set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val) +static bool set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, void *val) { if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector || type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor) @@ -302,8 +309,10 @@ static void set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, v fval[7] = f[2].y; fval[8] = f[2].z; } + + return true; } - else { + else if(type == TypeDesc::TypeFloat) { float *fval = (float *)val; fval[0] = average(f[0]); @@ -311,10 +320,25 @@ static void set_attribute_float3(float3 f[3], TypeDesc type, bool derivatives, v fval[1] = average(f[1]); fval[2] = average(f[2]); } + + return true; } + + return false; +} + +static bool set_attribute_float3(float3 f, TypeDesc type, bool derivatives, void *val) +{ + float3 fv[3]; + + fv[0] = f; + fv[1] = make_float3(0.0f, 0.0f, 0.0f); + fv[2] = make_float3(0.0f, 0.0f, 0.0f); + + return set_attribute_float3(fv, type, derivatives, val); } -static void set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val) +static bool set_attribute_float(float f[3], TypeDesc type, bool derivatives, void *val) { if (type == TypeDesc::TypePoint || type == TypeDesc::TypeVector || type == TypeDesc::TypeNormal || type == TypeDesc::TypeColor) @@ -333,8 +357,10 @@ static void set_attribute_float(float f[3], TypeDesc type, bool derivatives, voi fval[7] = f[2]; fval[8] = f[2]; } + + return true; } - else { + else if(type == TypeDesc::TypeFloat) { float *fval = (float *)val; fval[0] = f[0]; @@ -342,7 +368,22 @@ static void set_attribute_float(float f[3], TypeDesc type, bool derivatives, voi fval[1] = f[1]; fval[2] = f[2]; } + + return true; } + + return false; +} + +static bool set_attribute_float(float f, TypeDesc type, bool derivatives, void *val) +{ + float fv[3]; + + fv[0] = f; + fv[1] = 0.0f; + fv[2] = 0.0f; + + return set_attribute_float(fv, type, derivatives, val); } static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val) @@ -362,6 +403,23 @@ static bool set_attribute_int(int i, TypeDesc type, bool derivatives, void *val) return false; } +static bool set_attribute_string(ustring str, TypeDesc type, bool derivatives, void *val) +{ + if(type.basetype == TypeDesc::INT && type.aggregate == TypeDesc::SCALAR && type.arraylen == 0) { + ustring *sval = (ustring *)val; + sval[0] = str; + + if (derivatives) { + sval[1] = OSLRenderServices::u_empty; + sval[2] = OSLRenderServices::u_empty; + } + + return true; + } + + return false; +} + static bool set_attribute_float3_3(float3 P[3], TypeDesc type, bool derivatives, void *val) { if(type.vecsemantics == TypeDesc::POINT && type.arraylen >= 3) { @@ -394,20 +452,18 @@ static bool get_mesh_attribute(KernelGlobals *kg, const ShaderData *sd, const OS const TypeDesc& type, bool derivatives, void *val) { if (attr.type == TypeDesc::TypePoint || attr.type == TypeDesc::TypeVector || - attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor) + attr.type == TypeDesc::TypeNormal || attr.type == TypeDesc::TypeColor) { float3 fval[3]; fval[0] = triangle_attribute_float3(kg, sd, attr.elem, attr.offset, (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL); - set_attribute_float3(fval, type, derivatives, val); - return true; + return set_attribute_float3(fval, type, derivatives, val); } else if (attr.type == TypeDesc::TypeFloat) { float fval[3]; fval[0] = triangle_attribute_float(kg, sd, attr.elem, attr.offset, (derivatives) ? &fval[1] : NULL, (derivatives) ? &fval[2] : NULL); - set_attribute_float(fval, type, derivatives, val); - return true; + return set_attribute_float(fval, type, derivatives, val); } else { return false; @@ -426,118 +482,76 @@ static void get_object_attribute(const OSLGlobals::Attribute& attr, bool derivat static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ustring name, TypeDesc type, bool derivatives, void *val) { - /* todo: turn this into hash table + callback once */ + /* todo: turn this into hash table returning int, which can be used in switch */ /* Object Attributes */ if (name == "object:location") { - float3 fval[3]; - fval[0] = object_location(kg, sd); - fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float3(fval, type, derivatives, val); - return true; + float3 f = object_location(kg, sd); + return set_attribute_float3(f, type, derivatives, val); } else if (name == "object:index") { - float fval[3]; - fval[0] = object_pass_id(kg, sd->object); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = object_pass_id(kg, sd->object); + return set_attribute_float(f, type, derivatives, val); } else if (name == "geom:dupli_generated") { - float3 fval[3]; - fval[0] = object_dupli_generated(kg, sd->object); - fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float3(fval, type, derivatives, val); - return true; + float3 f = object_dupli_generated(kg, sd->object); + return set_attribute_float3(f, type, derivatives, val); } else if (name == "geom:dupli_uv") { - float3 fval[3]; - fval[0] = object_dupli_uv(kg, sd->object); - fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float3(fval, type, derivatives, val); - return true; + float3 f = object_dupli_uv(kg, sd->object); + return set_attribute_float3(f, type, derivatives, val); } else if (name == "material:index") { - float fval[3]; - fval[0] = shader_pass_id(kg, sd); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = shader_pass_id(kg, sd); + return set_attribute_float(f, type, derivatives, val); } else if (name == "object:random") { - float fval[3]; - fval[0] = object_random_number(kg, sd->object); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = object_random_number(kg, sd->object); + return set_attribute_float(f, type, derivatives, val); } /* Particle Attributes */ else if (name == "particle:index") { - float fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_index(kg, particle_id); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = particle_index(kg, particle_id); + return set_attribute_float(f, type, derivatives, val); } else if (name == "particle:age") { - float fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_age(kg, particle_id); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = particle_age(kg, particle_id); + return set_attribute_float(f, type, derivatives, val); } else if (name == "particle:lifetime") { - float fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_lifetime(kg, particle_id); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f= particle_lifetime(kg, particle_id); + return set_attribute_float(f, type, derivatives, val); } else if (name == "particle:location") { - float3 fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_location(kg, particle_id); - fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float3(fval, type, derivatives, val); - return true; + float3 f = particle_location(kg, particle_id); + return set_attribute_float3(f, type, derivatives, val); } #if 0 /* unsupported */ else if (name == "particle:rotation") { - float4 fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_rotation(kg, particle_id); - fval[1] = fval[2] = make_float4(0.0, 0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float4(fval, type, derivatives, val); - return true; + float4 f = particle_rotation(kg, particle_id); + return set_attribute_float4(f, type, derivatives, val); } #endif else if (name == "particle:size") { - float fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_size(kg, particle_id); - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = particle_size(kg, particle_id); + return set_attribute_float(f, type, derivatives, val); } else if (name == "particle:velocity") { - float3 fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_velocity(kg, particle_id); - fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float3(fval, type, derivatives, val); - return true; + float3 f = particle_velocity(kg, particle_id); + return set_attribute_float3(f, type, derivatives, val); } else if (name == "particle:angular_velocity") { - float3 fval[3]; uint particle_id = object_particle_id(kg, sd->object); - fval[0] = particle_angular_velocity(kg, particle_id); - fval[1] = fval[2] = make_float3(0.0, 0.0, 0.0); /* derivates set to 0 */ - set_attribute_float3(fval, type, derivatives, val); - return true; + float3 f = particle_angular_velocity(kg, particle_id); + return set_attribute_float3(f, type, derivatives, val); } else if (name == "geom:numpolyvertices") { return set_attribute_int(3, type, derivatives, val); @@ -550,6 +564,10 @@ static bool get_object_standard_attribute(KernelGlobals *kg, ShaderData *sd, ust object_position_transform(kg, sd, &P[2]); return set_attribute_float3_3(P, type, derivatives, val); } + else if(name == "geom:name") { + ustring object_name = kg->osl.object_names[sd->object]; + return set_attribute_string(object_name, type, derivatives, val); + } else return false; } @@ -559,11 +577,8 @@ static bool get_background_attribute(KernelGlobals *kg, ShaderData *sd, ustring { /* Ray Length */ if (name == "path:ray_length") { - float fval[3]; - fval[0] = sd->ray_length; - fval[1] = fval[2] = 0.0; /* derivates set to 0 */ - set_attribute_float(fval, type, derivatives, val); - return true; + float f = sd->ray_length; + return set_attribute_float(f, type, derivatives, val); } else @@ -647,4 +662,107 @@ int OSLRenderServices::pointcloud_get(ustring filename, size_t *indices, int cou return 0; } +bool OSLRenderServices::trace(TraceOpt &options, OSL::ShaderGlobals *sg, + const OSL::Vec3 &P, const OSL::Vec3 &dPdx, + const OSL::Vec3 &dPdy, const OSL::Vec3 &R, + const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy) +{ + /* todo: options.shader support, maybe options.traceset */ + ShaderData *sd = (ShaderData *)(sg->renderstate); + + /* setup ray */ + Ray ray; + + ray.P = TO_FLOAT3(P); + ray.D = TO_FLOAT3(R); + ray.t = (options.maxdist == 1.0e30)? FLT_MAX: options.maxdist - options.mindist; + ray.time = sd->time; + + if(options.mindist == 0.0f) { + /* avoid self-intersections */ + if(ray.P == sd->P) { + bool transmit = (dot(sd->Ng, ray.D) < 0.0f); + ray.P = ray_offset(sd->P, (transmit)? -sd->Ng: sd->Ng); + } + } + else { + /* offset for minimum distance */ + ray.P += options.mindist*ray.D; + } + + /* ray differentials */ + ray.dP.dx = TO_FLOAT3(dPdx); + ray.dP.dy = TO_FLOAT3(dPdy); + ray.dD.dx = TO_FLOAT3(dRdx); + ray.dD.dy = TO_FLOAT3(dRdy); + + /* allocate trace data */ + TraceData *tracedata = new TraceData(); + tracedata->ray = ray; + tracedata->setup = false; + + if(sg->tracedata) + delete (TraceData*)sg->tracedata; + sg->tracedata = tracedata; + + /* raytrace */ + return scene_intersect(kernel_globals, &ray, ~0, &tracedata->isect); +} + + +bool OSLRenderServices::getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name, + TypeDesc type, void *val, bool derivatives) +{ + TraceData *tracedata = (TraceData*)sg->tracedata; + + if(source == "trace" && tracedata) { + if(name == "hit") { + return set_attribute_int((tracedata->isect.prim != ~0), type, derivatives, val); + } + else if(tracedata->isect.prim != ~0) { + if(name == "hitdist") { + float f[3] = {tracedata->isect.t, 0.0f, 0.0f}; + return set_attribute_float(f, type, derivatives, val); + } + else { + KernelGlobals *kg = kernel_globals; + ShaderData *sd = &tracedata->sd; + + if(!tracedata->setup) { + /* lazy shader data setup */ + shader_setup_from_ray(kg, sd, &tracedata->isect, &tracedata->ray); + tracedata->setup = true; + } + + if(name == "N") { + return set_attribute_float3(sd->N, type, derivatives, val); + } + else if(name == "Ng") { + return set_attribute_float3(sd->Ng, type, derivatives, val); + } + else if(name == "P") { + float3 f[3] = {sd->P, sd->dP.dx, sd->dP.dy}; + return set_attribute_float3(f, type, derivatives, val); + } + else if(name == "I") { + float3 f[3] = {sd->I, sd->dI.dx, sd->dI.dy}; + return set_attribute_float3(f, type, derivatives, val); + } + else if(name == "u") { + float f[3] = {sd->u, sd->du.dx, sd->du.dy}; + return set_attribute_float(f, type, derivatives, val); + } + else if(name == "v") { + float f[3] = {sd->v, sd->dv.dx, sd->dv.dy}; + return set_attribute_float(f, type, derivatives, val); + } + + return get_attribute(sd, derivatives, u_empty, type, name, val); + } + } + } + + return false; +} + CCL_NAMESPACE_END diff --git a/intern/cycles/kernel/osl/osl_services.h b/intern/cycles/kernel/osl/osl_services.h index fa32d47a89f..ce62eaf8994 100644 --- a/intern/cycles/kernel/osl/osl_services.h +++ b/intern/cycles/kernel/osl/osl_services.h @@ -76,8 +76,20 @@ public: int pointcloud_get(ustring filename, size_t *indices, int count, ustring attr_name, TypeDesc attr_type, void *out_data); -private: - KernelGlobals *kernel_globals; + bool trace(TraceOpt &options, OSL::ShaderGlobals *sg, + const OSL::Vec3 &P, const OSL::Vec3 &dPdx, + const OSL::Vec3 &dPdy, const OSL::Vec3 &R, + const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy); + + bool getmessage(OSL::ShaderGlobals *sg, ustring source, ustring name, + TypeDesc type, void *val, bool derivatives); + + struct TraceData { + Ray ray; + Intersection isect; + ShaderData sd; + bool setup; + }; static ustring u_distance; static ustring u_index; @@ -86,6 +98,9 @@ private: static ustring u_raster; static ustring u_ndc; static ustring u_empty; + +private: + KernelGlobals *kernel_globals; }; CCL_NAMESPACE_END diff --git a/intern/cycles/kernel/osl/osl_shader.cpp b/intern/cycles/kernel/osl/osl_shader.cpp index 5d26f543efc..0c8de058b5b 100644 --- a/intern/cycles/kernel/osl/osl_shader.cpp +++ b/intern/cycles/kernel/osl/osl_shader.cpp @@ -93,6 +93,7 @@ static void shaderdata_to_shaderglobals(KernelGlobals *kg, ShaderData *sd, /* shader data to be used in services callbacks */ globals->renderstate = sd; + globals->tracedata = NULL; /* hacky, we leave it to services to fetch actual object matrix */ globals->shader2common = sd; @@ -219,6 +220,10 @@ void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, float randb, int if (kg->osl.surface_state[shader]) ss->execute(*ctx, *(kg->osl.surface_state[shader]), *globals); + /* free trace data */ + if(globals->tracedata) + delete (OSLRenderServices::TraceData*)globals->tracedata; + /* flatten closure tree */ sd->num_closure = 0; sd->randb_closure = randb; @@ -274,6 +279,10 @@ float3 OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, int path_fl if (kg->osl.background_state) ss->execute(*ctx, *(kg->osl.background_state), *globals); + /* free trace data */ + if(globals->tracedata) + delete (OSLRenderServices::TraceData*)globals->tracedata; + /* return background color immediately */ if (globals->Ci) return flatten_background_closure_tree(globals->Ci); @@ -352,6 +361,10 @@ void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, float randb, int if (kg->osl.volume_state[shader]) ss->execute(*ctx, *(kg->osl.volume_state[shader]), *globals); + /* free trace data */ + if(globals->tracedata) + delete (OSLRenderServices::TraceData*)globals->tracedata; + if (globals->Ci) flatten_volume_closure_tree(sd, globals->Ci); } @@ -375,6 +388,10 @@ void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd) if (kg->osl.displacement_state[shader]) ss->execute(*ctx, *(kg->osl.displacement_state[shader]), *globals); + /* free trace data */ + if(globals->tracedata) + delete (OSLRenderServices::TraceData*)globals->tracedata; + /* get back position */ sd->P = TO_FLOAT3(globals->P); } |