-
-
Notifications
You must be signed in to change notification settings - Fork 61
feat: unary operator ! support and std.not
#2346
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
cieplypolar
wants to merge
11
commits into
main
Choose a base branch
from
impr/operator-!
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b2c137b
operator !
cieplypolar 4f793c1
operator ! cleanup
cieplypolar 21125b7
std.not
cieplypolar 7780f13
Merge branch 'main' into impr/operator-!
cieplypolar 8308ba7
missing braces
cieplypolar 3d768c7
more accurate behavior comparing to JS
cieplypolar c959ad2
std.not mimics WGSL
cieplypolar b7cb36f
correct NaN handling
cieplypolar 96efef2
Merge branch 'main' into impr/operator-!
cieplypolar e892f33
review changes + correct imports
cieplypolar 41a90e1
Merge branch 'main' into impr/operator-!
cieplypolar 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 |
|---|---|---|
| @@ -1,11 +1,158 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { vec2b, vec3b, vec4b } from '../../../src/data/index.ts'; | ||
| import { describe, expect } from 'vitest'; | ||
| import { it } from 'typegpu-testing-utility'; | ||
| import { not } from '../../../src/std/boolean.ts'; | ||
| import tgpu, { d, std } from '../../../src/index.js'; | ||
|
|
||
| describe('neg', () => { | ||
| it('negates', () => { | ||
| expect(not(vec2b(true, false))).toStrictEqual(vec2b(false, true)); | ||
| expect(not(vec3b(false, false, true))).toStrictEqual(vec3b(true, true, false)); | ||
| expect(not(vec4b(true, true, false, false))).toStrictEqual(vec4b(false, false, true, true)); | ||
| describe('not', () => { | ||
| it('negates booleans', () => { | ||
| expect(not(true)).toBe(false); | ||
| expect(not(false)).toBe(true); | ||
| }); | ||
|
|
||
| it('converts numbers to booleans and negates', () => { | ||
| expect(not(0)).toBe(true); | ||
| expect(not(-1)).toBe(false); | ||
| expect(not(42)).toBe(false); | ||
| }); | ||
|
|
||
| it('negates boolean vectors', () => { | ||
| expect(not(d.vec2b(true, false))).toStrictEqual(d.vec2b(false, true)); | ||
| expect(not(d.vec3b(false, false, true))).toStrictEqual(d.vec3b(true, true, false)); | ||
| expect(not(d.vec4b(true, true, false, false))).toStrictEqual(d.vec4b(false, false, true, true)); | ||
| }); | ||
|
|
||
| it('converts numeric vectors to booleans vectors and negates component-wise', () => { | ||
| expect(not(d.vec2f(0.0, 1.0))).toStrictEqual(d.vec2b(true, false)); | ||
| expect(not(d.vec3i(0, 5, -1))).toStrictEqual(d.vec3b(true, false, false)); | ||
| expect(not(d.vec4u(0, 0, 1, 0))).toStrictEqual(d.vec4b(true, true, false, true)); | ||
| expect(not(d.vec4h(0, 3.14, 0, -2.5))).toStrictEqual(d.vec4b(true, false, true, false)); | ||
| }); | ||
|
|
||
| it('negates truthiness check', () => { | ||
| expect(not(null)).toBe(true); | ||
| expect(not(undefined)).toBe(true); | ||
| expect(not({})).toBe(false); | ||
| }); | ||
|
|
||
| it('mimics WGSL behavior on NaN', () => { | ||
| expect(not(NaN)).toBe(false); | ||
| }); | ||
|
|
||
| it('generates correct WGSL on a boolean runtime-known argument', () => { | ||
| const testFn = tgpu.fn( | ||
| [d.bool], | ||
| d.bool, | ||
| )((v) => { | ||
| return not(v); | ||
| }); | ||
| expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` | ||
| "fn testFn(v: bool) -> bool { | ||
| return !v; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('generates correct WGSL on a numeric runtime-known argument', () => { | ||
| const testFn = tgpu.fn( | ||
| [d.i32], | ||
| d.bool, | ||
| )((v) => { | ||
| return not(v); | ||
| }); | ||
| expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` | ||
| "fn testFn(v: i32) -> bool { | ||
| return !bool(v); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('generates correct WGSL on a boolean vector runtime-known argument', () => { | ||
| const testFn = tgpu.fn( | ||
| [d.vec3b], | ||
| d.vec3b, | ||
| )((v) => { | ||
| return not(v); | ||
| }); | ||
| expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` | ||
| "fn testFn(v: vec3<bool>) -> vec3<bool> { | ||
| return !(v); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('generates correct WGSL on a numeric vector runtime-known argument', () => { | ||
| const testFn = tgpu.fn( | ||
| [d.vec3f], | ||
| d.vec3b, | ||
| )((v) => { | ||
| return not(v); | ||
| }); | ||
| expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` | ||
| "fn testFn(v: vec3f) -> vec3<bool> { | ||
| return !(vec3<bool>(v)); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('generates correct WGSL on a numeric vector comptime-known argument', () => { | ||
| const f = () => { | ||
| 'use gpu'; | ||
| const v = not(d.vec4f(Infinity, -Infinity, 0, NaN)); | ||
| }; | ||
|
|
||
| expect(tgpu.resolve([f])).toMatchInlineSnapshot(` | ||
| "fn f() { | ||
| var v = vec4<bool>(false, false, true, false); | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('evaluates at compile time for comptime-known arguments', () => { | ||
| const getN = tgpu.comptime(() => 42); | ||
| const slot = tgpu.slot<{ a?: number }>({}); | ||
|
|
||
| const f = () => { | ||
| 'use gpu'; | ||
| if (not(getN()) && not(slot.$.a) && not(d.vec4f(1, 8, 8, 2)).x) { | ||
| return 1; | ||
| } | ||
| return -1; | ||
| }; | ||
|
|
||
| expect(tgpu.resolve([f])).toMatchInlineSnapshot(` | ||
| "fn f() -> i32 { | ||
| if (((false && true) && false)) { | ||
| return 1; | ||
| } | ||
| return -1; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('mimics JS on non-primitive values', ({ root }) => { | ||
| const buffer = root.createUniform(d.mat4x4f); | ||
| const testFn = tgpu.fn([d.vec3f, d.atomic(d.u32), d.ptrPrivate(d.u32)])((v, a, p) => { | ||
| const _b0 = !buffer; | ||
| const _b1 = !buffer.$; | ||
| const _b2 = !v; | ||
| const _b3 = !a; | ||
| const _b4 = !std.atomicLoad(a); | ||
| const _b5 = !p; | ||
| const _b6 = !p.$; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([testFn])).toMatchInlineSnapshot(` | ||
| "@group(0) @binding(0) var<uniform> buffer: mat4x4f; | ||
|
|
||
| fn testFn(v: vec3f, a: atomic<u32>, p: ptr<private, u32>) { | ||
| const _b0 = false; | ||
| const _b1 = false; | ||
| const _b2 = false; | ||
| const _b3 = false; | ||
| let _b4 = !bool(atomicLoad(&a)); | ||
| const _b5 = false; | ||
| let _b6 = !bool((*p)); | ||
| }" | ||
| `); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.