2019-01-23 22:17:55 -05:00
|
|
|
// Copyright 2019 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <optional>
|
|
|
|
#include <thread>
|
|
|
|
#include <variant>
|
2020-12-12 01:26:14 -05:00
|
|
|
|
2019-02-18 20:58:32 -05:00
|
|
|
#include "common/threadsafe_queue.h"
|
2020-12-12 01:26:14 -05:00
|
|
|
#include "video_core/framebuffer_config.h"
|
2019-02-18 20:58:32 -05:00
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
namespace Tegra {
|
|
|
|
struct FramebufferConfig;
|
|
|
|
class DmaPusher;
|
|
|
|
} // namespace Tegra
|
|
|
|
|
2019-03-30 20:08:09 -04:00
|
|
|
namespace Core {
|
2020-03-24 22:58:49 -04:00
|
|
|
namespace Frontend {
|
|
|
|
class GraphicsContext;
|
|
|
|
}
|
2019-03-30 20:08:09 -04:00
|
|
|
class System;
|
|
|
|
} // namespace Core
|
2019-01-23 22:17:55 -05:00
|
|
|
|
2020-12-12 01:26:14 -05:00
|
|
|
namespace VideoCore {
|
2020-12-12 03:24:33 -05:00
|
|
|
class RendererBase;
|
2020-12-12 01:26:14 -05:00
|
|
|
} // namespace VideoCore
|
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
namespace VideoCommon::GPUThread {
|
|
|
|
|
2019-02-18 20:58:32 -05:00
|
|
|
/// Command to signal to the GPU thread that processing has ended
|
|
|
|
struct EndProcessingCommand final {};
|
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
/// Command to signal to the GPU thread that a command list is ready for processing
|
|
|
|
struct SubmitListCommand final {
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit SubmitListCommand(Tegra::CommandList&& entries_) : entries{std::move(entries_)} {}
|
2019-01-23 22:17:55 -05:00
|
|
|
|
|
|
|
Tegra::CommandList entries;
|
|
|
|
};
|
|
|
|
|
2020-10-26 23:07:36 -04:00
|
|
|
/// Command to signal to the GPU thread that a cdma command list is ready for processing
|
|
|
|
struct SubmitChCommandEntries final {
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit SubmitChCommandEntries(Tegra::ChCommandHeaderList&& entries_)
|
|
|
|
: entries{std::move(entries_)} {}
|
2020-10-26 23:07:36 -04:00
|
|
|
|
|
|
|
Tegra::ChCommandHeaderList entries;
|
|
|
|
};
|
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
/// Command to signal to the GPU thread that a swap buffers is pending
|
|
|
|
struct SwapBuffersCommand final {
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit SwapBuffersCommand(std::optional<const Tegra::FramebufferConfig> framebuffer_)
|
|
|
|
: framebuffer{std::move(framebuffer_)} {}
|
2019-01-23 22:17:55 -05:00
|
|
|
|
2019-02-18 20:58:32 -05:00
|
|
|
std::optional<Tegra::FramebufferConfig> framebuffer;
|
2019-01-23 22:17:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread to flush a region
|
|
|
|
struct FlushRegionCommand final {
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit constexpr FlushRegionCommand(VAddr addr_, u64 size_) : addr{addr_}, size{size_} {}
|
2019-01-23 22:17:55 -05:00
|
|
|
|
2020-04-05 12:58:23 -04:00
|
|
|
VAddr addr;
|
2019-02-18 20:58:32 -05:00
|
|
|
u64 size;
|
2019-01-23 22:17:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread to invalidate a region
|
|
|
|
struct InvalidateRegionCommand final {
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit constexpr InvalidateRegionCommand(VAddr addr_, u64 size_) : addr{addr_}, size{size_} {}
|
2019-01-23 22:17:55 -05:00
|
|
|
|
2020-04-05 12:58:23 -04:00
|
|
|
VAddr addr;
|
2019-02-18 20:58:32 -05:00
|
|
|
u64 size;
|
2019-01-23 22:17:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Command to signal to the GPU thread to flush and invalidate a region
|
|
|
|
struct FlushAndInvalidateRegionCommand final {
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit constexpr FlushAndInvalidateRegionCommand(VAddr addr_, u64 size_)
|
|
|
|
: addr{addr_}, size{size_} {}
|
2019-01-23 22:17:55 -05:00
|
|
|
|
2020-04-05 12:58:23 -04:00
|
|
|
VAddr addr;
|
2019-02-18 20:58:32 -05:00
|
|
|
u64 size;
|
2019-01-23 22:17:55 -05:00
|
|
|
};
|
|
|
|
|
2020-02-20 10:55:32 -05:00
|
|
|
/// Command called within the gpu, to schedule actions after a command list end
|
2020-02-17 17:10:23 -05:00
|
|
|
struct OnCommandListEndCommand final {};
|
|
|
|
|
2020-02-20 10:55:32 -05:00
|
|
|
/// Command to make the gpu look into pending requests
|
|
|
|
struct GPUTickCommand final {};
|
|
|
|
|
2019-02-18 20:58:32 -05:00
|
|
|
using CommandData =
|
2020-10-26 23:07:36 -04:00
|
|
|
std::variant<EndProcessingCommand, SubmitListCommand, SubmitChCommandEntries,
|
|
|
|
SwapBuffersCommand, FlushRegionCommand, InvalidateRegionCommand,
|
|
|
|
FlushAndInvalidateRegionCommand, OnCommandListEndCommand, GPUTickCommand>;
|
2019-02-18 20:58:32 -05:00
|
|
|
|
|
|
|
struct CommandDataContainer {
|
|
|
|
CommandDataContainer() = default;
|
|
|
|
|
2020-12-04 14:39:12 -05:00
|
|
|
explicit CommandDataContainer(CommandData&& data_, u64 next_fence_)
|
|
|
|
: data{std::move(data_)}, fence{next_fence_} {}
|
2019-02-18 20:58:32 -05:00
|
|
|
|
|
|
|
CommandData data;
|
2019-03-30 20:08:09 -04:00
|
|
|
u64 fence{};
|
2019-02-18 20:58:32 -05:00
|
|
|
};
|
2019-01-23 22:17:55 -05:00
|
|
|
|
|
|
|
/// Struct used to synchronize the GPU thread
|
|
|
|
struct SynchState final {
|
2019-02-18 20:58:32 -05:00
|
|
|
std::atomic_bool is_running{true};
|
|
|
|
|
2020-02-01 21:01:47 -05:00
|
|
|
using CommandQueue = Common::MPSCQueue<CommandDataContainer>;
|
2019-02-18 20:58:32 -05:00
|
|
|
CommandQueue queue;
|
2019-03-30 20:08:09 -04:00
|
|
|
u64 last_fence{};
|
|
|
|
std::atomic<u64> signaled_fence{};
|
2019-01-23 22:17:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Class used to manage the GPU thread
|
|
|
|
class ThreadManager final {
|
|
|
|
public:
|
2020-12-12 03:24:33 -05:00
|
|
|
explicit ThreadManager(Core::System& system_, bool is_async_);
|
2019-01-23 22:17:55 -05:00
|
|
|
~ThreadManager();
|
|
|
|
|
2019-04-09 14:02:00 -04:00
|
|
|
/// Creates and starts the GPU thread.
|
2020-03-24 22:58:49 -04:00
|
|
|
void StartThread(VideoCore::RendererBase& renderer, Core::Frontend::GraphicsContext& context,
|
2020-10-26 23:07:36 -04:00
|
|
|
Tegra::DmaPusher& dma_pusher, Tegra::CDmaPusher& cdma_pusher);
|
2019-04-09 14:02:00 -04:00
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
/// Push GPU command entries to be processed
|
|
|
|
void SubmitList(Tegra::CommandList&& entries);
|
|
|
|
|
2020-10-26 23:07:36 -04:00
|
|
|
/// Push GPU CDMA command buffer entries to be processed
|
|
|
|
void SubmitCommandBuffer(Tegra::ChCommandHeaderList&& entries);
|
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
/// Swap buffers (render frame)
|
2019-08-21 00:55:25 -04:00
|
|
|
void SwapBuffers(const Tegra::FramebufferConfig* framebuffer);
|
2019-01-23 22:17:55 -05:00
|
|
|
|
|
|
|
/// Notify rasterizer that any caches of the specified region should be flushed to Switch memory
|
2020-04-05 12:58:23 -04:00
|
|
|
void FlushRegion(VAddr addr, u64 size);
|
2019-01-23 22:17:55 -05:00
|
|
|
|
|
|
|
/// Notify rasterizer that any caches of the specified region should be invalidated
|
2020-04-05 12:58:23 -04:00
|
|
|
void InvalidateRegion(VAddr addr, u64 size);
|
2019-01-23 22:17:55 -05:00
|
|
|
|
|
|
|
/// Notify rasterizer that any caches of the specified region should be flushed and invalidated
|
2020-04-05 12:58:23 -04:00
|
|
|
void FlushAndInvalidateRegion(VAddr addr, u64 size);
|
2019-01-23 22:17:55 -05:00
|
|
|
|
2019-09-26 19:08:22 -04:00
|
|
|
// Wait until the gpu thread is idle.
|
|
|
|
void WaitIdle() const;
|
|
|
|
|
2020-02-17 17:10:23 -05:00
|
|
|
void OnCommandListEnd();
|
|
|
|
|
2019-01-23 22:17:55 -05:00
|
|
|
private:
|
|
|
|
/// Pushes a command to be executed by the GPU thread
|
2019-03-30 20:08:09 -04:00
|
|
|
u64 PushCommand(CommandData&& command_data);
|
2019-01-23 22:17:55 -05:00
|
|
|
|
|
|
|
SynchState state;
|
2019-03-30 20:08:09 -04:00
|
|
|
Core::System& system;
|
2019-03-07 16:05:46 -05:00
|
|
|
std::thread thread;
|
|
|
|
std::thread::id thread_id;
|
2020-12-12 03:24:33 -05:00
|
|
|
const bool is_async;
|
2019-01-23 22:17:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace VideoCommon::GPUThread
|