Skip to content

Commit a33f03c

Browse files
authored
Add shortcut for glEnumStr() => wtu.glEnumToString(), and cache WebGLRenderingContext versions for glEnumToString eagerly, to make glOrExt arg optional for most cases. (#3720)
Also glEnumToString now sorts the list of enum names it returns.
1 parent b7ad199 commit a33f03c

File tree

1 file changed

+91
-20
lines changed

1 file changed

+91
-20
lines changed

sdk/tests/js/webgl-test-utils.js

Lines changed: 91 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,87 @@ var loggingOff = function() {
3434
};
3535

3636
const ENUM_NAME_REGEX = RegExp('[A-Z][A-Z0-9_]*');
37-
const ENUM_NAME_BY_VALUE = {};
38-
const ENUM_NAME_PROTOTYPES = new Map();
37+
const ENUM_SOURCES_CACHED = {};
38+
const ENUM_LIST_BY_VALUE = new Map();
39+
40+
/**
41+
* @param {Map} dest
42+
* @param {object} src
43+
* @param {?(s: string) => bool} fn_filter_key The enum value.
44+
*/
45+
function accumKeysByValue(dest, src, fn_filter_key) {
46+
for (const [key,val] of Object.entries(src)) {
47+
if (fn_filter_key && !fn_filter_key(key)) continue;
48+
49+
let keys = dest.get(val);
50+
if (!keys) {
51+
keys = [];
52+
dest.set(val, keys);
53+
}
54+
if (!keys.includes(key)) {
55+
keys.push(key);
56+
}
57+
}
58+
}
59+
60+
function accumGlEnumsByValue(src) {
61+
console.assert(src, 'bad src');
62+
if (!src.name) {
63+
src = src.constructor;
64+
}
65+
if (ENUM_SOURCES_CACHED[src.name]) return;
66+
ENUM_SOURCES_CACHED[src.name] = true;
67+
accumKeysByValue(ENUM_LIST_BY_VALUE, src, k => ENUM_NAME_REGEX.test(k));
68+
}
3969

4070
/**
4171
* Converts a WebGL enum to a string.
42-
* @param {!WebGLRenderingContext} gl The WebGLRenderingContext to use.
72+
* @param {object?} glOrExt What object is this from, needed if not in WebGL2RenderingContext.
4373
* @param {number} value The enum value.
44-
* @return {string} The enum as a string.
74+
* @return {string} If found, enum name(s) as a string, else value hex string e.g. `0x1234`.
4575
*/
46-
var glEnumToString = function(glOrExt, value) {
47-
if (value === undefined)
76+
function glEnumToString(glOrExt, value) {
77+
if (value === undefined) {
4878
throw new Error('glEnumToString: `value` must not be undefined');
79+
}
4980

50-
const proto = glOrExt.__proto__;
51-
if (!ENUM_NAME_PROTOTYPES.has(proto)) {
52-
ENUM_NAME_PROTOTYPES.set(proto, true);
81+
let found = ENUM_LIST_BY_VALUE.get(value);
5382

54-
for (const k in proto) {
55-
if (!ENUM_NAME_REGEX.test(k)) continue;
83+
if (!found && !ENUM_LIST_BY_VALUE.size) {
84+
ENUM_LIST_BY_VALUE.set(0, ['NONE']); // List NONE before POINTS.
85+
accumGlEnumsByValue(globalThis.WebGL2RenderingContext || WebGLRenderingContext);
86+
found = ENUM_LIST_BY_VALUE.get(value);
87+
}
5688

57-
const v = glOrExt[k];
58-
if (ENUM_NAME_BY_VALUE[v] === undefined) {
59-
ENUM_NAME_BY_VALUE[v] = k;
60-
} else {
61-
ENUM_NAME_BY_VALUE[v] += '/' + k;
89+
if (!found && glOrExt) {
90+
const CACHE_UNUSUAL_CLASSES = false;
91+
if (!CACHE_UNUSUAL_CLASSES) {
92+
for (const [k,v] of Object.entries(glOrExt)) {
93+
if (v == value) {
94+
found = [k];
95+
break;
96+
}
6297
}
98+
} else {
99+
accumGlEnumsByValue(glOrExt);
100+
found = ENUM_LIST_BY_VALUE.get(value);
63101
}
64102
}
65103

66-
const key = ENUM_NAME_BY_VALUE[value];
67-
if (key !== undefined) return key;
104+
if (!found) {
105+
found = ["0x" + Number(value).toString(16)];
106+
}
107+
found = found.toSorted();
108+
return found.join('/');
109+
}
110+
{
111+
let was, expect;
112+
console.assert((was = glEnumToString(null, WebGLRenderingContext.RGBA), expect = 'RGBA', was == expect), {was,expect});
113+
console.assert((was = glEnumToString(null, 0x123456), expect = '0x123456', was == expect), {was,expect});
114+
console.assert((was = glEnumToString({name: 'FakeRenderingContext', UNUSUAL: 0x123456}, 0x123456), expect = 'UNUSUAL', was == expect), {was,expect});
115+
}
68116

69-
return "0x" + Number(value).toString(16);
70-
};
117+
// -
71118

72119
var lastError = "";
73120

@@ -3675,3 +3722,27 @@ Object.defineProperties(API, {
36753722
return API;
36763723

36773724
}());
3725+
3726+
// -
3727+
// Useful shortcuts:
3728+
3729+
/**
3730+
* @typedef {number} GLenum
3731+
*/
3732+
/**
3733+
* * `glEnumStr(GL.RGBA) => 'RGBA'`
3734+
* * `glEnumStr(GL.POINTS) => 'NONE/NO_ERROR/POINTS/ZERO'`
3735+
* * `glEnumStr(0x123456) => '0x123456'`
3736+
* @param {GLenum} val
3737+
* @returns {string}
3738+
*/
3739+
function glEnumStr(val) {
3740+
return WebGLTestUtils.glEnumToString(null, val);
3741+
}
3742+
{
3743+
const GL = WebGLRenderingContext;
3744+
let was;
3745+
console.assert((was = glEnumStr(GL.RGBA)) == 'RGBA', was);
3746+
console.assert((was = glEnumStr(GL.POINTS)) == 'NONE/NO_ERROR/POINTS/ZERO', was);
3747+
console.assert((was = glEnumStr(0x123456)) == '0x123456', was);
3748+
}

0 commit comments

Comments
 (0)