|
59 | 59 | #include "pxr/usd/usdLux/nonboundableLightBase.h"
|
60 | 60 |
|
61 | 61 | #include "boost/algorithm/string/predicate.hpp"
|
| 62 | +#include "boost/container/flat_map.hpp" |
62 | 63 |
|
63 | 64 | #include "fmt/format.h"
|
64 | 65 |
|
@@ -319,6 +320,63 @@ Plug *loadPrimDefinitionAttribute( const UsdPrimDefinition::Attribute &attribute
|
319 | 320 |
|
320 | 321 | const IECore::InternedString g_surface( "surface" );
|
321 | 322 | const IECore::InternedString g_displacement( "displacement" );
|
| 323 | +const IECore::InternedString g_volume( "volume" ); |
| 324 | + |
| 325 | +using ShaderRemap = boost::container::flat_map<IECore::InternedString, IECore::InternedString>; |
| 326 | + |
| 327 | +ShaderRemap &shaderNameSpace() |
| 328 | +{ |
| 329 | + static ShaderRemap g_shaderNameSpace; |
| 330 | + return g_shaderNameSpace; |
| 331 | +} |
| 332 | + |
| 333 | +const std::string remapShaderType( const TfToken sourceType, const TfToken context ) |
| 334 | +{ |
| 335 | + const IECore::InternedString stype( sourceType.GetString() ); |
| 336 | + const IECore::InternedString ctx( context.GetString() ); |
| 337 | + std::string nameSpace = stype.string(); |
| 338 | + std::string outputType = g_surface.string(); |
| 339 | + |
| 340 | + const ShaderRemap &shaderNS = shaderNameSpace(); |
| 341 | + ShaderRemap::const_iterator it = shaderNS.find( stype ); |
| 342 | + if( it != shaderNS.end() ) |
| 343 | + { |
| 344 | + nameSpace = it->second.string(); |
| 345 | + } |
| 346 | + |
| 347 | + if( ctx == g_displacement ) |
| 348 | + { |
| 349 | + outputType = g_displacement.string(); |
| 350 | + } |
| 351 | + else if( ctx == g_volume ) |
| 352 | + { |
| 353 | + outputType = g_volume.string(); |
| 354 | + } |
| 355 | + |
| 356 | + if( nameSpace.empty() ) |
| 357 | + { |
| 358 | + return outputType; |
| 359 | + } |
| 360 | + |
| 361 | + return nameSpace + ":" + outputType; |
| 362 | +} |
| 363 | + |
| 364 | +// USD Lux types seem to be set to USD for sourceType, remove the namespace. |
| 365 | +const bool g_usdShaderNameSpaceRegistration = USDShader::registerShaderNameSpace( "USD", "" ); |
| 366 | +// Ideally this one should translate to `gl` however USDPreviewSurface and friends |
| 367 | +// have their sourceType set to `glslfx` so we leave these without a namespace. |
| 368 | +const bool g_glslfxShaderNameSpaceRegistration = USDShader::registerShaderNameSpace( "glslfx", "" ); |
| 369 | +// Prman tends to register their built-in OSL shaders as `OSL` sourceType, which might |
| 370 | +// need to be re-looked at if we don't want Prman built-in shaders to be used in |
| 371 | +// other OSL-compatible renderers or in other GafferOSL functionality. |
| 372 | +const bool g_oslShaderNameSpaceRegistration = USDShader::registerShaderNameSpace( "OSL", "osl" ); |
| 373 | + |
| 374 | +// TODO: Put these registers into their respective plugins maybe? |
| 375 | + |
| 376 | +// We shouldn't use Arnold shaders as USD shaders anyways as they're already available in Gaffer from Arnold itself. |
| 377 | +const bool g_arnoldShaderNameSpaceRegistration = USDShader::registerShaderNameSpace( "arnold", "ai" ); |
| 378 | +const bool g_prmanShaderNameSpaceRegistration = USDShader::registerShaderNameSpace( "RmanCpp", "ri" ); |
| 379 | + |
322 | 380 |
|
323 | 381 | } // namespace
|
324 | 382 |
|
@@ -386,7 +444,7 @@ void USDShader::loadShader( const std::string &shaderName, bool keepExistingValu
|
386 | 444 | // Set name and type and delete old parameters if necessary.
|
387 | 445 |
|
388 | 446 | namePlug()->setValue( shaderName );
|
389 |
| - typePlug()->setValue( "surface" ); |
| 447 | + typePlug()->setValue( remapShaderType( shader->GetSourceType(), shader->GetContext() ) ); |
390 | 448 |
|
391 | 449 | Plug *parametersPlug = this->parametersPlug()->source();
|
392 | 450 | Plug *outPlug = this->outPlug();
|
@@ -474,3 +532,9 @@ IECore::ConstCompoundObjectPtr USDShader::attributes( const Gaffer::Plug *output
|
474 | 532 | }
|
475 | 533 | return result;
|
476 | 534 | }
|
| 535 | + |
| 536 | +bool USDShader::registerShaderNameSpace( const IECore::InternedString sourceType, const IECore::InternedString nameSpace ) |
| 537 | +{ |
| 538 | + ShaderRemap &shaderNS = shaderNameSpace(); |
| 539 | + return shaderNS.insert( { sourceType, nameSpace } ).second; |
| 540 | +} |
0 commit comments