Eliminate GLEW dependency

GL entrypoint resolution is now done manually. This has a couple
immediate benefits, such as not having to retrieve hundreds of
functions pointers that we'll never use. It's also nice to have
an exact overview of all the entrypoints used by mkxp.

This change allows mkxp to run fine with core contexts, not sure
how relevant that is going to be in the future.

What's noteworthy is that  _all_ entrypoints, even the ones core
in 1.1 and guaranteed to be in every libGL, are resolved
dynamically.
This has the added benefit of not having to link directly against
libGL anymore, which also cleans up the output of `ldd` quite
a bit (SDL2 loads most system deps dynamically at runtime).

GL headers are still required at build time.
This commit is contained in:
Jonas Kulla 2014-05-30 23:01:35 +02:00
parent 640b01e518
commit 6c481e5eb8
16 changed files with 470 additions and 272 deletions

View File

@ -84,10 +84,8 @@ pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image) pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL_SOUND REQUIRED SDL_sound) pkg_check_modules(SDL_SOUND REQUIRED SDL_sound)
find_package(GLEW 1.9.0 REQUIRED)
find_package(Boost 1.49 COMPONENTS program_options REQUIRED) find_package(Boost 1.49 COMPONENTS program_options REQUIRED)
find_package(OpenAL REQUIRED) find_package(OpenAL REQUIRED)
find_package(OpenGL REQUIRED)
find_package(ZLIB REQUIRED) find_package(ZLIB REQUIRED)
## Setup main source ## ## Setup main source ##
@ -135,6 +133,7 @@ set(MAIN_HEADERS
src/al-util.h src/al-util.h
src/boost-hash.h src/boost-hash.h
src/debugwriter.h src/debugwriter.h
src/gl-fun.h
) )
set(MAIN_SOURCE set(MAIN_SOURCE
@ -164,6 +163,7 @@ set(MAIN_SOURCE
src/tileatlas.cpp src/tileatlas.cpp
src/perftimer.cpp src/perftimer.cpp
src/sharedstate.cpp src/sharedstate.cpp
src/gl-fun.cpp
) )
source_group("MKXP Source" FILES ${MAIN_SOURCE} ${MAIN_HEADERS}) source_group("MKXP Source" FILES ${MAIN_SOURCE} ${MAIN_HEADERS})
@ -352,7 +352,6 @@ target_include_directories(${PROJECT_NAME} PRIVATE
${PHYSFS_INCLUDE_DIRS} ${PHYSFS_INCLUDE_DIRS}
${SDL2_INCLUDE_DIRS} # Blindly assume other SDL bits are in same directory ${SDL2_INCLUDE_DIRS} # Blindly assume other SDL bits are in same directory
${Boost_INCLUDE_DIR} ${Boost_INCLUDE_DIR}
${GLEW_INCLUDE_DIR}/GL
${MRI_INCLUDE_DIRS} ${MRI_INCLUDE_DIRS}
${OPENAL_INCLUDE_DIR} ${OPENAL_INCLUDE_DIR}
) )
@ -368,8 +367,6 @@ target_link_libraries(${PROJECT_NAME}
${Boost_LIBRARIES} ${Boost_LIBRARIES}
${MRI_LIBRARIES} ${MRI_LIBRARIES}
${OPENAL_LIBRARY} ${OPENAL_LIBRARY}
${OPENGL_gl_LIBRARY}
${GLEW_LIBRARY}
${ZLIB_LIBRARY} ${ZLIB_LIBRARY}
${PLATFORM_LIBRARIES} ${PLATFORM_LIBRARIES}

View File

@ -35,7 +35,6 @@ This binding only exists for testing purposes and does nothing (the engine quits
* Boost.Program_options * Boost.Program_options
* libsigc++ 2.0 * libsigc++ 2.0
* PhysFS (latest hg) * PhysFS (latest hg)
* GLEW >= 1.7
* OpenAL * OpenAL
* SDL2 * SDL2
* SDL2_image * SDL2_image

View File

@ -50,7 +50,7 @@ RGSS3 {
unix { unix {
CONFIG += link_pkgconfig CONFIG += link_pkgconfig
PKGCONFIG += sigc++-2.0 glew pixman-1 zlib physfs \ PKGCONFIG += sigc++-2.0 pixman-1 zlib physfs \
sdl2 SDL2_image SDL2_ttf SDL_sound openal sdl2 SDL2_image SDL2_ttf SDL_sound openal
RGSS2 { RGSS2 {
@ -124,7 +124,8 @@ HEADERS += \
src/sharedstate.h \ src/sharedstate.h \
src/al-util.h \ src/al-util.h \
src/boost-hash.h \ src/boost-hash.h \
src/debugwriter.h src/debugwriter.h \
src/gl-fun.h
SOURCES += \ SOURCES += \
src/main.cpp \ src/main.cpp \
@ -152,7 +153,8 @@ SOURCES += \
src/config.cpp \ src/config.cpp \
src/tileatlas.cpp \ src/tileatlas.cpp \
src/perftimer.cpp \ src/perftimer.cpp \
src/sharedstate.cpp src/sharedstate.cpp \
src/gl-fun.cpp
EMBED = \ EMBED = \
shader/transSimple.frag \ shader/transSimple.frag \

View File

@ -655,7 +655,7 @@ Color Bitmap::getPixel(int x, int y) const
glState.viewport.pushSet(IntRect(0, 0, width(), height())); glState.viewport.pushSet(IntRect(0, 0, width(), height()));
uint8_t pixel[4]; uint8_t pixel[4];
glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel); gl.ReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &pixel);
glState.viewport.pop(); glState.viewport.pop();

View File

@ -22,9 +22,10 @@
#include "debuglogger.h" #include "debuglogger.h"
#include "debugwriter.h" #include "debugwriter.h"
#include <glew.h>
#include <iostream> #include <iostream>
#include "gl-fun.h"
struct DebugLoggerPrivate struct DebugLoggerPrivate
{ {
std::ostream *stream; std::ostream *stream;
@ -53,31 +54,13 @@ struct DebugLoggerPrivate
} }
}; };
static void amdDebugFunc(GLuint id, static void APIENTRY arbDebugFunc(GLenum source,
GLenum category, GLenum type,
GLenum severity, GLuint id,
GLsizei length, GLenum severity,
const GLchar* message, GLsizei length,
GLvoid* userParam) const GLchar* message,
{ GLvoid* userParam)
DebugLoggerPrivate *p = static_cast<DebugLoggerPrivate*>(userParam);
(void) id;
(void) category;
(void) severity;
(void) length;
p->writeTimestamp();
p->writeLine(message);
}
static void arbDebugFunc(GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
GLvoid* userParam)
{ {
DebugLoggerPrivate *p = static_cast<DebugLoggerPrivate*>(userParam); DebugLoggerPrivate *p = static_cast<DebugLoggerPrivate*>(userParam);
@ -95,12 +78,8 @@ DebugLogger::DebugLogger(const char *filename)
{ {
p = new DebugLoggerPrivate(filename); p = new DebugLoggerPrivate(filename);
if (GLEW_KHR_debug) if (gl.DebugMessageCallback)
glDebugMessageCallback(arbDebugFunc, p); gl.DebugMessageCallback(arbDebugFunc, p);
else if (GLEW_ARB_debug_output)
glDebugMessageCallbackARB(arbDebugFunc, p);
else if (GLEW_AMD_debug_output)
glDebugMessageCallbackAMD(amdDebugFunc, p);
else else
Debug() << "DebugLogger: no debug extensions found"; Debug() << "DebugLogger: no debug extensions found";
} }

144
src/gl-fun.cpp Normal file
View File

@ -0,0 +1,144 @@
#include "gl-fun.h"
#include "boost-hash.h"
#include "exception.h"
#include <SDL_video.h>
#include <string>
GLFunctions gl;
typedef const GLubyte* (APIENTRYP PFNGLGETSTRINGIPROC) (GLenum, GLuint);
static void parseExtensionsCore(PFNGLGETINTEGERVPROC GetIntegerv, BoostSet<std::string> &out)
{
PFNGLGETSTRINGIPROC GetStringi =
(PFNGLGETSTRINGIPROC) SDL_GL_GetProcAddress("glGetStringi");
GLint extCount = 0;
GetIntegerv(GL_NUM_EXTENSIONS, &extCount);
for (GLint i = 0; i < extCount; ++i)
out.insert((const char*) GetStringi(GL_EXTENSIONS, i));
}
static void parseExtensionsCompat(PFNGLGETSTRINGPROC GetString, BoostSet<std::string> &out)
{
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);
/* Assume single digit */
int glMajor = *ver - '0';
if (glMajor < 2)
throw EXC("At least OpenGL 2.0 is required");
BoostSet<std::string> ext;
if (glMajor >= 3)
parseExtensionsCore(gl.GetIntegerv, ext);
else
parseExtensionsCompat(gl.GetString, ext);
#define HAVE_EXT(_ext) ext.contains("GL_" #_ext)
if (!HAVE_EXT(ARB_framebuffer_object))
{
if (!(HAVE_EXT(EXT_framebuffer_object) && HAVE_EXT(EXT_framebuffer_blit)))
throw EXC("No FBO extensions available");
#undef EXT_SUFFIX
#define EXT_SUFFIX "EXT"
GL_FBO_FUN;
}
else
{
#undef EXT_SUFFIX
#define EXT_SUFFIX ""
GL_FBO_FUN;
}
if (!HAVE_EXT(ARB_vertex_array_object))
{
if (!HAVE_EXT(APPLE_vertex_array_object))
throw EXC("No VAO extensions available");
#undef EXT_SUFFIX
#define EXT_SUFFIX "APPLE"
GL_VAO_FUN;
}
else
{
#undef EXT_SUFFIX
#define EXT_SUFFIX ""
GL_VAO_FUN;
}
if (HAVE_EXT(ARB_timer_query))
{
gl.timerQuery = true;
#undef EXT_SUFFIX
#define EXT_SUFFIX ""
GL_TIMER_QUERY_FUN;
}
else if (HAVE_EXT(EXT_timer_query))
{
gl.timerQuery = true;
#undef EXT_SUFFIX
#define EXT_SUFFIX "EXT"
GL_TIMER_QUERY_FUN;
}
else
{
gl.timerQuery = false;
}
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;
}
}

148
src/gl-fun.h Normal file
View File

@ -0,0 +1,148 @@
#ifndef GLFUN_H
#define GLFUN_H
#include <SDL_opengl.h>
/* Etc */
typedef GLenum (APIENTRYP PFNGLGETERRORPROC) (void);
typedef void (APIENTRYP PFNGLCLEARCOLORPROC) (GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
typedef void (APIENTRYP PFNGLCLEARPROC) (GLbitfield mask);
typedef const GLubyte * (APIENTRYP PFNGLGETSTRINGPROC) (GLenum name);
typedef void (APIENTRYP PFNGLGETINTEGERVPROC) (GLenum pname, GLint *params);
typedef void (APIENTRYP PFNGLPIXELSTOREIPROC) (GLenum pname, GLint param);
typedef void (APIENTRYP PFNGLREADPIXELSPROC) (GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid *pixels);
typedef void (APIENTRYP PFNGLENABLEPROC) (GLenum cap);
typedef void (APIENTRYP PFNGLDISABLEPROC) (GLenum cap);
typedef void (APIENTRYP PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
typedef void (APIENTRYP PFNGLVIEWPORTPROC) (GLint x, GLint y, GLsizei width, GLsizei height);
typedef void (APIENTRYP PFNGLBLENDFUNCPROC) (GLenum sfactor, GLenum dfactor);
typedef void (APIENTRYP PFNGLBLENDFUNCSEPARATEPROC) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha);
typedef void (APIENTRYP PFNGLBLENDEQUATIONPROC) (GLenum mode);
typedef void (APIENTRYP PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const GLvoid *indices);
/* Texture */
typedef void (APIENTRYP PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures);
typedef void (APIENTRYP PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures);
typedef void (APIENTRYP PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture);
typedef void (APIENTRYP PFNGLTEXIMAGE2DPROC) (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels);
typedef void (APIENTRYP PFNGLTEXSUBIMAGE2DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels);
typedef void (APIENTRYP PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param);
typedef void (APIENTRYP PFNGLACTIVETEXTUREPROC) (GLenum texture);
/* Debug callback */
typedef void (APIENTRY * GLDEBUGPROC) (GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, GLvoid* userParam);
typedef void (APIENTRYP PFNGLDEBUGMESSAGECALLBACKPROC) (GLDEBUGPROC callback, const GLvoid *userParam);
#define GL_20_FUN \
/* Etc */ \
GL_FUN(GetError, PFNGLGETERRORPROC) \
GL_FUN(ClearColor, PFNGLCLEARCOLORPROC) \
GL_FUN(Clear, PFNGLCLEARPROC) \
GL_FUN(GetString, PFNGLGETSTRINGPROC) \
GL_FUN(GetIntegerv, PFNGLGETINTEGERVPROC) \
GL_FUN(PixelStorei, PFNGLPIXELSTOREIPROC) \
GL_FUN(ReadPixels, PFNGLREADPIXELSPROC) \
GL_FUN(Enable, PFNGLENABLEPROC) \
GL_FUN(Disable, PFNGLDISABLEPROC) \
GL_FUN(Scissor, PFNGLSCISSORPROC) \
GL_FUN(Viewport, PFNGLVIEWPORTPROC) \
GL_FUN(BlendFunc, PFNGLBLENDFUNCPROC) \
GL_FUN(BlendFuncSeparate, PFNGLBLENDFUNCSEPARATEPROC) \
GL_FUN(BlendEquation, PFNGLBLENDEQUATIONPROC) \
GL_FUN(DrawElements, PFNGLDRAWELEMENTSPROC) \
/* Texture */ \
GL_FUN(GenTextures, PFNGLGENTEXTURESPROC) \
GL_FUN(DeleteTextures, PFNGLDELETETEXTURESPROC) \
GL_FUN(BindTexture, PFNGLBINDTEXTUREPROC) \
GL_FUN(TexImage2D, PFNGLTEXIMAGE2DPROC) \
GL_FUN(TexSubImage2D, PFNGLTEXSUBIMAGE2DPROC) \
GL_FUN(TexParameteri, PFNGLTEXPARAMETERIPROC) \
GL_FUN(ActiveTexture, PFNGLACTIVETEXTUREPROC) \
/* Buffer objcet */ \
GL_FUN(GenBuffers, PFNGLGENBUFFERSPROC) \
GL_FUN(DeleteBuffers, PFNGLDELETEBUFFERSPROC) \
GL_FUN(BindBuffer, PFNGLBINDBUFFERPROC) \
GL_FUN(BufferData, PFNGLBUFFERDATAPROC) \
GL_FUN(BufferSubData, PFNGLBUFFERSUBDATAPROC) \
/* Shader */ \
GL_FUN(CreateShader, PFNGLCREATESHADERPROC) \
GL_FUN(DeleteShader, PFNGLDELETESHADERPROC) \
GL_FUN(ShaderSource, PFNGLSHADERSOURCEPROC) \
GL_FUN(CompileShader, PFNGLCOMPILESHADERPROC) \
GL_FUN(AttachShader, PFNGLATTACHSHADERPROC) \
GL_FUN(GetObjectParameterivARB, PFNGLGETOBJECTPARAMETERIVARBPROC) \
GL_FUN(GetShaderiv, PFNGLGETSHADERIVPROC) \
GL_FUN(GetShaderInfoLog, PFNGLGETSHADERINFOLOGPROC) \
/* Program */ \
GL_FUN(CreateProgram, PFNGLCREATEPROGRAMPROC) \
GL_FUN(DeleteProgram, PFNGLDELETEPROGRAMPROC) \
GL_FUN(UseProgram, PFNGLUSEPROGRAMPROC) \
GL_FUN(LinkProgram, PFNGLLINKPROGRAMPROC) \
GL_FUN(GetProgramiv, PFNGLGETPROGRAMIVPROC) \
GL_FUN(GetProgramInfoLog, PFNGLGETPROGRAMINFOLOGPROC) \
/* Uniform */ \
GL_FUN(GetUniformLocation, PFNGLGETUNIFORMLOCATIONPROC) \
GL_FUN(Uniform1f, PFNGLUNIFORM1FPROC) \
GL_FUN(Uniform2f, PFNGLUNIFORM2FPROC) \
GL_FUN(Uniform4f, PFNGLUNIFORM4FPROC) \
GL_FUN(Uniform1i, PFNGLUNIFORM1IPROC) \
GL_FUN(UniformMatrix4fv, PFNGLUNIFORMMATRIX4FVPROC) \
/* Vertex attribute */ \
GL_FUN(BindAttribLocation, PFNGLBINDATTRIBLOCATIONPROC) \
GL_FUN(EnableVertexAttribArray, PFNGLENABLEVERTEXATTRIBARRAYPROC) \
GL_FUN(VertexAttribPointer, PFNGLVERTEXATTRIBPOINTERPROC) \
/* Queries */ \
GL_FUN(GenQueries, PFNGLGENQUERIESPROC) \
GL_FUN(DeleteQueries, PFNGLDELETEQUERIESPROC) \
GL_FUN(BeginQuery, PFNGLBEGINQUERYPROC) \
GL_FUN(EndQuery, PFNGLENDQUERYPROC) \
GL_FUN(GetQueryObjectiv, PFNGLGETQUERYOBJECTIVPROC)
#define GL_FBO_FUN \
/* Framebuffer object */ \
GL_FUN(GenFramebuffers, PFNGLGENFRAMEBUFFERSPROC) \
GL_FUN(DeleteFramebuffers, PFNGLDELETEFRAMEBUFFERSPROC) \
GL_FUN(BindFramebuffer, PFNGLBINDFRAMEBUFFERPROC) \
GL_FUN(FramebufferTexture2D, PFNGLFRAMEBUFFERTEXTURE2DPROC) \
GL_FUN(FramebufferRenderbuffer, PFNGLFRAMEBUFFERRENDERBUFFERPROC) \
GL_FUN(BlitFramebuffer, PFNGLBLITFRAMEBUFFERPROC) \
/* Renderbuffer object */ \
GL_FUN(GenRenderbuffers, PFNGLGENRENDERBUFFERSPROC) \
GL_FUN(DeleteRenderbuffers, PFNGLDELETERENDERBUFFERSPROC) \
GL_FUN(BindRenderbuffer, PFNGLBINDRENDERBUFFERPROC) \
GL_FUN(RenderbufferStorage, PFNGLRENDERBUFFERSTORAGEPROC)
#define GL_VAO_FUN \
/* Vertex array object */ \
GL_FUN(GenVertexArrays, PFNGLGENVERTEXARRAYSPROC) \
GL_FUN(DeleteVertexArrays, PFNGLDELETEVERTEXARRAYSPROC) \
GL_FUN(BindVertexArray, PFNGLBINDVERTEXARRAYPROC)
#define GL_TIMER_QUERY_FUN \
GL_FUN(GetQueryObjecti64v, PFNGLGETQUERYOBJECTI64VPROC) \
GL_FUN(GetQueryObjectui64v, PFNGLGETQUERYOBJECTUI64VPROC)
#define GL_DEBUG_KHR_FUN \
GL_FUN(DebugMessageCallback, PFNGLDEBUGMESSAGECALLBACKPROC)
struct GLFunctions
{
#define GL_FUN(name, type) type name;
GL_20_FUN
GL_FBO_FUN
GL_VAO_FUN
bool timerQuery;
GL_TIMER_QUERY_FUN
GL_DEBUG_KHR_FUN
#undef GL_FUN
};
extern GLFunctions gl;
void initGLFunctions();
#endif // GLFUN_H

View File

@ -22,8 +22,7 @@
#ifndef GLUTIL_H #ifndef GLUTIL_H
#define GLUTIL_H #define GLUTIL_H
#include <glew.h> #include "gl-fun.h"
#include "etc-internal.h" #include "etc-internal.h"
/* Struct wrapping GLuint for some light type safety */ /* Struct wrapping GLuint for some light type safety */
@ -53,19 +52,19 @@ namespace TEX
inline ID gen() inline ID gen()
{ {
ID id; ID id;
glGenTextures(1, &id.gl); gl.GenTextures(1, &id.gl);
return id; return id;
} }
inline void del(ID id) inline void del(ID id)
{ {
glDeleteTextures(1, &id.gl); gl.DeleteTextures(1, &id.gl);
} }
inline void bind(ID id) inline void bind(ID id)
{ {
glBindTexture(GL_TEXTURE_2D, id.gl); gl.BindTexture(GL_TEXTURE_2D, id.gl);
} }
inline void unbind() inline void unbind()
@ -75,29 +74,29 @@ namespace TEX
inline void uploadImage(GLsizei width, GLsizei height, const void *data, GLenum format) inline void uploadImage(GLsizei width, GLsizei height, const void *data, GLenum format)
{ {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, format, GL_UNSIGNED_BYTE, data); gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, format, GL_UNSIGNED_BYTE, data);
} }
inline void uploadSubImage(GLint x, GLint y, GLsizei width, GLsizei height, const void *data, GLenum format) inline void uploadSubImage(GLint x, GLint y, GLsizei width, GLsizei height, const void *data, GLenum format)
{ {
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, format, GL_UNSIGNED_BYTE, data); gl.TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, format, GL_UNSIGNED_BYTE, data);
} }
inline void allocEmpty(GLsizei width, GLsizei height) inline void allocEmpty(GLsizei width, GLsizei height)
{ {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); gl.TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
} }
inline void setRepeat(bool mode) inline void setRepeat(bool mode)
{ {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode ? GL_REPEAT : GL_CLAMP_TO_EDGE); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mode ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode ? GL_REPEAT : GL_CLAMP_TO_EDGE); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mode ? GL_REPEAT : GL_CLAMP_TO_EDGE);
} }
inline void setSmooth(bool mode) inline void setSmooth(bool mode)
{ {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode ? GL_LINEAR : GL_NEAREST); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mode ? GL_LINEAR : GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode ? GL_LINEAR : GL_NEAREST); gl.TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mode ? GL_LINEAR : GL_NEAREST);
} }
} }
@ -109,19 +108,19 @@ namespace RBO
inline ID gen() inline ID gen()
{ {
ID id; ID id;
glGenRenderbuffers(1, &id.gl); gl.GenRenderbuffers(1, &id.gl);
return id; return id;
} }
inline void del(ID id) inline void del(ID id)
{ {
glDeleteRenderbuffers(1, &id.gl); gl.DeleteRenderbuffers(1, &id.gl);
} }
inline void bind(ID id) inline void bind(ID id)
{ {
glBindRenderbuffer(GL_RENDERBUFFER, id.gl); gl.BindRenderbuffer(GL_RENDERBUFFER, id.gl);
} }
inline void unbind() inline void unbind()
@ -131,7 +130,7 @@ namespace RBO
inline void allocEmpty(GLsizei width, GLsizei height) inline void allocEmpty(GLsizei width, GLsizei height)
{ {
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height); gl.RenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
} }
} }
@ -155,14 +154,14 @@ namespace FBO
inline ID gen() inline ID gen()
{ {
ID id; ID id;
glGenFramebuffers(1, &id.gl); gl.GenFramebuffers(1, &id.gl);
return id; return id;
} }
inline void del(ID id) inline void del(ID id)
{ {
glDeleteFramebuffers(1, &id.gl); gl.DeleteFramebuffers(1, &id.gl);
} }
inline void bind(ID id, Mode mode) inline void bind(ID id, Mode mode)
@ -173,7 +172,7 @@ namespace FBO
GL_READ_FRAMEBUFFER GL_READ_FRAMEBUFFER
}; };
glBindFramebuffer(modes[mode], id.gl); gl.BindFramebuffer(modes[mode], id.gl);
} }
inline void unbind(Mode mode) inline void unbind(Mode mode)
@ -183,12 +182,12 @@ namespace FBO
inline void setTarget(TEX::ID target, unsigned colorAttach = 0) inline void setTarget(TEX::ID target, unsigned colorAttach = 0)
{ {
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttach, GL_TEXTURE_2D, target.gl, 0); gl.FramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttach, GL_TEXTURE_2D, target.gl, 0);
} }
inline void setTarget(RBO::ID target, unsigned colorAttach = 0) inline void setTarget(RBO::ID target, unsigned colorAttach = 0)
{ {
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttach, GL_RENDERBUFFER, target.gl); gl.FramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorAttach, GL_RENDERBUFFER, target.gl);
} }
inline void blit(int srcX, int srcY, inline void blit(int srcX, int srcY,
@ -203,9 +202,9 @@ namespace FBO
GL_LINEAR GL_LINEAR
}; };
glBlitFramebuffer(srcX, srcY, srcX+srcW, srcY+srcH, gl.BlitFramebuffer(srcX, srcY, srcX+srcW, srcY+srcH,
dstX, dstY, dstX+dstW, dstY+dstH, dstX, dstY, dstX+dstW, dstY+dstH,
GL_COLOR_BUFFER_BIT, modes[mode]); GL_COLOR_BUFFER_BIT, modes[mode]);
} }
inline void blit(int srcX, int srcY, inline void blit(int srcX, int srcY,
@ -218,7 +217,7 @@ namespace FBO
inline void clear() inline void clear()
{ {
glClear(GL_COLOR_BUFFER_BIT); gl.Clear(GL_COLOR_BUFFER_BIT);
} }
} }
@ -230,19 +229,19 @@ namespace VAO
inline ID gen() inline ID gen()
{ {
ID id; ID id;
glGenVertexArrays(1, &id.gl); gl.GenVertexArrays(1, &id.gl);
return id; return id;
} }
inline void del(ID id) inline void del(ID id)
{ {
glDeleteVertexArrays(1, &id.gl); gl.DeleteVertexArrays(1, &id.gl);
} }
inline void bind(ID id) inline void bind(ID id)
{ {
glBindVertexArray(id.gl); gl.BindVertexArray(id.gl);
} }
inline void unbind() inline void unbind()
@ -259,19 +258,19 @@ struct GenericBO
inline static ID gen() inline static ID gen()
{ {
ID id; ID id;
glGenBuffers(1, &id.gl); gl.GenBuffers(1, &id.gl);
return id; return id;
} }
inline static void del(ID id) inline static void del(ID id)
{ {
glDeleteBuffers(1, &id.gl); gl.DeleteBuffers(1, &id.gl);
} }
inline static void bind(ID id) inline static void bind(ID id)
{ {
glBindBuffer(target, id.gl); gl.BindBuffer(target, id.gl);
} }
inline static void unbind() inline static void unbind()
@ -281,12 +280,12 @@ struct GenericBO
inline static void uploadData(GLsizeiptr size, const GLvoid *data, GLenum usage = GL_STATIC_DRAW) inline static void uploadData(GLsizeiptr size, const GLvoid *data, GLenum usage = GL_STATIC_DRAW)
{ {
glBufferData(target, size, data, usage); gl.BufferData(target, size, data, usage);
} }
inline static void uploadSubData(GLintptr offset, GLsizeiptr size, const GLvoid *data) inline static void uploadSubData(GLintptr offset, GLsizeiptr size, const GLvoid *data)
{ {
glBufferSubData(target, offset, size, data); gl.BufferSubData(target, offset, size, data);
} }
inline static void allocEmpty(GLsizeiptr size, GLenum usage = GL_STATIC_DRAW) inline static void allocEmpty(GLsizeiptr size, GLenum usage = GL_STATIC_DRAW)
@ -309,9 +308,9 @@ namespace PixelStore
* itself is part of a bigger image in client memory */ * itself is part of a bigger image in client memory */
inline void setupSubImage(GLint imgWidth, GLint subX, GLint subY) inline void setupSubImage(GLint imgWidth, GLint subX, GLint subY)
{ {
glPixelStorei(GL_UNPACK_ROW_LENGTH, imgWidth); gl.PixelStorei(GL_UNPACK_ROW_LENGTH, imgWidth);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, subX); gl.PixelStorei(GL_UNPACK_SKIP_PIXELS, subX);
glPixelStorei(GL_UNPACK_SKIP_ROWS, subY); gl.PixelStorei(GL_UNPACK_SKIP_ROWS, subY);
} }
/* Reset all states set with 'setupSubImage()' */ /* Reset all states set with 'setupSubImage()' */

