-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexturepool.js
More file actions
58 lines (48 loc) · 1.43 KB
/
texturepool.js
File metadata and controls
58 lines (48 loc) · 1.43 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
import { GL } from './gl.js'
export class TexturePool {
static #textures = []
static get(width, height, format, type, internalFormat) {
// Try to find existing texture
const tex = TexturePool.#textures.find(t =>
t.width === width &&
t.height === height &&
t.format === format &&
t.type === type &&
!t.inUse
)
if (tex) {
tex.inUse = true
return tex
}
return TexturePool.#create(width, height, format, type, internalFormat)
}
static release(texture) {
const tex = TexturePool.#textures.find(t => t === texture)
if (tex) {
tex.inUse = false
}
}
static #create(width, height, format, type, internalFormat) {
const texture = GL.gl.createTexture()
GL.gl.bindTexture(GL.gl.TEXTURE_2D, texture)
GL.gl.texStorage2D(GL.gl.TEXTURE_2D, 1, internalFormat, width, height)
TexturePool.#params(GL.gl.LINEAR, GL.gl.CLAMP_TO_EDGE)
const newTex = {
texture,
width,
height,
format,
type,
internalFormat,
inUse: true
}
TexturePool.#textures.push(newTex)
return newTex
}
static #params(filter, wrap) {
GL.gl.texParameteri(GL.gl.TEXTURE_2D, GL.gl.TEXTURE_MIN_FILTER, filter)
GL.gl.texParameteri(GL.gl.TEXTURE_2D, GL.gl.TEXTURE_MAG_FILTER, filter)
GL.gl.texParameteri(GL.gl.TEXTURE_2D, GL.gl.TEXTURE_WRAP_S, wrap)
GL.gl.texParameteri(GL.gl.TEXTURE_2D, GL.gl.TEXTURE_WRAP_T, wrap)
}
}