blob: 84469ceeba9816b5d3c4d9f3cf0e4db382630b15 [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 Madill6c7ab7f2018-03-31 14:19:15 -040036 // Allocates a write node via getNewWriteNode and returns a started command buffer.
37 // The started command buffer will render outside of a RenderPass.
38 Error beginWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
39
Jamie Madill316c6062018-05-29 10:49:45 -040040 // Check if we have started writing outside a RenderPass.
41 bool hasStartedWriteResource() const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040042
Jamie Madill316c6062018-05-29 10:49:45 -040043 // Starts rendering to an existing command buffer for the resource.
44 // The started command buffer will render outside of a RenderPass.
45 // Calls beginWriteResource if we have not yet started writing.
46 Error appendWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
47
48 // Begins a command buffer on the current graph node for in-RenderPass rendering.
49 // Currently only called from FramebufferVk::getCommandBufferForDraw.
50 Error beginRenderPass(RendererVk *renderer,
51 const Framebuffer &framebuffer,
52 const gl::Rectangle &renderArea,
53 const RenderPassDesc &renderPassDesc,
54 const std::vector<VkClearValue> &clearValues,
55 CommandBuffer **commandBufferOut) const;
56
57 // Checks if we're in a RenderPass.
58 bool hasStartedRenderPass() const;
59
60 // Returns a started command buffer if we've already called beginRenderPass.
61 void appendToRenderPass(CommandBuffer **commandBufferOut) const;
62
63 // Accessor for RenderPass RenderArea.
64 const gl::Rectangle &getRenderPassRenderArea() const;
65
66 // Called when 'this' object changes, but we'd like to start a new command buffer later.
67 void onResourceChanged(RendererVk *renderer);
68
69 // Sets up dependency relations. 'this' resource is the resource being written to.
70 void addWriteDependency(CommandGraphResource *writingResource);
71
72 // Sets up dependency relations. 'this' resource is the resource being read.
73 void addReadDependency(CommandGraphResource *readingResource);
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040074
Jamie Madill9cceac42018-03-31 14:19:16 -040075 // Returns false if the resource is not in use, and clears any current read/write nodes.
Jamie Madill316c6062018-05-29 10:49:45 -040076 // TODO(jmadill): Remove this. http://anglebug.com/2539
Jamie Madill9cceac42018-03-31 14:19:16 -040077 bool checkResourceInUseAndRefreshDeps(RendererVk *renderer);
78
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040079 private:
Jamie Madill316c6062018-05-29 10:49:45 -040080 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
81
82 // Returns true if this node has a current writing node with no children.
83 bool hasChildlessWritingNode() const;
84
85 // Allocates a new write node and calls onWriteResource internally.
86 CommandGraphNode *getNewWritingNode(RendererVk *renderer);
87
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040088 Serial mStoredQueueSerial;
89 std::vector<CommandGraphNode *> mCurrentReadingNodes;
90 CommandGraphNode *mCurrentWritingNode;
91};
92
Jamie Madill1f46bc12018-02-20 16:09:43 -050093enum class VisitedState
94{
95 Unvisited,
96 Ready,
97 Visited,
98};
99
100// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
101// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
102// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
103// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
104// when processing interleaved draw operations on independent Framebuffers.
105//
106// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
107// command submission. We also sometimes call this command re-ordering. A brief summary:
108//
109// During GL command processing, we record Vulkan commands into secondary command buffers, which
110// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
111// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
112// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
113// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400114// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500115// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
116// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
117
118class CommandGraphNode final : angle::NonCopyable
119{
120 public:
121 CommandGraphNode();
122 ~CommandGraphNode();
123
124 // Immutable queries for when we're walking the commands tree.
125 CommandBuffer *getOutsideRenderPassCommands();
126 CommandBuffer *getInsideRenderPassCommands();
127
128 // For outside the render pass (copies, transitions, etc).
129 Error beginOutsideRenderPassRecording(VkDevice device,
130 const CommandPool &commandPool,
131 CommandBuffer **commandsOut);
132
133 // For rendering commands (draws).
134 Error beginInsideRenderPassRecording(RendererVk *renderer, CommandBuffer **commandsOut);
135
136 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
137 void storeRenderPassInfo(const Framebuffer &framebuffer,
138 const gl::Rectangle renderArea,
Jamie Madillbcf467f2018-05-23 09:46:00 -0400139 const vk::RenderPassDesc &renderPassDesc,
Jamie Madill1f46bc12018-02-20 16:09:43 -0500140 const std::vector<VkClearValue> &clearValues);
141
Jamie Madill1f46bc12018-02-20 16:09:43 -0500142 // Dependency commands order node execution in the command graph.
143 // Once a node has commands that must happen after it, recording is stopped and the node is
144 // frozen forever.
145 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
146 CommandGraphNode *afterNode);
147 static void SetHappensBeforeDependencies(const std::vector<CommandGraphNode *> &beforeNodes,
148 CommandGraphNode *afterNode);
149 bool hasParents() const;
150 bool hasChildren() const;
151
152 // Commands for traversing the node on a flush operation.
153 VisitedState visitedState() const;
154 void visitParents(std::vector<CommandGraphNode *> *stack);
155 Error visitAndExecute(VkDevice device,
156 Serial serial,
157 RenderPassCache *renderPassCache,
158 CommandBuffer *primaryCommandBuffer);
159
Luc Ferron14f48172018-04-11 08:43:28 -0400160 const gl::Rectangle &getRenderPassRenderArea() const;
161
Jamie Madill1f46bc12018-02-20 16:09:43 -0500162 private:
163 void setHasChildren();
164
165 // Used for testing only.
166 bool isChildOf(CommandGraphNode *parent);
167
168 // Only used if we need a RenderPass for these commands.
169 RenderPassDesc mRenderPassDesc;
170 Framebuffer mRenderPassFramebuffer;
171 gl::Rectangle mRenderPassRenderArea;
172 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
173
174 // Keep a separate buffers for commands inside and outside a RenderPass.
175 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
176 CommandBuffer mOutsideRenderPassCommands;
177 CommandBuffer mInsideRenderPassCommands;
178
179 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
180 std::vector<CommandGraphNode *> mParents;
181
182 // If this is true, other commands exist that must be submitted after 'this' command.
183 bool mHasChildren;
184
185 // Used when traversing the dependency graph.
186 VisitedState mVisitedState;
187};
188
189// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
190// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
191// also submitting the whole command graph via submitCommands.
192class CommandGraph final : angle::NonCopyable
193{
194 public:
195 CommandGraph();
196 ~CommandGraph();
197
198 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
199 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
200 // to set up dependency relations.
201 CommandGraphNode *allocateNode();
202
203 Error submitCommands(VkDevice device,
204 Serial serial,
205 RenderPassCache *renderPassCache,
206 CommandPool *commandPool,
207 CommandBuffer *primaryCommandBufferOut);
208 bool empty() const;
209
210 private:
211 std::vector<CommandGraphNode *> mNodes;
212};
213
214} // namespace vk
215} // namespace rx
216
217#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_