diff --git a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp index 1de264785753e54b00d83595458cc49be38bb2eb..64385ecd04e141314d6368a781db58e15362fad0 100644 --- a/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp +++ b/Source/InstancedMeshLineRendering/Private/GPUInstancedLineComponent.cpp @@ -1,12 +1,19 @@ -// Fill out your copyright notice in the Description page of Project Settings. - - #include "GPUInstancedLineComponent.h" #include "InstancedMeshLineRendering.h" // Only needed for logging //#pragma optimize("", on) +#pragma optimize("", off) +int UGPUInstancedLineComponent::DumpTexture() +{ + // Create texture if it doesn't exist + FTexture2DMipMap& Mip = PositionTexture->GetPlatformData()->Mips[0]; + void* TextureData = Mip.BulkData.Lock( LOCK_READ_WRITE ); + Mip.BulkData.Unlock( ); + return 0; +} +#pragma optimize("", on) UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer& ObjectInitializer) { NumCustomDataFloats = 5; @@ -15,8 +22,8 @@ UGPUInstancedLineComponent::UGPUInstancedLineComponent(const FObjectInitializer& CurrentTextureIndex = 0, //NextFreeId = 0; - TextureWidth = 2048; - TextureHeight = 2048; + TextureWidth = 3; + TextureHeight = 3; SetWorldTransform(FTransform::Identity); SetUsingAbsoluteLocation(true); @@ -73,14 +80,20 @@ void UGPUInstancedLineComponent::UpdateWholeTexture() const 0, 0, 0, - 0, FMath::Min(LinearLineData.Num(), TextureWidth), FMath::CeilToInt(static_cast<float>(LinearLineData.Num()) / static_cast<float>(TextureWidth))); + 0, FMath::Min(LinearLineData.Num(), TextureWidth), + FMath::CeilToInt(static_cast<float>(LinearLineData.Num()) / static_cast<float>(TextureWidth))); - TArray<FVector4>* TextureData = new TArray<FVector4>(LinearLineData); + TArray<FVector4f>* TextureData = new TArray<FVector4f>(LinearLineData); // Needed for packaging - works without this in the editor but crashes when packaging. TextureData->SetNumUninitialized(TextureWidth * TextureHeight, false); - PositionTexture->UpdateTextureRegions(0, 1, Region, TextureWidth * sizeof(FVector4), sizeof(FVector4), (uint8*)TextureData->GetData(), + + if(Region->Width * Region->Height == 0) + { + return; + } + PositionTexture->UpdateTextureRegions(0, 1, Region, TextureWidth * sizeof(FVector4f), sizeof(FVector4f), (uint8*)TextureData->GetData(), [](auto InTextureData, auto InRegions) { delete InTextureData; @@ -116,9 +129,14 @@ void UGPUInstancedLineComponent::UpdateTexture(const FIntPoint& StartIndex, int3 { int32 NumRegions = 0; FUpdateTextureRegion2D* Regions = CalculateTextureRegions(StartIndex, NumberOfPoints, NumRegions); + UE_LOG(LogTemp,Error,TEXT("Num Regions: %i"),NumRegions); + for (int i = 0; i < NumRegions; i++) + { + UE_LOG(LogTemp,Error,TEXT("Regions: %i, %i"),Regions[i].Width, Regions[i].Height) + } const uint32 Pitch = TextureWidth; - PositionTexture->UpdateTextureRegions(0, NumRegions, Regions, Pitch * sizeof(FVector4), sizeof(FVector4), SrcData, + PositionTexture->UpdateTextureRegions(0, NumRegions, Regions, Pitch * sizeof(FVector4f), sizeof(FVector4f), SrcData, [](auto InTextureData, auto InRegions) { // Clean up the copied data @@ -139,6 +157,7 @@ void UGPUInstancedLineComponent::Init() PositionTexture = UTexture2D::CreateTransient(TextureWidth, TextureHeight, PF_A32B32G32R32F); //// Allocate the texture RHI PositionTexture->UpdateResource(); + // PositionTexture-> UpdateWholeTexture(); } if (DynamicLineMaterial == nullptr) // FOR WHATEVER REASON I HAVE NO IDEA @@ -186,7 +205,20 @@ FUpdateTextureRegion2D* UGPUInstancedLineComponent::CalculateTextureRegions(cons (*Regions)[0].SrcX = 0; //InitialTextureMarker.X; (*Regions)[0].SrcY = 0; // InitialTextureMarker.Y; (*Regions)[0].Width = FMath::Min(NumberOfPoints, TextureWidth - static_cast<int32>((*Regions)[0].DestX)); - (*Regions)[0].Height = StartIndex.X == 0 ? (NumberOfPoints / TextureWidth) : 1; + // (*Regions)[0].Height = StartIndex.X == 0 ? (NumberOfPoints / TextureWidth) : 1; + // (*Regions)[0].Height = StartIndex.X == 0 ? (NumberOfPoints / TextureWidth) : 1; + if(StartIndex.X != 0) + { + (*Regions)[0].Height = 1; + } else if(StartIndex.X == 0 && (NumberOfPoints/TextureWidth) == 0) + { + (*Regions)[0].Height = 1; + NumberOfRegionsOut = 1; + return Regions->GetData(); + } else + { + (*Regions)[0].Height = (NumberOfPoints / TextureWidth); + } 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()); @@ -366,7 +398,7 @@ void UGPUInstancedLineComponent::PostEditChangeChainProperty(FPropertyChangedCha RemovePoint(EditorLines[EditorLineIndex].RespectiveLineId, 2); // just repeatedly delete point 2 } - TArray<FVector> Pts = { FVector(0, 0, 0), FVector(0, 0, 0) }; + TArray<FVector3f> Pts = { FVector3f(0, 0, 0), FVector3f(0, 0, 0) }; UpdatePoints(EditorLines[EditorLineIndex].RespectiveLineId, 0, Pts); break; @@ -573,7 +605,7 @@ void UGPUInstancedLineComponent::AddBulkInternal(int32 NumberOfLines, int32 Numb } void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, - TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths) + TArray<FVector4f>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths) { if (LinearLineData.Num() != 0 || LineMap.Num() != 0 || GetInstanceCount() != 0) { @@ -598,7 +630,7 @@ void UGPUInstancedLineComponent::InitializeLinesInBulk(int32 NumberOfLines, int3 } void UGPUInstancedLineComponent::InitializeLinesInBulkTesselate(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, - TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths) + TArray<FVector4f>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths) { // Just as a test const int32 NumOriginalSegments = NumberOfSegmentsPerLine; @@ -623,19 +655,19 @@ void UGPUInstancedLineComponent::InitializeLinesInBulkTesselate(int32 NumberOfLi for (int LineIdx = NumberOfLines - 1; LineIdx >= 0; --LineIdx) { // Get start and end of line - const FVector4 Start = LinearLineData[LineIdx * (NumOriginalSegments + 1)]; - const FVector4 End = LinearLineData[LineIdx * (NumOriginalSegments + 1) + 1]; - const FVector4 Dist = End - Start; + const FVector4f Start = LinearLineData[LineIdx * (NumOriginalSegments + 1)]; + const FVector4f End = LinearLineData[LineIdx * (NumOriginalSegments + 1) + 1]; + const FVector4f Dist = End - Start; // Tesselate - const FVector4 First = Start + 0.1 * Dist; - const FVector4 Second = Start + 0.2 * Dist; - const FVector4 Third = Start + 0.3 * Dist; - const FVector4 Fourth = Start + 0.4 * Dist; - const FVector4 Fith = Start + 0.5 * Dist; - const FVector4 Sixth = Start + 0.6 * Dist; - const FVector4 Seventh = Start + 0.7 * Dist; - const FVector4 Eigth = Start + 0.8 * Dist; - const FVector4 Nineth = Start + 0.9 * Dist; + const FVector4f First = Start + 0.1 * Dist; + const FVector4f Second = Start + 0.2 * Dist; + const FVector4f Third = Start + 0.3 * Dist; + const FVector4f Fourth = Start + 0.4 * Dist; + const FVector4f Fith = Start + 0.5 * Dist; + const FVector4f Sixth = Start + 0.6 * Dist; + const FVector4f Seventh = Start + 0.7 * Dist; + const FVector4f Eigth = Start + 0.8 * Dist; + const FVector4f Nineth = Start + 0.9 * Dist; LinearLineData.Insert({ First, Second, Third, Fourth, Fith, Sixth, Seventh, Eigth, Nineth }, LineIdx * (NumOriginalSegments + 1) + 1); @@ -645,7 +677,7 @@ void UGPUInstancedLineComponent::InitializeLinesInBulkTesselate(int32 NumberOfLi } -int32 UGPUInstancedLineComponent::AddLine(const TArray<FVector>& Line, FLinearColor Color, float Width) +int32 UGPUInstancedLineComponent::AddLine(const TArray<FVector3f>& Line, FLinearColor Color, float Width) { if (Line.Num() < 2) { @@ -681,7 +713,7 @@ int32 UGPUInstancedLineComponent::AddLine(const TArray<FVector>& Line, FLinearCo CurrentTextureIndex++; // Recreate the data on the heap to allow asynchronous texture update. - TArray<FVector4>* TextureData = new TArray<FVector4>(Line); + TArray<FVector4f>* TextureData = new TArray<FVector4f>(Line); // "Mark" the first entry with w=0 as first segment of the line, and the last with w=-1 (*TextureData)[0].W = 0.0; TextureData->Last().W = -1.0; @@ -694,7 +726,7 @@ int32 UGPUInstancedLineComponent::AddLine(const TArray<FVector>& Line, FLinearCo return LineId; } -int32 UGPUInstancedLineComponent::AddLine(TArray<FVector4>& Line, FLinearColor Color, float Width, bool bMarkRenderStateDirty) +int32 UGPUInstancedLineComponent::AddLine(TArray<FVector4f>& Line, FLinearColor Color, float Width, bool bMarkRenderStateDirty) { if (Line.Num() < 2) { @@ -727,7 +759,7 @@ int32 UGPUInstancedLineComponent::AddLine(TArray<FVector4>& Line, FLinearColor C CurrentTextureIndex++; // Recreate the data on the heap to allow asynchronous texture update. - TArray<FVector4>* TextureData = new TArray<FVector4>(MoveTemp(Line)); + TArray<FVector4f>* TextureData = new TArray<FVector4f>(MoveTemp(Line)); // Store the points in a linear array here @@ -742,7 +774,7 @@ int32 UGPUInstancedLineComponent::AddLine(TArray<FVector4>& Line, FLinearColor C int32 UGPUInstancedLineComponent::AddLineFromEditorData(FEditorLineData& LineData) { const TArray<FEditorPoint>* EditorLineDataPtr = &LineData.Points; - const TArray<FVector>* FVectorLineDataPtr = reinterpret_cast<const TArray<FVector>*>(EditorLineDataPtr); + const TArray<FVector3f>* FVectorLineDataPtr = reinterpret_cast<const TArray<FVector3f>*>(EditorLineDataPtr); LineData.RespectiveLineId = AddLine(*FVectorLineDataPtr, LineData.Color, LineData.Width); EditorLineIds.Add(LineData.RespectiveLineId); @@ -752,7 +784,7 @@ int32 UGPUInstancedLineComponent::AddLineFromEditorData(FEditorLineData& LineDat // Add a point to the end of the specified line -bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) +bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector3f& Point) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::AddPoint")) @@ -772,13 +804,13 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) - FVector4& PreviousLastPoint = LinearLineData[Indices.TextureIndex]; + FVector4f& PreviousLastPoint = LinearLineData[Indices.TextureIndex]; PreviousLastPoint.W = 1.0; // This isn't the line end anymore. if(LineId == LineMap.Num() - 1) { - LinearLineData.Add(FVector4(Point, -1)); // This is the last point now. + LinearLineData.Add(FVector4f(Point, -1)); // This is the last point now. // Add a new dummy entry for the last point Line.IndexArray.Add(FGPULineIndices{ -1, LinearLineData.Num() - 1 }); @@ -794,9 +826,9 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) 0, 1, 1); // Copy for now - no need to do that - TArray<FVector4>* TextureData = new TArray<FVector4>{LinearLineData.Last()}; + TArray<FVector4f>* TextureData = new TArray<FVector4f>{LinearLineData.Last()}; - PositionTexture->UpdateTextureRegions(0, 1, Region, TextureWidth * sizeof(FVector4), sizeof(FVector4), (uint8*)TextureData->GetData(), + PositionTexture->UpdateTextureRegions(0, 1, Region, TextureWidth * sizeof(FVector4f), sizeof(FVector4f), (uint8*)TextureData->GetData(), [](auto InTextureData, auto InRegions) { delete InTextureData; @@ -805,7 +837,7 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) // Update old point - TArray<FVector4>* TextureData2 = new TArray<FVector4>{ PreviousLastPoint }; + TArray<FVector4f>* TextureData2 = new TArray<FVector4f>{ PreviousLastPoint }; // Update the new point int32 PrevX = InitialTextureMarker.X - 1; int32 PrevY = InitialTextureMarker.Y; @@ -820,7 +852,7 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) PrevY, 0, 0, 1, 1); - PositionTexture->UpdateTextureRegions(0, 1, Region2, TextureWidth * sizeof(FVector4), sizeof(FVector4), (uint8*)TextureData2->GetData(), + PositionTexture->UpdateTextureRegions(0, 1, Region2, TextureWidth * sizeof(FVector4f), sizeof(FVector4f), (uint8*)TextureData2->GetData(), [](auto InTextureData, auto InRegions) { delete InTextureData; @@ -852,7 +884,7 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) } } // Insert the point into the LinearData: - LinearLineData.Insert(FVector4(Point, -1), NewPointTextureIndex); + LinearLineData.Insert(FVector4f(Point, -1), NewPointTextureIndex); // Add it to the LineMap - this is the new dummy segment because we add it at the end of the line Line.IndexArray.Add(FGPULineIndices{ -1, NewPointTextureIndex }); //SetCustomDataValue(InstanceId, 4, NewPointTextureIndex, true); @@ -867,7 +899,7 @@ bool UGPUInstancedLineComponent::AddPoint(int32 LineId, const FVector& Point) return true; } -bool UGPUInstancedLineComponent::InsertPoint(int32 LineId, int32 SegmentId, const FVector& Point, const FLinearColor& Color) +bool UGPUInstancedLineComponent::InsertPoint(int32 LineId, int32 SegmentId, const FVector3f& Point, const FLinearColor& Color) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::InsertPoint")) @@ -880,7 +912,7 @@ bool UGPUInstancedLineComponent::InsertPoint(int32 LineId, int32 SegmentId, cons const float Width = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 3]; const int32 LinearDataIndex = Line.IndexArray[SegmentId].TextureIndex; - LinearLineData.Insert(FVector4(Point), LinearDataIndex); + LinearLineData.Insert(FVector4f(Point), LinearDataIndex); if (SegmentId == 0) { // This is now the new start point @@ -925,18 +957,18 @@ bool UGPUInstancedLineComponent::InsertPoint(int32 LineId, int32 SegmentId, cons return true; } -bool UGPUInstancedLineComponent::InsertPointWithSameColor(int32 LineId, int32 SegmentId, const FVector& Point) +bool UGPUInstancedLineComponent::InsertPointWithSameColor(int32 LineId, int32 SegmentId, const FVector3f& Point) { return InsertPoint(LineId, SegmentId, Point, GetLineColor(LineId)); } -bool UGPUInstancedLineComponent::InsertPointWithColor(int32 LineId, int32 SegmentId, const FVector& Point, +bool UGPUInstancedLineComponent::InsertPointWithColor(int32 LineId, int32 SegmentId, const FVector3f& Point, const FLinearColor& Color) { return InsertPoint(LineId, SegmentId, Point, Color); } -bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>& Points) +bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector3f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::AddPoint")) @@ -954,7 +986,7 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>& FGPULineIndices& Indices = Line.IndexArray.Last(); const int32 PointTextureIndex = Indices.TextureIndex; - FVector4& PreviousLastPoint = LinearLineData[Indices.TextureIndex]; + FVector4f& PreviousLastPoint = LinearLineData[Indices.TextureIndex]; PreviousLastPoint.W = 1.0; // This isn't the line end anymore. if (LineId == LineMap.Num() - 1) @@ -982,7 +1014,7 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>& MoveTextureMarker(); // Recreate the data on the heap to allow asynchronous texture update. - TArray<FVector4>* TextureData = new TArray<FVector4>(Points); + TArray<FVector4f>* TextureData = new TArray<FVector4f>(Points); UpdateTexture(InitialTextureMarker, Points.Num(), (uint8*)TextureData->GetData()); @@ -1027,7 +1059,7 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>& // Insert the points into the LinearData: - TArray<FVector4> TextureData = TArray<FVector4>(Points); + TArray<FVector4f> TextureData = TArray<FVector4f>(Points); TextureData.Last().W = -1; LinearLineData.Insert(TextureData, NewPointTextureIndex); @@ -1045,7 +1077,7 @@ bool UGPUInstancedLineComponent::AddPoints(int32 LineId, const TArray<FVector>& return true; } -bool UGPUInstancedLineComponent::InsertPoints(int32 LineId, int32 SegmentId, const TArray<FVector>& Points) +bool UGPUInstancedLineComponent::InsertPoints(int32 LineId, int32 SegmentId, const TArray<FVector3f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::InsertPoint")) @@ -1059,7 +1091,7 @@ bool UGPUInstancedLineComponent::InsertPoints(int32 LineId, int32 SegmentId, con const float Width = PerInstanceSMCustomData[Line.IndexArray[0].InstanceIndex * NumCustomDataFloats + 3]; const int32 LinearDataIndex = Line.IndexArray[SegmentId].TextureIndex; - LinearLineData.Insert(TArray<FVector4>(Points), LinearDataIndex); + LinearLineData.Insert(TArray<FVector4f>(Points), LinearDataIndex); if (SegmentId == 0) { // This is now the new start point @@ -1108,7 +1140,7 @@ bool UGPUInstancedLineComponent::InsertPoints(int32 LineId, int32 SegmentId, con } // todo test this not sure if it actually works -bool UGPUInstancedLineComponent::UpdateLine(int32 LineId, TArray<FVector>& Points) +bool UGPUInstancedLineComponent::UpdateLine(int32 LineId, TArray<FVector3f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::UpdateLine")) @@ -1119,13 +1151,13 @@ bool UGPUInstancedLineComponent::UpdateLine(int32 LineId, TArray<FVector>& Point return false; } - TArray<FVector4>* TextureData = new TArray<FVector4>(MoveTemp(Points)); + TArray<FVector4f>* TextureData = new TArray<FVector4f>(MoveTemp(Points)); TextureData->Last().W = -1; (*TextureData)[0].W = 0; const int32 TextureIndex = Line.IndexArray[0].TextureIndex; const FIntPoint StartIndex(TextureIndex % TextureWidth, TextureIndex / TextureWidth); - FMemory::Memcpy(LinearLineData.GetData() + TextureIndex, TextureData->GetData(), TextureData->Num() * sizeof(FVector4)); + FMemory::Memcpy(LinearLineData.GetData() + TextureIndex, TextureData->GetData(), TextureData->Num() * sizeof(FVector4f)); UpdateTexture(StartIndex, TextureData->Num(), (uint8*)TextureData->GetData()); @@ -1133,7 +1165,7 @@ bool UGPUInstancedLineComponent::UpdateLine(int32 LineId, TArray<FVector>& Point } // Use the Points array to update the lines, assuming the array contains already well defined data. -bool UGPUInstancedLineComponent::UpdateLinesDataDirectly(int32 LineIdStart, TArray<FVector4>& Points) +bool UGPUInstancedLineComponent::UpdateLinesDataDirectly(int32 LineIdStart, TArray<FVector4f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::UpdateLines - AlignedMem")) @@ -1141,15 +1173,15 @@ bool UGPUInstancedLineComponent::UpdateLinesDataDirectly(int32 LineIdStart, TArr return false; { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::UpdateLines - Memcopy LinearLineData")) - FMemory::Memcpy(LinearLineData.GetData() + LineIdStart, Points.GetData(), Points.Num() * sizeof(FVector4)); + FMemory::Memcpy(LinearLineData.GetData() + LineIdStart, Points.GetData(), Points.Num() * sizeof(FVector4f)); } const int32 X = LineIdStart % TextureWidth; const int32 Y = LineIdStart / TextureWidth; - TArray<FVector4>* TextureData; + TArray<FVector4f>* TextureData; { // Copy for now TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::UpdateLines - MoveTemp Texture Data")) - TextureData = new TArray<FVector4>(MoveTemp(Points)); + TextureData = new TArray<FVector4f>(MoveTemp(Points)); } UpdateTexture(FIntPoint(X, Y), TextureData->Num(), (uint8*)TextureData->GetData()); @@ -1158,22 +1190,22 @@ bool UGPUInstancedLineComponent::UpdateLinesDataDirectly(int32 LineIdStart, TArr bool UGPUInstancedLineComponent::UpdateLineFromEditorData(const FEditorLineData& LineData) { - TArray<FVector> Points; + TArray<FVector3f> Points; Points.SetNumUninitialized(LineData.Points.Num()); - FMemory::Memcpy(Points.GetData(), LineData.Points.GetData(), sizeof(FVector) * LineData.Points.Num()); + FMemory::Memcpy(Points.GetData(), LineData.Points.GetData(), sizeof(FVector3f) * LineData.Points.Num()); return UpdateLine(LineData.RespectiveLineId, Points); } -bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>& Points) +bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVector4f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Move")) const int32 X = LineIdStart % TextureWidth; const int32 Y = LineIdStart / TextureWidth; - TArray<FVector4>* TextureData; + TArray<FVector4f>* TextureData; { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - MoveTemp Texture Data")) - TextureData = new TArray<FVector4>(MoveTemp(Points)); + TextureData = new TArray<FVector4f>(MoveTemp(Points)); } UpdateTexture(FIntPoint(X, Y), TextureData->Num(), (uint8*)TextureData->GetData()); @@ -1181,15 +1213,15 @@ bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVe return true; } -bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, const TArray<FVector4>& Points) +bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, const TArray<FVector4f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Memcpy")) - TArray<FVector4> Cpy = Points; + TArray<FVector4f> Cpy = Points; return DrawLinesDirectly(LineIdStart, Cpy); } -bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>* Points) +bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, TArray<FVector4f>* Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::DrawLinesDirectly - Ptr")) @@ -1214,7 +1246,7 @@ bool UGPUInstancedLineComponent::DrawLinesDirectly(int32 LineIdStart, uint8* Src } // todo test this not sure if it actually works -bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const FVector& Point) +bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const FVector3f& Point) { // Should check for validity const FGPULineIndices& PointIndices = LineMap[LineId].IndexArray[PointId]; @@ -1238,10 +1270,10 @@ bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const 0, 1, 1); // Copy for now - no need to do that - TArray<FVector4>* TextureData = new TArray<FVector4>{ LinearLineData[PointIndices.TextureIndex] }; + TArray<FVector4f>* TextureData = new TArray<FVector4f>{ LinearLineData[PointIndices.TextureIndex] }; - PositionTexture->UpdateTextureRegions(0, 1, Region, TextureWidth * sizeof(FVector4), sizeof(FVector4), (uint8*)TextureData->GetData(), + PositionTexture->UpdateTextureRegions(0, 1, Region, TextureWidth * sizeof(FVector4f), sizeof(FVector4f), (uint8*)TextureData->GetData(), [](auto InTextureData, auto InRegions) { delete InTextureData; @@ -1253,7 +1285,7 @@ bool UGPUInstancedLineComponent::UpdatePoint(int32 LineId, int32 PointId, const } // todo test this not sure if it actually works -bool UGPUInstancedLineComponent::UpdatePoints(int32 LineId, int32 StartingPointId, TArray<FVector>& Points) +bool UGPUInstancedLineComponent::UpdatePoints(int32 LineId, int32 StartingPointId, TArray<FVector3f>& Points) { TRACE_CPUPROFILER_EVENT_SCOPE(TEXT("UGPUInstancedLineComponent::UpdatePoints")) @@ -1268,7 +1300,7 @@ bool UGPUInstancedLineComponent::UpdatePoints(int32 LineId, int32 StartingPointI return UpdateLine(LineId, Points); } - TArray<FVector4>* TextureData = new TArray<FVector4>(MoveTemp(Points)); + TArray<FVector4f>* TextureData = new TArray<FVector4f>(MoveTemp(Points)); if(StartingPointId == 0) { (*TextureData)[0].W = 0; @@ -1281,7 +1313,7 @@ bool UGPUInstancedLineComponent::UpdatePoints(int32 LineId, int32 StartingPointI const int32 StartTextureIndex = Line.IndexArray[StartingPointId].TextureIndex; const FIntPoint StartIndex(StartTextureIndex % TextureWidth, StartTextureIndex / TextureWidth); - FMemory::Memcpy(LinearLineData.GetData() + StartTextureIndex, TextureData->GetData(), TextureData->Num() * sizeof(FVector4)); + FMemory::Memcpy(LinearLineData.GetData() + StartTextureIndex, TextureData->GetData(), TextureData->Num() * sizeof(FVector4f)); UpdateTexture(StartIndex, TextureData->Num(), (uint8*)TextureData->GetData()); diff --git a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h index ddb24ac0df444ef61a677da82494568d3bfb54d6..c5b50b56e9de61f46a5a7a8e908298724a1eabf3 100644 --- a/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h +++ b/Source/InstancedMeshLineRendering/Public/GPUInstancedLineComponent.h @@ -13,14 +13,14 @@ struct FEditorPoint GENERATED_USTRUCT_BODY() UPROPERTY(EditAnywhere, meta = (MakeEditWidget = true)) - FVector Point; + FVector3f Point; FEditorPoint() { - Point = FVector(0, 0, 0); + Point = FVector3f(0, 0, 0); } - FEditorPoint(const FVector& InPoint) + FEditorPoint(const FVector3f& InPoint) : Point(InPoint) { } @@ -51,7 +51,7 @@ struct FEditorLineData Color = FColor::Red; } - FEditorLineData(const TArray<FVector>& InPoints, float InWidth, const FColor& InColor) + FEditorLineData(const TArray<FVector3f>& InPoints, float InWidth, const FColor& InColor) : Points(InPoints), Width(InWidth), Color(InColor) { if(Points.Num() < 2) @@ -182,9 +182,9 @@ public: * @return int32 Id of the line, can be used to access it later on. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - void InitializeLinesInBulk(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths); + void InitializeLinesInBulk(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, TArray<FVector4f>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths); - void InitializeLinesInBulkTesselate(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, TArray<FVector4>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths); + void InitializeLinesInBulkTesselate(int32 NumberOfLines, int32 NumberOfSegmentsPerLine, TArray<FVector4f>& Points, const TArray<FLinearColor>& Colors, const TArray<float>& Widths); /** @@ -197,10 +197,12 @@ public: * @return int32 Id of the line, can be used to access it later on. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - int32 AddLine(const TArray<FVector>& Line, FLinearColor Color, float Width = 1.0); + int32 AddLine(const TArray<FVector3f>& Line, FLinearColor Color, float Width = 1.0); + UFUNCTION(BlueprintCallable) + int DumpTexture(); - int32 AddLine(TArray<FVector4>& Line, FLinearColor Color, float Width = 1.0, bool bMarkRenderStateDirty = true); + int32 AddLine(TArray<FVector4f>& Line, FLinearColor Color, float Width = 1.0, bool bMarkRenderStateDirty = true); /** * Adds a line and returns the respective ID of the line, which can be used to identify it for updating, modifying and removing. @@ -221,10 +223,10 @@ public: * @return bool Returns true on successful addition of points. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool AddPoint(int32 LineId, const FVector& Point); + bool AddPoint(int32 LineId, const FVector3f& Point); // Internal insertion function not callable via blueprints. - bool InsertPoint(int32 LineId, int32 SegmentId, const FVector& Point, const FLinearColor& Color); + bool InsertPoint(int32 LineId, int32 SegmentId, const FVector3f& Point, const FLinearColor& Color); /** * Inserts a point into the specified line before the specified segment with the same color as the original line. @@ -236,7 +238,7 @@ public: * @return bool Returns true on successful insertion of points. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool InsertPointWithSameColor(int32 LineId, int32 SegmentId, const FVector& Point); + bool InsertPointWithSameColor(int32 LineId, int32 SegmentId, const FVector3f& Point); /** * Inserts a point into the specified line before the specified segment with a specified color. @@ -249,7 +251,7 @@ public: * @return bool Returns true on successful insertion of points. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool InsertPointWithColor(int32 LineId, int32 SegmentId, const FVector& Point, const FLinearColor& Color); + bool InsertPointWithColor(int32 LineId, int32 SegmentId, const FVector3f& Point, const FLinearColor& Color); /** * Adds a number of consecutive points at the end of the specified line. @@ -260,7 +262,7 @@ public: * @return bool Returns true on successful insertion of points. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool AddPoints(int32 LineId, const TArray<FVector>& Points); + bool AddPoints(int32 LineId, const TArray<FVector3f>& Points); /** * Inserts a number of consecutive points into the specified line before the specified segment. @@ -273,7 +275,7 @@ public: * @return bool Returns true on successful insertion. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool InsertPoints(int32 LineId, int32 SegmentId, const TArray<FVector>& Points); + bool InsertPoints(int32 LineId, int32 SegmentId, const TArray<FVector3f>& Points); // Update Functions @@ -286,7 +288,7 @@ public: * @return bool Returns true on successful update. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool UpdateLine(int32 LineId, TArray<FVector>& Points); + bool UpdateLine(int32 LineId, TArray<FVector3f>& Points); /** @@ -300,7 +302,7 @@ public: * The array will be MOVED, so Points will be empty on return. * @return bool Returns true on successful update. */ - bool UpdateLinesDataDirectly(int32 LineIdStart, TArray<FVector4>& Points); + bool UpdateLinesDataDirectly(int32 LineIdStart, TArray<FVector4f>& Points); /** * Updates a line from a FEditorLineData struct and returns true on success. @@ -324,12 +326,12 @@ public: * The array will be MOVED, not copied, so Points will be empty on return. * @return bool Returns true on successful update. */ - bool DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>& Points); + bool DrawLinesDirectly(int32 LineIdStart, TArray<FVector4f>& Points); - bool DrawLinesDirectly(int32 LineIdStart, const TArray<FVector4>& Points); + bool DrawLinesDirectly(int32 LineIdStart, const TArray<FVector4f>& Points); - bool DrawLinesDirectly(int32 LineIdStart, TArray<FVector4>* Points); + bool DrawLinesDirectly(int32 LineIdStart, TArray<FVector4f>* Points); bool DrawLinesDirectly(int32 LineIdStart, uint8* SrcData, int32 Num); @@ -342,11 +344,11 @@ public: * @return bool Returns true on successful update. */ UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool UpdatePoint(int32 LineId, int32 PointId, const FVector& Point); + bool UpdatePoint(int32 LineId, int32 PointId, const FVector3f& Point); // todo UFUNCTION(BlueprintCallable, Category = "Components|InstancedLineComponent") - bool UpdatePoints(int32 LineId, int32 StartingPointId, TArray<FVector>& Points); + bool UpdatePoints(int32 LineId, int32 StartingPointId, TArray<FVector3f>& Points); // Removal Functions // todo @@ -471,7 +473,7 @@ public: //private: UPROPERTY() - TArray<FVector4> LinearLineData; + TArray<FVector4f> LinearLineData; // Array that keeps track of editor lines. This is ONLY needed because the array can be cleared by a simple stupid button press in the // details panel, circumventing the PostEditChangeChainProperty function by clearing the array first. The change callback gets called, but