From b2ed544ee8b98ecb655fd12e0179c8035200edea Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Fri, 18 Mar 2022 22:47:52 -0700 Subject: [PATCH] More cleanups in PostProcessManager --- filament/src/PostProcessManager.cpp | 54 ++++++++++++----------------- filament/src/PostProcessManager.h | 14 +++++--- filament/src/details/Renderer.cpp | 10 +++--- 3 files changed, 35 insertions(+), 43 deletions(-) diff --git a/filament/src/PostProcessManager.cpp b/filament/src/PostProcessManager.cpp index e86e82e84b..97403da6a2 100644 --- a/filament/src/PostProcessManager.cpp +++ b/filament/src/PostProcessManager.cpp @@ -1136,12 +1136,10 @@ FrameGraphId PostProcessManager::gaussianBlurPass(FrameGraph& return output; } -FrameGraphId PostProcessManager::generateMipmapSSR(FrameGraph& fg, - FrameGraphId input, - const float verticalFieldOfView, - filament::Viewport const& svp, float2 scale, - TextureFormat format, - float* pLodOffset) const noexcept { +FrameGraphId PostProcessManager::generateMipmapSSR( + PostProcessManager& ppm, FrameGraph& fg, FrameGraphId input, + const float verticalFieldOfView, float2 scale, TextureFormat format, + float* pLodOffset) noexcept { // TODO: add an option to generate a 1/4 res texture (for performance) @@ -1257,48 +1255,40 @@ FrameGraphId PostProcessManager::generateMipmapSSR(FrameGraph // Then copy the color buffer into a texture, making sure that we keep the original // buffer aspect-ratio (this is because dynamic-resolution is not necessarily homogenous). - uint32_t w = svp.width; - uint32_t h = svp.height; + uint32_t w = desc.width; + uint32_t h = desc.height; if (scale.x != scale.y) { // dynamic resolution wasn't homogenous, which would affect the blur, so make sure to // keep an intermediary buffer that has the same aspect-ratio as the original. const float homogenousScale = std::sqrt(scale.x * scale.y); - w = uint32_t((homogenousScale / scale.x) * float(svp.width)); - h = uint32_t((homogenousScale / scale.y) * float(svp.height)); + w = uint32_t((homogenousScale / scale.x) * float(desc.width)); + h = uint32_t((homogenousScale / scale.y) * float(desc.height)); } - PostProcessManager& ppm = mEngine.getPostProcessManager(); - /* * Resolve if needed + copy the image into first LOD */ + FrameGraphId output; + const FrameGraphTexture::Descriptor outDesc{ + .width = w, .height = h, .depth = 1, + .levels = roughnessLodCount, + .type = SamplerType::SAMPLER_2D_ARRAY, + .format = format, + }; + if (desc.samples > 1 && - (w == svp.width && h == svp.height) && + (w == desc.width && h == desc.height) && desc.format == format) { // Here we can resolve directly into a texture with the right dimensions, level count and format // (resolve CANNOT scale or convert formats) - input = ppm.resolveBaseLevelNoCheck(fg, "Resolved Color Buffer", input, { - .width = w, - .height = h, - .depth = 1, - .levels = roughnessLodCount, - .type = SamplerType::SAMPLER_2D_ARRAY, - .format = format, - }); + output = ppm.resolveBaseLevelNoCheck(fg, "Resolved Color Buffer", input, outDesc); } else { // first resolve (if needed) - input = ppm.resolveBaseLevel(fg, "Resolved Color Buffer", input); + output = ppm.resolveBaseLevel(fg, "Resolved Color Buffer", input); // then blit into an appropriate texture // this handles scaling, format conversion and mipmaping - input = ppm.opaqueBlit(fg, input, { - .width = w, - .height = h, - .depth = 1, - .levels = roughnessLodCount, - .type = SamplerType::SAMPLER_2D_ARRAY, - .format = format, - }); + output = ppm.opaqueBlit(fg, output, outDesc); // Note: it's not possible to use the FrameGraph's forwardResource(), as an optimization // because the SSR buffer must be distinct from the color buffer (input here), because // we can't read and write into the same buffer, later in the refraction pass. @@ -1309,9 +1299,9 @@ FrameGraphId PostProcessManager::generateMipmapSSR(FrameGraph */ *pLodOffset = refractionLodOffset; - input = ppm.generateGaussianMipmap(fg, input, roughnessLodCount, + output = ppm.generateGaussianMipmap(fg, output, roughnessLodCount, true, kernelSize, sigma0); - return input; + return output; } FrameGraphId PostProcessManager::dof(FrameGraph& fg, diff --git a/filament/src/PostProcessManager.h b/filament/src/PostProcessManager.h index b019bfac65..989d20eacf 100644 --- a/filament/src/PostProcessManager.h +++ b/filament/src/PostProcessManager.h @@ -96,11 +96,15 @@ public: FrameGraphId input, size_t levels, bool reinhard, size_t kernelWidth, float sigma) noexcept; - FrameGraphId generateMipmapSSR(FrameGraph& fg, - FrameGraphId input, float verticalFieldOfView, - filament::Viewport const& svp, math::float2 scale, backend::TextureFormat format, - float* pLodOffset) const noexcept; - + // Helper to generate gaussian mipmaps for SSR (refraction and reflections). + // This performs the following tasks: + // - resolves input if needed + // - rescale input so it has a homogenous scale + // - generate a new texture with gaussian mips + static FrameGraphId generateMipmapSSR(PostProcessManager& ppm, + FrameGraph& fg, + FrameGraphId input, float verticalFieldOfView, math::float2 scale, + backend::TextureFormat format, float* pLodOffset) noexcept; // Depth-of-field FrameGraphId dof(FrameGraph& fg, FrameGraphId input, diff --git a/filament/src/details/Renderer.cpp b/filament/src/details/Renderer.cpp index e866f0a90e..7f10e68aea 100644 --- a/filament/src/details/Renderer.cpp +++ b/filament/src/details/Renderer.cpp @@ -485,9 +485,8 @@ void FRenderer::renderJob(ArenaScope& arena, FView& view) { { .width = svp.width, .height = svp.height }); // generate the mipchain - reflections = ppm.generateMipmapSSR(fg, reflections, - view.getCameraUser().getFieldOfView(Camera::Fov::VERTICAL), - config.svp, config.scale, + reflections = PostProcessManager::generateMipmapSSR(ppm, fg, reflections, + view.getCameraUser().getFieldOfView(Camera::Fov::VERTICAL), config.scale, TextureFormat::RGBA16F, &config.ssrLodOffset); @@ -759,9 +758,8 @@ FrameGraphId FRenderer::refractionPass(FrameGraph& fg, view); // generate the mipmap chain - input = ppm.generateMipmapSSR(fg, input, - view.getCameraUser().getFieldOfView(Camera::Fov::VERTICAL), - config.svp, config.scale, + input = PostProcessManager::generateMipmapSSR(ppm, fg, input, + view.getCameraUser().getFieldOfView(Camera::Fov::VERTICAL), config.scale, TextureFormat::R11F_G11F_B10F, &refractionLodOffset);