1
0
Fork 0
forked from suyu/suyu

video_core: Eliminate the g_renderer global variable

We move the initialization of the renderer to the core class, while
keeping the creation of it and any other specifics in video_core. This
way we can ensure that the renderer is initialized and doesn't give
unfettered access to the renderer. This also makes dependencies on types
more explicit.

For example, the GPU class doesn't need to depend on the
existence of a renderer, it only needs to care about whether or not it
has a rasterizer, but since it was accessing the global variable, it was
also making the renderer a part of its dependency chain. By adjusting
the interface, we can get rid of this dependency.
This commit is contained in:
Lioncash 2018-08-03 12:55:58 -04:00
parent 762fcaf5de
commit 6030c5ce41
19 changed files with 101 additions and 75 deletions

View file

@ -18,6 +18,7 @@
#include "core/loader/loader.h" #include "core/loader/loader.h"
#include "core/settings.h" #include "core/settings.h"
#include "file_sys/vfs_real.h" #include "file_sys/vfs_real.h"
#include "video_core/renderer_base.h"
#include "video_core/video_core.h" #include "video_core/video_core.h"
namespace Core { namespace Core {
@ -174,7 +175,6 @@ System::ResultStatus System::Init(EmuWindow& emu_window) {
cpu_cores[index] = std::make_shared<Cpu>(cpu_exclusive_monitor, cpu_barrier, index); cpu_cores[index] = std::make_shared<Cpu>(cpu_exclusive_monitor, cpu_barrier, index);
} }
gpu_core = std::make_unique<Tegra::GPU>();
telemetry_session = std::make_unique<Core::TelemetrySession>(); telemetry_session = std::make_unique<Core::TelemetrySession>();
service_manager = std::make_shared<Service::SM::ServiceManager>(); service_manager = std::make_shared<Service::SM::ServiceManager>();
@ -182,10 +182,13 @@ System::ResultStatus System::Init(EmuWindow& emu_window) {
Service::Init(service_manager); Service::Init(service_manager);
GDBStub::Init(); GDBStub::Init();
if (!VideoCore::Init(emu_window)) { renderer = VideoCore::CreateRenderer(emu_window);
if (!renderer->Init()) {
return ResultStatus::ErrorVideoCore; return ResultStatus::ErrorVideoCore;
} }
gpu_core = std::make_unique<Tegra::GPU>(*renderer->Rasterizer());
// Create threads for CPU cores 1-3, and build thread_to_cpu map // Create threads for CPU cores 1-3, and build thread_to_cpu map
// CPU core 0 is run on the main thread // CPU core 0 is run on the main thread
thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0]; thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
@ -217,7 +220,7 @@ void System::Shutdown() {
perf_results.frametime * 1000.0); perf_results.frametime * 1000.0);
// Shutdown emulation session // Shutdown emulation session
VideoCore::Shutdown(); renderer.reset();
GDBStub::Shutdown(); GDBStub::Shutdown();
Service::Shutdown(); Service::Shutdown();
Kernel::Shutdown(); Kernel::Shutdown();

View file

