Skip to content

Commit

Permalink
GPU Expressions (#2202)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSylvester authored May 17, 2024
1 parent b812e97 commit 33a1e16
Show file tree
Hide file tree
Showing 64 changed files with 2,336 additions and 318 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/linux-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ jobs:
run: xvfb-run -a build/mbgl-render-test-runner --manifestPath=metrics/linux-${{ matrix.renderer }}.json

- name: Upload render test result
if: always() && steps.render_test.outcome == 'failure' && matrix.renderer == 'drawable'
if: always() && steps.render_test.outcome == 'failure'
uses: actions/upload-artifact@v4
with:
name: render-test-result
name: render-test-result-${{ matrix.renderer }}
path: |
metrics/linux-drawable.html
metrics/linux-${{ matrix.renderer }}.html
# expression tests

Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/drawable_tweaker.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/drawable_atlases_tweaker.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/drawable_custom_layer_host_tweaker.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/gpu_expression.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/uniform_block.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/uniform_buffer.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/gfx/vertex_attribute.hpp
Expand Down Expand Up @@ -177,6 +178,7 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_builder_impl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_atlases_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/drawable_custom_layer_host_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/gpu_expression.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/hillshade_prepare_drawable_data.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/image_drawable_data.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gfx/line_drawable_data.hpp
Expand Down
2 changes: 2 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ MLN_DRAWABLES_SOURCE = [
"src/mbgl/gfx/drawable_builder_impl.cpp",
"src/mbgl/gfx/drawable_atlases_tweaker.cpp",
"src/mbgl/gfx/drawable_custom_layer_host_tweaker.cpp",
"src/mbgl/gfx/gpu_expression.cpp",
"src/mbgl/gfx/hillshade_prepare_drawable_data.hpp",
"src/mbgl/gfx/image_drawable_data.hpp",
"src/mbgl/gfx/line_drawable_data.hpp",
Expand Down Expand Up @@ -976,6 +977,7 @@ MLN_DRAWABLES_HEADERS = [
"include/mbgl/gfx/drawable_tweaker.hpp",
"include/mbgl/gfx/drawable_atlases_tweaker.hpp",
"include/mbgl/gfx/drawable_custom_layer_host_tweaker.hpp",
"include/mbgl/gfx/gpu_expression.hpp",
"include/mbgl/gfx/uniform_block.hpp",
"include/mbgl/gfx/uniform_buffer.hpp",
"include/mbgl/gfx/vertex_attribute.hpp",
Expand Down
5 changes: 5 additions & 0 deletions include/mbgl/gfx/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class Backend {

static Type GetType() { return Value(DefaultType); }

static bool getEnableGPUExpressionEval() { return enableGPUExpressionEval; }
static void setEnableGPUExpressionEval(bool value) { enableGPUExpressionEval = value; }

template <typename T, typename... Args>
static std::unique_ptr<T> Create(Args... args) {
#if MLN_RENDER_BACKEND_METAL
Expand All @@ -48,6 +51,8 @@ class Backend {
static const Type type = value;
return type;
}

static bool enableGPUExpressionEval;
};

} // namespace gfx
Expand Down
108 changes: 108 additions & 0 deletions include/mbgl/gfx/gpu_expression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#pragma once

#include <mbgl/util/color.hpp>
#include <mbgl/util/variant.hpp>

#include <optional>

#if MLN_DRAWABLE_RENDERER

namespace mbgl {
namespace style {
namespace expression {
class Expression;
class Interpolate;
class Step;
} // namespace expression
using ZoomCurvePtr = variant<std::nullptr_t, const expression::Interpolate*, const expression::Step*>;
} // namespace style
namespace gfx {

enum class GPUInterpType : std::uint16_t {
Step,
Linear,
Exponential,
Bezier
};
enum class GPUOutputType : std::uint16_t {
Float,
Color,
};
enum class GPUOptions : std::uint16_t {
None = 0,
IntegerZoom = 1 << 0,
Transitioning = 1 << 1,
};

class GPUExpression;
using UniqueGPUExpression = std::unique_ptr<GPUExpression>;

class alignas(16) GPUExpression {
public:
static constexpr std::size_t maxStops = 16;

GPUExpression() = delete;
GPUExpression(GPUOutputType type, uint16_t count)
: outputType(type),
stopCount(count) {}
GPUExpression(GPUExpression&&) = default;
GPUExpression(const GPUExpression&) = default;
GPUExpression(const GPUExpression* ptr)
: GPUExpression(ptr ? *ptr : empty) {}
GPUExpression& operator=(GPUExpression&&) = delete;
GPUExpression& operator=(const GPUExpression&) = delete;

/* 0 */ const GPUOutputType outputType;
/* 2 */ const std::uint16_t stopCount;
/* 4 */ GPUOptions options;
/* 6 */ GPUInterpType interpolation;

/* 8 */ union InterpOptions {
struct Exponential {
float base;
} exponential;

struct Bezier {
float x1;
float y1;
float x2;
float y2;
} bezier;
} interpOptions;

/* 24 */ float inputs[maxStops];

/* 24 + (4 * maxStops) = 88 */ union Stops {
float floats[maxStops];
float colors[2 * maxStops];
} stops;

static UniqueGPUExpression create(GPUOutputType, std::uint16_t stopCount);
static UniqueGPUExpression create(const style::expression::Expression&, const style::ZoomCurvePtr&, bool intZoom);

float evaluateFloat(const float zoom) const;
Color evaluateColor(const float zoom) const;

template <typename T>
auto evaluate(const float zoom) const;

Color getColor(std::size_t index) const;

static const GPUExpression empty;
};
static_assert(sizeof(GPUExpression) == 32 + (4 + 8) * GPUExpression::maxStops);
static_assert(sizeof(GPUExpression) % 16 == 0);

template <>
inline auto GPUExpression::evaluate<Color>(const float zoom) const {
return evaluateColor(zoom);
}
template <>
inline auto GPUExpression::evaluate<float>(const float zoom) const {
return evaluateFloat(zoom);
}

} // namespace gfx
} // namespace mbgl

#endif
3 changes: 2 additions & 1 deletion include/mbgl/shaders/layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ struct alignas(16) GlobalPaintParamsUBO {
float symbol_fade_change;
float aspect_ratio;
float pixel_ratio;
float pad1, pad2;
float zoom;
float pad1;
};
static_assert(sizeof(GlobalPaintParamsUBO) == 3 * 16);

Expand Down
32 changes: 31 additions & 1 deletion include/mbgl/shaders/line_layer_ubo.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
#pragma once

#include <mbgl/shaders/layer_ubo.hpp>
#include <mbgl/style/property_expression.hpp>
#include <mbgl/util/bitmask_operations.hpp>

#if MLN_DRAWABLE_RENDERER
#include <mbgl/gfx/gpu_expression.hpp>
#endif // MLN_DRAWABLE_RENDERER

namespace mbgl {
namespace shaders {

enum class LineExpressionMask : uint32_t {
None = 0,
Color = 1 << 0,
Opacity = 1 << 1,
Blur = 1 << 2,
Width = 1 << 3,
GapWidth = 1 << 4,
FloorWidth = 1 << 5,
Offset = 1 << 6,
};

//
// Line

Expand All @@ -26,6 +43,17 @@ struct alignas(16) LineInterpolationUBO {
};
static_assert(sizeof(LineInterpolationUBO) % 16 == 0);

struct alignas(16) LineExpressionUBO {
gfx::GPUExpression color;
gfx::GPUExpression blur;
gfx::GPUExpression opacity;
gfx::GPUExpression gapwidth;
gfx::GPUExpression offset;
gfx::GPUExpression width;
gfx::GPUExpression floorWidth;
};
static_assert(sizeof(LineExpressionUBO) % 16 == 0);

//
// Line gradient

Expand Down Expand Up @@ -110,7 +138,8 @@ struct alignas(16) LineEvaluatedPropsUBO {
float offset;
float width;
float floorwidth;
float pad1, pad2;
LineExpressionMask expressionMask;
float pad1;
};
static_assert(sizeof(LineEvaluatedPropsUBO) % 16 == 0);

Expand All @@ -119,6 +148,7 @@ enum {
idLineInterpolationUBO,
idLineTilePropertiesUBO,
idLineEvaluatedPropsUBO,
idLineExpressionUBO,
lineUBOCount
};

Expand Down
Loading

0 comments on commit 33a1e16

Please sign in to comment.