bsdf_ashikhmin_shirley.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2011-2014 Blender Foundation
  3. *
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #ifndef __BSDF_ASHIKHMIN_SHIRLEY_H__
  17. #define __BSDF_ASHIKHMIN_SHIRLEY_H__
  18. /*
  19. ASHIKHMIN SHIRLEY BSDF
  20. Implementation of
  21. Michael Ashikhmin and Peter Shirley: "An Anisotropic Phong BRDF Model" (2000)
  22. The Fresnel factor is missing to get a separable bsdf (intensity*color), as is
  23. the case with all other microfacet-based BSDF implementations in Cycles.
  24. Other than that, the implementation directly follows the paper.
  25. */
  26. CCL_NAMESPACE_BEGIN
  27. ccl_device int bsdf_ashikhmin_shirley_setup(MicrofacetBsdf *bsdf)
  28. {
  29. bsdf->alpha_x = clamp(bsdf->alpha_x, 1e-4f, 1.0f);
  30. bsdf->alpha_y = bsdf->alpha_x;
  31. bsdf->type = CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID;
  32. return SD_BSDF | SD_BSDF_HAS_EVAL;
  33. }
  34. ccl_device int bsdf_ashikhmin_shirley_aniso_setup(MicrofacetBsdf *bsdf)
  35. {
  36. bsdf->alpha_x = clamp(bsdf->alpha_x, 1e-4f, 1.0f);
  37. bsdf->alpha_y = clamp(bsdf->alpha_y, 1e-4f, 1.0f);
  38. bsdf->type = CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ANISO_ID;
  39. return SD_BSDF | SD_BSDF_HAS_EVAL;
  40. }
  41. ccl_device void bsdf_ashikhmin_shirley_blur(ShaderClosure *sc, float roughness)
  42. {
  43. MicrofacetBsdf *bsdf = (MicrofacetBsdf *)sc;
  44. bsdf->alpha_x = fmaxf(roughness, bsdf->alpha_x);
  45. bsdf->alpha_y = fmaxf(roughness, bsdf->alpha_y);
  46. }
  47. ccl_device_inline float bsdf_ashikhmin_shirley_roughness_to_exponent(float roughness)
  48. {
  49. return 2.0f / (roughness * roughness) - 2.0f;
  50. }
  51. ccl_device_forceinline float3 bsdf_ashikhmin_shirley_eval_reflect(const ShaderClosure *sc,
  52. const float3 I,
  53. const float3 omega_in,
  54. float *pdf)
  55. {
  56. const MicrofacetBsdf *bsdf = (const MicrofacetBsdf *)sc;
  57. float3 N = bsdf->N;
  58. float NdotI = dot(N, I); /* in Cycles/OSL convention I is omega_out */
  59. float NdotO = dot(N, omega_in); /* and consequently we use for O omaga_in ;) */
  60. float out = 0.0f;
  61. if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f)
  62. return make_float3(0.0f, 0.0f, 0.0f);
  63. if (NdotI > 0.0f && NdotO > 0.0f) {
  64. NdotI = fmaxf(NdotI, 1e-6f);
  65. NdotO = fmaxf(NdotO, 1e-6f);
  66. float3 H = normalize(omega_in + I);
  67. float HdotI = fmaxf(fabsf(dot(H, I)), 1e-6f);
  68. float HdotN = fmaxf(dot(H, N), 1e-6f);
  69. /* pump from original paper
  70. * (first derivative disc., but cancels the HdotI in the pdf nicely) */
  71. float pump = 1.0f / fmaxf(1e-6f, (HdotI * fmaxf(NdotO, NdotI)));
  72. /* pump from d-brdf paper */
  73. /*float pump = 1.0f / fmaxf(1e-4f, ((NdotO + NdotI) * (NdotO*NdotI))); */
  74. float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
  75. float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
  76. if (n_x == n_y) {
  77. /* isotropic */
  78. float e = n_x;
  79. float lobe = powf(HdotN, e);
  80. float norm = (n_x + 1.0f) / (8.0f * M_PI_F);
  81. out = NdotO * norm * lobe * pump;
  82. /* this is p_h / 4(H.I) (conversion from 'wh measure' to 'wi measure', eq. 8 in paper). */
  83. *pdf = norm * lobe / HdotI;
  84. }
  85. else {
  86. /* anisotropic */
  87. float3 X, Y;
  88. make_orthonormals_tangent(N, bsdf->T, &X, &Y);
  89. float HdotX = dot(H, X);
  90. float HdotY = dot(H, Y);
  91. float lobe;
  92. if (HdotN < 1.0f) {
  93. float e = (n_x * HdotX * HdotX + n_y * HdotY * HdotY) / (1.0f - HdotN * HdotN);
  94. lobe = powf(HdotN, e);
  95. }
  96. else {
  97. lobe = 1.0f;
  98. }
  99. float norm = sqrtf((n_x + 1.0f) * (n_y + 1.0f)) / (8.0f * M_PI_F);
  100. out = NdotO * norm * lobe * pump;
  101. *pdf = norm * lobe / HdotI;
  102. }
  103. }
  104. return make_float3(out, out, out);
  105. }
  106. ccl_device float3 bsdf_ashikhmin_shirley_eval_transmit(const ShaderClosure *sc,
  107. const float3 I,
  108. const float3 omega_in,
  109. float *pdf)
  110. {
  111. return make_float3(0.0f, 0.0f, 0.0f);
  112. }
  113. ccl_device_inline void bsdf_ashikhmin_shirley_sample_first_quadrant(
  114. float n_x, float n_y, float randu, float randv, float *phi, float *cos_theta)
  115. {
  116. *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * randu));
  117. float cos_phi = cosf(*phi);
  118. float sin_phi = sinf(*phi);
  119. *cos_theta = powf(randv, 1.0f / (n_x * cos_phi * cos_phi + n_y * sin_phi * sin_phi + 1.0f));
  120. }
  121. ccl_device int bsdf_ashikhmin_shirley_sample(const ShaderClosure *sc,
  122. float3 Ng,
  123. float3 I,
  124. float3 dIdx,
  125. float3 dIdy,
  126. float randu,
  127. float randv,
  128. float3 *eval,
  129. float3 *omega_in,
  130. float3 *domega_in_dx,
  131. float3 *domega_in_dy,
  132. float *pdf)
  133. {
  134. const MicrofacetBsdf *bsdf = (const MicrofacetBsdf *)sc;
  135. float3 N = bsdf->N;
  136. int label = LABEL_REFLECT | LABEL_GLOSSY;
  137. float NdotI = dot(N, I);
  138. if (NdotI > 0.0f) {
  139. float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
  140. float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
  141. /* get x,y basis on the surface for anisotropy */
  142. float3 X, Y;
  143. if (n_x == n_y)
  144. make_orthonormals(N, &X, &Y);
  145. else
  146. make_orthonormals_tangent(N, bsdf->T, &X, &Y);
  147. /* sample spherical coords for h in tangent space */
  148. float phi;
  149. float cos_theta;
  150. if (n_x == n_y) {
  151. /* isotropic sampling */
  152. phi = M_2PI_F * randu;
  153. cos_theta = powf(randv, 1.0f / (n_x + 1.0f));
  154. }
  155. else {
  156. /* anisotropic sampling */
  157. if (randu < 0.25f) { /* first quadrant */
  158. float remapped_randu = 4.0f * randu;
  159. bsdf_ashikhmin_shirley_sample_first_quadrant(
  160. n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
  161. }
  162. else if (randu < 0.5f) { /* second quadrant */
  163. float remapped_randu = 4.0f * (.5f - randu);
  164. bsdf_ashikhmin_shirley_sample_first_quadrant(
  165. n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
  166. phi = M_PI_F - phi;
  167. }
  168. else if (randu < 0.75f) { /* third quadrant */
  169. float remapped_randu = 4.0f * (randu - 0.5f);
  170. bsdf_ashikhmin_shirley_sample_first_quadrant(
  171. n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
  172. phi = M_PI_F + phi;
  173. }
  174. else { /* fourth quadrant */
  175. float remapped_randu = 4.0f * (1.0f - randu);
  176. bsdf_ashikhmin_shirley_sample_first_quadrant(
  177. n_x, n_y, remapped_randu, randv, &phi, &cos_theta);
  178. phi = 2.0f * M_PI_F - phi;
  179. }
  180. }
  181. /* get half vector in tangent space */
  182. float sin_theta = sqrtf(fmaxf(0.0f, 1.0f - cos_theta * cos_theta));
  183. float cos_phi = cosf(phi);
  184. float sin_phi = sinf(phi); /* no sqrt(1-cos^2) here b/c it causes artifacts */
  185. float3 h = make_float3(sin_theta * cos_phi, sin_theta * sin_phi, cos_theta);
  186. /* half vector to world space */
  187. float3 H = h.x * X + h.y * Y + h.z * N;
  188. float HdotI = dot(H, I);
  189. if (HdotI < 0.0f)
  190. H = -H;
  191. /* reflect I on H to get omega_in */
  192. *omega_in = -I + (2.0f * HdotI) * H;
  193. if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f) {
  194. /* Some high number for MIS. */
  195. *pdf = 1e6f;
  196. *eval = make_float3(1e6f, 1e6f, 1e6f);
  197. label = LABEL_REFLECT | LABEL_SINGULAR;
  198. }
  199. else {
  200. /* leave the rest to eval_reflect */
  201. *eval = bsdf_ashikhmin_shirley_eval_reflect(sc, I, *omega_in, pdf);
  202. }
  203. #ifdef __RAY_DIFFERENTIALS__
  204. /* just do the reflection thing for now */
  205. *domega_in_dx = (2.0f * dot(N, dIdx)) * N - dIdx;
  206. *domega_in_dy = (2.0f * dot(N, dIdy)) * N - dIdy;
  207. #endif
  208. }
  209. return label;
  210. }
  211. CCL_NAMESPACE_END
  212. #endif /* __BSDF_ASHIKHMIN_SHIRLEY_H__ */