|
| 1 | +// Copyright 2025 Timothé Lapetite and contributors |
| 2 | +// Released under the MIT license https://opensource.org/license/MIT/ |
| 3 | + |
| 4 | +#include "AssetStaging/PCGExStaging.h" |
| 5 | + |
| 6 | +namespace PCGExStaging |
| 7 | +{ |
| 8 | + FPickPacker::FPickPacker(FPCGExContext* InContext) |
| 9 | + : Context(InContext) |
| 10 | + { |
| 11 | + BaseHash = static_cast<uint16>(InContext->GetInputSettings<UPCGSettings>()->UID); |
| 12 | + } |
| 13 | + |
| 14 | + uint64 FPickPacker::GetPickIdx(const UPCGExAssetCollection* InCollection, const int16 InIndex, const int16 InSecondaryIndex) |
| 15 | + { |
| 16 | + const uint32 ItemHash = PCGEx::H32(InIndex, InSecondaryIndex + 1); |
| 17 | + |
| 18 | + { |
| 19 | + FReadScopeLock ReadScopeLock(AssetCollectionsLock); |
| 20 | + if (const uint32* ColIdxPtr = CollectionMap.Find(InCollection)) { return PCGEx::H64(*ColIdxPtr, ItemHash); } |
| 21 | + } |
| 22 | + |
| 23 | + { |
| 24 | + FWriteScopeLock WriteScopeLock(AssetCollectionsLock); |
| 25 | + if (const uint32* ColIdxPtr = CollectionMap.Find(InCollection)) { return PCGEx::H64(*ColIdxPtr, ItemHash); } |
| 26 | + |
| 27 | + uint32 ColIndex = PCGEx::H32(BaseHash, AssetCollections.Add(InCollection)); |
| 28 | + CollectionMap.Add(InCollection, ColIndex); |
| 29 | + return PCGEx::H64(ColIndex, ItemHash); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + void FPickPacker::PackToDataset(const UPCGParamData* InAttributeSet) |
| 34 | + { |
| 35 | + FPCGMetadataAttribute<int32>* CollectionIdx = InAttributeSet->Metadata->FindOrCreateAttribute<int32>(Tag_CollectionIdx, 0, false, true, true); |
| 36 | + FPCGMetadataAttribute<FSoftObjectPath>* CollectionPath = InAttributeSet->Metadata->FindOrCreateAttribute<FSoftObjectPath>(Tag_CollectionPath, FSoftObjectPath(), false, true, true); |
| 37 | + |
| 38 | + for (const TPair<const UPCGExAssetCollection*, uint32>& Pair : CollectionMap) |
| 39 | + { |
| 40 | + const int64 Key = InAttributeSet->Metadata->AddEntry(); |
| 41 | + CollectionIdx->SetValue(Key, Pair.Value); |
| 42 | + CollectionPath->SetValue(Key, FSoftObjectPath(Pair.Key)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + bool IPickUnpacker::UnpackDataset(FPCGContext* InContext, const UPCGParamData* InAttributeSet) |
| 47 | + { |
| 48 | + const UPCGMetadata* Metadata = InAttributeSet->Metadata; |
| 49 | + TUniquePtr<FPCGAttributeAccessorKeysEntries> Keys = MakeUnique<FPCGAttributeAccessorKeysEntries>(Metadata); |
| 50 | + |
| 51 | + const int32 NumEntries = Keys->GetNum(); |
| 52 | + if (NumEntries == 0) |
| 53 | + { |
| 54 | + PCGE_LOG_C(Error, GraphAndLog, InContext, FTEXT("Attribute set is empty.")); |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + CollectionMap.Reserve(CollectionMap.Num() + NumEntries); |
| 59 | + |
| 60 | + const FPCGMetadataAttribute<int32>* CollectionIdx = InAttributeSet->Metadata->GetConstTypedAttribute<int32>(Tag_CollectionIdx); |
| 61 | + const FPCGMetadataAttribute<FSoftObjectPath>* CollectionPath = InAttributeSet->Metadata->GetConstTypedAttribute<FSoftObjectPath>(Tag_CollectionPath); |
| 62 | + |
| 63 | + if (!CollectionIdx || !CollectionPath) |
| 64 | + { |
| 65 | + PCGE_LOG_C(Error, GraphAndLog, InContext, FTEXT("Missing required attributes, or unsupported type.")); |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + for (int i = 0; i < NumEntries; i++) |
| 70 | + { |
| 71 | + int32 Idx = CollectionIdx->GetValueFromItemKey(i); |
| 72 | + |
| 73 | + UPCGExAssetCollection* Collection = PCGExHelpers::LoadBlocking_AnyThread<UPCGExAssetCollection>(TSoftObjectPtr<UPCGExAssetCollection>(CollectionPath->GetValueFromItemKey(i))); |
| 74 | + |
| 75 | + if (!Collection) |
| 76 | + { |
| 77 | + PCGE_LOG_C(Error, GraphAndLog, InContext, FTEXT("Some collections could not be loaded.")); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (CollectionMap.Contains(Idx)) |
| 82 | + { |
| 83 | + if (CollectionMap[Idx] == Collection) { continue; } |
| 84 | + |
| 85 | + PCGE_LOG_C(Error, GraphAndLog, InContext, FTEXT("Collection Idx collision.")); |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + CollectionMap.Add(Idx, Collection); |
| 90 | + NumUniqueEntries += Collection->GetValidEntryNum(); |
| 91 | + } |
| 92 | + |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + void IPickUnpacker::UnpackPin(FPCGContext* InContext, const FName InPinLabel) |
| 97 | + { |
| 98 | + for (TArray<FPCGTaggedData> Params = InContext->InputData.GetParamsByPin(InPinLabel); |
| 99 | + const FPCGTaggedData& InTaggedData : Params) |
| 100 | + { |
| 101 | + const UPCGParamData* ParamData = Cast<UPCGParamData>(InTaggedData.Data); |
| 102 | + |
| 103 | + if (!ParamData) { continue; } |
| 104 | + const TSharedPtr<PCGEx::FAttributesInfos> Infos = PCGEx::FAttributesInfos::Get(ParamData->Metadata); |
| 105 | + |
| 106 | + if (!ParamData->Metadata->HasAttribute(Tag_CollectionIdx) || !ParamData->Metadata->HasAttribute(Tag_CollectionPath)) { continue; } |
| 107 | + |
| 108 | + UnpackDataset(InContext, ParamData); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + bool IPickUnpacker::BuildPartitions(const UPCGBasePointData* InPointData, TArray<FPCGMeshInstanceList>& InstanceLists) |
| 113 | + { |
| 114 | + TRACE_CPUPROFILER_EVENT_SCOPE(TPickUnpacker::BuildPartitions_Indexed); |
| 115 | + |
| 116 | + FPCGAttributePropertyInputSelector HashSelector; |
| 117 | + HashSelector.Update(Tag_EntryIdx.ToString()); |
| 118 | + |
| 119 | + TUniquePtr<const IPCGAttributeAccessor> HashAttributeAccessor = PCGAttributeAccessorHelpers::CreateConstAccessor(InPointData, HashSelector); |
| 120 | + TUniquePtr<const IPCGAttributeAccessorKeys> HashKeys = PCGAttributeAccessorHelpers::CreateConstKeys(InPointData, HashSelector); |
| 121 | + |
| 122 | + if (!HashAttributeAccessor || !HashKeys) { return false; } |
| 123 | + |
| 124 | + TArray<int64> Hashes; |
| 125 | + Hashes.SetNumUninitialized(HashKeys->GetNum()); |
| 126 | + |
| 127 | + if (!HashAttributeAccessor->GetRange<int64>(Hashes, 0, *HashKeys, EPCGAttributeAccessorFlags::AllowBroadcastAndConstructible)) |
| 128 | + { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + const int32 NumPoints = InPointData->GetNumPoints(); |
| 133 | + const int32 SafeReserve = NumPoints / (NumUniqueEntries * 2); |
| 134 | + |
| 135 | + // Build partitions |
| 136 | + for (int i = 0; i < NumPoints; i++) |
| 137 | + { |
| 138 | + const uint64 EntryHash = Hashes[i]; |
| 139 | + if (const int32* Index = IndexedPartitions.Find(EntryHash); !Index) |
| 140 | + { |
| 141 | + FPCGMeshInstanceList& NewInstanceList = InstanceLists.Emplace_GetRef(); |
| 142 | + NewInstanceList.AttributePartitionIndex = EntryHash; |
| 143 | + NewInstanceList.PointData = InPointData; |
| 144 | + NewInstanceList.InstancesIndices.Reserve(SafeReserve); |
| 145 | + NewInstanceList.InstancesIndices.Emplace(i); |
| 146 | + |
| 147 | + IndexedPartitions.Add(EntryHash, InstanceLists.Num() - 1); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + InstanceLists[*Index].InstancesIndices.Emplace(i); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return !IndexedPartitions.IsEmpty(); |
| 156 | + } |
| 157 | + |
| 158 | + void IPickUnpacker::RetrievePartitions(const UPCGBasePointData* InPointData, TArray<FPCGMeshInstanceList>& InstanceLists) |
| 159 | + { |
| 160 | + TRACE_CPUPROFILER_EVENT_SCOPE(TPickUnpacker::BuildPartitions_Indexed); |
| 161 | + |
| 162 | + PointData = InPointData; |
| 163 | + |
| 164 | + for (FPCGMeshInstanceList& InstanceList : InstanceLists) |
| 165 | + { |
| 166 | + IndexedPartitions.Add(InstanceList.AttributePartitionIndex, InstanceLists.Num() - 1); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + void IPickUnpacker::InsertEntry(const uint64 EntryHash, const int32 EntryIndex, TArray<FPCGMeshInstanceList>& InstanceLists) |
| 171 | + { |
| 172 | + if (const int32* Index = IndexedPartitions.Find(EntryHash); !Index) |
| 173 | + { |
| 174 | + FPCGMeshInstanceList& NewInstanceList = InstanceLists.Emplace_GetRef(); |
| 175 | + NewInstanceList.AttributePartitionIndex = EntryHash; |
| 176 | + NewInstanceList.PointData = PointData; |
| 177 | + NewInstanceList.InstancesIndices.Reserve(PointData->GetNumPoints() / (NumUniqueEntries * 2)); |
| 178 | + NewInstanceList.InstancesIndices.Emplace(EntryIndex); |
| 179 | + |
| 180 | + IndexedPartitions.Add(EntryHash, InstanceLists.Num() - 1); |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + InstanceLists[*Index].InstancesIndices.Emplace(EntryIndex); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + UPCGExAssetCollection* IPickUnpacker::UnpackHash(const uint64 EntryHash, int16& OutPrimaryIndex, int16& OutSecondaryIndex) |
| 189 | + { |
| 190 | + uint32 CollectionIdx = 0; |
| 191 | + uint32 OutEntryIndices = 0; |
| 192 | + |
| 193 | + PCGEx::H64(EntryHash, CollectionIdx, OutEntryIndices); |
| 194 | + |
| 195 | + uint16 EntryIndex = 0; |
| 196 | + uint16 SecondaryIndex = 0; |
| 197 | + |
| 198 | + PCGEx::H32(OutEntryIndices, EntryIndex, SecondaryIndex); |
| 199 | + OutSecondaryIndex = SecondaryIndex - 1; // minus one because we do +1 during packing |
| 200 | + |
| 201 | + UPCGExAssetCollection** Collection = CollectionMap.Find(CollectionIdx); |
| 202 | + if (!Collection || !(*Collection)->IsValidIndex(EntryIndex)) { return nullptr; } |
| 203 | + |
| 204 | + OutPrimaryIndex = EntryIndex; |
| 205 | + |
| 206 | + return *Collection; |
| 207 | + } |
| 208 | +} |
0 commit comments