bugfix: don't cache textures bound to a specific GLContext

This commit is contained in:
Ilion Beyst 2022-07-18 21:55:35 +02:00
parent 7daf8f6437
commit 338dc6ac63
2 changed files with 31 additions and 26 deletions

View file

@ -24,7 +24,7 @@ import { VertexBufferLayout, VertexArray } from "./webgl/vertexBufferLayout";
import { defaultLabelFactory, LabelFactory, Align, Label } from "./webgl/text"; import { defaultLabelFactory, LabelFactory, Align, Label } from "./webgl/text";
import { VoronoiBuilder } from "./voronoi/voronoi"; import { VoronoiBuilder } from "./voronoi/voronoi";
import * as assets from "./assets"; import * as assets from "./assets";
import { Texture } from "./webgl/texture"; import { loadImage, Texture } from "./webgl/texture";
function to_bbox(box: number[]): BBox { function to_bbox(box: number[]): BBox {
@ -585,16 +585,16 @@ export class GameInstance {
} }
var game_instance: GameInstance; var game_instance: GameInstance;
var textures: Texture[]; var texture_images: HTMLImageElement[];
var shaders: Dictionary<ShaderFactory>; var shaders: Dictionary<ShaderFactory>;
export async function set_instance(source: string): Promise<GameInstance> { export async function set_instance(source: string): Promise<GameInstance> {
// TODO: embed shader programs // TODO: this loading code is a mess. Please clean me up!
if (!textures || !shaders) { if (!texture_images || !shaders) {
const texture_promises = [ const image_promises = [
Texture.fromImage(GL, assets.fontPng, "font"), loadImage(assets.fontPng),
Texture.fromImage(GL, assets.shipPng, "ship"), loadImage(assets.shipPng),
Texture.fromImage(GL, assets.earthPng, "earth") loadImage(assets.earthPng),
]; ];
const shader_promies = [ const shader_promies = [
@ -633,8 +633,8 @@ export async function set_instance(source: string): Promise<GameInstance> {
]; ];
let shaders_array: [string, ShaderFactory][]; let shaders_array: [string, ShaderFactory][];
[textures, shaders_array] = await Promise.all([ [texture_images, shaders_array] = await Promise.all([
Promise.all(texture_promises), Promise.all(image_promises),
Promise.all(shader_promies), Promise.all(shader_promies),
]); ]);
@ -643,12 +643,15 @@ export async function set_instance(source: string): Promise<GameInstance> {
} }
resizeCanvasToDisplaySize(CANVAS); resizeCanvasToDisplaySize(CANVAS);
const fontTexture = Texture.fromImage(GL, texture_images[0], "font");
const shipTexture = Texture.fromImage(GL, texture_images[1], "ship");
const earthTexture = Texture.fromImage(GL, texture_images[2], "earth");
game_instance = new GameInstance( game_instance = new GameInstance(
Game.new(source), Game.new(source),
textures.slice(2), [earthTexture],
textures[1], shipTexture,
textures[0], fontTexture,
shaders shaders
); );

View file

@ -1,5 +1,15 @@
import type { Renderer } from "./renderer"; import type { Renderer } from "./renderer";
export function loadImage(image_url: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const image = new Image();
image.onload = () => { resolve(image) }
image.onerror = reject;
image.src = image_url;
})
}
export class Texture { export class Texture {
texture: WebGLTexture; texture: WebGLTexture;
width: number; width: number;
@ -9,20 +19,12 @@ export class Texture {
static fromImage( static fromImage(
gl: WebGLRenderingContext, gl: WebGLRenderingContext,
path: string, image: HTMLImageElement,
name: string, name: string,
): Promise<Texture> { ): Texture {
return new Promise((resolve, reject) => { const tx = new Texture(gl, name);
const out = new Texture(gl, name); tx.setImage(gl, image);
return tx;
const image = new Image();
image.onload = () => {
out.setImage(gl, image);
resolve(out);
}
image.onerror = reject;
image.src = path;
})
} }
static fromRenderer( static fromRenderer(