Skip to content

Commit

Permalink
Move uniform updates to tweaker
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-cojocaru committed Sep 24, 2024
1 parent 31b2207 commit 4744604
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ if(MLN_DRAWABLE_RENDERER)
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/hillshade_prepare_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/line_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/line_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/location_indicator_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/location_indicator_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/raster_layer_tweaker.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/raster_layer_tweaker.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/renderer/layers/symbol_layer_tweaker.cpp
Expand Down
60 changes: 60 additions & 0 deletions src/mbgl/renderer/layers/location_indicator_layer_tweaker.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <mbgl/renderer/layers/location_indicator_layer_tweaker.hpp>

#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/layers/render_location_indicator_layer.hpp>
#include <mbgl/style/layers/location_indicator_layer_properties.hpp>

namespace mbgl {

void LocationIndicatorLayerTweaker::execute(LayerGroupBase& layerGroup, const PaintParameters& params) {

if (layerGroup.empty()) {
return;
}

const auto& props = static_cast<const style::LocationIndicatorLayerProperties&>(*evaluatedProperties);

const shaders::CommonUBO quadUBO = {/* .matrix */ util::cast<float>(projectionPuck),
/* .color */ Color::black()};

visitLayerGroupDrawables(layerGroup, [&](gfx::Drawable& drawable) {
auto& drawableUniforms = drawable.mutableUniformBuffers();

if (!drawable.getEnabled()) {
return;
}

switch (static_cast<RenderLocationIndicatorLayer::LocationIndicatorComponentType>(drawable.getType())) {
case RenderLocationIndicatorLayer::LocationIndicatorComponentType::Circle: {
shaders::CommonUBO circleUBO = {/* .matrix */ util::cast<float>(projectionCircle),
/* .color */ props.evaluated.get<style::AccuracyRadiusColor>()};

drawableUniforms.createOrUpdate(shaders::idCommonUBO, &circleUBO, params.context);
break;
}

case RenderLocationIndicatorLayer::LocationIndicatorComponentType::CircleOutline: {
shaders::CommonUBO circleUBO = {/* .matrix */ util::cast<float>(projectionCircle),
/* .color */ props.evaluated.get<style::AccuracyRadiusBorderColor>()};

drawableUniforms.createOrUpdate(shaders::idCommonUBO, &circleUBO, params.context);
break;
}

case RenderLocationIndicatorLayer::LocationIndicatorComponentType::PuckShadow:
[[fallthrough]];
case RenderLocationIndicatorLayer::LocationIndicatorComponentType::Puck:
[[fallthrough]];
case RenderLocationIndicatorLayer::LocationIndicatorComponentType::PuckHat:
drawableUniforms.createOrUpdate(shaders::idCommonUBO, &quadUBO, params.context);
break;

default:
assert(false);
break;
}
});
}

} // namespace mbgl
33 changes: 33 additions & 0 deletions src/mbgl/renderer/layers/location_indicator_layer_tweaker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <mbgl/renderer/layer_tweaker.hpp>
#include <mbgl/gfx/uniform_buffer.hpp>

#include <string>

namespace mbgl {

/**
Location indicator layer specific tweaker
*/
class LocationIndicatorLayerTweaker : public LayerTweaker {
public:
LocationIndicatorLayerTweaker(std::string id_,
Immutable<style::LayerProperties> properties,
const mbgl::mat4& projectionCircle_,
const mbgl::mat4& projectionPuck_)
: LayerTweaker(std::move(id_), properties),
projectionCircle(projectionCircle_),
projectionPuck(projectionPuck_) {}

public:
~LocationIndicatorLayerTweaker() override = default;

void execute(LayerGroupBase&, const PaintParameters& params) override;

private:
const mbgl::mat4& projectionCircle;
const mbgl::mat4& projectionPuck;
};

} // namespace mbgl
38 changes: 16 additions & 22 deletions src/mbgl/renderer/layers/render_location_indicator_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include <mbgl/gfx/drawable_tweaker.hpp>
#include <mbgl/gfx/texture2d.hpp>
#include <mbgl/shaders/shader_defines.hpp>
#include <mbgl/renderer/layers/location_indicator_layer_tweaker.hpp>

#endif

Expand Down Expand Up @@ -1076,6 +1077,12 @@ void RenderLocationIndicatorLayer::update(gfx::ShaderRegistry& shaders,
}
}

if (!layerTweaker) {
layerTweaker = std::make_shared<LocationIndicatorLayerTweaker>(
getID(), evaluatedProperties, renderImpl->getProjectionCircle(), renderImpl->getProjectionPuck());
layerGroup->addLayerTweaker(layerTweaker);
}

auto* localLayerGroup = static_cast<LayerGroup*>(layerGroup.get());

