Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bsdf_principled_sheen.h « closure « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5f4677cba3d4fb814fa16ae03fb2c46ee39d2bbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

#pragma once

/* DISNEY PRINCIPLED SHEEN BRDF
 *
 * Shading model by Brent Burley (Disney): "Physically Based Shading at Disney" (2012)
 */

#include "kernel/closure/bsdf_util.h"

CCL_NAMESPACE_BEGIN

typedef struct PrincipledSheenBsdf {
  SHADER_CLOSURE_BASE;
  float avg_value;
  float roughness;
} PrincipledSheenBsdf;

static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledSheenBsdf),
              "PrincipledSheenBsdf is too large!");

ccl_device_inline float calculate_avg_principled_sheen_brdf(float3 N, float3 I)
{
  /* To compute the average, we set the half-vector to the normal, resulting in
   * NdotI = NdotL = NdotV = LdotH */
  float NdotI = dot(N, I);
  if (NdotI < 0.0f) {
    return 0.0f;
  }

  return schlick_fresnel(NdotI) * NdotI;
}

ccl_device float3
calculate_principled_sheen_brdf(float3 N, float3 V, float3 L, float3 H, ccl_private float *pdf)
{
  float NdotL = dot(N, L);
  float NdotV = dot(N, V);

  if (NdotL < 0 || NdotV < 0) {
    *pdf = 0.0f;
    return make_float3(0.0f, 0.0f, 0.0f);
  }

  float LdotH = dot(L, H);

  float value = schlick_fresnel(LdotH) * NdotL;

  return make_float3(value, value, value);
}

/* Based on
 * https://dassaultsystemes-technology.github.io/EnterprisePBRShadingModel/spec-2022x.md.html#components/sheen.
 */
ccl_device_inline float sheen_v2_lambda(float mu, float w)
{
  float a = mix(11.9095f, 13.7000f, w);
  float b = mix(4.68753f, 2.92754f, w);
  float c = mix(0.33467f, 0.28670f, w);
  float d = mix(-2.22664f, -0.81757f, w);
  float e = mix(-1.76591f, -1.22466f, w);

  float exponent;
  if (mu < 0.5f) {
    exponent = a / (1 + b * powf(mu, c)) + d * mu + e;
  }
  else {
    exponent = 2 * a / (1 + b * exp2(-c)) - a / (1 + b * powf(1 - mu, c)) + d * mu + e;
  }
  return expf(exponent);
}

ccl_device_inline float3 sheen_v2_eval(float3 N, float3 V, float3 L, float3 H, float r, float *pdf)
{
  float cosNH = dot(N, H), cosNV = dot(N, V), cosNL = dot(N, L);

  if (cosNH < 0 || cosNV < 0 || cosNL < 0) {
    return zero_float3();
  }

  /* Evaluate microfacet distribution. */
  float sinTheta2 = 1 - sqr(cosNH);
  float invR = 1 / r;
  float D = M_1_2PI_F * (2 + invR) * powf(sinTheta2, 0.5f * invR);

  /* Evaluate shadowing-masking terms. */
  float w = -1.59612f / (1 + 0.20375f * fast_safe_powf(r, -0.55825f)) + 1.32805f;
  float lambdaV = sheen_v2_lambda(cosNV, w);
  float lambdaL = sheen_v2_lambda(cosNL, w);

  /* Soften shadow terminator. */
  lambdaL = fast_safe_powf(lambdaL, 1 + 2 * sqr(sqr(sqr(1 - cosNL))));

  /* Combined microfacet BSDF.
   * Usual form is F*D*G/(4*cosNL*cosNV), but here we have no Fresnel, we skip dividing by cosNL
   * since Cycles convention is returning BSDF*cosNL, and we use the combined shadowing-masking
   * term G=1/(1+lambdaV+lambdaL).
   */
  float val = D / (4 * cosNV * (1 + lambdaV + lambdaL));
  return make_float3(val, val, val);
}

ccl_device_forceinline float sheen_v2_E(KernelGlobals kg, float mu, float rough)
{
  return lookup_table_read_2D(kg, mu, 1 - sqrtf(rough), kernel_data.tables.sheen_E_offset, 32, 32);
}

