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/serialization/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/serialization/source')
47 files changed, 2524 insertions, 0 deletions
diff --git a/NvBlast/sdk/extensions/serialization/source/BlastSerialization.capn b/NvBlast/sdk/extensions/serialization/source/BlastSerialization.capn new file mode 100644 index 0000000..49b1dbb --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/BlastSerialization.capn @@ -0,0 +1,162 @@ +@0xaffe4498f275ee58; + +using Cxx = import "/capnp/c++.capnp"; + +$Cxx.namespace("Nv::Blast::Serialization"); + +struct Asset +{ + header @0 :NvBlastDataBlock; + + iD @1 :UUID; + + chunkCount @2 :UInt32; + + graph @3 :NvBlastSupportGraph; + + leafChunkCount @4 :UInt32; + + firstSubsupportChunkIndex @5 :UInt32; + + bondCount @6 :UInt32; + + chunks @7: List(NvBlastChunk); + + bonds @8: List(NvBlastBond); + + subtreeLeafChunkCounts @9: List(UInt32); + + chunkToGraphNodeMap @10: List(UInt32); +} + +struct TkAsset +{ + assetLL @0 :Asset; + + jointDescs @1 :List(TkAssetJointDesc); + +} + +struct ExtPxAsset +{ + asset @0 :TkAsset; + chunks @1 :List(ExtPxChunk); + subchunks @2 :List(ExtPxSubchunk); +} + +struct ExtPxChunk +{ + firstSubchunkIndex @0 :UInt32; + subchunkCount @1 :UInt32; + isStatic @2 :Bool; +} + +struct ExtPxSubchunk +{ + transform @0 :PxTransform; + geometry @1 :PxConvexMeshGeometry; +} + +struct PxConvexMeshGeometry +{ + scale @0 :PxMeshScale; + convexMesh @1 :Data; + meshFlags @2 :UInt8; + + enum Type + { + eSPHERE @0; + ePLANE @1; + eCAPSULE @2; + eBOX @3; + eCONVEXMESH @4; + eTRIANGLEMESH @5; + eHEIGHTFIELD @6; + } + + type @3 :Type; +} + +struct NvBlastDataBlock +{ + enum Type + { + assetDataBlock @0; + instanceDataBlock @1; + } + + dataType @0 :Type; + + formatVersion @1 :UInt32; + + size @2 :UInt32; +} + +struct NvBlastChunk +{ + centroid @0 :List(Float32); + + volume @1 :Float32; + + parentChunkIndex @2 :UInt32; + firstChildIndex @3 :UInt32; + childIndexStop @4 :UInt32; + userData @5 :UInt32; +} + +struct NvBlastBond +{ + normal @0 :List(Float32); + area @1 :Float32; + centroid @2 :List(Float32); + userData @3 :UInt32; +} + +struct TkAssetJointDesc +{ + nodeIndices @0 :List(UInt32); + attachPositions @1 :List(PxVec3); +} + +struct PxVec3 +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; +} + +struct PxQuat +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; + w @3 :Float32; +} + +struct PxMeshScale +{ + scale @0 :PxVec3; + rotation @1 :PxQuat; +} + +struct PxTransform +{ + q @0 :PxQuat; + p @1 :PxVec3; +} + +struct NvBlastSupportGraph +{ + nodeCount @0 : UInt32; + + chunkIndices @1 : List(UInt32); + adjacencyPartition @2 : List(UInt32); + adjacentNodeIndices @3 : List(UInt32); + adjacentBondIndices @4 : List(UInt32); +} + +struct UUID +{ + value @0 : Data; +} + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.cpp new file mode 100644 index 0000000..8d035fc --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.cpp @@ -0,0 +1,187 @@ +/* +* Copyright (c) 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 "AssetDTO.h" +#include "NvBlastIDDTO.h" +#include "NvBlastChunkDTO.h" +#include "NvBlastBondDTO.h" +#include "NvBlastAsset.h" +#include "NvBlastExtSerializationLLImpl.h" +#include "NvBlastExtGlobals.h" + +#if !defined(BLAST_LL_ALLOC) +#include "NvBlastExtAllocator.h" +#endif + +namespace Nv +{ + namespace Blast + { + bool AssetDTO::serialize(Nv::Blast::Serialization::Asset::Builder builder, const Nv::Blast::Asset * poco) + { + NvBlastIDDTO::serialize(builder.initID(), &poco->m_ID); + + builder.setLeafChunkCount(poco->m_leafChunkCount); + + builder.setFirstSubsupportChunkIndex(poco->m_firstSubsupportChunkIndex); + + capnp::List<Nv::Blast::Serialization::NvBlastChunk>::Builder chunks = builder.initChunks(poco->m_chunkCount); + + builder.setChunkCount(poco->m_chunkCount); + + NVBLAST_ASSERT_WITH_MESSAGE(builder.getChunkCount() == poco->m_chunkCount, "WTF"); + + for (uint32_t i = 0; i < poco->m_chunkCount; i++) + { + NvBlastChunk& chunk = poco->getChunks()[i]; + + NvBlastChunkDTO::serialize(chunks[i], &chunk); + } + + NVBLAST_ASSERT_WITH_MESSAGE(builder.getChunkCount() == poco->m_chunkCount, "WTF"); + + capnp::List<Nv::Blast::Serialization::NvBlastBond>::Builder bonds = builder.initBonds(poco->m_bondCount); + + builder.setBondCount(poco->m_bondCount); + + for (uint32_t i = 0; i < poco->m_bondCount; i++) + { + NvBlastBond& bond = poco->getBonds()[i]; + + NvBlastBondDTO::serialize(bonds[i], &bond); + } + + kj::ArrayPtr<uint32_t> stlcArray(poco->getSubtreeLeafChunkCounts(), poco->m_chunkCount); + builder.initSubtreeLeafChunkCounts(poco->m_chunkCount); + builder.setSubtreeLeafChunkCounts(stlcArray); + + kj::ArrayPtr<uint32_t> ctgnArray(poco->getChunkToGraphNodeMap(), poco->m_chunkCount); + builder.setChunkToGraphNodeMap(ctgnArray); + + Nv::Blast::Serialization::NvBlastSupportGraph::Builder graphBulder = builder.initGraph(); + + graphBulder.setNodeCount(poco->m_graph.m_nodeCount); + + uint32_t* ciPtr = poco->m_graph.getChunkIndices(); + + kj::ArrayPtr<const uint32_t> ciArray(ciPtr, poco->m_graph.m_nodeCount); + graphBulder.setChunkIndices(ciArray); + + kj::ArrayPtr<const uint32_t> adjPart(poco->m_graph.getAdjacencyPartition(), poco->m_graph.m_nodeCount + 1); + graphBulder.setAdjacencyPartition(adjPart); + + NVBLAST_ASSERT(graphBulder.getAdjacencyPartition().size() == poco->m_graph.m_nodeCount + 1); + + kj::ArrayPtr<const uint32_t> nodeIndices(poco->m_graph.getAdjacentNodeIndices(), poco->m_bondCount * 2); + graphBulder.setAdjacentNodeIndices(nodeIndices); + + NVBLAST_ASSERT(graphBulder.getAdjacentNodeIndices().size() == poco->m_bondCount * 2); + + kj::ArrayPtr<const uint32_t> bondIndices(poco->m_graph.getAdjacentBondIndices(), poco->m_bondCount * 2); + graphBulder.setAdjacentBondIndices(bondIndices); + + return true; + } + + Nv::Blast::Asset* AssetDTO::deserialize(Nv::Blast::Serialization::Asset::Reader reader) + { + NvBlastID EmptyId = {}; + + NvBlastExtAlloc allocFn = gAlloc; + NvBlastLog logFn = gLog; + +#if !defined(BLAST_LL_ALLOC) + allocFn = ExtAllocator::alignedAlloc16; + logFn = NvBlastTkFrameworkGet()->getLogFn(); +#endif + + void* mem = allocFn(reader.totalSize().wordCount * sizeof(uint64_t)); + + auto asset = Nv::Blast::initializeAsset(mem, EmptyId, reader.getChunkCount(), reader.getGraph().getNodeCount(), reader.getLeafChunkCount(), reader.getFirstSubsupportChunkIndex(), reader.getBondCount(), + logFn); + + bool result = deserializeInto(reader, asset); + + return result ? asset : nullptr; + } + + bool AssetDTO::deserializeInto(Nv::Blast::Serialization::Asset::Reader reader, Nv::Blast::Asset * poco) + { + NvBlastIDDTO::deserializeInto(reader.getID(), &poco->m_ID); + + NvBlastBond* bonds = poco->getBonds(); + + uint32_t bondCount = reader.getBondCount(); + for (uint32_t i = 0; i < bondCount; i++) + { + auto bondReader = reader.getBonds()[i]; + + NvBlastBondDTO::deserializeInto(bondReader, &bonds[i]); + } + + NvBlastChunk* chunks = poco->getChunks(); + + uint32_t chunkCount = reader.getChunkCount(); + for (uint32_t i = 0; i < chunkCount; i++) + { + auto chunkReader = reader.getChunks()[i]; + + NvBlastChunkDTO::deserializeInto(chunkReader, &chunks[i]); + } + + poco->m_graph.m_nodeCount = reader.getGraph().getNodeCount(); + + NVBLAST_ASSERT(reader.getSubtreeLeafChunkCounts().size() == poco->m_chunkCount); + for (uint32_t i = 0; i < poco->m_chunkCount; i++) + { + poco->getSubtreeLeafChunkCounts()[i] = reader.getSubtreeLeafChunkCounts()[i]; + } + + for (uint32_t i = 0; i < chunkCount; i++) + { + poco->getChunkToGraphNodeMap()[i] = reader.getChunkToGraphNodeMap()[i]; + } + + uint32_t* ciPtr = poco->m_graph.getChunkIndices(); + + NVBLAST_ASSERT(reader.getGraph().getChunkIndices().size() == poco->m_graph.m_nodeCount); + for (uint32_t i = 0; i < poco->m_graph.m_nodeCount; i++) + { + ciPtr[i] = reader.getGraph().getChunkIndices()[i]; + } + + uint32_t* adjPartition = poco->m_graph.getAdjacencyPartition(); + uint32_t idx = 0; + + for (uint32_t adjPartIndex : reader.getGraph().getAdjacencyPartition()) + { + adjPartition[idx++] = adjPartIndex; + } + + uint32_t* adjNodes = poco->m_graph.getAdjacentNodeIndices(); + idx = 0; + + for (uint32_t adjNodeIndex : reader.getGraph().getAdjacentNodeIndices()) + { + adjNodes[idx++] = adjNodeIndex; + } + + uint32_t* adjBonds = poco->m_graph.getAdjacentBondIndices(); + idx = 0; + + for (uint32_t adjBondIndex : reader.getGraph().getAdjacentBondIndices()) + { + adjBonds[idx++] = adjBondIndex; + } + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.h new file mode 100644 index 0000000..c090b5f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/AssetDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastAsset.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + +DTO_CLASS_LL(Asset, Nv::Blast::Asset, Nv::Blast::Serialization::Asset) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/DTOMacros.h b/NvBlast/sdk/extensions/serialization/source/DTO/DTOMacros.h new file mode 100644 index 0000000..a234aec --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/DTOMacros.h @@ -0,0 +1,44 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once + +#define DTO_CLASS(_NAME, _POCO, _SERIALIZER) \ +namespace Nv { \ +namespace Blast { \ +class _NAME ## DTO \ +{ \ +public: \ + static class physx::PxCooking* Cooking; \ + static class physx::PxPhysics* Physics; \ + \ + static bool serialize(_SERIALIZER::Builder builder, const _POCO * poco); \ + static _POCO* deserialize(_SERIALIZER::Reader reader); \ + static bool deserializeInto(_SERIALIZER::Reader reader, _POCO * poco); \ +}; \ +} \ +} \ + \ + +#define DTO_CLASS_LL(_NAME, _POCO, _SERIALIZER) \ +namespace Nv { \ +namespace Blast { \ +class _NAME ## DTO \ +{ \ +public: \ + \ + static bool serialize(_SERIALIZER::Builder builder, const _POCO * poco); \ + static _POCO* deserialize(_SERIALIZER::Reader reader); \ + static bool deserializeInto(_SERIALIZER::Reader reader, _POCO * poco); \ +}; \ +} \ +} \ + \ + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.cpp new file mode 100644 index 0000000..cf4cadc --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.cpp @@ -0,0 +1,78 @@ +/* +* Copyright (c) 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 "ExtPxAssetDTO.h" +#include "TkAssetDTO.h" +#include "ExtPxChunkDTO.h" +#include "ExtPxSubchunkDTO.h" +#include "physics/NvBlastExtPxAssetImpl.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + bool ExtPxAssetDTO::serialize(Nv::Blast::Serialization::ExtPxAsset::Builder builder, const Nv::Blast::ExtPxAsset * poco) + { + TkAssetDTO::serialize(builder.getAsset(), &poco->getTkAsset()); + + auto chunks = builder.initChunks(poco->getChunkCount()); + + for (uint32_t i = 0; i <poco->getChunkCount(); i++) + { + ExtPxChunkDTO::serialize(chunks[i], &poco->getChunks()[i]); + } + + auto subchunks = builder.initSubchunks(poco->getSubchunkCount()); + + for (uint32_t i = 0; i < poco->getSubchunkCount(); i++) + { + ExtPxSubchunkDTO::serialize(subchunks[i], &poco->getSubchunks()[i]); + } + + return true; + } + + Nv::Blast::ExtPxAsset* ExtPxAssetDTO::deserialize(Nv::Blast::Serialization::ExtPxAsset::Reader reader) + { + auto tkAsset = TkAssetDTO::deserialize(reader.getAsset()); + + Nv::Blast::ExtPxAssetImpl* asset = reinterpret_cast<Nv::Blast::ExtPxAssetImpl*>(Nv::Blast::ExtPxAsset::create(tkAsset)); + + NVBLAST_ASSERT(asset != nullptr); + + auto chunks = asset->getChunksArray(); + + chunks.resize(reader.getChunks().size()); + for (uint32_t i = 0; i < reader.getChunks().size(); i++) + { + ExtPxChunkDTO::deserializeInto(reader.getChunks()[i], &chunks[i]); + } + + auto subchunks = asset->getSubchunksArray(); + + subchunks.resize(reader.getSubchunks().size()); + for (uint32_t i = 0; i < reader.getSubchunks().size(); i++) + { + ExtPxSubchunkDTO::deserializeInto(reader.getSubchunks()[i], &subchunks[i]); + } + + return asset; + } + + bool ExtPxAssetDTO::deserializeInto(Nv::Blast::Serialization::ExtPxAsset::Reader reader, Nv::Blast::ExtPxAsset * poco) + { + reader = reader; + poco = nullptr; + //NOTE: Because of the way this is structured, can't do this. + return false; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.h new file mode 100644 index 0000000..a35d38a --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxAssetDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "NvBlastBondDTO.h" +#include "NvBlastExtPxAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" + +DTO_CLASS(ExtPxAsset, Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.cpp new file mode 100644 index 0000000..e096bc1 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.cpp @@ -0,0 +1,43 @@ +/* +* Copyright (c) 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 "ExtPxChunkDTO.h" + +namespace Nv +{ + namespace Blast + { + bool ExtPxChunkDTO::serialize(Nv::Blast::Serialization::ExtPxChunk::Builder builder, const Nv::Blast::ExtPxChunk * poco) + { + builder.setFirstSubchunkIndex(poco->firstSubchunkIndex); + builder.setSubchunkCount(poco->subchunkCount); + builder.setIsStatic(poco->isStatic); + + return true; + } + + Nv::Blast::ExtPxChunk* ExtPxChunkDTO::deserialize(Nv::Blast::Serialization::ExtPxChunk::Reader reader) + { + reader = reader; + //TODO: Allocate with ExtContext and return + + return nullptr; + } + + bool ExtPxChunkDTO::deserializeInto(Nv::Blast::Serialization::ExtPxChunk::Reader reader, Nv::Blast::ExtPxChunk * poco) + { + poco->firstSubchunkIndex = reader.getFirstSubchunkIndex(); + poco->subchunkCount = reader.getSubchunkCount(); + poco->isStatic = reader.getIsStatic(); + + return true; + } + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.h new file mode 100644 index 0000000..1ff36df --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxChunkDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastExtPxAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" + +DTO_CLASS(ExtPxChunk, Nv::Blast::ExtPxChunk, Nv::Blast::Serialization::ExtPxChunk) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.cpp new file mode 100644 index 0000000..cc2be96 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.cpp @@ -0,0 +1,43 @@ +/* +* Copyright (c) 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 "ExtPxSubchunkDTO.h" +#include "PxTransformDTO.h" +#include "PxConvexMeshGeometryDTO.h" + +namespace Nv +{ + namespace Blast + { + bool ExtPxSubchunkDTO::serialize(Nv::Blast::Serialization::ExtPxSubchunk::Builder builder, const Nv::Blast::ExtPxSubchunk * poco) + { + PxTransformDTO::serialize(builder.getTransform(), &poco->transform); + PxConvexMeshGeometryDTO::serialize(builder.getGeometry(), &poco->geometry); + + return true; + } + + Nv::Blast::ExtPxSubchunk* ExtPxSubchunkDTO::deserialize(Nv::Blast::Serialization::ExtPxSubchunk::Reader reader) + { + reader = reader; + //TODO: Allocate with ExtContext and return + + return nullptr; + } + + bool ExtPxSubchunkDTO::deserializeInto(Nv::Blast::Serialization::ExtPxSubchunk::Reader reader, Nv::Blast::ExtPxSubchunk * poco) + { + PxTransformDTO::deserializeInto(reader.getTransform(), &poco->transform); + + return true; + } + + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.h new file mode 100644 index 0000000..91f78e0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/ExtPxSubchunkDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "NvBlastExtPxAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "DTOMacros.h" + +DTO_CLASS(ExtPxSubchunk, Nv::Blast::ExtPxSubchunk, Nv::Blast::Serialization::ExtPxSubchunk) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.cpp new file mode 100644 index 0000000..27cbb11 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.cpp @@ -0,0 +1,63 @@ +/* +* Copyright (c) 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 "NvBlastBondDTO.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + + bool NvBlastBondDTO::serialize(Nv::Blast::Serialization::NvBlastBond::Builder builder, const NvBlastBond * poco) + { + NVBLAST_ASSERT(poco != nullptr); + + kj::ArrayPtr<const float> normArray(poco->normal, 3); + + builder.setNormal(normArray); + + builder.setArea(poco->area); + + kj::ArrayPtr<const float> centArray(poco->centroid, 3); + + builder.setCentroid(centArray); + + builder.setUserData(poco->userData); + + return true; + } + + NvBlastBond* NvBlastBondDTO::deserialize(Nv::Blast::Serialization::NvBlastBond::Reader reader) + { + //FIXME + reader = reader; + //TODO: Allocate with ExtContext and return + return nullptr; + } + + bool NvBlastBondDTO::deserializeInto(Nv::Blast::Serialization::NvBlastBond::Reader reader, NvBlastBond * poco) + { + poco->area = reader.getArea(); + + poco->centroid[0] = reader.getCentroid()[0]; + poco->centroid[1] = reader.getCentroid()[1]; + poco->centroid[2] = reader.getCentroid()[2]; + + poco->normal[0] = reader.getNormal()[0]; + poco->normal[1] = reader.getNormal()[1]; + poco->normal[2] = reader.getNormal()[2]; + + poco->userData = reader.getUserData(); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.h new file mode 100644 index 0000000..8b67bd7 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastBondDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTypes.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + +DTO_CLASS_LL(NvBlastBond, NvBlastBond, Nv::Blast::Serialization::NvBlastBond) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.cpp new file mode 100644 index 0000000..38814ed --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.cpp @@ -0,0 +1,60 @@ +/* +* Copyright (c) 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 "NvBlastChunkDTO.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + bool NvBlastChunkDTO::serialize(Nv::Blast::Serialization::NvBlastChunk::Builder builder, const NvBlastChunk* poco) + { + NVBLAST_ASSERT(poco != nullptr); + + kj::ArrayPtr<const float> centArray(poco->centroid, 3); + builder.setCentroid(centArray); + + builder.setVolume(poco->volume); + + builder.setParentChunkIndex(poco->parentChunkIndex); + builder.setFirstChildIndex(poco->firstChildIndex); + builder.setChildIndexStop(poco->childIndexStop); + builder.setUserData(poco->userData); + + return true; + } + + NvBlastChunk* NvBlastChunkDTO::deserialize(Nv::Blast::Serialization::NvBlastChunk::Reader reader) + { + //FIXME + reader = reader; + + return nullptr; + } + + bool NvBlastChunkDTO::deserializeInto(Nv::Blast::Serialization::NvBlastChunk::Reader reader, NvBlastChunk* target) + { + NVBLAST_ASSERT(target != nullptr); + + target->centroid[0] = reader.getCentroid()[0]; + target->centroid[1] = reader.getCentroid()[1]; + target->centroid[2] = reader.getCentroid()[2]; + + target->childIndexStop = reader.getChildIndexStop(); + target->firstChildIndex = reader.getFirstChildIndex(); + target->parentChunkIndex = reader.getParentChunkIndex(); + target->userData = reader.getUserData(); + target->volume = reader.getVolume(); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.h new file mode 100644 index 0000000..5fec498 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastChunkDTO.h @@ -0,0 +1,18 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTypes.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + + +DTO_CLASS_LL(NvBlastChunk, NvBlastChunk, Nv::Blast::Serialization::NvBlastChunk) + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.cpp new file mode 100644 index 0000000..e540cd8 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.cpp @@ -0,0 +1,48 @@ +/* +* Copyright (c) 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 "NvBlastIDDTO.h" +#include "NvBlastTypes.h" +#include "NvBlastAssert.h" +#include "generated/NvBlastExtSerializationLL.capn.h" + + +namespace Nv +{ + namespace Blast + { + + bool NvBlastIDDTO::serialize(Nv::Blast::Serialization::UUID::Builder builder, const NvBlastID * poco) + { + capnp::Data::Reader idArrayReader((unsigned char *)poco->data, 16); + builder.setValue(idArrayReader); + + return true; + } + + NvBlastID* NvBlastIDDTO::deserialize(Nv::Blast::Serialization::UUID::Reader reader) + { + //FIXME + reader = reader; + //TODO: Allocate with ExtContext and return + + return nullptr; + } + + bool NvBlastIDDTO::deserializeInto(Nv::Blast::Serialization::UUID::Reader reader, NvBlastID * poco) + { + NVBLAST_ASSERT_WITH_MESSAGE(reader.getValue().size() == 16, "BlastID must be 16 bytes"); + + memcpy(poco, reader.getValue().begin(), 16); + + return true; + } + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.h new file mode 100644 index 0000000..afe6cf0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/NvBlastIDDTO.h @@ -0,0 +1,16 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "NvBlastTypes.h" +#include "generated/NvBlastExtSerializationLL.capn.h" +#include "DTOMacros.h" + +DTO_CLASS_LL(NvBlastID, NvBlastID, ::Nv::Blast::Serialization::UUID) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.cpp new file mode 100644 index 0000000..1c46f9e --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.cpp @@ -0,0 +1,127 @@ +/* +* Copyright (c) 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 "PxConvexMeshGeometryDTO.h" +#include "PxMeshScaleDTO.h" +#include "NvBlastAssert.h" +#include "NvBlastExtKJPxInputStream.h" +#include "NvBlastExtKJPxOutputStream.h" +#include "PxConvexMeshDesc.h" +#include "NvBlastExtSerialization.h" +#include "PxVec3.h" +#include <algorithm> +#include "PxPhysics.h" + + +namespace Nv +{ + namespace Blast + { + physx::PxCooking* PxConvexMeshGeometryDTO::Cooking = nullptr; + physx::PxPhysics* PxConvexMeshGeometryDTO::Physics = nullptr; + + bool PxConvexMeshGeometryDTO::serialize(Nv::Blast::Serialization::PxConvexMeshGeometry::Builder builder, const physx::PxConvexMeshGeometry * poco) + { + PxMeshScaleDTO::serialize(builder.getScale(), &poco->scale); + + //TODO: Use cooking.cookConvexMesh to cook the mesh to a stream - then get that backing buffer and put it into the Data field + + physx::PxConvexMeshDesc desc; + desc.points.data = poco->convexMesh->getVertices(); + desc.points.count = poco->convexMesh->getNbVertices(); + desc.points.stride = sizeof(physx::PxVec3); + + std::vector<uint32_t> indicesScratch; + std::vector<physx::PxHullPolygon> hullPolygonsScratch; + + hullPolygonsScratch.resize(poco->convexMesh->getNbPolygons()); + + uint32_t indexCount = 0; + for (uint32_t i = 0; i < hullPolygonsScratch.size(); i++) + { + physx::PxHullPolygon polygon; + poco->convexMesh->getPolygonData(i, polygon); + if (polygon.mNbVerts) + { + indexCount = std::max<uint32_t>(indexCount, polygon.mIndexBase + polygon.mNbVerts); + } + } + indicesScratch.resize(indexCount); + + for (uint32_t i = 0; i < hullPolygonsScratch.size(); i++) + { + physx::PxHullPolygon polygon; + poco->convexMesh->getPolygonData(i, polygon); + for (uint32_t j = 0; j < polygon.mNbVerts; j++) + { + indicesScratch[polygon.mIndexBase + j] = poco->convexMesh->getIndexBuffer()[polygon.mIndexBase + j]; + } + + hullPolygonsScratch[i] = polygon; + } + + desc.indices.count = indexCount; + desc.indices.data = indicesScratch.data(); + desc.indices.stride = sizeof(uint32_t); + + desc.polygons.count = poco->convexMesh->getNbPolygons(); + desc.polygons.data = hullPolygonsScratch.data(); + desc.polygons.stride = sizeof(physx::PxHullPolygon); + + + std::vector<unsigned char> buffer; + buffer.resize(16 * 1024 * 1024); // No idea how much memory is needed! Allocate 16MB + kj::ArrayPtr<unsigned char> bufferArray(buffer.data(), buffer.size()); + + Nv::Blast::ExtKJPxOutputStream outputStream(bufferArray); + + bool cookResult = Cooking->cookConvexMesh(desc, outputStream); + + if (!cookResult) + { + return false; + } + + kj::ArrayPtr<unsigned char> cookedBuffer(outputStream.getBuffer().begin(), outputStream.getWrittenBytes()); + + builder.setConvexMesh(cookedBuffer); + + // builder.getConvexMesh(). + + return true; + } + + physx::PxConvexMeshGeometry* PxConvexMeshGeometryDTO::deserialize(Nv::Blast::Serialization::PxConvexMeshGeometry::Reader reader) + { + NVBLAST_ASSERT(PxConvexMeshGeometryDTO::Cooking != nullptr); + + reader = reader; + + return nullptr; + } + + bool PxConvexMeshGeometryDTO::deserializeInto(Nv::Blast::Serialization::PxConvexMeshGeometry::Reader reader, physx::PxConvexMeshGeometry * poco) + { + NVBLAST_ASSERT(PxConvexMeshGeometryDTO::Cooking != nullptr); + + PxMeshScaleDTO::deserializeInto(reader.getScale(), &poco->scale); + + Nv::Blast::ExtKJPxInputStream inputStream(reader.getConvexMesh()); + + //NOTE: Naive approach, no shared convex hulls + poco->convexMesh = Physics->createConvexMesh(inputStream); + + return false; + } + + + + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.h new file mode 100644 index 0000000..27b3754 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxConvexMeshGeometryDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxConvexMeshGeometry.h" +#include "PxCooking.h" + +DTO_CLASS(PxConvexMeshGeometry, physx::PxConvexMeshGeometry, Nv::Blast::Serialization::PxConvexMeshGeometry) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.cpp new file mode 100644 index 0000000..8fee6ad --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.cpp @@ -0,0 +1,42 @@ +/* +* Copyright (c) 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 "PxMeshScaleDTO.h" +#include "PxVec3DTO.h" +#include "PxQuatDTO.h" + +namespace Nv +{ + namespace Blast + { + bool PxMeshScaleDTO::serialize(Nv::Blast::Serialization::PxMeshScale::Builder builder, const physx::PxMeshScale * poco) + { + PxVec3DTO::serialize(builder.getScale(), &poco->scale); + PxQuatDTO::serialize(builder.getRotation(), &poco->rotation); + + return true; + } + + physx::PxMeshScale* PxMeshScaleDTO::deserialize(Nv::Blast::Serialization::PxMeshScale::Reader reader) + { + reader = reader; + return nullptr; + } + + bool PxMeshScaleDTO::deserializeInto(Nv::Blast::Serialization::PxMeshScale::Reader reader, physx::PxMeshScale * poco) + { + PxVec3DTO::deserializeInto(reader.getScale(), &poco->scale); + PxQuatDTO::deserializeInto(reader.getRotation(), &poco->rotation); + + return true; + } + } +} + diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.h new file mode 100644 index 0000000..7b758c8 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxMeshScaleDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "PxMeshScale.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(PxMeshScale, physx::PxMeshScale, Nv::Blast::Serialization::PxMeshScale) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.cpp new file mode 100644 index 0000000..8faeaa6 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.cpp @@ -0,0 +1,45 @@ +/* +* Copyright (c) 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 "PxQuatDTO.h" + +namespace Nv +{ + namespace Blast + { + + bool PxQuatDTO::serialize(Nv::Blast::Serialization::PxQuat::Builder builder, const physx::PxQuat * poco) + { + builder.setX(poco->x); + builder.setY(poco->y); + builder.setZ(poco->z); + builder.setW(poco->w); + + return true; + } + + physx::PxQuat* PxQuatDTO::deserialize(Nv::Blast::Serialization::PxQuat::Reader reader) + { + reader = reader; + return nullptr; + } + + bool PxQuatDTO::deserializeInto(Nv::Blast::Serialization::PxQuat::Reader reader, physx::PxQuat * poco) + { + poco->x = reader.getX(); + poco->y = reader.getY(); + poco->z = reader.getZ(); + poco->w = reader.getW(); + + return true; + } + + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.h new file mode 100644 index 0000000..460d6c5 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxQuatDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "PxQuat.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(PxQuat, physx::PxQuat, Nv::Blast::Serialization::PxQuat) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.cpp new file mode 100644 index 0000000..20a7cbb --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.cpp @@ -0,0 +1,42 @@ +/* +* Copyright (c) 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 "PxTransformDTO.h" +#include "PxQuatDTO.h" +#include "PxVec3DTO.h" + +namespace Nv +{ + namespace Blast + { + + bool PxTransformDTO::serialize(Nv::Blast::Serialization::PxTransform::Builder builder, const physx::PxTransform * poco) + { + PxQuatDTO::serialize(builder.getQ(), &poco->q); + PxVec3DTO::serialize(builder.getP(), &poco->p); + + return true; + } + + physx::PxTransform* PxTransformDTO::deserialize(Nv::Blast::Serialization::PxTransform::Reader reader) + { + reader = reader; + return nullptr; + } + + bool PxTransformDTO::deserializeInto(Nv::Blast::Serialization::PxTransform::Reader reader, physx::PxTransform * poco) + { + PxQuatDTO::deserializeInto(reader.getQ(), &poco->q); + PxVec3DTO::deserializeInto(reader.getP(), &poco->p); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.h new file mode 100644 index 0000000..49a6b73 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxTransformDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "PxTransform.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(PxTransform, physx::PxTransform, Nv::Blast::Serialization::PxTransform) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.cpp new file mode 100644 index 0000000..9827cd0 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.cpp @@ -0,0 +1,45 @@ +/* +* Copyright (c) 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 "PxVec3DTO.h" +#include "NvBlastAssert.h" + +namespace Nv +{ + namespace Blast + { + bool PxVec3DTO::serialize(Nv::Blast::Serialization::PxVec3::Builder builder, const physx::PxVec3 * poco) + { + NVBLAST_ASSERT(poco != nullptr); + + builder.setX(poco->x); + builder.setY(poco->y); + builder.setZ(poco->z); + + return true; + } + + physx::PxVec3* PxVec3DTO::deserialize(Nv::Blast::Serialization::PxVec3::Reader reader) + { + //TODO: Allocate using ExtContext and return + reader = reader; + return nullptr; + } + + bool PxVec3DTO::deserializeInto(Nv::Blast::Serialization::PxVec3::Reader reader, physx::PxVec3* target) + { + target->x = reader.getX(); + target->y = reader.getY(); + target->z = reader.getZ(); + + return true; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.h new file mode 100644 index 0000000..8a04c8b --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/PxVec3DTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxVec3.h" +#include "PxCooking.h" + +DTO_CLASS(PxVec3, physx::PxVec3, Nv::Blast::Serialization::PxVec3) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.cpp new file mode 100644 index 0000000..acc55ba --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.cpp @@ -0,0 +1,67 @@ +/* +* Copyright (c) 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 "TkAssetDTO.h" +#include "AssetDTO.h" +#include "TkAssetJointDescDTO.h" +#include <vector> +#include "NvBlastTkFramework.h" + + + +namespace Nv +{ + namespace Blast + { + bool TkAssetDTO::serialize(Nv::Blast::Serialization::TkAsset::Builder builder, const Nv::Blast::TkAsset * poco) + { + const Asset* assetLL = reinterpret_cast<const Nv::Blast::Asset*>(poco->getAssetLL()); + + Nv::Blast::AssetDTO::serialize(builder.getAssetLL(), assetLL); + + uint32_t jointDescCount = poco->getJointDescCount(); + + capnp::List<Nv::Blast::Serialization::TkAssetJointDesc>::Builder jointDescs = builder.initJointDescs(jointDescCount); + + for (uint32_t i = 0; i < jointDescCount; i++) + { + TkAssetJointDescDTO::serialize(jointDescs[i], &poco->getJointDescs()[i]); + } + + return true; + } + + Nv::Blast::TkAsset* TkAssetDTO::deserialize(Nv::Blast::Serialization::TkAsset::Reader reader) + { + const NvBlastAsset* assetLL = reinterpret_cast<const NvBlastAsset*>(AssetDTO::deserialize(reader.getAssetLL())); + + std::vector<Nv::Blast::TkAssetJointDesc> jointDescs; + jointDescs.resize(reader.getJointDescs().size()); + + for (uint32_t i = 0; i < jointDescs.size(); i++) + { + TkAssetJointDescDTO::deserializeInto(reader.getJointDescs()[i], &jointDescs[i]); + } + + // Make sure to set ownsAsset to true - this is serialization and no one else owns it. + Nv::Blast::TkAsset* asset = NvBlastTkFrameworkGet()->createAsset(assetLL, jointDescs.data(), jointDescs.size(), true); + + return asset; + } + + bool TkAssetDTO::deserializeInto(Nv::Blast::Serialization::TkAsset::Reader reader, Nv::Blast::TkAsset * poco) + { + reader = reader; + poco = nullptr; + // NOTE: Because of the way TkAsset is currently structured, this won't work. + return false; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.h new file mode 100644 index 0000000..1b21eba --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTkAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(TkAsset, Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset) diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.cpp b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.cpp new file mode 100644 index 0000000..9118d19 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.cpp @@ -0,0 +1,53 @@ +/* +* Copyright (c) 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 "TkAssetJointDescDTO.h" +#include "PxVec3DTO.h" + + +namespace Nv +{ + namespace Blast + { + + bool TkAssetJointDescDTO::serialize(Nv::Blast::Serialization::TkAssetJointDesc::Builder builder, const Nv::Blast::TkAssetJointDesc * poco) + { + kj::ArrayPtr<const uint32_t> nodeIndices(poco->nodeIndices, 2); + builder.setNodeIndices(nodeIndices); + + for (int i = 0; i < 2; i++) + { + PxVec3DTO::serialize(builder.getAttachPositions()[i], &poco->attachPositions[i]); + } + + return true; + } + + Nv::Blast::TkAssetJointDesc* TkAssetJointDescDTO::deserialize(Nv::Blast::Serialization::TkAssetJointDesc::Reader reader) + { + //TODO: Allocate with ExtContent and return + + reader = reader; + + return nullptr; + } + + bool TkAssetJointDescDTO::deserializeInto(Nv::Blast::Serialization::TkAssetJointDesc::Reader reader, Nv::Blast::TkAssetJointDesc * poco) + { + PxVec3DTO::deserializeInto(reader.getAttachPositions()[0], &poco->attachPositions[0]); + PxVec3DTO::deserializeInto(reader.getAttachPositions()[1], &poco->attachPositions[1]); + + poco->nodeIndices[0] = reader.getNodeIndices()[0]; + poco->nodeIndices[1] = reader.getNodeIndices()[1]; + + return true; + } + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.h b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.h new file mode 100644 index 0000000..88364bd --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/DTO/TkAssetJointDescDTO.h @@ -0,0 +1,17 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "DTOMacros.h" +#include "NvBlastTkAsset.h" +#include "generated/NvBlastExtSerialization.capn.h" +#include "PxCooking.h" + +DTO_CLASS(TkAssetJointDesc, Nv::Blast::TkAssetJointDesc, Nv::Blast::Serialization::TkAssetJointDesc) diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtGlobals.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtGlobals.h new file mode 100644 index 0000000..dd50afd --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtGlobals.h @@ -0,0 +1,10 @@ +#pragma once + +/** +Function pointer type for allocation - has same signature as stdlib malloc. +*/ +typedef void* (*NvBlastExtAlloc)(size_t size); + +extern NvBlastExtAlloc gAlloc; +extern NvBlastLog gLog; + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.cpp new file mode 100644 index 0000000..7ef9b62 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.cpp @@ -0,0 +1,38 @@ +/* +* Copyright (c) 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 "NvBlastExtInputStream.h" + + +Nv::Blast::ExtInputStream::ExtInputStream(std::istream &inputStream): + m_inputStream(inputStream) +{ + +} + + +size_t Nv::Blast::ExtInputStream::tryRead(void* buffer, size_t /*minBytes*/, size_t maxBytes) +{ + m_inputStream.read((char *) buffer, maxBytes); + + if (m_inputStream.fail()) + { + // Throw exception, log error +// NVBLASTEXT_LOG_ERROR("Failure when reading from stream"); + } + + // Since we're using a blocking read above, if we don't have maxBytes we're probably done + if ((size_t) m_inputStream.gcount() < maxBytes) + { +// NVBLASTEXT_LOG_ERROR("Failed to read requested number of bytes during blocking read."); + } + + return m_inputStream.gcount(); +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.h new file mode 100644 index 0000000..9b19d9c --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtInputStream.h @@ -0,0 +1,32 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "kj/io.h" +#include <istream> + +namespace Nv +{ + namespace Blast + { + class ExtInputStream : public kj::InputStream + { + public: + ExtInputStream() = delete; + ExtInputStream(std::istream &inputStream); + + // Returns a read of maxBytes. This is supposed to be happy doing partial reads, but currently isn't. + virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; + + private: + std::istream &m_inputStream; + }; + } +}
\ No newline at end of file diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.cpp new file mode 100644 index 0000000..9af13a9 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.cpp @@ -0,0 +1,32 @@ +/* +* Copyright (c) 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 "NvBlastExtKJPxInputStream.h" + +namespace Nv +{ + namespace Blast + { + ExtKJPxInputStream::ExtKJPxInputStream(capnp::Data::Reader inReader) : + dataReader(inReader), + inputStream(nullptr) + { + kj::ArrayPtr<const unsigned char> buffer(inReader.begin(), inReader.size()); + + inputStream = std::make_shared<kj::ArrayInputStream>(buffer); + } + + uint32_t ExtKJPxInputStream::read(void* dest, uint32_t count) + { + return inputStream->tryRead(dest, count, count); + } + } +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.h new file mode 100644 index 0000000..452892d --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxInputStream.h @@ -0,0 +1,41 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "PxIO.h" +#include "capnp/common.h" +#include "kj/io.h" +#include <memory> +#include "generated/NvBlastExtSerialization.capn.h" + +namespace Nv +{ + namespace Blast + { + /* + A wrapper around a Capn Proto Data reader. + + Since it needs to behave like a stream, it's internally wrapped in a stream. + + */ + class ExtKJPxInputStream : public physx::PxInputStream + { + public: + ExtKJPxInputStream(capnp::Data::Reader inReader); + ~ExtKJPxInputStream() = default; + + virtual uint32_t read(void* dest, uint32_t count) override; + + private: + capnp::Data::Reader dataReader; + std::shared_ptr<kj::ArrayInputStream> inputStream; + }; + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.cpp new file mode 100644 index 0000000..0f17a01 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.cpp @@ -0,0 +1,35 @@ +/* +* Copyright (c) 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 "NvBlastExtKJPxOutputStream.h" + +namespace Nv +{ + namespace Blast + { + ExtKJPxOutputStream::ExtKJPxOutputStream(kj::ArrayPtr<unsigned char> inBuffer) : + writtenBytes(0), + Buffer(inBuffer), + outputStream(nullptr) + { + outputStream = std::make_shared<kj::ArrayOutputStream>(inBuffer); + } + + uint32_t ExtKJPxOutputStream::write(const void* src, uint32_t count) + { + outputStream->write(src, count); + + writtenBytes += count; + + return count; + } + } +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.h new file mode 100644 index 0000000..0ed563f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtKJPxOutputStream.h @@ -0,0 +1,40 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "PxIO.h" +#include "kj/common.h" +#include <memory> +#include "kj/io.h" + +namespace Nv +{ + namespace Blast + { + class ExtKJPxOutputStream : public physx::PxOutputStream + { + public: + ExtKJPxOutputStream(kj::ArrayPtr<unsigned char> inBuffer); + ~ExtKJPxOutputStream() = default; + + virtual uint32_t write(const void* src, uint32_t count) override; + + uint32_t getWrittenBytes() { return writtenBytes; } + + kj::ArrayPtr<unsigned char> getBuffer() { return Buffer; } + + private: + uint32_t writtenBytes; + + kj::ArrayPtr<unsigned char> Buffer; + std::shared_ptr<kj::ArrayOutputStream> outputStream; + }; + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.cpp new file mode 100644 index 0000000..cf6e31f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.cpp @@ -0,0 +1,24 @@ +/* +* Copyright (c) 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 "NvBlastExtOutputStream.h" + + + +Nv::Blast::ExtOutputStream::ExtOutputStream(std::ostream &outputStream): + m_outputStream(outputStream) +{ + +} + +void Nv::Blast::ExtOutputStream::write(const void* buffer, size_t size) +{ + m_outputStream.write((char *) buffer, size); +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.h new file mode 100644 index 0000000..ba0044e --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtOutputStream.h @@ -0,0 +1,31 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "kj/io.h" +#include <ostream> + +namespace Nv +{ + namespace Blast + { + class ExtOutputStream : public kj::OutputStream + { + + public: + ExtOutputStream() = delete; + ExtOutputStream(std::ostream &outputStream); + + virtual void write(const void* buffer, size_t size) override; + private: + std::ostream &m_outputStream; + }; + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.capn b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.capn new file mode 100644 index 0000000..ddc439a --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.capn @@ -0,0 +1,95 @@ +# Copyright (c) 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. + +@0xaffe4498f275ee58; + +using Cxx = import "/capnp/c++.capnp"; + +using LL = import "NvBlastExtSerializationLL.capn"; + +$Cxx.namespace("Nv::Blast::Serialization"); + +struct TkAsset +{ + assetLL @0 :LL.Asset; + + jointDescs @1 :List(TkAssetJointDesc); +} + +struct ExtPxAsset +{ + asset @0 :TkAsset; + chunks @1 :List(ExtPxChunk); + subchunks @2 :List(ExtPxSubchunk); +} + +struct ExtPxChunk +{ + firstSubchunkIndex @0 :UInt32; + subchunkCount @1 :UInt32; + isStatic @2 :Bool; +} + +struct ExtPxSubchunk +{ + transform @0 :PxTransform; + geometry @1 :PxConvexMeshGeometry; +} + +struct PxConvexMeshGeometry +{ + scale @0 :PxMeshScale; + convexMesh @1 :Data; + meshFlags @2 :UInt8; + + enum Type + { + eSPHERE @0; + ePLANE @1; + eCAPSULE @2; + eBOX @3; + eCONVEXMESH @4; + eTRIANGLEMESH @5; + eHEIGHTFIELD @6; + } + + type @3 :Type; +} + +struct TkAssetJointDesc +{ + nodeIndices @0 :List(UInt32); + attachPositions @1 :List(PxVec3); +} + +struct PxVec3 +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; +} + +struct PxQuat +{ + x @0 :Float32; + y @1 :Float32; + z @2 :Float32; + w @3 :Float32; +} + +struct PxMeshScale +{ + scale @0 :PxVec3; + rotation @1 :PxQuat; +} + +struct PxTransform +{ + q @0 :PxQuat; + p @1 :PxVec3; +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.cpp new file mode 100644 index 0000000..28a1553 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.cpp @@ -0,0 +1,146 @@ +#include "NvBlastExtSerialization.h" +#include "BlastSerialization.h" +#include <memory> +#include "PxPhysicsVersion.h" +#include "PxConvexMeshGeometryDTO.h" +#include "NvBlastExtDefs.h" + + +// This is terrible. +physx::PxPhysics* g_Physics = nullptr; + + +std::shared_ptr<physx::PxCooking> getCooking() +{ + physx::PxCookingParams cookingParams(g_Physics->getTolerancesScale()); + cookingParams.buildGPUData = true; + + std::shared_ptr<physx::PxCooking> m_Cooking = std::shared_ptr<physx::PxCooking>(PxCreateCooking(PX_PHYSICS_VERSION, g_Physics->getFoundation(), cookingParams), [=](physx::PxCooking* cooking) + { + cooking->release(); + }); + + NVBLASTEXT_CHECK_ERROR(m_Cooking, "Error: failed to create PhysX Cooking\n", return nullptr); + + return m_Cooking; +} + + +extern "C" +{ + NVBLAST_API void setPhysXSDK(physx::PxPhysics* physXSDK) + { + g_Physics = physXSDK; + } + + NVBLAST_API NvBlastAsset* deserializeAsset(const unsigned char* input, uint32_t size) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserialize(input, size); + } + + NVBLAST_API NvBlastAsset* deserializeAssetFromStream(std::istream &inputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeAssetIntoStream(const NvBlastAsset *asset, std::ostream &outputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::Asset *>(asset), outputStream); + } + + NVBLAST_API bool serializeAssetIntoNewBuffer(const NvBlastAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeAssetIntoExistingBuffer(const NvBlastAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), buffer, maxSize, usedSize); + } + + ////////////////////////////////////////////////////////////////////////// + // TkAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAsset(const unsigned char* input, uint32_t size) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAssetFromStream(std::istream &inputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeTkAssetIntoStream(const Nv::Blast::TkAsset *asset, std::ostream &outputStream) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeTkAssetIntoNewBuffer(const Nv::Blast::TkAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeTkAssetIntoExistingBuffer(const Nv::Blast::TkAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + return Nv::Blast::BlastSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), buffer, maxSize, usedSize); + } + + ////////////////////////////////////////////////////////////////////////// + // ExtPxAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAsset(const unsigned char* input, uint32_t size) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAssetFromStream(std::istream &inputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoStream(const Nv::Blast::ExtPxAsset *asset, std::ostream &outputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoNewBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeExtPxAssetIntoExistingBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::BlastSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), buffer, maxSize, usedSize); + } + + +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.h new file mode 100644 index 0000000..5d44554 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerialization.h @@ -0,0 +1,172 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once +#include "kj/io.h" +#include "capnp/serialize.h" +#include "generated/NvBlastExtSerializationLL.capn.h" +#include <vector> +#include "NvBlastExtInputStream.h" +#include "NvBlastExtOutputStream.h" + +#if !defined(BLAST_LL_ALLOC) +#include "NvBlastExtAllocator.h" +#endif +#include "NvBlastExtGlobals.h" + +namespace Nv +{ + namespace Blast + { + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + class ExtSerialization + { + public: + static TAsset* deserialize(const unsigned char* input, uint32_t size); + static TAsset* deserializeFromStream(std::istream &inputStream); + + static bool serializeIntoExistingBuffer(const TAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize); + static bool serializeIntoNewBuffer(const TAsset *asset, unsigned char **outBuffer, uint32_t &outSize); + static bool serializeIntoStream(const TAsset *asset, std::ostream &outputStream); + + private: + + static void serializeMessageIntoNewBuffer(capnp::MallocMessageBuilder& message, unsigned char ** outBuffer, uint32_t &outSize); + + // Specialized + static bool serializeIntoBuilder(TSerializationBuilder& assetBuilder, const TAsset* asset); + static bool serializeIntoMessage(capnp::MallocMessageBuilder& message, const TAsset* asset); + static TAsset* deserializeFromStreamReader(capnp::InputStreamMessageReader &message); + }; + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + TAsset* ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::deserialize(const unsigned char* input, uint32_t size) + { + kj::ArrayPtr<const unsigned char> source(input, size); + + kj::ArrayInputStream inputStream(source); + + std::vector<uint64_t> scratch; + scratch.resize(size); + kj::ArrayPtr<capnp::word> scratchArray((capnp::word*) scratch.data(), size); + + capnp::InputStreamMessageReader message(inputStream, capnp::ReaderOptions(), scratchArray); + + return deserializeFromStreamReader(message); + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + TAsset* ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::deserializeFromStream(std::istream &inputStream) + { + Nv::Blast::ExtInputStream readStream(inputStream); + + capnp::InputStreamMessageReader message(readStream); + + return deserializeFromStreamReader(message); + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + bool ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeIntoExistingBuffer(const TAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + capnp::MallocMessageBuilder message; + + bool result = serializeIntoMessage(message, asset); + + if (result == false) + { + usedSize = 0; + return false; + } + + uint32_t messageSize = computeSerializedSizeInWords(message) * sizeof(uint64_t); + + if (maxSize < messageSize) + { + NvBlastLog logFn = gLog; + +#if !defined(BLAST_LL_ALLOC) + logFn = NvBlastTkFrameworkGet()->getLogFn(); +#endif + + NVBLAST_LOG_ERROR(logFn, "When attempting to serialize into an existing buffer, the provided buffer was too small."); + usedSize = 0; + return false; + } + + kj::ArrayPtr<unsigned char> outputBuffer(buffer, maxSize); + kj::ArrayOutputStream outputStream(outputBuffer); + + capnp::writeMessage(outputStream, message); + + usedSize = messageSize;; + return true; + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + bool ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeIntoNewBuffer(const TAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + capnp::MallocMessageBuilder message; + + bool result = serializeIntoMessage(message, asset); + + if (result == false) + { + *outBuffer = nullptr; + outSize = 0; + return false; + } + + serializeMessageIntoNewBuffer(message, outBuffer, outSize); + + return true; + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + bool ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeIntoStream(const TAsset *asset, std::ostream &outputStream) + { + capnp::MallocMessageBuilder message; + + bool result = serializeIntoMessage(message, asset); + + if (result == false) + { + return false; + } + + Nv::Blast::ExtOutputStream blastOutputStream(outputStream); + + writeMessage(blastOutputStream, message); + + return true; + } + + template<typename TAsset, typename TSerializationReader, typename TSerializationBuilder> + void ExtSerialization<TAsset, TSerializationReader, TSerializationBuilder>::serializeMessageIntoNewBuffer(capnp::MallocMessageBuilder& message, unsigned char ** outBuffer, uint32_t &outSize) + { + uint32_t messageSize = computeSerializedSizeInWords(message) * sizeof(uint64_t); + + NvBlastExtAlloc allocFn = gAlloc; + +#if !defined(BLAST_LL_ALLOC) + allocFn = ExtAllocator::alignedAlloc16; +#endif + + unsigned char* buffer = static_cast<unsigned char *>(allocFn(messageSize)); + + kj::ArrayPtr<unsigned char> outputBuffer(buffer, messageSize); + kj::ArrayOutputStream outputStream(outputBuffer); + + capnp::writeMessage(outputStream, message); + + *outBuffer = buffer; + outSize = messageSize; + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationImpl.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationImpl.h new file mode 100644 index 0000000..7f9fbc9 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationImpl.h @@ -0,0 +1,75 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once + +#include "NvBlastExtSerialization.h" +#include "NvBlastTkAsset.h" +#include "NvBlastExtPxAsset.h" +#include "TkAssetDTO.h" +#include "ExtPxAssetDTO.h" + +namespace Nv +{ + namespace Blast + { + /* + Specializations here - one set for each top level asset. (TkAsset, ExtPxAsset) + */ + + + // TkAsset + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoBuilder(Nv::Blast::Serialization::TkAsset::Builder& assetBuilder, const Nv::Blast::TkAsset* asset) + { + return TkAssetDTO::serialize(assetBuilder, asset); + } + + template<> + NV_INLINE Nv::Blast::TkAsset* ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserializeFromStreamReader(capnp::InputStreamMessageReader &message) + { + Nv::Blast::Serialization::TkAsset::Reader reader = message.getRoot<Nv::Blast::Serialization::TkAsset>(); + + return TkAssetDTO::deserialize(reader); + } + + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoMessage(capnp::MallocMessageBuilder& message, const Nv::Blast::TkAsset* asset) + { + Nv::Blast::Serialization::TkAsset::Builder assetBuilder = message.initRoot<Nv::Blast::Serialization::TkAsset>(); + + return serializeIntoBuilder(assetBuilder, asset); + } + + + //ExtPxAsset + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoBuilder(Nv::Blast::Serialization::ExtPxAsset::Builder& assetBuilder, const Nv::Blast::ExtPxAsset* asset) + { + return ExtPxAssetDTO::serialize(assetBuilder, asset); + } + + template<> + NV_INLINE Nv::Blast::ExtPxAsset* ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserializeFromStreamReader(capnp::InputStreamMessageReader &message) + { + Nv::Blast::Serialization::ExtPxAsset::Reader reader = message.getRoot<Nv::Blast::Serialization::ExtPxAsset>(); + + return ExtPxAssetDTO::deserialize(reader); + } + + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoMessage(capnp::MallocMessageBuilder& message, const Nv::Blast::ExtPxAsset* asset) + { + Nv::Blast::Serialization::ExtPxAsset::Builder assetBuilder = message.initRoot<Nv::Blast::Serialization::ExtPxAsset>(); + + return serializeIntoBuilder(assetBuilder, asset); + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationInterface.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationInterface.cpp new file mode 100644 index 0000000..bebee5b --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationInterface.cpp @@ -0,0 +1,133 @@ +/* +* Copyright (c) 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 "NvBlastExtSerializationImpl.h" +#include <memory> +#include "PxPhysicsVersion.h" +#include "PxConvexMeshGeometryDTO.h" +#include "NvBlastExtDefs.h" +#include "PxPhysics.h" +#include "NvBlastAssert.h" + + +// This is terrible. +physx::PxPhysics* g_Physics = nullptr; + +using namespace Nv::Blast; + +std::shared_ptr<physx::PxCooking> getCooking() +{ + physx::PxCookingParams cookingParams(g_Physics->getTolerancesScale()); + cookingParams.buildGPUData = true; + + std::shared_ptr<physx::PxCooking> m_Cooking = std::shared_ptr<physx::PxCooking>(PxCreateCooking(PX_PHYSICS_VERSION, g_Physics->getFoundation(), cookingParams), [=](physx::PxCooking* cooking) + { + cooking->release(); + }); + + NVBLASTEXT_CHECK_ERROR(m_Cooking, "Error: failed to create PhysX Cooking\n", return nullptr); + + return m_Cooking; +} + + +extern "C" +{ + NVBLAST_API void setPhysXSDK(physx::PxPhysics* physXSDK) + { + g_Physics = physXSDK; + } + + ////////////////////////////////////////////////////////////////////////// + // TkAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAsset(const unsigned char* input, uint32_t size) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::TkAsset* deserializeTkAssetFromStream(std::istream &inputStream) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeTkAssetIntoStream(const Nv::Blast::TkAsset *asset, std::ostream &outputStream) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeTkAssetIntoNewBuffer(const Nv::Blast::TkAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeTkAssetIntoExistingBuffer(const Nv::Blast::TkAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + return Nv::Blast::ExtSerialization<Nv::Blast::TkAsset, Nv::Blast::Serialization::TkAsset::Reader, Nv::Blast::Serialization::TkAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::TkAsset *>(asset), buffer, maxSize, usedSize); + } + + ////////////////////////////////////////////////////////////////////////// + // ExtPxAsset + ////////////////////////////////////////////////////////////////////////// + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAsset(const unsigned char* input, uint32_t size) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserialize(input, size); + } + + NVBLAST_API Nv::Blast::ExtPxAsset* deserializeExtPxAssetFromStream(std::istream &inputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoStream(const Nv::Blast::ExtPxAsset *asset, std::ostream &outputStream) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outputStream); + } + + NVBLAST_API bool serializeExtPxAssetIntoNewBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeExtPxAssetIntoExistingBuffer(const Nv::Blast::ExtPxAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { + NVBLAST_ASSERT(g_Physics != nullptr); + + auto cooking = getCooking(); + + PxConvexMeshGeometryDTO::Cooking = cooking.get(); + PxConvexMeshGeometryDTO::Physics = g_Physics; + + return Nv::Blast::ExtSerialization<Nv::Blast::ExtPxAsset, Nv::Blast::Serialization::ExtPxAsset::Reader, Nv::Blast::Serialization::ExtPxAsset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::ExtPxAsset *>(asset), buffer, maxSize, usedSize); + } + + +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLL.capn b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLL.capn new file mode 100644 index 0000000..026056f --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLL.capn @@ -0,0 +1,89 @@ +# Copyright (c) 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. + +@0x9a4a58fac38375e0; + +using Cxx = import "/capnp/c++.capnp"; + +$Cxx.namespace("Nv::Blast::Serialization"); + +struct Asset +{ + header @0 :NvBlastDataBlock; + + iD @1 :UUID; + + chunkCount @2 :UInt32; + + graph @3 :NvBlastSupportGraph; + + leafChunkCount @4 :UInt32; + + firstSubsupportChunkIndex @5 :UInt32; + + bondCount @6 :UInt32; + + chunks @7: List(NvBlastChunk); + + bonds @8: List(NvBlastBond); + + subtreeLeafChunkCounts @9: List(UInt32); + + chunkToGraphNodeMap @10: List(UInt32); +} + +struct NvBlastDataBlock +{ + enum Type + { + assetDataBlock @0; + instanceDataBlock @1; + } + + dataType @0 :Type; + + formatVersion @1 :UInt32; + + size @2 :UInt32; +} + +struct NvBlastChunk +{ + centroid @0 :List(Float32); + + volume @1 :Float32; + + parentChunkIndex @2 :UInt32; + firstChildIndex @3 :UInt32; + childIndexStop @4 :UInt32; + userData @5 :UInt32; +} + +struct NvBlastBond +{ + normal @0 :List(Float32); + area @1 :Float32; + centroid @2 :List(Float32); + userData @3 :UInt32; +} + +struct NvBlastSupportGraph +{ + nodeCount @0 : UInt32; + + chunkIndices @1 : List(UInt32); + adjacencyPartition @2 : List(UInt32); + adjacentNodeIndices @3 : List(UInt32); + adjacentBondIndices @4 : List(UInt32); +} + +struct UUID +{ + value @0 : Data; +} + diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLImpl.h b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLImpl.h new file mode 100644 index 0000000..d7595ba --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLImpl.h @@ -0,0 +1,48 @@ +/* +* Copyright (c) 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. +*/ + +#pragma once + +#include "NvBlastExtSerialization.h" +#include "NvBlastAsset.h" +#include "AssetDTO.h" + +namespace Nv +{ + namespace Blast + { + /* + Specializations here - LL asset only + */ + + // Asset + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoBuilder(Nv::Blast::Serialization::Asset::Builder& assetBuilder, const Nv::Blast::Asset* asset) + { + return AssetDTO::serialize(assetBuilder, asset); + } + + template<> + NV_INLINE Nv::Blast::Asset* ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserializeFromStreamReader(capnp::InputStreamMessageReader &message) + { + Nv::Blast::Serialization::Asset::Reader reader = message.getRoot<Nv::Blast::Serialization::Asset>(); + + return AssetDTO::deserialize(reader); + } + + template<> + NV_INLINE bool ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoMessage(capnp::MallocMessageBuilder& message, const Nv::Blast::Asset* asset) + { + Nv::Blast::Serialization::Asset::Builder assetBuilder = message.initRoot<Nv::Blast::Serialization::Asset>(); + + return serializeIntoBuilder(assetBuilder, asset); + } + } +} diff --git a/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLInterface.cpp b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLInterface.cpp new file mode 100644 index 0000000..26d8667 --- /dev/null +++ b/NvBlast/sdk/extensions/serialization/source/NvBlastExtSerializationLLInterface.cpp @@ -0,0 +1,101 @@ +/* +* Copyright (c) 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 "NvBlastExtSerializationLLInterface.h" +#include "NvBlastExtSerializationLLImpl.h" +#include <memory> +#include "NvBlastExtSerialization.h" +#include "NvBlastAsset.h" +#include <iostream> +#include "NvBlastExtGlobals.h" + + +NvBlastExtAlloc gAlloc = nullptr; +NvBlastLog gLog = nullptr; + +extern "C" +{ + NVBLAST_API void setAllocator(NvBlastExtAlloc alloc) + { + gAlloc = alloc; + } + + NVBLAST_API void setLog(NvBlastLog log) + { + gLog = log; + } + + NVBLAST_API NvBlastAsset* deserializeAsset(const unsigned char* input, uint32_t size) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return nullptr; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserialize(input, size); + } + + NVBLAST_API NvBlastAsset* deserializeAssetFromStream(std::istream &inputStream) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return nullptr; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::deserializeFromStream(inputStream); + } + + NVBLAST_API bool serializeAssetIntoStream(const NvBlastAsset *asset, std::ostream &outputStream) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return false; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoStream(reinterpret_cast<const Nv::Blast::Asset *>(asset), outputStream); + } + + NVBLAST_API bool serializeAssetIntoNewBuffer(const NvBlastAsset *asset, unsigned char **outBuffer, uint32_t &outSize) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return false; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoNewBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), outBuffer, outSize); + } + + NVBLAST_API bool serializeAssetIntoExistingBuffer(const NvBlastAsset *asset, unsigned char *buffer, uint32_t maxSize, uint32_t &usedSize) + { +#if defined(BLAST_LL_ALLOC) + if (gAlloc == nullptr || gLog == nullptr) + { + std::cerr << "Must set allocator and log when using low level serialization library. See setAllocator() and setLog() functions." << std::endl; + return false; + } +#endif + + return Nv::Blast::ExtSerialization<Nv::Blast::Asset, Nv::Blast::Serialization::Asset::Reader, Nv::Blast::Serialization::Asset::Builder>::serializeIntoExistingBuffer(reinterpret_cast<const Nv::Blast::Asset *>(asset), buffer, maxSize, usedSize); + } + +} + |