Skip to content

Commit

Permalink
Device pixel ratio send as define instead of UBO field (maplibre#1936)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
alexcristici and pre-commit-ci[bot] authored Dec 9, 2023
1 parent 1c2faae commit ea73a57
Show file tree
Hide file tree
Showing 44 changed files with 114 additions and 107 deletions.
3 changes: 1 addition & 2 deletions include/mbgl/shaders/circle_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ static_assert(sizeof(CircleDrawableUBO) == 5 * 16);

struct alignas(16) CirclePaintParamsUBO {
/* 0 */ float camera_to_center_distance;
/* 4 */ float device_pixel_ratio;
/* 8 */ float pad1, pad2;
/* 4 */ float pad1, pad2, pad3;
/* 16 */
};
static_assert(sizeof(CirclePaintParamsUBO) == 1 * 16);
Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/shaders/gl/drawable_circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ layout (std140) uniform CircleDrawableUBO {
layout (std140) uniform CirclePaintParamsUBO {
highp float u_camera_to_center_distance;
lowp float u_device_pixel_ratio;
lowp vec2 pad3_;
lowp float pad3_;
lowp vec2 pad4_;
};
layout (std140) uniform CircleEvaluatedPropsUBO {
Expand Down Expand Up @@ -145,7 +145,7 @@ lowp float stroke_opacity = u_stroke_opacity;
// This is a minimum blur distance that serves as a faux-antialiasing for
// the circle. since blur is a ratio of the circle's size and the intent is
// to keep the blur at roughly 1px, the two are inversely related.
lowp float antialiasblur = 1.0 / u_device_pixel_ratio / (radius + stroke_width);
lowp float antialiasblur = 1.0 / DEVICE_PIXEL_RATIO / (radius + stroke_width);
v_data = vec3(extrude.x, extrude.y, antialiasblur);
}
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/shaders/gl/drawable_line.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ layout (std140) uniform LineUBO {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
lowp float pad0;
};
layout (std140) uniform LinePropertiesUBO {
Expand Down Expand Up @@ -110,7 +110,7 @@ mediump float width = u_width;
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;
Expand Down Expand Up @@ -162,7 +162,7 @@ mediump float width = u_width;
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
lowp float pad0;
};
layout (std140) uniform LinePropertiesUBO {
Expand Down Expand Up @@ -219,7 +219,7 @@ lowp float opacity = u_opacity;
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_width2.t) or when fading out
// (v_width2.s)
float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale;
float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
fragColor = color * (alpha * opacity);
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/shaders/gl/drawable_line_basic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ layout (std140) uniform LineBasicUBO {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
lowp float pad0;
};
layout (std140) uniform LineBasicPropertiesUBO {
Expand All @@ -42,7 +42,7 @@ out highp float v_linesofar;
void main() {
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
vec2 a_extrude = a_data.xy - 128.0;
Expand Down Expand Up @@ -81,7 +81,7 @@ void main() {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
lowp float pad0;
};
layout (std140) uniform LineBasicPropertiesUBO {
Expand All @@ -103,7 +103,7 @@ void main() {
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_width2.t) or when fading out
// (v_width2.s)
float blur2 = (1.0 / u_device_pixel_ratio) * v_gamma_scale;
float blur2 = (1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale;
float alpha = clamp(min(dist + blur2, v_width - dist) / blur2, 0.0, 1.0);
fragColor = u_color * (alpha * u_opacity);
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/shaders/gl/drawable_line_gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ layout (std140) uniform LineGradientUBO {
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
lowp float pad0;
};
layout (std140) uniform LineGradientPropertiesUBO {
Expand Down Expand Up @@ -104,7 +104,7 @@ mediump float width = u_width;
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;
Expand Down Expand Up @@ -156,7 +156,7 @@ mediump float width = u_width;
highp mat4 u_matrix;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
lowp float pad0;
};
layout (std140) uniform LineGradientPropertiesUBO {
Expand Down Expand Up @@ -209,7 +209,7 @@ lowp float opacity = u_opacity;
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_width2.t) or when fading out
// (v_width2.s)
float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale;
float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
// For gradient lines, v_lineprogress is the ratio along the entire line,
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/shaders/gl/drawable_line_pattern.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ layout (std140) uniform LinePatternUBO {
highp vec2 u_texsize;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
highp float u_fade;
lowp float pad0;
highp float pad1;
};
Expand Down Expand Up @@ -133,7 +133,7 @@ mediump vec4 pattern_to = u_pattern_to;
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;
Expand Down Expand Up @@ -187,9 +187,9 @@ mediump vec4 pattern_to = u_pattern_to;
highp vec2 u_texsize;
highp vec2 u_units_to_pixels;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
highp float u_fade;
lowp float pad0;
highp float pad1;
};
Expand Down Expand Up @@ -278,7 +278,7 @@ lowp float opacity = u_opacity;
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_width2.t) or when fading out
// (v_width2.s)
float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale;
float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
float x_a = mod(v_linesofar / pattern_size_a.x, 1.0);
Expand Down
10 changes: 6 additions & 4 deletions include/mbgl/shaders/gl/drawable_line_sdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ layout (std140) uniform LineSDFUBO {
highp vec2 u_patternscale_a;
highp vec2 u_patternscale_b;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
highp float u_tex_y_a;
highp float u_tex_y_b;
highp float u_sdfgamma;
highp float u_mix;
lowp float pad0;
};
layout (std140) uniform LineSDFPropertiesUBO {
Expand Down Expand Up @@ -132,7 +133,7 @@ lowp float floorwidth = u_floorwidth;
// the distance over which the line edge fades out.
// Retina devices need a smaller distance to avoid aliasing.
float ANTIALIASING = 1.0 / u_device_pixel_ratio / 2.0;
float ANTIALIASING = 1.0 / DEVICE_PIXEL_RATIO / 2.0;
vec2 a_extrude = a_data.xy - 128.0;
float a_direction = mod(a_data.z, 4.0) - 1.0;
Expand Down Expand Up @@ -189,11 +190,12 @@ layout (std140) uniform LineSDFUBO {
highp vec2 u_patternscale_a;
highp vec2 u_patternscale_b;
mediump float u_ratio;
lowp float u_device_pixel_ratio;
highp float u_tex_y_a;
highp float u_tex_y_b;
highp float u_sdfgamma;
highp float u_mix;
lowp float pad0;
};
layout (std140) uniform LineSDFPropertiesUBO {
Expand Down Expand Up @@ -267,7 +269,7 @@ lowp float floorwidth = u_floorwidth;
// Calculate the antialiasing fade factor. This is either when fading in
// the line in case of an offset line (v_width2.t) or when fading out
// (v_width2.s)
float blur2 = (blur + 1.0 / u_device_pixel_ratio) * v_gamma_scale;
float blur2 = (blur + 1.0 / DEVICE_PIXEL_RATIO) * v_gamma_scale;
float alpha = clamp(min(dist - (v_width2.t - blur2), v_width2.s - dist) / blur2, 0.0, 1.0);
float sdfdist_a = texture(u_image, v_tex_a).a;
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/shaders/gl/drawable_symbol_icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ layout (std140) uniform SymbolDrawableUBO {
layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_camera_to_center_distance;
highp float u_device_pixel_ratio;
highp float u_aspect_ratio;
highp float pad0;
};
layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/shaders/gl/drawable_symbol_sdf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ layout (std140) uniform SymbolDrawableUBO {
layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_camera_to_center_distance;
highp float u_device_pixel_ratio;
highp float u_aspect_ratio;
highp float pad0;
};
layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down Expand Up @@ -209,8 +209,8 @@ layout (std140) uniform SymbolDrawableUBO {
layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_camera_to_center_distance;
highp float u_device_pixel_ratio;
highp float u_aspect_ratio;
highp float pad0;
};
layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down Expand Up @@ -280,7 +280,7 @@ lowp float halo_width = u_halo_width;
lowp float halo_blur = u_halo_blur;
#endif
float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
float EDGE_GAMMA = 0.105 / DEVICE_PIXEL_RATIO;
vec2 tex = v_data0.xy;
float gamma_scale = v_data1.x;
Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/shaders/gl/drawable_symbol_text_and_icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ layout (std140) uniform SymbolDrawableUBO {
layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_camera_to_center_distance;
highp float u_device_pixel_ratio;
highp float u_aspect_ratio;
highp float pad0;
};
layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down Expand Up @@ -212,8 +212,8 @@ layout (std140) uniform SymbolDrawableUBO {
layout (std140) uniform SymbolDynamicUBO {
highp float u_fade_change;
highp float u_camera_to_center_distance;
highp float u_device_pixel_ratio;
highp float u_aspect_ratio;
highp float pad0;
};
layout (std140) uniform SymbolDrawablePaintUBO {
Expand Down Expand Up @@ -299,7 +299,7 @@ lowp float halo_blur = u_halo_blur;
vec2 tex = v_data0.xy;
float EDGE_GAMMA = 0.105 / u_device_pixel_ratio;
float EDGE_GAMMA = 0.105 / DEVICE_PIXEL_RATIO;
float gamma_scale = v_data1.x;
float size = v_data1.y;
Expand Down
6 changes: 5 additions & 1 deletion include/mbgl/shaders/gl/shader_group_gl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class ShaderGroupGL final : public gfx::ShaderGroup {

// We could cache these by key here to avoid creating a string key each time, but we
// would need another mutex. We could also push string IDs down into `ShaderGroup`.
const std::string shaderName = getShaderName(name, propertyHash(propertiesAsUniforms));
std::size_t seed = 0;
mbgl::util::hash_combine(seed, propertyHash(propertiesAsUniforms));
mbgl::util::hash_combine(seed, programParameters.getDefinesHash());
const std::string shaderName = getShaderName(name, seed);

auto shader = get<gl::ShaderProgramGL>(shaderName);
if (shader) {
return shader;
Expand Down
7 changes: 3 additions & 4 deletions include/mbgl/shaders/line_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct alignas(16) LineUBO {
std::array<float, 4 * 4> matrix;
std::array<float, 2> units_to_pixels;
float ratio;
float device_pixel_ratio;
float pad;
};
static_assert(sizeof(LineUBO) % 16 == 0);

Expand Down Expand Up @@ -42,9 +42,8 @@ struct alignas(16) LinePatternUBO {
std::array<float, 2> texsize;
std::array<float, 2> units_to_pixels;
float ratio;
float device_pixel_ratio;
float fade;
float pad1;
float pad1, pad2;
};
static_assert(sizeof(LinePatternUBO) % 16 == 0);

Expand All @@ -64,11 +63,11 @@ struct alignas(16) LineSDFUBO {
std::array<float, 2> patternscale_a;
std::array<float, 2> patternscale_b;
float ratio;
float device_pixel_ratio;
float tex_y_a;
float tex_y_b;
float sdfgamma;
float mix;
float pad;
};
static_assert(sizeof(LineSDFUBO) % 16 == 0);

Expand Down
5 changes: 2 additions & 3 deletions include/mbgl/shaders/mtl/circle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ struct alignas(16) CircleDrawableUBO {
};
struct alignas(16) CirclePaintParamsUBO {
float camera_to_center_distance;
float device_pixel_ratio;
float pad1,pad2;
float pad1,pad2,pad3;
};
struct alignas(16) CircleEvaluatedPropsUBO {
float4 color;
Expand Down Expand Up @@ -157,7 +156,7 @@ FragmentStage vertex vertexMain(thread const VertexStage vertx [[stage_in]],
// This is a minimum blur distance that serves as a faux-antialiasing for
// the circle. since blur is a ratio of the circle's size and the intent is
// to keep the blur at roughly 1px, the two are inversely related.
const half antialiasblur = 1.0 / params.device_pixel_ratio / (radius + stroke_width);
const half antialiasblur = 1.0 / DEVICE_PIXEL_RATIO / (radius + stroke_width);
return {
.position = position,
Expand Down
8 changes: 4 additions & 4 deletions include/mbgl/shaders/mtl/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ struct alignas(16) LineUBO {
float4x4 matrix;
float2 units_to_pixels;
float ratio;
float device_pixel_ratio;
float pad;
};
struct alignas(16) LineBasicUBO {
float4x4 matrix;
float2 units_to_pixels;
float ratio;
float device_pixel_ratio;
float pad;
};
struct alignas(16) LineGradientUBO {
float4x4 matrix;
float2 units_to_pixels;
float ratio;
float device_pixel_ratio;
float pad;
};
struct alignas(16) LinePropertiesUBO {
Expand Down Expand Up @@ -159,8 +159,8 @@ static_assert(sizeof(SymbolDrawableUBO) == 14 * 16, "unexpected padding");
struct alignas(16) SymbolDynamicUBO {
float fade_change;
float camera_to_center_distance;
float device_pixel_ratio;
float aspect_ratio;
float pad;
};
static_assert(sizeof(SymbolDynamicUBO) == 16, "unexpected padding");
Expand Down
Loading

0 comments on commit ea73a57

Please sign in to comment.