blob: b44269d943940aae491578d81d529e9d6250c50c [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{
20
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040021// This is a helper class for back-end objects used in Vk command buffers. It records a serial
22// at command recording times indicating an order in the queue. We use Fences to detect when
23// commands finish, and then release any unreferenced and deleted resources based on the stored
24// queue serial in a special 'garbage' queue. Resources also track current read and write
25// dependencies. Only one command buffer node can be writing to the Resource at a time, but many
26// can be reading from it. Together the dependencies will form a command graph at submission time.
27class CommandGraphResource
28{
29 public:
30 CommandGraphResource();
31 virtual ~CommandGraphResource();
32
33 void updateQueueSerial(Serial queueSerial);
34 Serial getQueueSerial() const;
35
Jamie Madill9cceac42018-03-31 14:19:16 -040036 // Returns true if this node has a current writing node with no children.
37 bool hasChildlessWritingNode() const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040038
Jamie Madill9cceac42018-03-31 14:19:16 -040039 // Returns the active write node.
40 CommandGraphNode *getCurrentWritingNode();
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040041
42 // Allocates a new write node and calls onWriteResource internally.
43 CommandGraphNode *getNewWritingNode(RendererVk *renderer);
44
45 // Allocates a write node via getNewWriteNode and returns a started command buffer.
46 // The started command buffer will render outside of a RenderPass.
47 Error beginWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
48
49 // Sets up dependency relations. 'writingNode' will modify 'this' ResourceVk.
50 void onWriteResource(CommandGraphNode *writingNode, Serial serial);
51
52 // Sets up dependency relations. 'readingNode' will read from 'this' ResourceVk.
53 void onReadResource(CommandGraphNode *readingNode, Serial serial);
54
Jamie Madill9cceac42018-03-31 14:19:16 -040055 // Returns false if the resource is not in use, and clears any current read/write nodes.
56 bool checkResourceInUseAndRefreshDeps(RendererVk *renderer);
57
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040058 private:
59 Serial mStoredQueueSerial;
60 std::vector<CommandGraphNode *> mCurrentReadingNodes;
61 CommandGraphNode *mCurrentWritingNode;
62};
63
Jamie Madill1f46bc12018-02-20 16:09:43 -050064enum class VisitedState
65{
66 Unvisited,
67 Ready,
68 Visited,
69};
70
71// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
72// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
73// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
74// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
75// when processing interleaved draw operations on independent Framebuffers.
76//
77// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
78// command submission. We also sometimes call this command re-ordering. A brief summary:
79//
80// During GL command processing, we record Vulkan commands into secondary command buffers, which
81// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
82// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
83// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
84// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040085// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -050086// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
87// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
88
89class CommandGraphNode final : angle::NonCopyable
90{
91 public:
92 CommandGraphNode();
93 ~CommandGraphNode();
94
95 // Immutable queries for when we're walking the commands tree.
96 CommandBuffer *getOutsideRenderPassCommands();
97 CommandBuffer *getInsideRenderPassCommands();
98
99 // For outside the render pass (copies, transitions, etc).
100 Error beginOutsideRenderPassRecording(VkDevice device,
101 const CommandPool &commandPool,
102 CommandBuffer **commandsOut);
103
104 // For rendering commands (draws).
105 Error beginInsideRenderPassRecording(RendererVk *renderer, CommandBuffer **commandsOut);
106
107 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
108 void storeRenderPassInfo(const Framebuffer &framebuffer,
109 const gl::Rectangle renderArea,
Jamie Madillbcf467f2018-05-23 09:46:00 -0400110 const vk::RenderPassDesc &renderPassDesc,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500111 const std::vector<VkClearValue> &clearValues);
112
Jamie Madill1f46bc12018-02-20 16:09:43 -0500113 // Dependency commands order node execution in the command graph.
114 // Once a node has commands that must happen after it, recording is stopped and the node is
115 // frozen forever.
116 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
117 CommandGraphNode *afterNode);
118 static void SetHappensBeforeDependencies(const std::vector<CommandGraphNode *> &beforeNodes,
119 CommandGraphNode *afterNode);
120 bool hasParents() const;
121 bool hasChildren() const;
122
123 // Commands for traversing the node on a flush operation.
124 VisitedState visitedState() const;
125 void visitParents(std::vector<CommandGraphNode *> *stack);
126 Error visitAndExecute(VkDevice device,
127 Serial serial,
128 RenderPassCache *renderPassCache,
129 CommandBuffer *primaryCommandBuffer);
130
Luc Ferron14f48172018-04-11 08:43:28 -0400131 const gl::Rectangle &getRenderPassRenderArea() const;
132
Jamie Madill1f46bc12018-02-20 16:09:43 -0500133 private:
134 void setHasChildren();
135
136 // Used for testing only.
137 bool isChildOf(CommandGraphNode *parent);
138
139 // Only used if we need a RenderPass for these commands.
140 RenderPassDesc mRenderPassDesc;
141 Framebuffer mRenderPassFramebuffer;
142 gl::Rectangle mRenderPassRenderArea;
143 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
144
145 // Keep a separate buffers for commands inside and outside a RenderPass.
146 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
147 CommandBuffer mOutsideRenderPassCommands;
148 CommandBuffer mInsideRenderPassCommands;
149
150 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
151 std::vector<CommandGraphNode *> mParents;
152
153 // If this is true, other commands exist that must be submitted after 'this' command.
154 bool mHasChildren;
155
156 // Used when traversing the dependency graph.
157 VisitedState mVisitedState;
158};
159
160// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
161// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
162// also submitting the whole command graph via submitCommands.
163class CommandGraph final : angle::NonCopyable
164{
165 public:
166 CommandGraph();
167 ~CommandGraph();
168
169 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
170 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
171 // to set up dependency relations.
172 CommandGraphNode *allocateNode();
173
174 Error submitCommands(VkDevice device,
175 Serial serial,
176 RenderPassCache *renderPassCache,
177 CommandPool *commandPool,
178 CommandBuffer *primaryCommandBufferOut);
179 bool empty() const;
180
181 private:
182 std::vector<CommandGraphNode *> mNodes;
183};
184
185} // namespace vk
186} // namespace rx
187
188#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_