Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2017 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 | // CommandGraph: |
| 7 | // Deferred work constructed by GL calls, that will later be flushed to Vulkan. |
| 8 | // |
| 9 | |
| 10 | #ifndef LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_ |
| 11 | #define LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_ |
| 12 | |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 13 | #include "libANGLE/renderer/vulkan/SecondaryCommandBuffer.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 14 | #include "libANGLE/renderer/vulkan/vk_cache_utils.h" |
| 15 | |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 16 | #if ANGLE_USE_CUSTOM_VULKAN_CMD_BUFFERS |
| 17 | using CommandBufferT = rx::vk::SecondaryCommandBuffer; |
| 18 | #else |
| 19 | using CommandBufferT = rx::vk::CommandBuffer; |
| 20 | #endif |
| 21 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 22 | namespace rx |
| 23 | { |
| 24 | |
| 25 | namespace vk |
| 26 | { |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 27 | enum class VisitedState |
| 28 | { |
| 29 | Unvisited, |
| 30 | Ready, |
| 31 | Visited, |
| 32 | }; |
| 33 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 34 | enum class CommandGraphResourceType |
| 35 | { |
| 36 | Buffer, |
| 37 | Framebuffer, |
| 38 | Image, |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 39 | Query, |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 40 | FenceSync, |
Shahbaz Youssefi | 4d15338 | 2019-02-26 15:08:11 +0000 | [diff] [blame] | 41 | DebugMarker, |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | // Certain functionality cannot be put in secondary command buffers, so they are special-cased in |
| 45 | // the node. |
| 46 | enum class CommandGraphNodeFunction |
| 47 | { |
| 48 | Generic, |
| 49 | BeginQuery, |
| 50 | EndQuery, |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 51 | WriteTimestamp, |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 52 | SetFenceSync, |
| 53 | WaitFenceSync, |
Shahbaz Youssefi | 4d15338 | 2019-02-26 15:08:11 +0000 | [diff] [blame] | 54 | InsertDebugMarker, |
| 55 | PushDebugMarker, |
| 56 | PopDebugMarker, |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 59 | // Receives notifications when a command buffer is no longer able to record. Can be used with |
| 60 | // inheritance. Faster than using an interface class since it has inlined methods. Could be used |
| 61 | // with composition by adding a getCommandBuffer method. |
| 62 | class CommandBufferOwner |
| 63 | { |
| 64 | public: |
| 65 | CommandBufferOwner() = default; |
| 66 | virtual ~CommandBufferOwner() {} |
| 67 | |
| 68 | ANGLE_INLINE void onCommandBufferFinished() { mCommandBuffer = nullptr; } |
| 69 | |
| 70 | protected: |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 71 | CommandBufferT *mCommandBuffer = nullptr; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 72 | }; |
| 73 | |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 74 | // Only used internally in the command graph. Kept in the header for better inlining performance. |
| 75 | class CommandGraphNode final : angle::NonCopyable |
| 76 | { |
| 77 | public: |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 78 | CommandGraphNode(CommandGraphNodeFunction function, angle::PoolAllocator *poolAllocator); |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 79 | ~CommandGraphNode(); |
| 80 | |
| 81 | // Immutable queries for when we're walking the commands tree. |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 82 | CommandBufferT *getOutsideRenderPassCommands() |
| 83 | { |
| 84 | ASSERT(!mHasChildren); |
| 85 | return &mOutsideRenderPassCommands; |
| 86 | } |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 87 | |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 88 | CommandBufferT *getInsideRenderPassCommands() |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 89 | { |
| 90 | ASSERT(!mHasChildren); |
| 91 | return &mInsideRenderPassCommands; |
| 92 | } |
| 93 | |
| 94 | // For outside the render pass (copies, transitions, etc). |
| 95 | angle::Result beginOutsideRenderPassRecording(Context *context, |
| 96 | const CommandPool &commandPool, |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 97 | CommandBufferT **commandsOut); |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 98 | |
| 99 | // For rendering commands (draws). |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 100 | angle::Result beginInsideRenderPassRecording(Context *context, CommandBufferT **commandsOut); |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 101 | |
| 102 | // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass. |
| 103 | void storeRenderPassInfo(const Framebuffer &framebuffer, |
| 104 | const gl::Rectangle renderArea, |
| 105 | const vk::RenderPassDesc &renderPassDesc, |
| 106 | const std::vector<VkClearValue> &clearValues); |
| 107 | |
| 108 | // Dependency commands order node execution in the command graph. |
| 109 | // Once a node has commands that must happen after it, recording is stopped and the node is |
| 110 | // frozen forever. |
| 111 | static void SetHappensBeforeDependency(CommandGraphNode *beforeNode, |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 112 | CommandGraphNode *afterNode) |
| 113 | { |
| 114 | ASSERT(beforeNode != afterNode && !beforeNode->isChildOf(afterNode)); |
| 115 | afterNode->mParents.emplace_back(beforeNode); |
| 116 | beforeNode->setHasChildren(); |
| 117 | } |
| 118 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 119 | static void SetHappensBeforeDependencies(CommandGraphNode **beforeNodes, |
| 120 | size_t beforeNodesCount, |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 121 | CommandGraphNode *afterNode); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 122 | static void SetHappensBeforeDependencies(CommandGraphNode *beforeNode, |
| 123 | CommandGraphNode **afterNodes, |
| 124 | size_t afterNodesCount); |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 125 | bool hasParents() const; |
| 126 | bool hasChildren() const { return mHasChildren; } |
| 127 | |
| 128 | // Commands for traversing the node on a flush operation. |
| 129 | VisitedState visitedState() const; |
| 130 | void visitParents(std::vector<CommandGraphNode *> *stack); |
| 131 | angle::Result visitAndExecute(Context *context, |
| 132 | Serial serial, |
| 133 | RenderPassCache *renderPassCache, |
| 134 | CommandBuffer *primaryCommandBuffer); |
| 135 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 136 | // Only used in the command graph diagnostics. |
| 137 | const std::vector<CommandGraphNode *> &getParentsForDiagnostics() const; |
| 138 | void setDiagnosticInfo(CommandGraphResourceType resourceType, uintptr_t resourceID); |
| 139 | |
| 140 | CommandGraphResourceType getResourceTypeForDiagnostics() const { return mResourceType; } |
| 141 | uintptr_t getResourceIDForDiagnostics() const { return mResourceID; } |
| 142 | |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 143 | const gl::Rectangle &getRenderPassRenderArea() const; |
| 144 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 145 | CommandGraphNodeFunction getFunction() const { return mFunction; } |
| 146 | |
| 147 | void setQueryPool(const QueryPool *queryPool, uint32_t queryIndex); |
Shahbaz Youssefi | de52ca3 | 2019-03-13 14:23:30 -0400 | [diff] [blame] | 148 | VkQueryPool getQueryPool() const { return mQueryPool; } |
| 149 | uint32_t getQueryIndex() const { return mQueryIndex; } |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 150 | void setFenceSync(const vk::Event &event); |
Shahbaz Youssefi | 4d15338 | 2019-02-26 15:08:11 +0000 | [diff] [blame] | 151 | void setDebugMarker(GLenum source, std::string &&marker); |
| 152 | const std::string &getDebugMarker() const { return mDebugMarker; } |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 153 | |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 154 | ANGLE_INLINE void addGlobalMemoryBarrier(VkFlags srcAccess, VkFlags dstAccess) |
| 155 | { |
| 156 | mGlobalMemoryBarrierSrcAccess |= srcAccess; |
| 157 | mGlobalMemoryBarrierDstAccess |= dstAccess; |
| 158 | } |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 159 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 160 | // This can only be set for RenderPass nodes. Each RenderPass node can have at most one owner. |
| 161 | void setCommandBufferOwner(CommandBufferOwner *owner) |
| 162 | { |
| 163 | ASSERT(mCommandBufferOwner == nullptr); |
| 164 | mCommandBufferOwner = owner; |
| 165 | } |
| 166 | |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 167 | private: |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 168 | ANGLE_INLINE void setHasChildren() |
| 169 | { |
| 170 | mHasChildren = true; |
| 171 | if (mCommandBufferOwner) |
| 172 | { |
| 173 | mCommandBufferOwner->onCommandBufferFinished(); |
| 174 | } |
| 175 | } |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 176 | |
| 177 | // Used for testing only. |
| 178 | bool isChildOf(CommandGraphNode *parent); |
| 179 | |
| 180 | // Only used if we need a RenderPass for these commands. |
| 181 | RenderPassDesc mRenderPassDesc; |
| 182 | Framebuffer mRenderPassFramebuffer; |
| 183 | gl::Rectangle mRenderPassRenderArea; |
| 184 | gl::AttachmentArray<VkClearValue> mRenderPassClearValues; |
| 185 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 186 | CommandGraphNodeFunction mFunction; |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 187 | angle::PoolAllocator *mPoolAllocator; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 188 | // Keep separate buffers for commands inside and outside a RenderPass. |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 189 | // TODO(jmadill): We might not need inside and outside RenderPass commands separate. |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 190 | CommandBufferT mOutsideRenderPassCommands; |
| 191 | CommandBufferT mInsideRenderPassCommands; |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 192 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 193 | // Special-function additional data: |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 194 | // Queries: |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 195 | VkQueryPool mQueryPool; |
| 196 | uint32_t mQueryIndex; |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 197 | // GLsync and EGLSync: |
| 198 | VkEvent mFenceSyncEvent; |
Shahbaz Youssefi | 4d15338 | 2019-02-26 15:08:11 +0000 | [diff] [blame] | 199 | // Debug markers: |
| 200 | GLenum mDebugMarkerSource; |
| 201 | std::string mDebugMarker; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 202 | |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 203 | // Parents are commands that must be submitted before 'this' CommandNode can be submitted. |
| 204 | std::vector<CommandGraphNode *> mParents; |
| 205 | |
| 206 | // If this is true, other commands exist that must be submitted after 'this' command. |
| 207 | bool mHasChildren; |
| 208 | |
| 209 | // Used when traversing the dependency graph. |
| 210 | VisitedState mVisitedState; |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 211 | |
| 212 | // Additional diagnostic information. |
| 213 | CommandGraphResourceType mResourceType; |
| 214 | uintptr_t mResourceID; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 215 | |
| 216 | // For global memory barriers. |
| 217 | VkFlags mGlobalMemoryBarrierSrcAccess; |
| 218 | VkFlags mGlobalMemoryBarrierDstAccess; |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 219 | |
| 220 | // Command buffer notifications. |
| 221 | CommandBufferOwner *mCommandBufferOwner; |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 222 | }; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 223 | |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 224 | // This is a helper class for back-end objects used in Vk command buffers. It records a serial |
| 225 | // at command recording times indicating an order in the queue. We use Fences to detect when |
| 226 | // commands finish, and then release any unreferenced and deleted resources based on the stored |
| 227 | // queue serial in a special 'garbage' queue. Resources also track current read and write |
| 228 | // dependencies. Only one command buffer node can be writing to the Resource at a time, but many |
| 229 | // can be reading from it. Together the dependencies will form a command graph at submission time. |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 230 | class CommandGraphResource : angle::NonCopyable |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 231 | { |
| 232 | public: |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 233 | virtual ~CommandGraphResource(); |
| 234 | |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 235 | // Returns true if the resource is in use by the renderer. |
| 236 | bool isResourceInUse(RendererVk *renderer) const; |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 237 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 238 | // Get the current queue serial for this resource. Used to release resources, and for |
| 239 | // queries, to know if the queue they are submitted on has finished execution. |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 240 | Serial getStoredQueueSerial() const { return mStoredQueueSerial; } |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 241 | |
Jamie Madill | d014c9e | 2018-05-18 15:15:59 -0400 | [diff] [blame] | 242 | // Sets up dependency relations. 'this' resource is the resource being written to. |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 243 | void addWriteDependency(CommandGraphResource *writingResource); |
Jamie Madill | d014c9e | 2018-05-18 15:15:59 -0400 | [diff] [blame] | 244 | |
| 245 | // Sets up dependency relations. 'this' resource is the resource being read. |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 246 | void addReadDependency(CommandGraphResource *readingResource); |
Jamie Madill | d014c9e | 2018-05-18 15:15:59 -0400 | [diff] [blame] | 247 | |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 248 | // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource |
| 249 | // was not used in this set of command nodes. |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 250 | ANGLE_INLINE void updateQueueSerial(Serial queueSerial) |
| 251 | { |
| 252 | ASSERT(queueSerial >= mStoredQueueSerial); |
| 253 | |
| 254 | if (queueSerial > mStoredQueueSerial) |
| 255 | { |
| 256 | mCurrentWritingNode = nullptr; |
| 257 | mCurrentReadingNodes.clear(); |
| 258 | mStoredQueueSerial = queueSerial; |
| 259 | } |
| 260 | } |
Shahbaz Youssefi | 254b32c | 2018-11-26 11:58:03 -0500 | [diff] [blame] | 261 | |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 262 | // Allocates a write node via getNewWriteNode and returns a started command buffer. |
| 263 | // The started command buffer will render outside of a RenderPass. |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 264 | // Will append to an existing command buffer/graph node if possible. |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 265 | angle::Result recordCommands(Context *context, CommandBufferT **commandBufferOut); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 266 | |
| 267 | // Begins a command buffer on the current graph node for in-RenderPass rendering. |
Shahbaz Youssefi | f83a28a | 2018-12-09 03:48:34 +0100 | [diff] [blame] | 268 | // Called from FramebufferVk::startNewRenderPass and UtilsVk functions. |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 269 | angle::Result beginRenderPass(ContextVk *contextVk, |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 270 | const Framebuffer &framebuffer, |
| 271 | const gl::Rectangle &renderArea, |
| 272 | const RenderPassDesc &renderPassDesc, |
| 273 | const std::vector<VkClearValue> &clearValues, |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 274 | CommandBufferT **commandBufferOut); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 275 | |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 276 | // Checks if we're in a RenderPass, returning true if so. Updates serial internally. |
| 277 | // Returns the started command buffer in commandBufferOut. |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 278 | ANGLE_INLINE bool appendToStartedRenderPass(Serial currentQueueSerial, |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 279 | CommandBufferT **commandBufferOut) |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 280 | { |
| 281 | updateQueueSerial(currentQueueSerial); |
| 282 | if (hasStartedRenderPass()) |
| 283 | { |
| 284 | *commandBufferOut = mCurrentWritingNode->getInsideRenderPassCommands(); |
| 285 | return true; |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | return false; |
| 290 | } |
| 291 | } |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 292 | |
| 293 | // Accessor for RenderPass RenderArea. |
| 294 | const gl::Rectangle &getRenderPassRenderArea() const; |
| 295 | |
| 296 | // Called when 'this' object changes, but we'd like to start a new command buffer later. |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 297 | void finishCurrentCommands(RendererVk *renderer); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 298 | |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 299 | // Store a deferred memory barrier. Will be recorded into a primary command buffer at submit. |
| 300 | void addGlobalMemoryBarrier(VkFlags srcAccess, VkFlags dstAccess) |
| 301 | { |
| 302 | ASSERT(mCurrentWritingNode); |
| 303 | mCurrentWritingNode->addGlobalMemoryBarrier(srcAccess, dstAccess); |
| 304 | } |
| 305 | |
Jamie Madill | 2d03ff4 | 2018-09-27 15:04:26 -0400 | [diff] [blame] | 306 | protected: |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 307 | explicit CommandGraphResource(CommandGraphResourceType resourceType); |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 308 | |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 309 | private: |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 310 | // Returns true if this node has a current writing node with no children. |
Jamie Madill | c759b8b | 2019-01-03 15:16:50 -0500 | [diff] [blame] | 311 | ANGLE_INLINE bool hasChildlessWritingNode() const |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 312 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 313 | // Note: currently, we don't have a resource that can issue both generic and special |
| 314 | // commands. We don't create read/write dependencies between mixed generic/special |
| 315 | // resources either. As such, we expect the function to always be generic here. If such a |
| 316 | // resource is added in the future, this can add a check for function == generic and fail if |
| 317 | // false. |
| 318 | ASSERT(mCurrentWritingNode == nullptr || |
| 319 | mCurrentWritingNode->getFunction() == CommandGraphNodeFunction::Generic); |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 320 | return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren()); |
| 321 | } |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 322 | |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 323 | // Checks if we're in a RenderPass without children. |
Jamie Madill | 3d61ac2 | 2018-08-28 16:58:55 -0400 | [diff] [blame] | 324 | bool hasStartedRenderPass() const |
| 325 | { |
| 326 | return hasChildlessWritingNode() && |
| 327 | mCurrentWritingNode->getInsideRenderPassCommands()->valid(); |
| 328 | } |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 329 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 330 | void startNewCommands(RendererVk *renderer); |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 331 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 332 | void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial); |
| 333 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 334 | Serial mStoredQueueSerial; |
| 335 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 336 | std::vector<CommandGraphNode *> mCurrentReadingNodes; |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 337 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 338 | // Current command graph writing node. |
| 339 | CommandGraphNode *mCurrentWritingNode; |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 340 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 341 | // Additional diagnostic information. |
| 342 | CommandGraphResourceType mResourceType; |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 343 | }; |
| 344 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 345 | // Translating OpenGL commands into Vulkan and submitting them immediately loses out on some |
| 346 | // of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically |
| 347 | // clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions |
| 348 | // can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends |
| 349 | // when processing interleaved draw operations on independent Framebuffers. |
| 350 | // |
| 351 | // ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred |
| 352 | // command submission. We also sometimes call this command re-ordering. A brief summary: |
| 353 | // |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 354 | // During GL command processing, we record Vulkan commands into SecondaryCommandBuffers, which |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 355 | // are stored in CommandGraphNodes, and these nodes are chained together via dependencies to |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 356 | // form a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 357 | // SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 358 | // CommandGraph, starting at the most senior nodes, recording SecondaryCommandBuffers inside |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 359 | // and outside RenderPasses as necessary, filled with the right load/store operations. Once |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 360 | // the primary CommandBuffer has recorded all of the SecondaryCommandBuffers from all the open |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 361 | // CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device. |
Jamie Madill | a5e0607 | 2018-05-18 14:36:05 -0400 | [diff] [blame] | 362 | // |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 363 | // The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new |
| 364 | // nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and |
| 365 | // also submitting the whole command graph via submitCommands. |
| 366 | class CommandGraph final : angle::NonCopyable |
| 367 | { |
| 368 | public: |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 369 | explicit CommandGraph(bool enableGraphDiagnostics, angle::PoolAllocator *poolAllocator); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 370 | ~CommandGraph(); |
| 371 | |
| 372 | // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering |
| 373 | // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 374 | // to set up dependency relations. If the node is a barrier, it will automatically add |
| 375 | // dependencies between the previous barrier, the new barrier and all nodes in between. |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 376 | CommandGraphNode *allocateNode(CommandGraphNodeFunction function); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 377 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 378 | angle::Result submitCommands(Context *context, |
| 379 | Serial serial, |
| 380 | RenderPassCache *renderPassCache, |
| 381 | CommandPool *commandPool, |
| 382 | CommandBuffer *primaryCommandBufferOut); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 383 | bool empty() const; |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 384 | void clear(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 385 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 386 | // The following create special-function nodes that don't require a graph resource. |
| 387 | // Queries: |
| 388 | void beginQuery(const QueryPool *queryPool, uint32_t queryIndex); |
| 389 | void endQuery(const QueryPool *queryPool, uint32_t queryIndex); |
| 390 | void writeTimestamp(const QueryPool *queryPool, uint32_t queryIndex); |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame] | 391 | // GLsync and EGLSync: |
| 392 | void setFenceSync(const vk::Event &event); |
| 393 | void waitFenceSync(const vk::Event &event); |
Shahbaz Youssefi | 4d15338 | 2019-02-26 15:08:11 +0000 | [diff] [blame] | 394 | // Debug markers: |
| 395 | void insertDebugMarker(GLenum source, std::string &&marker); |
| 396 | void pushDebugMarker(GLenum source, std::string &&marker); |
| 397 | void popDebugMarker(); |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 398 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 399 | private: |
Shahbaz Youssefi | de52ca3 | 2019-03-13 14:23:30 -0400 | [diff] [blame] | 400 | CommandGraphNode *allocateBarrierNode(CommandGraphNodeFunction function, |
| 401 | CommandGraphResourceType resourceType, |
| 402 | uintptr_t resourceID); |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 403 | void setNewBarrier(CommandGraphNode *newBarrier); |
| 404 | CommandGraphNode *getLastBarrierNode(size_t *indexOut); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 405 | void addDependenciesToNextBarrier(size_t begin, size_t end, CommandGraphNode *nextBarrier); |
| 406 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 407 | void dumpGraphDotFile(std::ostream &out) const; |
| 408 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 409 | std::vector<CommandGraphNode *> mNodes; |
| 410 | bool mEnableGraphDiagnostics; |
Tobin Ehlis | 134425c | 2019-03-15 17:02:17 -0600 | [diff] [blame^] | 411 | angle::PoolAllocator *mPoolAllocator; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 412 | |
| 413 | // A set of nodes (eventually) exist that act as barriers to guarantee submission order. For |
| 414 | // example, a glMemoryBarrier() calls would lead to such a barrier or beginning and ending a |
| 415 | // query. This is because the graph can reorder operations if it sees fit. Let's call a barrier |
| 416 | // node Bi, and the other nodes Ni. The edges between Ni don't interest us. Before a barrier is |
| 417 | // inserted, we have: |
| 418 | // |
| 419 | // N0 N1 ... Na |
| 420 | // \___\__/_/ (dependency egdes, which we don't care about so I'll stop drawing them. |
| 421 | // \/ |
| 422 | // |
| 423 | // When the first barrier is inserted, we will have: |
| 424 | // |
| 425 | // ______ |
| 426 | // / ____\ |
| 427 | // / / \ |
| 428 | // / / /\ |
| 429 | // N0 N1 ... Na B0 |
| 430 | // |
| 431 | // This makes sure all N0..Na are called before B0. From then on, B0 will be the current |
| 432 | // "barrier point" which extends an edge to every next node: |
| 433 | // |
| 434 | // ______ |
| 435 | // / ____\ |
| 436 | // / / \ |
| 437 | // / / /\ |
| 438 | // N0 N1 ... Na B0 Na+1 ... Nb |
| 439 | // \/ / |
| 440 | // \______/ |
| 441 | // |
| 442 | // |
| 443 | // When the next barrier B1 is met, all nodes between B0 and B1 will add a depenency on B1 as |
| 444 | // well, and the "barrier point" is updated. |
| 445 | // |
| 446 | // ______ |
| 447 | // / ____\ ______ ______ |
| 448 | // / / \ / \ / \ |
| 449 | // / / /\ / /\ / /\ |
| 450 | // N0 N1 ... Na B0 Na+1 ... Nb B1 Nb+1 ... Nc B2 ... |
| 451 | // \/ / / \/ / / |
| 452 | // \______/ / \______/ / |
| 453 | // \_______/ \_______/ |
| 454 | // |
| 455 | // |
| 456 | // When barrier Bi is introduced, all nodes added since Bi-1 need to add a dependency to Bi |
| 457 | // (including Bi-1). We therefore keep track of the node index of the last barrier that was |
| 458 | // issued. |
| 459 | static constexpr size_t kInvalidNodeIndex = std::numeric_limits<std::size_t>::max(); |
| 460 | size_t mLastBarrierIndex; |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 461 | }; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 462 | } // namespace vk |
| 463 | } // namespace rx |
| 464 | |
| 465 | #endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_ |