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 "VkImageView.hpp" // For ResolveIdentityMapping()
19 #include "Device/Config.hpp"
20 #include "Device/Memset.hpp"
21 #include "System/Math.hpp"
22
23 #include <atomic>
24
25 namespace vk {
26
27 struct SamplerState : sw::Memset<SamplerState>
28 {
29 SamplerState(const VkSamplerCreateInfo *pCreateInfo, const vk::SamplerYcbcrConversion *ycbcrConversion, VkSamplerFilteringPrecisionModeGOOGLE filteringPrecision);
30
31 // Prevents accessing mipmap levels out of range.
ClampLodvk::SamplerState32 static float ClampLod(float lod)
33 {
34 return sw::clamp(lod, 0.0f, (float)(sw::MAX_TEXTURE_LOD));
35 }
36
37 const VkFilter magFilter = VK_FILTER_NEAREST;
38 const VkFilter minFilter = VK_FILTER_NEAREST;
39 const VkSamplerMipmapMode mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
40 const VkSamplerAddressMode addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
41 const VkSamplerAddressMode addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
42 const VkSamplerAddressMode addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
43 const float mipLodBias = 0.0f;
44 const VkBool32 anisotropyEnable = VK_FALSE;
45 const float maxAnisotropy = 0.0f;
46 const VkBool32 compareEnable = VK_FALSE;
47 const VkCompareOp compareOp = VK_COMPARE_OP_NEVER;
48 const float minLod = 0.0f;
49 const float maxLod = 0.0f;
50 const VkBorderColor borderColor = VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK;
51 const VkBool32 unnormalizedCoordinates = VK_FALSE;
52
53 VkSamplerYcbcrModelConversion ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
54 const VkSamplerFilteringPrecisionModeGOOGLE filteringPrecision = VK_SAMPLER_FILTERING_PRECISION_MODE_LOW_GOOGLE;
55 bool studioSwing = false; // Narrow range
56 bool swappedChroma = false; // Cb/Cr components in reverse order
57 };
58
59 class Sampler : public Object<Sampler, VkSampler>, public SamplerState
60 {
61 public:
62 Sampler(const VkSamplerCreateInfo *pCreateInfo, void *mem, const SamplerState &samplerState, uint32_t samplerID);
63
ComputeRequiredAllocationSize(const VkSamplerCreateInfo * pCreateInfo)64 static size_t ComputeRequiredAllocationSize(const VkSamplerCreateInfo *pCreateInfo)
65 {
66 return 0;
67 }
68
69 const uint32_t id = 0;
70 };
71
72 class SamplerYcbcrConversion : public Object<SamplerYcbcrConversion, VkSamplerYcbcrConversion>
73 {
74 public:
SamplerYcbcrConversion(const VkSamplerYcbcrConversionCreateInfo * pCreateInfo,void * mem)75 SamplerYcbcrConversion(const VkSamplerYcbcrConversionCreateInfo *pCreateInfo, void *mem)
76 : format(pCreateInfo->format)
77 , ycbcrModel(pCreateInfo->ycbcrModel)
78 , ycbcrRange(pCreateInfo->ycbcrRange)
79 , components(ResolveIdentityMapping(pCreateInfo->components))
80 , xChromaOffset(pCreateInfo->xChromaOffset)
81 , yChromaOffset(pCreateInfo->yChromaOffset)
82 , chromaFilter(pCreateInfo->chromaFilter)
83 , forceExplicitReconstruction(pCreateInfo->forceExplicitReconstruction)
84 {
85 }
86
87 ~SamplerYcbcrConversion() = default;
88
ComputeRequiredAllocationSize(const VkSamplerYcbcrConversionCreateInfo * pCreateInfo)89 static size_t ComputeRequiredAllocationSize(const VkSamplerYcbcrConversionCreateInfo *pCreateInfo)
90 {
91 return 0;
92 }
93
94 const VkFormat format = VK_FORMAT_UNDEFINED;
95 const VkSamplerYcbcrModelConversion ycbcrModel = VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY;
96 const VkSamplerYcbcrRange ycbcrRange = VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
97 const VkComponentMapping components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
98 const VkChromaLocation xChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
99 const VkChromaLocation yChromaOffset = VK_CHROMA_LOCATION_COSITED_EVEN;
100 const VkFilter chromaFilter = VK_FILTER_NEAREST;
101 const VkBool32 forceExplicitReconstruction = VK_FALSE;
102 };
103
Cast(VkSampler object)104 static inline Sampler *Cast(VkSampler object)
105 {
106 return Sampler::Cast(object);
107 }
108
Cast(VkSamplerYcbcrConversion object)109 static inline SamplerYcbcrConversion *Cast(VkSamplerYcbcrConversion object)
110 {
111 return SamplerYcbcrConversion::Cast(object);
112 }
113
114 } // namespace vk
115
116 #endif // VK_SAMPLER_HPP_