2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-18 01:08:10 +02:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 05:29:41 +02:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 06:25:12 +01:00
|
|
|
|
2015-12-21 03:49:49 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2015-09-26 22:39:47 +02:00
|
|
|
#include "Common/Assert.h"
|
2018-10-03 15:02:45 +02:00
|
|
|
#include "Common/GL/GLContext.h"
|
2015-09-18 18:40:00 +02:00
|
|
|
#include "Common/GL/GLUtil.h"
|
2015-09-18 19:40:46 +02:00
|
|
|
#include "Common/Logging/Log.h"
|
2010-07-16 23:56:40 +02:00
|
|
|
|
2018-03-10 05:52:48 +01:00
|
|
|
namespace GLUtil
|
|
|
|
{
|
|
|
|
GLuint CompileProgram(const std::string& vertexShader, const std::string& fragmentShader)
|
2012-12-12 10:40:03 +01:00
|
|
|
{
|
|
|
|
// generate objects
|
|
|
|
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
GLuint programID = glCreateProgram();
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
// compile vertex shader
|
2015-10-09 09:25:09 +02:00
|
|
|
const char* shader = vertexShader.c_str();
|
|
|
|
glShaderSource(vertexShaderID, 1, &shader, nullptr);
|
2012-12-12 10:40:03 +01:00
|
|
|
glCompileShader(vertexShaderID);
|
2018-02-09 12:01:47 +01:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2013-01-11 21:24:59 +01:00
|
|
|
GLint Result = GL_FALSE;
|
|
|
|
char stringBuffer[1024];
|
|
|
|
GLsizei stringBufferUsage = 0;
|
2012-12-12 10:40:03 +01:00
|
|
|
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderInfoLog(vertexShaderID, 1024, &stringBufferUsage, stringBuffer);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-08-15 20:09:53 +02:00
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
2015-10-09 09:25:09 +02:00
|
|
|
ERROR_LOG(VIDEO, "GLSL vertex shader warnings:\n%s%s", stringBuffer, vertexShader.c_str());
|
2014-08-15 20:09:53 +02:00
|
|
|
}
|
|
|
|
else if (!Result)
|
|
|
|
{
|
2015-10-09 09:25:09 +02:00
|
|
|
ERROR_LOG(VIDEO, "GLSL vertex shader error:\n%s%s", stringBuffer, vertexShader.c_str());
|
2014-08-15 20:09:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-25 01:06:47 +02:00
|
|
|
INFO_LOG(VIDEO, "GLSL vertex shader compiled:\n%s", vertexShader.c_str());
|
2012-12-12 10:40:03 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
bool shader_errors = !Result;
|
|
|
|
#endif
|
2013-10-29 06:23:17 +01:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
// compile fragment shader
|
2015-10-09 09:25:09 +02:00
|
|
|
shader = fragmentShader.c_str();
|
|
|
|
glShaderSource(fragmentShaderID, 1, &shader, nullptr);
|
2012-12-12 10:40:03 +01:00
|
|
|
glCompileShader(fragmentShaderID);
|
2018-02-09 12:01:47 +01:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2012-12-12 10:40:03 +01:00
|
|
|
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderInfoLog(fragmentShaderID, 1024, &stringBufferUsage, stringBuffer);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-08-15 20:09:53 +02:00
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
2015-10-09 09:25:09 +02:00
|
|
|
ERROR_LOG(VIDEO, "GLSL fragment shader warnings:\n%s%s", stringBuffer, fragmentShader.c_str());
|
2014-08-15 20:09:53 +02:00
|
|
|
}
|
|
|
|
else if (!Result)
|
|
|
|
{
|
2015-10-09 09:25:09 +02:00
|
|
|
ERROR_LOG(VIDEO, "GLSL fragment shader error:\n%s%s", stringBuffer, fragmentShader.c_str());
|
2014-08-15 20:09:53 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-25 01:06:47 +02:00
|
|
|
INFO_LOG(VIDEO, "GLSL fragment shader compiled:\n%s", fragmentShader.c_str());
|
2012-12-12 10:40:03 +01:00
|
|
|
}
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
shader_errors |= !Result;
|
|
|
|
#endif
|
2013-10-29 06:23:17 +01:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
// link them
|
|
|
|
glAttachShader(programID, vertexShaderID);
|
|
|
|
glAttachShader(programID, fragmentShaderID);
|
|
|
|
glLinkProgram(programID);
|
2018-02-09 12:01:47 +01:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2012-12-12 10:40:03 +01:00
|
|
|
glGetProgramiv(programID, GL_LINK_STATUS, &Result);
|
|
|
|
glGetProgramInfoLog(programID, 1024, &stringBufferUsage, stringBuffer);
|
2016-06-24 10:43:46 +02:00
|
|
|
|
2014-08-15 20:09:53 +02:00
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
2015-10-09 09:25:09 +02:00
|
|
|
ERROR_LOG(VIDEO, "GLSL linker warnings:\n%s%s%s", stringBuffer, vertexShader.c_str(),
|
|
|
|
fragmentShader.c_str());
|
2014-08-15 20:09:53 +02:00
|
|
|
}
|
|
|
|
else if (!Result && !shader_errors)
|
|
|
|
{
|
2015-10-09 09:25:09 +02:00
|
|
|
ERROR_LOG(VIDEO, "GLSL linker error:\n%s%s%s", stringBuffer, vertexShader.c_str(),
|
|
|
|
fragmentShader.c_str());
|
2012-12-12 10:40:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-29 06:23:17 +01:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
// cleanup
|
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
glDeleteShader(fragmentShaderID);
|
2013-10-29 06:23:17 +01:00
|
|
|
|
2012-12-12 10:40:03 +01:00
|
|
|
return programID;
|
|
|
|
}
|
2018-03-10 05:54:44 +01:00
|
|
|
|
2018-10-03 15:03:26 +02:00
|
|
|
void EnablePrimitiveRestart(const GLContext* context)
|
2018-03-10 05:54:44 +01:00
|
|
|
{
|
|
|
|
constexpr GLuint PRIMITIVE_RESTART_INDEX = 65535;
|
|
|
|
|
2018-10-03 15:03:26 +02:00
|
|
|
if (context->IsGLES())
|
2018-03-10 05:54:44 +01:00
|
|
|
{
|
|
|
|
glEnable(GL_PRIMITIVE_RESTART_FIXED_INDEX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GLExtensions::Version() >= 310)
|
|
|
|
{
|
|
|
|
glEnable(GL_PRIMITIVE_RESTART);
|
|
|
|
glPrimitiveRestartIndex(PRIMITIVE_RESTART_INDEX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glEnableClientState(GL_PRIMITIVE_RESTART_NV);
|
|
|
|
glPrimitiveRestartIndexNV(PRIMITIVE_RESTART_INDEX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 15:02:45 +02:00
|
|
|
} // namespace GLUtil
|