xref: /aosp_15_r20/external/swiftshader/src/Device/Sampler.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2016 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 sw_Sampler_hpp
16 #define sw_Sampler_hpp
17 
18 #include "Device/Config.hpp"
19 #include "System/Types.hpp"
20 #include "Vulkan/VkFormat.hpp"
21 
22 namespace sw {
23 
24 struct Mipmap
25 {
26 	const void *buffer;
27 
28 	ushort4 uHalf;
29 	ushort4 vHalf;
30 	ushort4 wHalf;
31 	uint4 width;
32 	uint4 height;
33 	uint4 depth;
34 	short4 onePitchP;
35 	uint4 pitchP;
36 	uint4 sliceP;
37 	uint4 samplePitchP;
38 	uint4 sampleMax;
39 };
40 
41 struct Texture
42 {
43 	Mipmap mipmap[MIPMAP_LEVELS];
44 
45 	float4 widthWidthHeightHeight;
46 	float4 width;
47 	float4 height;
48 	float4 depth;
49 };
50 
51 enum FilterType ENUM_UNDERLYING_TYPE_UNSIGNED_INT
52 {
53 	FILTER_POINT,
54 	FILTER_GATHER,
55 	FILTER_MIN_POINT_MAG_LINEAR,
56 	FILTER_MIN_LINEAR_MAG_POINT,
57 	FILTER_LINEAR,
58 	FILTER_ANISOTROPIC,
59 
60 	FILTER_LAST = FILTER_ANISOTROPIC
61 };
62 
63 enum MipmapType ENUM_UNDERLYING_TYPE_UNSIGNED_INT
64 {
65 	MIPMAP_NONE,
66 	MIPMAP_POINT,
67 	MIPMAP_LINEAR,
68 
69 	MIPMAP_LAST = MIPMAP_LINEAR
70 };
71 
72 enum AddressingMode ENUM_UNDERLYING_TYPE_UNSIGNED_INT
73 {
74 	ADDRESSING_UNUSED,
75 	ADDRESSING_WRAP,
76 	ADDRESSING_CLAMP,
77 	ADDRESSING_MIRROR,
78 	ADDRESSING_MIRRORONCE,
79 	ADDRESSING_BORDER,    // Single color
80 	ADDRESSING_SEAMLESS,  // Border of pixels
81 	ADDRESSING_CUBEFACE,  // Cube face layer
82 	ADDRESSING_TEXELFETCH,
83 
84 	ADDRESSING_LAST = ADDRESSING_TEXELFETCH
85 };
86 
87 struct Sampler
88 {
89 	VkImageViewType textureType;
90 	vk::Format textureFormat;
91 	FilterType textureFilter;
92 	AddressingMode addressingModeU;
93 	AddressingMode addressingModeV;
94 	AddressingMode addressingModeW;
95 	MipmapType mipmapFilter;
96 	VkComponentMapping swizzle;
97 	int gatherComponent;
98 	bool highPrecisionFiltering;
99 	bool compareEnable;
100 	VkCompareOp compareOp;
101 	VkBorderColor border;
102 	VkClearColorValue customBorder;
103 	bool unnormalizedCoordinates;
104 
105 	VkSamplerYcbcrModelConversion ycbcrModel;
106 	bool studioSwing;    // Narrow range
107 	bool swappedChroma;  // Cb/Cr components in reverse order
108 	FilterType chromaFilter;
109 	VkChromaLocation chromaXOffset;
110 	VkChromaLocation chromaYOffset;
111 
112 	float mipLodBias = 0.0f;
113 	float maxAnisotropy = 0.0f;
114 	float minLod = -1000.0f;
115 	float maxLod = 1000.0f;
116 
is1Dsw::Sampler117 	bool is1D() const
118 	{
119 		switch(textureType)
120 		{
121 		case VK_IMAGE_VIEW_TYPE_1D:
122 		case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
123 			return true;
124 		case VK_IMAGE_VIEW_TYPE_2D:
125 		case VK_IMAGE_VIEW_TYPE_3D:
126 		case VK_IMAGE_VIEW_TYPE_CUBE:
127 		case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
128 		case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
129 			return false;
130 		default:
131 			UNSUPPORTED("VkImageViewType %d", (int)textureType);
132 			return false;
133 		}
134 	}
135 
is2Dsw::Sampler136 	bool is2D() const
137 	{
138 		switch(textureType)
139 		{
140 		case VK_IMAGE_VIEW_TYPE_2D:
141 		case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
142 			return true;
143 		case VK_IMAGE_VIEW_TYPE_1D:
144 		case VK_IMAGE_VIEW_TYPE_3D:
145 		case VK_IMAGE_VIEW_TYPE_CUBE:
146 		case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
147 		case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
148 			return false;
149 		default:
150 			UNSUPPORTED("VkImageViewType %d", (int)textureType);
151 			return false;
152 		}
153 	}
154 
is3Dsw::Sampler155 	bool is3D() const
156 	{
157 		switch(textureType)
158 		{
159 		case VK_IMAGE_VIEW_TYPE_3D:
160 			return true;
161 		case VK_IMAGE_VIEW_TYPE_1D:
162 		case VK_IMAGE_VIEW_TYPE_2D:
163 		case VK_IMAGE_VIEW_TYPE_CUBE:
164 		case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
165 		case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
166 		case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
167 			return false;
168 		default:
169 			UNSUPPORTED("VkImageViewType %d", (int)textureType);
170 			return false;
171 		}
172 	}
173 
isCubesw::Sampler174 	bool isCube() const
175 	{
176 		switch(textureType)
177 		{
178 		case VK_IMAGE_VIEW_TYPE_CUBE:
179 		case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
180 			return true;
181 		case VK_IMAGE_VIEW_TYPE_1D:
182 		case VK_IMAGE_VIEW_TYPE_2D:
183 		case VK_IMAGE_VIEW_TYPE_3D:
184 		case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
185 		case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
186 			return false;
187 		default:
188 			UNSUPPORTED("VkImageViewType %d", (int)textureType);
189 			return false;
190 		}
191 	}
192 
isArrayedsw::Sampler193 	bool isArrayed() const
194 	{
195 		switch(textureType)
196 		{
197 		case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
198 		case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
199 		case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
200 			return true;
201 		case VK_IMAGE_VIEW_TYPE_1D:
202 		case VK_IMAGE_VIEW_TYPE_2D:
203 		case VK_IMAGE_VIEW_TYPE_3D:
204 		case VK_IMAGE_VIEW_TYPE_CUBE:
205 			return false;
206 		default:
207 			UNSUPPORTED("VkImageViewType %d", (int)textureType);
208 			return false;
209 		}
210 	}
211 
212 	// Returns the number of coordinates required to sample the image,
213 	// not including any array coordinate, which is indicated by isArrayed().
dimensionalitysw::Sampler214 	unsigned int dimensionality() const
215 	{
216 		switch(textureType)
217 		{
218 		case VK_IMAGE_VIEW_TYPE_1D:
219 		case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
220 			return 1;
221 		case VK_IMAGE_VIEW_TYPE_2D:
222 		case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
223 			return 2;
224 		case VK_IMAGE_VIEW_TYPE_3D:
225 		case VK_IMAGE_VIEW_TYPE_CUBE:
226 		case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
227 			return 3;
228 		default:
229 			UNSUPPORTED("VkImageViewType %d", (int)textureType);
230 			return 0;
231 		}
232 	}
233 };
234 
235 }  // namespace sw
236 
237 #endif  // sw_Sampler_hpp
238