-
-
Notifications
You must be signed in to change notification settings - Fork 76
impr: Warn when schema is not uniform aligned #2750
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
aleksanderkatan
wants to merge
24
commits into
main
Choose a base branch
from
impr/warn-when-schema-is-not-uniform-aligned
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
24 commits
Select commit
Hold shift + click to select a range
f7c6a76
Replace warn with throw
aleksanderkatan e2ce177
Add logger, replace console.warns with logger warns
aleksanderkatan c0a42d0
Rename warning types
aleksanderkatan 1e567ec
Split apis into two
aleksanderkatan b82d259
Rename warning types again
aleksanderkatan 5cae02c
Add custom warn message, change log tests to snapshots
aleksanderkatan 8a8d1ed
Change `enable` to `reset`, add tests
aleksanderkatan 7e1368b
Pass prod mode to logger
aleksanderkatan 1c4bde9
Change warn to invariant
aleksanderkatan d7ce0dc
Merge remote-tracking branch 'origin/main' into feat/logger
aleksanderkatan b4b2ba7
Change remaining calls to warns
aleksanderkatan 5932429
Add docs
aleksanderkatan 24dfc22
nr fix
aleksanderkatan 46fc32e
Add tests
aleksanderkatan bf5b386
Finish warn implementation
aleksanderkatan 90ff2ec
Report when giving usage
aleksanderkatan 664d90f
Remove 'uniform' usage from confetti examples
aleksanderkatan 742c695
Self review
aleksanderkatan 7357da4
Review fixes
aleksanderkatan 521ca90
Unused import
aleksanderkatan a4207b5
Merge branch 'main' into feat/logger
aleksanderkatan 7b8f4d2
Review fixes
aleksanderkatan 25ed48e
Merge branch 'feat/logger' into impr/warn-when-schema-is-not-uniform-…
aleksanderkatan 7d87abc
Update snapshots
aleksanderkatan 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
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 was deleted.
Oops, something went wrong.
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
111 changes: 111 additions & 0 deletions
111
packages/typegpu/src/core/pipeline/webgpuLimitations.ts
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,111 @@ | ||||||
| import { alignmentOf } from '../../data/alignmentOf.ts'; | ||||||
| import { memoryLayoutOf } from '../../data/offsetUtils.ts'; | ||||||
| import { sizeOf } from '../../data/sizeOf.ts'; | ||||||
| import { isWgslArray, isWgslStruct, type BaseData } from '../../data/wgslTypes.ts'; | ||||||
| import { invariant } from '../../errors.ts'; | ||||||
| import { getName } from '../../internal.ts'; | ||||||
| import { roundUp } from '../../mathUtils.ts'; | ||||||
| import type { TgpuBindGroupLayout } from '../../tgpuBindGroupLayout.ts'; | ||||||
| import { logger } from '../../tgpuLogger.ts'; | ||||||
|
|
||||||
| /** | ||||||
| * Warns if layout exceeds supported buffer count limits. | ||||||
| */ | ||||||
| export function warnIfOverflow(layouts: TgpuBindGroupLayout[], limits: GPUSupportedLimits) { | ||||||
| const entries = Object.values(layouts) | ||||||
| .flatMap((layout) => Object.values(layout.entries)) | ||||||
| .filter((entry) => entry !== null); | ||||||
|
|
||||||
| const uniform = entries.filter((entry) => 'uniform' in entry).length; | ||||||
| const storage = entries.filter((entry) => 'storage' in entry).length; | ||||||
|
|
||||||
| if (uniform > limits.maxUniformBuffersPerShaderStage) { | ||||||
| logger.warn( | ||||||
| 'webgpu-limits-exceeded', | ||||||
| `Total number of uniform buffers (${uniform}) exceeds maxUniformBuffersPerShaderStage (${limits.maxUniformBuffersPerShaderStage}). Consider: | ||||||
| 1. Grouping some of the uniforms into one using 'd.struct', | ||||||
| 2. Increasing the limit when requesting a device or creating a root.`, | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| if (storage > limits.maxStorageBuffersPerShaderStage) { | ||||||
| logger.warn( | ||||||
| 'webgpu-limits-exceeded', | ||||||
| `Total number of storage buffers (${storage}) exceeds maxStorageBuffersPerShaderStage (${limits.maxStorageBuffersPerShaderStage}).`, | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| function requiredAlignOf(schema: BaseData) { | ||||||
| if (isWgslStruct(schema) || isWgslArray(schema)) { | ||||||
| return roundUp(alignmentOf(schema), 16); | ||||||
| } | ||||||
| return alignmentOf(schema); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * See https://www.w3.org/TR/WGSL/#address-space-layout-constraints | ||||||
| */ | ||||||
| export function warnIfNotUniformAligned(schema: BaseData) { | ||||||
| if (isWgslArray(schema)) { | ||||||
| warnIfNotUniformAligned(schema.elementType); | ||||||
|
|
||||||
| const stride = roundUp(sizeOf(schema.elementType), alignmentOf(schema.elementType)); | ||||||
| if (stride % 16) { | ||||||
| logger.warn( | ||||||
| 'uniform-schema-misaligned', | ||||||
| `\ | ||||||
| Schema '${getName(schema.elementType) ?? '<unnamed>'}' is used in an array in an uniform buffer, and its stride (${stride}) is not a multiple of 16. | ||||||
| This is not portable (see https://www.w3.org/TR/WGSL/#address-space-layout-constraints), and will break on some devices. | ||||||
| To address this, wrap the element in 'd.align(16, ...)'.`, | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
| if (isWgslStruct(schema)) { | ||||||
| Object.values(schema.propTypes).forEach(warnIfNotUniformAligned); | ||||||
|
|
||||||
| Object.entries(schema.propTypes).forEach(([key, value]) => { | ||||||
| const offset = memoryLayoutOf(schema, (schema) => schema[key]).offset; | ||||||
| const requiredAlignment = requiredAlignOf(value); | ||||||
|
|
||||||
| if (offset % requiredAlignment) { | ||||||
| logger.warn( | ||||||
| 'uniform-schema-misaligned', | ||||||
| `\ | ||||||
| Schema '${getName(schema) ?? '<unnamed>'}' is used in an uniform buffer, and its property '${key}' does not meet required alignment (offset is ${offset}, required alignment is ${requiredAlignment}). | ||||||
| This is not portable (see https://www.w3.org/TR/WGSL/#address-space-layout-constraints), and will break on some devices. | ||||||
| To address this, wrap the property '${key}' in 'd.align(${requiredAlignment}, ...)'.`, | ||||||
| ); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| const keys = Object.keys(schema.propTypes); | ||||||
| for (let i = 0; i < keys.length - 1; i++) { | ||||||
| const thisKey = keys[i]; | ||||||
| const nextKey = keys[i + 1]; | ||||||
| invariant(thisKey && nextKey); | ||||||
|
|
||||||
| const thisValue = schema.propTypes[thisKey]; | ||||||
| invariant(thisValue); | ||||||
|
|
||||||
| if (!isWgslStruct(thisValue)) { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
| const minimumDifference = roundUp(16, sizeOf(thisValue)); | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| const thisKeyOffset = memoryLayoutOf(schema, (schema) => schema[thisKey]).offset; | ||||||
| const nextKeyOffset = memoryLayoutOf(schema, (schema) => schema[nextKey]).offset; | ||||||
| const difference = nextKeyOffset - thisKeyOffset; | ||||||
|
|
||||||
| if (minimumDifference > difference) { | ||||||
| logger.warn( | ||||||
| 'uniform-schema-misaligned', | ||||||
| `\ | ||||||
| Schema '${getName(schema) ?? '<unnamed>'}' is used in an uniform buffer, and the difference between memory offsets of '${thisKey}' and '${nextKey}' props (${difference}) is less than recommended (${minimumDifference}). | ||||||
| This is not portable (see https://www.w3.org/TR/WGSL/#address-space-layout-constraints), and will break on some devices. | ||||||
| To address this, wrap the '${thisKey}' prop in 'd.size(${minimumDifference}, ...)'.`, | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
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
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.
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.
You cannot wrap array element with
d.align