-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: tgpu.fn(callback) for providing slots and accessors #2029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lursz
wants to merge
22
commits into
main
Choose a base branch
from
feat/tgpufn-callback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+279
−21
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b0e36da
TgpuGenericFn
lursz 218f8a1
🦕
lursz 51c8fdf
still not passing...
lursz 3d71e24
🦕
lursz e717fae
Proposed way to proceed with tgpu.fn (#2030)
iwoplaza 77446c2
isTgpuGenericFn
lursz 5a92b03
isTgpuGenericFn_vol2
lursz 225e920
Merge branch 'main' into feat/tgpufn-callback
lursz eb735ca
🦕
lursz 3df2489
test corrections
lursz 7618c0c
shelllessCall fix - used to return undefined
lursz c906a4c
Merge branch 'main' into feat/tgpufn-callback
lursz c58e141
tests tests tests
lursz 67b21c6
Merge remote-tracking branch 'refs/remotes/origin/feat/tgpufn-callbac…
lursz 92625c3
Merge branch 'main' into feat/tgpufn-callback
lursz 34f8c42
more tests
lursz a741df7
Merge remote-tracking branch 'refs/remotes/origin/feat/tgpufn-callbac…
lursz d0a9e21
🦕🦕🦕🦕🦕🦕
lursz f5610d3
🦕
lursz 836d7f6
pr fixes, mostly generics
lursz 87eacef
more tests
lursz e635728
Merge branch 'main' into feat/tgpufn-callback
lursz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import { describe, expect } from 'vitest'; | ||
| import * as d from '../src/data/index.ts'; | ||
| import tgpu from '../src/index.ts'; | ||
| import { it } from './utils/extendedIt.ts'; | ||
|
|
||
| describe('TgpuGenericFn - shellless callback wrapper', () => { | ||
| it('generates only one definition when both original and wrapped function are used', () => { | ||
| const countAccess = tgpu['~unstable'].accessor(d.f32, 2); | ||
|
|
||
| const getDouble = () => { | ||
| 'use gpu'; | ||
| return countAccess.$ * 2; | ||
| }; | ||
|
|
||
| const getDouble4 = tgpu.fn(getDouble); | ||
|
|
||
| const main = () => { | ||
| 'use gpu'; | ||
| const original = getDouble(); | ||
| const wrapped = getDouble4(); | ||
| return original + wrapped; | ||
| }; | ||
|
|
||
| expect(tgpu.resolve([main])).toMatchInlineSnapshot(` | ||
| "fn getDouble() -> f32 { | ||
| return 4f; | ||
| } | ||
|
|
||
| fn main() -> f32 { | ||
| let original = getDouble(); | ||
| let wrapped = getDouble(); | ||
| return (original + wrapped); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('works when only the wrapped function is used', () => { | ||
| const countAccess = tgpu['~unstable'].accessor(d.f32, 0); | ||
|
|
||
| const getDouble = () => { | ||
| 'use gpu'; | ||
| return countAccess.$ * 2; | ||
| }; | ||
|
|
||
| const getDouble4 = tgpu.fn(getDouble); | ||
|
|
||
| const main = () => { | ||
| 'use gpu'; | ||
| return getDouble4(); | ||
| }; | ||
|
|
||
| const wgsl = tgpu.resolve([main]); | ||
| expect(tgpu.resolve([main])).toMatchInlineSnapshot(` | ||
| "fn getDouble() -> f32 { | ||
| return 0f; | ||
| } | ||
|
|
||
| fn main() -> f32 { | ||
| return getDouble(); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('does not duplicate the same function', () => { | ||
| const countAccess = tgpu['~unstable'].accessor(d.f32, 0); | ||
|
|
||
| const getDouble = () => { | ||
| 'use gpu'; | ||
| return countAccess.$ * 2; | ||
| }; | ||
|
|
||
| const getDouble4 = tgpu.fn(getDouble); | ||
|
|
||
| const main = () => { | ||
| 'use gpu'; | ||
| return getDouble4() + getDouble4(); | ||
| }; | ||
|
|
||
| const wgsl = tgpu.resolve([main]); | ||
| expect(tgpu.resolve([main])).toMatchInlineSnapshot(` | ||
| "fn getDouble() -> f32 { | ||
| return 0f; | ||
| } | ||
|
|
||
| fn main() -> f32 { | ||
| return (getDouble() + getDouble()); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('supports .with for slot bindings on generic functions', () => { | ||
| const multiplier = tgpu.slot(2).$name('multiplier'); | ||
|
|
||
| const scale = () => { | ||
| 'use gpu'; | ||
| return d.f32(multiplier.$) * d.f32(2); | ||
| }; | ||
|
|
||
| const scaleGeneric = tgpu.fn(scale); | ||
| const scaleBy3 = scaleGeneric.with(multiplier, 3); | ||
| const scaleBy4 = scaleGeneric.with(multiplier, 4); | ||
|
|
||
| const main = () => { | ||
| 'use gpu'; | ||
| return scaleBy3() + scaleBy4(); | ||
| }; | ||
|
|
||
| expect(tgpu.resolve([main])).toMatchInlineSnapshot(` | ||
| "fn scale() -> f32 { | ||
| return 6f; | ||
| } | ||
|
|
||
| fn scale_1() -> f32 { | ||
| return 8f; | ||
| } | ||
|
|
||
| fn main() -> f32 { | ||
| return (scale() + scale_1()); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('supports .with for accessor bindings on generic functions', () => { | ||
| const valueAccess = tgpu['~unstable'].accessor(d.f32); | ||
|
|
||
| const getValue = () => { | ||
| 'use gpu'; | ||
| return valueAccess.$; | ||
| }; | ||
|
|
||
| const getValueGeneric = tgpu.fn(getValue).with(valueAccess, 2); | ||
|
|
||
| const main = () => { | ||
| 'use gpu'; | ||
| return getValueGeneric(); | ||
| }; | ||
|
|
||
| expect(tgpu.resolve([main])).toMatchInlineSnapshot(` | ||
| "fn getValue() -> f32 { | ||
| return 2f; | ||
| } | ||
|
|
||
| fn main() -> f32 { | ||
| return getValue(); | ||
| }" | ||
| `); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add more tests to see if the
.withAPI works on these generic functions?