blob: 93a54d70f06c9d82c62399e9c1af852f0d0d7ba3 [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 Madilla5e06072018-05-18 14:36:05 -040020class CommandGraphNode;
Jamie Madill1f46bc12018-02-20 16:09:43 -050021
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040022// This is a helper class for back-end objects used in Vk command buffers. It records a serial
23// at command recording times indicating an order in the queue. We use Fences to detect when
24// commands finish, and then release any unreferenced and deleted resources based on the stored
25// queue serial in a special 'garbage' queue. Resources also track current read and write
26// dependencies. Only one command buffer node can be writing to the Resource at a time, but many
27// can be reading from it. Together the dependencies will form a command graph at submission time.
28class CommandGraphResource
29{
30 public:
31 CommandGraphResource();
32 virtual ~CommandGraphResource();
33
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040034 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
Jamie Madill5dca6512018-05-30 10:53:51 -040057 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
58 // Returns the started command buffer in commandBufferOut.
59 bool appendToStartedRenderPass(RendererVk *renderer, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -040060
61 // Accessor for RenderPass RenderArea.
62 const gl::Rectangle &getRenderPassRenderArea() const;
63
64 // Called when 'this' object changes, but we'd like to start a new command buffer later.
65 void onResourceChanged(RendererVk *renderer);
66
67 // Sets up dependency relations. 'this' resource is the resource being written to.
68 void addWriteDependency(CommandGraphResource *writingResource);
69
70 // Sets up dependency relations. 'this' resource is the resource being read.
71 void addReadDependency(CommandGraphResource *readingResource);
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040072
73 private:
Jamie Madill316c6062018-05-29 10:49:45 -040074 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
75
76 // Returns true if this node has a current writing node with no children.
77 bool hasChildlessWritingNode() const;
78
79 // Allocates a new write node and calls onWriteResource internally.
80 CommandGraphNode *getNewWritingNode(RendererVk *renderer);
81
Jamie Madill5dca6512018-05-30 10:53:51 -040082 // Checks if we're in a RenderPass without children.
83 bool hasStartedRenderPass() const;
84
85 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
86 // was not used in this set of command nodes.
87 void updateQueueSerial(Serial queueSerial);
88
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040089 Serial mStoredQueueSerial;
90 std::vector<CommandGraphNode *> mCurrentReadingNodes;
91 CommandGraphNode *mCurrentWritingNode;
92};
93
Jamie Madill1f46bc12018-02-20 16:09:43 -050094enum class VisitedState
95{
96 Unvisited,
97 Ready,
98 Visited,
99};
100
101// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
102// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
103// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
104// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
105// when processing interleaved draw operations on independent Framebuffers.
106//
107// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
108// command submission. We also sometimes call this command re-ordering. A brief summary:
109//
110// During GL command processing, we record Vulkan commands into secondary command buffers, which
111// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
112// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
113// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
114// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400115// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500116// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
117// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400118//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500119// 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_