• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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() == GrMipmapped::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         VkFormatFeatureFlags flags = ycbcrInfo.fFormatFeatures;
98         if (!SkToBool(flags & VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_LINEAR_FILTER_BIT)) {
99             createInfo.magFilter = VK_FILTER_NEAREST;
100             createInfo.minFilter = VK_FILTER_NEAREST;
101         } else if (
102                 !(flags &
103                   VK_FORMAT_FEATURE_SAMPLED_IMAGE_YCBCR_CONVERSION_SEPARATE_RECONSTRUCTION_FILTER_BIT)) {
104             createInfo.magFilter = ycbcrInfo.fChromaFilter;
105             createInfo.minFilter = ycbcrInfo.fChromaFilter;
106         }
107 
108         // Required values when using ycbcr conversion
109         createInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
110         createInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
111         createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
112         createInfo.anisotropyEnable = VK_FALSE;
113         createInfo.unnormalizedCoordinates = VK_FALSE;
114     }
115 
116     VkSampler sampler;
117     VkResult result;
118     GR_VK_CALL_RESULT(gpu, result, CreateSampler(gpu->device(), &createInfo, nullptr, &sampler));
119     if (result != VK_SUCCESS) {
120         ycbcrConversion->unref();
121         return nullptr;
122     }
123 
124     return new GrVkSampler(gpu, sampler, ycbcrConversion, GenerateKey(samplerState, ycbcrInfo));
125 }
126 
freeGPUData() const127 void GrVkSampler::freeGPUData() const {
128     SkASSERT(fSampler);
129     GR_VK_CALL(fGpu->vkInterface(), DestroySampler(fGpu->device(), fSampler, nullptr));
130     if (fYcbcrConversion) {
131         fYcbcrConversion->unref();
132     }
133 }
134 
GenerateKey(GrSamplerState samplerState,const GrVkYcbcrConversionInfo & ycbcrInfo)135 GrVkSampler::Key GrVkSampler::GenerateKey(GrSamplerState samplerState,
136                                           const GrVkYcbcrConversionInfo& ycbcrInfo) {
137     // In VK the max aniso value is specified in addition to min/mag/mip filters and the
138     // driver is encouraged to consider the other filter settings when doing aniso.
139     return {samplerState.asKey(/*anisoIsOrthogonal=*/true),
140             GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)};
141 }
142