Flutter Engine
The Flutter Engine
VulkanSampler.cpp
Go to the documentation of this file.
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
9
13
14namespace skgpu::graphite {
15
16VulkanSampler::VulkanSampler(const VulkanSharedContext* sharedContext,
17 VkSampler sampler,
18 sk_sp<VulkanYcbcrConversion> ycbcrConversion)
19 : Sampler(sharedContext)
20 , fSampler(sampler)
21 , fYcbcrConversion(ycbcrConversion) {}
22
24 switch (tileMode) {
33 }
35}
36
38 const VulkanSharedContext* sharedContext,
39 const SkSamplingOptions& samplingOptions,
40 SkTileMode xTileMode,
41 SkTileMode yTileMode,
42 sk_sp<VulkanYcbcrConversion> ycbcrConversion) {
43
44 VkSamplerCreateInfo samplerInfo;
45 memset(&samplerInfo, 0, sizeof(VkSamplerCreateInfo));
47 // TODO(b/311392779): If a ycbcrConversion is passed in, assign pNext to
48 // VkSamplerYcbcrConversion.
49 samplerInfo.pNext = nullptr;
50 samplerInfo.flags = 0;
51
52 VkFilter minMagFilter = [&] {
53 switch (samplingOptions.filter) {
56 }
58 }();
59
60 VkSamplerMipmapMode mipmapMode = [&] {
61 switch (samplingOptions.mipmap) {
62 // There is no disable mode. We use max level to disable mip mapping.
63 // It may make more sense to use NEAREST for kNone but Chrome pixel tests have
64 // been dependent on subtle rendering differences introduced by switching this.
68 }
70 }();
71
72 samplerInfo.magFilter = minMagFilter;
73 samplerInfo.minFilter = minMagFilter;
74 samplerInfo.mipmapMode = mipmapMode;
75 samplerInfo.addressModeU = tile_mode_to_vk_sampler_address(xTileMode);
76 samplerInfo.addressModeV = tile_mode_to_vk_sampler_address(yTileMode);
78 samplerInfo.mipLodBias = 0;
79 samplerInfo.anisotropyEnable = VK_FALSE;
80 samplerInfo.maxAnisotropy = 1; // TODO: when we start using aniso, need to add to key
81 samplerInfo.compareEnable = VK_FALSE;
82 samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
83 // Vulkan doesn't have a direct mapping to use nearest or linear filters for minFilter since
84 // there is always a mipmapMode. To get the same effect we can set minLod = maxLod = 0.0.
85 // This works since our min and mag filters are the same (this forces us to use mag on the 0
86 // level mip). If the filters weren't the same we could set min = 0 and max = 0.25 to force
87 // the minFilter on mip level 0.
88 samplerInfo.minLod = 0;
89 samplerInfo.maxLod = (samplingOptions.mipmap == SkMipmapMode::kNone) ? 0.0f : VK_LOD_CLAMP_NONE;
92
93 VkSampler sampler;
95 VULKAN_CALL_RESULT(sharedContext,
96 result,
97 CreateSampler(sharedContext->device(), &samplerInfo, nullptr, &sampler));
98 if (result != VK_SUCCESS) {
99 return nullptr;
100 }
101
102 return sk_sp<VulkanSampler>(new VulkanSampler(sharedContext,
103 sampler,
104 std::move(ycbcrConversion)));
105}
106
107void VulkanSampler::freeGpuData() {
108 const VulkanSharedContext* sharedContext =
109 static_cast<const VulkanSharedContext*>(this->sharedContext());
110 SkASSERT(fSampler);
111 VULKAN_CALL(sharedContext->interface(),
112 DestroySampler(sharedContext->device(), fSampler, nullptr));
113 fSampler = VK_NULL_HANDLE;
114}
115
116} // namespace skgpu::graphite
117
#define SkUNREACHABLE
Definition: SkAssert.h:135
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkTileMode
Definition: SkTileMode.h:13
#define VULKAN_CALL(IFACE, X)
#define VULKAN_CALL_RESULT(SHARED_CONTEXT, RESULT, X)
const skgpu::VulkanInterface * interface() const
GAsyncResult * result
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
static vk::UniqueSampler CreateSampler(const vk::Device &device, const SamplerDescriptor &desc, const std::shared_ptr< YUVConversionVK > &yuv_conversion)
Definition: sampler_vk.cc:14
static VkSamplerAddressMode tile_mode_to_vk_sampler_address(SkTileMode tileMode)
const SkFilterMode filter
const SkMipmapMode mipmap
VkBool32 unnormalizedCoordinates
Definition: vulkan_core.h:3722
VkSamplerAddressMode addressModeU
Definition: vulkan_core.h:3711
VkBool32 anisotropyEnable
Definition: vulkan_core.h:3715
VkBorderColor borderColor
Definition: vulkan_core.h:3721
const void * pNext
Definition: vulkan_core.h:3706
VkSamplerMipmapMode mipmapMode
Definition: vulkan_core.h:3710
VkSamplerAddressMode addressModeW
Definition: vulkan_core.h:3713
VkSamplerCreateFlags flags
Definition: vulkan_core.h:3707
VkCompareOp compareOp
Definition: vulkan_core.h:3718
VkStructureType sType
Definition: vulkan_core.h:3705
VkSamplerAddressMode addressModeV
Definition: vulkan_core.h:3712
#define VK_LOD_CLAMP_NONE
Definition: vulkan_core.h:126
@ VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
Definition: vulkan_core.h:2089
VkSamplerMipmapMode
Definition: vulkan_core.h:2118
@ VK_SAMPLER_MIPMAP_MODE_NEAREST
Definition: vulkan_core.h:2119
@ VK_SAMPLER_MIPMAP_MODE_LINEAR
Definition: vulkan_core.h:2120
@ VK_COMPARE_OP_NEVER
Definition: vulkan_core.h:1919
#define VK_FALSE
Definition: vulkan_core.h:125
VkFilter
Definition: vulkan_core.h:2100
@ VK_FILTER_NEAREST
Definition: vulkan_core.h:2101
@ VK_FILTER_LINEAR
Definition: vulkan_core.h:2102
VkResult
Definition: vulkan_core.h:140
@ VK_SUCCESS
Definition: vulkan_core.h:141
VkSamplerAddressMode
Definition: vulkan_core.h:2108
@ VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
Definition: vulkan_core.h:2110
@ VK_SAMPLER_ADDRESS_MODE_REPEAT
Definition: vulkan_core.h:2109
@ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
Definition: vulkan_core.h:2111
@ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
Definition: vulkan_core.h:2112
#define VK_NULL_HANDLE
Definition: vulkan_core.h:46
@ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO
Definition: vulkan_core.h:233