Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebukam committed Oct 2, 2024
1 parent 3edd25c commit 68354aa
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 37 deletions.
12 changes: 11 additions & 1 deletion Source/PCGExtendedToolkit/Private/Data/PCGExPointData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,23 @@ void UPCGExPointData::InitializeFromPCGExData(const UPCGExPointData* InPCGExPoin

void UPCGExPointData::BeginDestroy()
{
ClearInternalFlags(EInternalObjectFlags::Async);
if (Metadata) { Metadata->ClearInternalFlags(EInternalObjectFlags::Async); }
Super::BeginDestroy();
//UE_LOG(LogTemp, Warning, TEXT("RELEASE UPCGExPointData"))
}

#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
UPCGSpatialData* UPCGExPointData::CopyInternal() const
{
PCGEX_NEW_OBJECT(UPCGExPointData, NewPointData)
NewPointData->CopyFrom(this);
return NewPointData;
}
#else
UPCGSpatialData* UPCGExPointData::CopyInternal(FPCGContext* Context) const
{
UPCGExPointData* NewPointData = FPCGContext::NewObject_AnyThread<UPCGExPointData>(Context);
NewPointData->CopyFrom(this);
return NewPointData;
}
#endif
16 changes: 11 additions & 5 deletions Source/PCGExtendedToolkit/Private/Data/PCGExPointIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace PCGExData

void FPointIO::InitializeOutput(FPCGExContext* InContext, const EInit InitOut)
{
if (Out != In) { PCGEX_DELETE_UOBJECT(Out) }
if (Out != In) { PCGEX_DELETE_UPCGDATA(Out) }
OutKeys.Reset();

if (InitOut == EInit::NoOutput)
Expand Down Expand Up @@ -76,11 +76,17 @@ namespace PCGExData
if (InitOut == EInit::DuplicateInput)
{
check(In)
FGCScopeGuard GCGuarded;
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
Out = Cast<UPCGPointData>(In->DuplicateData(true));
{
FGCScopeGuard GCGuarded;
Out = Cast<UPCGPointData>(In->DuplicateData(true));
}
#else
Out = Cast<UPCGPointData>(In->DuplicateData(Context, true));
{
PCGEX_ENFORCE_CONTEXT_ASYNC(Context)
FWriteScopeLock WriteScopeLock(Context->AsyncObjectLock); // Ugh
Out = Cast<UPCGPointData>(In->DuplicateData(Context, true));
}
#endif
Out->AddToRoot();
}
Expand Down Expand Up @@ -151,7 +157,7 @@ namespace PCGExData
if (!bWritten)
{
// Delete unused outputs
if (Out && Out != In) { PCGEX_DELETE_UOBJECT(Out) }
if (Out && Out != In) { PCGEX_DELETE_UPCGDATA(Out) }
}
}

Expand Down
24 changes: 24 additions & 0 deletions Source/PCGExtendedToolkit/Private/Graph/Data/PCGExClusterData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void UPCGExClusterNodesData::BeginDestroy()
BoundClusters.Empty();
}

#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
UPCGSpatialData* UPCGExClusterNodesData::CopyInternal() const
{
UPCGExClusterNodesData* NewNodeData = nullptr;
Expand All @@ -39,6 +40,17 @@ UPCGSpatialData* UPCGExClusterNodesData::CopyInternal() const
NewNodeData->CopyFrom(this);
return NewNodeData;
}
#else
UPCGSpatialData* UPCGExClusterNodesData::CopyInternal(FPCGContext* Context) const
{
PCGEX_ENFORCE_CONTEXT_ASYNC(Context)

UPCGExClusterNodesData* NewNodeData = FPCGContext::NewObject_AnyThread<UPCGExClusterNodesData>(Context);
NewNodeData->CopyFrom(this);
return NewNodeData;
}
#endif


void UPCGExClusterEdgesData::InitializeFromPCGExData(const UPCGExPointData* InPCGExPointData, const PCGExData::EInit InitMode)
{
Expand Down Expand Up @@ -66,6 +78,7 @@ const TSharedPtr<PCGExCluster::FCluster>& UPCGExClusterEdgesData::GetBoundCluste
return Cluster;
}

