blob: c4f7122022bbde09b4d464ac87b9d7ca089bae6f [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
34 void updateQueueSerial(Serial queueSerial);
35 Serial getQueueSerial() const;
36
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040037 // Allocates a write node via getNewWriteNode and returns a started command buffer.
38 // The started command buffer will render outside of a RenderPass.
39 Error beginWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
40
Jamie Madill316c6062018-05-29 10:49:45 -040041 // Check if we have started writing outside a RenderPass.
42 bool hasStartedWriteResource() const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040043
Jamie Madill316c6062018-05-29 10:49:45 -040044 // Starts rendering to an existing command buffer for the resource.
45 // The started command buffer will render outside of a RenderPass.
46 // Calls beginWriteResource if we have not yet started writing.
47 Error appendWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
48
49 // Begins a command buffer on the current graph node for in-RenderPass rendering.
50 // Currently only called from FramebufferVk::getCommandBufferForDraw.
51 Error beginRenderPass(RendererVk *renderer,
52 const Framebuffer &framebuffer,
53 const gl::Rectangle &renderArea,
54 const RenderPassDesc &renderPassDesc,
55 const std::vector<VkClearValue> &clearValues,
56 CommandBuffer **commandBufferOut) const;
57
58 // Checks if we're in a RenderPass.
59 bool hasStartedRenderPass() const;
60
61 // Returns a started command buffer if we've already called beginRenderPass.
62 void appendToRenderPass(CommandBuffer **commandBufferOut) const;
63
64 // Accessor for RenderPass RenderArea.
65 const gl::Rectangle &getRenderPassRenderArea() const;
66
67 // Called when 'this' object changes, but we'd like to start a new command buffer later.
68 void onResourceChanged(RendererVk *renderer);
69
70 // Sets up dependency relations. 'this' resource is the resource being written to.
71 void addWriteDependency(CommandGraphResource *writingResource);
72
73 // Sets up dependency relations. 'this' resource is the resource being read.
74 void addReadDependency(CommandGraphResource *readingResource);
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040075
Jamie Madill9cceac42018-03-31 14:19:16 -040076 // Returns false if the resource is not in use, and clears any current read/write nodes.
Jamie Madill316c6062018-05-29 10:49:45 -040077 // TODO(jmadill): Remove this. http://anglebug.com/2539
Jamie Madill9cceac42018-03-31 14:19:16 -040078 bool checkResourceInUseAndRefreshDeps(RendererVk *renderer);
79
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040080 private:
Jamie Madill316c6062018-05-29 10:49:45 -040081 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
82
83 // Returns true if this node has a current writing node with no children.
84 bool hasChildlessWritingNode() const;
85
86 // Allocates a new write node and calls onWriteResource internally.
87 CommandGraphNode *getNewWritingNode(RendererVk *renderer);
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_