Skip to content

Commit fece947

Browse files
fix: console.log implicit pointers (#2398)
1 parent b4d49dc commit fece947

4 files changed

Lines changed: 100 additions & 17 deletions

File tree

packages/typegpu/src/data/dataTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ export function undecorate(data: BaseData): BaseData {
145145
return data;
146146
}
147147

148+
export function unptr(data: BaseData): BaseData;
149+
export function unptr(data: BaseData | UnknownData): BaseData | UnknownData;
148150
export function unptr(data: BaseData | UnknownData): BaseData | UnknownData {
149151
if (wgsl.isPtr(data)) {
150152
return data.inner;

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { TgpuRoot } from '../../core/root/rootTypes.ts';
44
import { shaderStageSlot } from '../../core/slot/internalSlots.ts';
55
import { arrayOf } from '../../data/array.ts';
66
import { atomic } from '../../data/atomic.ts';
7-
import { UnknownData } from '../../data/dataTypes.ts';
7+
import { UnknownData, unptr } from '../../data/dataTypes.ts';
88
import { u32 } from '../../data/numeric.ts';
99
import { snip, type Snippet } from '../../data/snippet.ts';
1010
import { struct } from '../../data/struct.ts';
@@ -15,8 +15,10 @@ import {
1515
Void,
1616
type WgslArray,
1717
} from '../../data/wgslTypes.ts';
18+
import { invariant } from '../../errors.ts';
1819
import { $internal } from '../../shared/symbols.ts';
19-
import { concretizeSnippets, type GenerationCtx } from '../generationHelpers.ts';
20+
import { convertToCommonType } from '../conversion.ts';
21+
import { concretizeSnippet, type GenerationCtx } from '../generationHelpers.ts';
2022
import { createLoggingFunction } from './serializers.ts';
2123
import {
2224
type LogGenerator,
@@ -87,30 +89,46 @@ export class LogGeneratorImpl implements LogGenerator {
8789
return fallbackSnippet;
8890
}
8991

90-
const concreteArgs = concretizeSnippets(args);
9192
const id = this.#firstUnusedId++;
9293

93-
const nonStringArgs = concreteArgs.filter((e) => e.dataType !== UnknownData);
94+
const concreteArgsWithStrings = args
95+
.map((arg) => {
96+
if (arg.dataType === UnknownData) {
97+
return arg;
98+
}
99+
const converted = convertToCommonType(ctx, [arg], [unptr(arg.dataType)])?.[0];
100+
invariant(
101+
converted,
102+
`Internal error. Expected type ${arg.dataType} to be convertible to ${unptr(arg.dataType)}`,
103+
);
104+
return converted;
105+
})
106+
.map(concretizeSnippet);
107+
108+
const concreteArgs = concreteArgsWithStrings.filter((arg) => arg.dataType !== UnknownData);
94109

95110
const logFn = createLoggingFunction(
96111
id,
97-
nonStringArgs.map((e) => e.dataType as AnyWgslData),
112+
concreteArgs.map((e) => e.dataType as AnyWgslData),
98113
this.#dataBuffer,
99114
this.#indexBuffer,
100115
this.#options,
101116
);
102117

103-
const argTypes = concreteArgs.map((e) =>
104-
e.dataType === UnknownData ? (e.value as string) : (e.dataType as AnyWgslData),
105-
);
106-
107-
this.#logIdToMeta.set(id, { op: op as SupportedLogOps, argTypes });
108-
109-
return snip(
110-
stitch`${ctx.resolve(logFn).value}(${nonStringArgs})`,
118+
const functionSnippet = snip(
119+
stitch`${ctx.resolve(logFn).value}(${concreteArgs})`,
111120
Void,
112121
/* origin */ 'runtime',
113122
);
123+
124+
this.#logIdToMeta.set(id, {
125+
op: op as SupportedLogOps,
126+
argTypes: concreteArgsWithStrings.map((e) =>
127+
e?.dataType === UnknownData ? (e?.value as string) : (e?.dataType as AnyWgslData),
128+
),
129+
});
130+
131+
return functionSnippet;
114132
}
115133

116134
get logResources(): LogResources | undefined {

packages/typegpu/src/tgsl/wgslGenerator.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,7 @@ ${this.ctx.pre}}`;
772772
);
773773
}
774774
// Taking care of abstract numerics and implicit pointers
775-
accessed = structType.provideProp(
776-
key,
777-
unptr(concretize(expr.dataType)) as wgsl.BaseData,
778-
);
775+
accessed = structType.provideProp(key, unptr(concretize(expr.dataType)));
779776
}
780777

781778
return [accessed.prop, expr];

packages/typegpu/tests/tgsl/consoleLog.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,72 @@ describe('wgslGenerator with console.log', () => {
682682
expect(consoleWarnSpy).toHaveBeenCalledWith("Unsupported log method 'trace'.");
683683
expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
684684
});
685+
686+
it('works with implicit pointers', ({ root }) => {
687+
const myUniform = root.createUniform(d.vec2f);
688+
const myPipeline = root.createGuardedComputePipeline((x) => {
689+
'use gpu';
690+
const v = myUniform.$;
691+
console.log(v);
692+
});
693+
694+
expect(tgpu.resolve([myPipeline.pipeline])).toMatchInlineSnapshot(`
695+
"@group(0) @binding(0) var<uniform> sizeUniform: vec3u;
696+
697+
@group(0) @binding(1) var<uniform> myUniform: vec2f;
698+
699+
@group(0) @binding(2) var<storage, read_write> indexBuffer: atomic<u32>;
700+
701+
struct SerializedLogData {
702+
id: u32,
703+
serializedData: array<u32, 63>,
704+
}
705+
706+
@group(0) @binding(3) var<storage, read_write> dataBuffer: array<SerializedLogData, 64>;
707+
708+
var<private> dataBlockIndex: u32;
709+
710+
var<private> dataByteIndex: u32;
711+
712+
fn nextByteIndex() -> u32 {
713+
let i = dataByteIndex;
714+
dataByteIndex = dataByteIndex + 1u;
715+
return i;
716+
}
717+
718+
fn serializeVec2f(v: vec2f) {
719+
dataBuffer[dataBlockIndex].serializedData[nextByteIndex()] = bitcast<u32>(v.x);
720+
dataBuffer[dataBlockIndex].serializedData[nextByteIndex()] = bitcast<u32>(v.y);
721+
}
722+
723+
fn log1serializer(_arg_0: vec2f) {
724+
serializeVec2f(_arg_0);
725+
}
726+
727+
fn log1(_arg_0: vec2f) {
728+
dataBlockIndex = atomicAdd(&indexBuffer, 1);
729+
if (dataBlockIndex >= 64) {
730+
return;
731+
}
732+
dataBuffer[dataBlockIndex].id = 1;
733+
dataByteIndex = 0;
734+
735+
log1serializer(_arg_0);
736+
}
737+
738+
fn wrappedCallback(x: u32, _arg_1: u32, _arg_2: u32) {
739+
let v = (&myUniform);
740+
log1((*v));
741+
}
742+
743+
@compute @workgroup_size(256, 1, 1) fn mainCompute(@builtin(global_invocation_id) id: vec3u) {
744+
if (any(id >= sizeUniform)) {
745+
return;
746+
}
747+
wrappedCallback(id.x, id.y, id.z);
748+
}"
749+
`);
750+
});
685751
});
686752

687753
describe('deserializeAndStringify', () => {

0 commit comments

Comments
 (0)