blob: 235d9d5a92854783a3ecb34e64ca380f6f23baba [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,
110 const std::vector<VkClearValue> &clearValues);
111
112 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
113 // Note: RenderTargets must be added in order, with the depth/stencil being added last.
114 void appendColorRenderTarget(Serial serial, RenderTargetVk *colorRenderTarget);
115 void appendDepthStencilRenderTarget(Serial serial, RenderTargetVk *depthStencilRenderTarget);
116
117 // Dependency commands order node execution in the command graph.
118 // Once a node has commands that must happen after it, recording is stopped and the node is
119 // frozen forever.
120 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
121 CommandGraphNode *afterNode);
122 static void SetHappensBeforeDependencies(const std::vector<CommandGraphNode *> &beforeNodes,
123 CommandGraphNode *afterNode);
124 bool hasParents() const;
125 bool hasChildren() const;
126
127 // Commands for traversing the node on a flush operation.
128 VisitedState visitedState() const;
129 void visitParents(std::vector<CommandGraphNode *> *stack);
130 Error visitAndExecute(VkDevice device,
131 Serial serial,
132 RenderPassCache *renderPassCache,
133 CommandBuffer *primaryCommandBuffer);
134
Luc Ferron14f48172018-04-11 08:43:28 -0400135 const gl::Rectangle &getRenderPassRenderArea() const;
136
Jamie Madill1f46bc12018-02-20 16:09:43 -0500137 private:
138 void setHasChildren();
139
140 // Used for testing only.
141 bool isChildOf(CommandGraphNode *parent);
142
143 // Only used if we need a RenderPass for these commands.
144 RenderPassDesc mRenderPassDesc;
145 Framebuffer mRenderPassFramebuffer;
146 gl::Rectangle mRenderPassRenderArea;
147 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
148
149 // Keep a separate buffers for commands inside and outside a RenderPass.
150 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
151 CommandBuffer mOutsideRenderPassCommands;
152 CommandBuffer mInsideRenderPassCommands;
153
154 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
155 std::vector<CommandGraphNode *> mParents;
156
157 // If this is true, other commands exist that must be submitted after 'this' command.
158 bool mHasChildren;
159
160 // Used when traversing the dependency graph.
161 VisitedState mVisitedState;
162};
163
164// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
165// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
166// also submitting the whole command graph via submitCommands.
167class CommandGraph final : angle::NonCopyable
168{
169 public:
170 CommandGraph();
171 ~CommandGraph();
172
173 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
174 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
175 // to set up dependency relations.
176 CommandGraphNode *allocateNode();
177
178 Error submitCommands(VkDevice device,
179 Serial serial,
180 RenderPassCache *renderPassCache,
181 CommandPool *commandPool,
182 CommandBuffer *primaryCommandBufferOut);
183 bool empty() const;
184
185 private:
186 std::vector<CommandGraphNode *> mNodes;
187};
188
189} // namespace vk
190} // namespace rx
191
192#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_