View File

@ -22,19 +22,18 @@
#include "glstate.h" #include "glstate.h"
#include "shader.h" #include "shader.h"
#include "etc.h" #include "etc.h"
#include "gl-fun.h"
#include <glew.h>
#include <SDL_rect.h> #include <SDL_rect.h>
void GLClearColor::apply(const Vec4 &value) void GLClearColor::apply(const Vec4 &value)
{ {
glClearColor(value.x, value.y, value.z, value.w); gl.ClearColor(value.x, value.y, value.z, value.w);
} }
void GLScissorBox::apply(const IntRect &value) void GLScissorBox::apply(const IntRect &value)
{ {
glScissor(value.x, value.y, value.w, value.h); gl.Scissor(value.x, value.y, value.w, value.h);
} }
void GLScissorBox::setIntersect(const IntRect &value) void GLScissorBox::setIntersect(const IntRect &value)
@ -53,7 +52,7 @@ void GLScissorBox::setIntersect(const IntRect &value)
void GLScissorTest::apply(const bool &value) void GLScissorTest::apply(const bool &value)
{ {
value ? glEnable(GL_SCISSOR_TEST) : glDisable(GL_SCISSOR_TEST); value ? gl.Enable(GL_SCISSOR_TEST) : gl.Disable(GL_SCISSOR_TEST);
} }
void GLBlendMode::apply(const BlendType &value) void GLBlendMode::apply(const BlendType &value)
@ -61,50 +60,50 @@ void GLBlendMode::apply(const BlendType &value)
switch (value) switch (value)
{ {
case BlendNone : case BlendNone :
glBlendEquation(GL_FUNC_ADD); gl.BlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_ONE, GL_ZERO); gl.BlendFunc(GL_ONE, GL_ZERO);
break; break;
case BlendNormal : case BlendNormal :
glBlendEquation(GL_FUNC_ADD); gl.BlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, gl.BlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_ONE, GL_ONE_MINUS_SRC_ALPHA); GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
break; break;
case BlendAddition : case BlendAddition :
glBlendEquation(GL_FUNC_ADD); gl.BlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, gl.BlendFuncSeparate(GL_SRC_ALPHA, GL_ONE,
GL_ONE, GL_ONE); GL_ONE, GL_ONE);
break; break;
case BlendSubstraction : case BlendSubstraction :
// FIXME Alpha calculation is untested // FIXME Alpha calculation is untested
glBlendEquation(GL_FUNC_REVERSE_SUBTRACT); gl.BlendEquation(GL_FUNC_REVERSE_SUBTRACT);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, gl.BlendFuncSeparate(GL_SRC_ALPHA, GL_ONE,
GL_ONE, GL_ONE); GL_ONE, GL_ONE);
break; break;
} }
} }
void GLViewport::apply(const IntRect &value) void GLViewport::apply(const IntRect &value)
{ {
glViewport(value.x, value.y, value.w, value.h); gl.Viewport(value.x, value.y, value.w, value.h);
} }
void GLProgram::apply(const unsigned int &value) void GLProgram::apply(const unsigned int &value)
{ {
glUseProgram(value); gl.UseProgram(value);
} }
GLState::Caps::Caps() GLState::Caps::Caps()
{ {
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize); gl.GetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
} }
GLState::GLState() GLState::GLState()
{ {
glEnable(GL_BLEND); gl.Enable(GL_BLEND);
glDisable(GL_DEPTH_TEST); gl.Disable(GL_DEPTH_TEST);
clearColor.init(Vec4(0, 0, 0, 1)); clearColor.init(Vec4(0, 0, 0, 1));
blendMode.init(BlendNormal); blendMode.init(BlendNormal);

View File

@ -60,7 +60,7 @@ struct PingPong
TEXFBO::init(rt[i]); TEXFBO::init(rt[i]);
TEXFBO::allocEmpty(rt[i], screenW, screenH); TEXFBO::allocEmpty(rt[i], screenW, screenH);
TEXFBO::linkFBO(rt[i]); TEXFBO::linkFBO(rt[i]);
glClearColor(0, 0, 0, 1); gl.ClearColor(0, 0, 0, 1);
FBO::clear(); FBO::clear();
} }
} }

