| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/ShaderCompiler.h" |
| 5 | |
| 6 | #include <cstddef> |
| 7 | #include <string> |
| 8 | |
| 9 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 10 | #include "VideoCommon/DriverDetails.h" |
| 11 | #include "VideoCommon/ShaderCompileUtils.h" |
| 12 | #include "VideoCommon/Spirv.h" |
| 13 | |
| 14 | namespace Vulkan::ShaderCompiler |
| 15 | { |
| 16 | // Regarding the UBO bind points, we subtract one from the binding index because |
| 17 | // the OpenGL backend requires UBO #0 for non-block uniforms (at least on NV). |
| 18 | // This allows us to share the same shaders but use bind point #0 in the Vulkan |
| 19 | // backend. None of the Vulkan-specific shaders use UBOs, instead they use push |
| 20 | // constants, so when/if the GL backend moves to uniform blocks completely this |
| 21 | // subtraction can be removed. |
| 22 | static const char SHADER_HEADER[] = R"( |
| 23 | // Target GLSL 4.5. |
| 24 | #version 450 core |
| 25 | |
| 26 | #extension GL_ARB_shading_language_include : enable |
| 27 | |
| 28 | #define ATTRIBUTE_LOCATION(x) layout(location = x) |
| 29 | #define FRAGMENT_OUTPUT_LOCATION(x) layout(location = x) |
| 30 | #define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y) layout(location = x, index = y) |
| 31 | #define UBO_BINDING(packing, x) layout(packing, set = 0, binding = (x - 1)) |
| 32 | #define SAMPLER_BINDING(x) layout(set = 1, binding = x) |
| 33 | #define TEXEL_BUFFER_BINDING(x) layout(set = 1, binding = (x + 8)) |
| 34 | #define SSBO_BINDING(x) layout(std430, set = 2, binding = x) |
| 35 | #define INPUT_ATTACHMENT_BINDING(x, y, z) layout(set = x, binding = y, input_attachment_index = z) |
| 36 | #define VARYING_LOCATION(x) layout(location = x) |
| 37 | #define FORCE_EARLY_Z layout(early_fragment_tests) in |
| 38 | |
| 39 | // Metal framebuffer fetch helpers. |
| 40 | #define FB_FETCH_VALUE subpassLoad(in_ocol0) |
| 41 | |
| 42 | // hlsl to glsl function translation |
| 43 | #define API_VULKAN 1 |
| 44 | #define float2 vec2 |
| 45 | #define float3 vec3 |
| 46 | #define float4 vec4 |
| 47 | #define uint2 uvec2 |
| 48 | #define uint3 uvec3 |
| 49 | #define uint4 uvec4 |
| 50 | #define int2 ivec2 |
| 51 | #define int3 ivec3 |
| 52 | #define int4 ivec4 |
| 53 | #define frac fract |
| 54 | #define lerp mix |
| 55 | |
| 56 | // These were changed in Vulkan |
| 57 | #define gl_VertexID gl_VertexIndex |
| 58 | #define gl_InstanceID gl_InstanceIndex |
| 59 | )"; |
| 60 | static const char COMPUTE_SHADER_HEADER[] = R"( |
| 61 | // Target GLSL 4.5. |
| 62 | #version 450 core |
| 63 | // All resources are packed into one descriptor set for compute. |
| 64 | #define UBO_BINDING(packing, x) layout(packing, set = 0, binding = (x - 1)) |
| 65 | #define SAMPLER_BINDING(x) layout(set = 0, binding = (1 + x)) |
| 66 | #define TEXEL_BUFFER_BINDING(x) layout(set = 0, binding = (9 + x)) |
| 67 | #define IMAGE_BINDING(format, x) layout(format, set = 0, binding = (11 + x)) |
| 68 | |
| 69 | // hlsl to glsl function translation |
| 70 | #define API_VULKAN 1 |
| 71 | #define float2 vec2 |
| 72 | #define float3 vec3 |
| 73 | #define float4 vec4 |
| 74 | #define uint2 uvec2 |
| 75 | #define uint3 uvec3 |
| 76 | #define uint4 uvec4 |
| 77 | #define int2 ivec2 |
| 78 | #define int3 ivec3 |
| 79 | #define int4 ivec4 |
| 80 | #define frac fract |
| 81 | #define lerp mix |
| 82 | )"; |
| 83 | static const char SUBGROUP_HELPER_HEADER[] = R"( |
| 84 | #extension GL_KHR_shader_subgroup_basic : enable |
| 85 | #extension GL_KHR_shader_subgroup_arithmetic : enable |
| 86 | #extension GL_KHR_shader_subgroup_ballot : enable |
| 87 | #extension GL_KHR_shader_subgroup_shuffle : enable |
| 88 | |
| 89 | #define SUPPORTS_SUBGROUP_REDUCTION 1 |
| 90 | #define IS_HELPER_INVOCATION gl_HelperInvocation |
| 91 | #define IS_FIRST_ACTIVE_INVOCATION (subgroupElect()) |
| 92 | #define SUBGROUP_MIN(value) value = subgroupMin(value) |
| 93 | #define SUBGROUP_MAX(value) value = subgroupMax(value) |
| 94 | )"; |
| 95 | |
| 96 | static std::string GetShaderCode(std::string_view source, std::string_view header) |
| 97 | { |
| 98 | std::string full_source_code; |
| 99 | if (!header.empty()) |
| 100 | { |
| 101 | constexpr size_t subgroup_helper_header_length = std::size(SUBGROUP_HELPER_HEADER) - 1; |
| 102 | full_source_code.reserve(header.size() + subgroup_helper_header_length + source.size()); |
| 103 | full_source_code.append(header); |
| 104 | if (g_vulkan_context->SupportsShaderSubgroupOperations()) |
| 105 | full_source_code.append(SUBGROUP_HELPER_HEADER, subgroup_helper_header_length); |
| 106 | if (DriverDetails::HasBug(DriverDetails::BUG_INVERTED_IS_HELPER)) |
| 107 | { |
| 108 | full_source_code.append("#define gl_HelperInvocation !gl_HelperInvocation " |
| 109 | "// Work around broken AMD Metal driver\n"); |
| 110 | } |
| 111 | if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_SUBGROUP_OPS_WITH_DISCARD)) |
| 112 | full_source_code.append("#define BROKEN_SUBGROUP_WITH_DISCARD 1\n"); |
| 113 | full_source_code.append(source); |
| 114 | } |
| 115 | |
| 116 | return full_source_code; |
| 117 | } |
| 118 | |
| 119 | static glslang::EShTargetLanguageVersion GetLanguageVersion() |
| 120 | { |
| 121 | // Sub-group operations require Vulkan 1.1 and SPIR-V 1.3. |
| 122 | if (g_vulkan_context->SupportsShaderSubgroupOperations()) |
| 123 | return glslang::EShTargetSpv_1_3; |
| 124 | |
| 125 | return glslang::EShTargetSpv_1_0; |
| 126 | } |
| 127 | |
| 128 | std::optional<SPIRVCodeVector> CompileVertexShader(std::string_view source_code, |
| 129 | VideoCommon::ShaderIncluder* shader_includer) |
| 130 | { |
| 131 | return SPIRV::CompileVertexShader(GetShaderCode(source_code, SHADER_HEADER), APIType::Vulkan, |
| 132 | GetLanguageVersion(), shader_includer); |
| 133 | } |
| 134 | |
| 135 | std::optional<SPIRVCodeVector> CompileGeometryShader(std::string_view source_code, |
| 136 | VideoCommon::ShaderIncluder* shader_includer) |
| 137 | { |
| 138 | return SPIRV::CompileGeometryShader(GetShaderCode(source_code, SHADER_HEADER), APIType::Vulkan, |
| 139 | GetLanguageVersion(), shader_includer); |
| 140 | } |
| 141 | |
| 142 | std::optional<SPIRVCodeVector> CompileFragmentShader(std::string_view source_code, |
| 143 | VideoCommon::ShaderIncluder* shader_includer) |
| 144 | { |
| 145 | return SPIRV::CompileFragmentShader(GetShaderCode(source_code, SHADER_HEADER), APIType::Vulkan, |
| 146 | GetLanguageVersion(), shader_includer); |
| 147 | } |
| 148 | |
| 149 | std::optional<SPIRVCodeVector> CompileComputeShader(std::string_view source_code, |
| 150 | VideoCommon::ShaderIncluder* shader_includer) |
| 151 | { |
| 152 | return SPIRV::CompileComputeShader(GetShaderCode(source_code, COMPUTE_SHADER_HEADER), |
| 153 | APIType::Vulkan, GetLanguageVersion(), shader_includer); |
| 154 | } |
| 155 | } // namespace Vulkan::ShaderCompiler |