Blender V5.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#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 "util/concurrent_set.h"
22#include "util/map.h"
23#include "util/thread.h"
24
25#include "scene/image.h"
26
27#include "kernel/osl/compat.h"
28#include "kernel/osl/types.h"
29
31
32class Scene;
33struct ShaderData;
35
36/* OSL Texture Handle
37 *
38 * OSL texture lookups are string based. If those strings are known at compile
39 * time, the OSL compiler can cache a texture handle to use instead of a string.
40 *
41 * By default it uses TextureSystem::TextureHandle. But since we want to support
42 * different kinds of textures and color space conversions, this is our own handle
43 * with additional data.
44 *
45 * These are stored in a concurrent hash map, because OSL can compile multiple
46 * shaders in parallel.
47 *
48 * NOTE: The svm_image_texture_ids array contains a compressed mapping of tile to
49 * svm_image_texture_ids pairs stored as follows: x:tile_a,
50 * y:svm_image_texture_ids_a, z:tile_b, w:svm_image_texture_ids_b etc. */
51
61
62/* The OSL shading system is shared between sessions, so the key for a texture
63 * includes both the filename and the per session image manager so it doesn't
64 * get shared and leaves dangling pointers when one session is freed while
65 * another is using its textures. */
68 const ImageManager *image_manager = nullptr;
69
70 bool operator==(const OSLTextureKey &other) const = default;
71};
72
74 size_t operator()(const OSLTextureKey &key) const
75 {
76 return key.filename.hash();
77 }
78};
79
81 OIIO::unordered_map_concurrent<OSLTextureKey, OSLTextureHandle, OSLTextureKeyHash>;
82using OSLTextureFilenameMap = concurrent_set<OSLUStringHash>;
83
84/* OSL Render Services
85 *
86 * Interface for OSL to access attributes, textures and other scene data. */
87
88class OSLRenderServices : public OSL::RendererServices {
89 public:
90 OSLRenderServices(const int device_type);
92
93 static void register_closures(OSL::ShadingSystem *ss);
94
95 int supports(string_view feature) const override;
96
97 bool get_matrix(OSL::ShaderGlobals *sg,
98 OSL::Matrix44 &result,
99 OSL::TransformationPtr xform,
100 float time) override;
101 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
102 OSL::Matrix44 &result,
103 OSL::TransformationPtr xform,
104 float time) override;
105
106 bool get_matrix(OSL::ShaderGlobals *sg,
107 OSL::Matrix44 &result,
108 OSLUStringHash from,
109 float time) override;
110 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
111 OSL::Matrix44 &result,
113 float time) override;
114
115 bool get_matrix(OSL::ShaderGlobals *sg,
116 OSL::Matrix44 &result,
117 OSL::TransformationPtr xform) override;
118 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
119 OSL::Matrix44 &result,
120 OSL::TransformationPtr xform) override;
121
122 bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSLUStringHash from) override;
123 bool get_inverse_matrix(OSL::ShaderGlobals *sg,
124 OSL::Matrix44 &result,
125 OSLUStringHash to) override;
126
127 bool get_array_attribute(OSL::ShaderGlobals *sg,
128 bool derivatives,
129 OSLUStringHash object,
130 const TypeDesc type,
131 OSLUStringHash name,
132 const int index,
133 void *val) override;
134 bool get_attribute(OSL::ShaderGlobals *sg,
135 bool derivatives,
136 OSLUStringHash object,
137 const TypeDesc type,
138 OSLUStringHash name,
139 void *val) override;
140
141 bool get_userdata(bool derivatives,
142 OSLUStringHash name,
143 const TypeDesc type,
144 OSL::ShaderGlobals *sg,
145 void *val) override;
146
147 int pointcloud_search(OSL::ShaderGlobals *sg,
148 OSLUStringHash filename,
149 const OSL::Vec3 &center,
150 const float radius,
151 const int max_points,
152 bool sort,
153#if OSL_LIBRARY_VERSION_CODE >= 11400
154 int *out_indices,
155#else
156 size_t *out_indices,
157#endif
158 float *out_distances,
159 int derivs_offset) override;
160
161 int pointcloud_get(OSL::ShaderGlobals *sg,
162 OSLUStringHash filename,
163#if OSL_LIBRARY_VERSION_CODE >= 11400
164 const int *indices,
165#else
166 size_t *indices,
167#endif
168
169 const int count,
170 OSLUStringHash attr_name,
171 const TypeDesc attr_type,
172 void *out_data) override;
173
174 bool pointcloud_write(OSL::ShaderGlobals *sg,
175 OSLUStringHash filename,
176 const OSL::Vec3 &pos,
177 const int nattribs,
178 const OSLUStringRep *names,
179 const TypeDesc *types,
180 const void **data) override;
181
182 bool trace(TraceOpt &options,
183 OSL::ShaderGlobals *sg,
184 const OSL::Vec3 &P,
185 const OSL::Vec3 &dPdx,
186 const OSL::Vec3 &dPdy,
187 const OSL::Vec3 &R,
188 const OSL::Vec3 &dRdx,
189 const OSL::Vec3 &dRdy) override;
190
191 bool getmessage(OSL::ShaderGlobals *sg,
192 OSLUStringHash source,
193 OSLUStringHash name,
194 const TypeDesc type,
195 void *val,
196 bool derivatives) override;
197
198 OSL::TextureSystem::TextureHandle *get_texture_handle(OSL::ustring filename,
199 OSL::ShadingContext *context,
200 const OSL::TextureOpt *options) override;
201 OSL::TextureSystem::TextureHandle *get_texture_handle(OSLUStringHash filename,
202 OSL::ShadingContext *context,
203 const OSL::TextureOpt *options) override;
204
205 bool good(OSL::TextureSystem::TextureHandle *texture_handle) override;
206 bool is_udim(OSL::TextureSystem::TextureHandle *texture_handle) override;
207
208 bool texture(OSLUStringHash filename,
209 OSL::TextureSystem::TextureHandle *texture_handle,
210 TexturePerthread *texture_thread_info,
211 OSL::TextureOpt &options,
212 OSL::ShaderGlobals *sg,
213 const float s,
214 const float t,
215 const float dsdx,
216 const float dtdx,
217 const float dsdy,
218 const float dtdy,
219 const 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 OSL::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 const 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 OSL::TextureOpt &options,
245 OSL::ShaderGlobals *sg,
246 const OSL::Vec3 &R,
247 const OSL::Vec3 &dRdx,
248 const OSL::Vec3 &dRdy,
249 const int nchannels,
250 float *result,
251 float *dresultds,
252 float *dresultdt,
253 OSLUStringHash *errormessage) override;
254
255 bool get_texture_info(OSLUStringHash filename,
256 TextureHandle *texture_handle,
257 TexturePerthread *texture_thread_info,
258 OSL::ShaderGlobals *sg,
259 const int subimage,
260 OSLUStringHash dataname,
261 const TypeDesc datatype,
262 void *data,
263 OSLUStringHash *errormessage) override;
264
265 bool get_texture_info(OSLUStringHash filename,
266 TextureHandle *texture_handle,
267 float s,
268 float t,
269 TexturePerthread *texture_thread_info,
270 OSL::ShaderGlobals *sg,
271 const int subimage,
272 OSLUStringHash dataname,
273 const TypeDesc datatype,
274 void *data,
275 OSLUStringHash *errormessage) override;
276
277 static bool get_attribute(ShaderGlobals *globals,
278 ShaderData *sd,
279 bool derivatives,
280 OSLUStringHash object_name,
281 TypeDesc type,
282 OSLUStringHash name,
283 void *val);
284
285 static bool get_background_attribute(ShaderGlobals *globals,
286 ShaderData *sd,
287 OSLUStringHash name,
288 TypeDesc type,
289 bool derivatives,
290 void *val);
291 static bool get_camera_attribute(
292 ShaderGlobals *globals, OSLUStringHash name, TypeDesc type, bool derivatives, void *val);
293 static bool get_object_standard_attribute(ShaderGlobals *globals,
294 ShaderData *sd,
295 OSLUStringHash name,
296 TypeDesc type,
297 bool derivatives,
298 void *val);
299
300 /* Texture system and texture handle map are part of the services instead of
301 * globals to be shared between different render sessions. This saves memory,
302 * and is required because texture handles are cached as part of the shared
303 * shading system. */
305
306 /* Image manager to add textures to, for shading contexts used to JIT compile
307 * shader groups. Multiple render sessions use this render services, so they are
308 * keyed by shading context. */
309 void image_manager_add(const OSL::ShadingContext *context, ImageManager *image_manager);
310 void image_manager_remove(const OSL::ShadingContext *context);
311
312 private:
313 ImageManager *image_manager_find(const OSL::ShadingContext *context);
314
315 unordered_map<const OSL::ShadingContext *, ImageManager *> image_managers_;
316 thread_mutex image_managers_mutex_;
317
318 int device_type_;
319
320 /* We don't support lookup by filename without a handle, which is required anyway
321 * for GPU, and simplifies the implementation on CPU. This keeps track of the
322 * ones we have seen to emit a warning only once. */
323 OSLTextureFilenameMap texture_filenames_seen_;
324
325 thread_mutex textures_mutex;
326};
327
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
static bool get_background_attribute(ShaderGlobals *globals, ShaderData *sd, OSLUStringHash name, TypeDesc type, bool derivatives, void *val)
Definition services.cpp:161
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
Definition services.cpp:430
bool is_udim(OSL::TextureSystem::TextureHandle *texture_handle) override
Definition services.cpp:311
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
Definition services.cpp:317
bool get_inverse_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time) override
Definition services.cpp:68
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
Definition services.cpp:482
~OSLRenderServices() override
int supports(string_view feature) const override
Definition services.cpp:42
static bool get_object_standard_attribute(ShaderGlobals *globals, ShaderData *sd, OSLUStringHash name, TypeDesc type, bool derivatives, void *val)
Definition services.cpp:150
static bool get_camera_attribute(ShaderGlobals *globals, OSLUStringHash name, TypeDesc type, bool derivatives, void *val)
Definition services.cpp:172
bool get_matrix(OSL::ShaderGlobals *sg, OSL::Matrix44 &result, OSL::TransformationPtr xform, float time) override
Definition services.cpp:55
OSLRenderServices(const int device_type)
Definition services.cpp:32
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
Definition services.cpp:499
OSLTextureHandleMap textures
Definition services.h:304
static void register_closures(OSL::ShadingSystem *ss)
Definition closures.cpp:68
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
Definition services.cpp:515
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
Definition services.cpp:395
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:139
bool get_attribute(OSL::ShaderGlobals *sg, bool derivatives, OSLUStringHash object, const TypeDesc type, OSLUStringHash name, void *val) override
Definition services.cpp:178
bool get_userdata(bool derivatives, OSLUStringHash name, const TypeDesc type, OSL::ShaderGlobals *sg, void *val) override
Definition services.cpp:238
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
Definition services.cpp:526
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
Definition services.cpp:358
void image_manager_remove(const OSL::ShadingContext *context)
Definition services.cpp:260
bool getmessage(OSL::ShaderGlobals *sg, OSLUStringHash source, OSLUStringHash name, const TypeDesc type, void *val, bool derivatives) override
Definition services.cpp:611
void image_manager_add(const OSL::ShadingContext *context, ImageManager *image_manager)
Definition services.cpp:253
OSL::TextureSystem::TextureHandle * get_texture_handle(OSL::ustring filename, OSL::ShadingContext *context, const OSL::TextureOpt *options) override
Definition services.cpp:273
bool good(OSL::TextureSystem::TextureHandle *texture_handle) override
Definition services.cpp:305
Definition scene.h:133
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
#define index(i)
OSLTextureHandleType
#define R
OSL::ustringhash OSLUStringHash
Definition osl/compat.h:11
OSL::ustringrep OSLUStringRep
Definition osl/compat.h:15
concurrent_set< OSLUStringHash > OSLTextureFilenameMap
Definition services.h:82
OIIO::unordered_map_concurrent< OSLTextureKey, OSLTextureHandle, OSLTextureKeyHash > OSLTextureHandleMap
Definition services.h:80
OSLTextureHandle(const ImageHandle &handle)
Definition services.h:55
OSLTextureHandleType type
Definition services.h:57
ImageHandle handle
Definition services.h:59
OSLTextureHandle(const OSLTextureHandleType type, const int id=-1)
Definition services.h:53
size_t operator()(const OSLTextureKey &key) const
Definition services.h:74
OSLUStringHash filename
Definition services.h:67
const ImageManager * image_manager
Definition services.h:68
bool operator==(const OSLTextureKey &other) const =default
std::mutex thread_mutex
Definition thread.h:27