View File

@ -19,7 +19,6 @@
** along with mkxp. If not, see <http://www.gnu.org/licenses/>. ** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <glew.h>
#include <alc.h> #include <alc.h>
#include <SDL.h> #include <SDL.h>
@ -34,6 +33,7 @@
#include "debuglogger.h" #include "debuglogger.h"
#include "debugwriter.h" #include "debugwriter.h"
#include "exception.h" #include "exception.h"
#include "gl-fun.h"
#include "binding.h" #include "binding.h"
@ -50,7 +50,7 @@ rgssThreadError(RGSSThreadData *rtData, const std::string &msg)
static inline const char* static inline const char*
glGetStringInt(GLenum name) glGetStringInt(GLenum name)
{ {
return (const char*) glGetString(name); return (const char*) gl.GetString(name);
} }
static void static void
@ -62,60 +62,6 @@ printGLInfo()
Debug() << "GLSL Version :" << glGetStringInt(GL_SHADING_LANGUAGE_VERSION); Debug() << "GLSL Version :" << glGetStringInt(GL_SHADING_LANGUAGE_VERSION);
} }
static bool
setupOptionalGLExtensions(RGSSThreadData* threadData)
{
if (!GLEW_ARB_framebuffer_object)
{
if (!GLEW_EXT_framebuffer_object && !GLEW_EXT_framebuffer_blit)
{
rgssThreadError(threadData, "GL extensions \"GL_ARB_framebuffer_object\" or compatible extensiosns GL_EXT_framebuffer_object and GL_EXT_framebuffer_blit are not present");
return false;
}
else
{
/* setup compat */
/* From EXT_framebuffer_object */
glGenRenderbuffers = glGenRenderbuffersEXT;
glDeleteRenderbuffers = glDeleteRenderbuffersEXT;
glBindRenderbuffer = glBindRenderbufferEXT;
glRenderbufferStorage = glRenderbufferStorageEXT;
glGenFramebuffers = glGenFramebuffersEXT;
glDeleteFramebuffers = glDeleteFramebuffersEXT;
glBindFramebuffer = glBindFramebufferEXT;
glFramebufferTexture2D = glFramebufferTexture2DEXT;
glFramebufferRenderbuffer = glFramebufferRenderbufferEXT;
/* From EXT_framebuffer_blit */
glBlitFramebuffer = glBlitFramebufferEXT;
}
}
if (!GLEW_ARB_timer_query && GLEW_EXT_timer_query)
{
glGetQueryObjecti64v = glGetQueryObjecti64vEXT;
glGetQueryObjectui64v = glGetQueryObjectui64vEXT;
}
if (!GLEW_ARB_vertex_array_object )
{
if (!GLEW_APPLE_vertex_array_object)
{
rgssThreadError(threadData, "GL extensions \"GL_ARB_vertex_array_object\" or compatible extensions GL_APPLE_vertex_array_object are not present");
return false;
}
else
{
/* setup compat */
glBindVertexArray = glBindVertexArrayAPPLE;
/* the cast is because apple's uses const GLuint* and ARB doesn't */
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)glGenVertexArraysAPPLE;
glDeleteVertexArrays = glDeleteVertexArraysAPPLE;
}
}
return true;
}
int rgssThreadFun(void *userdata) int rgssThreadFun(void *userdata)
{ {
RGSSThreadData *threadData = static_cast<RGSSThreadData*>(userdata); RGSSThreadData *threadData = static_cast<RGSSThreadData*>(userdata);
@ -136,36 +82,24 @@ int rgssThreadFun(void *userdata)
return 0; return 0;
} }
if (glewInit() != GLEW_OK) try
{ {
rgssThreadError(threadData, "Error initializing glew"); initGLFunctions();
}
catch (const Exception &exc)
{
rgssThreadError(threadData, exc.msg);
SDL_GL_DeleteContext(glCtx); SDL_GL_DeleteContext(glCtx);
return 0; return 0;
} }
glClearColor(0, 0, 0, 1); gl.ClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT); gl.Clear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(win); SDL_GL_SwapWindow(win);
printGLInfo(); printGLInfo();
/* Check for required GL version */
if (!GLEW_VERSION_2_0)
{
rgssThreadError(threadData, "At least OpenGL 2.0 is required");
SDL_GL_DeleteContext(glCtx);
return 0;
}
/* Setup optional GL extensions */
if (!setupOptionalGLExtensions(threadData))
{
SDL_GL_DeleteContext(glCtx);
return 0;
}
SDL_GL_SetSwapInterval(threadData->config.vsync ? 1 : 0); SDL_GL_SetSwapInterval(threadData->config.vsync ? 1 : 0);
DebugLogger dLogger; DebugLogger dLogger;

