blob: 40d0a7e0bf26b8a8e8df64f384fa21c69f3eb206 [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,
Jamie Madillc759b8b2019-01-03 15:16:50 -050079 CommandGraphNode *afterNode)
80 {
81 ASSERT(beforeNode != afterNode && !beforeNode->isChildOf(afterNode));
82 afterNode->mParents.emplace_back(beforeNode);
83 beforeNode->setHasChildren();
84 }
85
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040086 static void SetHappensBeforeDependencies(CommandGraphNode **beforeNodes,
87 size_t beforeNodesCount,
Jamie Madill3d61ac22018-08-28 16:58:55 -040088 CommandGraphNode *afterNode);
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -040089 static void SetHappensBeforeDependencies(CommandGraphNode *beforeNode,
90 CommandGraphNode **afterNodes,
91 size_t afterNodesCount);
Jamie Madill3d61ac22018-08-28 16:58:55 -040092 bool hasParents() const;
93 bool hasChildren() const { return mHasChildren; }
94
95 // Commands for traversing the node on a flush operation.
96 VisitedState visitedState() const;
97 void visitParents(std::vector<CommandGraphNode *> *stack);
98 angle::Result visitAndExecute(Context *context,
99 Serial serial,
100 RenderPassCache *renderPassCache,
101 CommandBuffer *primaryCommandBuffer);
102
Jamie Madill0da73fe2018-10-02 09:31:39 -0400103 // Only used in the command graph diagnostics.
104 const std::vector<CommandGraphNode *> &getParentsForDiagnostics() const;
105 void setDiagnosticInfo(CommandGraphResourceType resourceType, uintptr_t resourceID);
106
107 CommandGraphResourceType getResourceTypeForDiagnostics() const { return mResourceType; }
108 uintptr_t getResourceIDForDiagnostics() const { return mResourceID; }
109
Jamie Madill3d61ac22018-08-28 16:58:55 -0400110 const gl::Rectangle &getRenderPassRenderArea() const;
111
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400112 CommandGraphNodeFunction getFunction() const { return mFunction; }
113
114 void setQueryPool(const QueryPool *queryPool, uint32_t queryIndex);
115
Jamie Madillc759b8b2019-01-03 15:16:50 -0500116 ANGLE_INLINE void addGlobalMemoryBarrier(VkFlags srcAccess, VkFlags dstAccess)
117 {
118 mGlobalMemoryBarrierSrcAccess |= srcAccess;
119 mGlobalMemoryBarrierDstAccess |= dstAccess;
120 }
Jamie Madill03d1a5e2018-11-12 11:34:24 -0500121
Jamie Madill3d61ac22018-08-28 16:58:55 -0400122 private:
Jamie Madillc759b8b2019-01-03 15:16:50 -0500123 void setHasChildren() { mHasChildren = true; }
Jamie Madill3d61ac22018-08-28 16:58:55 -0400124
125 // Used for testing only.
126 bool isChildOf(CommandGraphNode *parent);
127
128 // Only used if we need a RenderPass for these commands.
129 RenderPassDesc mRenderPassDesc;
130 Framebuffer mRenderPassFramebuffer;
131 gl::Rectangle mRenderPassRenderArea;
132 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
133
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400134 CommandGraphNodeFunction mFunction;
135
136 // Keep separate buffers for commands inside and outside a RenderPass.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400137 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
138 CommandBuffer mOutsideRenderPassCommands;
139 CommandBuffer mInsideRenderPassCommands;
140
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400141 // Special-function additional data:
142 VkQueryPool mQueryPool;
143 uint32_t mQueryIndex;
144
Jamie Madill3d61ac22018-08-28 16:58:55 -0400145 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
146 std::vector<CommandGraphNode *> mParents;
147
148 // If this is true, other commands exist that must be submitted after 'this' command.
149 bool mHasChildren;
150
151 // Used when traversing the dependency graph.
152 VisitedState mVisitedState;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400153
154 // Additional diagnostic information.
155 CommandGraphResourceType mResourceType;
156 uintptr_t mResourceID;
Jamie Madill03d1a5e2018-11-12 11:34:24 -0500157
158 // For global memory barriers.
159 VkFlags mGlobalMemoryBarrierSrcAccess;
160 VkFlags mGlobalMemoryBarrierDstAccess;
Jamie Madill3d61ac22018-08-28 16:58:55 -0400161};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500162
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400163// This is a helper class for back-end objects used in Vk command buffers. It records a serial
164// at command recording times indicating an order in the queue. We use Fences to detect when
165// commands finish, and then release any unreferenced and deleted resources based on the stored
166// queue serial in a special 'garbage' queue. Resources also track current read and write
167// dependencies. Only one command buffer node can be writing to the Resource at a time, but many
168// can be reading from it. Together the dependencies will form a command graph at submission time.
Jamie Madill0da73fe2018-10-02 09:31:39 -0400169class CommandGraphResource : angle::NonCopyable
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400170{
171 public:
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400172 virtual ~CommandGraphResource();
173
Jamie Madillc57ee252018-05-30 19:53:48 -0400174 // Returns true if the resource is in use by the renderer.
175 bool isResourceInUse(RendererVk *renderer) const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400176
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400177 // Returns true if the resource has unsubmitted work pending.
178 bool hasPendingWork(RendererVk *renderer) const;
179
Jamie Madill193a2842018-10-30 17:28:41 -0400180 // Get the current queue serial for this resource. Used to release resources, and for
181 // queries, to know if the queue they are submitted on has finished execution.
Jamie Madillc759b8b2019-01-03 15:16:50 -0500182 Serial getStoredQueueSerial() const { return mStoredQueueSerial; }
Jamie Madill193a2842018-10-30 17:28:41 -0400183
184 protected:
185 explicit CommandGraphResource(CommandGraphResourceType resourceType);
186
187 Serial mStoredQueueSerial;
188
189 // Additional diagnostic information.
190 CommandGraphResourceType mResourceType;
191
192 // Current command graph writing node.
193 CommandGraphNode *mCurrentWritingNode;
194};
195
196// Subclass of graph resources that can record command buffers. Images/Buffers/Framebuffers.
197// Does not include Query graph resources.
198class RecordableGraphResource : public CommandGraphResource
199{
200 public:
201 ~RecordableGraphResource() override;
202
Jamie Madilld014c9e2018-05-18 15:15:59 -0400203 // Sets up dependency relations. 'this' resource is the resource being written to.
Jamie Madill193a2842018-10-30 17:28:41 -0400204 void addWriteDependency(RecordableGraphResource *writingResource);
Jamie Madilld014c9e2018-05-18 15:15:59 -0400205
206 // Sets up dependency relations. 'this' resource is the resource being read.
Jamie Madill193a2842018-10-30 17:28:41 -0400207 void addReadDependency(RecordableGraphResource *readingResource);
Jamie Madilld014c9e2018-05-18 15:15:59 -0400208
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500209 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
210 // was not used in this set of command nodes.
Jamie Madillc759b8b2019-01-03 15:16:50 -0500211 ANGLE_INLINE void updateQueueSerial(Serial queueSerial)
212 {
213 ASSERT(queueSerial >= mStoredQueueSerial);
214
215 if (queueSerial > mStoredQueueSerial)
216 {
217 mCurrentWritingNode = nullptr;
218 mCurrentReadingNodes.clear();
219 mStoredQueueSerial = queueSerial;
220 }
221 }
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500222
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400223 // Allocates a write node via getNewWriteNode and returns a started command buffer.
224 // The started command buffer will render outside of a RenderPass.
Jamie Madille2d22702018-09-19 08:11:48 -0400225 // Will append to an existing command buffer/graph node if possible.
226 angle::Result recordCommands(Context *context, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400227
228 // Begins a command buffer on the current graph node for in-RenderPass rendering.
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100229 // Called from FramebufferVk::startNewRenderPass and UtilsVk functions.
Jamie Madill21061022018-07-12 23:56:30 -0400230 angle::Result beginRenderPass(Context *context,
231 const Framebuffer &framebuffer,
232 const gl::Rectangle &renderArea,
233 const RenderPassDesc &renderPassDesc,
234 const std::vector<VkClearValue> &clearValues,
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400235 CommandBuffer **commandBufferOut);
236
Jamie Madill5dca6512018-05-30 10:53:51 -0400237 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
238 // Returns the started command buffer in commandBufferOut.
Jamie Madillc759b8b2019-01-03 15:16:50 -0500239 ANGLE_INLINE bool appendToStartedRenderPass(Serial currentQueueSerial,
240 CommandBuffer **commandBufferOut)
241 {
242 updateQueueSerial(currentQueueSerial);
243 if (hasStartedRenderPass())
244 {
245 *commandBufferOut = mCurrentWritingNode->getInsideRenderPassCommands();
246 return true;
247 }
248 else
249 {
250 return false;
251 }
252 }
Jamie Madill316c6062018-05-29 10:49:45 -0400253
254 // Accessor for RenderPass RenderArea.
255 const gl::Rectangle &getRenderPassRenderArea() const;
256
257 // Called when 'this' object changes, but we'd like to start a new command buffer later.
Jamie Madille2d22702018-09-19 08:11:48 -0400258 void finishCurrentCommands(RendererVk *renderer);
Jamie Madill316c6062018-05-29 10:49:45 -0400259
Jamie Madill03d1a5e2018-11-12 11:34:24 -0500260 // Store a deferred memory barrier. Will be recorded into a primary command buffer at submit.
261 void addGlobalMemoryBarrier(VkFlags srcAccess, VkFlags dstAccess)
262 {
263 ASSERT(mCurrentWritingNode);
264 mCurrentWritingNode->addGlobalMemoryBarrier(srcAccess, dstAccess);
265 }
266
Jamie Madill2d03ff42018-09-27 15:04:26 -0400267 protected:
Jamie Madill193a2842018-10-30 17:28:41 -0400268 explicit RecordableGraphResource(CommandGraphResourceType resourceType);
Jamie Madill0da73fe2018-10-02 09:31:39 -0400269
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400270 private:
Jamie Madill316c6062018-05-29 10:49:45 -0400271 // Returns true if this node has a current writing node with no children.
Jamie Madillc759b8b2019-01-03 15:16:50 -0500272 ANGLE_INLINE bool hasChildlessWritingNode() const
Jamie Madill3d61ac22018-08-28 16:58:55 -0400273 {
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400274 // Note: currently, we don't have a resource that can issue both generic and special
275 // commands. We don't create read/write dependencies between mixed generic/special
276 // resources either. As such, we expect the function to always be generic here. If such a
277 // resource is added in the future, this can add a check for function == generic and fail if
278 // false.
279 ASSERT(mCurrentWritingNode == nullptr ||
280 mCurrentWritingNode->getFunction() == CommandGraphNodeFunction::Generic);
Jamie Madill3d61ac22018-08-28 16:58:55 -0400281 return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren());
282 }
Jamie Madill316c6062018-05-29 10:49:45 -0400283
Jamie Madill5dca6512018-05-30 10:53:51 -0400284 // Checks if we're in a RenderPass without children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400285 bool hasStartedRenderPass() const
286 {
287 return hasChildlessWritingNode() &&
288 mCurrentWritingNode->getInsideRenderPassCommands()->valid();
289 }
Jamie Madill5dca6512018-05-30 10:53:51 -0400290
Jamie Madill193a2842018-10-30 17:28:41 -0400291 void startNewCommands(RendererVk *renderer);
Jamie Madill0da73fe2018-10-02 09:31:39 -0400292
Jamie Madill193a2842018-10-30 17:28:41 -0400293 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
294
295 std::vector<CommandGraphNode *> mCurrentReadingNodes;
296};
297
298// Specialized command graph node for queries. Not for use with any exposed command buffers.
299class QueryGraphResource : public CommandGraphResource
300{
301 public:
302 ~QueryGraphResource() override;
303
304 void beginQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
305 void endQuery(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
306 void writeTimestamp(Context *context, const QueryPool *queryPool, uint32_t queryIndex);
307
308 protected:
309 QueryGraphResource();
310
311 private:
312 void startNewCommands(RendererVk *renderer, CommandGraphNodeFunction function);
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400313};
314
Jamie Madill1f46bc12018-02-20 16:09:43 -0500315// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
316// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
317// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
318// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
319// when processing interleaved draw operations on independent Framebuffers.
320//
321// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
322// command submission. We also sometimes call this command re-ordering. A brief summary:
323//
324// During GL command processing, we record Vulkan commands into secondary command buffers, which
325// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
326// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
327// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
328// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400329// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500330// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
331// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400332//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500333// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
334// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
335// also submitting the whole command graph via submitCommands.
336class CommandGraph final : angle::NonCopyable
337{
338 public:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400339 explicit CommandGraph(bool enableGraphDiagnostics);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500340 ~CommandGraph();
341
342 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
343 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400344 // to set up dependency relations. If the node is a barrier, it will automatically add
345 // dependencies between the previous barrier, the new barrier and all nodes in between.
Jamie Madill193a2842018-10-30 17:28:41 -0400346 CommandGraphNode *allocateNode(CommandGraphNodeFunction function);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500347
Jamie Madill21061022018-07-12 23:56:30 -0400348 angle::Result submitCommands(Context *context,
349 Serial serial,
350 RenderPassCache *renderPassCache,
351 CommandPool *commandPool,
352 CommandBuffer *primaryCommandBufferOut);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500353 bool empty() const;
Yuly Novikovb56ddbb2018-11-02 16:53:18 -0400354 void clear();
Jamie Madill1f46bc12018-02-20 16:09:43 -0500355
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400356 CommandGraphNode *getLastBarrierNode(size_t *indexOut);
357
Jamie Madill193a2842018-10-30 17:28:41 -0400358 void setNewBarrier(CommandGraphNode *newBarrier);
359
Jamie Madill1f46bc12018-02-20 16:09:43 -0500360 private:
Jamie Madill0da73fe2018-10-02 09:31:39 -0400361 void dumpGraphDotFile(std::ostream &out) const;
Jamie Madill1f46bc12018-02-20 16:09:43 -0500362
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400363 void addDependenciesToNextBarrier(size_t begin, size_t end, CommandGraphNode *nextBarrier);
364
Jamie Madill0da73fe2018-10-02 09:31:39 -0400365 std::vector<CommandGraphNode *> mNodes;
366 bool mEnableGraphDiagnostics;
Shahbaz Youssefi563fbaa2018-10-02 11:22:01 -0400367
368 // A set of nodes (eventually) exist that act as barriers to guarantee submission order. For
369 // example, a glMemoryBarrier() calls would lead to such a barrier or beginning and ending a
370 // query. This is because the graph can reorder operations if it sees fit. Let's call a barrier
371 // node Bi, and the other nodes Ni. The edges between Ni don't interest us. Before a barrier is
372 // inserted, we have:
373 //
374 // N0 N1 ... Na
375 // \___\__/_/ (dependency egdes, which we don't care about so I'll stop drawing them.
376 // \/
377 //
378 // When the first barrier is inserted, we will have:
379 //
380 // ______
381 // / ____\
382 // / / \
383 // / / /\
384 // N0 N1 ... Na B0
385 //
386 // This makes sure all N0..Na are called before B0. From then on, B0 will be the current
387 // "barrier point" which extends an edge to every next node:
388 //
389 // ______
390 // / ____\
391 // / / \
392 // / / /\
393 // N0 N1 ... Na B0 Na+1 ... Nb
394 // \/ /
395 // \______/
396 //
397 //
398 // When the next barrier B1 is met, all nodes between B0 and B1 will add a depenency on B1 as
399 // well, and the "barrier point" is updated.
400 //
401 // ______
402 // / ____\ ______ ______
403 // / / \ / \ / \
404 // / / /\ / /\ / /\
405 // N0 N1 ... Na B0 Na+1 ... Nb B1 Nb+1 ... Nc B2 ...
406 // \/ / / \/ / /
407 // \______/ / \______/ /
408 // \_______/ \_______/
409 //
410 //
411 // When barrier Bi is introduced, all nodes added since Bi-1 need to add a dependency to Bi
412 // (including Bi-1). We therefore keep track of the node index of the last barrier that was
413 // issued.
414 static constexpr size_t kInvalidNodeIndex = std::numeric_limits<std::size_t>::max();
415 size_t mLastBarrierIndex;
Jamie Madill0da73fe2018-10-02 09:31:39 -0400416};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500417} // namespace vk
418} // namespace rx
419
420#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_