blob: 2ac17ef18a2001c09a008f4f3578e554ef372380 [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
27// Only used internally in the command graph. Kept in the header for better inlining performance.
28class CommandGraphNode final : angle::NonCopyable
29{
30 public:
31 CommandGraphNode();
32 ~CommandGraphNode();
33
34 // Immutable queries for when we're walking the commands tree.
35 CommandBuffer *getOutsideRenderPassCommands();
36
37 CommandBuffer *getInsideRenderPassCommands()
38 {
39 ASSERT(!mHasChildren);
40 return &mInsideRenderPassCommands;
41 }
42
43 // For outside the render pass (copies, transitions, etc).
44 angle::Result beginOutsideRenderPassRecording(Context *context,
45 const CommandPool &commandPool,
46 CommandBuffer **commandsOut);
47
48 // For rendering commands (draws).
49 angle::Result beginInsideRenderPassRecording(Context *context, CommandBuffer **commandsOut);
50
51 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
52 void storeRenderPassInfo(const Framebuffer &framebuffer,
53 const gl::Rectangle renderArea,
54 const vk::RenderPassDesc &renderPassDesc,
55 const std::vector<VkClearValue> &clearValues);
56
57 // Dependency commands order node execution in the command graph.
58 // Once a node has commands that must happen after it, recording is stopped and the node is
59 // frozen forever.
60 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
61 CommandGraphNode *afterNode);
62 static void SetHappensBeforeDependencies(const std::vector<CommandGraphNode *> &beforeNodes,
63 CommandGraphNode *afterNode);
64 bool hasParents() const;
65 bool hasChildren() const { return mHasChildren; }
66
67 // Commands for traversing the node on a flush operation.
68 VisitedState visitedState() const;
69 void visitParents(std::vector<CommandGraphNode *> *stack);
70 angle::Result visitAndExecute(Context *context,
71 Serial serial,
72 RenderPassCache *renderPassCache,
73 CommandBuffer *primaryCommandBuffer);
74
75 const gl::Rectangle &getRenderPassRenderArea() const;
76
77 private:
78 void setHasChildren();
79
80 // Used for testing only.
81 bool isChildOf(CommandGraphNode *parent);
82
83 // Only used if we need a RenderPass for these commands.
84 RenderPassDesc mRenderPassDesc;
85 Framebuffer mRenderPassFramebuffer;
86 gl::Rectangle mRenderPassRenderArea;
87 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
88
89 // Keep a separate buffers for commands inside and outside a RenderPass.
90 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
91 CommandBuffer mOutsideRenderPassCommands;
92 CommandBuffer mInsideRenderPassCommands;
93
94 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
95 std::vector<CommandGraphNode *> mParents;
96
97 // If this is true, other commands exist that must be submitted after 'this' command.
98 bool mHasChildren;
99
100 // Used when traversing the dependency graph.
101 VisitedState mVisitedState;
102};
Jamie Madill1f46bc12018-02-20 16:09:43 -0500103
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400104// This is a helper class for back-end objects used in Vk command buffers. It records a serial
105// at command recording times indicating an order in the queue. We use Fences to detect when
106// commands finish, and then release any unreferenced and deleted resources based on the stored
107// queue serial in a special 'garbage' queue. Resources also track current read and write
108// dependencies. Only one command buffer node can be writing to the Resource at a time, but many
109// can be reading from it. Together the dependencies will form a command graph at submission time.
110class CommandGraphResource
111{
112 public:
113 CommandGraphResource();
114 virtual ~CommandGraphResource();
115
Jamie Madillc57ee252018-05-30 19:53:48 -0400116 // Returns true if the resource is in use by the renderer.
117 bool isResourceInUse(RendererVk *renderer) const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400118
Jamie Madilld014c9e2018-05-18 15:15:59 -0400119 // Sets up dependency relations. 'this' resource is the resource being written to.
120 void addWriteDependency(CommandGraphResource *writingResource);
121
122 // Sets up dependency relations. 'this' resource is the resource being read.
123 void addReadDependency(CommandGraphResource *readingResource);
124
125 protected:
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400126 // Allocates a write node via getNewWriteNode and returns a started command buffer.
127 // The started command buffer will render outside of a RenderPass.
Jamie Madille2d22702018-09-19 08:11:48 -0400128 // Will append to an existing command buffer/graph node if possible.
129 angle::Result recordCommands(Context *context, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400130
131 // Begins a command buffer on the current graph node for in-RenderPass rendering.
132 // Currently only called from FramebufferVk::getCommandBufferForDraw.
Jamie Madill21061022018-07-12 23:56:30 -0400133 angle::Result beginRenderPass(Context *context,
134 const Framebuffer &framebuffer,
135 const gl::Rectangle &renderArea,
136 const RenderPassDesc &renderPassDesc,
137 const std::vector<VkClearValue> &clearValues,
138 CommandBuffer **commandBufferOut) const;
Jamie Madill316c6062018-05-29 10:49:45 -0400139
Jamie Madill5dca6512018-05-30 10:53:51 -0400140 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
141 // Returns the started command buffer in commandBufferOut.
142 bool appendToStartedRenderPass(RendererVk *renderer, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400143
144 // Accessor for RenderPass RenderArea.
145 const gl::Rectangle &getRenderPassRenderArea() const;
146
147 // Called when 'this' object changes, but we'd like to start a new command buffer later.
Jamie Madille2d22702018-09-19 08:11:48 -0400148 void finishCurrentCommands(RendererVk *renderer);
Jamie Madill316c6062018-05-29 10:49:45 -0400149
Jamie Madillc57ee252018-05-30 19:53:48 -0400150 // Get the current queue serial for this resource. Only used to release resources.
151 Serial getStoredQueueSerial() const;
152
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400153 private:
Jamie Madill316c6062018-05-29 10:49:45 -0400154 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
155
156 // Returns true if this node has a current writing node with no children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400157 bool hasChildlessWritingNode() const
158 {
159 return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren());
160 }
Jamie Madill316c6062018-05-29 10:49:45 -0400161
Jamie Madill5dca6512018-05-30 10:53:51 -0400162 // Checks if we're in a RenderPass without children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400163 bool hasStartedRenderPass() const
164 {
165 return hasChildlessWritingNode() &&
166 mCurrentWritingNode->getInsideRenderPassCommands()->valid();
167 }
Jamie Madill5dca6512018-05-30 10:53:51 -0400168
169 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
170 // was not used in this set of command nodes.
171 void updateQueueSerial(Serial queueSerial);
172
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400173 Serial mStoredQueueSerial;
174 std::vector<CommandGraphNode *> mCurrentReadingNodes;
175 CommandGraphNode *mCurrentWritingNode;
176};
177
Jamie Madill1f46bc12018-02-20 16:09:43 -0500178// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
179// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
180// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
181// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
182// when processing interleaved draw operations on independent Framebuffers.
183//
184// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
185// command submission. We also sometimes call this command re-ordering. A brief summary:
186//
187// During GL command processing, we record Vulkan commands into secondary command buffers, which
188// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
189// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
190// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
191// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400192// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500193// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
194// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400195//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500196// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
197// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
198// also submitting the whole command graph via submitCommands.
199class CommandGraph final : angle::NonCopyable
200{
201 public:
202 CommandGraph();
203 ~CommandGraph();
204
205 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
206 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
207 // to set up dependency relations.
208 CommandGraphNode *allocateNode();
209
Jamie Madill21061022018-07-12 23:56:30 -0400210 angle::Result submitCommands(Context *context,
211 Serial serial,
212 RenderPassCache *renderPassCache,
213 CommandPool *commandPool,
214 CommandBuffer *primaryCommandBufferOut);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500215 bool empty() const;
216
217 private:
218 std::vector<CommandGraphNode *> mNodes;
219};
220
221} // namespace vk
222} // namespace rx
223
224#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_