• 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 "common/SimpleMutex.h"
15 #include "libANGLE/renderer/vulkan/vk_command_buffer_utils.h"
16 #include "libANGLE/renderer/vulkan/vk_wrapper.h"
17 
18 namespace rx
19 {
20 namespace vk
21 {
22 class ErrorContext;
23 class VulkanSecondaryCommandBuffer;
24 
25 // VkCommandPool must be externally synchronized when its Command Buffers are: allocated, freed,
26 // reset, or recorded. This class ensures that Command Buffers are freed from the thread that
27 // recording commands (Context thread).
28 class SecondaryCommandPool final : angle::NonCopyable
29 {
30   public:
31     SecondaryCommandPool();
32     ~SecondaryCommandPool();
33 
34     angle::Result init(ErrorContext *context,
35                        uint32_t queueFamilyIndex,
36                        ProtectionType protectionType);
37     void destroy(VkDevice device);
38 
valid()39     bool valid() const { return mCommandPool.valid(); }
40 
41     // Call only from the Context thread that owns the SecondaryCommandPool instance.
42     angle::Result allocate(ErrorContext *context, VulkanSecondaryCommandBuffer *buffer);
43 
44     // Single threaded - use external synchronization.
45     void collect(VulkanSecondaryCommandBuffer *buffer);
46 
47   private:
48     static constexpr size_t kFixedQueueLimit = 100u;
49 
50     // Context thread access members.
51 
52     void freeCollectedBuffers(VkDevice device);
53 
54     // Use single pool for now.
55     CommandPool mCommandPool;
56 
57     // Other thread access members.
58 
59     // Fast lock free queue for processing buffers while new may be added from the other thread.
60     angle::FixedQueue<VkCommandBuffer> mCollectedBuffers;
61 
62     // Overflow vector to use in cases when FixedQueue is filled.
63     std::vector<VkCommandBuffer> mCollectedBuffersOverflow;
64     angle::SimpleMutex mOverflowMutex;
65     std::atomic<bool> mHasOverflow;
66 };
67 
68 }  // namespace vk
69 }  // namespace rx
70 
71 #endif  // LIBANGLE_RENDERER_VULKAN_SECONDARYCOMMANDPOOL_H_
72