Skip to content

Commit 09f714a

Browse files
impr: console and Math method reference checks (#2438)
1 parent db38f03 commit 09f714a

11 files changed

Lines changed: 204 additions & 150 deletions

File tree

packages/typegpu/src/data/dataTypes.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { setName, type TgpuNamable } from '../shared/meta.ts';
1+
import { type TgpuNamable } from '../shared/meta.ts';
22
import { isMarkedInternal } from '../shared/symbols.ts';
33
import type {
44
Infer,
@@ -23,7 +23,6 @@ import type {
2323
$reprPatch,
2424
$validVertexSchema,
2525
} from '../shared/symbols.ts';
26-
import { $internal } from '../shared/symbols.ts';
2726
import type { Prettify } from '../shared/utilityTypes.ts';
2827
import { vertexFormats } from '../shared/vertexFormat.ts';
2928
import type { WgslExternalTexture, WgslStorageTexture, WgslTexture } from './texture.ts';
@@ -261,13 +260,3 @@ export class MatrixColumnsAccess {
261260
this.matrix = matrix;
262261
}
263262
}
264-
265-
export class ConsoleLog {
266-
[$internal] = true;
267-
readonly op: string;
268-
269-
constructor(op: string) {
270-
this.op = op;
271-
setName(this, 'consoleLog');
272-
}
273-
}

packages/typegpu/src/resolutionCtx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
type TgpuLayoutEntry,
3232
} from './tgpuBindGroupLayout.ts';
3333
import { LogGeneratorImpl, LogGeneratorNullImpl } from './tgsl/consoleLog/logGenerator.ts';
34-
import type { LogGenerator, LogResources } from './tgsl/consoleLog/types.ts';
34+
import type { LogGenerator, LogResources, SupportedLogOp } from './tgsl/consoleLog/types.ts';
3535
import { getBestConversion } from './tgsl/conversion.ts';
3636
import { coerceToSnippet, concretize, numericLiteralToSnippet } from './tgsl/generationHelpers.ts';
3737
import type { ShaderGenerator } from './tgsl/shaderGenerator.ts';
@@ -458,7 +458,7 @@ export class ResolutionCtxImpl implements ResolutionCtx {
458458
this._itemStateStack.clearBlockExternals();
459459
}
460460

