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

279 lines
7 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;
}
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)
2015-05-19 19:43:22 +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]);
if (m_prog.unnormalized_coords & (1 << index))
samplerType = "sampler2DRect";
OS << "uniform " << samplerType << " " << PI.name << ";" << 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;
}
2015-05-19 19:43:22 +02:00
void GLFragmentDecompilerThread::insertMainStart(std::stringstream & OS)
{
insert_glsl_legacy_function(OS);
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";
}
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" },
};
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;
}
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";
}
}
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()
{
2015-07-01 00:25:52 +02:00
//if (m_decompiler_thread)
//{
// Wait();
// if (m_decompiler_thread->IsAlive())
// {
// m_decompiler_thread->Stop();
// }
2015-07-01 00:25:52 +02:00
// delete m_decompiler_thread;
// m_decompiler_thread = nullptr;
//}
Delete();
}
2015-07-01 00:25:52 +02:00
//void GLFragmentProgram::Wait()
//{
// if (m_decompiler_thread && m_decompiler_thread->IsAlive())
// {
// m_decompiler_thread->Join();
// }
//}
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);
}
}
}
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();
//}
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 = gsl::narrow<int>(shader.length());
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;
}
LOG_NOTICE(RSX, shader.c_str()); // 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
}