blob: eb20678f7a25a7c0c02860e6250672ff74139133 [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,
Shahbaz Youssefic2b576d2018-10-12 14:45:34 -040042 WriteTimestamp,
Jamie Madill0da73fe2018-10-02 09:31:39 -040043};
44
Jamie Madill3d61ac22018-08-28 16:58:55 -040045// Only used internally in the command graph. Kept in the header for better inlining performance.
46class CommandGraphNode final : angle::NonCopyable
47{
48 public:
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040049 CommandGraphNode(CommandGraphNodeFunction function);
Jamie Madill3d61ac22018-08-28 16:58:55 -040050 ~CommandGraphNode();
51
52 // Immutable queries for when we're walking the commands tree.
53 CommandBuffer *getOutsideRenderPassCommands();
54
55 CommandBuffer *getInsideRenderPassCommands()
56 {
57 ASSERT(!mHasChildren);
58 return &mInsideRenderPassCommands;
59 }
60
61 // For outside the render pass (copies, transitions, etc).
62 angle::Result beginOutsideRenderPassRecording(Context *context,
63 const CommandPool &commandPool,
64 CommandBuffer **commandsOut);
65
66 // For rendering commands (draws).
67 angle::Result beginInsideRenderPassRecording(Context *context, CommandBuffer **commandsOut);
68
69 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
70 void storeRenderPassInfo(const Framebuffer &framebuffer,
71 const gl::Rectangle renderArea,
72 const vk::RenderPassDesc &renderPassDesc,
73 const std::vector<VkClearValue> &clearValues);
74
75 // Dependency commands order node execution in the command graph.
76 // Once a node has commands that must happen after it, recording is stopped and the node is
77 // frozen forever.
78 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
79 CommandGraphNode *afterNode);
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040080 static void SetHappensBeforeDependencies(CommandGraphNode **beforeNodes,
81 size_t beforeNodesCount,
Jamie Madill3d61ac22018-08-28 16:58:55 -040082 CommandGraphNode *afterNode);
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040083 static void SetHappensBeforeDependencies(CommandGraphNode *beforeNode,
84 CommandGraphNode **afterNodes,
85 size_t afterNodesCount);
Jamie Madill3d61ac22018-08-28 16:58:55 -040086 bool hasParents() const;
87 bool hasChildren() const { return mHasChildren; }
88
89 // Commands for traversing the node on a flush operation.
90 VisitedState visitedState() const;
91 void visitParents(std::vector<CommandGraphNode *> *stack);
92 angle::Result visitAndExecute(Context *context,
93 Serial serial,
94 RenderPassCache *renderPassCache,
95 CommandBuffer *primaryCommandBuffer);
96
Jamie Madill0da73fe2018-10-02 09:31:39 -040097 // Only used in the command graph diagnostics.
98 const std::vector<CommandGraphNode *> &getParentsForDiagnostics() const;
99 void setDiagnosticInfo(CommandGraphResourceType resourceType, uintptr_t resourceID);
100
101 CommandGraphResourceType getResourceTypeForDiagnostics() const { return mResourceType; }
102 uintptr_t getResourceIDForDiagnostics() const { return mResourceID; }
103
Jamie Madill3d61ac22018-08-28 16:58:55 -0400104 const gl::Rectangle &getRenderPassRenderArea() const;
105
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400106 CommandGraphNodeFunction getFunction() const { return mFunction; }
107
108 void setQueryPool(const QueryPool *queryPool, uint32_t queryIndex);
109
Jamie Madill3d61ac22018-08-28 16:58:55 -0400110 private:
111 void setHasChildren();
112
113 // Used for testing only.
114 bool isChildOf(CommandGraphNode *parent);
115
116 // Only used if we need a RenderPass for these commands.
117 RenderPassDesc mRenderPassDesc;
118 Framebuffer mRenderPassFramebuffer;
119 gl::Rectangle mRenderPassRenderArea;
120 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
121
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400122 CommandGraphNodeFunction mFunction;
123
124 // Keep separate buffers for commands inside and outside a RenderPass.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400125 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
126 CommandBuffer mOutsideRenderPassCommands;
127 CommandBuffer mInsideRenderPassCommands;
128
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400129 // Special-function additional data:
130 VkQueryPool mQueryPool;
131 uint32_t mQueryIndex;
132
Jamie Madill3d61ac22018-08-28 16:58:55 -0400133 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
134 std::vector<CommandGraphNode *> mParents;
135
136 // If this is true, other commands exist that must be submitted after 'this' command.
137 bool mHasChildren;
138
139 // Used when traversing the dependency graph.
140 VisitedState mVisitedState;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400141
142 // Additional diagnostic information.
143 CommandGraphResourceType mResourceType;
144 uintptr_t mResourceID;
Jamie Madill3d61ac22018-08-28 16:58:55 -0400145};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500146
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400147// This is a helper class for back-end objects used in Vk command buffers. It records a serial
148// at command recording times indicating an order in the queue. We use Fences to detect when
149// commands finish, and then release any unreferenced and deleted resources based on the stored
150// queue serial in a special 'garbage' queue. Resources also track current read and write
151// dependencies. Only one command buffer node can be writing to the Resource at a time, but many
152// can be reading from it. Together the dependencies will form a command graph at submission time.
Jamie Madill0da73fe2018-10-02 09:31:39 -0400153class CommandGraphResource : angle::NonCopyable
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400154{
155 public:
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400156 virtual ~CommandGraphResource();
157
Jamie Madillc57ee252018-05-30 19:53:48 -0400158 // Returns true if the resource is in use by the renderer.
159 bool isResourceInUse(RendererVk *renderer) const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400160
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400161 // Returns true if the resource has unsubmitted work pending.
162 bool hasPendingWork(RendererVk *renderer) const;
163
Jamie Madill193a2842018-10-30 17:28:41 -0400164 // Get the current queue serial for this resource. Used to release resources, and for
165 // queries, to know if the queue they are submitted on has finished execution.
166 Serial getStoredQueueSerial() const;
167
168 protected:
169 explicit CommandGraphResource(CommandGraphResourceType resourceType);
170
171 Serial mStoredQueueSerial;
172
173 // Additional diagnostic information.
174 CommandGraphResourceType mResourceType;
175
176 // Current command graph writing node.
177 CommandGraphNode *mCurrentWritingNode;
178};
179
180// Subclass of graph resources that can record command buffers. Images/Buffers/Framebuffers.
181// Does not include Query graph resources.
182class RecordableGraphResource : public CommandGraphResource
183{
184 public:
185 ~RecordableGraphResource() override;
186
Jamie Madilld014c9e2018-05-18 15:15:59 -0400187 // Sets up dependency relations. 'this' resource is the resource being written to.
Jamie Madill193a2842018-10-30 17:28:41 -0400188 void addWriteDependency(RecordableGraphResource *writingResource);
Jamie Madilld014c9e2018-05-18 15:15:59 -0400189
190 // Sets up dependency relations. 'this' resource is the resource being read.
Jamie Madill193a2842018-10-30 17:28:41 -0400191 void addReadDependency(RecordableGraphResource *readingResource);
Jamie Madilld014c9e2018-05-18 15:15:59 -0400192
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400193 // Allocates a write node via getNewWriteNode and returns a started command buffer.
194 // The started command buffer will render outside of a RenderPass.
Jamie Madille2d22702018-09-19 08:11:48 -0400195 // Will append to an existing command buffer/graph node if possible.
196 angle::Result recordCommands(Context *context, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400197
198 // Begins a command buffer on the current graph node for in-RenderPass rendering.
199 // Currently only called from FramebufferVk::getCommandBufferForDraw.
Jamie Madill21061022018-07-12 23:56:30 -0400200 angle::Result beginRenderPass(Context *context,
201 const Framebuffer &framebuffer,
202 const gl::Rectangle &renderArea,
203 const RenderPassDesc &renderPassDesc,
204 const std::vector<VkClearValue> &clearValues,
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400205 CommandBuffer **commandBufferOut);
206
Jamie Madill5dca6512018-05-30 10:53:51 -0400207 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
208 // Returns the started command buffer in commandBufferOut.
209 bool appendToStartedRenderPass(RendererVk *renderer, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400210
211 // Accessor for RenderPass RenderArea.
212 const gl::Rectangle &getRenderPassRenderArea() const;
213
214 // Called when 'this' object changes, but we'd like to start a new command buffer later.
Jamie Madille2d22702018-09-19 08:11:48 -0400215 void finishCurrentCommands(RendererVk *renderer);
Jamie Madill316c6062018-05-29 10:49:45 -0400216
Jamie Madill2d03ff42018-09-27 15:04:26 -0400217 protected:
Jamie Madill193a2842018-10-30 17:28:41 -0400218 explicit RecordableGraphResource(CommandGraphResourceType resourceType);
Jamie Madill0da73fe2018-10-02 09:31:39 -0400219
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400220 private:
Jamie Madill316c6062018-05-29 10:49:45 -0400221
222 // Returns true if this node has a current writing node with no children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400223 bool hasChildlessWritingNode() const
224 {
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400225 // Note: currently, we don't have a resource that can issue both generic and special
226 // commands. We don't create read/write dependencies between mixed generic/special
227 // resources either. As such, we expect the function to always be generic here. If such a
228 // resource is added in the future, this can add a check for function == generic and fail if
229 // false.
230 ASSERT(mCurrentWritingNode == nullptr ||
231 mCurrentWritingNode->getFunction() == CommandGraphNodeFunction::Generic);
Jamie Madill3d61ac22018-08-28 16:58:55 -0400232 return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren());
233 }
Jamie Madill316c6062018-05-29 10:49:45 -0400234
Jamie Madill5dca6512018-05-30 10:53:51 -0400235 // Checks if we're in a RenderPass without children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400236 bool hasStartedRenderPass() const
237 {
238 return hasChildlessWritingNode() &&
239 mCurrentWritingNode->getInsideRenderPassCommands()->valid();
240 }
Jamie Madill5dca6512018-05-30 10:53:51 -0400241
242 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
243 // was not used in this set of command nodes.
244 void updateQueueSerial(Serial queueSerial);
245
Jamie Madill193a2842018-10-30 17:28:41 -0400246 void startNewCommands(RendererVk *renderer);
Jamie Madill0da73fe2018-10-02 09:31:39 -0400247
Jamie Madill193a2842018-10-30 17:28:41 -0400248 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
249
250 std::vector<CommandGraphNode *> mCurrentReadingNodes;
251};
252
253// Specialized command graph node for queries. Not for use with any exposed command buffers.
254class QueryGraphResource : public CommandGraphResource
255{
256 public:
257 ~QueryGraphResource() override;
258
259 void beginQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
260 void endQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
261 void writeTimestamp(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
262
263 protected:
264 QueryGraphResource();
265
266 private:
267 void startNewCommands(RendererVk *renderer, CommandGraphNodeFunction function);
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400268};
269
Jamie Madill1f46bc12018-02-20 16:09:43 -0500270// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
271// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
272// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
273// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
274// when processing interleaved draw operations on independent Framebuffers.
275//
276// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
277// command submission. We also sometimes call this command re-ordering. A brief summary:
278//
279// During GL command processing, we record Vulkan commands into secondary command buffers, which
280// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
281// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
282// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
283// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400284// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500285// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
286// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400287//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500288// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
289// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
290// also submitting the whole command graph via submitCommands.
291class CommandGraph final : angle::NonCopyable
292{
293 public:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400294 explicit CommandGraph(bool enableGraphDiagnostics);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500295 ~CommandGraph();
296
297 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
298 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400299 // to set up dependency relations. If the node is a barrier, it will automatically add
300 // dependencies between the previous barrier, the new barrier and all nodes in between.
Jamie Madill193a2842018-10-30 17:28:41 -0400301 CommandGraphNode *allocateNode(CommandGraphNodeFunction function);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500302
Jamie Madill21061022018-07-12 23:56:30 -0400303 angle::Result submitCommands(Context *context,
304 Serial serial,
305 RenderPassCache *renderPassCache,
306 CommandPool *commandPool,
307 CommandBuffer *primaryCommandBufferOut);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500308 bool empty() const;
309
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400310 CommandGraphNode *getLastBarrierNode(size_t *indexOut);
311
Jamie Madill193a2842018-10-30 17:28:41 -0400312 void setNewBarrier(CommandGraphNode *newBarrier);
313
Jamie Madill1f46bc12018-02-20 16:09:43 -0500314 private:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400315 void dumpGraphDotFile(std::ostream &out) const;
Jamie Madill1f46bc12018-02-20 16:09:43 -0500316
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400317 void addDependenciesToNextBarrier(size_t begin, size_t end, CommandGraphNode *nextBarrier);
318
Jamie Madill0da73fe2018-10-02 09:31:39 -0400319 std::vector<CommandGraphNode *> mNodes;
320 bool mEnableGraphDiagnostics;
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400321
322 // A set of nodes (eventually) exist that act as barriers to guarantee submission order. For
323 // example, a glMemoryBarrier() calls would lead to such a barrier or beginning and ending a
324 // query. This is because the graph can reorder operations if it sees fit. Let's call a barrier
325 // node Bi, and the other nodes Ni. The edges between Ni don't interest us. Before a barrier is
326 // inserted, we have:
327 //
328 // N0 N1 ... Na
329 // \___\__/_/ (dependency egdes, which we don't care about so I'll stop drawing them.
330 // \/
331 //
332 // When the first barrier is inserted, we will have:
333 //
334 // ______
335 // / ____\
336 // / / \
337 // / / /\
338 // N0 N1 ... Na B0
339 //
340 // This makes sure all N0..Na are called before B0. From then on, B0 will be the current
341 // "barrier point" which extends an edge to every next node:
342 //
343 // ______
344 // / ____\
345 // / / \
346 // / / /\
347 // N0 N1 ... Na B0 Na+1 ... Nb
348 // \/ /
349 // \______/
350 //
351 //
352 // When the next barrier B1 is met, all nodes between B0 and B1 will add a depenency on B1 as
353 // well, and the "barrier point" is updated.
354 //
355 // ______
356 // / ____\ ______ ______
357 // / / \ / \ / \
358 // / / /\ / /\ / /\
359 // N0 N1 ... Na B0 Na+1 ... Nb B1 Nb+1 ... Nc B2 ...
360 // \/ / / \/ / /
361 // \______/ / \______/ /
362 // \_______/ \_______/
363 //
364 //
365 // When barrier Bi is introduced, all nodes added since Bi-1 need to add a dependency to Bi
366 // (including Bi-1). We therefore keep track of the node index of the last barrier that was
367 // issued.
368 static constexpr size_t kInvalidNodeIndex = std::numeric_limits<std::size_t>::max();
369 size_t mLastBarrierIndex;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400370};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500371} // namespace vk
372} // namespace rx
373
374#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_