2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2021-12-09 19:22:16 -07:00
|
|
|
#include "Common/GL/GLUtil.h"
|
|
|
|
|
2015-12-20 19:49:49 -07:00
|
|
|
#include <memory>
|
|
|
|
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2018-10-03 07:02:45 -06:00
|
|
|
#include "Common/GL/GLContext.h"
|
2015-09-18 11:40:46 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2010-07-16 15:56:40 -06:00
|
|
|
|
2018-03-09 21:52:48 -07:00
|
|
|
namespace GLUtil
|
|
|
|
{
|
|
|
|
GLuint CompileProgram(const std::string& vertexShader, const std::string& fragmentShader)
|
2012-12-12 02:40:03 -07:00
|
|
|
{
|
|
|
|
// generate objects
|
|
|
|
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
GLuint programID = glCreateProgram();
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
// compile vertex shader
|
2015-10-09 01:25:09 -06:00
|
|
|
const char* shader = vertexShader.c_str();
|
|
|
|
glShaderSource(vertexShaderID, 1, &shader, nullptr);
|
2012-12-12 02:40:03 -07:00
|
|
|
glCompileShader(vertexShaderID);
|
2018-02-09 04:01:47 -07:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2013-01-11 13:24:59 -07:00
|
|
|
GLint Result = GL_FALSE;
|
|
|
|
char stringBuffer[1024];
|
|
|
|
GLsizei stringBufferUsage = 0;
|
2012-12-12 02:40:03 -07:00
|
|
|
glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderInfoLog(vertexShaderID, 1024, &stringBufferUsage, stringBuffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-15 12:09:53 -06:00
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(VIDEO, "GLSL vertex shader warnings:\n{}{}", stringBuffer, vertexShader);
|
2014-08-15 12:09:53 -06:00
|
|
|
}
|
|
|
|
else if (!Result)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(VIDEO, "GLSL vertex shader error:\n{}{}", stringBuffer, vertexShader);
|
2014-08-15 12:09:53 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
INFO_LOG_FMT(VIDEO, "GLSL vertex shader compiled:\n{}", vertexShader);
|
2012-12-12 02:40:03 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
bool shader_errors = !Result;
|
|
|
|
#endif
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
// compile fragment shader
|
2015-10-09 01:25:09 -06:00
|
|
|
shader = fragmentShader.c_str();
|
|
|
|
glShaderSource(fragmentShaderID, 1, &shader, nullptr);
|
2012-12-12 02:40:03 -07:00
|
|
|
glCompileShader(fragmentShaderID);
|
2018-02-09 04:01:47 -07:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2012-12-12 02:40:03 -07:00
|
|
|
glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &Result);
|
|
|
|
glGetShaderInfoLog(fragmentShaderID, 1024, &stringBufferUsage, stringBuffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-15 12:09:53 -06:00
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(VIDEO, "GLSL fragment shader warnings:\n{}{}", stringBuffer, fragmentShader);
|
2014-08-15 12:09:53 -06:00
|
|
|
}
|
|
|
|
else if (!Result)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(VIDEO, "GLSL fragment shader error:\n{}{}", stringBuffer, fragmentShader);
|
2014-08-15 12:09:53 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
INFO_LOG_FMT(VIDEO, "GLSL fragment shader compiled:\n{}", fragmentShader);
|
2012-12-12 02:40:03 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
shader_errors |= !Result;
|
|
|
|
#endif
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
// link them
|
|
|
|
glAttachShader(programID, vertexShaderID);
|
|
|
|
glAttachShader(programID, fragmentShaderID);
|
|
|
|
glLinkProgram(programID);
|
2018-02-09 04:01:47 -07:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2012-12-12 02:40:03 -07:00
|
|
|
glGetProgramiv(programID, GL_LINK_STATUS, &Result);
|
|
|
|
glGetProgramInfoLog(programID, 1024, &stringBufferUsage, stringBuffer);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-15 12:09:53 -06:00
|
|
|
if (Result && stringBufferUsage)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(VIDEO, "GLSL linker warnings:\n{}{}{}", stringBuffer, vertexShader,
|
|
|
|
fragmentShader);
|
2014-08-15 12:09:53 -06:00
|
|
|
}
|
|
|
|
else if (!Result && !shader_errors)
|
|
|
|
{
|
2020-10-23 12:41:30 -06:00
|
|
|
ERROR_LOG_FMT(VIDEO, "GLSL linker error:\n{}{}{}", stringBuffer, vertexShader, fragmentShader);
|
2012-12-12 02:40:03 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
// cleanup
|
|
|
|
glDeleteShader(vertexShaderID);
|
|
|
|
glDeleteShader(fragmentShaderID);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-12-12 02:40:03 -07:00
|
|
|
return programID;
|
|
|
|
}
|
2018-03-09 21:54:44 -07:00
|
|
|
|
2018-10-03 07:03:26 -06:00
|
|
|
void EnablePrimitiveRestart(const GLContext* context)
|
2018-03-09 21:54:44 -07:00
|
|
|
{
|
|
|
|
constexpr GLuint PRIMITIVE_RESTART_INDEX = 65535;
|
|
|
|
|
2018-10-03 07:03:26 -06:00
|
|
|
if (context->IsGLES())
|
2018-03-09 21:54:44 -07: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 07:02:45 -06:00
|
|
|
} // namespace GLUtil
|