2025-07-19 15:57:09 +03:00
|
|
|
#pragma once
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include "../CollabList.hpp"
|
|
|
|
|
#include "../DrawComponents/DrawComponent.hpp"
|
2025-07-19 19:41:25 +03:00
|
|
|
#include <include/core/SkSurface.h>
|
2025-07-20 15:24:44 +03:00
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
|
|
class DrawingProgram;
|
2025-07-19 15:57:09 +03:00
|
|
|
|
|
|
|
|
class DrawingProgramCache {
|
|
|
|
|
public:
|
|
|
|
|
typedef CollabList<std::shared_ptr<DrawComponent>, ServerClientID> CollabListType;
|
2025-07-20 15:24:44 +03:00
|
|
|
DrawingProgramCache(DrawingProgram& initDrawP);
|
|
|
|
|
|
|
|
|
|
void update();
|
2025-07-19 15:57:09 +03:00
|
|
|
void erase_component(const CollabListType::ObjectInfoPtr& c);
|
|
|
|
|
void add_component(const CollabListType::ObjectInfoPtr& c);
|
2025-07-20 15:24:44 +03:00
|
|
|
void refresh_all_draw_cache(SkCanvas* canvas, const DrawData& drawData);
|
2025-07-19 15:57:09 +03:00
|
|
|
|
|
|
|
|
struct BVHNode {
|
|
|
|
|
SCollision::AABB<WorldScalar> bounds;
|
|
|
|
|
std::vector<CollabListType::ObjectInfoPtr> components;
|
|
|
|
|
std::vector<BVHNode> children;
|
2025-07-20 15:24:44 +03:00
|
|
|
|
|
|
|
|
CoordSpaceHelper coords;
|
|
|
|
|
Vector2i resolution;
|
2025-07-19 19:41:25 +03:00
|
|
|
struct DrawCacheData {
|
|
|
|
|
uint64_t lastDrawnComponentPlacement;
|
|
|
|
|
sk_sp<SkSurface> surface;
|
2025-07-20 15:24:44 +03:00
|
|
|
std::chrono::steady_clock::time_point lastRenderTime;
|
2025-07-19 19:41:25 +03:00
|
|
|
};
|
2025-07-20 15:24:44 +03:00
|
|
|
|
2025-07-19 19:41:25 +03:00
|
|
|
std::optional<DrawCacheData> drawCache;
|
2025-07-19 15:57:09 +03:00
|
|
|
};
|
|
|
|
|
|
2025-07-20 15:24:44 +03:00
|
|
|
std::unordered_set<BVHNode*> nodesWithCachedSurfaces;
|
2025-07-19 19:41:25 +03:00
|
|
|
|
|
|
|
|
void traverse_bvh_run_function(const SCollision::AABB<WorldScalar>& aabb, std::function<bool(BVHNode* node, const std::vector<CollabListType::ObjectInfoPtr>& components)> f);
|
2025-07-19 15:57:09 +03:00
|
|
|
|
|
|
|
|
// Run before actually updating the component
|
|
|
|
|
void preupdate_component(DrawComponent* c);
|
2025-07-19 19:41:25 +03:00
|
|
|
|
|
|
|
|
void invalidate_cache_before_pos(uint64_t placementToInvalidateAt);
|
2025-07-19 15:57:09 +03:00
|
|
|
private:
|
2025-07-20 15:24:44 +03:00
|
|
|
void build_bvh_node_coords_and_resolution(BVHNode& node);
|
|
|
|
|
void refresh_draw_cache(BVHNode& bvhNode, const DrawData& drawData);
|
2025-07-19 19:41:25 +03:00
|
|
|
|
2025-07-19 15:57:09 +03:00
|
|
|
void build(std::vector<CollabListType::ObjectInfoPtr> components);
|
|
|
|
|
|
2025-07-19 19:41:25 +03:00
|
|
|
void traverse_bvh_run_function_recursive(BVHNode& bvhNode, const SCollision::AABB<WorldScalar>& aabb, std::function<bool(BVHNode* node, const std::vector<CollabListType::ObjectInfoPtr>& components)> f);
|
2025-07-19 15:57:09 +03:00
|
|
|
std::pair<DrawingProgramCache::BVHNode*, std::vector<DrawingProgramCache::CollabListType::ObjectInfoPtr>::iterator> get_bvh_node_fully_containing_recursive(BVHNode& bvhNode, DrawComponent* c);
|
|
|
|
|
|
|
|
|
|
void build_bvh_node(BVHNode& node, std::vector<CollabListType::ObjectInfoPtr> components);
|
|
|
|
|
BVHNode bvhRoot;
|
|
|
|
|
std::vector<CollabListType::ObjectInfoPtr> unsortedComponents;
|
2025-07-20 15:24:44 +03:00
|
|
|
DrawingProgram& drawP;
|
2025-07-19 15:57:09 +03:00
|
|
|
};
|