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 "GrVkSampler.h"
9
10 #include "GrVkGpu.h"
11
wrap_mode_to_vk_sampler_address(GrSamplerState::WrapMode wrapMode)12 static inline VkSamplerAddressMode wrap_mode_to_vk_sampler_address(
13 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 }
22 SK_ABORT("Unknown wrap mode.");
23 return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
24 }
25
Create(const GrVkGpu * gpu,const GrSamplerState & samplerState,uint32_t maxMipLevel)26 GrVkSampler* GrVkSampler::Create(const GrVkGpu* gpu, const GrSamplerState& samplerState,
27 uint32_t maxMipLevel) {
28 static VkFilter vkMinFilterModes[] = {
29 VK_FILTER_NEAREST,
30 VK_FILTER_LINEAR,
31 VK_FILTER_LINEAR
32 };
33 static VkFilter vkMagFilterModes[] = {
34 VK_FILTER_NEAREST,
35 VK_FILTER_LINEAR,
36 VK_FILTER_LINEAR
37 };
38
39 VkSamplerCreateInfo createInfo;
40 memset(&createInfo, 0, sizeof(VkSamplerCreateInfo));
41 createInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
42 createInfo.pNext = 0;
43 createInfo.flags = 0;
44 createInfo.magFilter = vkMagFilterModes[static_cast<int>(samplerState.filter())];
45 createInfo.minFilter = vkMinFilterModes[static_cast<int>(samplerState.filter())];
46 createInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
47 createInfo.addressModeU = wrap_mode_to_vk_sampler_address(samplerState.wrapModeX());
48 createInfo.addressModeV = wrap_mode_to_vk_sampler_address(samplerState.wrapModeY());
49 createInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; // Shouldn't matter
50 createInfo.mipLodBias = 0.0f;
51 createInfo.anisotropyEnable = VK_FALSE;
52 createInfo.maxAnisotropy = 1.0f;
53 createInfo.compareEnable = VK_FALSE;
54 createInfo.compareOp = VK_COMPARE_OP_NEVER;
55 // Vulkan doesn't have a direct mapping of GL's nearest or linear filters for minFilter since
56 // there is always a mipmapMode. To get the same effect as GL we can set minLod = maxLod = 0.0.
57 // This works since our min and mag filters are the same (this forces us to use mag on the 0
58 // level mip). If the filters weren't the same we could set min = 0 and max = 0.25 to force
59 // the minFilter on mip level 0.
60 createInfo.minLod = 0.0f;
61 bool useMipMaps = GrSamplerState::Filter::kMipMap == samplerState.filter() && maxMipLevel > 0;
62 createInfo.maxLod = !useMipMaps ? 0.0f : (float)(maxMipLevel);
63 createInfo.borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
64 createInfo.unnormalizedCoordinates = VK_FALSE;
65
66 VkSampler sampler;
67 GR_VK_CALL_ERRCHECK(gpu->vkInterface(), CreateSampler(gpu->device(),
68 &createInfo,
69 nullptr,
70 &sampler));
71
72 return new GrVkSampler(sampler, GenerateKey(samplerState, maxMipLevel));
73 }
74
freeGPUData(const GrVkGpu * gpu) const75 void GrVkSampler::freeGPUData(const GrVkGpu* gpu) const {
76 SkASSERT(fSampler);
77 GR_VK_CALL(gpu->vkInterface(), DestroySampler(gpu->device(), fSampler, nullptr));
78 }
79
GenerateKey(const GrSamplerState & samplerState,uint32_t maxMipLevel)80 uint16_t GrVkSampler::GenerateKey(const GrSamplerState& samplerState, uint32_t maxMipLevel) {
81 const int kTileModeXShift = 2;
82 const int kTileModeYShift = 4;
83 const int kMipLevelShift = 6;
84
85 SkASSERT(static_cast<int>(samplerState.filter()) <= 3);
86 uint16_t key = static_cast<uint16_t>(samplerState.filter());
87
88 SkASSERT(static_cast<int>(samplerState.wrapModeX()) <= 4);
89 key |= (static_cast<uint16_t>(samplerState.wrapModeX()) << kTileModeXShift);
90
91 SkASSERT(static_cast<int>(samplerState.wrapModeY()) <= 4);
92 key |= (static_cast<uint16_t>(samplerState.wrapModeY()) << kTileModeYShift);
93
94 SkASSERT(maxMipLevel < 1024);
95 key |= (maxMipLevel << kMipLevelShift);
96
97 return key;
98 }
99