Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
DawnSampler.cpp
Go to the documentation of this file.
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
9
13
14#include <cfloat>
15
16namespace skgpu::graphite {
17
18namespace {
19
20wgpu::FilterMode filter_mode_to_dawn_filter_mode(SkFilterMode mode) {
21 switch (mode) {
23 return wgpu::FilterMode::Nearest;
25 return wgpu::FilterMode::Linear;
26 }
28}
29
30wgpu::MipmapFilterMode mipmap_mode_to_dawn_filter_mode(SkMipmapMode mode) {
31 switch (mode) {
33 // Dawn doesn't have none filter mode.
34 return wgpu::MipmapFilterMode::Nearest;
36 return wgpu::MipmapFilterMode::Nearest;
38 return wgpu::MipmapFilterMode::Linear;
39 }
41}
42}
43
44DawnSampler::DawnSampler(const DawnSharedContext* sharedContext,
45 wgpu::Sampler sampler)
46 : Sampler(sharedContext)
47 , fSampler(std::move(sampler)) {}
48
49static inline wgpu::AddressMode tile_mode_to_dawn_address_mode(SkTileMode tileMode) {
50 switch (tileMode) {
52 return wgpu::AddressMode::ClampToEdge;
54 return wgpu::AddressMode::Repeat;
56 return wgpu::AddressMode::MirrorRepeat;
58 // Dawn doesn't support this mode.
59 return wgpu::AddressMode::ClampToEdge;
60 }
62}
63
64sk_sp<DawnSampler> DawnSampler::Make(const DawnSharedContext* sharedContext,
65 const SkSamplingOptions& samplingOptions,
66 SkTileMode xTileMode,
67 SkTileMode yTileMode) {
68 wgpu::SamplerDescriptor desc;
69 desc.addressModeU = tile_mode_to_dawn_address_mode(xTileMode);
70 desc.addressModeV = tile_mode_to_dawn_address_mode(yTileMode);
71 desc.magFilter = filter_mode_to_dawn_filter_mode(samplingOptions.filter);
72 desc.minFilter = desc.magFilter;
73 desc.mipmapFilter = mipmap_mode_to_dawn_filter_mode(samplingOptions.mipmap);
74 desc.lodMinClamp = 0.0f;
75 if (samplingOptions.mipmap == SkMipmapMode::kNone) {
76 // Disabling mipmap by clamping max lod to first level only.
77 desc.lodMaxClamp = 0.0f;
78 } else {
79 desc.lodMaxClamp = FLT_MAX;
80 }
81 desc.maxAnisotropy = 1;
82 desc.compare = wgpu::CompareFunction::Undefined;
83
84#ifdef SK_DEBUG
85 static const char* tileModeLabels[] = {
86 "Clamp",
87 "Repeat",
88 "Mirror",
89 "Decal"
90 };
91 static const char* minMagFilterLabels[] = {
92 "Nearest",
93 "Linear"
94 };
95 static const char* mipFilterLabels[] = {
96 "MipNone",
97 "MipNearest",
98 "MipLinear"
99 };
100 std::string label;
101 label.append("X").append(tileModeLabels[static_cast<int>(xTileMode)]);
102 label.append("Y").append(tileModeLabels[static_cast<int>(yTileMode)]);
103 label.append(minMagFilterLabels[static_cast<int>(samplingOptions.filter)]);
104 label.append(mipFilterLabels[static_cast<int>(samplingOptions.mipmap)]);
105 desc.label = label.c_str();
106#endif
107
108 auto sampler = sharedContext->device().CreateSampler(&desc);
109 if (!sampler) {
110 return {};
111 }
112 return sk_sp<DawnSampler>(new DawnSampler(sharedContext, std::move(sampler)));
113}
114
115void DawnSampler::freeGpuData() {
116 fSampler = nullptr;
117}
118
119} // namespace skgpu::graphite
120
#define SkUNREACHABLE
Definition SkAssert.h:135
SkFilterMode
SkMipmapMode
SkTileMode
Definition SkTileMode.h:13
const wgpu::Device & device() const
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
static wgpu::AddressMode tile_mode_to_dawn_address_mode(SkTileMode tileMode)
Definition ref_ptr.h:256
const SkFilterMode filter
const SkMipmapMode mipmap