mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-12-09 16:21:51 +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:
parent
b106c35c03
commit
2990f55dfb
@ -1,7 +1,6 @@
|
||||
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 { round1Digit } from "./utils";
|
||||
|
||||
/**
|
||||
* @typedef {{
|
||||
@ -35,7 +34,7 @@ export class BufferMaintainer {
|
||||
* Returns the buffer stats
|
||||
*/
|
||||
getStats() {
|
||||
let stats = {
|
||||
const stats = {
|
||||
rootKeys: 0,
|
||||
subKeys: 0,
|
||||
vramBytes: 0,
|
||||
@ -59,25 +58,16 @@ export class BufferMaintainer {
|
||||
* for a few iterations
|
||||
*/
|
||||
garbargeCollect() {
|
||||
let totalKeys = 0;
|
||||
let deletedKeys = 0;
|
||||
const minIteration = this.iterationIndex;
|
||||
|
||||
this.cache.forEach((subCache, key) => {
|
||||
let unusedSubKeys = [];
|
||||
const unusedSubKeys = [];
|
||||
|
||||
// Filter sub cache
|
||||
subCache.forEach((cacheEntry, subKey) => {
|
||||
if (
|
||||
cacheEntry.lastUse < minIteration ||
|
||||
// @ts-ignore
|
||||
cacheEntry.canvas._contextLost
|
||||
) {
|
||||
if (cacheEntry.lastUse < minIteration) {
|
||||
unusedSubKeys.push(subKey);
|
||||
freeCanvas(cacheEntry.canvas);
|
||||
++deletedKeys;
|
||||
} else {
|
||||
++totalKeys;
|
||||
}
|
||||
});
|
||||
|
||||
@ -90,30 +80,6 @@ export class BufferMaintainer {
|
||||
// Make sure our backlog never gets too big
|
||||
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;
|
||||
}
|
||||
|
||||
@ -180,7 +146,7 @@ export class BufferMaintainer {
|
||||
*
|
||||
*/
|
||||
getForKeyOrNullNoUpdate({ key, subKey }) {
|
||||
let parent = this.cache.get(key);
|
||||
const parent = this.cache.get(key);
|
||||
if (!parent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -82,15 +82,7 @@ export function clearBufferBacklog() {
|
||||
* @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}
|
||||
*/
|
||||
export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, label = "buffer" }) {
|
||||
assert(w > 0 && h > 0, "W or H < 0");
|
||||
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);
|
||||
}
|
||||
assert(w >= 1 && h >= 1, "Invalid offscreen buffer size: W or H < 1");
|
||||
|
||||
const recommendedSize = 1024 * 1024;
|
||||
if (w * h > recommendedSize) {
|
||||
@ -140,20 +132,7 @@ export function makeOffscreenBuffer(w, h, { smooth = true, reusable = true, labe
|
||||
|
||||
// Initial state
|
||||
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
|
||||
canvas.label = label;
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
import { freeCanvas, makeOffscreenBuffer } from "../core/buffer_utils";
|
||||
import { globalConfig } from "../core/config";
|
||||
import { DrawParameters } from "../core/draw_parameters";
|
||||
import { BaseMap } from "./map";
|
||||
import { freeCanvas, makeOffscreenBuffer } from "../core/buffer_utils";
|
||||
import { Entity } from "./entity";
|
||||
import { THEME } from "./theme";
|
||||
import { MapChunkView } from "./map_chunk_view";
|
||||
import { BaseMap } from "./map";
|
||||
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
|
||||
@ -241,12 +241,6 @@ export class MapView extends BaseMap {
|
||||
key = "placing";
|
||||
}
|
||||
|
||||
// @ts-ignore`
|
||||
if (this.cachedBackgroundCanvases[key]._contextLost) {
|
||||
freeCanvas(this.cachedBackgroundCanvases[key]);
|
||||
this.internalInitializeCachedBackgroundCanvases();
|
||||
}
|
||||
|
||||
parameters.context.fillStyle = parameters.context.createPattern(
|
||||
this.cachedBackgroundCanvases[key],
|
||||
"repeat"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user