2019-12-26 15:33:43 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
2019-08-21 08:42:46 -04:00
|
|
|
|
|
|
|
|
#include "RuntimeVirtualTextureThumbnailRenderer.h"
|
|
|
|
|
|
|
|
|
|
#include "Components/RuntimeVirtualTextureComponent.h"
|
2023-01-26 21:41:50 -05:00
|
|
|
#include "Engine/World.h"
|
2022-12-16 16:24:02 -05:00
|
|
|
#include "Materials/MaterialRenderProxy.h"
|
2019-09-02 12:38:54 -04:00
|
|
|
#include "MaterialShared.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
#include "Math/Box.h"
|
|
|
|
|
#include "Math/Box2D.h"
|
|
|
|
|
#include "Math/BoxSphereBounds.h"
|
|
|
|
|
#include "Math/Transform.h"
|
|
|
|
|
#include "Math/UnrealMathSSE.h"
|
|
|
|
|
#include "Math/Vector2D.h"
|
|
|
|
|
#include "Misc/AssertionMacros.h"
|
|
|
|
|
#include "PixelFormat.h"
|
|
|
|
|
#include "RHI.h"
|
2020-10-26 16:44:44 -04:00
|
|
|
#include "RenderGraphBuilder.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
#include "RenderingThread.h"
|
2019-08-21 09:38:23 -04:00
|
|
|
#include "SceneInterface.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
#include "Templates/Casts.h"
|
|
|
|
|
#include "UObject/Object.h"
|
2019-08-21 09:38:23 -04:00
|
|
|
#include "UObject/UObjectIterator.h"
|
2022-08-24 22:45:13 -04:00
|
|
|
#include "UnrealClient.h"
|
|
|
|
|
#include "VT/RuntimeVirtualTexture.h"
|
|
|
|
|
#include "VT/RuntimeVirtualTextureEnum.h"
|
|
|
|
|
#include "VT/RuntimeVirtualTextureRender.h"
|
|
|
|
|
#include "VirtualTexturing.h"
|
|
|
|
|
|
|
|
|
|
class FCanvas;
|
|
|
|
|
class FRHICommandListImmediate;
|
2019-08-21 08:42:46 -04:00
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
/** Find a matching component for this URuntimeVirtualTexture. */
|
|
|
|
|
URuntimeVirtualTextureComponent* FindComponent(URuntimeVirtualTexture* RuntimeVirtualTexture)
|
|
|
|
|
{
|
|
|
|
|
for (TObjectIterator<URuntimeVirtualTextureComponent> It; It; ++It)
|
|
|
|
|
{
|
|
|
|
|
URuntimeVirtualTextureComponent* RuntimeVirtualTextureComponent = *It;
|
|
|
|
|
if (RuntimeVirtualTextureComponent->GetVirtualTexture() == RuntimeVirtualTexture)
|
|
|
|
|
{
|
|
|
|
|
return RuntimeVirtualTextureComponent;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
URuntimeVirtualTextureThumbnailRenderer::URuntimeVirtualTextureThumbnailRenderer(const FObjectInitializer& ObjectInitializer)
|
|
|
|
|
: Super(ObjectInitializer)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool URuntimeVirtualTextureThumbnailRenderer::CanVisualizeAsset(UObject* Object)
|
|
|
|
|
{
|
|
|
|
|
URuntimeVirtualTexture* RuntimeVirtualTexture = Cast<URuntimeVirtualTexture>(Object);
|
2020-09-24 00:43:27 -04:00
|
|
|
|
|
|
|
|
// We need a matching URuntimeVirtualTextureComponent in a Scene to be able to render a thumbnail
|
|
|
|
|
URuntimeVirtualTextureComponent* RuntimeVirtualTextureComponent = FindComponent(RuntimeVirtualTexture);
|
2021-09-02 15:43:08 -04:00
|
|
|
if (RuntimeVirtualTextureComponent != nullptr && RuntimeVirtualTextureComponent->GetScene() != nullptr)
|
2019-08-21 08:42:46 -04:00
|
|
|
{
|
2021-09-02 15:43:08 -04:00
|
|
|
return true;
|
2019-08-21 08:42:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 10:59:27 -05:00
|
|
|
void URuntimeVirtualTextureThumbnailRenderer::Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height, FRenderTarget* RenderTarget, FCanvas* Canvas, bool bAdditionalViewFamily)
|
2019-08-21 08:42:46 -04:00
|
|
|
{
|
2020-02-04 22:17:14 -05:00
|
|
|
//todo[vt]: Handle case where a null or floating point render target is passed in. (This happens on package save.)
|
|
|
|
|
if (RenderTarget->GetRenderTargetTexture() == nullptr || RenderTarget->GetRenderTargetTexture()->GetFormat() != PF_B8G8R8A8)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 08:42:46 -04:00
|
|
|
URuntimeVirtualTexture* RuntimeVirtualTexture = Cast<URuntimeVirtualTexture>(Object);
|
|
|
|
|
URuntimeVirtualTextureComponent* RuntimeVirtualTextureComponent = FindComponent(RuntimeVirtualTexture);
|
|
|
|
|
FSceneInterface* Scene = RuntimeVirtualTextureComponent != nullptr ? RuntimeVirtualTextureComponent->GetScene() : nullptr;
|
|
|
|
|
check(Scene != nullptr);
|
|
|
|
|
|
2023-01-26 15:39:08 -05:00
|
|
|
if (UWorld* World = Scene->GetWorld())
|
|
|
|
|
{
|
|
|
|
|
World->SendAllEndOfFrameUpdates();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 16:50:18 -04:00
|
|
|
const FBox2D DestBox = FBox2D(FVector2D(X, Y), FVector2D(Width, Height));
|
2020-06-23 18:40:00 -04:00
|
|
|
const FTransform Transform = RuntimeVirtualTextureComponent->GetComponentTransform();
|
2020-03-17 23:19:30 -04:00
|
|
|
const FBox Bounds = RuntimeVirtualTextureComponent->Bounds.GetBox();
|
2019-08-21 08:42:46 -04:00
|
|
|
const uint32 VirtualTextureSceneIndex = RuntimeVirtualTexture::GetRuntimeVirtualTextureSceneIndex_GameThread(RuntimeVirtualTextureComponent);
|
|
|
|
|
const ERuntimeVirtualTextureMaterialType MaterialType = RuntimeVirtualTexture->GetMaterialType();
|
|
|
|
|
|
|
|
|
|
FVTProducerDescription VTDesc;
|
2020-09-24 00:43:27 -04:00
|
|
|
RuntimeVirtualTexture->GetProducerDescription(VTDesc, URuntimeVirtualTexture::FInitSettings(), Transform);
|
2019-08-21 08:42:46 -04:00
|
|
|
const int32 MaxLevel = (int32)FMath::CeilLogTwo(FMath::Max(VTDesc.BlockWidthInTiles, VTDesc.BlockHeightInTiles));
|
|
|
|
|
|
2023-08-14 13:39:43 -04:00
|
|
|
UE::RenderCommandPipe::FSyncScope SyncScope;
|
|
|
|
|
|
2019-08-21 08:42:46 -04:00
|
|
|
ENQUEUE_RENDER_COMMAND(BakeStreamingTextureTileCommand)(
|
2020-03-17 23:19:30 -04:00
|
|
|
[Scene, VirtualTextureSceneIndex, MaterialType, RenderTarget, DestBox, Transform, Bounds, MaxLevel](FRHICommandListImmediate& RHICmdList)
|
2019-08-21 08:42:46 -04:00
|
|
|
{
|
2019-09-02 12:38:54 -04:00
|
|
|
FMaterialRenderProxy::UpdateDeferredCachedUniformExpressions();
|
|
|
|
|
|
2021-01-14 20:15:23 -04:00
|
|
|
FRDGBuilder GraphBuilder(RHICmdList);
|
|
|
|
|
|
2019-09-29 16:50:18 -04:00
|
|
|
RuntimeVirtualTexture::FRenderPageBatchDesc Desc;
|
|
|
|
|
Desc.Scene = Scene->GetRenderScene();
|
|
|
|
|
Desc.RuntimeVirtualTextureMask = 1 << VirtualTextureSceneIndex;
|
|
|
|
|
Desc.UVToWorld = Transform;
|
2020-03-17 23:19:30 -04:00
|
|
|
Desc.WorldBounds = Bounds;
|
2019-09-29 16:50:18 -04:00
|
|
|
Desc.MaterialType = MaterialType;
|
2024-04-15 19:31:17 -04:00
|
|
|
Desc.MaxLevel = IntCastChecked<uint8>(MaxLevel);
|
2019-09-29 16:50:18 -04:00
|
|
|
Desc.bClearTextures = true;
|
2019-10-31 16:06:30 -04:00
|
|
|
Desc.bIsThumbnails = true;
|
2024-01-10 16:32:21 -05:00
|
|
|
Desc.FixedColor = FLinearColor::Transparent;
|
2019-09-29 16:50:18 -04:00
|
|
|
Desc.NumPageDescs = 1;
|
2019-09-29 17:29:22 -04:00
|
|
|
Desc.Targets[0].Texture = RenderTarget->GetRenderTargetTexture();
|
2024-05-09 10:05:44 -04:00
|
|
|
// The target is in RTV state and should be left in RTV state (RenderPage needs the input/output state) :
|
|
|
|
|
Desc.Targets[0].TextureAccessBefore = ERHIAccess::RTV;
|
|
|
|
|
Desc.Targets[0].TextureAccessAfter = ERHIAccess::RTV;
|
2019-09-29 16:50:18 -04:00
|
|
|
Desc.PageDescs[0].DestBox[0] = DestBox;
|
|
|
|
|
Desc.PageDescs[0].UVRange = FBox2D(FVector2D(0, 0), FVector2D(1, 1));
|
2024-04-15 19:31:17 -04:00
|
|
|
Desc.PageDescs[0].vLevel = IntCastChecked<uint8>(MaxLevel);
|
2019-09-29 16:50:18 -04:00
|
|
|
|
2021-01-14 20:15:23 -04:00
|
|
|
RuntimeVirtualTexture::RenderPagesStandAlone(GraphBuilder, Desc);
|
|
|
|
|
|
|
|
|
|
GraphBuilder.Execute();
|
2019-08-21 08:42:46 -04:00
|
|
|
});
|
|
|
|
|
}
|