Skip to content

Commit ce37664

Browse files
committed
Moved textureAllocator to separate repo.
1 parent cb01800 commit ce37664

18 files changed

+59
-604
lines changed

build/index.js

Lines changed: 6 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/index.js.map

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bun.lockb

39.7 KB
Binary file not shown.

example/package-lock.json

Lines changed: 0 additions & 98 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"dependencies": {
3131
"avl": "^1.5.3",
32-
"gl-matrix": "^3.4.3"
32+
"gl-matrix": "^3.4.3",
33+
"texture-slot-allocator": "github:jacklehamster/texture-slot-allocator"
3334
}
3435
}

src/GLEngine.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ import {
1414
TRANSFORM_LOC,
1515
SLOT_SIZE_LOC,
1616
CAM_LOC,
17+
MAX_TEXTURE_SIZE_LOC,
1718
} from './gl/attributes/Contants';
1819
import { TextureManager } from './gl/texture/TextureManager';
20+
import { ImageManager } from 'gl/texture/ImageManager';
1921
import vertexShader from 'generated/src/gl/resources/vertexShader.txt';
2022
import fragmentShader from 'generated/src/gl/resources/fragmentShader.txt';
2123
import { replaceTilda } from 'gl/utils/replaceTilda';
2224
import Matrix from 'gl/transform/Matrix';
2325
import { GLCamera } from 'gl/camera/GLCamera';
24-
import { ImageManager } from 'gl/texture/ImageManager';
25-
import { TextureSlotAllocator } from 'gl/texture/TextureSlotAllocator';
26+
//import { TextureSlotAllocator } from 'gl/texture/TextureSlotAllocator';
27+
import { TextureSlotAllocator } from 'texture-slot-allocator/src';
2628

