|
| 1 | +// Copyright Contributors to the Open Shading Language project. |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | +// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage |
| 4 | + |
| 5 | + |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <BSDL/MTX/bsdf_conductor_decl.h> |
| 9 | + |
| 10 | +BSDL_ENTER_NAMESPACE |
| 11 | + |
| 12 | +namespace mtx { |
| 13 | + |
| 14 | +BSDL_INLINE_METHOD |
| 15 | +ConductorFresnel::ConductorFresnel(Power IOR, Power extinction, float lambda_0) |
| 16 | + : IOR(IOR), extinction(extinction), lambda_0(lambda_0) |
| 17 | +{ |
| 18 | +} |
| 19 | + |
| 20 | +BSDL_INLINE_METHOD Power |
| 21 | +ConductorFresnel::eval(float cos_theta) const |
| 22 | +{ |
| 23 | + cos_theta = CLAMP(cos_theta, 0.0f, 1.0f); |
| 24 | + const Power one(1, lambda_0); |
| 25 | + const Power cosTheta2(cos_theta * cos_theta, lambda_0); |
| 26 | + const Power sinTheta2 = one - cosTheta2; |
| 27 | + const Power& n = IOR; |
| 28 | + const Power& k = extinction; |
| 29 | + const Power n2 = n * n; |
| 30 | + const Power k2 = k * k; |
| 31 | + const Power t0 = n2 - k2 - sinTheta2; |
| 32 | + const Power a2plusb2 = sqrt(t0 * t0 + 4 * n2 * k2); |
| 33 | + const Power t1 = a2plusb2 + cosTheta2; |
| 34 | + const Power a = sqrt(0.5f * (a2plusb2 + t0)); |
| 35 | + const Power t2 = (2.0f * cos_theta) * a; |
| 36 | + const Power rs = (t1 - t2) / (t1 + t2).clamped(FLOAT_MIN, BIG); |
| 37 | + |
| 38 | + const Power t3 = cosTheta2 * a2plusb2 + sinTheta2 * sinTheta2; |
| 39 | + const Power t4 = t2 * sinTheta2; |
| 40 | + const Power rp = rs * (t3 - t4) / (t3 + t4).clamped(FLOAT_MIN, BIG); |
| 41 | + |
| 42 | + return 0.5f * (rp + rs).clamped(0, 2); |
| 43 | +} |
| 44 | + |
| 45 | +BSDL_INLINE_METHOD Power |
| 46 | +ConductorFresnel::F0() const |
| 47 | +{ |
| 48 | + const Power one(1, lambda_0); |
| 49 | + const Power& n = IOR; |
| 50 | + const Power& k = extinction; |
| 51 | + const Power n2 = n * n; |
| 52 | + const Power k2 = k * k; |
| 53 | + const Power t0 = n2 - k2; |
| 54 | + const Power a2plusb2 = sqrt(t0 * t0 + 4 * n2 * k2); |
| 55 | + const Power t1 = a2plusb2 + one; |
| 56 | + const Power a = sqrt(0.5f * (a2plusb2 + t0)); |
| 57 | + const Power t2 = 2.0f * a; |
| 58 | + const Power rs = (t1 - t2) / (t1 + t2).clamped(FLOAT_MIN, BIG); |
| 59 | + |
| 60 | + return rs.clamped(0, 1); |
| 61 | +} |
| 62 | + |
| 63 | +BSDL_INLINE_METHOD Power |
| 64 | +ConductorFresnel::avg() const |
| 65 | +{ |
| 66 | + return Power( |
| 67 | + [&](int i) { |
| 68 | + // Very simple fit for cosine weighted average fresnel. Not very |
| 69 | + // accurate but enough for albedo based sampling decisions. |
| 70 | + constexpr float a = -0.32775145f, b = 0.18346033f, c = 0.61146583f, |
| 71 | + d = -0.07785134f; |
| 72 | + const float x = IOR[i], y = extinction[i]; |
| 73 | + const float p = a + b * x + c * y + d * x * y; |
| 74 | + return p / (1 + p); |
| 75 | + }, |
| 76 | + lambda_0); |
| 77 | +} |
| 78 | + |
| 79 | +template<typename BSDF_ROOT> |
| 80 | +template<typename T> |
| 81 | +BSDL_INLINE_METHOD |
| 82 | +ConductorLobe<BSDF_ROOT>::ConductorLobe(T* lobe, const BsdfGlobals& globals, |
| 83 | + const Data& data) |
| 84 | + : Base(lobe, globals.visible_normal(data.N), data.U, 0.0f, globals.lambda_0, |
| 85 | + false) |
| 86 | +{ |
| 87 | + Base::sample_filter = globals.get_sample_filter(Base::frame.Z, true); |
| 88 | + // MaterialX expects the raw x/y roughness as input, but for albedo tables it |
| 89 | + // is better to use the roughness/anisotropy parametrization so we can |
| 90 | + // ignore roughness |
| 91 | + const float rx = CLAMP(data.roughness_x, EPSILON, 2.0f); |
| 92 | + const float ry = CLAMP(data.roughness_y, EPSILON, 2.0f); |
| 93 | + const float ax = std::max(rx, ry); |
| 94 | + const float ay = std::min(rx, ry); |
| 95 | + const float b = ay / ax; |
| 96 | + const float aniso = (1 - b) / (1 + b); |
| 97 | + // Also assume we square the roughness for linearity |
| 98 | + const float roughness = globals.regularize_roughness( |
| 99 | + sqrtf(ax / (1 + aniso))); |
| 100 | + const float cosNO = Base::frame.Z.dot(globals.wo); |
| 101 | + // Flip aniso if roughness_x < roughness_y |
| 102 | + dist = GGXDist(roughness, aniso, (rx < ry)); |
| 103 | + fresnel = ConductorFresnel(globals.wave(data.IOR), |
| 104 | + globals.wave(data.extinction), globals.lambda_0); |
| 105 | + TabulatedEnergyCurve<spi::MiniMicrofacetGGX> curve(roughness, 0.0f); |
| 106 | + E_ms = curve.Emiss_eval(cosNO); |
| 107 | + Base::set_roughness(roughness); |
| 108 | +} |
| 109 | + |
| 110 | +template<typename BSDF_ROOT> |
| 111 | +BSDL_INLINE_METHOD Sample |
| 112 | +ConductorLobe<BSDF_ROOT>::eval_impl(const Imath::V3f& wo, |
| 113 | + const Imath::V3f& wi) const |
| 114 | +{ |
| 115 | + Sample s = eval_turquin_microms_reflection(dist, fresnel, E_ms, wo, wi); |
| 116 | + s.roughness = Base::roughness(); |
| 117 | + return s; |
| 118 | +} |
| 119 | + |
| 120 | +template<typename BSDF_ROOT> |
| 121 | +BSDL_INLINE_METHOD Sample |
| 122 | +ConductorLobe<BSDF_ROOT>::sample_impl(const Imath::V3f& wo, |
| 123 | + const Imath::V3f& rnd) const |
| 124 | +{ |
| 125 | + const float cosNO = wo.z; |
| 126 | + if (cosNO <= 0) |
| 127 | + return {}; |
| 128 | + |
| 129 | + // sample microfacet (half vector) |
| 130 | + // generate outgoing direction |
| 131 | + Imath::V3f wi = reflect(wo, dist.sample(wo, rnd.x, rnd.y)); |
| 132 | + // evaluate brdf on outgoing direction |
| 133 | + return eval_impl(wo, wi); |
| 134 | +} |
| 135 | + |
| 136 | +} // namespace mtx |
| 137 | + |
| 138 | +BSDL_LEAVE_NAMESPACE |
0 commit comments