Blender V4.5
services.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7/* OSL Render Services
8 *
9 * Implementation of OSL render services, to retriever matrices, attributes,
10 * textures and point clouds. In principle this should only be accessing
11 * kernel data, but currently we also reach back into the Scene to retrieve
12 * attributes.
13 */
14
15#include <OSL/oslclosure.h>
16#include <OSL/oslexec.h>
17#include <OSL/rendererservices.h>
18
19#include <OpenImageIO/unordered_map_concurrent.h>
20
21#include "scene/image.h"
22
23#include "kernel/osl/compat.h"
24
26
27class Scene;
28struct ShaderData;
30
31/* OSL Texture Handle
32 *
33 * OSL texture lookups are string based. If those strings are known at compile
34 * time, the OSL compiler can cache a texture handle to use instead of a string.
35 *
36 * By default it uses TextureSystem::TextureHandle. But since we want to support
37 * different kinds of textures and color space conversions, this is our own handle
38 * with additional data.
39 *
40 * These are stored in a concurrent hash map, because OSL can compile multiple
41 * shaders in parallel.
42 *
43 * NOTE: The svm_slots array contains a compressed mapping of tile to svm_slot pairs
44 * stored as follows: x:tile_a, y:svm_slot_a, z:tile_b, w:svm_slot_b etc. */
45
47 enum Type { OIIO, SVM, IES, BEVEL, AO };
48
50
51 OSLTextureHandle(Type type = OIIO, const int svm_slot = -1)
52 : OSLTextureHandle(type, {make_int4(0, svm_slot, -1, -1)})
53 {
54 }
55
57 : type(SVM), svm_slots(handle.get_svm_slots()), handle(handle)
58 {
59 }
60
63 OSL::TextureSystem::TextureHandle *oiio_handle = nullptr;
64 ColorSpaceProcessor *processor = nullptr;
66};
67
68using OSLTextureHandleMap = OIIO::unordered_map_concurrent<OSLUStringHash, OSLTextureHandle>;
69
70/* OSL Render Services
71 *
72 * Interface for OSL to access attributes, textures and other scene data. */
73
74class OSLRenderServices : public OSL::RendererServices {
75 public:
76 OSLRenderServices(OSL::TextureSystem *texture_system, const int device_type);
77 ~OSLRenderServices() override;
78
79 static void register_closures(OSL::ShadingSystem *ss);
80
81 int supports(string_view feature) const override;
82
83 bool get_matrix(OSL::ShaderGlobals *sg,
84 OSL::Matrix44 &result,
85 OSL::TransformationPtr xform,
86 float time) override;
87 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
88 OSL::Matrix44 &result,
89 OSL::TransformationPtr xform,
90 float time) override;
91
92 bool get_matrix(OSL::ShaderGlobals *sg,
93 OSL::Matrix44 &result,
94 OSLUStringHash from,
95 float time) override;
96 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
97 OSL::Matrix44 &result,
99 float time) override;
100
101 bool get_matrix(OSL::ShaderGlobals *sg,
102 OSL::Matrix44 &result,
103 OSL::TransformationPtr xform) override;
104 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
105 OSL::Matrix44 &result,
106 OSL::TransformationPtr xform) override;
107
108 bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSLUStringHash from) override;
109 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
110 OSL::Matrix44 &result,
111 OSLUStringHash to) override;
112
113 bool get_array_attribute(OSL::ShaderGlobals *sg,
114 bool derivatives,
115 OSLUStringHash object,
116 const TypeDesc type,
117 OSLUStringHash name,
118 const int index,
119 void *val) override;
120 bool get_attribute(OSL::ShaderGlobals *sg,
121 bool derivatives,
122 OSLUStringHash object,
123 const TypeDesc type,
124 OSLUStringHash name,
125 void *val) override;
126
127 bool get_userdata(bool derivatives,
128 OSLUStringHash name,
129 const TypeDesc type,
130 OSL::ShaderGlobals *sg,
131 void *val) override;
132
133 int pointcloud_search(OSL::ShaderGlobals *sg,
134 OSLUStringHash filename,
135 const OSL::Vec3 &center,
136 const float radius,
137 const int max_points,
138 bool sort,
139#if OSL_LIBRARY_VERSION_CODE >= 11400
140 int *out_indices,
141#else
142 size_t *out_indices,
143#endif
144 float *out_distances,
145 int derivs_offset) override;
146
147 int pointcloud_get(OSL::ShaderGlobals *sg,
148 OSLUStringHash filename,
149#if OSL_LIBRARY_VERSION_CODE >= 11400
150 const int *indices,
151#else
152 size_t *indices,
153#endif
154
155 const int count,
156 OSLUStringHash attr_name,
157 const TypeDesc attr_type,
158 void *out_data) override;
159
160 bool pointcloud_write(OSL::ShaderGlobals *sg,
161 OSLUStringHash filename,
162 const OSL::Vec3 &pos,
163 const int nattribs,
164 const OSLUStringRep *names,
165 const TypeDesc *types,
166 const void **data) override;
167
168 bool trace(TraceOpt &options,
169 OSL::ShaderGlobals *sg,
170 const OSL::Vec3 &P,
171 const OSL::Vec3 &dPdx,
172 const OSL::Vec3 &dPdy,
173 const OSL::Vec3 &R,
174 const OSL::Vec3 &dRdx,
175 const OSL::Vec3 &dRdy) override;
176
177 bool getmessage(OSL::ShaderGlobals *sg,
178 OSLUStringHash source,
179 OSLUStringHash name,
180 const TypeDesc type,
181 void *val,
182 bool derivatives) override;
183
184 OSL::TextureSystem::TextureHandle *get_texture_handle(OSL::ustring filename,
185 OSL::ShadingContext *context,
186 const OSL::TextureOpt *options) override;
187 OSL::TextureSystem::TextureHandle *get_texture_handle(OSLUStringHash filename,
188 OSL::ShadingContext *context,
189 const OSL::TextureOpt *options) override;
190
191 bool good(OSL::TextureSystem::TextureHandle *texture_handle) override;
192
193 bool texture(OSLUStringHash filename,
194 OSL::TextureSystem::TextureHandle *texture_handle,
195 TexturePerthread *texture_thread_info,
196 OSL::TextureOpt &options,
197 OSL::ShaderGlobals *sg,
198 const float s,
199 const float t,
200 const float dsdx,
201 const float dtdx,
202 const float dsdy,
203 const float dtdy,
204 const int nchannels,
205 float *result,
206 float *dresultds,
207 float *dresultdt,
208 OSLUStringHash *errormessage) override;
209
210 bool texture3d(OSLUStringHash filename,
211 TextureHandle *texture_handle,
212 TexturePerthread *texture_thread_info,
213 OSL::TextureOpt &options,
214 OSL::ShaderGlobals *sg,
215 const OSL::Vec3 &P,
216 const OSL::Vec3 &dPdx,
217 const OSL::Vec3 &dPdy,
218 const OSL::Vec3 &dPdz,
219 const int nchannels,
220 float *result,
221 float *dresultds,
222 float *dresultdt,
223 float *dresultdr,
224 OSLUStringHash *errormessage) override;
225
226 bool environment(OSLUStringHash filename,
227 TextureHandle *texture_handle,
228 TexturePerthread *texture_thread_info,
229 OSL::TextureOpt &options,
230 OSL::ShaderGlobals *sg,
231 const OSL::Vec3 &R,
232 const OSL::Vec3 &dRdx,
233 const OSL::Vec3 &dRdy,
234 const int nchannels,
235 float *result,
236 float *dresultds,
237 float *dresultdt,
238 OSLUStringHash *errormessage) override;
239
240 bool get_texture_info(OSLUStringHash filename,
241 TextureHandle *texture_handle,
242 TexturePerthread *texture_thread_info,
243 OSL::ShaderGlobals *sg,
244 const int subimage,
245 OSLUStringHash dataname,
246 const TypeDesc datatype,
247 void *data,
248 OSLUStringHash *errormessage) override;
249
250 static bool get_background_attribute(ShaderGlobals *globals,
251 OSLUStringHash name,
252 const TypeDesc type,
253 bool derivatives,
254 void *val);
255 static bool get_camera_attribute(
256 ShaderGlobals *globals, OSLUStringHash name, TypeDesc type, bool derivatives, void *val);
257 static bool get_object_standard_attribute(ShaderGlobals *globals,
258 OSLUStringHash name,
259 const TypeDesc type,
260 bool derivatives,
261 void *val);
262
263 static ustring u_distance;
264 static ustring u_index;
265 static ustring u_world;
266 static ustring u_camera;
267 static ustring u_screen;
268 static ustring u_raster;
269 static ustring u_ndc;
270 static ustring u_object_location;
271 static ustring u_object_color;
272 static ustring u_object_alpha;
273 static ustring u_object_index;
274 static ustring u_object_is_light;
275 static ustring u_bump_map_normal;
277 static ustring u_geom_dupli_uv;
278 static ustring u_material_index;
279 static ustring u_object_random;
280 static ustring u_particle_index;
281 static ustring u_particle_random;
282 static ustring u_particle_age;
283 static ustring u_particle_lifetime;
284 static ustring u_particle_location;
285 static ustring u_particle_rotation;
286 static ustring u_particle_size;
287 static ustring u_particle_velocity;
291 static ustring u_geom_polyvertices;
292 static ustring u_geom_name;
293 static ustring u_geom_undisplaced;
294 static ustring u_is_smooth;
295 static ustring u_is_curve;
296 static ustring u_curve_thickness;
297 static ustring u_curve_length;
299 static ustring u_curve_random;
300 static ustring u_is_point;
301 static ustring u_point_position;
302 static ustring u_point_radius;
303 static ustring u_point_random;
304 static ustring u_normal_map_normal;
305 static ustring u_path_ray_length;
306 static ustring u_path_ray_depth;
307 static ustring u_path_diffuse_depth;
308 static ustring u_path_glossy_depth;
311 static ustring u_trace;
312 static ustring u_hit;
313 static ustring u_hitdist;
314 static ustring u_N;
315 static ustring u_Ng;
316 static ustring u_P;
317 static ustring u_I;
318 static ustring u_u;
319 static ustring u_v;
320 static ustring u_empty;
321 static ustring u_at_bevel;
322 static ustring u_at_ao;
323
324 /* Attributes for camera shaders. */
325 static ustring u_sensor_size;
326 static ustring u_image_resolution;
328 static ustring u_aperture_size;
329 static ustring u_aperture_position;
330 static ustring u_focal_distance;
331
332 /* Texture system and texture handle map are part of the services instead of
333 * globals to be shared between different render sessions. This saves memory,
334 * and is required because texture handles are cached as part of the shared
335 * shading system. */
337
339
340 private:
341 int device_type_;
342};
343
BMesh const char void * data
static DBVT_INLINE btDbvtNode * sort(btDbvtNode *n, btDbvtNode *&r)
Definition btDbvt.cpp:418
static ustring u_bump_map_normal
Definition services.h:275
static ustring u_path_transmission_depth
Definition services.h:310
static bool get_background_attribute(ShaderGlobals *globals, OSLUStringHash name, const TypeDesc type, bool derivatives, void *val)
Definition services.cpp:876
static ustring u_particle_location
Definition services.h:284
static ImageManager * image_manager
Definition services.h:338
static ustring u_object_alpha
Definition services.h:272
bool get_texture_info(OSLUStringHash filename, TextureHandle *texture_handle, TexturePerthread *texture_thread_info, OSL::ShaderGlobals *sg, const int subimage, OSLUStringHash dataname, const TypeDesc datatype, void *data, OSLUStringHash *errormessage) override
static ustring u_u
Definition services.h:318
static ustring u_sensor_size
Definition services.h:325
bool texture(OSLUStringHash filename, OSL::TextureSystem::TextureHandle *texture_handle, TexturePerthread *texture_thread_info, OSL::TextureOpt &options, OSL::ShaderGlobals *sg, const float s, const float t, const float dsdx, const float dtdx, const float dsdy, const float dtdy, const int nchannels, float *result, float *dresultds, float *dresultdt, OSLUStringHash *errormessage) override
bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time) override
Definition services.cpp:190
static ustring u_raster
Definition services.h:268
static ustring u_empty
Definition services.h:320
static ustring u_curve_thickness
Definition services.h:296
int pointcloud_search(OSL::ShaderGlobals *sg, OSLUStringHash filename, const OSL::Vec3 &center, const float radius, const int max_points, bool sort, size_t *out_indices, float *out_distances, int derivs_offset) override
static ustring u_ndc
Definition services.h:269
static ustring u_particle_index
Definition services.h:280
static ustring u_point_position
Definition services.h:301
static ustring u_point_radius
Definition services.h:302
static ustring u_curve_tangent_normal
Definition services.h:298
~OSLRenderServices() override
Definition services.cpp:134
int supports(string_view feature) const override
Definition services.cpp:141
static ustring u_image_resolution
Definition services.h:326
static ustring u_path_diffuse_depth
Definition services.h:307
static bool get_camera_attribute(ShaderGlobals *globals, OSLUStringHash name, TypeDesc type, bool derivatives, void *val)
Definition services.cpp:959
static ustring u_normal_map_normal
Definition services.h:304
static ustring u_index
Definition services.h:264
bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time) override
Definition services.cpp:152
static ustring u_curve_length
Definition services.h:297
static ustring u_aperture_aspect_ratio
Definition services.h:327
static ustring u_hit
Definition services.h:312
static ustring u_material_index
Definition services.h:278
static ustring u_path_ray_depth
Definition services.h:306
static ustring u_point_random
Definition services.h:303
static ustring u_is_smooth
Definition services.h:294
static ustring u_object_location
Definition services.h:270
static ustring u_curve_random
Definition services.h:299
static ustring u_particle_rotation
Definition services.h:285
static ustring u_focal_distance
Definition services.h:330
static ustring u_particle_velocity
Definition services.h:287
static ustring u_particle_angular_velocity
Definition services.h:288
static ustring u_N
Definition services.h:314
int pointcloud_get(OSL::ShaderGlobals *sg, OSLUStringHash filename, size_t *indices, const int count, OSLUStringHash attr_name, const TypeDesc attr_type, void *out_data) override
static ustring u_path_glossy_depth
Definition services.h:308
static ustring u_hitdist
Definition services.h:313
static bool get_object_standard_attribute(ShaderGlobals *globals, OSLUStringHash name, const TypeDesc type, bool derivatives, void *val)
Definition services.cpp:695
static ustring u_geom_dupli_uv
Definition services.h:277
static ustring u_aperture_size
Definition services.h:328
static ustring u_I
Definition services.h:317
static ustring u_geom_undisplaced
Definition services.h:293
OSLTextureHandleMap textures
Definition services.h:336
static ustring u_geom_numpolyvertices
Definition services.h:289
static ustring u_v
Definition services.h:319
static ustring u_world
Definition services.h:265
static ustring u_object_random
Definition services.h:279
static ustring u_at_bevel
Definition services.h:321
static ustring u_path_ray_length
Definition services.h:305
static void register_closures(OSL::ShadingSystem *ss)
Definition closures.cpp:66
static ustring u_screen
Definition services.h:267
static ustring u_object_is_light
Definition services.h:274
static ustring u_particle_random
Definition services.h:281
static ustring u_camera
Definition services.h:266
static ustring u_particle_age
Definition services.h:282
static ustring u_P
Definition services.h:316
bool pointcloud_write(OSL::ShaderGlobals *sg, OSLUStringHash filename, const OSL::Vec3 &pos, const int nattribs, const OSLUStringRep *names, const TypeDesc *types, const void **data) override
bool environment(OSLUStringHash filename, TextureHandle *texture_handle, TexturePerthread *texture_thread_info, OSL::TextureOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &R, const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy, const int nchannels, float *result, float *dresultds, float *dresultdt, OSLUStringHash *errormessage) override
bool get_array_attribute(OSL::ShaderGlobals *sg, bool derivatives, OSLUStringHash object, const TypeDesc type, OSLUStringHash name, const int index, void *val) override
Definition services.cpp:398
static ustring u_object_index
Definition services.h:273
bool get_attribute(OSL::ShaderGlobals *sg, bool derivatives, OSLUStringHash object, const TypeDesc type, OSLUStringHash name, void *val) override
Definition services.cpp:989
static ustring u_is_point
Definition services.h:300
static ustring u_path_transparent_depth
Definition services.h:309
bool get_userdata(bool derivatives, OSLUStringHash name, const TypeDesc type, OSL::ShaderGlobals *sg, void *val) override
bool trace(TraceOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &P, const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy, const OSL::Vec3 &R, const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy) override
static ustring u_particle_lifetime
Definition services.h:283
static ustring u_is_curve
Definition services.h:295
static ustring u_object_color
Definition services.h:271
static ustring u_distance
Definition services.h:263
static ustring u_aperture_position
Definition services.h:329
static ustring u_Ng
Definition services.h:315
static ustring u_at_ao
Definition services.h:322
static ustring u_trace
Definition services.h:311
static ustring u_geom_trianglevertices
Definition services.h:290
bool texture3d(OSLUStringHash filename, TextureHandle *texture_handle, TexturePerthread *texture_thread_info, OSL::TextureOpt &options, OSL::ShaderGlobals *sg, const OSL::Vec3 &P, const OSL::Vec3 &dPdx, const OSL::Vec3 &dPdy, const OSL::Vec3 &dPdz, const int nchannels, float *result, float *dresultds, float *dresultdt, float *dresultdr, OSLUStringHash *errormessage) override
static ustring u_geom_polyvertices
Definition services.h:291
bool getmessage(OSL::ShaderGlobals *sg, OSLUStringHash source, OSLUStringHash name, const TypeDesc type, void *val, bool derivatives) override
static ustring u_particle_size
Definition services.h:286
OSL::TextureSystem::TextureHandle * get_texture_handle(OSL::ustring filename, OSL::ShadingContext *context, const OSL::TextureOpt *options) override
static ustring u_geom_dupli_generated
Definition services.h:276
OSLRenderServices(OSL::TextureSystem *texture_system, const int device_type)
Definition services.cpp:129
bool good(OSL::TextureSystem::TextureHandle *texture_handle) override
static ustring u_geom_name
Definition services.h:292
CCL_NAMESPACE_BEGIN struct Options options
#define CCL_NAMESPACE_END
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
static ushort indices[]
uint pos
int count
static char ** types
Definition makesdna.cc:71
#define R
OSL::ustringhash OSLUStringHash
Definition osl/compat.h:11
OSL::ustringrep OSLUStringRep
Definition osl/compat.h:15
OIIO::unordered_map_concurrent< OSLUStringHash, OSLTextureHandle > OSLTextureHandleMap
Definition services.h:68
long long TypeDesc
OSLTextureHandle(const ImageHandle &handle)
Definition services.h:56
ColorSpaceProcessor * processor
Definition services.h:64
OSLTextureHandle(Type type=OIIO, const int svm_slot=-1)
Definition services.h:51
OSLTextureHandle(Type type, const vector< int4 > &svm_slots)
Definition services.h:49
OSL::TextureSystem::TextureHandle * oiio_handle
Definition services.h:63
ImageHandle handle
Definition services.h:65
vector< int4 > svm_slots
Definition services.h:62