Use timestamps for attribute updates (#2629)

This commit is contained in:
Tim Sylvester
2024-08-20 07:33:29 -07:00
committed by GitHub
parent c3a2fa247c
commit fceaa18caf
43 changed files with 439 additions and 163 deletions

View File

@@ -5,9 +5,10 @@
#include <mbgl/gfx/uniform_buffer.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/identity.hpp>
#include <mbgl/util/traits.hpp>
#include <mbgl/util/containers.hpp>
#include <mbgl/util/identity.hpp>
#include <mbgl/util/monotonic_timer.hpp>
#include <mbgl/util/traits.hpp>
#include <cstdint>
#include <cstddef>
@@ -244,6 +245,8 @@ public:
/// Set origin point
void setOrigin(std::optional<Point<double>> p) { origin = std::move(p); }
const std::chrono::duration<double> createTime = util::MonotonicTimer::now();
protected:
bool enabled = true;
bool enableColor = true;
@@ -264,6 +267,7 @@ protected:
UniqueDrawableData drawableData{};
gfx::VertexAttributeArrayPtr vertexAttributes;
gfx::VertexAttributeArrayPtr instanceAttributes;
std::chrono::duration<double> attributeUpdateTime = util::MonotonicTimer::now();
struct Impl;
std::unique_ptr<Impl> impl;

View File

@@ -117,7 +117,7 @@ public:
void setIndex(int value) { index = value; }
/// @brief Get the stride of the vertex attribute
std::size_t getStride() const { return stride; }
virtual std::size_t getStride() const { return stride; }
void setStride(std::size_t value) { stride = value; }
/// @brief Get the count of vertex attribute items
@@ -180,12 +180,16 @@ public:
items.clear();
}
/// @brief Get dirty state
bool isDirty() const { return dirty; }
/// @brief Set dirty state
void setDirty(bool value = true) { dirty = value; }
bool isModifiedAfter(std::chrono::duration<double> time) const {
if (sharedRawData) {
return sharedRawData->isModifiedAfter(time);
}
return dirty;
}
template <std::size_t I = 0, typename... Tp>
inline typename std::enable_if<I == sizeof...(Tp), void>::type set(std::size_t, std::tuple<Tp...>, std::size_t) {}
@@ -255,6 +259,9 @@ protected:
}
protected:
friend class VertexAttributeArray;
bool isDirty() const { return dirty; }
int index;
std::size_t stride;
@@ -310,8 +317,9 @@ public:
std::size_t count = 0);
/// Indicates whether any values have changed
virtual bool isDirty() const {
return std::any_of(attrs.begin(), attrs.end(), [](const auto& attr) { return attr && attr->isDirty(); });
bool isModifiedAfter(std::chrono::duration<double> time) const {
return std::any_of(
attrs.begin(), attrs.end(), [&](const auto& attr) { return attr && attr->isModifiedAfter(time); });
}
/// Clear the collection
@@ -328,11 +336,17 @@ public:
}
/// Call the provided delegate with each value, providing the override if one exists.
template <typename Func /* void(const size_t, VertexAttribute&, const std::unique_ptr<VertexAttribute>&) */>
void resolve(const VertexAttributeArray& overrides, Func delegate) const {
/// @param foundDelegate Called for each attribute in `default`, with the corresponding override, if present
/// @param missingDelegate Called for any overrides with no corresponding default
template <typename ResolveFunc /* void(const size_t, VertexAttribute&, const std::unique_ptr<VertexAttribute>&) */,
typename MissingFunc /* void(const size_t, const std::unique_ptr<VertexAttribute>&) */>
void resolve(const VertexAttributeArray& overrides, ResolveFunc foundDelegate, MissingFunc missingDelegate) const {
for (size_t id = 0; id < attrs.size(); id++) {
const auto& override = overrides.get(id);
if (const auto& attr = attrs[id]) {
delegate(id, *attr, overrides.get(id));
foundDelegate(id, *attr, override);
} else if (override) {
missingDelegate(id, override);
}
}
}

View File

@@ -31,9 +31,11 @@ public:
bool getNormalized() const { return normalized; }
void setNormalized(bool value) { normalized = value; }
std::size_t getStride() const;
std::size_t getStride() const override;
static const std::vector<std::uint8_t>& getRaw(gfx::VertexAttribute& attr, platform::GLenum);
static const std::vector<std::uint8_t>& getRaw(gfx::VertexAttribute& attr,
platform::GLenum,
std::chrono::duration<double> lastUpdate);
private:
static int getSize(platform::GLenum glType);
@@ -58,9 +60,6 @@ public:
}
VertexAttributeArrayGL& operator=(const VertexAttributeArrayGL&) = delete;
/// Indicates whether any values have changed
bool isDirty() const override;
private:
std::unique_ptr<gfx::VertexAttribute> create(int index,
gfx::AttributeDataType dataType,

View File

@@ -66,6 +66,7 @@ public:
const gfx::VertexAttributeArray& defaults,
const gfx::VertexAttributeArray& overrides,
gfx::BufferUsageType,
const std::chrono::duration<double> lastUpdate,
/*out*/ std::vector<std::unique_ptr<gfx::VertexBufferResource>>& outBuffers) override;
std::unique_ptr<gfx::TextureResource> createTextureResource(Size,

View File

@@ -39,7 +39,7 @@ public:
VertexAttributeArray& operator=(const VertexAttributeArray& other) = delete;
/// Indicates whether any values have changed
bool isDirty() const override;
bool isModifiedAfter(std::chrono::duration<double> time) const;
private:
gfx::UniqueVertexAttribute create(int index, gfx::AttributeDataType dataType, std::size_t count) const override {

View File

@@ -1,6 +1,7 @@
#pragma once
#include <mbgl/mtl/buffer_resource.hpp>
#include <mbgl/util/monotonic_timer.hpp>
#include <memory>
@@ -21,8 +22,12 @@ public:
BufferResource& get() noexcept { return buffer; }
const BufferResource& get() const noexcept { return buffer; }
std::chrono::duration<double> getLastUpdated() const { return lastUpdated; }
void setLastUpdated(std::chrono::duration<double> time) { lastUpdated = time; }
protected:
BufferResource buffer;
std::chrono::duration<double> lastUpdated;
};
using UniqueVertexBufferResource = std::unique_ptr<VertexBufferResource>;

View File

@@ -25,12 +25,12 @@ const void* castGpuIdToTracyPtr(GpuId id) {
"MLN_RENDER_BACKEND_OPENGL is not defined. MLN_RENDER_BACKEND_OPENGL is expected to be defined in CMake and Bazel"
#endif
#define MLN_TRACE_FUNC() ZoneScoped;
#define MLN_TRACE_ZONE(label) ZoneScopedN(#label);
#define MLN_TRACE_FUNC() ZoneScoped
#define MLN_TRACE_ZONE(label) ZoneScopedN(#label)
#define MLN_ZONE_TEXT(label) ZoneText(#label, strlen(#label));
#define MLN_ZONE_STR(str) ZoneText(str.c_str(), str.size());
#define MLN_ZONE_VALUE(val) ZoneValue(static_cast<uint64_t>(val));
#define MLN_ZONE_TEXT(text, size) ZoneText(text, size)
#define MLN_ZONE_STR(str) ZoneText(str.c_str(), str.size())
#define MLN_ZONE_VALUE(n) ZoneValue(n)
constexpr const char* tracyTextureMemoryLabel = "Texture Memory";
#define MLN_TRACE_ALLOC_TEXTURE(id, size) TracyAllocN(castGpuIdToTracyPtr(id), size, tracyTextureMemoryLabel);
@@ -119,4 +119,4 @@ constexpr const char* tracyConstMemoryLabel = "Constant Buffer Memory";
#define MLN_TRACE_FUNC()
#define MLN_TRACE_ZONE(label)
#endif // MLN_TRACY_ENABLE
#endif // MLN_TRACY_ENABLE

View File

@@ -61,6 +61,7 @@ public:
const gfx::VertexAttributeArray& defaults,
const gfx::VertexAttributeArray& overrides,
gfx::BufferUsageType,
const std::chrono::duration<double> lastUpdate,
/*out*/ std::vector<std::unique_ptr<gfx::VertexBufferResource>>& outBuffers) override;
std::unique_ptr<gfx::TextureResource> createTextureResource(Size,

View File

@@ -39,9 +39,6 @@ public:
}
VertexAttributeArray& operator=(const VertexAttributeArray& other) = delete;
/// Indicates whether any values have changed
bool isDirty() const override;
private:
gfx::UniqueVertexAttribute create(int index, gfx::AttributeDataType dataType, std::size_t count) const override {
return std::make_unique<VertexAttribute>(index, dataType, count);

View File

@@ -22,8 +22,12 @@ public:
BufferResource& get() noexcept { return buffer; }
const BufferResource& get() const noexcept { return buffer; }
std::chrono::duration<double> getLastUpdated() const { return lastUpdated; }
void setLastUpdated(std::chrono::duration<double> time) { lastUpdated = time; }
protected:
BufferResource buffer;
std::chrono::duration<double> lastUpdated;
};
using UniqueVertexBufferResource = std::unique_ptr<VertexBufferResource>;