Skip to content

Commit 93b69c9

Browse files
authored
Merge branch 'main' into feat/inspector-perf-metrics
2 parents 3dc37e5 + 8f3a738 commit 93b69c9

File tree

5 files changed

+159
-37
lines changed

5 files changed

+159
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lightningjs/renderer",
3-
"version": "3.0.0-beta12",
3+
"version": "3.0.0-beta14",
44
"description": "Lightning 3 Renderer",
55
"type": "module",
66
"main": "./dist/exports/index.js",

src/core/lib/WebGlContextWrapper.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,16 +716,17 @@ export class WebGlContextWrapper {
716716
* @param program
717717
* @returns object with numbers
718718
*/
719-
getAttributeLocations(program: WebGLProgram): Record<string, number> {
719+
getAttributeLocations(program: WebGLProgram): string[] {
720720
const gl = this.gl;
721721
const length = gl.getProgramParameter(
722722
program,
723723
gl.ACTIVE_ATTRIBUTES,
724724
) as number;
725-
const result = {} as Record<string, number>;
725+
726+
const result: string[] = [];
726727
for (let i = 0; i < length; i++) {
727728
const { name } = gl.getActiveAttrib(program, i) as WebGLActiveInfo;
728-
result[name] = i;
729+
result[gl.getAttribLocation(program, name)] = name;
729730
}
730731
return result;
731732
}

src/core/renderers/webgl/WebGlShaderProgram.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class WebGlShaderProgram implements CoreShaderProgram {
4545
protected vao: WebGLVertexArrayObject | undefined;
4646
protected renderer: WebGlRenderer;
4747
protected glw: WebGlContextWrapper;
48-
protected attributeLocations: Record<string, number>;
48+
protected attributeLocations: string[];
4949
protected uniformLocations: Record<string, WebGLUniformLocation> | null;
5050
protected lifecycle: Pick<WebGlShaderType, 'update' | 'canBatch'>;
5151
protected useSystemAlpha = false;
@@ -135,8 +135,7 @@ export class WebGlShaderProgram implements CoreShaderProgram {
135135

136136
disableAttributes() {
137137
const glw = this.glw;
138-
const attribs = Object.keys(this.attributeLocations);
139-
const attribLen = attribs.length;
138+
const attribLen = this.attributeLocations.length;
140139
for (let i = 0; i < attribLen; i++) {
141140
glw.disableVertexAttribArray(i);
142141
}
@@ -278,7 +277,7 @@ export class WebGlShaderProgram implements CoreShaderProgram {
278277

279278
bindBufferCollection(buffer: BufferCollection) {
280279
const { glw } = this;
281-
const attribs = Object.keys(this.attributeLocations);
280+
const attribs = this.attributeLocations;
282281
const attribLen = attribs.length;
283282

284283
for (let i = 0; i < attribLen; i++) {
@@ -332,8 +331,8 @@ export class WebGlShaderProgram implements CoreShaderProgram {
332331
this.program = null;
333332
this.uniformLocations = null;
334333

335-
const attribs = Object.keys(this.attributeLocations);
336-
const attribLen = attribs.length;
334+
const attribs = this.attributeLocations;
335+
const attribLen = this.attributeLocations.length;
337336
for (let i = 0; i < attribLen; i++) {
338337
this.glw.deleteBuffer(attribs[i]!);
339338
}

src/core/text-rendering/sdf/Utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ export const measureLines = (
3636

3737
while (remainingLines > 0) {
3838
const line = lines[i];
39+
i++;
40+
remainingLines--;
3941
if (line === undefined) {
4042
continue;
4143
}
4244
const width = measureText(line, fontFamily, designLetterSpacing);
4345
measuredLines.push([line, width]);
44-
i++;
45-
remainingLines--;
4646
}
4747

4848
return [

src/main-api/Renderer.ts

Lines changed: 147 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,127 @@ import type {
3838
import { WebPlatform } from '../core/platforms/web/WebPlatform.js';
3939
import { Platform } from '../core/platforms/Platform.js';
4040

41+
/**
42+
* FPS Update Event Data
43+
*
44+
* @category Events
45+
* @example
46+
* ```typescript
47+
* renderer.on('fpsUpdate', (data) => {
48+
* console.log(`Current FPS: ${data.fps}`);
49+
* if (data.contextSpyData) {
50+
* console.log('WebGL calls:', data.contextSpyData);
51+
* }
52+
* });
53+
* ```
54+
*/
55+
export interface RendererMainFpsUpdateEvent {
56+
/** Current frames per second */
57+
fps: number;
58+
/** Context spy data (if enabled) - contains WebGL call statistics */
59+
contextSpyData?: unknown;
60+
}
61+
62+
/**
63+
* Frame Tick Event Data
64+
*
65+
* @category Events
66+
* @example
67+
* ```typescript
68+
* renderer.on('frameTick', (data) => {
69+
* console.log(`Frame time: ${data.time}ms, delta: ${data.delta}ms`);
70+
* });
71+
* ```
72+
*/
73+
export interface RendererMainFrameTickEvent {
74+
/** Current timestamp */
75+
time: number;
76+
/** Time delta since last frame */
77+
delta: number;
78+
}
79+
80+
/**
81+
* Quads Update Event Data
82+
*
83+
* @category Events
84+
* @example
85+
* ```typescript
86+
* renderer.on('quadsUpdate', (data) => {
87+
* console.log(`Rendered quads: ${data.quads}`);
88+
* });
89+
* ```
90+
*/
91+
export interface RendererMainQuadsUpdateEvent {
92+
/** Number of rendered quads */
93+
quads: number;
94+
}
95+
96+
/**
97+
* Idle Event Data
98+
*
99+
* @category Events
100+
* @remarks
101+
* This event is emitted when the renderer has no scene updates to process.
102+
* The event has no payload - use this for performance optimizations during idle periods.
103+
*
104+
* @example
105+
* ```typescript
106+
* renderer.on('idle', () => {
107+
* // Renderer is idle - perfect time for cleanup, analytics, etc.
108+
* console.log('Renderer is idle - no scene changes');
109+
*
110+
* // Example: Perform background tasks
111+
* performBackgroundCleanup();
112+
* sendAnalytics();
113+
* });
114+
* ```
115+
*/
116+
export interface RendererMainIdleEvent {
117+
/** This event has no payload - listen without parameters */
118+
readonly __eventHasNoPayload?: never;
119+
}
120+
121+
/**
122+
* Critical Cleanup Event Data
123+
*
124+
* @category Events
125+
* @example
126+
* ```typescript
127+
* renderer.on('criticalCleanup', (data) => {
128+
* console.log(`Memory cleanup triggered!`);
129+
* console.log(`Memory used: ${data.memUsed} bytes`);
130+
* console.log(`Critical threshold: ${data.criticalThreshold} bytes`);
131+
* });
132+
* ```
133+
*/
134+
export interface RendererMainCriticalCleanupEvent {
135+
/** Memory used before cleanup (bytes) */
136+
memUsed: number;
137+
/** Critical threshold (bytes) */
138+
criticalThreshold: number;
139+
}
140+
141+
/**
142+
* Critical Cleanup Failed Event Data
143+
*
144+
* @category Events
145+
* @example
146+
* ```typescript
147+
* renderer.on('criticalCleanupFailed', (data) => {
148+
* console.warn(`Memory cleanup failed!`);
149+
* console.log(`Memory still used: ${data.memUsed} bytes`);
150+
* console.log(`Critical threshold: ${data.criticalThreshold} bytes`);
151+
* // Consider reducing texture usage or forcing cleanup
152+
* });
153+
* ```
154+
*/
155+
export interface RendererMainCriticalCleanupFailedEvent {
156+
/** Memory used after cleanup (bytes) */
157+
memUsed: number;
158+
/** Critical threshold (bytes) */
159+
criticalThreshold: number;
160+
}
161+
41162
/**
42163
* Settings for the Renderer that can be updated during runtime.
43164
*/
@@ -330,31 +451,32 @@ export type RendererMainSettings = RendererRuntimeSettings & {
330451
* );
331452
* ```
332453
*
333-
* ## Events
334-
* - `fpsUpdate`
335-
* - Emitted every `fpsUpdateInterval` milliseconds with the current FPS
336-
* - `frameTick`
337-
* - Emitted every frame tick
338-
* - `quadsUpdate`
339-
* - Emitted when number of quads rendered is updated
340-
* - `idle`
341-
* - Emitted when the renderer is idle (no changes to the scene
342-
* graph/animations running)
343-
* - `criticalCleanup`
344-
* - Emitted when the Texture Memory Manager Cleanup process is triggered
345-
* - Payload: { memUsed: number, criticalThreshold: number }
346-
* - `memUsed` - The amount of memory (in bytes) used by textures before the
347-
* cleanup process
348-
* - `criticalThreshold` - The critical threshold (in bytes)
349-
* - `criticalCleanupFailed`
350-
* - Emitted when the Texture Memory Manager Cleanup process is unable to free
351-
* up enough texture memory to reach below the critical threshold.
352-
* This can happen when there is not enough non-renderable textures to
353-
* free up.
354-
* - Payload (object with keys):
355-
* - `memUsed` - The amount of memory (in bytes) used by textures after
356-
* the cleanup process
357-
* - `criticalThreshold` - The critical threshold (in bytes)
454+
* ## Event Handling
455+
*
456+
* Listen to events using the standard EventEmitter API:
457+
* ```typescript
458+
* renderer.on('fpsUpdate', (data: RendererMainFpsUpdateEvent) => {
459+
* console.log(`FPS: ${data.fps}`);
460+
* });
461+
*
462+
* renderer.on('idle', (data: RendererMainIdleEvent) => {
463+
* // Renderer is idle - no scene changes
464+
* });
465+
* ```
466+
*
467+
* @see {@link RendererMainFpsUpdateEvent}
468+
* @see {@link RendererMainFrameTickEvent}
469+
* @see {@link RendererMainQuadsUpdateEvent}
470+
* @see {@link RendererMainIdleEvent}
471+
* @see {@link RendererMainCriticalCleanupEvent}
472+
* @see {@link RendererMainCriticalCleanupFailedEvent}
473+
*
474+
* @fires RendererMain#fpsUpdate
475+
* @fires RendererMain#frameTick
476+
* @fires RendererMain#quadsUpdate
477+
* @fires RendererMain#idle
478+
* @fires RendererMain#criticalCleanup
479+
* @fires RendererMain#criticalCleanupFailed
358480
*/
359481
export class RendererMain extends EventEmitter {
360482
readonly root: INode;

0 commit comments

Comments
 (0)