blob: 49769f6efe69ba9de3d3d1607cfc69e24617ce65 [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
Jamie Madill2d03ff42018-09-27 15:04:26 -0400196 protected:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400197 explicit CommandGraphResource(CommandGraphResourceType resourceType);
198
Jamie Madillc57ee252018-05-30 19:53:48 -0400199 // Get the current queue serial for this resource. Only used to release resources.
200 Serial getStoredQueueSerial() const;
201
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400202 private:
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400203 void startNewCommands(RendererVk *renderer, CommandGraphNodeFunction function);
204
Jamie Madill316c6062018-05-29 10:49:45 -0400205 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
206
207 // Returns true if this node has a current writing node with no children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400208 bool hasChildlessWritingNode() const
209 {
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400210 // Note: currently, we don't have a resource that can issue both generic and special
211 // commands. We don't create read/write dependencies between mixed generic/special
212 // resources either. As such, we expect the function to always be generic here. If such a
213 // resource is added in the future, this can add a check for function == generic and fail if
214 // false.
215 ASSERT(mCurrentWritingNode == nullptr ||
216 mCurrentWritingNode->getFunction() == CommandGraphNodeFunction::Generic);
Jamie Madill3d61ac22018-08-28 16:58:55 -0400217 return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren());
218 }
Jamie Madill316c6062018-05-29 10:49:45 -0400219
Jamie Madill5dca6512018-05-30 10:53:51 -0400220 // Checks if we're in a RenderPass without children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400221 bool hasStartedRenderPass() const
222 {
223 return hasChildlessWritingNode() &&
224 mCurrentWritingNode->getInsideRenderPassCommands()->valid();
225 }
Jamie Madill5dca6512018-05-30 10:53:51 -0400226
227 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
228 // was not used in this set of command nodes.
229 void updateQueueSerial(Serial queueSerial);
230
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400231 Serial mStoredQueueSerial;
232 std::vector<CommandGraphNode *> mCurrentReadingNodes;
233 CommandGraphNode *mCurrentWritingNode;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400234
235 // Additional diagnostic information.
236 CommandGraphResourceType mResourceType;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400237};
238
Jamie Madill1f46bc12018-02-20 16:09:43 -0500239// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
240// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
241// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
242// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
243// when processing interleaved draw operations on independent Framebuffers.
244//
245// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
246// command submission. We also sometimes call this command re-ordering. A brief summary:
247//
248// During GL command processing, we record Vulkan commands into secondary command buffers, which
249// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
250// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
251// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
252// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400253// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500254// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
255// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400256//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500257// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
258// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
259// also submitting the whole command graph via submitCommands.
260class CommandGraph final : angle::NonCopyable
261{
262 public:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400263 explicit CommandGraph(bool enableGraphDiagnostics);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500264 ~CommandGraph();
265
266 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
267 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400268 // to set up dependency relations. If the node is a barrier, it will automatically add
269 // dependencies between the previous barrier, the new barrier and all nodes in between.
270 CommandGraphNode *allocateNode(bool isBarrier, CommandGraphNodeFunction function);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500271
Jamie Madill21061022018-07-12 23:56:30 -0400272 angle::Result submitCommands(Context *context,
273 Serial serial,
274 RenderPassCache *renderPassCache,
275 CommandPool *commandPool,
276 CommandBuffer *primaryCommandBufferOut);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500277 bool empty() const;
278
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400279 CommandGraphNode *getLastBarrierNode(size_t *indexOut);
280
Jamie Madill1f46bc12018-02-20 16:09:43 -0500281 private:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400282 void dumpGraphDotFile(std::ostream &out) const;
Jamie Madill1f46bc12018-02-20 16:09:43 -0500283
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400284 void setNewBarrier(CommandGraphNode *newBarrier);
285 void addDependenciesToNextBarrier(size_t begin, size_t end, CommandGraphNode *nextBarrier);
286
Jamie Madill0da73fe2018-10-02 09:31:39 -0400287 std::vector<CommandGraphNode *> mNodes;
288 bool mEnableGraphDiagnostics;
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400289
290 // A set of nodes (eventually) exist that act as barriers to guarantee submission order. For
291 // example, a glMemoryBarrier() calls would lead to such a barrier or beginning and ending a
292 // query. This is because the graph can reorder operations if it sees fit. Let's call a barrier
293 // node Bi, and the other nodes Ni. The edges between Ni don't interest us. Before a barrier is
294 // inserted, we have:
295 //
296 // N0 N1 ... Na
297 // \___\__/_/ (dependency egdes, which we don't care about so I'll stop drawing them.
298 // \/
299 //
300 // When the first barrier is inserted, we will have:
301 //
302 // ______
303 // / ____\
304 // / / \
305 // / / /\
306 // N0 N1 ... Na B0
307 //
308 // This makes sure all N0..Na are called before B0. From then on, B0 will be the current
309 // "barrier point" which extends an edge to every next node:
310 //
311 // ______
312 // / ____\
313 // / / \
314 // / / /\
315 // N0 N1 ... Na B0 Na+1 ... Nb
316 // \/ /
317 // \______/
318 //
319 //
320 // When the next barrier B1 is met, all nodes between B0 and B1 will add a depenency on B1 as
321 // well, and the "barrier point" is updated.
322 //
323 // ______
324 // / ____\ ______ ______
325 // / / \ / \ / \
326 // / / /\ / /\ / /\
327 // N0 N1 ... Na B0 Na+1 ... Nb B1 Nb+1 ... Nc B2 ...
328 // \/ / / \/ / /
329 // \______/ / \______/ /
330 // \_______/ \_______/
331 //
332 //
333 // When barrier Bi is introduced, all nodes added since Bi-1 need to add a dependency to Bi
334 // (including Bi-1). We therefore keep track of the node index of the last barrier that was
335 // issued.
336 static constexpr size_t kInvalidNodeIndex = std::numeric_limits<std::size_t>::max();
337 size_t mLastBarrierIndex;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400338};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500339} // namespace vk
340} // namespace rx
341
342#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_