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] Flutter GPU: Add HostBuffer. #44696
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
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,40 @@ | ||
| // 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. | ||
|
|
||
| #include "flutter/lib/gpu/host_buffer.h" | ||
|
|
||
| #include "impeller/core/host_buffer.h" | ||
| #include "impeller/core/platform.h" | ||
| #include "third_party/tonic/typed_data/dart_byte_data.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| IMPLEMENT_WRAPPERTYPEINFO(gpu, HostBuffer); | ||
|
|
||
| HostBuffer::HostBuffer() : host_buffer_(impeller::HostBuffer::Create()) {} | ||
|
|
||
| HostBuffer::~HostBuffer() = default; | ||
|
|
||
| size_t HostBuffer::EmplaceBytes(const tonic::DartByteData& byte_data) { | ||
| auto view = | ||
| host_buffer_->Emplace(byte_data.data(), byte_data.length_in_bytes(), | ||
| impeller::DefaultUniformAlignment()); | ||
| return view.range.offset; | ||
| } | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| //---------------------------------------------------------------------------- | ||
| /// Exports | ||
| /// | ||
|
|
||
| void InternalFlutterGpu_HostBuffer_Initialize(Dart_Handle wrapper) { | ||
| auto res = fml::MakeRefCounted<flutter::HostBuffer>(); | ||
| res->AssociateWithDartWrapper(wrapper); | ||
| } | ||
|
|
||
| size_t InternalFlutterGpu_HostBuffer_EmplaceBytes(flutter::HostBuffer* wrapper, | ||
| Dart_Handle byte_data) { | ||
| return wrapper->EmplaceBytes(tonic::DartByteData(byte_data)); | ||
| } |
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,47 @@ | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "flutter/lib/gpu/export.h" | ||
| #include "flutter/lib/ui/dart_wrapper.h" | ||
| #include "impeller/core/host_buffer.h" | ||
| #include "third_party/tonic/typed_data/dart_byte_data.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| class HostBuffer : public RefCountedDartWrappable<HostBuffer> { | ||
| DEFINE_WRAPPERTYPEINFO(); | ||
| FML_FRIEND_MAKE_REF_COUNTED(HostBuffer); | ||
|
|
||
| public: | ||
| explicit HostBuffer(); | ||
|
|
||
| ~HostBuffer() override; | ||
|
|
||
| size_t EmplaceBytes(const tonic::DartByteData& byte_data); | ||
|
|
||
| private: | ||
| std::shared_ptr<impeller::HostBuffer> host_buffer_; | ||
|
|
||
| FML_DISALLOW_COPY_AND_ASSIGN(HostBuffer); | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| //---------------------------------------------------------------------------- | ||
| /// Exports | ||
| /// | ||
|
|
||
| extern "C" { | ||
|
|
||
| FLUTTER_GPU_EXPORT | ||
| extern void InternalFlutterGpu_HostBuffer_Initialize(Dart_Handle wrapper); | ||
|
|
||
| FLUTTER_GPU_EXPORT | ||
| extern size_t InternalFlutterGpu_HostBuffer_EmplaceBytes( | ||
| flutter::HostBuffer* wrapper, | ||
| Dart_Handle byte_data); | ||
|
|
||
| } // extern "C" |
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,67 @@ | ||
| // 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. | ||
|
|
||
| import 'dart:ffi'; | ||
| import 'dart:nativewrappers'; | ||
| import 'dart:typed_data'; | ||
|
|
||
| /// A reference to a byte range within a GPU-resident [Buffer]. | ||
| class BufferView { | ||
| /// The buffer of this view. | ||
| final HostBuffer buffer; | ||
|
|
||
| /// The start of the view, in bytes starting from the beginning of the | ||
| /// [buffer]. | ||
| final int offsetInBytes; | ||
|
|
||
| /// The length of the view. | ||
| final int lengthInBytes; | ||
|
|
||
| /// Create a new view into a buffer on the GPU. | ||
| const BufferView(this.buffer, | ||
| {required this.offsetInBytes, required this.lengthInBytes}); | ||
| } | ||
|
|
||
| /// [HostBuffer] is a [Buffer] which is allocated on the host (native CPU | ||
| /// resident memory) and lazily uploaded to the GPU. A [HostBuffer] can be | ||
| /// safely mutated or extended at any time on the host, and will be | ||
| /// automatically re-uploaded to the GPU the next time a GPU operation needs to | ||
| /// access it. | ||
| /// | ||
| /// This is useful for efficiently chunking sparse data uploads, especially | ||
| /// ephemeral uniform data that needs to change from frame to frame. | ||
| /// | ||
| /// Different platforms have different data alignment requirements for accessing | ||
| /// device buffer data. The [HostBuffer] takes these requirements into account | ||
| /// and automatically inserts padding between emplaced data if necessary. | ||
| base class HostBuffer extends NativeFieldWrapperClass1 { | ||
| /// Creates a new HostBuffer. | ||
| HostBuffer() { | ||
| _initialize(); | ||
| } | ||
|
|
||
| /// Wrap with native counterpart. | ||
| @Native<Void Function(Handle)>( | ||
| symbol: 'InternalFlutterGpu_HostBuffer_Initialize') | ||
| external void _initialize(); | ||
|
|
||
| /// Append byte data to the end of the [HostBuffer] and produce a [BufferView] | ||
| /// that references the new data in the buffer. | ||
| /// | ||
| /// This method automatically inserts padding in-between emplace calls in the | ||
| /// buffer if necessary to abide by platform-specific uniform alignment | ||
| /// requirements. | ||
| /// | ||
| /// The updated buffer will be uploaded to the GPU if the returned | ||
| /// [BufferView] is used by a rendering command. | ||
| BufferView emplace(ByteData bytes) { | ||
| int resultOffset = _emplaceBytes(bytes); | ||
| return BufferView(this, | ||
| offsetInBytes: resultOffset, lengthInBytes: bytes.lengthInBytes); | ||
| } | ||
|
|
||
| @Native<Uint64 Function(Pointer<Void>, Handle)>( | ||
| symbol: 'InternalFlutterGpu_HostBuffer_EmplaceBytes') | ||
| external int _emplaceBytes(ByteData bytes); | ||
| } | ||
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.
its not really "on the gpu though"? For more functional pieces like the buffer view it may be easier to document once there are things that can be done with it. for example, "a view into a buffer that can be used for binding uniform data yada yada"
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.
I think
BufferViews are effectively GPU-only because they'll only be used for binding buffers toCommands. I don't think we'd use aBufferViewfor updating a range on the host buffer, for example.