Skip to content

Commit 9258fe6

Browse files
committed
feat: render first backgrounds early
1 parent 8427907 commit 9258fe6

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"backgroundColor":1445381,"backgroundResolution":512,"backgroundSize":100,"backgroundStart":[-1757,-1141],"backgroundCount":[35,20],"backgroundLength":286}
1+
{"color":1445381,"resolution":512,"size":100,"startPos":[-1757,-1141]}

src/process/compress_backgrounds.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,9 @@ for(let i = 0; i < filenames.length; i++) {
141141
}
142142

143143
const bgInfo = {}
144-
bgInfo.backgroundColor = bgInt
145-
bgInfo.backgroundResolution = 512
146-
bgInfo.backgroundSize = B.backgroundSize
147-
bgInfo.backgroundStart = B.backgroundStart
148-
bgInfo.backgroundCount = B.backgroundCount
149-
bgInfo.backgroundLength = B.backgrounds.length
144+
bgInfo.color = bgInt
145+
bgInfo.resolution = 512
146+
bgInfo.size = B.backgroundSize
147+
bgInfo.startPos = B.backgroundStart
150148

151149
fs.writeFileSync(dstInfo, JSON.stringify(bgInfo))

src/renderBackground.js

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import * as bkg2 from '$/backgrounds.json'
1+
import * as B from '$/backgrounds.json'
22
import { loadShader, checkProg } from './render_util.js'
33
import backgroundsUrl from '$/backgrounds.pak'
44

5-
const actualResolution = bkg2.backgroundResolution
6-
const texturesC = bkg2.backgroundLength
7-
85
// THIS LANGUAGE... IMAGINE NOT BEING ABLE TO PRINT A NUMBER WITH DECIMAL POINT
96
// NO, toFixed() ALSO ROUNDS THE NUMBER OR ADDS A MILLION ZEROS
107
// NO, toString() PRINTS INTEGERS WITHOUT DECIMAL POINT
11-
const bgSize = bkg2.backgroundSize + '.0'
8+
const bgSize = B.size + '.0'
129

