Blender V4.5
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, const float roughness) {}
50
51ccl_device Spectrum bsdf_diffuse_ramp_eval(const ccl_private ShaderClosure *sc,
52 const float3 wi,
53 const float3 wo,
54 ccl_private float *pdf)
55{
56 const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
57 const float3 N = bsdf->N;
58
59 const float cosNO = fmaxf(dot(N, wo), 0.0f);
60 if (cosNO >= 0.0f) {
61 *pdf = cosNO * M_1_PI_F;
62 return rgb_to_spectrum(bsdf_diffuse_ramp_get_color(bsdf->colors, cosNO) * M_1_PI_F);
63 }
64 *pdf = 0.0f;
65 return zero_spectrum();
66}
67
68ccl_device int bsdf_diffuse_ramp_sample(const ccl_private ShaderClosure *sc,
69 const float3 Ng,
70 const float3 wi,
71 const float2 rand,
74 ccl_private float *pdf)
75{
76 const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
77 const float3 N = bsdf->N;
78
79 // distribution over the hemisphere
80 sample_cos_hemisphere(N, rand, wo, pdf);
81
82 if (dot(Ng, *wo) > 0.0f) {
83 *eval = rgb_to_spectrum(bsdf_diffuse_ramp_get_color(bsdf->colors, *pdf * M_PI_F) * M_1_PI_F);
84 }
85 else {
86 *pdf = 0.0f;
87 *eval = zero_spectrum();
88 }
90}
91
92#endif /* __OSL__ */
93
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
#define SHADER_CLOSURE_BASE
#define ccl_device
#define zero_spectrum
#define ccl_private
#define M_1_PI_F
#define M_PI_F
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
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
ccl_device_inline void sample_cos_hemisphere(const float3 N, const float2 rand_in, ccl_private float3 *wo, ccl_private float *pdf)
float3 Spectrum