Debian Codegraph @

src/gpu/ganesh/vk/GrVkSampler.cpp

raw
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 #include <atomic>
24 #include <optional>
25
26 static VkSamplerAddressMode wrap_mode_to_vk_sampler_address(GrSamplerState::WrapMode wrapMode) {
27 switch (wrapMode) {
28 case GrSamplerState::WrapMode::kClamp:
29 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
30 case GrSamplerState::WrapMode::kRepeat:
31 return VK_SAMPLER_ADDRESS_MODE_REPEAT;
32 case GrSamplerState::WrapMode::kMirrorRepeat:
33 return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;
34 case GrSamplerState::WrapMode::kClampToBorder:
35 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
36 }
37 SkUNREACHABLE;
38 }
39
40 static VkSamplerMipmapMode mipmap_mode_to_vk_sampler_mipmap_mode(GrSamplerState::MipmapMode mm) {
41 switch (mm) {
42 // There is no disable mode. We use max level to disable mip mapping.
43 // It may make more sense to use NEAREST for kNone but Chrome pixel tests seam dependent
44 // on subtle rendering differences introduced by switching this.
45 case GrSamplerState::MipmapMode::kNone: return VK_SAMPLER_MIPMAP_MODE_LINEAR;
46 case GrSamplerState::MipmapMode::kNearest: return VK_SAMPLER_MIPMAP_MODE_NEAREST;
47 case GrSamplerState::MipmapMode::kLinear: return VK_SAMPLER_MIPMAP_MODE_LINEAR;
48 }
49 SkUNREACHABLE;
50 }
51
52 GrVkSampler* GrVkSampler::Create(GrVkGpu* gpu,
53 GrSamplerState samplerState,
54 const skgpu::VulkanYcbcrConversionInfo& ycbcrInfo) {
55 static VkFilter vkMinFilterModes[] = {
56 VK_FILTER_NEAREST,
57 VK_FILTER_LINEAR,
58 VK_FILTER_LINEAR
59 };
60 static VkFilter vkMagFilterModes[] = {
61 VK_FILTER_NEAREST,
62 VK_FILTER_LINEAR,
63 VK_FILTER_LINEAR
64 };
65
66 VkSamplerCreateInfo createInfo;
67 memset(&createInfo, 0, sizeof(VkSamplerCreateInfo));
68 createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
69 createInfo.pNext = nullptr;
70 createInfo.flags = 0;
71 createInfo.magFilter = vkMagFilterModes[static_cast<int>(samplerState.filter())];
72 createInfo.minFilter = vkMinFilterModes[static_cast<int>(samplerState.filter())];
73 createInfo.mipmapMode = mipmap_mode_to_vk_sampler_mipmap_mode(samplerState.mipmapMode());
74 createInfo.addressModeU = wrap_mode_to_vk_sampler_address(samplerState.wrapModeX());
75 createInfo.addressModeV = wrap_mode_to_vk_sampler_address(samplerState.wrapModeY());
76 createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // Shouldn't matter
77 createInfo.mipLodBias = 0.0f;
78 createInfo.anisotropyEnable = samplerState.isAniso() ? VK_TRUE : VK_FALSE;
79 createInfo.maxAnisotropy = std::min(static_cast<float>(samplerState.maxAniso()),
80 gpu->vkCaps().maxSamplerAnisotropy());
81 createInfo.compareEnable = VK_FALSE;
82 createInfo.compareOp = VK_COMPARE_OP_NEVER;
83 // Vulkan doesn't have a direct mapping of GL's nearest or linear filters for minFilter since
84 // there is always a mipmapMode. To get the same effect as GL 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 createInfo.minLod = 0.0f;
89 bool useMipMaps = samplerState.mipmapped() == skgpu::Mipmapped::kYes;
90 createInfo.maxLod = !useMipMaps ? 0.0f : 10000.0f;
91 createInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
92 createInfo.unnormalizedCoordinates = VK_FALSE;
93
94 VkSamplerYcbcrConversionInfo conversionInfo;
95 GrVkSamplerYcbcrConversion* ycbcrConversion = nullptr;
96 if (ycbcrInfo.isValid()) {
97 SkASSERT(gpu->vkCaps().supportsYcbcrConversion());
98
99 ycbcrConversion =
100 gpu->resourceProvider().findOrCreateCompatibleSamplerYcbcrConversion(ycbcrInfo);
101 if (!ycbcrConversion) {
102 return nullptr;
103 }
104
105 conversionInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO;
106 conversionInfo.pNext = nullptr;
107 conversionInfo.conversion = ycbcrConversion->ycbcrConversion();
108
109 createInfo.pNext = &conversionInfo;
110
111 if (ycbcrConversion->requiredFilter().has_value()) {
112 createInfo.magFilter = ycbcrConversion->requiredFilter().value();
113 createInfo.minFilter = ycbcrConversion->requiredFilter().value();
114 }
115
116 // Required values when using ycbcr conversion
117 createInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
118 createInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
119 createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
120 createInfo.anisotropyEnable = VK_FALSE;
121 createInfo.unnormalizedCoordinates = VK_FALSE;
122 }
123
124 VkSampler sampler;
125 VkResult result;
126 GR_VK_CALL_RESULT(gpu, result, CreateSampler(gpu->device(), &createInfo, nullptr, &sampler));
127 if (result != VK_SUCCESS) {
128 ycbcrConversion->unref();
129 return nullptr;
130 }
131
132 return new GrVkSampler(gpu, sampler, ycbcrConversion, GenerateKey(samplerState, ycbcrInfo));
133 }
134
135 void GrVkSampler::freeGPUData() const {
136 SkASSERT(fSampler);
137 GR_VK_CALL(fGpu->vkInterface(), DestroySampler(fGpu->device(), fSampler, nullptr));
138 if (fYcbcrConversion) {
139 fYcbcrConversion->unref();
140 }
141 }
142
143 GrVkSampler::Key GrVkSampler::GenerateKey(GrSamplerState samplerState,
144 const skgpu::VulkanYcbcrConversionInfo& ycbcrInfo) {
145 // In VK the max aniso value is specified in addition to min/mag/mip filters and the
146 // driver is encouraged to consider the other filter settings when doing aniso.
147 return {samplerState.asKey(/*anisoIsOrthogonal=*/true),
148 GrVkSamplerYcbcrConversion::GenerateKey(ycbcrInfo)};
149 }
150
151 uint32_t GrVkSampler::GenID() {
152 static std::atomic<uint32_t> nextID{1};
153 uint32_t id;
154 do {
155 id = nextID++;
156 } while (id == SK_InvalidUniqueID);
157 return id;
158 }