diff --git a/Content/DynamicLineMaterial.uasset b/Content/DynamicLineMaterial.uasset index 6580c535488fc33a5879a824895a47af80cb5b61..e4529ef79a4492ebb3bb941980aab51f06487134 100644 Binary files a/Content/DynamicLineMaterial.uasset and b/Content/DynamicLineMaterial.uasset differ diff --git a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp index 3be69d7e47b585606e686ba7d5da8d1c301e4532..c48e28501c634fd9e2eacd497b268e2ea3ba17d5 100644 --- a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp +++ b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp @@ -4,7 +4,7 @@ #include "GPUInstancedLineComponent.h" #include "InstancedMeshLineRendering.h" // Only needed for logging -#pragma optimize("", off) +//#pragma optimize("", on) UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer& ObjectInitializer) @@ -15,8 +15,8 @@ UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer& CurrentTextureIndex = 0, NextFreeId = 0; - TextureWidth = 1024; - TextureHeight = 1024; + TextureWidth = 2048; + TextureHeight = 2048; //PrimaryComponentTick.bCanEverTick = true; //bTickInEditor = true; @@ -407,41 +407,44 @@ void UGPUInstancedLineComponent::OnUpdateTransform(EUpdateTransformFlags UpdateT SetWorldTransform(FTransform::Identity); } -TStructOnScope<FActorComponentInstanceData> UGPUInstancedLineComponent::GetComponentInstanceData() const -{ - TStructOnScope<FActorComponentInstanceData> InstanceData; -#if WITH_EDITOR - InstanceData.InitializeAs<FUGPUInstancedLineComponentInstanceData>(this); - FUGPUInstancedLineComponentInstanceData* LineInstanceData = InstanceData.Cast<FUGPUInstancedLineComponentInstanceData>(); - - //LineInstanceData->PositionTexture = PositionTexture; - //LineInstanceData->DynamicMaterial = DynamicLineMaterial; - //LineInstanceData->LineMap = LineMap; - -#endif - return InstanceData; -} #endif -void UGPUInstancedLineComponent::ApplyComponentInstanceData(FUGPUInstancedLineComponentInstanceData* ComponentInstanceData) -{ -#if WITH_EDITOR - //check(ComponentInstanceData); - //if(ComponentInstanceData->PositionTexture != nullptr) - // PositionTexture = ComponentInstanceData->PositionTexture; - //if (ComponentInstanceData->DynamicMaterial != nullptr) - //{ - // DynamicLineMaterial = ComponentInstanceData->DynamicMaterial; - // DynamicLineMaterial->SetTextureParameterValue("PositionTexture", PositionTexture); - // DynamicLineMaterial->SetScalarParameterValue("TextureWidth", TextureWidth); - // SetMaterial(0, DynamicLineMaterial); - //} - //if (ComponentInstanceData->LineMap.Num() > 0) - // LineMap = ComponentInstanceData->LineMap; - -#endif -} +//TStructOnScope<FActorComponentInstanceData> UGPUInstancedLineComponent::GetComponentInstanceData() const +//{ +// TStructOnScope<FActorComponentInstanceData> InstanceData; +//#if WITH_EDITOR +// InstanceData.InitializeAs<FUGPUInstancedLineComponentInstanceData>(this); +// FUGPUInstancedLineComponentInstanceData* LineInstanceData = InstanceData.Cast<FUGPUInstancedLineComponentInstanceData>(); +// +// //LineInstanceData->PositionTexture = PositionTexture; +// //LineInstanceData->DynamicMaterial = DynamicLineMaterial; +// //LineInstanceData->LineMap = LineMap; +// +//#endif +// return InstanceData; +//} +//#endif +// +//void UGPUInstancedLineComponent::ApplyComponentInstanceData(FUGPUInstancedLineComponentInstanceData* ComponentInstanceData) +//{ +//#if WITH_EDITOR +// //check(ComponentInstanceData); +// //if(ComponentInstanceData->PositionTexture != nullptr) +// // PositionTexture = ComponentInstanceData->PositionTexture; +// //if (ComponentInstanceData->DynamicMaterial != nullptr) +// //{ +// // DynamicLineMaterial = ComponentInstanceData->DynamicMaterial; +// // DynamicLineMaterial->SetTextureParameterValue("PositionTexture", PositionTexture); +// // DynamicLineMaterial->SetScalarParameterValue("TextureWidth", TextureWidth); +// // SetMaterial(0, DynamicLineMaterial); +// //} +// //if (ComponentInstanceData->LineMap.Num() > 0) +// // LineMap = ComponentInstanceData->LineMap; +// +// +//#endif +//} // Doesn't do anything yet, just as a testing function to see execution order on Unreal startup. void UGPUInstancedLineComponent::PostLoad() @@ -1162,7 +1165,7 @@ bool UGPUInstancedLineComponent::UpdateLineFromEditorData(const FEditorLineData& bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>& Points) { - TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly")) + TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Move")) const int32 X = LineIdStart % TextureWidth; const int32 Y = LineIdStart / TextureWidth; @@ -1183,6 +1186,52 @@ bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVe return true; } +bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, const TArray<FVector4>& Points) +{ + TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Memcpy")) + + TArray<FVector4> Cpy = Points; + return DrawLinesDirectly(LineIdStart, Cpy); +} + +bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>* Points) +{ + TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Ptr")) + + const int32 X = LineIdStart % TextureWidth; + const int32 Y = LineIdStart / TextureWidth; + + int32 NumberOfRegions = 0; + FUpdateTextureRegion2D* Regions = CalculateTextureRegions(FIntPoint(X, Y), Points->Num(), NumberOfRegions); + PositionTexture->UpdateTextureRegions(0, NumberOfRegions, Regions, TextureWidth * sizeof(FVector4), sizeof(FVector4), (uint8*)Points->GetData(), + [](auto InTextureData, auto InRegions) + { + delete InTextureData; + delete InRegions; + }); + + return true; +} + +bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, uint8* SrcData, int32 Num) +{ + TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Raw Ptr")) + + const int32 X = LineIdStart % TextureWidth; + const int32 Y = LineIdStart / TextureWidth; + + int32 NumberOfRegions = 0; + FUpdateTextureRegion2D* Regions = CalculateTextureRegions(FIntPoint(X, Y), Num, NumberOfRegions); + PositionTexture->UpdateTextureRegions(0, NumberOfRegions, Regions, TextureWidth * sizeof(FVector4), sizeof(FVector4), SrcData, + [](auto InTextureData, auto InRegions) + { + delete InTextureData; + delete InRegions; + }); + + return true; +} + // todo test this not sure if it actually works bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const FVector& Point) { @@ -1499,4 +1548,4 @@ bool UGPUInstancedLineComponent::SetSegmentWidth(int32 LineId, int32 SegmentId, SetCustomDataValue(InstanceId, 3, Width, false); return true; } -#pragma optimize("", on) +//#pragma optimize("", on) diff --git a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h index c026ebfdfb7af0bd04ff7a55362c2a6f40ebc0d4..a7f01c49e81b66cd3b4ceaa0870565d1a4a03b3b 100644 --- a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h +++ b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h @@ -107,10 +107,10 @@ public: virtual void OnUpdateTransform(EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport = ETeleportType::None) override; #endif - virtual TStructOnScope<FActorComponentInstanceData> GetComponentInstanceData() const override; + //virtual TStructOnScope<FActorComponentInstanceData> GetComponentInstanceData() const override; /** Applies the cached component instance data to a newly blueprint constructed component. */ - virtual void ApplyComponentInstanceData(struct FUGPUInstancedLineComponentInstanceData* ComponentInstanceData); + //virtual void ApplyComponentInstanceData(struct FUGPUInstancedLineComponentInstanceData* ComponentInstanceData); virtual void PostLoad() override; virtual void OnComponentCreated() override; @@ -319,6 +319,13 @@ public: * @return bool Returns true on successful update. */ bool DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>& Points); + + + bool DrawLinesDirectly(int32 LineIdStart, const TArray<FVector4>& Points); + + bool DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>* Points); + + bool DrawLinesDirectly(int32 LineIdStart, uint8* SrcData, int32 Num); /** * This function updates the location of a given point in an existing line. @@ -481,45 +488,45 @@ public: /** Helper class used to preserve texture pointer across blueprint reinstancing */ -USTRUCT() -struct FUGPUInstancedLineComponentInstanceData : public FSceneComponentInstanceData -{ - GENERATED_BODY() -public: - FUGPUInstancedLineComponentInstanceData() = default; - FUGPUInstancedLineComponentInstanceData(const UGPUInstancedLineComponent* InComponent) - : FSceneComponentInstanceData(InComponent) - , PositionTexture(InComponent->PositionTexture), LineMap(InComponent->LineMap) - {} - virtual ~FUGPUInstancedLineComponentInstanceData() = default; - - virtual bool ContainsData() const override - { - return true; - } - - virtual void ApplyToComponent(UActorComponent* Component, const ECacheApplyPhase CacheApplyPhase) override - { - Super::ApplyToComponent(Component, CacheApplyPhase); - CastChecked<UGPUInstancedLineComponent>(Component)->ApplyComponentInstanceData(this); - } - - virtual void AddReferencedObjects(FReferenceCollector& Collector) override - { - Super::AddReferencedObjects(Collector); - Collector.AddReferencedObject(PositionTexture); - } - -public: - - - - //UPROPERTY() - UTexture2D* PositionTexture; - - //UPROPERTY() - UMaterialInstanceDynamic* DynamicMaterial; - - //UPROPERTY() - TMap<int32, FGPULineArray> LineMap; -}; \ No newline at end of file +//USTRUCT() +//struct FUGPUInstancedLineComponentInstanceData : public FSceneComponentInstanceData +//{ +// GENERATED_BODY() +//public: +// FUGPUInstancedLineComponentInstanceData() = default; +// FUGPUInstancedLineComponentInstanceData(const UGPUInstancedLineComponent* InComponent) +// : FSceneComponentInstanceData(InComponent) +// , PositionTexture(InComponent->PositionTexture), LineMap(InComponent->LineMap) +// {} +// virtual ~FUGPUInstancedLineComponentInstanceData() = default; +// +// virtual bool ContainsData() const override +// { +// return true; +// } +// +// virtual void ApplyToComponent(UActorComponent* Component, const ECacheApplyPhase CacheApplyPhase) override +// { +// Super::ApplyToComponent(Component, CacheApplyPhase); +// CastChecked<UGPUInstancedLineComponent>(Component)->ApplyComponentInstanceData(this); +// } +// +// virtual void AddReferencedObjects(FReferenceCollector& Collector) override +// { +// Super::AddReferencedObjects(Collector); +// Collector.AddReferencedObject(PositionTexture); +// } +// +//public: +// +// +// +// //UPROPERTY() +// UTexture2D* PositionTexture; +// +// //UPROPERTY() +// UMaterialInstanceDynamic* DynamicMaterial; +// +// //UPROPERTY() +// TMap<int32, FGPULineArray> LineMap; +//}; \ No newline at end of file