Skip to content

Commit

Permalink
HYDRA-1071 : Add a flow viewport example that uses a custom GLSLFX sh…
Browse files Browse the repository at this point in the history
…ader for Storm.
  • Loading branch information
lanierd-adsk committed Jul 5, 2024
1 parent 1a0384c commit f20c203
Show file tree
Hide file tree
Showing 25 changed files with 1,544 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/flowViewport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ add_subdirectory(colorPreferences)
add_subdirectory(sceneIndex)
add_subdirectory(API)
add_subdirectory(selection)
add_subdirectory(usdPlugins)
1 change: 1 addition & 0 deletions lib/flowViewport/usdPlugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(shadersDiscoveryPlugin)
79 changes: 79 additions & 0 deletions lib/flowViewport/usdPlugins/shadersDiscoveryPlugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
set(TARGET_NAME flowViewportShadersDiscoveryPlugin)

add_library(${TARGET_NAME} SHARED)

# -----------------------------------------------------------------------------
# sources
# -----------------------------------------------------------------------------
target_sources(${TARGET_NAME}
PRIVATE
fvpShadersDiscoveryPlugin.cpp
)

set(HEADERS
fvpShadersDiscoveryPlugin.h
)

# -----------------------------------------------------------------------------
# include directories
# -----------------------------------------------------------------------------
target_include_directories(${TARGET_NAME}
PUBLIC
${PXR_INCLUDE_DIRS}
${CMAKE_BINARY_DIR}/include
)

# -----------------------------------------------------------------------------
# compiler configuration
# -----------------------------------------------------------------------------
target_compile_definitions(${TARGET_NAME}
PRIVATE
FVP_CUSTOMSHADERS_EXPORT
$<$<BOOL:${IS_MACOSX}>:OSMac_>
# Copy-pasted from lib/mayaUsd/CMakeLists.txt
)

mayaHydra_compile_config(${TARGET_NAME})

# -----------------------------------------------------------------------------
# link libraries
# -----------------------------------------------------------------------------
target_link_libraries(${TARGET_NAME}
PRIVATE
ar
gf
tf
hd
hdx
sdf
usdImaging
)

# -----------------------------------------------------------------------------
# install
# -----------------------------------------------------------------------------

set(PLUG_INFO_PATH "plugInfo.json")

configure_file(${PLUG_INFO_PATH} ${CMAKE_CURRENT_BINARY_DIR}/${PLUG_INFO_PATH})

install(FILES ${PLUG_INFO_PATH}
DESTINATION
${CMAKE_INSTALL_PREFIX}/lib/usd/flowViewportShadersDiscoveryPlugin/resources
)

install(TARGETS ${TARGET_NAME}
LIBRARY
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/usd/flowViewportShadersDiscoveryPlugin
RUNTIME
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib/usd/flowViewportShadersDiscoveryPlugin
)

if(IS_WINDOWS)
if (NOT CODE_COVERAGE)
install(FILES $<TARGET_PDB_FILE:${TARGET_NAME}>
DESTINATION ${CMAKE_INSTALL_PREFIX}/lib OPTIONAL)
endif()
endif()

add_subdirectory(shaders)
5 changes: 5 additions & 0 deletions lib/flowViewport/usdPlugins/shadersDiscoveryPlugin/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This is a pure usd plugin that is a shaders discovery plugin.
The shaders defined inshadersDef.usda will be loaded by usd/hydra and available for use in a material network in any Hydra plugin.

You can add the plugInfo.json file path in the environment variable PXR_PLUGINPATH_NAME or use another .json file to load it
so USD/Hydra loads it at startup.
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//

//
// Copyright 2024 Autodesk, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "fvpShadersDiscoveryPlugin.h"

#include <pxr/base/plug/plugin.h>
#include <pxr/base/plug/thisPlugin.h>

#include <pxr/usd/ar/resolver.h>
#include <pxr/usd/ar/resolverContextBinder.h>

#include <pxr/usd/usdShade/shader.h>
#include <pxr/usd/usdShade/shaderDefUtils.h>

PXR_NAMESPACE_OPEN_SCOPE

NDR_REGISTER_DISCOVERY_PLUGIN(FlowViewportShadersDiscoveryPlugin);


