This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] Create reusable prefix sum. #42167
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // Parallel exclusive prefix sum computes the prefix in place in storage. | ||
| // BLOCK_SIZE is the overall storage size while ident must be the global | ||
| // x identifier. | ||
| #define ExclusivePrefixSum(ident, storage, BLOCK_SIZE) \ | ||
| do { \ | ||
| uint offset = 1; \ | ||
| for (uint n = BLOCK_SIZE / 2; n > 0; n /= 2) { \ | ||
| if (ident < n) { \ | ||
| uint ai = offset * (2 * ident + 1) - 1; \ | ||
| uint bi = offset * (2 * ident + 2) - 1; \ | ||
| storage[bi] += storage[ai]; \ | ||
| } \ | ||
| offset *= 2; \ | ||
| barrier(); \ | ||
| } \ | ||
| \ | ||
| if (ident == 0) { \ | ||
| storage[BLOCK_SIZE - 1] = 0; \ | ||
| } \ | ||
| barrier(); \ | ||
| \ | ||
| for (uint n = 1; n < BLOCK_SIZE; n *= 2) { \ | ||
| offset /= 2; \ | ||
| barrier(); \ | ||
| if (ident < n) { \ | ||
| uint ai = offset * (2 * ident + 1) - 1; \ | ||
| uint bi = offset * (2 * ident + 2) - 1; \ | ||
| uint temp = storage[ai]; \ | ||
| storage[ai] = storage[bi]; \ | ||
| storage[bi] += temp; \ | ||
| } \ | ||
| } \ | ||
| barrier(); \ | ||
| } while (false) | ||
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,43 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| layout(local_size_x = 512, local_size_y = 1) in; | ||
| layout(std430) buffer; | ||
|
|
||
| #include <impeller/prefix_sum.glsl> | ||
|
|
||
| #define BLOCK_SIZE 1024 | ||
|
|
||
| layout(binding = 0) readonly buffer InputData { | ||
| uint count; | ||
| uint data[]; | ||
| } | ||
| input_data; | ||
|
|
||
| layout(binding = 1) writeonly buffer OutputData { | ||
| uint data[]; | ||
| } | ||
| output_data; | ||
|
|
||
| // Needs to be number of threads per threadgroup. | ||
| shared uint memory[BLOCK_SIZE]; | ||
|
|
||
| void main() { | ||
| uint ident = gl_GlobalInvocationID.x; | ||
|
|
||
| uint value = 0; | ||
| if (ident < input_data.count) { | ||
| value = input_data.data[ident]; | ||
| } | ||
|
|
||
| memory[ident] = value; | ||
| barrier(); | ||
|
|
||
| ExclusivePrefixSum(ident, memory, BLOCK_SIZE); | ||
|
|
||
| if (ident < input_data.count) { | ||
| // Convert exclusive to inclusive sum. | ||
| output_data.data[ident] = memory[ident] + value; | ||
| } | ||
| } |
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
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.
Why a macro and not a function?
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.
Needs to work over threadgroup shared memory which can't be passed as a ptr in glsl as far as I can tell
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.
Right, GLSL doesn't have pointers, you can't pass shared memory to a function defined in another file (it will try to do a copy)