2020-12-05 13:08:24 +01:00
|
|
|
#include "stdafx.h"
|
2016-02-21 16:50:49 +01:00
|
|
|
#include "VKFragmentProgram.h"
|
|
|
|
|
#include "VKCommonDecompiler.h"
|
|
|
|
|
#include "VKHelpers.h"
|
2021-01-09 19:46:50 +01:00
|
|
|
#include "vkutils/device.h"
|
|
|
|
|
#include "Emu/system_config.h"
|
2021-05-12 23:56:01 +02:00
|
|
|
#include "../Program/GLSLCommon.h"
|
2016-02-21 16:50:49 +01:00
|
|
|
#include "../GCM.h"
|
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
std::string VKFragmentDecompilerThread::getFloatTypeName(usz elementCount)
|
2016-02-21 16:50:49 +01:00
|
|
|
{
|
2017-08-04 16:11:39 +02:00
|
|
|
return glsl::getFloatTypeNameImpl(elementCount);
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
std::string VKFragmentDecompilerThread::getHalfTypeName(usz elementCount)
|
2019-04-12 23:25:44 +02:00
|
|
|
{
|
|
|
|
|
return glsl::getHalfTypeNameImpl(elementCount);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-21 16:50:49 +01:00
|
|
|
std::string VKFragmentDecompilerThread::getFunction(FUNCTION f)
|
|
|
|
|
{
|
2017-11-05 23:48:34 +01:00
|
|
|
return glsl::getFunctionImpl(f);
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string VKFragmentDecompilerThread::compareFunction(COMPARE f, const std::string &Op0, const std::string &Op1)
|
|
|
|
|
{
|
2017-08-04 16:11:39 +02:00
|
|
|
return glsl::compareFunctionImpl(f, Op0, Op1);
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentDecompilerThread::insertHeader(std::stringstream & OS)
|
|
|
|
|
{
|
2022-03-13 09:32:04 +01:00
|
|
|
int version = 420;
|
|
|
|
|
std::vector<const char*> required_extensions;
|
|
|
|
|
|
2019-04-12 23:25:44 +02:00
|
|
|
if (device_props.has_native_half_support)
|
|
|
|
|
{
|
2022-03-13 09:32:04 +01:00
|
|
|
version = std::max(version, 450);
|
|
|
|
|
required_extensions.emplace_back("GL_EXT_shader_explicit_arithmetic_types_float16");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (properties.multisampled_sampler_mask)
|
|
|
|
|
{
|
|
|
|
|
version = std::max(version, 450);
|
|
|
|
|
required_extensions.emplace_back("GL_ARB_shader_texture_image_samples");
|
2019-04-12 23:25:44 +02:00
|
|
|
}
|
2022-03-13 09:32:04 +01:00
|
|
|
|
|
|
|
|
OS << "#version " << version << "\n";
|
|
|
|
|
for (const auto ext : required_extensions)
|
2019-04-12 23:25:44 +02:00
|
|
|
{
|
2022-03-13 09:32:04 +01:00
|
|
|
OS << "#extension " << ext << ": require\n";
|
2019-04-12 23:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << "#extension GL_ARB_separate_shader_objects: enable\n\n";
|
2019-10-13 23:30:34 +02:00
|
|
|
|
|
|
|
|
glsl::insert_subheader_block(OS);
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
2018-04-29 08:41:51 +02:00
|
|
|
void VKFragmentDecompilerThread::insertInputs(std::stringstream & OS)
|
2016-02-21 16:50:49 +01:00
|
|
|
{
|
|
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
|
|
|
|
|
{
|
|
|
|
|
for (const ParamItem& PI : PT.items)
|
|
|
|
|
{
|
2016-04-02 17:19:06 +02:00
|
|
|
//ssa is defined in the program body and is not a varying type
|
|
|
|
|
if (PI.name == "ssa") continue;
|
|
|
|
|
|
2019-06-09 09:03:27 +02:00
|
|
|
const auto reg_location = vk::get_varying_register_location(PI.name);
|
2016-02-21 16:50:49 +01:00
|
|
|
std::string var_name = PI.name;
|
2016-07-17 18:57:50 +02:00
|
|
|
|
2016-02-21 16:50:49 +01:00
|
|
|
if (var_name == "fogc")
|
2019-08-23 18:36:01 +02:00
|
|
|
{
|
2016-02-21 16:50:49 +01:00
|
|
|
var_name = "fog_c";
|
2019-08-23 18:36:01 +02:00
|
|
|
}
|
|
|
|
|
else if (m_prog.two_sided_lighting)
|
|
|
|
|
{
|
|
|
|
|
if (var_name == "diff_color")
|
|
|
|
|
{
|
|
|
|
|
var_name = "diff_color0";
|
|
|
|
|
}
|
|
|
|
|
else if (var_name == "spec_color")
|
|
|
|
|
{
|
|
|
|
|
var_name = "spec_color0";
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-21 16:50:49 +01:00
|
|
|
|
2019-06-09 09:03:27 +02:00
|
|
|
OS << "layout(location=" << reg_location << ") in " << PT.type << " " << var_name << ";\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-07-17 18:57:50 +02:00
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (m_prog.two_sided_lighting)
|
2016-07-17 18:57:50 +02:00
|
|
|
{
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_diff_color)
|
2016-07-17 18:57:50 +02:00
|
|
|
{
|
2019-08-23 18:36:01 +02:00
|
|
|
OS << "layout(location=" << vk::get_varying_register_location("diff_color1") << ") in vec4 diff_color1;\n";
|
2016-07-17 18:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_spec_color)
|
2016-07-17 18:57:50 +02:00
|
|
|
{
|
2019-08-23 18:36:01 +02:00
|
|
|
OS << "layout(location=" << vk::get_varying_register_location("spec_color1") << ") in vec4 spec_color1;\n";
|
2016-07-17 18:57:50 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentDecompilerThread::insertOutputs(std::stringstream & OS)
|
|
|
|
|
{
|
|
|
|
|
const std::pair<std::string, std::string> table[] =
|
|
|
|
|
{
|
|
|
|
|
{ "ocol0", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r0" : "h0" },
|
|
|
|
|
{ "ocol1", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r2" : "h4" },
|
|
|
|
|
{ "ocol2", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r3" : "h6" },
|
|
|
|
|
{ "ocol3", m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS ? "r4" : "h8" },
|
|
|
|
|
};
|
|
|
|
|
|
2017-06-27 16:49:07 +02:00
|
|
|
//NOTE: We do not skip outputs, the only possible combinations are a(0), b(0), ab(0,1), abc(0,1,2), abcd(0,1,2,3)
|
|
|
|
|
u8 output_index = 0;
|
2019-04-13 12:20:50 +02:00
|
|
|
const bool float_type = (m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS) || !device_props.has_native_half_support;
|
|
|
|
|
const auto reg_type = float_type ? "vec4" : getHalfTypeName(4);
|
2020-02-19 18:03:59 +01:00
|
|
|
for (uint i = 0; i < std::size(table); ++i)
|
2016-02-21 16:50:49 +01:00
|
|
|
{
|
2019-04-12 23:25:44 +02:00
|
|
|
if (m_parr.HasParam(PF_PARAM_NONE, reg_type, table[i].second))
|
2018-02-23 15:35:44 +01:00
|
|
|
{
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << "layout(location=" << std::to_string(output_index++) << ") " << "out vec4 " << table[i].first << ";\n";
|
2021-05-22 20:46:10 +02:00
|
|
|
vk_prog->output_color_masks[i] = -1;
|
2018-02-23 15:35:44 +01:00
|
|
|
}
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentDecompilerThread::insertConstants(std::stringstream & OS)
|
|
|
|
|
{
|
2020-01-08 17:30:35 +01:00
|
|
|
u32 location = m_binding_table.textures_first_bind_slot;
|
2016-02-21 16:50:49 +01:00
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
|
|
|
|
|
{
|
|
|
|
|
if (PT.type != "sampler1D" &&
|
|
|
|
|
PT.type != "sampler2D" &&
|
|
|
|
|
PT.type != "sampler3D" &&
|
|
|
|
|
PT.type != "samplerCube")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (const ParamItem& PI : PT.items)
|
|
|
|
|
{
|
|
|
|
|
std::string samplerType = PT.type;
|
|
|
|
|
|
2022-03-13 09:32:04 +01:00
|
|
|
ensure(PI.name.length() > 3);
|
|
|
|
|
int index = atoi(&PI.name[3]);
|
2017-06-14 00:33:53 +02:00
|
|
|
const auto mask = (1 << index);
|
|
|
|
|
|
2022-03-13 09:32:04 +01:00
|
|
|
if (properties.multisampled_sampler_mask & mask)
|
|
|
|
|
{
|
|
|
|
|
if (samplerType != "sampler1D" && samplerType != "sampler2D")
|
|
|
|
|
{
|
|
|
|
|
rsx_log.error("Unexpected multisampled image type '%s'", samplerType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
samplerType = "sampler2DMS";
|
|
|
|
|
}
|
|
|
|
|
else if (properties.shadow_sampler_mask & mask)
|
2017-06-14 00:33:53 +02:00
|
|
|
{
|
2021-01-20 21:23:00 +01:00
|
|
|
if (properties.common_access_sampler_mask & mask)
|
|
|
|
|
{
|
2020-12-24 19:44:15 +01:00
|
|
|
rsx_log.error("Texture unit %d is sampled as both a shadow texture and a depth texture", index);
|
2021-01-20 21:23:00 +01:00
|
|
|
}
|
2020-12-24 19:44:15 +01:00
|
|
|
else
|
2021-01-20 21:23:00 +01:00
|
|
|
{
|
|
|
|
|
samplerType += "Shadow";
|
|
|
|
|
}
|
2017-06-14 00:33:53 +02:00
|
|
|
}
|
2016-02-21 16:50:49 +01:00
|
|
|
|
|
|
|
|
vk::glsl::program_input in;
|
|
|
|
|
in.location = location;
|
2017-08-04 16:11:39 +02:00
|
|
|
in.domain = glsl::glsl_fragment_program;
|
2016-02-21 16:50:49 +01:00
|
|
|
in.name = PI.name;
|
|
|
|
|
in.type = vk::glsl::input_type_texture;
|
|
|
|
|
|
|
|
|
|
inputs.push_back(in);
|
|
|
|
|
|
2017-07-22 00:54:28 +02:00
|
|
|
OS << "layout(set=0, binding=" << location++ << ") uniform " << samplerType << " " << PI.name << ";\n";
|
2018-11-24 13:54:46 +01:00
|
|
|
|
2020-12-24 19:44:15 +01:00
|
|
|
if (properties.redirected_sampler_mask & mask)
|
2018-11-24 13:54:46 +01:00
|
|
|
{
|
|
|
|
|
// Insert stencil mirror declaration
|
|
|
|
|
in.name += "_stencil";
|
|
|
|
|
in.location = location;
|
|
|
|
|
|
|
|
|
|
inputs.push_back(in);
|
|
|
|
|
|
2022-03-13 09:32:04 +01:00
|
|
|
OS << "layout(set=0, binding=" << location++ << ") uniform u" << samplerType << " " << in.name << ";\n";
|
2018-11-24 13:54:46 +01:00
|
|
|
}
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 08:47:45 +01:00
|
|
|
ensure(location <= m_binding_table.vertex_textures_first_bind_slot); // "Too many sampler descriptors!"
|
2018-11-24 13:54:46 +01:00
|
|
|
|
2018-10-20 16:43:00 +02:00
|
|
|
std::string constants_block;
|
2016-02-21 16:50:49 +01:00
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
|
|
|
|
|
{
|
|
|
|
|
if (PT.type == "sampler1D" ||
|
|
|
|
|
PT.type == "sampler2D" ||
|
|
|
|
|
PT.type == "sampler3D" ||
|
|
|
|
|
PT.type == "samplerCube")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (const ParamItem& PI : PT.items)
|
2018-10-20 16:43:00 +02:00
|
|
|
{
|
|
|
|
|
constants_block += " " + PT.type + " " + PI.name + ";\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!constants_block.empty())
|
|
|
|
|
{
|
2019-06-18 15:38:50 +02:00
|
|
|
OS << "layout(std140, set = 0, binding = 2) uniform FragmentConstantsBuffer\n";
|
2018-10-20 16:43:00 +02:00
|
|
|
OS << "{\n";
|
|
|
|
|
OS << constants_block;
|
|
|
|
|
OS << "};\n\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
2019-06-18 15:38:50 +02:00
|
|
|
OS << "layout(std140, set = 0, binding = 3) uniform FragmentStateBuffer\n";
|
2018-10-20 16:43:00 +02:00
|
|
|
OS << "{\n";
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << " float fog_param0;\n";
|
|
|
|
|
OS << " float fog_param1;\n";
|
2018-03-23 12:47:03 +01:00
|
|
|
OS << " uint rop_control;\n";
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << " float alpha_ref;\n";
|
2018-03-23 12:47:03 +01:00
|
|
|
OS << " uint reserved;\n";
|
2017-08-21 19:56:31 +02:00
|
|
|
OS << " uint fog_mode;\n";
|
2017-09-26 15:24:43 +02:00
|
|
|
OS << " float wpos_scale;\n";
|
|
|
|
|
OS << " float wpos_bias;\n";
|
2018-10-20 16:43:00 +02:00
|
|
|
OS << "};\n\n";
|
|
|
|
|
|
2019-06-18 15:38:50 +02:00
|
|
|
OS << "layout(std140, set = 0, binding = 4) uniform TextureParametersBuffer\n";
|
2018-10-20 16:43:00 +02:00
|
|
|
OS << "{\n";
|
2019-10-13 23:30:34 +02:00
|
|
|
OS << " sampler_info texture_parameters[16];\n";
|
2018-10-20 16:43:00 +02:00
|
|
|
OS << "};\n\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
|
2020-05-28 23:51:36 +02:00
|
|
|
OS << "layout(std140, set = 0, binding = " << std::to_string(m_binding_table.rasterizer_env_bind_slot) << ") uniform RasterizerHeap\n";
|
|
|
|
|
OS << "{\n";
|
|
|
|
|
OS << " uvec4 stipple_pattern[8];\n";
|
|
|
|
|
OS << "};\n\n";
|
|
|
|
|
|
2016-02-21 16:50:49 +01:00
|
|
|
vk::glsl::program_input in;
|
2020-01-08 17:30:35 +01:00
|
|
|
in.location = m_binding_table.fragment_constant_buffers_bind_slot;
|
2017-08-04 16:11:39 +02:00
|
|
|
in.domain = glsl::glsl_fragment_program;
|
2016-02-21 16:50:49 +01:00
|
|
|
in.name = "FragmentConstantsBuffer";
|
|
|
|
|
in.type = vk::glsl::input_type_uniform_buffer;
|
2018-10-20 16:43:00 +02:00
|
|
|
inputs.push_back(in);
|
|
|
|
|
|
2020-01-08 17:30:35 +01:00
|
|
|
in.location = m_binding_table.fragment_state_bind_slot;
|
2018-10-20 16:43:00 +02:00
|
|
|
in.name = "FragmentStateBuffer";
|
|
|
|
|
inputs.push_back(in);
|
2016-02-21 16:50:49 +01:00
|
|
|
|
2020-01-08 17:30:35 +01:00
|
|
|
in.location = m_binding_table.fragment_texture_params_bind_slot;
|
2018-10-20 16:43:00 +02:00
|
|
|
in.name = "TextureParametersBuffer";
|
2016-02-21 16:50:49 +01:00
|
|
|
inputs.push_back(in);
|
2020-05-28 23:51:36 +02:00
|
|
|
|
|
|
|
|
in.location = m_binding_table.rasterizer_env_bind_slot;
|
|
|
|
|
in.name = "RasterizerHeap";
|
|
|
|
|
inputs.push_back(in);
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-02 13:13:13 +01:00
|
|
|
void VKFragmentDecompilerThread::insertGlobalFunctions(std::stringstream &OS)
|
2016-02-21 16:50:49 +01:00
|
|
|
{
|
2019-10-14 00:24:04 +02:00
|
|
|
m_shader_props.domain = glsl::glsl_fragment_program;
|
|
|
|
|
m_shader_props.require_lit_emulation = properties.has_lit_op;
|
|
|
|
|
m_shader_props.fp32_outputs = !!(m_prog.ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS);
|
2020-12-24 19:44:15 +01:00
|
|
|
m_shader_props.require_depth_conversion = properties.redirected_sampler_mask != 0;
|
2019-10-14 00:24:04 +02:00
|
|
|
m_shader_props.require_wpos = !!(properties.in_register_mask & in_wpos);
|
|
|
|
|
m_shader_props.require_texture_ops = properties.has_tex_op;
|
2020-12-24 19:44:15 +01:00
|
|
|
m_shader_props.require_shadow_ops = properties.shadow_sampler_mask != 0;
|
2022-03-13 09:32:04 +01:00
|
|
|
m_shader_props.require_msaa_ops = m_prog.texture_state.multisampled_textures != 0;
|
2020-06-11 21:46:53 +02:00
|
|
|
m_shader_props.require_texture_expand = properties.has_exp_tex_op;
|
2021-06-05 01:40:39 +02:00
|
|
|
m_shader_props.require_srgb_to_linear = properties.has_upg;
|
|
|
|
|
m_shader_props.require_linear_to_srgb = properties.has_pkg;
|
2019-10-14 00:24:04 +02:00
|
|
|
m_shader_props.emulate_coverage_tests = g_cfg.video.antialiasing_level == msaa_level::none;
|
|
|
|
|
m_shader_props.emulate_shadow_compare = device_props.emulate_depth_compare;
|
2021-06-09 00:37:59 +02:00
|
|
|
m_shader_props.low_precision_tests = device_props.has_low_precision_rounding;
|
2019-10-14 00:24:04 +02:00
|
|
|
m_shader_props.disable_early_discard = vk::get_driver_vendor() != vk::driver_vendor::NVIDIA;
|
|
|
|
|
m_shader_props.supports_native_fp16 = device_props.has_native_half_support;
|
2022-01-15 20:28:11 +01:00
|
|
|
m_shader_props.srgb_output_rounding = vk::get_driver_vendor() == vk::driver_vendor::NVIDIA;
|
2019-10-14 00:24:04 +02:00
|
|
|
|
|
|
|
|
glsl::insert_glsl_legacy_function(OS, m_shader_props);
|
2017-12-02 13:13:13 +01:00
|
|
|
}
|
2016-02-21 16:50:49 +01:00
|
|
|
|
2017-12-02 13:13:13 +01:00
|
|
|
void VKFragmentDecompilerThread::insertMainStart(std::stringstream & OS)
|
|
|
|
|
{
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_fogc)
|
|
|
|
|
glsl::insert_fog_declaration(OS);
|
2017-08-21 19:56:31 +02:00
|
|
|
|
2019-08-25 16:23:46 +02:00
|
|
|
std::set<std::string> output_registers;
|
|
|
|
|
if (m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS)
|
2016-09-26 14:21:17 +02:00
|
|
|
{
|
2019-08-25 16:23:46 +02:00
|
|
|
output_registers = { "r0", "r2", "r3", "r4" };
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
output_registers = { "h0", "h4", "h6", "h8" };
|
|
|
|
|
}
|
2016-09-26 14:21:17 +02:00
|
|
|
|
2019-08-25 16:23:46 +02:00
|
|
|
if (m_ctrl & CELL_GCM_SHADER_CONTROL_DEPTH_EXPORT)
|
|
|
|
|
{
|
|
|
|
|
output_registers.insert("r1");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string registers;
|
|
|
|
|
std::string reg_type;
|
2019-04-12 23:25:44 +02:00
|
|
|
const auto half4 = getHalfTypeName(4);
|
2019-08-25 16:23:46 +02:00
|
|
|
for (auto ®_name : output_registers)
|
2016-09-26 14:21:17 +02:00
|
|
|
{
|
2019-04-12 23:25:44 +02:00
|
|
|
const auto type = (reg_name[0] == 'r' || !device_props.has_native_half_support)? "vec4" : half4;
|
2020-02-05 08:00:08 +01:00
|
|
|
if (reg_type == type) [[likely]]
|
2019-08-25 16:23:46 +02:00
|
|
|
{
|
|
|
|
|
registers += ", " + reg_name + " = " + type + "(0.)";
|
|
|
|
|
}
|
|
|
|
|
else
|
2016-09-26 14:21:17 +02:00
|
|
|
{
|
2019-08-25 16:23:46 +02:00
|
|
|
if (!registers.empty())
|
|
|
|
|
registers += ";\n";
|
2016-09-26 14:21:17 +02:00
|
|
|
|
2019-08-25 16:23:46 +02:00
|
|
|
registers += type + " " + reg_name + " = " + type + "(0.)";
|
2016-09-26 14:21:17 +02:00
|
|
|
}
|
2019-08-25 16:23:46 +02:00
|
|
|
|
|
|
|
|
reg_type = type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!registers.empty())
|
|
|
|
|
{
|
|
|
|
|
OS << registers << ";\n";
|
2016-09-26 14:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-25 16:23:46 +02:00
|
|
|
OS << "void fs_main()\n";
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << "{\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
|
|
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_NONE])
|
|
|
|
|
{
|
|
|
|
|
for (const ParamItem& PI : PT.items)
|
|
|
|
|
{
|
2019-08-25 16:23:46 +02:00
|
|
|
if (output_registers.find(PI.name) != output_registers.end())
|
2016-09-26 14:21:17 +02:00
|
|
|
continue;
|
|
|
|
|
|
2016-02-21 16:50:49 +01:00
|
|
|
OS << " " << PT.type << " " << PI.name;
|
|
|
|
|
if (!PI.value.empty())
|
|
|
|
|
OS << " = " << PI.value;
|
2016-09-26 14:21:17 +02:00
|
|
|
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << ";\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 16:23:46 +02:00
|
|
|
if (properties.has_w_access)
|
|
|
|
|
OS << " float in_w = (1. / gl_FragCoord.w);\n";
|
|
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_ssa)
|
2018-01-24 22:09:27 +01:00
|
|
|
OS << " vec4 ssa = gl_FrontFacing ? vec4(1.) : vec4(-1.);\n";
|
|
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_wpos)
|
2018-01-24 22:09:27 +01:00
|
|
|
OS << " vec4 wpos = get_wpos();\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_fogc)
|
|
|
|
|
OS << " vec4 fogc = fetch_fog_value(fog_mode);\n";
|
2016-08-24 02:50:07 +02:00
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (m_prog.two_sided_lighting)
|
2016-02-21 16:50:49 +01:00
|
|
|
{
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_diff_color)
|
|
|
|
|
OS << " vec4 diff_color = gl_FrontFacing ? diff_color1 : diff_color0;\n";
|
2016-07-17 18:57:50 +02:00
|
|
|
|
2019-08-23 18:36:01 +02:00
|
|
|
if (properties.in_register_mask & in_spec_color)
|
|
|
|
|
OS << " vec4 spec_color = gl_FrontFacing ? spec_color1 : spec_color0;\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentDecompilerThread::insertMainEnd(std::stringstream & OS)
|
|
|
|
|
{
|
2018-03-09 10:31:01 +01:00
|
|
|
OS << "}\n\n";
|
|
|
|
|
|
|
|
|
|
OS << "void main()\n";
|
|
|
|
|
OS << "{\n";
|
|
|
|
|
|
2020-05-28 23:51:36 +02:00
|
|
|
::glsl::insert_rop_init(OS);
|
|
|
|
|
|
2019-08-25 16:23:46 +02:00
|
|
|
OS << "\n" << " fs_main();\n\n";
|
2018-03-09 10:31:01 +01:00
|
|
|
|
2019-10-14 00:24:04 +02:00
|
|
|
glsl::insert_rop(OS, m_shader_props);
|
2016-09-26 14:21:17 +02:00
|
|
|
|
|
|
|
|
if (m_ctrl & CELL_GCM_SHADER_CONTROL_DEPTH_EXPORT)
|
|
|
|
|
{
|
2017-03-07 11:40:38 +01:00
|
|
|
if (m_parr.HasParam(PF_PARAM_NONE, "vec4", "r1"))
|
2016-09-26 14:21:17 +02:00
|
|
|
{
|
2020-01-01 19:52:07 +01:00
|
|
|
// NOTE: Depth writes are always from a fp32 register. See issues section on nvidia's NV_fragment_program spec
|
|
|
|
|
// https://www.khronos.org/registry/OpenGL/extensions/NV/NV_fragment_program.txt
|
|
|
|
|
|
|
|
|
|
// NOTE: Depth writes in OpenGL (and by extension RSX) are clamped to 0,1 range.
|
|
|
|
|
// Indeed, hardware tests on realhw prove that even in depth float mode, values outside this range are clamped.
|
|
|
|
|
OS << " gl_FragDepth = _saturate(r1.z);\n";
|
2016-09-26 14:21:17 +02:00
|
|
|
}
|
2017-03-07 11:40:38 +01:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//Input not declared. Leave commented to assist in debugging the shader
|
|
|
|
|
OS << " //gl_FragDepth = r1.z;\n";
|
|
|
|
|
}
|
2016-09-26 14:21:17 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-28 18:36:27 +02:00
|
|
|
OS << "}\n";
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentDecompilerThread::Task()
|
|
|
|
|
{
|
2021-01-09 19:46:50 +01:00
|
|
|
m_binding_table = vk::g_render_device->get_pipeline_binding_table();
|
2016-02-21 16:50:49 +01:00
|
|
|
m_shader = Decompile();
|
|
|
|
|
vk_prog->SetInputs(inputs);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-08 09:33:48 +02:00
|
|
|
VKFragmentProgram::VKFragmentProgram() = default;
|
2016-02-21 16:50:49 +01:00
|
|
|
|
|
|
|
|
VKFragmentProgram::~VKFragmentProgram()
|
|
|
|
|
{
|
|
|
|
|
Delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentProgram::Decompile(const RSXFragmentProgram& prog)
|
|
|
|
|
{
|
|
|
|
|
u32 size;
|
2018-06-12 17:46:59 +02:00
|
|
|
std::string source;
|
|
|
|
|
VKFragmentDecompilerThread decompiler(source, parr, prog, size, *this);
|
2019-04-12 23:25:44 +02:00
|
|
|
|
2019-04-15 19:39:42 +02:00
|
|
|
const auto pdev = vk::get_current_renderer();
|
2019-04-12 23:25:44 +02:00
|
|
|
if (!g_cfg.video.disable_native_float16)
|
|
|
|
|
{
|
2019-04-15 19:39:42 +02:00
|
|
|
decompiler.device_props.has_native_half_support = pdev->get_shader_types_support().allow_float16;
|
2019-04-12 23:25:44 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-15 19:39:42 +02:00
|
|
|
decompiler.device_props.emulate_depth_compare = !pdev->get_formats_support().d24_unorm_s8;
|
2021-06-09 00:37:59 +02:00
|
|
|
decompiler.device_props.has_low_precision_rounding = vk::get_driver_vendor() == vk::driver_vendor::NVIDIA;
|
2016-02-21 16:50:49 +01:00
|
|
|
decompiler.Task();
|
2018-06-12 17:46:59 +02:00
|
|
|
|
|
|
|
|
shader.create(::glsl::program_domain::glsl_fragment_program, source);
|
2018-12-24 16:47:46 +01:00
|
|
|
|
2016-02-21 16:50:49 +01:00
|
|
|
for (const ParamType& PT : decompiler.m_parr.params[PF_PARAM_UNIFORM])
|
|
|
|
|
{
|
|
|
|
|
for (const ParamItem& PI : PT.items)
|
|
|
|
|
{
|
2016-09-26 14:21:17 +02:00
|
|
|
if (PT.type == "sampler1D" ||
|
|
|
|
|
PT.type == "sampler2D" ||
|
|
|
|
|
PT.type == "sampler3D" ||
|
|
|
|
|
PT.type == "samplerCube")
|
2016-02-21 16:50:49 +01:00
|
|
|
continue;
|
2016-09-26 14:21:17 +02:00
|
|
|
|
2020-12-18 08:39:54 +01:00
|
|
|
usz offset = atoi(PI.name.c_str() + 2);
|
2016-02-21 16:50:49 +01:00
|
|
|
FragmentConstantOffsetCache.push_back(offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentProgram::Compile()
|
|
|
|
|
{
|
2019-05-31 16:41:33 +02:00
|
|
|
if (g_cfg.video.log_programs)
|
|
|
|
|
fs::file(fs::get_cache_dir() + "shaderlog/FragmentProgram" + std::to_string(id) + ".spirv", fs::rewrite).write(shader.get_source());
|
2018-06-12 17:46:59 +02:00
|
|
|
handle = shader.compile();
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentProgram::Delete()
|
|
|
|
|
{
|
2018-06-12 17:46:59 +02:00
|
|
|
shader.destroy();
|
2016-02-21 16:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void VKFragmentProgram::SetInputs(std::vector<vk::glsl::program_input>& inputs)
|
|
|
|
|
{
|
|
|
|
|
for (auto &it : inputs)
|
|
|
|
|
{
|
|
|
|
|
uniforms.push_back(it);
|
|
|
|
|
}
|
|
|
|
|
}
|