From 582d616deb5b1febbaa1c8dfb5e3fb405f8cad53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Fri, 7 Nov 2014 18:17:24 +0100 Subject: load VAO extension --- include/mbgl/geometry/vao.hpp | 24 ++++---------- include/mbgl/platform/default/headless_view.hpp | 1 + include/mbgl/platform/event.hpp | 2 ++ include/mbgl/platform/gl.hpp | 42 ++++++++++++------------- ios/mapbox-gl-cocoa | 2 +- platform/default/glfw_view.cpp | 18 +++++++++++ platform/default/headless_view.cpp | 38 ++++++++++++++++++++++ src/geometry/vao.cpp | 22 ++++++++----- src/platform/gl.cpp | 14 +++++++-- src/renderer/prerendered_texture.cpp | 4 +++ 10 files changed, 117 insertions(+), 50 deletions(-) diff --git a/include/mbgl/geometry/vao.hpp b/include/mbgl/geometry/vao.hpp index bb1f571ff..2ecba731f 100644 --- a/include/mbgl/geometry/vao.hpp +++ b/include/mbgl/geometry/vao.hpp @@ -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 - void bind(Shader& shader, Buffers& buffers, char *offset) { - for (auto &buffer : buffers) { - buffer.bind(); - } - shader.bind(offset); - } -}; - -#endif - } #endif diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp index f14033834..f599dde27 100644 --- a/include/mbgl/platform/default/headless_view.hpp +++ b/include/mbgl/platform/default/headless_view.hpp @@ -25,6 +25,7 @@ public: ~HeadlessView(); void createContext(); + void loadExtensions(); void resize(uint16_t width, uint16_t height, float pixelRatio); std::unique_ptr readPixels(); diff --git a/include/mbgl/platform/event.hpp b/include/mbgl/platform/event.hpp index 2aa4aa0c9..df2d8b03e 100644 --- a/include/mbgl/platform/event.hpp +++ b/include/mbgl/platform/event.hpp @@ -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" }, }); diff --git a/include/mbgl/platform/gl.hpp b/include/mbgl/platform/gl.hpp index d5162f2c7..73753746a 100644 --- a/include/mbgl/platform/gl.hpp +++ b/include/mbgl/platform/gl.hpp @@ -3,48 +3,48 @@ #include -#ifdef NVIDIA - #include - #include - - extern PFNGLDISCARDFRAMEBUFFEREXTPROC glDiscardFramebufferEXT; - - #define GL_DEPTH24_STENCIL8 GL_DEPTH24_STENCIL8_OES -#elif __APPLE__ +#if __APPLE__ #include "TargetConditionals.h" #if TARGET_OS_IPHONE #include #include - #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 #include #elif TARGET_OS_MAC #include #include - #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 + #include #else - #define GL_GLEXT_PROTOTYPES #include #include #endif 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()); diff --git a/ios/mapbox-gl-cocoa b/ios/mapbox-gl-cocoa index eb8f0b81b..f801f5b9f 160000 --- a/ios/mapbox-gl-cocoa +++ b/ios/mapbox-gl-cocoa @@ -1 +1 @@ -Subproject commit eb8f0b81bdec68be0e0cc7e511e62eb8061829f3 +Subproject commit f801f5b9fa9ca0e55c379f95295bc13338b19110 diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index baacfbfd9..7f8ca6994 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -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,19 @@ 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_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); } diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp index 71096beba..8ce93030d 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -11,16 +11,54 @@ static PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = nullptr; #endif #endif +#ifdef MBGL_USE_CGL +#include + +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()) { createContext(); + loadExtensions(); } HeadlessView::HeadlessView(std::shared_ptr 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 + make_inactive(); } diff --git a/src/geometry/vao.cpp b/src/geometry/vao.cpp index 4c459c436..d2fd2727e 100644 --- a/src/geometry/vao.cpp +++ b/src/geometry/vao.cpp @@ -1,20 +1,30 @@ #include +#include 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 (!gl::GenVertexArrays || !gl::BindVertexArray) { + static bool reported = false; + if (!reported) { + Log::Warning(Event::OpenGL, "Not using Vertex Array Objects"); + reported = true; + } + return; + } + if (!vao) { - glGenVertexArrays(1, &vao); + gl::GenVertexArrays(1, &vao); } - glBindVertexArray(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 - } diff --git a/src/platform/gl.cpp b/src/platform/gl.cpp index a1bf0ffc6..3133f128b 100644 --- a/src/platform/gl.cpp +++ b/src/platform/gl.cpp @@ -2,9 +2,17 @@ #include -#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; diff --git a/src/renderer/prerendered_texture.cpp b/src/renderer/prerendered_texture.cpp index adb7a5910..c9507d602 100644 --- a/src/renderer/prerendered_texture.cpp +++ b/src/renderer/prerendered_texture.cpp @@ -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); } -- cgit v1.2.3 From 8fe8b2f739e997b76fcc768348c66f2007e0968b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Fri, 7 Nov 2014 09:38:07 -0800 Subject: add glx support --- include/mbgl/platform/gl.hpp | 1 + platform/default/glfw_view.cpp | 8 +++++++- platform/default/headless_view.cpp | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/mbgl/platform/gl.hpp b/include/mbgl/platform/gl.hpp index 73753746a..6685d1f37 100644 --- a/include/mbgl/platform/gl.hpp +++ b/include/mbgl/platform/gl.hpp @@ -26,6 +26,7 @@ #include #include #else + #define GL_GLEXT_PROTOTYPES #include #include #endif diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 7f8ca6994..34b9ddbef 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -73,7 +73,13 @@ void GLFWView::initialize(mbgl::Map *map_) { { using namespace mbgl; - if (extensions.find("GL_APPLE_vertex_array_object") != std::string::npos) { + + 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"); diff --git a/platform/default/headless_view.cpp b/platform/default/headless_view.cpp index 8ce93030d..687f3ecf7 100644 --- a/platform/default/headless_view.cpp +++ b/platform/default/headless_view.cpp @@ -57,6 +57,14 @@ void HeadlessView::loadExtensions() { 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(); } -- cgit v1.2.3