Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 : Sampler(sharedContext)
19 , fSampler(sampler) {}
20
34
35sk_sp<VulkanSampler> VulkanSampler::Make(const VulkanSharedContext* sharedContext,
36 const SkSamplingOptions& samplingOptions,
37 SkTileMode xTileMode,
38 SkTileMode yTileMode) {
39 VkSamplerCreateInfo samplerInfo;
40 memset(&samplerInfo, 0, sizeof(VkSamplerCreateInfo));
42 samplerInfo.pNext = nullptr;
43 samplerInfo.flags = 0;
44
45 VkFilter minMagFilter = [&] {
46 switch (samplingOptions.filter) {
49 }
51 }();
52
53 VkSamplerMipmapMode mipmapMode = [&] {
54 switch (samplingOptions.mipmap) {
55 // There is no disable mode. We use max level to disable mip mapping.
56 // It may make more sense to use NEAREST for kNone but Chrome pixel tests have
57 // been dependent on subtle rendering differences introduced by switching this.
61 }
63 }();
64
65 samplerInfo.magFilter = minMagFilter;
66 samplerInfo.minFilter = minMagFilter;
67 samplerInfo.mipmapMode = mipmapMode;
68 samplerInfo.addressModeU = tile_mode_to_vk_sampler_address(xTileMode);
69 samplerInfo.addressModeV = tile_mode_to_vk_sampler_address(yTileMode);
71 samplerInfo.mipLodBias = 0;
72 samplerInfo.anisotropyEnable = VK_FALSE;
73 samplerInfo.maxAnisotropy = 1; // TODO: when we start using aniso, need to add to key
74 samplerInfo.compareEnable = VK_FALSE;
75 samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
76 // Vulkan doesn't have a direct mapping to use nearest or linear filters for minFilter since
77 // there is always a mipmapMode. To get the same effect we can set minLod = maxLod = 0.0.
78 // This works since our min and mag filters are the same (this forces us to use mag on the 0
79 // level mip). If the filters weren't the same we could set min = 0 and max = 0.25 to force
80 // the minFilter on mip level 0.
81 samplerInfo.minLod = 0;
82 samplerInfo.maxLod = (samplingOptions.mipmap == SkMipmapMode::kNone) ? 0.0f : VK_LOD_CLAMP_NONE;
85
86 // TODO: Add VkSamplerYcbcrConversion support by adding YCbCr conversion information to the
87 // graphite-level sampler key. Currently, the ResourceProvider only generates a key based on
88 // samplingOptions and tileModes.
89
90 VkSampler sampler;
92 VULKAN_CALL_RESULT(sharedContext,
93 result,
94 CreateSampler(sharedContext->device(), &samplerInfo, nullptr, &sampler));
95 if (result != VK_SUCCESS) {
96 return nullptr;
97 }
98
99 return sk_sp<VulkanSampler>(new VulkanSampler(sharedContext, sampler));
100}
101
102void VulkanSampler::freeGpuData() {
103 const VulkanSharedContext* sharedContext =
104 static_cast<const VulkanSharedContext*>(this->sharedContext());
105 SkASSERT(fSampler);
106 VULKAN_CALL(sharedContext->interface(),
107 DestroySampler(sharedContext->device(), fSampler, nullptr));
108 fSampler = VK_NULL_HANDLE;
109}
110
111} // namespace skgpu::graphite
112
#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
std::function< ProfileSample(void)> Sampler
Sampler is run during SamplingProfiler::SampleRepeatedly. Each platform should implement its version ...
static VkSamplerAddressMode tile_mode_to_vk_sampler_address(SkTileMode tileMode)
const SkFilterMode filter
const SkMipmapMode mipmap
VkBool32 unnormalizedCoordinates
VkSamplerAddressMode addressModeU
VkBorderColor borderColor
VkSamplerMipmapMode mipmapMode
VkSamplerAddressMode addressModeW
VkSamplerCreateFlags flags
VkCompareOp compareOp
VkStructureType sType
VkSamplerAddressMode addressModeV
#define VK_LOD_CLAMP_NONE
@ VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK
VkSamplerMipmapMode
@ VK_SAMPLER_MIPMAP_MODE_NEAREST
@ VK_SAMPLER_MIPMAP_MODE_LINEAR
@ VK_COMPARE_OP_NEVER
#define VK_FALSE
VkFilter
@ VK_FILTER_NEAREST
@ VK_FILTER_LINEAR
VkResult
@ VK_SUCCESS
VkSamplerAddressMode
@ VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT
@ VK_SAMPLER_ADDRESS_MODE_REPEAT
@ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE
@ VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER
#define VK_NULL_HANDLE
Definition vulkan_core.h:46
@ VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO