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