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 | |
| 19 | namespace rx |
| 20 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 21 | namespace vk |
| 22 | { |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 23 | namespace |
| 24 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 25 | angle::Result InitAndBeginCommandBuffer(vk::Context *context, |
| 26 | const CommandPool &commandPool, |
| 27 | const VkCommandBufferInheritanceInfo &inheritanceInfo, |
| 28 | VkCommandBufferUsageFlags flags, |
| 29 | CommandBuffer *commandBuffer) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 30 | { |
| 31 | ASSERT(!commandBuffer->valid()); |
| 32 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 33 | VkCommandBufferAllocateInfo createInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 34 | createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 35 | createInfo.commandPool = commandPool.getHandle(); |
| 36 | createInfo.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY; |
| 37 | createInfo.commandBufferCount = 1; |
| 38 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 39 | ANGLE_TRY(commandBuffer->init(context, createInfo)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 40 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 41 | VkCommandBufferBeginInfo beginInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 42 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 43 | beginInfo.flags = flags | VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 44 | beginInfo.pInheritanceInfo = &inheritanceInfo; |
| 45 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 46 | ANGLE_TRY(commandBuffer->begin(context, beginInfo)); |
| 47 | return angle::Result::Continue(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 50 | const char *GetResourceTypeName(CommandGraphResourceType resourceType) |
| 51 | { |
| 52 | switch (resourceType) |
| 53 | { |
| 54 | case CommandGraphResourceType::Buffer: |
| 55 | return "Buffer"; |
| 56 | case CommandGraphResourceType::Framebuffer: |
| 57 | return "Framebuffer"; |
| 58 | case CommandGraphResourceType::Image: |
| 59 | return "Image"; |
| 60 | default: |
| 61 | UNREACHABLE(); |
| 62 | return ""; |
| 63 | } |
| 64 | } |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 65 | } // anonymous namespace |
| 66 | |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 67 | // CommandGraphResource implementation. |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 68 | CommandGraphResource::CommandGraphResource(CommandGraphResourceType resourceType) |
| 69 | : mCurrentWritingNode(nullptr), mResourceType(resourceType) |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 73 | CommandGraphResource::~CommandGraphResource() = default; |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 74 | |
| 75 | void CommandGraphResource::updateQueueSerial(Serial queueSerial) |
| 76 | { |
| 77 | ASSERT(queueSerial >= mStoredQueueSerial); |
| 78 | |
| 79 | if (queueSerial > mStoredQueueSerial) |
| 80 | { |
| 81 | mCurrentWritingNode = nullptr; |
| 82 | mCurrentReadingNodes.clear(); |
| 83 | mStoredQueueSerial = queueSerial; |
| 84 | } |
| 85 | } |
| 86 | |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 87 | bool CommandGraphResource::isResourceInUse(RendererVk *renderer) const |
| 88 | { |
| 89 | return renderer->isSerialInUse(mStoredQueueSerial); |
| 90 | } |
| 91 | |
| 92 | Serial CommandGraphResource::getStoredQueueSerial() const |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 93 | { |
| 94 | return mStoredQueueSerial; |
| 95 | } |
| 96 | |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 97 | angle::Result CommandGraphResource::recordCommands(Context *context, |
| 98 | CommandBuffer **commandBufferOut) |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 99 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 100 | updateQueueSerial(context->getRenderer()->getCurrentQueueSerial()); |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 101 | |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 102 | if (!hasChildlessWritingNode() || hasStartedRenderPass()) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 103 | { |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 104 | finishCurrentCommands(context->getRenderer()); |
| 105 | return mCurrentWritingNode->beginOutsideRenderPassRecording( |
| 106 | context, context->getRenderer()->getCommandPool(), commandBufferOut); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | CommandBuffer *outsideRenderPassCommands = mCurrentWritingNode->getOutsideRenderPassCommands(); |
| 110 | if (!outsideRenderPassCommands->valid()) |
| 111 | { |
| 112 | ANGLE_TRY(mCurrentWritingNode->beginOutsideRenderPassRecording( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 113 | context, context->getRenderer()->getCommandPool(), commandBufferOut)); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 114 | } |
| 115 | else |
| 116 | { |
| 117 | *commandBufferOut = outsideRenderPassCommands; |
| 118 | } |
| 119 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 120 | return angle::Result::Continue(); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 121 | } |
| 122 | |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 123 | bool CommandGraphResource::appendToStartedRenderPass(RendererVk *renderer, |
| 124 | CommandBuffer **commandBufferOut) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 125 | { |
Jamie Madill | 5dca651 | 2018-05-30 10:53:51 -0400 | [diff] [blame] | 126 | updateQueueSerial(renderer->getCurrentQueueSerial()); |
| 127 | if (hasStartedRenderPass()) |
| 128 | { |
| 129 | *commandBufferOut = mCurrentWritingNode->getInsideRenderPassCommands(); |
| 130 | return true; |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | return false; |
| 135 | } |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 138 | const gl::Rectangle &CommandGraphResource::getRenderPassRenderArea() const |
| 139 | { |
| 140 | ASSERT(hasStartedRenderPass()); |
| 141 | return mCurrentWritingNode->getRenderPassRenderArea(); |
| 142 | } |
| 143 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 144 | angle::Result CommandGraphResource::beginRenderPass(Context *context, |
| 145 | const Framebuffer &framebuffer, |
| 146 | const gl::Rectangle &renderArea, |
| 147 | const RenderPassDesc &renderPassDesc, |
| 148 | const std::vector<VkClearValue> &clearValues, |
| 149 | CommandBuffer **commandBufferOut) const |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 150 | { |
| 151 | // Hard-code RenderPass to clear the first render target to the current clear value. |
| 152 | // TODO(jmadill): Proper clear value implementation. http://anglebug.com/2361 |
| 153 | mCurrentWritingNode->storeRenderPassInfo(framebuffer, renderArea, renderPassDesc, clearValues); |
| 154 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 155 | return mCurrentWritingNode->beginInsideRenderPassRecording(context, commandBufferOut); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 156 | } |
| 157 | |
Jamie Madill | e2d2270 | 2018-09-19 08:11:48 -0400 | [diff] [blame] | 158 | void CommandGraphResource::finishCurrentCommands(RendererVk *renderer) |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 159 | { |
Jamie Madill | 86ce210 | 2018-05-22 11:54:42 -0400 | [diff] [blame] | 160 | CommandGraphNode *newCommands = renderer->getCommandGraph()->allocateNode(); |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 161 | newCommands->setDiagnosticInfo(mResourceType, reinterpret_cast<uintptr_t>(this)); |
Jamie Madill | 86ce210 | 2018-05-22 11:54:42 -0400 | [diff] [blame] | 162 | onWriteImpl(newCommands, renderer->getCurrentQueueSerial()); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void CommandGraphResource::addWriteDependency(CommandGraphResource *writingResource) |
| 166 | { |
| 167 | CommandGraphNode *writingNode = writingResource->mCurrentWritingNode; |
| 168 | ASSERT(writingNode); |
| 169 | |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 170 | onWriteImpl(writingNode, writingResource->getStoredQueueSerial()); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void CommandGraphResource::onWriteImpl(CommandGraphNode *writingNode, Serial currentSerial) |
| 174 | { |
| 175 | updateQueueSerial(currentSerial); |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 176 | |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 177 | // Make sure any open reads and writes finish before we execute 'writingNode'. |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 178 | if (!mCurrentReadingNodes.empty()) |
| 179 | { |
| 180 | CommandGraphNode::SetHappensBeforeDependencies(mCurrentReadingNodes, writingNode); |
| 181 | mCurrentReadingNodes.clear(); |
| 182 | } |
| 183 | |
| 184 | if (mCurrentWritingNode && mCurrentWritingNode != writingNode) |
| 185 | { |
| 186 | CommandGraphNode::SetHappensBeforeDependency(mCurrentWritingNode, writingNode); |
| 187 | } |
| 188 | |
| 189 | mCurrentWritingNode = writingNode; |
| 190 | } |
| 191 | |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 192 | void CommandGraphResource::addReadDependency(CommandGraphResource *readingResource) |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 193 | { |
Jamie Madill | c57ee25 | 2018-05-30 19:53:48 -0400 | [diff] [blame] | 194 | updateQueueSerial(readingResource->getStoredQueueSerial()); |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 195 | |
| 196 | CommandGraphNode *readingNode = readingResource->mCurrentWritingNode; |
| 197 | ASSERT(readingNode); |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 198 | |
| 199 | if (hasChildlessWritingNode()) |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 200 | { |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 201 | // Ensure 'readingNode' happens after the current writing node. |
| 202 | CommandGraphNode::SetHappensBeforeDependency(mCurrentWritingNode, readingNode); |
| 203 | } |
| 204 | |
| 205 | // Add the read node to the list of nodes currently reading this resource. |
| 206 | mCurrentReadingNodes.push_back(readingNode); |
| 207 | } |
| 208 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 209 | // CommandGraphNode implementation. |
Jamie Madill | 316c606 | 2018-05-29 10:49:45 -0400 | [diff] [blame] | 210 | CommandGraphNode::CommandGraphNode() |
| 211 | : mRenderPassClearValues{}, mHasChildren(false), mVisitedState(VisitedState::Unvisited) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 212 | { |
| 213 | } |
| 214 | |
| 215 | CommandGraphNode::~CommandGraphNode() |
| 216 | { |
| 217 | mRenderPassFramebuffer.setHandle(VK_NULL_HANDLE); |
| 218 | |
| 219 | // Command buffers are managed by the command pool, so don't need to be freed. |
| 220 | mOutsideRenderPassCommands.releaseHandle(); |
| 221 | mInsideRenderPassCommands.releaseHandle(); |
| 222 | } |
| 223 | |
| 224 | CommandBuffer *CommandGraphNode::getOutsideRenderPassCommands() |
| 225 | { |
| 226 | ASSERT(!mHasChildren); |
| 227 | return &mOutsideRenderPassCommands; |
| 228 | } |
| 229 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 230 | angle::Result CommandGraphNode::beginOutsideRenderPassRecording(Context *context, |
| 231 | const CommandPool &commandPool, |
| 232 | CommandBuffer **commandsOut) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 233 | { |
| 234 | ASSERT(!mHasChildren); |
| 235 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 236 | VkCommandBufferInheritanceInfo inheritanceInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 237 | inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 238 | inheritanceInfo.renderPass = VK_NULL_HANDLE; |
| 239 | inheritanceInfo.subpass = 0; |
| 240 | inheritanceInfo.framebuffer = VK_NULL_HANDLE; |
| 241 | inheritanceInfo.occlusionQueryEnable = VK_FALSE; |
| 242 | inheritanceInfo.queryFlags = 0; |
| 243 | inheritanceInfo.pipelineStatistics = 0; |
| 244 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 245 | ANGLE_TRY(InitAndBeginCommandBuffer(context, commandPool, inheritanceInfo, 0, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 246 | &mOutsideRenderPassCommands)); |
| 247 | |
| 248 | *commandsOut = &mOutsideRenderPassCommands; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 249 | return angle::Result::Continue(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 252 | angle::Result CommandGraphNode::beginInsideRenderPassRecording(Context *context, |
| 253 | CommandBuffer **commandsOut) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 254 | { |
| 255 | ASSERT(!mHasChildren); |
| 256 | |
| 257 | // Get a compatible RenderPass from the cache so we can initialize the inheritance info. |
| 258 | // TODO(jmadill): Support query for compatible/conformant render pass. htto://anglebug.com/2361 |
| 259 | RenderPass *compatibleRenderPass; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 260 | ANGLE_TRY(context->getRenderer()->getCompatibleRenderPass(context, mRenderPassDesc, |
| 261 | &compatibleRenderPass)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 262 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 263 | VkCommandBufferInheritanceInfo inheritanceInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 264 | inheritanceInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 265 | inheritanceInfo.renderPass = compatibleRenderPass->getHandle(); |
| 266 | inheritanceInfo.subpass = 0; |
| 267 | inheritanceInfo.framebuffer = mRenderPassFramebuffer.getHandle(); |
| 268 | inheritanceInfo.occlusionQueryEnable = VK_FALSE; |
| 269 | inheritanceInfo.queryFlags = 0; |
| 270 | inheritanceInfo.pipelineStatistics = 0; |
| 271 | |
| 272 | ANGLE_TRY(InitAndBeginCommandBuffer( |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 273 | context, context->getRenderer()->getCommandPool(), inheritanceInfo, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 274 | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &mInsideRenderPassCommands)); |
| 275 | |
| 276 | *commandsOut = &mInsideRenderPassCommands; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 277 | return angle::Result::Continue(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void CommandGraphNode::storeRenderPassInfo(const Framebuffer &framebuffer, |
| 281 | const gl::Rectangle renderArea, |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 282 | const vk::RenderPassDesc &renderPassDesc, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 283 | const std::vector<VkClearValue> &clearValues) |
| 284 | { |
Jamie Madill | bcf467f | 2018-05-23 09:46:00 -0400 | [diff] [blame] | 285 | mRenderPassDesc = renderPassDesc; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 286 | mRenderPassFramebuffer.setHandle(framebuffer.getHandle()); |
| 287 | mRenderPassRenderArea = renderArea; |
| 288 | std::copy(clearValues.begin(), clearValues.end(), mRenderPassClearValues.begin()); |
| 289 | } |
| 290 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 291 | // static |
| 292 | void CommandGraphNode::SetHappensBeforeDependency(CommandGraphNode *beforeNode, |
| 293 | CommandGraphNode *afterNode) |
| 294 | { |
Jamie Madill | 9cceac4 | 2018-03-31 14:19:16 -0400 | [diff] [blame] | 295 | ASSERT(beforeNode != afterNode && !beforeNode->isChildOf(afterNode)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 296 | afterNode->mParents.emplace_back(beforeNode); |
| 297 | beforeNode->setHasChildren(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // static |
| 301 | void CommandGraphNode::SetHappensBeforeDependencies( |
| 302 | const std::vector<CommandGraphNode *> &beforeNodes, |
| 303 | CommandGraphNode *afterNode) |
| 304 | { |
| 305 | afterNode->mParents.insert(afterNode->mParents.end(), beforeNodes.begin(), beforeNodes.end()); |
| 306 | |
| 307 | // TODO(jmadill): is there a faster way to do this? |
| 308 | for (CommandGraphNode *beforeNode : beforeNodes) |
| 309 | { |
| 310 | beforeNode->setHasChildren(); |
| 311 | |
| 312 | ASSERT(beforeNode != afterNode && !beforeNode->isChildOf(afterNode)); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | bool CommandGraphNode::hasParents() const |
| 317 | { |
| 318 | return !mParents.empty(); |
| 319 | } |
| 320 | |
| 321 | void CommandGraphNode::setHasChildren() |
| 322 | { |
| 323 | mHasChildren = true; |
| 324 | } |
| 325 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 326 | // Do not call this in anything but testing code, since it's slow. |
| 327 | bool CommandGraphNode::isChildOf(CommandGraphNode *parent) |
| 328 | { |
| 329 | std::set<CommandGraphNode *> visitedList; |
| 330 | std::vector<CommandGraphNode *> openList; |
| 331 | openList.insert(openList.begin(), mParents.begin(), mParents.end()); |
| 332 | while (!openList.empty()) |
| 333 | { |
| 334 | CommandGraphNode *current = openList.back(); |
| 335 | openList.pop_back(); |
| 336 | if (visitedList.count(current) == 0) |
| 337 | { |
| 338 | if (current == parent) |
| 339 | { |
| 340 | return true; |
| 341 | } |
| 342 | visitedList.insert(current); |
| 343 | openList.insert(openList.end(), current->mParents.begin(), current->mParents.end()); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | VisitedState CommandGraphNode::visitedState() const |
| 351 | { |
| 352 | return mVisitedState; |
| 353 | } |
| 354 | |
| 355 | void CommandGraphNode::visitParents(std::vector<CommandGraphNode *> *stack) |
| 356 | { |
| 357 | ASSERT(mVisitedState == VisitedState::Unvisited); |
| 358 | stack->insert(stack->end(), mParents.begin(), mParents.end()); |
| 359 | mVisitedState = VisitedState::Ready; |
| 360 | } |
| 361 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 362 | angle::Result CommandGraphNode::visitAndExecute(vk::Context *context, |
| 363 | Serial serial, |
| 364 | RenderPassCache *renderPassCache, |
| 365 | CommandBuffer *primaryCommandBuffer) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 366 | { |
| 367 | if (mOutsideRenderPassCommands.valid()) |
| 368 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 369 | ANGLE_TRY(mOutsideRenderPassCommands.end(context)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 370 | primaryCommandBuffer->executeCommands(1, &mOutsideRenderPassCommands); |
| 371 | } |
| 372 | |
| 373 | if (mInsideRenderPassCommands.valid()) |
| 374 | { |
| 375 | // Pull a compatible RenderPass from the cache. |
| 376 | // TODO(jmadill): Insert real ops and layout transitions. |
| 377 | RenderPass *renderPass = nullptr; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 378 | ANGLE_TRY(renderPassCache->getCompatibleRenderPass(context, serial, mRenderPassDesc, |
| 379 | &renderPass)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 380 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 381 | ANGLE_TRY(mInsideRenderPassCommands.end(context)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 382 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 383 | VkRenderPassBeginInfo beginInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 384 | beginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 385 | beginInfo.renderPass = renderPass->getHandle(); |
| 386 | beginInfo.framebuffer = mRenderPassFramebuffer.getHandle(); |
| 387 | beginInfo.renderArea.offset.x = static_cast<uint32_t>(mRenderPassRenderArea.x); |
| 388 | beginInfo.renderArea.offset.y = static_cast<uint32_t>(mRenderPassRenderArea.y); |
| 389 | beginInfo.renderArea.extent.width = static_cast<uint32_t>(mRenderPassRenderArea.width); |
| 390 | beginInfo.renderArea.extent.height = static_cast<uint32_t>(mRenderPassRenderArea.height); |
| 391 | beginInfo.clearValueCount = mRenderPassDesc.attachmentCount(); |
| 392 | beginInfo.pClearValues = mRenderPassClearValues.data(); |
| 393 | |
| 394 | primaryCommandBuffer->beginRenderPass(beginInfo, |
| 395 | VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS); |
| 396 | primaryCommandBuffer->executeCommands(1, &mInsideRenderPassCommands); |
| 397 | primaryCommandBuffer->endRenderPass(); |
| 398 | } |
| 399 | |
| 400 | mVisitedState = VisitedState::Visited; |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 401 | return angle::Result::Continue(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 402 | } |
| 403 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 404 | const std::vector<CommandGraphNode *> &CommandGraphNode::getParentsForDiagnostics() const |
| 405 | { |
| 406 | return mParents; |
| 407 | } |
| 408 | |
| 409 | void CommandGraphNode::setDiagnosticInfo(CommandGraphResourceType resourceType, |
| 410 | uintptr_t resourceID) |
| 411 | { |
| 412 | mResourceType = resourceType; |
| 413 | mResourceID = resourceID; |
| 414 | } |
| 415 | |
Luc Ferron | 14f4817 | 2018-04-11 08:43:28 -0400 | [diff] [blame] | 416 | const gl::Rectangle &CommandGraphNode::getRenderPassRenderArea() const |
| 417 | { |
| 418 | return mRenderPassRenderArea; |
| 419 | } |
| 420 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 421 | // CommandGraph implementation. |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 422 | CommandGraph::CommandGraph(bool enableGraphDiagnostics) |
| 423 | : mEnableGraphDiagnostics(enableGraphDiagnostics) |
| 424 | { |
| 425 | } |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 426 | |
| 427 | CommandGraph::~CommandGraph() |
| 428 | { |
| 429 | ASSERT(empty()); |
| 430 | } |
| 431 | |
| 432 | CommandGraphNode *CommandGraph::allocateNode() |
| 433 | { |
| 434 | // TODO(jmadill): Use a pool allocator for the CPU node allocations. |
| 435 | CommandGraphNode *newCommands = new CommandGraphNode(); |
| 436 | mNodes.emplace_back(newCommands); |
| 437 | return newCommands; |
| 438 | } |
| 439 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 440 | angle::Result CommandGraph::submitCommands(Context *context, |
| 441 | Serial serial, |
| 442 | RenderPassCache *renderPassCache, |
| 443 | CommandPool *commandPool, |
| 444 | CommandBuffer *primaryCommandBufferOut) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 445 | { |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 446 | VkCommandBufferAllocateInfo primaryInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 447 | primaryInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 448 | primaryInfo.commandPool = commandPool->getHandle(); |
| 449 | primaryInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 450 | primaryInfo.commandBufferCount = 1; |
| 451 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 452 | ANGLE_TRY(primaryCommandBufferOut->init(context, primaryInfo)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 453 | |
| 454 | if (mNodes.empty()) |
| 455 | { |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 456 | return angle::Result::Continue(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 459 | if (mEnableGraphDiagnostics) |
| 460 | { |
| 461 | dumpGraphDotFile(std::cout); |
| 462 | } |
| 463 | |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 464 | std::vector<CommandGraphNode *> nodeStack; |
| 465 | |
Shahbaz Youssefi | 06270c9 | 2018-10-03 17:00:25 -0400 | [diff] [blame] | 466 | VkCommandBufferBeginInfo beginInfo = {}; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 467 | beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 468 | beginInfo.flags = 0; |
| 469 | beginInfo.pInheritanceInfo = nullptr; |
| 470 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 471 | ANGLE_TRY(primaryCommandBufferOut->begin(context, beginInfo)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 472 | |
| 473 | for (CommandGraphNode *topLevelNode : mNodes) |
| 474 | { |
| 475 | // Only process commands that don't have child commands. The others will be pulled in |
| 476 | // automatically. Also skip commands that have already been visited. |
| 477 | if (topLevelNode->hasChildren() || topLevelNode->visitedState() != VisitedState::Unvisited) |
| 478 | continue; |
| 479 | |
| 480 | nodeStack.push_back(topLevelNode); |
| 481 | |
| 482 | while (!nodeStack.empty()) |
| 483 | { |
| 484 | CommandGraphNode *node = nodeStack.back(); |
| 485 | |
| 486 | switch (node->visitedState()) |
| 487 | { |
| 488 | case VisitedState::Unvisited: |
| 489 | node->visitParents(&nodeStack); |
| 490 | break; |
| 491 | case VisitedState::Ready: |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 492 | ANGLE_TRY(node->visitAndExecute(context, serial, renderPassCache, |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 493 | primaryCommandBufferOut)); |
| 494 | nodeStack.pop_back(); |
| 495 | break; |
| 496 | case VisitedState::Visited: |
| 497 | nodeStack.pop_back(); |
| 498 | break; |
| 499 | default: |
| 500 | UNREACHABLE(); |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 506 | ANGLE_TRY(primaryCommandBufferOut->end(context)); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 507 | |
| 508 | // 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] | 509 | for (CommandGraphNode *node : mNodes) |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 510 | { |
| 511 | delete node; |
| 512 | } |
| 513 | mNodes.clear(); |
| 514 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 515 | return angle::Result::Continue(); |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | bool CommandGraph::empty() const |
| 519 | { |
| 520 | return mNodes.empty(); |
| 521 | } |
| 522 | |
Jamie Madill | 0da73fe | 2018-10-02 09:31:39 -0400 | [diff] [blame] | 523 | // Dumps the command graph into a dot file that works with graphviz. |
| 524 | void CommandGraph::dumpGraphDotFile(std::ostream &out) const |
| 525 | { |
| 526 | // This ID maps a node pointer to a monatonic ID. It allows us to look up parent node IDs. |
| 527 | std::map<const CommandGraphNode *, int> nodeIDMap; |
| 528 | std::map<uintptr_t, int> objectIDMap; |
| 529 | |
| 530 | // Map nodes to ids. |
| 531 | for (size_t nodeIndex = 0; nodeIndex < mNodes.size(); ++nodeIndex) |
| 532 | { |
| 533 | const CommandGraphNode *node = mNodes[nodeIndex]; |
| 534 | nodeIDMap[node] = static_cast<int>(nodeIndex) + 1; |
| 535 | } |
| 536 | |
| 537 | int bufferIDCounter = 1; |
| 538 | int framebufferIDCounter = 1; |
| 539 | int imageIDCounter = 1; |
| 540 | |
| 541 | out << "digraph {" << std::endl; |
| 542 | |
| 543 | for (const CommandGraphNode *node : mNodes) |
| 544 | { |
| 545 | int nodeID = nodeIDMap[node]; |
| 546 | |
| 547 | std::stringstream strstr; |
| 548 | strstr << GetResourceTypeName(node->getResourceTypeForDiagnostics()); |
| 549 | strstr << " "; |
| 550 | |
| 551 | auto it = objectIDMap.find(node->getResourceIDForDiagnostics()); |
| 552 | if (it != objectIDMap.end()) |
| 553 | { |
| 554 | strstr << it->second; |
| 555 | } |
| 556 | else |
| 557 | { |
| 558 | int id = 0; |
| 559 | |
| 560 | switch (node->getResourceTypeForDiagnostics()) |
| 561 | { |
| 562 | case CommandGraphResourceType::Buffer: |
| 563 | id = bufferIDCounter++; |
| 564 | break; |
| 565 | case CommandGraphResourceType::Framebuffer: |
| 566 | id = framebufferIDCounter++; |
| 567 | break; |
| 568 | case CommandGraphResourceType::Image: |
| 569 | id = imageIDCounter++; |
| 570 | break; |
| 571 | default: |
| 572 | UNREACHABLE(); |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | objectIDMap[node->getResourceIDForDiagnostics()] = id; |
| 577 | strstr << id; |
| 578 | } |
| 579 | |
| 580 | const std::string &label = strstr.str(); |
| 581 | out << " " << nodeID << "[label =<" << label << "<BR/> <FONT POINT-SIZE=\"10\">Node ID " |
| 582 | << nodeID << "</FONT>>];" << std::endl; |
| 583 | } |
| 584 | |
| 585 | for (const CommandGraphNode *node : mNodes) |
| 586 | { |
| 587 | int nodeID = nodeIDMap[node]; |
| 588 | |
| 589 | for (const CommandGraphNode *parent : node->getParentsForDiagnostics()) |
| 590 | { |
| 591 | int parentID = nodeIDMap[parent]; |
| 592 | out << " " << parentID << " -> " << nodeID << ";" << std::endl; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | out << "}" << std::endl; |
| 597 | } |
Jamie Madill | 1f46bc1 | 2018-02-20 16:09:43 -0500 | [diff] [blame] | 598 | } // namespace vk |
| 599 | } // namespace rx |