2012-06-09 17:22:52 +00:00
|
|
|
/*
|
|
|
|
* Adapted from Open Shading Language with this license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Modifications Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Sony Pictures Imageworks nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
#ifndef __BSDF_WARD_H__
|
|
|
|
#define __BSDF_WARD_H__
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* WARD */
|
|
|
|
|
2012-10-20 12:18:00 +00:00
|
|
|
__device int bsdf_ward_setup(ShaderClosure *sc)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-10-20 12:18:00 +00:00
|
|
|
float ax = sc->data0;
|
|
|
|
float ay = sc->data1;
|
|
|
|
|
2012-10-17 23:09:12 +00:00
|
|
|
float m_ax = clamp(ax, 1e-4f, 1.0f);
|
|
|
|
float m_ay = clamp(ay, 1e-4f, 1.0f);
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
sc->data0 = m_ax;
|
|
|
|
sc->data1 = m_ay;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
sc->type = CLOSURE_BSDF_WARD_ID;
|
2012-10-20 12:18:00 +00:00
|
|
|
return SD_BSDF|SD_BSDF_HAS_EVAL|SD_BSDF_GLOSSY;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2011-09-12 13:13:56 +00:00
|
|
|
__device void bsdf_ward_blur(ShaderClosure *sc, float roughness)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
sc->data0 = fmaxf(roughness, sc->data0);
|
|
|
|
sc->data1 = fmaxf(roughness, sc->data1);
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-10-17 12:17:17 +00:00
|
|
|
__device float3 bsdf_ward_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
float m_ax = sc->data0;
|
|
|
|
float m_ay = sc->data1;
|
2012-10-17 12:17:17 +00:00
|
|
|
float3 N = sc->N;
|
|
|
|
float3 T = sc->T;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-10-17 12:17:17 +00:00
|
|
|
float cosNO = dot(N, I);
|
|
|
|
float cosNI = dot(N, omega_in);
|
2011-09-08 18:58:07 +00:00
|
|
|
|
2012-11-06 19:59:02 +00:00
|
|
|
if(cosNI > 0.0f && cosNO > 0.0f) {
|
|
|
|
cosNO = max(cosNO, 1e-4f);
|
|
|
|
cosNI = max(cosNI, 1e-4f);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
// get half vector and get x,y basis on the surface for anisotropy
|
|
|
|
float3 H = normalize(omega_in + I); // normalize needed for pdf
|
|
|
|
float3 X, Y;
|
2012-10-17 12:17:17 +00:00
|
|
|
make_orthonormals_tangent(N, T, &X, &Y);
|
2011-04-27 11:58:34 +00:00
|
|
|
// eq. 4
|
2011-09-08 18:58:07 +00:00
|
|
|
float dotx = dot(H, X) / m_ax;
|
|
|
|
float doty = dot(H, Y) / m_ay;
|
2012-10-17 12:17:17 +00:00
|
|
|
float dotn = dot(H, N);
|
2011-04-27 11:58:34 +00:00
|
|
|
float exp_arg = (dotx * dotx + doty * doty) / (dotn * dotn);
|
2011-09-08 18:58:07 +00:00
|
|
|
float denom = (4 * M_PI_F * m_ax * m_ay * sqrtf(cosNO * cosNI));
|
2011-04-27 11:58:34 +00:00
|
|
|
float exp_val = expf(-exp_arg);
|
|
|
|
float out = cosNI * exp_val / denom;
|
|
|
|
float oh = dot(H, I);
|
2011-09-08 18:58:07 +00:00
|
|
|
denom = 4 * M_PI_F * m_ax * m_ay * oh * dotn * dotn * dotn;
|
2011-04-27 11:58:34 +00:00
|
|
|
*pdf = exp_val / denom;
|
|
|
|
return make_float3 (out, out, out);
|
|
|
|
}
|
2012-10-10 13:02:20 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
return make_float3 (0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-10-17 12:17:17 +00:00
|
|
|
__device float3 bsdf_ward_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
return make_float3(0.0f, 0.0f, 0.0f);
|
|
|
|
}
|
|
|
|
|
2012-10-17 12:17:17 +00:00
|
|
|
__device int bsdf_ward_sample(const ShaderClosure *sc, float3 Ng, float3 I, float3 dIdx, float3 dIdy, float randu, float randv, float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2011-09-12 13:13:56 +00:00
|
|
|
float m_ax = sc->data0;
|
|
|
|
float m_ay = sc->data1;
|
2012-10-17 12:17:17 +00:00
|
|
|
float3 N = sc->N;
|
|
|
|
float3 T = sc->T;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-10-17 12:17:17 +00:00
|
|
|
float cosNO = dot(N, I);
|
2012-11-06 19:59:02 +00:00
|
|
|
if(cosNO > 0.0f) {
|
2011-04-27 11:58:34 +00:00
|
|
|
// get x,y basis on the surface for anisotropy
|
|
|
|
float3 X, Y;
|
2012-10-17 12:17:17 +00:00
|
|
|
make_orthonormals_tangent(N, T, &X, &Y);
|
2011-04-27 11:58:34 +00:00
|
|
|
// generate random angles for the half vector
|
|
|
|
// eq. 7 (taking care around discontinuities to keep
|
|
|
|
//ttoutput angle in the right quadrant)
|
|
|
|
// we take advantage of cos(atan(x)) == 1/sqrt(1+x^2)
|
|
|
|
//tttt and sin(atan(x)) == x/sqrt(1+x^2)
|
2011-09-08 18:58:07 +00:00
|
|
|
float alphaRatio = m_ay / m_ax;
|
2011-04-27 11:58:34 +00:00
|
|
|
float cosPhi, sinPhi;
|
|
|
|
if(randu < 0.25f) {
|
|
|
|
float val = 4 * randu;
|
|
|
|
float tanPhi = alphaRatio * tanf(M_PI_2_F * val);
|
|
|
|
cosPhi = 1 / sqrtf(1 + tanPhi * tanPhi);
|
|
|
|
sinPhi = tanPhi * cosPhi;
|
2012-09-20 12:29:28 +00:00
|
|
|
}
|
|
|
|
else if(randu < 0.5f) {
|
2011-04-27 11:58:34 +00:00
|
|
|
float val = 1 - 4 * (0.5f - randu);
|
|
|
|
float tanPhi = alphaRatio * tanf(M_PI_2_F * val);
|
|
|
|
// phi = M_PI_F - phi;
|
|
|
|
cosPhi = -1 / sqrtf(1 + tanPhi * tanPhi);
|
|
|
|
sinPhi = -tanPhi * cosPhi;
|
2012-09-20 12:29:28 +00:00
|
|
|
}
|
|
|
|
else if(randu < 0.75f) {
|
2011-04-27 11:58:34 +00:00
|
|
|
float val = 4 * (randu - 0.5f);
|
|
|
|
float tanPhi = alphaRatio * tanf(M_PI_2_F * val);
|
|
|
|
//phi = M_PI_F + phi;
|
|
|
|
cosPhi = -1 / sqrtf(1 + tanPhi * tanPhi);
|
|
|
|
sinPhi = tanPhi * cosPhi;
|
2012-09-20 12:29:28 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-04-27 11:58:34 +00:00
|
|
|
float val = 1 - 4 * (1 - randu);
|
|
|
|
float tanPhi = alphaRatio * tanf(M_PI_2_F * val);
|
|
|
|
// phi = 2 * M_PI_F - phi;
|
|
|
|
cosPhi = 1 / sqrtf(1 + tanPhi * tanPhi);
|
|
|
|
sinPhi = -tanPhi * cosPhi;
|
|
|
|
}
|
|
|
|
// eq. 6
|
|
|
|
// we take advantage of cos(atan(x)) == 1/sqrt(1+x^2)
|
|
|
|
//tttt and sin(atan(x)) == x/sqrt(1+x^2)
|
2011-09-08 18:58:07 +00:00
|
|
|
float thetaDenom = (cosPhi * cosPhi) / (m_ax * m_ax) + (sinPhi * sinPhi) / (m_ay * m_ay);
|
2011-04-27 11:58:34 +00:00
|
|
|
float tanTheta2 = -logf(1 - randv) / thetaDenom;
|
|
|
|
float cosTheta = 1 / sqrtf(1 + tanTheta2);
|
|
|
|
float sinTheta = cosTheta * sqrtf(tanTheta2);
|
|
|
|
|
|
|
|
float3 h; // already normalized becaused expressed from spherical coordinates
|
|
|
|
h.x = sinTheta * cosPhi;
|
|
|
|
h.y = sinTheta * sinPhi;
|
|
|
|
h.z = cosTheta;
|
|
|
|
// compute terms that are easier in local space
|
2011-09-08 18:58:07 +00:00
|
|
|
float dotx = h.x / m_ax;
|
|
|
|
float doty = h.y / m_ay;
|
2011-04-27 11:58:34 +00:00
|
|
|
float dotn = h.z;
|
|
|
|
// transform to world space
|
2012-10-17 12:17:17 +00:00
|
|
|
h = h.x * X + h.y * Y + h.z * N;
|
2011-04-27 11:58:34 +00:00
|
|
|
// generate the final sample
|
2012-10-17 12:17:17 +00:00
|
|
|
float oh = dot(h, I);
|
|
|
|
*omega_in = 2.0f * oh * h - I;
|
|
|
|
if(dot(Ng, *omega_in) > 0) {
|
|
|
|
float cosNI = dot(N, *omega_in);
|
2011-04-27 11:58:34 +00:00
|
|
|
if(cosNI > 0) {
|
2012-11-06 19:59:02 +00:00
|
|
|
cosNO = max(cosNO, 1e-4f);
|
|
|
|
cosNI = max(cosNI, 1e-4f);
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
// eq. 9
|
|
|
|
float exp_arg = (dotx * dotx + doty * doty) / (dotn * dotn);
|
2011-09-08 18:58:07 +00:00
|
|
|
float denom = 4 * M_PI_F * m_ax * m_ay * oh * dotn * dotn * dotn;
|
2011-04-27 11:58:34 +00:00
|
|
|
*pdf = expf(-exp_arg) / denom;
|
|
|
|
// compiler will reuse expressions already computed
|
2011-09-08 18:58:07 +00:00
|
|
|
denom = (4 * M_PI_F * m_ax * m_ay * sqrtf(cosNO * cosNI));
|
2011-04-27 11:58:34 +00:00
|
|
|
float power = cosNI * expf(-exp_arg) / denom;
|
|
|
|
*eval = make_float3(power, power, power);
|
|
|
|
#ifdef __RAY_DIFFERENTIALS__
|
2012-10-17 12:17:17 +00:00
|
|
|
*domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
|
|
|
|
*domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
|
2011-04-27 11:58:34 +00:00
|
|
|
// Since there is some blur to this reflection, make the
|
|
|
|
// derivatives a bit bigger. In theory this varies with the
|
|
|
|
// roughness but the exact relationship is complex and
|
|
|
|
// requires more ops than are practical.
|
2011-05-20 12:26:01 +00:00
|
|
|
*domega_in_dx *= 10.0f;
|
|
|
|
*domega_in_dy *= 10.0f;
|
2011-04-27 11:58:34 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LABEL_REFLECT|LABEL_GLOSSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __BSDF_WARD_H__ */
|
|
|
|
|