2014-06-15 05:52:26 +00:00
|
|
|
/*
|
|
|
|
** gl-fun.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2014 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-05-30 21:01:35 +00:00
|
|
|
#include "gl-fun.h"
|
|
|
|
|
|
|
|
#include "boost-hash.h"
|
|
|
|
#include "exception.h"
|
|
|
|
|
|
|
|
#include <SDL_video.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
GLFunctions gl;
|
|
|
|
|
2014-11-22 16:03:16 +00:00
|
|
|
typedef const GLubyte* (APIENTRYP _PFNGLGETSTRINGIPROC) (GLenum, GLuint);
|
2014-05-30 21:01:35 +00:00
|
|
|
|
2014-11-22 16:03:16 +00:00
|
|
|
static void parseExtensionsCore(_PFNGLGETINTEGERVPROC GetIntegerv, BoostSet<std::string> &out)
|
2014-05-30 21:01:35 +00:00
|
|
|
{
|
2014-11-22 16:03:16 +00:00
|
|
|
_PFNGLGETSTRINGIPROC GetStringi =
|
|
|
|
(_PFNGLGETSTRINGIPROC) SDL_GL_GetProcAddress("glGetStringi");
|
2014-05-30 21:01:35 +00:00
|
|
|
|
|
|
|
GLint extCount = 0;
|
|
|
|
GetIntegerv(GL_NUM_EXTENSIONS, &extCount);
|
|
|
|
|
|
|
|
for (GLint i = 0; i < extCount; ++i)
|
|
|
|
out.insert((const char*) GetStringi(GL_EXTENSIONS, i));
|
|
|
|
}
|
|
|
|
|
2014-11-22 16:03:16 +00:00
|
|
|
static void parseExtensionsCompat(_PFNGLGETSTRINGPROC GetString, BoostSet<std::string> &out)
|
2014-05-30 21:01:35 +00:00
|
|
|
{
|
|
|
|
const char *ext = (const char*) GetString(GL_EXTENSIONS);
|
|
|
|
|
|
|
|
if (!ext)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buffer[0x100];
|
|
|
|
size_t bufferI;
|
|
|
|
|
|
|
|
while (*ext)
|
|
|
|
{
|
|
|
|
bufferI = 0;
|
|
|
|
while (*ext && *ext != ' ')
|
|
|
|
buffer[bufferI++] = *ext++;
|
|
|
|
|
|
|
|
buffer[bufferI] = '\0';
|
|
|
|
|
|
|
|
out.insert(buffer);
|
|
|
|
|
|
|
|
if (*ext == ' ')
|
|
|
|
++ext;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GL_FUN(name, type) \
|
|
|
|
gl.name = (type) SDL_GL_GetProcAddress("gl" #name EXT_SUFFIX);
|
|
|
|
|
|
|
|
#define EXC(msg) \
|
|
|
|
Exception(Exception::MKXPError, "%s", msg)
|
|
|
|
|
|
|
|
void initGLFunctions()
|
|
|
|
{
|
|
|
|
#define EXT_SUFFIX ""
|
|
|
|
GL_20_FUN;
|
|
|
|
|
|
|
|
/* Determine GL version */
|
|
|
|
const char *ver = (const char*) gl.GetString(GL_VERSION);
|
|
|
|
|
2014-07-19 14:26:42 +00:00
|
|
|
const char *glesPrefix = "OpenGL ES ";
|
|
|
|
size_t glesPrefixN = strlen(glesPrefix);
|
|
|
|
|
|
|
|
bool gles = false;
|
|
|
|
|
|
|
|
if (!strncmp(ver, glesPrefix, glesPrefixN))
|
|
|
|
{
|
|
|
|
gles = true;
|
|
|
|
gl.glsles = true;
|
|
|
|
|
|
|
|
ver += glesPrefixN;
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:01:35 +00:00
|
|
|
/* Assume single digit */
|
|
|
|
int glMajor = *ver - '0';
|
|
|
|
|
|
|
|
if (glMajor < 2)
|
2014-07-19 14:26:42 +00:00
|
|
|
throw EXC("At least OpenGL (ES) 2.0 is required");
|
2014-05-30 21:01:35 +00:00
|
|
|
|
2014-12-23 18:23:08 +00:00
|
|
|
if (gles)
|
|
|
|
{
|
|
|
|
GL_ES_FUN;
|
|
|
|
}
|
|
|
|
|
2014-05-30 21:01:35 +00:00
|
|
|
BoostSet<std::string> ext;
|
|
|
|
|
|
|
|
if (glMajor >= 3)
|
|
|
|
parseExtensionsCore(gl.GetIntegerv, ext);
|
|
|
|
else
|
|
|
|
parseExtensionsCompat(gl.GetString, ext);
|
|
|
|
|
|
|
|
#define HAVE_EXT(_ext) ext.contains("GL_" #_ext)
|
|
|
|
|
2014-07-13 11:38:50 +00:00
|
|
|
/* FBO entrypoints */
|
2014-07-19 14:26:42 +00:00
|
|
|
if (glMajor >= 3 || HAVE_EXT(ARB_framebuffer_object))
|
2014-05-30 21:01:35 +00:00
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
2014-07-19 14:26:42 +00:00
|
|
|
#define EXT_SUFFIX ""
|
2014-05-30 21:01:35 +00:00
|
|
|
GL_FBO_FUN;
|
2014-07-19 14:26:42 +00:00
|
|
|
GL_FBO_BLIT_FUN;
|
2014-05-30 21:01:35 +00:00
|
|
|
}
|
2014-07-19 14:26:42 +00:00
|
|
|
else if (gles && glMajor == 2)
|
|
|
|
{
|
|
|
|
GL_FBO_FUN;
|
|
|
|
}
|
|
|
|
else if (HAVE_EXT(EXT_framebuffer_object))
|
2014-05-30 21:01:35 +00:00
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
2014-07-19 14:26:42 +00:00
|
|
|
#define EXT_SUFFIX "EXT"
|
2014-05-30 21:01:35 +00:00
|
|
|
GL_FBO_FUN;
|
2014-07-19 14:26:42 +00:00
|
|
|
|
|
|
|
if (HAVE_EXT(EXT_framebuffer_blit))
|
|
|
|
{
|
|
|
|
GL_FBO_BLIT_FUN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw EXC("No FBO support available");
|
2014-05-30 21:01:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 11:38:50 +00:00
|
|
|
/* VAO entrypoints */
|
2014-07-19 14:26:42 +00:00
|
|
|
if (HAVE_EXT(ARB_vertex_array_object) || glMajor >= 3)
|
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
|
|
|
#define EXT_SUFFIX ""
|
|
|
|
GL_VAO_FUN;
|
|
|
|
}
|
|
|
|
else if (HAVE_EXT(APPLE_vertex_array_object))
|
2014-05-30 21:01:35 +00:00
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
|
|
|
#define EXT_SUFFIX "APPLE"
|
|
|
|
GL_VAO_FUN;
|
|
|
|
}
|
2014-07-19 14:26:42 +00:00
|
|
|
else if (HAVE_EXT(OES_vertex_array_object))
|
2014-05-30 21:01:35 +00:00
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
2014-07-19 14:26:42 +00:00
|
|
|
#define EXT_SUFFIX "OES"
|
2014-05-30 21:01:35 +00:00
|
|
|
GL_VAO_FUN;
|
|
|
|
}
|
|
|
|
|
2014-07-13 11:38:50 +00:00
|
|
|
/* Debug callback entrypoints */
|
2014-05-30 21:01:35 +00:00
|
|
|
if (HAVE_EXT(KHR_debug))
|
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
|
|
|
#define EXT_SUFFIX ""
|
|
|
|
GL_DEBUG_KHR_FUN;
|
|
|
|
}
|
|
|
|
else if (HAVE_EXT(ARB_debug_output))
|
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
|
|
|
#define EXT_SUFFIX "ARB"
|
|
|
|
GL_DEBUG_KHR_FUN;
|
|
|
|
}
|
2014-07-19 14:26:42 +00:00
|
|
|
|
2014-11-29 16:33:39 +00:00
|
|
|
if (HAVE_EXT(GREMEDY_string_marker))
|
|
|
|
{
|
|
|
|
#undef EXT_SUFFIX
|
|
|
|
#define EXT_SUFFIX "GREMEDY"
|
|
|
|
GL_GREMEMDY_FUN;
|
|
|
|
}
|
|
|
|
|
2014-07-19 14:26:42 +00:00
|
|
|
/* Misc caps */
|
|
|
|
if (!gles || glMajor >= 3 || HAVE_EXT(EXT_unpack_subimage))
|
|
|
|
gl.unpack_subimage = true;
|
|
|
|
|
|
|
|
if (!gles || glMajor >= 3 || HAVE_EXT(OES_texture_npot))
|
|
|
|
gl.npot_repeat = true;
|
2014-05-30 21:01:35 +00:00
|
|
|
}
|