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