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_RINGBUFFERALLOCATOR_H_ 16 #define DAWNNATIVE_RINGBUFFERALLOCATOR_H_ 17 18 #include "common/SerialQueue.h" 19 #include "dawn_native/IntegerTypes.h" 20 21 #include <limits> 22 #include <memory> 23 24 // RingBufferAllocator is the front-end implementation used to manage a ring buffer in GPU memory. 25 namespace dawn_native { 26 27 class RingBufferAllocator { 28 public: 29 RingBufferAllocator() = default; 30 RingBufferAllocator(uint64_t maxSize); 31 ~RingBufferAllocator() = default; 32 RingBufferAllocator(const RingBufferAllocator&) = default; 33 RingBufferAllocator& operator=(const RingBufferAllocator&) = default; 34 35 uint64_t Allocate(uint64_t allocationSize, ExecutionSerial serial); 36 void Deallocate(ExecutionSerial lastCompletedSerial); 37 38 uint64_t GetSize() const; 39 bool Empty() const; 40 uint64_t GetUsedSize() const; 41 42 static constexpr uint64_t kInvalidOffset = std::numeric_limits<uint64_t>::max(); 43 44 private: 45 struct Request { 46 uint64_t endOffset; 47 uint64_t size; 48 }; 49 50 SerialQueue<ExecutionSerial, Request> 51 mInflightRequests; // Queue of the recorded sub-alloc requests 52 // (e.g. frame of resources). 53 54 uint64_t mUsedEndOffset = 0; // Tail of used sub-alloc requests (in bytes). 55 uint64_t mUsedStartOffset = 0; // Head of used sub-alloc requests (in bytes). 56 uint64_t mMaxBlockSize = 0; // Max size of the ring buffer (in bytes). 57 uint64_t mUsedSize = 0; // Size of the sub-alloc requests (in bytes) of the ring buffer. 58 uint64_t mCurrentRequestSize = 59 0; // Size of the sub-alloc requests (in bytes) of the current serial. 60 }; 61 } // namespace dawn_native 62 63 #endif // DAWNNATIVE_RINGBUFFERALLOCATOR_H_ 64