2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:29:41 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2015-12-20 19:49:49 -07:00
|
|
|
#include <memory>
|
|
|
|
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2015-09-18 10:40:00 -06:00
|
|
|
#include "Common/GL/GLInterfaceBase.h"
|
|
|
|
#include "Common/GL/GLUtil.h"
|
2015-09-18 11:40:46 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2010-07-16 15:56:40 -06:00
|
|
|
|
2015-12-20 19:49:49 -07:00
|
|
|
std::unique_ptr<cInterfaceBase> GLInterface;
|
2014-12-04 00:24:58 -07:00
|
|
|
static GLuint attributelessVAO = 0;
|
|
|
|
static GLuint attributelessVBO = 0;
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2012-12-17 13:54:20 -07:00
|
|
|
void InitInterface()
|
2010-02-15 21:59:45 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
GLInterface = HostGL_CreateGLInterface();
|
2010-02-15 21:59:45 -07:00
|
|
|
}
|
2009-01-04 14:53:41 -07:00
|
|
|
|
2015-10-09 01:25:09 -06:00
|
|
|
GLuint OpenGL_CompileProgram(const std::string& vertexShader, const std::string& fragmentShader)
|
2012-12-12 02:40:03 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
// generate objects
|
|
|
|
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
GLuint programID = glCreateProgram();
|
|
|
|
|
|
|
|
// compile vertex shader
|
|
|
|
const char* shader = vertexShader.c_str();
|
|
|
|
glShaderSource(vertexShaderID, 1, &shader, nullptr);
|
|
|
|
glCompileShader(vertexShaderID);
|
2012-12-12 02:40:03 -07:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
|
2016-06-24 02:43:46 -06:00
|
|
|
GLint Result = GL_FALSE;
|
|
|
|
char stringBuffer[1024];
|
|
|
|
GLsizei stringBufferUsage = 0;
|
|
|
|
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderInfoLog(vertexShaderID, 1024, &stringBufferUsage, stringBuffer);
|
|
|
|
|
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLSL vertex shader warnings:\n%s%s", stringBuffer, vertexShader.c_str());
|
|
|
|
}
|
|
|
|
else if (!Result)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLSL vertex shader error:\n%s%s", stringBuffer, vertexShader.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_LOG(VIDEO, "GLSL vertex shader compiled:\n%s", vertexShader.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool shader_errors = !Result;
|
2012-12-12 02:40:03 -07:00
|
|
|
#endif
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// compile fragment shader
|
|
|
|
shader = fragmentShader.c_str();
|
|
|
|
glShaderSource(fragmentShaderID, 1, &shader, nullptr);
|
|
|
|
glCompileShader(fragmentShaderID);
|
2012-12-12 02:40:03 -07:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
|
2016-06-24 02:43:46 -06:00
|
|
|
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderInfoLog(fragmentShaderID, 1024, &stringBufferUsage, stringBuffer);
|
|
|
|
|
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLSL fragment shader warnings:\n%s%s", stringBuffer, fragmentShader.c_str());
|
|
|
|
}
|
|
|
|
else if (!Result)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLSL fragment shader error:\n%s%s", stringBuffer, fragmentShader.c_str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUG_LOG(VIDEO, "GLSL fragment shader compiled:\n%s", fragmentShader.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
shader_errors |= !Result;
|
2012-12-12 02:40:03 -07:00
|
|
|
#endif
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// link them
|
|
|
|
glAttachShader(programID, vertexShaderID);
|
|
|
|
glAttachShader(programID, fragmentShaderID);
|
|
|
|
glLinkProgram(programID);
|
2012-12-12 02:40:03 -07:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
|
2016-06-24 02:43:46 -06:00
|
|
|
glGetProgramiv(programID, GL_LINK_STATUS, &Result);
|
|
|
|
glGetProgramInfoLog(programID, 1024, &stringBufferUsage, stringBuffer);
|
|
|
|
|
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLSL linker warnings:\n%s%s%s", stringBuffer, vertexShader.c_str(),
|
|
|
|
fragmentShader.c_str());
|
|
|
|
}
|
|
|
|
else if (!Result && !shader_errors)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "GLSL linker error:\n%s%s%s", stringBuffer, vertexShader.c_str(),
|
|
|
|
fragmentShader.c_str());
|
|
|
|
}
|
2012-12-12 02:40:03 -07:00
|
|
|
#endif
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// cleanup
|
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
glDeleteShader(fragmentShaderID);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return programID;
|
2012-12-12 02:40:03 -07:00
|
|
|
}
|
|
|
|
|
2014-12-11 02:00:37 -07:00
|
|
|
void OpenGL_CreateAttributelessVAO()
|
2014-12-04 00:24:58 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
glGenVertexArrays(1, &attributelessVAO);
|
|
|
|
_dbg_assert_msg_(VIDEO, attributelessVAO != 0,
|
|
|
|
"Attributeless VAO should have been created successfully.")
|
|
|
|
|
|
|
|
// In a compatibility context, we require a valid, bound array buffer.
|
|
|
|
glGenBuffers(1, &attributelessVBO);
|
|
|
|
_dbg_assert_msg_(VIDEO, attributelessVBO != 0,
|
|
|
|
"Attributeless VBO should have been created successfully.")
|
|
|
|
|
|
|
|
// Initialize the buffer with nothing. 16 floats is an arbitrary size that may work around
|
|
|
|
// driver issues.
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, attributelessVBO);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 16, nullptr, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// We must also define vertex attribute 0.
|
|
|
|
glBindVertexArray(attributelessVAO);
|
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
|
|
|
|
glEnableVertexAttribArray(0);
|
2014-12-04 00:24:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGL_BindAttributelessVAO()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
_dbg_assert_msg_(VIDEO, attributelessVAO != 0,
|
|
|
|
"Attributeless VAO should have already been created.")
|
|
|
|
glBindVertexArray(attributelessVAO);
|
2014-12-04 00:24:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGL_DeleteAttributelessVAO()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
_dbg_assert_msg_(VIDEO, attributelessVAO != 0,
|
|
|
|
"Attributeless VAO should have already been created.") if (attributelessVAO != 0)
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(1, &attributelessVAO);
|
|
|
|
glDeleteBuffers(1, &attributelessVBO);
|
|
|
|
|
|
|
|
attributelessVAO = 0;
|
|
|
|
attributelessVBO = 0;
|
|
|
|
}
|
2014-12-04 00:24:58 -07:00
|
|
|
}
|