| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/D3DCommon/Shader.h" |
| 5 | |
| 6 | #include <fstream> |
| 7 | #include <optional> |
| 8 | #include <string_view> |
| 9 | |
| 10 | #include <fmt/format.h> |
| 11 | #include <glslang/SPIRV/disassemble.h> |
| 12 | #include <wrl/client.h> |
| 13 | #include "spirv_hlsl.hpp" |
| 14 | |
| 15 | #include "Common/Assert.h" |
| 16 | #include "Common/FileUtil.h" |
| 17 | #include "Common/HRWrap.h" |
| 18 | #include "Common/Logging/Log.h" |
| 19 | #include "Common/MsgHandler.h" |
| 20 | #include "Common/StringUtil.h" |
| 21 | #include "Common/Version.h" |
| 22 | |
| 23 | #include "VideoCommon/ShaderCompileUtils.h" |
| 24 | #include "VideoCommon/Spirv.h" |
| 25 | #include "VideoCommon/VideoBackendBase.h" |
| 26 | #include "VideoCommon/VideoConfig.h" |
| 27 | |
| 28 | namespace |
| 29 | { |
| 30 | // Regarding the UBO bind points, we subtract one from the binding index because |
| 31 | // the OpenGL backend requires UBO #0 for non-block uniforms (at least on NV). |
| 32 | // This allows us to share the same shaders but use bind point #0 in the D3D |
| 33 | // backends. None of the specific shaders use UBOs, instead they use push |
| 34 | // constants, so when/if the GL backend moves to uniform blocks completely this |
| 35 | // subtraction can be removed. |
| 36 | constexpr std::string_view SHADER_HEADER = R"( |
| 37 | // Target GLSL 4.5. |
| 38 | #version 450 core |
| 39 | |
| 40 | #extension GL_ARB_shading_language_include : enable |
| 41 | |
| 42 | #define ATTRIBUTE_LOCATION(x) layout(location = x) |
| 43 | #define FRAGMENT_OUTPUT_LOCATION(x) layout(location = x) |
| 44 | #define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y) layout(location = x, index = y) |
| 45 | #define UBO_BINDING(packing, x) layout(packing, binding = (x - 1)) |
| 46 | #define SAMPLER_BINDING(x) layout(binding = x) |
| 47 | #define TEXEL_BUFFER_BINDING(x) layout(binding = x) |
| 48 | #define SSBO_BINDING(x) layout(binding = (x + 2)) |
| 49 | #define VARYING_LOCATION(x) layout(location = x) |
| 50 | #define FORCE_EARLY_Z layout(early_fragment_tests) in |
| 51 | |
| 52 | // hlsl to glsl function translation |
| 53 | #define float2 vec2 |
| 54 | #define float3 vec3 |
| 55 | #define float4 vec4 |
| 56 | #define uint2 uvec2 |
| 57 | #define uint3 uvec3 |
| 58 | #define uint4 uvec4 |
| 59 | #define int2 ivec2 |
| 60 | #define int3 ivec3 |
| 61 | #define int4 ivec4 |
| 62 | #define frac fract |
| 63 | #define lerp mix |
| 64 | |
| 65 | #define API_D3D 1 |
| 66 | )"; |
| 67 | constexpr std::string_view COMPUTE_SHADER_HEADER = R"( |
| 68 | // Target GLSL 4.5. |
| 69 | #version 450 core |
| 70 | // All resources are packed into one descriptor set for compute. |
| 71 | #define UBO_BINDING(packing, x) layout(packing, binding = (x - 1)) |
| 72 | #define SAMPLER_BINDING(x) layout(binding = x) |
| 73 | #define TEXEL_BUFFER_BINDING(x) layout(binding = x) |
| 74 | #define IMAGE_BINDING(format, x) layout(format, binding = x) |
| 75 | |
| 76 | // hlsl to glsl function translation |
| 77 | #define float2 vec2 |
| 78 | #define float3 vec3 |
| 79 | #define float4 vec4 |
| 80 | #define uint2 uvec2 |
| 81 | #define uint3 uvec3 |
| 82 | #define uint4 uvec4 |
| 83 | #define int2 ivec2 |
| 84 | #define int3 ivec3 |
| 85 | #define int4 ivec4 |
| 86 | #define frac fract |
| 87 | #define lerp mix |
| 88 | |
| 89 | #define API_D3D 1 |
| 90 | )"; |
| 91 | |
| 92 | std::optional<std::string> GetHLSLFromSPIRV(SPIRV::CodeVector spv, D3D_FEATURE_LEVEL feature_level) |
| 93 | { |
| 94 | spirv_cross::CompilerHLSL::Options options; |
| 95 | switch (feature_level) |
| 96 | { |
| 97 | case D3D_FEATURE_LEVEL_10_0: |
| 98 | options.shader_model = 40; |
| 99 | break; |
| 100 | case D3D_FEATURE_LEVEL_10_1: |
| 101 | options.shader_model = 41; |
| 102 | break; |
| 103 | default: |
| 104 | options.shader_model = 50; |
| 105 | break; |
| 106 | }; |
| 107 | |
| 108 | spirv_cross::CompilerHLSL compiler(std::move(spv)); |
| 109 | compiler.set_hlsl_options(options); |
| 110 | |
| 111 | return compiler.compile(); |
| 112 | } |
| 113 | |
| 114 | std::optional<SPIRV::CodeVector> GetSpirv(ShaderStage stage, std::string_view source, |
| 115 | glslang::TShader::Includer* shader_includer) |
| 116 | { |
| 117 | switch (stage) |
| 118 | { |
| 119 | case ShaderStage::Vertex: |
| 120 | { |
| 121 | const auto full_source = fmt::format("{}{}", SHADER_HEADER, source); |
| 122 | return SPIRV::CompileVertexShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0, |
| 123 | shader_includer); |
| 124 | } |
| 125 | |
| 126 | case ShaderStage::Geometry: |
| 127 | { |
| 128 | // Spirv cross does not currently support hlsl geometry shaders |
| 129 | return std::nullopt; |
| 130 | } |
| 131 | |
| 132 | case ShaderStage::Pixel: |
| 133 | { |
| 134 | const auto full_source = fmt::format("{}{}", SHADER_HEADER, source); |
| 135 | return SPIRV::CompileFragmentShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0, |
| 136 | shader_includer); |
| 137 | } |
| 138 | |
| 139 | case ShaderStage::Compute: |
| 140 | { |
| 141 | const auto full_source = fmt::format("{}{}", COMPUTE_SHADER_HEADER, source); |
| 142 | return SPIRV::CompileComputeShader(full_source, APIType::D3D, glslang::EShTargetSpv_1_0, |
| 143 | shader_includer); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | return std::nullopt; |
| 148 | } |
| 149 | |
| 150 | std::optional<std::string> GetHLSL(D3D_FEATURE_LEVEL feature_level, ShaderStage stage, |
| 151 | std::string_view source, |
| 152 | VideoCommon::ShaderIncluder* shader_includer) |
| 153 | { |
| 154 | if (stage == ShaderStage::Geometry) |
| 155 | { |
| 156 | return std::string{source}; |
| 157 | } |
| 158 | else if (const auto spirv = GetSpirv(stage, source, shader_includer)) |
| 159 | { |
| 160 | return GetHLSLFromSPIRV(std::move(*spirv), feature_level); |
| 161 | } |
| 162 | |
| 163 | return std::nullopt; |
| 164 | } |
| 165 | } // namespace |
| 166 | |
| 167 | namespace D3DCommon |
| 168 | { |
| 169 | Shader::Shader(ShaderStage stage, BinaryData bytecode) |
| 170 | : AbstractShader(stage), m_bytecode(std::move(bytecode)) |
| 171 | { |
| 172 | } |
| 173 | |
| 174 | Shader::~Shader() = default; |
| 175 | |
| 176 | AbstractShader::BinaryData Shader::GetBinary() const |
| 177 | { |
| 178 | return m_bytecode; |
| 179 | } |
| 180 | |
| 181 | static const char* GetCompileTarget(D3D_FEATURE_LEVEL feature_level, ShaderStage stage) |
| 182 | { |
| 183 | switch (stage) |
| 184 | { |
| 185 | case ShaderStage::Vertex: |
| 186 | { |
| 187 | switch (feature_level) |
| 188 | { |
| 189 | case D3D_FEATURE_LEVEL_10_0: |
| 190 | return "vs_4_0"; |
| 191 | case D3D_FEATURE_LEVEL_10_1: |
| 192 | return "vs_4_1"; |
| 193 | default: |
| 194 | return "vs_5_0"; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | case ShaderStage::Geometry: |
| 199 | { |
| 200 | switch (feature_level) |
| 201 | { |
| 202 | case D3D_FEATURE_LEVEL_10_0: |
| 203 | return "gs_4_0"; |
| 204 | case D3D_FEATURE_LEVEL_10_1: |
| 205 | return "gs_4_1"; |
| 206 | default: |
| 207 | return "gs_5_0"; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | case ShaderStage::Pixel: |
| 212 | { |
| 213 | switch (feature_level) |
| 214 | { |
| 215 | case D3D_FEATURE_LEVEL_10_0: |
| 216 | return "ps_4_0"; |
| 217 | case D3D_FEATURE_LEVEL_10_1: |
| 218 | return "ps_4_1"; |
| 219 | default: |
| 220 | return "ps_5_0"; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | case ShaderStage::Compute: |
| 225 | { |
| 226 | switch (feature_level) |
| 227 | { |
| 228 | case D3D_FEATURE_LEVEL_10_0: |
| 229 | case D3D_FEATURE_LEVEL_10_1: |
| 230 | return ""; |
| 231 | |
| 232 | default: |
| 233 | return "cs_5_0"; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | default: |
| 238 | return ""; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | std::optional<Shader::BinaryData> |
| 243 | Shader::CompileShader(D3D_FEATURE_LEVEL feature_level, ShaderStage stage, std::string_view source, |
| 244 | VideoCommon::ShaderIncluder* shader_includer) |
| 245 | { |
| 246 | const auto hlsl = GetHLSL(feature_level, stage, source, shader_includer); |
| 247 | if (!hlsl) |
| 248 | return std::nullopt; |
| 249 | |
| 250 | static constexpr D3D_SHADER_MACRO macros[] = {{"API_D3D", "1"}, {nullptr, nullptr}}; |
| 251 | const UINT flags = g_ActiveConfig.bEnableValidationLayer ? |
| 252 | (D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION) : |
| 253 | (D3DCOMPILE_OPTIMIZATION_LEVEL3 | D3DCOMPILE_SKIP_VALIDATION); |
| 254 | const char* target = GetCompileTarget(feature_level, stage); |
| 255 | |
| 256 | Microsoft::WRL::ComPtr<ID3DBlob> code; |
| 257 | Microsoft::WRL::ComPtr<ID3DBlob> errors; |
| 258 | HRESULT hr = d3d_compile(hlsl->data(), hlsl->size(), nullptr, macros, nullptr, "main", target, |
| 259 | flags, 0, &code, &errors); |
| 260 | if (FAILED(hr)) |
| 261 | { |
| 262 | static int num_failures = 0; |
| 263 | std::string filename = VideoBackendBase::BadShaderFilename(target, num_failures++); |
| 264 | std::ofstream file; |
| 265 | File::OpenFStream(file, filename, std::ios_base::out); |
| 266 | file.write(hlsl->data(), hlsl->size()); |
| 267 | file << "\n"; |
| 268 | file.write(static_cast<const char*>(errors->GetBufferPointer()), errors->GetBufferSize()); |
| 269 | file << "\n"; |
| 270 | file << "Dolphin Version: " + Common::GetScmRevStr() + "\n"; |
| 271 | file << "Video Backend: " + g_video_backend->GetDisplayName(); |
| 272 | |
| 273 | if (const auto spirv = GetSpirv(stage, source, shader_includer)) |
| 274 | { |
| 275 | file << "\nOriginal Source: \n"; |
| 276 | file << source << std::endl; |
| 277 | file << "SPIRV: \n"; |
| 278 | spv::Disassemble(file, *spirv); |
| 279 | } |
| 280 | file.close(); |
| 281 | |
| 282 | PanicAlertFmt("Failed to compile {}: {}\nDebug info ({}):\n{}", filename, Common::HRWrap(hr), |
| 283 | target, static_cast<const char*>(errors->GetBufferPointer())); |
| 284 | return std::nullopt; |
| 285 | } |
| 286 | |
| 287 | if (errors && errors->GetBufferSize() > 0) |
| 288 | { |
| 289 | WARN_LOG_FMT(VIDEO, "{} compilation succeeded with warnings:\n{}", target, |
| 290 | static_cast<const char*>(errors->GetBufferPointer())); |
| 291 | } |
| 292 | |
| 293 | return CreateByteCode(code->GetBufferPointer(), code->GetBufferSize()); |
| 294 | } |
| 295 | |
| 296 | AbstractShader::BinaryData Shader::CreateByteCode(const void* data, size_t length) |
| 297 | { |
| 298 | const auto* const begin = static_cast<const u8*>(data); |
| 299 | const auto* const end = begin + length; |
| 300 | |
| 301 | return {begin, end}; |
| 302 | } |
| 303 | |
| 304 | } // namespace D3DCommon |