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