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