BlueGit

timfa questward

Files

questward / client / src / render / chunkAtlasCache.js

chunkAtlasCache.js

chunkAtlasCache.js 3.5 KiB Download Raw
1import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
2import { EMPTY_BLOCK_ROW_KEY, EMPTY_BLOOM_KEY } from "/shared/render/chunkAtlasKeys.js";
3import { frameworkImageFromCanvas } from "./canvasSprite.js";
4
5export { EMPTY_BLOCK_ROW_KEY, EMPTY_BLOOM_KEY } from "/shared/render/chunkAtlasKeys.js";
6
7/**
8 * LRU cache of baked chunk atlas canvases (ground, block rows, bloom).
9 * Shared across chunks with identical tile visuals — skips CPU bake and GPU re-upload.
10 */
11export function createChunkAtlasCache ({
12 maxGround = 48,
13 maxBlockRows = 96,
14 maxBloom = 24
15} = {})
16{
17 return new ChunkAtlasCache({ maxGround, maxBlockRows, maxBloom });
18}
19
20class ChunkAtlasCache
21{
22 #limits;
23 #ground = new Map();
24 #blockRows = new Map();
25 #bloom = new Map();
26 #canvasEntry = new WeakMap();
27
28 constructor (limits)
29 {
30 this.#limits = limits;
31 }
32
33 acquireGround (key, factory)
34 {
35 return this.#acquire(this.#ground, key, factory, this.#limits.maxGround);
36 }
37
38 acquireBlockRow (key, factory)
39 {
40 if (key === EMPTY_BLOCK_ROW_KEY)
41 { return this.#acquireEmptyBlockRow(); }
42 return this.#acquire(this.#blockRows, key, factory, this.#limits.maxBlockRows);
43 }
44
45 acquireBloom (key, factory)
46 {
47 if (key === EMPTY_BLOOM_KEY)
48 { return this.#acquireEmptyBloom(); }
49 return this.#acquire(this.#bloom, key, factory, this.#limits.maxBloom);
50 }
51
52 getImage (entry)
53 {
54 if (!entry)
55 { return null; }
56 if (!entry.image)
57 {
58 entry.image = frameworkImageFromCanvas(entry.canvas);
59 }
60 return entry.image;
61 }
62
63 release (entry)
64 {
65 if (!entry)
66 { return; }
67 entry.refCount = Math.max(0, entry.refCount - 1);
68 }
69
70 markWarmed (canvas)
71 {
72 const entry = this.#canvasEntry.get(canvas);
73 if (entry)
74 { entry.warmed = true; }
75 }
76
77 isWarmed (canvas)
78 {
79 return this.#canvasEntry.get(canvas)?.warmed ?? false;
80 }
81
82 #acquireEmptyBlockRow ()
83 {
84 let entry = this.#blockRows.get(EMPTY_BLOCK_ROW_KEY);
85 if (!entry)
86 {
87 const canvas = makeCanvas(CHUNK_SIZE * SPRITE_SIZE_PX, SPRITE_SIZE_PX);
88 entry = this.#makeEntry(EMPTY_BLOCK_ROW_KEY, canvas);
89 this.#blockRows.set(EMPTY_BLOCK_ROW_KEY, entry);
90 }
91 entry.refCount += 1;
92 entry.lastUsed = performance.now();
93 return entry;
94 }
95
96 #acquireEmptyBloom ()
97 {
98 let entry = this.#bloom.get(EMPTY_BLOOM_KEY);
99 if (!entry)
100 {
101 const size = CHUNK_SIZE * SPRITE_SIZE_PX;
102 const canvas = makeCanvas(size, size);
103 entry = this.#makeEntry(EMPTY_BLOOM_KEY, canvas);
104 this.#bloom.set(EMPTY_BLOOM_KEY, entry);
105 }
106 entry.refCount += 1;
107 entry.lastUsed = performance.now();
108 return entry;
109 }
110
111 #acquire (map, key, factory, limit)
112 {
113 let entry = map.get(key);
114 if (entry)
115 {
116 entry.refCount += 1;
117 entry.lastUsed = performance.now();
118 return entry;
119 }
120 const canvas = factory();
121 entry = this.#makeEntry(key, canvas);
122 map.set(key, entry);
123 this.#evict(map, limit);
124 return entry;
125 }
126
127 #makeEntry (key, canvas)
128 {
129 const entry = {
130 key,
131 canvas,
132 image: null,
133 refCount: 1,
134 warmed: false,
135 lastUsed: performance.now()
136 };
137 this.#canvasEntry.set(canvas, entry);
138 return entry;
139 }
140
141 #evict (map, limit)
142 {
143 if (map.size <= limit)
144 { return; }
145 const candidates = [...map.values()]
146 .filter((entry) => entry.refCount <= 0)
147 .sort((a, b) => a.lastUsed - b.lastUsed);
148 for (const entry of candidates)
149 {
150 if (map.size <= limit)
151 { break; }
152 map.delete(entry.key);
153 this.#canvasEntry.delete(entry.canvas);
154 }
155 }
156}
157
158function makeCanvas (width, height)
159{
160 const canvas = document.createElement("canvas");
161 canvas.width = width;
162 canvas.height = height;
163 return canvas;
164}