rpcsx/rpcs3/Emu/RSX/GL/GLFragmentProgram.cpp

396 lines
11 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Emu/Memory/Memory.h"
#include "Emu/System.h"
2013-11-09 22:29:49 +01:00
#include "GLFragmentProgram.h"
#include "GLCommonDecompiler.h"
#include "../GCM.h"
2015-05-19 19:43:22 +02:00
std::string GLFragmentDecompilerThread::getFloatTypeName(size_t elementCount)
{
return getFloatTypeNameImpl(elementCount);
}
2015-05-19 19:43:22 +02:00
std::string GLFragmentDecompilerThread::getFunction(FUNCTION f)
{
return getFunctionImpl(f);
}
2015-05-19 19:43:22 +02:00
std::string GLFragmentDecompilerThread::saturate(const std::string & code)
{
2015-05-19 19:43:22 +02:00
return "clamp(" + code + ", 0., 1.)";
}
2015-05-19 19:43:22 +02:00
std::string GLFragmentDecompilerThread::compareFunction(COMPARE f, const std::string &Op0, const std::string &Op1)
{
return compareFunctionImpl(f, Op0, Op1);
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertHeader(std::stringstream & OS)
{
2015-11-28 20:41:30 +01:00
OS << "#version 420" << std::endl;
OS << "layout(std140, binding = 0) uniform ScaleOffsetBuffer\n";
OS << "{\n";
OS << " mat4 scaleOffsetMat;\n";
OS << " float fog_param0;\n";
OS << " float fog_param1;\n";
OS << " uint alpha_test;\n";
OS << " float alpha_ref;\n";
OS << "};\n";
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertIntputs(std::stringstream & OS)
{
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
{
for (const ParamItem& PI : PT.items)
{
//Rename fogc to fog_c to differentiate the input register from the variable
if (PI.name == "fogc")
OS << "in vec4 fog_c;" << std::endl;
2016-08-26 18:26:23 +02:00
OS << "in " << PT.type << " " << PI.name << ";" << std::endl;
}
}
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertOutputs(std::stringstream & OS)
{
2015-05-19 19:43:22 +02:00
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" },
};
2015-05-19 19:43:22 +02:00
for (int i = 0; i < sizeof(table) / sizeof(*table); ++i)
{
2015-05-19 19:43:22 +02:00
if (m_parr.HasParam(PF_PARAM_NONE, "vec4", table[i].second))
OS << "out vec4 " << table[i].first << ";" << std::endl;
}
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertConstants(std::stringstream & OS)
{
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
{
if (PT.type != "sampler1D" &&
PT.type != "sampler2D" &&
PT.type != "sampler3D" &&
PT.type != "samplerCube")
2015-05-19 19:43:22 +02:00
continue;
for (const ParamItem& PI : PT.items)
{
std::string samplerType = PT.type;
int index = atoi(&PI.name.data()[3]);
OS << "uniform " << samplerType << " " << PI.name << ";" << std::endl;
OS << "uniform " << "vec4 " << "f" << PI.name << "_cm = vec4(1.0);" << std::endl;
}
}
2015-11-28 20:41:30 +01:00
OS << "layout(std140, binding = 2) uniform FragmentConstantsBuffer" << std::endl;
OS << "{" << std::endl;
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
{
if (PT.type == "sampler1D" ||
PT.type == "sampler2D" ||
PT.type == "sampler3D" ||
PT.type == "samplerCube")
2015-05-19 19:43:22 +02:00
continue;
for (const ParamItem& PI : PT.items)
OS << " " << PT.type << " " << PI.name << ";" << std::endl;
}
2015-11-28 20:41:30 +01:00
// A dummy value otherwise it's invalid to create an empty uniform buffer
OS << " vec4 void_value;" << std::endl;
OS << "};" << std::endl;
}
namespace
{
// Note: It's not clear whether fog is computed per pixel or per vertex.
// But it makes more sense to compute exp of interpoled value than to interpolate exp values.
void insert_fog_declaration(std::stringstream & OS, rsx::fog_mode mode)
{
std::string fog_func = {};
switch (mode)
{
case rsx::fog_mode::linear:
fog_func = "fog_param1 * fog_c.x + (fog_param0 - 1.), fog_param1 * fog_c.x + (fog_param0 - 1.)";
break;
case rsx::fog_mode::exponential:
fog_func = "11.084 * (fog_param1 * fog_c.x + fog_param0 - 1.5), exp(11.084 * (fog_param1 * fog_c.x + fog_param0 - 1.5))";
break;
case rsx::fog_mode::exponential2:
fog_func = "4.709 * (fog_param1 * fog_c.x + fog_param0 - 1.5), exp(-pow(4.709 * (fog_param1 * fog_c.x + fog_param0 - 1.5)), 2.)";
break;
case rsx::fog_mode::linear_abs:
fog_func = "fog_param1 * abs(fog_c.x) + (fog_param0 - 1.), fog_param1 * abs(fog_c.x) + (fog_param0 - 1.)";
break;
case rsx::fog_mode::exponential_abs:
fog_func = "11.084 * (fog_param1 * abs(fog_c.x) + fog_param0 - 1.5), exp(11.084 * (fog_param1 * abs(fog_c.x) + fog_param0 - 1.5))";
break;
case rsx::fog_mode::exponential2_abs:
fog_func = "4.709 * (fog_param1 * abs(fog_c.x) + fog_param0 - 1.5), exp(-pow(4.709 * (fog_param1 * abs(fog_c.x) + fog_param0 - 1.5)), 2.)";
break;
default: fog_func = "0.0, 0.0";
}
OS << " vec4 fogc = clamp(vec4(" << fog_func << ", 0., 0.), 0., 1.);\n";
}
void insert_texture_fetch(std::stringstream & OS, const RSXFragmentProgram& prog, const ParamArray& param)
{
OS << "vec4 texture_fetch(int index, vec4 coord)\n{\n";
OS << " switch (index)\n\t{\n";
for (u8 id = 0; id < 16; id++)
{
if (prog.textures_alpha_kill[id])
{
OS << " case " + std::to_string(id) + ": return ";
switch (prog.get_texture_dimension(id))
{
case rsx::texture_dimension_extended::texture_dimension_1d: OS << "texture(tex" + std::to_string(id) + ", coord.x)"; break;
case rsx::texture_dimension_extended::texture_dimension_2d: OS << "texture(tex" + std::to_string(id) + ", coord.xy)"; break;
case rsx::texture_dimension_extended::texture_dimension_3d:
case rsx::texture_dimension_extended::texture_dimension_cubemap: OS << "texture(tex" + std::to_string(id) + ", coord.xyz)"; break;
default: OS << "vec4(0.0)";
}
OS << ";\n";
}
}
OS << " default: return vec4(0.0);\n";
OS << " }\n";
OS << "}\n";
}
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertMainStart(std::stringstream & OS)
{
insert_glsl_legacy_function(OS);
insert_texture_fetch(OS, m_prog, m_parr);
2015-05-19 19:43:22 +02:00
OS << "void main ()" << std::endl;
OS << "{" << std::endl;
for (const ParamType& PT : m_parr.params[PF_PARAM_NONE])
{
for (const ParamItem& PI : PT.items)
{
2015-05-19 19:43:22 +02:00
OS << " " << PT.type << " " << PI.name;
if (!PI.value.empty())
OS << " = " << PI.value;
OS << ";" << std::endl;
}
}
OS << " vec4 ssa = gl_FrontFacing ? vec4(1.) : vec4(-1.);\n";
2016-03-29 17:04:21 +02:00
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
{
if (PT.type != "sampler2D")
continue;
for (const ParamItem& PI : PT.items)
{
std::string samplerType = PT.type;
int index = atoi(&PI.name.data()[3]);
if (m_prog.unnormalized_coords & (1 << index))
{
OS << " vec2 tex" << index << "_coord_scale = 1. / textureSize(" << PI.name << ", 0);\n";
2016-03-29 17:04:21 +02:00
}
else
{
OS << " vec2 tex" << index << "_coord_scale = vec2(1.);\n";
2016-03-29 17:04:21 +02:00
}
}
}
// search if there is fogc in inputs
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
{
for (const ParamItem& PI : PT.items)
{
if (PI.name == "fogc")
{
insert_fog_declaration(OS, m_prog.fog_equation);
2016-08-26 18:26:23 +02:00
continue;
}
}
}
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertMainEnd(std::stringstream & OS)
{
2014-07-23 20:36:57 +02:00
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" },
};
std::string first_output_name;
for (int i = 0; i < sizeof(table) / sizeof(*table); ++i)
{
2015-05-19 19:43:22 +02:00
if (m_parr.HasParam(PF_PARAM_NONE, "vec4", table[i].second))
{
2015-05-19 19:43:22 +02:00
OS << " " << table[i].first << " = " << table[i].second << ";" << std::endl;
if (first_output_name.empty()) first_output_name = table[i].first;
}
}
if (m_ctrl & CELL_GCM_SHADER_CONTROL_DEPTH_EXPORT)
{
{
/** Note: Naruto Shippuden : Ultimate Ninja Storm 2 sets CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS in a shader
* but it writes depth in r1.z and not h2.z.
* Maybe there's a different flag for depth ?
*/
//OS << ((m_ctrl & CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS) ? "\tgl_FragDepth = r1.z;\n" : "\tgl_FragDepth = h0.z;\n") << std::endl;
OS << " gl_FragDepth = r1.z;\n";
}
}
if (!first_output_name.empty())
{
auto make_comparison_test = [](rsx::comparison_function compare_func, const std::string &test, const std::string &a, const std::string &b) -> std::string
{
if (compare_func == rsx::comparison_function::always) return{};
if (compare_func == rsx::comparison_function::never) return " discard;\n";
std::string compare;
switch (compare_func)
{
case rsx::comparison_function::equal: compare = " == "; break;
case rsx::comparison_function::not_equal: compare = " != "; break;
case rsx::comparison_function::less_or_equal: compare = " <= "; break;
case rsx::comparison_function::less: compare = " < "; break;
case rsx::comparison_function::greater: compare = " > "; break;
case rsx::comparison_function::greater_or_equal: compare = " >= "; break;
}
return " if (" + test + "!(" + a + compare + b + ")) discard;\n";
};
for (u8 index = 0; index < 16; ++index)
{
if (m_prog.textures_alpha_kill[index])
{
std::string index_string = std::to_string(index);
std::string fetch_texture = "texture_fetch(" + index_string + ", tc" + index_string + " * ftex" + index_string + "_cm).a";
OS << make_comparison_test((rsx::comparison_function)m_prog.textures_zfunc[index], "", "0", fetch_texture);
}
}
OS << make_comparison_test(m_prog.alpha_func, "alpha_test != 0 && ", first_output_name + ".a", "alpha_ref");
}
OS << "}" << std::endl;
}
2013-11-09 22:29:49 +01:00
void GLFragmentDecompilerThread::Task()
{
2015-05-19 19:43:22 +02:00
m_shader = Decompile();
}
GLFragmentProgram::GLFragmentProgram()
{
}
GLFragmentProgram::~GLFragmentProgram()
{
Delete();
}
void GLFragmentProgram::Decompile(const RSXFragmentProgram& prog)
{
u32 size;
GLFragmentDecompilerThread decompiler(shader, parr, prog, size);
decompiler.Task();
2015-05-19 19:43:22 +02:00
for (const ParamType& PT : decompiler.m_parr.params[PF_PARAM_UNIFORM])
{
for (const ParamItem& PI : PT.items)
2015-05-19 19:43:22 +02:00
{
2015-06-05 00:44:27 +02:00
if (PT.type == "sampler2D")
continue;
2015-05-19 19:43:22 +02:00
size_t offset = atoi(PI.name.c_str() + 2);
FragmentConstantOffsetCache.push_back(offset);
}
}
}
void GLFragmentProgram::Compile()
{
2015-05-16 01:10:27 +02:00
if (id)
2014-12-28 23:37:32 +01:00
{
glDeleteShader(id);
2014-12-28 23:37:32 +01:00
}
id = glCreateShader(GL_FRAGMENT_SHADER);
const char* str = shader.c_str();
const int strlen = ::narrow<int>(shader.length());
fs::create_path(fs::get_config_dir() + "/shaderlog");
fs::file(fs::get_config_dir() + "shaderlog/FragmentProgram" + std::to_string(id) + ".glsl", fs::rewrite).write(str);
glShaderSource(id, 1, &str, &strlen);
glCompileShader(id);
GLint compileStatus = GL_FALSE;
glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus); // Determine the result of the glCompileShader call
if (compileStatus != GL_TRUE) // If the shader failed to compile...
{
GLint infoLength;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &infoLength); // Retrieve the length in bytes (including trailing NULL) of the shader info log
if (infoLength > 0)
{
GLsizei len;
char* buf = new char[infoLength]; // Buffer to store infoLog
glGetShaderInfoLog(id, infoLength, &len, buf); // Retrieve the shader info log into our buffer
LOG_ERROR(RSX, "Failed to compile shader: %s", buf); // Write log to the console
delete[] buf;
}
2016-08-14 23:04:42 +02:00
LOG_NOTICE(RSX, "%s", shader); // Log the text of the shader that failed to compile
Emu.Pause(); // Pause the emulator, we can't really continue from here
}
}
void GLFragmentProgram::Delete()
{
shader.clear();
if (id)
{
2014-06-19 22:34:09 +02:00
if (Emu.IsStopped())
{
LOG_WARNING(RSX, "GLFragmentProgram::Delete(): glDeleteShader(%d) avoided", id);
2014-06-19 22:34:09 +02:00
}
else
{
glDeleteShader(id);
2014-06-19 22:34:09 +02:00
}
id = 0;
}
2013-11-19 11:30:58 +01:00
}