2 * Copyright 2011-2013 Blender Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 /* VOLUME EXTINCTION */
24 ccl_device void volume_extinction_setup(ShaderData *sd, float3 weight)
26 if(sd->flag & SD_EXTINCTION) {
27 sd->closure_transparent_extinction += weight;
30 sd->flag |= SD_EXTINCTION;
31 sd->closure_transparent_extinction = weight;
35 /* HENYEY-GREENSTEIN CLOSURE */
37 typedef ccl_addr_space struct HenyeyGreensteinVolume {
41 } HenyeyGreensteinVolume;
43 /* Given cosine between rays, return probability density that a photon bounces
44 * to that direction. The g parameter controls how different it is from the
45 * uniform sphere. g=0 uniform diffuse-like, g=1 close to sharp single ray. */
46 ccl_device float single_peaked_henyey_greenstein(float cos_theta, float g)
48 return ((1.0f - g * g) / safe_powf(1.0f + g * g - 2.0f * g * cos_theta, 1.5f)) * (M_1_PI_F * 0.25f);
51 ccl_device int volume_henyey_greenstein_setup(HenyeyGreensteinVolume *volume)
53 volume->type = CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID;
55 /* clamp anisotropy to avoid delta function */
56 volume->g = signf(volume->g) * min(fabsf(volume->g), 1.0f - 1e-3f);
61 ccl_device bool volume_henyey_greenstein_merge(const ShaderClosure *a, const ShaderClosure *b)
63 const HenyeyGreensteinVolume *volume_a = (const HenyeyGreensteinVolume*)a;
64 const HenyeyGreensteinVolume *volume_b = (const HenyeyGreensteinVolume*)b;
66 return (volume_a->g == volume_b->g);
69 ccl_device float3 volume_henyey_greenstein_eval_phase(const ShaderClosure *sc, const float3 I, float3 omega_in, float *pdf)
71 const HenyeyGreensteinVolume *volume = (const HenyeyGreensteinVolume*)sc;
74 /* note that I points towards the viewer */
75 if(fabsf(g) < 1e-3f) {
76 *pdf = M_1_PI_F * 0.25f;
79 float cos_theta = dot(-I, omega_in);
80 *pdf = single_peaked_henyey_greenstein(cos_theta, g);
83 return make_float3(*pdf, *pdf, *pdf);
86 ccl_device float3 henyey_greenstrein_sample(float3 D, float g, float randu, float randv, float *pdf)
88 /* match pdf for small g */
90 bool isotropic = fabsf(g) < 1e-3f;
93 cos_theta = (1.0f - 2.0f * randu);
95 *pdf = M_1_PI_F * 0.25f;
99 float k = (1.0f - g * g) / (1.0f - g + 2.0f * g * randu);
100 cos_theta = (1.0f + g * g - k * k) / (2.0f * g);
102 *pdf = single_peaked_henyey_greenstein(cos_theta, g);
106 float sin_theta = safe_sqrtf(1.0f - cos_theta * cos_theta);
107 float phi = M_2PI_F * randv;
108 float3 dir = make_float3(sin_theta * cosf(phi), sin_theta * sinf(phi), cos_theta);
111 make_orthonormals(D, &T, &B);
112 dir = dir.x * T + dir.y * B + dir.z * D;
117 ccl_device int volume_henyey_greenstein_sample(const ShaderClosure *sc, float3 I, float3 dIdx, float3 dIdy, float randu, float randv,
118 float3 *eval, float3 *omega_in, float3 *domega_in_dx, float3 *domega_in_dy, float *pdf)
120 const HenyeyGreensteinVolume *volume = (const HenyeyGreensteinVolume*)sc;
123 /* note that I points towards the viewer and so is used negated */
124 *omega_in = henyey_greenstrein_sample(-I, g, randu, randv, pdf);
125 *eval = make_float3(*pdf, *pdf, *pdf); /* perfect importance sampling */
127 #ifdef __RAY_DIFFERENTIALS__
128 /* todo: implement ray differential estimation */
129 *domega_in_dx = make_float3(0.0f, 0.0f, 0.0f);
130 *domega_in_dy = make_float3(0.0f, 0.0f, 0.0f);
133 return LABEL_VOLUME_SCATTER;
138 ccl_device float3 volume_phase_eval(const ShaderData *sd, const ShaderClosure *sc, float3 omega_in, float *pdf)
140 kernel_assert(sc->type == CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID);
142 return volume_henyey_greenstein_eval_phase(sc, sd->I, omega_in, pdf);
145 ccl_device int volume_phase_sample(const ShaderData *sd, const ShaderClosure *sc, float randu,
146 float randv, float3 *eval, float3 *omega_in, differential3 *domega_in, float *pdf)
151 case CLOSURE_VOLUME_HENYEY_GREENSTEIN_ID:
152 label = volume_henyey_greenstein_sample(sc, sd->I, sd->dI.dx, sd->dI.dy, randu, randv, eval, omega_in, &domega_in->dx, &domega_in->dy, pdf);
155 *eval = make_float3(0.0f, 0.0f, 0.0f);