osl_closures.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef __OSL_CLOSURES_H__
  33. #define __OSL_CLOSURES_H__
  34. #include "util/util_types.h"
  35. #include "kernel/kernel_types.h"
  36. #include <OSL/oslclosure.h>
  37. #include <OSL/oslexec.h>
  38. #include <OSL/genclosure.h>
  39. CCL_NAMESPACE_BEGIN
  40. OSL::ClosureParam *closure_emission_params();
  41. OSL::ClosureParam *closure_background_params();
  42. OSL::ClosureParam *closure_holdout_params();
  43. OSL::ClosureParam *closure_bsdf_diffuse_ramp_params();
  44. OSL::ClosureParam *closure_bsdf_phong_ramp_params();
  45. OSL::ClosureParam *closure_bsdf_transparent_params();
  46. OSL::ClosureParam *closure_bssrdf_params();
  47. OSL::ClosureParam *closure_absorption_params();
  48. OSL::ClosureParam *closure_henyey_greenstein_params();
  49. OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_params();
  50. OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_params();
  51. OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_params();
  52. OSL::ClosureParam *closure_bsdf_microfacet_ggx_fresnel_params();
  53. OSL::ClosureParam *closure_bsdf_microfacet_ggx_aniso_fresnel_params();
  54. OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_fresnel_params();
  55. OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_fresnel_params();
  56. OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_fresnel_params();
  57. OSL::ClosureParam *closure_bsdf_principled_clearcoat_params();
  58. void closure_emission_prepare(OSL::RendererServices *, int id, void *data);
  59. void closure_background_prepare(OSL::RendererServices *, int id, void *data);
  60. void closure_holdout_prepare(OSL::RendererServices *, int id, void *data);
  61. void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
  62. void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
  63. void closure_bsdf_transparent_prepare(OSL::RendererServices *, int id, void *data);
  64. void closure_bssrdf_prepare(OSL::RendererServices *, int id, void *data);
  65. void closure_absorption_prepare(OSL::RendererServices *, int id, void *data);
  66. void closure_henyey_greenstein_prepare(OSL::RendererServices *, int id, void *data);
  67. void closure_bsdf_microfacet_multi_ggx_prepare(OSL::RendererServices *, int id, void *data);
  68. void closure_bsdf_microfacet_multi_ggx_glass_prepare(OSL::RendererServices *, int id, void *data);
  69. void closure_bsdf_microfacet_multi_ggx_aniso_prepare(OSL::RendererServices *, int id, void *data);
  70. void closure_bsdf_microfacet_ggx_fresnel_prepare(OSL::RendererServices *, int id, void *data);
  71. void closure_bsdf_microfacet_ggx_aniso_fresnel_prepare(OSL::RendererServices *,
  72. int id,
  73. void *data);
  74. void closure_bsdf_microfacet_multi_ggx_fresnel_prepare(OSL::RendererServices *,
  75. int id,
  76. void *data);
  77. void closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare(OSL::RendererServices *,
  78. int id,
  79. void *data);
  80. void closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare(OSL::RendererServices *,
  81. int id,
  82. void *data);
  83. void closure_bsdf_principled_clearcoat_prepare(OSL::RendererServices *, int id, void *data);
  84. void closure_bsdf_principled_hair_prepare(OSL::RendererServices *, int id, void *data);
  85. #define CCLOSURE_PREPARE(name, classname) \
  86. void name(RendererServices *, int id, void *data) \
  87. { \
  88. memset(data, 0, sizeof(classname)); \
  89. new (data) classname(); \
  90. }
  91. #define CCLOSURE_PREPARE_STATIC(name, classname) static CCLOSURE_PREPARE(name, classname)
  92. #define CLOSURE_FLOAT3_PARAM(st, fld) \
  93. { \
  94. TypeDesc::TypeVector, (int)reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) \
  95. }
  96. #define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
  97. #define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
  98. #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
  99. /* Closure */
  100. class CClosurePrimitive {
  101. public:
  102. virtual void setup(ShaderData *sd, int path_flag, float3 weight) = 0;
  103. OSL::ustring label;
  104. };
  105. /* BSDF */
  106. class CBSDFClosure : public CClosurePrimitive {
  107. public:
  108. bool skip(const ShaderData *sd, int path_flag, int scattering);
  109. };
  110. #define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, structname, TYPE) \
  111. \
  112. class Upper##Closure : public CBSDFClosure { \
  113. public: \
  114. structname params; \
  115. float3 unused; \
  116. \
  117. void setup(ShaderData *sd, int path_flag, float3 weight) \
  118. { \
  119. if (!skip(sd, path_flag, TYPE)) { \
  120. structname *bsdf = (structname *)bsdf_alloc_osl(sd, sizeof(structname), weight, &params); \
  121. sd->flag |= (bsdf) ? bsdf_##lower##_setup(bsdf) : 0; \
  122. } \
  123. } \
  124. }; \
  125. \
  126. static ClosureParam *bsdf_##lower##_params() \
  127. { \
  128. static ClosureParam params[] = {
  129. /* parameters */
  130. #define BSDF_CLOSURE_CLASS_END(Upper, lower) \
  131. CLOSURE_STRING_KEYPARAM(Upper##Closure, label, "label"), CLOSURE_FINISH_PARAM(Upper##Closure) \
  132. } \
  133. ; \
  134. return params; \
  135. } \
  136. \
  137. CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
  138. CCL_NAMESPACE_END
  139. #endif /* __OSL_CLOSURES_H__ */