blob: f598fde26b5c6aa2e065b595f9383afc31a92f4d [file] [log] [blame]
Jamie Madill1f46bc12018-02-20 16:09:43 -05001//
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
15namespace rx
16{
17
18namespace vk
19{
Jamie Madill3d61ac22018-08-28 16:58:55 -040020enum class VisitedState
21{
22 Unvisited,
23 Ready,
24 Visited,
25};
26
Jamie Madill0da73fe2018-10-02 09:31:39 -040027enum class CommandGraphResourceType
28{
29 Buffer,
30 Framebuffer,
31 Image,
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040032 Query,
33};
34
35// Certain functionality cannot be put in secondary command buffers, so they are special-cased in
36// the node.
37enum class CommandGraphNodeFunction
38{
39 Generic,
40 BeginQuery,
41 EndQuery,
Jamie Madill0da73fe2018-10-02 09:31:39 -040042};
43
Jamie Madill3d61ac22018-08-28 16:58:55 -040044// Only used internally in the command graph. Kept in the header for better inlining performance.
45class CommandGraphNode final : angle::NonCopyable
46{
47 public:
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040048 CommandGraphNode(CommandGraphNodeFunction function);
Jamie Madill3d61ac22018-08-28 16:58:55 -040049 ~CommandGraphNode();
50
51 // Immutable queries for when we're walking the commands tree.
52 CommandBuffer *getOutsideRenderPassCommands();
53
54 CommandBuffer *getInsideRenderPassCommands()
55 {
56 ASSERT(!mHasChildren);
57 return &mInsideRenderPassCommands;
58 }
59
60 // For outside the render pass (copies, transitions, etc).
61 angle::Result beginOutsideRenderPassRecording(Context *context,
62 const CommandPool &commandPool,
63 CommandBuffer **commandsOut);
64
65 // For rendering commands (draws).
66 angle::Result beginInsideRenderPassRecording(Context *context, CommandBuffer **commandsOut);
67
68 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
69 void storeRenderPassInfo(const Framebuffer &framebuffer,
70 const gl::Rectangle renderArea,
71 const vk::RenderPassDesc &renderPassDesc,
72 const std::vector<VkClearValue> &clearValues);
73
74 // Dependency commands order node execution in the command graph.
75 // Once a node has commands that must happen after it, recording is stopped and the node is
76 // frozen forever.
77 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
78 CommandGraphNode *afterNode);
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040079 static void SetHappensBeforeDependencies(CommandGraphNode **beforeNodes,
80 size_t beforeNodesCount,
Jamie Madill3d61ac22018-08-28 16:58:55 -040081 CommandGraphNode *afterNode);
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040082 static void SetHappensBeforeDependencies(CommandGraphNode *beforeNode,
83 CommandGraphNode **afterNodes,
84 size_t afterNodesCount);
Jamie Madill3d61ac22018-08-28 16:58:55 -040085 bool hasParents() const;
86 bool hasChildren() const { return mHasChildren; }
87
88 // Commands for traversing the node on a flush operation.
89 VisitedState visitedState() const;
90 void visitParents(std::vector<CommandGraphNode *> *stack);
91 angle::Result visitAndExecute(Context *context,
92 Serial serial,
93 RenderPassCache *renderPassCache,
94 CommandBuffer *primaryCommandBuffer);
95
Jamie Madill0da73fe2018-10-02 09:31:39 -040096 // Only used in the command graph diagnostics.
97 const std::vector<CommandGraphNode *> &getParentsForDiagnostics() const;
98 void setDiagnosticInfo(CommandGraphResourceType resourceType, uintptr_t resourceID);
99
100 CommandGraphResourceType getResourceTypeForDiagnostics() const { return mResourceType; }
101 uintptr_t getResourceIDForDiagnostics() const { return mResourceID; }
102
Jamie Madill3d61ac22018-08-28 16:58:55 -0400103 const gl::Rectangle &getRenderPassRenderArea() const;
104
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400105 CommandGraphNodeFunction getFunction() const { return mFunction; }
106
107 void setQueryPool(const QueryPool *queryPool, uint32_t queryIndex);
108
Jamie Madill3d61ac22018-08-28 16:58:55 -0400109 private:
110 void setHasChildren();
111
112 // Used for testing only.
113 bool isChildOf(CommandGraphNode *parent);
114
115 // Only used if we need a RenderPass for these commands.
116 RenderPassDesc mRenderPassDesc;
117 Framebuffer mRenderPassFramebuffer;
118 gl::Rectangle mRenderPassRenderArea;
119 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
120
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400121 CommandGraphNodeFunction mFunction;
122
123 // Keep separate buffers for commands inside and outside a RenderPass.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400124 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
125 CommandBuffer mOutsideRenderPassCommands;
126 CommandBuffer mInsideRenderPassCommands;
127
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400128 // Special-function additional data:
129 VkQueryPool mQueryPool;
130 uint32_t mQueryIndex;
131
Jamie Madill3d61ac22018-08-28 16:58:55 -0400132 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
133 std::vector<CommandGraphNode *> mParents;
134
135 // If this is true, other commands exist that must be submitted after 'this' command.
136 bool mHasChildren;
137
138 // Used when traversing the dependency graph.
139 VisitedState mVisitedState;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400140
141 // Additional diagnostic information.
142 CommandGraphResourceType mResourceType;
143 uintptr_t mResourceID;
Jamie Madill3d61ac22018-08-28 16:58:55 -0400144};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500145
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400146// This is a helper class for back-end objects used in Vk command buffers. It records a serial
147// at command recording times indicating an order in the queue. We use Fences to detect when
148// commands finish, and then release any unreferenced and deleted resources based on the stored
149// queue serial in a special 'garbage' queue. Resources also track current read and write
150// dependencies. Only one command buffer node can be writing to the Resource at a time, but many
151// can be reading from it. Together the dependencies will form a command graph at submission time.
Jamie Madill0da73fe2018-10-02 09:31:39 -0400152class CommandGraphResource : angle::NonCopyable
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400153{
154 public:
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400155 virtual ~CommandGraphResource();
156
Jamie Madillc57ee252018-05-30 19:53:48 -0400157 // Returns true if the resource is in use by the renderer.
158 bool isResourceInUse(RendererVk *renderer) const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400159
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400160 // Returns true if the resource has unsubmitted work pending.
161 bool hasPendingWork(RendererVk *renderer) const;
162
Jamie Madilld014c9e2018-05-18 15:15:59 -0400163 // Sets up dependency relations. 'this' resource is the resource being written to.
164 void addWriteDependency(CommandGraphResource *writingResource);
165
166 // Sets up dependency relations. 'this' resource is the resource being read.
167 void addReadDependency(CommandGraphResource *readingResource);
168
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400169 // Allocates a write node via getNewWriteNode and returns a started command buffer.
170 // The started command buffer will render outside of a RenderPass.
Jamie Madille2d22702018-09-19 08:11:48 -0400171 // Will append to an existing command buffer/graph node if possible.
172 angle::Result recordCommands(Context *context, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400173
174 // Begins a command buffer on the current graph node for in-RenderPass rendering.
175 // Currently only called from FramebufferVk::getCommandBufferForDraw.
Jamie Madill21061022018-07-12 23:56:30 -0400176 angle::Result beginRenderPass(Context *context,
177 const Framebuffer &framebuffer,
178 const gl::Rectangle &renderArea,
179 const RenderPassDesc &renderPassDesc,
180 const std::vector<VkClearValue> &clearValues,
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400181 CommandBuffer **commandBufferOut);
182
183 void beginQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
184 void endQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
Jamie Madill316c6062018-05-29 10:49:45 -0400185
Jamie Madill5dca6512018-05-30 10:53:51 -0400186 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
187 // Returns the started command buffer in commandBufferOut.
188 bool appendToStartedRenderPass(RendererVk *renderer, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400189
190 // Accessor for RenderPass RenderArea.
191 const gl::Rectangle &getRenderPassRenderArea() const;
192
193 // Called when 'this' object changes, but we'd like to start a new command buffer later.
Jamie Madille2d22702018-09-19 08:11:48 -0400194 void finishCurrentCommands(RendererVk *renderer);
Jamie Madill316c6062018-05-29 10:49:45 -0400195
Shahbaz Youssefic4765aa2018-10-12 14:40:29 -0400196 // Get the current queue serial for this resource. Used to release resources, and for
197 // queries, to know if the queue they are submitted on has finished execution.
198 Serial getStoredQueueSerial() const;
199
Jamie Madill2d03ff42018-09-27 15:04:26 -0400200 protected:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400201 explicit CommandGraphResource(CommandGraphResourceType resourceType);
202
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400203 private:
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400204 void startNewCommands(RendererVk *renderer, CommandGraphNodeFunction function);
205
Jamie Madill316c6062018-05-29 10:49:45 -0400206 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
207
208 // Returns true if this node has a current writing node with no children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400209 bool hasChildlessWritingNode() const
210 {
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400211 // Note: currently, we don't have a resource that can issue both generic and special
212 // commands. We don't create read/write dependencies between mixed generic/special
213 // resources either. As such, we expect the function to always be generic here. If such a
214 // resource is added in the future, this can add a check for function == generic and fail if
215 // false.
216 ASSERT(mCurrentWritingNode == nullptr ||
217 mCurrentWritingNode->getFunction() == CommandGraphNodeFunction::Generic);
Jamie Madill3d61ac22018-08-28 16:58:55 -0400218 return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren());
219 }
Jamie Madill316c6062018-05-29 10:49:45 -0400220
Jamie Madill5dca6512018-05-30 10:53:51 -0400221 // Checks if we're in a RenderPass without children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400222 bool hasStartedRenderPass() const
223 {
224 return hasChildlessWritingNode() &&
225 mCurrentWritingNode->getInsideRenderPassCommands()->valid();
226 }
Jamie Madill5dca6512018-05-30 10:53:51 -0400227
228 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
229 // was not used in this set of command nodes.
230 void updateQueueSerial(Serial queueSerial);
231
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400232 Serial mStoredQueueSerial;
233 std::vector<CommandGraphNode *> mCurrentReadingNodes;
234 CommandGraphNode *mCurrentWritingNode;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400235
236 // Additional diagnostic information.
237 CommandGraphResourceType mResourceType;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400238};
239
Jamie Madill1f46bc12018-02-20 16:09:43 -0500240// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
241// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
242// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
243// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
244// when processing interleaved draw operations on independent Framebuffers.
245//
246// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
247// command submission. We also sometimes call this command re-ordering. A brief summary:
248//
249// During GL command processing, we record Vulkan commands into secondary command buffers, which
250// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
251// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
252// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
253// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400254// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500255// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
256// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400257//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500258// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
259// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
260// also submitting the whole command graph via submitCommands.
261class CommandGraph final : angle::NonCopyable
262{
263 public:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400264 explicit CommandGraph(bool enableGraphDiagnostics);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500265 ~CommandGraph();
266
267 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
268 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400269 // to set up dependency relations. If the node is a barrier, it will automatically add
270 // dependencies between the previous barrier, the new barrier and all nodes in between.
271 CommandGraphNode *allocateNode(bool isBarrier, CommandGraphNodeFunction function);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500272
Jamie Madill21061022018-07-12 23:56:30 -0400273 angle::Result submitCommands(Context *context,
274 Serial serial,
275 RenderPassCache *renderPassCache,
276 CommandPool *commandPool,
277 CommandBuffer *primaryCommandBufferOut);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500278 bool empty() const;
279
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400280 CommandGraphNode *getLastBarrierNode(size_t *indexOut);
281
Jamie Madill1f46bc12018-02-20 16:09:43 -0500282 private:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400283 void dumpGraphDotFile(std::ostream &out) const;
Jamie Madill1f46bc12018-02-20 16:09:43 -0500284
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400285 void setNewBarrier(CommandGraphNode *newBarrier);
286 void addDependenciesToNextBarrier(size_t begin, size_t end, CommandGraphNode *nextBarrier);
287
Jamie Madill0da73fe2018-10-02 09:31:39 -0400288 std::vector<CommandGraphNode *> mNodes;
289 bool mEnableGraphDiagnostics;
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400290
291 // A set of nodes (eventually) exist that act as barriers to guarantee submission order. For
292 // example, a glMemoryBarrier() calls would lead to such a barrier or beginning and ending a
293 // query. This is because the graph can reorder operations if it sees fit. Let's call a barrier
294 // node Bi, and the other nodes Ni. The edges between Ni don't interest us. Before a barrier is
295 // inserted, we have:
296 //
297 // N0 N1 ... Na
298 // \___\__/_/ (dependency egdes, which we don't care about so I'll stop drawing them.
299 // \/
300 //
301 // When the first barrier is inserted, we will have:
302 //
303 // ______
304 // / ____\
305 // / / \
306 // / / /\
307 // N0 N1 ... Na B0
308 //
309 // This makes sure all N0..Na are called before B0. From then on, B0 will be the current
310 // "barrier point" which extends an edge to every next node:
311 //
312 // ______
313 // / ____\
314 // / / \
315 // / / /\
316 // N0 N1 ... Na B0 Na+1 ... Nb
317 // \/ /
318 // \______/
319 //
320 //
321 // When the next barrier B1 is met, all nodes between B0 and B1 will add a depenency on B1 as
322 // well, and the "barrier point" is updated.
323 //
324 // ______
325 // / ____\ ______ ______
326 // / / \ / \ / \
327 // / / /\ / /\ / /\
328 // N0 N1 ... Na B0 Na+1 ... Nb B1 Nb+1 ... Nc B2 ...
329 // \/ / / \/ / /
330 // \______/ / \______/ /
331 // \_______/ \_______/
332 //
333 //
334 // When barrier Bi is introduced, all nodes added since Bi-1 need to add a dependency to Bi
335 // (including Bi-1). We therefore keep track of the node index of the last barrier that was
336 // issued.
337 static constexpr size_t kInvalidNodeIndex = std::numeric_limits<std::size_t>::max();
338 size_t mLastBarrierIndex;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400339};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500340} // namespace vk
341} // namespace rx
342
343#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_