| 1 |
/* |
| 2 |
* Copyright 2022 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/graphite/task/ComputeTask.h" |
| 9 |
|
| 10 |
#include "include/private/base/SkAssert.h" |
| 11 |
#include "src/gpu/graphite/CommandBuffer.h" |
| 12 |
#include "src/gpu/graphite/ComputePipeline.h" // IWYU pragma: keep |
| 13 |
#include "src/gpu/graphite/Sampler.h" // IWYU pragma: keep |
| 14 |
#include "src/gpu/graphite/TextureProxy.h" // IWYU pragma: keep |
| 15 |
#include "src/gpu/graphite/compute/DispatchGroup.h" |
| 16 |
|
| 17 |
#include <cstddef> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
namespace skgpu::graphite { |
| 21 |
|
| 22 |
sk_sp<ComputeTask> ComputeTask::Make(DispatchGroupList dispatchGroups) { |
| 23 |
return sk_sp<ComputeTask>(new ComputeTask(std::move(dispatchGroups))); |
| 24 |
} |
| 25 |
|
| 26 |
ComputeTask::ComputeTask(DispatchGroupList dispatchGroups) |
| 27 |
: fDispatchGroups(std::move(dispatchGroups)), fChildTasks(fDispatchGroups.size()) { |
| 28 |
for (auto& group : fDispatchGroups) { |
| 29 |
fChildTasks.push_back(group->snapChildTask()); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
ComputeTask::~ComputeTask() = default; |
| 34 |
|
| 35 |
Task::Status ComputeTask::prepareResources(ResourceProvider* provider, |
| 36 |
ScratchResourceManager* scratchManager, |
| 37 |
sk_sp<const RuntimeEffectDictionary> rtd) { |
| 38 |
for (auto& child : fChildTasks) { |
| 39 |
if (child) { |
| 40 |
Status status = child->prepareResources(provider, scratchManager, rtd); |
| 41 |
if (status == Status::kFail) { |
| 42 |
return Status::kFail; |
| 43 |
} else if (status == Status::kDiscard) { |
| 44 |
child.reset(); |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
for (const auto& group : fDispatchGroups) { |
| 49 |
// TODO: Allow ComputeTasks to instantiate with scratch textures and return them. |
| 50 |
if (!group->prepareResources(provider)) { |
| 51 |
return Status::kFail; |
| 52 |
} |
| 53 |
} |
| 54 |
return Status::kSuccess; |
| 55 |
} |
| 56 |
|
| 57 |
Task::Status ComputeTask::addCommands(Context* ctx, |
| 58 |
CommandBuffer* commandBuffer, |
| 59 |
ReplayTargetData rtd) { |
| 60 |
if (fDispatchGroups.empty()) { |
| 61 |
return Status::kDiscard; |
| 62 |
} |
| 63 |
SkASSERT(fDispatchGroups.size() == fChildTasks.size()); |
| 64 |
const std::unique_ptr<DispatchGroup>* currentSpanPtr = &fDispatchGroups[0]; |
| 65 |
size_t currentSpanSize = 0u; |
| 66 |
for (int i = 0; i < fDispatchGroups.size(); ++i) { |
| 67 |
// If the next DispatchGroup has a dependent task, then encode the accumulated span as a |
| 68 |
// compute pass now. CommandBuffer encodes each compute pass with a separate encoder, so |
| 69 |
// the dependent task can use a non-compute encoder if needed. |
| 70 |
Task* child = fChildTasks[i].get(); |
| 71 |
if (child) { |
| 72 |
if (currentSpanSize > 0u) { |
| 73 |
if (!commandBuffer->addComputePass({currentSpanPtr, currentSpanSize})) { |
| 74 |
return Status::kFail; |
| 75 |
} |
| 76 |
currentSpanPtr = &fDispatchGroups[i]; |
| 77 |
currentSpanSize = 0u; |
| 78 |
} |
| 79 |
|
| 80 |
Status status = child->addCommands(ctx, commandBuffer, rtd); |
| 81 |
if (status == Status::kFail) { |
| 82 |
return Status::kFail; |
| 83 |
} else if (status == Status::kDiscard) { |
| 84 |
fChildTasks[i].reset(); |
| 85 |
} |
| 86 |
} |
| 87 |
currentSpanSize++; |
| 88 |
} |
| 89 |
return (currentSpanSize == 0u || |
| 90 |
commandBuffer->addComputePass({currentSpanPtr, currentSpanSize})) ? Status::kSuccess |
| 91 |
: Status::kFail; |
| 92 |
} |
| 93 |
|
| 94 |
} // namespace skgpu::graphite |