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