2019-12-26 14:45:42 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2019-06-11 18:27:07 -04:00
|
|
|
|
|
|
|
|
#include "VT/RuntimeVirtualTextureSceneProxy.h"
|
|
|
|
|
|
|
|
|
|
#include "Components/RuntimeVirtualTextureComponent.h"
|
2021-12-02 08:41:45 -05:00
|
|
|
#include "RendererOnScreenNotification.h"
|
2019-08-08 23:35:07 -04:00
|
|
|
#include "VirtualTextureSystem.h"
|
2019-06-11 18:27:07 -04:00
|
|
|
#include "VT/RuntimeVirtualTexture.h"
|
|
|
|
|
#include "VT/RuntimeVirtualTextureProducer.h"
|
2020-04-14 13:33:06 -04:00
|
|
|
#include "VT/VirtualTexture.h"
|
|
|
|
|
#include "VT/VirtualTextureBuilder.h"
|
2020-09-24 00:43:27 -04:00
|
|
|
#include "VT/VirtualTextureScalability.h"
|
2019-06-11 18:27:07 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
#define LOCTEXT_NAMESPACE "VirtualTexture"
|
|
|
|
|
|
2019-06-11 18:27:07 -04:00
|
|
|
int32 FRuntimeVirtualTextureSceneProxy::ProducerIdGenerator = 1;
|
|
|
|
|
|
|
|
|
|
FRuntimeVirtualTextureSceneProxy::FRuntimeVirtualTextureSceneProxy(URuntimeVirtualTextureComponent* InComponent)
|
2019-08-15 13:51:44 -04:00
|
|
|
: SceneIndex(0)
|
|
|
|
|
, ProducerId(0)
|
|
|
|
|
, VirtualTexture(nullptr)
|
2020-09-24 00:43:27 -04:00
|
|
|
, bHidePrimitivesInEditor(false)
|
|
|
|
|
, bHidePrimitivesInGame(false)
|
2019-08-08 23:35:07 -04:00
|
|
|
, CombinedDirtyRect(0, 0, 0, 0)
|
2019-06-11 18:27:07 -04:00
|
|
|
{
|
2020-09-24 00:43:27 -04:00
|
|
|
// Evaluate the flags used to hide primitives writing to this virtual texture.
|
|
|
|
|
InComponent->GetHidePrimitiveSettings(bHidePrimitivesInEditor, bHidePrimitivesInGame);
|
|
|
|
|
|
|
|
|
|
if (InComponent->GetVirtualTexture() != nullptr)
|
2019-06-11 18:27:07 -04:00
|
|
|
{
|
|
|
|
|
// We store a ProducerId here so that we will be able to find our SceneIndex from the Producer during rendering.
|
|
|
|
|
// We will need the SceneIndex to determine which primitives should render to this Producer.
|
|
|
|
|
ProducerId = ProducerIdGenerator++;
|
|
|
|
|
|
2020-09-24 00:43:27 -04:00
|
|
|
URuntimeVirtualTexture::FInitSettings InitSettings;
|
|
|
|
|
InitSettings.TileCountBias = InComponent->IsScalable() ? VirtualTextureScalability::GetRuntimeVirtualTextureSizeBias(InComponent->GetScalabilityGroup()) : 0;
|
|
|
|
|
|
2019-08-21 08:57:14 -04:00
|
|
|
VirtualTexture = InComponent->GetVirtualTexture();
|
2020-06-23 18:40:00 -04:00
|
|
|
Transform = InComponent->GetComponentTransform();
|
2020-03-17 23:19:30 -04:00
|
|
|
const FBox Bounds = InComponent->Bounds.GetBox();
|
2019-06-11 18:27:07 -04:00
|
|
|
|
2019-06-17 14:20:00 -04:00
|
|
|
// The producer description is calculated using the transform to determine the aspect ratio
|
2021-02-01 11:30:52 -04:00
|
|
|
FVTProducerDescription ProducerDesc;
|
|
|
|
|
VirtualTexture->GetProducerDescription(ProducerDesc, InitSettings, Transform);
|
|
|
|
|
VirtualTextureSize = FIntPoint(ProducerDesc.BlockWidthInTiles * ProducerDesc.TileSize, ProducerDesc.BlockHeightInTiles * ProducerDesc.TileSize);
|
2019-08-21 11:53:10 -04:00
|
|
|
// We only need to dirty flush up to the producer description MaxLevel which accounts for the RemoveLowMips
|
2021-02-01 11:30:52 -04:00
|
|
|
MaxDirtyLevel = ProducerDesc.MaxLevel;
|
2019-06-17 14:20:00 -04:00
|
|
|
|
2019-08-21 08:57:14 -04:00
|
|
|
const ERuntimeVirtualTextureMaterialType MaterialType = VirtualTexture->GetMaterialType();
|
2019-09-03 10:52:02 -04:00
|
|
|
const bool bClearTextures = VirtualTexture->GetClearTextures();
|
2019-08-21 08:57:14 -04:00
|
|
|
|
2021-02-01 11:30:52 -04:00
|
|
|
// The producer object created here will be passed into the virtual texture system which will take ownership.
|
|
|
|
|
IVirtualTexture* Producer = new FRuntimeVirtualTextureProducer(ProducerDesc, ProducerId, MaterialType, bClearTextures, InComponent->GetScene(), Transform, Bounds);
|
2019-08-21 08:57:14 -04:00
|
|
|
|
2021-02-01 11:30:52 -04:00
|
|
|
// Create a producer for the streaming low mips.
|
|
|
|
|
// This is bound with the main producer so that one allocated VT can use both runtime or streaming producers dependent on mip level.
|
2020-05-01 19:13:15 -04:00
|
|
|
if (InComponent->IsStreamingLowMips())
|
2019-08-21 08:57:14 -04:00
|
|
|
{
|
2021-11-18 14:37:34 -05:00
|
|
|
if (InComponent->IsStreamingTextureInvalid())
|
|
|
|
|
{
|
|
|
|
|
#if !UE_BUILD_SHIPPING
|
|
|
|
|
// Notify that streaming texture is invalid since this can cause performance regression.
|
|
|
|
|
const FString Name = InComponent->GetPathName();
|
2021-12-02 08:41:45 -05:00
|
|
|
OnScreenWarningDelegateHandle = FRendererOnScreenNotification::Get().AddLambda([Name](FCoreDelegates::FSeverityMessageMap& OutMessages)
|
2021-11-18 14:37:34 -05:00
|
|
|
{
|
|
|
|
|
OutMessages.Add(
|
|
|
|
|
FCoreDelegates::EOnScreenMessageSeverity::Warning,
|
|
|
|
|
FText::Format(LOCTEXT("SVTInvalid", "Runtime Virtual Texture '{0}' streaming mips needs to be rebuilt."), FText::FromString(Name)));
|
|
|
|
|
});
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UVirtualTexture2D* StreamingTexture = InComponent->GetStreamingTexture()->Texture;
|
2021-02-01 11:30:52 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
FVTProducerDescription StreamingProducerDesc;
|
|
|
|
|
IVirtualTexture* StreamingProducer = RuntimeVirtualTexture::CreateStreamingTextureProducer(StreamingTexture, ProducerDesc, StreamingProducerDesc);
|
2021-02-01 11:30:52 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
ensure(ProducerDesc.MaxLevel >= StreamingProducerDesc.MaxLevel);
|
|
|
|
|
const int32 TransitionLevel = ProducerDesc.MaxLevel - StreamingProducerDesc.MaxLevel;
|
2021-02-01 11:30:52 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
Producer = RuntimeVirtualTexture::BindStreamingTextureProducer(Producer, StreamingProducer, TransitionLevel);
|
2021-02-01 11:30:52 -04:00
|
|
|
|
2021-11-18 14:37:34 -05:00
|
|
|
// Any dirty flushes don't need to flush the streaming mips (they only change with a build step).
|
|
|
|
|
MaxDirtyLevel = TransitionLevel - 1;
|
|
|
|
|
}
|
2019-08-21 08:57:14 -04:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 14:20:00 -04:00
|
|
|
// The Initialize() call will allocate the virtual texture by spawning work on the render thread.
|
2021-02-01 11:30:52 -04:00
|
|
|
VirtualTexture->Initialize(Producer, ProducerDesc, Transform, Bounds);
|
2022-02-02 07:39:44 -05:00
|
|
|
|
|
|
|
|
// Store the ProducerHandle and SpaceID immediately after virtual texture is initialized.
|
|
|
|
|
ENQUEUE_RENDER_COMMAND(GetProducerHandle)(
|
2022-04-13 09:38:53 -04:00
|
|
|
[this, VirtualTexturePtr = VirtualTexture](FRHICommandList& RHICmdList)
|
2022-02-02 07:39:44 -05:00
|
|
|
{
|
2022-04-13 09:38:53 -04:00
|
|
|
ProducerHandle = VirtualTexturePtr->GetProducerHandle();
|
|
|
|
|
SpaceID = VirtualTexturePtr->GetAllocatedVirtualTexture()->GetSpaceID();
|
2022-02-02 07:39:44 -05:00
|
|
|
});
|
2019-06-11 18:27:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FRuntimeVirtualTextureSceneProxy::~FRuntimeVirtualTextureSceneProxy()
|
|
|
|
|
{
|
|
|
|
|
checkSlow(IsInRenderingThread());
|
2021-11-18 14:37:34 -05:00
|
|
|
|
|
|
|
|
#if !UE_BUILD_SHIPPING
|
2021-12-02 08:41:45 -05:00
|
|
|
FRendererOnScreenNotification::Get().Remove(OnScreenWarningDelegateHandle);
|
2021-11-18 14:37:34 -05:00
|
|
|
#endif
|
2019-06-11 18:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FRuntimeVirtualTextureSceneProxy::Release()
|
|
|
|
|
{
|
2022-02-02 07:39:44 -05:00
|
|
|
checkSlow(!IsInRenderingThread());
|
|
|
|
|
|
2019-06-11 18:27:07 -04:00
|
|
|
if (VirtualTexture != nullptr)
|
|
|
|
|
{
|
|
|
|
|
VirtualTexture->Release();
|
|
|
|
|
VirtualTexture = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-08 23:35:07 -04:00
|
|
|
|
|
|
|
|
void FRuntimeVirtualTextureSceneProxy::Dirty(FBoxSphereBounds const& Bounds)
|
|
|
|
|
{
|
|
|
|
|
// Transform world bounds into Virtual Texture UV space
|
|
|
|
|
const FVector O = Transform.GetTranslation();
|
|
|
|
|
const FVector U = Transform.GetUnitAxis(EAxis::X) * 1.f / Transform.GetScale3D().X;
|
|
|
|
|
const FVector V = Transform.GetUnitAxis(EAxis::Y) * 1.f / Transform.GetScale3D().Y;
|
|
|
|
|
const FVector P = Bounds.GetSphere().Center - O;
|
|
|
|
|
const FVector2D UVCenter = FVector2D(FVector::DotProduct(P, U), FVector::DotProduct(P, V));
|
|
|
|
|
const float Scale = FMath::Max(1.f / Transform.GetScale3D().X, 1.f / Transform.GetScale3D().Y);
|
|
|
|
|
const float UVRadius = Bounds.GetSphere().W * Scale;
|
|
|
|
|
const FVector2D UVExtent(UVRadius, UVRadius);
|
|
|
|
|
const FBox2D UVRect = FBox2D(UVCenter - UVExtent, UVCenter + UVExtent);
|
|
|
|
|
|
|
|
|
|
// Convert to Texel coordinate space
|
|
|
|
|
const FIntRect TextureRect(0, 0, VirtualTextureSize.X, VirtualTextureSize.Y);
|
|
|
|
|
FIntRect TexelRect(
|
|
|
|
|
FMath::FloorToInt(UVRect.Min.X * VirtualTextureSize.X),
|
|
|
|
|
FMath::FloorToInt(UVRect.Min.Y * VirtualTextureSize.Y),
|
|
|
|
|
FMath::CeilToInt(UVRect.Max.X * VirtualTextureSize.X),
|
|
|
|
|
FMath::CeilToInt(UVRect.Max.Y * VirtualTextureSize.Y));
|
|
|
|
|
TexelRect.Clip(TextureRect);
|
|
|
|
|
|
|
|
|
|
// Only add rect if it has some area
|
|
|
|
|
if (TexelRect.Min != TexelRect.Max)
|
|
|
|
|
{
|
|
|
|
|
const bool bFirst = DirtyRects.Add(TexelRect) == 0;
|
|
|
|
|
if (bFirst)
|
|
|
|
|
{
|
|
|
|
|
CombinedDirtyRect = TexelRect;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CombinedDirtyRect.Union(TexelRect);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FRuntimeVirtualTextureSceneProxy::FlushDirtyPages()
|
|
|
|
|
{
|
|
|
|
|
// If Producer handle is not initialized yet it's safe to do nothing because we won't have rendered anything to the VT that needs flushing.
|
|
|
|
|
if (ProducerHandle.PackedValue != 0)
|
|
|
|
|
{
|
2021-02-01 11:30:52 -04:00
|
|
|
// Don't do any work if we won't mark anything dirty.
|
2021-04-28 09:04:21 -04:00
|
|
|
if (MaxDirtyLevel >= 0 && CombinedDirtyRect.Width() != 0 && CombinedDirtyRect.Height() != 0)
|
2021-02-01 11:30:52 -04:00
|
|
|
{
|
|
|
|
|
//todo[vt]:
|
|
|
|
|
// Profile to work out best heuristic for when we should use the CombinedDirtyRect
|
|
|
|
|
// Also consider using some other structure to represent dirty area such as a course 2D bitfield
|
|
|
|
|
const bool bCombinedFlush = (DirtyRects.Num() > 2 || CombinedDirtyRect == FIntRect(0, 0, VirtualTextureSize.X, VirtualTextureSize.Y));
|
2019-08-08 23:35:07 -04:00
|
|
|
|
2021-02-01 11:30:52 -04:00
|
|
|
if (bCombinedFlush)
|
2019-08-08 23:35:07 -04:00
|
|
|
{
|
2022-02-02 07:39:44 -05:00
|
|
|
FVirtualTextureSystem::Get().FlushCache(ProducerHandle, SpaceID, CombinedDirtyRect, MaxDirtyLevel);
|
2021-02-01 11:30:52 -04:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (FIntRect Rect : DirtyRects)
|
|
|
|
|
{
|
2022-02-02 07:39:44 -05:00
|
|
|
FVirtualTextureSystem::Get().FlushCache(ProducerHandle, SpaceID, Rect, MaxDirtyLevel);
|
2021-02-01 11:30:52 -04:00
|
|
|
}
|
2019-08-08 23:35:07 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DirtyRects.Reset();
|
|
|
|
|
CombinedDirtyRect = FIntRect(0, 0, 0, 0);
|
|
|
|
|
}
|
2021-11-18 14:37:34 -05:00
|
|
|
|
|
|
|
|
#undef LOCTEXT_NAMESPACE
|