-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Improve performance of line rendering #6230
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19649ac
Improve performance of line rendering
davepagurek 26f491f
Add comments
davepagurek 7f43aea
Rename DataVector to DataArray
davepagurek 3efab94
Add docs example
davepagurek 5161362
Merge branch 'main' into feat/faster-lines
davepagurek 8baa00e
Merge branch 'main' into feat/faster-lines
davepagurek 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,110 @@ | ||
| import p5 from '../core/main'; | ||
|
|
||
| /** | ||
| * An internal class to store data that will be sent to a p5.RenderBuffer. | ||
| * Those need to eventually go into a Float32Array, so this class provides a | ||
| * variable-length array container backed by a Float32Array so that it can be | ||
| * sent to the GPU without allocating a new array each frame. | ||
| * | ||
| * Like a C++ vector, its fixed-length Float32Array backing its contents will | ||
| * double in size when it goes over its capacity. | ||
| * | ||
| * @example | ||
| * <div> | ||
| * <code> | ||
| * // Initialize storage with a capacity of 4 | ||
| * const storage = new DataArray(4); | ||
| * console.log(storage.data.length); // 4 | ||
| * console.log(storage.length); // 0 | ||
| * console.log(storage.dataArray()); // Empty Float32Array | ||
| * | ||
| * storage.push(1, 2, 3, 4, 5, 6); | ||
| * console.log(storage.data.length); // 8 | ||
| * console.log(storage.length); // 6 | ||
| * console.log(storage.dataArray()); // Float32Array{1, 2, 3, 4, 5, 6} | ||
| * </code> | ||
| * </div> | ||
| */ | ||
| p5.DataArray = class DataArray { | ||
| constructor(initialLength = 128) { | ||
| this.length = 0; | ||
| this.data = new Float32Array(initialLength); | ||
| this.initialLength = initialLength; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a Float32Array window sized to the exact length of the data | ||
| */ | ||
| dataArray() { | ||
| return this.subArray(0, this.length); | ||
| } | ||
|
|
||
| /** | ||
| * A "soft" clear, which keeps the underlying storage size the same, but | ||
| * empties the contents of its dataArray() | ||
| */ | ||
| clear() { | ||
| this.length = 0; | ||
| } | ||
|
|
||
| /** | ||
| * Can be used to scale a DataArray back down to fit its contents. | ||
| */ | ||
| rescale() { | ||
| if (this.length < this.data.length / 2) { | ||
| // Find the power of 2 size that fits the data | ||
| const targetLength = 1 << Math.ceil(Math.log2(this.length)); | ||
| const newData = new Float32Array(targetLength); | ||
| newData.set(this.data.subarray(0, this.length), 0); | ||
| this.data = newData; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A full reset, which allocates a new underlying Float32Array at its initial | ||
| * length | ||
| */ | ||
| reset() { | ||
| this.clear(); | ||
| this.data = new Float32Array(this.initialLength); | ||
| } | ||
|
|
||
| /** | ||
| * Adds values to the DataArray, expanding its internal storage to | ||
| * accommodate the new items. | ||
| */ | ||
| push(...values) { | ||
| this.ensureLength(this.length + values.length); | ||
| this.data.set(values, this.length); | ||
| this.length += values.length; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a copy of the data from the index `from`, inclusive, to the index | ||
| * `to`, exclusive | ||
| */ | ||
| slice(from, to) { | ||
| return this.data.slice(from, Math.min(to, this.length)); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a mutable Float32Array window from the index `from`, inclusive, to | ||
| * the index `to`, exclusive | ||
| */ | ||
| subArray(from, to) { | ||
| return this.data.subarray(from, Math.min(to, this.length)); | ||
| } | ||
|
|
||
| /** | ||
| * Expand capacity of the internal storage until it can fit a target size | ||
| */ | ||
| ensureLength(target) { | ||
| while (this.data.length < target) { | ||
| const newData = new Float32Array(this.data.length * 2); | ||
| newData.set(this.data, 0); | ||
| this.data = newData; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default p5.DataArray; |
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.
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.