static std::string _GetShaderResourcePath(char const* resourceName = "")
{
static PlugPluginPtr plugin = PlugRegistry::GetInstance().GetPluginWithName("flowViewportShadersDiscoveryPlugin");
const std::string path = PlugFindPluginResource(plugin, resourceName);

TF_VERIFY(!path.empty(), "Could not find shader resource: %s\n", resourceName);

return path;
}

const NdrStringVec& FlowViewportShadersDiscoveryPlugin::GetSearchURIs() const
{
static const NdrStringVec searchPaths{_GetShaderResourcePath()};
return searchPaths;
}

NdrNodeDiscoveryResultVec FlowViewportShadersDiscoveryPlugin::DiscoverNodes(const Context& context)
{
NdrNodeDiscoveryResultVec result;

static std::string shaderDefsFile = _GetShaderResourcePath("shaderDefs.usda");
if (shaderDefsFile.empty())
return result;

auto resolverContext = ArGetResolver().CreateDefaultContextForAsset(shaderDefsFile);

const UsdStageRefPtr stage = UsdStage::Open(shaderDefsFile, resolverContext);

if (!stage)
{
TF_RUNTIME_ERROR("Could not open file '%s' on a USD stage.", shaderDefsFile.c_str());
return result;
}

ArResolverContextBinder binder(resolverContext);
auto rootPrims = stage->GetPseudoRoot().GetChildren();
for (const auto& shaderDef : rootPrims)
{
UsdShadeShader shader(shaderDef);
if (!shader)
{
continue;
}

auto discoveryResults = UsdShadeShaderDefUtils::GetNodeDiscoveryResults(shader, shaderDefsFile);

result.insert(result.end(), discoveryResults.begin(), discoveryResults.end());

if (discoveryResults.empty())
{
TF_RUNTIME_ERROR(
"Found shader definition <%s> with no valid "
"discovery results. This is likely because there are no "
"resolvable info:sourceAsset values.",
shaderDef.GetPath().GetText());
}
}

return result;
}

PXR_NAMESPACE_CLOSE_SCOPE
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Copyright 2018 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//

//
// Copyright 2024 Autodesk, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#ifndef FLOW_VIEWPORT_SHADERS_DISCOVERY_PLUGIN_H
#define FLOW_VIEWPORT_SHADERS_DISCOVERY_PLUGIN_H

#include <pxr/usd/ndr/discoveryPlugin.h>

PXR_NAMESPACE_OPEN_SCOPE

class FlowViewportShadersDiscoveryPlugin;
TF_DECLARE_WEAK_AND_REF_PTRS(FlowViewportShadersDiscoveryPlugin);

class FlowViewportShadersDiscoveryPlugin : public NdrDiscoveryPlugin
{
public:
FlowViewportShadersDiscoveryPlugin() = default;
~FlowViewportShadersDiscoveryPlugin() override = default;
NdrNodeDiscoveryResultVec DiscoverNodes(const Context& context) override;
const NdrStringVec& GetSearchURIs() const override;
};

PXR_NAMESPACE_CLOSE_SCOPE

#endif // FLOW_VIEWPORT_SHADERS_DISCOVERY_PLUGIN_H
20 changes: 20 additions & 0 deletions lib/flowViewport/usdPlugins/shadersDiscoveryPlugin/plugInfo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"Plugins":[
{
"Info": {
"Types": {
"FlowViewportShadersDiscoveryPlugin": {
"bases": [ "NdrDiscoveryPlugin" ],
"displayName": "Flow viewport custom shaders discovery plugin."
}
},
"ShaderResources": "shaders"
},
"LibraryPath": "flowViewportShadersDiscoveryPlugin",
"Name": "flowViewportShadersDiscoveryPlugin",
"ResourcePath": "shaders",
"Root": "..",
"Type": "library"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

set(SHADERS_FILES
customBasicLightingShader.glslfx
shaderDefs.usda
)

# -----------------------------------------------------------------------------
# install
# -----------------------------------------------------------------------------

install(FILES ${SHADERS_FILES}
DESTINATION
${CMAKE_INSTALL_PREFIX}/lib/usd/flowViewportShadersDiscoveryPlugin/shaders
)
Loading

0 comments on commit f20c203

Please sign in to comment.