BlueGit

timfa questward

Reuse lightmap pixel buffers and update GL textures in place.

Skip delete/recreate uploads on cache misses by using texSubImage2D and a persistent ImageData view over the chunk lightmap pixels.

timfa Tim Falken · pushed by timfa

Authored 2026-09-06 · 2 months ago

Hash
ef1165994e1ba9b5406d9c28998679a3ae5dc143
Parent
95e465ec
2 files changed +78 −5
client/src/render/canvasSprite.js +53 −0
@@ -19,3 +19,56 @@ export function invalidateCanvasTexture (scene, canvas)
19 19 glContext.gl.deleteTexture(texture);
20 20 glContext.textures.delete(canvas);
21 21 }
22
23 /**
24 * Upload/replace RGBA pixels for a canvas-keyed GL texture.
25 * Reuses the existing texture via texSubImage2D when possible (no delete/recreate).
26 * @returns {boolean} true when a new texture was created
27 */
28 export function uploadCanvasTextureRgba (scene, canvas, pixels, width, height, filter = null)
29 {
30 const glContext = scene?.glContext;
31 const gl = glContext?.gl;
32 if (!gl || !glContext?.textures || !canvas || !pixels)
33 { return false; }
34
35 let texture = glContext.textures.get(canvas);
36 if (texture)
37 {
38 gl.bindTexture(gl.TEXTURE_2D, texture);
39 gl.texSubImage2D(
40 gl.TEXTURE_2D,
41 0,
42 0,
43 0,
44 width,
45 height,
46 gl.RGBA,
47 gl.UNSIGNED_BYTE,
48 pixels
49 );
50 return false;
51 }
52
53 texture = gl.createTexture();
54 gl.bindTexture(gl.TEXTURE_2D, texture);
55 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
56 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
57 const minFilter = filter ?? glContext.textureMinFilter ?? gl.LINEAR;
58 const magFilter = filter ?? glContext.textureMagFilter ?? gl.LINEAR;
59 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
60 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
61 gl.texImage2D(
62 gl.TEXTURE_2D,
63 0,
64 gl.RGBA,
65 width,
66 height,
67 0,
68 gl.RGBA,
69 gl.UNSIGNED_BYTE,
70 pixels
71 );
72 glContext.textures.set(canvas, texture);
73 return true;
74 }
@@ -19,3 +19,56 @@ export function invalidateCanvasTexture (scene, canvas)
19 glContext.gl.deleteTexture(texture);
19 glContext.gl.deleteTexture(texture);
20 glContext.textures.delete(canvas);
20 glContext.textures.delete(canvas);
21}
21}
22
23/**
24 * Upload/replace RGBA pixels for a canvas-keyed GL texture.
25 * Reuses the existing texture via texSubImage2D when possible (no delete/recreate).
26 * @returns {boolean} true when a new texture was created
27 */
28export function uploadCanvasTextureRgba (scene, canvas, pixels, width, height, filter = null)
29{
30 const glContext = scene?.glContext;
31 const gl = glContext?.gl;
32 if (!gl || !glContext?.textures || !canvas || !pixels)
33 { return false; }
34
35 let texture = glContext.textures.get(canvas);
36 if (texture)
37 {
38 gl.bindTexture(gl.TEXTURE_2D, texture);
39 gl.texSubImage2D(
40 gl.TEXTURE_2D,
41 0,
42 0,
43 0,
44 width,
45 height,
46 gl.RGBA,
47 gl.UNSIGNED_BYTE,
48 pixels
49 );
50 return false;
51 }
52
53 texture = gl.createTexture();
54 gl.bindTexture(gl.TEXTURE_2D, texture);
55 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
56 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
57 const minFilter = filter ?? glContext.textureMinFilter ?? gl.LINEAR;
58 const magFilter = filter ?? glContext.textureMagFilter ?? gl.LINEAR;
59 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
60 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
61 gl.texImage2D(
62 gl.TEXTURE_2D,
63 0,
64 gl.RGBA,
65 width,
66 height,
67 0,
68 gl.RGBA,
69 gl.UNSIGNED_BYTE,
70 pixels
71 );
72 glContext.textures.set(canvas, texture);
73 return true;
74}
client/src/render/worldView.js +25 −5
@@ -24,7 +24,8 @@ import { createChunkAtlasCache } from "./chunkAtlasCache.js";
24 24 import
25 25 {
26 26 frameworkImageFromCanvas,
27 invalidateCanvasTexture
27 invalidateCanvasTexture,
28 uploadCanvasTextureRgba
28 29 } from "./canvasSprite.js";
29 30 import { setNearestFilter } from "./sprites.js";
30 31 import { waterDebugUniforms, lavaDebugUniforms } from "../play/waterDebug.js";
@@ -5958,10 +5959,29 @@ export class WorldView
5958 5959 }
5959 5960 }
5960 5961 }
5961 const ctx = baked.lightmapCanvas.getContext("2d");
5962 ctx.putImageData(new ImageData(pixels, atlasPx, atlasPx), 0, 0);
5963 invalidateCanvasTexture(this.scene, baked.lightmapCanvas);
5964 this.#queueCanvasTextureWarm(baked.lightmapCanvas, baked.chunkX, baked.chunkY);
5962 // Keep canvas pixels in sync for Framework getTexture fallbacks, but upload
5963 // to GL via texSubImage2D when the texture already exists (no delete/recreate).
5964 if (!baked.lightmapImageData || baked.lightmapImageData.data !== pixels)
5965 { baked.lightmapImageData = new ImageData(pixels, atlasPx, atlasPx); }
5966 if (baked.lightmapUploadSize !== atlasPx)
5967 {
5968 invalidateCanvasTexture(this.scene, baked.lightmapCanvas);
5969 baked.lightmapUploadSize = atlasPx;
5970 }
5971 const gl = this.scene?.glContext?.gl;
5972 const created = uploadCanvasTextureRgba(
5973 this.scene,
5974 baked.lightmapCanvas,
5975 pixels,
5976 atlasPx,
5977 atlasPx,
5978 gl?.NEAREST ?? null
5979 );
5980 if (created)
5981 {
5982 const ctx = baked.lightmapCanvas.getContext("2d");
5983 ctx.putImageData(baked.lightmapImageData, 0, 0);
5984 }
5965 5985 baked.lightmapCacheKey = cacheKey;
5966 5986 }
5967 5987
@@ -24,7 +24,8 @@ import { createChunkAtlasCache } from "./chunkAtlasCache.js";
24import
24import
25{
25{
26 frameworkImageFromCanvas,
26 frameworkImageFromCanvas,
27 invalidateCanvasTexture
27 invalidateCanvasTexture,
28 uploadCanvasTextureRgba
28} from "./canvasSprite.js";
29} from "./canvasSprite.js";
29import { setNearestFilter } from "./sprites.js";
30import { setNearestFilter } from "./sprites.js";
30import { waterDebugUniforms, lavaDebugUniforms } from "../play/waterDebug.js";
31import { waterDebugUniforms, lavaDebugUniforms } from "../play/waterDebug.js";
@@ -5958,10 +5959,29 @@ export class WorldView
5958 }
5959 }
5959 }
5960 }
5960 }
5961 }
5961 const ctx = baked.lightmapCanvas.getContext("2d");
5962 // Keep canvas pixels in sync for Framework getTexture fallbacks, but upload
5962 ctx.putImageData(new ImageData(pixels, atlasPx, atlasPx), 0, 0);
5963 // to GL via texSubImage2D when the texture already exists (no delete/recreate).
5963 invalidateCanvasTexture(this.scene, baked.lightmapCanvas);
5964 if (!baked.lightmapImageData || baked.lightmapImageData.data !== pixels)
5964 this.#queueCanvasTextureWarm(baked.lightmapCanvas, baked.chunkX, baked.chunkY);
5965 { baked.lightmapImageData = new ImageData(pixels, atlasPx, atlasPx); }
5966 if (baked.lightmapUploadSize !== atlasPx)
5967 {
5968 invalidateCanvasTexture(this.scene, baked.lightmapCanvas);
5969 baked.lightmapUploadSize = atlasPx;
5970 }
5971 const gl = this.scene?.glContext?.gl;
5972 const created = uploadCanvasTextureRgba(
5973 this.scene,
5974 baked.lightmapCanvas,
5975 pixels,
5976 atlasPx,
5977 atlasPx,
5978 gl?.NEAREST ?? null
5979 );
5980 if (created)
5981 {
5982 const ctx = baked.lightmapCanvas.getContext("2d");
5983 ctx.putImageData(baked.lightmapImageData, 0, 0);
5984 }
5965 baked.lightmapCacheKey = cacheKey;
5985 baked.lightmapCacheKey = cacheKey;
5966 }
5986 }
5967
5987