Blender V5.0
bsdf_phong_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
13
15
16#ifdef __OSL__
17
18struct PhongRampBsdf {
20
21 float exponent;
22 ccl_private float3 *colors;
23};
24
25static_assert(sizeof(ShaderClosure) >= sizeof(PhongRampBsdf), "PhongRampBsdf is too large!");
26
27ccl_device float3 bsdf_phong_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_phong_ramp_setup(ccl_private PhongRampBsdf *bsdf)
44{
45 bsdf->type = CLOSURE_BSDF_PHONG_RAMP_ID;
46 bsdf->exponent = max(bsdf->exponent, 0.0f);
48}
49
50ccl_device Spectrum bsdf_phong_ramp_eval(const ccl_private ShaderClosure *sc,
51 const float3 wi,
52 const float3 wo,
53 ccl_private float *pdf)
54{
55 const ccl_private PhongRampBsdf *bsdf = (const ccl_private PhongRampBsdf *)sc;
56 const float m_exponent = bsdf->exponent;
57 const float cosNI = dot(bsdf->N, wi);
58 const float cosNO = dot(bsdf->N, wo);
59
60 if (cosNI > 0 && cosNO > 0) {
61 // reflect the view vector
62 const float3 R = (2 * cosNI) * bsdf->N - wi;
63 const float cosRO = dot(R, wo);
64 if (cosRO > 0) {
65 const float cosp = powf(cosRO, m_exponent);
66 const float common = 0.5f * M_1_PI_F * cosp;
67 const float out = cosNO * (m_exponent + 2) * common;
68 *pdf = (m_exponent + 1) * common;
69 return rgb_to_spectrum(bsdf_phong_ramp_get_color(bsdf->colors, cosp) * out);
70 }
71 }
72 *pdf = 0.0f;
73 return zero_spectrum();
74}
75
76ccl_device_inline float phong_ramp_exponent_to_roughness(const float exponent)
77{
78 return sqrt(1.0f / ((exponent + 2.0f) / 2.0f));
79}
80
81ccl_device int bsdf_phong_ramp_sample(const ccl_private ShaderClosure *sc,
82 const float3 Ng,
83 const float3 wi,
84 const float2 rand,
87 ccl_private float *pdf,
88 ccl_private float2 *sampled_roughness)
89{
90 const ccl_private PhongRampBsdf *bsdf = (const ccl_private PhongRampBsdf *)sc;
91 const float cosNI = dot(bsdf->N, wi);
92 const float m_exponent = bsdf->exponent;
93 const float m_roughness = phong_ramp_exponent_to_roughness(m_exponent);
94 *sampled_roughness = make_float2(m_roughness, m_roughness);
95
96 if (cosNI > 0) {
97 // reflect the view vector
98 const float3 R = (2 * cosNI) * bsdf->N - wi;
99 float3 T;
100 float3 B;
101 make_orthonormals(R, &T, &B);
102 const float phi = M_2PI_F * rand.x;
103 const float cosTheta = powf(rand.y, 1 / (m_exponent + 1));
104 *wo = to_global(spherical_cos_to_direction(cosTheta, phi), T, B, R);
105 if (dot(Ng, *wo) > 0.0f) {
106 // common terms for pdf and eval
107 const float cosNO = dot(bsdf->N, *wo);
108 // make sure the direction we chose is still in the right hemisphere
109 if (cosNO > 0) {
110 const float cosp = powf(cosTheta, m_exponent);
111 const float common = 0.5f * M_1_PI_F * cosp;
112 *pdf = (m_exponent + 1) * common;
113 const float out = cosNO * (m_exponent + 2) * common;
114 *eval = rgb_to_spectrum(bsdf_phong_ramp_get_color(bsdf->colors, cosp) * out);
115 }
116 }
117 }
118 else {
119 *eval = zero_spectrum();
120 *pdf = 0.0f;
121 }
123}
124
125#endif /* __OSL__ */
126
nullptr float
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
ccl_device float3 spherical_cos_to_direction(const float cos_theta, const float phi)
ccl_device_inline T to_global(const float2 p, const T X, const T Y)
#define SHADER_CLOSURE_BASE
#define zero_spectrum
#define ccl_private
#define ccl_device_inline
#define M_1_PI_F
#define powf(x, y)
#define CCL_NAMESPACE_END
uint pos
#define common
#define out
#define sqrt
VecBase< float, 3 > float3
@ CLOSURE_BSDF_PHONG_RAMP_ID
@ SD_BSDF_HAS_EVAL
@ SD_BSDF
@ LABEL_GLOSSY
@ 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
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define T
#define B
#define R
#define ccl_device
#define M_2PI_F
#define make_float2
float x
float y
max
Definition text_draw.cc:251
float3 Spectrum