blob: 16a7d155e7b3c4b5920dd779f74dff4d68b74977 [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 Madill21061022018-07-12 23:56:30 -0400128 angle::Result beginWriteResource(Context *context, CommandBuffer **commandBufferOut);
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400129
Jamie Madill316c6062018-05-29 10:49:45 -0400130 // Check if we have started writing outside a RenderPass.
131 bool hasStartedWriteResource() const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400132
Jamie Madill316c6062018-05-29 10:49:45 -0400133 // Starts rendering to an existing command buffer for the resource.
134 // The started command buffer will render outside of a RenderPass.
135 // Calls beginWriteResource if we have not yet started writing.
Jamie Madill21061022018-07-12 23:56:30 -0400136 angle::Result appendWriteResource(Context *context, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400137
138 // Begins a command buffer on the current graph node for in-RenderPass rendering.
139 // Currently only called from FramebufferVk::getCommandBufferForDraw.
Jamie Madill21061022018-07-12 23:56:30 -0400140 angle::Result beginRenderPass(Context *context,
141 const Framebuffer &framebuffer,
142 const gl::Rectangle &renderArea,
143 const RenderPassDesc &renderPassDesc,
144 const std::vector<VkClearValue> &clearValues,
145 CommandBuffer **commandBufferOut) const;
Jamie Madill316c6062018-05-29 10:49:45 -0400146
Jamie Madill5dca6512018-05-30 10:53:51 -0400147 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
148 // Returns the started command buffer in commandBufferOut.
149 bool appendToStartedRenderPass(RendererVk *renderer, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -0400150
151 // Accessor for RenderPass RenderArea.
152 const gl::Rectangle &getRenderPassRenderArea() const;
153
154 // Called when 'this' object changes, but we'd like to start a new command buffer later.
155 void onResourceChanged(RendererVk *renderer);
156
Jamie Madillc57ee252018-05-30 19:53:48 -0400157 // Get the current queue serial for this resource. Only used to release resources.
158 Serial getStoredQueueSerial() const;
159
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400160 private:
Jamie Madill316c6062018-05-29 10:49:45 -0400161 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
162
163 // Returns true if this node has a current writing node with no children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400164 bool hasChildlessWritingNode() const
165 {
166 return (mCurrentWritingNode != nullptr && !mCurrentWritingNode->hasChildren());
167 }
Jamie Madill316c6062018-05-29 10:49:45 -0400168
Jamie Madill5dca6512018-05-30 10:53:51 -0400169 // Checks if we're in a RenderPass without children.
Jamie Madill3d61ac22018-08-28 16:58:55 -0400170 bool hasStartedRenderPass() const
171 {
172 return hasChildlessWritingNode() &&
173 mCurrentWritingNode->getInsideRenderPassCommands()->valid();
174 }
Jamie Madill5dca6512018-05-30 10:53:51 -0400175
176 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
177 // was not used in this set of command nodes.
178 void updateQueueSerial(Serial queueSerial);
179
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400180 Serial mStoredQueueSerial;
181 std::vector<CommandGraphNode *> mCurrentReadingNodes;
182 CommandGraphNode *mCurrentWritingNode;
183};
184
Jamie Madill1f46bc12018-02-20 16:09:43 -0500185// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
186// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
187// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
188// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
189// when processing interleaved draw operations on independent Framebuffers.
190//
191// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
192// command submission. We also sometimes call this command re-ordering. A brief summary:
193//
194// During GL command processing, we record Vulkan commands into secondary command buffers, which
195// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
196// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
197// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
198// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400199// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500200// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
201// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400202//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500203// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
204// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
205// also submitting the whole command graph via submitCommands.
206class CommandGraph final : angle::NonCopyable
207{
208 public:
209 CommandGraph();
210 ~CommandGraph();
211
212 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
213 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
214 // to set up dependency relations.
215 CommandGraphNode *allocateNode();
216
Jamie Madill21061022018-07-12 23:56:30 -0400217 angle::Result submitCommands(Context *context,
218 Serial serial,
219 RenderPassCache *renderPassCache,
220 CommandPool *commandPool,
221 CommandBuffer *primaryCommandBufferOut);
Jamie Madill1f46bc12018-02-20 16:09:43 -0500222 bool empty() const;
223
224 private:
225 std::vector<CommandGraphNode *> mNodes;
226};
227
228} // namespace vk
229} // namespace rx
230
231#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_