add upload API to gl objects

This commit is contained in:
Konstantin Käfer
2015-04-22 17:52:10 +02:00
parent 5beb635028
commit c891df0b5f
6 changed files with 26 additions and 45 deletions

View File

@@ -2,6 +2,7 @@
#define MBGL_GEOMETRY_BUFFER
#include <mbgl/platform/gl.hpp>
#include <mbgl/platform/log.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/map/environment.hpp>
@@ -38,17 +39,16 @@ public:
}
// Transfers this buffer to the GPU and binds the buffer to the GL context.
void bind(bool force = false) {
if (buffer == 0) {
void bind() {
if (buffer) {
MBGL_CHECK_ERROR(glBindBuffer(bufferType, buffer));
} else {
MBGL_CHECK_ERROR(glGenBuffers(1, &buffer));
force = true;
}
MBGL_CHECK_ERROR(glBindBuffer(bufferType, buffer));
if (force) {
MBGL_CHECK_ERROR(glBindBuffer(bufferType, buffer));
if (array == nullptr) {
throw std::runtime_error("Buffer was already deleted or doesn't contain elements");
Log::Debug(Event::OpenGL, "Buffer doesn't contain elements");
pos = 0;
}
MBGL_CHECK_ERROR(glBufferData(bufferType, pos, array, GL_STATIC_DRAW));
if (!retainAfterUpload) {
cleanup();
@@ -67,6 +67,12 @@ public:
return buffer;
}
inline void upload() {
if (!buffer) {
bind();
}
}
protected:
// increase the buffer size by at least /required/ bytes.
inline void *addElement() {

View File

@@ -157,7 +157,7 @@ std::unique_ptr<Bucket> TileParser::createFillBucket(const GeometryTileLayer& la
tile.triangleElementsBuffer,
tile.lineElementsBuffer);
addBucketGeometries(bucket, layer, bucket_desc.filter);
return std::move(bucket);
return bucket->hasData() ? std::move(bucket) : nullptr;
}
std::unique_ptr<Bucket> TileParser::createLineBucket(const GeometryTileLayer& layer,
@@ -174,7 +174,7 @@ std::unique_ptr<Bucket> TileParser::createLineBucket(const GeometryTileLayer& la
applyLayoutProperty(PropertyKey::LineRoundLimit, bucket_desc.layout, layout.round_limit, z);
addBucketGeometries(bucket, layer, bucket_desc.filter);
return std::move(bucket);
return bucket->hasData() ? std::move(bucket) : nullptr;
}
std::unique_ptr<Bucket> TileParser::createSymbolBucket(const GeometryTileLayer& layer,
@@ -224,6 +224,6 @@ std::unique_ptr<Bucket> TileParser::createSymbolBucket(const GeometryTileLayer&
bucket->addFeatures(
layer, bucket_desc.filter, reinterpret_cast<uintptr_t>(&tile), spriteAtlas, *sprite, glyphAtlas, glyphStore);
return std::move(bucket);
return bucket->hasData() ? std::move(bucket) : nullptr;
}
}

View File

@@ -29,13 +29,6 @@ void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices
MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index()));
}
void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array, GLuint texture_) {
raster.bind(texture_);
shader.u_image = 0;
array.bind(shader, vertices, BUFFER_OFFSET(0));
MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index()));
}
bool RasterBucket::hasData() const {
return raster.isLoaded();
}

View File

@@ -26,8 +26,6 @@ public:
void drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array);
void drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array, GLuint texture);
Raster raster;
};

View File

@@ -46,16 +46,7 @@ void Raster::bind(bool linear) {
}
if (img && !textured) {
texture = texturePool.getTextureID();
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
#ifndef GL_ES_VERSION_2_0
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
#endif
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData()));
img.reset();
textured = true;
upload();
} else if (textured) {
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
}
@@ -68,24 +59,17 @@ void Raster::bind(bool linear) {
}
}
// overload ::bind for prerendered raster textures
void Raster::bind(const GLuint custom_texture) {
void Raster::upload() {
if (img && !textured) {
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, custom_texture));
texture = texturePool.getTextureID();
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, texture));
#ifndef GL_ES_VERSION_2_0
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0));
#endif
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
MBGL_CHECK_ERROR(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, img->getData()));
img.reset();
textured = true;
} else if (textured) {
MBGL_CHECK_ERROR(glBindTexture(GL_TEXTURE_2D, custom_texture));
}
GLuint new_filter = GL_LINEAR;
if (new_filter != this->filter) {
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, new_filter));
MBGL_CHECK_ERROR(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, new_filter));
filter = new_filter;
}
}

View File

@@ -25,8 +25,8 @@ public:
// bind current texture
void bind(bool linear = false);
// bind prerendered texture
void bind(const GLuint texture);
// uploads the texture if it hasn't been uploaded yet.
void upload();
// loaded status
bool isLoaded() const;