40map<DeviceType, std::shared_ptr<OSL::ShadingSystem>> ss_shared;
42OSL::ErrorHandler errhandler;
44std::atomic<int> OSLCompiler::texture_shared_unique_id = 0;
52 shading_system_free();
57# ifdef OSL_HAS_BLENDER_CLEANUP_FIX
63 OSL::pvt::LLVM_Util::Cleanup();
69 shading_system_free();
73OSL::ShadingSystem *OSLManager::get_shading_system(
Device *sub_device)
75 return ss_map[sub_device->
info.
type].get();
78void OSLManager::foreach_shading_system(
const std::function<
void(OSL::ShadingSystem *)> &callback)
80 for (
const auto &[device_type, ss] : ss_map) {
85void OSLManager::foreach_render_services(
const std::function<
void(
OSLRenderServices *)> &callback)
87 for (
const auto &[device_type, ss] : ss_map) {
88 callback(
static_cast<OSLRenderServices *
>(ss->renderer()));
92void OSLManager::foreach_osl_device(
Device *device,
93 const std::function<
void(
Device *, OSLGlobals *)> &callback)
98 callback(sub_device, og);
116 shading_system_init(scene->
shader_manager->get_scene_linear_interop_id());
126 const ImageManager *image_manager = scene->
image_manager.get();
127 foreach_render_services([image_manager](OSLRenderServices *services) {
140class OSLThreadInfoPool {
144 for (
const auto &[ss, thread_info] : thread_infos_) {
145 ss->destroy_thread_info(thread_info);
149 OSL::PerThreadInfo *acquire(OSL::ShadingSystem *ss)
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);
159 return ss->create_thread_info();
162 void release(OSL::ShadingSystem *ss, OSL::PerThreadInfo *thread_info)
165 thread_infos_.emplace_back(ss, thread_info);
170 vector<std::pair<OSL::ShadingSystem *, OSL::PerThreadInfo *>> thread_infos_;
173static void optimize_group(OSLThreadInfoPool &thread_infos,
174 OSL::ShadingSystem *ss,
176 OSL::ShaderGroup *group)
181 OSL::PerThreadInfo *thread_info = thread_infos.acquire(ss);
182 OSL::ShadingContext *
context = ss->get_context(thread_info);
185 ss->optimize_group(group, context,
true);
188 ss->release_context(context);
189 thread_infos.release(ss, thread_info);
192static void optimize_group(
TaskPool &pool,
193 OSLThreadInfoPool &thread_infos,
194 OSL::ShadingSystem *ss,
196 const OSL::ShaderGroupRef &group_ref)
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);
215 optimize_group(thread_infos, ss, image_manager, group);
223 const bool reload_kernels)
230 foreach_osl_device(device, [
this, scene](Device *sub_device, OSLGlobals *og) {
231 OSL::ShadingSystem *ss = get_shading_system(sub_device);
233 OSL::ShaderGroupRef group = ss->ShaderGroupBegin(
"camera_group");
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);
243 ss->Parameter(*group, name, type, (
const void *)
data.data());
247 ss->ShaderGroupEnd(*group);
250 og->services =
static_cast<OSLRenderServices *
>(ss->renderer());
252 og->camera_state = group;
253 og->use_camera =
true;
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;
269 auto add_param = [&](
const char *name, OIIO::TypeDesc type,
bool derivs,
int offset) {
270 ss->add_symlocs(group.get(),
274 OSL::SymArena::Outputs,
275 offset *
sizeof(
float)));
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);
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);
290 foreach_osl_device(device, [](Device *, OSLGlobals *og) {
291 og->camera_state.reset();
292 og->use_camera =
false;
297 scoped_callback_timer timer([scene](
double time) {
322 OSLThreadInfoPool thread_infos;
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()) {
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})
334 for (
const OSL::ShaderGroupRef &group_ref : *states) {
335 optimize_group(pool, thread_infos, ss, image_manager, group_ref);
338 optimize_group(pool, thread_infos, ss, image_manager, og->camera_state);
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);
352 og->services =
static_cast<OSLRenderServices *
>(ss->renderer());
362 need_update_ =
false;
368 foreach_osl_device(device, [](Device *, OSLGlobals *og) {
369 og->use_shading =
false;
370 og->use_camera =
false;
372 og->camera_state.reset();
377 foreach_render_services([scene](OSLRenderServices *services) {
378 for (
auto it = services->
textures.begin(); it != services->
textures.end();) {
381 services->
textures.erase(it->first,
false);
393void OSLManager::shading_system_init(
const string &colorspace_interop_id)
396 if (!ss_map.empty()) {
403 foreach_osl_device(device_, [
this, colorspace_interop_id](Device *sub_device, OSLGlobals *) {
406 if (!ss_shared[device_type]) {
417 const string shader_path = string_to_ansi(
path_get(
"shader"));
419 const string shader_path =
path_get(
"shader");
425 auto *ts =
reinterpret_cast<OSL::TextureSystem *
>(1);
427 auto ss = std::shared_ptr<OSL::ShadingSystem>(
428 new OSL::ShadingSystem(services, ts, &errhandler), [](OSL::ShadingSystem *ss) {
432 ss->attribute(
"lockgeom", 1);
433 ss->attribute(
"commonspace",
"world");
434 ss->attribute(
"searchpath:shader", shader_path);
435 ss->attribute(
"greedyjit", 1);
438 if (colorspace_interop_id ==
"lin_rec709_scene") {
439 ss->attribute(
"colorspace", OSL::Strings::Rec709);
441 else if (colorspace_interop_id ==
"lin_rec2020_scene") {
442 ss->attribute(
"colorspace", OSL::Strings::HDTV);
444 else if (colorspace_interop_id ==
"lin_ap1_scene") {
445 ss->attribute(
"colorspace", OSL::Strings::ACEScg);
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));
453 ss->attribute(
"max_optix_groupdata_alloc", 2048);
456 LOG_INFO <<
"Using shader search path: " << shader_path;
459 static const char *raytypes[] = {
500 const int nraytypes =
sizeof(raytypes) /
sizeof(raytypes[0]);
501 ss->attribute(
"raytypes", TypeDesc(TypeDesc::STRING, nraytypes), (
const void *)raytypes);
504 ss_shared[device_type] = std::move(ss);
506 ss_map[device_type] = ss_shared[device_type];
510void OSLManager::shading_system_free()
517 for (
auto &[device_type, ss] : ss_shared) {
518 if (ss.use_count() == 1) {
523 loaded_shaders.clear();
526bool OSLManager::osl_compile(
const string &inputfile,
const string &outputfile)
530 const string shader_path =
path_get(
"shader");
537 const string include_path_arg = string(
"-I") + shader_path;
538 options.push_back(include_path_arg);
540 stdosl_path =
path_join(shader_path,
"stdcycles.h");
548 OSL::OSLCompiler compiler = OSL::OSLCompiler(&OSL::ErrorHandler::default_handler());
549 const bool ok = compiler.compile(string_view(inputfile),
options, string_view(stdosl_path));
554bool OSLManager::osl_query(OSL::OSLQuery &query,
const string &filepath)
557 return query.open(filepath, searchpath);
560static string shader_filepath_hash(
const string &filepath,
const uint64_t modified_time)
564 md5.
append((
const uint8_t *)filepath.c_str(), filepath.size());
565 md5.
append((
const uint8_t *)&modified_time,
sizeof(modified_time));
570const char *OSLManager::shader_test_loaded(
const string &
hash)
572 const map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(
hash);
573 return (it == loaded_shaders.end()) ?
nullptr : it->first.c_str();
576OSLShaderInfo *OSLManager::shader_loaded_info(
const string &
hash)
578 const map<string, OSLShaderInfo>::iterator it = loaded_shaders.find(
hash);
579 return (it == loaded_shaders.end()) ?
nullptr : &it->second;
582const char *OSLManager::shader_load_filepath(
string filepath)
584 const size_t len = filepath.size();
585 const string extension = filepath.substr(
len - 4);
588 if (extension ==
".osl") {
590 const string osopath = filepath.substr(0,
len - 4) +
".oso";
594 if (oso_modified_time != 0) {
595 const char *
hash = shader_test_loaded(shader_filepath_hash(osopath, oso_modified_time));
603 if (oso_modified_time == 0 || (oso_modified_time < modified_time)) {
604 OSLManager::osl_compile(filepath, osopath);
608 modified_time = oso_modified_time;
614 if (extension ==
".oso") {
627 const char *
hash = shader_test_loaded(shader_filepath_hash(filepath, modified_time));
635 const string bytecode_hash = shader_filepath_hash(filepath, modified_time);
639 LOG_ERROR <<
"Shader graph: failed to read file " << filepath;
640 const OSLShaderInfo info;
641 loaded_shaders[bytecode_hash] = info;
645 return shader_load_bytecode(bytecode_hash, bytecode);
648const char *OSLManager::shader_load_bytecode(
const string &
hash,
const string &bytecode)
650 foreach_shading_system(
651 [
hash, bytecode](OSL::ShadingSystem *ss) { ss->LoadMemoryCompiledShader(
hash, bytecode); });
657 if (!info.query.open_bytecode(bytecode)) {
658 LOG_ERROR <<
"OSL query error: " << info.query.geterror();
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);
666 loaded_shaders[
hash] = info;
668 return loaded_shaders.find(
hash)->first.c_str();
671uint64_t OSLShaderManager::get_attribute_id(ustring name)
681void OSLShaderManager::device_update_specific(
Device *device,
686 if (!need_update()) {
699 OSLManager::foreach_osl_device(device, [scene](
Device *sub_device, OSLGlobals *og) {
700 OSL::ShadingSystem *ss = scene->
osl_manager->get_shading_system(sub_device);
704 og->use_shading =
true;
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();
721 auto compile = [scene, &progress, shader, background_shader](
Device *sub_device,
723 OSL::ShadingSystem *ss = scene->
osl_manager->get_shading_system(sub_device);
725 OSLCompiler compiler(ss, scene, progress, sub_device);
726 compiler.background = (shader == background_shader);
727 compiler.compile(shader);
730 task_pool.
push([device, compile] { OSLManager::foreach_osl_device(device, compile); });
734 if (progress.get_cancel()) {
740 OSLManager::OSLManager::foreach_osl_device(
741 device, [shader, background_shader](
Device *sub_device, OSLGlobals *og) {
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);
749 if (shader == background_shader) {
750 og->background_state = cache.surface;
762 int background_id = scene->
shader_manager->get_shader_id(background_shader);
764 OSLManager::foreach_osl_device(device, [background_id](
Device *, OSLGlobals *og) {
765 og->background_state = og->surface_state[background_id &
SHADER_MASK];
772 update_flags = UPDATE_NONE;
774 device_update_common(device, dscene, scene, progress);
779 device_free_common(device, dscene, scene);
782 OSLManager::foreach_osl_device(device, [](
Device *, OSLGlobals *og) {
783 og->use_shading =
false;
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();
797 const std::string &filepath,
798 const std::string &bytecode_hash,
799 const std::string &bytecode)
811 if (!filepath.empty()) {
817 hash = scene->
osl_manager->shader_load_bytecode(bytecode_hash, bytecode);
828 size_t num_inputs = 0;
830 for (
int i = 0; i < info->query.nparams(); i++) {
831 const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
834 if (param->varlenarray || param->isstruct || param->type.arraylen > 1) {
838 if (!param->isoutput) {
847 const set<void *> used_sockets;
849 for (
int i = 0; i < info->query.nparams(); i++) {
850 const OSL::OSLQuery::Parameter *param = info->query.getparam(i);
853 if (param->varlenarray || param->isstruct || param->type.arraylen > 1) {
860 if (param->isclosure) {
863 else if (param->type.vecsemantics != TypeDesc::NOSEMANTICS) {
864 if (param->type.vecsemantics == TypeDesc::COLOR) {
867 else if (param->type.vecsemantics == TypeDesc::POINT) {
870 else if (param->type.vecsemantics == TypeDesc::VECTOR) {
873 else if (param->type.vecsemantics == TypeDesc::NORMAL) {
880 if (!param->isoutput && param->validdefault) {
882 default_value->
x = param->fdefault[0];
883 default_value->
y = param->fdefault[1];
884 default_value->
z = param->fdefault[2];
887 else if (param->type.aggregate == TypeDesc::SCALAR) {
888 if (param->type.basetype == TypeDesc::INT) {
891 if (!param->isoutput && param->validdefault) {
892 *(
int *)
node->input_default_value() = param->idefault[0];
895 else if (param->type.basetype == TypeDesc::FLOAT) {
898 if (!param->isoutput && param->validdefault) {
899 *(
float *)
node->input_default_value() = param->fdefault[0];
902 else if (param->type.basetype == TypeDesc::STRING) {
905 if (!param->isoutput && param->validdefault) {
906 *(ustring *)
node->input_default_value() = param->sdefault[0];
917 if (param->isoutput) {
918 node->add_output(param->name, socket_type);
923 int socket_flags = 0;
924 if (!param->validdefault) {
927 for (
const OSL::OSLQuery::Parameter &metadata : param->metadata) {
928 if (metadata.type == TypeDesc::STRING) {
929 if (metadata.name ==
"widget" && metadata.sdefault[0] ==
"null") {
932 else if (metadata.name ==
"mtlx_defaultgeomprop") {
936 if (metadata.sdefault[0] ==
"Nobject") {
939 else if (metadata.sdefault[0] ==
"Nworld") {
942 else if (metadata.sdefault[0] ==
"Pobject") {
945 else if (metadata.sdefault[0] ==
"Pworld") {
948 else if (metadata.sdefault[0] ==
"Tworld") {
951 else if (metadata.sdefault[0] ==
"UV0") {
958 node->add_input(param->name, socket_type, socket_flags);
963 if (!bytecode_hash.empty()) {
964 node->bytecode_hash = bytecode_hash;
967 node->filepath = filepath;
971 node->create_inputs_outputs(
node->type);
976void OSLShaderManager::osl_image_handles(
Device *device,
978 set<const ImageSingle *> &handles)
980 set<OSLRenderServices *> services_shared;
983 services_shared.insert(og->services);
987 for (
auto it = services->
textures.begin(); it != services->
textures.end(); ++it) {
998OSLCompiler::OSLCompiler(OSL::ShadingSystem *ss,
Scene *scene,
Progress &progress,
Device *device)
1000 progress(&progress),
1006 current_shader =
nullptr;
1013 std::stringstream stream;
1017 stream.imbue(std::locale(
"C"));
1019 stream <<
"node_" <<
node->type->name <<
"_" <<
node;
1021 return stream.str();
1024string_view OSLCompiler::get_shader_usage()
const
1026 switch (current_type) {
1032 return "displacement";
1034 return "displacement";
1036 LOG_DFATAL <<
"Unhandled shader type " << int(current_type);
1040OSLCompiler::LayerParam OSLCompiler::get_output_layer_param(
const string_view
layer,
1041 const string_view param)
const
1043 const LayerParam layer_param(
layer, param);
1045 const auto it = remapped_outputs_.find(layer_param);
1046 if (it != remapped_outputs_.end()) {
1055 string sname(input->
name().string());
1059 while ((i = sname.find(
" ")) != string::npos) {
1060 sname.replace(i, 1,
"");
1076 string sname(
output->name().string());
1080 while ((i = sname.find(
" ")) != string::npos) {
1081 sname.replace(i, 1,
"");
1085 for (ShaderInput *input :
node->inputs) {
1119 if (input->
name() ==
"Height") {
1138 if (name ==
nullptr) {
1144 for (ShaderInput *input :
node->inputs) {
1147 if (node_skip_input(
node, input)) {
1154 const string param_name = compatible_name(
node, input);
1156 switch (input->
type()) {
1188 ss->Shader(*current_group, get_shader_usage(), name,
id(
node));
1191 for (ShaderInput *input :
node->inputs) {
1193 if (node_skip_input(
node, input)) {
1198 const LayerParam layer_param_from = get_output_layer_param(
1200 const string id_to = id(
node);
1201 const string param_to = compatible_name(
node, input);
1204 *current_group, layer_param_from.layer, layer_param_from.param, id_to, param_to);
1215 OSLNode *oslnode =
static_cast<OSLNode *
>(
node);
1218 if (info->has_surface_transparent) {
1219 current_shader->has_surface_transparent =
true;
1221 if (info->has_surface_bssrdf) {
1222 current_shader->has_surface_bssrdf =
true;
1223 current_shader->has_bssrdf_bump =
true;
1225 current_shader->has_bump_from_surface =
true;
1226 current_shader->has_surface_raytrace =
true;
1229 if (
node->has_spatial_varying()) {
1230 current_shader->has_surface_spatial_varying =
true;
1234 if (
node->has_spatial_varying()) {
1235 current_shader->has_volume_spatial_varying =
true;
1237 if (
node->has_attribute_dependency()) {
1238 current_shader->has_volume_attribute_dependency =
true;
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)
1250 const string node_id = id(
node);
1251 const string converter_id = node_id +
"_" + string(shader_output_parameter);
1253 ss->Shader(*current_group, get_shader_usage(), converter_name, converter_id);
1255 *current_group, node_id, node_output_parameter, converter_id, converter_input_parameter);
1257 remapped_outputs_.insert(pair(LayerParam(node_id, shader_output_parameter),
1258 LayerParam(converter_id, converter_output_parameter)));
1261static TypeDesc array_typedesc(
const TypeDesc typedesc,
const int arraylength)
1263 return TypeDesc((TypeDesc::BASETYPE)typedesc.basetype,
1264 (TypeDesc::AGGREGATE)typedesc.aggregate,
1265 (TypeDesc::VECSEMANTICS)typedesc.vecsemantics,
1271 const ustring uname = ustring(name);
1272 const SocketType &socket = *(
node->type->find_input(uname));
1274 switch (socket.
type) {
1276 int value =
node->get_bool(socket);
1277 ss->Parameter(*current_group, name, TypeInt, &value);
1281 float value =
node->get_float(socket);
1282 ss->Parameter(*current_group, uname, TypeFloat, &value);
1286 int value =
node->get_int(socket);
1287 ss->Parameter(*current_group, uname, TypeInt, &value);
1292 ss->Parameter(*current_group, uname, TypeColor, &value);
1297 ss->Parameter(*current_group, uname, TypeVector, &value);
1302 ss->Parameter(*current_group, uname, TypePoint, &value);
1307 ss->Parameter(*current_group, uname, TypeNormal, &value);
1312 ss->Parameter(*current_group,
1314 TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT),
1319 ustring value =
node->get_string(socket);
1320 ss->Parameter(*current_group, uname, TypeString, &value);
1324 ustring value =
node->get_string(socket);
1325 ss->Parameter(*current_group, uname, TypeString, &value);
1330 ProjectionTransform projection(value);
1332 ss->Parameter(*current_group, uname, TypeMatrix, &projection);
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];
1342 ss->Parameter(*current_group, uname, array_typedesc(TypeInt, value.
size()), intvalue.data());
1346 const array<float> &value =
node->get_float_array(socket);
1347 ss->Parameter(*current_group, uname, array_typedesc(TypeFloat, value.
size()), value.
data());
1351 const array<int> &value =
node->get_int_array(socket);
1352 ss->Parameter(*current_group, uname, array_typedesc(TypeInt, value.
size()), value.
data());
1361 switch (socket.
type) {
1363 typedesc = TypeColor;
1366 typedesc = TypeVector;
1369 typedesc = TypePoint;
1372 typedesc = TypeNormal;
1379 const array<packed_float3> &value =
node->get_float3_array(socket);
1380 ss->Parameter(*current_group, uname, array_typedesc(typedesc, value.
size()), value.
data());
1384 const array<float2> &value =
node->get_float2_array(socket);
1388 array_typedesc(TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC2, TypeDesc::POINT), value.
size()),
1393 const array<ustring> &value =
node->get_string_array(socket);
1394 ss->Parameter(*current_group, uname, array_typedesc(TypeString, value.
size()), value.
data());
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++) {
1404 *current_group, uname, array_typedesc(TypeMatrix, fvalue.size()), fvalue.data());
1422 ss->Parameter(*current_group, name, TypeFloat, &f);
1427 ss->Parameter(*current_group, name, TypeColor, &f);
1432 ss->Parameter(*current_group, name, TypePoint, &f);
1437 ss->Parameter(*current_group, name, TypeNormal, &f);
1442 ss->Parameter(*current_group, name, TypeVector, &f);
1447 ss->Parameter(*current_group, name, TypeInt, &f);
1452 ss->Parameter(*current_group, name, TypeString, (
const void *)&s);
1457 const char *
str = s.c_str();
1458 ss->Parameter(*current_group, name, TypeString, (
const void *)&
str);
1463 ProjectionTransform projection(tfm);
1465 ss->Parameter(*current_group, name, TypeMatrix, (
float *)&projection);
1470 TypeDesc type = TypeFloat;
1471 type.arraylen = arraylen;
1472 ss->Parameter(*current_group, name, type, f);
1477 TypeDesc type = TypeColor;
1478 type.arraylen = f.
size();
1479 ss->Parameter(*current_group, name, type, f.
data());
1488 TypeDesc type = TypeString;
1489 type.arraylen = a.
size();
1490 ss->Parameter(*current_group, name, type, a.
data());
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);
1509 dependencies.insert(
node);
1521 for (ShaderNode *
node : nodes) {
1522 if (!done.contains(
node)) {
1523 bool inputs_done =
true;
1525 for (ShaderInput *input :
node->inputs) {
1526 if (!node_skip_input(
node, input)) {
1528 inputs_done =
false;
1534 node->compile(*
this);
1538 if (
node->has_surface_transparent()) {
1539 current_shader->has_surface_transparent =
true;
1542 current_shader->has_surface_raytrace =
true;
1544 if (
node->has_spatial_varying()) {
1545 current_shader->has_surface_spatial_varying =
true;
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;
1553 if (
node->has_bump()) {
1554 current_shader->has_bump_from_surface =
true;
1556 if (
node->has_dispersion()) {
1557 current_shader->has_dispersion =
true;
1561 if (
node->has_spatial_varying()) {
1562 current_shader->has_volume_spatial_varying =
true;
1571 }
while (!nodes_done);
1576 current_type = type;
1579 std::stringstream name;
1580 name.imbue(std::locale(
"C"));
1581 name <<
"shader_" << shader->
name.hash();
1583 current_group = ss->ShaderGroupBegin(name.str());
1590 find_dependencies(dependencies,
output->input(
"Surface"));
1591 generate_nodes(dependencies);
1596 find_dependencies(dependencies,
output->input(
"Normal"));
1597 generate_nodes(dependencies);
1602 find_dependencies(dependencies,
output->input(
"Volume"));
1603 generate_nodes(dependencies);
1608 find_dependencies(dependencies,
output->input(
"Displacement"));
1609 generate_nodes(dependencies);
1616 ss->ShaderGroupEnd(*current_group);
1618 return std::move(current_group);
1624 ShaderGraph *graph = shader->
graph.get();
1627 current_shader = shader;
1629 Shader::OSLCache cache;
1646 shader->osl_cache[device] = std::move(cache);
unsigned long long int uint64_t
static ustring osl_name(AttributeStandard std)
Shader * get_shader(const Scene *scene)
map< ustring, pair< vector< uint8_t >, TypeDesc > > script_params
virtual const string & error_message()
virtual bool load_osl_kernels()
virtual void foreach_device(const std::function< void(Device *)> &callback)
virtual OSLGlobals * get_cpu_osl_memory()
ImageManager * get_manager() const
void add_to_set(set< const ImageSingle * > &images) const
void append(const uint8_t *data, const int nbytes)
void parameter_array(const char *name, const float f[], int arraylen)
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)
void add(ShaderNode *node, const char *name, bool isfilepath=false)
void parameter_color_array(const char *name, const array< packed_float3 > &f)
void parameter(ShaderNode *node, const char *name)
void compile(Shader *shader)
void parameter_texture(const char *name, const ImageHandle &handle)
void parameter_texture_ies(const char *name, const int svm_slot)
void parameter_point(const char *name, const float3 f)
void parameter_string_array(const char *name, const array< ustring > &a)
void parameter_vector(const char *name, const float3 f)
ShaderGraph * current_graph
void parameter_attribute(const char *name, ustring s)
void parameter_color(const char *name, const float3 f)
void parameter_normal(const char *name, const float3 f)
void device_free(Device *device, DeviceScene *dscene, Scene *scene)
void device_update_pre(Device *device, Scene *scene)
static void free_memory()
void device_update_post(Device *device, Scene *scene, Progress &progress, const bool reload_kernels)
OSLManager(Device *device)
static OSLNode * create(ShaderGraph *graph, const size_t num_inputs, const OSLNode *from=nullptr)
OSLTextureHandleMap textures
static void register_closures(OSL::ShadingSystem *ss)
void image_manager_remove(const OSL::ShadingContext *context)
void image_manager_add(const OSL::ShadingContext *context, ImageManager *image_manager)
void set_error(const string &error_message_)
unique_ptr< LightManager > light_manager
unique_ptr< SceneUpdateStats > update_stats
unique_ptr_vector< Shader > shaders
unique_ptr< ShaderManager > shader_manager
unique_ptr< OSLManager > osl_manager
unique_ptr< ImageManager > image_manager
const SocketType & socket_type
SocketType::Type type() const
ShaderNodeSpecialType special_type
bool has_bump_from_displacement
EmissionSampling emission_sampling
NODE_DECLARE unique_ptr< ShaderGraph > graph
void push(TaskRunFunction &&task)
void wait_work(Summary *stats=nullptr)
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
#define assert(assertion)
VecBase< float, 2 > float2
VecBase< float, 3 > float3
@ SHADER_TYPE_DISPLACEMENT
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
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)
BMesh const char void * data
OSL::ustringhash OSLUStringHash
string path_user_get(const string &sub)
string path_dirname(const string &path)
string path_get(const string &sub)
uint64_t path_modified_time(const string &path)
string path_join(const string &dir, const string &file)
bool path_read_text(const string &path, string &text)
@ 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,...)
int reference_count() const
OSLTextureHandle(const OSLTextureHandleType type, const int id=-1)
std::unique_lock< std::mutex > thread_scoped_lock