From 22ad97591e235280a39d6ce5085b3fe8bb99d15d Mon Sep 17 00:00:00 2001
From: David Gilbert <david.gilbert@rwth-aachen.de>
Date: Fri, 9 Oct 2020 08:57:53 +0200
Subject: [PATCH] Removed more redundant code and fixed some unused variables,
 casts and const specifiers

---
 .../Private/GPUInstancedLineComponent.cpp     | 94 +++++--------------
 .../Public/GPUInstancedLineComponent.h        | 26 ++---
 2 files changed, 33 insertions(+), 87 deletions(-)

diff --git a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp
index 657ebf0..021c885 100644
--- a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp
+++ b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp
@@ -61,11 +61,6 @@ UGPUInstancedLineComponent::~UGPUInstancedLineComponent()
 // Not implemented yet because the data structure isn't clear yet.
 void UGPUInstancedLineComponent::ReleaseData()
 {
-	//LineMap.Empty();
-	//LinearLineData.Empty();
-	//NextFreeId = 0;
-	//CurrentTextureMarker.X = 0;
-	//CurrentTextureMarker.Y = 0;
 }
 
 void UGPUInstancedLineComponent::UpdateWholeTexture() const
@@ -92,10 +87,18 @@ void UGPUInstancedLineComponent::UpdateWholeTexture() const
 	});
 }
 
+FLinearColor UGPUInstancedLineComponent::GetLineColor(int32 LineId)
+{
+	const float R = PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats];
+	const float G = PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats + 1];
+	const float B = PerInstanceSMCustomData[LineMap[LineId].IndexArray[0].InstanceIndex * NumCustomDataFloats + 2];
+	return { R, G, B };
+}
+
 int32 UGPUInstancedLineComponent::AddNewSegmentInstance(const FLinearColor& Color, float Width, int32 Index)
 {
 	// Add with a dummy transform
-	int32 InstanceId = AddInstance(FTransform::Identity);
+	const int32 InstanceId = AddInstance(FTransform::Identity);
 	SetCustomDataValue(InstanceId, 0, Color.R, false);
 	SetCustomDataValue(InstanceId, 1, Color.G, false);
 	SetCustomDataValue(InstanceId, 2, Color.B, false);
@@ -138,11 +141,6 @@ void UGPUInstancedLineComponent::Init()
 		//UE_LOG(LogLineRendering, Display, TEXT("UGPUInstancedLineComponent::Init MID was nullptr!"));
 		DynamicLineMaterial = UMaterialInstanceDynamic::Create(LineMaterialInterface, GetTransientPackage());
 		SetMaterial(0, DynamicLineMaterial);
-		//check(DynamicLineMaterial);
-
-		//DynamicLineMaterial = CreateAndSetMaterialInstanceDynamic(0);
-		//DynamicLineMaterial->SetTextureParameterValue("PositionTexture", PositionTexture);
-		//DynamicLineMaterial->SetScalarParameterValue("TextureWidth", TextureWidth);
 	}
 
 	DynamicLineMaterial->SetTextureParameterValue("PositionTexture", PositionTexture);
