blob: d8f3501556ed2b3e013041b97a2b0055fe93f807 [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 Madilld014c9e2018-05-18 15:15:59 -040036 // Sets up dependency relations. 'this' resource is the resource being written to.
37 void addWriteDependency(CommandGraphResource *writingResource);
38
39 // Sets up dependency relations. 'this' resource is the resource being read.
40 void addReadDependency(CommandGraphResource *readingResource);
41
42 protected:
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040043 // Allocates a write node via getNewWriteNode and returns a started command buffer.
44 // The started command buffer will render outside of a RenderPass.
45 Error beginWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
46
Jamie Madill316c6062018-05-29 10:49:45 -040047 // Check if we have started writing outside a RenderPass.
48 bool hasStartedWriteResource() const;
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040049
Jamie Madill316c6062018-05-29 10:49:45 -040050 // Starts rendering to an existing command buffer for the resource.
51 // The started command buffer will render outside of a RenderPass.
52 // Calls beginWriteResource if we have not yet started writing.
53 Error appendWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
54
55 // Begins a command buffer on the current graph node for in-RenderPass rendering.
56 // Currently only called from FramebufferVk::getCommandBufferForDraw.
57 Error beginRenderPass(RendererVk *renderer,
58 const Framebuffer &framebuffer,
59 const gl::Rectangle &renderArea,
60 const RenderPassDesc &renderPassDesc,
61 const std::vector<VkClearValue> &clearValues,
62 CommandBuffer **commandBufferOut) const;
63
Jamie Madill5dca6512018-05-30 10:53:51 -040064 // Checks if we're in a RenderPass, returning true if so. Updates serial internally.
65 // Returns the started command buffer in commandBufferOut.
66 bool appendToStartedRenderPass(RendererVk *renderer, CommandBuffer **commandBufferOut);
Jamie Madill316c6062018-05-29 10:49:45 -040067
68 // Accessor for RenderPass RenderArea.
69 const gl::Rectangle &getRenderPassRenderArea() const;
70
71 // Called when 'this' object changes, but we'd like to start a new command buffer later.
72 void onResourceChanged(RendererVk *renderer);
73
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040074 private:
Jamie Madill316c6062018-05-29 10:49:45 -040075 void onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial);
76
77 // Returns true if this node has a current writing node with no children.
78 bool hasChildlessWritingNode() const;
79
80 // Allocates a new write node and calls onWriteResource internally.
81 CommandGraphNode *getNewWritingNode(RendererVk *renderer);
82
Jamie Madill5dca6512018-05-30 10:53:51 -040083 // Checks if we're in a RenderPass without children.
84 bool hasStartedRenderPass() const;
85
86 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
87 // was not used in this set of command nodes.
88 void updateQueueSerial(Serial queueSerial);
89
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040090 Serial mStoredQueueSerial;
91 std::vector<CommandGraphNode *> mCurrentReadingNodes;
92 CommandGraphNode *mCurrentWritingNode;
93};
94
Jamie Madill1f46bc12018-02-20 16:09:43 -050095enum class VisitedState
96{
97 Unvisited,
98 Ready,
99 Visited,
100};
101
102// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
103// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
104// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
105// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
106// when processing interleaved draw operations on independent Framebuffers.
107//
108// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
109// command submission. We also sometimes call this command re-ordering. A brief summary:
110//
111// During GL command processing, we record Vulkan commands into secondary command buffers, which
112// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
113// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
114// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
115// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400116// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500117// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
118// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400119//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500120// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
121// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
122// also submitting the whole command graph via submitCommands.
123class CommandGraph final : angle::NonCopyable
124{
125 public:
126 CommandGraph();
127 ~CommandGraph();
128
129 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
130 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
131 // to set up dependency relations.
132 CommandGraphNode *allocateNode();
133
134 Error submitCommands(VkDevice device,
135 Serial serial,
136 RenderPassCache *renderPassCache,
137 CommandPool *commandPool,
138 CommandBuffer *primaryCommandBufferOut);
139 bool empty() const;
140
141 private:
142 std::vector<CommandGraphNode *> mNodes;
143};
144
145} // namespace vk
146} // namespace rx
147
148#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_