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 "Main/Config.hpp"
19 #include "Renderer/Surface.hpp"
20 #include "Common/Types.hpp"
21 
22 namespace sw
23 {
24 	struct Mipmap
25 	{
26 		const void *buffer[6];
27 
28 		float4 fWidth;
29 		float4 fHeight;
30 		float4 fDepth;
31 
32 		short uHalf[4];
33 		short vHalf[4];
34 		short wHalf[4];
35 		short width[4];
36 		short height[4];
37 		short depth[4];
38 		short onePitchP[4];
39 		int4 pitchP;
40 		int4 sliceP;
41 	};
42 
43 	struct Texture
44 	{
45 		Mipmap mipmap[MIPMAP_LEVELS];
46 
47 		float LOD;
48 		float4 widthHeightLOD;
49 		float4 widthLOD;
50 		float4 heightLOD;
51 		float4 depthLOD;
52 
53 		word4 borderColor4[4];
54 		float4 borderColorF[4];
55 		float maxAnisotropy;
56 		int baseLevel;
57 		int maxLevel;
58 		float minLod;
59 		float maxLod;
60 	};
61 
62 	enum SamplerType
63 	{
64 		SAMPLER_PIXEL,
65 		SAMPLER_VERTEX
66 	};
67 
68 	enum TextureType ENUM_UNDERLYING_TYPE_UNSIGNED_INT
69 	{
70 		TEXTURE_NULL,
71 		TEXTURE_2D,
72 		TEXTURE_RECTANGLE,
73 		TEXTURE_CUBE,
74 		TEXTURE_3D,
75 		TEXTURE_2D_ARRAY,
76 
77 		TEXTURE_LAST = TEXTURE_2D_ARRAY
78 	};
79 
80 	enum FilterType ENUM_UNDERLYING_TYPE_UNSIGNED_INT
81 	{
82 		FILTER_POINT,
83 		FILTER_GATHER,
84 		FILTER_MIN_POINT_MAG_LINEAR,
85 		FILTER_MIN_LINEAR_MAG_POINT,
86 		FILTER_LINEAR,
87 		FILTER_ANISOTROPIC,
88 
89 		FILTER_LAST = FILTER_ANISOTROPIC
90 	};
91 
92 	enum MipmapType ENUM_UNDERLYING_TYPE_UNSIGNED_INT
93 	{
94 		MIPMAP_NONE,
95 		MIPMAP_POINT,
96 		MIPMAP_LINEAR,
97 
98 		MIPMAP_LAST = MIPMAP_LINEAR
99 	};
100 
101 	enum AddressingMode ENUM_UNDERLYING_TYPE_UNSIGNED_INT
102 	{
103 		ADDRESSING_WRAP,
104 		ADDRESSING_CLAMP,
105 		ADDRESSING_MIRROR,
106 		ADDRESSING_MIRRORONCE,
107 		ADDRESSING_BORDER,     // Single color
108 		ADDRESSING_SEAMLESS,   // Border of pixels
109 		ADDRESSING_LAYER,
110 		ADDRESSING_TEXELFETCH,
111 
112 		ADDRESSING_LAST = ADDRESSING_TEXELFETCH
113 	};
114 
115 	enum CompareFunc ENUM_UNDERLYING_TYPE_UNSIGNED_INT
116 	{
117 		COMPARE_BYPASS,
118 		COMPARE_LESSEQUAL,
119 		COMPARE_GREATEREQUAL,
120 		COMPARE_LESS,
121 		COMPARE_GREATER,
122 		COMPARE_EQUAL,
123 		COMPARE_NOTEQUAL,
124 		COMPARE_ALWAYS,
125 		COMPARE_NEVER,
126 
127 		COMPARE_LAST = COMPARE_NEVER
128 	};
129 
130 	enum SwizzleType ENUM_UNDERLYING_TYPE_UNSIGNED_INT
131 	{
132 		SWIZZLE_RED,
133 		SWIZZLE_GREEN,
134 		SWIZZLE_BLUE,
135 		SWIZZLE_ALPHA,
136 		SWIZZLE_ZERO,
137 		SWIZZLE_ONE,
138 
139 		SWIZZLE_LAST = SWIZZLE_ONE
140 	};
141 
142 	class Sampler
143 	{
144 	public:
145 		struct State
146 		{
147 			State();
148 
149 			TextureType textureType        : BITS(TEXTURE_LAST);
150 			Format textureFormat           : BITS(FORMAT_LAST);
151 			FilterType textureFilter       : BITS(FILTER_LAST);
152 			AddressingMode addressingModeU : BITS(ADDRESSING_LAST);
153 			AddressingMode addressingModeV : BITS(ADDRESSING_LAST);
154 			AddressingMode addressingModeW : BITS(ADDRESSING_LAST);
155 			MipmapType mipmapFilter        : BITS(FILTER_LAST);
156 			bool sRGB                      : 1;
157 			SwizzleType swizzleR           : BITS(SWIZZLE_LAST);
158 			SwizzleType swizzleG           : BITS(SWIZZLE_LAST);
159 			SwizzleType swizzleB           : BITS(SWIZZLE_LAST);
160 			SwizzleType swizzleA           : BITS(SWIZZLE_LAST);
161 			bool highPrecisionFiltering    : 1;
162 			CompareFunc compare            : BITS(COMPARE_LAST);
163 
164 			#if PERF_PROFILE
165 			bool compressedFormat          : 1;
166 			#endif
167 		};
168 
169 		Sampler();
170 
171 		~Sampler();
172 
173 		State samplerState() const;
174 
175 		void setTextureLevel(int face, int level, Surface *surface, TextureType type);
176 
177 		void setTextureFilter(FilterType textureFilter);
178 		void setMipmapFilter(MipmapType mipmapFilter);
179 		void setGatherEnable(bool enable);
180 		void setAddressingModeU(AddressingMode addressingMode);
181 		void setAddressingModeV(AddressingMode addressingMode);
182 		void setAddressingModeW(AddressingMode addressingMode);
183 		void setReadSRGB(bool sRGB);
184 		void setBorderColor(const Color<float> &borderColor);
185 		void setMaxAnisotropy(float maxAnisotropy);
186 		void setHighPrecisionFiltering(bool highPrecisionFiltering);
187 		void setSwizzleR(SwizzleType swizzleR);
188 		void setSwizzleG(SwizzleType swizzleG);
189 		void setSwizzleB(SwizzleType swizzleB);
190 		void setSwizzleA(SwizzleType swizzleA);
191 		void setCompareFunc(CompareFunc compare);
192 		void setBaseLevel(int baseLevel);
193 		void setMaxLevel(int maxLevel);
194 		void setMinLod(float minLod);
195 		void setMaxLod(float maxLod);
196 		void setSyncRequired(bool isSincRequired);
197 
198 		static void setFilterQuality(FilterType maximumFilterQuality);
199 		static void setMipmapQuality(MipmapType maximumFilterQuality);
200 		void setMipmapLOD(float lod);
201 
202 		bool hasTexture() const;
203 		bool hasUnsignedTexture() const;
204 		bool hasCubeTexture() const;
205 		bool hasVolumeTexture() const;
206 		bool requiresSync() const;
207 
208 		const Texture &getTextureData();
209 
210 	private:
211 		MipmapType mipmapFilter() const;
212 		TextureType getTextureType() const;
213 		FilterType getTextureFilter() const;
214 		AddressingMode getAddressingModeU() const;
215 		AddressingMode getAddressingModeV() const;
216 		AddressingMode getAddressingModeW() const;
217 		CompareFunc getCompareFunc() const;
218 
219 		Format externalTextureFormat;
220 		Format internalTextureFormat;
221 		TextureType textureType;
222 
223 		FilterType textureFilter;
224 		AddressingMode addressingModeU;
225 		AddressingMode addressingModeV;
226 		AddressingMode addressingModeW;
227 		MipmapType mipmapFilterState;
228 		bool sRGB;
229 		bool gather;
230 		bool highPrecisionFiltering;
231 		bool syncRequired;
232 		int border;
233 
234 		SwizzleType swizzleR;
235 		SwizzleType swizzleG;
236 		SwizzleType swizzleB;
237 		SwizzleType swizzleA;
238 		CompareFunc compare;
239 
240 		Texture texture;
241 		float exp2LOD;
242 
243 		static FilterType maximumTextureFilterQuality;
244 		static MipmapType maximumMipmapFilterQuality;
245 	};
246 }
247 
248 #endif   // sw_Sampler_hpp
249