1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2014-2018 Esteban Tovagliari, The appleseedhq Organization
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 //
28 
29 #pragma once
30 
31 // appleseed.renderer headers.
32 #include "renderer/modeling/entity/entity.h"
33 
34 // appleseed.foundation headers.
35 #include "foundation/platform/compiler.h"
36 #include "foundation/utility/autoreleaseptr.h"
37 
38 // appleseed.main headers.
39 #include "main/dllsymbol.h"
40 
41 // Standard headers.
42 #include <cstddef>
43 #include <string>
44 #include <vector>
45 
46 // Forward declarations.
47 namespace renderer  { class OSLShadingSystem; }
48 namespace renderer  { class Shader; }
49 
50 namespace renderer
51 {
52 
53 //
54 // Enumeration of all supported OSL parameter types.
55 //
56 
57 enum OSLParamType
58 {
59     OSLParamTypeColor,
60     OSLParamTypeColorArray,
61     OSLParamTypeFloat,
62     OSLParamTypeFloatArray,
63     OSLParamTypeInt,
64     OSLParamTypeIntArray,
65     OSLParamTypeMatrix,
66     OSLParamTypeMatrixArray,
67     OSLParamTypeNormal,
68     OSLParamTypeNormalArray,
69     OSLParamTypePoint,
70     OSLParamTypePointArray,
71     OSLParamTypeString,
72     OSLParamTypeVector,
73     OSLParamTypeVectorArray
74 };
75 
76 
77 //
78 // A parameter in an OSL shader.
79 //
80 
81 class APPLESEED_DLLSYMBOL ShaderParam
82   : public Entity
83 {
84   public:
85     // Delete this instance.
86     void release() override;
87 
88     // todo: STL classes cannot be used in DLL-exported classes.
89     std::string get_value_as_string() const;
90 
91   private:
92     friend class Shader;
93 
94     struct Impl;
95     Impl* impl;
96 
97     // Constructor.
98     explicit ShaderParam(const char* name);
99 
100     // Destructor.
101     ~ShaderParam() override;
102 
103     // Create an int param.
104     static foundation::auto_release_ptr<ShaderParam> create_int_param(
105         const char*         name,
106         const int           value);
107 
108     // Create an int array param.
109     static foundation::auto_release_ptr<ShaderParam> create_int_array_param(
110         const char*         name,
111         std::vector<int>&   value);
112 
113     // Create a float param.
114     static foundation::auto_release_ptr<ShaderParam> create_float_param(
115         const char*         name,
116         const float         value);
117 
118     // Create a float array param.
119     static foundation::auto_release_ptr<ShaderParam> create_float_array_param(
120         const char*         name,
121         std::vector<float>& value);
122 
123     // Create a vector param.
124     static foundation::auto_release_ptr<ShaderParam> create_vector_param(
125         const char*         name,
126         const float         vx,
127         const float         vy,
128         const float         vz);
129 
130     // Create a vector array param.
131     static foundation::auto_release_ptr<ShaderParam> create_vector_array_param(
132         const char*         name,
133         std::vector<float>& value);
134 
135     // Create a normal param.
136     static foundation::auto_release_ptr<ShaderParam> create_normal_param(
137         const char*         name,
138         const float         nx,
139         const float         ny,
140         const float         nz);
141 
142     // Create a normal array param.
143     static foundation::auto_release_ptr<ShaderParam> create_normal_array_param(
144         const char*         name,
145         std::vector<float>& value);
146 
147     // Create a point param.
148     static foundation::auto_release_ptr<ShaderParam> create_point_param(
149         const char*         name,
150         const float         vx,
151         const float         vy,
152         const float         vz);
153 
154     // Create a point array param.
155     static foundation::auto_release_ptr<ShaderParam> create_point_array_param(
156         const char*         name,
157         std::vector<float>& value);
158 
159     // Create a color param.
160     static foundation::auto_release_ptr<ShaderParam> create_color_param(
161         const char*         name,
162         const float         vx,
163         const float         vy,
164         const float         vz);
165 
166     // Create a color array param.
167     static foundation::auto_release_ptr<ShaderParam> create_color_array_param(
168         const char*         name,
169         std::vector<float>& value);
170 
171     // Create a matrix param.
172     static foundation::auto_release_ptr<ShaderParam> create_matrix_param(
173         const char*         name,
174         const float*        values);
175 
176     // Create a matrix array param.
177     static foundation::auto_release_ptr<ShaderParam> create_matrix_array_param(
178         const char*         name,
179         std::vector<float>& value);
180 
181     // Create a string param.
182     static foundation::auto_release_ptr<ShaderParam> create_string_param(
183         const char*         name,
184         const char*         value);
185 
186     // Return a const void pointer to this param value.
187     const void* get_value() const;
188 
189     // Add this param to OSL's shading system.
190     bool add(OSLShadingSystem& shading_system);
191 };
192 
193 }   // namespace renderer
194