1// Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef VK_SAMPLER_HPP_
16#define VK_SAMPLER_HPP_
17
18#include "VkDevice.hpp"
19#include "VkImageView.hpp" // For ResolveIdentityMapping()
20#include "Device/Config.hpp"
21#include "System/Math.hpp"
22
23#include <atomic>
24
25namespace vk
26{
27
28class Sampler : public Object<Sampler, VkSampler>
29{
30public:
31 Sampler(const VkSamplerCreateInfo* pCreateInfo, void* mem, const vk::SamplerYcbcrConversion *ycbcrConversion) :
32 magFilter(pCreateInfo->magFilter),
33 minFilter(pCreateInfo->minFilter),
34 mipmapMode(pCreateInfo->mipmapMode),
35 addressModeU(pCreateInfo->addressModeU),
36 addressModeV(pCreateInfo->addressModeV),
37 addressModeW(pCreateInfo->addressModeW),
38 mipLodBias(pCreateInfo->mipLodBias),
39 anisotropyEnable(pCreateInfo->anisotropyEnable),
40 maxAnisotropy(pCreateInfo->maxAnisotropy),
41 compareEnable(pCreateInfo->compareEnable),
42 compareOp(pCreateInfo->compareOp),
43 minLod(ClampLod(pCreateInfo->minLod)),
44 maxLod(ClampLod(pCreateInfo->maxLod)),
45 borderColor(pCreateInfo->borderColor),
46 unnormalizedCoordinates(pCreateInfo->unnormalizedCoordinates),
47 ycbcrConversion(ycbcrConversion)
48 {
49 }
50
51 static size_t ComputeRequiredAllocationSize(const VkSamplerCreateInfo* pCreateInfo)
52 {
53 return 0;
54 }
55
56 // Prevents accessing mipmap levels out of range.
57 static float ClampLod(float lod)
58 {
59 return sw::clamp(lod, 0.0f, (float)(sw::MAX_TEXTURE_LOD));
60 }
61
62 const uint32_t id = nextID++;
63 const VkFilter magFilter = VK_FILTER_NEAREST;
64 const VkFilter minFilter = VK_FILTER_NEAREST;
65 const VkSamplerMipmapMode mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
66 const VkSamplerAddressMode addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
67 const VkSamplerAddressMode addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
68 const VkSamplerAddressMode addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
69 const float mipLodBias = 0.0f;
70 const VkBool32 anisotropyEnable = VK_FALSE;
71 const float maxAnisotropy = 0.0f;
72 const VkBool32 compareEnable = VK_FALSE;
73 const VkCompareOp compareOp = VK_COMPARE_OP_NEVER;
74 const float minLod = 0.0f;
75 const float maxLod = 0.0f;
76 const VkBorderColor borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
77 const VkBool32 unnormalizedCoordinates = VK_FALSE;
78
79 const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr;
80
81private:
82 static std::atomic<uint32_t> nextID;
83};
84
85class SamplerYcbcrConversion : public Object<SamplerYcbcrConversion, VkSamplerYcbcrConversion>
86{
87public:
88 SamplerYcbcrConversion(const VkSamplerYcbcrConversionCreateInfo* pCreateInfo, void* mem) :
89 format(pCreateInfo->format),
90 ycbcrModel(pCreateInfo->ycbcrModel),
91 ycbcrRange(pCreateInfo->ycbcrRange),
92 components(ResolveIdentityMapping(pCreateInfo->components)),
93 xChromaOffset(pCreateInfo->xChromaOffset),
94 yChromaOffset(pCreateInfo->yChromaOffset),
95 chromaFilter(pCreateInfo->chromaFilter),
96 forceExplicitReconstruction(pCreateInfo->forceExplicitReconstruction)
97 {
98 }
99
100 ~SamplerYcbcrConversion() = default;
101
102 static size_t ComputeRequiredAllocationSize(const VkSamplerYcbcrConversionCreateInfo* pCreateInfo)
103 {
104 return 0;
105 }
106
107 const VkFormat format = VK_FORMAT_UNDEFINED;
108 const VkSamplerYcbcrModelConversion ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
109 const VkSamplerYcbcrRange ycbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
110 const VkComponentMapping components = {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A};
111 const VkChromaLocation xChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
112 const VkChromaLocation yChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
113 const VkFilter chromaFilter = VK_FILTER_NEAREST;
114 const VkBool32 forceExplicitReconstruction = VK_FALSE;
115};
116
117static inline Sampler* Cast(VkSampler object)
118{
119 return Sampler::Cast(object);
120}
121
122static inline SamplerYcbcrConversion* Cast(VkSamplerYcbcrConversion object)
123{
124 return SamplerYcbcrConversion::Cast(object);
125}
126
127} // namespace vk
128
129#endif // VK_SAMPLER_HPP_