View File

@ -1,8 +1,8 @@
#include "perftimer.h" #include "perftimer.h"
#include <SDL_timer.h> #include <SDL_timer.h>
#include <glew.h>
#include "gl-fun.h"
#include "debugwriter.h" #include "debugwriter.h"
struct TimerQuery struct TimerQuery
@ -14,7 +14,7 @@ struct TimerQuery
TimerQuery() TimerQuery()
: thisQueryActive(false) : thisQueryActive(false)
{ {
glGenQueries(1, &query); gl.GenQueries(1, &query);
} }
void begin() void begin()
@ -25,7 +25,7 @@ struct TimerQuery
if (thisQueryActive) if (thisQueryActive)
return; return;
glBeginQuery(GL_TIME_ELAPSED, query); gl.BeginQuery(GL_TIME_ELAPSED, query);
queryActive = true; queryActive = true;
thisQueryActive = true; thisQueryActive = true;
} }
@ -35,7 +35,7 @@ struct TimerQuery
if (!thisQueryActive) if (!thisQueryActive)
return; return;
glEndQuery(GL_TIME_ELAPSED); gl.EndQuery(GL_TIME_ELAPSED);
queryActive = false; queryActive = false;
thisQueryActive = false; thisQueryActive = false;
} }
@ -46,14 +46,14 @@ struct TimerQuery
return false; return false;
GLint isReady = GL_FALSE; GLint isReady = GL_FALSE;
glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &isReady); gl.GetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &isReady);
if (isReady != GL_TRUE) if (isReady != GL_TRUE)
return false; return false;
glGetQueryObjectui64v(query, GL_QUERY_RESULT, result); gl.GetQueryObjectui64v(query, GL_QUERY_RESULT, result);
if (glGetError() == GL_INVALID_OPERATION) if (gl.GetError() == GL_INVALID_OPERATION)
return false; return false;
return true; return true;
@ -68,9 +68,9 @@ struct TimerQuery
GLint isReady = GL_FALSE; GLint isReady = GL_FALSE;
while (isReady == GL_FALSE) while (isReady == GL_FALSE)
glGetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &isReady); gl.GetQueryObjectiv(query, GL_QUERY_RESULT_AVAILABLE, &isReady);
glGetQueryObjectui64v(query, GL_QUERY_RESULT, &result); gl.GetQueryObjectui64v(query, GL_QUERY_RESULT, &result);
return result; return result;
} }
@ -80,7 +80,7 @@ struct TimerQuery
if (thisQueryActive) if (thisQueryActive)
end(); end();
glDeleteQueries(1, &query); gl.DeleteQueries(1, &query);
} }
}; };
@ -193,7 +193,7 @@ PerfTimer *createCPUTimer(int iter)
PerfTimer *createGPUTimer(int iter) PerfTimer *createGPUTimer(int iter)
{ {
if (GLEW_ARB_timer_query || GLEW_EXT_timer_query) if (gl.timerQuery)
{ {
return new GPUTimerGLQuery(iter); return new GPUTimerGLQuery(iter);
} }

View File

@ -22,7 +22,6 @@
#ifndef QUAD_H #ifndef QUAD_H
#define QUAD_H #define QUAD_H
#include "glew.h"
#include "etc-internal.h" #include "etc-internal.h"
#include "gl-util.h" #include "gl-util.h"
#include "sharedstate.h" #include "sharedstate.h"
@ -114,13 +113,13 @@ struct Quad
VBO::allocEmpty(sizeof(Vertex[4]), GL_DYNAMIC_DRAW); VBO::allocEmpty(sizeof(Vertex[4]), GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(Shader::Color); gl.EnableVertexAttribArray(Shader::Color);
glEnableVertexAttribArray(Shader::Position); gl.EnableVertexAttribArray(Shader::Position);
glEnableVertexAttribArray(Shader::TexCoord); gl.EnableVertexAttribArray(Shader::TexCoord);
glVertexAttribPointer(Shader::Color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::colorOffset()); gl.VertexAttribPointer(Shader::Color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::colorOffset());
glVertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::posOffset()); gl.VertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::posOffset());
glVertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::texPosOffset()); gl.VertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::texPosOffset());
VAO::unbind(); VAO::unbind();
VBO::unbind(); VBO::unbind();
@ -177,7 +176,7 @@ struct Quad
} }
VAO::bind(vao); VAO::bind(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); gl.DrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
VAO::unbind(); VAO::unbind();
} }
}; };

