- 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
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
import { renderer } from "./../video.js";
import * as fileUtil from "./../../utils/file.js";
import { TextureAtlas, createAtlas } from "./atlas.js";
import { isPowerOfTwo } from "./../../math/math.js";
import { ArrayMultimap } from "@teppeis/multimaps";
/**
* a basic texture cache object
* @ignore
*/
class TextureCache {
/**
* @ignore
*/
constructor(max_size = Infinity) {
// cache uses an array to allow for duplicated key
this.cache = new ArrayMultimap();
this.tinted = new Map();
this.units = new Map();
this.usedUnits = new Set();
this.max_size = max_size;
this.clear();
}
/**
* @ignore
*/
clear() {
this.cache.clear();
this.tinted.clear();
this.units.clear();
this.usedUnits.clear();
}
/**
* @ignore
*/
allocateTextureUnit() {
// find the first unit available among the max_size
for (let unit = 0; unit < this.max_size; unit++) {
// Check if unit is available
if (!this.usedUnits.has(unit)) {
// Add to used set
this.usedUnits.add(unit);
// return the new unit
return unit;
}
}
// No units available
// TODO: Merge textures instead of throwing an exception
throw new Error(
"Texture cache overflow: " + this.max_size +
" texture units available for this GPU."
);
}
/**
* @ignore
*/
freeTextureUnit(texture) {
let unit = this.units.get(texture);
// was a texture unit allocated ?
if (typeof unit !== "undefined") {
this.usedUnits.delete(unit);
this.units.delete(texture);
}
}
/**
* @ignore
*/
get(image, atlas) {
let entry;
if (typeof atlas === "undefined") {
entry = this.cache.get(image)[0];
} else {
// manage cases where a specific atlas is specified
this.cache.forEach((value, key) => {
let _atlas = value.getAtlas()[0];
if (key === image && _atlas.width === atlas.framewidth && _atlas.height === atlas.frameheight) {
entry = value;
}
});
}
if (typeof entry === "undefined") {
if (!atlas) {
atlas = createAtlas(image.width || image.videoWidth, image.videoHeight, image.src ? fileUtil.getBasename(image.src) : undefined);
}
entry = new TextureAtlas(atlas, image, false);
this.set(image, entry);
}
return entry;
}
/**
* @ignore
*/
delete(image) {
if (this.cache.has(image)) {
let texture = this.cache.get(image)[0];
if (typeof texture !== "undefined") {
this.freeTextureUnit(texture);
}
this.cache.delete(image);
}
}
/**
* @ignore
*/
tint(src, color) {
// make sure the src is in the cache
let image_cache = this.tinted.get(src);
if (image_cache === undefined) {
image_cache = this.tinted.set(src, new Map());
}
if (!image_cache.has(color)) {
image_cache.set(color, renderer.tint(src, color, "multiply"));
}
return image_cache.get(color);
}
/**
* @ignore
*/
set(image, texture) {
let width = image.width || image.videoWidth;
let height = image.height || image.videoHeight;
// warn if a non POT texture is added to the cache when using WebGL1
if (renderer.WebGLVersion === 1 && (!isPowerOfTwo(width) || !isPowerOfTwo(height))) {
let src = typeof image.src !== "undefined" ? image.src : image;
console.warn(
"[Texture] " + src + " is not a POT texture " +
"(" + width + "x" + height + ")"
);
}
return this.cache.put(image, texture);
}
/**
* @ignore
*/
getUnit(texture) {
if (!this.units.has(texture)) {
this.units.set(texture, this.allocateTextureUnit());
}
return this.units.get(texture);
}
}
export default TextureCache;