@ -27,6 +27,10 @@ namespace Service::SM {
class ServiceManager; class ServiceManager;
} }
namespace VideoCore {
class RendererBase;
}
namespace Core { namespace Core {
class System { class System {
@ -127,11 +131,26 @@ public:
/// Gets a CPU interface to the CPU core with the specified index /// Gets a CPU interface to the CPU core with the specified index
Cpu& CpuCore(size_t core_index); Cpu& CpuCore(size_t core_index);
/// Gets the GPU interface /// Gets a mutable reference to the GPU interface
Tegra::GPU& GPU() { Tegra::GPU& GPU() {
return *gpu_core; return *gpu_core;
} }
/// Gets an immutable reference to the GPU interface.
const Tegra::GPU& GPU() const {
return *gpu_core;
}
/// Gets a mutable reference to the renderer.
VideoCore::RendererBase& Renderer() {
return *renderer;
}
/// Gets an immutable reference to the renderer.
const VideoCore::RendererBase& Renderer() const {
return *renderer;
}
/// Gets the scheduler for the CPU core that is currently running /// Gets the scheduler for the CPU core that is currently running
Kernel::Scheduler& CurrentScheduler() { Kernel::Scheduler& CurrentScheduler() {
return *CurrentCpuCore().Scheduler(); return *CurrentCpuCore().Scheduler();
@ -195,6 +214,7 @@ private:
/// AppLoader used to load the current executing application /// AppLoader used to load the current executing application
std::unique_ptr<Loader::AppLoader> app_loader; std::unique_ptr<Loader::AppLoader> app_loader;
std::unique_ptr<VideoCore::RendererBase> renderer;
std::unique_ptr<Tegra::GPU> gpu_core; std::unique_ptr<Tegra::GPU> gpu_core;
std::shared_ptr<Tegra::DebugContext> debug_context; std::shared_ptr<Tegra::DebugContext> debug_context;
Kernel::SharedPtr<Kernel::Process> current_process; Kernel::SharedPtr<Kernel::Process> current_process;

View file

@ -30,9 +30,9 @@ void nvdisp_disp0::flip(u32 buffer_handle, u32 offset, u32 format, u32 width, u3
addr, offset, width, height, stride, static_cast<PixelFormat>(format), addr, offset, width, height, stride, static_cast<PixelFormat>(format),
transform, crop_rect}; transform, crop_rect};
Core::System::GetInstance().perf_stats.EndGameFrame(); auto& instance = Core::System::GetInstance();
instance.perf_stats.EndGameFrame();
VideoCore::g_renderer->SwapBuffers(framebuffer); instance.Renderer().SwapBuffers(framebuffer);
} }
} // namespace Service::Nvidia::Devices } // namespace Service::Nvidia::Devices

View file

@ -150,15 +150,16 @@ u32 nvhost_as_gpu::UnmapBuffer(const std::vector<u8>& input, std::vector<u8>& ou
LOG_DEBUG(Service_NVDRV, "called, offset=0x{:X}", params.offset); LOG_DEBUG(Service_NVDRV, "called, offset=0x{:X}", params.offset);
auto& gpu = Core::System::GetInstance().GPU(); const auto itr = buffer_mappings.find(params.offset);
auto itr = buffer_mappings.find(params.offset);
ASSERT_MSG(itr != buffer_mappings.end(), "Tried to unmap invalid mapping"); ASSERT_MSG(itr != buffer_mappings.end(), "Tried to unmap invalid mapping");
// Remove this memory region from the rasterizer cache. auto& system_instance = Core::System::GetInstance();
VideoCore::g_renderer->Rasterizer()->FlushAndInvalidateRegion(params.offset, itr->second.size);
// Remove this memory region from the rasterizer cache.
system_instance.Renderer().Rasterizer()->FlushAndInvalidateRegion(params.offset,
itr->second.size);
auto& gpu = system_instance.GPU();
params.offset = gpu.memory_manager->UnmapBuffer(params.offset, itr->second.size); params.offset = gpu.memory_manager->UnmapBuffer(params.offset, itr->second.size);
buffer_mappings.erase(itr->second.offset); buffer_mappings.erase(itr->second.offset);

View file

@ -127,9 +127,11 @@ void NVFlinger::Compose() {
MicroProfileFlip(); MicroProfileFlip();
if (buffer == boost::none) { if (buffer == boost::none) {
auto& system_instance = Core::System::GetInstance();
// There was no queued buffer to draw, render previous frame // There was no queued buffer to draw, render previous frame
Core::System::GetInstance().perf_stats.EndGameFrame(); system_instance.perf_stats.EndGameFrame();
VideoCore::g_renderer->SwapBuffers({}); system_instance.Renderer().SwapBuffers({});
continue; continue;
} }

View file

@ -326,34 +326,36 @@ void RasterizerMarkRegionCached(Tegra::GPUVAddr gpu_addr, u64 size, bool cached)
} }
void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) { void RasterizerFlushVirtualRegion(VAddr start, u64 size, FlushMode mode) {
auto& system_instance = Core::System::GetInstance();
// Since pages are unmapped on shutdown after video core is shutdown, the renderer may be // Since pages are unmapped on shutdown after video core is shutdown, the renderer may be
// null here // null here
if (VideoCore::g_renderer == nullptr) { if (!system_instance.IsPoweredOn()) {
return; return;
} }
VAddr end = start + size; VAddr end = start + size;
auto CheckRegion = [&](VAddr region_start, VAddr region_end) { const auto CheckRegion = [&](VAddr region_start, VAddr region_end) {
if (start >= region_end || end <= region_start) { if (start >= region_end || end <= region_start) {
// No overlap with region // No overlap with region
return; return;
} }
VAddr overlap_start = std::max(start, region_start); const VAddr overlap_start = std::max(start, region_start);
VAddr overlap_end = std::min(end, region_end); const VAddr overlap_end = std::min(end, region_end);
std::vector<Tegra::GPUVAddr> gpu_addresses = const std::vector<Tegra::GPUVAddr> gpu_addresses =
Core::System::GetInstance().GPU().memory_manager->CpuToGpuAddress(overlap_start); system_instance.GPU().memory_manager->CpuToGpuAddress(overlap_start);
if (gpu_addresses.empty()) { if (gpu_addresses.empty()) {
return; return;
} }
u64 overlap_size = overlap_end - overlap_start; const u64 overlap_size = overlap_end - overlap_start;
for (const auto& gpu_address : gpu_addresses) { for (const auto& gpu_address : gpu_addresses) {
auto* rasterizer = VideoCore::g_renderer->Rasterizer(); auto* rasterizer = system_instance.Renderer().Rasterizer();
switch (mode) { switch (mode) {
case FlushMode::Flush: case FlushMode::Flush:
rasterizer->FlushRegion(gpu_address, overlap_size); rasterizer->FlushRegion(gpu_address, overlap_size);

View file

@ -2,6 +2,7 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "core/core.h"
#include "core/gdbstub/gdbstub.h" #include "core/gdbstub/gdbstub.h"
#include "core/hle/service/hid/hid.h" #include "core/hle/service/hid/hid.h"
#include "core/settings.h" #include "core/settings.h"
@ -19,8 +20,9 @@ void Apply() {
VideoCore::g_toggle_framelimit_enabled = values.toggle_framelimit; VideoCore::g_toggle_framelimit_enabled = values.toggle_framelimit;
if (VideoCore::g_renderer) { auto& system_instance = Core::System::GetInstance();
VideoCore::g_renderer->UpdateCurrentFramebufferLayout(); if (system_instance.IsPoweredOn()) {
system_instance.Renderer().UpdateCurrentFramebufferLayout();
} }
Service::HID::ReloadInputDevices(); Service::HID::ReloadInputDevices();

View file

@ -19,8 +19,8 @@ namespace Engines {
/// First register id that is actually a Macro call. /// First register id that is actually a Macro call.
constexpr u32 MacroRegistersStart = 0xE00; constexpr u32 MacroRegistersStart = 0xE00;
Maxwell3D::Maxwell3D(MemoryManager& memory_manager) Maxwell3D::Maxwell3D(VideoCore::RasterizerInterface& rasterizer, MemoryManager& memory_manager)
: memory_manager(memory_manager), macro_interpreter(*this) {} : memory_manager(memory_manager), rasterizer{rasterizer}, macro_interpreter(*this) {}
void Maxwell3D::CallMacroMethod(u32 method, std::vector<u32> parameters) { void Maxwell3D::CallMacroMethod(u32 method, std::vector<u32> parameters) {
auto macro_code = uploaded_macros.find(method); auto macro_code = uploaded_macros.find(method);
@ -130,7 +130,7 @@ void Maxwell3D::WriteReg(u32 method, u32 value, u32 remaining_params) {
break; break;
} }
VideoCore::g_renderer->Rasterizer()->NotifyMaxwellRegisterChanged(method); rasterizer.NotifyMaxwellRegisterChanged(method);
if (debug_context) { if (debug_context) {
debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandProcessed, nullptr); debug_context->OnEvent(Tegra::DebugContext::Event::MaxwellCommandProcessed, nullptr);
@ -218,7 +218,7 @@ void Maxwell3D::DrawArrays() {
} }
const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count}; const bool is_indexed{regs.index_array.count && !regs.vertex_buffer.count};
VideoCore::g_renderer->Rasterizer()->AccelerateDrawBatch(is_indexed); rasterizer.AccelerateDrawBatch(is_indexed);
// TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if // TODO(bunnei): Below, we reset vertex count so that we can use these registers to determine if
// the game is trying to draw indexed or direct mode. This needs to be verified on HW still - // the game is trying to draw indexed or direct mode. This needs to be verified on HW still -
@ -393,7 +393,7 @@ void Maxwell3D::ProcessClearBuffers() {
regs.clear_buffers.R == regs.clear_buffers.B && regs.clear_buffers.R == regs.clear_buffers.B &&
regs.clear_buffers.R == regs.clear_buffers.A); regs.clear_buffers.R == regs.clear_buffers.A);
VideoCore::g_renderer->Rasterizer()->Clear(); rasterizer.Clear();
} }
} // namespace Engines } // namespace Engines

View file

@ -17,6 +17,10 @@
#include "video_core/memory_manager.h" #include "video_core/memory_manager.h"
#include "video_core/textures/texture.h" #include "video_core/textures/texture.h"
namespace VideoCore {
class RasterizerInterface;
}
namespace Tegra::Engines { namespace Tegra::Engines {
#define MAXWELL3D_REG_INDEX(field_name) \ #define MAXWELL3D_REG_INDEX(field_name) \
@ -24,7 +28,7 @@ namespace Tegra::Engines {
class Maxwell3D final { class Maxwell3D final {
public: public:
explicit Maxwell3D(MemoryManager& memory_manager); explicit Maxwell3D(VideoCore::RasterizerInterface& rasterizer, MemoryManager& memory_manager);
~Maxwell3D() = default; ~Maxwell3D() = default;
/// Register structure of the Maxwell3D engine. /// Register structure of the Maxwell3D engine.
@ -818,6 +822,8 @@ public:
Texture::FullTextureInfo GetStageTexture(Regs::ShaderStage stage, size_t offset) const; Texture::FullTextureInfo GetStageTexture(Regs::ShaderStage stage, size_t offset) const;
private: private:
VideoCore::RasterizerInterface& rasterizer;
std::unordered_map<u32, std::vector<u32>> uploaded_macros; std::unordered_map<u32, std::vector<u32>> uploaded_macros;
/// Macro method that is currently being executed / being fed parameters. /// Macro method that is currently being executed / being fed parameters.

View file

@ -7,12 +7,13 @@
#include "video_core/engines/maxwell_compute.h" #include "video_core/engines/maxwell_compute.h"
#include "video_core/engines/maxwell_dma.h" #include "video_core/engines/maxwell_dma.h"
#include "video_core/gpu.h" #include "video_core/gpu.h"
#include "video_core/rasterizer_interface.h"
namespace Tegra { namespace Tegra {
GPU::GPU() { GPU::GPU(VideoCore::RasterizerInterface& rasterizer) {
memory_manager = std::make_unique<MemoryManager>(); memory_manager = std::make_unique<MemoryManager>();
maxwell_3d = std::make_unique<Engines::Maxwell3D>(*memory_manager); maxwell_3d = std::make_unique<Engines::Maxwell3D>(rasterizer, *memory_manager);
fermi_2d = std::make_unique<Engines::Fermi2D>(*memory_manager); fermi_2d = std::make_unique<Engines::Fermi2D>(*memory_manager);
maxwell_compute = std::make_unique<Engines::MaxwellCompute>(); maxwell_compute = std::make_unique<Engines::MaxwellCompute>();
maxwell_dma = std::make_unique<Engines::MaxwellDMA>(*memory_manager); maxwell_dma = std::make_unique<Engines::MaxwellDMA>(*memory_manager);

View file

@ -11,6 +11,10 @@
#include "core/hle/service/nvflinger/buffer_queue.h" #include "core/hle/service/nvflinger/buffer_queue.h"
#include "video_core/memory_manager.h" #include "video_core/memory_manager.h"
namespace VideoCore {
class RasterizerInterface;
}
namespace Tegra { namespace Tegra {
enum class RenderTargetFormat : u32 { enum class RenderTargetFormat : u32 {
@ -98,7 +102,7 @@ enum class EngineID {
class GPU final { class GPU final {
public: public:
GPU(); explicit GPU(VideoCore::RasterizerInterface& rasterizer);
~GPU(); ~GPU();
/// Processes a command list stored at the specified address in GPU memory. /// Processes a command list stored at the specified address in GPU memory.

View file

@ -7,6 +7,8 @@
#include "video_core/renderer_base.h" #include "video_core/renderer_base.h"
#include "video_core/renderer_opengl/gl_rasterizer.h" #include "video_core/renderer_opengl/gl_rasterizer.h"
namespace VideoCore {
RendererBase::RendererBase(EmuWindow& window) : render_window{window} {} RendererBase::RendererBase(EmuWindow& window) : render_window{window} {}
RendererBase::~RendererBase() = default; RendererBase::~RendererBase() = default;
@ -21,3 +23,5 @@ void RendererBase::RefreshRasterizerSetting() {
rasterizer = std::make_unique<RasterizerOpenGL>(render_window); rasterizer = std::make_unique<RasterizerOpenGL>(render_window);
} }
} }
} // namespace VideoCore

View file

@ -13,6 +13,8 @@
class EmuWindow; class EmuWindow;
namespace VideoCore {
class RendererBase : NonCopyable { class RendererBase : NonCopyable {
public: public:
/// Used to reference a framebuffer /// Used to reference a framebuffer
@ -44,7 +46,7 @@ public:
return m_current_frame; return m_current_frame;
} }
VideoCore::RasterizerInterface* Rasterizer() const { RasterizerInterface* Rasterizer() const {
return rasterizer.get(); return rasterizer.get();
} }
@ -52,7 +54,9 @@ public:
protected: protected:
EmuWindow& render_window; ///< Reference to the render window handle. EmuWindow& render_window; ///< Reference to the render window handle.
std::unique_ptr<VideoCore::RasterizerInterface> rasterizer; std::unique_ptr<RasterizerInterface> rasterizer;
f32 m_current_fps = 0.0f; ///< Current framerate, should be set by the renderer f32 m_current_fps = 0.0f; ///< Current framerate, should be set by the renderer
int m_current_frame = 0; ///< Current frame, should be set by the renderer int m_current_frame = 0; ///< Current frame, should be set by the renderer
}; };
} // namespace VideoCore

View file

@ -103,7 +103,7 @@ ScopeAcquireGLContext::~ScopeAcquireGLContext() {
} }
} }
RendererOpenGL::RendererOpenGL(EmuWindow& window) : RendererBase{window} {} RendererOpenGL::RendererOpenGL(EmuWindow& window) : VideoCore::RendererBase{window} {}
RendererOpenGL::~RendererOpenGL() = default; RendererOpenGL::~RendererOpenGL() = default;
/// Swap buffers (render frame) /// Swap buffers (render frame)

View file

@ -41,7 +41,7 @@ private:
EmuWindow& emu_window; EmuWindow& emu_window;
}; };
class RendererOpenGL : public RendererBase { class RendererOpenGL : public VideoCore::RendererBase {
public: public:
explicit RendererOpenGL(EmuWindow& window); explicit RendererOpenGL(EmuWindow& window);
~RendererOpenGL() override; ~RendererOpenGL() override;

View file

@ -3,37 +3,16 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory> #include <memory>
#include "common/logging/log.h"
#include "video_core/renderer_base.h" #include "video_core/renderer_base.h"
#include "video_core/renderer_opengl/renderer_opengl.h" #include "video_core/renderer_opengl/renderer_opengl.h"
#include "video_core/video_core.h" #include "video_core/video_core.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// Video Core namespace
namespace VideoCore { namespace VideoCore {
std::unique_ptr<RendererBase> g_renderer; ///< Renderer plugin
std::atomic<bool> g_toggle_framelimit_enabled; std::atomic<bool> g_toggle_framelimit_enabled;
/// Initialize the video core std::unique_ptr<RendererBase> CreateRenderer(EmuWindow& emu_window) {
bool Init(EmuWindow& emu_window) { return std::make_unique<RendererOpenGL>(emu_window);
g_renderer = std::make_unique<RendererOpenGL>(emu_window);
if (g_renderer->Init()) {
LOG_DEBUG(Render, "initialized OK");
} else {
LOG_CRITICAL(Render, "initialization failed !");
return false;
}
return true;
}
/// Shutdown the video core
void Shutdown() {
g_renderer.reset();
LOG_DEBUG(Render, "shutdown OK");
} }
} // namespace VideoCore } // namespace VideoCore

View file

@ -8,25 +8,23 @@
#include <memory> #include <memory>
class EmuWindow; class EmuWindow;
class RendererBase;
////////////////////////////////////////////////////////////////////////////////////////////////////
// Video Core namespace
namespace VideoCore { namespace VideoCore {
enum class Renderer { Software, OpenGL }; class RendererBase;
extern std::unique_ptr<RendererBase> g_renderer; ///< Renderer plugin enum class Renderer { Software, OpenGL };
// TODO: Wrap these in a user settings struct along with any other graphics settings (often set from // TODO: Wrap these in a user settings struct along with any other graphics settings (often set from
// qt ui) // qt ui)
extern std::atomic<bool> g_toggle_framelimit_enabled; extern std::atomic<bool> g_toggle_framelimit_enabled;
/// Initialize the video core /**
bool Init(EmuWindow& emu_window); * Creates a renderer instance.
*
/// Shutdown the video core * @note The returned renderer instance is simply allocated. Its Init()
void Shutdown(); * function still needs to be called to fully complete its setup.
*/
std::unique_ptr<RendererBase> CreateRenderer(EmuWindow& emu_window);
} // namespace VideoCore } // namespace VideoCore

View file

@ -445,7 +445,7 @@ bool GMainWindow::LoadROM(const QString& filename) {
case Core::System::ResultStatus::ErrorVideoCore: case Core::System::ResultStatus::ErrorVideoCore:
QMessageBox::critical( QMessageBox::critical(
this, tr("An error occured in the video core."), this, tr("An error occurred initializing the video core."),
tr("yuzu has encountered an error while running the video core, please see the " tr("yuzu has encountered an error while running the video core, please see the "
"log for more details." "log for more details."
"For more information on accessing the log, please see the following page: " "For more information on accessing the log, please see the following page: "
@ -459,7 +459,7 @@ bool GMainWindow::LoadROM(const QString& filename) {
default: default:
QMessageBox::critical( QMessageBox::critical(
this, tr("Error while loading ROM!"), this, tr("Error while loading ROM!"),
tr("An unknown error occured. Please see the log for more details.")); tr("An unknown error occurred. Please see the log for more details."));
break; break;
} }
return false; return false;

View file

@ -187,7 +187,7 @@ int main(int argc, char** argv) {
LOG_CRITICAL(Frontend, "Failed to determine system mode!"); LOG_CRITICAL(Frontend, "Failed to determine system mode!");
return -1; return -1;
case Core::System::ResultStatus::ErrorVideoCore: case Core::System::ResultStatus::ErrorVideoCore:
LOG_CRITICAL(Frontend, "VideoCore not initialized"); LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
return -1; return -1;
case Core::System::ResultStatus::Success: case Core::System::ResultStatus::Success:
break; // Expected case break; // Expected case