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