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