From 8a63adc40d03c43183fa879788585f77b2334476 Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Fri, 25 Feb 2022 08:14:11 +0000 Subject: [PATCH] Uses CDeviceCommandContext as an input parameter for CCanvas2D. This was SVN commit r26480. --- source/graphics/Canvas2D.cpp | 12 +-- source/graphics/Canvas2D.h | 3 +- source/graphics/TextRenderer.cpp | 15 +++- source/gui/CGUI.cpp | 3 +- source/gui/CGUI.h | 4 +- source/gui/GUIManager.cpp | 4 +- source/gui/GUIManager.h | 5 +- source/ps/CConsole.cpp | 6 +- source/ps/CConsole.h | 4 +- source/ps/CLogger.cpp | 5 +- source/ps/CLogger.h | 7 +- source/ps/ProfileViewer.cpp | 13 +-- source/ps/ProfileViewer.h | 6 +- source/renderer/Renderer.cpp | 82 ++++++++++--------- source/renderer/Renderer.h | 1 + source/renderer/SceneRenderer.cpp | 4 +- source/renderer/SceneRenderer.h | 3 +- source/renderer/TerrainRenderer.cpp | 3 +- source/renderer/TerrainRenderer.h | 3 +- .../tools/atlas/GameInterface/ActorViewer.cpp | 8 +- 20 files changed, 103 insertions(+), 88 deletions(-) diff --git a/source/graphics/Canvas2D.cpp b/source/graphics/Canvas2D.cpp index 226cb546e8..a7b2a3060e 100644 --- a/source/graphics/Canvas2D.cpp +++ b/source/graphics/Canvas2D.cpp @@ -27,7 +27,6 @@ #include "maths/Rect.h" #include "maths/Vector2D.h" #include "ps/CStrInternStatic.h" -#include "renderer/backend/gl/DeviceCommandContext.h" #include "renderer/Renderer.h" #include @@ -69,9 +68,8 @@ inline void DrawTextureImpl( class CCanvas2D::Impl { public: - Impl() - // TODO: remove global renderer access as pass as an argument. - : DeviceCommandContext(g_Renderer.GetDeviceCommandContext()) + Impl(Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext) + : DeviceCommandContext(deviceCommandContext) { } @@ -103,7 +101,9 @@ public: CShaderTechniquePtr Tech; }; -CCanvas2D::CCanvas2D() : m(std::make_unique()) +CCanvas2D::CCanvas2D( + Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext) + : m(std::make_unique(deviceCommandContext)) { } @@ -319,7 +319,7 @@ void CCanvas2D::DrawText(CTextRenderer& textRenderer) { m->BindTechIfNeeded(); - CShaderProgramPtr shader = m->Tech->GetShader(); + const CShaderProgramPtr& shader = m->Tech->GetShader(); shader->Uniform(str_grayscaleFactor, 0.0f); textRenderer.Render(m->DeviceCommandContext, shader, GetDefaultGuiMatrix()); diff --git a/source/graphics/Canvas2D.h b/source/graphics/Canvas2D.h index b9817e0c6f..a62c61ab4a 100644 --- a/source/graphics/Canvas2D.h +++ b/source/graphics/Canvas2D.h @@ -20,6 +20,7 @@ #include "graphics/Texture.h" #include "maths/Vector2D.h" +#include "renderer/backend/gl/DeviceCommandContext.h" #include #include @@ -34,7 +35,7 @@ struct CColor; class CCanvas2D { public: - CCanvas2D(); + CCanvas2D(Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext); ~CCanvas2D(); CCanvas2D(const CCanvas2D&) = delete; diff --git a/source/graphics/TextRenderer.cpp b/source/graphics/TextRenderer.cpp index 3aa8e21e08..90dbbdb900 100644 --- a/source/graphics/TextRenderer.cpp +++ b/source/graphics/TextRenderer.cpp @@ -225,6 +225,8 @@ void CTextRenderer::Render( ++it; } + bool transformChanged = false; + CTexture* lastTexture = nullptr; for (std::list::iterator it = m_Batches.begin(); it != m_Batches.end(); ++it) { @@ -239,9 +241,13 @@ void CTextRenderer::Render( shader->BindTexture(str_tex, lastTexture->GetBackendTexture()); } - CMatrix3D translation; - translation.SetTranslation(batch.translate.X, batch.translate.Y, 0.0f); - shader->Uniform(str_transform, transform * translation); + if (batch.translate.X != 0.0f || batch.translate.Y != 0.0f) + { + CMatrix3D translation; + translation.SetTranslation(batch.translate.X, batch.translate.Y, 0.0f); + shader->Uniform(str_transform, transform * translation); + transformChanged = true; + } // ALPHA-only textures will have .rgb sampled as 0, so we need to // replace it with white (but not affect RGBA textures) @@ -320,4 +326,7 @@ void CTextRenderer::Render( } m_Batches.clear(); + + if (transformChanged) + shader->Uniform(str_transform, transform); } diff --git a/source/gui/CGUI.cpp b/source/gui/CGUI.cpp index 86a5a46306..1d38295557 100644 --- a/source/gui/CGUI.cpp +++ b/source/gui/CGUI.cpp @@ -325,7 +325,7 @@ void CGUI::SendEventToAll(const CStr& eventName, const JS::HandleValueArray& par object->ScriptEvent(eventName, paramData); } -void CGUI::Draw() +void CGUI::Draw(CCanvas2D& canvas) { using Arena = Allocators::DynamicArena<128 * KiB>; using ObjectListAllocator = ProxyAllocator; @@ -342,7 +342,6 @@ void CGUI::Draw() return visibleObject1.index < visibleObject2.index; }); - CCanvas2D canvas; for (const VisibleObject& visibleObject : visibleObjects) visibleObject.object->Draw(canvas); } diff --git a/source/gui/CGUI.h b/source/gui/CGUI.h index dfc68cc414..5a906018cd 100644 --- a/source/gui/CGUI.h +++ b/source/gui/CGUI.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -97,7 +97,7 @@ public: /** * Displays the whole GUI */ - void Draw(); + void Draw(CCanvas2D& canvas); /** * Draw GUI Sprite diff --git a/source/gui/GUIManager.cpp b/source/gui/GUIManager.cpp index d6fc119a09..51e6e7d0b5 100644 --- a/source/gui/GUIManager.cpp +++ b/source/gui/GUIManager.cpp @@ -372,12 +372,12 @@ void CGUIManager::TickObjects() p.gui->TickObjects(); } -void CGUIManager::Draw() const +void CGUIManager::Draw(CCanvas2D& canvas) const { PROFILE3_GPU("gui"); for (const SGUIPage& p : m_PageStack) - p.gui->Draw(); + p.gui->Draw(canvas); } void CGUIManager::UpdateResolution() diff --git a/source/gui/GUIManager.h b/source/gui/GUIManager.h index 527b104533..c0c212b81f 100644 --- a/source/gui/GUIManager.h +++ b/source/gui/GUIManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -28,6 +28,7 @@ #include #include +class CCanvas2D; class CGUI; /** @@ -106,7 +107,7 @@ public: /** * See CGUI::Draw; applies to @em all loaded pages. */ - void Draw() const; + void Draw(CCanvas2D& canvas) const; /** * See CGUI::UpdateResolution; applies to @em all loaded pages. diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index 0f2b9502f8..e26e23054f 100644 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -190,15 +190,13 @@ void CConsole::Update(const float deltaRealTime) } } -void CConsole::Render() +void CConsole::Render(CCanvas2D& canvas) { if (!(m_Visible || m_Toggle)) return; PROFILE3_GPU("console"); - CCanvas2D canvas; - DrawWindow(canvas); CTextRenderer textRenderer; diff --git a/source/ps/CConsole.h b/source/ps/CConsole.h index f199e5e2bf..37e3e63d1b 100644 --- a/source/ps/CConsole.h +++ b/source/ps/CConsole.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -60,7 +60,7 @@ public: */ void Update(const float deltaRealTime); - void Render(); + void Render(CCanvas2D& canvas); void InsertChar(const int szChar, const wchar_t cooked); diff --git a/source/ps/CLogger.cpp b/source/ps/CLogger.cpp index 258252ea6a..c2a8e714c6 100644 --- a/source/ps/CLogger.cpp +++ b/source/ps/CLogger.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -203,7 +203,7 @@ void CLogger::WriteWarning(const char* message) PushRenderMessage(Warning, message); } -void CLogger::Render() +void CLogger::Render(CCanvas2D& canvas) { PROFILE3_GPU("logger"); @@ -255,7 +255,6 @@ void CLogger::Render() textRenderer.Translate(0.0f, (float)lineSpacing); } - CCanvas2D canvas; canvas.DrawText(textRenderer); } diff --git a/source/ps/CLogger.h b/source/ps/CLogger.h index 20d881619e..e23781868c 100644 --- a/source/ps/CLogger.h +++ b/source/ps/CLogger.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,11 +19,12 @@ #define INCLUDED_CLOGGER #include +#include #include #include #include -#include +class CCanvas2D; class CLogger; extern CLogger* g_Logger; @@ -69,7 +70,7 @@ public: void WriteWarning(const char* message); // Render recent log messages onto the screen - void Render(); + void Render(CCanvas2D& canvas); private: void Init(); diff --git a/source/ps/ProfileViewer.cpp b/source/ps/ProfileViewer.cpp index e76fff8dad..c37f87962b 100644 --- a/source/ps/ProfileViewer.cpp +++ b/source/ps/ProfileViewer.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -15,11 +15,6 @@ * along with 0 A.D. If not, see . */ -/* - * Implementation of profile display (containing only display routines, - * the data model(s) are implemented elsewhere). - */ - #include "precompiled.h" #include "ProfileViewer.h" @@ -40,8 +35,8 @@ #include "scriptinterface/Object.h" #include -#include #include +#include struct CProfileViewerInternals { @@ -149,7 +144,7 @@ CProfileViewer::~CProfileViewer() // Render -void CProfileViewer::RenderProfile() +void CProfileViewer::RenderProfile(CCanvas2D& canvas) { if (!m->profileVisible) return; @@ -170,8 +165,6 @@ void CProfileViewer::RenderProfile() CFontMetrics font(font_name); int lineSpacing = font.GetLineSpacing(); - CCanvas2D canvas; - // Render background. float estimateWidth = 50.0f; for (const ProfileColumn& column : columns) diff --git a/source/ps/ProfileViewer.h b/source/ps/ProfileViewer.h index 9e9f2dd43a..a62d9d7490 100644 --- a/source/ps/ProfileViewer.h +++ b/source/ps/ProfileViewer.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2022 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -28,6 +28,8 @@ #include +class CCanvas2D; + /** * Struct ProfileColumn: Describes one column of an AbstractProfileTable. */ @@ -138,7 +140,7 @@ public: * RenderProfile: Render the profile display using OpenGL if the user * has enabled it. */ - void RenderProfile(); + void RenderProfile(CCanvas2D& canvas); /** * Input: Filter and handle any input events that the profile display diff --git a/source/renderer/Renderer.cpp b/source/renderer/Renderer.cpp index 7a68b6048a..2db9522a4e 100644 --- a/source/renderer/Renderer.cpp +++ b/source/renderer/Renderer.cpp @@ -466,8 +466,6 @@ void CRenderer::RenderFrameImpl(const bool renderGUI, const bool renderLogger) m->deviceCommandContext->SetFramebuffer( m->deviceCommandContext->GetDevice()->GetCurrentBackbuffer()); - m->sceneRenderer.RenderTextOverlays(); - // If we're in Atlas game view, render special tools if (g_AtlasGameLoop && g_AtlasGameLoop->view) { @@ -481,42 +479,7 @@ void CRenderer::RenderFrameImpl(const bool renderGUI, const bool renderLogger) ogl_WarnIfError(); } - if (renderGUI) - { - GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render GUI"); - // All GUI elements are drawn in Z order to render semi-transparent - // objects correctly. - g_GUI->Draw(); - ogl_WarnIfError(); - } - - // If we're in Atlas game view, render special overlays (e.g. editor bandbox). - if (g_AtlasGameLoop && g_AtlasGameLoop->view) - { - CCanvas2D canvas; - g_AtlasGameLoop->view->DrawOverlays(canvas); - ogl_WarnIfError(); - } - - { - GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render console"); - g_Console->Render(); - ogl_WarnIfError(); - } - - if (renderLogger) - { - GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render logger"); - g_Logger->Render(); - ogl_WarnIfError(); - } - - { - GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render profiler"); - // Profile information - g_ProfileViewer.RenderProfile(); - ogl_WarnIfError(); - } + RenderFrame2D(renderGUI, renderLogger); EndFrame(); @@ -535,6 +498,49 @@ void CRenderer::RenderFrameImpl(const bool renderGUI, const bool renderLogger) ogl_WarnIfError(); } +void CRenderer::RenderFrame2D(const bool renderGUI, const bool renderLogger) +{ + CCanvas2D canvas(m->deviceCommandContext.get()); + + m->sceneRenderer.RenderTextOverlays(canvas); + + if (renderGUI) + { + GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render GUI"); + // All GUI elements are drawn in Z order to render semi-transparent + // objects correctly. + g_GUI->Draw(canvas); + ogl_WarnIfError(); + } + + // If we're in Atlas game view, render special overlays (e.g. editor bandbox). + if (g_AtlasGameLoop && g_AtlasGameLoop->view) + { + g_AtlasGameLoop->view->DrawOverlays(canvas); + ogl_WarnIfError(); + } + + { + GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render console"); + g_Console->Render(canvas); + ogl_WarnIfError(); + } + + if (renderLogger) + { + GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render logger"); + g_Logger->Render(canvas); + ogl_WarnIfError(); + } + + { + GPU_SCOPED_LABEL(m->deviceCommandContext.get(), "Render profiler"); + // Profile information + g_ProfileViewer.RenderProfile(canvas); + ogl_WarnIfError(); + } +} + void CRenderer::RenderScreenShot() { m_ScreenShotType = ScreenShotType::NONE; diff --git a/source/renderer/Renderer.h b/source/renderer/Renderer.h index ad98e3a2ef..e3f3eb4df9 100644 --- a/source/renderer/Renderer.h +++ b/source/renderer/Renderer.h @@ -149,6 +149,7 @@ protected: bool ShouldRender() const; void RenderFrameImpl(const bool renderGUI, const bool renderLogger); + void RenderFrame2D(const bool renderGUI, const bool renderLogger); void RenderScreenShot(); void RenderBigScreenShot(const bool needsPresent); diff --git a/source/renderer/SceneRenderer.cpp b/source/renderer/SceneRenderer.cpp index 8143bf63a1..efa2c8cb0a 100644 --- a/source/renderer/SceneRenderer.cpp +++ b/source/renderer/SceneRenderer.cpp @@ -1025,12 +1025,12 @@ void CSceneRenderer::DisplayFrustum() } // Text overlay rendering -void CSceneRenderer::RenderTextOverlays() +void CSceneRenderer::RenderTextOverlays(CCanvas2D& canvas) { PROFILE3_GPU("text overlays"); if (m_DisplayTerrainPriorities) - m->terrainRenderer.RenderPriorities(CULL_DEFAULT); + m->terrainRenderer.RenderPriorities(canvas, CULL_DEFAULT); ogl_WarnIfError(); } diff --git a/source/renderer/SceneRenderer.h b/source/renderer/SceneRenderer.h index 05f54a34ff..dc3df31010 100644 --- a/source/renderer/SceneRenderer.h +++ b/source/renderer/SceneRenderer.h @@ -28,6 +28,7 @@ #include +class CCanvas2D; class CLightEnv; class CMaterial; class CMaterialManager; @@ -109,7 +110,7 @@ public: * Assumes the caller has set up the GL environment for orthographic rendering * with texturing and blending. */ - void RenderTextOverlays(); + void RenderTextOverlays(CCanvas2D& canvas); // set the current lighting environment; (note: the passed pointer is just copied to a variable within the renderer, // so the lightenv passed must be scoped such that it is not destructed until after the renderer is no longer rendering) diff --git a/source/renderer/TerrainRenderer.cpp b/source/renderer/TerrainRenderer.cpp index 8090a4db00..8f0db6d83e 100644 --- a/source/renderer/TerrainRenderer.cpp +++ b/source/renderer/TerrainRenderer.cpp @@ -612,13 +612,12 @@ void TerrainRenderer::RenderWaterFoamOccluders( deviceCommandContext->GetDevice()->GetCurrentBackbuffer()); } -void TerrainRenderer::RenderPriorities(int cullGroup) +void TerrainRenderer::RenderPriorities(CCanvas2D& canvas, int cullGroup) { PROFILE("priorities"); ENSURE(m->phase == Phase_Render); - CCanvas2D canvas; CTextRenderer textRenderer; textRenderer.SetCurrentFont(CStrIntern("mono-stroke-10")); textRenderer.SetCurrentColor(CColor(1.0f, 1.0f, 0.0f, 1.0f)); diff --git a/source/renderer/TerrainRenderer.h b/source/renderer/TerrainRenderer.h index 2a9b148269..7afedf5e54 100644 --- a/source/renderer/TerrainRenderer.h +++ b/source/renderer/TerrainRenderer.h @@ -29,6 +29,7 @@ #include "renderer/backend/gl/Texture.h" class CCamera; +class CCanvas2D; class CMatrix3D; class CModelDecal; class CPatch; @@ -154,7 +155,7 @@ public: /** * Render priority text for all submitted patches, for debugging. */ - void RenderPriorities(int cullGroup); + void RenderPriorities(CCanvas2D& canvas, int cullGroup); /** * Render texture unit 0 over the terrain mesh, with UV coords calculated diff --git a/source/tools/atlas/GameInterface/ActorViewer.cpp b/source/tools/atlas/GameInterface/ActorViewer.cpp index 21ded5f163..1109b13b8e 100644 --- a/source/tools/atlas/GameInterface/ActorViewer.cpp +++ b/source/tools/atlas/GameInterface/ActorViewer.cpp @@ -21,6 +21,7 @@ #include "View.h" +#include "graphics/Canvas2D.h" #include "graphics/ColladaManager.h" #include "graphics/LOSTexture.h" #include "graphics/MiniMapTexture.h" @@ -524,8 +525,11 @@ void ActorViewer::Render() g_Renderer.GetSceneRenderer().RenderScene(g_Renderer.GetDeviceCommandContext(), m); - g_Logger->Render(); - g_ProfileViewer.RenderProfile(); + { + CCanvas2D canvas(g_Renderer.GetDeviceCommandContext()); + g_Logger->Render(canvas); + g_ProfileViewer.RenderProfile(canvas); + } g_Renderer.EndFrame();