blob: 00ef4366ecaa81f917a0bb93dac4c5ba41a2b633 [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.
46 Error beginWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
47
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.
54 Error appendWriteResource(RendererVk *renderer, CommandBuffer **commandBufferOut);
55
56 // Begins a command buffer on the current graph node for in-RenderPass rendering.
57 // Currently only called from FramebufferVk::getCommandBufferForDraw.
58 Error beginRenderPass(RendererVk *renderer,
59 const Framebuffer &framebuffer,
60 const gl::Rectangle &renderArea,
61 const RenderPassDesc &renderPassDesc,
62 const std::vector<VkClearValue> &clearValues,
63 CommandBuffer **commandBufferOut) const;
64
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
84 // Allocates a new write node and calls onWriteResource internally.
85 CommandGraphNode *getNewWritingNode(RendererVk *renderer);
86
Jamie Madill5dca6512018-05-30 10:53:51 -040087 // Checks if we're in a RenderPass without children.
88 bool hasStartedRenderPass() const;
89
90 // Updates the in-use serial tracked for this resource. Will clear dependencies if the resource
91 // was not used in this set of command nodes.
92 void updateQueueSerial(Serial queueSerial);
93
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040094 Serial mStoredQueueSerial;
95 std::vector<CommandGraphNode *> mCurrentReadingNodes;
96 CommandGraphNode *mCurrentWritingNode;
97};
98
Jamie Madill1f46bc12018-02-20 16:09:43 -050099enum class VisitedState
100{
101 Unvisited,
102 Ready,
103 Visited,
104};
105
106// Translating OpenGL commands into Vulkan and submitting them immediately loses out on some
107// of the powerful flexiblity Vulkan offers in RenderPasses. Load/Store ops can automatically
108// clear RenderPass attachments, or preserve the contents. RenderPass automatic layout transitions
109// can improve certain performance cases. Also, we can remove redundant RenderPass Begin and Ends
110// when processing interleaved draw operations on independent Framebuffers.
111//
112// ANGLE's CommandGraph (and CommandGraphNode) attempt to solve these problems using deferred
113// command submission. We also sometimes call this command re-ordering. A brief summary:
114//
115// During GL command processing, we record Vulkan commands into secondary command buffers, which
116// are stored in CommandGraphNodes, and these nodes are chained together via dependencies to
117// for a directed acyclic CommandGraph. When we need to submit the CommandGraph, say during a
118// SwapBuffers or ReadPixels call, we begin a primary Vulkan CommandBuffer, and walk the
119// CommandGraph, starting at the most senior nodes, recording secondary CommandBuffers inside
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400120// and outside RenderPasses as necessary, filled with the right load/store operations. Once
Jamie Madill1f46bc12018-02-20 16:09:43 -0500121// the primary CommandBuffer has recorded all of the secondary CommandBuffers from all the open
122// CommandGraphNodes, we submit the primary CommandBuffer to the VkQueue on the device.
Jamie Madilla5e06072018-05-18 14:36:05 -0400123//
Jamie Madill1f46bc12018-02-20 16:09:43 -0500124// The Command Graph consists of an array of open Command Graph Nodes. It supports allocating new
125// nodes for the graph, which are linked via dependency relation calls in CommandGraphNode, and
126// also submitting the whole command graph via submitCommands.
127class CommandGraph final : angle::NonCopyable
128{
129 public:
130 CommandGraph();
131 ~CommandGraph();
132
133 // Allocates a new CommandGraphNode and adds it to the list of current open nodes. No ordering
134 // relations exist in the node by default. Call CommandGraphNode::SetHappensBeforeDependency
135 // to set up dependency relations.
136 CommandGraphNode *allocateNode();
137
138 Error submitCommands(VkDevice device,
139 Serial serial,
140 RenderPassCache *renderPassCache,
141 CommandPool *commandPool,
142 CommandBuffer *primaryCommandBufferOut);
143 bool empty() const;
144
145 private:
146 std::vector<CommandGraphNode *> mNodes;
147};
148
149} // namespace vk
150} // namespace rx
151
152#endif // LIBANGLE_RENDERER_VULKAN_COMMAND_GRAPH_H_