Blender V4.3
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/util/color.h"
11
13
14#ifdef __OSL__
15
16typedef struct PhongRampBsdf {
18
19 float exponent;
20 ccl_private float3 *colors;
21} PhongRampBsdf;
22
23static_assert(sizeof(ShaderClosure) >= sizeof(PhongRampBsdf), "PhongRampBsdf is too large!");
24
25ccl_device float3 bsdf_phong_ramp_get_color(const float3 colors[8], float pos)
26{
27 int MAXCOLORS = 8;
28
29 float npos = pos * (float)(MAXCOLORS - 1);
30 int ipos = float_to_int(npos);
31 if (ipos < 0)
32 return colors[0];
33 if (ipos >= (MAXCOLORS - 1))
34 return colors[MAXCOLORS - 1];
35 float offset = npos - (float)ipos;
36 return colors[ipos] * (1.0f - offset) + colors[ipos + 1] * offset;
37}
38
39ccl_device int bsdf_phong_ramp_setup(ccl_private PhongRampBsdf *bsdf)
40{
41 bsdf->type = CLOSURE_BSDF_PHONG_RAMP_ID;
42 bsdf->exponent = max(bsdf->exponent, 0.0f);
44}
45
46ccl_device Spectrum bsdf_phong_ramp_eval(ccl_private const ShaderClosure *sc,
47 const float3 wi,
48 const float3 wo,
49 ccl_private float *pdf)
50{
51 ccl_private const PhongRampBsdf *bsdf = (ccl_private const PhongRampBsdf *)sc;
52 float m_exponent = bsdf->exponent;
53 float cosNI = dot(bsdf->N, wi);
54 float cosNO = dot(bsdf->N, wo);
55
56 if (cosNI > 0 && cosNO > 0) {
57 // reflect the view vector
58 float3 R = (2 * cosNI) * bsdf->N - wi;
59 float cosRO = dot(R, wo);
60 if (cosRO > 0) {
61 float cosp = powf(cosRO, m_exponent);
62 float common = 0.5f * M_1_PI_F * cosp;
63 float out = cosNO * (m_exponent + 2) * common;
64 *pdf = (m_exponent + 1) * common;
65 return rgb_to_spectrum(bsdf_phong_ramp_get_color(bsdf->colors, cosp) * out);
66 }
67 }
68 *pdf = 0.0f;
69 return zero_spectrum();
70}
71
72ccl_device_inline float phong_ramp_exponent_to_roughness(float exponent)
73{
74 return sqrt(1.0f / ((exponent + 2.0f) / 2.0f));
75}
76
77ccl_device int bsdf_phong_ramp_sample(ccl_private const ShaderClosure *sc,
78 float3 Ng,
79 float3 wi,
80 const float2 rand,
83 ccl_private float *pdf,
84 ccl_private float2 *sampled_roughness)
85{
86 ccl_private const PhongRampBsdf *bsdf = (ccl_private const PhongRampBsdf *)sc;
87 float cosNI = dot(bsdf->N, wi);
88 float m_exponent = bsdf->exponent;
89 const float m_roughness = phong_ramp_exponent_to_roughness(m_exponent);
90 *sampled_roughness = make_float2(m_roughness, m_roughness);
91
92 if (cosNI > 0) {
93 // reflect the view vector
94 float3 R = (2 * cosNI) * bsdf->N - wi;
95 float3 T, B;
96 make_orthonormals(R, &T, &B);
97 float phi = M_2PI_F * rand.x;
98 float cosTheta = powf(rand.y, 1 / (m_exponent + 1));
99 float sinTheta2 = 1 - cosTheta * cosTheta;
100 float sinTheta = sinTheta2 > 0 ? sqrtf(sinTheta2) : 0;
101 *wo = (cosf(phi) * sinTheta) * T + (sinf(phi) * sinTheta) * B + (cosTheta)*R;
102 if (dot(Ng, *wo) > 0.0f) {
103 // common terms for pdf and eval
104 float cosNO = dot(bsdf->N, *wo);
105 // make sure the direction we chose is still in the right hemisphere
106 if (cosNO > 0) {
107 float cosp = powf(cosTheta, m_exponent);
108 float common = 0.5f * M_1_PI_F * cosp;
109 *pdf = (m_exponent + 1) * common;
110 float out = cosNO * (m_exponent + 2) * common;
111 *eval = rgb_to_spectrum(bsdf_phong_ramp_get_color(bsdf->colors, cosp) * out);
112 }
113 }
114 }
115 else {
116 *eval = zero_spectrum();
117 *pdf = 0.0f;
118 }
120}
121
122#endif /* __OSL__ */
123
sqrt(x)+1/max(0
additional_info("compositor_sum_squared_difference_float_shared") .push_constant(Type output_img float dot(value.rgb, luminance_coefficients)") .define("LOAD(value)"
#define sinf(x)
#define cosf(x)
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define powf(x, y)
#define CCL_NAMESPACE_END
ccl_device_forceinline float2 make_float2(const float x, const float y)
#define sqrtf(x)
draw_view in_light_buf[] float
@ CLOSURE_BSDF_PHONG_RAMP_ID
@ SD_BSDF_HAS_EVAL
@ SD_BSDF
#define SHADER_CLOSURE_BASE
@ LABEL_GLOSSY
@ LABEL_REFLECT
ShaderClosure
ccl_device_inline Spectrum rgb_to_spectrum(float3 rgb)
#define T
#define B
#define R
#define M_2PI_F
Definition sky_float3.h:23
float x
float y
float max
#define zero_spectrum
SPECTRUM_DATA_TYPE Spectrum
ccl_device_inline int float_to_int(float f)
Definition util/math.h:424
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
Definition util/math.h:593