1310
const vsSource = `#version 300 es
1411
precision highp float;
@@ -110,13 +107,11 @@ function convToRGB565(gl, inputC) {
110107
return res
111108
}
112109

113-
function updateBackground(context, imageData, chunks) {
110+
function updateBackground(context, index, chunks) {
114111
const rd = context.backgrounds
115112
if(rd?.loadImages !== true) return
116113

117-
const imgData = rd.images[imageData.i]
118-
imgData.x = imageData.x
119-
imgData.y = imageData.y
114+
const imgData = rd.images[index]
120115

121116
const blob = new Blob(chunks, { type: 'image/png' })
122117
const url = URL.createObjectURL(blob)
@@ -128,7 +123,7 @@ function updateBackground(context, imageData, chunks) {
128123
// Technically can be the last texture, so this will make
129124
// mimpaps not appear. But only until the user moves the screen
130125
// or something else triggers a rerender, so shouldn't be a big deal
131-
context.backgrounds.changed.push(imageData.i)
126+
rd.changed.push(index)
132127
imgData.done = true
133128
URL.revokeObjectURL(url)
134129
console.log('err')
@@ -139,23 +134,32 @@ function updateBackground(context, imageData, chunks) {
139134
gl.bindTexture(gl.TEXTURE_2D_ARRAY, rd.bgTextures)
140135
gl.texSubImage3D(
141136
gl.TEXTURE_2D_ARRAY, 0,
142-
0, 0, imageData.i,
143-
actualResolution, actualResolution, 1,
137+
0, 0, index,
138+
B.resolution, B.resolution, 1,
144139
gl.RGB, gl.UNSIGNED_BYTE,
145140
img
146141
)
147142

148-
rd.changed.push(imageData.i)
143+
rd.changed.push(index)
149144
imgData.ok = true
150145
imgData.done = true
151146

152-
context.requestRender(2, { timeout: 1000 })
147+
if(index < 2) {
148+
context.requestRender(1)
149+
}
150+
else {
151+
context.requestRender(2, { timeout: 1000 })
152+
}
153153
URL.revokeObjectURL(url)
154154
})
155155

156156
}
157157

158158
async function downloadBackgrounds(context) {
159+
const gl = context.gl
160+
const rd = context.backgrounds
161+
if(!rd) return
162+
159163
const resp = await fetch(backgroundsUrl)
160164
const body = resp.body
161165
const reader = body.getReader()
@@ -207,7 +211,6 @@ async function downloadBackgrounds(context) {
207211
const header = combineChunks(await read(headerLen), headerLen)
208212

209213
var index = 0
210-
211214
// duplicate from load.js
212215
function parseCompressedInt() {
213216
var res = 0
@@ -220,38 +223,46 @@ async function downloadBackgrounds(context) {
220223
return res
221224
}
222225

223-
const len = parseCompressedInt()
224-
const imageDatas = []
225-
for(let i = 0; i < len; i++) {
226+
const texturesC = parseCompressedInt()
227+
228+
gl.bindTexture(gl.TEXTURE_2D_ARRAY, rd.bgTextures)
229+
gl.texStorage3D(
230+
gl.TEXTURE_2D_ARRAY, __backgrounds_mipmap_levels,
231+
gl.RGB565, B.resolution, B.resolution,
232+
texturesC
233+
)
234+
235+
gl.bindBuffer(gl.ARRAY_BUFFER, rd.buf)
236+
gl.bufferData(gl.ARRAY_BUFFER, texturesC * 12, gl.DYNAMIC_DRAW)
237+
238+
const coordsDv = new DataView(new ArrayBuffer(texturesC * 12))
239+
rd.dataView = coordsDv
240+
241+
const imageDatas = Array(texturesC)
242+
rd.images = imageDatas
243+
244+
for(let i = 0; i < texturesC; i++) {
226245
const size = parseCompressedInt()
227246
const xi = parseCompressedInt()
228247
const yi = parseCompressedInt()
229-
const x = bkg2.backgroundStart[0] + xi * bkg2.backgroundSize
230-
const y = bkg2.backgroundStart[1] + yi * bkg2.backgroundSize
231-
imageDatas.push({ size, i, x, y })
248+
249+
const x = B.startPos[0] + xi * B.size
250+
const y = B.startPos[1] + yi * B.size
251+
imageDatas[i] = { ok: false, done: false, size, x, y }
232252
}
233253

234-
for(let i = 0; i < imageDatas.length; i++) {
254+
for(let i = 0; i < texturesC; i++) {
235255
const id = imageDatas[i]
236256
var chunks = tryRead(id.size)
237257
if(chunks == null) chunks = await read(id.size)
238-
updateBackground(context, id, chunks)
258+
updateBackground(context, i, chunks)
239259
}
240260
}
241261

242262
export function setup(context) {
243-
if(__backgrounds) {
244-
downloadBackgrounds(context).catch(e => {
245-
console.error('Error processing backgrounds', e)
246-
})
247-
}
248-
else {
249-
console.warn('skipping backgrounds')
250-
}
251-
252263
const { gl } = context
253264

254-
const renderData = { changed: [], curCount: 0 }
265+
const renderData = { changed: [], curCount: 0, doneCount: 0 }
255266
context.backgrounds = renderData
256267

257268
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource)
@@ -273,28 +284,15 @@ export function setup(context) {
273284
renderData.bgTextures = bgTextures
274285

275286
gl.bindTexture(gl.TEXTURE_2D_ARRAY, bgTextures)
276-
gl.texStorage3D(
277-
gl.TEXTURE_2D_ARRAY, __backgrounds_mipmap_levels,
278-
gl.RGB565, actualResolution, actualResolution,
279-
texturesC
280-
)
281-
282287
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST) // for now
283288
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
284289
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
285290
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
286291

287-
const images = Array(texturesC)
288-
renderData.images = images
292+
renderData.images = null
289293

290294
const buf = gl.createBuffer()
291295
renderData.buf = buf
292-
gl.bindBuffer(gl.ARRAY_BUFFER, buf)
293-
gl.bufferData(gl.ARRAY_BUFFER, texturesC * 12, gl.DYNAMIC_DRAW)
294-
295-
const coords = new ArrayBuffer(texturesC * 12)
296-
const coordsDv = new DataView(coords)
297-
renderData.dataView = coordsDv
298296

299297
const vao = gl.createVertexArray()
300298
renderData.vao = vao
@@ -317,9 +315,7 @@ export function setup(context) {
317315
const texturesU = gl.getUniformLocation(prog, 'textures')
318316
gl.uniform1i(texturesU, 0)
319317

320-
// by the way, how is this color the correct one?
321-
// I palletized the images, hasn't it change?
322-
const c = bkg2.backgroundColor
318+
const c = B.color
323319
const inputData = new Uint8Array(3)
324320
inputData[0] = (c ) & 0xff
325321
inputData[1] = (c >> 8) & 0xff
@@ -329,12 +325,17 @@ export function setup(context) {
329325

330326
renderData.ok = true
331327

332-
for(let i = 0; i < texturesC; i++) {
333-
images[i] = { ok: false, done: false }
334-
}
335-
336328
renderData.loadImages = true
337329
renderData.showImages = true
330+
331+
if(__backgrounds) {
332+
downloadBackgrounds(context).catch(e => {
333+
console.error('Error processing backgrounds', e)
334+
})
335+
}
336+
else {
337+
console.warn('skipping backgrounds')
338+
}
338339
}
339340

340341
export function setFiltered(context, showImages) {
@@ -359,30 +360,32 @@ export function render(context) {
359360
if(rd.changed.length != 0) {
360361
const dv = rd.dataView
361362

362-
var coordsCount = 0
363-
var done = true
364-
for(let i = 0; i < texturesC; i++) {
365-
const it = rd.images[i]
366-
done = done & it.done
363+
const ch = rd.changed
364+
const im = rd.images
365+
let addedC = 0
366+
for(let i = 0; i < ch.length; i++) {
367+
const index = ch[i]
368+
const it = im[index]
367369
if(!it.ok) continue
368-
dv.setFloat32(coordsCount * 12 , it.x, true)
369-
dv.setFloat32(coordsCount * 12 + 4, it.y, true)
370-
dv.setUint32 (coordsCount * 12 + 8, i, true)
371-
coordsCount++
370+
dv.setFloat32(addedC * 12 , it.x, true)
371+
dv.setFloat32(addedC * 12 + 4, it.y, true)
372+
dv.setUint32 (addedC * 12 + 8, index, true)
373+
addedC++
372374
}
373375

374-
if(done && !rd.mimpaps) {
376+
gl.bindBuffer(gl.ARRAY_BUFFER, rd.buf)
377+
gl.bufferSubData(gl.ARRAY_BUFFER, rd.curCount * 12, dv, 0, addedC * 12)
378+
379+
rd.curCount += addedC
380+
rd.doneCount += ch.length
381+
ch.length = 0
382+
383+
if(rd.doneCount === rd.images.length && !rd.mimpaps) {
375384
rd.mimpaps = true
376385
gl.bindTexture(gl.TEXTURE_2D_ARRAY, rd.bgTextures)
377386
gl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
378387
gl.generateMipmap(gl.TEXTURE_2D_ARRAY)
379388
}
380-
381-
gl.bindBuffer(gl.ARRAY_BUFFER, rd.buf)
382-
gl.bufferSubData(gl.ARRAY_BUFFER, 0, dv, 0, coordsCount * 12)
383-
384-
rd.curCount = coordsCount
385-
rd.changed.length = 0
386389
}
387390

388391
if(rd.showImages) {

vite.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => {
125125
'$/markers.png': srcPath('./data-raw/markers/markers.png'),
126126
'$/backgrounds.pak': srcPath('./data-processed/backgrounds.pak'),
127127

128-
'$/backgrounds.js': srcPath('./data-raw/backgrounds/backgrounds.js'),
129128
'$/backgrounds.json': srcPath('./data-processed/backgrounds.json'),
130129
'$/markers-meta.json': srcPath('./data-processed/markers-meta.json'),
131130
'$/markers.json': srcPath('./data-processed/markers.json'),

0 commit comments

Comments
 (0)