osl_bssrdf.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Adapted from Open Shading Language with this license:
  3. *
  4. * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
  5. * All Rights Reserved.
  6. *
  7. * Modifications Copyright 2011, Blender Foundation.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * * Neither the name of Sony Pictures Imageworks nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <OSL/genclosure.h>
  33. #include "kernel/kernel_compat_cpu.h"
  34. #include "kernel/osl/osl_closures.h"
  35. #include "kernel/kernel_types.h"
  36. #include "kernel/kernel_montecarlo.h"
  37. #include "kernel/closure/alloc.h"
  38. #include "kernel/closure/bsdf_util.h"
  39. #include "kernel/closure/bsdf_diffuse.h"
  40. #include "kernel/closure/bsdf_principled_diffuse.h"
  41. #include "kernel/closure/bssrdf.h"
  42. CCL_NAMESPACE_BEGIN
  43. using namespace OSL;
  44. static ustring u_cubic("cubic");
  45. static ustring u_gaussian("gaussian");
  46. static ustring u_burley("burley");
  47. static ustring u_principled("principled");
  48. static ustring u_random_walk("random_walk");
  49. static ustring u_principled_random_walk("principled_random_walk");
  50. class CBSSRDFClosure : public CClosurePrimitive {
  51. public:
  52. Bssrdf params;
  53. ustring method;
  54. CBSSRDFClosure()
  55. {
  56. params.texture_blur = 0.0f;
  57. params.sharpness = 0.0f;
  58. params.roughness = 0.0f;
  59. }
  60. void setup(ShaderData *sd, int path_flag, float3 weight)
  61. {
  62. if (method == u_cubic) {
  63. alloc(sd, path_flag, weight, CLOSURE_BSSRDF_CUBIC_ID);
  64. }
  65. else if (method == u_gaussian) {
  66. alloc(sd, path_flag, weight, CLOSURE_BSSRDF_GAUSSIAN_ID);
  67. }
  68. else if (method == u_burley) {
  69. alloc(sd, path_flag, weight, CLOSURE_BSSRDF_BURLEY_ID);
  70. }
  71. else if (method == u_principled) {
  72. alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_ID);
  73. }
  74. else if (method == u_random_walk) {
  75. alloc(sd, path_flag, weight, CLOSURE_BSSRDF_RANDOM_WALK_ID);
  76. }
  77. else if (method == u_principled_random_walk) {
  78. alloc(sd, path_flag, weight, CLOSURE_BSSRDF_PRINCIPLED_RANDOM_WALK_ID);
  79. }
  80. }
  81. void alloc(ShaderData *sd, int path_flag, float3 weight, ClosureType type)
  82. {
  83. Bssrdf *bssrdf = bssrdf_alloc(sd, weight);
  84. if (bssrdf) {
  85. /* disable in case of diffuse ancestor, can't see it well then and
  86. * adds considerably noise due to probabilities of continuing path
  87. * getting lower and lower */
  88. if (path_flag & PATH_RAY_DIFFUSE_ANCESTOR) {
  89. params.radius = make_float3(0.0f, 0.0f, 0.0f);
  90. }
  91. /* create one closure per color channel */
  92. bssrdf->radius = params.radius;
  93. bssrdf->albedo = params.albedo;
  94. bssrdf->texture_blur = params.texture_blur;
  95. bssrdf->sharpness = params.sharpness;
  96. bssrdf->N = params.N;
  97. bssrdf->roughness = params.roughness;
  98. sd->flag |= bssrdf_setup(sd, bssrdf, (ClosureType)type);
  99. }
  100. }
  101. };
  102. ClosureParam *closure_bssrdf_params()
  103. {
  104. static ClosureParam params[] = {
  105. CLOSURE_STRING_PARAM(CBSSRDFClosure, method),
  106. CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.N),
  107. CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.radius),
  108. CLOSURE_FLOAT3_PARAM(CBSSRDFClosure, params.albedo),
  109. CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.texture_blur, "texture_blur"),
  110. CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.sharpness, "sharpness"),
  111. CLOSURE_FLOAT_KEYPARAM(CBSSRDFClosure, params.roughness, "roughness"),
  112. CLOSURE_STRING_KEYPARAM(CBSSRDFClosure, label, "label"),
  113. CLOSURE_FINISH_PARAM(CBSSRDFClosure)};
  114. return params;
  115. }
  116. CCLOSURE_PREPARE(closure_bssrdf_prepare, CBSSRDFClosure)
  117. CCL_NAMESPACE_END