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>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <string>
|
|
|
|
|
2021-12-10 02:22:16 +00:00
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
2023-05-18 00:28:27 +00:00
|
|
|
#include "VideoCommon/DriverDetails.h"
|
2022-05-25 04:12:47 +00:00
|
|
|
#include "VideoCommon/Spirv.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-06-17 20:57:30 +00:00
|
|
|
namespace Vulkan::ShaderCompiler
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// 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))
|
2022-06-16 22:26:30 +00:00
|
|
|
#define SSBO_BINDING(x) layout(std430, 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))
|
2023-06-10 17:35:36 +00:00
|
|
|
#define TEXEL_BUFFER_BINDING(x) layout(set = 0, binding = (9 + x))
|
|
|
|
#define IMAGE_BINDING(format, x) layout(format, set = 0, binding = (11 + 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
|
2023-03-24 20:42:43 +00:00
|
|
|
#extension GL_KHR_shader_subgroup_shuffle : enable
|
2019-03-22 10:39:37 +00:00
|
|
|
|
|
|
|
#define SUPPORTS_SUBGROUP_REDUCTION 1
|
|
|
|
#define IS_HELPER_INVOCATION gl_HelperInvocation
|
2023-01-31 17:10:48 +00:00
|
|
|
#define IS_FIRST_ACTIVE_INVOCATION (subgroupElect())
|
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
|
|
|
|
2022-05-25 04:12:47 +00:00
|
|
|
static std::string GetShaderCode(std::string_view source, std::string_view header)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
std::string full_source_code;
|
2019-05-30 07:07:40 +00:00
|
|
|
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);
|
2023-05-18 00:28:27 +00:00
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_INVERTED_IS_HELPER))
|
|
|
|
{
|
|
|
|
full_source_code.append("#define gl_HelperInvocation !gl_HelperInvocation "
|
|
|
|
"// Work around broken AMD Metal driver\n");
|
|
|
|
}
|
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_SUBGROUP_OPS_WITH_DISCARD))
|
|
|
|
full_source_code.append("#define BROKEN_SUBGROUP_WITH_DISCARD 1\n");
|
2019-05-30 07:07:40 +00:00
|
|
|
full_source_code.append(source);
|
2021-08-05 01:45:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 04:12:47 +00:00
|
|
|
return full_source_code;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 04:12:47 +00:00
|
|
|
static glslang::EShTargetLanguageVersion GetLanguageVersion()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2022-05-25 04:12:47 +00:00
|
|
|
// Sub-group operations require Vulkan 1.1 and SPIR-V 1.3.
|
|
|
|
if (g_vulkan_context->SupportsShaderSubgroupOperations())
|
|
|
|
return glslang::EShTargetSpv_1_3;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2022-05-25 04:12:47 +00:00
|
|
|
return glslang::EShTargetSpv_1_0;
|
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
|
|
|
{
|
2022-05-25 04:12:47 +00:00
|
|
|
return SPIRV::CompileVertexShader(GetShaderCode(source_code, SHADER_HEADER), APIType::Vulkan,
|
|
|
|
GetLanguageVersion());
|
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
|
|
|
{
|
2022-05-25 04:12:47 +00:00
|
|
|
return SPIRV::CompileGeometryShader(GetShaderCode(source_code, SHADER_HEADER), APIType::Vulkan,
|
|
|
|
GetLanguageVersion());
|
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
|
|
|
{
|
2022-05-25 04:12:47 +00:00
|
|
|
return SPIRV::CompileFragmentShader(GetShaderCode(source_code, SHADER_HEADER), APIType::Vulkan,
|
|
|
|
GetLanguageVersion());
|
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
|
|
|
{
|
2022-07-09 23:01:58 +00:00
|
|
|
return SPIRV::CompileComputeShader(GetShaderCode(source_code, COMPUTE_SHADER_HEADER),
|
|
|
|
APIType::Vulkan, GetLanguageVersion());
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2019-06-17 20:57:30 +00:00
|
|
|
} // namespace Vulkan::ShaderCompiler
|