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_RINGBUFFER_H_ 16 #define DAWNNATIVE_RINGBUFFER_H_ 17 18 #include "common/SerialQueue.h" 19 #include "dawn_native/StagingBuffer.h" 20 21 #include <memory> 22 23 // RingBuffer is the front-end implementation used to manage a ring buffer in GPU memory. 24 namespace dawn_native { 25 26 struct UploadHandle { 27 uint8_t* mappedBuffer = nullptr; 28 size_t startOffset = 0; 29 StagingBufferBase* stagingBuffer = nullptr; 30 }; 31 32 class DeviceBase; 33 34 class RingBuffer { 35 public: 36 RingBuffer(DeviceBase* device, size_t size); 37 ~RingBuffer() = default; 38 39 MaybeError Initialize(); 40 41 UploadHandle SubAllocate(size_t requestedSize); 42 43 void Tick(Serial lastCompletedSerial); 44 size_t GetSize() const; 45 bool Empty() const; 46 size_t GetUsedSize() const; 47 StagingBufferBase* GetStagingBuffer() const; 48 49 // Seperated for testing. 50 void Track(); 51 52 private: 53 std::unique_ptr<StagingBufferBase> mStagingBuffer; 54 55 struct Request { 56 size_t endOffset; 57 size_t size; 58 }; 59 60 SerialQueue<Request> mInflightRequests; // Queue of the recorded sub-alloc requests (e.g. 61 // frame of resources). 62 63 size_t mUsedEndOffset = 0; // Tail of used sub-alloc requests (in bytes). 64 size_t mUsedStartOffset = 0; // Head of used sub-alloc requests (in bytes). 65 size_t mBufferSize = 0; // Max size of the ring buffer (in bytes). 66 size_t mUsedSize = 0; // Size of the sub-alloc requests (in bytes) of the ring buffer. 67 size_t mCurrentRequestSize = 68 0; // Size of the sub-alloc requests (in bytes) of the current serial. 69 70 DeviceBase* mDevice; 71 }; 72 } // namespace dawn_native 73 74 #endif // DAWNNATIVE_RINGBUFFER_H_