2012-11-15 00:39:56 +01:00
|
|
|
#include "stdafx.h"
|
2014-06-17 17:44:03 +02:00
|
|
|
#include "Utilities/Log.h"
|
2014-06-02 19:27:24 +02:00
|
|
|
#include "Emu/Memory/Memory.h"
|
2013-11-09 22:29:49 +01:00
|
|
|
#include "GLProgram.h"
|
2012-11-15 00:39:56 +01:00
|
|
|
#include "GLGSRender.h"
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
GLProgram::GLProgram() : id(0)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 02:33:55 +02:00
|
|
|
int GLProgram::GetLocation(const std::string& name)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2014-04-10 00:54:32 +02:00
|
|
|
for(u32 i=0; i<m_locations.size(); ++i)
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
2014-04-01 02:33:55 +02:00
|
|
|
if(!m_locations[i].name.compare(name))
|
2013-06-30 10:46:29 +02:00
|
|
|
{
|
|
|
|
|
return m_locations[i].loc;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-10 00:54:32 +02:00
|
|
|
m_locations.emplace_back();
|
|
|
|
|
u32 pos = m_locations.size()-1;
|
2013-06-30 10:46:29 +02:00
|
|
|
m_locations[pos].name = name;
|
|
|
|
|
|
2014-04-01 02:33:55 +02:00
|
|
|
m_locations[pos].loc = glGetUniformLocation(id, name.c_str());
|
|
|
|
|
checkForGlError(fmt::Format("glGetUniformLocation(0x%x, %s)", id, name.c_str()));
|
2013-08-10 23:56:24 +02:00
|
|
|
return m_locations[pos].loc;
|
2013-06-30 10:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
bool GLProgram::IsCreated() const
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
return id > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
void GLProgram::Create(const u32 vp, const u32 fp)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
if(IsCreated()) Delete();
|
|
|
|
|
id = glCreateProgram();
|
|
|
|
|
|
|
|
|
|
glAttachShader(id, vp);
|
|
|
|
|
glAttachShader(id, fp);
|
|
|
|
|
|
|
|
|
|
glLinkProgram(id);
|
|
|
|
|
|
|
|
|
|
GLint linkStatus = GL_FALSE;
|
|
|
|
|
glGetProgramiv(id, GL_LINK_STATUS, &linkStatus);
|
|
|
|
|
if(linkStatus != GL_TRUE)
|
|
|
|
|
{
|
|
|
|
|
GLint bufLength = 0;
|
|
|
|
|
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &bufLength);
|
|
|
|
|
|
|
|
|
|
if (bufLength)
|
|
|
|
|
{
|
|
|
|
|
char* buf = new char[bufLength+1];
|
|
|
|
|
memset(buf, 0, bufLength+1);
|
|
|
|
|
glGetProgramInfoLog(id, bufLength, NULL, buf);
|
2014-06-27 15:26:46 +02:00
|
|
|
LOG_ERROR(RSX, "Could not link program: %s", buf);
|
2012-11-15 00:39:56 +01:00
|
|
|
delete[] buf;
|
2014-02-19 18:27:52 +01:00
|
|
|
|
|
|
|
|
return;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-06-27 15:26:46 +02:00
|
|
|
//else LOG_NOTICE(HLE, "program linked!");
|
2012-11-15 00:39:56 +01:00
|
|
|
|
|
|
|
|
glGetProgramiv(id, GL_VALIDATE_STATUS, &linkStatus);
|
|
|
|
|
if(linkStatus != GL_TRUE)
|
|
|
|
|
{
|
|
|
|
|
GLint bufLength = 0;
|
|
|
|
|
glGetProgramiv(id, GL_INFO_LOG_LENGTH, &bufLength);
|
|
|
|
|
|
|
|
|
|
if (bufLength)
|
|
|
|
|
{
|
|
|
|
|
char* buf = new char[bufLength];
|
|
|
|
|
memset(buf, 0, bufLength);
|
|
|
|
|
glGetProgramInfoLog(id, bufLength, NULL, buf);
|
2014-06-27 15:26:46 +02:00
|
|
|
LOG_ERROR(RSX, "Could not link program: %s", buf);
|
2012-11-15 00:39:56 +01:00
|
|
|
delete[] buf;
|
2014-02-19 18:27:52 +01:00
|
|
|
|
|
|
|
|
return;
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
void GLProgram::UnUse()
|
2013-08-10 23:56:24 +02:00
|
|
|
{
|
|
|
|
|
id = 0;
|
2014-04-10 00:54:32 +02:00
|
|
|
m_locations.clear();
|
2013-08-10 23:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
void GLProgram::Use()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
glUseProgram(id);
|
|
|
|
|
checkForGlError("glUseProgram");
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
void GLProgram::SetTex(u32 index)
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
2014-04-01 02:33:55 +02:00
|
|
|
int loc = GetLocation(fmt::Format("tex%u", index));
|
2013-08-17 00:22:26 +02:00
|
|
|
glProgramUniform1i(id, loc, index);
|
2014-04-01 02:33:55 +02:00
|
|
|
checkForGlError(fmt::Format("SetTex(%u - %d - %d)", id, index, loc));
|
2012-11-15 00:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
2013-11-09 22:29:49 +01:00
|
|
|
void GLProgram::Delete()
|
2012-11-15 00:39:56 +01:00
|
|
|
{
|
|
|
|
|
if(!IsCreated()) return;
|
|
|
|
|
glDeleteProgram(id);
|
|
|
|
|
id = 0;
|
2014-04-10 00:54:32 +02:00
|
|
|
m_locations.clear();
|
2013-11-19 11:30:58 +01:00
|
|
|
}
|