1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-11 09:11:50 +00:00

Minor "buffers" cleanup

Remove the canvas context loss handling as it was incorrect, simplify
the error conditions in makeOffscreenBuffer and remove commented out
code.
This commit is contained in:
Даниїл Григор'єв 2025-06-19 05:38:39 +03:00
parent b106c35c03
commit 2990f55dfb
No known key found for this signature in database
GPG Key ID: B890DF16341D8C1D
3 changed files with 10 additions and 71 deletions

View File

@ -1,7 +1,6 @@
import { GameRoot } from "../game/root"; import { GameRoot } from "../game/root";
import { clearBufferBacklog, freeCanvas, getBufferStats, makeOffscreenBuffer } from "./buffer_utils"; import { clearBufferBacklog, freeCanvas, makeOffscreenBuffer } from "./buffer_utils";
import { createLogger } from "./logging"; import { createLogger } from "./logging";
import { round1Digit } from "./utils";
/** /**
* @typedef {{ * @typedef {{
@ -35,7 +34,7 @@ export class BufferMaintainer {
* Returns the buffer stats * Returns the buffer stats
*/ */
getStats() { getStats() {
let stats = { const stats = {
rootKeys: 0, rootKeys: 0,
subKeys: 0, subKeys: 0,
vramBytes: 0, vramBytes: 0,
@ -59,25 +58,16 @@ export class BufferMaintainer {
* for a few iterations * for a few iterations
*/ */
garbargeCollect() { garbargeCollect() {
let totalKeys = 0;
let deletedKeys = 0;
const minIteration = this.iterationIndex; const minIteration = this.iterationIndex;
this.cache.forEach((subCache, key) => { this.cache.forEach((subCache, key) => {
let unusedSubKeys = []; const unusedSubKeys = [];
// Filter sub cache // Filter sub cache
subCache.forEach((cacheEntry, subKey) => { subCache.forEach((cacheEntry, subKey) => {
if ( if (cacheEntry.lastUse < minIteration) {
cacheEntry.lastUse < minIteration ||
// @ts-ignore
cacheEntry.canvas._contextLost
) {
unusedSubKeys.push(subKey); unusedSubKeys.push(subKey);
freeCanvas(cacheEntry.canvas); freeCanvas(cacheEntry.canvas);
++deletedKeys;
} else {
++totalKeys;
} }
}); });
@ -90,30 +80,6 @@ export class BufferMaintainer {
// Make sure our backlog never gets too big // Make sure our backlog never gets too big
clearBufferBacklog(); clearBufferBacklog();
// if (G_IS_DEV) {
// const bufferStats = getBufferStats();
// const mbUsed = round1Digit(bufferStats.vramUsage / (1024 * 1024));
// logger.log(
// "GC: Remove",
// (deletedKeys + "").padStart(4),
// ", Remain",
// (totalKeys + "").padStart(4),
// "(",
// (bufferStats.bufferCount + "").padStart(4),
// "total",
// ")",
// "(",
// (bufferStats.backlogSize + "").padStart(4),
// "backlog",
// ")",
// "VRAM:",
// mbUsed,
// "MB"
// );
// }
++this.iterationIndex; ++this.iterationIndex;
} }
@ -180,7 +146,7 @@ export class BufferMaintainer {
* *
*/ */
getForKeyOrNullNoUpdate({ key, subKey }) { getForKeyOrNullNoUpdate({ key, subKey }) {
let parent = this.cache.get(key); const parent = this.cache.get(key);
if (!parent) { if (!parent) {
return null; return null;
} }

View File

@ -82,15 +82,7 @@ export function clearBufferBacklog() {
* @returns {[HTMLCanvasElement, CanvasRenderingContext2D]} * @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}
*/ */
export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, label = "buffer" }) { export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, label = "buffer" }) {
assert(w > 0 && h > 0, "W or H < 0"); assert(w >= 1 && h >= 1, "Invalid offscreen buffer size: W or H < 1");
if (w % 1 !== 0 || h % 1 !== 0) {
// console.warn("Subpixel offscreen buffer size:", w, h);
}
if (w < 1 || h < 1) {
logger.error("Offscreen buffer size < 0:", w, "x", h);
w = Math.max(1, w);
h = Math.max(1, h);
}
const recommendedSize = 1024 * 1024; const recommendedSize = 1024 * 1024;
if (w * h > recommendedSize) { if (w * h > recommendedSize) {
@ -140,20 +132,7 @@ export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, labe
// Initial state // Initial state
context.save(); context.save();
canvas.addEventListener("webglcontextlost", () => {
console.warn("canvas::webglcontextlost", canvas);
// @ts-ignore
canvas._contextLost = true;
});
canvas.addEventListener("contextlost", () => {
console.warn("canvas::contextlost", canvas);
// @ts-ignore
canvas._contextLost = true;
});
} }
// @ts-ignore
canvas._contextLost = false;
// @ts-ignore // @ts-ignore
canvas.label = label; canvas.label = label;

View File

@ -1,11 +1,11 @@
import { freeCanvas, makeOffscreenBuffer } from "../core/buffer_utils";
import { globalConfig } from "../core/config"; import { globalConfig } from "../core/config";
import { DrawParameters } from "../core/draw_parameters"; import { DrawParameters } from "../core/draw_parameters";
import { BaseMap } from "./map";
import { freeCanvas, makeOffscreenBuffer } from "../core/buffer_utils";
import { Entity } from "./entity"; import { Entity } from "./entity";
import { THEME } from "./theme"; import { BaseMap } from "./map";
import { MapChunkView } from "./map_chunk_view";
import { MapChunkAggregate } from "./map_chunk_aggregate"; import { MapChunkAggregate } from "./map_chunk_aggregate";
import { MapChunkView } from "./map_chunk_view";
import { THEME } from "./theme";
/** /**
* This is the view of the map, it extends the map which is the raw model and allows * This is the view of the map, it extends the map which is the raw model and allows
@ -241,12 +241,6 @@ export class MapView extends BaseMap {
key = "placing"; key = "placing";
} }
// @ts-ignore`
if (this.cachedBackgroundCanvases[key]._contextLost) {
freeCanvas(this.cachedBackgroundCanvases[key]);
this.internalInitializeCachedBackgroundCanvases();
}
parameters.context.fillStyle = parameters.context.createPattern( parameters.context.fillStyle = parameters.context.createPattern(
this.cachedBackgroundCanvases[key], this.cachedBackgroundCanvases[key],
"repeat" "repeat"