@@ -173,6 +171,7 @@ FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(cons
 {	
 	// Calculate the first consecutive region:
 	int32 RemainingPoints = NumberOfPoints;
+	const uint32 TextureWidthU = static_cast<uint32>(TextureWidth);
 	
 	TArray<FUpdateTextureRegion2D>* Regions = new TArray<FUpdateTextureRegion2D>();
 	Regions->AddZeroed(1);
@@ -184,7 +183,7 @@ FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(cons
 	(*Regions)[0].Width = FMath::Min(NumberOfPoints, TextureWidth - static_cast<int32>((*Regions)[0].DestX));
 	(*Regions)[0].Height = StartIndex.X == 0 ? (NumberOfPoints / TextureWidth) : 1;
 
-	checkf((*Regions)[0].DestX + (*Regions)[0].Width <= (uint32)TextureWidth, TEXT("Region[0] out of bounds on X. Texture: %i, %i, %i"), (*Regions)[0].DestX, (*Regions)[0].Width, PositionTexture->GetSizeX());
+	checkf((*Regions)[0].DestX + (*Regions)[0].Width <= TextureWidthU, TEXT("Region[0] out of bounds on X. Texture: %i, %i, %i"), (*Regions)[0].DestX, (*Regions)[0].Width, PositionTexture->GetSizeX());
 	
 	RemainingPoints -= (*Regions)[0].Height == 1 ? (*Regions)[0].Width : (*Regions)[0].Width * (*Regions)[0].Height;
 	if(RemainingPoints == 0)
@@ -203,7 +202,7 @@ FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(cons
 	(*Regions)[1].Height = RemainingPoints == 0 ? 1 : (RemainingPoints / TextureWidth);
 	RemainingPoints -= (*Regions)[1].Width * ((*Regions)[1].Height - 1);
 
-	checkf((*Regions)[1].DestX + (*Regions)[1].Width <= (uint32)TextureWidth, TEXT("Region[1] out of bounds on X. Texture: %i, %i, %i"), (*Regions)[1].DestX, (*Regions)[1].Width, PositionTexture->GetSizeX());
+	checkf((*Regions)[1].DestX + (*Regions)[1].Width <= TextureWidthU, TEXT("Region[1] out of bounds on X. Texture: %i, %i, %i"), (*Regions)[1].DestX, (*Regions)[1].Width, PositionTexture->GetSizeX());
 
 	if (RemainingPoints == 0)
 	{
@@ -224,7 +223,7 @@ FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(cons
 	(*Regions)[2].Height = RemainingPoints == 0 ? 1 : (RemainingPoints / TextureWidth);
 	RemainingPoints -= (*Regions)[2].Width * ((*Regions)[1].Height - 1);
 
-	checkf((*Regions)[2].DestX + (*Regions)[2].Width <= (uint32)TextureWidth, TEXT("Region[2] out of bounds on X. Texture: %i, %i, %i"), (*Regions)[2].DestX, (*Regions)[2].Width, PositionTexture->GetSizeX());
+	checkf((*Regions)[2].DestX + (*Regions)[2].Width <= TextureWidthU, TEXT("Region[2] out of bounds on X. Texture: %i, %i, %i"), (*Regions)[2].DestX, (*Regions)[2].Width, PositionTexture->GetSizeX());
 
 	
 	if (RemainingPoints > 0)
@@ -245,20 +244,13 @@ void UGPUInstancedLineComponent::BeginPlay()
 	}
 }
 
-// Doesn't do anything yet, just as a testing function to see execution order on Unreal startup.
-void UGPUInstancedLineComponent::PostInitProperties()
-{
-	Super::PostInitProperties();
-	//if (FApp::CanEverRender() && !HasAnyFlags(RF_ClassDefaultObject | RF_ArchetypeObject))
-		//UE_LOG(LogLineRendering, Display, TEXT("UGPUInstancedLineComponent::PostInitProperties IF"));
-}
 
 #if WITH_EDITOR
 void UGPUInstancedLineComponent::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent)
 {
 	Init();
 	
-	if (PropertyChangedEvent.Property != NULL)
+	if (PropertyChangedEvent.Property != nullptr)
 	{
 		if (PropertyChangedEvent.Property->GetFName() == GET_MEMBER_NAME_CHECKED(UGPUInstancedLineComponent, EditorLines))
 		{
@@ -422,7 +414,7 @@ void UGPUInstancedLineComponent::PostEditChangeProperty(FPropertyChangedEvent& P
 	Init();
 	const FName PropertyName = PropertyChangedEvent.GetPropertyName();
 
-	if (PropertyChangedEvent.Property != NULL)
+	if (PropertyChangedEvent.Property != nullptr)
 	{
 		if (PropertyName == GET_MEMBER_NAME_CHECKED(FEditorPoint, Point))
 		{
@@ -466,29 +458,12 @@ void UGPUInstancedLineComponent::TickComponent(float DeltaTime, ELevelTick TickT
 
 #endif
 
-
-// Doesn't do anything yet, just as a testing function to see execution order on Unreal startup.
-void UGPUInstancedLineComponent::PostLoad()
-{
-	Super::PostLoad();
-}
-
-// Doesn't do anything yet, just as a testing function to see execution order on Unreal startup.
-void UGPUInstancedLineComponent::OnComponentCreated()
-{
-	Super::OnComponentCreated();
-}
-
 void UGPUInstancedLineComponent::BeginDestroy()
 {
 	Super::BeginDestroy();
 	ReleaseData();
 }
 
-void UGPUInstancedLineComponent::Serialize(FArchive& Ar)
-{
-	Super::Serialize(Ar);
-}
 
 void UGPUInstancedLineComponent::ReserveMemory(int32 NumberOfSegments, int32 NumberOfLines)
 {
@@ -582,7 +557,7 @@ void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int3
 	{
 		const FLinearColor Color = UniformColor ? Colors[0] : Colors[LineIndex];
 		const float Width = UniformWidth ? Widths[0] : Widths[LineIndex];
-		int32 Idx = LineMap.Add(FGPULineArray());
+		const int32 Idx = LineMap.Add(FGPULineArray());
 		FGPULineArray& NewLineArray = LineMap[Idx];
 		NewLineArray.IndexArray.Reserve(NumberOfSegmentsPerLine + 1);
 		
@@ -721,17 +696,13 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point)
 	// Get color and width of the line
 	FGPULineArray& Line = LineMap[LineId];
 
-	
-	const float R = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats];
-	const float G = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 1];
-	const float B = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 2];
 	const float Width = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 3];
 
 	const FIntPoint InitialTextureMarker = CurrentTextureMarker;
 	// Check if it's the last line:
 
 	FGPULineIndices& Indices = Line.IndexArray.Last();
-	const int32 InstanceId = AddNewSegmentInstance({R, G, B}, Width, Indices.TextureIndex);
+	const int32 InstanceId = AddNewSegmentInstance(GetLineColor(LineId), Width, Indices.TextureIndex);
 	Line.IndexArray.Last().InstanceIndex = InstanceId;
 
 	
@@ -891,12 +862,7 @@ bool UGPUInstancedLineComponent::InsertPoint(int32 LineId, int32 SegmentId, cons
 
 bool UGPUInstancedLineComponent::InsertPointWithSameColor(int32 LineId, int32 SegmentId, const FVector& Point)
 {
-	FGPULineArray& Line = LineMap[LineId];	
-	const float R = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats];
-	const float G = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 1];
-	const float B = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 2];
-
-	return InsertPoint(LineId, SegmentId, Point, { R, G, B });
+	return InsertPoint(LineId, SegmentId, Point, GetLineColor(LineId));
 }
 
 bool UGPUInstancedLineComponent::InsertPointWithColor(int32 LineId, int32 SegmentId, const FVector& Point,
@@ -913,9 +879,9 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>&
 
 	// Get color and width of the line
 	FGPULineArray& Line = LineMap[LineId];
-	const float R = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats];
-	const float G = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 1];
-	const float B = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 2];
+
+	const FLinearColor Color = GetLineColor(LineId);
+	
 	const float Width = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 3];
 
 	const FIntPoint InitialTextureMarker = CurrentTextureMarker;
@@ -930,7 +896,7 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>&
 	{
 		for (int32 PointIndex = 0; PointIndex < Points.Num(); ++PointIndex)
 		{
-			const int32 InstanceId = AddNewSegmentInstance({ R, G, B }, Width, PointTextureIndex + PointIndex);	
+			const int32 InstanceId = AddNewSegmentInstance(Color, Width, PointTextureIndex + PointIndex);
 
 			// Update the latest dummy entry to point to the actual segment.
 			if (PointIndex == 0)
@@ -981,7 +947,7 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>&
 
 	for (int32 PointIndex = 0; PointIndex < Points.Num(); ++PointIndex)
 	{
-		const int32 InstanceId = AddNewSegmentInstance({ R, G, B }, Width, NewPointTextureIndex - 1 + PointIndex);
+		const int32 InstanceId = AddNewSegmentInstance(Color, Width, NewPointTextureIndex - 1 + PointIndex);
 		
 		// Update the latest dummy entry to point to the actual segment.
 		if (PointIndex == 0)
@@ -1024,9 +990,7 @@ bool UGPUInstancedLineComponent::InsertPoints(int32 LineId, int32 SegmentId, con
 	{
 		return AddPoints(LineId, Points);
 	}
-	const float R = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats];
-	const float G = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 1];
-	const float B = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 2];
+
 	const float Width = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 3];
 
 	const int32 LinearDataIndex = Line.IndexArray[SegmentId].TextureIndex;
@@ -1041,7 +1005,7 @@ bool UGPUInstancedLineComponent::InsertPoints(int32 LineId, int32 SegmentId, con
 	
 	for (int32 PointIndex = 0; PointIndex < Points.Num(); ++PointIndex)
 	{
-		const int32 InstanceId = AddNewSegmentInstance({ R, G, B }, Width, LinearDataIndex + PointIndex);
+		const int32 InstanceId = AddNewSegmentInstance(GetLineColor(LineId), Width, LinearDataIndex + PointIndex);
 		
 		Line.IndexArray.Insert({ InstanceId, LinearDataIndex + PointIndex }, SegmentId + PointIndex);
 		CurrentTextureIndex++;
@@ -1195,10 +1159,7 @@ bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const
 	LinearLineData[PointIndices.TextureIndex].Y = Point.Y;
 	LinearLineData[PointIndices.TextureIndex].Z = Point.Z;
 
-	const int32 InstanceId = (PointIndices.InstanceIndex != -1) ? PointIndices.InstanceIndex : LineMap[LineId].IndexArray[PointId - 1].InstanceIndex;
-
 	const int32 TextureIndex = PointIndices.TextureIndex;
-	//const int32 Y = PerInstanceSMCustomData[InstanceId * NumCustomDataFloats + 5];
 	const int32 X = TextureIndex % TextureWidth;
 	const int32 Y = TextureIndex / TextureWidth;
 
@@ -1211,8 +1172,6 @@ bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const
 			0,
 			0, 1, 1);
 
-
-
 	// Copy for now - no need to do that 
 	TArray<FVector4>* TextureData = new TArray<FVector4>{ LinearLineData[PointIndices.TextureIndex] };
 
@@ -1244,9 +1203,6 @@ bool UGPUInstancedLineComponent::UpdatePoints(int32 LineId, int32 StartingPointI
 		return UpdateLine(LineId, Points);
 	}
 
-	// Should check for validity
-	const FGPULineIndices& FirstPointIndices = LineMap[LineId].IndexArray[StartingPointId];
-
 	TArray<FVector4>* TextureData = new TArray<FVector4>(MoveTemp(Points));
 	if(StartingPointId == 0)
 	{
diff --git a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h
index 6dd243b..e7a3b86 100644
--- a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h
+++ b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h
@@ -99,7 +99,6 @@ public:
 
 	virtual void BeginPlay() override;
 
-	virtual void PostInitProperties() override;
 #if WITH_EDITOR
 	virtual void PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent) override;
 	virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override; // need this for the widget as for some godforsaken reason the Chain event doesn't fire...
@@ -107,16 +106,7 @@ public:
 	virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
 #endif
 
-	//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 PostLoad() override;
-	virtual void OnComponentCreated() override;
 	virtual void BeginDestroy() override;
-
-	virtual void Serialize(FArchive& Ar) override;
 	
 private:
 
@@ -134,21 +124,21 @@ private:
 
 	void Init();
 
-	//void RegisterSerializedEditorLines();
-
 	void UpdateAllEditorLines();
 
 	FUpdateTextureRegion2D* CalculateTextureRegions(const FIntPoint& StartIndex, int32 NumberOfPoints, int32& NumberOfRegionsOut);
 
-public:
-	// todo
-	void ReleaseData();
-
-	void UpdateWholeTexture() const;
-
+	void ReleaseData(); // todo
+	
 	int32 AddNewSegmentInstance(const FLinearColor& Color, float Width, int32 Index);
 
 	void UpdateTexture(const FIntPoint& StartIndex, int32 NumberOfPoints, uint8* SrcData, bool bMarkRenderStateDirty = true);
+
+
+public:
+	void UpdateWholeTexture() const;
+
+	FLinearColor GetLineColor(int32 LineId);
 	
 	/**
 	 * Reserves internal memory for a given amount of Lines and Segments per Line.
-- 
GitLab