renderer_opengl: refactor context acquire

This commit is contained in:
Liam 2022-12-13 12:30:15 -05:00 committed by Alexander Orzechowski
parent a4696285af
commit d5f53da79d
6 changed files with 62 additions and 38 deletions

View file

@ -223,8 +223,6 @@ struct GPU::Impl {
/// core timing events. /// core timing events.
void Start() { void Start() {
gpu_thread.StartThread(*renderer, renderer->Context(), *scheduler); gpu_thread.StartThread(*renderer, renderer->Context(), *scheduler);
cpu_context = renderer->GetRenderWindow().CreateSharedContext();
cpu_context->MakeCurrent();
} }
void NotifyShutdown() { void NotifyShutdown() {
@ -235,6 +233,9 @@ struct GPU::Impl {
/// Obtain the CPU Context /// Obtain the CPU Context
void ObtainContext() { void ObtainContext() {
if (!cpu_context) {
cpu_context = renderer->GetRenderWindow().CreateSharedContext();
}
cpu_context->MakeCurrent(); cpu_context->MakeCurrent();
} }

View file

@ -126,9 +126,11 @@ Device::Device() {
const bool is_intel = vendor_name == "Intel"; const bool is_intel = vendor_name == "Intel";
#ifdef __unix__ #ifdef __unix__
const bool is_linux = true; constexpr bool is_linux = true;
const bool is_wayland = strcasecmp(getenv("XDG_SESSION_TYPE"), "wayland") == 0;
#else #else
const bool is_linux = false; constexpr bool is_linux = false;
constexpr bool is_wayland = false;
#endif #endif
bool disable_fast_buffer_sub_data = false; bool disable_fast_buffer_sub_data = false;
@ -194,9 +196,11 @@ Device::Device() {
} }
// Blocks AMD and Intel OpenGL drivers on Windows from using asynchronous shader compilation. // Blocks AMD and Intel OpenGL drivers on Windows from using asynchronous shader compilation.
// Blocks EGL on Wayland from using asynchronous shader compilation.
use_asynchronous_shaders = Settings::values.use_asynchronous_shaders.GetValue() && use_asynchronous_shaders = Settings::values.use_asynchronous_shaders.GetValue() &&
!(is_amd || (is_intel && !is_linux)); !(is_amd || (is_intel && !is_linux)) && !is_wayland;
use_driver_cache = is_nvidia; use_driver_cache = is_nvidia;
strict_context_required = is_wayland;
LOG_INFO(Render_OpenGL, "Renderer_VariableAOFFI: {}", has_variable_aoffi); LOG_INFO(Render_OpenGL, "Renderer_VariableAOFFI: {}", has_variable_aoffi);
LOG_INFO(Render_OpenGL, "Renderer_ComponentIndexingBug: {}", has_component_indexing_bug); LOG_INFO(Render_OpenGL, "Renderer_ComponentIndexingBug: {}", has_component_indexing_bug);

View file

@ -173,6 +173,10 @@ public:
return can_report_memory; return can_report_memory;
} }
bool StrictContextRequired() const {
return strict_context_required;
}
private: private:
static bool TestVariableAoffi(); static bool TestVariableAoffi();
static bool TestPreciseBug(); static bool TestPreciseBug();
@ -216,6 +220,7 @@ private:
bool has_cbuf_ftou_bug{}; bool has_cbuf_ftou_bug{};
bool has_bool_ref_bug{}; bool has_bool_ref_bug{};
bool can_report_memory{}; bool can_report_memory{};
bool strict_context_required{};
std::string vendor_name; std::string vendor_name;
}; };

View file

