Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 1 | // |
| 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 | #include "libANGLE/renderer/vulkan/CommandGraph.h" |
| 11 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 12 | #include <iostream> |
| 13 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 14 | #include "libANGLE/renderer/vulkan/ContextVk.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 15 | #include "libANGLE/renderer/vulkan/RenderTargetVk.h" |
| 16 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
| 17 | #include "libANGLE/renderer/vulkan/vk_format_utils.h" |
Jamie Madill | 26084d0 | 2018-04-09 13:44:04 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/vulkan/vk_helpers.h" |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 19 | |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 20 | #include "third_party/trace_event/trace_event.h" |
| 21 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 22 | namespace rx |
| 23 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 24 | namespace vk |
| 25 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 26 | namespace |
| 27 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 28 | angle::Result InitAndBeginCommandBuffer(vk::Context *context, |
| 29 | const CommandPool &commandPool, |
| 30 | const VkCommandBufferInheritanceInfo &inheritanceInfo, |
| 31 | VkCommandBufferUsageFlags flags, |
| 32 | CommandBuffer *commandBuffer) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 33 | { |
| 34 | ASSERT(!commandBuffer->valid()); |
| 35 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 36 | VkCommandBufferAllocateInfo createInfo = {}; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 37 | createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 38 | createInfo.commandPool = commandPool.getHandle(); |
| 39 | createInfo.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY; |
| 40 | createInfo.commandBufferCount = 1; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 41 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 42 | ANGLE_VK_TRY(context, commandBuffer->init(context->getDevice(), createInfo)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 43 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 44 | VkCommandBufferBeginInfo beginInfo = {}; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 45 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 46 | beginInfo.flags = flags | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 47 | beginInfo.pInheritanceInfo = &inheritanceInfo; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 48 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 49 | ANGLE_VK_TRY(context, commandBuffer->begin(beginInfo)); |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 50 | return angle::Result::Continue; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Shahbaz Youssefi | 6eba3c6 | 2018-10-11 14:00:08 -0400 | [diff] [blame] | 53 | const char *GetResourceTypeName(CommandGraphResourceType resourceType, |
| 54 | CommandGraphNodeFunction function) |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 55 | { |
| 56 | switch (resourceType) |
| 57 | { |
| 58 | case CommandGraphResourceType::Buffer: |
| 59 | return "Buffer"; |
| 60 | case CommandGraphResourceType::Framebuffer: |
| 61 | return "Framebuffer"; |
| 62 | case CommandGraphResourceType::Image: |
| 63 | return "Image"; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 64 | case CommandGraphResourceType::Query: |
Shahbaz Youssefi | 6eba3c6 | 2018-10-11 14:00:08 -0400 | [diff] [blame] | 65 | switch (function) |
| 66 | { |
| 67 | case CommandGraphNodeFunction::BeginQuery: |
| 68 | return "BeginQuery"; |
| 69 | case CommandGraphNodeFunction::EndQuery: |
| 70 | return "EndQuery"; |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 71 | case CommandGraphNodeFunction::WriteTimestamp: |
| 72 | return "WriteTimestamp"; |
Shahbaz Youssefi | 6eba3c6 | 2018-10-11 14:00:08 -0400 | [diff] [blame] | 73 | default: |
| 74 | UNREACHABLE(); |
| 75 | return "Query"; |
| 76 | } |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame^] | 77 | case CommandGraphResourceType::FenceSync: |
| 78 | switch (function) |
| 79 | { |
| 80 | case CommandGraphNodeFunction::SetFenceSync: |
| 81 | return "SetFenceSync"; |
| 82 | case CommandGraphNodeFunction::WaitFenceSync: |
| 83 | return "WaitFenceSync"; |
| 84 | default: |
| 85 | UNREACHABLE(); |
| 86 | return "FenceSync"; |
| 87 | } |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 88 | default: |
| 89 | UNREACHABLE(); |
| 90 | return ""; |
| 91 | } |
| 92 | } |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 93 | } // anonymous namespace |
| 94 | |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 95 | // CommandGraphResource implementation. |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 96 | CommandGraphResource::CommandGraphResource(CommandGraphResourceType resourceType) |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 97 | : mCurrentWritingNode(nullptr), mResourceType(resourceType) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 98 | {} |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 99 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 100 | CommandGraphResource::~CommandGraphResource() = default; |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 101 | |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 102 | bool CommandGraphResource::isResourceInUse(RendererVk *renderer) const |
| 103 | { |
| 104 | return renderer->isSerialInUse(mStoredQueueSerial); |
| 105 | } |
| 106 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 107 | angle::Result CommandGraphResource::recordCommands(Context *context, |
| 108 | CommandBuffer **commandBufferOut) |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 109 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 110 | updateQueueSerial(context->getRenderer()->getCurrentQueueSerial()); |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 111 | |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 112 | if (!hasChildlessWritingNode() || hasStartedRenderPass()) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 113 | { |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 114 | startNewCommands(context->getRenderer()); |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 115 | return mCurrentWritingNode->beginOutsideRenderPassRecording( |
| 116 | context, context->getRenderer()->getCommandPool(), commandBufferOut); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | CommandBuffer *outsideRenderPassCommands = mCurrentWritingNode->getOutsideRenderPassCommands(); |
| 120 | if (!outsideRenderPassCommands->valid()) |
| 121 | { |
| 122 | ANGLE_TRY(mCurrentWritingNode->beginOutsideRenderPassRecording( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 123 | context, context->getRenderer()->getCommandPool(), commandBufferOut)); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 124 | } |
| 125 | else |
| 126 | { |
| 127 | *commandBufferOut = outsideRenderPassCommands; |
| 128 | } |
| 129 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 130 | return angle::Result::Continue; |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 131 | } |
| 132 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 133 | const gl::Rectangle &CommandGraphResource::getRenderPassRenderArea() const |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 134 | { |
| 135 | ASSERT(hasStartedRenderPass()); |
| 136 | return mCurrentWritingNode->getRenderPassRenderArea(); |
| 137 | } |
| 138 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 139 | angle::Result CommandGraphResource::beginRenderPass(ContextVk *contextVk, |
| 140 | const Framebuffer &framebuffer, |
| 141 | const gl::Rectangle &renderArea, |
| 142 | const RenderPassDesc &renderPassDesc, |
| 143 | const std::vector<VkClearValue> &clearValues, |
| 144 | CommandBuffer **commandBufferOut) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 145 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 146 | // If a barrier has been inserted in the meantime, stop the command buffer. |
| 147 | if (!hasChildlessWritingNode()) |
| 148 | { |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 149 | startNewCommands(contextVk->getRenderer()); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 152 | // Hard-code RenderPass to clear the first render target to the current clear value. |
| 153 | // TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361 |
| 154 | mCurrentWritingNode->storeRenderPassInfo(framebuffer, renderArea, renderPassDesc, clearValues); |
| 155 | |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 156 | mCurrentWritingNode->setCommandBufferOwner(contextVk); |
| 157 | |
| 158 | return mCurrentWritingNode->beginInsideRenderPassRecording(contextVk, commandBufferOut); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 161 | void CommandGraphResource::addWriteDependency(CommandGraphResource *writingResource) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 162 | { |
| 163 | CommandGraphNode *writingNode = writingResource->mCurrentWritingNode; |
| 164 | ASSERT(writingNode); |
| 165 | |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 166 | onWriteImpl(writingNode, writingResource->getStoredQueueSerial()); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 169 | void CommandGraphResource::addReadDependency(CommandGraphResource *readingResource) |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 170 | { |
| 171 | updateQueueSerial(readingResource->getStoredQueueSerial()); |
| 172 | |
| 173 | CommandGraphNode *readingNode = readingResource->mCurrentWritingNode; |
| 174 | ASSERT(readingNode); |
| 175 | |
Shahbaz Youssefi | b5ba549 | 2019-01-02 15:19:22 -0500 | [diff] [blame] | 176 | if (mCurrentWritingNode) |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 177 | { |
| 178 | // Ensure 'readingNode' happens after the current writing node. |
| 179 | CommandGraphNode::SetHappensBeforeDependency(mCurrentWritingNode, readingNode); |
| 180 | } |
| 181 | |
| 182 | // Add the read node to the list of nodes currently reading this resource. |
| 183 | mCurrentReadingNodes.push_back(readingNode); |
| 184 | } |
| 185 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 186 | void CommandGraphResource::finishCurrentCommands(RendererVk *renderer) |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 187 | { |
| 188 | startNewCommands(renderer); |
| 189 | } |
| 190 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 191 | void CommandGraphResource::startNewCommands(RendererVk *renderer) |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 192 | { |
| 193 | CommandGraphNode *newCommands = |
| 194 | renderer->getCommandGraph()->allocateNode(CommandGraphNodeFunction::Generic); |
| 195 | newCommands->setDiagnosticInfo(mResourceType, reinterpret_cast<uintptr_t>(this)); |
| 196 | onWriteImpl(newCommands, renderer->getCurrentQueueSerial()); |
| 197 | } |
| 198 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 199 | void CommandGraphResource::onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 200 | { |
| 201 | updateQueueSerial(currentSerial); |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 202 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 203 | // Make sure any open reads and writes finish before we execute 'writingNode'. |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 204 | if (!mCurrentReadingNodes.empty()) |
| 205 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 206 | CommandGraphNode::SetHappensBeforeDependencies(mCurrentReadingNodes.data(), |
| 207 | mCurrentReadingNodes.size(), writingNode); |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 208 | mCurrentReadingNodes.clear(); |
| 209 | } |
| 210 | |
| 211 | if (mCurrentWritingNode && mCurrentWritingNode != writingNode) |
| 212 | { |
| 213 | CommandGraphNode::SetHappensBeforeDependency(mCurrentWritingNode, writingNode); |
| 214 | } |
| 215 | |
| 216 | mCurrentWritingNode = writingNode; |
| 217 | } |
| 218 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 219 | // CommandGraphNode implementation. |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 220 | CommandGraphNode::CommandGraphNode(CommandGraphNodeFunction function) |
| 221 | : mRenderPassClearValues{}, |
| 222 | mFunction(function), |
| 223 | mQueryPool(VK_NULL_HANDLE), |
| 224 | mQueryIndex(0), |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame^] | 225 | mFenceSyncEvent(VK_NULL_HANDLE), |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 226 | mHasChildren(false), |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 227 | mVisitedState(VisitedState::Unvisited), |
| 228 | mGlobalMemoryBarrierSrcAccess(0), |
Jamie Madill | 85ca189 | 2019-01-16 13:27:15 -0500 | [diff] [blame] | 229 | mGlobalMemoryBarrierDstAccess(0), |
| 230 | mCommandBufferOwner(nullptr) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 231 | {} |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 232 | |
| 233 | CommandGraphNode::~CommandGraphNode() |
| 234 | { |
| 235 | mRenderPassFramebuffer.setHandle(VK_NULL_HANDLE); |
| 236 | |
| 237 | // Command buffers are managed by the command pool, so don't need to be freed. |
| 238 | mOutsideRenderPassCommands.releaseHandle(); |
| 239 | mInsideRenderPassCommands.releaseHandle(); |
| 240 | } |
| 241 | |
| 242 | CommandBuffer *CommandGraphNode::getOutsideRenderPassCommands() |
| 243 | { |
| 244 | ASSERT(!mHasChildren); |
| 245 | return &mOutsideRenderPassCommands; |
| 246 | } |
| 247 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 248 | angle::Result CommandGraphNode::beginOutsideRenderPassRecording(Context *context, |
| 249 | const CommandPool &commandPool, |
| 250 | CommandBuffer **commandsOut) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 251 | { |
| 252 | ASSERT(!mHasChildren); |
| 253 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 254 | VkCommandBufferInheritanceInfo inheritanceInfo = {}; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 255 | inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
| 256 | inheritanceInfo.renderPass = VK_NULL_HANDLE; |
| 257 | inheritanceInfo.subpass = 0; |
| 258 | inheritanceInfo.framebuffer = VK_NULL_HANDLE; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 259 | inheritanceInfo.occlusionQueryEnable = |
| 260 | context->getRenderer()->getPhysicalDeviceFeatures().inheritedQueries; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 261 | inheritanceInfo.queryFlags = 0; |
| 262 | inheritanceInfo.pipelineStatistics = 0; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 263 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 264 | ANGLE_TRY(InitAndBeginCommandBuffer(context, commandPool, inheritanceInfo, 0, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 265 | &mOutsideRenderPassCommands)); |
| 266 | |
| 267 | *commandsOut = &mOutsideRenderPassCommands; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 268 | return angle::Result::Continue; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 271 | angle::Result CommandGraphNode::beginInsideRenderPassRecording(Context *context, |
| 272 | CommandBuffer **commandsOut) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 273 | { |
| 274 | ASSERT(!mHasChildren); |
| 275 | |
| 276 | // Get a compatible RenderPass from the cache so we can initialize the inheritance info. |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 277 | // TODO(jmadill): Support query for compatible/conformant render pass. http://anglebug.com/2361 |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 278 | RenderPass *compatibleRenderPass; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 279 | ANGLE_TRY(context->getRenderer()->getCompatibleRenderPass(context, mRenderPassDesc, |
| 280 | &compatibleRenderPass)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 281 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 282 | VkCommandBufferInheritanceInfo inheritanceInfo = {}; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 283 | inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
| 284 | inheritanceInfo.renderPass = compatibleRenderPass->getHandle(); |
| 285 | inheritanceInfo.subpass = 0; |
| 286 | inheritanceInfo.framebuffer = mRenderPassFramebuffer.getHandle(); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 287 | inheritanceInfo.occlusionQueryEnable = |
| 288 | context->getRenderer()->getPhysicalDeviceFeatures().inheritedQueries; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 289 | inheritanceInfo.queryFlags = 0; |
| 290 | inheritanceInfo.pipelineStatistics = 0; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 291 | |
| 292 | ANGLE_TRY(InitAndBeginCommandBuffer( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 293 | context, context->getRenderer()->getCommandPool(), inheritanceInfo, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 294 | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &mInsideRenderPassCommands)); |
| 295 | |
| 296 | *commandsOut = &mInsideRenderPassCommands; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 297 | return angle::Result::Continue; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void CommandGraphNode::storeRenderPassInfo(const Framebuffer &framebuffer, |
| 301 | const gl::Rectangle renderArea, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 302 | const vk::RenderPassDesc &renderPassDesc, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 303 | const std::vector<VkClearValue> &clearValues) |
| 304 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 305 | mRenderPassDesc = renderPassDesc; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 306 | mRenderPassFramebuffer.setHandle(framebuffer.getHandle()); |
| 307 | mRenderPassRenderArea = renderArea; |
| 308 | std::copy(clearValues.begin(), clearValues.end(), mRenderPassClearValues.begin()); |
| 309 | } |
| 310 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 311 | // static |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 312 | void CommandGraphNode::SetHappensBeforeDependencies(CommandGraphNode **beforeNodes, |
| 313 | size_t beforeNodesCount, |
| 314 | CommandGraphNode *afterNode) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 315 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 316 | afterNode->mParents.insert(afterNode->mParents.end(), beforeNodes, |
| 317 | beforeNodes + beforeNodesCount); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 318 | |
| 319 | // TODO(jmadill): is there a faster way to do this? |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 320 | for (size_t i = 0; i < beforeNodesCount; ++i) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 321 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 322 | beforeNodes[i]->setHasChildren(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 323 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 324 | ASSERT(beforeNodes[i] != afterNode && !beforeNodes[i]->isChildOf(afterNode)); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | void CommandGraphNode::SetHappensBeforeDependencies(CommandGraphNode *beforeNode, |
| 329 | CommandGraphNode **afterNodes, |
| 330 | size_t afterNodesCount) |
| 331 | { |
| 332 | for (size_t i = 0; i < afterNodesCount; ++i) |
| 333 | { |
| 334 | SetHappensBeforeDependency(beforeNode, afterNodes[i]); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | bool CommandGraphNode::hasParents() const |
| 339 | { |
| 340 | return !mParents.empty(); |
| 341 | } |
| 342 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 343 | void CommandGraphNode::setQueryPool(const QueryPool *queryPool, uint32_t queryIndex) |
| 344 | { |
| 345 | ASSERT(mFunction == CommandGraphNodeFunction::BeginQuery || |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 346 | mFunction == CommandGraphNodeFunction::EndQuery || |
| 347 | mFunction == CommandGraphNodeFunction::WriteTimestamp); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 348 | mQueryPool = queryPool->getHandle(); |
| 349 | mQueryIndex = queryIndex; |
| 350 | } |
| 351 | |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame^] | 352 | void CommandGraphNode::setFenceSync(const vk::Event &event) |
| 353 | { |
| 354 | ASSERT(mFunction == CommandGraphNodeFunction::SetFenceSync || |
| 355 | mFunction == CommandGraphNodeFunction::WaitFenceSync); |
| 356 | mFenceSyncEvent = event.getHandle(); |
| 357 | } |
| 358 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 359 | // Do not call this in anything but testing code, since it's slow. |
| 360 | bool CommandGraphNode::isChildOf(CommandGraphNode *parent) |
| 361 | { |
| 362 | std::set<CommandGraphNode *> visitedList; |
| 363 | std::vector<CommandGraphNode *> openList; |
| 364 | openList.insert(openList.begin(), mParents.begin(), mParents.end()); |
| 365 | while (!openList.empty()) |
| 366 | { |
| 367 | CommandGraphNode *current = openList.back(); |
| 368 | openList.pop_back(); |
| 369 | if (visitedList.count(current) == 0) |
| 370 | { |
| 371 | if (current == parent) |
| 372 | { |
| 373 | return true; |
| 374 | } |
| 375 | visitedList.insert(current); |
| 376 | openList.insert(openList.end(), current->mParents.begin(), current->mParents.end()); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | VisitedState CommandGraphNode::visitedState() const |
| 384 | { |
| 385 | return mVisitedState; |
| 386 | } |
| 387 | |
| 388 | void CommandGraphNode::visitParents(std::vector<CommandGraphNode *> *stack) |
| 389 | { |
| 390 | ASSERT(mVisitedState == VisitedState::Unvisited); |
| 391 | stack->insert(stack->end(), mParents.begin(), mParents.end()); |
| 392 | mVisitedState = VisitedState::Ready; |
| 393 | } |
| 394 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 395 | angle::Result CommandGraphNode::visitAndExecute(vk::Context *context, |
| 396 | Serial serial, |
| 397 | RenderPassCache *renderPassCache, |
| 398 | CommandBuffer *primaryCommandBuffer) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 399 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 400 | switch (mFunction) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 401 | { |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 402 | case CommandGraphNodeFunction::Generic: |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame^] | 403 | ASSERT(mQueryPool == VK_NULL_HANDLE && mFenceSyncEvent == VK_NULL_HANDLE); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 404 | |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 405 | // Record the deferred pipeline barrier if necessary. |
| 406 | ASSERT((mGlobalMemoryBarrierDstAccess == 0) == (mGlobalMemoryBarrierSrcAccess == 0)); |
| 407 | if (mGlobalMemoryBarrierSrcAccess) |
| 408 | { |
| 409 | VkMemoryBarrier memoryBarrier = {}; |
| 410 | memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; |
| 411 | memoryBarrier.srcAccessMask = mGlobalMemoryBarrierSrcAccess; |
| 412 | memoryBarrier.dstAccessMask = mGlobalMemoryBarrierDstAccess; |
| 413 | |
Shahbaz Youssefi | 471358f | 2018-11-28 15:21:55 -0500 | [diff] [blame] | 414 | // Use the all pipe stage to keep the state management simple. |
| 415 | primaryCommandBuffer->pipelineBarrier(VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, |
| 416 | VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 417 | &memoryBarrier, 0, nullptr, 0, nullptr); |
| 418 | } |
| 419 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 420 | if (mOutsideRenderPassCommands.valid()) |
| 421 | { |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 422 | ANGLE_VK_TRY(context, mOutsideRenderPassCommands.end()); |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 423 | primaryCommandBuffer->executeCommands(1, &mOutsideRenderPassCommands); |
| 424 | } |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 425 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 426 | if (mInsideRenderPassCommands.valid()) |
| 427 | { |
| 428 | // Pull a compatible RenderPass from the cache. |
| 429 | // TODO(jmadill): Insert real ops and layout transitions. |
| 430 | RenderPass *renderPass = nullptr; |
| 431 | ANGLE_TRY(renderPassCache->getCompatibleRenderPass(context, serial, mRenderPassDesc, |
| 432 | &renderPass)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 433 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 434 | ANGLE_VK_TRY(context, mInsideRenderPassCommands.end()); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 435 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 436 | VkRenderPassBeginInfo beginInfo = {}; |
| 437 | beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
| 438 | beginInfo.renderPass = renderPass->getHandle(); |
| 439 | beginInfo.framebuffer = mRenderPassFramebuffer.getHandle(); |
| 440 | beginInfo.renderArea.offset.x = static_cast<uint32_t>(mRenderPassRenderArea.x); |
| 441 | beginInfo.renderArea.offset.y = static_cast<uint32_t>(mRenderPassRenderArea.y); |
| 442 | beginInfo.renderArea.extent.width = |
| 443 | static_cast<uint32_t>(mRenderPassRenderArea.width); |
| 444 | beginInfo.renderArea.extent.height = |
| 445 | static_cast<uint32_t>(mRenderPassRenderArea.height); |
| 446 | beginInfo.clearValueCount = mRenderPassDesc.attachmentCount(); |
| 447 | beginInfo.pClearValues = mRenderPassClearValues.data(); |
| 448 | |
| 449 | primaryCommandBuffer->beginRenderPass( |
| 450 | beginInfo, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS); |
| 451 | primaryCommandBuffer->executeCommands(1, &mInsideRenderPassCommands); |
| 452 | primaryCommandBuffer->endRenderPass(); |
| 453 | } |
| 454 | break; |
| 455 | |
| 456 | case CommandGraphNodeFunction::BeginQuery: |
| 457 | ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid()); |
| 458 | ASSERT(mQueryPool != VK_NULL_HANDLE); |
| 459 | |
| 460 | primaryCommandBuffer->resetQueryPool(mQueryPool, mQueryIndex, 1); |
| 461 | primaryCommandBuffer->beginQuery(mQueryPool, mQueryIndex, 0); |
| 462 | |
| 463 | break; |
| 464 | |
| 465 | case CommandGraphNodeFunction::EndQuery: |
| 466 | ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid()); |
| 467 | ASSERT(mQueryPool != VK_NULL_HANDLE); |
| 468 | |
| 469 | primaryCommandBuffer->endQuery(mQueryPool, mQueryIndex); |
| 470 | |
| 471 | break; |
| 472 | |
Shahbaz Youssefi | c2b576d | 2018-10-12 14:45:34 -0400 | [diff] [blame] | 473 | case CommandGraphNodeFunction::WriteTimestamp: |
| 474 | ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid()); |
| 475 | ASSERT(mQueryPool != VK_NULL_HANDLE); |
| 476 | |
| 477 | primaryCommandBuffer->resetQueryPool(mQueryPool, mQueryIndex, 1); |
| 478 | primaryCommandBuffer->writeTimestamp(VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, mQueryPool, |
| 479 | mQueryIndex); |
| 480 | |
| 481 | break; |
| 482 | |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame^] | 483 | case CommandGraphNodeFunction::SetFenceSync: |
| 484 | ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid()); |
| 485 | ASSERT(mFenceSyncEvent != VK_NULL_HANDLE); |
| 486 | |
| 487 | primaryCommandBuffer->setEvent(mFenceSyncEvent, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT); |
| 488 | |
| 489 | break; |
| 490 | |
| 491 | case CommandGraphNodeFunction::WaitFenceSync: |
| 492 | ASSERT(!mOutsideRenderPassCommands.valid() && !mInsideRenderPassCommands.valid()); |
| 493 | ASSERT(mFenceSyncEvent != VK_NULL_HANDLE); |
| 494 | |
| 495 | // Fence Syncs are purely execution barriers, so there are no memory barriers attached. |
| 496 | primaryCommandBuffer->waitEvents( |
| 497 | 1, &mFenceSyncEvent, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, |
| 498 | VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, nullptr, 0, nullptr, 0, nullptr); |
| 499 | |
| 500 | break; |
| 501 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 502 | default: |
| 503 | UNREACHABLE(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | mVisitedState = VisitedState::Visited; |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 507 | return angle::Result::Continue; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 508 | } |
| 509 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 510 | const std::vector<CommandGraphNode *> &CommandGraphNode::getParentsForDiagnostics() const |
| 511 | { |
| 512 | return mParents; |
| 513 | } |
| 514 | |
| 515 | void CommandGraphNode::setDiagnosticInfo(CommandGraphResourceType resourceType, |
| 516 | uintptr_t resourceID) |
| 517 | { |
| 518 | mResourceType = resourceType; |
| 519 | mResourceID = resourceID; |
| 520 | } |
| 521 | |
Luc Ferron | 14f4817 | 2018-04-11 08:43:28 -0400 | [diff] [blame] | 522 | const gl::Rectangle &CommandGraphNode::getRenderPassRenderArea() const |
| 523 | { |
| 524 | return mRenderPassRenderArea; |
| 525 | } |
| 526 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 527 | // CommandGraph implementation. |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 528 | CommandGraph::CommandGraph(bool enableGraphDiagnostics) |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 529 | : mEnableGraphDiagnostics(enableGraphDiagnostics), mLastBarrierIndex(kInvalidNodeIndex) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 530 | {} |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 531 | |
| 532 | CommandGraph::~CommandGraph() |
| 533 | { |
| 534 | ASSERT(empty()); |
| 535 | } |
| 536 | |
Jamie Madill | 193a284 | 2018-10-30 17:28:41 -0400 | [diff] [blame] | 537 | CommandGraphNode *CommandGraph::allocateNode(CommandGraphNodeFunction function) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 538 | { |
| 539 | // TODO(jmadill): Use a pool allocator for the CPU node allocations. |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 540 | CommandGraphNode *newCommands = new CommandGraphNode(function); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 541 | mNodes.emplace_back(newCommands); |
| 542 | return newCommands; |
| 543 | } |
| 544 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 545 | CommandGraphNode *CommandGraph::allocateBarrierNode(CommandGraphResourceType resourceType, |
| 546 | CommandGraphNodeFunction function) |
| 547 | { |
| 548 | CommandGraphNode *newNode = allocateNode(function); |
| 549 | newNode->setDiagnosticInfo(resourceType, 0); |
| 550 | setNewBarrier(newNode); |
| 551 | |
| 552 | return newNode; |
| 553 | } |
| 554 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 555 | void CommandGraph::setNewBarrier(CommandGraphNode *newBarrier) |
| 556 | { |
| 557 | size_t previousBarrierIndex = 0; |
| 558 | CommandGraphNode *previousBarrier = getLastBarrierNode(&previousBarrierIndex); |
| 559 | |
| 560 | // Add a dependency from previousBarrier to all nodes in (previousBarrier, newBarrier]. |
| 561 | if (previousBarrier && previousBarrierIndex + 1 < mNodes.size()) |
| 562 | { |
| 563 | size_t afterNodesCount = mNodes.size() - (previousBarrierIndex + 1); |
| 564 | CommandGraphNode::SetHappensBeforeDependencies( |
| 565 | previousBarrier, &mNodes[previousBarrierIndex + 1], afterNodesCount); |
| 566 | } |
| 567 | |
| 568 | // Add a dependency from all nodes in (previousBarrier, newBarrier) to newBarrier. |
| 569 | addDependenciesToNextBarrier(previousBarrierIndex + 1, mNodes.size() - 1, newBarrier); |
| 570 | |
| 571 | mLastBarrierIndex = mNodes.size() - 1; |
| 572 | } |
| 573 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 574 | angle::Result CommandGraph::submitCommands(Context *context, |
| 575 | Serial serial, |
| 576 | RenderPassCache *renderPassCache, |
| 577 | CommandPool *commandPool, |
| 578 | CommandBuffer *primaryCommandBufferOut) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 579 | { |
Shahbaz Youssefi | 3a48217 | 2018-10-11 10:34:44 -0400 | [diff] [blame] | 580 | // There is no point in submitting an empty command buffer, so make sure not to call this |
| 581 | // function if there's nothing to do. |
| 582 | ASSERT(!mNodes.empty()); |
| 583 | |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 584 | size_t previousBarrierIndex = 0; |
| 585 | CommandGraphNode *previousBarrier = getLastBarrierNode(&previousBarrierIndex); |
| 586 | |
| 587 | // Add a dependency from previousBarrier to all nodes in (previousBarrier, end]. |
| 588 | if (previousBarrier && previousBarrierIndex + 1 < mNodes.size()) |
| 589 | { |
| 590 | size_t afterNodesCount = mNodes.size() - (previousBarrierIndex + 1); |
| 591 | CommandGraphNode::SetHappensBeforeDependencies( |
| 592 | previousBarrier, &mNodes[previousBarrierIndex + 1], afterNodesCount); |
| 593 | } |
| 594 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 595 | VkCommandBufferAllocateInfo primaryInfo = {}; |
Jamie Madill | 03d1a5e | 2018-11-12 11:34:24 -0500 | [diff] [blame] | 596 | primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 597 | primaryInfo.commandPool = commandPool->getHandle(); |
| 598 | primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 599 | primaryInfo.commandBufferCount = 1; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 600 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 601 | ANGLE_VK_TRY(context, primaryCommandBufferOut->init(context->getDevice(), primaryInfo)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 602 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 603 | if (mEnableGraphDiagnostics) |
| 604 | { |
| 605 | dumpGraphDotFile(std::cout); |
| 606 | } |
| 607 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 608 | std::vector<CommandGraphNode *> nodeStack; |
| 609 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 610 | VkCommandBufferBeginInfo beginInfo = {}; |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 611 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 612 | beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 613 | beginInfo.pInheritanceInfo = nullptr; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 614 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 615 | ANGLE_VK_TRY(context, primaryCommandBufferOut->begin(beginInfo)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 616 | |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 617 | ANGLE_TRY(context->getRenderer()->traceGpuEvent( |
| 618 | context, primaryCommandBufferOut, TRACE_EVENT_PHASE_BEGIN, "Primary Command Buffer")); |
| 619 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 620 | for (CommandGraphNode *topLevelNode : mNodes) |
| 621 | { |
| 622 | // Only process commands that don't have child commands. The others will be pulled in |
| 623 | // automatically. Also skip commands that have already been visited. |
| 624 | if (topLevelNode->hasChildren() || topLevelNode->visitedState() != VisitedState::Unvisited) |
| 625 | continue; |
| 626 | |
| 627 | nodeStack.push_back(topLevelNode); |
| 628 | |
| 629 | while (!nodeStack.empty()) |
| 630 | { |
| 631 | CommandGraphNode *node = nodeStack.back(); |
| 632 | |
| 633 | switch (node->visitedState()) |
| 634 | { |
| 635 | case VisitedState::Unvisited: |
| 636 | node->visitParents(&nodeStack); |
| 637 | break; |
| 638 | case VisitedState::Ready: |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 639 | ANGLE_TRY(node->visitAndExecute(context, serial, renderPassCache, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 640 | primaryCommandBufferOut)); |
| 641 | nodeStack.pop_back(); |
| 642 | break; |
| 643 | case VisitedState::Visited: |
| 644 | nodeStack.pop_back(); |
| 645 | break; |
| 646 | default: |
| 647 | UNREACHABLE(); |
| 648 | break; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
Shahbaz Youssefi | 25224e7 | 2018-10-22 11:56:02 -0400 | [diff] [blame] | 653 | ANGLE_TRY(context->getRenderer()->traceGpuEvent( |
| 654 | context, primaryCommandBufferOut, TRACE_EVENT_PHASE_END, "Primary Command Buffer")); |
| 655 | |
Yuly Novikov | 2778029 | 2018-11-09 11:19:49 -0500 | [diff] [blame] | 656 | ANGLE_VK_TRY(context, primaryCommandBufferOut->end()); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 657 | |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 658 | clear(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 659 | |
Jamie Madill | 7c985f5 | 2018-11-29 18:16:17 -0500 | [diff] [blame] | 660 | return angle::Result::Continue; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | bool CommandGraph::empty() const |
| 664 | { |
| 665 | return mNodes.empty(); |
| 666 | } |
| 667 | |
Yuly Novikov | b56ddbb | 2018-11-02 16:53:18 -0400 | [diff] [blame] | 668 | void CommandGraph::clear() |
| 669 | { |
| 670 | mLastBarrierIndex = kInvalidNodeIndex; |
| 671 | |
| 672 | // TODO(jmadill): Use pool allocator for performance. http://anglebug.com/2951 |
| 673 | for (CommandGraphNode *node : mNodes) |
| 674 | { |
| 675 | delete node; |
| 676 | } |
| 677 | mNodes.clear(); |
| 678 | } |
| 679 | |
Shahbaz Youssefi | c81e7bf | 2019-01-18 15:35:55 -0500 | [diff] [blame] | 680 | void CommandGraph::beginQuery(const QueryPool *queryPool, uint32_t queryIndex) |
| 681 | { |
| 682 | CommandGraphNode *newNode = |
| 683 | allocateBarrierNode(CommandGraphResourceType::Query, CommandGraphNodeFunction::BeginQuery); |
| 684 | newNode->setQueryPool(queryPool, queryIndex); |
| 685 | } |
| 686 | |
| 687 | void CommandGraph::endQuery(const QueryPool *queryPool, uint32_t queryIndex) |
| 688 | { |
| 689 | CommandGraphNode *newNode = |
| 690 | allocateBarrierNode(CommandGraphResourceType::Query, CommandGraphNodeFunction::EndQuery); |
| 691 | newNode->setQueryPool(queryPool, queryIndex); |
| 692 | } |
| 693 | |
| 694 | void CommandGraph::writeTimestamp(const QueryPool *queryPool, uint32_t queryIndex) |
| 695 | { |
| 696 | CommandGraphNode *newNode = allocateBarrierNode(CommandGraphResourceType::Query, |
| 697 | CommandGraphNodeFunction::WriteTimestamp); |
| 698 | newNode->setQueryPool(queryPool, queryIndex); |
| 699 | } |
| 700 | |
Shahbaz Youssefi | 82fddcb | 2019-01-18 14:27:43 -0500 | [diff] [blame^] | 701 | void CommandGraph::setFenceSync(const vk::Event &event) |
| 702 | { |
| 703 | CommandGraphNode *newNode = allocateBarrierNode(CommandGraphResourceType::FenceSync, |
| 704 | CommandGraphNodeFunction::SetFenceSync); |
| 705 | newNode->setFenceSync(event); |
| 706 | } |
| 707 | |
| 708 | void CommandGraph::waitFenceSync(const vk::Event &event) |
| 709 | { |
| 710 | CommandGraphNode *newNode = allocateBarrierNode(CommandGraphResourceType::FenceSync, |
| 711 | CommandGraphNodeFunction::WaitFenceSync); |
| 712 | newNode->setFenceSync(event); |
| 713 | } |
| 714 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 715 | // Dumps the command graph into a dot file that works with graphviz. |
| 716 | void CommandGraph::dumpGraphDotFile(std::ostream &out) const |
| 717 | { |
| 718 | // This ID maps a node pointer to a monatonic ID. It allows us to look up parent node IDs. |
| 719 | std::map<const CommandGraphNode *, int> nodeIDMap; |
| 720 | std::map<uintptr_t, int> objectIDMap; |
| 721 | |
| 722 | // Map nodes to ids. |
| 723 | for (size_t nodeIndex = 0; nodeIndex < mNodes.size(); ++nodeIndex) |
| 724 | { |
| 725 | const CommandGraphNode *node = mNodes[nodeIndex]; |
| 726 | nodeIDMap[node] = static_cast<int>(nodeIndex) + 1; |
| 727 | } |
| 728 | |
| 729 | int bufferIDCounter = 1; |
| 730 | int framebufferIDCounter = 1; |
| 731 | int imageIDCounter = 1; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 732 | int queryIDCounter = 1; |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 733 | |
| 734 | out << "digraph {" << std::endl; |
| 735 | |
| 736 | for (const CommandGraphNode *node : mNodes) |
| 737 | { |
| 738 | int nodeID = nodeIDMap[node]; |
| 739 | |
| 740 | std::stringstream strstr; |
Shahbaz Youssefi | 6eba3c6 | 2018-10-11 14:00:08 -0400 | [diff] [blame] | 741 | strstr << GetResourceTypeName(node->getResourceTypeForDiagnostics(), node->getFunction()); |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 742 | strstr << " "; |
| 743 | |
| 744 | auto it = objectIDMap.find(node->getResourceIDForDiagnostics()); |
| 745 | if (it != objectIDMap.end()) |
| 746 | { |
| 747 | strstr << it->second; |
| 748 | } |
| 749 | else |
| 750 | { |
| 751 | int id = 0; |
| 752 | |
| 753 | switch (node->getResourceTypeForDiagnostics()) |
| 754 | { |
| 755 | case CommandGraphResourceType::Buffer: |
| 756 | id = bufferIDCounter++; |
| 757 | break; |
| 758 | case CommandGraphResourceType::Framebuffer: |
| 759 | id = framebufferIDCounter++; |
| 760 | break; |
| 761 | case CommandGraphResourceType::Image: |
| 762 | id = imageIDCounter++; |
| 763 | break; |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 764 | case CommandGraphResourceType::Query: |
| 765 | id = queryIDCounter++; |
| 766 | break; |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 767 | default: |
| 768 | UNREACHABLE(); |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | objectIDMap[node->getResourceIDForDiagnostics()] = id; |
| 773 | strstr << id; |
| 774 | } |
| 775 | |
| 776 | const std::string &label = strstr.str(); |
| 777 | out << " " << nodeID << "[label =<" << label << "<BR/> <FONT POINT-SIZE=\"10\">Node ID " |
| 778 | << nodeID << "</FONT>>];" << std::endl; |
| 779 | } |
| 780 | |
| 781 | for (const CommandGraphNode *node : mNodes) |
| 782 | { |
| 783 | int nodeID = nodeIDMap[node]; |
| 784 | |
| 785 | for (const CommandGraphNode *parent : node->getParentsForDiagnostics()) |
| 786 | { |
| 787 | int parentID = nodeIDMap[parent]; |
| 788 | out << " " << parentID << " -> " << nodeID << ";" << std::endl; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | out << "}" << std::endl; |
| 793 | } |
Shahbaz Youssefi | 563fbaa | 2018-10-02 11:22:01 -0400 | [diff] [blame] | 794 | |
| 795 | CommandGraphNode *CommandGraph::getLastBarrierNode(size_t *indexOut) |
| 796 | { |
| 797 | *indexOut = mLastBarrierIndex == kInvalidNodeIndex ? 0 : mLastBarrierIndex; |
| 798 | return mLastBarrierIndex == kInvalidNodeIndex ? nullptr : mNodes[mLastBarrierIndex]; |
| 799 | } |
| 800 | |
| 801 | void CommandGraph::addDependenciesToNextBarrier(size_t begin, |
| 802 | size_t end, |
| 803 | CommandGraphNode *nextBarrier) |
| 804 | { |
| 805 | for (size_t i = begin; i < end; ++i) |
| 806 | { |
| 807 | // As a small optimization, only add edges to childless nodes. The others have an |
| 808 | // indirect dependency. |
| 809 | if (!mNodes[i]->hasChildren()) |
| 810 | { |
| 811 | CommandGraphNode::SetHappensBeforeDependency(mNodes[i], nextBarrier); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 816 | } // namespace vk |
| 817 | } // namespace rx |