| 1 |
/* |
| 2 |
* Copyright 2023 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/compute/ComputeStep.h" |
| 9 |
|
| 10 |
#include "include/core/SkColorType.h" |
| 11 |
#include "include/core/SkSize.h" |
| 12 |
#include "include/core/SkTileMode.h" |
| 13 |
#include "include/private/base/SkAssert.h" |
| 14 |
#include "include/private/base/SkDebug.h" |
| 15 |
|
| 16 |
#include <atomic> |
| 17 |
#include <unordered_set> |
| 18 |
|
| 19 |
namespace skgpu::graphite { |
| 20 |
namespace { |
| 21 |
|
| 22 |
static uint32_t next_id() { |
| 23 |
static std::atomic<uint32_t> nextId{0}; |
| 24 |
// Not worried about overflow since a Context isn't expected to have that many ComputeSteps. |
| 25 |
// Even if this it wraps around to 0, that ComputeStep will not be in the same Context as the |
| 26 |
// original 0. |
| 27 |
return nextId.fetch_add(1, std::memory_order_relaxed); |
| 28 |
} |
| 29 |
|
| 30 |
} // namespace |
| 31 |
|
| 32 |
ComputeStep::ComputeStep(std::string_view name, |
| 33 |
WorkgroupSize localDispatchSize, |
| 34 |
SkSpan<const ResourceDesc> resources, |
| 35 |
SkSpan<const WorkgroupBufferDesc> workgroupBuffers, |
| 36 |
Flags baseFlags) |
| 37 |
: fUniqueID(next_id()) |
| 38 |
, fFlags(baseFlags) |
| 39 |
, fName(name) |
| 40 |
, fResources(resources.data(), resources.size()) |
| 41 |
, fWorkgroupBuffers(workgroupBuffers.data(), workgroupBuffers.size()) |
| 42 |
, fLocalDispatchSize(localDispatchSize) { |
| 43 |
#ifdef SK_DEBUG |
| 44 |
std::unordered_set<int> slots; |
| 45 |
for (const ResourceDesc& r : fResources) { |
| 46 |
// Validate that slot assignments within a ComputeStep are unique. |
| 47 |
if (r.fFlow == DataFlow::kShared) { |
| 48 |
SkASSERT(r.fSlot > -1); |
| 49 |
SkASSERT(r.fSlot < kMaxComputeDataFlowSlots); |
| 50 |
auto [_, inserted] = slots.insert(r.fSlot); |
| 51 |
SkASSERT(inserted); |
| 52 |
} |
| 53 |
} |
| 54 |
#endif // SK_DEBUG |
| 55 |
} |
| 56 |
|
| 57 |
void ComputeStep::prepareStorageBuffer(int, const ResourceDesc&, BufferWriter&&) const { |
| 58 |
SK_ABORT("ComputeSteps that initialize a mapped storage buffer must override " |
| 59 |
"prepareStorageBuffer()"); |
| 60 |
} |
| 61 |
|
| 62 |
void ComputeStep::prepareUniformBuffer(int, const ResourceDesc&, UniformManager*) const { |
| 63 |
SK_ABORT("ComputeSteps that initialize a uniform buffer must override prepareUniformBuffer()"); |
| 64 |
} |
| 65 |
|
| 66 |
std::string ComputeStep::computeSkSL() const { |
| 67 |
SK_ABORT("ComputeSteps must override computeSkSL() unless they support native shader source"); |
| 68 |
} |
| 69 |
|
| 70 |
ComputeStep::NativeShaderSource ComputeStep::nativeShaderSource(NativeShaderFormat) const { |
| 71 |
SK_ABORT("ComputeSteps that support native shader source must override nativeShaderSource()"); |
| 72 |
} |
| 73 |
|
| 74 |
size_t ComputeStep::calculateBufferSize(int, const ResourceDesc&) const { |
| 75 |
SK_ABORT("ComputeSteps that initialize a storage buffer must override calculateBufferSize()"); |
| 76 |
} |
| 77 |
|
| 78 |
std::tuple<SkISize, SkColorType> ComputeStep::calculateTextureParameters( |
| 79 |
int, const ResourceDesc&) const { |
| 80 |
SK_ABORT("ComputeSteps that initialize a texture must override calculateTextureParameters()"); |
| 81 |
} |
| 82 |
|
| 83 |
SamplerDesc ComputeStep::calculateSamplerParameters(int resourceIndex, const ResourceDesc&) const { |
| 84 |
SK_ABORT("ComputeSteps that initialize a sampler must override calculateSamplerParameters()"); |
| 85 |
} |
| 86 |
|
| 87 |
WorkgroupSize ComputeStep::calculateGlobalDispatchSize() const { |
| 88 |
SK_ABORT("ComputeSteps must override calculateGlobalDispatchSize() unless " |
| 89 |
"the workgroup count is determined out-of-band"); |
| 90 |
} |
| 91 |
|
| 92 |
} // namespace skgpu::graphite |