From 060a32f3f210c56cb3a624c04e94b55ea3aeef71 Mon Sep 17 00:00:00 2001 From: Tim Sylvester Date: Mon, 5 Jun 2023 11:45:42 -0700 Subject: [PATCH] Drawable Renderer (#1570), minor work on fill pattern --- .../renderer/layers/fill_layer_tweaker.cpp | 55 ++++++--- .../renderer/layers/render_fill_layer.cpp | 104 ++++++++---------- 2 files changed, 86 insertions(+), 73 deletions(-) diff --git a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp index f5d56de02..be236ba1f 100644 --- a/src/mbgl/renderer/layers/fill_layer_tweaker.cpp +++ b/src/mbgl/renderer/layers/fill_layer_tweaker.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -38,14 +39,21 @@ struct alignas(16) FillDrawableUBO { /* 176 */ std::array pattern_from; /* 208 */ std::array pattern_to; - // Pattern texture - /* ? */ // Drawable::TextureAttachment? image; - /* */ // std::array padding; /* 208 */ }; static_assert(sizeof(FillDrawableUBO) == 208); +// move to utils, or does it already exist somewhere? +template +auto concat(const std::array& a1, const std::array& a2) +{ + std::array 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(); + const auto color = (colorProperty.isConstant() && colorProperty.constant()) ? *colorProperty.constant() : Color(0.0f, 0.0f, 0.0f, 0.0f); + + const auto& outlineColorProperty = evaluated.get(); + const auto outlineColor = (outlineColorProperty.isConstant() && outlineColorProperty.constant()) ? *outlineColorProperty.constant() : Color(0.0f, 0.0f, 0.0f, 0.0f); + + const auto& opacityProperty = evaluated.get(); + const auto opacity = (opacityProperty.isConstant() && opacityProperty.constant()) ? *opacityProperty.constant() : 1.0f; + + const auto& fillPatternProperty = evaluated.get(); + if (fillPatternProperty.isConstant() && fillPatternProperty.constant()) { + Faded 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(); const auto anchor = evaluated.get(); 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(matrix), /*.scale=*/{pixelRatio, tileRatio, crossfade.fromScale, crossfade.toScale}, /*.world=*/{(float)renderableSize.width, (float)renderableSize.height}, /*.pixel_coord_upper=*/{static_cast(pixelX >> 16), static_cast(pixelY >> 16)}, /*.pixel_coord_lower=*/{static_cast(pixelX & 0xFFFF), static_cast(pixelY & 0xFFFF)}, - /*.texsize=*/{0.0f, 0.0f}, // tile.getIconAtlasTexture().size + /*.texsize=*/ { static_cast(textureSize.width), static_cast(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(outlineColor.toArray()), /*.pattern_from=*/{0.0f}, /*.pattern_to=*/{0.0f}, - /*.image=*/ // TextureAttachment(tile.getIconAtlasTexture().getResource(), Linear) }; if (auto& ubo = drawable.mutableUniformBuffers().get("FillDrawableUBO")) { diff --git a/src/mbgl/renderer/layers/render_fill_layer.cpp b/src/mbgl/renderer/layers/render_fill_layer.cpp index 3df3719a5..93eeed57b 100644 --- a/src/mbgl/renderer/layers/render_fill_layer.cpp +++ b/src/mbgl/renderer/layers/render_fill_layer.cpp @@ -326,6 +326,8 @@ void RenderFillLayer::update(gfx::ShaderRegistry& shaders, std::vector 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{"", ""}); 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()) { + 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>*>( + binder->getVertexValue(i)) + ->a1; + attr->set(i, {opacity[0], opacity[0]}); + } + } + } + } +// if (auto& binder = paintPropertyBinders.get()) { +// 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>*>( +// binder->getVertexValue(i)) +// ->a1; +// attr->set(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(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(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() && unevaluated.get().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}, - // }); - // } - //} } } }