Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] s/operator bool/created()/ in {GL,TexturePool}Holder
Browse files Browse the repository at this point in the history
Prevents confusing usage of GL holder objects.
  • Loading branch information
brunoabinader committed May 26, 2016
1 parent b2b2797 commit c111250
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/mbgl/geometry/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Buffer : private util::noncopyable {

// Transfers this buffer to the GPU and binds the buffer to the GL context.
void bind(gl::GLObjectStore& glObjectStore) {
if (buffer) {
if (buffer.created()) {
MBGL_CHECK_ERROR(glBindBuffer(bufferType, getID()));
} else {
buffer.create(glObjectStore);
Expand Down Expand Up @@ -65,15 +65,15 @@ class Buffer : private util::noncopyable {

// Uploads the buffer to the GPU to be available when we need it.
inline void upload(gl::GLObjectStore& glObjectStore) {
if (!buffer) {
if (!buffer.created()) {
bind(glObjectStore);
}
}

protected:
// increase the buffer size by at least /required/ bytes.
inline void *addElement() {
if (buffer) {
if (buffer.created()) {
throw std::runtime_error("Can't add elements after buffer was bound to GPU");
}
if (length < pos + itemSize) {
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/geometry/glyph_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void GlyphAtlas::removeGlyphs(uintptr_t tileUID) {

void GlyphAtlas::upload(gl::GLObjectStore& glObjectStore) {
if (dirty) {
const bool first = !texture;
const bool first = !texture.created();
bind(glObjectStore);

std::lock_guard<std::mutex> lock(mtx);
Expand Down Expand Up @@ -184,7 +184,7 @@ void GlyphAtlas::upload(gl::GLObjectStore& glObjectStore) {
}

void GlyphAtlas::bind(gl::GLObjectStore& glObjectStore) {
if (!texture) {
if (!texture.created()) {
texture.create(glObjectStore);
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture.getID()));
#ifndef GL_ES_VERSION_2_0
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/geometry/line_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void LineAtlas::upload(gl::GLObjectStore& glObjectStore) {

void LineAtlas::bind(gl::GLObjectStore& glObjectStore) {
bool first = false;
if (!texture) {
if (!texture.created()) {
texture.create(glObjectStore);
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture.getID()));
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
Expand Down
4 changes: 3 additions & 1 deletion src/mbgl/geometry/vao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ void VertexArrayObject::bindVertexArrayObject(gl::GLObjectStore& glObjectStore)
return;
}

if (!vao) vao.create(glObjectStore);
if (!vao.created()) {
vao.create(glObjectStore);
}
MBGL_CHECK_ERROR(gl::BindVertexArray(vao.getID()));
}

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/geometry/vao.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class VertexArrayObject : public util::noncopyable {
if (bound_shader == 0) {
vertexBuffer.bind(glObjectStore);
shader.bind(offset);
if (vao) {
if (vao.created()) {
storeBinding(shader, vertexBuffer.getID(), 0, offset);
}
} else {
Expand All @@ -39,7 +39,7 @@ class VertexArrayObject : public util::noncopyable {
vertexBuffer.bind(glObjectStore);
elementsBuffer.bind(glObjectStore);
shader.bind(offset);
if (vao) {
if (vao.created()) {
storeBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
}
} else {
Expand Down
24 changes: 12 additions & 12 deletions src/mbgl/gl/gl_object_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,61 @@ namespace mbgl {
namespace gl {

void ProgramHolder::create(GLObjectStore& objectStore_) {
if (id) return;
if (created()) return;
objectStore = &objectStore_;
id = MBGL_CHECK_ERROR(glCreateProgram());
}

void ProgramHolder::reset() {
if (!id) return;
if (!created()) return;
objectStore->abandonedPrograms.push_back(id);
id = 0;
}

void ShaderHolder::create(GLObjectStore& objectStore_) {
if (id) return;
if (created()) return;
objectStore = &objectStore_;
id = MBGL_CHECK_ERROR(glCreateShader(type));
}

void ShaderHolder::reset() {
if (!id) return;
if (!created()) return;
objectStore->abandonedShaders.push_back(id);
id = 0;
}

void BufferHolder::create(GLObjectStore& objectStore_) {
if (id) return;
if (created()) return;
objectStore = &objectStore_;
MBGL_CHECK_ERROR(glGenBuffers(1, &id));
}

void BufferHolder::reset() {
if (!id) return;
if (!created()) return;
objectStore->abandonedBuffers.push_back(id);
id = 0;
}

void TextureHolder::create(GLObjectStore& objectStore_) {
if (id) return;
if (created()) return;
objectStore = &objectStore_;
MBGL_CHECK_ERROR(glGenTextures(1, &id));
}

void TextureHolder::reset() {
if (!id) return;
if (!created()) return;
objectStore->abandonedTextures.push_back(id);
id = 0;
}

void TexturePoolHolder::create(GLObjectStore& objectStore_) {
if (bool()) return;
if (created()) return;
objectStore = &objectStore_;
MBGL_CHECK_ERROR(glGenTextures(TextureMax, ids.data()));
}

void TexturePoolHolder::reset() {
if (!bool()) return;
if (!created()) return;
for (GLuint& id : ids) {
if (id == 0) continue;
objectStore->abandonedTextures.push_back(id);
Expand All @@ -69,13 +69,13 @@ void TexturePoolHolder::reset() {
}

void VAOHolder::create(GLObjectStore& objectStore_) {
if (id) return;
if (created()) return;
objectStore = &objectStore_;
MBGL_CHECK_ERROR(gl::GenVertexArrays(1, &id));
}

void VAOHolder::reset() {
if (!id) return;
if (!created()) return;
objectStore->abandonedVAOs.push_back(id);
id = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/gl/gl_object_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GLHolder : private util::noncopyable {
GLHolder(GLHolder&& o) noexcept : id(o.id), objectStore(o.objectStore) { o.id = 0; }
GLHolder& operator=(GLHolder&& o) noexcept { id = o.id; objectStore = o.objectStore; o.id = 0; return *this; }

explicit operator bool() const { return id; }
bool created() const { return id; }
GLuint getID() const { return id; }

protected:
Expand Down Expand Up @@ -110,7 +110,7 @@ class TexturePoolHolder : private util::noncopyable {
TexturePoolHolder(TexturePoolHolder&& o) noexcept : ids(std::move(o.ids)), objectStore(o.objectStore) { o.ids.fill(0); }
TexturePoolHolder& operator=(TexturePoolHolder&& o) noexcept { ids = std::move(o.ids); objectStore = o.objectStore; o.ids.fill(0); return *this; }

explicit operator bool() const { return std::any_of(ids.begin(), ids.end(), [](int id) { return id; }); }
bool created() const { return std::any_of(ids.begin(), ids.end(), [](int id) { return id; }); }
const std::array<GLuint, TextureMax>& getIDs() const { return ids; }
const GLuint& operator[](size_t pos) { return ids[pos]; }

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/renderer/frame_history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool FrameHistory::needsAnimation(const Duration& duration) const {
void FrameHistory::upload(gl::GLObjectStore& glObjectStore) {

if (changed) {
const bool first = !texture;
const bool first = !texture.created();
bind(glObjectStore);

if (first) {
Expand Down Expand Up @@ -95,7 +95,7 @@ void FrameHistory::upload(gl::GLObjectStore& glObjectStore) {
}

void FrameHistory::bind(gl::GLObjectStore& glObjectStore) {
if (!texture) {
if (!texture.created()) {
texture.create(glObjectStore);
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture.getID()));
#ifndef GL_ES_VERSION_2_0
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/shader/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool Shader::compileShader(gl::ShaderHolder& shader, const GLchar *source[]) {
}

Shader::~Shader() {
if (program) {
if (program.created()) {
MBGL_CHECK_ERROR(glDetachShader(program.getID(), vertexShader.getID()));
MBGL_CHECK_ERROR(glDetachShader(program.getID(), fragmentShader.getID()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/sprite/sprite_atlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void SpriteAtlas::bind(bool linear, gl::GLObjectStore& glObjectStore) {
return; // Empty atlas
}

if (!texture) {
if (!texture.created()) {
texture.create(glObjectStore);
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture.getID()));
#ifndef GL_ES_VERSION_2_0
Expand Down

0 comments on commit c111250

Please sign in to comment.