Blender V5.3
bsdf_sheen.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7/* Shading model by Tizian Zeltner, Brent Burley, Matt Jen-Yuan Chiang:
8 * "Practical Multiple-Scattering Sheen Using Linearly Transformed Cosines" (2022)
9 * https://tizianzeltner.com/projects/Zeltner2022Practical/
10 */
11
14
15#include "kernel/constants.h"
17
19
26
27static_assert(sizeof(ShaderClosure) >= sizeof(SheenBsdf), "SheenBsdf is too large!");
28
29/* Set up sheen BSDF, and return the closure albedo for layering. */
31 ccl_private ShaderData *sd,
32 const Spectrum weight,
33 const float3 N,
34 const float roughness)
35{
38
39 if (!bsdf) {
40 return zero_spectrum();
41 }
42
43 bsdf->type = CLOSURE_BSDF_SHEEN_ID;
44 bsdf->N = N;
45 bsdf->roughness = clamp(roughness, 1e-3f, 1.0f);
46 make_orthonormals_safe_tangent(bsdf->N, sd->wi, &bsdf->T, &bsdf->B);
47 const float cosNI = dot(bsdf->N, sd->wi);
48
49 const int offset = kernel_data.tables.sheen_ltc;
50 bsdf->transformA = lookup_table_read_2D(
51 kg, cosNI, bsdf->roughness, offset, SHEEN_RES_MU, SHEEN_RES_ROUGH);
52 bsdf->transformB = lookup_table_read_2D(kg,
53 cosNI,
54 bsdf->roughness,
58 const float albedo = lookup_table_read_2D(kg,
59 cosNI,
60 bsdf->roughness,
61 offset + 2 * SHEEN_RES_MU * SHEEN_RES_ROUGH,
64
65 /* If the given roughness and angle result in an invalid LTC, skip the closure. */
66 if (fabsf(bsdf->transformA) < 1e-5f || albedo < 1e-5f) {
67 bsdf->type = CLOSURE_NONE_ID;
68 bsdf->sample_weight = 0.0f;
69 return zero_spectrum();
70 }
71
72 bsdf->weight *= albedo;
73 bsdf->sample_weight *= albedo;
74 if (bsdf != &sheen) {
75 /* Add flag only if the closure is actually allocated in `sd->closure`. */
76 sd->runtime_flag |= (SR_BSDF | SR_BSDF_HAS_EVAL);
77 }
78
79 /* Sheen do not tint the base. */
80 /* NOTE(OpenPBR): spec v1.1.1 mentions the albedo-scaling for sheen is explicitly modified to not
81 * tint the base, but it is unclear how to remove the tint, so we use `reduce_max` to keep
82 * compatibility. */
83 return make_spectrum(reduce_max(bsdf->weight));
84}
85
87 const float3 /*wi*/,
88 const float3 wo,
89 ccl_private float *pdf)
90{
91 const ccl_private SheenBsdf *bsdf = (const ccl_private SheenBsdf *)sc;
92 const float3 N = bsdf->N;
93 const float3 T = bsdf->T;
94 const float3 B = bsdf->B;
95 const float a = bsdf->transformA;
96 const float b = bsdf->transformB;
97
98 const float3 localO = to_local(wo, T, B, N);
99
100 const float lenSqr = sqr(a * localO.x + b * localO.z) + sqr(a * localO.y) + sqr(localO.z);
101 const float val = M_1_PI_F * fmaxf(localO.z, 0.0f) * sqr(a / lenSqr);
102
103 *pdf = val;
104 return make_spectrum(val);
105}
106
107ccl_device int bsdf_sheen_sample(const ccl_private ShaderClosure *sc,
108 const float3 Ng,
109 const float3 /*wi*/,
110 const float2 rand,
111 ccl_private Spectrum *eval,
113 ccl_private float *pdf)
114{
115 const ccl_private SheenBsdf *bsdf = (const ccl_private SheenBsdf *)sc;
116 const float3 N = bsdf->N;
117 const float3 T = bsdf->T;
118 const float3 B = bsdf->B;
119 const float a = bsdf->transformA;
120 const float b = bsdf->transformB;
121
122 const float2 disk = sample_uniform_disk(rand);
123 const float diskZ = safe_sqrtf(1.0f - dot(disk, disk));
124 const float3 localO = normalize(make_float3((disk.x - diskZ * b), disk.y, diskZ * a));
125
126 *wo = to_global(localO, T, B, N);
127
128 if (dot(Ng, *wo) <= 0) {
129 *eval = zero_spectrum();
130 *pdf = 0.0f;
132 }
133
134 const float lenSqr = sqr(a * localO.x + b * localO.z) + sqr(a * localO.y) + sqr(localO.z);
135 const float val = M_1_PI_F * localO.z * sqr(a / lenSqr);
136 *pdf = val;
137 *eval = make_spectrum(val);
138
140}
141
ccl_device_inline ccl_private Bsdf * bsdf_alloc_maybe_emission(ccl_private ShaderData *sd, ccl_private Bsdf *bsdf, Spectrum weight)
Definition alloc.h:97
ccl_device Spectrum bsdf_sheen_setup(KernelGlobals kg, ccl_private ShaderData *sd, const Spectrum weight, const float3 N, const float roughness)
Definition bsdf_sheen.h:30
ccl_device int bsdf_sheen_sample(const ccl_private ShaderClosure *sc, const float3 Ng, const float3, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf)
Definition bsdf_sheen.h:107
ccl_device Spectrum bsdf_sheen_eval(const ccl_private ShaderClosure *sc, const float3, const float3 wo, ccl_private float *pdf)
Definition bsdf_sheen.h:86
#define SHEEN_RES_MU
Definition constants.h:25
#define SHEEN_RES_ROUGH
Definition constants.h:24
#define kernel_data
#define zero_spectrum
#define make_spectrum(f)
#define ccl_private
#define M_1_PI_F
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float reduce_max(float2 a)
@ CLOSURE_BSDF_SHEEN_ID
@ CLOSURE_NONE_ID
@ SR_BSDF_HAS_EVAL
@ SR_BSDF
@ LABEL_DIFFUSE
@ LABEL_REFLECT
ccl_device float lookup_table_read_2D(KernelGlobals kg, const float x, float y, const int offset, const int xsize, const int ysize)
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
#define N
#define T
#define B
#define sqr
#define fabsf
#define ccl_device
#define fmaxf
CCL_NAMESPACE_BEGIN ccl_device float2 sample_uniform_disk(const float2 rand)
ccl_device void make_orthonormals_safe_tangent(const float3 N, const float3 T, ccl_private float3 *a, ccl_private float3 *b)
closure color sheen(normal N, float roughness)
Definition stdcycles.h:96
float roughness
Definition bsdf_sheen.h:22
float transformA
Definition bsdf_sheen.h:23
float3 T
Definition bsdf_sheen.h:24
float3 B
Definition bsdf_sheen.h:24
float transformB
Definition bsdf_sheen.h:23
SHADER_CLOSURE_BASE
Definition bsdf_sheen.h:21
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
ccl_device_inline float2 to_local(const T p, const T X, const T Y)
Definition transform.h:33
CCL_NAMESPACE_BEGIN ccl_device_inline T to_global(const float2 p, const T X, const T Y)
Definition transform.h:21
float3 Spectrum