Skip to content

Commit

Permalink
Remove implicit window render target from Renderer
Browse files Browse the repository at this point in the history
- GuiApplication is now responsible for creating the MSAA backbuffer target for rendering.
- Graphics::Renderer::StateTicket resets render target + viewport state on cleanup
- Removed Renderer::SetClearColor et al, clears are expected to provide an explicit clear color.
- Active render target state is still messy; we want to reduce overall state tracking.
- GuiApplication can own the window backbuffer but a GBuffer/color buffer should be owned by some scene-rendering related facility.
  • Loading branch information
sturnclaw committed Sep 5, 2023
1 parent 77cbd16 commit 00372bb
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 153 deletions.
1 change: 1 addition & 0 deletions src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "galaxy/StarSystem.h"
#include "graphics/TextureBuilder.h"
#include "graphics/Types.h"
#include "graphics/RenderState.h"

using namespace Graphics;

Expand Down
12 changes: 3 additions & 9 deletions src/GasGiant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,14 +832,8 @@ void GasGiant::SetRenderTargetCubemap(const Uint32 face, Graphics::Texture *pTex
s_renderTarget->SetCubeFaceTexture(face, pTexture);
}

//static
void GasGiant::BeginRenderTarget()
{
Pi::renderer->SetRenderTarget(s_renderTarget);
}