461-
generateLog(op: string, args: Snippet[]): Snippet {
461+
generateLog(op: SupportedLogOp, args: Snippet[]): Snippet {
462462
return this.#logGenerator.generateLog(this, op, args);
463463
}
464464

packages/typegpu/src/tgsl/consoleLog/deserializers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function logDataFromGPU(resources: LogResources) {
174174
if (results.length === 0) {
175175
results.push('');
176176
}
177-
console[op](
177+
op.bind(console)(
178178
`%c${options.messagePrefix}%c ${results[0]}`,
179179
'background: #936ff5; color: white;',
180180
'color: inherit; background: none',

packages/typegpu/src/tgsl/consoleLog/logGenerator.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import {
2626
type LogMeta,
2727
type LogResources,
2828
type SerializedLogCallData,
29-
type SupportedLogOps,
30-
supportedLogOps,
29+
type SupportedLogOp,
3130
} from './types.ts';
3231

3332
const defaultOptions: Required<LogGeneratorOptions> = {
@@ -78,14 +77,9 @@ export class LogGeneratorImpl implements LogGenerator {
7877
* @param args Argument snippets. Snippets of UnknownType will be treated as string literals.
7978
* @returns A snippet containing the call to the logging function.
8079
*/
81-
generateLog(ctx: GenerationCtx, op: string, args: Snippet[]): Snippet {
80+
generateLog(ctx: GenerationCtx, op: SupportedLogOp, args: Snippet[]): Snippet {
8281
if (shaderStageSlot.$ === 'vertex') {
83-
console.warn(`'console.${op}' is not supported in vertex shaders.`);
84-
return fallbackSnippet;
85-
}
86-
87-
if (!supportedLogOps.includes(op as SupportedLogOps)) {
88-
console.warn(`Unsupported log method '${op}'.`);
82+
console.warn(`'console' operations are not supported in vertex shaders.`);
8983
return fallbackSnippet;
9084
}
9185

@@ -122,7 +116,7 @@ export class LogGeneratorImpl implements LogGenerator {
122116
);
123117

124118
this.#logIdToMeta.set(id, {
125-
op: op as SupportedLogOps,
119+
op,
126120
argTypes: concreteArgsWithStrings.map((e) =>
127121
e?.dataType === UnknownData ? (e?.value as string) : (e?.dataType as AnyWgslData),
128122
),

packages/typegpu/src/tgsl/consoleLog/types.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import type { TgpuMutable } from '../../core/buffer/bufferShorthand.ts';
22
import type { Snippet } from '../../data/snippet.ts';
33
import type { AnyWgslData, Atomic, U32, WgslArray, WgslStruct } from '../../data/wgslTypes.ts';
44
import type { GenerationCtx } from '../generationHelpers.ts';
5+
import type { supportedLogOps } from '../jsPolyfills.ts';
6+
7+
export type SupportedLogOp = ReturnType<typeof supportedLogOps>[number];
58

69
/**
710
* Options for configuring GPU log generation.
@@ -32,7 +35,7 @@ export type SerializedLogCallData = WgslStruct<{
3235
}>;
3336

3437
export interface LogMeta {
35-
op: SupportedLogOps;
38+
op: SupportedLogOp;
3639
argTypes: (string | AnyWgslData)[];
3740
}
3841

@@ -52,10 +55,6 @@ export interface LogResources {
5255
}
5356

5457
export interface LogGenerator {
55-
generateLog(ctx: GenerationCtx, op: string, args: Snippet[]): Snippet;
58+
generateLog(ctx: GenerationCtx, op: SupportedLogOp, args: Snippet[]): Snippet;
5659
get logResources(): LogResources | undefined;
5760
}
58-
59-
export const supportedLogOps = ['log', 'debug', 'info', 'warn', 'error', 'clear'] as const;
60-
61-
export type SupportedLogOps = (typeof supportedLogOps)[number];

packages/typegpu/src/tgsl/generationHelpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type { ShelllessRepository } from './shellless.ts';
2929
import { stitch } from '../../src/core/resolve/stitch.ts';
3030
import { WgslTypeError } from '../../src/errors.ts';
3131
import { $internal, $resolve } from '../../src/shared/symbols.ts';
32+
import type { SupportedLogOp } from './consoleLog/types.ts';
3233

3334
export function numericLiteralToSnippet(value: number): Snippet {
3435
if (value >= 2 ** 63 || value < -(2 ** 63)) {
@@ -85,7 +86,7 @@ export type GenerationCtx = ResolutionCtx & {
8586
dedent(): string;
8687
pushBlockScope(): void;
8788
popBlockScope(): void;
88-
generateLog(op: string, args: Snippet[]): Snippet;
89+
generateLog(op: SupportedLogOp, args: Snippet[]): Snippet;
8990
getById(id: string): Snippet | null;
9091
defineVariable(id: string, snippet: Snippet): void;
9192
setBlockExternals(externals: Record<string, Snippet>): void;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import type { AnyFn } from '../core/function/fnTypes.ts';
2+
import { f32 } from '../data/numeric.ts';
3+
import {
4+
abs,
5+
acos,
6+
acosh,
7+
asin,
8+
asinh,
9+
atan,
10+
atan2,
11+
atanh,
12+
ceil,
13+
cos,
14+
cosh,
15+
countLeadingZeros,
16+
exp,
17+
floor,
18+
log,
19+
log2,
20+
max,
21+
min,
22+
pow,
23+
sign,
24+
sin,
25+
sinh,
26+
sqrt,
27+
tan,
28+
tanh,
29+
trunc,
30+
} from '../std/numeric.ts';
31+
import type { DualFn } from '../types.ts';
32+
33+
export const mathToStd = new Map<AnyFn, DualFn<AnyFn>>([
34+
// -- one to one Math to WGSL correlation --
35+
[Math.abs, abs],
36+
[Math.acos, acos],
37+
[Math.acosh, acosh],
38+
[Math.asin, asin],
39+
[Math.asinh, asinh],
40+
[Math.atan, atan],
41+
[Math.atan2, atan2],
42+
[Math.atanh, atanh],
43+
[Math.ceil, ceil],
44+
[Math.cos, cos],
45+
[Math.cosh, cosh],
46+
[Math.exp, exp],
47+
[Math.floor, floor],
48+
[Math.fround, f32 as DualFn<AnyFn>],
49+
[Math.clz32, countLeadingZeros],
50+
[Math.trunc, trunc],
51+
[Math.log, log],
52+
[Math.log2, log2],
53+
[Math.pow, pow],
54+
[Math.sign, sign],
55+
[Math.sin, sin],
56+
[Math.sinh, sinh],
57+
[Math.sqrt, sqrt],
58+
[Math.tan, tan],
59+
[Math.tanh, tanh],
60+
// -- varying in Math and two arg in WGSL, but we support varying in std --
61+
[Math.max, max],
62+
[Math.min, min],
63+
// -- possible if we extend std --
64+
// [Math.cbrt, ???],
65+
// [Math.log10, ???],
66+
// [Math.log1p, ???],
67+
// [Math.f16round, ???],
68+
// [Math.hypot, ???],
69+
// [Math.expm1, ???],
70+
// -- skipped --
71+
// [Math.random, ???],
72+
// [Math.imul, ???],
73+
// [Math.round, ???], // round(2.5) is 3 in JS and 2 in WGSL
74+
]);
75+
76+
// deferring the array creation lets us handle console mocks
77+
export const supportedLogOps = () =>
78+
[console.log, console.debug, console.info, console.warn, console.error, console.clear] as const;

packages/typegpu/src/tgsl/math.ts

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

0 commit comments

Comments
 (0)