ccl_device int bsdf_principled_sheen_setup(ccl_private const ShaderData *sd,
                                           ccl_private PrincipledSheenBsdf *bsdf)
{
  bsdf->type = CLOSURE_BSDF_PRINCIPLED_SHEEN_ID;
  bsdf->avg_value = calculate_avg_principled_sheen_brdf(bsdf->N, sd->I);
  bsdf->sample_weight *= bsdf->avg_value;
  return SD_BSDF | SD_BSDF_HAS_EVAL;
}

ccl_device int bsdf_principled_sheen_v2_setup(KernelGlobals kg,
                                              ccl_private const ShaderData *sd,
                                              ccl_private PrincipledSheenBsdf *bsdf)
{
  // TODO: Also expose as separate node. Add enum to Velvet BSDF maybe?
  bsdf->type = CLOSURE_BSDF_PRINCIPLED_SHEEN_V2_ID;

  bsdf->roughness = clamp(bsdf->roughness, 1e-3f, 1.0f);

  bsdf->avg_value = sheen_v2_E(kg, dot(bsdf->N, sd->I), bsdf->roughness);
  bsdf->sample_weight *= bsdf->avg_value;

  return SD_BSDF | SD_BSDF_HAS_EVAL;
}

ccl_device float3 bsdf_principled_sheen_eval_reflect(ccl_private const ShaderClosure *sc,
                                                     const float3 I,
                                                     const float3 omega_in,
                                                     ccl_private float *pdf)
{
  ccl_private const PrincipledSheenBsdf *bsdf = (ccl_private const PrincipledSheenBsdf *)sc;

  float3 N = bsdf->N;
  float3 V = I;         // outgoing
  float3 L = omega_in;  // incoming
  float3 H = normalize(L + V);

  if (dot(N, omega_in) > 0.0f) {
    *pdf = M_1_2PI_F;
    if (bsdf->type == CLOSURE_BSDF_PRINCIPLED_SHEEN_V2_ID) {
      return sheen_v2_eval(N, V, L, H, bsdf->roughness, pdf);
    }
    else {
      return calculate_principled_sheen_brdf(N, V, L, H, pdf);
    }
  }
  else {
    *pdf = 0.0f;
    return make_float3(0.0f, 0.0f, 0.0f);
  }
}

ccl_device float3 bsdf_principled_sheen_eval_transmit(ccl_private const ShaderClosure *sc,
                                                      const float3 I,
                                                      const float3 omega_in,
                                                      ccl_private float *pdf)
{
  *pdf = 0.0f;
  return make_float3(0.0f, 0.0f, 0.0f);
}

ccl_device int bsdf_principled_sheen_sample(ccl_private const ShaderClosure *sc,
                                            float3 Ng,
                                            float3 I,
                                            float3 dIdx,
                                            float3 dIdy,
                                            float randu,
                                            float randv,
                                            ccl_private float3 *eval,
                                            ccl_private float3 *omega_in,
                                            ccl_private float3 *domega_in_dx,
                                            ccl_private float3 *domega_in_dy,
                                            ccl_private float *pdf)
{
  ccl_private const PrincipledSheenBsdf *bsdf = (ccl_private const PrincipledSheenBsdf *)sc;

  float3 N = bsdf->N;

  sample_uniform_hemisphere(N, randu, randv, omega_in, pdf);

  if (dot(Ng, *omega_in) > 0) {
    float3 H = normalize(I + *omega_in);

    if (bsdf->type == CLOSURE_BSDF_PRINCIPLED_SHEEN_V2_ID) {
      *eval = sheen_v2_eval(N, I, *omega_in, H, bsdf->roughness, pdf);
    }
    else {
      *eval = calculate_principled_sheen_brdf(N, I, *omega_in, H, pdf);
    }

#ifdef __RAY_DIFFERENTIALS__
    // TODO: find a better approximation for the diffuse bounce
    *domega_in_dx = -((2 * dot(N, dIdx)) * N - dIdx);
    *domega_in_dy = -((2 * dot(N, dIdy)) * N - dIdy);
#endif
  }
  else {
    *eval = make_float3(0.0f, 0.0f, 0.0f);
    *pdf = 0.0f;
  }
  return LABEL_REFLECT | LABEL_DIFFUSE;
}

CCL_NAMESPACE_END