Blender V5.0
bsdf_diffuse_ramp.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
2 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Adapted code from Open Shading Language. */
7
8#pragma once
9
10#include "kernel/types.h"
11
14
16
17#ifdef __OSL__
18
19struct DiffuseRampBsdf {
21
22 ccl_private float3 *colors;
23};
24
25static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseRampBsdf), "DiffuseRampBsdf is too large!");
26
27ccl_device float3 bsdf_diffuse_ramp_get_color(const float3 colors[8], float pos)
28{
29 const int MAXCOLORS = 8;
30
31 const float npos = pos * (float)(MAXCOLORS - 1);
32 const int ipos = float_to_int(npos);
33 if (ipos < 0) {
34 return colors[0];
35 }
36 if (ipos >= (MAXCOLORS - 1)) {
37 return colors[MAXCOLORS - 1];
38 }
39 const float offset = npos - (float)ipos;
40 return colors[ipos] * (1.0f - offset) + colors[ipos + 1] * offset;
41}
42
43ccl_device int bsdf_diffuse_ramp_setup(DiffuseRampBsdf *bsdf)
44{
47}
48
49ccl_device void bsdf_diffuse_ramp_blur(ccl_private ShaderClosure * /*sc*/,
50 const float /*roughness*/)
51{
52}
53
54ccl_device Spectrum bsdf_diffuse_ramp_eval(const ccl_private ShaderClosure *sc,
55 const float3 /*wi*/,
56 const float3 wo,
57 ccl_private float *pdf)
58{
59 const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
60 const float3 N = bsdf->N;
61
62 const float cosNO = fmaxf(dot(N, wo), 0.0f);
63 if (cosNO >= 0.0f) {
64 *pdf = cosNO * M_1_PI_F;
65 return rgb_to_spectrum(bsdf_diffuse_ramp_get_color(bsdf->colors, cosNO) * M_1_PI_F);
66 }
67 *pdf = 0.0f;
68 return zero_spectrum();
69}
70
71ccl_device int bsdf_diffuse_ramp_sample(const ccl_private ShaderClosure *sc,
72 const float3 Ng,
73 const float3 /*wi*/,
74 const float2 rand,
77 ccl_private float *pdf)
78{
79 const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
80 const float3 N = bsdf->N;
81
82 // distribution over the hemisphere
83 sample_cos_hemisphere(N, rand, wo, pdf);
84
85 if (dot(Ng, *wo) > 0.0f) {
86 *eval = rgb_to_spectrum(bsdf_diffuse_ramp_get_color(bsdf->colors, *pdf * M_PI_F) * M_1_PI_F);
87 }
88 else {
89 *pdf = 0.0f;
90 *eval = zero_spectrum();
91 }
93}
94
95#endif /* __OSL__ */
96
nullptr float
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define SHADER_CLOSURE_BASE
#define zero_spectrum
#define ccl_private
#define M_1_PI_F
#define CCL_NAMESPACE_END
uint pos
VecBase< float, 3 > float3
@ CLOSURE_BSDF_DIFFUSE_RAMP_ID
@ SD_BSDF_HAS_EVAL
@ SD_BSDF
@ LABEL_DIFFUSE
@ LABEL_REFLECT
ccl_device_inline Spectrum rgb_to_spectrum(const float3 rgb)
ccl_device_inline int float_to_int(const float f)
Definition math_base.h:407
#define N
#define ccl_device
#define fmaxf
#define M_PI_F
ccl_device_inline void sample_cos_hemisphere(const float3 N, const float2 rand_in, ccl_private float3 *wo, ccl_private float *pdf)
float3 Spectrum