| 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/dawn/DawnSampler.h" |
| 9 |
|
| 10 |
#include "include/core/SkSamplingOptions.h" |
| 11 |
#include "src/gpu/graphite/dawn/DawnCaps.h" |
| 12 |
#include "src/gpu/graphite/dawn/DawnGraphiteUtils.h" |
| 13 |
#include "src/gpu/graphite/dawn/DawnSharedContext.h" |
| 14 |
|
| 15 |
#include <cfloat> |
| 16 |
|
| 17 |
namespace skgpu::graphite { |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
wgpu::FilterMode filter_mode_to_dawn_filter_mode(SkFilterMode mode) { |
| 22 |
switch (mode) { |
| 23 |
case SkFilterMode::kNearest: |
| 24 |
return wgpu::FilterMode::Nearest; |
| 25 |
case SkFilterMode::kLinear: |
| 26 |
return wgpu::FilterMode::Linear; |
| 27 |
} |
| 28 |
SkUNREACHABLE; |
| 29 |
} |
| 30 |
|
| 31 |
wgpu::MipmapFilterMode mipmap_mode_to_dawn_filter_mode(SkMipmapMode mode) { |
| 32 |
switch (mode) { |
| 33 |
case SkMipmapMode::kNone: |
| 34 |
// Dawn doesn't have none filter mode. |
| 35 |
return wgpu::MipmapFilterMode::Nearest; |
| 36 |
case SkMipmapMode::kNearest: |
| 37 |
return wgpu::MipmapFilterMode::Nearest; |
| 38 |
case SkMipmapMode::kLinear: |
| 39 |
return wgpu::MipmapFilterMode::Linear; |
| 40 |
} |
| 41 |
SkUNREACHABLE; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
DawnSampler::DawnSampler(const DawnSharedContext* sharedContext, |
| 46 |
SamplerDesc samplerDesc, |
| 47 |
wgpu::Sampler sampler) |
| 48 |
: Sampler(sharedContext) |
| 49 |
, fSampler(std::move(sampler)) |
| 50 |
, fSamplerDesc(samplerDesc) {} |
| 51 |
|
| 52 |
static inline std::pair<wgpu::AddressMode, wgpu::AddressMode> |
| 53 |
tile_modes_to_dawn_address_modes(const SamplerDesc& samplerDesc) { |
| 54 |
auto to_dawn_mode = [](SkTileMode tm) -> wgpu::AddressMode { |
| 55 |
switch (tm) { |
| 56 |
case SkTileMode::kClamp: |
| 57 |
return wgpu::AddressMode::ClampToEdge; |
| 58 |
case SkTileMode::kRepeat: |
| 59 |
return wgpu::AddressMode::Repeat; |
| 60 |
case SkTileMode::kMirror: |
| 61 |
return wgpu::AddressMode::MirrorRepeat; |
| 62 |
case SkTileMode::kDecal: |
| 63 |
// Dawn doesn't support kDecal; considered an error if we reach this point. |
| 64 |
SkASSERT(false); |
| 65 |
return {}; |
| 66 |
} |
| 67 |
SkUNREACHABLE; |
| 68 |
}; |
| 69 |
|
| 70 |
return {to_dawn_mode(samplerDesc.tileModeX()), to_dawn_mode(samplerDesc.tileModeY())}; |
| 71 |
} |
| 72 |
|
| 73 |
sk_sp<DawnSampler> DawnSampler::Make(const DawnSharedContext* sharedContext, |
| 74 |
SamplerDesc samplerDesc) { |
| 75 |
wgpu::SamplerDescriptor desc; |
| 76 |
const SkSamplingOptions& samplingOptions = samplerDesc.samplingOptions(); |
| 77 |
std::tie(desc.addressModeU, desc.addressModeV) = tile_modes_to_dawn_address_modes(samplerDesc); |
| 78 |
desc.magFilter = filter_mode_to_dawn_filter_mode(samplingOptions.filter); |
| 79 |
desc.minFilter = desc.magFilter; |
| 80 |
desc.mipmapFilter = mipmap_mode_to_dawn_filter_mode(samplingOptions.mipmap); |
| 81 |
desc.lodMinClamp = 0.0f; |
| 82 |
if (samplingOptions.mipmap == SkMipmapMode::kNone) { |
| 83 |
// Disabling mipmap by clamping max lod to first level only. |
| 84 |
desc.lodMaxClamp = 0.0f; |
| 85 |
} else { |
| 86 |
desc.lodMaxClamp = FLT_MAX; |
| 87 |
} |
| 88 |
desc.maxAnisotropy = 1; |
| 89 |
desc.compare = wgpu::CompareFunction::Undefined; |
| 90 |
|
| 91 |
#if !defined(__EMSCRIPTEN__) |
| 92 |
wgpu::YCbCrVkDescriptor ycbcrDescriptor; |
| 93 |
if (samplerDesc.isImmutable()) { |
| 94 |
ycbcrDescriptor = |
| 95 |
DawnDescriptorFromImmutableSamplerInfo(samplerDesc.immutableSamplerInfo()); |
| 96 |
desc.nextInChain = &ycbcrDescriptor; |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
std::string label; |
| 101 |
if (sharedContext->caps()->setBackendLabels()) { |
| 102 |
static const char* tileModeLabels[] = {"Clamp", "Repeat", "Mirror", "Decal"}; |
| 103 |
static const char* minMagFilterLabels[] = {"Nearest", "Linear"}; |
| 104 |
static const char* mipFilterLabels[] = {"MipNone", "MipNearest", "MipLinear"}; |
| 105 |
label.append("X").append(tileModeLabels[static_cast<int>(samplerDesc.tileModeX())]); |
| 106 |
label.append("Y").append(tileModeLabels[static_cast<int>(samplerDesc.tileModeY())]); |
| 107 |
label.append(minMagFilterLabels[static_cast<int>(samplingOptions.filter)]); |
| 108 |
label.append(mipFilterLabels[static_cast<int>(samplingOptions.mipmap)]); |
| 109 |
#if !defined(__EMSCRIPTEN__) |
| 110 |
if (DawnDescriptorIsValid(ycbcrDescriptor)) { |
| 111 |
label.append("YCbCr"); |
| 112 |
|
| 113 |
if (DawnDescriptorUsesExternalFormat(ycbcrDescriptor)) { |
| 114 |
label.append("ExternalFormat"); |
| 115 |
label.append(std::to_string(ycbcrDescriptor.externalFormat)); |
| 116 |
} else { |
| 117 |
label.append("KnownFormat").append(std::to_string(ycbcrDescriptor.vkFormat)); |
| 118 |
} |
| 119 |
|
| 120 |
label.append("Model").append(std::to_string(ycbcrDescriptor.vkYCbCrModel)); |
| 121 |
label.append("Range").append(std::to_string(ycbcrDescriptor.vkYCbCrRange)); |
| 122 |
|
| 123 |
label.append("ComponentSwizzleRGBA"); |
| 124 |
label.append(std::to_string(ycbcrDescriptor.vkComponentSwizzleRed)); |
| 125 |
label.append(std::to_string(ycbcrDescriptor.vkComponentSwizzleGreen)); |
| 126 |
label.append(std::to_string(ycbcrDescriptor.vkComponentSwizzleBlue)); |
| 127 |
label.append(std::to_string(ycbcrDescriptor.vkComponentSwizzleAlpha)); |
| 128 |
|
| 129 |
label.append("ChromaOffset"); |
| 130 |
label.append("X").append(std::to_string(ycbcrDescriptor.vkXChromaOffset)); |
| 131 |
label.append("Y").append(std::to_string(ycbcrDescriptor.vkYChromaOffset)); |
| 132 |
|
| 133 |
static const char* chromaFilterLabels[] = { |
| 134 |
"WGPU_Undefined", "WGPU_Nearest", "WGPU_Linear"}; |
| 135 |
label.append("ChromaFilter"); |
| 136 |
label.append(chromaFilterLabels[static_cast<int>(ycbcrDescriptor.vkChromaFilter)]); |
| 137 |
|
| 138 |
label.append("ForceExplicitReconstruct"); |
| 139 |
label.append(std::to_string(ycbcrDescriptor.forceExplicitReconstruction)); |
| 140 |
} |
| 141 |
#endif |
| 142 |
desc.label = label.c_str(); |
| 143 |
} |
| 144 |
|
| 145 |
auto sampler = sharedContext->device().CreateSampler(&desc); |
| 146 |
if (!sampler) { |
| 147 |
return {}; |
| 148 |
} |
| 149 |
return sk_sp<DawnSampler>(new DawnSampler(sharedContext, samplerDesc, std::move(sampler))); |
| 150 |
} |
| 151 |
|
| 152 |
void DawnSampler::freeGpuData() { |
| 153 |
fSampler = nullptr; |
| 154 |
} |
| 155 |
|
| 156 |
} // namespace skgpu::graphite |