if (localLayerGroup->getDrawableCount() == 0) {
Expand Down Expand Up @@ -1110,9 +1117,11 @@ void RenderLocationIndicatorLayer::update(gfx::ShaderRegistry& shaders,
drawable.setIndexData(indices.vector(), std::move(drawSegments));
};

const auto createQuadDrawable = [&](RenderLocationIndicatorImpl::QuadDrawableInfo& drawableInfo,
std::string&& name) {
const auto createQuadDrawable = [&](RenderLocationIndicatorImpl::QuadDrawableInfo & drawableInfo,
std::string&& name,
LocationIndicatorComponentType type) {
auto& drawable = builder->getCurrentDrawable(true);
drawable->setType(static_cast<uint8_t>(type));

drawable->setName(name);
drawable->setRenderPass(drawPasses);
Expand Down Expand Up @@ -1155,6 +1164,7 @@ void RenderLocationIndicatorLayer::update(gfx::ShaderRegistry& shaders,

{
auto& drawable = getCircleDrawable("locationAccuracyCircle", vertexAttrib);
drawable->setType(static_cast<uint8_t>(LocationIndicatorComponentType::Circle));

std::vector<gfx::Drawable::UniqueDrawSegment> drawSegments;
std::vector<uint16_t> indices;
Expand All @@ -1176,6 +1186,7 @@ void RenderLocationIndicatorLayer::update(gfx::ShaderRegistry& shaders,

{
auto& drawable = getCircleDrawable("locationAccuracyCircleOutline", vertexAttrib);
drawable->setType(static_cast<uint8_t>(LocationIndicatorComponentType::CircleOutline));

std::vector<gfx::Drawable::UniqueDrawSegment> drawSegments;
std::vector<uint16_t> indices;
Expand All @@ -1196,9 +1207,9 @@ void RenderLocationIndicatorLayer::update(gfx::ShaderRegistry& shaders,
};

createCircleDrawable();
createQuadDrawable(renderImpl->shadowDrawableInfo, "locationShadow");
createQuadDrawable(renderImpl->puckDrawableInfo, "locationPuck");
createQuadDrawable(renderImpl->hatDrawableInfo, "locationPuckHat");
createQuadDrawable(renderImpl->shadowDrawableInfo, "locationShadow", LocationIndicatorComponentType::PuckShadow);
createQuadDrawable(renderImpl->puckDrawableInfo, "locationPuck", LocationIndicatorComponentType::Puck);
createQuadDrawable(renderImpl->hatDrawableInfo, "locationPuckHat", LocationIndicatorComponentType::PuckHat);
};

const auto updateCircleDrawable = [&]() {
Expand Down Expand Up @@ -1233,28 +1244,11 @@ void RenderLocationIndicatorLayer::update(gfx::ShaderRegistry& shaders,
verts, 0, 0, sizeof(RenderLocationIndicatorImpl::vec2), gfx::AttributeDataType::Float2);
}
}

// update uniforms
shaders::CommonUBO commonUBO = {/* .matrix */ util::cast<float>(renderImpl->getProjectionCircle()),
/* .color */ renderImpl->parameters.errorRadiusColor};

auto& drawableUniforms = circleDrawable.mutableUniformBuffers();
drawableUniforms.createOrUpdate(shaders::idCommonUBO, &commonUBO, context);

commonUBO.color = renderImpl->parameters.errorRadiusBorderColor;
auto& outlineDrawableUniforms = circleOutlineDrawable.mutableUniformBuffers();
outlineDrawableUniforms.createOrUpdate(shaders::idCommonUBO, &commonUBO, context);
};

const shaders::CommonUBO ubo = {/* .matrix */ util::cast<float>(renderImpl->getProjectionPuck()),
/* .color */ Color::black()};

const auto updateQuadDrawable = [&](RenderLocationIndicatorImpl::QuadDrawableInfo& info) {
auto& drawable = info.getDrawable();

auto& drawableUniforms = drawable.mutableUniformBuffers();
drawableUniforms.createOrUpdate(shaders::idCommonUBO, &ubo, context);

if (info.dirty) {
auto vertexAttrs = drawable.getVertexAttributes();

Expand Down
10 changes: 10 additions & 0 deletions src/mbgl/renderer/layers/render_location_indicator_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ namespace mbgl {
class RenderLocationIndicatorImpl;
class RenderLocationIndicatorLayer final : public RenderLayer {
public:

enum class LocationIndicatorComponentType : uint8_t {
Circle,
CircleOutline,
PuckShadow,
Puck,
PuckHat,
Undefined = 255
};

explicit RenderLocationIndicatorLayer(Immutable<style::LocationIndicatorLayer::Impl>);
~RenderLocationIndicatorLayer() override;

Expand Down

0 comments on commit 4744604

Please sign in to comment.