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