Drawable Renderer (#1570), minor work on fill pattern

This commit is contained in:
Tim Sylvester
2023-06-05 11:45:42 -07:00
committed by Bart Louwers
parent dc5f6c4d69
commit 060a32f3f2
2 changed files with 86 additions and 73 deletions

View File

@@ -7,6 +7,7 @@
#include <mbgl/renderer/layer_group.hpp>
#include <mbgl/renderer/render_tree.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/paint_property_binder.hpp>
#include <mbgl/style/layers/fill_layer_properties.hpp>
#include <mbgl/util/convert.hpp>
@@ -38,14 +39,21 @@ struct alignas(16) FillDrawableUBO {
/* 176 */ std::array<float, 4> pattern_from;
/* 208 */ std::array<float, 4> pattern_to;
// Pattern texture
/* ? */ // Drawable::TextureAttachment? image;
/* */ // std::array<float, 3> padding;
/* 208 */
};
static_assert(sizeof(FillDrawableUBO) == 208);
// move to utils, or does it already exist somewhere?
template<typename T, std::size_t N, std::size_t M>
auto concat(const std::array<T, N>& a1, const std::array<T, M>& a2)
{
std::array<T, N+M> result;
std::copy (a1.cbegin(), a1.cend(), result.begin());
std::copy (a2.cbegin(), a2.cend(), result.begin() + N);
return result;
}
void FillLayerTweaker::execute(LayerGroup& layerGroup,
const RenderTree& renderTree,
const PaintParameters& parameters) {
@@ -75,6 +83,24 @@ void FillLayerTweaker::execute(LayerGroup& layerGroup,
const UnwrappedTileID tileID = drawable.getTileID()->toUnwrapped();
const auto& colorProperty = evaluated.get<FillColor>();
const auto color = (colorProperty.isConstant() && colorProperty.constant()) ? *colorProperty.constant() : Color(0.0f, 0.0f, 0.0f, 0.0f);
const auto& outlineColorProperty = evaluated.get<FillOutlineColor>();
const auto outlineColor = (outlineColorProperty.isConstant() && outlineColorProperty.constant()) ? *outlineColorProperty.constant() : Color(0.0f, 0.0f, 0.0f, 0.0f);
const auto& opacityProperty = evaluated.get<FillOpacity>();
const auto opacity = (opacityProperty.isConstant() && opacityProperty.constant()) ? *opacityProperty.constant() : 1.0f;
const auto& fillPatternProperty = evaluated.get<FillPattern>();
if (fillPatternProperty.isConstant() && fillPatternProperty.constant()) {
Faded<style::expression::Image> pattern = *fillPatternProperty.constant();
const auto& patternFrom = pattern.from;
const auto& patternTo = pattern.to;
__unused const auto& idFrom = patternFrom.id();
__unused const auto& idTo = patternTo.id();
}
const auto& translation = evaluated.get<FillTranslate>();
const auto anchor = evaluated.get<FillTranslateAnchor>();
constexpr bool inViewportPixelUnits = false; // from RenderTile::translatedMatrix
@@ -93,26 +119,29 @@ void FillLayerTweaker::execute(LayerGroup& layerGroup,
const int32_t pixelY = tileSizeAtNearestZoom * tileID.canonical.y;
const auto pixelRatio = parameters.pixelRatio;
const auto& textures = drawable.getTextures();
const auto hasTex = !textures.empty() && textures[0].texture;
const Size textureSize = hasTex ? textures[0].texture->getSize() : Size(0,0);
const FillDrawableUBO drawableUBO = {
/*.matrix=*/util::cast<float>(matrix),
/*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale},
/*.world=*/{(float)renderableSize.width, (float)renderableSize.height},
/*.pixel_coord_upper=*/{static_cast<float>(pixelX >> 16), static_cast<float>(pixelY >> 16)},
/*.pixel_coord_lower=*/{static_cast<float>(pixelX & 0xFFFF), static_cast<float>(pixelY & 0xFFFF)},
/*.texsize=*/{0.0f, 0.0f}, // tile.getIconAtlasTexture().size
/*.texsize=*/ { static_cast<float>(textureSize.width), static_cast<float>(textureSize.height) },
/*.fade=*/crossfade.t,
/*.color_t=*/0.0f,
/*.opacity_t=*/0.0f,
/*.outline_color_t=*/0.0f,
/*.pattern_from_t=*/0.0f,
/*.pattern_to_t=*/0.0f,
/*.color=*/{0.0f},
/*.opacity=*/{0.0f},
/*.color_t=*/crossfade.t,
/*.opacity_t=*/crossfade.t,
/*.outline_color_t=*/crossfade.t,
/*.pattern_from_t=*/crossfade.t,
/*.pattern_to_t=*/crossfade.t,
/*.color=*/ attributeValue(color),
/*.opacity=*/ {opacity, opacity},
/*.outline_color_pad=*/{0.0f},
/*.outline_color=*/{0.0f},
/*.outline_color=*/ util::cast<float>(outlineColor.toArray()),
/*.pattern_from=*/{0.0f},
/*.pattern_to=*/{0.0f},
/*.image=*/ // TextureAttachment(tile.getIconAtlasTexture().getResource(), Linear)
};
if (auto& ubo = drawable.mutableUniformBuffers().get("FillDrawableUBO")) {

View File

@@ -326,6 +326,8 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
std::vector<gfx::DrawablePtr> newTiles;
gfx::VertexAttributeArray fillVertexAttrs;
gfx::VertexAttributeArray outlineVertexAttrs;
gfx::VertexAttributeArray patternVertexAttrs;
gfx::VertexAttributeArray patternOutlineVertexAttrs;
const auto finish = [&](gfx::DrawableBuilder& builder, RenderPass pass, const OverscaledTileID& tileID) {
builder.flush();
@@ -352,7 +354,7 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
}
});
// parameters.renderTileClippingMasks(renderTiles);
constexpr auto samplerLocation = 0;
for (const RenderTile& tile : *renderTiles) {
const auto& tileID = tile.getOverscaledTileID();
@@ -507,9 +509,41 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
Faded<expression::Image>{"", ""});
const auto patternPosA = tile.getPattern(fillPatternValue.from.id());
const auto patternPosB = tile.getPattern(fillPatternValue.to.id());
paintPropertyBinders.setPatternParameters(patternPosA, patternPosB, crossfade);
// if (samplerLocation < 0) curShader->getSamplerLocation("u_image");
if (auto& binder = paintPropertyBinders.get<FillOpacity>()) {
const auto count = binder->getVertexCount();
for (auto& attrs :
{std::reference_wrapper(patternVertexAttrs), std::reference_wrapper(patternOutlineVertexAttrs)}) {
if (auto& attr = attrs.get().getOrAdd("a_opacity")) {
for (std::size_t i = 0; i < count; ++i) {
const auto& opacity =
static_cast<const gfx::detail::VertexType<gfx::AttributeType<float, 1>>*>(
binder->getVertexValue(i))
->a1;
attr->set<gfx::VertexAttribute::float2>(i, {opacity[0], opacity[0]});
}
}
}
}
// if (auto& binder = paintPropertyBinders.get<FillOpacity>()) {
// const auto count = binder->getVertexCount();
// for (auto& attrs :
// {std::reference_wrapper(patternVertexAttrs), std::reference_wrapper(patternOutlineVertexAttrs)}) {
// if (auto& attr = attrs.get().getOrAdd("a_pattern_from")) {
// for (std::size_t i = 0; i < count; ++i) {
// const auto& opacity =
// static_cast<const gfx::detail::VertexType<gfx::AttributeType<float, 4>>*>(
// binder->getVertexValue(i))
// ->a1;
// attr->set<gfx::VertexAttribute::float2>(i, {});
// }
// }
// }
// }
if (!patternBuilder && patternShader) {
patternBuilder = context.createDrawableBuilder("fill-pattern");
patternBuilder->setShader(fillShader);
@@ -517,7 +551,9 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
patternBuilder->setDepthType(gfx::DepthMaskType::ReadWrite);
patternBuilder->setCullFaceMode(gfx::CullFaceMode::disabled());
patternBuilder->setSubLayerIndex(1);
// patternBuilder->setTexture(tile.getIconAtlasTexture(), 0);
if (auto& tex = tile.getIconAtlasTexture()) {
patternBuilder->setTexture(tex, samplerLocation);
}
}
if (doOutline && !outlinePatternBuilder && outlinePatternShader) {
outlinePatternBuilder = context.createDrawableBuilder("fill-outline-pattern");
@@ -527,21 +563,18 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
outlinePatternBuilder->setDepthType(gfx::DepthMaskType::ReadOnly);
outlinePatternBuilder->setCullFaceMode(gfx::CullFaceMode::disabled());
outlinePatternBuilder->setSubLayerIndex(2);
// outlinePatternBuilder->setTexture(tile.getIconAtlasTexture(), 0);
if (auto& tex = tile.getIconAtlasTexture()) {
patternBuilder->setTexture(tex, samplerLocation);
}
}
// parameters.stencilModeForClipping(tile.id),
// parameters.colorModeForRenderPass(),
// gfx::CullFaceMode::disabled(),
// std::forward<decltype(textureBindings)>(textureBindings),
if (patternBuilder) {
patternBuilder->setRenderPass(renderPass);
// patternBuilder->setVertexAttributes(fillVertexAttrs);
patternBuilder->setVertexAttributes(fillVertexAttrs);
}
if (outlinePatternBuilder) {
outlinePatternBuilder->setRenderPass(renderPass);
// outlinePatternBuilder->setVertexAttributes(outlineVertexAttrs);
outlinePatternBuilder->setVertexAttributes(outlineVertexAttrs);
}
if (patternBuilder) {
@@ -563,56 +596,7 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders,
finish(*outlinePatternBuilder, renderPass, tileID);
}
// auto draw = [&](auto& programInstance,
// const auto& drawMode,
// const auto& depthMode,
// const auto& indexBuffer,
// const auto& segments,
// auto&& textureBindings) {
// const auto& paintPropertyBinders = bucket.paintPropertyBinders.at(getID());
// paintPropertyBinders.setPatternParameters(patternPosA, patternPosB, crossfade);
//
// const auto allUniformValues = programInstance.computeAllUniformValues(
// FillPatternProgram::layoutUniformValues(
// parameters.backend.getDefaultRenderable().getSize(),
// tile.getIconAtlasTexture().size,
// crossfade,
// tile.id,
// parameters.state,
// parameters.pixelRatio),
// paintPropertyBinders,
// evaluated,
// static_cast<float>(parameters.state.getZoom()));
// const auto allAttributeBindings = programInstance.computeAllAttributeBindings(
// *bucket.vertexBuffer, paintPropertyBinders, evaluated);
//
// checkRenderability(parameters, programInstance.activeBindingCount(allAttributeBindings));
//
// };
//
// if (bucket.triangleIndexBuffer) {
// draw(*fillPatternProgram,
// gfx::Triangles(),
// parameters.depthModeForSublayer(1, gfx::DepthMaskType::ReadWrite),
// *bucket.triangleIndexBuffer,
// bucket.triangleSegments,
// FillPatternProgram::TextureBindings{
// textures::image::Value{tile.getIconAtlasTexture().getResource(),
// gfx::TextureFilterType::Linear},
// });
// }
// if (evaluated.get<FillAntialias>() && unevaluated.get<FillOutlineColor>().isUndefined()) {
// draw(*fillOutlinePatternProgram,
// gfx::Lines{2.0f},
// parameters.depthModeForSublayer(2, gfx::DepthMaskType::ReadOnly),
// *bucket.lineIndexBuffer,
// bucket.lineSegments,
// FillOutlinePatternProgram::TextureBindings{
// textures::image::Value{tile.getIconAtlasTexture().getResource(),
// gfx::TextureFilterType::Linear},
// });
// }
//}
}
}
}