View File

@ -37,22 +37,22 @@ typedef uint32_t index_t;
/* A small hack to get mutable QuadArray constructors */ /* A small hack to get mutable QuadArray constructors */
inline void initBufferBindings(Vertex *) inline void initBufferBindings(Vertex *)
{ {
glEnableVertexAttribArray(Shader::Color); gl.EnableVertexAttribArray(Shader::Color);
glEnableVertexAttribArray(Shader::Position); gl.EnableVertexAttribArray(Shader::Position);
glEnableVertexAttribArray(Shader::TexCoord); gl.EnableVertexAttribArray(Shader::TexCoord);
glVertexAttribPointer(Shader::Color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::colorOffset()); gl.VertexAttribPointer(Shader::Color, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::colorOffset());
glVertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::posOffset()); gl.VertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::posOffset());
glVertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::texPosOffset()); gl.VertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), Vertex::texPosOffset());
} }
inline void initBufferBindings(SVertex *) inline void initBufferBindings(SVertex *)
{ {
glEnableVertexAttribArray(Shader::Position); gl.EnableVertexAttribArray(Shader::Position);
glEnableVertexAttribArray(Shader::TexCoord); gl.EnableVertexAttribArray(Shader::TexCoord);
glVertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::posOffset()); gl.VertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::posOffset());
glVertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::texPosOffset()); gl.VertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::texPosOffset());
} }
template<class VertexType> template<class VertexType>
@ -135,7 +135,7 @@ struct QuadArray
VAO::bind(vao); VAO::bind(vao);
const char *_offset = (const char*) 0 + offset * 6 * sizeof(index_t); const char *_offset = (const char*) 0 + offset * 6 * sizeof(index_t);
glDrawElements(GL_TRIANGLES, count * 6, _GL_INDEX_TYPE, _offset); gl.DrawElements(GL_TRIANGLES, count * 6, _GL_INDEX_TYPE, _offset);
VAO::unbind(); VAO::unbind();
} }