#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
UPCGSpatialData* UPCGExClusterEdgesData::CopyInternal() const
{
UPCGExClusterEdgesData* NewEdgeData = nullptr;
Expand All @@ -76,6 +89,17 @@ UPCGSpatialData* UPCGExClusterEdgesData::CopyInternal() const
NewEdgeData->CopyFrom(this);
return NewEdgeData;
}
#else
UPCGSpatialData* UPCGExClusterEdgesData::CopyInternal(FPCGContext* Context) const
{
PCGEX_ENFORCE_CONTEXT_ASYNC(Context)

UPCGExClusterEdgesData* NewEdgeData = FPCGContext::NewObject_AnyThread<UPCGExClusterEdgesData>(Context);
NewEdgeData->CopyFrom(this);
return NewEdgeData;
}
#endif


void UPCGExClusterEdgesData::BeginDestroy()
{
Expand Down
6 changes: 2 additions & 4 deletions Source/PCGExtendedToolkit/Private/PCGExContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,14 @@ void FPCGExContext::FutureOutput(const FName Pin, UPCGData* InData)
}
}

void FPCGExContext::StartAsyncWork(const bool bPauseOnly)
void FPCGExContext::StartAsyncWork()
{
bIsPaused = true;
if (!bPauseOnly) { AsyncState.bIsRunningOnMainThread = false; }
}

void FPCGExContext::StopAsyncWork()
{
bIsPaused = false;
AsyncState.bIsRunningOnMainThread = true;
}

void FPCGExContext::WriteFutureOutputs()
Expand Down Expand Up @@ -163,7 +161,7 @@ void FPCGExContext::LoadAssets()

