• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2023 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // SecondaryCommandPool:
7 //    A class for allocating Command Buffers for VulkanSecondaryCommandBuffer.
8 //
9 
10 #ifndef LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
11 #define LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
12 
13 #include "common/FixedQueue.h"
14 #include "libANGLE/renderer/vulkan/vk_command_buffer_utils.h"
15 #include "libANGLE/renderer/vulkan/vk_wrapper.h"
16 
17 namespace rx
18 {
19 namespace vk
20 {
21 class Context;
22 class VulkanSecondaryCommandBuffer;
23 
24 // VkCommandPool must be externally synchronized when its Command Buffers are: allocated, freed,
25 // reset, or recorded. This class ensures that Command Buffers are freed from the thread that
26 // recording commands (Context thread).
27 class SecondaryCommandPool final : angle::NonCopyable
28 {
29   public:
30     SecondaryCommandPool();
31     ~SecondaryCommandPool();
32 
33     angle::Result init(Context *context, uint32_t queueFamilyIndex, ProtectionType protectionType);
34     void destroy(VkDevice device);
35 
valid()36     bool valid() const { return mCommandPool.valid(); }
37 
38     // Call only from the Context thread that owns the SecondaryCommandPool instance.
39     angle::Result allocate(Context *context, VulkanSecondaryCommandBuffer *buffer);
40 
41     // Single threaded - use external synchronization.
42     // Example threads: any Context thread or "asyncCommandQueue" thread.
43     void collect(VulkanSecondaryCommandBuffer *buffer);
44 
45   private:
46     static constexpr size_t kFixedQueueLimit = 100u;
47 
48     // Context thread access members.
49 
50     void freeCollectedBuffers(VkDevice device);
51 
52     // Use single pool for now.
53     CommandPool mCommandPool;
54 
55     // Other thread access members.
56 
57     // Fast lock free queue for processing buffers while new may be added from the other thread.
58     angle::FixedQueue<VkCommandBuffer, kFixedQueueLimit> mCollectedBuffers;
59 
60     // Overflow vector to use in cases when FixedQueue is filled.
61     std::vector<VkCommandBuffer> mCollectedBuffersOverflow;
62     std::mutex mOverflowMutex;
63     std::atomic<bool> mHasOverflow;
64 };
65 
66 }  // namespace vk
67 }  // namespace rx
68 
69 #endif  // LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
70