1 /*
2 * Copyright 2016 Google Inc.
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/ganesh/vk/GrVkSampler.h"
9
10 #include "src/gpu/ganesh/vk/GrVkGpu.h"
11 #include "src/gpu/ganesh/vk/GrVkSamplerYcbcrConversion.h"
12
wrap_mode_to_vk_sampler_address(GrSamplerState::WrapMode wrapMode)13 static VkSamplerAddressMode wrap_mode_to_vk_sampler_address(GrSamplerState::WrapMode wrapMode) {
14 switch (wrapMode) {
15 case GrSamplerState::WrapMode::kClamp:
16 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
17 case GrSamplerState::WrapMode::kRepeat:
18 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
19 case GrSamplerState::WrapMode::kMirrorRepeat:
20 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
21 case GrSamplerState::WrapMode::kClampToBorder:
22 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
23 }
24 SkUNREACHABLE;
25 }
26
mipmap_mode_to_vk_sampler_mipmap_mode(GrSamplerState::MipmapMode mm)27 static VkSamplerMipmapMode mipmap_mode_to_vk_sampler_mipmap_mode(GrSamplerState::MipmapMode mm) {
28 switch (mm) {
29 // There is no disable mode. We use max level to disable mip mapping.
30 // It may make more sense to use NEAREST for kNone but Chrome pixel tests seam dependent
31 // on subtle rendering differences introduced by switching this.
32 case GrSamplerState::MipmapMode::kNone: return VK_SAMPLER_MIPMAP_MODE_LINEAR;
33 case GrSamplerState::MipmapMode::kNearest: return VK_SAMPLER_MIPMAP_MODE_NEAREST;
34 case GrSamplerState::MipmapMode::kLinear: return VK_SAMPLER_MIPMAP_MODE_LINEAR;
35 }
36 SkUNREACHABLE;
37 }
38
Create(GrVkGpu * gpu,GrSamplerState samplerState,const GrVkYcbcrConversionInfo & ycbcrInfo)39 GrVkSampler* GrVkSampler::Create(GrVkGpu* gpu, GrSamplerState samplerState,
40 const GrVkYcbcrConversionInfo& ycbcrInfo) {
41 static VkFilter vkMinFilterModes[] = {
42 VK_FILTER_NEAREST,
43 VK_FILTER_LINEAR,
44 VK_FILTER_LINEAR
45 };
46 static VkFilter vkMagFilterModes[] = {
47 VK_FILTER_NEAREST,
48 VK_FILTER_LINEAR,
49 VK_FILTER_LINEAR
50 };
51
52 VkSamplerCreateInfo createInfo;
53 memset(&createInfo, 0, sizeof(VkSamplerCreateInfo));
54 createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
55 createInfo.pNext = nullptr;
56 createInfo.flags = 0;
57 createInfo.magFilter = vkMagFilterModes[static_cast<int>(samplerState.filter())];
58 createInfo.minFilter = vkMinFilterModes[static_cast<int>(samplerState.filter())];
59 createInfo.mipmapMode = mipmap_mode_to_vk_sampler_mipmap_mode(samplerState.mipmapMode());
60 createInfo.addressModeU = wrap_mode_to_vk_sampler_address(samplerState.wrapModeX());
61 createInfo.addressModeV = wrap_mode_to_vk_sampler_address(samplerState.wrapModeY());
62 createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // Shouldn't matter
63 createInfo.mipLodBias = 0.0f;
64 createInfo.anisotropyEnable = samplerState.isAniso() ? VK_TRUE : VK_FALSE;
65 createInfo.maxAnisotropy = std::min(static_cast<float>(samplerState.maxAniso()),
66 gpu->vkCaps().maxSamplerAnisotropy());
67 createInfo.compareEnable = VK_FALSE;
68 createInfo.compareOp = VK_COMPARE_OP_NEVER;
69 // Vulkan doesn't have a direct mapping of GL's nearest or linear filters for minFilter since
70 // there is always a mipmapMode. To get the same effect as GL we can set minLod = maxLod = 0.0.
71 // This works since our min and mag filters are the same (this forces us to use mag on the 0
72 // level mip). If the filters weren't the same we could set min = 0 and max = 0.25 to force
73 // the minFilter on mip level 0.
74 createInfo.minLod = 0.0f;
75 bool useMipMaps = samplerState.mipmapped() == skgpu::Mipmapped::kYes;
76 createInfo.maxLod = !useMipMaps ? 0.0f : 10000.0f;
77 createInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
78 createInfo.unnormalizedCoordinates = VK_FALSE;
79
80 VkSamplerYcbcrConversionInfo conversionInfo;
81 GrVkSamplerYcbcrConversion* ycbcrConversion = nullptr;
82 if (ycbcrInfo.isValid()) {
83 SkASSERT(gpu->vkCaps().supportsYcbcrConversion());
84
85 ycbcrConversion =
86 gpu->resourceProvider().findOrCreateCompatibleSamplerYcbcrConversion(ycbcrInfo);
87 if (!ycbcrConversion) {
88 return nullptr;
89 }
90
91 conversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO;
92 conversionInfo.pNext = nullptr;
93 conversionInfo.conversion = ycbcrConversion->ycbcrConversion();
94
95 createInfo.pNext = &conversionInfo;
96
97 VkFilter chromaFilter = ycbcrInfo.fChromaFilter;
98 VkFormatFeatureFlags flags = ycbcrInfo.fFormatFeatures;
99 if (!SkToBool(flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT)) {
100 createInfo.magFilter = VK_FILTER_NEAREST;
101 createInfo.minFilter = VK_FILTER_NEAREST;
102 // This doesn't explicitly do anything itself, but if we don't support separate
103 // reconstruction filters below, then we need the chromaFilter to match the mag/min here
104 // so we set it nearest. Because we don't currently update the VulkanYcbcrConversionInfo
105 // that we're querying here, this logic around the chromaFilter must match the logic in
106 // the SetupSamplerYcbcrConversionInfo helper function.
107 chromaFilter = VK_FILTER_NEAREST;
108 }
109 if (!(flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT)) {
110 createInfo.magFilter = chromaFilter;
111 createInfo.minFilter = chromaFilter;
112 }
113
114 // Required values when using ycbcr conversion
115 createInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
116 createInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
117 createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
118 createInfo.anisotropyEnable = VK_FALSE;
119 createInfo.unnormalizedCoordinates = VK_FALSE;
120 }
121
122 VkSampler sampler;
123 VkResult result;
124 GR_VK_CALL_RESULT(gpu, result, CreateSampler(gpu->device(), &createInfo, nullptr, &sampler));
125 if (result != VK_SUCCESS) {
126 ycbcrConversion->unref();
127 return nullptr;
128 }
129
130 return new GrVkSampler(gpu, sampler, ycbcrConversion, GenerateKey(samplerState, ycbcrInfo));
131 }
132
freeGPUData() const133 void GrVkSampler::freeGPUData() const {
134 SkASSERT(fSampler);
135 GR_VK_CALL(fGpu->vkInterface(), DestroySampler(fGpu->device(), fSampler, nullptr));
136 if (fYcbcrConversion) {
137 fYcbcrConversion->unref();
138 }
139 }
140
GenerateKey(GrSamplerState samplerState,const GrVkYcbcrConversionInfo & ycbcrInfo)141 GrVkSampler::Key GrVkSampler::GenerateKey(GrSamplerState samplerState,
142 const GrVkYcbcrConversionInfo& ycbcrInfo) {
143 // In VK the max aniso value is specified in addition to min/mag/mip filters and the
144 // driver is encouraged to consider the other filter settings when doing aniso.
145 return {samplerState.asKey(/*anisoIsOrthogonal=*/true),
146 GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)};
147 }
148