Blender V5.3
osl.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "device/device.h"
6
7#include "scene/background.h"
8#include "scene/camera.h"
9#include "scene/light.h"
10#include "scene/osl.h"
11#include "scene/scene.h"
12#include "scene/shader.h"
13#include "scene/shader_graph.h"
14#include "scene/shader_nodes.h"
15#include "scene/stats.h"
16
17#ifdef WITH_OSL
18
19# include "kernel/osl/globals.h"
20# include "kernel/osl/services.h"
21
22# include "util/aligned_malloc.h"
23# include "util/log.h"
24# include "util/md5.h"
25# include "util/path.h"
26# include "util/progress.h"
27# include "util/projection.h"
28# include "util/task.h"
29
30#endif
31
33
34#ifdef WITH_OSL
35
36/* Shared Texture and Shading System */
37
38thread_mutex ts_shared_mutex;
39
40map<DeviceType, std::shared_ptr<OSL::ShadingSystem>> ss_shared;
41thread_mutex ss_shared_mutex;
42OSL::ErrorHandler errhandler;
43
44std::atomic<int> OSLCompiler::texture_shared_unique_id = 0;
45
46/* Shader Manager */
47
48OSLManager::OSLManager(Device *device) : device_(device), need_update_(true) {}
49
51{
52 shading_system_free();
53}
54
56{
57# ifdef OSL_HAS_BLENDER_CLEANUP_FIX
58 /* There is a problem with LLVM+OSL: The order global destructors across
59 * different compilation units run cannot be guaranteed, on windows this means
60 * that the LLVM destructors run before the osl destructors, causing a crash
61 * when the process exits. the OSL in svn has a special cleanup hack to
62 * sidestep this behavior */
63 OSL::pvt::LLVM_Util::Cleanup();
64# endif
65}
66
67void OSLManager::reset(Scene * /*scene*/)
68{
69 shading_system_free();
70 tag_update();
71}
72
73OSL::ShadingSystem *OSLManager::get_shading_system(Device *sub_device)
74{
75 return ss_map[sub_device->info.type].get();
76}
77
78void OSLManager::foreach_shading_system(const std::function<void(OSL::ShadingSystem *)> &callback)
79{
80 for (const auto &[device_type, ss] : ss_map) {
81 callback(ss.get());
82 }
83}
84
85void OSLManager::foreach_render_services(const std::function<void(OSLRenderServices *)> &callback)
86{
87 for (const auto &[device_type, ss] : ss_map) {
88 callback(static_cast<OSLRenderServices *>(ss->renderer()));
89 }
90}
91
92void OSLManager::foreach_osl_device(Device *device,
93 const std::function<void(Device *, OSLGlobals *)> &callback)
94{
95 device->foreach_device([callback](Device *sub_device) {
96 OSLGlobals *og = sub_device->get_cpu_osl_memory();
97 if (og != nullptr) {
98 callback(sub_device, og);
99 }
100 });
101}
102
104{
105 need_update_ = true;
106}
107
108bool OSLManager::need_update() const
109{
110 return need_update_;
111}
112
113void OSLManager::device_update_pre(Device * /*device*/, Scene *scene)
114{
115 if (scene->shader_manager->use_osl() || !scene->camera->script_name.empty()) {
116 shading_system_init(scene->shader_manager->get_scene_linear_interop_id());
117 }
118
119 if (!need_update()) {
120 return;
121 }
122
123 /* set texture system (only on CPU devices, since GPU devices cannot use OIIO) */
124 if (scene->shader_manager->use_osl()) {
125 /* add special builtin texture types */
126 const ImageManager *image_manager = scene->image_manager.get();
127 foreach_render_services([image_manager](OSLRenderServices *services) {
128 services->textures.insert({OSLUStringHash("@ao"), image_manager},
130 services->textures.insert({OSLUStringHash("@bevel"), image_manager},
132 });
133 }
134}
135
136/* Thread infos reused for optimizing multiple groups. Each thread info that
137 * JITs creates a memory manager that OSL keeps as long as the shading system
138 * exists. This only reduces the problem and does not solve it for repeated
139 * compilation, and OSL's own optimize_all_groups() has the same problem. */
140class OSLThreadInfoPool {
141 public:
142 ~OSLThreadInfoPool()
143 {
144 for (const auto &[ss, thread_info] : thread_infos_) {
145 ss->destroy_thread_info(thread_info);
146 }
147 }
148
149 OSL::PerThreadInfo *acquire(OSL::ShadingSystem *ss)
150 {
151 const thread_scoped_lock lock(mutex_);
152 for (auto it = thread_infos_.begin(); it != thread_infos_.end(); ++it) {
153 if (it->first == ss) {
154 OSL::PerThreadInfo *thread_info = it->second;
155 thread_infos_.erase(it);
156 return thread_info;
157 }
158 }
159 return ss->create_thread_info();
160 }
161
162 void release(OSL::ShadingSystem *ss, OSL::PerThreadInfo *thread_info)
163 {
164 const thread_scoped_lock lock(mutex_);
165 thread_infos_.emplace_back(ss, thread_info);
166 }
167
168 private:
169 thread_mutex mutex_;
170 vector<std::pair<OSL::ShadingSystem *, OSL::PerThreadInfo *>> thread_infos_;
171};
172
173static void optimize_group(OSLThreadInfoPool &thread_infos,
174 OSL::ShadingSystem *ss,
175 ImageManager *image_manager,
176 OSL::ShaderGroup *group)
177{
178 /* Shading context for this group only, as it can't be shared between threads. It
179 * identifies the image manager in #OSLRenderServices::get_texture_handle(). */
180 OSLRenderServices *services = static_cast<OSLRenderServices *>(ss->renderer());
181 OSL::PerThreadInfo *thread_info = thread_infos.acquire(ss);
182 OSL::ShadingContext *context = ss->get_context(thread_info);
183 services->image_manager_add(context, image_manager);
184
185 ss->optimize_group(group, context, true);
186
187 services->image_manager_remove(context);
188 ss->release_context(context);
189 thread_infos.release(ss, thread_info);
190}
191
192static void optimize_group(TaskPool &pool,
193 OSLThreadInfoPool &thread_infos,
194 OSL::ShadingSystem *ss,
195 ImageManager *image_manager,
196 const OSL::ShaderGroupRef &group_ref)
197{
198 if (!group_ref) {
199 return;
200 }
201
202 /* Workaround #156348: depending on the system-wide `libgcc` version, EH frames
203 * registered and de-registered during OSL JIT might leave the process in a bad state.
204 * There is a bug report about it in the gcc:
205 * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119151
206 *
207 * Note that it is the runtime libgcc version that matters, as it is linked dynamically.
208 */
209 OSL::ShaderGroup *group = group_ref.get();
210# if defined(_WIN32) || defined(__APPLE__)
211 pool.push([&thread_infos, ss, image_manager, group]() {
212 optimize_group(thread_infos, ss, image_manager, group);
213 });
214# else
215 optimize_group(thread_infos, ss, image_manager, group);
216 (void)pool;
217# endif
218}
219
221 Scene *scene,
222 Progress &progress,
223 const bool reload_kernels)
224{
225 /* Create the camera shader. */
226 if (need_update() && !scene->camera->script_name.empty()) {
227 if (progress.get_cancel()) {
228 return;
229 }
230 foreach_osl_device(device, [this, scene](Device *sub_device, OSLGlobals *og) {
231 OSL::ShadingSystem *ss = get_shading_system(sub_device);
232
233 OSL::ShaderGroupRef group = ss->ShaderGroupBegin("camera_group");
234 for (const auto &param : scene->camera->script_params) {
235 const ustring &name = param.first;
236 const vector<uint8_t> &data = param.second.first;
237 const TypeDesc &type = param.second.second;
238 if (type.basetype == TypeDesc::STRING) {
239 const void *string = data.data();
240 ss->Parameter(*group, name, type, (const void *)&string);
241 }
242 else {
243 ss->Parameter(*group, name, type, (const void *)data.data());
244 }
245 }
246 ss->Shader(*group, "shader", scene->camera->script_name, "camera");
247 ss->ShaderGroupEnd(*group);
248
249 og->ss = ss;
250 og->services = static_cast<OSLRenderServices *>(ss->renderer());
251
252 og->camera_state = group;
253 og->use_camera = true;
254
255 /* Memory layout is {P, dPdx, dPdy, D, dDdx, dDdy, T}.
256 * If we request derivs from OSL, it will automatically output them after the main parameter.
257 * However, some scripts might have more efficient ways to compute them explicitly, so if a
258 * script has any of the derivative outputs we use those instead. */
259
260 OSLShaderInfo *info = shader_loaded_info(scene->camera->script_name);
261 const string deriv_args[] = {"dPdx", "dPdy", "dDdx", "dDdy"};
262 bool explicit_derivs = false;
263 for (const auto &arg : deriv_args) {
264 if (info->query.getparam(arg) != nullptr) {
265 explicit_derivs = true;
266 }
267 }
268
269 auto add_param = [&](const char *name, OIIO::TypeDesc type, bool derivs, int offset) {
270 ss->add_symlocs(group.get(),
271 OSL::SymLocationDesc(string_printf("camera.%s", name),
272 type,
273 derivs,
274 OSL::SymArena::Outputs,
275 offset * sizeof(float)));
276 };
277
278 if (explicit_derivs) {
279 add_param("dPdx", OIIO::TypeVector, false, 3);
280 add_param("dPdy", OIIO::TypeVector, false, 6);
281 add_param("dDdx", OIIO::TypeVector, false, 12);
282 add_param("dDdy", OIIO::TypeVector, false, 15);
283 }
284 add_param("position", OIIO::TypePoint, !explicit_derivs, 0);
285 add_param("direction", OIIO::TypeVector, !explicit_derivs, 9);
286 add_param("throughput", OIIO::TypeColor, false, 18);
287 });
288 }
289 else if (need_update()) {
290 foreach_osl_device(device, [](Device *, OSLGlobals *og) {
291 og->camera_state.reset();
292 og->use_camera = false;
293 });
294 }
295
296 if (need_update()) {
297 scoped_callback_timer timer([scene](double time) {
298 if (scene->update_stats) {
299 scene->update_stats->osl.times.add_entry({"jit", time});
300 }
301 });
302
303 /* Perform greedyjit optimization.
304 *
305 * This might waste time on optimizing groups which are never actually
306 * used, but this prevents OSL from allocating data on TLS at render
307 * time.
308 *
309 * This is much better for us because this way we aren't required to
310 * stop task scheduler threads to make sure all TLS is clean and don't
311 * have issues with TLS data free accessing freed memory if task scheduler
312 * is being freed after the Session is freed.
313 */
314 const thread_scoped_lock lock(ss_shared_mutex);
315
316 /* Only optimize the groups of this scene, with its own image manager for texture handles.
317 * The shading system is shared with other sessions, which optimize their own groups.
318 *
319 * Isolate to avoid running tasks that wait for the lock held by this thread. */
320 ImageManager *image_manager = scene->image_manager.get();
321 isolate_task([&]() {
322 OSLThreadInfoPool thread_infos;
323 TaskPool pool;
324
325 foreach_osl_device(device, [&](Device *sub_device, OSLGlobals *og) {
326 const auto ss_it = ss_map.find(sub_device->info.type);
327 if (ss_it == ss_map.end()) {
328 return;
329 }
330 OSL::ShadingSystem *ss = ss_it->second.get();
331 for (const vector<OSL::ShaderGroupRef> *states :
332 {&og->surface_state, &og->volume_state, &og->displacement_state, &og->bump_state})
333 {
334 for (const OSL::ShaderGroupRef &group_ref : *states) {
335 optimize_group(pool, thread_infos, ss, image_manager, group_ref);
336 }
337 }
338 optimize_group(pool, thread_infos, ss, image_manager, og->camera_state);
339 });
340
341 pool.wait_work();
342 });
343 }
344
345 /* Load OSL kernels on changes to shaders, or when main kernels got reloaded. */
346 if (need_update() || reload_kernels) {
347 foreach_osl_device(device, [this, &progress](Device *sub_device, OSLGlobals *og) {
348 if (og->use_shading || og->use_camera) {
349 OSL::ShadingSystem *ss = get_shading_system(sub_device);
350
351 og->ss = ss;
352 og->services = static_cast<OSLRenderServices *>(ss->renderer());
353
354 /* load kernels */
355 if (!sub_device->load_osl_kernels()) {
356 progress.set_error(sub_device->error_message());
357 }
358 }
359 });
360 }
361
362 need_update_ = false;
363}
364
365void OSLManager::device_free(Device *device, DeviceScene * /*dscene*/, Scene *scene)
366{
367 /* clear shader engine */
368 foreach_osl_device(device, [](Device *, OSLGlobals *og) {
369 og->use_shading = false;
370 og->use_camera = false;
371 og->ss = nullptr;
372 og->camera_state.reset();
373 });
374
375 /* Remove any textures specific to an image manager from shared render services textures, since
376 * the image manager may get destroyed next. */
377 foreach_render_services([scene](OSLRenderServices *services) {
378 for (auto it = services->textures.begin(); it != services->textures.end();) {
379 if (it->first.image_manager == scene->image_manager.get()) {
380 /* Don't lock again, since the iterator already did so. */
381 services->textures.erase(it->first, false);
382 it.clear();
383 /* Iterator was invalidated, start from the beginning again. */
384 it = services->textures.begin();
385 }
386 else {
387 ++it;
388 }
389 }
390 });
391}
392
393void OSLManager::shading_system_init(const string &colorspace_interop_id)
394{
395 /* No need to do anything if we already have shading systems. */
396 if (!ss_map.empty()) {
397 return;
398 }
399
400 /* create shading system, shared between different renders to reduce memory usage */
401 const thread_scoped_lock lock(ss_shared_mutex);
402
403 foreach_osl_device(device_, [this, colorspace_interop_id](Device *sub_device, OSLGlobals *) {
404 const DeviceType device_type = sub_device->info.type;
405
406 if (!ss_shared[device_type]) {
407 OSLRenderServices *services = util_aligned_new<OSLRenderServices>(device_type);
408# ifdef _WIN32
409 /* Annoying thing, Cycles stores paths in UTF8 code-page, so it can
410 * operate with file paths with any character. This requires to use wide
411 * char functions, but OSL uses old fashioned ANSI functions which means:
412 *
413 * - We have to convert our paths to ANSI before passing to OSL
414 * - OSL can't be used when there's a multi-byte character in the path
415 * to the shaders folder.
416 */
417 const string shader_path = string_to_ansi(path_get("shader"));
418# else
419 const string shader_path = path_get("shader");
420# endif
421
422 /* Dummy texture system pointer so OSL doesn't create its own. Such an opaque texture system
423 * pointer is supported and normally would be done with the OSL_NO_DEFAULT_TEXTURESYSTEM
424 * build option, but we want to work with OSL builds that don't have it. */
425 auto *ts = reinterpret_cast<OSL::TextureSystem *>(1);
426
427 auto ss = std::shared_ptr<OSL::ShadingSystem>(
428 new OSL::ShadingSystem(services, ts, &errhandler), [](OSL::ShadingSystem *ss) {
429 util_aligned_delete(static_cast<OSLRenderServices *>(ss->renderer()));
430 delete ss;
431 });
432 ss->attribute("lockgeom", 1);
433 ss->attribute("commonspace", "world");
434 ss->attribute("searchpath:shader", shader_path);
435 ss->attribute("greedyjit", 1);
436
437 /* OSL doesn't accept an arbitrary space, so support a few specific spaces. */
438 if (colorspace_interop_id == "lin_rec709_scene") {
439 ss->attribute("colorspace", OSL::Strings::Rec709);
440 }
441 else if (colorspace_interop_id == "lin_rec2020_scene") {
442 ss->attribute("colorspace", OSL::Strings::HDTV);
443 }
444 else if (colorspace_interop_id == "lin_ap1_scene") {
445 ss->attribute("colorspace", OSL::Strings::ACEScg);
446 }
447
448 const char *groupdata_alloc_str = getenv("CYCLES_OSL_GROUPDATA_ALLOC");
449 if (groupdata_alloc_str) {
450 ss->attribute("max_optix_groupdata_alloc", atoi(groupdata_alloc_str));
451 }
452 else {
453 ss->attribute("max_optix_groupdata_alloc", 2048);
454 }
455
456 LOG_INFO << "Using shader search path: " << shader_path;
457
458 /* our own ray types */
459 static const char *raytypes[] = {
460 /* Bits from the PathRayVisibilityFlag.
461 * Low word of 16 bits of the packed raytype. */
462 "camera", /* Bit 0: PATH_RAY_VISIBILITY_CAMERA */
463 "refraction", /* Bit 1: PATH_RAY_VISIBILITY_TRANSMIT */
464 "diffuse", /* Bit 2: PATH_RAY_VISIBILITY_DIFFUSE */
465 "glossy", /* Bit 3: PATH_RAY_VISIBILITY_GLOSSY */
466 "volume_scatter", /* Bit 4: PATH_RAY_VISIBILITY_VOLUME_SCATTER */
467 "shadow", /* Bit 5: PATH_RAY_VISIBILITY_SHADOW_OPAQUE */
468 "shadow", /* Bit 6: PATH_RAY_VISIBILITY_SHADOW_TRANSPARENT */
469 "__unused__", /* Bit 7 */
470 "__unused__", /* Bit 8 */
471 "__unused__", /* Bit 9 */
472 "__unused__", /* Bit 10 */
473 "__unused__", /* Bit 11 */
474 "__unused__", /* Bit 12 */
475 "__unused__", /* Bit 13 */
476 "__unused__", /* Bit 14 */
477 "__unused__", /* Bit 15 */
478
479 /* Bits from the PathRayFlag.
480 * High word of 16 bits of the packed raytype. */
481
482 "reflection", /* Bit 0: PATH_RAY_REFLECT */
483 "singular", /* Bit 1: PATH_RAY_SINGULAR */
484 "transparent", /* Bit 2: PATH_RAY_TRANSPARENT */
485 "importance_bake", /* Bit 3: PATH_RAY_IMPORTANCE_BAKE */
486 "diffuse_ancestor", /* Bit 4: PATH_RAY_DIFFUSE_ANCESTOR */
487 "__unused__", /* Bit 5 */
488 "__unused__", /* Bit 6 */
489 "__unused__", /* Bit 7 */
490 "__unused__", /* Bit 8 */
491 "__unused__", /* Bit 9 */
492 "__unused__", /* Bit 10 */
493 "__unused__", /* Bit 11 */
494 "__unused__", /* Bit 12 */
495 "__unused__", /* Bit 13 */
496 "__unused__", /* Bit 14 */
497 "__unused__", /* Bit 15 */
498 };
499
500 const int nraytypes = sizeof(raytypes) / sizeof(raytypes[0]);
501 ss->attribute("raytypes", TypeDesc(TypeDesc::STRING, nraytypes), (const void *)raytypes);
502
504 ss_shared[device_type] = std::move(ss);
505 }
506 ss_map[device_type] = ss_shared[device_type];
507 });
508}
509
510void OSLManager::shading_system_free()
511{
512 ss_map.clear();
513
514 /* if ss_shared is the only reference to the underlying shading system,
515 * no users remain, so free it. */
516 const thread_scoped_lock lock(ss_shared_mutex);
517 for (auto &[device_type, ss] : ss_shared) {
518 if (ss.use_count() == 1) {
519 ss.reset();
520 }
521 }
522
523 loaded_shaders.clear();
524}
525
526bool OSLManager::osl_compile(const string &inputfile, const string &outputfile)
527{
528 vector<string> options;
529 string stdosl_path;
530 const string shader_path = path_get("shader");
531
532 /* Specify output file name. */
533 options.push_back("-o");
534 options.push_back(outputfile);
535
536 /* Specify standard include path. */
537 const string include_path_arg = string("-I") + shader_path;
538 options.push_back(include_path_arg);
539
540 stdosl_path = path_join(shader_path, "stdcycles.h");
541
542 /* Compile.
543 *
544 * Mutex protected because the OSL compiler does not appear to be thread safe, see #92503. */
545 static thread_mutex osl_compiler_mutex;
546 const thread_scoped_lock lock(osl_compiler_mutex);
547
548 OSL::OSLCompiler compiler = OSL::OSLCompiler(&OSL::ErrorHandler::default_handler());
549 const bool ok = compiler.compile(string_view(inputfile), options, string_view(stdosl_path));
550
551 return ok;
552}
553
554bool OSLManager::osl_query(OSL::OSLQuery &query, const string &filepath)
555{
556 const string searchpath = path_user_get("shaders");
557 return query.open(filepath, searchpath);
558}
559
560static string shader_filepath_hash(const string &filepath, const uint64_t modified_time)
561{
562 /* compute a hash from filepath and modified time to detect changes */
563 MD5Hash md5;
564 md5.append((const uint8_t *)filepath.c_str(), filepath.size());
565 md5.append((const uint8_t *)&modified_time, sizeof(modified_time));
566
567 return md5.get_hex();
568}
569
570const char *OSLManager::shader_test_loaded(const string &hash)
571{
572 const map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
573 return (it == loaded_shaders.end()) ? nullptr : it->first.c_str();
574}
575
576OSLShaderInfo *OSLManager::shader_loaded_info(const string &hash)
577{
578 const map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(hash);
579 return (it == loaded_shaders.end()) ? nullptr : &it->second;
580}
581
582const char *OSLManager::shader_load_filepath(string filepath)
583{
584 const size_t len = filepath.size();
585 const string extension = filepath.substr(len - 4);
586 uint64_t modified_time = path_modified_time(filepath);
587
588 if (extension == ".osl") {
589 /* .OSL File */
590 const string osopath = filepath.substr(0, len - 4) + ".oso";
591 const uint64_t oso_modified_time = path_modified_time(osopath);
592
593 /* test if we have loaded the corresponding .OSO already */
594 if (oso_modified_time != 0) {
595 const char *hash = shader_test_loaded(shader_filepath_hash(osopath, oso_modified_time));
596
597 if (hash) {
598 return hash;
599 }
600 }
601
602 /* Auto-compile .OSL to .OSO if needed. */
603 if (oso_modified_time == 0 || (oso_modified_time < modified_time)) {
604 OSLManager::osl_compile(filepath, osopath);
605 modified_time = path_modified_time(osopath);
606 }
607 else {
608 modified_time = oso_modified_time;
609 }
610
611 filepath = osopath;
612 }
613 else {
614 if (extension == ".oso") {
615 /* .OSO File, nothing to do */
616 }
617 else if (path_dirname(filepath).empty()) {
618 /* .OSO File in search path */
619 filepath = path_join(path_user_get("shaders"), filepath + ".oso");
620 }
621 else {
622 /* unknown file */
623 return nullptr;
624 }
625
626 /* test if we have loaded this .OSO already */
627 const char *hash = shader_test_loaded(shader_filepath_hash(filepath, modified_time));
628
629 if (hash) {
630 return hash;
631 }
632 }
633
634 /* read oso bytecode from file */
635 const string bytecode_hash = shader_filepath_hash(filepath, modified_time);
636 string bytecode;
637
638 if (!path_read_text(filepath, bytecode)) {
639 LOG_ERROR << "Shader graph: failed to read file " << filepath;
640 const OSLShaderInfo info;
641 loaded_shaders[bytecode_hash] = info; /* to avoid repeat tries */
642 return nullptr;
643 }
644
645 return shader_load_bytecode(bytecode_hash, bytecode);
646}
647
648const char *OSLManager::shader_load_bytecode(const string &hash, const string &bytecode)
649{
650 foreach_shading_system(
651 [hash, bytecode](OSL::ShadingSystem *ss) { ss->LoadMemoryCompiledShader(hash, bytecode); });
652
653 tag_update();
654
655 OSLShaderInfo info;
656
657 if (!info.query.open_bytecode(bytecode)) {
658 LOG_ERROR << "OSL query error: " << info.query.geterror();
659 }
660
661 /* this is a bit weak, but works */
662 info.has_surface_emission = (bytecode.find("\"emission\"") != string::npos);
663 info.has_surface_transparent = (bytecode.find("\"transparent\"") != string::npos);
664 info.has_surface_bssrdf = (bytecode.find("\"bssrdf\"") != string::npos);
665
666 loaded_shaders[hash] = info;
667
668 return loaded_shaders.find(hash)->first.c_str();
669}
670
671uint64_t OSLShaderManager::get_attribute_id(ustring name)
672{
673 return name.hash();
674}
675
676uint64_t OSLShaderManager::get_attribute_id(AttributeStandard std)
677{
678 return Attribute::osl_name(std).hash();
679}
680
681void OSLShaderManager::device_update_specific(Device *device,
682 DeviceScene *dscene,
683 Scene *scene,
684 Progress &progress)
685{
686 if (!need_update()) {
687 return;
688 }
689
690 scoped_callback_timer timer([scene](double time) {
691 if (scene->update_stats) {
692 scene->update_stats->osl.times.add_entry({"device_update", time});
693 }
694 });
695
696 LOG_INFO << "Total " << scene->shaders.size() << " shaders.";
697
698 /* setup shader engine */
699 OSLManager::foreach_osl_device(device, [scene](Device *sub_device, OSLGlobals *og) {
700 OSL::ShadingSystem *ss = scene->osl_manager->get_shading_system(sub_device);
701 og->ss = ss;
702 og->services = static_cast<OSLRenderServices *>(ss->renderer());
703
704 og->use_shading = true;
705
706 og->surface_state.clear();
707 og->volume_state.clear();
708 og->displacement_state.clear();
709 og->bump_state.clear();
710 og->background_state.reset();
711 });
712
713 /* create shaders */
714 Shader *background_shader = scene->background->get_shader(scene);
715
716 /* compile each shader to OSL shader groups */
717 TaskPool task_pool;
718 for (Shader *shader : scene->shaders) {
719 assert(shader->graph);
720
721 auto compile = [scene, &progress, shader, background_shader](Device *sub_device,
722 OSLGlobals *) {
723 OSL::ShadingSystem *ss = scene->osl_manager->get_shading_system(sub_device);
724
725 OSLCompiler compiler(ss, scene, progress, sub_device);
726 compiler.background = (shader == background_shader);
727 compiler.compile(shader);
728 };
729
730 task_pool.push([device, compile] { OSLManager::foreach_osl_device(device, compile); });
731 }
732 task_pool.wait_work();
733
734 if (progress.get_cancel()) {
735 return;
736 }
737
738 /* collect shader groups from all shaders */
739 for (Shader *shader : scene->shaders) {
740 OSLManager::OSLManager::foreach_osl_device(
741 device, [shader, background_shader](Device *sub_device, OSLGlobals *og) {
742 /* push state to array for lookup */
743 const Shader::OSLCache &cache = shader->osl_cache[sub_device];
744 og->surface_state.push_back(cache.surface);
745 og->volume_state.push_back(cache.volume);
746 og->displacement_state.push_back(cache.displacement);
747 og->bump_state.push_back(cache.bump);
748
749 if (shader == background_shader) {
750 og->background_state = cache.surface;
751 }
752 });
753
755 scene->light_manager->tag_update(scene, LightManager::SHADER_COMPILED);
756 }
757
758 scene->osl_manager->tag_update();
759 }
760
761 /* set background shader */
762 int background_id = scene->shader_manager->get_shader_id(background_shader);
763
764 OSLManager::foreach_osl_device(device, [background_id](Device *, OSLGlobals *og) {
765 og->background_state = og->surface_state[background_id & SHADER_MASK];
766 });
767
768 for (Shader *shader : scene->shaders) {
769 shader->clear_modified();
770 }
771
772 update_flags = UPDATE_NONE;
773
774 device_update_common(device, dscene, scene, progress);
775}
776
777void OSLShaderManager::device_free(Device *device, DeviceScene *dscene, Scene *scene)
778{
779 device_free_common(device, dscene, scene);
780
781 /* clear shader engine */
782 OSLManager::foreach_osl_device(device, [](Device *, OSLGlobals *og) {
783 og->use_shading = false;
784
785 og->surface_state.clear();
786 og->volume_state.clear();
787 og->displacement_state.clear();
788 og->bump_state.clear();
789 og->background_state.reset();
790 });
791}
792
793/* This is a static function to avoid RTTI link errors with only this
794 * file being compiled without RTTI to match OSL and LLVM libraries. */
795OSLNode *OSLShaderManager::osl_node(ShaderGraph *graph,
796 Scene *scene,
797 const std::string &filepath,
798 const std::string &bytecode_hash,
799 const std::string &bytecode)
800{
801 if (!scene->shader_manager->use_osl()) {
802 return nullptr;
803 }
804
805 /* Ensure shading system exists before we try to load a shader. */
806 scene->osl_manager->shading_system_init(scene->shader_manager->get_scene_linear_interop_id());
807
808 /* Load shader code. */
809 const char *hash;
810
811 if (!filepath.empty()) {
812 hash = scene->osl_manager->shader_load_filepath(filepath);
813 }
814 else {
815 hash = scene->osl_manager->shader_test_loaded(bytecode_hash);
816 if (!hash) {
817 hash = scene->osl_manager->shader_load_bytecode(bytecode_hash, bytecode);
818 }
819 }
820
821 if (!hash) {
822 return nullptr;
823 }
824
825 OSLShaderInfo *info = scene->osl_manager->shader_loaded_info(hash);
826
827 /* count number of inputs */
828 size_t num_inputs = 0;
829
830 for (int i = 0; i < info->query.nparams(); i++) {
831 const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
832
833 /* skip unsupported types */
834 if (param->varlenarray || param->isstruct || param->type.arraylen > 1) {
835 continue;
836 }
837
838 if (!param->isoutput) {
839 num_inputs++;
840 }
841 }
842
843 /* create node */
844 OSLNode *node = OSLNode::create(graph, num_inputs);
845
846 /* add new sockets from parameters */
847 const set<void *> used_sockets;
848
849 for (int i = 0; i < info->query.nparams(); i++) {
850 const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
851
852 /* skip unsupported types */
853 if (param->varlenarray || param->isstruct || param->type.arraylen > 1) {
854 continue;
855 }
856
857 SocketType::Type socket_type;
858
859 /* Read type and default value. */
860 if (param->isclosure) {
861 socket_type = SocketType::CLOSURE;
862 }
863 else if (param->type.vecsemantics != TypeDesc::NOSEMANTICS) {
864 if (param->type.vecsemantics == TypeDesc::COLOR) {
865 socket_type = SocketType::COLOR;
866 }
867 else if (param->type.vecsemantics == TypeDesc::POINT) {
868 socket_type = SocketType::POINT;
869 }
870 else if (param->type.vecsemantics == TypeDesc::VECTOR) {
871 socket_type = SocketType::VECTOR;
872 }
873 else if (param->type.vecsemantics == TypeDesc::NORMAL) {
874 socket_type = SocketType::NORMAL;
875 }
876 else {
877 continue;
878 }
879
880 if (!param->isoutput && param->validdefault) {
881 float3 *default_value = (float3 *)node->input_default_value();
882 default_value->x = param->fdefault[0];
883 default_value->y = param->fdefault[1];
884 default_value->z = param->fdefault[2];
885 }
886 }
887 else if (param->type.aggregate == TypeDesc::SCALAR) {
888 if (param->type.basetype == TypeDesc::INT) {
889 socket_type = SocketType::INT;
890
891 if (!param->isoutput && param->validdefault) {
892 *(int *)node->input_default_value() = param->idefault[0];
893 }
894 }
895 else if (param->type.basetype == TypeDesc::FLOAT) {
896 socket_type = SocketType::FLOAT;
897
898 if (!param->isoutput && param->validdefault) {
899 *(float *)node->input_default_value() = param->fdefault[0];
900 }
901 }
902 else if (param->type.basetype == TypeDesc::STRING) {
903 socket_type = SocketType::STRING;
904
905 if (!param->isoutput && param->validdefault) {
906 *(ustring *)node->input_default_value() = param->sdefault[0];
907 }
908 }
909 else {
910 continue;
911 }
912 }
913 else {
914 continue;
915 }
916
917 if (param->isoutput) {
918 node->add_output(param->name, socket_type);
919 }
920 else {
921 /* Detect if we should leave parameter initialization to OSL, either though
922 * not constant default or widget metadata. */
923 int socket_flags = 0;
924 if (!param->validdefault) {
925 socket_flags |= SocketType::LINK_OSL_INITIALIZER;
926 }
927 for (const OSL::OSLQuery::Parameter &metadata : param->metadata) {
928 if (metadata.type == TypeDesc::STRING) {
929 if (metadata.name == "widget" && metadata.sdefault[0] == "null") {
930 socket_flags |= SocketType::LINK_OSL_INITIALIZER;
931 }
932 else if (metadata.name == "mtlx_defaultgeomprop") {
933 /* the following match up to MaterialX default geometry properties
934 * that we use to help set socket flags to the corresponding
935 * geometry link equivalents. */
936 if (metadata.sdefault[0] == "Nobject") {
937 socket_flags |= SocketType::LINK_TEXTURE_NORMAL;
938 }
939 else if (metadata.sdefault[0] == "Nworld") {
940 socket_flags |= SocketType::LINK_NORMAL;
941 }
942 else if (metadata.sdefault[0] == "Pobject") {
944 }
945 else if (metadata.sdefault[0] == "Pworld") {
946 socket_flags |= SocketType::LINK_POSITION;
947 }
948 else if (metadata.sdefault[0] == "Tworld") {
949 socket_flags |= SocketType::LINK_TANGENT;
950 }
951 else if (metadata.sdefault[0] == "UV0") {
952 socket_flags |= SocketType::LINK_TEXTURE_UV;
953 }
954 }
955 }
956 }
957
958 node->add_input(param->name, socket_type, socket_flags);
959 }
960 }
961
962 /* Set byte-code hash or file-path. */
963 if (!bytecode_hash.empty()) {
964 node->bytecode_hash = bytecode_hash;
965 }
966 else {
967 node->filepath = filepath;
968 }
969
970 /* Generate inputs and outputs */
971 node->create_inputs_outputs(node->type);
972
973 return node;
974}
975
976void OSLShaderManager::osl_image_handles(Device *device,
977 ImageManager *image_manager,
978 set<const ImageSingle *> &handles)
979{
980 set<OSLRenderServices *> services_shared;
981 device->foreach_device([&services_shared](Device *sub_device) {
982 OSLGlobals *og = sub_device->get_cpu_osl_memory();
983 services_shared.insert(og->services);
984 });
985
986 for (OSLRenderServices *services : services_shared) {
987 for (auto it = services->textures.begin(); it != services->textures.end(); ++it) {
988 const ImageHandle &handle = it->second.handle;
989 if (handle.get_manager() == image_manager && !handle.empty()) {
990 handle.add_to_set(handles);
991 }
992 }
993 }
994}
995
996/* Graph Compiler */
997
998OSLCompiler::OSLCompiler(OSL::ShadingSystem *ss, Scene *scene, Progress &progress, Device *device)
999 : scene(scene),
1000 progress(&progress),
1001 services(static_cast<OSLRenderServices *>(ss->renderer())),
1002 ss(ss),
1003 device(device)
1004{
1005 current_type = SHADER_TYPE_SURFACE;
1006 current_shader = nullptr;
1007 background = false;
1008}
1009
1010string OSLCompiler::id(const ShaderNode *node)
1011{
1012 /* assign layer unique name based on pointer address + bump mode */
1013 std::stringstream stream;
1014
1015 /* Ensure that no grouping characters (e.g. commas with en_US locale)
1016 * are added to the pointer string. */
1017 stream.imbue(std::locale("C"));
1018
1019 stream << "node_" << node->type->name << "_" << node;
1020
1021 return stream.str();
1022}
1023
1024string_view OSLCompiler::get_shader_usage() const
1025{
1026 switch (current_type) {
1028 return "surface";
1029 case SHADER_TYPE_VOLUME:
1030 return "surface";
1032 return "displacement";
1033 case SHADER_TYPE_BUMP:
1034 return "displacement";
1035 }
1036 LOG_DFATAL << "Unhandled shader type " << int(current_type);
1037 return "";
1038}
1039
1040OSLCompiler::LayerParam OSLCompiler::get_output_layer_param(const string_view layer,
1041 const string_view param) const
1042{
1043 const LayerParam layer_param(layer, param);
1044
1045 const auto it = remapped_outputs_.find(layer_param);
1046 if (it != remapped_outputs_.end()) {
1047 return it->second;
1048 }
1049
1050 return layer_param;
1051}
1052
1053string OSLCompiler::compatible_name(ShaderNode *node, ShaderInput *input)
1054{
1055 string sname(input->name().string());
1056 size_t i;
1057
1058 /* Strip white-space. */
1059 while ((i = sname.find(" ")) != string::npos) {
1060 sname.replace(i, 1, "");
1061 }
1062
1063 /* if output exists with the same name, add "In" suffix */
1064 for (ShaderOutput *output : node->outputs) {
1065 if (input->name() == output->name()) {
1066 sname += "In";
1067 break;
1068 }
1069 }
1070
1071 return sname;
1072}
1073
1074string OSLCompiler::compatible_name(ShaderNode *node, ShaderOutput *output)
1075{
1076 string sname(output->name().string());
1077 size_t i;
1078
1079 /* Strip white-space. */
1080 while ((i = sname.find(" ")) != string::npos) {
1081 sname.replace(i, 1, "");
1082 }
1083
1084 /* if input exists with the same name, add "Out" suffix */
1085 for (ShaderInput *input : node->inputs) {
1086 if (input->name() == output->name()) {
1087 sname += "Out";
1088 break;
1089 }
1090 }
1091
1092 return sname;
1093}
1094
1095bool OSLCompiler::node_skip_input(ShaderNode *node, ShaderInput *input)
1096{
1097 /* exception for output node, only one input is actually used
1098 * depending on the current shader type */
1099
1100 if (input->flags() & SocketType::SVM_INTERNAL) {
1101 return true;
1102 }
1103
1104 if (node->special_type == SHADER_SPECIAL_TYPE_OUTPUT) {
1105 if (input->name() == "Surface" && current_type != SHADER_TYPE_SURFACE) {
1106 return true;
1107 }
1108 if (input->name() == "Volume" && current_type != SHADER_TYPE_VOLUME) {
1109 return true;
1110 }
1111 if (input->name() == "Displacement" && current_type != SHADER_TYPE_DISPLACEMENT) {
1112 return true;
1113 }
1114 if (input->name() == "Normal" && current_type != SHADER_TYPE_BUMP) {
1115 return true;
1116 }
1117 }
1118 else if (node->special_type == SHADER_SPECIAL_TYPE_BUMP) {
1119 if (input->name() == "Height") {
1120 return true;
1121 }
1122 }
1123 else if (current_type == SHADER_TYPE_DISPLACEMENT && input->link &&
1125 {
1126 return true;
1127 }
1128
1129 return false;
1130}
1131
1132void OSLCompiler::add(ShaderNode *node, const char *name, bool isfilepath)
1133{
1134 /* load filepath */
1135 if (isfilepath) {
1136 name = scene->osl_manager->shader_load_filepath(name);
1137
1138 if (name == nullptr) {
1139 return;
1140 }
1141 }
1142
1143 /* pass in fixed parameter values */
1144 for (ShaderInput *input : node->inputs) {
1145 if (!input->link) {
1146 /* checks to untangle graphs */
1147 if (node_skip_input(node, input)) {
1148 continue;
1149 }
1150 if ((input->flags() & SocketType::LINK_OSL_INITIALIZER) && !(input->constant_folded_in)) {
1151 continue;
1152 }
1153
1154 const string param_name = compatible_name(node, input);
1155 const SocketType &socket = input->socket_type;
1156 switch (input->type()) {
1157 case SocketType::COLOR:
1158 parameter_color(param_name.c_str(), node->get_float3(socket));
1159 break;
1160 case SocketType::POINT:
1161 parameter_point(param_name.c_str(), node->get_float3(socket));
1162 break;
1163 case SocketType::VECTOR:
1164 parameter_vector(param_name.c_str(), node->get_float3(socket));
1165 break;
1166 case SocketType::NORMAL:
1167 parameter_normal(param_name.c_str(), node->get_float3(socket));
1168 break;
1169 case SocketType::FLOAT:
1170 parameter(param_name.c_str(), node->get_float(socket));
1171 break;
1172 case SocketType::INT:
1173 parameter(param_name.c_str(), node->get_int(socket));
1174 break;
1175 case SocketType::STRING:
1176 parameter(param_name.c_str(), node->get_string(socket));
1177 break;
1180 default:
1181 break;
1182 }
1183 }
1184 }
1185
1186 /* Create shader of the appropriate type. OSL only distinguishes between "surface"
1187 * and "displacement" at the moment. */
1188 ss->Shader(*current_group, get_shader_usage(), name, id(node));
1189
1190 /* link inputs to other nodes */
1191 for (ShaderInput *input : node->inputs) {
1192 if (input->link) {
1193 if (node_skip_input(node, input)) {
1194 continue;
1195 }
1196
1197 /* connect shaders */
1198 const LayerParam layer_param_from = get_output_layer_param(
1199 id(input->link->parent), compatible_name(input->link->parent, input->link));
1200 const string id_to = id(node);
1201 const string param_to = compatible_name(node, input);
1202
1203 ss->ConnectShaders(
1204 *current_group, layer_param_from.layer, layer_param_from.param, id_to, param_to);
1205 }
1206 }
1207
1208 /* test if we shader contains specific closures */
1209 OSLShaderInfo *info = scene->osl_manager->shader_loaded_info(name);
1210
1211 if (current_type == SHADER_TYPE_SURFACE) {
1212 if (info) {
1213 if (info->has_surface_emission && node->special_type == SHADER_SPECIAL_TYPE_OSL) {
1214 /* Will be used by Shader::estimate_emission. */
1215 OSLNode *oslnode = static_cast<OSLNode *>(node);
1216 oslnode->has_emission = true;
1217 }
1218 if (info->has_surface_transparent) {
1219 current_shader->has_surface_transparent = true;
1220 }
1221 if (info->has_surface_bssrdf) {
1222 current_shader->has_surface_bssrdf = true;
1223 current_shader->has_bssrdf_bump = true; /* can't detect yet */
1224 }
1225 current_shader->has_bump_from_surface = true; /* can't detect yet */
1226 current_shader->has_surface_raytrace = true; /* can't detect yet */
1227 }
1228
1229 if (node->has_spatial_varying()) {
1230 current_shader->has_surface_spatial_varying = true;
1231 }
1232 }
1233 else if (current_type == SHADER_TYPE_VOLUME) {
1234 if (node->has_spatial_varying()) {
1235 current_shader->has_volume_spatial_varying = true;
1236 }
1237 if (node->has_attribute_dependency()) {
1238 current_shader->has_volume_attribute_dependency = true;
1239 }
1240 }
1241}
1242
1244 string_view converter_name,
1245 string_view node_output_parameter,
1246 string_view converter_input_parameter,
1247 string_view converter_output_parameter,
1248 string_view shader_output_parameter)
1249{
1250 const string node_id = id(node);
1251 const string converter_id = node_id + "_" + string(shader_output_parameter);
1252
1253 ss->Shader(*current_group, get_shader_usage(), converter_name, converter_id);
1254 ss->ConnectShaders(
1255 *current_group, node_id, node_output_parameter, converter_id, converter_input_parameter);
1256
1257 remapped_outputs_.insert(pair(LayerParam(node_id, shader_output_parameter),
1258 LayerParam(converter_id, converter_output_parameter)));
1259}
1260
1261static TypeDesc array_typedesc(const TypeDesc typedesc, const int arraylength)
1262{
1263 return TypeDesc((TypeDesc::BASETYPE)typedesc.basetype,
1264 (TypeDesc::AGGREGATE)typedesc.aggregate,
1265 (TypeDesc::VECSEMANTICS)typedesc.vecsemantics,
1266 arraylength);
1267}
1268
1269void OSLCompiler::parameter(ShaderNode *node, const char *name)
1270{
1271 const ustring uname = ustring(name);
1272 const SocketType &socket = *(node->type->find_input(uname));
1273
1274 switch (socket.type) {
1275 case SocketType::BOOLEAN: {
1276 int value = node->get_bool(socket);
1277 ss->Parameter(*current_group, name, TypeInt, &value);
1278 break;
1279 }
1280 case SocketType::FLOAT: {
1281 float value = node->get_float(socket);
1282 ss->Parameter(*current_group, uname, TypeFloat, &value);
1283 break;
1284 }
1285 case SocketType::INT: {
1286 int value = node->get_int(socket);
1287 ss->Parameter(*current_group, uname, TypeInt, &value);
1288 break;
1289 }
1290 case SocketType::COLOR: {
1291 float3 value = node->get_float3(socket);
1292 ss->Parameter(*current_group, uname, TypeColor, &value);
1293 break;
1294 }
1295 case SocketType::VECTOR: {
1296 float3 value = node->get_float3(socket);
1297 ss->Parameter(*current_group, uname, TypeVector, &value);
1298 break;
1299 }
1300 case SocketType::POINT: {
1301 float3 value = node->get_float3(socket);
1302 ss->Parameter(*current_group, uname, TypePoint, &value);
1303 break;
1304 }
1305 case SocketType::NORMAL: {
1306 float3 value = node->get_float3(socket);
1307 ss->Parameter(*current_group, uname, TypeNormal, &value);
1308 break;
1309 }
1310 case SocketType::POINT2: {
1311 float2 value = node->get_float2(socket);
1312 ss->Parameter(*current_group,
1313 uname,
1314 TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT),
1315 &value);
1316 break;
1317 }
1318 case SocketType::STRING: {
1319 ustring value = node->get_string(socket);
1320 ss->Parameter(*current_group, uname, TypeString, &value);
1321 break;
1322 }
1323 case SocketType::ENUM: {
1324 ustring value = node->get_string(socket);
1325 ss->Parameter(*current_group, uname, TypeString, &value);
1326 break;
1327 }
1328 case SocketType::TRANSFORM: {
1329 const Transform value = node->get_transform(socket);
1330 ProjectionTransform projection(value);
1331 projection = projection_transpose(projection);
1332 ss->Parameter(*current_group, uname, TypeMatrix, &projection);
1333 break;
1334 }
1336 // OSL does not support booleans, so convert to int
1337 const array<bool> &value = node->get_bool_array(socket);
1338 array<int> intvalue(value.size());
1339 for (size_t i = 0; i < value.size(); i++) {
1340 intvalue[i] = value[i];
1341 }
1342 ss->Parameter(*current_group, uname, array_typedesc(TypeInt, value.size()), intvalue.data());
1343 break;
1344 }
1346 const array<float> &value = node->get_float_array(socket);
1347 ss->Parameter(*current_group, uname, array_typedesc(TypeFloat, value.size()), value.data());
1348 break;
1349 }
1350 case SocketType::INT_ARRAY: {
1351 const array<int> &value = node->get_int_array(socket);
1352 ss->Parameter(*current_group, uname, array_typedesc(TypeInt, value.size()), value.data());
1353 break;
1354 }
1359 TypeDesc typedesc;
1360
1361 switch (socket.type) {
1363 typedesc = TypeColor;
1364 break;
1366 typedesc = TypeVector;
1367 break;
1369 typedesc = TypePoint;
1370 break;
1372 typedesc = TypeNormal;
1373 break;
1374 default:
1375 assert(0);
1376 break;
1377 }
1378
1379 const array<packed_float3> &value = node->get_float3_array(socket);
1380 ss->Parameter(*current_group, uname, array_typedesc(typedesc, value.size()), value.data());
1381 break;
1382 }
1384 const array<float2> &value = node->get_float2_array(socket);
1385 ss->Parameter(
1386 *current_group,
1387 uname,
1388 array_typedesc(TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT), value.size()),
1389 value.data());
1390 break;
1391 }
1393 const array<ustring> &value = node->get_string_array(socket);
1394 ss->Parameter(*current_group, uname, array_typedesc(TypeString, value.size()), value.data());
1395 break;
1396 }
1398 const array<Transform> &value = node->get_transform_array(socket);
1399 array<ProjectionTransform> fvalue(value.size());
1400 for (size_t i = 0; i < value.size(); i++) {
1401 fvalue[i] = projection_transpose(ProjectionTransform(value[i]));
1402 }
1403 ss->Parameter(
1404 *current_group, uname, array_typedesc(TypeMatrix, fvalue.size()), fvalue.data());
1405 break;
1406 }
1408 case SocketType::NODE:
1410 case SocketType::UINT:
1411 case SocketType::UINT64:
1413 case SocketType::NUM_TYPES: {
1414 assert(0);
1415 break;
1416 }
1417 }
1418}
1419
1420void OSLCompiler::parameter(const char *name, const float f)
1421{
1422 ss->Parameter(*current_group, name, TypeFloat, &f);
1423}
1424
1425void OSLCompiler::parameter_color(const char *name, const float3 f)
1426{
1427 ss->Parameter(*current_group, name, TypeColor, &f);
1428}
1429
1430void OSLCompiler::parameter_point(const char *name, const float3 f)
1431{
1432 ss->Parameter(*current_group, name, TypePoint, &f);
1433}
1434
1435void OSLCompiler::parameter_normal(const char *name, const float3 f)
1436{
1437 ss->Parameter(*current_group, name, TypeNormal, &f);
1438}
1439
1440void OSLCompiler::parameter_vector(const char *name, const float3 f)
1441{
1442 ss->Parameter(*current_group, name, TypeVector, &f);
1443}
1444
1445void OSLCompiler::parameter(const char *name, const int f)
1446{
1447 ss->Parameter(*current_group, name, TypeInt, &f);
1448}
1449
1450void OSLCompiler::parameter(const char *name, const char *s)
1451{
1452 ss->Parameter(*current_group, name, TypeString, (const void *)&s);
1453}
1454
1455void OSLCompiler::parameter(const char *name, ustring s)
1456{
1457 const char *str = s.c_str();
1458 ss->Parameter(*current_group, name, TypeString, (const void *)&str);
1459}
1460
1461void OSLCompiler::parameter(const char *name, const Transform &tfm)
1462{
1463 ProjectionTransform projection(tfm);
1464 projection = projection_transpose(projection);
1465 ss->Parameter(*current_group, name, TypeMatrix, (float *)&projection);
1466}
1467
1468void OSLCompiler::parameter_array(const char *name, const float f[], int arraylen)
1469{
1470 TypeDesc type = TypeFloat;
1471 type.arraylen = arraylen;
1472 ss->Parameter(*current_group, name, type, f);
1473}
1474
1475void OSLCompiler::parameter_color_array(const char *name, const array<packed_float3> &f)
1476{
1477 TypeDesc type = TypeColor;
1478 type.arraylen = f.size();
1479 ss->Parameter(*current_group, name, type, f.data());
1480}
1481
1482void OSLCompiler::parameter_string_array(const char *name, const array<ustring> &a)
1483{
1484 if (a.empty()) {
1485 return;
1486 }
1487
1488 TypeDesc type = TypeString;
1489 type.arraylen = a.size();
1490 ss->Parameter(*current_group, name, type, a.data());
1491}
1492
1493void OSLCompiler::parameter_attribute(const char *name, ustring s)
1494{
1496}
1497
1498void OSLCompiler::find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input)
1499{
1500 ShaderNode *node = (input->link) ? input->link->parent : nullptr;
1501
1502 if (node != nullptr && !dependencies.contains(node)) {
1503 for (ShaderInput *in : node->inputs) {
1504 if (!node_skip_input(node, in)) {
1505 find_dependencies(dependencies, in);
1506 }
1507 }
1508
1509 dependencies.insert(node);
1510 }
1511}
1512
1513void OSLCompiler::generate_nodes(const ShaderNodeSet &nodes)
1514{
1515 ShaderNodeSet done;
1516 bool nodes_done;
1517
1518 do {
1519 nodes_done = true;
1520
1521 for (ShaderNode *node : nodes) {
1522 if (!done.contains(node)) {
1523 bool inputs_done = true;
1524
1525 for (ShaderInput *input : node->inputs) {
1526 if (!node_skip_input(node, input)) {
1527 if (input->link && !done.contains(input->link->parent)) {
1528 inputs_done = false;
1529 }
1530 }
1531 }
1532
1533 if (inputs_done) {
1534 node->compile(*this);
1535 done.insert(node);
1536
1537 if (current_type == SHADER_TYPE_SURFACE) {
1538 if (node->has_surface_transparent()) {
1539 current_shader->has_surface_transparent = true;
1540 }
1541 if (node->get_feature() & KERNEL_FEATURE_NODE_RAYTRACE) {
1542 current_shader->has_surface_raytrace = true;
1543 }
1544 if (node->has_spatial_varying()) {
1545 current_shader->has_surface_spatial_varying = true;
1546 }
1547 if (node->has_surface_bssrdf()) {
1548 current_shader->has_surface_bssrdf = true;
1549 if (node->has_bssrdf_bump()) {
1550 current_shader->has_bssrdf_bump = true;
1551 }
1552 }
1553 if (node->has_bump()) {
1554 current_shader->has_bump_from_surface = true;
1555 }
1556 if (node->has_dispersion()) {
1557 current_shader->has_dispersion = true;
1558 }
1559 }
1560 else if (current_type == SHADER_TYPE_VOLUME) {
1561 if (node->has_spatial_varying()) {
1562 current_shader->has_volume_spatial_varying = true;
1563 }
1564 }
1565 }
1566 else {
1567 nodes_done = false;
1568 }
1569 }
1570 }
1571 } while (!nodes_done);
1572}
1573
1574OSL::ShaderGroupRef OSLCompiler::compile_type(Shader *shader, ShaderGraph *graph, ShaderType type)
1575{
1576 current_type = type;
1577
1578 /* Use name hash to identify shader group to avoid issues with non-alphanumeric characters */
1579 std::stringstream name;
1580 name.imbue(std::locale("C"));
1581 name << "shader_" << shader->name.hash();
1582
1583 current_group = ss->ShaderGroupBegin(name.str());
1584
1585 ShaderNode *output = graph->output();
1586 ShaderNodeSet dependencies;
1587
1588 if (type == SHADER_TYPE_SURFACE) {
1589 /* generate surface shader */
1590 find_dependencies(dependencies, output->input("Surface"));
1591 generate_nodes(dependencies);
1592 output->compile(*this);
1593 }
1594 else if (type == SHADER_TYPE_BUMP) {
1595 /* generate bump shader */
1596 find_dependencies(dependencies, output->input("Normal"));
1597 generate_nodes(dependencies);
1598 output->compile(*this);
1599 }
1600 else if (type == SHADER_TYPE_VOLUME) {
1601 /* generate volume shader */
1602 find_dependencies(dependencies, output->input("Volume"));
1603 generate_nodes(dependencies);
1604 output->compile(*this);
1605 }
1606 else if (type == SHADER_TYPE_DISPLACEMENT) {
1607 /* generate displacement shader */
1608 find_dependencies(dependencies, output->input("Displacement"));
1609 generate_nodes(dependencies);
1610 output->compile(*this);
1611 }
1612 else {
1613 assert(0);
1614 }
1615
1616 ss->ShaderGroupEnd(*current_group);
1617
1618 return std::move(current_group);
1619}
1620
1621void OSLCompiler::compile(Shader *shader)
1622{
1623 if (shader->is_modified()) {
1624 ShaderGraph *graph = shader->graph.get();
1625 current_graph = graph;
1626
1627 current_shader = shader;
1628
1629 Shader::OSLCache cache;
1630
1631 if (shader->reference_count()) {
1632 if (shader->has_surface) {
1633 cache.surface = compile_type(shader, graph, SHADER_TYPE_SURFACE);
1634 if (shader->has_bump_from_displacement) {
1635 cache.bump = compile_type(shader, graph, SHADER_TYPE_BUMP);
1636 }
1637 }
1638 if (shader->has_volume) {
1639 cache.volume = compile_type(shader, graph, SHADER_TYPE_VOLUME);
1640 }
1641 if (shader->has_displacement) {
1642 cache.displacement = compile_type(shader, graph, SHADER_TYPE_DISPLACEMENT);
1643 }
1644 }
1645
1646 shader->osl_cache[device] = std::move(cache);
1647
1648 /* Estimate emission for MIS. */
1649 shader->estimate_emission();
1650 }
1651}
1652
1653void OSLCompiler::parameter_texture(const char *name, const ImageHandle &handle)
1654{
1655 /* Texture loaded through SVM image texture system. We generate a unique
1656 * name, which ends up being used in OSLRenderServices::get_texture_handle
1657 * to get handle again. Note that this name must be unique between multiple
1658 * render sessions as the render services are shared. */
1659 const ustring filename(string_printf("@svm%d", texture_shared_unique_id++).c_str());
1660 services->textures.insert({OSLUStringHash(filename), scene->image_manager.get()},
1661 OSLTextureHandle(handle));
1662 parameter(name, filename);
1663}
1664
1665void OSLCompiler::parameter_texture_ies(const char *name, const int svm_slot)
1666{
1667 /* IES light textures stored in SVM. */
1668 const ustring filename(string_printf("@svm%d", texture_shared_unique_id++).c_str());
1669 services->textures.insert({OSLUStringHash(filename), scene->image_manager.get()},
1671 parameter(name, filename);
1672}
1673
1674#else
1675
1678
1680void OSLManager::reset(Scene * /*scene*/) {}
1681
1682void OSLManager::device_update_pre(Device * /*device*/, Scene * /*scene*/) {}
1684 Scene * /*scene*/,
1685 Progress & /*progress*/,
1686 const bool /*reload_kernels*/)
1687{
1688}
1689void OSLManager::device_free(Device * /*device*/, DeviceScene * /*dscene*/, Scene * /*scene*/) {}
1690
1693{
1694 return false;
1695}
1696
1697void OSLCompiler::add(ShaderNode * /*node*/, const char * /*name*/, bool /*isfilepath*/) {}
1698
1700 string_view /*converter_name*/,
1701 string_view /*node_output_parameter*/,
1702 string_view /*converter_input_parameter*/,
1703 string_view /*converter_output_parameter*/,
1704 string_view /*shader_output_parameter*/)
1705{
1706}
1707
1708void OSLCompiler::parameter(ShaderNode * /*node*/, const char * /*name*/) {}
1709
1710void OSLCompiler::parameter(const char * /*name*/, float /*f*/) {}
1711
1712void OSLCompiler::parameter_color(const char * /*name*/, float3 /*f*/) {}
1713
1714void OSLCompiler::parameter_vector(const char * /*name*/, float3 /*f*/) {}
1715
1716void OSLCompiler::parameter_point(const char * /*name*/, float3 /*f*/) {}
1717
1718void OSLCompiler::parameter_normal(const char * /*name*/, float3 /*f*/) {}
1719
1720void OSLCompiler::parameter(const char * /*name*/, int /*f*/) {}
1721
1722void OSLCompiler::parameter(const char * /*name*/, const char * /*s*/) {}
1723
1724void OSLCompiler::parameter(const char * /*name*/, ustring /*s*/) {}
1725
1726void OSLCompiler::parameter(const char * /*name*/, const Transform & /*tfm*/) {}
1727
1728void OSLCompiler::parameter_array(const char * /*name*/, const float /*f*/[], int /*arraylen*/) {}
1729
1730void OSLCompiler::parameter_color_array(const char * /*name*/, const array<packed_float3> & /*f*/)
1731{
1732}
1733
1734void OSLCompiler::parameter_string_array(const char * /*name*/, const array<ustring> & /*a*/) {}
1735
1736void OSLCompiler::parameter_attribute(const char * /*name*/, ustring /*s*/) {}
1737
1738void OSLCompiler::parameter_texture(const char * /*name*/, const ImageHandle & /*handle*/) {}
1739
1740void OSLCompiler::parameter_texture_ies(const char * /*name*/, int /*svm_image_texture_id*/) {}
1741
1742#endif /* WITH_OSL */
1743
volatile int lock
unsigned long long int uint64_t
#define output
static ustring osl_name(AttributeStandard std)
Shader * get_shader(const Scene *scene)
std::string script_name
map< ustring, pair< vector< uint8_t >, TypeDesc > > script_params
DeviceType type
virtual const string & error_message()
virtual bool load_osl_kernels()
DeviceInfo info
virtual void foreach_device(const std::function< void(Device *)> &callback)
virtual OSLGlobals * get_cpu_osl_memory()
ImageManager * get_manager() const
bool empty() const
void add_to_set(set< const ImageSingle * > &images) const
Definition md5.h:19
void append(const uint8_t *data, const int nbytes)
Definition md5.cpp:268
string get_hex()
Definition md5.cpp:367
void parameter_array(const char *name, const float f[], int arraylen)
Definition osl.cpp:1728
void add_output_converter(const ShaderNode *node, string_view converter_name, string_view node_output_parameter, string_view converter_input_parameter, string_view converter_output_parameter, string_view shader_output_parameter)
Definition osl.cpp:1699
void add(ShaderNode *node, const char *name, bool isfilepath=false)
Definition osl.cpp:1697
void parameter_color_array(const char *name, const array< packed_float3 > &f)
Definition osl.cpp:1730
void parameter(ShaderNode *node, const char *name)
Definition osl.cpp:1708
void compile(Shader *shader)
void parameter_texture(const char *name, const ImageHandle &handle)
Definition osl.cpp:1738
void parameter_texture_ies(const char *name, const int svm_slot)
Definition osl.cpp:1740
void parameter_point(const char *name, const float3 f)
Definition osl.cpp:1716
void parameter_string_array(const char *name, const array< ustring > &a)
Definition osl.cpp:1734
Scene * scene
Definition scene/osl.h:200
void parameter_vector(const char *name, const float3 f)
Definition osl.cpp:1714
ShaderGraph * current_graph
Definition scene/osl.h:202
void parameter_attribute(const char *name, ustring s)
Definition osl.cpp:1736
void parameter_color(const char *name, const float3 f)
Definition osl.cpp:1712
void parameter_normal(const char *name, const float3 f)
Definition osl.cpp:1718
void reset(Scene *scene)
Definition osl.cpp:1680
~OSLManager()
Definition osl.cpp:1677
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
Definition osl.cpp:1689
void device_update_pre(Device *device, Scene *scene)
Definition osl.cpp:1682
static void free_memory()
Definition osl.cpp:1679
void tag_update()
Definition osl.cpp:1691
void device_update_post(Device *device, Scene *scene, Progress &progress, const bool reload_kernels)
Definition osl.cpp:1683
OSLManager(Device *device)
Definition osl.cpp:1676
bool need_update() const
Definition osl.cpp:1692
static OSLNode * create(ShaderGraph *graph, const size_t num_inputs, const OSLNode *from=nullptr)
bool has_emission
OSLTextureHandleMap textures
Definition services.h:304
static void register_closures(OSL::ShadingSystem *ss)
Definition closures.cpp:68
void image_manager_remove(const OSL::ShadingContext *context)
Definition services.cpp:260
void image_manager_add(const OSL::ShadingContext *context, ImageManager *image_manager)
Definition services.cpp:253
bool get_cancel() const
Definition progress.h:77
void set_error(const string &error_message_)
Definition progress.h:98
Definition scene.h:133
unique_ptr< LightManager > light_manager
Definition scene.h:167
unique_ptr< SceneUpdateStats > update_stats
Definition scene.h:197
Background * background
Definition scene.h:148
unique_ptr_vector< Shader > shaders
Definition scene.h:158
unique_ptr< ShaderManager > shader_manager
Definition scene.h:169
Camera * camera
Definition scene.h:145
unique_ptr< OSLManager > osl_manager
Definition scene.h:168
unique_ptr< ImageManager > image_manager
Definition scene.h:166
OutputNode * output()
ustring name() const
const SocketType & socket_type
ShaderNode * parent
SocketType::Type type() const
ShaderOutput * link
bool constant_folded_in
int flags() const
ShaderNodeSpecialType special_type
void estimate_emission()
bool has_volume
bool has_surface
bool has_bump_from_displacement
EmissionSampling emission_sampling
bool has_displacement
NODE_DECLARE unique_ptr< ShaderGraph > graph
void push(TaskRunFunction &&task)
Definition task.cpp:21
void wait_work(Summary *stats=nullptr)
Definition task.cpp:27
size_t size() const
size_t empty() const
size_t size() const
T * util_aligned_new(Args... args)
void util_aligned_delete(T *t)
ccl_device_inline ProjectionTransform projection_transpose(const ProjectionTransform a)
CCL_NAMESPACE_BEGIN struct Options options
#define KERNEL_FEATURE_NODE_RAYTRACE
#define CCL_NAMESPACE_END
DeviceType
#define str(s)
#define assert(assertion)
#define layer
VecBase< float, 2 > float2
VecBase< float, 3 > float3
@ SHADER_TYPE_BUMP
@ SHADER_TYPE_SURFACE
@ SHADER_TYPE_VOLUME
@ SHADER_TYPE_DISPLACEMENT
@ EMISSION_SAMPLING_NONE
@ SHADER_MASK
AttributeStandard
#define LOG_DFATAL
Definition log.h:100
#define LOG_ERROR
Definition log.h:101
#define LOG_INFO
Definition log.h:106
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
int context(const bContext *C, const char *member, bContextDataResult *result)
std::string filename(const std::string_view &str, size_t pos)
auto isolate_task(const Function &function)
Definition BLI_task.hh:224
BMesh const char void * data
#define hash
Definition noise_c.cc:156
OSL::ustringhash OSLUStringHash
Definition osl/compat.h:11
string path_user_get(const string &sub)
Definition path.cpp:378
string path_dirname(const string &path)
Definition path.cpp:422
string path_get(const string &sub)
Definition path.cpp:368
uint64_t path_modified_time(const string &path)
Definition path.cpp:818
string path_join(const string &dir, const string &file)
Definition path.cpp:436
bool path_read_text(const string &path, string &text)
Definition path.cpp:788
@ SHADER_SPECIAL_TYPE_BUMP
@ SHADER_SPECIAL_TYPE_OUTPUT
@ SHADER_SPECIAL_TYPE_OSL
set< ShaderNode *, ShaderNodeIDComparator > ShaderNodeSet
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
ustring name
Definition graph/node.h:178
int reference_count() const
Definition graph/node.h:184
void clear_modified()
bool is_modified() const
OSLTextureHandle(const OSLTextureHandleType type, const int id=-1)
Definition services.h:53
ProjectionTransform()=default
@ BOOLEAN_ARRAY
Definition node_type.h:48
@ TRANSFORM_ARRAY
Definition node_type.h:57
Type type
Definition node_type.h:85
@ LINK_OSL_INITIALIZER
Definition node_type.h:79
@ LINK_TEXTURE_UV
Definition node_type.h:73
@ LINK_TEXTURE_GENERATED
Definition node_type.h:71
@ LINK_TEXTURE_NORMAL
Definition node_type.h:72
@ LINK_POSITION
Definition node_type.h:77
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28