|
| 1 | +////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright (c) 2023, Cinesite VFX Ltd. All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are |
| 7 | +// met: |
| 8 | +// |
| 9 | +// * Redistributions of source code must retain the above copyright |
| 10 | +// notice, this list of conditions and the following disclaimer. |
| 11 | +// |
| 12 | +// * Redistributions in binary form must reproduce the above copyright |
| 13 | +// notice, this list of conditions and the following disclaimer in the |
| 14 | +// documentation and/or other materials provided with the distribution. |
| 15 | +// |
| 16 | +// * Neither the name of Image Engine Design nor the names of any |
| 17 | +// other contributors to this software may be used to endorse or |
| 18 | +// promote products derived from this software without specific prior |
| 19 | +// written permission. |
| 20 | +// |
| 21 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 22 | +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 23 | +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 24 | +// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 25 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 26 | +// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 27 | +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 | +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | +////////////////////////////////////////////////////////////////////////// |
| 34 | + |
| 35 | +#include "IECoreUSD/ObjectAlgo.h" |
| 36 | + |
| 37 | +#include "IECoreVDB/VDBObject.h" |
| 38 | + |
| 39 | +#include "IECore/MessageHandler.h" |
| 40 | + |
| 41 | +IECORE_PUSH_DEFAULT_VISIBILITY |
| 42 | +#include "pxr/usd/usdVol/volume.h" |
| 43 | +#include "pxr/usd/usdVol/openVDBAsset.h" |
| 44 | +IECORE_POP_DEFAULT_VISIBILITY |
| 45 | + |
| 46 | +using namespace pxr; |
| 47 | +using namespace IECore; |
| 48 | +using namespace IECoreScene; |
| 49 | +using namespace IECoreUSD; |
| 50 | + |
| 51 | +// Overview |
| 52 | +// ======== |
| 53 | +// |
| 54 | +// The closest analogue to `UsdVolVolume` in Cortex is `IECoreVDB::VDBObject`, |
| 55 | +// but the two classes have significant differences. |
| 56 | +// |
| 57 | +// - UsdVol provides schemas for referencing volume files on disk, but currently |
| 58 | +// provides no access to the volume data itself. On the other hand, VDBObject |
| 59 | +// provides direct access to data via `openvdb::GridBase::Ptr`, making it |
| 60 | +// suitable for use in live volume generation and processing. We take |
| 61 | +// advantage of this in Gaffer by providing various nodes for manipulating |
| 62 | +// volumes. |
| 63 | +// - UsdVolVolume allows the referencing of multiple fields (grids in VDB |
| 64 | +// parlance) from multiple different files, and allows the fields to be given |
| 65 | +// names that are distinct from the grid names themselves. While a VDBObject |
| 66 | +// _can_ be composed of multiple grids from arbitrary sources, this is done by |
| 67 | +// calling `insertGrid()`, and doesn't track the file of origin (if there even |
| 68 | +// was one). VDBObject is only guaranteed to reference file-backed data when |
| 69 | +// constructed from a single filename and when the loaded grids have not been |
| 70 | +// modified subsequently. |
| 71 | +// |
| 72 | +// While there is scope for extending VDBObject to provide a cleaner mapping, at |
| 73 | +// present this would have limited benefit. All our supported VDB-consuming |
| 74 | +// renderers have volume objects that reference only a single file, so we would |
| 75 | +// just be pushing the mismatch further down the pipeline. So for now we provide |
| 76 | +// the closest mapping we can, and issue warnings for any loss of data. |
| 77 | + |
| 78 | +// Reading |
| 79 | +// ======= |
| 80 | + |
| 81 | +namespace |
| 82 | +{ |
| 83 | + |
| 84 | +IECore::ObjectPtr readVolume( pxr::UsdVolVolume &volume, pxr::UsdTimeCode time, const Canceller *canceller ) |
| 85 | +{ |
| 86 | + std::string fileName; |
| 87 | + for( const auto &[fieldName, fieldPath] : volume.GetFieldPaths() ) |
| 88 | + { |
| 89 | + UsdVolOpenVDBAsset fieldAsset( volume.GetPrim().GetPrimAtPath( fieldPath ) ); |
| 90 | + if( !fieldAsset ) |
| 91 | + { |
| 92 | + IECore::msg( |
| 93 | + IECore::Msg::Warning, "IECoreVDB::VolumeAlgo::readVolume", |
| 94 | + "Ignoring \"" + fieldPath.GetAsString() + "\" because it is not an OpenVDBAsset" |
| 95 | + ); |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + SdfAssetPath fieldAssetPath; |
| 100 | + fieldAsset.GetFilePathAttr().Get( &fieldAssetPath, time ); |
| 101 | + const std::string fieldFileName = fieldAssetPath.GetResolvedPath(); |
| 102 | + |
| 103 | + if( fileName.empty() ) |
| 104 | + { |
| 105 | + fileName = fieldFileName; |
| 106 | + } |
| 107 | + else if( fieldFileName != fileName ) |
| 108 | + { |
| 109 | + IECore::msg( |
| 110 | + IECore::Msg::Warning, "IECoreVDB::VolumeAlgo::readVolume", |
| 111 | + "Ignoring file \"" + fieldFileName + "\" from field \"" + fieldPath.GetAsString() + "\"" |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if( fileName.empty() ) |
| 117 | + { |
| 118 | + IECore::msg( |
| 119 | + IECore::Msg::Warning, "IECoreVDB::VolumeAlgo::readVolume", |
| 120 | + "No file found for \"" + volume.GetPrim().GetPath().GetAsString() + "\"" |
| 121 | + ); |
| 122 | + return nullptr; |
| 123 | + } |
| 124 | + |
| 125 | + return new IECoreVDB::VDBObject( fileName ); |
| 126 | +} |
| 127 | + |
| 128 | +bool volumeMightBeTimeVarying( pxr::UsdVolVolume &volume ) |
| 129 | +{ |
| 130 | + for( const auto &[fieldName, fieldPath] : volume.GetFieldPaths() ) |
| 131 | + { |
| 132 | + UsdVolOpenVDBAsset fieldAsset( volume.GetPrim().GetPrimAtPath( fieldPath ) ); |
| 133 | + if( !fieldAsset ) |
| 134 | + { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if( fieldAsset.GetFilePathAttr().ValueMightBeTimeVarying() ) |
| 139 | + { |
| 140 | + return true; |
| 141 | + } |
| 142 | + } |
| 143 | + return false; |
| 144 | +} |
| 145 | + |
| 146 | +ObjectAlgo::ReaderDescription<pxr::UsdVolVolume> g_volumeReaderDescription( pxr::TfToken( "Volume" ), readVolume, volumeMightBeTimeVarying ); |
| 147 | + |
| 148 | +} // namespace |
| 149 | + |
| 150 | +////////////////////////////////////////////////////////////////////////// |
| 151 | +// Writing |
| 152 | +////////////////////////////////////////////////////////////////////////// |
| 153 | + |
| 154 | +namespace |
| 155 | +{ |
| 156 | + |
| 157 | +pxr::TfToken gridClass( const openvdb::GridBase *grid ) |
| 158 | +{ |
| 159 | + switch( grid->getGridClass() ) |
| 160 | + { |
| 161 | + case openvdb::GRID_LEVEL_SET : |
| 162 | + return pxr::TfToken( "GRID_LEVEL_SET" ); |
| 163 | + case openvdb::GRID_FOG_VOLUME : |
| 164 | + return pxr::TfToken( "GRID_FOG_VOLUME" ); |
| 165 | + case openvdb::GRID_STAGGERED : |
| 166 | + return pxr::TfToken( "GRID_STAGGERED" ); |
| 167 | + default : |
| 168 | + return pxr::TfToken( "GRID_UNKNOWN" ); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +bool writeVolume( const IECoreVDB::VDBObject *object, const pxr::UsdStagePtr &stage, const pxr::SdfPath &path, pxr::UsdTimeCode time ) |
| 173 | +{ |
| 174 | + if( !object->unmodifiedFromFile() ) |
| 175 | + { |
| 176 | + IECore::msg( |
| 177 | + IECore::Msg::Warning, "IECoreVDB::VolumeAlgo::writeVolume", |
| 178 | + "Not writing \"" + path.GetAsString() + "\"because VDBObject is not backed by a file" |
| 179 | + ); |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + auto volume = UsdVolVolume::Define( stage, path ); |
| 184 | + |
| 185 | + for( const auto &gridName : object->gridNames() ) |
| 186 | + { |
| 187 | + const TfToken gridNameToken( gridName ); |
| 188 | + auto fieldPath = path.AppendChild( gridNameToken ); |
| 189 | + auto fieldAsset = UsdVolOpenVDBAsset::Define( stage, fieldPath ); |
| 190 | + fieldAsset.CreateFilePathAttr().Set( SdfAssetPath( object->fileName() ), time ); |
| 191 | + fieldAsset.CreateFieldNameAttr().Set( gridNameToken ); |
| 192 | + fieldAsset.CreateFieldClassAttr().Set( gridClass( object->findGrid( gridName ).get() ) ); |
| 193 | + volume.CreateFieldRelationship( gridNameToken, fieldPath ); |
| 194 | + } |
| 195 | + |
| 196 | + return true; |
| 197 | +} |
| 198 | + |
| 199 | +ObjectAlgo::WriterDescription<IECoreVDB::VDBObject> g_volumeWriterDescription( writeVolume ); |
| 200 | + |
| 201 | +} // namespace |
0 commit comments