From 5b14c85b780249b904680978744226ad2b81eec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 11 Nov 2014 16:56:20 +0100 Subject: use 4-byte aligned vertex attributes for performance reason reverts 9d5f02ccaa051a0f3459f6fc94df807338fa2552 and 4c0b4a79017bc7c7ae864e6f961f186605be617f --- src/mbgl/geometry/icon_buffer.cpp | 16 +++++----- src/mbgl/geometry/icon_buffer.hpp | 2 +- src/mbgl/geometry/line_buffer.cpp | 4 +-- src/mbgl/geometry/sprite_atlas.cpp | 15 ++++++++-- src/mbgl/geometry/text_buffer.cpp | 34 +++++++++++---------- src/mbgl/renderer/painter_symbol.cpp | 9 ++---- src/mbgl/shader/icon.vertex.glsl | 18 +++++++----- src/mbgl/shader/icon_shader.cpp | 34 +++++---------------- src/mbgl/shader/icon_shader.hpp | 9 ++---- src/mbgl/shader/line.vertex.glsl | 6 ++-- src/mbgl/shader/line_shader.cpp | 10 ++----- src/mbgl/shader/line_shader.hpp | 3 +- src/mbgl/shader/sdf.vertex.glsl | 18 +++++++----- src/mbgl/shader/sdf_shader.cpp | 57 +++++++----------------------------- src/mbgl/shader/sdf_shader.hpp | 9 ++---- 15 files changed, 97 insertions(+), 147 deletions(-) (limited to 'src') diff --git a/src/mbgl/geometry/icon_buffer.cpp b/src/mbgl/geometry/icon_buffer.cpp index c571dfa69e..5e7b76fde2 100644 --- a/src/mbgl/geometry/icon_buffer.cpp +++ b/src/mbgl/geometry/icon_buffer.cpp @@ -19,15 +19,17 @@ size_t IconVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, int16_t t shorts[3] /* offset */ = std::round(oy * 64); uint8_t *ubytes = static_cast(data); - ubytes[8] /* labelminzoom */ = labelminzoom * 10; - ubytes[9] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160. - ubytes[10] /* maxzoom */ = std::fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160. + // a_data1 + ubytes[8] /* tex */ = tx / 4; + ubytes[9] /* tex */ = ty / 4; + ubytes[10] /* labelminzoom */ = labelminzoom * 10; ubytes[11] /* angle */ = (int16_t)std::round(angle * angleFactor) % 256; - ubytes[12] /* rangeend */ = util::max((int16_t)std::round(range[0] * angleFactor), (int16_t)0) % 256; - ubytes[13] /* rangestart */ = util::min((int16_t)std::round(range[1] * angleFactor), (int16_t)255) % 256; - shorts[8] /* tex */ = tx; - shorts[9] /* tex */ = ty; + // a_data2 + ubytes[12] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160. + ubytes[13] /* maxzoom */ = std::fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160. + ubytes[14] /* rangeend */ = util::max((int16_t)std::round(range[0] * angleFactor), (int16_t)0) % 256; + ubytes[15] /* rangestart */ = util::min((int16_t)std::round(range[1] * angleFactor), (int16_t)255) % 256; return idx; } diff --git a/src/mbgl/geometry/icon_buffer.hpp b/src/mbgl/geometry/icon_buffer.hpp index 08c9687004..90ce19b1ed 100644 --- a/src/mbgl/geometry/icon_buffer.hpp +++ b/src/mbgl/geometry/icon_buffer.hpp @@ -8,7 +8,7 @@ namespace mbgl { class IconVertexBuffer : public Buffer< - 20 + 16 > { public: static const double angleFactor; diff --git a/src/mbgl/geometry/line_buffer.cpp b/src/mbgl/geometry/line_buffer.cpp index 50a6e66b93..7704838483 100644 --- a/src/mbgl/geometry/line_buffer.cpp +++ b/src/mbgl/geometry/line_buffer.cpp @@ -16,8 +16,8 @@ size_t LineVertexBuffer::add(vertex_type x, vertex_type y, float ex, float ey, i int8_t *extrude = static_cast(data); extrude[4] = std::round(extrudeScale * ex); extrude[5] = std::round(extrudeScale * ey); - - coords[3] = linesofar; + extrude[6] = static_cast(linesofar / 128); + extrude[7] = static_cast(linesofar % 128); return idx; } diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index 0f39de74ba..7a070f2d65 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -78,17 +78,26 @@ void copy_bitmap(const uint32_t *src, const int src_stride, const int src_x, con } Rect SpriteAtlas::allocateImage(size_t pixel_width, size_t pixel_height) { + uint16_t pack_width = pixel_width + buffer * 2; + uint16_t pack_height = pixel_height + buffer * 2; + + // Increase to next number divisible by 4, but at least 1. + // This is so we can scale down the texture coordinates and pack them + // into 2 bytes rather than 4 bytes. + pack_width += (4 - pack_width % 4); + pack_height += (4 - pack_height % 4); + // We have to allocate a new area in the bin, and store an empty image in it. // Add a 1px border around every image. - Rect rect = bin.allocate(pixel_width + 2 * buffer, pixel_height + 2 * buffer); + Rect rect = bin.allocate(pack_width, pack_height); if (rect.w == 0) { return rect; } rect.x += buffer; rect.y += buffer; - rect.w -= 2 * buffer; - rect.h -= 2 * buffer; + rect.w = pixel_width; + rect.h = pixel_height; return rect; } diff --git a/src/mbgl/geometry/text_buffer.cpp b/src/mbgl/geometry/text_buffer.cpp index 295ff02efa..e9bfc503be 100644 --- a/src/mbgl/geometry/text_buffer.cpp +++ b/src/mbgl/geometry/text_buffer.cpp @@ -4,30 +4,34 @@ #include -using namespace mbgl; +namespace mbgl { const double TextVertexBuffer::angleFactor = 128.0 / M_PI; size_t TextVertexBuffer::add(int16_t x, int16_t y, float ox, float oy, uint16_t tx, uint16_t ty, float angle, float minzoom, std::array range, float maxzoom, float labelminzoom) { - size_t idx = index(); + const size_t idx = index(); void *data = addElement(); int16_t *shorts = static_cast(data); - shorts[0] = x; - shorts[1] = y; - shorts[2] = std::round(ox * 64); // use 1/64 pixels for placement - shorts[3] = std::round(oy * 64); + shorts[0] /* pos */ = x; + shorts[1] /* pos */ = y; + shorts[2] /* offset */ = std::round(ox * 64); // use 1/64 pixels for placement + shorts[3] /* offset */ = std::round(oy * 64); uint8_t *ubytes = static_cast(data); - ubytes[8] = labelminzoom * 10; - ubytes[9] = minzoom * 10; // 1/10 zoom levels: z16 == 160. - ubytes[10] = fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160. - ubytes[11] = (int16_t)round(angle * angleFactor) % 256; - ubytes[12] = util::max((int16_t)std::round(range[0] * angleFactor), (int16_t)0) % 256; - ubytes[13] = util::min((int16_t)std::round(range[1] * angleFactor), (int16_t)255) % 256; - - ubytes[14] = tx / 4; - ubytes[15] = ty / 4; + // a_data1 + ubytes[8] /* tex */ = tx / 4; + ubytes[9] /* tex */ = ty / 4; + ubytes[10] /* labelminzoom */ = labelminzoom * 10; + ubytes[11] /* angle */ = (int16_t)std::round(angle * angleFactor) % 256; + + // a_data2 + ubytes[12] /* minzoom */ = minzoom * 10; // 1/10 zoom levels: z16 == 160. + ubytes[13] /* maxzoom */ = std::fmin(maxzoom, 25) * 10; // 1/10 zoom levels: z16 == 160. + ubytes[14] /* rangeend */ = util::max((int16_t)std::round(range[0] * angleFactor), (int16_t)0) % 256; + ubytes[15] /* rangestart */ = util::min((int16_t)std::round(range[1] * angleFactor), (int16_t)255) % 256; return idx; } + +} diff --git a/src/mbgl/renderer/painter_symbol.cpp b/src/mbgl/renderer/painter_symbol.cpp index f5320b4be7..d9e4a58c23 100644 --- a/src/mbgl/renderer/painter_symbol.cpp +++ b/src/mbgl/renderer/painter_symbol.cpp @@ -134,11 +134,6 @@ void Painter::renderSymbol(SymbolBucket &bucket, util::ptr layer_des spriteAtlas.bind(state.isChanging() || bucket.properties.placement == PlacementType::Line || angleOffset != 0 || fontScale != 1 || sdf); - std::array texsize = {{ - float(spriteAtlas.getWidth()), - float(spriteAtlas.getHeight()) - }}; - if (sdf) { renderSDF(bucket, id, @@ -146,7 +141,7 @@ void Painter::renderSymbol(SymbolBucket &bucket, util::ptr layer_des bucket.properties.icon, properties.icon, 1.0f, - texsize, + {{ float(spriteAtlas.getWidth()) / 4.0f, float(spriteAtlas.getHeight()) / 4.0f }}, *sdfIconShader, &SymbolBucket::drawIcons); } else { @@ -164,7 +159,7 @@ void Painter::renderSymbol(SymbolBucket &bucket, util::ptr layer_des useProgram(iconShader->program); iconShader->u_matrix = vtxMatrix; iconShader->u_exmatrix = exMatrix; - iconShader->u_texsize = texsize; + iconShader->u_texsize = {{ float(spriteAtlas.getWidth()) / 4.0f, float(spriteAtlas.getHeight()) / 4.0f }}; // Convert the -pi..pi to an int8 range. const float angle = std::round(state.getAngle() / M_PI * 128); diff --git a/src/mbgl/shader/icon.vertex.glsl b/src/mbgl/shader/icon.vertex.glsl index 8c69c40410..fd7afd5b7d 100644 --- a/src/mbgl/shader/icon.vertex.glsl +++ b/src/mbgl/shader/icon.vertex.glsl @@ -1,12 +1,7 @@ attribute vec2 a_pos; attribute vec2 a_offset; -attribute vec2 a_tex; -attribute float a_angle; -attribute float a_minzoom; -attribute float a_maxzoom; -attribute float a_rangeend; -attribute float a_rangestart; -attribute float a_labelminzoom; +attribute vec4 a_data1; +attribute vec4 a_data2; // matrix is for the vertex position, exmatrix is for rotating and projecting @@ -28,6 +23,15 @@ varying vec2 v_tex; varying float v_alpha; void main() { + vec2 a_tex = a_data1.xy; + float a_labelminzoom = a_data1[2]; + float a_angle = a_data1[3]; + vec2 a_zoom = a_data2.st; + float a_minzoom = a_zoom[0]; + float a_maxzoom = a_zoom[1]; + vec2 a_range = a_data2.pq; + float a_rangeend = a_range[0]; + float a_rangestart = a_range[1]; float a_fadedist = 10.0; float rev = 0.0; diff --git a/src/mbgl/shader/icon_shader.cpp b/src/mbgl/shader/icon_shader.cpp index fa397fb274..0dfd67e25b 100644 --- a/src/mbgl/shader/icon_shader.cpp +++ b/src/mbgl/shader/icon_shader.cpp @@ -19,17 +19,12 @@ IconShader::IconShader() a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos")); a_offset = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_offset")); - a_tex = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_tex")); - a_angle = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_angle")); - a_minzoom = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_minzoom")); - a_maxzoom = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_maxzoom")); - a_rangeend = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_rangeend")); - a_rangestart = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_rangestart")); - a_labelminzoom = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_labelminzoom")); + a_data1 = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data1")); + a_data2 = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data2")); } void IconShader::bind(char *offset) { - const int stride = 20; + const int stride = 16; MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0)); @@ -37,24 +32,9 @@ void IconShader::bind(char *offset) { MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_offset)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_labelminzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_labelminzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 8)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data1)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data1, 4, GL_UNSIGNED_BYTE, false, stride, offset + 8)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_minzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_minzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 9)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_maxzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_maxzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 10)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_angle)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_angle, 1, GL_UNSIGNED_BYTE, false, stride, offset + 11)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_rangeend)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_rangeend, 1, GL_UNSIGNED_BYTE, false, stride, offset + 12)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_rangestart)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_rangestart, 1, GL_UNSIGNED_BYTE, false, stride, offset + 13)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_tex)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_tex, 2, GL_SHORT, false, stride, offset + 16)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data2)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data2, 4, GL_UNSIGNED_BYTE, false, stride, offset + 12)); } diff --git a/src/mbgl/shader/icon_shader.hpp b/src/mbgl/shader/icon_shader.hpp index 645d7e21b6..155b9addca 100644 --- a/src/mbgl/shader/icon_shader.hpp +++ b/src/mbgl/shader/icon_shader.hpp @@ -27,13 +27,8 @@ public: private: int32_t a_pos = -1; int32_t a_offset = -1; - int32_t a_tex = -1; - int32_t a_angle = -1; - int32_t a_minzoom = -1; - int32_t a_maxzoom = -1; - int32_t a_rangeend = -1; - int32_t a_rangestart = -1; - int32_t a_labelminzoom = -1; + int32_t a_data1 = -1; + int32_t a_data2 = -1; }; } diff --git a/src/mbgl/shader/line.vertex.glsl b/src/mbgl/shader/line.vertex.glsl index 1d8e687c95..bf0b537e83 100644 --- a/src/mbgl/shader/line.vertex.glsl +++ b/src/mbgl/shader/line.vertex.glsl @@ -7,8 +7,7 @@ #define scale 0.015873016 attribute vec2 a_pos; -attribute vec2 a_extrude; -attribute float a_linesofar; +attribute vec4 a_data; // matrix is for the vertex position, exmatrix is for rotating and projecting // the extrusion vector. @@ -24,6 +23,9 @@ varying vec2 v_normal; varying float v_linesofar; void main() { + vec2 a_extrude = a_data.xy; + float a_linesofar = a_data.z * 128.0 + a_data.w; + // We store the texture normals in the most insignificant bit // transform y so that 0 => -1 and 1 => 1 // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap diff --git a/src/mbgl/shader/line_shader.cpp b/src/mbgl/shader/line_shader.cpp index 5ad257eabd..432a64f695 100644 --- a/src/mbgl/shader/line_shader.cpp +++ b/src/mbgl/shader/line_shader.cpp @@ -18,17 +18,13 @@ LineShader::LineShader() } a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos")); - a_extrude = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_extrude")); - a_linesofar = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_linesofar")); + a_data = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data")); } void LineShader::bind(char *offset) { MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_extrude)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_extrude, 2, GL_BYTE, false, 8, offset + 4)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_linesofar)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_linesofar, 1, GL_SHORT, false, 8, offset + 6)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_BYTE, false, 8, offset + 4)); } diff --git a/src/mbgl/shader/line_shader.hpp b/src/mbgl/shader/line_shader.hpp index b789330882..c6adc0ff8d 100644 --- a/src/mbgl/shader/line_shader.hpp +++ b/src/mbgl/shader/line_shader.hpp @@ -22,8 +22,7 @@ public: private: int32_t a_pos = -1; - int32_t a_extrude = -1; - int32_t a_linesofar = -1; + int32_t a_data = -1; }; diff --git a/src/mbgl/shader/sdf.vertex.glsl b/src/mbgl/shader/sdf.vertex.glsl index c5166ae46f..bbfc44919e 100644 --- a/src/mbgl/shader/sdf.vertex.glsl +++ b/src/mbgl/shader/sdf.vertex.glsl @@ -1,12 +1,7 @@ attribute vec2 a_pos; attribute vec2 a_offset; -attribute vec2 a_tex; -attribute float a_angle; -attribute float a_minzoom; -attribute float a_maxzoom; -attribute float a_rangeend; -attribute float a_rangestart; -attribute float a_labelminzoom; +attribute vec4 a_data1; +attribute vec4 a_data2; // matrix is for the vertex position, exmatrix is for rotating and projecting @@ -27,6 +22,15 @@ varying vec2 v_tex; varying float v_alpha; void main() { + vec2 a_tex = a_data1.xy; + float a_labelminzoom = a_data1[2]; + float a_angle = a_data1[3]; + vec2 a_zoom = a_data2.st; + float a_minzoom = a_zoom[0]; + float a_maxzoom = a_zoom[1]; + vec2 a_range = a_data2.pq; + float a_rangeend = a_range[0]; + float a_rangestart = a_range[1]; float rev = 0.0; diff --git a/src/mbgl/shader/sdf_shader.cpp b/src/mbgl/shader/sdf_shader.cpp index 25b226ee23..757884f39c 100644 --- a/src/mbgl/shader/sdf_shader.cpp +++ b/src/mbgl/shader/sdf_shader.cpp @@ -19,13 +19,8 @@ SDFShader::SDFShader() a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos")); a_offset = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_offset")); - a_tex = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_tex")); - a_angle = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_angle")); - a_minzoom = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_minzoom")); - a_maxzoom = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_maxzoom")); - a_rangeend = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_rangeend")); - a_rangestart = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_rangestart")); - a_labelminzoom = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_labelminzoom")); + a_data1 = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data1")); + a_data2 = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data2")); } void SDFGlyphShader::bind(char *offset) { @@ -37,30 +32,15 @@ void SDFGlyphShader::bind(char *offset) { MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_offset)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_labelminzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_labelminzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 8)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data1)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data1, 4, GL_UNSIGNED_BYTE, false, stride, offset + 8)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_minzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_minzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 9)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_maxzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_maxzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 10)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_angle)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_angle, 1, GL_UNSIGNED_BYTE, false, stride, offset + 11)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_rangeend)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_rangeend, 1, GL_UNSIGNED_BYTE, false, stride, offset + 12)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_rangestart)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_rangestart, 1, GL_UNSIGNED_BYTE, false, stride, offset + 13)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_tex)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_tex, 2, GL_UNSIGNED_BYTE, false, stride, offset + 14)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data2)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data2, 4, GL_UNSIGNED_BYTE, false, stride, offset + 12)); } void SDFIconShader::bind(char *offset) { - const int stride = 20; + const int stride = 16; MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0)); @@ -68,24 +48,9 @@ void SDFIconShader::bind(char *offset) { MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_offset)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_labelminzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_labelminzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 8)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_minzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_minzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 9)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_maxzoom)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_maxzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 10)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_angle)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_angle, 1, GL_UNSIGNED_BYTE, false, stride, offset + 11)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_rangeend)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_rangeend, 1, GL_UNSIGNED_BYTE, false, stride, offset + 12)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_rangestart)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_rangestart, 1, GL_UNSIGNED_BYTE, false, stride, offset + 13)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data1)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data1, 4, GL_UNSIGNED_BYTE, false, stride, offset + 8)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_tex)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_tex, 2, GL_SHORT, false, stride, offset + 16)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data2)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data2, 4, GL_UNSIGNED_BYTE, false, stride, offset + 12)); } diff --git a/src/mbgl/shader/sdf_shader.hpp b/src/mbgl/shader/sdf_shader.hpp index 0737c25ee1..08f3053b7d 100644 --- a/src/mbgl/shader/sdf_shader.hpp +++ b/src/mbgl/shader/sdf_shader.hpp @@ -29,13 +29,8 @@ public: protected: int32_t a_pos = -1; int32_t a_offset = -1; - int32_t a_tex = -1; - int32_t a_angle = -1; - int32_t a_minzoom = -1; - int32_t a_maxzoom = -1; - int32_t a_rangeend = -1; - int32_t a_rangestart = -1; - int32_t a_labelminzoom = -1; + int32_t a_data1 = -1; + int32_t a_data2 = -1; }; class SDFGlyphShader : public SDFShader { -- cgit v1.2.1 From 2648e6a9647001c987cf6db46115b0b8b21127be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 11 Nov 2014 16:59:39 +0100 Subject: add debug line for showing the sprite atlas --- src/mbgl/geometry/sprite_atlas.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index 7a070f2d65..db75414c0b 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -258,6 +258,8 @@ void SpriteAtlas::bind(bool linear) { } dirty = false; + + // platform::show_color_debug_image("Sprite Atlas", reinterpret_cast(data), width, height, width * pixelRatio, height * pixelRatio); } }; -- cgit v1.2.1 From f8ba23377d4377a6047f8cb9b28c8ce94a00e8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Tue, 11 Nov 2014 16:59:50 +0100 Subject: [performance] remove glFlush() --- src/mbgl/renderer/painter.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/mbgl/renderer/painter.cpp b/src/mbgl/renderer/painter.cpp index 67d3c9afe2..1af33c4130 100644 --- a/src/mbgl/renderer/painter.cpp +++ b/src/mbgl/renderer/painter.cpp @@ -244,8 +244,6 @@ void Painter::render(const Style& style, const std::set>& for (const util::ptr &source : sources) { source->source->finishRender(*this); } - - MBGL_CHECK_ERROR(glFlush()); } void Painter::renderLayers(util::ptr group) { -- cgit v1.2.1 From febfb7ac0965852ee224744bd33a3e3e5461ba80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 11 Dec 2014 11:58:46 +0100 Subject: fix rendering errors --- src/mbgl/geometry/sprite_atlas.cpp | 34 ++++++++++++++++++--------------- src/mbgl/renderer/painter_line.cpp | 3 ++- src/mbgl/shader/linepattern.vertex.glsl | 11 +++++++---- src/mbgl/shader/linepattern_shader.cpp | 11 +++-------- src/mbgl/shader/linepattern_shader.hpp | 3 +-- 5 files changed, 32 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index db75414c0b..d3771b4695 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -77,15 +77,12 @@ void copy_bitmap(const uint32_t *src, const int src_stride, const int src_x, con } } -Rect SpriteAtlas::allocateImage(size_t pixel_width, size_t pixel_height) { - uint16_t pack_width = pixel_width + buffer * 2; - uint16_t pack_height = pixel_height + buffer * 2; - +Rect SpriteAtlas::allocateImage(const size_t pixel_width, const size_t pixel_height) { // Increase to next number divisible by 4, but at least 1. // This is so we can scale down the texture coordinates and pack them // into 2 bytes rather than 4 bytes. - pack_width += (4 - pack_width % 4); - pack_height += (4 - pack_height % 4); + const uint16_t pack_width = pixel_width + (4 - pixel_width % 4); + const uint16_t pack_height = pixel_height + (4 - pixel_width % 4); // We have to allocate a new area in the bin, and store an empty image in it. // Add a 1px border around every image. @@ -96,8 +93,6 @@ Rect SpriteAtlas::allocateImage(size_t pixel_width, size rect.x += buffer; rect.y += buffer; - rect.w = pixel_width; - rect.h = pixel_height; return rect; } @@ -132,15 +127,24 @@ Rect SpriteAtlas::getImage(const std::string& name) { SpriteAtlasPosition SpriteAtlas::getPosition(const std::string& name, bool repeating) { std::lock_guard lock(mtx); - // `repeating` indicates that the image will be used in a repeating pattern - // repeating pattern images are assumed to have a 1px padding that mirrors the opposite edge - // positions for repeating images are adjusted to exclude the edge + Rect rect = getImage(name); - const int r = repeating ? 1 : 0; + if (repeating) { + // When the image is repeating, get the correct position of the image, rather than the + // one rounded up to 4 pixels. + const SpritePosition &pos = sprite->getSpritePosition(name); + if (!pos.width || !pos.height) { + return SpriteAtlasPosition {}; + } + + rect.w = pos.width; + rect.h = pos.height; + } + return SpriteAtlasPosition { - {{ float(rect.w) / pixelRatio, float(rect.h) / pixelRatio }}, - {{ float(rect.x + r) / width, float(rect.y + r) / height }}, - {{ float(rect.x + rect.w - 2*r) / width, float(rect.y + rect.h - 2*r) / height }} + {{ float(rect.w) / pixelRatio, float(rect.h) / pixelRatio }}, + {{ float(rect.x) / width, float(rect.y) / height }}, + {{ float(rect.x + rect.w) / width, float(rect.y + rect.h) / height }} }; } diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index c46bc23439..af5017b14c 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -61,7 +61,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c } if (properties.image.size()) { - SpriteAtlasPosition imagePos = spriteAtlas.getPosition(properties.image); + SpriteAtlasPosition imagePos = spriteAtlas.getPosition(properties.image, true); float factor = 8.0 / std::pow(2, state.getIntegerZoom() - id.z); float fade = std::fmod(state.getZoom(), 1.0); @@ -79,6 +79,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c linepatternShader->u_pattern_br = imagePos.br; linepatternShader->u_fade = fade; + glActiveTexture(GL_TEXTURE0); spriteAtlas.bind(true); MBGL_CHECK_ERROR(glDepthRange(strata + strata_epsilon, 1.0f)); // may or may not matter diff --git a/src/mbgl/shader/linepattern.vertex.glsl b/src/mbgl/shader/linepattern.vertex.glsl index 4600ebf65b..ada384a3d1 100644 --- a/src/mbgl/shader/linepattern.vertex.glsl +++ b/src/mbgl/shader/linepattern.vertex.glsl @@ -3,11 +3,11 @@ // stored in a byte (-128..127). we scale regular normals up to length 63, but // there are also "special" normals that have a bigger length (of up to 126 in // this case). -#define scale 63.0 +// #define scale 63.0 +#define scale 0.015873016 attribute vec2 a_pos; -attribute vec2 a_extrude; -attribute float a_linesofar; +attribute vec4 a_data; // matrix is for the vertex position, exmatrix is for rotating and projecting // the extrusion vector. @@ -24,6 +24,9 @@ varying vec2 v_normal; varying float v_linesofar; void main() { + vec2 a_extrude = a_data.xy; + float a_linesofar = a_data.z * 128.0 + a_data.w; + // We store the texture normals in the most insignificant bit // transform y so that 0 => -1 and 1 => 1 // In the texture normal, x is 0 if the normal points straight up/down and 1 if it's a round cap @@ -34,7 +37,7 @@ void main() { // Scale the extrusion vector down to a normal and then up by the line width // of this vertex. - vec2 extrude = a_extrude / scale; + vec2 extrude = a_extrude * scale; vec2 dist = u_linewidth.s * extrude * (1.0 - u_point); // If the x coordinate is the maximum integer, we move the z coordinates out diff --git a/src/mbgl/shader/linepattern_shader.cpp b/src/mbgl/shader/linepattern_shader.cpp index 492a8f2a85..c45378378d 100644 --- a/src/mbgl/shader/linepattern_shader.cpp +++ b/src/mbgl/shader/linepattern_shader.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include @@ -19,17 +18,13 @@ LinepatternShader::LinepatternShader() } a_pos = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_pos")); - a_extrude = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_extrude")); - a_linesofar = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_linesofar")); + a_data = MBGL_CHECK_ERROR(glGetAttribLocation(program, "a_data")); } void LinepatternShader::bind(char *offset) { MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos)); MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_SHORT, false, 8, offset + 0)); - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_extrude)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_extrude, 2, GL_BYTE, false, 8, offset + 4)); - - MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_linesofar)); - MBGL_CHECK_ERROR(glVertexAttribPointer(a_linesofar, 1, GL_SHORT, false, 8, offset + 6)); + MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_data)); + MBGL_CHECK_ERROR(glVertexAttribPointer(a_data, 4, GL_BYTE, false, 8, offset + 4)); } diff --git a/src/mbgl/shader/linepattern_shader.hpp b/src/mbgl/shader/linepattern_shader.hpp index bf85940b8a..ace8198aed 100644 --- a/src/mbgl/shader/linepattern_shader.hpp +++ b/src/mbgl/shader/linepattern_shader.hpp @@ -25,8 +25,7 @@ public: private: int32_t a_pos = -1; - int32_t a_extrude = -1; - int32_t a_linesofar = -1; + int32_t a_data = -1; }; } -- cgit v1.2.1 From bec97a3d408756bfdf379bc28e0836c65e081326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 11 Dec 2014 12:14:06 +0100 Subject: warp in MBGL_CHECK_ERROR() call --- src/mbgl/renderer/painter_line.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index af5017b14c..a25942b2dc 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -79,7 +79,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c linepatternShader->u_pattern_br = imagePos.br; linepatternShader->u_fade = fade; - glActiveTexture(GL_TEXTURE0); + MBGL_CHECK_ERROR(glActiveTexture(GL_TEXTURE0)); spriteAtlas.bind(true); MBGL_CHECK_ERROR(glDepthRange(strata + strata_epsilon, 1.0f)); // may or may not matter -- cgit v1.2.1 From 6cabaf02302c9e3ee6ac293217e74fcb0317dff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 11 Dec 2014 12:27:26 +0100 Subject: provide explicit constructor to appease GCC --- src/mbgl/geometry/sprite_atlas.hpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src') diff --git a/src/mbgl/geometry/sprite_atlas.hpp b/src/mbgl/geometry/sprite_atlas.hpp index 9e0fe995bb..ebfd69edf5 100644 --- a/src/mbgl/geometry/sprite_atlas.hpp +++ b/src/mbgl/geometry/sprite_atlas.hpp @@ -19,6 +19,10 @@ class Sprite; class SpritePosition; struct SpriteAtlasPosition { + inline SpriteAtlasPosition(const std::array size_ = {{0, 0}}, + const std::array tl_ = {{0, 0}}, + const std::array br_ = {{0, 0}}) + : size(size_), tl(tl_), br(br_) {} std::array size; std::array tl; std::array br; -- cgit v1.2.1 From 59b51f0a9f7062ef4087217149dd30e4f3e941d7 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 8 Jan 2015 14:00:06 -0500 Subject: fix fill pattern spacing js: 22e24f7f67c4db89d09c0b65df67d0fcf1c3b614 --- src/mbgl/geometry/sprite_atlas.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/mbgl/geometry/sprite_atlas.cpp b/src/mbgl/geometry/sprite_atlas.cpp index d3771b4695..2d6a8a7835 100644 --- a/src/mbgl/geometry/sprite_atlas.cpp +++ b/src/mbgl/geometry/sprite_atlas.cpp @@ -137,12 +137,12 @@ SpriteAtlasPosition SpriteAtlas::getPosition(const std::string& name, bool repea return SpriteAtlasPosition {}; } - rect.w = pos.width; - rect.h = pos.height; + rect.w = pos.width / pos.pixelRatio; + rect.h = pos.height / pos.pixelRatio; } return SpriteAtlasPosition { - {{ float(rect.w) / pixelRatio, float(rect.h) / pixelRatio }}, + {{ float(rect.w), float(rect.h) }}, {{ float(rect.x) / width, float(rect.y) / height }}, {{ float(rect.x + rect.w) / width, float(rect.y + rect.h) / height }} }; -- cgit v1.2.1 From 166a3e21fac8632cdf5afdf4d38bf429e45dc699 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Thu, 8 Jan 2015 14:40:19 -0500 Subject: rephrase line uniform calculations to match js js: 359a576471e20373333cae80a5eccda95ee4cdc2 --- src/mbgl/renderer/painter_line.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index a25942b2dc..71364f40c4 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -15,13 +15,25 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c const LineProperties &properties = layer_desc->getProperties(); + // the distance over which the line edge fades out. + // Retina devices need a smaller distance to avoid aliasing. float antialiasing = 1 / state.getPixelRatio(); - float width = properties.width; - float offset = properties.gap_width == 0 ? 0 : (properties.gap_width + width) / 2; + float blur = properties.blur + antialiasing; + float edgeWidth = properties.width / 2.0; + float inset = -1; + float offset = 0; + float shift = 0; + + if (properties.gap_width != 0) { + inset = properties.gap_width / 2.0 + antialiasing * 0.5; + edgeWidth = properties.width; + + // shift outer lines half a pixel towards the middle to eliminate the crack + offset = inset - antialiasing / 2.0; + } - float inset = std::fmin((std::fmax(-1, offset - width / 2 - antialiasing / 2) + 1), 16.0f); - float outset = std::fmin(offset + width / 2 + antialiasing / 2, 16.0f); + float outset = offset + edgeWidth + antialiasing / 2.0 + shift; Color color = properties.color; color[0] *= properties.opacity; -- cgit v1.2.1