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
25 namespace vk {
26
27 class Sampler : public Object<Sampler, VkSampler>
28 {
29 public:
Sampler(const VkSamplerCreateInfo * pCreateInfo,void * mem,const vk::SamplerYcbcrConversion * ycbcrConversion)30 Sampler(const VkSamplerCreateInfo *pCreateInfo, void *mem, const vk::SamplerYcbcrConversion *ycbcrConversion)
31 : magFilter(pCreateInfo->magFilter)
32 , minFilter(pCreateInfo->minFilter)
33 , mipmapMode(pCreateInfo->mipmapMode)
34 , addressModeU(pCreateInfo->addressModeU)
35 , addressModeV(pCreateInfo->addressModeV)
36 , addressModeW(pCreateInfo->addressModeW)
37 , mipLodBias(pCreateInfo->mipLodBias)
38 , anisotropyEnable(pCreateInfo->anisotropyEnable)
39 , maxAnisotropy(pCreateInfo->maxAnisotropy)
40 , compareEnable(pCreateInfo->compareEnable)
41 , compareOp(pCreateInfo->compareOp)
42 , minLod(ClampLod(pCreateInfo->minLod))
43 , maxLod(ClampLod(pCreateInfo->maxLod))
44 , borderColor(pCreateInfo->borderColor)
45 , unnormalizedCoordinates(pCreateInfo->unnormalizedCoordinates)
46 , ycbcrConversion(ycbcrConversion)
47 {
48 }
49
ComputeRequiredAllocationSize(const VkSamplerCreateInfo * pCreateInfo)50 static size_t ComputeRequiredAllocationSize(const VkSamplerCreateInfo *pCreateInfo)
51 {
52 return 0;
53 }
54
55 // Prevents accessing mipmap levels out of range.
ClampLod(float lod)56 static float ClampLod(float lod)
57 {
58 return sw::clamp(lod, 0.0f, (float)(sw::MAX_TEXTURE_LOD));
59 }
60
61 const uint32_t id = nextID++;
62 const VkFilter magFilter = VK_FILTER_NEAREST;
63 const VkFilter minFilter = VK_FILTER_NEAREST;
64 const VkSamplerMipmapMode mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
65 const VkSamplerAddressMode addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
66 const VkSamplerAddressMode addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
67 const VkSamplerAddressMode addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
68 const float mipLodBias = 0.0f;
69 const VkBool32 anisotropyEnable = VK_FALSE;
70 const float maxAnisotropy = 0.0f;
71 const VkBool32 compareEnable = VK_FALSE;
72 const VkCompareOp compareOp = VK_COMPARE_OP_NEVER;
73 const float minLod = 0.0f;
74 const float maxLod = 0.0f;
75 const VkBorderColor borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
76 const VkBool32 unnormalizedCoordinates = VK_FALSE;
77
78 const vk::SamplerYcbcrConversion *ycbcrConversion = nullptr;
79
80 private:
81 static std::atomic<uint32_t> nextID;
82 };
83
84 class SamplerYcbcrConversion : public Object<SamplerYcbcrConversion, VkSamplerYcbcrConversion>
85 {
86 public:
SamplerYcbcrConversion(const VkSamplerYcbcrConversionCreateInfo * pCreateInfo,void * mem)87 SamplerYcbcrConversion(const VkSamplerYcbcrConversionCreateInfo *pCreateInfo, void *mem)
88 : format(pCreateInfo->format)
89 , ycbcrModel(pCreateInfo->ycbcrModel)
90 , ycbcrRange(pCreateInfo->ycbcrRange)
91 , components(ResolveIdentityMapping(pCreateInfo->components))
92 , xChromaOffset(pCreateInfo->xChromaOffset)
93 , yChromaOffset(pCreateInfo->yChromaOffset)
94 , chromaFilter(pCreateInfo->chromaFilter)
95 , forceExplicitReconstruction(pCreateInfo->forceExplicitReconstruction)
96 {
97 }
98
99 ~SamplerYcbcrConversion() = default;
100
ComputeRequiredAllocationSize(const VkSamplerYcbcrConversionCreateInfo * pCreateInfo)101 static size_t ComputeRequiredAllocationSize(const VkSamplerYcbcrConversionCreateInfo *pCreateInfo)
102 {
103 return 0;
104 }
105
106 const VkFormat format = VK_FORMAT_UNDEFINED;
107 const VkSamplerYcbcrModelConversion ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
108 const VkSamplerYcbcrRange ycbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
109 const VkComponentMapping components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
110 const VkChromaLocation xChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
111 const VkChromaLocation yChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
112 const VkFilter chromaFilter = VK_FILTER_NEAREST;
113 const VkBool32 forceExplicitReconstruction = VK_FALSE;
114 };
115
Cast(VkSampler object)116 static inline Sampler *Cast(VkSampler object)
117 {
118 return Sampler::Cast(object);
119 }
120
Cast(VkSamplerYcbcrConversion object)121 static inline SamplerYcbcrConversion *Cast(VkSamplerYcbcrConversion object)
122 {
123 return SamplerYcbcrConversion::Cast(object);
124 }
125
126 } // namespace vk
127
128 #endif // VK_SAMPLER_HPP_