View File

@ -24,8 +24,6 @@
#include "glstate.h" #include "glstate.h"
#include "exception.h" #include "exception.h"
#include <glew.h>
#include <assert.h> #include <assert.h>
#include <iostream> #include <iostream>
@ -57,15 +55,15 @@
#vert, #frag, #name); \ #vert, #frag, #name); \
} }
#define GET_U(name) u_##name = glGetUniformLocation(program, #name) #define GET_U(name) u_##name = gl.GetUniformLocation(program, #name)
static void printShaderLog(GLuint shader) static void printShaderLog(GLuint shader)
{ {
GLint logLength; GLint logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength); gl.GetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
std::string log(logLength, '\0'); std::string log(logLength, '\0');
glGetShaderInfoLog(shader, log.size(), 0, &log[0]); gl.GetShaderInfoLog(shader, log.size(), 0, &log[0]);
std::clog << "Shader log:\n" << log; std::clog << "Shader log:\n" << log;
} }
@ -73,28 +71,28 @@ static void printShaderLog(GLuint shader)
static void printProgramLog(GLuint program) static void printProgramLog(GLuint program)
{ {
GLint logLength; GLint logLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength); gl.GetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
std::string log(logLength, '\0'); std::string log(logLength, '\0');
glGetProgramInfoLog(program, log.size(), 0, &log[0]); gl.GetProgramInfoLog(program, log.size(), 0, &log[0]);
std::clog << "Program log:\n" << log; std::clog << "Program log:\n" << log;
} }
Shader::Shader() Shader::Shader()
{ {
vertShader = glCreateShader(GL_VERTEX_SHADER); vertShader = gl.CreateShader(GL_VERTEX_SHADER);
fragShader = glCreateShader(GL_FRAGMENT_SHADER); fragShader = gl.CreateShader(GL_FRAGMENT_SHADER);
program = glCreateProgram(); program = gl.CreateProgram();
} }
Shader::~Shader() Shader::~Shader()
{ {
glUseProgram(0); gl.UseProgram(0);
glDeleteProgram(program); gl.DeleteProgram(program);
glDeleteShader(vertShader); gl.DeleteShader(vertShader);
glDeleteShader(fragShader); gl.DeleteShader(fragShader);
} }
void Shader::bind() void Shader::bind()
@ -104,7 +102,7 @@ void Shader::bind()
void Shader::unbind() void Shader::unbind()
{ {
glActiveTexture(GL_TEXTURE0); gl.ActiveTexture(GL_TEXTURE0);
glState.program.set(0); glState.program.set(0);
} }
@ -116,10 +114,10 @@ void Shader::init(const unsigned char *vert, int vertSize,
GLint success; GLint success;
/* Compile vertex shader */ /* Compile vertex shader */
glShaderSource(vertShader, 1, (const GLchar**) &vert, (const GLint*) &vertSize); gl.ShaderSource(vertShader, 1, (const GLchar**) &vert, (const GLint*) &vertSize);
glCompileShader(vertShader); gl.CompileShader(vertShader);
glGetObjectParameterivARB(vertShader, GL_COMPILE_STATUS, &success); gl.GetObjectParameterivARB(vertShader, GL_COMPILE_STATUS, &success);
if (!success) if (!success)
{ {
@ -130,10 +128,10 @@ void Shader::init(const unsigned char *vert, int vertSize,
} }
/* Compile fragment shader */ /* Compile fragment shader */
glShaderSource(fragShader, 1, (const GLchar**) &frag, (const GLint*) &fragSize); gl.ShaderSource(fragShader, 1, (const GLchar**) &frag, (const GLint*) &fragSize);
glCompileShader(fragShader); gl.CompileShader(fragShader);
glGetObjectParameterivARB(fragShader, GL_COMPILE_STATUS, &success); gl.GetObjectParameterivARB(fragShader, GL_COMPILE_STATUS, &success);
if (!success) if (!success)
{ {
@ -144,16 +142,16 @@ void Shader::init(const unsigned char *vert, int vertSize,
} }
/* Link shader program */ /* Link shader program */
glAttachShader(program, vertShader); gl.AttachShader(program, vertShader);
glAttachShader(program, fragShader); gl.AttachShader(program, fragShader);
glBindAttribLocation(program, Position, "position"); gl.BindAttribLocation(program, Position, "position");
glBindAttribLocation(program, TexCoord, "texCoord"); gl.BindAttribLocation(program, TexCoord, "texCoord");
glBindAttribLocation(program, Color, "color"); gl.BindAttribLocation(program, Color, "color");
glLinkProgram(program); gl.LinkProgram(program);
glGetObjectParameterivARB(program, GL_LINK_STATUS, &success); gl.GetObjectParameterivARB(program, GL_LINK_STATUS, &success);
if (!success) if (!success)
{ {
@ -178,17 +176,17 @@ void Shader::initFromFile(const char *_vertFile, const char *_fragFile,
void Shader::setVec4Uniform(GLint location, const Vec4 &vec) void Shader::setVec4Uniform(GLint location, const Vec4 &vec)
{ {
glUniform4f(location, vec.x, vec.y, vec.z, vec.w); gl.Uniform4f(location, vec.x, vec.y, vec.z, vec.w);
} }
void Shader::setTexUniform(GLint location, unsigned unitIndex, TEX::ID texture) void Shader::setTexUniform(GLint location, unsigned unitIndex, TEX::ID texture)
{ {
GLenum texUnit = GL_TEXTURE0 + unitIndex; GLenum texUnit = GL_TEXTURE0 + unitIndex;
glActiveTexture(texUnit); gl.ActiveTexture(texUnit);
glBindTexture(GL_TEXTURE_2D, texture.gl); gl.BindTexture(GL_TEXTURE_2D, texture.gl);
glUniform1i(location, unitIndex); gl.Uniform1i(location, unitIndex);
glActiveTexture(GL_TEXTURE0); gl.ActiveTexture(GL_TEXTURE0);
} }
void ShaderBase::GLProjMat::apply(const Vec2i &value) void ShaderBase::GLProjMat::apply(const Vec2i &value)
@ -206,7 +204,7 @@ void ShaderBase::GLProjMat::apply(const Vec2i &value)
-1, -1, -1, 1 -1, -1, -1, 1
}; };
glUniformMatrix4fv(u_mat, 1, GL_FALSE, mat); gl.UniformMatrix4fv(u_mat, 1, GL_FALSE, mat);
} }
void ShaderBase::init() void ShaderBase::init()
@ -214,7 +212,7 @@ void ShaderBase::init()
GET_U(texSizeInv); GET_U(texSizeInv);
GET_U(translation); GET_U(translation);
projMat.u_mat = glGetUniformLocation(program, "projMat"); projMat.u_mat = gl.GetUniformLocation(program, "projMat");
} }
void ShaderBase::applyViewportProj() void ShaderBase::applyViewportProj()
@ -225,12 +223,12 @@ void ShaderBase::applyViewportProj()
void ShaderBase::setTexSize(const Vec2i &value) void ShaderBase::setTexSize(const Vec2i &value)
{ {
glUniform2f(u_texSizeInv, 1.f / value.x, 1.f / value.y); gl.Uniform2f(u_texSizeInv, 1.f / value.x, 1.f / value.y);
} }
void ShaderBase::setTranslation(const Vec2i &value) void ShaderBase::setTranslation(const Vec2i &value)
{ {
glUniform2f(u_translation, value.x, value.y); gl.Uniform2f(u_translation, value.x, value.y);
} }
@ -245,7 +243,7 @@ SimpleShader::SimpleShader()
void SimpleShader::setTexOffsetX(int value) void SimpleShader::setTexOffsetX(int value)
{ {
glUniform1f(u_texOffsetX, value); gl.Uniform1f(u_texOffsetX, value);
} }
@ -276,7 +274,7 @@ SimpleSpriteShader::SimpleSpriteShader()
void SimpleSpriteShader::setSpriteMat(const float value[16]) void SimpleSpriteShader::setSpriteMat(const float value[16])
{ {
glUniformMatrix4fv(u_spriteMat, 1, GL_FALSE, value); gl.UniformMatrix4fv(u_spriteMat, 1, GL_FALSE, value);
} }
@ -310,12 +308,12 @@ void TransShader::setTransMap(TEX::ID tex)
void TransShader::setProg(float value) void TransShader::setProg(float value)
{ {
glUniform1f(u_prog, value); gl.Uniform1f(u_prog, value);
} }
void TransShader::setVague(float value) void TransShader::setVague(float value)
{ {
glUniform1f(u_vague, value); gl.Uniform1f(u_vague, value);
} }
@ -342,7 +340,7 @@ void SimpleTransShader::setFrozenScene(TEX::ID tex)
void SimpleTransShader::setProg(float value) void SimpleTransShader::setProg(float value)
{ {
glUniform1f(u_prog, value); gl.Uniform1f(u_prog, value);
} }
@ -362,7 +360,7 @@ SpriteShader::SpriteShader()
void SpriteShader::setSpriteMat(const float value[16]) void SpriteShader::setSpriteMat(const float value[16])
{ {
glUniformMatrix4fv(u_spriteMat, 1, GL_FALSE, value); gl.UniformMatrix4fv(u_spriteMat, 1, GL_FALSE, value);
} }
void SpriteShader::setTone(const Vec4 &tone) void SpriteShader::setTone(const Vec4 &tone)
@ -377,17 +375,17 @@ void SpriteShader::setColor(const Vec4 &color)
void SpriteShader::setOpacity(float value) void SpriteShader::setOpacity(float value)
{ {
glUniform1f(u_opacity, value); gl.Uniform1f(u_opacity, value);
} }
void SpriteShader::setBushDepth(float value) void SpriteShader::setBushDepth(float value)
{ {
glUniform1f(u_bushDepth, value); gl.Uniform1f(u_bushDepth, value);
} }
void SpriteShader::setBushOpacity(float value) void SpriteShader::setBushOpacity(float value)
{ {
glUniform1f(u_bushOpacity, value); gl.Uniform1f(u_bushOpacity, value);
} }
@ -420,7 +418,7 @@ void PlaneShader::setFlash(const Vec4 &flash)
void PlaneShader::setOpacity(float value) void PlaneShader::setOpacity(float value)
{ {
glUniform1f(u_opacity, value); gl.Uniform1f(u_opacity, value);
} }
@ -435,7 +433,7 @@ FlashMapShader::FlashMapShader()
void FlashMapShader::setAlpha(float value) void FlashMapShader::setAlpha(float value)
{ {
glUniform1f(u_alpha, value); gl.Uniform1f(u_alpha, value);
} }
@ -451,7 +449,7 @@ HueShader::HueShader()
void HueShader::setHueAdjust(float value) void HueShader::setHueAdjust(float value)
{ {
glUniform1f(u_hueAdjust, value); gl.Uniform1f(u_hueAdjust, value);
} }
void HueShader::setInputTexture(TEX::ID tex) void HueShader::setInputTexture(TEX::ID tex)
@ -473,7 +471,7 @@ SimpleMatrixShader::SimpleMatrixShader()
void SimpleMatrixShader::setMatrix(const float value[16]) void SimpleMatrixShader::setMatrix(const float value[16])
{ {
glUniformMatrix4fv(u_matrix, 1, GL_FALSE, value); gl.UniformMatrix4fv(u_matrix, 1, GL_FALSE, value);
} }
@ -508,7 +506,7 @@ BltShader::BltShader()
void BltShader::setSource() void BltShader::setSource()
{ {
glUniform1i(u_source, 0); gl.Uniform1i(u_source, 0);
} }
void BltShader::setDestination(const TEX::ID value) void BltShader::setDestination(const TEX::ID value)
@ -518,10 +516,10 @@ void BltShader::setDestination(const TEX::ID value)
void BltShader::setSubRect(const FloatRect &value) void BltShader::setSubRect(const FloatRect &value)
{ {
glUniform4f(u_subRect, value.x, value.y, value.w, value.h); gl.Uniform4f(u_subRect, value.x, value.y, value.w, value.h);
} }
void BltShader::setOpacity(float value) void BltShader::setOpacity(float value)
{ {
glUniform1f(u_opacity, value); gl.Uniform1f(u_opacity, value);
} }

