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