summaryrefslogtreecommitdiffstats
path: root/src/video_core/shader/async_shaders.h
blob: d4eeb8fb6b6981850e3c99c28159c5e12a54138c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <condition_variable>
#include <deque>
#include <memory>
#include <shared_mutex>
#include <thread>
#include "common/bit_field.h"
#include "common/common_types.h"
#include "video_core/renderer_opengl/gl_device.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_vulkan/vk_device.h"
#include "video_core/renderer_vulkan/vk_pipeline_cache.h"
#include "video_core/renderer_vulkan/vk_scheduler.h"
#include "video_core/renderer_vulkan/vk_update_descriptor.h"

namespace Core::Frontend {
class EmuWindow;
class GraphicsContext;
} // namespace Core::Frontend

namespace Tegra {
class GPU;
}

namespace Vulkan {
class VKPipelineCache;
}

namespace VideoCommon::Shader {

class AsyncShaders {
public:
    enum class Backend {
        OpenGL,
        GLASM,
        Vulkan,
    };

    struct ResultPrograms {
        OpenGL::OGLProgram opengl;
        OpenGL::OGLAssemblyProgram glasm;
    };

    struct Result {
        u64 uid;
        VAddr cpu_address;
        Backend backend;
        ResultPrograms program;
        std::vector<u64> code;
        std::vector<u64> code_b;
        Tegra::Engines::ShaderType shader_type;
        std::unique_ptr<Vulkan::VKGraphicsPipeline> pipeline;
    };

    explicit AsyncShaders(Core::Frontend::EmuWindow& emu_window);
    ~AsyncShaders();

    /// Start up shader worker threads
    void AllocateWorkers(std::size_t num_workers);

    /// Clear the shader queue and kill all worker threads
    void FreeWorkers();

    // Force end all threads
    void KillWorkers();

    /// Check to see if any shaders have actually been compiled
    bool HasCompletedWork();

    /// Deduce if a shader can be build on another thread of MUST be built in sync. We cannot build
    /// every shader async as some shaders are only built and executed once. We try to "guess" which
    /// shader would be used only once
    bool IsShaderAsync(const Tegra::GPU& gpu) const;

    /// Pulls completed compiled shaders
    std::vector<Result> GetCompletedWork();

    void QueueOpenGLShader(const OpenGL::Device& device, Tegra::Engines::ShaderType shader_type,
                           u64 uid, std::vector<u64> code, std::vector<u64> code_b, u32 main_offset,
                           VideoCommon::Shader::CompilerSettings compiler_settings,
                           const VideoCommon::Shader::Registry& registry, VAddr cpu_addr);

    void QueueVulkanShader(Vulkan::VKPipelineCache* pp_cache,
                           std::vector<VkDescriptorSetLayoutBinding> bindings,
                           Vulkan::SPIRVProgram program, Vulkan::RenderPassParams renderpass_params,
                           u32 padding,
                           std::array<GPUVAddr, Vulkan::Maxwell::MaxShaderProgram> shaders,
                           Vulkan::FixedPipelineState fixed_state);

private:
    void ShaderCompilerThread(Core::Frontend::GraphicsContext* context);

    /// Check our worker queue to see if we have any work queued already
    bool HasWorkQueued();

    struct WorkerParams {
        Backend backend;
        // For OGL
        const OpenGL::Device* device;
        Tegra::Engines::ShaderType shader_type;
        u64 uid;
        std::vector<u64> code;
        std::vector<u64> code_b;
        u32 main_offset;
        VideoCommon::Shader::CompilerSettings compiler_settings;
        const VideoCommon::Shader::Registry* registry;
        VAddr cpu_address;

        // For Vulkan
        Vulkan::VKPipelineCache* pp_cache;
        std::vector<VkDescriptorSetLayoutBinding> bindings;
        Vulkan::SPIRVProgram program;
        Vulkan::RenderPassParams renderpass_params;
        u32 padding;
        std::array<GPUVAddr, Vulkan::Maxwell::MaxShaderProgram> shaders;
        Vulkan::FixedPipelineState fixed_state;
    };

    std::condition_variable cv;
    std::mutex queue_mutex;
    std::shared_mutex completed_mutex;
    std::atomic<bool> is_thread_exiting{};
    std::vector<std::unique_ptr<Core::Frontend::GraphicsContext>> context_list;
    std::vector<std::thread> worker_threads;
    std::queue<std::unique_ptr<WorkerParams>> pending_queue;
    std::vector<AsyncShaders::Result> finished_work;
    Core::Frontend::EmuWindow& emu_window;
};

} // namespace VideoCommon::Shader