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