2012-11-15 00:39:56 +01:00
|
|
|
#include "stdafx.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#include "Emu/Memory/Memory.h"
|
|
|
|
|
#include "Emu/System.h"
|
2013-11-09 22:29:49 +01:00
|
|
|
#include "GLFragmentProgram.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-05-22 19:18:20 +02:00
|
|
|
#include "GLCommonDecompiler.h"
|
2015-11-30 19:39:14 +01:00
|
|
|
#include "../GCM.h"
|
2015-05-22 19:18:20 +02:00
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
std::string GLFragmentDecompilerThread::getFloatTypeName(size_t elementCount)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-05-22 19:18:20 +02:00
|
|
|
return getFloatTypeNameImpl(elementCount);
|
2014-06-07 16:15:49 +02:00
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
std::string GLFragmentDecompilerThread::getFunction(FUNCTION f)
|
2014-06-07 16:15:49 +02:00
|
|
|
{
|
2015-05-22 19:18:20 +02:00
|
|
|
return getFunctionImpl(f);
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
std::string GLFragmentDecompilerThread::saturate(const std::string & code)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-05-19 19:43:22 +02:00
|
|
|
return "clamp(" + code + ", 0., 1.)";
|
2013-08-26 16:18:59 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
std::string GLFragmentDecompilerThread::compareFunction(COMPARE f, const std::string &Op0, const std::string &Op1)
|
2013-08-26 16:18:59 +02:00
|
|
|
{
|
2015-05-22 19:18:20 +02:00
|
|
|
return compareFunctionImpl(f, Op0, Op1);
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
void GLFragmentDecompilerThread::insertHeader(std::stringstream & OS)
|
2013-08-19 01:06:11 +02:00
|
|
|
{
|
2015-11-28 20:41:30 +01:00
|
|
|
OS << "#version 420" << std::endl;
|
2013-08-19 01:06:11 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
void GLFragmentDecompilerThread::insertIntputs(std::stringstream & OS)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_IN])
|
2014-02-02 20:42:32 +01:00
|
|
|
{
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamItem& PI : PT.items)
|
2015-05-19 19:43:22 +02:00
|
|
|
OS << "in " << PT.type << " " << PI.name << ";" << std::endl;
|
2014-02-02 20:42:32 +01:00
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
void GLFragmentDecompilerThread::insertOutputs(std::stringstream & OS)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-05-19 19:43:22 +02:00
|
|
|
const std::pair<std::string, std::string> table[] =
|
2014-06-07 16:15:49 +02:00
|
|
|
{
|
2015-11-30 19:39:14 +01:00
|
|
|
{ "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" },
|
2014-06-07 16:15:49 +02:00
|
|
|
};
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
for (int i = 0; i < sizeof(table) / sizeof(*table); ++i)
|
2014-06-07 16:15:49 +02:00
|
|
|
{
|
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;
|
2014-06-07 16:15:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
void GLFragmentDecompilerThread::insertConstants(std::stringstream & OS)
|
2014-12-17 15:01:59 +01:00
|
|
|
{
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
|
2014-12-17 15:01:59 +01:00
|
|
|
{
|
2016-02-03 11:05:18 +01:00
|
|
|
if (PT.type != "sampler1D" &&
|
|
|
|
|
PT.type != "sampler2D" &&
|
|
|
|
|
PT.type != "sampler3D" &&
|
|
|
|
|
PT.type != "samplerCube")
|
2015-05-19 19:43:22 +02:00
|
|
|
continue;
|
2016-02-03 11:05:18 +01:00
|
|
|
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamItem& PI : PT.items)
|
2016-02-03 11:05:18 +01:00
|
|
|
{
|
|
|
|
|
std::string samplerType = PT.type;
|
|
|
|
|
int index = atoi(&PI.name.data()[3]);
|
2014-12-17 15:01:59 +01:00
|
|
|
|
2016-02-03 11:05:18 +01:00
|
|
|
if (m_prog.unnormalized_coords & (1 << index))
|
|
|
|
|
samplerType = "sampler2DRect";
|
|
|
|
|
|
|
|
|
|
OS << "uniform " << samplerType << " " << PI.name << ";" << std::endl;
|
|
|
|
|
}
|
2014-12-17 15:01:59 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-28 20:41:30 +01:00
|
|
|
OS << "layout(std140, binding = 2) uniform FragmentConstantsBuffer" << std::endl;
|
|
|
|
|
OS << "{" << std::endl;
|
2016-02-03 11:05:18 +01:00
|
|
|
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_UNIFORM])
|
2014-12-17 15:01:59 +01:00
|
|
|
{
|
2016-02-03 11:05:18 +01:00
|
|
|
if (PT.type == "sampler1D" ||
|
|
|
|
|
PT.type == "sampler2D" ||
|
|
|
|
|
PT.type == "sampler3D" ||
|
|
|
|
|
PT.type == "samplerCube")
|
2015-05-19 19:43:22 +02:00
|
|
|
continue;
|
2016-02-03 11:05:18 +01:00
|
|
|
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamItem& PI : PT.items)
|
2016-02-03 11:05:18 +01:00
|
|
|
OS << " " << PT.type << " " << PI.name << ";" << std::endl;
|
2014-12-17 15:01:59 +01:00
|
|
|
}
|
2016-02-03 11:05:18 +01:00
|
|
|
|
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;
|
2014-12-17 15:01:59 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
void GLFragmentDecompilerThread::insertMainStart(std::stringstream & OS)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-31 20:01:00 +01:00
|
|
|
insert_glsl_legacy_function(OS);
|
2016-01-03 18:40:19 +01:00
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
OS << "void main ()" << std::endl;
|
|
|
|
|
OS << "{" << std::endl;
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamType& PT : m_parr.params[PF_PARAM_NONE])
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-11-29 18:20:43 +01:00
|
|
|
for (const ParamItem& PI : PT.items)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-05-19 19:43:22 +02:00
|
|
|
OS << " " << PT.type << " " << PI.name;
|
|
|
|
|
if (!PI.value.empty())
|
|
|
|
|
OS << " = " << PI.value;
|
|
|
|
|
OS << ";" << std::endl;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-22 21:53:46 +01:00
|
|
|
|
|
|
|
|
OS << " vec4 ssa = gl_FrontFacing ? vec4(1.) : vec4(-1.);\n";
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-19 19:43:22 +02:00
|
|
|
void GLFragmentDecompilerThread::insertMainEnd(std::stringstream & OS)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2014-07-23 20:36:57 +02:00
|
|
|
const std::pair<std::string, std::string> table[] =
|
2014-06-07 16:15:49 +02:00
|
|
|
{
|
2015-11-30 19:39:14 +01:00
|
|
|
{ "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" },
|
2014-06-07 16:15:49 +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 << " " << table[i].first << " = " << table[i].second << ";" << std::endl;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-30 19:39:14 +01:00
|
|
|
if (m_ctrl & CELL_GCM_SHADER_CONTROL_DEPTH_EXPORT)
|
2016-01-15 17:29:28 +01:00
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
/** Note: Naruto Shippuden : Ultimate Ninja Storm 2 sets CELL_GCM_SHADER_CONTROL_32_BITS_EXPORTS in a shader
|
2016-02-03 11:05:18 +01:00
|
|
|
* but it writes depth in r1.z and not h2.z.
|
|
|
|
|
* Maybe there's a different flag for depth ?
|
|
|
|
|
*/
|
2016-01-15 17:29:28 +01:00
|
|
|
//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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 19:39:14 +01:00
|
|
|
|
2015-10-05 17:40:22 +02:00
|
|
|
OS << "}" << std::endl;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
void GLFragmentDecompilerThread::Task()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-05-19 19:43:22 +02:00
|
|
|
m_shader = Decompile();
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
GLFragmentProgram::GLFragmentProgram()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
GLFragmentProgram::~GLFragmentProgram()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-07-01 00:25:52 +02:00
|
|
|
//if (m_decompiler_thread)
|
|
|
|
|
//{
|
|
|
|
|
// Wait();
|
|
|
|
|
// if (m_decompiler_thread->IsAlive())
|
|
|
|
|
// {
|
|
|
|
|
// m_decompiler_thread->Stop();
|
|
|
|
|
// }
|
2014-12-17 15:01:59 +01:00
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
// delete m_decompiler_thread;
|
|
|
|
|
// m_decompiler_thread = nullptr;
|
|
|
|
|
//}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
|
Delete();
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
//void GLFragmentProgram::Wait()
|
|
|
|
|
//{
|
|
|
|
|
// if (m_decompiler_thread && m_decompiler_thread->IsAlive())
|
|
|
|
|
// {
|
|
|
|
|
// m_decompiler_thread->Join();
|
|
|
|
|
// }
|
|
|
|
|
//}
|
2014-04-15 16:12:15 +02:00
|
|
|
|
2016-01-10 20:09:56 +01:00
|
|
|
void GLFragmentProgram::Decompile(const RSXFragmentProgram& prog)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2016-01-10 20:09:56 +01:00
|
|
|
u32 size;
|
|
|
|
|
GLFragmentDecompilerThread decompiler(shader, parr, prog, size);
|
2014-04-15 16:12:15 +02:00
|
|
|
decompiler.Task();
|
2015-05-19 19:43:22 +02:00
|
|
|
for (const ParamType& PT : decompiler.m_parr.params[PF_PARAM_UNIFORM])
|
|
|
|
|
{
|
2015-11-29 18:20:43 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-04-15 16:12:15 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-01 00:25:52 +02:00
|
|
|
//void GLFragmentProgram::DecompileAsync(RSXFragmentProgram& prog)
|
|
|
|
|
//{
|
|
|
|
|
// if (m_decompiler_thread)
|
|
|
|
|
// {
|
|
|
|
|
// Wait();
|
|
|
|
|
// if (m_decompiler_thread->IsAlive())
|
|
|
|
|
// {
|
|
|
|
|
// m_decompiler_thread->Stop();
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// delete m_decompiler_thread;
|
|
|
|
|
// m_decompiler_thread = nullptr;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// m_decompiler_thread = new GLFragmentDecompilerThread(shader, parr, prog.addr, prog.size, prog.ctrl);
|
|
|
|
|
// m_decompiler_thread->Start();
|
|
|
|
|
//}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
void GLFragmentProgram::Compile()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-05-16 01:10:27 +02:00
|
|
|
if (id)
|
2014-12-28 23:37:32 +01:00
|
|
|
{
|
2015-01-01 23:55:02 +01:00
|
|
|
glDeleteShader(id);
|
2014-12-28 23:37:32 +01:00
|
|
|
}
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
id = glCreateShader(GL_FRAGMENT_SHADER);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
const char* str = shader.c_str();
|
2016-01-13 00:25:58 +01:00
|
|
|
const int strlen = gsl::narrow<int>(shader.length());
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
glShaderSource(id, 1, &str, &strlen);
|
|
|
|
|
glCompileShader(id);
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2014-04-15 16:12:15 +02:00
|
|
|
GLint compileStatus = GL_FALSE;
|
2015-01-01 23:55:02 +01:00
|
|
|
glGetShaderiv(id, GL_COMPILE_STATUS, &compileStatus); // Determine the result of the glCompileShader call
|
2014-04-15 16:12:15 +02:00
|
|
|
if (compileStatus != GL_TRUE) // If the shader failed to compile...
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2014-04-15 16:12:15 +02:00
|
|
|
GLint infoLength;
|
2015-01-01 23:55:02 +01:00
|
|
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &infoLength); // Retrieve the length in bytes (including trailing NULL) of the shader info log
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2014-04-15 16:12:15 +02:00
|
|
|
if (infoLength > 0)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
GLsizei len;
|
2014-04-15 16:12:15 +02:00
|
|
|
char* buf = new char[infoLength]; // Buffer to store infoLog
|
|
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
glGetShaderInfoLog(id, infoLength, &len, buf); // Retrieve the shader info log into our buffer
|
2014-06-27 15:26:46 +02:00
|
|
|
LOG_ERROR(RSX, "Failed to compile shader: %s", buf); // Write log to the console
|
2014-04-15 16:12:15 +02:00
|
|
|
|
2013-06-30 10:46:29 +02:00
|
|
|
delete[] buf;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
LOG_NOTICE(RSX, shader.c_str()); // Log the text of the shader that failed to compile
|
2014-04-15 16:12:15 +02:00
|
|
|
Emu.Pause(); // Pause the emulator, we can't really continue from here
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
void GLFragmentProgram::Delete()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2015-01-01 23:55:02 +01:00
|
|
|
shader.clear();
|
2012-11-15 00:39:56 +01:00
|
|
|
|
2015-01-01 23:55:02 +01:00
|
|
|
if (id)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2014-06-19 22:34:09 +02:00
|
|
|
if (Emu.IsStopped())
|
|
|
|
|
{
|
2015-01-01 23:55:02 +01:00
|
|
|
LOG_WARNING(RSX, "GLFragmentProgram::Delete(): glDeleteShader(%d) avoided", id);
|
2014-06-19 22:34:09 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2015-01-01 23:55:02 +01:00
|
|
|
glDeleteShader(id);
|
2014-06-19 22:34:09 +02:00
|
|
|
}
|
2015-01-01 23:55:02 +01:00
|
|
|
id = 0;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
2013-11-19 11:30:58 +01:00
|
|
|
}
|