-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalething.js
More file actions
87 lines (69 loc) · 2.97 KB
/
scalething.js
File metadata and controls
87 lines (69 loc) · 2.97 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
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
import { GL } from './gl.js'
export class ScaleThing {
static #width
static #height
static #texturePing
static #texturePong
static #framebufferPing
static #framebufferPong
static #setTextureParams(wrap, 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)
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)
}
static #checkFramebufferStatus() {
const status = GL.gl.checkFramebufferStatus(GL.gl.DRAW_FRAMEBUFFER)
switch (status) {
case GL.gl.FRAMEBUFFER_COMPLETE:
return true
case GL.gl.FRAMEBUFFER_UNSUPPORTED:
throw new Error('FRAMEBUFFER_UNSUPPORTED')
case GL.gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
throw new Error('FRAMEBUFFER_INCOMPLETE_ATTACHMENT')
case GL.gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:
throw new Error('FRAMEBUFFER_INCOMPLETE_DIMENSIONS')
case GL.gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
throw new Error('FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT')
}
throw new Error(`unexpected checkFramebufferStatus: ${status}`)
}
static init(width, height) {
if (ScaleThing.#width === width && ScaleThing.#height === height) {
return
}
if (!ScaleThing.#width) {
ScaleThing.#framebufferPing = GL.gl.createFramebuffer()
ScaleThing.#framebufferPong = GL.gl.createFramebuffer()
} else {
GL.gl.deleteTexture(ScaleThing.#texturePing)
GL.gl.deleteTexture(ScaleThing.#texturePong)
}
ScaleThing.#texturePing = GL.gl.createTexture()
GL.gl.bindTexture(GL.gl.TEXTURE_2D, ScaleThing.#texturePing)
GL.gl.texStorage2D(GL.gl.TEXTURE_2D, 1, GL.gl.R11F_G11F_B10F, width, height)
ScaleThing.#setTextureParams(GL.gl.CLAMP_TO_EDGE, GL.gl.NEAREST)
ScaleThing.#texturePong = GL.gl.createTexture()
GL.gl.bindTexture(GL.gl.TEXTURE_2D, ScaleThing.#texturePong)
GL.gl.texStorage2D(GL.gl.TEXTURE_2D, 1, GL.gl.R11F_G11F_B10F, width, height)
ScaleThing.#setTextureParams(GL.gl.CLAMP_TO_EDGE, GL.gl.NEAREST)
GL.gl.bindFramebuffer(GL.gl.DRAW_FRAMEBUFFER, ScaleThing.#framebufferPing)
GL.gl.framebufferTexture2D(GL.gl.DRAW_FRAMEBUFFER, GL.gl.COLOR_ATTACHMENT0, GL.gl.TEXTURE_2D, ScaleThing.#texturePing, 0)
if (!ScaleThing.#checkFramebufferStatus()) {
return false
}
GL.gl.bindFramebuffer(GL.gl.DRAW_FRAMEBUFFER, ScaleThing.#framebufferPong)
GL.gl.framebufferTexture2D(GL.gl.DRAW_FRAMEBUFFER, GL.gl.COLOR_ATTACHMENT0, GL.gl.TEXTURE_2D, ScaleThing.#texturePong, 0)
if (!ScaleThing.#checkFramebufferStatus()) {
return false
}
}
free() {
GL.gl.deleteFramebuffer(ScaleThing.#framebufferPing)
GL.gl.deleteFramebuffer(ScaleThing.#framebufferPong)
GL.gl.deleteTexture(ScaleThing.#texturePing)
GL.gl.deleteTexture(ScaleThing.#texturePong)
ScaleThing.#width = 0
ScaleThing.#height = 0
}
}