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