Debian Codegraph @

src/gpu/graphite/vk/VulkanSampler.cpp

raw
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/vk/VulkanSampler.h"
9
10 #include "include/core/SkSamplingOptions.h"
11 #include "src/gpu/graphite/vk/VulkanCaps.h"
12 #include "src/gpu/graphite/vk/VulkanGraphiteUtils.h"
13
14 namespace skgpu::graphite {
15
16 VulkanSampler::VulkanSampler(const VulkanSharedContext* sharedContext,
17 const SamplerDesc& desc,
18 VkSampler sampler,
19 sk_sp<VulkanYcbcrConversion> ycbcrConversion)
20 : Sampler(sharedContext)
21 , fDesc(desc)
22 , fSampler(sampler)
23 , fYcbcrConversion(ycbcrConversion) {}
24
25 static VkSamplerAddressMode tile_mode_to_vk_sampler_address(SkTileMode tileMode) {
26 switch (tileMode) {
27 case SkTileMode::kClamp:
28 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
29 case SkTileMode::kRepeat:
30 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
31 case SkTileMode::kMirror:
32 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
33 case SkTileMode::kDecal:
34 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
35 }
36 SkUNREACHABLE;
37 }
38
39 sk_sp<VulkanSampler> VulkanSampler::Make(
40 const VulkanSharedContext* sharedContext,
41 const SamplerDesc& desc,
42 sk_sp<VulkanYcbcrConversion> ycbcrConversion) {
43 VkSamplerCreateInfo samplerInfo = {};
44 samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
45
46 VkSamplerYcbcrConversionInfo conversionInfo = {};
47 if (ycbcrConversion) {
48 conversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO;
49 conversionInfo.conversion = ycbcrConversion->ycbcrConversion();
50 samplerInfo.pNext = &conversionInfo;
51 }
52
53 VkFilter minMagFilter = [&] {
54 switch (desc.samplingOptions().filter) {
55 case SkFilterMode::kNearest: return VK_FILTER_NEAREST;
56 case SkFilterMode::kLinear: return VK_FILTER_LINEAR;
57 }
58 SkUNREACHABLE;
59 }();
60 if (ycbcrConversion && ycbcrConversion->requiredFilter().has_value()) {
61 minMagFilter = ycbcrConversion->requiredFilter().value();
62 }
63
64 VkSamplerMipmapMode mipmapMode = [&] {
65 switch (desc.samplingOptions().mipmap) {
66 // There is no disable mode. We use max level to disable mip mapping.
67 // It may make more sense to use NEAREST for kNone but Chrome pixel tests have
68 // been dependent on subtle rendering differences introduced by switching this.
69 case SkMipmapMode::kNone: return VK_SAMPLER_MIPMAP_MODE_LINEAR;
70 case SkMipmapMode::kNearest: return VK_SAMPLER_MIPMAP_MODE_NEAREST;
71 case SkMipmapMode::kLinear: return VK_SAMPLER_MIPMAP_MODE_LINEAR;
72 }
73 SkUNREACHABLE;
74 }();
75
76 samplerInfo.magFilter = minMagFilter;
77 samplerInfo.minFilter = minMagFilter;
78 samplerInfo.mipmapMode = mipmapMode;
79 samplerInfo.addressModeU = tile_mode_to_vk_sampler_address(desc.tileModeX());
80 samplerInfo.addressModeV = tile_mode_to_vk_sampler_address(desc.tileModeY());
81 samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
82 samplerInfo.mipLodBias = 0;
83 samplerInfo.anisotropyEnable = VK_FALSE;
84 samplerInfo.maxAnisotropy = 1; // TODO: when we start using aniso, need to add to key
85 samplerInfo.compareEnable = VK_FALSE;
86 samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
87 // Vulkan doesn't have a direct mapping to use nearest or linear filters for minFilter since
88 // there is always a mipmapMode. To get the same effect we can set minLod = maxLod = 0.0.
89 // This works since our min and mag filters are the same (this forces us to use mag on the 0
90 // level mip). If the filters weren't the same we could set min = 0 and max = 0.25 to force
91 // the minFilter on mip level 0.
92 samplerInfo.minLod = 0;
93 samplerInfo.maxLod = (desc.samplingOptions().mipmap == SkMipmapMode::kNone) ? 0.0f
94 : VK_LOD_CLAMP_NONE;
95 samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
96 samplerInfo.unnormalizedCoordinates = VK_FALSE;
97
98 VkSampler sampler;
99 VkResult result;
100 VULKAN_CALL_RESULT(sharedContext,
101 result,
102 CreateSampler(sharedContext->device(), &samplerInfo, nullptr, &sampler));
103 if (result != VK_SUCCESS) {
104 return nullptr;
105 }
106
107 return sk_sp<VulkanSampler>(new VulkanSampler(sharedContext,
108 desc,
109 sampler,
110 std::move(ycbcrConversion)));
111 }
112
113 void VulkanSampler::freeGpuData() {
114 const VulkanSharedContext* sharedContext =
115 static_cast<const VulkanSharedContext*>(this->sharedContext());
116 SkASSERT(fSampler);
117 VULKAN_CALL(sharedContext->interface(),
118 DestroySampler(sharedContext->device(), fSampler, nullptr));
119 fSampler = VK_NULL_HANDLE;
120 }
121
122 } // namespace skgpu::graphite