Merge pull request #564 from mapbox/load-extensions

Load extensions with *GetProcAddress
This commit is contained in:
Konstantin Käfer
2014-11-07 20:11:45 +01:00
10 changed files with 132 additions and 50 deletions

View File

@@ -9,7 +9,6 @@
namespace mbgl {
#if GL_ARB_vertex_array_object
class VertexArrayObject : public util::noncopyable {
public:
inline VertexArrayObject() {};
@@ -28,7 +27,9 @@ public:
if (bound_shader == 0) {
vertexBuffer.bind();
shader.bind(offset);
storeBinding(shader, vertexBuffer.getID(), 0, offset);
if (vao) {
storeBinding(shader, vertexBuffer.getID(), 0, offset);
}
} else {
verifyBinding(shader, vertexBuffer.getID(), 0, offset);
}
@@ -41,7 +42,9 @@ public:
vertexBuffer.bind();
elementsBuffer.bind();
shader.bind(offset);
storeBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
if (vao) {
storeBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
}
} else {
verifyBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
}
@@ -65,21 +68,6 @@ private:
char *bound_offset = 0;
};
#else
class VertexArrayObject : public util::noncopyable {
public:
template <typename Shader, typename Buffers>
void bind(Shader& shader, Buffers& buffers, char *offset) {
for (auto &buffer : buffers) {
buffer.bind();
}
shader.bind(offset);
}
};
#endif
}
#endif

View File

@@ -25,6 +25,7 @@ public:
~HeadlessView();
void createContext();
void loadExtensions();
void resize(uint16_t width, uint16_t height, float pixelRatio);
std::unique_ptr<uint32_t[]> readPixels();

View File

