Add GlobalSurfaceAtlas pass to Renderer (wip)
This commit is contained in:
@@ -854,9 +854,14 @@ API_ENUM() enum class ViewMode
|
||||
QuadOverdraw = 23,
|
||||
|
||||
/// <summary>
|
||||
/// Draw global Sign Distant Field (SDF) preview.
|
||||
/// Draw Global Sign Distant Field (SDF) preview.
|
||||
/// </summary>
|
||||
GlobalSDF = 24,
|
||||
|
||||
/// <summary>
|
||||
/// Draw Global Surface Atlas preview.
|
||||
/// </summary>
|
||||
GlobalSurfaceAtlas = 25,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
|
||||
170
Source/Engine/Renderer/GlobalSurfaceAtlasPass.cpp
Normal file
170
Source/Engine/Renderer/GlobalSurfaceAtlasPass.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
|
||||
|
||||
#include "GlobalSurfaceAtlasPass.h"
|
||||
#include "GlobalSignDistanceFieldPass.h"
|
||||
#include "RenderList.h"
|
||||
#include "Engine/Engine/Engine.h"
|
||||
#include "Engine/Content/Content.h"
|
||||
#include "Engine/Graphics/GPUDevice.h"
|
||||
#include "Engine/Graphics/RenderTask.h"
|
||||
#include "Engine/Graphics/RenderBuffers.h"
|
||||
#include "Engine/Graphics/RenderTargetPool.h"
|
||||
#include "Engine/Graphics/Shaders/GPUShader.h"
|
||||
|
||||
PACK_STRUCT(struct Data0
|
||||
{
|
||||
Vector3 ViewWorldPos;
|
||||
float ViewNearPlane;
|
||||
Vector3 Padding00;
|
||||
float ViewFarPlane;
|
||||
Vector4 ViewFrustumWorldRays[4];
|
||||
GlobalSignDistanceFieldPass::GlobalSDFData GlobalSDF;
|
||||
});
|
||||
|
||||
class GlobalSurfaceAtlasCustomBuffer : public RenderBuffers::CustomBuffer
|
||||
{
|
||||
public:
|
||||
GPUTexture* Dummy = nullptr; // TODO use some actual atlas textures
|
||||
GlobalSurfaceAtlasPass::BindingData Result;
|
||||
|
||||
~GlobalSurfaceAtlasCustomBuffer()
|
||||
{
|
||||
RenderTargetPool::Release(Dummy);
|
||||
}
|
||||
};
|
||||
|
||||
String GlobalSurfaceAtlasPass::ToString() const
|
||||
{
|
||||
return TEXT("GlobalSurfaceAtlasPass");
|
||||
}
|
||||
|
||||
bool GlobalSurfaceAtlasPass::Init()
|
||||
{
|
||||
// Check platform support
|
||||
const auto device = GPUDevice::Instance;
|
||||
_supported = device->GetFeatureLevel() >= FeatureLevel::SM5 && device->Limits.HasCompute && device->Limits.HasTypedUAVLoad;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GlobalSurfaceAtlasPass::setupResources()
|
||||
{
|
||||
// Load shader
|
||||
if (!_shader)
|
||||
{
|
||||
_shader = Content::LoadAsyncInternal<Shader>(TEXT("Shaders/GlobalSurfaceAtlas"));
|
||||
if (_shader == nullptr)
|
||||
return true;
|
||||
#if COMPILE_WITH_DEV_ENV
|
||||
_shader.Get()->OnReloading.Bind<GlobalSurfaceAtlasPass, &GlobalSurfaceAtlasPass::OnShaderReloading>(this);
|
||||
#endif
|
||||
}
|
||||
if (!_shader->IsLoaded())
|
||||
return true;
|
||||
|
||||
const auto device = GPUDevice::Instance;
|
||||
const auto shader = _shader->GetShader();
|
||||
_cb0 = shader->GetCB(0);
|
||||
|
||||
// Create pipeline state
|
||||
GPUPipelineState::Description psDesc = GPUPipelineState::Description::DefaultFullscreenTriangle;
|
||||
if (!_psDebug)
|
||||
{
|
||||
_psDebug = device->CreatePipelineState();
|
||||
psDesc.PS = shader->GetPS("PS_Debug");
|
||||
if (_psDebug->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#if COMPILE_WITH_DEV_ENV
|
||||
|
||||
void GlobalSurfaceAtlasPass::OnShaderReloading(Asset* obj)
|
||||
{
|
||||
SAFE_DELETE_GPU_RESOURCE(_psDebug);
|
||||
invalidateResources();
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void GlobalSurfaceAtlasPass::Dispose()
|
||||
{
|
||||
RendererPass::Dispose();
|
||||
|
||||
// Cleanup
|
||||
SAFE_DELETE_GPU_RESOURCE(_psDebug);
|
||||
_cb0 = nullptr;
|
||||
_shader = nullptr;
|
||||
}
|
||||
|
||||
bool GlobalSurfaceAtlasPass::Render(RenderContext& renderContext, GPUContext* context, BindingData& result)
|
||||
{
|
||||
// Skip if not supported
|
||||
if (setupResources())
|
||||
return true;
|
||||
if (renderContext.List->Scenes.Count() == 0)
|
||||
return true;
|
||||
auto& surfaceAtlasData = *renderContext.Buffers->GetCustomBuffer<GlobalSurfaceAtlasCustomBuffer>(TEXT("GlobalSurfaceAtlas"));
|
||||
|
||||
// Skip if already done in the current frame
|
||||
const auto currentFrame = Engine::FrameCount;
|
||||
if (surfaceAtlasData.LastFrameUsed == currentFrame)
|
||||
{
|
||||
result = surfaceAtlasData.Result;
|
||||
return false;
|
||||
}
|
||||
|
||||
PROFILE_GPU_CPU("Global Surface Atlas");
|
||||
|
||||
return false;
|
||||
// TODO: configurable via graphics settings
|
||||
const int32 resolution = 4096;
|
||||
// TODO: configurable via postFx settings (maybe use Global SDF distance?)
|
||||
const float distance = 20000;
|
||||
|
||||
// TODO: Initialize buffers
|
||||
surfaceAtlasData.LastFrameUsed = currentFrame;
|
||||
|
||||
// TODO: Rasterize world geometry into Global Surface Atlas
|
||||
|
||||
// Copy results
|
||||
result.Dummy = surfaceAtlasData.Dummy;
|
||||
surfaceAtlasData.Result = result;
|
||||
return false;
|
||||
}
|
||||
|
||||
void GlobalSurfaceAtlasPass::RenderDebug(RenderContext& renderContext, GPUContext* context, GPUTexture* output)
|
||||
{
|
||||
GlobalSignDistanceFieldPass::BindingData bindingDataSDF;
|
||||
BindingData bindingData;
|
||||
if (GlobalSignDistanceFieldPass::Instance()->Render(renderContext, context, bindingDataSDF) || Render(renderContext, context, bindingData))
|
||||
{
|
||||
context->Draw(output, renderContext.Buffers->GBuffer0);
|
||||
return;
|
||||
}
|
||||
|
||||
PROFILE_GPU_CPU("Global Surface Atlas Debug");
|
||||
const Vector2 outputSize(output->Size());
|
||||
if (_cb0)
|
||||
{
|
||||
Data0 data;
|
||||
data.ViewWorldPos = renderContext.View.Position;
|
||||
data.ViewNearPlane = renderContext.View.Near;
|
||||
data.ViewFarPlane = renderContext.View.Far;
|
||||
for (int32 i = 0; i < 4; i++)
|
||||
data.ViewFrustumWorldRays[i] = Vector4(renderContext.List->FrustumCornersWs[i + 4], 0);
|
||||
data.GlobalSDF = bindingDataSDF.GlobalSDF;
|
||||
context->UpdateCB(_cb0, &data);
|
||||
context->BindCB(0, _cb0);
|
||||
}
|
||||
for (int32 i = 0; i < 4; i++)
|
||||
{
|
||||
context->BindSR(i, bindingDataSDF.Cascades[i]->ViewVolume());
|
||||
context->BindSR(i + 4, bindingDataSDF.CascadeMips[i]->ViewVolume());
|
||||
}
|
||||
context->SetState(_psDebug);
|
||||
context->SetRenderTarget(output->View());
|
||||
context->SetViewportAndScissors(outputSize.X, outputSize.Y);
|
||||
context->DrawFullscreenTriangle();
|
||||
}
|
||||
57
Source/Engine/Renderer/GlobalSurfaceAtlasPass.h
Normal file
57
Source/Engine/Renderer/GlobalSurfaceAtlasPass.h
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "RendererPass.h"
|
||||
|
||||
/// <summary>
|
||||
/// Global Surface Atlas rendering pass. Captures scene geometry into a single atlas texture which contains surface diffuse color, normal vector, emission light, and calculates direct+indirect lighting. Used by Global Illumination and Reflections.
|
||||
/// </summary>
|
||||
class FLAXENGINE_API GlobalSurfaceAtlasPass : public RendererPass<GlobalSurfaceAtlasPass>
|
||||
{
|
||||
public:
|
||||
// Binding data for the GPU.
|
||||
struct BindingData
|
||||
{
|
||||
GPUTexture* Dummy; // TODO: add textures
|
||||
};
|
||||
|
||||
private:
|
||||
bool _supported = false;
|
||||
AssetReference<Shader> _shader;
|
||||
GPUPipelineState* _psDebug = nullptr;
|
||||
GPUConstantBuffer* _cb0 = nullptr;
|
||||
|
||||
public:
|
||||
/// <summary>
|
||||
/// Renders the Global Surface Atlas.
|
||||
/// </summary>
|
||||
/// <param name="renderContext">The rendering context.</param>
|
||||
/// <param name="context">The GPU context.</param>
|
||||
/// <param name="result">The result Global Surface Atlas data for binding to the shaders.</param>
|
||||
/// <returns>True if failed to render (platform doesn't support it, out of video memory, disabled feature or effect is not ready), otherwise false.</returns>
|
||||
bool Render(RenderContext& renderContext, GPUContext* context, BindingData& result);
|
||||
|
||||
/// <summary>
|
||||
/// Renders the debug view.
|
||||
/// </summary>
|
||||
/// <param name="renderContext">The rendering context.</param>
|
||||
/// <param name="context">The GPU context.</param>
|
||||
/// <param name="output">The output buffer.</param>
|
||||
void RenderDebug(RenderContext& renderContext, GPUContext* context, GPUTexture* output);
|
||||
|
||||
private:
|
||||
#if COMPILE_WITH_DEV_ENV
|
||||
void OnShaderReloading(Asset* obj);
|
||||
#endif
|
||||
|
||||
public:
|
||||
// [RendererPass]
|
||||
String ToString() const override;
|
||||
bool Init() override;
|
||||
void Dispose() override;
|
||||
|
||||
protected:
|
||||
// [RendererPass]
|
||||
bool setupResources() override;
|
||||
};
|
||||
@@ -22,6 +22,7 @@
|
||||
#include "HistogramPass.h"
|
||||
#include "AtmospherePreCompute.h"
|
||||
#include "GlobalSignDistanceFieldPass.h"
|
||||
#include "GlobalSurfaceAtlasPass.h"
|
||||
#include "Utils/MultiScaler.h"
|
||||
#include "Utils/BitonicSort.h"
|
||||
#include "AntiAliasing/FXAA.h"
|
||||
@@ -83,6 +84,7 @@ bool RendererService::Init()
|
||||
PassList.Add(SMAA::Instance());
|
||||
PassList.Add(HistogramPass::Instance());
|
||||
PassList.Add(GlobalSignDistanceFieldPass::Instance());
|
||||
PassList.Add(GlobalSurfaceAtlasPass::Instance());
|
||||
#if USE_EDITOR
|
||||
PassList.Add(QuadOverdrawPass::Instance());
|
||||
#endif
|
||||
@@ -352,11 +354,12 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext)
|
||||
|
||||
// Debug drawing
|
||||
if (renderContext.View.Mode == ViewMode::GlobalSDF)
|
||||
{
|
||||
GlobalSignDistanceFieldPass::Instance()->RenderDebug(renderContext, context, lightBuffer);
|
||||
}
|
||||
if (renderContext.View.Mode == ViewMode::Emissive ||
|
||||
renderContext.View.Mode == ViewMode::LightmapUVsDensity ||
|
||||
else if (renderContext.View.Mode == ViewMode::GlobalSurfaceAtlas)
|
||||
GlobalSurfaceAtlasPass::Instance()->RenderDebug(renderContext, context, lightBuffer);
|
||||
if (renderContext.View.Mode == ViewMode::Emissive ||
|
||||
renderContext.View.Mode == ViewMode::LightmapUVsDensity ||
|
||||
renderContext.View.Mode == ViewMode::GlobalSurfaceAtlas ||
|
||||
renderContext.View.Mode == ViewMode::GlobalSDF)
|
||||
{
|
||||
context->ResetRenderTarget();
|
||||
|
||||
Reference in New Issue
Block a user