2016-08-13 12:57:50 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-10-01 03:07:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/ShaderCompiler.h"
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdlib>
|
2017-10-10 13:09:12 +00:00
|
|
|
#include <cstring>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <fstream>
|
2016-10-01 03:07:50 +00:00
|
|
|
#include <memory>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
// glslang includes
|
|
|
|
#include "GlslangToSpv.h"
|
2020-03-02 15:56:41 +00:00
|
|
|
#include "ResourceLimits.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "ShaderLang.h"
|
|
|
|
#include "disassemble.h"
|
|
|
|
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2020-01-24 08:29:38 +00:00
|
|
|
#include "Common/Version.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
2020-01-24 08:29:38 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
|
|
|
|
2019-06-17 20:57:30 +00:00
|
|
|
namespace Vulkan::ShaderCompiler
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Registers itself for cleanup via atexit
|
|
|
|
bool InitializeGlslang();
|
|
|
|
|
|
|
|
// Resource limits used when compiling shaders
|
|
|
|
static const TBuiltInResource* GetCompilerResourceLimits();
|
|
|
|
|
|
|
|
// Regarding the UBO bind points, we subtract one from the binding index because
|
|
|
|
// the OpenGL backend requires UBO #0 for non-block uniforms (at least on NV).
|
|
|
|
// This allows us to share the same shaders but use bind point #0 in the Vulkan
|
|
|
|
// backend. None of the Vulkan-specific shaders use UBOs, instead they use push
|
|
|
|
// constants, so when/if the GL backend moves to uniform blocks completely this
|
|
|
|
// subtraction can be removed.
|
|
|
|
static const char SHADER_HEADER[] = R"(
|
|
|
|
// Target GLSL 4.5.
|
|
|
|
#version 450 core
|
|
|
|
#define ATTRIBUTE_LOCATION(x) layout(location = x)
|
|
|
|
#define FRAGMENT_OUTPUT_LOCATION(x) layout(location = x)
|
|
|
|
#define FRAGMENT_OUTPUT_LOCATION_INDEXED(x, y) layout(location = x, index = y)
|
|
|
|
#define UBO_BINDING(packing, x) layout(packing, set = 0, binding = (x - 1))
|
|
|
|
#define SAMPLER_BINDING(x) layout(set = 1, binding = x)
|
2019-02-15 01:59:50 +00:00
|
|
|
#define TEXEL_BUFFER_BINDING(x) layout(set = 1, binding = (x + 8))
|
2016-08-13 12:57:50 +00:00
|
|
|
#define SSBO_BINDING(x) layout(set = 2, binding = x)
|
2021-07-30 08:09:22 +00:00
|
|
|
#define INPUT_ATTACHMENT_BINDING(x, y, z) layout(set = x, binding = y, input_attachment_index = z)
|
2016-08-13 12:57:50 +00:00
|
|
|
#define VARYING_LOCATION(x) layout(location = x)
|
|
|
|
#define FORCE_EARLY_Z layout(early_fragment_tests) in
|
|
|
|
|
2021-08-03 03:40:59 +00:00
|
|
|
// Metal framebuffer fetch helpers.
|
|
|
|
#define FB_FETCH_VALUE subpassLoad(in_ocol0)
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// hlsl to glsl function translation
|
2019-02-15 01:59:50 +00:00
|
|
|
#define API_VULKAN 1
|
2016-08-13 12:57:50 +00:00
|
|
|
#define float2 vec2
|
|
|
|
#define float3 vec3
|
|
|
|
#define float4 vec4
|
|
|
|
#define uint2 uvec2
|
|
|
|
#define uint3 uvec3
|
|
|
|
#define uint4 uvec4
|
|
|
|
#define int2 ivec2
|
|
|
|
#define int3 ivec3
|
|
|
|
#define int4 ivec4
|
|
|
|
#define frac fract
|
|
|
|
#define lerp mix
|
|
|
|
|
|
|
|
// These were changed in Vulkan
|
|
|
|
#define gl_VertexID gl_VertexIndex
|
|
|
|
#define gl_InstanceID gl_InstanceIndex
|
|
|
|
)";
|
2016-12-09 12:23:04 +00:00
|
|
|
static const char COMPUTE_SHADER_HEADER[] = R"(
|
|
|
|
// Target GLSL 4.5.
|
|
|
|
#version 450 core
|
|
|
|
// All resources are packed into one descriptor set for compute.
|
2019-02-15 01:59:50 +00:00
|
|
|
#define UBO_BINDING(packing, x) layout(packing, set = 0, binding = (x - 1))
|
2016-12-09 12:23:04 +00:00
|
|
|
#define SAMPLER_BINDING(x) layout(set = 0, binding = (1 + x))
|
2019-02-15 01:59:50 +00:00
|
|
|
#define TEXEL_BUFFER_BINDING(x) layout(set = 0, binding = (3 + x))
|
|
|
|
#define IMAGE_BINDING(format, x) layout(format, set = 0, binding = (5 + x))
|
2016-12-09 12:23:04 +00:00
|
|
|
|
|
|
|
// hlsl to glsl function translation
|
2019-02-15 01:59:50 +00:00
|
|
|
#define API_VULKAN 1
|
2016-12-09 12:23:04 +00:00
|
|
|
#define float2 vec2
|
|
|
|
#define float3 vec3
|
|
|
|
#define float4 vec4
|
|
|
|
#define uint2 uvec2
|
|
|
|
#define uint3 uvec3
|
|
|
|
#define uint4 uvec4
|
|
|
|
#define int2 ivec2
|
|
|
|
#define int3 ivec3
|
|
|
|
#define int4 ivec4
|
|
|
|
#define frac fract
|
|
|
|
#define lerp mix
|
|
|
|
)";
|
2019-03-22 10:39:37 +00:00
|
|
|
static const char SUBGROUP_HELPER_HEADER[] = R"(
|
|
|
|
#extension GL_KHR_shader_subgroup_basic : enable
|
|
|
|
#extension GL_KHR_shader_subgroup_arithmetic : enable
|
|
|
|
#extension GL_KHR_shader_subgroup_ballot : enable
|
|
|
|
|
|
|
|
#define SUPPORTS_SUBGROUP_REDUCTION 1
|
|
|
|
#define CAN_USE_SUBGROUP_REDUCTION true
|
|
|
|
#define IS_HELPER_INVOCATION gl_HelperInvocation
|
2021-06-08 02:32:25 +00:00
|
|
|
#define IS_FIRST_ACTIVE_INVOCATION (gl_SubgroupInvocationID == subgroupBallotFindLSB(subgroupBallot(!gl_HelperInvocation)))
|
2019-03-22 10:39:37 +00:00
|
|
|
#define SUBGROUP_MIN(value) value = subgroupMin(value)
|
|
|
|
#define SUBGROUP_MAX(value) value = subgroupMax(value)
|
|
|
|
)";
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2020-03-23 07:59:19 +00:00
|
|
|
static std::optional<SPIRVCodeVector> CompileShaderToSPV(EShLanguage stage,
|
|
|
|
const char* stage_filename,
|
|
|
|
std::string_view source,
|
|
|
|
std::string_view header)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
if (!InitializeGlslang())
|
2019-05-30 07:52:05 +00:00
|
|
|
return std::nullopt;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
std::unique_ptr<glslang::TShader> shader = std::make_unique<glslang::TShader>(stage);
|
|
|
|
std::unique_ptr<glslang::TProgram> program;
|
2018-06-02 07:26:43 +00:00
|
|
|
glslang::TShader::ForbidIncluder includer;
|
2016-08-13 12:57:50 +00:00
|
|
|
EProfile profile = ECoreProfile;
|
|
|
|
EShMessages messages =
|
|
|
|
static_cast<EShMessages>(EShMsgDefault | EShMsgSpvRules | EShMsgVulkanRules);
|
|
|
|
int default_version = 450;
|
|
|
|
|
|
|
|
std::string full_source_code;
|
2019-05-30 07:07:40 +00:00
|
|
|
const char* pass_source_code = source.data();
|
|
|
|
int pass_source_code_length = static_cast<int>(source.size());
|
|
|
|
if (!header.empty())
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-05-30 07:54:56 +00:00
|
|
|
constexpr size_t subgroup_helper_header_length = std::size(SUBGROUP_HELPER_HEADER) - 1;
|
2019-05-30 07:07:40 +00:00
|
|
|
full_source_code.reserve(header.size() + subgroup_helper_header_length + source.size());
|
|
|
|
full_source_code.append(header);
|
2019-03-22 10:39:37 +00:00
|
|
|
if (g_vulkan_context->SupportsShaderSubgroupOperations())
|
|
|
|
full_source_code.append(SUBGROUP_HELPER_HEADER, subgroup_helper_header_length);
|
2019-05-30 07:07:40 +00:00
|
|
|
full_source_code.append(source);
|
2016-08-13 12:57:50 +00:00
|
|
|
pass_source_code = full_source_code.c_str();
|
|
|
|
pass_source_code_length = static_cast<int>(full_source_code.length());
|
|
|
|
}
|
|
|
|
|
2019-03-22 10:39:37 +00:00
|
|
|
// Sub-group operations require Vulkan 1.1 and SPIR-V 1.3.
|
|
|
|
if (g_vulkan_context->SupportsShaderSubgroupOperations())
|
|
|
|
shader->setEnvTarget(glslang::EShTargetSpv, glslang::EShTargetSpv_1_3);
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
shader->setStringsWithLengths(&pass_source_code, &pass_source_code_length, 1);
|
|
|
|
|
|
|
|
auto DumpBadShader = [&](const char* msg) {
|
|
|
|
static int counter = 0;
|
2020-01-24 08:29:38 +00:00
|
|
|
std::string filename = VideoBackendBase::BadShaderFilename(stage_filename, counter++);
|
2016-08-13 12:57:50 +00:00
|
|
|
std::ofstream stream;
|
2017-01-15 21:46:43 +00:00
|
|
|
File::OpenFStream(stream, filename, std::ios_base::out);
|
2016-08-13 12:57:50 +00:00
|
|
|
if (stream.good())
|
|
|
|
{
|
|
|
|
stream << full_source_code << std::endl;
|
|
|
|
stream << msg << std::endl;
|
|
|
|
stream << "Shader Info Log:" << std::endl;
|
|
|
|
stream << shader->getInfoLog() << std::endl;
|
|
|
|
stream << shader->getInfoDebugLog() << std::endl;
|
|
|
|
if (program)
|
|
|
|
{
|
|
|
|
stream << "Program Info Log:" << std::endl;
|
|
|
|
stream << program->getInfoLog() << std::endl;
|
|
|
|
stream << program->getInfoDebugLog() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 08:29:38 +00:00
|
|
|
stream << "\n";
|
2022-01-13 23:04:22 +00:00
|
|
|
stream << "Dolphin Version: " + Common::GetScmRevStr() + "\n";
|
2020-01-24 08:29:38 +00:00
|
|
|
stream << "Video Backend: " + g_video_backend->GetDisplayName();
|
|
|
|
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("{} (written to {})", msg, filename);
|
2016-08-13 12:57:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!shader->parse(GetCompilerResourceLimits(), default_version, profile, false, true, messages,
|
|
|
|
includer))
|
|
|
|
{
|
|
|
|
DumpBadShader("Failed to parse shader");
|
2019-05-30 07:52:05 +00:00
|
|
|
return std::nullopt;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Even though there's only a single shader, we still need to link it to generate SPV
|
|
|
|
program = std::make_unique<glslang::TProgram>();
|
|
|
|
program->addShader(shader.get());
|
|
|
|
if (!program->link(messages))
|
|
|
|
{
|
|
|
|
DumpBadShader("Failed to link program");
|
2019-05-30 07:52:05 +00:00
|
|
|
return std::nullopt;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glslang::TIntermediate* intermediate = program->getIntermediate(stage);
|
|
|
|
if (!intermediate)
|
|
|
|
{
|
|
|
|
DumpBadShader("Failed to generate SPIR-V");
|
2019-05-30 07:52:05 +00:00
|
|
|
return std::nullopt;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
SPIRVCodeVector out_code;
|
2016-08-13 12:57:50 +00:00
|
|
|
spv::SpvBuildLogger logger;
|
2021-08-05 01:45:38 +00:00
|
|
|
glslang::SpvOptions options;
|
|
|
|
|
|
|
|
if (g_ActiveConfig.bEnableValidationLayer)
|
|
|
|
{
|
|
|
|
// Attach the source code to the SPIR-V for tools like RenderDoc.
|
|
|
|
intermediate->addSourceText(pass_source_code, pass_source_code_length);
|
|
|
|
|
|
|
|
options.generateDebugInfo = true;
|
|
|
|
options.disableOptimizer = true;
|
|
|
|
options.optimizeSize = false;
|
|
|
|
options.disassemble = false;
|
|
|
|
options.validate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
glslang::GlslangToSpv(*intermediate, out_code, &logger, &options);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Write out messages
|
|
|
|
// Temporary: skip if it contains "Warning, version 450 is not yet complete; most version-specific
|
|
|
|
// features are present, but some are missing."
|
|
|
|
if (strlen(shader->getInfoLog()) > 108)
|
2020-11-09 08:26:14 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Shader info log: {}", shader->getInfoLog());
|
2016-08-13 12:57:50 +00:00
|
|
|
if (strlen(shader->getInfoDebugLog()) > 0)
|
2020-11-09 08:26:14 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Shader debug info log: {}", shader->getInfoDebugLog());
|
2016-08-13 12:57:50 +00:00
|
|
|
if (strlen(program->getInfoLog()) > 25)
|
2020-11-09 08:26:14 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Program info log: {}", program->getInfoLog());
|
2016-08-13 12:57:50 +00:00
|
|
|
if (strlen(program->getInfoDebugLog()) > 0)
|
2020-11-09 08:26:14 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "Program debug info log: {}", program->getInfoDebugLog());
|
|
|
|
const std::string spv_messages = logger.getAllMessages();
|
2016-08-13 12:57:50 +00:00
|
|
|
if (!spv_messages.empty())
|
2020-11-09 08:26:14 +00:00
|
|
|
WARN_LOG_FMT(VIDEO, "SPIR-V conversion messages: {}", spv_messages);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Dump source code of shaders out to file if enabled.
|
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS)
|
|
|
|
{
|
|
|
|
static int counter = 0;
|
|
|
|
std::string filename = StringFromFormat("%s%s_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(),
|
|
|
|
stage_filename, counter++);
|
|
|
|
|
|
|
|
std::ofstream stream;
|
2017-01-15 21:46:43 +00:00
|
|
|
File::OpenFStream(stream, filename, std::ios_base::out);
|
2016-08-13 12:57:50 +00:00
|
|
|
if (stream.good())
|
|
|
|
{
|
|
|
|
stream << full_source_code << std::endl;
|
|
|
|
stream << "Shader Info Log:" << std::endl;
|
|
|
|
stream << shader->getInfoLog() << std::endl;
|
|
|
|
stream << shader->getInfoDebugLog() << std::endl;
|
|
|
|
stream << "Program Info Log:" << std::endl;
|
|
|
|
stream << program->getInfoLog() << std::endl;
|
|
|
|
stream << program->getInfoDebugLog() << std::endl;
|
|
|
|
stream << "SPIR-V conversion messages: " << std::endl;
|
|
|
|
stream << spv_messages;
|
|
|
|
stream << "SPIR-V:" << std::endl;
|
2019-05-30 07:52:05 +00:00
|
|
|
spv::Disassemble(stream, out_code);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
return out_code;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InitializeGlslang()
|
|
|
|
{
|
|
|
|
static bool glslang_initialized = false;
|
|
|
|
if (glslang_initialized)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!glslang::InitializeProcess())
|
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmt("Failed to initialize glslang shader compiler");
|
2016-08-13 12:57:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atexit([]() { glslang::FinalizeProcess(); });
|
|
|
|
|
|
|
|
glslang_initialized = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TBuiltInResource* GetCompilerResourceLimits()
|
|
|
|
{
|
2020-03-02 15:56:41 +00:00
|
|
|
return &glslang::DefaultTBuiltInResource;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
std::optional<SPIRVCodeVector> CompileVertexShader(std::string_view source_code)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-05-30 07:52:05 +00:00
|
|
|
return CompileShaderToSPV(EShLangVertex, "vs", source_code, SHADER_HEADER);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
std::optional<SPIRVCodeVector> CompileGeometryShader(std::string_view source_code)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-05-30 07:52:05 +00:00
|
|
|
return CompileShaderToSPV(EShLangGeometry, "gs", source_code, SHADER_HEADER);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
std::optional<SPIRVCodeVector> CompileFragmentShader(std::string_view source_code)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-05-30 07:52:05 +00:00
|
|
|
return CompileShaderToSPV(EShLangFragment, "ps", source_code, SHADER_HEADER);
|
2016-12-09 12:23:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-30 07:52:05 +00:00
|
|
|
std::optional<SPIRVCodeVector> CompileComputeShader(std::string_view source_code)
|
2016-12-09 12:23:04 +00:00
|
|
|
{
|
2019-05-30 07:52:05 +00:00
|
|
|
return CompileShaderToSPV(EShLangCompute, "cs", source_code, COMPUTE_SHADER_HEADER);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2019-06-17 20:57:30 +00:00
|
|
|
} // namespace Vulkan::ShaderCompiler
|