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