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

Commit

Permalink
[core] merge gl::ObjectStore into gl::Context
Browse files Browse the repository at this point in the history
  • Loading branch information
kkaefer authored and jfirebaugh committed Sep 27, 2016
1 parent ce42d22 commit 44c7e9d
Show file tree
Hide file tree
Showing 76 changed files with 555 additions and 580 deletions.
5 changes: 3 additions & 2 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ set(MBGL_CORE_FILES

# gl
include/mbgl/gl/gl.hpp
src/mbgl/gl/context.cpp
src/mbgl/gl/context.hpp
src/mbgl/gl/debugging.cpp
src/mbgl/gl/debugging.hpp
src/mbgl/gl/gl.cpp
src/mbgl/gl/object_store.cpp
src/mbgl/gl/object_store.hpp
src/mbgl/gl/object.cpp
src/mbgl/gl/object.hpp
src/mbgl/gl/state.hpp
src/mbgl/gl/value.cpp
src/mbgl/gl/value.hpp
Expand Down
9 changes: 4 additions & 5 deletions src/mbgl/geometry/buffer.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/object_store.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/noncopyable.hpp>
Expand Down Expand Up @@ -40,10 +39,10 @@ class Buffer : private util::noncopyable {
}

// Transfers this buffer to the GPU and binds the buffer to the GL context.
void bind(gl::ObjectStore& store, gl::Context& context) {
void bind(gl::Context& context) {
const bool initialized { buffer };
if (!initialized) {
buffer = store.createBuffer();
buffer = context.createBuffer();
}

if (target == GL_ARRAY_BUFFER) {
Expand Down Expand Up @@ -76,9 +75,9 @@ class Buffer : private util::noncopyable {
}

// Uploads the buffer to the GPU to be available when we need it.
void upload(gl::ObjectStore& store, gl::Context& context) {
void upload(gl::Context& context) {
if (!buffer) {
bind(store, context);
bind(context);
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/mbgl/geometry/line_atlas.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <mbgl/geometry/line_atlas.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/object_store.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/platform/platform.hpp>
Expand Down Expand Up @@ -121,16 +120,16 @@ LinePatternPos LineAtlas::addDash(const std::vector<float>& dasharray, LinePatte
return position;
}

void LineAtlas::upload(gl::ObjectStore& store, gl::Context& context, uint32_t unit) {
void LineAtlas::upload(gl::Context& context, uint32_t unit) {
if (dirty) {
bind(store, context, unit);
bind(context, unit);
}
}

void LineAtlas::bind(gl::ObjectStore& store, gl::Context& context, uint32_t unit) {
void LineAtlas::bind(gl::Context& context, uint32_t unit) {
bool first = false;
if (!texture) {
texture = store.createTexture();
texture = context.createTexture();
context.activeTexture = unit;
context.texture[unit] = *texture;
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
Expand Down
7 changes: 4 additions & 3 deletions src/mbgl/geometry/line_atlas.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once

#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/object_store.hpp>
#include <mbgl/gl/object.hpp>
#include <mbgl/util/optional.hpp>

#include <vector>
#include <unordered_map>
#include <memory>

namespace mbgl {

Expand All @@ -30,11 +31,11 @@ class LineAtlas {
~LineAtlas();

// Binds the atlas texture to the GPU, and uploads data if it is out of date.
void bind(gl::ObjectStore&, gl::Context&, uint32_t unit);
void bind(gl::Context&, uint32_t unit);

// Uploads the texture to the GPU to be available when we need it. This is a lazy operation;
// the texture is only bound when the data is out of date (=dirty).
void upload(gl::ObjectStore&, gl::Context&, uint32_t unit);
void upload(gl::Context&, uint32_t unit);

LinePatternPos getDashPosition(const std::vector<float>&, LinePatternCap);
LinePatternPos addDash(const std::vector<float>& dasharray, LinePatternCap);
Expand Down
5 changes: 2 additions & 3 deletions src/mbgl/geometry/vao.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <mbgl/geometry/vao.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/gl/object_store.hpp>
#include <mbgl/util/string.hpp>

namespace mbgl {
Expand All @@ -10,7 +9,7 @@ VertexArrayObject::VertexArrayObject() {

VertexArrayObject::~VertexArrayObject() = default;

void VertexArrayObject::bindVertexArrayObject(gl::ObjectStore& store, gl::Context& context) {
void VertexArrayObject::bindVertexArrayObject(gl::Context& context) {
if (!gl::GenVertexArrays || !gl::BindVertexArray) {
static bool reported = false;
if (!reported) {
Expand All @@ -21,7 +20,7 @@ void VertexArrayObject::bindVertexArrayObject(gl::ObjectStore& store, gl::Contex
}

if (!vao) {
vao = store.createVAO();
vao = context.createVAO();
context.vertexBuffer.setDirty();
context.elementBuffer.setDirty();
}
Expand Down
15 changes: 6 additions & 9 deletions src/mbgl/geometry/vao.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <mbgl/shader/shader.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/gl/object_store.hpp>
#include <mbgl/gl/context.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/optional.hpp>
Expand All @@ -20,11 +19,10 @@ class VertexArrayObject : public util::noncopyable {
void bind(Shader& shader,
VertexBuffer& vertexBuffer,
GLbyte* offset,
gl::ObjectStore& store,
gl::Context& context) {
bindVertexArrayObject(store, context);
bindVertexArrayObject(context);
if (bound_shader == 0) {
vertexBuffer.bind(store, context);
vertexBuffer.bind(context);
shader.bind(offset);
if (vao) {
storeBinding(shader, vertexBuffer.getID(), 0, offset);
Expand All @@ -39,12 +37,11 @@ class VertexArrayObject : public util::noncopyable {
VertexBuffer& vertexBuffer,
ElementsBuffer& elementsBuffer,
GLbyte* offset,
gl::ObjectStore& store,
gl::Context& context) {
bindVertexArrayObject(store, context);
bindVertexArrayObject(context);
if (bound_shader == 0) {
vertexBuffer.bind(store, context);
elementsBuffer.bind(store, context);
vertexBuffer.bind(context);
elementsBuffer.bind(context);
shader.bind(offset);
if (vao) {
storeBinding(shader, vertexBuffer.getID(), elementsBuffer.getID(), offset);
Expand All @@ -59,7 +56,7 @@ class VertexArrayObject : public util::noncopyable {
}

private:
void bindVertexArrayObject(gl::ObjectStore&, gl::Context&);
void bindVertexArrayObject(gl::Context&);
void storeBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset);
void verifyBinding(Shader &shader, GLuint vertexBuffer, GLuint elementsBuffer, GLbyte *offset);

Expand Down
95 changes: 95 additions & 0 deletions src/mbgl/gl/context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <mbgl/gl/context.hpp>

namespace mbgl {
namespace gl {

Context::~Context() {
reset();
}

void Context::reset() {
std::copy(pooledTextures.begin(), pooledTextures.end(), std::back_inserter(abandonedTextures));
pooledTextures.resize(0);
performCleanup();
}

namespace {

template <typename Fn>
void applyStateFunction(Context& context, Fn&& fn) {
fn(context.stencilFunc);
fn(context.stencilMask);
fn(context.stencilTest);
fn(context.stencilOp);
fn(context.depthRange);
fn(context.depthMask);
fn(context.depthTest);
fn(context.depthFunc);
fn(context.blend);
fn(context.blendFunc);
fn(context.blendColor);
fn(context.colorMask);
fn(context.clearDepth);
fn(context.clearColor);
fn(context.clearStencil);
fn(context.program);
fn(context.lineWidth);
fn(context.activeTexture);
fn(context.bindFramebuffer);
fn(context.viewport);
#ifndef GL_ES_VERSION_2_0
fn(context.pixelZoom);
fn(context.rasterPos);
#endif // GL_ES_VERSION_2_0
for (auto& tex : context.texture) {
fn(tex);
}
fn(context.vertexBuffer);
fn(context.elementBuffer);
fn(context.vertexArrayObject);
}

} // namespace

void Context::resetState() {
applyStateFunction(*this, [](auto& state) { state.reset(); });
}

void Context::setDirtyState() {
applyStateFunction(*this, [](auto& state) { state.setDirty(); });
}

void Context::performCleanup() {
for (GLuint id : abandonedPrograms) {
MBGL_CHECK_ERROR(glDeleteProgram(id));
}
abandonedPrograms.clear();

for (GLuint id : abandonedShaders) {
MBGL_CHECK_ERROR(glDeleteShader(id));
}
abandonedShaders.clear();

if (!abandonedBuffers.empty()) {
MBGL_CHECK_ERROR(glDeleteBuffers(int(abandonedBuffers.size()), abandonedBuffers.data()));
abandonedBuffers.clear();
}

if (!abandonedTextures.empty()) {
MBGL_CHECK_ERROR(glDeleteTextures(int(abandonedTextures.size()), abandonedTextures.data()));
abandonedTextures.clear();
}

if (!abandonedVAOs.empty()) {
MBGL_CHECK_ERROR(gl::DeleteVertexArrays(int(abandonedVAOs.size()), abandonedVAOs.data()));
abandonedVAOs.clear();
}

if (!abandonedFBOs.empty()) {
MBGL_CHECK_ERROR(glDeleteFramebuffers(int(abandonedFBOs.size()), abandonedFBOs.data()));
abandonedFBOs.clear();
}
}

} // namespace gl
} // namespace mbgl
Loading

0 comments on commit 44c7e9d

Please sign in to comment.