Skip to content

Commit a86350a

Browse files
authored
fix: Add a catch-all overload for root.createBuffer (#2339)
1 parent ee92676 commit a86350a

3 files changed

Lines changed: 50 additions & 17 deletions

File tree

packages/typegpu/src/core/root/rootTypes.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,13 +835,19 @@ export interface TgpuRoot extends Unwrapper, WithBinding {
835835
*/
836836
createBuffer<TData extends AnyData>(
837837
typeSchema: ValidateBufferSchema<TData>,
838-
initializer: (buffer: TgpuBuffer<TData>) => void,
838+
// NoInfer is there to infer the schema type just based on the first parameter
839+
initializer: (buffer: TgpuBuffer<NoInfer<TData>>) => void,
839840
): TgpuBuffer<TData>;
840841
createBuffer<TData extends AnyData>(
841842
typeSchema: ValidateBufferSchema<TData>,
842843
// NoInfer is there to infer the schema type just based on the first parameter
843844
initial?: InferInput<NoInfer<TData>>,
844845
): TgpuBuffer<TData>;
846+
createBuffer<TData extends AnyData>(
847+
typeSchema: ValidateBufferSchema<TData>,
848+
// NoInfer is there to infer the schema type just based on the first parameter
849+
initial?: ((buffer: TgpuBuffer<NoInfer<TData>>) => void) | InferInput<NoInfer<TData>>,
850+
): TgpuBuffer<TData>;
845851

846852
/**
847853
* Allocates memory on the GPU, allows passing data between host and shader.
@@ -867,13 +873,18 @@ export interface TgpuRoot extends Unwrapper, WithBinding {
867873
*/
868874
createUniform<TData extends AnyWgslData>(
869875
typeSchema: ValidateUniformSchema<TData>,
870-
initializer: (buffer: TgpuBuffer<TData>) => void,
876+
initializer: (buffer: TgpuBuffer<NoInfer<TData>>) => void,
871877
): TgpuUniform<TData>;
872878
createUniform<TData extends AnyWgslData>(
873879
typeSchema: ValidateUniformSchema<TData>,
874880
// NoInfer is there to infer the schema type just based on the first parameter
875881
initial?: InferInput<NoInfer<TData>>,
876882
): TgpuUniform<TData>;
883+
createUniform<TData extends AnyWgslData>(
884+
typeSchema: ValidateUniformSchema<TData>,
885+
// NoInfer is there to infer the schema type just based on the first parameter
886+
initial?: ((buffer: TgpuBuffer<NoInfer<TData>>) => void) | InferInput<NoInfer<TData>>,
887+
): TgpuUniform<TData>;
877888

878889
/**
879890
* Allocates memory on the GPU, allows passing data between host and shader.
@@ -898,13 +909,18 @@ export interface TgpuRoot extends Unwrapper, WithBinding {
898909
*/
899910
createMutable<TData extends AnyWgslData>(
900911
typeSchema: ValidateStorageSchema<TData>,
901-
initializer: (buffer: TgpuBuffer<TData>) => void,
912+
initializer: (buffer: TgpuBuffer<NoInfer<TData>>) => void,
902913
): TgpuMutable<TData>;
903914
createMutable<TData extends AnyWgslData>(
904915
typeSchema: ValidateStorageSchema<TData>,
905916
// NoInfer is there to infer the schema type just based on the first parameter
906917
initial?: InferInput<NoInfer<TData>>,
907918
): TgpuMutable<TData>;
919+
createMutable<TData extends AnyWgslData>(
920+
typeSchema: ValidateStorageSchema<TData>,
921+
// NoInfer is there to infer the schema type just based on the first parameter
922+
initial?: ((buffer: TgpuBuffer<NoInfer<TData>>) => void) | InferInput<NoInfer<TData>>,
923+
): TgpuMutable<TData>;
908924

909925
/**
910926
* Allocates memory on the GPU, allows passing data between host and shader.
@@ -929,13 +945,18 @@ export interface TgpuRoot extends Unwrapper, WithBinding {
929945
*/
930946
createReadonly<TData extends AnyWgslData>(
931947
typeSchema: ValidateStorageSchema<TData>,
932-
initializer: (buffer: TgpuBuffer<TData>) => void,
948+
initializer: (buffer: TgpuBuffer<NoInfer<TData>>) => void,
933949
): TgpuReadonly<TData>;
934950
createReadonly<TData extends AnyWgslData>(
935951
typeSchema: ValidateStorageSchema<TData>,
936952
// NoInfer is there to infer the schema type just based on the first parameter
937953
initial?: InferInput<NoInfer<TData>>,
938954
): TgpuReadonly<TData>;
955+
createReadonly<TData extends AnyWgslData>(
956+
typeSchema: ValidateStorageSchema<TData>,
957+
// NoInfer is there to infer the schema type just based on the first parameter
958+
initial?: ((buffer: TgpuBuffer<NoInfer<TData>>) => void) | InferInput<NoInfer<TData>>,
959+
): TgpuReadonly<TData>;
939960

940961
/**
941962
* Allocates memory on the GPU, allows passing data between host and shader.

packages/typegpu/tests/buffer.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ describe('TgpuBuffer', () => {
727727

728728
// @ts-expect-error: boolean is not allowed in buffer schemas
729729
attest(root.createBuffer(boolSchema)).type.errors.snap(
730-
"Argument of type 'WgslStruct<{ a: U32; b: Bool; }>' is not assignable to parameter of type '\"(Error) in struct property 'b' — Bool is not host-shareable, use U32 or I32 instead\"'.",
730+
"No overload matches this call.Overload 1 of 4, '(typeSchema: \"(Error) in struct property 'b' — Bool is not host-shareable, use U32 or I32 instead\", initial?: InferInput<NoInfer<WgslStruct<{ a: U32; b: Bool; }>>> | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U32; b: Bool; }>' is not assignable to parameter of type '\"(Error) in struct property 'b' — Bool is not host-shareable, use U32 or I32 instead\"'.\nOverload 2 of 4, '(typeSchema: \"(Error) in struct property 'b' — Bool is not host-shareable, use U32 or I32 instead\", initial?: InferInput<NoInfer<WgslStruct<{ a: U32; b: Bool; }>>> | ((buffer: TgpuBuffer<...>) => void) | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U32; b: Bool; }>' is not assignable to parameter of type '\"(Error) in struct property 'b' — Bool is not host-shareable, use U32 or I32 instead\"'.",
731731
);
732732

733733
const nestedBoolSchema = d.struct({
@@ -742,7 +742,8 @@ describe('TgpuBuffer', () => {
742742

743743
// @ts-expect-error: boolean is not allowed in buffer schemas
744744
attest(root.createBuffer(nestedBoolSchema)).type.errors.snap(
745-
"Argument of type 'WgslStruct<{ a: U32; b: WgslStruct<{ c: F32; d: WgslStruct<{ e: Bool; }>; }>; }>' is not assignable to parameter of type '\"(Error) in struct property 'b' — in struct property 'd' — in struct property 'e' — Bool is not host-shareable, use U32 or I32 instead\"'.",
745+
`No overload matches this call.Overload 1 of 4, '(typeSchema: "(Error) in struct property 'b' — in struct property 'd' — in struct property 'e' — Bool is not host-shareable, use U32 or I32 instead", initial?: InferInput<NoInfer<WgslStruct<{ a: U32; b: WgslStruct<...>; }>>> | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U32; b: WgslStruct<{ c: F32; d: WgslStruct<{ e: Bool; }>; }>; }>' is not assignable to parameter of type '"(Error) in struct property 'b' — in struct property 'd' — in struct property 'e' — Bool is not host-shareable, use U32 or I32 instead"'.
746+
Overload 2 of 4, '(typeSchema: "(Error) in struct property 'b' — in struct property 'd' — in struct property 'e' — Bool is not host-shareable, use U32 or I32 instead", initial?: InferInput<NoInfer<WgslStruct<{ a: U32; b: WgslStruct<...>; }>>> | ((buffer: TgpuBuffer<...>) => void) | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U32; b: WgslStruct<{ c: F32; d: WgslStruct<{ e: Bool; }>; }>; }>' is not assignable to parameter of type '"(Error) in struct property 'b' — in struct property 'd' — in struct property 'e' — Bool is not host-shareable, use U32 or I32 instead"'.`,
746747
);
747748
});
748749

@@ -759,7 +760,8 @@ describe('TgpuBuffer', () => {
759760

760761
// @ts-expect-error
761762
attest(root.createBuffer(notFine)).type.errors.snap(
762-
"Argument of type 'WgslStruct<{ a: U16; b: U32; }>' is not assignable to parameter of type '\"(Error) in struct property 'a' — U16 is only usable inside arrays for index buffers, use U32 or I32 instead\"'.",
763+
`No overload matches this call.Overload 1 of 4, '(typeSchema: "(Error) in struct property 'a' — U16 is only usable inside arrays for index buffers, use U32 or I32 instead", initial?: InferInput<NoInfer<WgslStruct<{ a: U16; b: U32; }>>> | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U16; b: U32; }>' is not assignable to parameter of type '"(Error) in struct property 'a' — U16 is only usable inside arrays for index buffers, use U32 or I32 instead"'.
764+
Overload 2 of 4, '(typeSchema: "(Error) in struct property 'a' — U16 is only usable inside arrays for index buffers, use U32 or I32 instead", initial?: InferInput<NoInfer<WgslStruct<{ a: U16; b: U32; }>>> | ((buffer: TgpuBuffer<...>) => void) | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U16; b: U32; }>' is not assignable to parameter of type '"(Error) in struct property 'a' — U16 is only usable inside arrays for index buffers, use U32 or I32 instead"'.`,
763765
);
764766

765767
const alsoNotFine = d.struct({
@@ -770,7 +772,8 @@ describe('TgpuBuffer', () => {
770772

771773
// @ts-expect-error
772774
attest(root.createBuffer(alsoNotFine)).type.errors.snap(
773-
"Argument of type 'WgslStruct<{ a: U32; b: WgslArray<U16>; c: F32; }>' is not assignable to parameter of type '\"(Error) in struct property 'b' — in array element — U16 is only usable inside arrays for index buffers, use U32 or I32 instead\"'.",
775+
`No overload matches this call.Overload 1 of 4, '(typeSchema: "(Error) in struct property 'b' — in array element — U16 is only usable inside arrays for index buffers, use U32 or I32 instead", initial?: InferInput<NoInfer<WgslStruct<{ a: U32; b: WgslArray<...>; c: F32; }>>> | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U32; b: WgslArray<U16>; c: F32; }>' is not assignable to parameter of type '"(Error) in struct property 'b' — in array element — U16 is only usable inside arrays for index buffers, use U32 or I32 instead"'.
776+
Overload 2 of 4, '(typeSchema: "(Error) in struct property 'b' — in array element — U16 is only usable inside arrays for index buffers, use U32 or I32 instead", initial?: InferInput<NoInfer<WgslStruct<{ a: U32; b: WgslArray<...>; c: F32; }>>> | ((buffer: TgpuBuffer<...>) => void) | undefined): TgpuBuffer<...>', gave the following error.Argument of type 'WgslStruct<{ a: U32; b: WgslArray<U16>; c: F32; }>' is not assignable to parameter of type '"(Error) in struct property 'b' — in array element — U16 is only usable inside arrays for index buffers, use U32 or I32 instead"'.`,
774777
);
775778
});
776779

0 commit comments

Comments
 (0)