Skip to content

Commit

Permalink
move GPUExpression to its own files
Browse files Browse the repository at this point in the history
rebase
  • Loading branch information
TimSylvester committed Apr 3, 2024
1 parent 0e4b252 commit 10a4b7c
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 267 deletions.
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 @@ -921,6 +921,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 @@ -975,6 +976,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
113 changes: 113 additions & 0 deletions include/mbgl/gfx/gpu_expression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#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<const GPUExpression>;
using MutableUniqueGPUExpression = std::unique_ptr<GPUExpression>;

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

GPUExpression() = delete;
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 MutableUniqueGPUExpression create(GPUOutputType, std::uint16_t stopCount);
static MutableUniqueGPUExpression 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;

private:
GPUExpression(GPUOutputType type, uint16_t count)
: outputType(type),
stopCount(count) {}
};
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
18 changes: 11 additions & 7 deletions include/mbgl/shaders/line_layer_ubo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#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 {

Expand Down Expand Up @@ -62,13 +66,13 @@ struct alignas(16) LineInterpolationUBO {
static_assert(sizeof(LineInterpolationUBO) % 16 == 0);

struct alignas(16) LineExpressionUBO {
style::GPUExpression color;
style::GPUExpression blur;
style::GPUExpression opacity;
style::GPUExpression gapwidth;
style::GPUExpression offset;
style::GPUExpression width;
style::GPUExpression floorWidth;
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);

Expand Down
98 changes: 12 additions & 86 deletions include/mbgl/style/property_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,95 +8,19 @@
#include <mbgl/util/bitmask_operations.hpp>
#include <mbgl/util/range.hpp>

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

#include <optional>

namespace mbgl {
namespace style {

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,
};

struct GPUExpression;
namespace gfx {
class GPUExpression;
using UniqueGPUExpression = std::unique_ptr<const GPUExpression>;
using MutableUniqueGPUExpression = std::unique_ptr<GPUExpression>;

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

GPUExpression() = delete;
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;
} // namespace gfx

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 MutableUniqueGPUExpression create(GPUOutputType, std::uint16_t stopCount);

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;

private:
GPUExpression(GPUOutputType type, uint16_t count)
: outputType(type),
stopCount(count) {}
};
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 style {

class PropertyExpressionBase {
public:
Expand Down Expand Up @@ -129,15 +53,17 @@ class PropertyExpressionBase {
std::shared_ptr<const Expression> getSharedExpression() const noexcept;

/// Build a cached GPU representation of the expression, with the same lifetime as this object.
const GPUExpression* getGPUExpression(bool intZoom);
const gfx::GPUExpression* getGPUExpression(bool intZoom);

Dependency getDependencies() const noexcept { return expression ? expression->dependencies : Dependency::None; }

const ZoomCurvePtr& getZoomCurve() const { return zoomCurve; }

protected:
std::shared_ptr<const Expression> expression;
UniqueGPUExpression gpuExpression;
#if MLN_DRAWABLE_RENDERER
gfx::UniqueGPUExpression gpuExpression;
#endif // MLN_DRAWABLE_RENDERER

ZoomCurvePtr zoomCurve;

Expand Down
Loading

0 comments on commit 10a4b7c

Please sign in to comment.