2017-07-20 07:10:02 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-07-20 07:10:02 +00:00
|
|
|
|
|
|
|
#include "VideoCommon/ShaderGenCommon.h"
|
2019-11-22 22:10:41 +00:00
|
|
|
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2017-07-20 07:10:02 +00:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
2019-12-22 18:40:40 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2017-07-20 07:10:02 +00:00
|
|
|
|
|
|
|
ShaderHostConfig ShaderHostConfig::GetCurrent()
|
|
|
|
{
|
|
|
|
ShaderHostConfig bits = {};
|
|
|
|
bits.msaa = g_ActiveConfig.iMultisamples > 1;
|
|
|
|
bits.ssaa = g_ActiveConfig.iMultisamples > 1 && g_ActiveConfig.bSSAA &&
|
|
|
|
g_ActiveConfig.backend_info.bSupportsSSAA;
|
2017-11-11 03:55:00 +00:00
|
|
|
bits.stereo = g_ActiveConfig.stereo_mode != StereoMode::Off;
|
2017-07-20 07:10:02 +00:00
|
|
|
bits.wireframe = g_ActiveConfig.bWireFrame;
|
|
|
|
bits.per_pixel_lighting = g_ActiveConfig.bEnablePixelLighting;
|
|
|
|
bits.vertex_rounding = g_ActiveConfig.UseVertexRounding();
|
|
|
|
bits.fast_depth_calc = g_ActiveConfig.bFastDepthCalc;
|
|
|
|
bits.bounding_box = g_ActiveConfig.bBBoxEnable;
|
|
|
|
bits.backend_dual_source_blend = g_ActiveConfig.backend_info.bSupportsDualSourceBlend;
|
|
|
|
bits.backend_geometry_shaders = g_ActiveConfig.backend_info.bSupportsGeometryShaders;
|
|
|
|
bits.backend_early_z = g_ActiveConfig.backend_info.bSupportsEarlyZ;
|
|
|
|
bits.backend_bbox = g_ActiveConfig.backend_info.bSupportsBBox;
|
|
|
|
bits.backend_gs_instancing = g_ActiveConfig.backend_info.bSupportsGSInstancing;
|
|
|
|
bits.backend_clip_control = g_ActiveConfig.backend_info.bSupportsClipControl;
|
|
|
|
bits.backend_ssaa = g_ActiveConfig.backend_info.bSupportsSSAA;
|
|
|
|
bits.backend_atomics = g_ActiveConfig.backend_info.bSupportsFragmentStoresAndAtomics;
|
|
|
|
bits.backend_depth_clamp = g_ActiveConfig.backend_info.bSupportsDepthClamp;
|
|
|
|
bits.backend_reversed_depth_range = g_ActiveConfig.backend_info.bSupportsReversedDepthRange;
|
2017-07-20 05:25:24 +00:00
|
|
|
bits.backend_bitfield = g_ActiveConfig.backend_info.bSupportsBitfield;
|
|
|
|
bits.backend_dynamic_sampler_indexing =
|
|
|
|
g_ActiveConfig.backend_info.bSupportsDynamicSamplerIndexing;
|
2017-10-26 05:44:39 +00:00
|
|
|
bits.backend_shader_framebuffer_fetch = g_ActiveConfig.backend_info.bSupportsFramebufferFetch;
|
2018-05-25 14:04:18 +00:00
|
|
|
bits.backend_logic_op = g_ActiveConfig.backend_info.bSupportsLogicOp;
|
2019-02-15 01:59:50 +00:00
|
|
|
bits.backend_palette_conversion = g_ActiveConfig.backend_info.bSupportsPaletteConversion;
|
2017-07-20 07:10:02 +00:00
|
|
|
return bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetDiskShaderCacheFileName(APIType api_type, const char* type, bool include_gameid,
|
2018-02-24 15:15:35 +00:00
|
|
|
bool include_host_config, bool include_api)
|
2017-07-20 07:10:02 +00:00
|
|
|
{
|
|
|
|
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
|
|
|
|
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
|
|
|
|
|
|
|
|
std::string filename = File::GetUserPath(D_SHADERCACHE_IDX);
|
2018-02-24 15:15:35 +00:00
|
|
|
if (include_api)
|
2017-07-20 07:10:02 +00:00
|
|
|
{
|
2018-02-24 15:15:35 +00:00
|
|
|
switch (api_type)
|
|
|
|
{
|
|
|
|
case APIType::D3D:
|
|
|
|
filename += "D3D";
|
|
|
|
break;
|
|
|
|
case APIType::OpenGL:
|
|
|
|
filename += "OpenGL";
|
|
|
|
break;
|
|
|
|
case APIType::Vulkan:
|
|
|
|
filename += "Vulkan";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
filename += '-';
|
2017-07-20 07:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filename += type;
|
|
|
|
|
|
|
|
if (include_gameid)
|
|
|
|
{
|
|
|
|
filename += '-';
|
|
|
|
filename += SConfig::GetInstance().GetGameID();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (include_host_config)
|
|
|
|
{
|
2017-10-26 05:44:39 +00:00
|
|
|
// We're using 21 bits, so 6 hex characters.
|
2017-07-20 07:10:02 +00:00
|
|
|
ShaderHostConfig host_config = ShaderHostConfig::GetCurrent();
|
2019-11-22 22:10:41 +00:00
|
|
|
filename += fmt::format("-{:06X}", host_config.bits);
|
2017-07-20 07:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
filename += ".cache";
|
|
|
|
return filename;
|
|
|
|
}
|
2019-12-22 18:40:40 +00:00
|
|
|
|
|
|
|
static void DefineOutputMember(ShaderCode& object, APIType api_type, std::string_view qualifier,
|
|
|
|
std::string_view type, std::string_view name, int var_index,
|
|
|
|
std::string_view semantic = {}, int semantic_index = -1)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("\t{} {} {}", qualifier, type, name);
|
2019-12-22 18:40:40 +00:00
|
|
|
|
|
|
|
if (var_index != -1)
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("{}", var_index);
|
2019-12-22 18:40:40 +00:00
|
|
|
|
|
|
|
if (api_type == APIType::D3D && !semantic.empty())
|
|
|
|
{
|
|
|
|
if (semantic_index != -1)
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write(" : {}{}", semantic, semantic_index);
|
2019-12-22 18:40:40 +00:00
|
|
|
else
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write(" : {}", semantic);
|
2019-12-22 18:40:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write(";\n");
|
2019-12-22 18:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateVSOutputMembers(ShaderCode& object, APIType api_type, u32 texgens,
|
|
|
|
const ShaderHostConfig& host_config, std::string_view qualifier)
|
|
|
|
{
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "pos", -1, "SV_Position");
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 0, "COLOR", 0);
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "colors_", 1, "COLOR", 1);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < texgens; ++i)
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float3", "tex", i, "TEXCOORD", i);
|
|
|
|
|
|
|
|
if (!host_config.fast_depth_calc)
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float4", "clipPos", -1, "TEXCOORD", texgens);
|
|
|
|
|
|
|
|
if (host_config.per_pixel_lighting)
|
|
|
|
{
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float3", "Normal", -1, "TEXCOORD",
|
|
|
|
texgens + 1);
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float3", "WorldPos", -1, "TEXCOORD",
|
|
|
|
texgens + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host_config.backend_geometry_shaders)
|
|
|
|
{
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float", "clipDist", 0, "SV_ClipDistance", 0);
|
|
|
|
DefineOutputMember(object, api_type, qualifier, "float", "clipDist", 1, "SV_ClipDistance", 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssignVSOutputMembers(ShaderCode& object, std::string_view a, std::string_view b, u32 texgens,
|
|
|
|
const ShaderHostConfig& host_config)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("\t{}.pos = {}.pos;\n", a, b);
|
|
|
|
object.Write("\t{}.colors_0 = {}.colors_0;\n", a, b);
|
|
|
|
object.Write("\t{}.colors_1 = {}.colors_1;\n", a, b);
|
2019-12-22 18:40:40 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < texgens; ++i)
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("\t{}.tex{} = {}.tex{};\n", a, i, b, i);
|
2019-12-22 18:40:40 +00:00
|
|
|
|
|
|
|
if (!host_config.fast_depth_calc)
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("\t{}.clipPos = {}.clipPos;\n", a, b);
|
2019-12-22 18:40:40 +00:00
|
|
|
|
|
|
|
if (host_config.per_pixel_lighting)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("\t{}.Normal = {}.Normal;\n", a, b);
|
|
|
|
object.Write("\t{}.WorldPos = {}.WorldPos;\n", a, b);
|
2019-12-22 18:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (host_config.backend_geometry_shaders)
|
|
|
|
{
|
2020-11-09 07:23:32 +00:00
|
|
|
object.Write("\t{}.clipDist0 = {}.clipDist0;\n", a, b);
|
|
|
|
object.Write("\t{}.clipDist1 = {}.clipDist1;\n", a, b);
|
2019-12-22 18:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* GetInterpolationQualifier(bool msaa, bool ssaa, bool in_glsl_interface_block, bool in)
|
|
|
|
{
|
|
|
|
if (!msaa)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// Without GL_ARB_shading_language_420pack support, the interpolation qualifier must be
|
|
|
|
// "centroid in" and not "centroid", even within an interface block.
|
|
|
|
if (in_glsl_interface_block && !g_ActiveConfig.backend_info.bSupportsBindingLayout)
|
|
|
|
{
|
|
|
|
if (!ssaa)
|
|
|
|
return in ? "centroid in" : "centroid out";
|
|
|
|
else
|
|
|
|
return in ? "sample in" : "sample out";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!ssaa)
|
|
|
|
return "centroid";
|
|
|
|
else
|
|
|
|
return "sample";
|
|
|
|
}
|
|
|
|
}
|