View File

@ -393,14 +393,14 @@ struct TilemapPrivate
tiles.vao = VAO::gen(); tiles.vao = VAO::gen();
VAO::bind(tiles.vao); VAO::bind(tiles.vao);
glEnableVertexAttribArray(Shader::Position); gl.EnableVertexAttribArray(Shader::Position);
glEnableVertexAttribArray(Shader::TexCoord); gl.EnableVertexAttribArray(Shader::TexCoord);
VBO::bind(tiles.vbo); VBO::bind(tiles.vbo);
shState->bindQuadIBO(); shState->bindQuadIBO();
glVertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::posOffset()); gl.VertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::posOffset());
glVertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::texPosOffset()); gl.VertexAttribPointer(Shader::TexCoord, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex), SVertex::texPosOffset());
VAO::unbind(); VAO::unbind();
VBO::unbind(); VBO::unbind();
@ -414,14 +414,14 @@ struct TilemapPrivate
VAO::bind(flash.vao); VAO::bind(flash.vao);
glEnableVertexAttribArray(Shader::Color); gl.EnableVertexAttribArray(Shader::Color);
glEnableVertexAttribArray(Shader::Position); gl.EnableVertexAttribArray(Shader::Position);
VBO::bind(flash.vbo); VBO::bind(flash.vbo);
shState->bindQuadIBO(); shState->bindQuadIBO();
glVertexAttribPointer(Shader::Color, 4, GL_FLOAT, GL_FALSE, sizeof(CVertex), CVertex::colorOffset()); gl.VertexAttribPointer(Shader::Color, 4, GL_FLOAT, GL_FALSE, sizeof(CVertex), CVertex::colorOffset());
glVertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(CVertex), CVertex::posOffset()); gl.VertexAttribPointer(Shader::Position, 2, GL_FLOAT, GL_FALSE, sizeof(CVertex), CVertex::posOffset());
VAO::unbind(); VAO::unbind();
VBO::unbind(); VBO::unbind();
@ -1162,13 +1162,13 @@ void GroundLayer::draw()
void GroundLayer::drawInt() void GroundLayer::drawInt()
{ {
glDrawElements(GL_TRIANGLES, vboCount, gl.DrawElements(GL_TRIANGLES, vboCount,
GL_UNSIGNED_INT, (GLvoid*) (p->tiles.frameIdx * p->tiles.bufferFrameSize)); GL_UNSIGNED_INT, (GLvoid*) (p->tiles.frameIdx * p->tiles.bufferFrameSize));
} }
void GroundLayer::drawFlashInt() void GroundLayer::drawFlashInt()
{ {
glDrawElements(GL_TRIANGLES, p->flash.quadCount * 6, GL_UNSIGNED_INT, 0); gl.DrawElements(GL_TRIANGLES, p->flash.quadCount * 6, GL_UNSIGNED_INT, 0);
} }
void GroundLayer::onGeometryChange(const Scene::Geometry &geo) void GroundLayer::onGeometryChange(const Scene::Geometry &geo)
@ -1220,8 +1220,8 @@ void ScanRow::draw()
void ScanRow::drawInt() void ScanRow::drawInt()
{ {
glDrawElements(GL_TRIANGLES, vboBatchCount, gl.DrawElements(GL_TRIANGLES, vboBatchCount,
GL_UNSIGNED_INT, (GLvoid*) (vboOffset + p->tiles.frameIdx * p->tiles.bufferFrameSize)); GL_UNSIGNED_INT, (GLvoid*) (vboOffset + p->tiles.frameIdx * p->tiles.bufferFrameSize));
} }
void ScanRow::initUpdateZ() void ScanRow::initUpdateZ()