diff options
| author | Bryan Galdrikian <[email protected]> | 2017-02-21 12:07:59 -0800 |
|---|---|---|
| committer | Bryan Galdrikian <[email protected]> | 2017-02-21 12:07:59 -0800 |
| commit | 446ce137c6823ba9eff273bdafdaf266287c7c98 (patch) | |
| tree | d20aab3e2ed08d7b3ca71c2f40db6a93ea00c459 /NvBlast/sdk/extensions/converter/source | |
| download | blast-1.0.0-beta.tar.xz blast-1.0.0-beta.zip | |
first commitv1.0.0-beta
Diffstat (limited to 'NvBlast/sdk/extensions/converter/source')
4 files changed, 400 insertions, 0 deletions
diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtAssetBlockVersionConverter_v0_v1.h b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtAssetBlockVersionConverter_v0_v1.h new file mode 100644 index 0000000..44a0b54 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtAssetBlockVersionConverter_v0_v1.h @@ -0,0 +1,88 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTASSETBLOCKVERSIONCONVERTER_V0_V1_H +#define NVBLASTEXTASSETBLOCKVERSIONCONVERTER_V0_V1_H + + +#include "NvBlastExtBinaryBlockConverter.h" + + +namespace Nv +{ +namespace Blast +{ + +/* + WARNING: THIS CLASS IS AN EXAMPLE. + REPLACE WITH ACTUAL CONVERSION CODE ONCE NEEDED. +*/ +class NvBlastAssetBlockVersionConverter_v0_v1 : public BinaryBlockConverter::VersionConverter +{ +public: + virtual uint32_t getVersionFrom() const { return NvBlastAssetDataFormat::Initial; } + + virtual uint32_t getVersionTo() const { return 1/*NvBlastAssetDataFormat::BondCountSwap*/; } + + // remains the same + struct SupportGraph + { + uint32_t m_nodeCount; + uint32_t m_chunkIndicesOffset; + uint32_t m_adjacencyPartitionOffset; + uint32_t m_adjacentNodeIndicesOffset; + uint32_t m_adjacentBondIndicesOffset; + }; + + // prev version + struct AssetDataHeaderPrev + { + uint32_t m_formatVersion; + uint32_t m_size; + NvBlastID m_ID; + uint32_t m_totalChunkCount; + SupportGraph m_graph; + uint32_t m_leafChunkCount; + uint32_t m_firstSubsupportChunkIndex; // 0 + uint32_t m_bondCount; // 1 + }; + + // new version + struct AssetDataHeaderNew + { + uint32_t m_formatVersion; + uint32_t m_size; + NvBlastID m_ID; + uint32_t m_totalChunkCount; + SupportGraph m_graph; + uint32_t m_leafChunkCount; + uint32_t m_bondCount; // 1 + uint32_t m_firstSubsupportChunkIndex; // 0 + }; + + bool convert(const std::vector<char>& from, std::vector<char>& to) const + { + to = from; + + const AssetDataHeaderPrev* headerPrev = reinterpret_cast<const AssetDataHeaderPrev*>(from.data()); + AssetDataHeaderNew* headerNew = reinterpret_cast<AssetDataHeaderNew*>(to.data()); + headerNew->m_bondCount = headerPrev->m_bondCount; + headerNew->m_firstSubsupportChunkIndex = headerPrev->m_firstSubsupportChunkIndex; + headerNew->m_formatVersion = (uint32_t)getVersionTo(); + + return true; + } +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTASSETBLOCKVERSIONCONVERTER_V0_V1_H diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.cpp b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.cpp new file mode 100644 index 0000000..a606b70 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.cpp @@ -0,0 +1,152 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtBinaryBlockConverter.h" +#include <iostream> +#include <algorithm> +#include <queue> +#include <limits> + + +namespace Nv +{ +namespace Blast +{ + +bool BinaryBlockConverter::convertBinaryBlock(std::vector<char>& outBlock, const std::vector<VersionConverterPtr>& converters, const std::vector<char>& inBlock, uint32_t outBlockVersion, uint32_t inBlockVersion) +{ + if (inBlock.empty()) + { + std::cerr << "Conversion failed: empty input block." << std::endl; + return false; + } + + // starting version + uint32_t version; + version = inBlockVersion; + std::cout << "Input block version: " << version << std::endl; + + // target version + const uint32_t targetVersion = outBlockVersion; + std::cout << "Target version: " << targetVersion << std::endl; + + // search conversion path + std::vector<VersionConverterPtr> conversionPath; + if (!findShortestPath(conversionPath, converters, version, targetVersion)) + { + std::cerr << "Conversion failed: can't find conversion path." << std::endl; + return false; + } + + // starting convertion loop + std::vector<char> blockFrom(inBlock.begin(), inBlock.end()); + std::vector<char> blockTo(inBlock.size()); + for (const VersionConverterPtr converter : conversionPath) + { + // actual conversion call + std::cout << "Converting from version: " << converter->getVersionFrom() << " -> " << converter->getVersionTo() << " Result: "; + if (!converter->convert(blockFrom, blockTo)) + { + std::cout << "Fail." << std::endl; + std::cerr << "Conversion failed: inside converter for version: " << version << std::endl; + return false; + } + else + { + std::cout << "Success." << std::endl; + blockFrom.swap(blockTo); + version = converter->getVersionTo(); + } + } + + // copy result + outBlock = blockFrom; + + return true; +} + + +/** +Finds shortest path form versionFrom to verstionTo using breadth-first search with DP +*/ +bool BinaryBlockConverter::findShortestPath(std::vector<VersionConverterPtr>& conversionPath, const std::vector<VersionConverterPtr>& converters, uint32_t versionFrom, uint32_t versionTo) +{ + // find max version + uint32_t versionMax = 0; + for (VersionConverterPtr c : converters) + { + versionMax = std::max(versionMax, c->getVersionFrom()); + versionMax = std::max(versionMax, c->getVersionTo()); + } + + // dynamic programming data + struct Node + { + uint32_t distance; + VersionConverterPtr converter; + + Node() : distance(std::numeric_limits<uint32_t>::max()), converter(nullptr) {} + }; + std::vector<Node> nodes(versionMax + 1); + + // initial state (start from versionTo) + std::queue<uint32_t> q; + q.emplace(versionTo); + nodes[versionTo].distance = 0; + nodes[versionTo].converter = nullptr; + + // breadth-first search + bool found = false; + while (!q.empty() && !found) + { + uint32_t v0 = q.front(); + q.pop(); + + for (const VersionConverterPtr c : converters) + { + if (c->getVersionTo() == v0) + { + uint32_t v1 = c->getVersionFrom(); + if (nodes[v1].distance > nodes[v0].distance + 1) + { + nodes[v1].distance = nodes[v0].distance + 1; + nodes[v1].converter = c; + q.emplace(v1); + } + + if (c->getVersionFrom() == versionFrom) + { + found = true; + break; + } + } + } + } + + if (found) + { + // unfold found path to result conversionPath + uint32_t v = versionFrom; + conversionPath.clear(); + while (nodes[v].converter.get() != nullptr) + { + conversionPath.push_back(nodes[v].converter); + v = nodes[v].converter->getVersionTo(); + } + return true; + } + else + { + return false; + } +} + +} // namespace Blast +} // namespace Nv diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.h b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.h new file mode 100644 index 0000000..83eb6b3 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtBinaryBlockConverter.h @@ -0,0 +1,57 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#ifndef NVBLASTEXTBINARYBLOCKCONVERTER_H +#define NVBLASTEXTBINARYBLOCKCONVERTER_H + + +#include "NvBlast.h" +#include <vector> +#include <memory> + + +namespace Nv +{ +namespace Blast +{ + +/** +Generic binary block converter class. + +BinaryBlockConverter is an abstract class, as well as it's member class VersionConverter. In order to implement your own +binary converter - implement for every version conversion BinaryBlockConverter::VersionConverter. Then implement BinaryBlockConverter +where getVersionConverters() should return all your implemented BinaryBlockConverter::VersionConverter's. + +*/ +class BinaryBlockConverter +{ +public: + class VersionConverter + { + public: + virtual uint32_t getVersionFrom() const = 0; + virtual uint32_t getVersionTo() const = 0; + virtual bool convert(const std::vector<char>& from, std::vector<char>& to) const = 0; + }; + + typedef std::shared_ptr<VersionConverter> VersionConverterPtr; + + static bool convertBinaryBlock(std::vector<char>& outBlock, const std::vector<VersionConverterPtr>& converters, const std::vector<char>& inBlock, uint32_t outBlockVersion, uint32_t inBlockVersion); +protected: + +private: + static bool findShortestPath(std::vector<VersionConverterPtr>& conversionPath, const std::vector<VersionConverterPtr>& converters, uint32_t versionFrom, uint32_t versionTo); +}; + +} // namespace Blast +} // namespace Nv + + +#endif // ifndef NVBLASTEXTBINARYBLOCKCONVERTER_H diff --git a/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtDataConverter.cpp b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtDataConverter.cpp new file mode 100644 index 0000000..fe8c745 --- /dev/null +++ b/NvBlast/sdk/extensions/converter/source/conversion/NvBlastExtDataConverter.cpp @@ -0,0 +1,103 @@ +/* +* Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved. +* +* NVIDIA CORPORATION and its licensors retain all intellectual property +* and proprietary rights in and to this software, related documentation +* and any modifications thereto. Any use, reproduction, disclosure or +* distribution of this software and related documentation without an express +* license agreement from NVIDIA CORPORATION is strictly prohibited. +*/ + +#include "NvBlastExtDataConverter.h" +#include "NvBlastExtBinaryBlockConverter.h" + +#include <iostream> + +// asset converters +#include "NvBlastExtAssetBlockVersionConverter_v0_v1.h" + + +namespace Nv +{ +namespace Blast +{ + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Asset Block Converter +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +NV_INLINE std::vector<BinaryBlockConverter::VersionConverterPtr> getAssetConverters() +{ + /** + +==========================================+ + | HINT: ADD NEW VERSION CONVERTERS THERE | + +==========================================+ + */ + BinaryBlockConverter::VersionConverterPtr converters[] = + { + std::make_shared<NvBlastAssetBlockVersionConverter_v0_v1>() + }; + + return std::vector<BinaryBlockConverter::VersionConverterPtr>(converters, converters + sizeof(converters) / sizeof(converters[0])); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Family Converter +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +NV_INLINE std::vector<BinaryBlockConverter::VersionConverterPtr> getFamilyConverters() +{ + /** + +==========================================+ + | HINT: ADD NEW VERSION CONVERTERS THERE | + +==========================================+ + */ + BinaryBlockConverter::VersionConverterPtr converters[] = + { + nullptr //std::make_shared<NvBlastFamilyVersionConverter_v0_v1>() + }; + + return std::vector<BinaryBlockConverter::VersionConverterPtr>(converters, converters + sizeof(converters) / sizeof(converters[0])); +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Generic Block Converter +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +bool convertDataBlock(std::vector<char>& outBlock, const std::vector<char>& inBlock, uint32_t* outBlockVersion) +{ + // Pick header to determine dataType and version + if (inBlock.size() < sizeof(NvBlastDataBlock)) + { + std::cerr << "Conversion failed: invalid block, passed block is too small." << std::endl; + return false; + } + const NvBlastDataBlock* dataBlock = reinterpret_cast<const NvBlastDataBlock*>(inBlock.data()); + + // Select appropriate converters and version based on dataType + std::vector<BinaryBlockConverter::VersionConverterPtr> converters; + uint32_t version; + switch (dataBlock->dataType) + { + case NvBlastDataBlock::AssetDataBlock: + std::cout << "Input block dataType: NvBlastDataBlock::Asset" << std::endl; + converters = getAssetConverters(); + version = (outBlockVersion == nullptr ? static_cast<uint32_t>(NvBlastAssetDataFormat::Current) : *outBlockVersion); + break; + case NvBlastDataBlock::FamilyDataBlock: + std::cout << "Input block dataType: NvBlastDataBlock::Family" << std::endl; + converters = getFamilyConverters(); + version = (outBlockVersion == nullptr ? static_cast<uint32_t>(NvBlastFamilyDataFormat::Current) : *outBlockVersion); + break; + default: + std::cerr << "Conversion failed: unsupported dataType: " << dataBlock->dataType << std::endl; + return false; + } + + return BinaryBlockConverter::convertBinaryBlock(outBlock, converters, inBlock, version, dataBlock->formatVersion); +} + +} // namespace Blast +} // namespace Nv |