Skip to content

Commit

Permalink
Track object creation (and texture update) stack trace and expose tra…
Browse files Browse the repository at this point in the history
…cked textures in the extension info call.
  • Loading branch information
sergeystoma authored and greggman committed Aug 3, 2024
1 parent a4e0db7 commit c769105
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/augment-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
getDrawingbufferInfo,
isBufferSource,
isNumber,
getStackTrace,
collectObjects,
} from './utils.js';

//------------ [ from https://github.com/KhronosGroup/WebGLDeveloperTools ]
Expand Down Expand Up @@ -87,6 +89,7 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
ctx: {
getMemoryInfo() {
const drawingbuffer = computeDrawingbufferSize(ctx, drawingBufferInfo);
const textures = collectObjects(sharedState, 'WebGLTexture');
return {
memory: {
...memory,
Expand All @@ -96,6 +99,7 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
resources: {
...resources,
},
textures,
};
},
},
Expand Down Expand Up @@ -203,6 +207,7 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {
++resources[typeName];
webglObjectToMemory.set(webglObj, {
size: 0,
stackCreated: getStackTrace(),
});
};
}
Expand Down Expand Up @@ -314,6 +319,8 @@ export function augmentAPI(ctx, nameOfClass, options = {}) {

memory.texture -= oldSize;
memory.texture += info.size;

info.stackUpdated = getStackTrace();
}

function updateTexStorage(target, levels, internalFormat, width, height, depth) {
Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,23 @@ export function computeDrawingbufferSize(gl, drawingBufferInfo) {
export function isNumber(v) {
return typeof v === 'number';
}

export function collectObjects(state, type) {
const list = [...state.webglObjectToMemory.keys()]
.filter((obj) => obj.constructor.name === type)
.map((obj) => state.webglObjectToMemory.get(obj));

return list;
}

function cleanStackLine(line) {
return line.replace(/^\s*at\s+/, '');
}

export function getStackTrace() {
const stack = (new Error()).stack;
const lines = stack.split('\n');
// Remove the first two entries, the error message and this function itself, or the webgl-memory itself.
const userLines = lines.slice(2).filter((l) => !l.includes('webgl-memory'));
return userLines.map((l) => cleanStackLine(l));
}
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import './tests/sync-tests.js';
import './tests/texture-tests.js';
import './tests/transformfeedback-tests.js';
import './tests/vertexarray-tests.js';
import './tests/stack-tests.js';

const settings = Object.fromEntries(new URLSearchParams(window.location.search).entries());
if (settings.reporter) {
Expand Down
8 changes: 5 additions & 3 deletions test/tests/info-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe('info tests', () => {
assertEqual(drawingbufferSize, canvasSize);

const info = ext.getMemoryInfo();
const {memory, resources} = info;
const {memory, resources, textures} = info;

assertEqual(memory.buffer, 0);
assertEqual(memory.texture, 0);
Expand All @@ -40,14 +40,15 @@ describe('info tests', () => {
assertEqual(resources.texture, 0);
assertEqual(resources.transformFeedback, undefined);
assertEqual(resources.vertexArray, undefined);
assertEqual(textures.length, 0);
});

it('test base state webgl2', () => {
const {ext, drawingbufferSize} = createContext2();
assertTruthy(ext, 'got extension');

const info = ext.getMemoryInfo();
const {memory, resources} = info;
const {memory, resources, textures} = info;

assertEqual(memory.buffer, 0);
assertEqual(memory.texture, 0);
Expand All @@ -65,6 +66,7 @@ describe('info tests', () => {
assertEqual(resources.texture, 0);
assertEqual(resources.transformFeedback, 0);
assertEqual(resources.vertexArray, 0);
assertEqual(textures.length, 0);
});

it('test canvas resize', () => {
Expand Down Expand Up @@ -92,4 +94,4 @@ describe('info tests', () => {

});

});
});
24 changes: 24 additions & 0 deletions test/tests/stack-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {describe, it} from '../mocha-support.js';
import {assertEqual, assertTruthy} from '../assert.js';
import {createContext} from '../webgl.js';

describe('stack tests', () => {

it('test stack capture', () => {
const {gl, ext} = createContext();

const tex1 = gl.createTexture();

gl.bindTexture(gl.TEXTURE_2D, tex1);
gl.texImage2D(gl.TEXTURE_2D, 1, gl.RGBA, 16, 8, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

const info = ext.getMemoryInfo();
const {textures} = info;

assertEqual(textures.length, 1);
assertTruthy(textures[0].stackCreated);
assertTruthy(textures[0].stackUpdated);

gl.deleteTexture(tex1);
});
});

0 comments on commit c769105

Please sign in to comment.