Blender  V3.3
bsdf_diffuse_ramp.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Adapted from Open Shading Language
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011-2022 Blender Foundation. */
8 
9 #pragma once
10 
11 #include "kernel/sample/mapping.h"
12 
14 
15 #ifdef __OSL__
16 
17 typedef struct DiffuseRampBsdf {
19 
20  ccl_private float3 *colors;
21 } DiffuseRampBsdf;
22 
23 static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseRampBsdf), "DiffuseRampBsdf is too large!");
24 
25 ccl_device float3 bsdf_diffuse_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 
39 ccl_device int bsdf_diffuse_ramp_setup(DiffuseRampBsdf *bsdf)
40 {
41  bsdf->type = CLOSURE_BSDF_DIFFUSE_RAMP_ID;
42  return SD_BSDF | SD_BSDF_HAS_EVAL;
43 }
44 
45 ccl_device void bsdf_diffuse_ramp_blur(ccl_private ShaderClosure *sc, float roughness)
46 {
47 }
48 
49 ccl_device float3 bsdf_diffuse_ramp_eval_reflect(ccl_private const ShaderClosure *sc,
50  const float3 I,
51  const float3 omega_in,
52  ccl_private float *pdf)
53 {
54  const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
55  float3 N = bsdf->N;
56 
57  float cos_pi = fmaxf(dot(N, omega_in), 0.0f);
58  *pdf = cos_pi * M_1_PI_F;
59  return bsdf_diffuse_ramp_get_color(bsdf->colors, cos_pi) * M_1_PI_F;
60 }
61 
62 ccl_device float3 bsdf_diffuse_ramp_eval_transmit(ccl_private const ShaderClosure *sc,
63  const float3 I,
64  const float3 omega_in,
65  ccl_private float *pdf)
66 {
67  return make_float3(0.0f, 0.0f, 0.0f);
68 }
69 
70 ccl_device int bsdf_diffuse_ramp_sample(ccl_private const ShaderClosure *sc,
71  float3 Ng,
72  float3 I,
73  float3 dIdx,
74  float3 dIdy,
75  float randu,
76  float randv,
77  ccl_private float3 *eval,
78  ccl_private float3 *omega_in,
79  ccl_private float3 *domega_in_dx,
80  ccl_private float3 *domega_in_dy,
81  ccl_private float *pdf)
82 {
83  const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
84  float3 N = bsdf->N;
85 
86  // distribution over the hemisphere
87  sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
88 
89  if (dot(Ng, *omega_in) > 0.0f) {
90  *eval = bsdf_diffuse_ramp_get_color(bsdf->colors, *pdf * M_PI_F) * M_1_PI_F;
91 # ifdef __RAY_DIFFERENTIALS__
92  *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
93  *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
94 # endif
95  }
96  else {
97  *pdf = 0.0f;
98  *eval = make_float3(0.0f, 0.0f, 0.0f);
99  }
100  return LABEL_REFLECT | LABEL_DIFFUSE;
101 }
102 
103 #endif /* __OSL__ */
104 
typedef float(TangentPoint)[2]
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
uint pos
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
@ CLOSURE_BSDF_DIFFUSE_RAMP_ID
@ SD_BSDF_HAS_EVAL
Definition: kernel/types.h:744
@ SD_BSDF
Definition: kernel/types.h:742
#define SHADER_CLOSURE_BASE
Definition: kernel/types.h:711
@ LABEL_DIFFUSE
Definition: kernel/types.h:319
@ LABEL_REFLECT
Definition: kernel/types.h:318
ShaderClosure
Definition: kernel/types.h:726
#define N
#define fmaxf(x, y)
Definition: metal/compat.h:228
#define make_float3(x, y, z)
Definition: metal/compat.h:204
T dot(const vec_base< T, Size > &a, const vec_base< T, Size > &b)
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
#define I
ccl_device_inline void sample_cos_hemisphere(const float3 N, float randu, float randv, ccl_private float3 *omega_in, ccl_private float *pdf)
ccl_device_inline int float_to_int(float f)
Definition: util/math.h:410
#define M_1_PI_F
Definition: util/math.h:43
#define M_PI_F
Definition: util/math.h:34