VERY experimental Nintendo Switch 2 emulator
C++
video_core: Enable GL SPIR-V shaders
lat9nq5443d4bd9c53
diff --git a/src/video_core/renderer_opengl/gl_compute_pipeline.cpp b/src/video_core/renderer_opengl/gl_compute_pipeline.cppindex 2d6442d744..c63e87a568 100644--- a/src/video_core/renderer_opengl/gl_compute_pipeline.cpp+++ b/src/video_core/renderer_opengl/gl_compute_pipeline.cpp@@ -5,6 +5,7 @@#include <cstring>#include "common/cityhash.h"+#include "common/settings.h" // for enum class Settings::ShaderBackend#include "video_core/renderer_opengl/gl_compute_pipeline.h"#include "video_core/renderer_opengl/gl_shader_manager.h"#include "video_core/renderer_opengl/gl_shader_util.h"@@ -40,15 +41,23 @@ ComputePipeline::ComputePipeline(const Device& device, TextureCache& texture_cacBufferCache& buffer_cache_, Tegra::MemoryManager& gpu_memory_,Tegra::Engines::KeplerCompute& kepler_compute_,ProgramManager& program_manager_, const Shader::Info& info_,- std::string code)+ std::string code, std::vector<u32> code_v): texture_cache{texture_cache_}, buffer_cache{buffer_cache_}, gpu_memory{gpu_memory_},kepler_compute{kepler_compute_}, program_manager{program_manager_}, info{info_} {- if (device.UseAssemblyShaders()) {- assembly_program = CompileProgram(code, GL_COMPUTE_PROGRAM_NV);- } else {+ switch (device.GetShaderBackend()) {+ case Settings::ShaderBackend::GLSL:source_program.handle = glCreateProgram();AttachShader(GL_COMPUTE_SHADER, source_program.handle, code);LinkProgram(source_program.handle);+ break;+ case Settings::ShaderBackend::GLASM:+ assembly_program = CompileProgram(code, GL_COMPUTE_PROGRAM_NV);+ break;+ case Settings::ShaderBackend::SPIRV:+ source_program.handle = glCreateProgram();+ AttachShader(GL_COMPUTE_SHADER, source_program.handle, code_v);+ LinkProgram(source_program.handle);+ break;}std::copy_n(info.constant_buffer_used_sizes.begin(), uniform_buffer_sizes.size(),uniform_buffer_sizes.begin());diff --git a/src/video_core/renderer_opengl/gl_compute_pipeline.h b/src/video_core/renderer_opengl/gl_compute_pipeline.hindex b5fc45f26c..50c676365b 100644--- a/src/video_core/renderer_opengl/gl_compute_pipeline.h+++ b/src/video_core/renderer_opengl/gl_compute_pipeline.h@@ -54,7 +54,7 @@ public:BufferCache& buffer_cache_, Tegra::MemoryManager& gpu_memory_,Tegra::Engines::KeplerCompute& kepler_compute_,ProgramManager& program_manager_, const Shader::Info& info_,- std::string code);+ std::string code, std::vector<u32> code_v);void Configure();diff --git a/src/video_core/renderer_opengl/gl_device.cpp b/src/video_core/renderer_opengl/gl_device.cppindex c4eeed53b6..99f8769fcf 100644--- a/src/video_core/renderer_opengl/gl_device.cpp+++ b/src/video_core/renderer_opengl/gl_device.cpp@@ -177,6 +177,11 @@ Device::Device() {GLAD_GL_NV_gpu_program5 && GLAD_GL_NV_compute_program5 && GLAD_GL_NV_transform_feedback &&GLAD_GL_NV_transform_feedback2;+ shader_backend = (Settings::values.shader_backend.GetValue() ==+ Settings::ShaderBackend::GLASM) == use_assembly_shaders+ ? Settings::values.shader_backend.GetValue()+ : Settings::ShaderBackend::GLSL;+// Blocks AMD and Intel OpenGL drivers on Windows from using asynchronous shader compilation.use_asynchronous_shaders = Settings::values.use_asynchronous_shaders.GetValue() &&!(is_amd || (is_intel && !is_linux));@@ -188,8 +193,7 @@ Device::Device() {LOG_INFO(Render_OpenGL, "Renderer_BrokenTextureViewFormats: {}",has_broken_texture_view_formats);- if (Settings::values.shader_backend.GetValue() == Settings::ShaderBackend::GLASM &&- !use_assembly_shaders) {+ if (shader_backend == Settings::ShaderBackend::GLASM && !use_assembly_shaders) {LOG_ERROR(Render_OpenGL, "Assembly shaders enabled but not supported");}diff --git a/src/video_core/renderer_opengl/gl_device.h b/src/video_core/renderer_opengl/gl_device.hindex 45ddf5e010..ee992aed4a 100644--- a/src/video_core/renderer_opengl/gl_device.h+++ b/src/video_core/renderer_opengl/gl_device.h@@ -8,6 +8,10 @@#include "common/common_types.h"#include "shader_recompiler/stage.h"+namespace Settings {+enum class ShaderBackend : u32;+};+namespace OpenGL {class Device {@@ -148,6 +152,10 @@ public:return need_fastmath_off;}+ Settings::ShaderBackend GetShaderBackend() const {+ return shader_backend;+ }+private:static bool TestVariableAoffi();static bool TestPreciseBug();@@ -159,6 +167,9 @@ private:u32 max_varyings{};u32 max_compute_shared_memory_size{};u32 max_glasm_storage_buffer_blocks{};++ Settings::ShaderBackend shader_backend{};+bool has_warp_intrinsics{};bool has_shader_ballot{};bool has_vertex_viewport_layer{};diff --git a/src/video_core/renderer_opengl/gl_graphics_pipeline.cpp b/src/video_core/renderer_opengl/gl_graphics_pipeline.cppindex a93b03cf71..1f19b58258 100644--- a/src/video_core/renderer_opengl/gl_graphics_pipeline.cpp+++ b/src/video_core/renderer_opengl/gl_graphics_pipeline.cpp@@ -3,7 +3,11 @@// Refer to the license.txt file included.#include <algorithm>+#include <array>+#include <string>+#include <vector>+#include "common/settings.h" // for enum class Settings::ShaderBackend#include "common/thread_worker.h"#include "shader_recompiler/shader_info.h"#include "video_core/renderer_opengl/gl_graphics_pipeline.h"@@ -179,7 +183,8 @@ GraphicsPipeline::GraphicsPipeline(Tegra::MemoryManager& gpu_memory_, Tegra::Engines::Maxwell3D& maxwell3d_,ProgramManager& program_manager_, StateTracker& state_tracker_, ShaderWorker* thread_worker,VideoCore::ShaderNotify* shader_notify, std::array<std::string, 5> sources,- const std::array<const Shader::Info*, 5>& infos, const GraphicsPipelineKey& key_)+ std::array<std::vector<u32>, 5> sources_spirv, const std::array<const Shader::Info*, 5>& infos,+ const GraphicsPipelineKey& key_): texture_cache{texture_cache_}, buffer_cache{buffer_cache_},gpu_memory{gpu_memory_}, maxwell3d{maxwell3d_}, program_manager{program_manager_},state_tracker{state_tracker_}, key{key_} {@@ -232,29 +237,44 @@ GraphicsPipeline::GraphicsPipeline(if (key.xfb_enabled && device.UseAssemblyShaders()) {GenerateTransformFeedbackState();}- auto func{[this, device, sources, shader_notify](ShaderContext::Context*) mutable {- if (!device.UseAssemblyShaders()) {- program.handle = glCreateProgram();- }- for (size_t stage = 0; stage < 5; ++stage) {- const auto code{sources[stage]};- if (code.empty()) {- continue;+ auto func{+ [this, device, sources, sources_spirv, shader_notify](ShaderContext::Context*) mutable {+ if (!device.UseAssemblyShaders()) {+ program.handle = glCreateProgram();}- if (device.UseAssemblyShaders()) {- assembly_programs[stage] = CompileProgram(code, AssemblyStage(stage));- } else {- AttachShader(Stage(stage), program.handle, code);+ for (size_t stage = 0; stage < 5; ++stage) {+ switch (device.GetShaderBackend()) {+ case Settings::ShaderBackend::GLSL: {+ const auto code{sources[stage]};+ if (code.empty()) {+ continue;+ }+ AttachShader(Stage(stage), program.handle, code);+ } break;+ case Settings::ShaderBackend::GLASM: {+ const auto code{sources[stage]};+ if (code.empty()) {+ continue;+ }+ assembly_programs[stage] = CompileProgram(code, AssemblyStage(stage));+ } break;+ case Settings::ShaderBackend::SPIRV: {+ const auto code{sources_spirv[stage]};+ if (code.empty()) {+ continue;+ }+ AttachShader(Stage(stage), program.handle, code);+ } break;+ }}- }- if (!device.UseAssemblyShaders()) {- LinkProgram(program.handle);- }- if (shader_notify) {- shader_notify->MarkShaderComplete();- }- is_built.store(true, std::memory_order_relaxed);- }};+ if (!device.UseAssemblyShaders()) {+ LinkProgram(program.handle);+ }+ if (shader_notify) {+ shader_notify->MarkShaderComplete();+ }+ is_built.store(true, std::memory_order_relaxed);+ }};if (thread_worker) {thread_worker->QueueWork(std::move(func));} else {diff --git a/src/video_core/renderer_opengl/gl_graphics_pipeline.h b/src/video_core/renderer_opengl/gl_graphics_pipeline.hindex f82d712f83..5f5d573855 100644--- a/src/video_core/renderer_opengl/gl_graphics_pipeline.h+++ b/src/video_core/renderer_opengl/gl_graphics_pipeline.h@@ -78,6 +78,7 @@ public:ProgramManager& program_manager_, StateTracker& state_tracker_,ShaderWorker* thread_worker, VideoCore::ShaderNotify* shader_notify,std::array<std::string, 5> sources,+ std::array<std::vector<u32>, 5> sources_spirv,const std::array<const Shader::Info*, 5>& infos,const GraphicsPipelineKey& key_);diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cppindex af8e9f44db..cde0f54c9d 100644--- a/src/video_core/renderer_opengl/gl_shader_cache.cpp+++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp@@ -15,6 +15,7 @@#include "common/fs/path_util.h"#include "common/logging/log.h"#include "common/scope_exit.h"+#include "common/settings.h"#include "common/thread_worker.h"#include "core/core.h"#include "shader_recompiler/backend/glasm/emit_glasm.h"@@ -415,6 +416,7 @@ std::unique_ptr<GraphicsPipeline> ShaderCache::CreateGraphicsPipeline(OGLProgram source_program;std::array<std::string, 5> sources;+ std::array<std::vector<u32>, 5> sources_spirv;Shader::Backend::Bindings binding;Shader::IR::Program* previous_program{};const bool use_glasm{device.UseAssemblyShaders()};@@ -431,17 +433,23 @@ std::unique_ptr<GraphicsPipeline> ShaderCache::CreateGraphicsPipeline(const auto runtime_info{MakeRuntimeInfo(key, program, previous_program, glasm_use_storage_buffers, use_glasm)};- if (use_glasm) {- sources[stage_index] = EmitGLASM(profile, runtime_info, program, binding);- } else {+ switch (device.GetShaderBackend()) {+ case Settings::ShaderBackend::GLSL:sources[stage_index] = EmitGLSL(profile, runtime_info, program, binding);+ break;+ case Settings::ShaderBackend::GLASM:+ sources[stage_index] = EmitGLASM(profile, runtime_info, program, binding);+ break;+ case Settings::ShaderBackend::SPIRV:+ sources_spirv[stage_index] = EmitSPIRV(profile, runtime_info, program, binding);+ break;}previous_program = &program;}auto* const thread_worker{build_in_parallel ? workers.get() : nullptr};- return std::make_unique<GraphicsPipeline>(device, texture_cache, buffer_cache, gpu_memory,- maxwell3d, program_manager, state_tracker,- thread_worker, &shader_notify, sources, infos, key);+ return std::make_unique<GraphicsPipeline>(+ device, texture_cache, buffer_cache, gpu_memory, maxwell3d, program_manager, state_tracker,+ thread_worker, &shader_notify, sources, sources_spirv, infos, key);} catch (Shader::Exception& exception) {LOG_ERROR(Render_OpenGL, "{}", exception.what());@@ -478,10 +486,24 @@ std::unique_ptr<ComputePipeline> ShaderCache::CreateComputePipeline(}Shader::RuntimeInfo info;info.glasm_use_storage_buffers = num_storage_buffers <= device.GetMaxGLASMStorageBufferBlocks();- const std::string code{device.UseAssemblyShaders() ? EmitGLASM(profile, info, program)- : EmitGLSL(profile, program)};++ std::string code{};+ std::vector<u32> code_spirv;+ switch (device.GetShaderBackend()) {+ case Settings::ShaderBackend::GLSL:+ code = EmitGLSL(profile, program);+ break;+ case Settings::ShaderBackend::GLASM:+ code = EmitGLASM(profile, info, program);+ break;+ case Settings::ShaderBackend::SPIRV:+ code_spirv = EmitSPIRV(profile, program);+ break;+ }+return std::make_unique<ComputePipeline>(device, texture_cache, buffer_cache, gpu_memory,- kepler_compute, program_manager, program.info, code);+ kepler_compute, program_manager, program.info, code,+ code_spirv);} catch (Shader::Exception& exception) {LOG_ERROR(Render_OpenGL, "{}", exception.what());return nullptr;