From b31d1929ef83dfe447448f0e80a7aea84406ae84 Mon Sep 17 00:00:00 2001 From: Webster Sheets Date: Tue, 5 Sep 2023 16:04:47 -0400 Subject: [PATCH] Add MSAA to ModelSpinner - Fix invalid lighting data being inherited from previous render passes - Add MSAA and resolve pass to ModelSpinner viewport rendering --- src/Camera.cpp | 15 +++++++-------- src/Intro.cpp | 3 +++ src/pigui/ModelSpinner.cpp | 30 ++++++++++++++++++++++++++---- src/pigui/ModelSpinner.h | 1 + 4 files changed, 37 insertions(+), 12 deletions(-) diff --git a/src/Camera.cpp b/src/Camera.cpp index ecc7fa01d0b..c8b3b6c8540 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -319,17 +319,16 @@ void Camera::Draw(const Body *excludeBody) continue; } - if (attrs->calcAtmosphereLighting) { - double ambient, direct; + double ambient = 0.05, direct = 1.0; + if (attrs->calcAtmosphereLighting) CalcLighting(attrs->body, ambient, direct); - for (size_t i = 0; i < m_lightSources.size(); i++) - lightIntensities[i] = direct * ShadowedIntensity(i, attrs->body); + for (size_t i = 0; i < m_lightSources.size(); i++) + lightIntensities[i] = direct * ShadowedIntensity(i, attrs->body); - // Setup dynamic lighting parameters - m_renderer->SetAmbientColor(Color(ambient * 255, ambient * 255, ambient * 255)); - m_renderer->SetLightIntensity(m_lightSources.size(), lightIntensities.data()); - } + // Setup dynamic lighting parameters + m_renderer->SetAmbientColor(Color(ambient * 255, ambient * 255, ambient * 255)); + m_renderer->SetLightIntensity(m_lightSources.size(), lightIntensities.data()); attrs->body->Render(m_renderer, this, attrs->viewCoords, attrs->viewTransform); } diff --git a/src/Intro.cpp b/src/Intro.cpp index 5552ea81a62..5b9a8443744 100644 --- a/src/Intro.cpp +++ b/src/Intro.cpp @@ -150,7 +150,10 @@ void Intro::Draw(float deltaTime) m_renderer->SetTransform(matrix4x4f::Identity()); m_renderer->SetAmbientColor(m_ambientColor); + + float intensity[4] = { 1.f, 1.f, 1.f, 1.f }; m_renderer->SetLights(m_lights.size(), &m_lights[0]); + m_renderer->SetLightIntensity(4, intensity); // XXX all this stuff will be gone when intro uses a Camera // rotate background by time, and a bit extra Z so it's not so flat diff --git a/src/pigui/ModelSpinner.cpp b/src/pigui/ModelSpinner.cpp index 71c0545fa13..5686872d7c7 100644 --- a/src/pigui/ModelSpinner.cpp +++ b/src/pigui/ModelSpinner.cpp @@ -8,6 +8,7 @@ #include "graphics/Graphics.h" #include "graphics/RenderTarget.h" #include "graphics/Renderer.h" +#include "graphics/Texture.h" #include "scenegraph/Tag.h" #include @@ -32,16 +33,29 @@ void ModelSpinner::CreateRenderTarget() { if (m_renderTarget) m_renderTarget.reset(); + if (m_resolveTarget) + m_resolveTarget.reset(); Graphics::RenderTargetDesc rtDesc{ uint16_t(m_size.x), uint16_t(m_size.y), Graphics::TextureFormat::TEXTURE_RGBA_8888, - Graphics::TextureFormat::TEXTURE_DEPTH, true + Graphics::TextureFormat::TEXTURE_DEPTH, true, + uint16_t(Pi::GetApp()->GetGraphicsSettings().requestedSamples) }; m_renderTarget.reset(Pi::renderer->CreateRenderTarget(rtDesc)); if (!m_renderTarget) Error("Error creating render target for model viewer."); + + Graphics::RenderTargetDesc resolveDesc{ + uint16_t(m_size.x), uint16_t(m_size.y), + Graphics::TextureFormat::TEXTURE_RGBA_8888, + Graphics::TextureFormat::TEXTURE_NONE, true + }; + + m_resolveTarget.reset(Pi::renderer->CreateRenderTarget(resolveDesc)); + if (!m_resolveTarget) Error("Error creating MSAA resolve render target for model viewer."); + m_needsResize = false; } @@ -66,9 +80,15 @@ void ModelSpinner::Render() Graphics::Renderer *r = Pi::renderer; Graphics::Renderer::StateTicket ticket(r); - r->SetRenderTarget(m_renderTarget.get()); const auto &desc = m_renderTarget.get()->GetDesc(); - r->SetViewport({ 0, 0, desc.width, desc.height }); + Graphics::ViewportExtents extents = { 0, 0, desc.width, desc.height }; + + r->SetRenderTarget(m_renderTarget.get()); + r->SetViewport(extents); + + float lightIntensity[4] = { 0.75f, 0.f, 0.f, 0.f }; + r->SetLightIntensity(4, lightIntensity); + r->SetAmbientColor(Color(64, 64, 64)); r->ClearScreen(Color(0, 0, 0, 0)); @@ -78,6 +98,8 @@ void ModelSpinner::Render() r->SetLights(1, &m_light); AnimationCurves::Approach(m_zoom, m_zoomTo, Pi::GetFrameTime(), 5.0f, 0.4f); m_model->Render(MakeModelViewMat()); + + r->ResolveRenderTarget(m_renderTarget.get(), m_resolveTarget.get(), extents); } void ModelSpinner::SetSize(vector2d size) @@ -108,7 +130,7 @@ void ModelSpinner::DrawPiGui() if (m_renderTarget) { // Draw the image and stretch it over the available region. // ImGui inverts the vertical axis to get top-left coordinates, so we need to invert our UVs to match. - ImGui::Image(m_renderTarget->GetColorTexture(), size, ImVec2(0, 1), ImVec2(1, 0)); + ImGui::Image(m_resolveTarget->GetColorTexture(), size, ImVec2(0, 1), ImVec2(1, 0)); } else { ImGui::Dummy(size); } diff --git a/src/pigui/ModelSpinner.h b/src/pigui/ModelSpinner.h index c3df02254dc..ecd8006147a 100644 --- a/src/pigui/ModelSpinner.h +++ b/src/pigui/ModelSpinner.h @@ -52,6 +52,7 @@ namespace PiGui { private: std::unique_ptr m_renderTarget; + std::unique_ptr m_resolveTarget; std::unique_ptr m_model; SceneGraph::ModelSkin m_skin; std::unique_ptr m_shields;