Add support for using per-texture group sampler in Materials

This commit is contained in:
Wojtek Figat
2021-06-29 16:16:56 +02:00
parent add88a783b
commit a3dfb1c5d3
18 changed files with 252 additions and 45 deletions

View File

@@ -9,6 +9,7 @@
#include "Engine/Threading/Threading.h"
#include "Engine/Threading/Task.h"
#include "Engine/Graphics/GPUDevice.h"
#include "Engine/Graphics/Textures/GPUSampler.h"
#include "Engine/Serialization/Serialization.h"
namespace StreamingManagerImpl
@@ -17,6 +18,8 @@ namespace StreamingManagerImpl
int32 LastUpdateResourcesIndex = 0;
CriticalSection ResourcesLock;
Array<StreamableResource*> Resources;
Array<GPUSampler*, InlinedAllocation<32>> TextureGroupSamplers;
GPUSampler* FallbackSampler = nullptr;
}
using namespace StreamingManagerImpl;
@@ -30,6 +33,7 @@ public:
}
void Update() override;
void BeforeExit() override;
};
StreamingManagerService StreamingManagerServiceInstance;
@@ -39,6 +43,8 @@ Array<TextureGroup, InlinedAllocation<32>> Streaming::TextureGroups;
void StreamingSettings::Apply()
{
Streaming::TextureGroups = TextureGroups;
SAFE_DELETE_GPU_RESOURCES(TextureGroupSamplers);
TextureGroupSamplers.Resize(TextureGroups.Count(), false);
}
void StreamingSettings::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier)
@@ -217,6 +223,13 @@ void StreamingManagerService::Update()
// TODO: add StreamingManager stats, update time per frame, updates per frame, etc.
}
void StreamingManagerService::BeforeExit()
{
SAFE_DELETE_GPU_RESOURCE(FallbackSampler);
SAFE_DELETE_GPU_RESOURCES(TextureGroupSamplers);
TextureGroupSamplers.Resize(0);
}
void Streaming::RequestStreamingUpdate()
{
PROFILE_CPU();
@@ -225,3 +238,35 @@ void Streaming::RequestStreamingUpdate()
e->RequestStreamingUpdate();
ResourcesLock.Unlock();
}
GPUSampler* Streaming::GetTextureGroupSampler(int32 index)
{
GPUSampler* sampler = nullptr;
if (index >= 0 && index < TextureGroupSamplers.Count())
{
// Sampler from texture group options
auto& group = TextureGroups[index];
auto desc = GPUSamplerDescription::New(group.SamplerFilter);
desc.MaxAnisotropy = group.MaxAnisotropy;
sampler = TextureGroupSamplers[index];
if (!sampler)
{
sampler = GPUSampler::New();
sampler->Init(desc);
TextureGroupSamplers[index] = sampler;
}
if (sampler->GetDescription().Filter != desc.Filter || sampler->GetDescription().MaxAnisotropy != desc.MaxAnisotropy)
sampler->Init(desc);
}
if (!sampler)
{
// Default sampler to prevent issue
if (!FallbackSampler)
{
FallbackSampler = GPUSampler::New();
FallbackSampler->Init(GPUSamplerDescription::New(GPUSamplerFilter::Trilinear));
}
sampler = FallbackSampler;
}
return sampler;
}