Blender  V2.93
bsdf_diffuse_ramp.h
Go to the documentation of this file.
1 /*
2  * Adapted from Open Shading Language with this license:
3  *
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2012, Blender Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of Sony Pictures Imageworks nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef __BSDF_DIFFUSE_RAMP_H__
34 #define __BSDF_DIFFUSE_RAMP_H__
35 
37 
38 #ifdef __OSL__
39 
40 typedef ccl_addr_space struct DiffuseRampBsdf {
42 
43  float3 *colors;
44 } DiffuseRampBsdf;
45 
46 static_assert(sizeof(ShaderClosure) >= sizeof(DiffuseRampBsdf), "DiffuseRampBsdf is too large!");
47 
48 ccl_device float3 bsdf_diffuse_ramp_get_color(const float3 colors[8], float pos)
49 {
50  int MAXCOLORS = 8;
51 
52  float npos = pos * (float)(MAXCOLORS - 1);
53  int ipos = float_to_int(npos);
54  if (ipos < 0)
55  return colors[0];
56  if (ipos >= (MAXCOLORS - 1))
57  return colors[MAXCOLORS - 1];
58  float offset = npos - (float)ipos;
59  return colors[ipos] * (1.0f - offset) + colors[ipos + 1] * offset;
60 }
61 
62 ccl_device int bsdf_diffuse_ramp_setup(DiffuseRampBsdf *bsdf)
63 {
64  bsdf->type = CLOSURE_BSDF_DIFFUSE_RAMP_ID;
65  return SD_BSDF | SD_BSDF_HAS_EVAL;
66 }
67 
68 ccl_device void bsdf_diffuse_ramp_blur(ShaderClosure *sc, float roughness)
69 {
70 }
71 
72 ccl_device float3 bsdf_diffuse_ramp_eval_reflect(const ShaderClosure *sc,
73  const float3 I,
74  const float3 omega_in,
75  float *pdf)
76 {
77  const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
78  float3 N = bsdf->N;
79 
80  float cos_pi = fmaxf(dot(N, omega_in), 0.0f);
81  *pdf = cos_pi * M_1_PI_F;
82  return bsdf_diffuse_ramp_get_color(bsdf->colors, cos_pi) * M_1_PI_F;
83 }
84 
85 ccl_device float3 bsdf_diffuse_ramp_eval_transmit(const ShaderClosure *sc,
86  const float3 I,
87  const float3 omega_in,
88  float *pdf)
89 {
90  return make_float3(0.0f, 0.0f, 0.0f);
91 }
92 
93 ccl_device int bsdf_diffuse_ramp_sample(const ShaderClosure *sc,
94  float3 Ng,
95  float3 I,
96  float3 dIdx,
97  float3 dIdy,
98  float randu,
99  float randv,
100  float3 *eval,
101  float3 *omega_in,
102  float3 *domega_in_dx,
103  float3 *domega_in_dy,
104  float *pdf)
105 {
106  const DiffuseRampBsdf *bsdf = (const DiffuseRampBsdf *)sc;
107  float3 N = bsdf->N;
108 
109  // distribution over the hemisphere
110  sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
111 
112  if (dot(Ng, *omega_in) > 0.0f) {
113  *eval = bsdf_diffuse_ramp_get_color(bsdf->colors, *pdf * M_PI_F) * M_1_PI_F;
114 # ifdef __RAY_DIFFERENTIALS__
115  *domega_in_dx = (2 * dot(N, dIdx)) * N - dIdx;
116  *domega_in_dy = (2 * dot(N, dIdy)) * N - dIdy;
117 # endif
118  }
119  else
120  *pdf = 0.0f;
121 
122  return LABEL_REFLECT | LABEL_DIFFUSE;
123 }
124 
125 #endif /* __OSL__ */
126 
128 
129 #endif /* __BSDF_DIFFUSE_RAMP_H__ */
typedef float(TangentPoint)[2]
uint pos
#define ccl_addr_space
#define ccl_device
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define make_float3(x, y, z)
ccl_device_inline void sample_cos_hemisphere(const float3 N, float randu, float randv, float3 *omega_in, float *pdf)
@ SD_BSDF_HAS_EVAL
Definition: kernel_types.h:849
@ SD_BSDF
Definition: kernel_types.h:847
#define SHADER_CLOSURE_BASE
Definition: kernel_types.h:816
@ LABEL_DIFFUSE
Definition: kernel_types.h:330
@ LABEL_REFLECT
Definition: kernel_types.h:329
ShaderClosure
Definition: kernel_types.h:831
static const pxr::TfToken roughness("roughness", pxr::TfToken::Immortal)
#define I
params N
@ CLOSURE_BSDF_DIFFUSE_RAMP_ID
Definition: svm_types.h:536
ccl_device_inline int float_to_int(float f)
Definition: util_math.h:321
#define M_1_PI_F
Definition: util_math.h:52
#define M_PI_F
Definition: util_math.h:43
ccl_device_inline float dot(const float2 &a, const float2 &b)