Cycles OSL minor optimizations: recycle shading context, don't do memory
[blender.git] / intern / cycles / kernel / osl / bsdf_phong_ramp.cpp
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 2011, 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 #include <OpenImageIO/fmath.h>
34
35 #include <OSL/genclosure.h>
36
37 #include "osl_closures.h"
38
39 #include "kernel_types.h"
40 #include "closure/bsdf_phong_ramp.h"
41
42 CCL_NAMESPACE_BEGIN
43
44 using namespace OSL;
45
46 class PhongRampClosure : public CBSDFClosure {
47 public:
48         PhongRampClosure() : CBSDFClosure(LABEL_GLOSSY) {}
49         Color3 colors[8];
50         float3 fcolors[8];
51
52         size_t memsize() const { return sizeof(*this); }
53         const char *name() const { return "phong_ramp"; }
54
55         void setup()
56         {
57                 sc.prim = this;
58                 m_shaderdata_flag = bsdf_phong_ramp_setup(&sc);
59
60                 for(int i = 0; i < 8; i++)
61                         fcolors[i] = TO_FLOAT3(colors[i]);
62         }
63
64         bool mergeable(const ClosurePrimitive *other) const
65         {
66                 return false;
67         }
68
69         void blur(float roughness)
70         {
71                 bsdf_phong_ramp_blur(&sc, roughness);
72         }
73
74         void print_on(std::ostream &out) const
75         {
76                 out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))";
77         }
78
79         float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const
80         {
81                 return bsdf_phong_ramp_eval_reflect(&sc, fcolors, omega_out, omega_in, &pdf);
82         }
83
84         float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const
85         {
86                 return bsdf_phong_ramp_eval_transmit(&sc, fcolors, omega_out, omega_in, &pdf);
87         }
88
89         int sample(const float3 &Ng,
90                    const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy,
91                    float randu, float randv,
92                    float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy,
93                    float &pdf, float3 &eval) const
94         {
95                 return bsdf_phong_ramp_sample(&sc, fcolors, Ng, omega_out, domega_out_dx, domega_out_dy,
96                         randu, randv, &eval, &omega_in, &domega_in_dx, &domega_in_dy, &pdf);
97         }
98 };
99
100 ClosureParam *closure_bsdf_phong_ramp_params()
101 {
102         static ClosureParam params[] = {
103                 CLOSURE_FLOAT3_PARAM(PhongRampClosure, sc.N),
104                 CLOSURE_FLOAT_PARAM(PhongRampClosure, sc.data0),
105                 CLOSURE_COLOR_ARRAY_PARAM(PhongRampClosure, colors, 8),
106                 CLOSURE_STRING_KEYPARAM("label"),
107             CLOSURE_FINISH_PARAM(PhongRampClosure)
108         };
109         return params;
110 }
111
112 CLOSURE_PREPARE(closure_bsdf_phong_ramp_prepare, PhongRampClosure)
113
114 CCL_NAMESPACE_END
115