inline void operator() (ustring name, Dual2<R> &result, const Dual2<S> &s, const Dual2<T> &t, ShaderGlobals *sg, const NoiseParams *opt) const { if (name == Strings::uperlin || name == Strings::noise) { Noise noise; noise(result, s, t); } else if (name == Strings::perlin || name == Strings::snoise) { SNoise snoise; snoise(result, s, t); } else if (name == Strings::simplexnoise || name == Strings::simplex) { SimplexNoise simplexnoise; simplexnoise(result, s, t); } else if (name == Strings::usimplexnoise || name == Strings::usimplex) { USimplexNoise usimplexnoise; usimplexnoise(result, s, t); } else if (name == Strings::cell) { CellNoise cellnoise; cellnoise(result.val(), s.val(), t.val()); result.clear_d(); } else if (name == Strings::gabor) { GaborNoise gnoise; gnoise (name, result, s, t, sg, opt); } else { ((ShadingContext *)sg->context)->error ("Unknown noise type \"%s\"", name.c_str()); } }
inline void operator() (ustring name, Dual2<R> &result, const Dual2<S> &s, const Dual2<T> &t, const S &sp, const T &tp, ShaderGlobals *sg, const NoiseParams *opt) const { if (name == Strings::uperlin || name == Strings::noise) { PeriodicNoise noise; noise(result, s, t, sp, tp); } else if (name == Strings::perlin || name == Strings::snoise) { PeriodicSNoise snoise; snoise(result, s, t, sp, tp); } else if (name == Strings::cell) { PeriodicCellNoise cellnoise; cellnoise(result.val(), s.val(), t.val(), sp, tp); result.clear_d(); } else if (name == Strings::gabor) { GaborPNoise gnoise; gnoise (name, result, s, t, sp, tp, sg, opt); } else if (name == Strings::hash) { PeriodicHashNoise hashnoise; hashnoise(result.val(), s.val(), t.val(), sp, tp); result.clear_d(); } else { ((ShadingContext *)sg->context)->error ("Unknown noise type \"%s\"", name.c_str()); } }
// set up the filter matrix static void gabor_setup_filter (const Dual2<Vec3> &P, GaborParams &gp) { // Make texture-space normal, tangent, bitangent Vec3 n, t, b; n = P.dx().cross (P.dy()); // normal to P if (n.dot(n) < 1.0e-6f) { /* length of deriv < 1/1000 */ // No way to do filter if we have no derivs, and no reason to // do it if it's too small to have any effect. gp.do_filter = false; return; // we won't need anything else if filtering is off } make_orthonormals (n, t, b); // Rotations from tangent<->texture space Matrix33 Mtex_to_tan = make_matrix33_cols (t, b, n); // M3_local Matrix33 Mscreen_to_tex = make_matrix33_cols (P.dx(), P.dy(), Vec3(0.0f,0.0f,0.0f)); Matrix33 Mscreen_to_tan = Mscreen_to_tex * Mtex_to_tan; // M3_scr_tan Matrix22 M_scr_tan (Mscreen_to_tan[0][0], Mscreen_to_tan[0][1], Mscreen_to_tan[1][0], Mscreen_to_tan[1][1]); float sigma_f_scr = 0.5f; Matrix22 Sigma_f_scr (sigma_f_scr * sigma_f_scr, 0.0f, 0.0f, sigma_f_scr * sigma_f_scr); Matrix22 M_scr_tan_t = M_scr_tan.transposed(); Matrix22 Sigma_f_tan = M_scr_tan_t * Sigma_f_scr * M_scr_tan; gp.N = n; gp.filter = Sigma_f_tan; gp.det_filter = determinant(Sigma_f_tan); gp.local = Mtex_to_tan; if (gp.det_filter < 1.0e-18f) { gp.do_filter = false; // Turn off filtering when tiny values will lead to numerical // errors later if we filter. Yes, it's kind of arbitrary. } }
inline Dual2<float> safe_fmod (const Dual2<float> &a, const Dual2<float> &b) { return Dual2<float> (safe_fmod (a.val(), b.val()), a.dx(), a.dy()); }
inline Dual2<float> fabsf (const Dual2<float> &x) { return x.val() >= 0 ? x : -x; }
OSL_HOSTDEVICE inline void operator() (Dual2<Vec3> &result, const Dual2<Vec3> &p, const Dual2<float> &t) const { result.set (v(), v(), v()); }
OSL_HOSTDEVICE inline void operator() (Dual2<float> &result, const Dual2<Vec3> &p, const Dual2<float> &t, int seed=0) const { result.set (0.5f, 0.5f, 0.5f); }
OSL_HOSTDEVICE inline void operator() (Dual2<float> &result, const Dual2<float> &x, const Dual2<float> &y, int seed=0) const { result.set (0.0f, 0.0f, 0.0f); }
// Helper function: per-component 'floor' of a Dual2<Vec3>. inline Vec3 floor (const Dual2<Vec3> &vd) { const Vec3 &v (vd.val()); return Vec3 (floorf(v[0]), floorf(v[1]), floorf(v[2])); }
// Evaluate the summed contribution of all gabor impulses within the // cell whose corner is c_i. x_c_i is vector from x (the point // we are trying to evaluate noise at) and c_i. Dual2<float> gabor_cell (GaborParams &gp, const Vec3 &c_i, const Dual2<Vec3> &x_c_i, int seed = 0) { fast_rng rng (gp.periodic ? Vec3(wrap(c_i,gp.period)) : c_i, seed); int n_impulses = rng.poisson (gp.lambda * gp.radius3); Dual2<float> sum = 0; for (int i = 0; i < n_impulses; i++) { // OLD code: Vec3 x_i_c (rng(), rng(), rng()); // Turned out that C++ spec says order of args are unspecified. // gcc appeared to do right-to-left, so to make sure our noise // function is locked down (and works identically for clang, // which evaluates left-to-right), we ask for the rng() calls // one at a time and match the way it looked before. float z_rng = rng(), y_rng = rng(), x_rng = rng(); Vec3 x_i_c (x_rng, y_rng, z_rng); Dual2<Vec3> x_k_i = gp.radius * (x_c_i - x_i_c); float phi_i; Vec3 omega_i; gabor_sample (gp, c_i, rng, omega_i, phi_i); if (x_k_i.val().length2() < gp.radius2) { if (! gp.do_filter) { // N.B. if determinant(gp.filter) is too small, we will // run into numerical problems. But the filtering isn't // needed in that case anyway, so just don't filter. // This seems to only come up when the filter region is // tiny. sum += gabor_kernel (gp.weight, omega_i, phi_i, gp.a, x_k_i); // 3D } else { // Transform the impulse's anisotropy into tangent space Vec3 omega_i_t; multMatrix (gp.local, omega_i, omega_i_t); // Slice to get a 2D kernel Dual2<float> d_i = -dot(gp.N, x_k_i); Dual2<float> w_i_t_s; Vec2 omega_i_t_s; Dual2<float> phi_i_t_s; slice_gabor_kernel_3d (d_i, gp.weight, gp.a, omega_i_t, phi_i, w_i_t_s, omega_i_t_s, phi_i_t_s); // Filter the 2D kernel Dual2<float> w_i_t_s_f; float a_i_t_s_f; Vec2 omega_i_t_s_f; Dual2<float> phi_i_t_s_f; filter_gabor_kernel_2d (gp.filter, w_i_t_s, gp.a, omega_i_t_s, phi_i_t_s, w_i_t_s_f, a_i_t_s_f, omega_i_t_s_f, phi_i_t_s_f); // Now evaluate the 2D filtered kernel Dual2<Vec3> xkit; multMatrix (gp.local, x_k_i, xkit); Dual2<Vec2> x_k_i_t = make_Vec2 (comp(xkit,0), comp(xkit,1)); Dual2<float> gk = gabor_kernel (w_i_t_s_f, omega_i_t_s_f, phi_i_t_s_f, a_i_t_s_f, x_k_i_t); // 2D if (! OIIO::isfinite(gk.val())) { // Numeric failure of the filtered version. Fall // back on the unfiltered. gk = gabor_kernel (gp.weight, omega_i, phi_i, gp.a, x_k_i); // 3D } sum += gk; } } } return sum; }