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