if (!bForceSynchronousAssetLoad)
{
StartAsyncWork(true);
StartAsyncWork();

LoadHandle = UAssetManager::GetStreamableManager().RequestAsyncLoad(
RequiredAssets.Array(), [&]()
Expand Down
4 changes: 2 additions & 2 deletions Source/PCGExtendedToolkit/Public/Data/PCGExData.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ namespace PCGExData

virtual void Write() override
{
if (!IsWritable()) { return; }
if (!IsWritable() || !OutAccessor || !OutValues) { return; }

TArrayView<const T> View = MakeArrayView(OutValues->GetData(), OutValues->Num());
OutAccessor->SetRange(View, 0, *Source->GetOutKeys(true));
Expand Down Expand Up @@ -374,7 +374,7 @@ namespace PCGExData
}

FORCEINLINE bool IsDataValid(const ESource InSource) const { return Source->IsDataValid(InSource); }

bool ShareSource(const FFacade* OtherManager) const { return this == OtherManager || OtherManager->Source == Source; }

template <typename T>
Expand Down
5 changes: 5 additions & 0 deletions Source/PCGExtendedToolkit/Public/Data/PCGExPointData.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@ class /*PCGEXTENDEDTOOLKIT_API*/ UPCGExPointData : public UPCGPointData
virtual void BeginDestroy() override;

protected:
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
virtual UPCGSpatialData* CopyInternal() const override;
#else
virtual UPCGSpatialData* CopyInternal(FPCGContext* Context) const override;
#endif

};
19 changes: 17 additions & 2 deletions Source/PCGExtendedToolkit/Public/Data/PCGExPointIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@

#include "Metadata/Accessors/PCGAttributeAccessorKeys.h"

#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 5
#define PCGEX_DELETE_UPCGDATA(_VALUE) \
if(_VALUE){ \
PCGEX_UNROOT(_VALUE) \
if(_VALUE->Metadata){ PCGEX_UNROOT(_VALUE->Metadata) } \
_VALUE->MarkAsGarbage(); \
_VALUE = nullptr; }
#else
#define PCGEX_DELETE_UPCGDATA(_VALUE) _VALUE = nullptr; // Handled by 5.5 AsyncObjects
#endif

namespace PCGExData
{
enum class EInit : uint8
Expand Down Expand Up @@ -123,7 +134,7 @@ namespace PCGExData
template <typename T>
void InitializeOutput(FPCGExContext* InContext, const EInit InitOut = EInit::NoOutput)
{
if (Out != In) { PCGEX_DELETE_UOBJECT(Out) }
if (Out != In) { PCGEX_DELETE_UPCGDATA(Out) }

if (InitOut == EInit::NewOutput)
{
Expand Down Expand Up @@ -160,7 +171,11 @@ namespace PCGExData
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
Out = Cast<UPCGPointData>(In->DuplicateData(true));
#else
Out = Cast<UPCGPointData>(In->DuplicateData(Context, true));
{
PCGEX_ENFORCE_CONTEXT_ASYNC(Context)
FWriteScopeLock WriteScopeLock(Context->AsyncObjectLock); // Ugh
Out = Cast<UPCGPointData>(In->DuplicateData(Context, true));
}
#endif
Out->AddToRoot();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ class /*PCGEXTENDEDTOOLKIT_API*/ UPCGExClusterNodesData : public UPCGExClusterDa
virtual void BeginDestroy() override;

protected:
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
virtual UPCGSpatialData* CopyInternal() const override;
#else
virtual UPCGSpatialData* CopyInternal(FPCGContext* Context) const override;
#endif
};

/**
Expand All @@ -71,7 +75,11 @@ class /*PCGEXTENDEDTOOLKIT_API*/ UPCGExClusterEdgesData : public UPCGExClusterDa

protected:
TSharedPtr<PCGExCluster::FCluster> Cluster;
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
virtual UPCGSpatialData* CopyInternal() const override;
#else
virtual UPCGSpatialData* CopyInternal(FPCGContext* Context) const override;
#endif
};

namespace PCGExClusterData
Expand Down
45 changes: 22 additions & 23 deletions Source/PCGExtendedToolkit/Public/PCGExContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "CoreMinimal.h"
#include "PCGContext.h"
#include "PCGExHelpers.h"
#include "Engine/StreamableManager.h"

struct /*PCGEXTENDEDTOOLKIT_API*/ FPCGExContext : public FPCGContext
Expand All @@ -24,6 +25,8 @@ struct /*PCGEXTENDEDTOOLKIT_API*/ FPCGExContext : public FPCGContext
void WriteFutureOutputs();

public:
mutable FRWLock AsyncObjectLock; //ugh

virtual ~FPCGExContext() override;

void UnrootFutures();
Expand All @@ -33,51 +36,47 @@ struct /*PCGEXTENDEDTOOLKIT_API*/ FPCGExContext : public FPCGContext
void FutureOutput(const FName Pin, UPCGData* InData, const TSet<FString>& InTags);
void FutureOutput(const FName Pin, UPCGData* InData);

void StartAsyncWork(const bool bPauseOnly = false);
void StartAsyncWork();
void StopAsyncWork();

template <class T, typename... Args>
T* PCGExNewObject(Args&&... InArgs)
{
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION < 5
PCGEX_ENFORCE_CONTEXT_ASYNC(this)

T* Object = nullptr;
if (!IsInGameThread())
{
{
FGCScopeGuard Scope;
Object = ::NewObject<T>(std::forward<Args>(InArgs)...);
}
check(Object);
}

Object = ::NewObject<T>(std::forward<Args>(InArgs)...);
Object->AddToRoot();
return Object;

#else
#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 5
if constexpr (!std::is_base_of_v<T, UPCGData>)
{
// Since we create ops & factories through this flow, make sure we only
// use the 5.5 code for UPCGData stuff
T* Object = nullptr;
if (!IsInGameThread())
// Since we create ops & factories through this flow, make sure we only use the 5.5 code for UPCGData stuff
#endif

if (AsyncState.bIsRunningOnMainThread)
{
{
FGCScopeGuard Scope;
Object = ::NewObject<T>(std::forward<Args>(InArgs)...);
}
check(Object);
}
else
{
Object = ::NewObject<T>(std::forward<Args>(InArgs)...);
}

Object = ::NewObject<T>(std::forward<Args>(InArgs)...);
Object->AddToRoot();
return Object;
}else

#if ENGINE_MAJOR_VERSION == 5 && ENGINE_MINOR_VERSION >= 5
}
else
{
return FPCGContext::NewObject_AnyThread<T>(this, std::forward<Args>(InArgs)...);
FWriteScopeLock WriteScopeLock(AsyncObjectLock);
Object = FPCGContext::NewObject_AnyThread<T>(this, std::forward<Args>(InArgs)...);
}
#endif

return Object;
}

virtual void OnComplete();
Expand Down
5 changes: 5 additions & 0 deletions Source/PCGExtendedToolkit/Public/PCGExHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include "PCGExHelpers.generated.h"

#define PCGEX_ENFORCE_CONTEXT_ASYNC(_CONTEXT)\
bool bRestoreTo = _CONTEXT->AsyncState.bIsRunningOnMainThread;\
ON_SCOPE_EXIT { _CONTEXT->AsyncState.bIsRunningOnMainThread = bRestoreTo; };\
_CONTEXT->AsyncState.bIsRunningOnMainThread = IsInGameThread(); // dirty trick

namespace PCGExHelpers
{
static TArray<FString> GetStringArrayFromCommaSeparatedList(const FString& InCommaSeparatedString)
Expand Down

0 comments on commit 68354aa

Please sign in to comment.