diff --git a/include/mbgl/platform/default/headless_view.hpp b/include/mbgl/platform/default/headless_view.hpp index c0ffd27fb..baa6a0382 100644 --- a/include/mbgl/platform/default/headless_view.hpp +++ b/include/mbgl/platform/default/headless_view.hpp @@ -22,6 +22,7 @@ typedef XID GLXPbuffer; #include #include +#include #include #include @@ -89,9 +90,9 @@ private: std::function mapChangeCallback; - GLuint fbo = 0; - GLuint fboDepthStencil = 0; - GLuint fboColor = 0; + gl::FramebufferID fbo = 0; + gl::RenderbufferID fboDepthStencil = 0; + gl::RenderbufferID fboColor = 0; }; } // namespace mbgl diff --git a/src/mbgl/geometry/buffer.hpp b/src/mbgl/geometry/buffer.hpp index 8e58af892..2faa9a9d8 100644 --- a/src/mbgl/geometry/buffer.hpp +++ b/src/mbgl/geometry/buffer.hpp @@ -70,7 +70,7 @@ public: } } - GLuint getID() const { + gl::BufferID getID() const { return buffer ? *buffer : 0; } diff --git a/src/mbgl/geometry/vao.cpp b/src/mbgl/geometry/vao.cpp index 0f29d65cf..d7bddcac7 100644 --- a/src/mbgl/geometry/vao.cpp +++ b/src/mbgl/geometry/vao.cpp @@ -28,8 +28,10 @@ void VertexArrayObject::bindVertexArrayObject(gl::Context& context) { context.vertexArrayObject = *vertexArray; } -void VertexArrayObject::verifyBinding(Shader& shader, GLuint vertexBuffer, GLuint elementsBuffer, - GLbyte *offset) { +void VertexArrayObject::verifyBinding(Shader& shader, + gl::BufferID vertexBuffer, + gl::BufferID elementsBuffer, + GLbyte* offset) { if (bound_shader != shader.getID()) { throw std::runtime_error(std::string("trying to rebind VAO to another shader from " + util::toString(bound_shader) + "(" + bound_shader_name + ") to " + @@ -43,8 +45,10 @@ void VertexArrayObject::verifyBinding(Shader& shader, GLuint vertexBuffer, GLuin } } -void VertexArrayObject::storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, - GLbyte *offset) { +void VertexArrayObject::storeBinding(Shader& shader, + gl::BufferID vertexBuffer, + gl::BufferID elementsBuffer, + GLbyte* offset) { bound_shader = shader.getID(); bound_shader_name = shader.name; bound_offset = offset; diff --git a/src/mbgl/geometry/vao.hpp b/src/mbgl/geometry/vao.hpp index a9e69c938..42527a7ad 100644 --- a/src/mbgl/geometry/vao.hpp +++ b/src/mbgl/geometry/vao.hpp @@ -51,23 +51,29 @@ public: } } - GLuint getID() const { + gl::VertexArrayID getID() const { return *vertexArray; } private: void bindVertexArrayObject(gl::Context&); - void storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset); - void verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset); + void storeBinding(Shader& shader, + gl::BufferID vertexBuffer, + gl::BufferID elementsBuffer, + GLbyte* offset); + void verifyBinding(Shader& shader, + gl::BufferID vertexBuffer, + gl::BufferID elementsBuffer, + GLbyte* offset); mbgl::optional vertexArray; // For debug reasons, we're storing the bind information so that we can // detect errors and report - GLuint bound_shader = 0; + gl::ProgramID bound_shader = 0; const char* bound_shader_name = ""; - GLuint bound_vertex_buffer = 0; - GLuint bound_elements_buffer = 0; + gl::BufferID bound_vertex_buffer = 0; + gl::BufferID bound_elements_buffer = 0; GLbyte *bound_offset = nullptr; }; diff --git a/src/mbgl/gl/context.cpp b/src/mbgl/gl/context.cpp index e70affd0b..0d6716487 100644 --- a/src/mbgl/gl/context.cpp +++ b/src/mbgl/gl/context.cpp @@ -3,6 +3,14 @@ namespace mbgl { namespace gl { +static_assert(std::is_same::value, "OpenGL type mismatch"); +static_assert(std::is_same::value, "OpenGL type mismatch"); +static_assert(std::is_same::value, "OpenGL type mismatch"); +static_assert(std::is_same::value, "OpenGL type mismatch"); +static_assert(std::is_same::value, "OpenGL type mismatch"); +static_assert(std::is_same::value, "OpenGL type mismatch"); +static_assert(std::is_same::value, "OpenGL type mismatch"); + Context::~Context() { reset(); } @@ -60,7 +68,7 @@ void Context::setDirtyState() { } void Context::performCleanup() { - for (GLuint id : abandonedPrograms) { + for (auto id : abandonedPrograms) { if (program == id) { program.setDirty(); } @@ -68,7 +76,7 @@ void Context::performCleanup() { } abandonedPrograms.clear(); - for (GLuint id : abandonedShaders) { + for (auto id : abandonedShaders) { MBGL_CHECK_ERROR(glDeleteShader(id)); } abandonedShaders.clear(); diff --git a/src/mbgl/gl/context.hpp b/src/mbgl/gl/context.hpp index cd42e02fc..133a56b6f 100644 --- a/src/mbgl/gl/context.hpp +++ b/src/mbgl/gl/context.hpp @@ -21,12 +21,16 @@ public: return UniqueProgram { MBGL_CHECK_ERROR(glCreateProgram()), { this } }; } - UniqueShader createShader(GLenum type) { - return UniqueShader { MBGL_CHECK_ERROR(glCreateShader(type)), { this } }; + UniqueShader createVertexShader() { + return UniqueShader { MBGL_CHECK_ERROR(glCreateShader(GL_VERTEX_SHADER)), { this } }; + } + + UniqueShader createFragmentShader() { + return UniqueShader { MBGL_CHECK_ERROR(glCreateShader(GL_FRAGMENT_SHADER)), { this } }; } UniqueBuffer createBuffer() { - GLuint id = 0; + BufferID id = 0; MBGL_CHECK_ERROR(glGenBuffers(1, &id)); return UniqueBuffer { std::move(id), { this } }; } @@ -37,19 +41,19 @@ public: MBGL_CHECK_ERROR(glGenTextures(TextureMax, pooledTextures.data())); } - GLuint id = pooledTextures.back(); + TextureID id = pooledTextures.back(); pooledTextures.pop_back(); return UniqueTexture { std::move(id), { this } }; } UniqueVertexArray createVertexArray() { - GLuint id = 0; + VertexArrayID id = 0; MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id)); return UniqueVertexArray { std::move(id), { this } }; } UniqueFramebuffer createFramebuffer() { - GLuint id = 0; + FramebufferID id = 0; MBGL_CHECK_ERROR(glGenFramebuffers(1, &id)); return UniqueFramebuffer { std::move(id), { this } }; } @@ -113,14 +117,14 @@ private: friend detail::VertexArrayDeleter; friend detail::FramebufferDeleter; - std::vector pooledTextures; + std::vector pooledTextures; - std::vector abandonedPrograms; - std::vector abandonedShaders; - std::vector abandonedBuffers; - std::vector abandonedTextures; - std::vector abandonedVertexArrays; - std::vector abandonedFramebuffers; + std::vector abandonedPrograms; + std::vector abandonedShaders; + std::vector abandonedBuffers; + std::vector abandonedTextures; + std::vector abandonedVertexArrays; + std::vector abandonedFramebuffers; }; } // namespace gl diff --git a/src/mbgl/gl/object.cpp b/src/mbgl/gl/object.cpp index 90451da71..aee87f883 100644 --- a/src/mbgl/gl/object.cpp +++ b/src/mbgl/gl/object.cpp @@ -7,22 +7,22 @@ namespace mbgl { namespace gl { namespace detail { -void ProgramDeleter::operator()(GLuint id) const { +void ProgramDeleter::operator()(ProgramID id) const { assert(context); context->abandonedPrograms.push_back(id); } -void ShaderDeleter::operator()(GLuint id) const { +void ShaderDeleter::operator()(ShaderID id) const { assert(context); context->abandonedShaders.push_back(id); } -void BufferDeleter::operator()(GLuint id) const { +void BufferDeleter::operator()(BufferID id) const { assert(context); context->abandonedBuffers.push_back(id); } -void TextureDeleter::operator()(GLuint id) const { +void TextureDeleter::operator()(TextureID id) const { assert(context); if (context->pooledTextures.size() >= TextureMax) { context->abandonedTextures.push_back(id); @@ -31,12 +31,12 @@ void TextureDeleter::operator()(GLuint id) const { } } -void VertexArrayDeleter::operator()(GLuint id) const { +void VertexArrayDeleter::operator()(VertexArrayID id) const { assert(context); context->abandonedVertexArrays.push_back(id); } -void FramebufferDeleter::operator()(GLuint id) const { +void FramebufferDeleter::operator()(FramebufferID id) const { assert(context); context->abandonedFramebuffers.push_back(id); } diff --git a/src/mbgl/gl/object.hpp b/src/mbgl/gl/object.hpp index 785e73e8a..1cc1f04a9 100644 --- a/src/mbgl/gl/object.hpp +++ b/src/mbgl/gl/object.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include @@ -13,42 +13,42 @@ namespace detail { struct ProgramDeleter { Context* context; - void operator()(GLuint) const; + void operator()(ProgramID) const; }; struct ShaderDeleter { Context* context; - void operator()(GLuint) const; + void operator()(ShaderID) const; }; struct BufferDeleter { Context* context; - void operator()(GLuint) const; + void operator()(BufferID) const; }; struct TextureDeleter { Context* context; - void operator()(GLuint) const; + void operator()(TextureID) const; }; struct VertexArrayDeleter { Context* context; - void operator()(GLuint) const; + void operator()(VertexArrayID) const; }; struct FramebufferDeleter { Context* context; - void operator()(GLuint) const; + void operator()(FramebufferID) const; }; } // namespace detail -using UniqueProgram = std_experimental::unique_resource; -using UniqueShader = std_experimental::unique_resource; -using UniqueBuffer = std_experimental::unique_resource; -using UniqueTexture = std_experimental::unique_resource; -using UniqueVertexArray = std_experimental::unique_resource; -using UniqueFramebuffer = std_experimental::unique_resource; +using UniqueProgram = std_experimental::unique_resource; +using UniqueShader = std_experimental::unique_resource; +using UniqueBuffer = std_experimental::unique_resource; +using UniqueTexture = std_experimental::unique_resource; +using UniqueVertexArray = std_experimental::unique_resource; +using UniqueFramebuffer = std_experimental::unique_resource; } // namespace gl } // namespace mbgl diff --git a/src/mbgl/gl/types.hpp b/src/mbgl/gl/types.hpp new file mode 100644 index 000000000..94426f5e3 --- /dev/null +++ b/src/mbgl/gl/types.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +namespace mbgl { +namespace gl { + +using ProgramID = uint32_t; +using ShaderID = uint32_t; +using BufferID = uint32_t; +using TextureID = uint32_t; +using VertexArrayID = uint32_t; +using FramebufferID = uint32_t; +using RenderbufferID = uint32_t; + +} // namespace gl +} // namespace mbgl diff --git a/src/mbgl/gl/value.hpp b/src/mbgl/gl/value.hpp index bfa4dce77..1250945ba 100644 --- a/src/mbgl/gl/value.hpp +++ b/src/mbgl/gl/value.hpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace mbgl { @@ -234,7 +235,7 @@ struct BlendColor { }; struct Program { - using Type = GLuint; + using Type = gl::ProgramID; static const constexpr Type Default = 0; static void Set(const Type& value) { MBGL_CHECK_ERROR(glUseProgram(value)); @@ -333,7 +334,7 @@ struct RasterPos { #endif // GL_ES_VERSION_2_0 struct BindTexture { - using Type = GLuint; + using Type = gl::TextureID; static const constexpr Type Default = 0; static void Set(const Type& value) { MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, value)); @@ -341,7 +342,7 @@ struct BindTexture { static Type Get() { GLint texture; MBGL_CHECK_ERROR(glGetIntegerv(GL_TEXTURE_BINDING_2D, &texture)); - return texture; + return static_cast(texture); } }; @@ -349,7 +350,7 @@ template struct BindBuffer { static_assert(target == GL_ARRAY_BUFFER || target == GL_ELEMENT_ARRAY_BUFFER, "target must be one of GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER"); - using Type = GLuint; + using Type = gl::BufferID; static const constexpr Type Default = 0; static void Set(const Type& value) { MBGL_CHECK_ERROR(glBindBuffer(target, value)); @@ -359,7 +360,7 @@ struct BindBuffer { MBGL_CHECK_ERROR(glGetIntegerv(target == GL_ARRAY_BUFFER ? GL_ARRAY_BUFFER_BINDING : GL_ELEMENT_ARRAY_BUFFER_BINDING, &binding)); - return binding; + return static_cast(binding); } }; @@ -367,7 +368,7 @@ template const typename BindBuffer::Type BindBuffer::Default; struct BindVertexArray { - using Type = GLuint; + using Type = gl::VertexArrayID; static const constexpr Type Default = 0; static void Set(const Type& value) { if (gl::BindVertexArray) { diff --git a/src/mbgl/shader/shader.cpp b/src/mbgl/shader/shader.cpp index 46b9b0a63..b41114711 100644 --- a/src/mbgl/shader/shader.cpp +++ b/src/mbgl/shader/shader.cpp @@ -23,8 +23,8 @@ Shader::Shader(const char* name_, Defines defines) : name(name_), program(context.createProgram()), - vertexShader(context.createShader(GL_VERTEX_SHADER)), - fragmentShader(context.createShader(GL_FRAGMENT_SHADER)) { + vertexShader(context.createVertexShader()), + fragmentShader(context.createFragmentShader()) { util::stopwatch stopwatch("shader compilation", Event::Shader); if (!compileShader(vertexShader, vertexSource)) { diff --git a/src/mbgl/shader/shader.hpp b/src/mbgl/shader/shader.hpp index b04c88548..b122bda28 100644 --- a/src/mbgl/shader/shader.hpp +++ b/src/mbgl/shader/shader.hpp @@ -16,7 +16,7 @@ public: ~Shader(); const char* name; - GLuint getID() const { + gl::ProgramID getID() const { return program.get(); } diff --git a/src/mbgl/util/raster.cpp b/src/mbgl/util/raster.cpp index 6ed8c62b6..c3d5e9df2 100644 --- a/src/mbgl/util/raster.cpp +++ b/src/mbgl/util/raster.cpp @@ -14,7 +14,7 @@ bool Raster::isLoaded() const { return loaded; } -GLuint Raster::getID() const { +gl::TextureID Raster::getID() const { return texture ? *texture : 0; } diff --git a/src/mbgl/util/raster.hpp b/src/mbgl/util/raster.hpp index 9f496484e..c9b19914a 100644 --- a/src/mbgl/util/raster.hpp +++ b/src/mbgl/util/raster.hpp @@ -34,7 +34,7 @@ public: bool isLoaded() const; // returns the OpenGL texture ID - GLuint getID() const; + gl::TextureID getID() const; // size of uploaded image. std::array getSize() const; diff --git a/test/gl/object.test.cpp b/test/gl/object.test.cpp index ba3580080..0502bbc84 100644 --- a/test/gl/object.test.cpp +++ b/test/gl/object.test.cpp @@ -80,7 +80,7 @@ TEST(GLObject, Store) { context.performCleanup(); EXPECT_TRUE(context.empty()); - mbgl::gl::UniqueShader shader = context.createShader(GL_VERTEX_SHADER); + mbgl::gl::UniqueShader shader = context.createVertexShader(); EXPECT_NE(shader.get(), 0u); shader.reset(); EXPECT_FALSE(context.empty());