blob: 5134ca2c66a9ce49064ca84f1b655a27bc6b47bc [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
36 // Returns true if any tracked read or write nodes match 'currentSerial'.
37 bool hasCurrentWritingNode(Serial currentSerial) const;
38
39 // Returns the active write node, and asserts 'currentSerial' matches the stored serial.
40 CommandGraphNode *getCurrentWritingNode(Serial currentSerial);
41
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
55 private:
56 Serial mStoredQueueSerial;
57 std::vector<CommandGraphNode *> mCurrentReadingNodes;
58 CommandGraphNode *mCurrentWritingNode;
59};
60
Jamie Madill1f46bc12018-02-20 16:09:43 -050061enum class VisitedState
62{
63 Unvisited,
64 Ready,
65 Visited,
66};
67
68// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
69// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
70// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
71// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
72// when processing interleaved draw operations on independent Framebuffers.
73//
74// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
75// command submission. We also sometimes call this command re-ordering. A brief summary:
76//
77// During GL command processing, we record Vulkan commands into secondary command buffers, which
78// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
79// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
80// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
81// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040082// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -050083// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
84// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
85
86class CommandGraphNode final : angle::NonCopyable
87{
88 public:
89 CommandGraphNode();
90 ~CommandGraphNode();
91
92 // Immutable queries for when we're walking the commands tree.
93 CommandBuffer *getOutsideRenderPassCommands();
94 CommandBuffer *getInsideRenderPassCommands();
95
96 // For outside the render pass (copies, transitions, etc).
97 Error beginOutsideRenderPassRecording(VkDevice device,
98 const CommandPool &commandPool,
99 CommandBuffer **commandsOut);
100
101 // For rendering commands (draws).
102 Error beginInsideRenderPassRecording(RendererVk *renderer, CommandBuffer **commandsOut);
103
104 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
105 void storeRenderPassInfo(const Framebuffer &framebuffer,
106 const gl::Rectangle renderArea,
107 const std::vector<VkClearValue> &clearValues);
108
109 // storeRenderPassInfo and append*RenderTarget store info relevant to the RenderPass.
110 // Note: RenderTargets must be added in order, with the depth/stencil being added last.
111 void appendColorRenderTarget(Serial serial, RenderTargetVk *colorRenderTarget);
112 void appendDepthStencilRenderTarget(Serial serial, RenderTargetVk *depthStencilRenderTarget);
113
114 // Dependency commands order node execution in the command graph.
115 // Once a node has commands that must happen after it, recording is stopped and the node is
116 // frozen forever.
117 static void SetHappensBeforeDependency(CommandGraphNode *beforeNode,
118 CommandGraphNode *afterNode);
119 static void SetHappensBeforeDependencies(const std::vector<CommandGraphNode *> &beforeNodes,
120 CommandGraphNode *afterNode);
121 bool hasParents() const;
122 bool hasChildren() const;
123
124 // Commands for traversing the node on a flush operation.
125 VisitedState visitedState() const;
126 void visitParents(std::vector<CommandGraphNode *> *stack);
127 Error visitAndExecute(VkDevice device,
128 Serial serial,
129 RenderPassCache *renderPassCache,
130 CommandBuffer *primaryCommandBuffer);
131
132 private:
133 void setHasChildren();
134
135 // Used for testing only.
136 bool isChildOf(CommandGraphNode *parent);
137
138 // Only used if we need a RenderPass for these commands.
139 RenderPassDesc mRenderPassDesc;
140 Framebuffer mRenderPassFramebuffer;
141 gl::Rectangle mRenderPassRenderArea;
142 gl::AttachmentArray<VkClearValue> mRenderPassClearValues;
143
144 // Keep a separate buffers for commands inside and outside a RenderPass.
145 // TODO(jmadill): We might not need inside and outside RenderPass commands separate.
146 CommandBuffer mOutsideRenderPassCommands;
147 CommandBuffer mInsideRenderPassCommands;
148
149 // Parents are commands that must be submitted before 'this' CommandNode can be submitted.
150 std::vector<CommandGraphNode *> mParents;
151
152 // If this is true, other commands exist that must be submitted after 'this' command.
153 bool mHasChildren;
154
155 // Used when traversing the dependency graph.
156 VisitedState mVisitedState;
157};
158
159// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
160// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
161// also submitting the whole command graph via submitCommands.
162class CommandGraph final : angle::NonCopyable
163{
164 public:
165 CommandGraph();
166 ~CommandGraph();
167
168 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
169 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
170 // to set up dependency relations.
171 CommandGraphNode *allocateNode();
172
173 Error submitCommands(VkDevice device,
174 Serial serial,
175 RenderPassCache *renderPassCache,
176 CommandPool *commandPool,
177 CommandBuffer *primaryCommandBufferOut);
178 bool empty() const;
179
180 private:
181 std::vector<CommandGraphNode *> mNodes;
182};
183
184} // namespace vk
185} // namespace rx
186
187#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_