@ -174,6 +174,7 @@ ShaderCache::ShaderCache(RasterizerOpenGL& rasterizer_, Core::Frontend::EmuWindo
texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, program_manager{program_manager_}, texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, program_manager{program_manager_},
state_tracker{state_tracker_}, shader_notify{shader_notify_}, state_tracker{state_tracker_}, shader_notify{shader_notify_},
use_asynchronous_shaders{device.UseAsynchronousShaders()}, use_asynchronous_shaders{device.UseAsynchronousShaders()},
strict_context_required{device.StrictContextRequired()},
profile{ profile{
.supported_spirv = 0x00010000, .supported_spirv = 0x00010000,
@ -255,9 +256,14 @@ void ShaderCache::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
} }
shader_cache_filename = base_dir / "opengl.bin"; shader_cache_filename = base_dir / "opengl.bin";
if (!workers) { if (!workers && !strict_context_required) {
workers = CreateWorkers(); workers = CreateWorkers();
} }
std::optional<Context> strict_context;
if (strict_context_required) {
strict_context.emplace(emu_window);
}
struct { struct {
std::mutex mutex; std::mutex mutex;
size_t total{}; size_t total{};
@ -265,11 +271,17 @@ void ShaderCache::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
bool has_loaded{}; bool has_loaded{};
} state; } state;
const auto queue_work{[&](Common::UniqueFunction<void, Context*>&& work) {
if (strict_context_required) {
work(&strict_context.value());
} else {
workers->QueueWork(std::move(work));
}
}};
const auto load_compute{[&](std::ifstream& file, FileEnvironment env) { const auto load_compute{[&](std::ifstream& file, FileEnvironment env) {
ComputePipelineKey key; ComputePipelineKey key;
file.read(reinterpret_cast<char*>(&key), sizeof(key)); file.read(reinterpret_cast<char*>(&key), sizeof(key));
workers->QueueWork( queue_work([this, key, env = std::move(env), &state, &callback](Context* ctx) mutable {
[this, key, env = std::move(env), &state, &callback](Context* ctx) mutable {
ctx->pools.ReleaseContents(); ctx->pools.ReleaseContents();
auto pipeline{CreateComputePipeline(ctx->pools, key, env)}; auto pipeline{CreateComputePipeline(ctx->pools, key, env)};
std::scoped_lock lock{state.mutex}; std::scoped_lock lock{state.mutex};
@ -286,8 +298,7 @@ void ShaderCache::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
const auto load_graphics{[&](std::ifstream& file, std::vector<FileEnvironment> envs) { const auto load_graphics{[&](std::ifstream& file, std::vector<FileEnvironment> envs) {
GraphicsPipelineKey key; GraphicsPipelineKey key;
file.read(reinterpret_cast<char*>(&key), sizeof(key)); file.read(reinterpret_cast<char*>(&key), sizeof(key));
workers->QueueWork( queue_work([this, key, envs = std::move(envs), &state, &callback](Context* ctx) mutable {
[this, key, envs = std::move(envs), &state, &callback](Context* ctx) mutable {
boost::container::static_vector<Shader::Environment*, 5> env_ptrs; boost::container::static_vector<Shader::Environment*, 5> env_ptrs;
for (auto& env : envs) { for (auto& env : envs) {
env_ptrs.push_back(&env); env_ptrs.push_back(&env);
@ -314,6 +325,9 @@ void ShaderCache::LoadDiskResources(u64 title_id, std::stop_token stop_loading,
state.has_loaded = true; state.has_loaded = true;
lock.unlock(); lock.unlock();
if (strict_context_required) {
return;
}
workers->WaitForRequests(stop_loading); workers->WaitForRequests(stop_loading);
if (!use_asynchronous_shaders) { if (!use_asynchronous_shaders) {
workers.reset(); workers.reset();

View file

@ -69,6 +69,7 @@ private:
StateTracker& state_tracker; StateTracker& state_tracker;
VideoCore::ShaderNotify& shader_notify; VideoCore::ShaderNotify& shader_notify;
const bool use_asynchronous_shaders; const bool use_asynchronous_shaders;
const bool strict_context_required;
GraphicsPipelineKey graphics_key{}; GraphicsPipelineKey graphics_key{};
GraphicsPipeline* current_pipeline{}; GraphicsPipeline* current_pipeline{};

View file

@ -61,8 +61,6 @@ void EmuThread::run() {
// Main process has been loaded. Make the context current to this thread and begin GPU and CPU // Main process has been loaded. Make the context current to this thread and begin GPU and CPU
// execution. // execution.
gpu.Start();
gpu.ObtainContext(); gpu.ObtainContext();
emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0); emit LoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);
@ -77,6 +75,7 @@ void EmuThread::run() {
emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0); emit LoadProgress(VideoCore::LoadCallbackStage::Complete, 0, 0);
gpu.ReleaseContext(); gpu.ReleaseContext();
gpu.Start();
system.GetCpuManager().OnGpuReady(); system.GetCpuManager().OnGpuReady();