Skip to content

Commit

Permalink
Add MSAA to ModelSpinner
Browse files Browse the repository at this point in the history
- Fix invalid lighting data being inherited from previous render passes
- Add MSAA and resolve pass to ModelSpinner viewport rendering
  • Loading branch information
sturnclaw committed Sep 5, 2023
1 parent 58cc33f commit b31d192
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
15 changes: 7 additions & 8 deletions src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Intro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 26 additions & 4 deletions src/pigui/ModelSpinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>
Expand All @@ -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;
}

Expand All @@ -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));

Expand All @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/pigui/ModelSpinner.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace PiGui {

private:
std::unique_ptr<Graphics::RenderTarget> m_renderTarget;
std::unique_ptr<Graphics::RenderTarget> m_resolveTarget;
std::unique_ptr<SceneGraph::Model> m_model;
SceneGraph::ModelSkin m_skin;
std::unique_ptr<Shields> m_shields;
Expand Down

0 comments on commit b31d192

Please sign in to comment.