|
| 1 | +// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O. |
| 2 | +// This file is part of the "Nabla Engine". |
| 3 | +// For conditions of distribution and use, see copyright notice in nabla.h |
| 4 | +#ifndef _NBL_BUILTIN_HLSL_BXDF_BASE_OREN_NAYAR_INCLUDED_ |
| 5 | +#define _NBL_BUILTIN_HLSL_BXDF_BASE_OREN_NAYAR_INCLUDED_ |
| 6 | + |
| 7 | +#include "nbl/builtin/hlsl/bxdf/common.hlsl" |
| 8 | +#include "nbl/builtin/hlsl/bxdf/config.hlsl" |
| 9 | +#include "nbl/builtin/hlsl/sampling/cos_weighted_spheres.hlsl" |
| 10 | + |
| 11 | +namespace nbl |
| 12 | +{ |
| 13 | +namespace hlsl |
| 14 | +{ |
| 15 | +namespace bxdf |
| 16 | +{ |
| 17 | +namespace base |
| 18 | +{ |
| 19 | + |
| 20 | +template<class Config, bool IsBSDF NBL_PRIMARY_REQUIRES(config_concepts::BasicConfiguration<Config>) |
| 21 | +struct SOrenNayarBase |
| 22 | +{ |
| 23 | + using this_t = SOrenNayarBase<Config, IsBSDF>; |
| 24 | + BXDF_CONFIG_TYPE_ALIASES(Config); |
| 25 | + |
| 26 | + NBL_CONSTEXPR_STATIC_INLINE BxDFClampMode _clamp = conditional_value<IsBSDF, BxDFClampMode, BxDFClampMode::BCM_ABS, BxDFClampMode::BCM_MAX>::value; |
| 27 | + |
| 28 | + struct SCreationParams |
| 29 | + { |
| 30 | + scalar_type A; |
| 31 | + }; |
| 32 | + using creation_type = SCreationParams; |
| 33 | + |
| 34 | + struct SQuery |
| 35 | + { |
| 36 | + scalar_type getVdotL() NBL_CONST_MEMBER_FUNC { return VdotL; } |
| 37 | + scalar_type VdotL; |
| 38 | + }; |
| 39 | + |
| 40 | + static this_t create(NBL_CONST_REF_ARG(creation_type) params) |
| 41 | + { |
| 42 | + this_t retval; |
| 43 | + retval.A2 = params.A * 0.5; |
| 44 | + retval.AB = vector2_type(1.0, 0.0) + vector2_type(-0.5, 0.45) * vector2_type(retval.A2, retval.A2) / vector2_type(retval.A2 + 0.33, retval.A2 + 0.09); |
| 45 | + return retval; |
| 46 | + } |
| 47 | + |
| 48 | + scalar_type __rec_pi_factored_out_wo_clamps(scalar_type VdotL, scalar_type clampedNdotL, scalar_type clampedNdotV) |
| 49 | + { |
| 50 | + scalar_type C = 1.0 / max<scalar_type>(clampedNdotL, clampedNdotV); |
| 51 | + scalar_type cos_phi_sin_theta = max<scalar_type>(VdotL - clampedNdotL * clampedNdotV, 0.0); |
| 52 | + return (AB.x + AB.y * cos_phi_sin_theta * C); |
| 53 | + } |
| 54 | + template<typename Query> |
| 55 | + spectral_type __eval(NBL_CONST_REF_ARG(Query) query, NBL_CONST_REF_ARG(sample_type) _sample, NBL_CONST_REF_ARG(isotropic_interaction_type) interaction) |
| 56 | + { |
| 57 | + scalar_type NdotL = _sample.getNdotL(_clamp); |
| 58 | + return hlsl::promote<spectral_type>(NdotL * numbers::inv_pi<scalar_type> * hlsl::mix(1.0, 0.5, IsBSDF) * __rec_pi_factored_out_wo_clamps(query.getVdotL(), NdotL, interaction.getNdotV(_clamp))); |
| 59 | + } |
| 60 | + |
| 61 | + spectral_type eval(NBL_CONST_REF_ARG(sample_type) _sample, NBL_CONST_REF_ARG(isotropic_interaction_type) interaction) |
| 62 | + { |
| 63 | + SQuery query; |
| 64 | + query.VdotL = hlsl::dot(interaction.getV().getDirection(), _sample.getL().getDirection()); |
| 65 | + return __eval<SQuery>(query, _sample, interaction); |
| 66 | + } |
| 67 | + |
| 68 | + sample_type generate(NBL_CONST_REF_ARG(anisotropic_interaction_type) interaction, const vector2_type u) |
| 69 | + { |
| 70 | + // static_assert(!IsBSDF); |
| 71 | + ray_dir_info_type L; |
| 72 | + L.direction = sampling::ProjectedHemisphere<scalar_type>::generate(u); |
| 73 | + return sample_type::createFromTangentSpace(L, interaction.getFromTangentSpace()); |
| 74 | + } |
| 75 | + |
| 76 | + sample_type generate(NBL_CONST_REF_ARG(anisotropic_interaction_type) interaction, const vector3_type u) |
| 77 | + { |
| 78 | + // static_assert(IsBSDF); |
| 79 | + ray_dir_info_type L; |
| 80 | + L.direction = sampling::ProjectedSphere<scalar_type>::generate(u); |
| 81 | + return sample_type::createFromTangentSpace(L, interaction.getFromTangentSpace()); |
| 82 | + } |
| 83 | + |
| 84 | + scalar_type pdf(NBL_CONST_REF_ARG(sample_type) _sample) |
| 85 | + { |
| 86 | + if (IsBSDF) |
| 87 | + return sampling::ProjectedSphere<scalar_type>::pdf(_sample.getNdotL(_clamp)); |
| 88 | + else |
| 89 | + return sampling::ProjectedHemisphere<scalar_type>::pdf(_sample.getNdotL(_clamp)); |
| 90 | + } |
| 91 | + |
| 92 | + template<typename Query> |
| 93 | + quotient_pdf_type __quotient_and_pdf(NBL_CONST_REF_ARG(Query) query, NBL_CONST_REF_ARG(sample_type) _sample, NBL_CONST_REF_ARG(isotropic_interaction_type) interaction) |
| 94 | + { |
| 95 | + scalar_type _pdf = pdf(_sample); |
| 96 | + scalar_type q = __rec_pi_factored_out_wo_clamps(query.getVdotL(), _sample.getNdotL(_clamp), interaction.getNdotV(_clamp)); |
| 97 | + return quotient_pdf_type::create(q, _pdf); |
| 98 | + } |
| 99 | + quotient_pdf_type quotient_and_pdf(NBL_CONST_REF_ARG(sample_type) _sample, NBL_CONST_REF_ARG(isotropic_interaction_type) interaction) |
| 100 | + { |
| 101 | + SQuery query; |
| 102 | + query.VdotL = hlsl::dot(interaction.getV().getDirection(), _sample.getL().getDirection()); |
| 103 | + return __quotient_and_pdf<SQuery>(query, _sample, interaction); |
| 104 | + } |
| 105 | + |
| 106 | + scalar_type A2; |
| 107 | + vector2_type AB; |
| 108 | +}; |
| 109 | + |
| 110 | +} |
| 111 | +} |
| 112 | +} |
| 113 | +} |
| 114 | + |
| 115 | +#endif |
0 commit comments