@@ -32,6 +32,7 @@ enum class Event : uint8_t {
Database,
HttpRequest,
Sprite,
OpenGL,
};
MBGL_DEFINE_ENUM_CLASS(EventClass, Event, {
@@ -44,6 +45,7 @@ MBGL_DEFINE_ENUM_CLASS(EventClass, Event, {
{ Event::Database, "Database" },
{ Event::HttpRequest, "HttpRequest" },
{ Event::Sprite, "Sprite" },
{ Event::OpenGL, "OpenGL" },
{ Event(-1), "Unknown" },
});

View File

@@ -3,38 +3,28 @@
#include <string>
#ifdef NVIDIA
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
extern PFNGLDISCARDFRAMEBUFFEREXTPROC glDiscardFramebufferEXT;
#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#elif __APPLE__
#if __APPLE__
#include "TargetConditionals.h"
#if TARGET_OS_IPHONE
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#define glGenVertexArrays glGenVertexArraysOES
#define glBindVertexArray glBindVertexArrayOES
#define glDeleteVertexArrays glDeleteVertexArraysOES
#define GL_ARB_vertex_array_object 1
#define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
// #define glGenVertexArrays glGenVertexArraysOES
// #define glBindVertexArray glBindVertexArrayOES
// #define glDeleteVertexArrays glDeleteVertexArraysOES
// #define GL_ARB_vertex_array_object 1
// #define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES
#elif TARGET_IPHONE_SIMULATOR
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#elif TARGET_OS_MAC
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#if GL_APPLE_vertex_array_object
#define GL_ARB_vertex_array_object 1
#define glGenVertexArrays glGenVertexArraysAPPLE
#define glBindVertexArray glBindVertexArrayAPPLE
#define glDeleteVertexArrays glDeleteVertexArraysAPPLE
#endif
#else
#error Unsupported Apple platform
#endif
#elif MBGL_USE_GLES2
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
@@ -43,8 +33,19 @@
namespace mbgl {
namespace gl {
typedef void (* PFNGLBINDVERTEXARRAYPROC) (GLuint array);
typedef void (* PFNGLDELETEVERTEXARRAYSPROC) (GLsizei n, const GLuint* arrays);
typedef void (* PFNGLGENVERTEXARRAYSPROC) (GLsizei n, GLuint* arrays);
typedef GLboolean (* PFNGLISVERTEXARRAYPROC) (GLuint array);
extern PFNGLBINDVERTEXARRAYPROC BindVertexArray;
extern PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays;
extern PFNGLGENVERTEXARRAYSPROC GenVertexArrays;
extern PFNGLISVERTEXARRAYPROC IsVertexArray;
// Debug group markers, useful for debuggin on iOS
#if defined(__APPLE__) && defined(DEBUG) && defined(GL_EXT_debug_marker)
#if __APPLE__ && defined(DEBUG) && defined(GL_EXT_debug_marker)
// static int indent = 0;
inline void start_group(const std::string &str) {
glPushGroupMarkerEXT(0, str.c_str());

View File

@@ -1,4 +1,5 @@
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/util/string.hpp>
@@ -26,6 +27,10 @@ void GLFWView::initialize(mbgl::Map *map_) {
monitor = glfwGetPrimaryMonitor();
}
#ifdef DEBUG
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
#endif
#ifdef GL_ES_VERSION_2_0
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
@@ -63,6 +68,25 @@ void GLFWView::initialize(mbgl::Map *map_) {
glfwSetScrollCallback(window, scroll);
glfwSetKeyCallback(window, key);
const std::string extensions = (char *)glGetString(GL_EXTENSIONS);
{
using namespace mbgl;
if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) {
gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glfwGetProcAddress("glBindVertexArrayARB");
gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glfwGetProcAddress("glDeleteVertexArraysARB");
gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glfwGetProcAddress("glGenVertexArraysARB");
gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glfwGetProcAddress("glIsVertexArrayARB");
} else if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) {
gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glfwGetProcAddress("glBindVertexArrayAPPLE");
gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glfwGetProcAddress("glDeleteVertexArraysAPPLE");
gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glfwGetProcAddress("glGenVertexArraysAPPLE");
gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glfwGetProcAddress("glIsVertexArrayAPPLE");
}
}
glfwMakeContextCurrent(nullptr);
}

View File

@@ -11,16 +11,62 @@ static PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = nullptr;
#endif
#endif
#ifdef MBGL_USE_CGL
#include <CoreFoundation/CoreFoundation.h>
typedef void (* CGLProc)(void);
CGLProc CGLGetProcAddress(const char *proc) {
static CFBundleRef framework = nullptr;
if (!framework) {
framework = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.opengl"));
if (!framework) {
throw std::runtime_error("Failed to load OpenGL.framework");
}
}
CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, proc, kCFStringEncodingASCII);
CGLProc symbol = (CGLProc)CFBundleGetFunctionPointerForName(framework, name);
CFRelease(name);
return symbol;
}
#endif
namespace mbgl {
HeadlessView::HeadlessView()
: display_(std::make_shared<HeadlessDisplay>()) {
createContext();
loadExtensions();
}
HeadlessView::HeadlessView(std::shared_ptr<HeadlessDisplay> display)
: display_(display) {
createContext();
loadExtensions();
}
void HeadlessView::loadExtensions() {
make_active();
const std::string extensions = (char *)glGetString(GL_EXTENSIONS);
#ifdef MBGL_USE_CGL
if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) {
gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)CGLGetProcAddress("glBindVertexArrayAPPLE");
gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)CGLGetProcAddress("glDeleteVertexArraysAPPLE");
gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)CGLGetProcAddress("glGenVertexArraysAPPLE");
gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)CGLGetProcAddress("glIsVertexArrayAPPLE");
}
#endif
#ifdef MBGL_USE_GLX
if (extensions.find("GL_ARB_vertex_array_object") != std::string::npos) {
gl::BindVertexArray = (gl::PFNGLBINDVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glBindVertexArrayARB");
gl::DeleteVertexArrays = (gl::PFNGLDELETEVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glDeleteVertexArraysARB");
gl::GenVertexArrays = (gl::PFNGLGENVERTEXARRAYSPROC)glXGetProcAddress((const GLubyte *)"glGenVertexArraysARB");
gl::IsVertexArray = (gl::PFNGLISVERTEXARRAYPROC)glXGetProcAddress((const GLubyte *)"glIsVertexArrayARB");
}
#endif
make_inactive();
}

View File

@@ -1,20 +1,30 @@
#include <mbgl/geometry/vao.hpp>
#include <mbgl/platform/log.hpp>
namespace mbgl {
#if GL_ARB_vertex_array_object
VertexArrayObject::~VertexArrayObject() {
if (!gl::DeleteVertexArrays) return;
if (vao) {
glDeleteVertexArrays(1, &vao);
gl::DeleteVertexArrays(1, &vao);
}
}
void VertexArrayObject::bindVertexArrayObject() {
if (!vao) {
glGenVertexArrays(1, &vao);
if (!gl::GenVertexArrays || !gl::BindVertexArray) {
static bool reported = false;
if (!reported) {
Log::Warning(Event::OpenGL, "Not using Vertex Array Objects");
reported = true;
}
return;
}
glBindVertexArray(vao);
if (!vao) {
gl::GenVertexArrays(1, &vao);
}
gl::BindVertexArray(vao);
}
void VertexArrayObject::verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer,
@@ -41,6 +51,4 @@ void VertexArrayObject::storeBinding(Shader &shader, GLuint vertexBuffer, GLuint
bound_elements_buffer = elementsBuffer;
}
#endif
}

View File

@@ -2,9 +2,17 @@
#include <iostream>
#ifdef NVIDIA
PFNGLDISCARDFRAMEBUFFEREXTPROC glDiscardFramebufferEXT;
#endif
namespace mbgl {
namespace gl {
PFNGLBINDVERTEXARRAYPROC BindVertexArray = nullptr;
PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays = nullptr;
PFNGLGENVERTEXARRAYSPROC GenVertexArrays = nullptr;
PFNGLISVERTEXARRAYPROC IsVertexArray = nullptr;
}
}
void _CHECK_GL_ERROR(const char *cmd, const char *file, int line) {
std::cout << cmd << ";" << std::endl;

View File

@@ -49,7 +49,11 @@ void PrerenderedTexture::bindFramebuffer() {
// Create depth/stencil buffer
glGenRenderbuffers(1, &fbo_depth_stencil);
glBindRenderbuffer(GL_RENDERBUFFER, fbo_depth_stencil);
#ifdef GL_ES_VERSION_2_0
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, properties.size, properties.size);
#else
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, properties.size, properties.size);
#endif
glBindRenderbuffer(GL_RENDERBUFFER, 0);
}