Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support structs
  • Loading branch information
Aleksander Katan committed Sep 19, 2025
commit 768359a6fe028396ece4e86a2ad8ab80fc4cea49
10 changes: 10 additions & 0 deletions apps/typegpu-docs/src/examples/tests/log-test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ export const controls = {
}
})(),
},
'Compound types': {
onButtonClick: () => {
const SimpleStruct = d.struct({ vec: d.vec3u, num: d.u32 });
prepareDispatch(root, () => {
'kernel';
const simpleStruct = SimpleStruct({ vec: d.vec3u(1, 2, 3), num: 4 });
console.log(simpleStruct);
})();
},
},
'Two threads': {
onButtonClick: () =>
prepareDispatch(root, (x) => {
Expand Down
36 changes: 29 additions & 7 deletions packages/typegpu/src/tgsl/consoleLog/deserializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ import {
vec4i,
vec4u,
} from '../../data/vector.ts';
import { type AnyWgslData, isWgslData } from '../../data/wgslTypes.ts';
import {
type AnyWgslData,
isWgslData,
isWgslStruct,
} from '../../data/wgslTypes.ts';
import type { Infer } from '../../shared/repr.ts';
import { safeStringify } from '../../shared/safeStringify.ts';
import { bitcastU32toF32, bitcastU32toI32 } from '../../std/bitcast.ts';
import { unpack2x16float } from '../../std/packing.ts';
import type { LogResources } from './types.ts';
Expand Down Expand Up @@ -93,6 +98,26 @@ const deserializerMap: DeserializerMap = {
// -------

function deserialize(
data: Uint32Array,
dataType: AnyWgslData,
): unknown {
const maybeDeserializer = deserializerMap[dataType.type];
if (maybeDeserializer) {
return maybeDeserializer(data);
}
if (isWgslStruct(dataType)) {
const props = Object.keys(dataType.propTypes);
const propTypes = Object.values(dataType.propTypes) as AnyWgslData[];
const decodedProps = deserializeCompound(data, propTypes);
return Object.fromEntries(
props.map((key, index) => [key, decodedProps[index]]),
);
}

throw new Error(`Cannot serialize data of type ${dataType.type}`);
}

function deserializeCompound(
data: Uint32Array,
logInfo: (AnyWgslData | string)[],
): unknown[] {
Expand All @@ -101,12 +126,8 @@ function deserialize(
if (!isWgslData(info)) {
return info;
}
const deserializer = deserializerMap[info.type];
if (!deserializer) {
throw new Error(`Cannot deserialize data of type ${info.type}`);
}
const size = Math.ceil(sizeOf(info) / 4);
const value = deserializer(data.subarray(index, index + size));
const value = deserialize(data.subarray(index, index + size), info);
index += size;
return value;
});
Expand All @@ -116,7 +137,8 @@ export function deserializeAndStringify(
serializedData: Uint32Array,
argTypes: (AnyWgslData | string)[],
): string {
return deserialize(serializedData, argTypes).join(' ');
return deserializeCompound(serializedData, argTypes)
.map((e) => safeStringify(e)).join(' ');
}

/**
Expand Down
14 changes: 12 additions & 2 deletions packages/typegpu/src/tgsl/consoleLog/serializers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import {
AnyWgslData,
Atomic,
isWgslStruct,
U32,
Void,
WgslArray,
Expand Down Expand Up @@ -198,8 +199,17 @@ function getSerializer(
dataType: AnyWgslData,
dataBuffer: TgpuMutable<WgslArray<SerializedLogCallData>>,
) {
if (serializerMap[dataType.type]) {
return serializerMap[dataType.type];
const maybeSerializer = serializerMap[dataType.type];
if (maybeSerializer) {
return maybeSerializer;
}
if (isWgslStruct(dataType)) {
const props = Object.keys(dataType.propTypes);
const propTypes = Object.values(dataType.propTypes) as AnyWgslData[];
const propSerializer = createCompoundSerializer(propTypes, dataBuffer);
return fn([dataType])`(arg) {
propSerializer(${props.map((prop) => `arg.${prop}`).join(', ')});
}`.$uses({ propSerializer });
}
throw new Error(`Cannot serialize data of type ${dataType.type}`);
}
Expand Down