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