Repository navigation
Expand file tree
/
Copy pathTileCache.h
More file actions
88 lines (68 loc) · 3.37 KB
/
Copy pathTileCache.h
File metadata and controls
88 lines (68 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#pragma once
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>
#include <imgui_internal.h>
#include <map>
#include <vector>
#include "GLFunctions.h" // Includes SDL_opengl.h
// Tile structure representing a fixed-size region of the node graph
struct Tile {
GLuint fbo;
GLuint texture;
ImRect worldBounds; // Bounds in node space coordinates
bool isDirty;
bool isAllocated;
bool isVisible_; // Currently visible in viewport (protected from eviction)
float lastAccessTime; // For LRU eviction
Tile() : fbo(0), texture(0), isDirty(true), isAllocated(false), isVisible_(false), lastAccessTime(0.0f) {}
};
// TileCache manages a sparse grid of rendered tiles for the node graph
class TileCache {
public:
static const int TILE_SIZE = 8192; // Fixed tile dimensions (8192x8192 = ~128MB/tile at RGB565, 2 bytes/px)
static const int TILE_BYTES_PER_PIXEL = 2; // RGB565 (see AllocateTile)
static int MAX_CACHED_TILES; // Dynamic limit based on RAM (set at startup)
static int TILE_BUDGET_CEILING; // Startup budget; upper bound when a lowered cap recovers
// Memory cost of one allocated tile. Derived from TILE_SIZE/TILE_BYTES_PER_PIXEL so callers
// sizing a tile budget can't drift from the format AllocateTile() actually uses.
static float TileSizeGB() {
return (TILE_SIZE * (float)TILE_SIZE * TILE_BYTES_PER_PIXEL) / (1024.0f * 1024.0f * 1024.0f);
}
// Tiles needed to cover `pixels` along one axis. Rounds UP: a partially covered tile still costs
// a full TILE_SIZE^2 allocation, so truncating here understates the grid (and the memory bill).
static int TilesToCover(int pixels) {
return pixels <= 0 ? 0 : (pixels + TILE_SIZE - 1) / TILE_SIZE;
}
TileCache();
~TileCache();
// Verify OpenGL function pointers are loaded
void Initialize();
// Get or create tile at grid coordinates (tileX, tileY)
Tile* GetTile(int tileX, int tileY);
// Mark all tiles dirty that intersect the given world rect
void MarkDirty(const ImRect& worldRect);
// Mark all tiles dirty
void MarkAllDirty();
// Get list of tiles visible in the current camera viewport
std::vector<Tile*> GetVisibleTiles(ImVec2 cameraOffset, float cameraZoom,
ImVec2 viewportSize, ImVec2 worldSize);
// Evict least recently used tiles if over memory limit. evictVisible allows visible tiles to be freed
// as a last resort once no hidden ones remain - see the definition; only the critical-memory shed in
// EnforceMemoryLimits() passes true.
void EvictLRU(bool evictVisible = false);
// Get total number of allocated tiles
int GetAllocatedTileCount() const;
// Set memory limits for runtime checks
void SetMemoryLimits(float maxOurProcessGB, float totalSystemRAM_GB);
// Allocate GL resources for a tile (public so rendering loop can call it)
void AllocateTile(Tile* tile);
private:
// Free GL resources for a tile
void FreeTile(Tile* tile);
// Sparse tile storage: only allocate tiles that contain nodes
std::map<std::pair<int,int>, Tile> tiles_;
// Memory limits for runtime safety checks
int safeTileLimit_; // Estimated max tiles (fallback for non-Windows)
float maxOurProcessGB_; // Our process memory budget (Check 1)
float totalSystemRAM_GB_; // System total RAM (for Check 2)
};