//static
void GasGiant::EndRenderTarget()
// static
Graphics::RenderTarget *GasGiant::GetRenderTarget()
{
Pi::renderer->SetRenderTarget(nullptr);
return s_renderTarget;
}
1 change: 1 addition & 0 deletions src/GasGiant.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class GasGiant : public BaseSphere {

static void CreateRenderTarget(const Uint16 width, const Uint16 height);
static void SetRenderTargetCubemap(const Uint32, Graphics::Texture *, const bool unBind = true);
static Graphics::RenderTarget *GetRenderTarget();
static void BeginRenderTarget();
static void EndRenderTarget();

Expand Down
5 changes: 3 additions & 2 deletions src/GasGiantJobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ namespace GasGiantJobs {
Pi::renderer->SetOrthographicProjection(0, mData->UVDims(), mData->UVDims(), 0, -1, 1);
Pi::renderer->SetTransform(matrix4x4f::Identity());

GasGiant::BeginRenderTarget();
// render to offscreen rt
Pi::renderer->SetRenderTarget(GasGiant::GetRenderTarget());

for (Uint32 iFace = 0; iFace < NUM_PATCHES; iFace++) {
// render the scene
GasGiant::SetRenderTargetCubemap(iFace, mData->Texture());
Expand All @@ -277,7 +279,6 @@ namespace GasGiantJobs {
// FIXME: use different render targets for each cubemap face
Pi::renderer->FlushCommandBuffers();
}
GasGiant::EndRenderTarget();

// add this patches data
SGPUGenResult *sr = new SGPUGenResult();
Expand Down
1 change: 1 addition & 0 deletions src/GasGiantJobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "JobQueue.h"
#include "graphics/Material.h"
#include "graphics/VertexBuffer.h"
#include "graphics/RenderState.h"
#include "profiler/Profiler.h"
#include "terrain/Terrain.h"
#include "vector3.h"
Expand Down
1 change: 0 additions & 1 deletion src/Pi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,6 @@ void MainMenu::Update(float deltaTime)

Pi::intro->Draw(deltaTime);

Pi::renderer->SetRenderTarget(0);
Pi::pigui->NewFrame();
PiGui::RunHandler(deltaTime, "mainMenu");

Expand Down
15 changes: 8 additions & 7 deletions src/SectorMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,18 @@ void SectorMap::DrawEmbed()
ImGui::Image(m_renderTarget->GetColorTexture(), m_size, ImVec2(0, 1), ImVec2(1, 0));

auto *r = m_context.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 });

r->SetClearColor(Color(0, 0, 0, 255));
{
// state ticket resets all draw state at the end of the scope
Graphics::Renderer::StateTicket ticket(r);

Draw3D();
DrawLabels(ImGui::IsItemHovered(), imagePos);
r->SetRenderTarget(m_renderTarget.get());
r->SetViewport({ 0, 0, desc.width, desc.height });

r->SetRenderTarget(nullptr);
Draw3D();
DrawLabels(ImGui::IsItemHovered(), imagePos);
}

if (ImGui::IsItemHovered()) {
ImGui::CaptureMouseFromApp(false);
Expand Down
1 change: 1 addition & 0 deletions src/Star.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "galaxy/SystemBody.h"
#include "graphics/Material.h"
#include "graphics/Renderer.h"
#include "graphics/RenderState.h"
#include "graphics/Types.h"
#include "graphics/VertexArray.h"
#include "graphics/VertexBuffer.h"
Expand Down
1 change: 1 addition & 0 deletions src/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "graphics/Graphics.h"
#include "graphics/Material.h"
#include "graphics/Renderer.h"
#include "graphics/RenderState.h"
#include "graphics/TextureBuilder.h"
#include "graphics/Types.h"

Expand Down
3 changes: 1 addition & 2 deletions src/Tombstone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ Tombstone::Tombstone(Graphics::Renderer *r, int width, int height) :

void Tombstone::Draw(float _time)
{
m_renderer->SetClearColor(Color::BLACK);
m_renderer->ClearScreen();
m_renderer->ClearScreen(Color::BLACK);

m_renderer->SetPerspectiveProjection(75, m_aspectRatio, 1.f, 10000.f);
m_renderer->SetTransform(matrix4x4f::Identity());
Expand Down
1 change: 1 addition & 0 deletions src/WorldView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "graphics/Graphics.h"
#include "graphics/Material.h"
#include "graphics/Renderer.h"
#include "graphics/RenderState.h"
#include "matrix4x4.h"
#include "ship/PlayerShipController.h"
#include "ship/ShipViewController.h"
Expand Down
79 changes: 11 additions & 68 deletions src/core/GuiApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,20 @@ GuiApplication::GuiApplication(std::string title) :
GuiApplication::~GuiApplication()
{ }

// FIXME: add support for offscreen rendertarget drawing and multisample RTs
#define RTT 0

void GuiApplication::BeginFrame()
{
PROFILE_SCOPED()
#if RTT
m_renderer->SetRenderTarget(m_renderTarget);
#endif
// TODO: render target size
m_renderer->SetViewport({ 0, 0, Graphics::GetScreenWidth(), Graphics::GetScreenHeight() });
m_renderer->BeginFrame();
}

void GuiApplication::DrawRenderTarget()
{
#if RTT
m_renderer->SetRenderTarget(nullptr);
m_renderer->SetRenderTarget(m_renderTarget.get());
m_renderer->SetViewport({ 0, 0, Graphics::GetScreenWidth(), Graphics::GetScreenHeight() });
m_renderer->ClearScreen();
m_renderer->SetViewport(0, 0, Graphics::GetScreenWidth(), Graphics::GetScreenHeight());
m_renderer->SetTransform(matrix4x4f::Identity());

{
m_renderer->SetMatrixMode(Graphics::MatrixMode::PROJECTION);
m_renderer->PushMatrix();
m_renderer->SetOrthographicProjection(0, Graphics::GetScreenWidth(), Graphics::GetScreenHeight(), 0, -1, 1);
m_renderer->SetMatrixMode(Graphics::MatrixMode::MODELVIEW);
m_renderer->PushMatrix();
m_renderer->LoadIdentity();
}

m_renderQuad->Draw(m_renderer);

{
m_renderer->SetMatrixMode(Graphics::MatrixMode::PROJECTION);
m_renderer->PopMatrix();
m_renderer->SetMatrixMode(Graphics::MatrixMode::MODELVIEW);
m_renderer->PopMatrix();
}

m_renderer->EndFrame();
#endif
m_renderer->BeginFrame();
}

void GuiApplication::EndFrame()
{
PROFILE_SCOPED()
#if RTT
DrawRenderTarget();
#endif

m_renderer->FlushCommandBuffers();
m_renderer->EndFrame();
Expand All @@ -83,35 +47,14 @@ void GuiApplication::EndFrame()

Graphics::RenderTarget *GuiApplication::CreateRenderTarget(const Graphics::Settings &settings)
{
/* @fluffyfreak here's a rendertarget implementation you can use for oculusing and other things. It's pretty simple:
- fill out a RenderTargetDesc struct and call Renderer::CreateRenderTarget
- pass target to Renderer::SetRenderTarget to start rendering to texture
- set up viewport, clear etc, then draw as usual
- SetRenderTarget(0) to resume render to screen
- you can access the attached texture with GetColorTexture to use it with a material
You can reuse the same target with multiple textures.
In that case, leave the color format to NONE so the initial texture is not created, then use SetColorTexture to attach your own.
*/
#if RTT
Graphics::RenderStateDesc rsd;
rsd.depthTest = false;
rsd.depthWrite = false;
rsd.blendMode = Graphics::BLEND_SOLID;
m_renderState.reset(m_renderer->CreateRenderState(rsd));

// Complete the RT description so we can request a buffer.
Graphics::RenderTargetDesc rtDesc(
width,
height,
Graphics::TEXTURE_RGB_888, // don't create a texture
Graphics::TEXTURE_DEPTH,
false, settings.requestedSamples);
m_renderTarget.reset(m_renderer->CreateRenderTarget(rtDesc));

m_renderTarget->SetColorTexture(*m_renderTexture);
#endif

return nullptr;
Graphics::RenderTargetDesc rtDesc = {
uint16_t(settings.width), uint16_t(settings.height),
Graphics::TEXTURE_RGBA_8888,
Graphics::TEXTURE_DEPTH, true,
uint16_t(settings.requestedSamples)
};

return m_renderer->CreateRenderTarget(rtDesc);
}

void GuiApplication::PollEvents()
Expand Down
11 changes: 6 additions & 5 deletions src/core/GuiApplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "RefCounted.h"
#include "SDL_events.h"

#include "graphics/RenderState.h"
#include "graphics/RenderTarget.h"
#include "graphics/Renderer.h"

class IniConfig;
Expand All @@ -21,6 +19,10 @@ namespace PiGui {
class Instance;
}

namespace Graphics {
class RenderTarget;
}

class GuiApplication : public Application {
public:
GuiApplication(std::string title);
Expand All @@ -30,10 +32,9 @@ class GuiApplication : public Application {
Input::Manager *GetInput() { return m_input.get(); }
PiGui::Instance *GetPiGui() { return m_pigui.Get(); }

Graphics::RenderTarget *GetRenderTarget() { return m_renderTarget.get(); }

protected:
// Called at the end of the frame automatically, blits the RT onto the application
// framebuffer
void DrawRenderTarget();

// Call this from your OnStartup() method
void SetupProfiler(IniConfig *config);
Expand Down
1 change: 1 addition & 0 deletions src/editor/ModelViewerWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "graphics/Graphics.h"
#include "graphics/Renderer.h"
#include "graphics/RenderState.h"
#include "graphics/TextureBuilder.h"

#include "scenegraph/Animation.h"
Expand Down
2 changes: 0 additions & 2 deletions src/editor/ViewportWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ void ViewportWindow::Update(float deltaTime)
r->ClearScreen(); // FIXME: add clear-command passing in immediate-state clear color

OnRender(r);

r->SetRenderTarget(nullptr);
}

ImGui::BeginChild("##ViewportContainer", ImVec2(0, 0), false,
Expand Down
9 changes: 7 additions & 2 deletions src/graphics/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ namespace Graphics {
//traditionally gui happens between endframe and swapbuffers
virtual bool SwapBuffers() = 0;

// returns currently bound render target (if any)
virtual RenderTarget *GetRenderTarget() = 0;
//set 0 to render to screen
virtual bool SetRenderTarget(RenderTarget *) = 0;

Expand All @@ -91,10 +93,10 @@ namespace Graphics {
virtual bool SetScissor(ViewportExtents scissor) = 0;

//clear color and depth buffer
virtual bool ClearScreen() = 0;
virtual bool ClearScreen(const Color &c = Color::BLACK, bool depthBuffer = true) = 0;

//clear depth buffer
virtual bool ClearDepthBuffer() = 0;
virtual bool SetClearColor(const Color &c) = 0;

virtual bool SetViewport(ViewportExtents vp) = 0;
virtual ViewportExtents GetViewport() const = 0;
Expand Down Expand Up @@ -189,11 +191,13 @@ namespace Graphics {
m_storedVP = m_renderer->GetViewport();
m_storedProj = m_renderer->GetProjection();
m_storedMV = m_renderer->GetTransform();
m_storedRT = m_renderer->GetRenderTarget();
}

virtual ~StateTicket()
{
m_renderer->PopState();
m_renderer->SetRenderTarget(m_storedRT);
m_renderer->SetViewport(m_storedVP);
m_renderer->SetTransform(m_storedMV);
m_renderer->SetProjection(m_storedProj);
Expand All @@ -204,6 +208,7 @@ namespace Graphics {

private:
Renderer *m_renderer;
RenderTarget *m_storedRT;
matrix4x4f m_storedProj;
matrix4x4f m_storedMV;
ViewportExtents m_storedVP;
Expand Down
7 changes: 4 additions & 3 deletions src/graphics/dummy/RendererDummy.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ namespace Graphics {
virtual bool EndFrame() override final { return true; }
virtual bool SwapBuffers() override final { return true; }

virtual bool SetRenderTarget(RenderTarget *) override final { return true; }
virtual RenderTarget *GetRenderTarget() override final { return m_rt; }
virtual bool SetRenderTarget(RenderTarget *rt) override final { m_rt = rt; return true; }
virtual bool SetScissor(ViewportExtents ext) override final { return true; }

virtual void CopyRenderTarget(RenderTarget *, RenderTarget *, ViewportExtents, ViewportExtents, bool) override final {}
virtual void ResolveRenderTarget(RenderTarget *, RenderTarget *, ViewportExtents) override final {}

virtual bool ClearScreen() override final { return true; }
virtual bool ClearScreen(const Color &, bool) override final { return true; }
virtual bool ClearDepthBuffer() override final { return true; }
virtual bool SetClearColor(const Color &c) override final { return true; }

virtual bool SetViewport(ViewportExtents v) override final { return true; }
virtual ViewportExtents GetViewport() const override final { return {}; }
Expand Down Expand Up @@ -98,6 +98,7 @@ namespace Graphics {

private:
const matrix4x4f m_identity;
Graphics::RenderTarget *m_rt;
};

} // namespace Graphics
Expand Down
Loading

0 comments on commit 00372bb

Please sign in to comment.