• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_DYNAMICUPLOADER_H_
16 #define DAWNNATIVE_DYNAMICUPLOADER_H_
17 
18 #include "dawn_native/Forward.h"
19 #include "dawn_native/IntegerTypes.h"
20 #include "dawn_native/RingBufferAllocator.h"
21 #include "dawn_native/StagingBuffer.h"
22 
23 // DynamicUploader is the front-end implementation used to manage multiple ring buffers for upload
24 // usage.
25 namespace dawn_native {
26 
27     struct UploadHandle {
28         uint8_t* mappedBuffer = nullptr;
29         uint64_t startOffset = 0;
30         StagingBufferBase* stagingBuffer = nullptr;
31     };
32 
33     class DynamicUploader {
34       public:
35         DynamicUploader(DeviceBase* device);
36         ~DynamicUploader() = default;
37 
38         // We add functions to Release StagingBuffers to the DynamicUploader as there's
39         // currently no place to track the allocated staging buffers such that they're freed after
40         // pending commands are finished. This should be changed when better resource allocation is
41         // implemented.
42         void ReleaseStagingBuffer(std::unique_ptr<StagingBufferBase> stagingBuffer);
43 
44         ResultOrError<UploadHandle> Allocate(uint64_t allocationSize,
45                                              ExecutionSerial serial,
46                                              uint64_t offsetAlignment);
47         void Deallocate(ExecutionSerial lastCompletedSerial);
48 
49       private:
50         static constexpr uint64_t kRingBufferSize = 4 * 1024 * 1024;
51 
52         struct RingBuffer {
53             std::unique_ptr<StagingBufferBase> mStagingBuffer;
54             RingBufferAllocator mAllocator;
55         };
56 
57         ResultOrError<UploadHandle> AllocateInternal(uint64_t allocationSize,
58                                                      ExecutionSerial serial);
59 
60         std::vector<std::unique_ptr<RingBuffer>> mRingBuffers;
61         SerialQueue<ExecutionSerial, std::unique_ptr<StagingBufferBase>> mReleasedStagingBuffers;
62         DeviceBase* mDevice;
63     };
64 }  // namespace dawn_native
65 
66 #endif  // DAWNNATIVE_DYNAMICUPLOADER_H_
67