2729
const DEFAULT_ATTRIBUTES: WebGLContextAttributes = {
2830
alpha: true,
@@ -93,8 +95,6 @@ export class GLEngine extends Disposable {
9395
this.camera = new GLCamera(this.gl, this.uniforms);
9496

9597
window.addEventListener('resize', this.checkCanvasSize.bind(this));
96-
97-
console.log(this.gl.getParameter(GL.MAX_VERTEX_ATTRIBS));
9898
}
9999

100100
checkCanvasSize() {
@@ -122,9 +122,12 @@ export class GLEngine extends Disposable {
122122
replaceTilda(vertexShader, replacementMap),
123123
replaceTilda(fragmentShader, replacementMap),
124124
);
125+
126+
console.log(performance.now(), 11);
125127
this.programs.useProgram(PROGRAM_NAME);
126128
console.log(this.uniforms.getUniformLocation(CAM_LOC));
127129

130+
console.log(performance.now(), 22);
128131
// enable depth
129132
this.gl.enable(GL.DEPTH_TEST);
130133
this.gl.depthFunc(GL.LESS);
@@ -140,6 +143,7 @@ export class GLEngine extends Disposable {
140143
this.gl.drawingBufferHeight,
141144
);
142145

146+
console.log(performance.now(), 33);
143147
{
144148
/*
145149
0 1
@@ -185,6 +189,7 @@ export class GLEngine extends Disposable {
185189
GL.STATIC_DRAW,
186190
);
187191
}
192+
console.log(performance.now(), 44);
188193

189194
{
190195
const location = TRANSFORM_LOC;
@@ -228,6 +233,13 @@ export class GLEngine extends Disposable {
228233
0,
229234
GL.DYNAMIC_DRAW,
230235
);
236+
237+
const loc = this.uniforms.getUniformLocation(MAX_TEXTURE_SIZE_LOC);
238+
if (loc) {
239+
this.gl.uniform1f(loc, this.textureAllocator.maxTextureSize);
240+
}
241+
242+
console.log(performance.now(), 55);
231243
}
232244

233245
await this.loadLogoTexture();
@@ -265,8 +277,9 @@ export class GLEngine extends Disposable {
265277
}
266278

267279
async loadLogoTexture() {
268-
const TEXTURE_SLOT_SIZE = 512;
269-
const LOGO_SIZE = 2048;
280+
const maxTextureSize = this.textureAllocator.maxTextureSize;
281+
const TEXTURE_SLOT_SIZE = maxTextureSize;
282+
const LOGO_SIZE = maxTextureSize / 4;
270283
await this.imageManager.drawImage('logo', (ctx) => {
271284
const { canvas } = ctx;
272285
canvas.width = LOGO_SIZE;
@@ -320,16 +333,23 @@ export class GLEngine extends Disposable {
320333
ctx.stroke();
321334
});
322335

336+
console.log(performance.now(), 1);
323337
const slot = this.textureAllocator.allocate(TEXTURE_SLOT_SIZE, TEXTURE_SLOT_SIZE);
324338
const logoMediaInfo = this.imageManager.getMedia('logo');
339+
console.log(performance.now(), 2);
325340
this.textureManager.applyImageToSlot(logoMediaInfo, slot)
341+
console.log(performance.now(), 3);
326342

327343
const groundSlot = this.textureAllocator.allocate(TEXTURE_SLOT_SIZE, TEXTURE_SLOT_SIZE);
344+
console.log(performance.now(), 4);
328345
const groundMediaInfo = this.imageManager.getMedia('ground');
346+
console.log(performance.now(), 5);
329347
this.textureManager.applyImageToSlot(groundMediaInfo, groundSlot)
348+
console.log(performance.now(), 6);
330349

331350
console.log(slot, groundSlot);
332351

352+
console.log(performance.now(), 7);
333353
this.textureManager.generateMipMap("TEXTURE0");
334354

335355
{

src/gl/attributes/Contants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ export const SLOT_SIZE_LOC: LocationName = 'slotSize_and_number';
1717
// Uniform
1818
export const CAM_LOC: LocationName = 'cam';
1919
export const PROJECTION_LOC: LocationName = 'projection';
20+
export const MAX_TEXTURE_SIZE_LOC: LocationName = 'maxTextureSize';
2021

2122
export const DEBUG = false;

src/gl/camera/GLCamera.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class GLCamera {
3131
camTiltMatrix = Matrix.create().getMatrix();
3232
camTurnMatrix = Matrix.create().getMatrix();
3333
private camMatrix: mat4 = mat4.create();
34+
3435
refresh() {
3536
// camMatrix = camTiltMatrix * camTurnMatrix * cameraMatrix;
3637
mat4.mul(this.camMatrix, this.camTiltMatrix, this.camTurnMatrix);

src/gl/resources/vertexShader.vert

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
precision highp float;
55

6-
const float TEXTURE_SIZE = 4096.;
7-
86
// IN
97
// shape
108
layout(location = 0) in vec4 position_and_tex;
@@ -14,6 +12,7 @@ layout(location = 1) in mat4 transform;
1412
layout(location = 5) in vec3 slotSize_and_number;
1513

1614
// UNIFORM
15+
uniform float maxTextureSize;
1716
uniform mat4 cam;
1817
uniform mat4 projection;
1918

@@ -29,6 +28,6 @@ void main() {
2928
vec2 slotOffset = vec2(mod(slotNumber, slotSize.x), floor(slotNumber / slotSize.x));
3029

3130
gl_Position = projection * cam * transform * vec4(position, 0.0, 1.0);
32-
vTex = (slotOffset + tex) * (slotSize / TEXTURE_SIZE);
31+
vTex = (slotOffset + tex) * (slotSize / maxTextureSize);
3332
textureIndex = 0.;
3433
}

src/gl/texture/TextureManager.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Disposable } from '../../disposable/Disposable';
22
import { GL } from '../attributes/Contants';
33
import { MediaInfo } from './MediaInfo';
4-
import { Slot, calculatePosition, calculateTextureIndex } from './TextureSlot';
5-
6-
export type TextureIndex = 0 | 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;
4+
import { Slot, TextureIndex } from "texture-slot-allocator/dist/src/texture/TextureSlot";
75

86
type TextureId = `TEXTURE${TextureIndex}`;
97

@@ -128,9 +126,7 @@ export class TextureManager extends Disposable {
128126
* @returns null or a callback to refresh the texture. Mainly used to refresh videos on texture
129127
*/
130128
applyImageToSlot(mediaInfo: MediaInfo, slot: Slot, generateMipMap: boolean = false): (() => void) | null {
131-
const slotPosition = calculatePosition(slot);
132-
const textureIndex: TextureIndex = calculateTextureIndex(slot);
133-
const textureId: TextureId = `TEXTURE${textureIndex}`;
129+
const textureId: TextureId = `TEXTURE${slot.textureIndex}`;
134130
const webGLTexture = this.getTexture(textureId);
135131
if (!webGLTexture) {
136132
console.warn(`Invalid texture Id ${textureId}`);
@@ -142,7 +138,7 @@ export class TextureManager extends Disposable {
142138
textureId,
143139
webGLTexture,
144140
[0, 0, mediaInfo.width, mediaInfo.height],
145-
[slotPosition.x, slotPosition.y, ...slotPosition.size],
141+
[slot.x, slot.y, ...slot.size],
146142
);
147143
if (generateMipMap) {
148144
this.generateMipMap(textureId);

src/gl/texture/TexturePosition.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/gl/texture/TextureSlot.test.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)