Skip to content

Commit 150232f

Browse files
committed
Replaced framegrabber with editor screenshorts for the viewport capture source method
1 parent 75008ca commit 150232f

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

StableDiffusionTools/Source/StableDiffusionTools/Private/StableDiffusionSubsystem.cpp

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -247,19 +247,6 @@ TSharedPtr<FSceneViewport> UStableDiffusionSubsystem::GetCapturingViewport()
247247
return OutSceneViewport;
248248
}
249249

250-
void UStableDiffusionSubsystem::StartCapturingViewport()
251-
{
252-
// Find active viewport
253-
TSharedPtr<FSceneViewport> OutSceneViewport = GetCapturingViewport();
254-
SetCaptureViewport(OutSceneViewport.ToSharedRef(), OutSceneViewport->GetSize());
255-
}
256-
257-
void UStableDiffusionSubsystem::SetCaptureViewport(TSharedRef<FSceneViewport> Viewport, FIntPoint FrameSize)
258-
{
259-
ViewportCapture = MakeShared<FFrameGrabber>(Viewport, FrameSize);
260-
ViewportCapture->StartCapturingFrames();
261-
}
262-
263250
void UStableDiffusionSubsystem::GenerateImage(FStableDiffusionInput Input, EInputImageSource ImageSourceType)
264251
{
265252
if (!GeneratorBridge)
@@ -588,9 +575,6 @@ void UStableDiffusionSubsystem::CaptureFromViewportSource(FStableDiffusionInput
588575
{
589576
auto ViewportSize = GetCapturingViewport()->GetSizeXY();
590577

591-
// Make sure viewport capture objects are available
592-
StartCapturingViewport();
593-
594578
FIntPoint MinBounds, MaxBounds;
595579
CalculateOverlayBounds(float(Input.Options.OutSizeX) / float(Input.Options.OutSizeY), MinBounds, MaxBounds);
596580
FIntRect FrameBounds(MinBounds.X, MinBounds.Y, MaxBounds.X, MaxBounds.Y);
@@ -618,31 +602,22 @@ void UStableDiffusionSubsystem::CaptureFromViewportSource(FStableDiffusionInput
618602
SceneCapture.SceneCapture->Destroy();
619603
}
620604

621-
// Create a frame payload we will wait on to be filled with a frame
622-
auto framePtr = MakeShared<FCapturedFramePayload>();
623-
framePtr->OnFrameCapture.AddLambda([=](FColor* Pixels, FIntPoint BufferSize, FIntPoint TargetSize) mutable {
624-
// Copy frame data
625-
TArray<FColor> CopiedFrame = CopyFrameData(FrameBounds, BufferSize, Pixels);
626-
627-
// Find a final colour layer as a destination for our captured frame
628-
auto FinalColorProcessor = Input.ProcessedLayers.FindByPredicate([](const FLayerData& Layer) { return Layer.Processor->IsA<UFinalColorLayerProcessor>(); });
629-
if (FinalColorProcessor) {
630-
FinalColorProcessor->LayerPixels = MoveTemp(CopiedFrame);
631-
}
632-
633-
// Don't need to keep capturing whilst generating
634-
ViewportCapture->StopCapturingFrames();
605+
// Take the screenshot of the active viewport
606+
TArray<FColor> Pixels;
607+
GetViewportScreenShot(UStableDiffusionSubsystem::GetCapturingViewport().Get(), Pixels, FrameBounds);
635608

636-
// Set size from viewport
637-
Input.Options.InSizeX = FrameBounds.Size().X;
638-
Input.Options.InSizeY = FrameBounds.Size().Y;
609+
// Find a final colour layer as a destination for our captured frame
610+
auto FinalColorProcessor = Input.ProcessedLayers.FindByPredicate([](const FLayerData& Layer) { return Layer.Processor->IsA<UFinalColorLayerProcessor>(); });
611+
if (FinalColorProcessor) {
612+
FinalColorProcessor->LayerPixels = MoveTemp(Pixels);
613+
}
639614

640-
// Only start image generation when we have a frame
641-
StartImageGeneration(Input);
642-
});
615+
// Set size from viewport
616+
Input.Options.InSizeX = FrameBounds.Size().X;
617+
Input.Options.InSizeY = FrameBounds.Size().Y;
643618

644-
// Start frame capture
645-
ViewportCapture->CaptureThisFrame(framePtr);
619+
// Only start image generation when we have a frame
620+
StartImageGeneration(Input);
646621
}
647622

648623
void UStableDiffusionSubsystem::CaptureFromSceneCaptureSource(FStableDiffusionInput Input)
@@ -745,7 +720,7 @@ EModelStatus UStableDiffusionSubsystem::GetModelStatus() const
745720
return EModelStatus::Unloaded;
746721
}
747722

748-
TArray<FColor> UStableDiffusionSubsystem::CopyFrameData(FIntRect Bounds, FIntPoint BufferSize, FColor* ColorBuffer)
723+
TArray<FColor> UStableDiffusionSubsystem::CopyFrameData(FIntRect Bounds, FIntPoint BufferSize, const FColor* ColorBuffer)
749724
{
750725
// Copy frame data
751726
TArray<FColor> CopiedFrame;

StableDiffusionTools/Source/StableDiffusionTools/Public/StableDiffusionSubsystem.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class STABLEDIFFUSIONTOOLS_API UStableDiffusionSubsystem : public UEditorSubsyst
266266
UFUNCTION(BlueprintCallable, Category = "StableDiffusion|Camera")
267267
void UpdateSceneCaptureCamera(FViewportSceneCapture& SceneCapture);
268268

269-
TArray<FColor> CopyFrameData(FIntRect Bounds, FIntPoint BufferSize, FColor* ColorBuffer);
269+
TArray<FColor> CopyFrameData(FIntRect Bounds, FIntPoint BufferSize, const FColor* ColorBuffer);
270270

271271
static TSharedPtr<FSceneViewport> GetCapturingViewport();
272272

@@ -282,12 +282,6 @@ class STABLEDIFFUSIONTOOLS_API UStableDiffusionSubsystem : public UEditorSubsyst
282282
void UpdateImageProgress(int32 Step, int32 Timestep, float Progress, FIntPoint Size, const TArray<FColor>& PixelData);
283283

284284
private:
285-
// Viewport capture
286-
void StartCapturingViewport();
287-
void SetCaptureViewport(TSharedRef<FSceneViewport> Viewport, FIntPoint FrameSize);
288-
TSharedPtr<FFrameGrabber> ViewportCapture;
289-
FDelegateHandle ActiveEndframeHandler;
290-
291285
// Scene Capture Component capture
292286
FViewportSceneCapture CurrentSceneCapture;
293287

0 commit comments

Comments
 (0)