2022-04-23 01:59:50 -07:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-01-23 19:17:55 -08:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <thread>
|
|
|
|
#include <variant>
|
2020-12-11 22:26:14 -08:00
|
|
|
|
2022-07-05 22:33:17 -07:00
|
|
|
#include "common/threadsafe_queue.h"
|
2020-12-11 22:26:14 -08:00
|
|
|
#include "video_core/framebuffer_config.h"
|
2019-02-18 17:58:32 -08:00
|
|
|
|
2019-01-23 19:17:55 -08:00
|
|
|
namespace Tegra {
|
|
|
|
struct FramebufferConfig;
|
2021-11-05 07:52:31 -07:00
|
|
|
namespace Control {
|
|
|
|
class Scheduler;
|
|
|
|
}
|
2019-01-23 19:17:55 -08:00
|
|
|
} // namespace Tegra
|
|
|
|
|
2019-03-30 17:08:09 -07:00
|
|
|
namespace Core {
|
2020-03-24 19:58:49 -07:00
|
|
|
namespace Frontend {
|
|
|
|
class GraphicsContext;
|
|
|
|
}
|
2019-03-30 17:08:09 -07:00
|
|
|
class System;
|
|
|
|
} // namespace Core
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2020-12-11 22:26:14 -08:00
|
|
|
namespace VideoCore {
|
2021-01-04 23:09:39 -08:00
|
|
|
class RasterizerInterface;
|
2020-12-12 00:24:33 -08:00
|
|
|
class RendererBase;
|
2020-12-11 22:26:14 -08:00
|
|
|
} // namespace VideoCore
|
|
|
|
|
2019-01-23 19:17:55 -08:00
|
|
|
namespace VideoCommon::GPUThread {
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread that a command list is ready for processing
|
|
|
|
struct SubmitListCommand final {
|
2021-11-05 07:52:31 -07:00
|
|
|
explicit SubmitListCommand(s32 channel_, Tegra::CommandList&& entries_)
|
|
|
|
: channel{channel_}, entries{std::move(entries_)} {}
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2021-11-05 07:52:31 -07:00
|
|
|
s32 channel;
|
2019-01-23 19:17:55 -08:00
|
|
|
Tegra::CommandList entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread that a swap buffers is pending
|
|
|
|
struct SwapBuffersCommand final {
|
2020-12-04 11:39:12 -08:00
|
|
|
explicit SwapBuffersCommand(std::optional<const Tegra::FramebufferConfig> framebuffer_)
|
|
|
|
: framebuffer{std::move(framebuffer_)} {}
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2019-02-18 17:58:32 -08:00
|
|
|
std::optional<Tegra::FramebufferConfig> framebuffer;
|
2019-01-23 19:17:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread to flush a region
|
|
|
|
struct FlushRegionCommand final {
|
2020-12-04 11:39:12 -08:00
|
|
|
explicit constexpr FlushRegionCommand(VAddr addr_, u64 size_) : addr{addr_}, size{size_} {}
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2020-04-05 09:58:23 -07:00
|
|
|
VAddr addr;
|
2019-02-18 17:58:32 -08:00
|
|
|
u64 size;
|
2019-01-23 19:17:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread to invalidate a region
|
|
|
|
struct InvalidateRegionCommand final {
|
2020-12-04 11:39:12 -08:00
|
|
|
explicit constexpr InvalidateRegionCommand(VAddr addr_, u64 size_) : addr{addr_}, size{size_} {}
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2020-04-05 09:58:23 -07:00
|
|
|
VAddr addr;
|
2019-02-18 17:58:32 -08:00
|
|
|
u64 size;
|
2019-01-23 19:17:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread to flush and invalidate a region
|
|
|
|
struct FlushAndInvalidateRegionCommand final {
|
2020-12-04 11:39:12 -08:00
|
|
|
explicit constexpr FlushAndInvalidateRegionCommand(VAddr addr_, u64 size_)
|
|
|
|
: addr{addr_}, size{size_} {}
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2020-04-05 09:58:23 -07:00
|
|
|
VAddr addr;
|
2019-02-18 17:58:32 -08:00
|
|
|
u64 size;
|
2019-01-23 19:17:55 -08:00
|
|
|
};
|
|
|
|
|
2020-02-20 07:55:32 -08:00
|
|
|
/// Command called within the gpu, to schedule actions after a command list end
|
2020-02-17 14:10:23 -08:00
|
|
|
struct OnCommandListEndCommand final {};
|
|
|
|
|
2020-02-20 07:55:32 -08:00
|
|
|
/// Command to make the gpu look into pending requests
|
|
|
|
struct GPUTickCommand final {};
|
|
|
|
|
2019-02-18 17:58:32 -08:00
|
|
|
using CommandData =
|
2021-09-15 17:32:54 -07:00
|
|
|
std::variant<std::monostate, SubmitListCommand, SwapBuffersCommand, FlushRegionCommand,
|
2021-02-28 19:03:00 -08:00
|
|
|
InvalidateRegionCommand, FlushAndInvalidateRegionCommand, OnCommandListEndCommand,
|
|
|
|
GPUTickCommand>;
|
2019-02-18 17:58:32 -08:00
|
|
|
|
|
|
|
struct CommandDataContainer {
|
|
|
|
CommandDataContainer() = default;
|
|
|
|
|
2021-04-07 02:41:31 -07:00
|
|
|
explicit CommandDataContainer(CommandData&& data_, u64 next_fence_, bool block_)
|
|
|
|
: data{std::move(data_)}, fence{next_fence_}, block(block_) {}
|
2019-02-18 17:58:32 -08:00
|
|
|
|
|
|
|
CommandData data;
|
2019-03-30 17:08:09 -07:00
|
|
|
u64 fence{};
|
2021-04-07 02:41:31 -07:00
|
|
|
bool block{};
|
2019-02-18 17:58:32 -08:00
|
|
|
};
|
2019-01-23 19:17:55 -08:00
|
|
|
|
|
|
|
/// Struct used to synchronize the GPU thread
|
|
|
|
struct SynchState final {
|
2022-07-05 22:33:17 -07:00
|
|
|
using CommandQueue = Common::MPSCQueue<CommandDataContainer, true>;
|
2021-04-07 04:57:49 -07:00
|
|
|
std::mutex write_lock;
|
2022-06-14 05:57:19 -07:00
|
|
|
CommandQueue queue;
|
2019-03-30 17:08:09 -07:00
|
|
|
u64 last_fence{};
|
|
|
|
std::atomic<u64> signaled_fence{};
|
2021-09-15 17:32:54 -07:00
|
|
|
std::condition_variable_any cv;
|
2019-01-23 19:17:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Class used to manage the GPU thread
|
|
|
|
class ThreadManager final {
|
|
|
|
public:
|
2020-12-12 00:24:33 -08:00
|
|
|
explicit ThreadManager(Core::System& system_, bool is_async_);
|
2019-01-23 19:17:55 -08:00
|
|
|
~ThreadManager();
|
|
|
|
|
2019-04-09 11:02:00 -07:00
|
|
|
/// Creates and starts the GPU thread.
|
2020-03-24 19:58:49 -07:00
|
|
|
void StartThread(VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context,
|
2021-11-05 07:52:31 -07:00
|
|
|
Tegra::Control::Scheduler& scheduler);
|
2019-04-09 11:02:00 -07:00
|
|
|
|
2019-01-23 19:17:55 -08:00
|
|
|
/// Push GPU command entries to be processed
|
2021-11-05 07:52:31 -07:00
|
|
|
void SubmitList(s32 channel, Tegra::CommandList&& entries);
|
2019-01-23 19:17:55 -08:00
|
|
|
|
|
|
|
/// Swap buffers (render frame)
|
2019-08-20 21:55:25 -07:00
|
|
|
void SwapBuffers(const Tegra::FramebufferConfig* framebuffer);
|
2019-01-23 19:17:55 -08:00
|
|
|
|
|
|
|
/// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
|
2020-04-05 09:58:23 -07:00
|
|
|
void FlushRegion(VAddr addr, u64 size);
|
2019-01-23 19:17:55 -08:00
|
|
|
|
|
|
|
/// Notify rasterizer that any caches of the specified region should be invalidated
|
2020-04-05 09:58:23 -07:00
|
|
|
void InvalidateRegion(VAddr addr, u64 size);
|
2019-01-23 19:17:55 -08:00
|
|
|
|
|
|
|
/// Notify rasterizer that any caches of the specified region should be flushed and invalidated
|
2020-04-05 09:58:23 -07:00
|
|
|
void FlushAndInvalidateRegion(VAddr addr, u64 size);
|
2019-01-23 19:17:55 -08:00
|
|
|
|
2020-02-17 14:10:23 -08:00
|
|
|
void OnCommandListEnd();
|
|
|
|
|
2022-01-30 01:31:13 -08:00
|
|
|
void TickGPU();
|
|
|
|
|
2019-01-23 19:17:55 -08:00
|
|
|
private:
|
|
|
|
/// Pushes a command to be executed by the GPU thread
|
2021-04-07 02:41:31 -07:00
|
|
|
u64 PushCommand(CommandData&& command_data, bool block = false);
|
2021-04-06 23:42:54 -07:00
|
|
|
|
2019-03-30 17:08:09 -07:00
|
|
|
Core::System& system;
|
2020-12-12 00:24:33 -08:00
|
|
|
const bool is_async;
|
2021-01-04 23:09:39 -08:00
|
|
|
VideoCore::RasterizerInterface* rasterizer = nullptr;
|
|
|
|
|
|
|
|
SynchState state;
|
2021-09-15 17:32:54 -07:00
|
|
|
std::jthread thread;
|
2019-01-23 19:17:55 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon::GPUThread
|