blob: d700a5df12bdcc5df35fb5ad2e746df4518e8fae [file] [log] [blame]
Nicolas Capens68a82382018-10-02 13:16:55 -04001// 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#include "SamplerCore.hpp"
16
17#include "Constants.hpp"
Ben Claytonbc1c067be2019-12-17 20:37:37 +000018#include "PixelRoutine.hpp"
Ben Clayton25e06e02020-02-07 11:19:08 +000019#include "System/Debug.hpp"
Ben Claytonbc1c067be2019-12-17 20:37:37 +000020#include "Vulkan/VkSampler.hpp"
Nicolas Capens68a82382018-10-02 13:16:55 -040021
Nicolas Capens157ba262019-12-10 17:49:14 -050022namespace sw {
23
Nicolas Capens9c273852021-12-15 13:44:47 -050024SamplerCore::SamplerCore(Pointer<Byte> &constants, const Sampler &state, SamplerFunction function)
Ben Claytonbc1c067be2019-12-17 20:37:37 +000025 : constants(constants)
26 , state(state)
Nicolas Capens9c273852021-12-15 13:44:47 -050027 , function(function)
Nicolas Capens68a82382018-10-02 13:16:55 -040028{
Nicolas Capens157ba262019-12-10 17:49:14 -050029}
Nicolas Capens9c273852021-12-15 13:44:47 -050030Vector4f SamplerCore::sampleTexture(Pointer<Byte> &texture, Float4 uvwa[4], Float4 &dRef, Float &&lodOrBias, Float4 &dsx, Float4 &dsy, Vector4i &offset, Int4 &sample)
Nicolas Capens157ba262019-12-10 17:49:14 -050031{
32 Vector4f c;
33
Nicolas Capens30873702020-08-01 09:17:23 -040034 Float4 u = uvwa[0];
35 Float4 v = uvwa[1];
36 Float4 w = uvwa[2];
37 Float4 a; // Array layer coordinate
38 switch(state.textureType)
39 {
Nicolas Capens112faf42019-12-13 17:32:26 -050040 case VK_IMAGE_VIEW_TYPE_1D_ARRAY: a = uvwa[1]; break;
41 case VK_IMAGE_VIEW_TYPE_2D_ARRAY: a = uvwa[2]; break;
42 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY: a = uvwa[3]; break;
43 default: break;
Nicolas Capens30873702020-08-01 09:17:23 -040044 }
Nicolas Capens157ba262019-12-10 17:49:14 -050045
46 Float lod;
47 Float anisotropy;
48 Float4 uDelta;
49 Float4 vDelta;
50 Float4 M; // Major axis
51
Nicolas Capensa202c202020-08-05 14:42:22 -040052 if(state.isCube())
Nicolas Capens68a82382018-10-02 13:16:55 -040053 {
Nicolas Capens30873702020-08-01 09:17:23 -040054 Int4 face = cubeFace(u, v, uvwa[0], uvwa[1], uvwa[2], M);
55 w = As<Float4>(face);
Nicolas Capens68a82382018-10-02 13:16:55 -040056 }
57
Nicolas Capensd4483092021-08-03 11:11:10 -040058 // Determine if we can skip the LOD computation. This is the case when the mipmap has only one level, except for LOD query,
59 // where we have to return the computed value. Anisotropic filtering requires computing the anisotropy factor even for a single mipmap level.
60 bool singleMipLevel = (state.minLod == state.maxLod);
Nicolas Capens15f39442021-08-04 00:03:40 -040061 bool requiresLodComputation = (function == Query) || (state.textureFilter == FILTER_ANISOTROPIC);
Nicolas Capensd4483092021-08-03 11:11:10 -040062 bool skipLodComputation = singleMipLevel && !requiresLodComputation;
Alexis Hetuf239c9f2021-02-09 18:02:11 -050063
Nicolas Capensd4483092021-08-03 11:11:10 -040064 if(skipLodComputation)
Alexis Hetuf239c9f2021-02-09 18:02:11 -050065 {
Alexis Hetuf239c9f2021-02-09 18:02:11 -050066 lod = state.minLod;
67 }
68 else if(function == Implicit || function == Bias || function == Grad || function == Query)
Nicolas Capens68a82382018-10-02 13:16:55 -040069 {
Nicolas Capensa202c202020-08-05 14:42:22 -040070 if(state.is1D())
Nicolas Capens2d548402019-04-16 10:41:01 -040071 {
Nicolas Capens9c273852021-12-15 13:44:47 -050072 computeLod1D(texture, lod, u, dsx, dsy);
Nicolas Capens73c24e42020-08-05 09:50:20 -040073 }
Nicolas Capensa202c202020-08-05 14:42:22 -040074 else if(state.is2D())
Nicolas Capens73c24e42020-08-05 09:50:20 -040075 {
Nicolas Capens9c273852021-12-15 13:44:47 -050076 computeLod2D(texture, lod, anisotropy, uDelta, vDelta, u, v, dsx, dsy);
Nicolas Capens73c24e42020-08-05 09:50:20 -040077 }
Nicolas Capensa202c202020-08-05 14:42:22 -040078 else if(state.isCube())
Nicolas Capens73c24e42020-08-05 09:50:20 -040079 {
Nicolas Capens9c273852021-12-15 13:44:47 -050080 computeLodCube(texture, lod, uvwa[0], uvwa[1], uvwa[2], dsx, dsy, M);
Nicolas Capens157ba262019-12-10 17:49:14 -050081 }
82 else
83 {
Nicolas Capens9c273852021-12-15 13:44:47 -050084 computeLod3D(texture, lod, u, v, w, dsx, dsy);
Nicolas Capens157ba262019-12-10 17:49:14 -050085 }
Nicolas Capensb1670ed2019-05-02 00:14:17 -040086
Nicolas Capens7f469172020-03-17 17:29:11 -040087 Float bias = state.mipLodBias;
Nicolas Capens324bdfe2019-07-30 17:27:56 -040088
Nicolas Capens157ba262019-12-10 17:49:14 -050089 if(function == Bias)
90 {
91 // Add SPIR-V Bias operand to the sampler provided bias and clamp to maxSamplerLodBias limit.
92 bias = Min(Max(bias + lodOrBias, -vk::MAX_SAMPLER_LOD_BIAS), vk::MAX_SAMPLER_LOD_BIAS);
93 }
94
95 lod += bias;
96 }
97 else if(function == Lod)
98 {
99 // Vulkan 1.1: "The absolute value of mipLodBias must be less than or equal to VkPhysicalDeviceLimits::maxSamplerLodBias"
100 // Hence no explicit clamping to maxSamplerLodBias is required in this case.
Nicolas Capens7f469172020-03-17 17:29:11 -0400101 lod = lodOrBias + state.mipLodBias;
Nicolas Capens157ba262019-12-10 17:49:14 -0500102 }
103 else if(function == Fetch)
104 {
105 // TODO: Eliminate int-float-int conversion.
106 lod = Float(As<Int>(lodOrBias));
107 }
108 else if(function == Base || function == Gather)
109 {
110 lod = Float(0);
111 }
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000112 else
113 UNREACHABLE("Sampler function %d", int(function));
Nicolas Capens157ba262019-12-10 17:49:14 -0500114
115 if(function != Base && function != Fetch && function != Gather)
116 {
117 if(function == Query)
118 {
119 c.y = Float4(lod); // Unclamped LOD.
120 }
121
Nicolas Capensd4483092021-08-03 11:11:10 -0400122 if(!skipLodComputation)
Alexis Hetuf239c9f2021-02-09 18:02:11 -0500123 {
124 lod = Max(lod, state.minLod);
125 lod = Min(lod, state.maxLod);
126 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500127
128 if(function == Query)
129 {
130 if(state.mipmapFilter == MIPMAP_POINT)
Nicolas Capensb1670ed2019-05-02 00:14:17 -0400131 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500132 lod = Round(lod); // TODO: Preferred formula is ceil(lod + 0.5) - 1
Nicolas Capens68a82382018-10-02 13:16:55 -0400133 }
Nicolas Capens324bdfe2019-07-30 17:27:56 -0400134
Nicolas Capens157ba262019-12-10 17:49:14 -0500135 c.x = lod;
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000136 // c.y contains unclamped LOD.
Nicolas Capens157ba262019-12-10 17:49:14 -0500137
138 return c;
Nicolas Capens68a82382018-10-02 13:16:55 -0400139 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500140 }
141
142 bool force32BitFiltering = state.highPrecisionFiltering && !isYcbcrFormat() && (state.textureFilter != FILTER_POINT);
Nicolas Capens157ba262019-12-10 17:49:14 -0500143 bool use32BitFiltering = hasFloatTexture() || hasUnnormalizedIntegerTexture() || force32BitFiltering ||
Nicolas Capensa202c202020-08-05 14:42:22 -0400144 state.isCube() || state.unnormalizedCoordinates || state.compareEnable ||
Nicolas Capens8866a852020-07-03 09:13:43 -0400145 borderModeActive() || (function == Gather) || (function == Fetch);
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200146 int numComponents = (function == Gather) ? 4 : textureComponentCount();
Nicolas Capens157ba262019-12-10 17:49:14 -0500147
148 if(use32BitFiltering)
149 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500150 c = sampleFloatFilter(texture, u, v, w, a, dRef, offset, sample, lod, anisotropy, uDelta, vDelta);
Nicolas Capens112faf42019-12-13 17:32:26 -0500151 }
152 else // 16-bit filtering.
153 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500154 Vector4s cs = sampleFilter(texture, u, v, w, a, offset, sample, lod, anisotropy, uDelta, vDelta);
Nicolas Capens112faf42019-12-13 17:32:26 -0500155
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200156 for(int component = 0; component < numComponents; component++)
Nicolas Capens112faf42019-12-13 17:32:26 -0500157 {
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200158 if(hasUnsignedTextureComponent(component))
Nicolas Capens112faf42019-12-13 17:32:26 -0500159 {
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200160 c[component] = Float4(As<UShort4>(cs[component]));
Nicolas Capens112faf42019-12-13 17:32:26 -0500161 }
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200162 else
163 {
164 c[component] = Float4(cs[component]);
165 }
Nicolas Capens592d4132021-12-09 19:37:53 -0500166 }
167 }
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200168
Nicolas Capens592d4132021-12-09 19:37:53 -0500169 if(hasNormalizedFormat() && !state.compareEnable)
170 {
171 sw::float4 scale = getComponentScale();
172
173 for(int component = 0; component < numComponents; component++)
174 {
175 int texelComponent = (function == Gather) ? getGatherComponent() : component;
176 c[component] *= Float4(1.0f / scale[texelComponent]);
Ari Suonpääcbec1d42021-11-23 08:04:50 +0200177 }
178 }
179
180 if(state.textureFormat.isSignedNormalized())
181 {
182 for(int component = 0; component < numComponents; component++)
183 {
184 c[component] = Max(c[component], Float4(-1.0f));
Nicolas Capens157ba262019-12-10 17:49:14 -0500185 }
186 }
187
188 if(state.textureFilter != FILTER_GATHER)
189 {
190 if((state.swizzle.r != VK_COMPONENT_SWIZZLE_R) ||
191 (state.swizzle.g != VK_COMPONENT_SWIZZLE_G) ||
192 (state.swizzle.b != VK_COMPONENT_SWIZZLE_B) ||
193 (state.swizzle.a != VK_COMPONENT_SWIZZLE_A))
194 {
Nicolas Capens30873702020-08-01 09:17:23 -0400195 const Vector4f col = c;
Nicolas Capens157ba262019-12-10 17:49:14 -0500196 bool integer = hasUnnormalizedIntegerTexture();
Nicolas Capens30873702020-08-01 09:17:23 -0400197 c.x = applySwizzle(col, state.swizzle.r, integer);
198 c.y = applySwizzle(col, state.swizzle.g, integer);
199 c.z = applySwizzle(col, state.swizzle.b, integer);
200 c.w = applySwizzle(col, state.swizzle.a, integer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500201 }
202 }
203 else // Gather
204 {
205 VkComponentSwizzle swizzle = gatherSwizzle();
206
207 // R/G/B/A swizzles affect the component collected from each texel earlier.
208 // Handle the ZERO and ONE cases here because we don't need to know the format.
209
210 if(swizzle == VK_COMPONENT_SWIZZLE_ZERO)
211 {
212 c.x = c.y = c.z = c.w = Float4(0);
213 }
214 else if(swizzle == VK_COMPONENT_SWIZZLE_ONE)
215 {
216 bool integer = hasUnnormalizedIntegerTexture();
217 c.x = c.y = c.z = c.w = integer ? As<Float4>(Int4(1)) : RValue<Float4>(Float4(1.0f));
218 }
219 }
220
221 return c;
222}
223
Nicolas Capens30873702020-08-01 09:17:23 -0400224Float4 SamplerCore::applySwizzle(const Vector4f &c, VkComponentSwizzle swizzle, bool integer)
225{
226 switch(swizzle)
227 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500228 default: UNSUPPORTED("VkComponentSwizzle %d", (int)swizzle);
229 case VK_COMPONENT_SWIZZLE_R: return c.x;
230 case VK_COMPONENT_SWIZZLE_G: return c.y;
231 case VK_COMPONENT_SWIZZLE_B: return c.z;
232 case VK_COMPONENT_SWIZZLE_A: return c.w;
233 case VK_COMPONENT_SWIZZLE_ZERO: return Float4(0.0f, 0.0f, 0.0f, 0.0f);
234 case VK_COMPONENT_SWIZZLE_ONE:
235 if(integer)
236 {
237 return Float4(As<Float4>(sw::Int4(1, 1, 1, 1)));
238 }
239 else
240 {
241 return Float4(1.0f, 1.0f, 1.0f, 1.0f);
242 }
243 break;
Nicolas Capens30873702020-08-01 09:17:23 -0400244 }
245};
246
Nicolas Capens157ba262019-12-10 17:49:14 -0500247Short4 SamplerCore::offsetSample(Short4 &uvw, Pointer<Byte> &mipmap, int halfOffset, bool wrap, int count, Float &lod)
248{
249 Short4 offset = *Pointer<Short4>(mipmap + halfOffset);
250
251 if(state.textureFilter == FILTER_MIN_LINEAR_MAG_POINT)
252 {
253 offset &= Short4(CmpNLE(Float4(lod), Float4(0.0f)));
254 }
255 else if(state.textureFilter == FILTER_MIN_POINT_MAG_LINEAR)
256 {
257 offset &= Short4(CmpLE(Float4(lod), Float4(0.0f)));
258 }
259
260 if(wrap)
261 {
262 switch(count)
263 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500264 case -1: return uvw - offset;
265 case 0: return uvw;
266 case +1: return uvw + offset;
267 case 2: return uvw + offset + offset;
Nicolas Capens157ba262019-12-10 17:49:14 -0500268 }
269 }
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000270 else // Clamp or mirror
Nicolas Capens157ba262019-12-10 17:49:14 -0500271 {
272 switch(count)
273 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500274 case -1: return SubSat(As<UShort4>(uvw), As<UShort4>(offset));
275 case 0: return uvw;
276 case +1: return AddSat(As<UShort4>(uvw), As<UShort4>(offset));
277 case 2: return AddSat(AddSat(As<UShort4>(uvw), As<UShort4>(offset)), As<UShort4>(offset));
Nicolas Capens157ba262019-12-10 17:49:14 -0500278 }
279 }
280
281 return uvw;
282}
283
Nicolas Capens9c273852021-12-15 13:44:47 -0500284Vector4s SamplerCore::sampleFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta)
Nicolas Capens157ba262019-12-10 17:49:14 -0500285{
Nicolas Capens9c273852021-12-15 13:44:47 -0500286 Vector4s c = sampleAniso(texture, u, v, w, a, offset, sample, lod, anisotropy, uDelta, vDelta, false);
Nicolas Capens157ba262019-12-10 17:49:14 -0500287
288 if(function == Fetch)
289 {
290 return c;
291 }
292
293 if(state.mipmapFilter == MIPMAP_LINEAR)
294 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500295 Vector4s cc = sampleAniso(texture, u, v, w, a, offset, sample, lod, anisotropy, uDelta, vDelta, true);
Nicolas Capens157ba262019-12-10 17:49:14 -0500296
297 lod *= Float(1 << 16);
298
Nicolas Capens9c273852021-12-15 13:44:47 -0500299 UShort4 utri = UShort4(Float4(lod)); // TODO: Optimize
300 Short4 stri = utri >> 1; // TODO: Optimize
Nicolas Capens157ba262019-12-10 17:49:14 -0500301
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000302 if(hasUnsignedTextureComponent(0))
303 cc.x = MulHigh(As<UShort4>(cc.x), utri);
304 else
305 cc.x = MulHigh(cc.x, stri);
306 if(hasUnsignedTextureComponent(1))
307 cc.y = MulHigh(As<UShort4>(cc.y), utri);
308 else
309 cc.y = MulHigh(cc.y, stri);
310 if(hasUnsignedTextureComponent(2))
311 cc.z = MulHigh(As<UShort4>(cc.z), utri);
312 else
313 cc.z = MulHigh(cc.z, stri);
314 if(hasUnsignedTextureComponent(3))
315 cc.w = MulHigh(As<UShort4>(cc.w), utri);
316 else
317 cc.w = MulHigh(cc.w, stri);
Nicolas Capens157ba262019-12-10 17:49:14 -0500318
319 utri = ~utri;
320 stri = Short4(0x7FFF) - stri;
321
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000322 if(hasUnsignedTextureComponent(0))
323 c.x = MulHigh(As<UShort4>(c.x), utri);
324 else
325 c.x = MulHigh(c.x, stri);
326 if(hasUnsignedTextureComponent(1))
327 c.y = MulHigh(As<UShort4>(c.y), utri);
328 else
329 c.y = MulHigh(c.y, stri);
330 if(hasUnsignedTextureComponent(2))
331 c.z = MulHigh(As<UShort4>(c.z), utri);
332 else
333 c.z = MulHigh(c.z, stri);
334 if(hasUnsignedTextureComponent(3))
335 c.w = MulHigh(As<UShort4>(c.w), utri);
336 else
337 c.w = MulHigh(c.w, stri);
Nicolas Capens157ba262019-12-10 17:49:14 -0500338
339 c.x += cc.x;
340 c.y += cc.y;
341 c.z += cc.z;
342 c.w += cc.w;
343
344 if(!hasUnsignedTextureComponent(0)) c.x += c.x;
345 if(!hasUnsignedTextureComponent(1)) c.y += c.y;
346 if(!hasUnsignedTextureComponent(2)) c.z += c.z;
347 if(!hasUnsignedTextureComponent(3)) c.w += c.w;
348 }
349
350 return c;
351}
352
Nicolas Capens9c273852021-12-15 13:44:47 -0500353Vector4s SamplerCore::sampleAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500354{
355 Vector4s c;
356
Nicolas Capens73c24e42020-08-05 09:50:20 -0400357 if(state.textureFilter != FILTER_ANISOTROPIC)
Nicolas Capens157ba262019-12-10 17:49:14 -0500358 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500359 c = sampleQuad(texture, u, v, w, a, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500360 }
361 else
362 {
Nicolas Capens30873702020-08-01 09:17:23 -0400363 Int N = RoundInt(anisotropy);
Nicolas Capens157ba262019-12-10 17:49:14 -0500364
365 Vector4s cSum;
366
367 cSum.x = Short4(0);
368 cSum.y = Short4(0);
369 cSum.z = Short4(0);
370 cSum.w = Short4(0);
371
Nicolas Capens30873702020-08-01 09:17:23 -0400372 Float4 A = *Pointer<Float4>(constants + OFFSET(Constants, uvWeight) + 16 * N);
373 Float4 B = *Pointer<Float4>(constants + OFFSET(Constants, uvStart) + 16 * N);
374 UShort4 cw = *Pointer<UShort4>(constants + OFFSET(Constants, cWeight) + 8 * N);
Nicolas Capens157ba262019-12-10 17:49:14 -0500375 Short4 sw = Short4(cw >> 1);
376
377 Float4 du = uDelta;
378 Float4 dv = vDelta;
379
380 Float4 u0 = u + B * du;
381 Float4 v0 = v + B * dv;
382
383 du *= A;
384 dv *= A;
385
386 Int i = 0;
387
388 Do
389 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500390 c = sampleQuad(texture, u0, v0, w, a, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500391
392 u0 += du;
393 v0 += dv;
394
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000395 if(hasUnsignedTextureComponent(0))
396 cSum.x += As<Short4>(MulHigh(As<UShort4>(c.x), cw));
397 else
398 cSum.x += MulHigh(c.x, sw);
399 if(hasUnsignedTextureComponent(1))
400 cSum.y += As<Short4>(MulHigh(As<UShort4>(c.y), cw));
401 else
402 cSum.y += MulHigh(c.y, sw);
403 if(hasUnsignedTextureComponent(2))
404 cSum.z += As<Short4>(MulHigh(As<UShort4>(c.z), cw));
405 else
406 cSum.z += MulHigh(c.z, sw);
407 if(hasUnsignedTextureComponent(3))
408 cSum.w += As<Short4>(MulHigh(As<UShort4>(c.w), cw));
409 else
410 cSum.w += MulHigh(c.w, sw);
Nicolas Capens157ba262019-12-10 17:49:14 -0500411
412 i++;
413 }
Nicolas Capens30873702020-08-01 09:17:23 -0400414 Until(i >= N);
Nicolas Capens157ba262019-12-10 17:49:14 -0500415
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000416 if(hasUnsignedTextureComponent(0))
417 c.x = cSum.x;
418 else
419 c.x = AddSat(cSum.x, cSum.x);
420 if(hasUnsignedTextureComponent(1))
421 c.y = cSum.y;
422 else
423 c.y = AddSat(cSum.y, cSum.y);
424 if(hasUnsignedTextureComponent(2))
425 c.z = cSum.z;
426 else
427 c.z = AddSat(cSum.z, cSum.z);
428 if(hasUnsignedTextureComponent(3))
429 c.w = cSum.w;
430 else
431 c.w = AddSat(cSum.w, cSum.w);
Nicolas Capens157ba262019-12-10 17:49:14 -0500432 }
433
434 return c;
435}
436
Nicolas Capens9c273852021-12-15 13:44:47 -0500437Vector4s SamplerCore::sampleQuad(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500438{
439 if(state.textureType != VK_IMAGE_VIEW_TYPE_3D)
440 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500441 return sampleQuad2D(texture, u, v, w, a, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500442 }
443 else
444 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500445 return sample3D(texture, u, v, w, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500446 }
447}
448
Nicolas Capens9c273852021-12-15 13:44:47 -0500449Vector4s SamplerCore::sampleQuad2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500450{
451 Vector4s c;
452
453 int componentCount = textureComponentCount();
454 bool gather = (state.textureFilter == FILTER_GATHER);
455
Nicolas Capens83373b92021-12-15 06:43:29 -0500456 Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
457 Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
Nicolas Capens157ba262019-12-10 17:49:14 -0500458
Nicolas Capens8866a852020-07-03 09:13:43 -0400459 Short4 uuuu = address(u, state.addressingModeU, mipmap);
460 Short4 vvvv = address(v, state.addressingModeV, mipmap);
461 Short4 wwww = address(w, state.addressingModeW, mipmap);
Nicolas Capens9c273852021-12-15 13:44:47 -0500462 Short4 layerIndex = computeLayerIndex16(a, mipmap);
Nicolas Capens157ba262019-12-10 17:49:14 -0500463
Nicolas Capens8866a852020-07-03 09:13:43 -0400464 if(state.textureFilter == FILTER_POINT)
Nicolas Capens157ba262019-12-10 17:49:14 -0500465 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500466 c = sampleTexel(uuuu, vvvv, wwww, layerIndex, offset, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500467 }
468 else
469 {
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000470 Short4 uuuu0 = offsetSample(uuuu, mipmap, OFFSET(Mipmap, uHalf), state.addressingModeU == ADDRESSING_WRAP, -1, lod);
471 Short4 vvvv0 = offsetSample(vvvv, mipmap, OFFSET(Mipmap, vHalf), state.addressingModeV == ADDRESSING_WRAP, -1, lod);
472 Short4 uuuu1 = offsetSample(uuuu, mipmap, OFFSET(Mipmap, uHalf), state.addressingModeU == ADDRESSING_WRAP, +1, lod);
473 Short4 vvvv1 = offsetSample(vvvv, mipmap, OFFSET(Mipmap, vHalf), state.addressingModeV == ADDRESSING_WRAP, +1, lod);
Nicolas Capens157ba262019-12-10 17:49:14 -0500474
Nicolas Capens9c273852021-12-15 13:44:47 -0500475 Vector4s c00 = sampleTexel(uuuu0, vvvv0, wwww, layerIndex, offset, sample, mipmap, buffer);
476 Vector4s c10 = sampleTexel(uuuu1, vvvv0, wwww, layerIndex, offset, sample, mipmap, buffer);
477 Vector4s c01 = sampleTexel(uuuu0, vvvv1, wwww, layerIndex, offset, sample, mipmap, buffer);
478 Vector4s c11 = sampleTexel(uuuu1, vvvv1, wwww, layerIndex, offset, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500479
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000480 if(!gather) // Blend
Nicolas Capens157ba262019-12-10 17:49:14 -0500481 {
482 // Fractions
Nicolas Capens71196862022-01-06 21:31:57 -0500483 UShort4 f0u = As<UShort4>(uuuu0) * UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, width)));
484 UShort4 f0v = As<UShort4>(vvvv0) * UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, height)));
Nicolas Capens157ba262019-12-10 17:49:14 -0500485
486 UShort4 f1u = ~f0u;
487 UShort4 f1v = ~f0v;
488
489 UShort4 f0u0v = MulHigh(f0u, f0v);
490 UShort4 f1u0v = MulHigh(f1u, f0v);
491 UShort4 f0u1v = MulHigh(f0u, f1v);
492 UShort4 f1u1v = MulHigh(f1u, f1v);
493
494 // Signed fractions
495 Short4 f1u1vs;
496 Short4 f0u1vs;
497 Short4 f1u0vs;
498 Short4 f0u0vs;
499
500 if(!hasUnsignedTextureComponent(0) || !hasUnsignedTextureComponent(1) || !hasUnsignedTextureComponent(2) || !hasUnsignedTextureComponent(3))
501 {
502 f1u1vs = f1u1v >> 1;
503 f0u1vs = f0u1v >> 1;
504 f1u0vs = f1u0v >> 1;
505 f0u0vs = f0u0v >> 1;
506 }
507
508 // Bilinear interpolation
509 if(componentCount >= 1)
510 {
511 if(has16bitTextureComponents() && hasUnsignedTextureComponent(0))
512 {
513 c00.x = As<UShort4>(c00.x) - MulHigh(As<UShort4>(c00.x), f0u) + MulHigh(As<UShort4>(c10.x), f0u);
514 c01.x = As<UShort4>(c01.x) - MulHigh(As<UShort4>(c01.x), f0u) + MulHigh(As<UShort4>(c11.x), f0u);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000515 c.x = As<UShort4>(c00.x) - MulHigh(As<UShort4>(c00.x), f0v) + MulHigh(As<UShort4>(c01.x), f0v);
Nicolas Capens157ba262019-12-10 17:49:14 -0500516 }
517 else
518 {
519 if(hasUnsignedTextureComponent(0))
520 {
521 c00.x = MulHigh(As<UShort4>(c00.x), f1u1v);
522 c10.x = MulHigh(As<UShort4>(c10.x), f0u1v);
523 c01.x = MulHigh(As<UShort4>(c01.x), f1u0v);
524 c11.x = MulHigh(As<UShort4>(c11.x), f0u0v);
525 }
526 else
527 {
528 c00.x = MulHigh(c00.x, f1u1vs);
529 c10.x = MulHigh(c10.x, f0u1vs);
530 c01.x = MulHigh(c01.x, f1u0vs);
531 c11.x = MulHigh(c11.x, f0u0vs);
532 }
533
534 c.x = (c00.x + c10.x) + (c01.x + c11.x);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000535 if(!hasUnsignedTextureComponent(0)) c.x = AddSat(c.x, c.x); // Correct for signed fractions
Nicolas Capens157ba262019-12-10 17:49:14 -0500536 }
537 }
538
539 if(componentCount >= 2)
540 {
541 if(has16bitTextureComponents() && hasUnsignedTextureComponent(1))
542 {
543 c00.y = As<UShort4>(c00.y) - MulHigh(As<UShort4>(c00.y), f0u) + MulHigh(As<UShort4>(c10.y), f0u);
544 c01.y = As<UShort4>(c01.y) - MulHigh(As<UShort4>(c01.y), f0u) + MulHigh(As<UShort4>(c11.y), f0u);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000545 c.y = As<UShort4>(c00.y) - MulHigh(As<UShort4>(c00.y), f0v) + MulHigh(As<UShort4>(c01.y), f0v);
Nicolas Capens157ba262019-12-10 17:49:14 -0500546 }
547 else
548 {
549 if(hasUnsignedTextureComponent(1))
550 {
551 c00.y = MulHigh(As<UShort4>(c00.y), f1u1v);
552 c10.y = MulHigh(As<UShort4>(c10.y), f0u1v);
553 c01.y = MulHigh(As<UShort4>(c01.y), f1u0v);
554 c11.y = MulHigh(As<UShort4>(c11.y), f0u0v);
555 }
556 else
557 {
558 c00.y = MulHigh(c00.y, f1u1vs);
559 c10.y = MulHigh(c10.y, f0u1vs);
560 c01.y = MulHigh(c01.y, f1u0vs);
561 c11.y = MulHigh(c11.y, f0u0vs);
562 }
563
564 c.y = (c00.y + c10.y) + (c01.y + c11.y);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000565 if(!hasUnsignedTextureComponent(1)) c.y = AddSat(c.y, c.y); // Correct for signed fractions
Nicolas Capens157ba262019-12-10 17:49:14 -0500566 }
567 }
568
569 if(componentCount >= 3)
570 {
571 if(has16bitTextureComponents() && hasUnsignedTextureComponent(2))
572 {
573 c00.z = As<UShort4>(c00.z) - MulHigh(As<UShort4>(c00.z), f0u) + MulHigh(As<UShort4>(c10.z), f0u);
574 c01.z = As<UShort4>(c01.z) - MulHigh(As<UShort4>(c01.z), f0u) + MulHigh(As<UShort4>(c11.z), f0u);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000575 c.z = As<UShort4>(c00.z) - MulHigh(As<UShort4>(c00.z), f0v) + MulHigh(As<UShort4>(c01.z), f0v);
Nicolas Capens157ba262019-12-10 17:49:14 -0500576 }
577 else
578 {
579 if(hasUnsignedTextureComponent(2))
580 {
581 c00.z = MulHigh(As<UShort4>(c00.z), f1u1v);
582 c10.z = MulHigh(As<UShort4>(c10.z), f0u1v);
583 c01.z = MulHigh(As<UShort4>(c01.z), f1u0v);
584 c11.z = MulHigh(As<UShort4>(c11.z), f0u0v);
585 }
586 else
587 {
588 c00.z = MulHigh(c00.z, f1u1vs);
589 c10.z = MulHigh(c10.z, f0u1vs);
590 c01.z = MulHigh(c01.z, f1u0vs);
591 c11.z = MulHigh(c11.z, f0u0vs);
592 }
593
594 c.z = (c00.z + c10.z) + (c01.z + c11.z);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000595 if(!hasUnsignedTextureComponent(2)) c.z = AddSat(c.z, c.z); // Correct for signed fractions
Nicolas Capens157ba262019-12-10 17:49:14 -0500596 }
597 }
598
599 if(componentCount >= 4)
600 {
601 if(has16bitTextureComponents() && hasUnsignedTextureComponent(3))
602 {
603 c00.w = As<UShort4>(c00.w) - MulHigh(As<UShort4>(c00.w), f0u) + MulHigh(As<UShort4>(c10.w), f0u);
604 c01.w = As<UShort4>(c01.w) - MulHigh(As<UShort4>(c01.w), f0u) + MulHigh(As<UShort4>(c11.w), f0u);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000605 c.w = As<UShort4>(c00.w) - MulHigh(As<UShort4>(c00.w), f0v) + MulHigh(As<UShort4>(c01.w), f0v);
Nicolas Capens157ba262019-12-10 17:49:14 -0500606 }
607 else
608 {
609 if(hasUnsignedTextureComponent(3))
610 {
611 c00.w = MulHigh(As<UShort4>(c00.w), f1u1v);
612 c10.w = MulHigh(As<UShort4>(c10.w), f0u1v);
613 c01.w = MulHigh(As<UShort4>(c01.w), f1u0v);
614 c11.w = MulHigh(As<UShort4>(c11.w), f0u0v);
615 }
616 else
617 {
618 c00.w = MulHigh(c00.w, f1u1vs);
619 c10.w = MulHigh(c10.w, f0u1vs);
620 c01.w = MulHigh(c01.w, f1u0vs);
621 c11.w = MulHigh(c11.w, f0u0vs);
622 }
623
624 c.w = (c00.w + c10.w) + (c01.w + c11.w);
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000625 if(!hasUnsignedTextureComponent(3)) c.w = AddSat(c.w, c.w); // Correct for signed fractions
Nicolas Capens157ba262019-12-10 17:49:14 -0500626 }
Nicolas Capens57253152019-05-10 20:25:17 -0700627 }
628 }
629 else // Gather
630 {
631 VkComponentSwizzle swizzle = gatherSwizzle();
Nicolas Capens157ba262019-12-10 17:49:14 -0500632 switch(swizzle)
Nicolas Capens57253152019-05-10 20:25:17 -0700633 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500634 case VK_COMPONENT_SWIZZLE_ZERO:
635 case VK_COMPONENT_SWIZZLE_ONE:
636 // Handled at the final component swizzle.
637 break;
638 default:
639 c.x = c01[swizzle - VK_COMPONENT_SWIZZLE_R];
640 c.y = c11[swizzle - VK_COMPONENT_SWIZZLE_R];
641 c.z = c10[swizzle - VK_COMPONENT_SWIZZLE_R];
642 c.w = c00[swizzle - VK_COMPONENT_SWIZZLE_R];
643 break;
Nicolas Capens57253152019-05-10 20:25:17 -0700644 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500645 }
646 }
647
648 return c;
649}
650
Nicolas Capens9c273852021-12-15 13:44:47 -0500651Vector4s SamplerCore::sample3D(Pointer<Byte> &texture, Float4 &u_, Float4 &v_, Float4 &w_, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500652{
653 Vector4s c_;
654
655 int componentCount = textureComponentCount();
656
Nicolas Capens83373b92021-12-15 06:43:29 -0500657 Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
658 Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
Nicolas Capens157ba262019-12-10 17:49:14 -0500659
Nicolas Capens8866a852020-07-03 09:13:43 -0400660 Short4 uuuu = address(u_, state.addressingModeU, mipmap);
661 Short4 vvvv = address(v_, state.addressingModeV, mipmap);
662 Short4 wwww = address(w_, state.addressingModeW, mipmap);
Nicolas Capens157ba262019-12-10 17:49:14 -0500663
Nicolas Capens8866a852020-07-03 09:13:43 -0400664 if(state.textureFilter == FILTER_POINT)
Nicolas Capens157ba262019-12-10 17:49:14 -0500665 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500666 c_ = sampleTexel(uuuu, vvvv, wwww, 0, offset, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500667 }
668 else
669 {
670 Vector4s c[2][2][2];
671
672 Short4 u[2][2][2];
673 Short4 v[2][2][2];
674 Short4 s[2][2][2];
675
676 for(int i = 0; i < 2; i++)
677 {
678 for(int j = 0; j < 2; j++)
Nicolas Capens57253152019-05-10 20:25:17 -0700679 {
Nicolas Capens157ba262019-12-10 17:49:14 -0500680 for(int k = 0; k < 2; k++)
681 {
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000682 u[i][j][k] = offsetSample(uuuu, mipmap, OFFSET(Mipmap, uHalf), state.addressingModeU == ADDRESSING_WRAP, i * 2 - 1, lod);
683 v[i][j][k] = offsetSample(vvvv, mipmap, OFFSET(Mipmap, vHalf), state.addressingModeV == ADDRESSING_WRAP, j * 2 - 1, lod);
684 s[i][j][k] = offsetSample(wwww, mipmap, OFFSET(Mipmap, wHalf), state.addressingModeW == ADDRESSING_WRAP, k * 2 - 1, lod);
Nicolas Capens157ba262019-12-10 17:49:14 -0500685 }
Nicolas Capens57253152019-05-10 20:25:17 -0700686 }
Nicolas Capens2d548402019-04-16 10:41:01 -0400687 }
688
Nicolas Capens157ba262019-12-10 17:49:14 -0500689 // Fractions
Nicolas Capens71196862022-01-06 21:31:57 -0500690 UShort4 f0u = As<UShort4>(u[0][0][0]) * UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, width)));
691 UShort4 f0v = As<UShort4>(v[0][0][0]) * UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, height)));
692 UShort4 f0s = As<UShort4>(s[0][0][0]) * UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, depth)));
Nicolas Capens157ba262019-12-10 17:49:14 -0500693
694 UShort4 f1u = ~f0u;
695 UShort4 f1v = ~f0v;
696 UShort4 f1s = ~f0s;
697
698 UShort4 f[2][2][2];
699 Short4 fs[2][2][2];
700
701 f[1][1][1] = MulHigh(f1u, f1v);
702 f[0][1][1] = MulHigh(f0u, f1v);
703 f[1][0][1] = MulHigh(f1u, f0v);
704 f[0][0][1] = MulHigh(f0u, f0v);
705 f[1][1][0] = MulHigh(f1u, f1v);
706 f[0][1][0] = MulHigh(f0u, f1v);
707 f[1][0][0] = MulHigh(f1u, f0v);
708 f[0][0][0] = MulHigh(f0u, f0v);
709
710 f[1][1][1] = MulHigh(f[1][1][1], f1s);
711 f[0][1][1] = MulHigh(f[0][1][1], f1s);
712 f[1][0][1] = MulHigh(f[1][0][1], f1s);
713 f[0][0][1] = MulHigh(f[0][0][1], f1s);
714 f[1][1][0] = MulHigh(f[1][1][0], f0s);
715 f[0][1][0] = MulHigh(f[0][1][0], f0s);
716 f[1][0][0] = MulHigh(f[1][0][0], f0s);
717 f[0][0][0] = MulHigh(f[0][0][0], f0s);
718
719 // Signed fractions
720 if(!hasUnsignedTextureComponent(0) || !hasUnsignedTextureComponent(1) || !hasUnsignedTextureComponent(2) || !hasUnsignedTextureComponent(3))
721 {
722 fs[0][0][0] = f[0][0][0] >> 1;
723 fs[0][0][1] = f[0][0][1] >> 1;
724 fs[0][1][0] = f[0][1][0] >> 1;
725 fs[0][1][1] = f[0][1][1] >> 1;
726 fs[1][0][0] = f[1][0][0] >> 1;
727 fs[1][0][1] = f[1][0][1] >> 1;
728 fs[1][1][0] = f[1][1][0] >> 1;
729 fs[1][1][1] = f[1][1][1] >> 1;
730 }
731
732 for(int i = 0; i < 2; i++)
733 {
734 for(int j = 0; j < 2; j++)
735 {
736 for(int k = 0; k < 2; k++)
737 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500738 c[i][j][k] = sampleTexel(u[i][j][k], v[i][j][k], s[i][j][k], 0, offset, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500739
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000740 if(componentCount >= 1)
741 {
742 if(hasUnsignedTextureComponent(0))
743 c[i][j][k].x = MulHigh(As<UShort4>(c[i][j][k].x), f[1 - i][1 - j][1 - k]);
744 else
745 c[i][j][k].x = MulHigh(c[i][j][k].x, fs[1 - i][1 - j][1 - k]);
746 }
747 if(componentCount >= 2)
748 {
749 if(hasUnsignedTextureComponent(1))
750 c[i][j][k].y = MulHigh(As<UShort4>(c[i][j][k].y), f[1 - i][1 - j][1 - k]);
751 else
752 c[i][j][k].y = MulHigh(c[i][j][k].y, fs[1 - i][1 - j][1 - k]);
753 }
754 if(componentCount >= 3)
755 {
756 if(hasUnsignedTextureComponent(2))
757 c[i][j][k].z = MulHigh(As<UShort4>(c[i][j][k].z), f[1 - i][1 - j][1 - k]);
758 else
759 c[i][j][k].z = MulHigh(c[i][j][k].z, fs[1 - i][1 - j][1 - k]);
760 }
761 if(componentCount >= 4)
762 {
763 if(hasUnsignedTextureComponent(3))
764 c[i][j][k].w = MulHigh(As<UShort4>(c[i][j][k].w), f[1 - i][1 - j][1 - k]);
765 else
766 c[i][j][k].w = MulHigh(c[i][j][k].w, fs[1 - i][1 - j][1 - k]);
767 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500768
769 if(i != 0 || j != 0 || k != 0)
770 {
771 if(componentCount >= 1) c[0][0][0].x += c[i][j][k].x;
772 if(componentCount >= 2) c[0][0][0].y += c[i][j][k].y;
773 if(componentCount >= 3) c[0][0][0].z += c[i][j][k].z;
774 if(componentCount >= 4) c[0][0][0].w += c[i][j][k].w;
775 }
776 }
777 }
778 }
779
780 if(componentCount >= 1) c_.x = c[0][0][0].x;
781 if(componentCount >= 2) c_.y = c[0][0][0].y;
782 if(componentCount >= 3) c_.z = c[0][0][0].z;
783 if(componentCount >= 4) c_.w = c[0][0][0].w;
784
785 // Correct for signed fractions
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000786 if(componentCount >= 1)
787 if(!hasUnsignedTextureComponent(0)) c_.x = AddSat(c_.x, c_.x);
788 if(componentCount >= 2)
789 if(!hasUnsignedTextureComponent(1)) c_.y = AddSat(c_.y, c_.y);
790 if(componentCount >= 3)
791 if(!hasUnsignedTextureComponent(2)) c_.z = AddSat(c_.z, c_.z);
792 if(componentCount >= 4)
793 if(!hasUnsignedTextureComponent(3)) c_.w = AddSat(c_.w, c_.w);
Nicolas Capens157ba262019-12-10 17:49:14 -0500794 }
795
796 return c_;
797}
798
Nicolas Capens9c273852021-12-15 13:44:47 -0500799Vector4f SamplerCore::sampleFloatFilter(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta)
Nicolas Capens157ba262019-12-10 17:49:14 -0500800{
Nicolas Capens9c273852021-12-15 13:44:47 -0500801 Vector4f c = sampleFloatAniso(texture, u, v, w, a, dRef, offset, sample, lod, anisotropy, uDelta, vDelta, false);
Nicolas Capens157ba262019-12-10 17:49:14 -0500802
803 if(function == Fetch)
804 {
Nicolas Capens68a82382018-10-02 13:16:55 -0400805 return c;
806 }
807
Nicolas Capens157ba262019-12-10 17:49:14 -0500808 if(state.mipmapFilter == MIPMAP_LINEAR)
Nicolas Capens68a82382018-10-02 13:16:55 -0400809 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500810 Vector4f cc = sampleFloatAniso(texture, u, v, w, a, dRef, offset, sample, lod, anisotropy, uDelta, vDelta, true);
Nicolas Capens68a82382018-10-02 13:16:55 -0400811
Nicolas Capens157ba262019-12-10 17:49:14 -0500812 Float4 lod4 = Float4(Frac(lod));
Nicolas Capens68a82382018-10-02 13:16:55 -0400813
Nicolas Capens157ba262019-12-10 17:49:14 -0500814 c.x = (cc.x - c.x) * lod4 + c.x;
815 c.y = (cc.y - c.y) * lod4 + c.y;
816 c.z = (cc.z - c.z) * lod4 + c.z;
817 c.w = (cc.w - c.w) * lod4 + c.w;
818 }
819
820 return c;
821}
822
Nicolas Capens9c273852021-12-15 13:44:47 -0500823Vector4f SamplerCore::sampleFloatAniso(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500824{
825 Vector4f c;
826
Nicolas Capens73c24e42020-08-05 09:50:20 -0400827 if(state.textureFilter != FILTER_ANISOTROPIC)
Nicolas Capens157ba262019-12-10 17:49:14 -0500828 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500829 c = sampleFloat(texture, u, v, w, a, dRef, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500830 }
831 else
832 {
Nicolas Capens30873702020-08-01 09:17:23 -0400833 Int N = RoundInt(anisotropy);
Nicolas Capens157ba262019-12-10 17:49:14 -0500834
835 Vector4f cSum;
836
837 cSum.x = Float4(0.0f);
838 cSum.y = Float4(0.0f);
839 cSum.z = Float4(0.0f);
840 cSum.w = Float4(0.0f);
841
Nicolas Capens30873702020-08-01 09:17:23 -0400842 Float4 A = *Pointer<Float4>(constants + OFFSET(Constants, uvWeight) + 16 * N);
843 Float4 B = *Pointer<Float4>(constants + OFFSET(Constants, uvStart) + 16 * N);
Nicolas Capens157ba262019-12-10 17:49:14 -0500844
845 Float4 du = uDelta;
846 Float4 dv = vDelta;
847
848 Float4 u0 = u + B * du;
849 Float4 v0 = v + B * dv;
850
851 du *= A;
852 dv *= A;
853
854 Int i = 0;
855
856 Do
Nicolas Capens68a82382018-10-02 13:16:55 -0400857 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500858 c = sampleFloat(texture, u0, v0, w, a, dRef, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500859
860 u0 += du;
861 v0 += dv;
862
863 cSum.x += c.x * A;
864 cSum.y += c.y * A;
865 cSum.z += c.z * A;
866 cSum.w += c.w * A;
867
868 i++;
869 }
Nicolas Capens30873702020-08-01 09:17:23 -0400870 Until(i >= N);
Nicolas Capens157ba262019-12-10 17:49:14 -0500871
872 c.x = cSum.x;
873 c.y = cSum.y;
874 c.z = cSum.z;
875 c.w = cSum.w;
876 }
877
878 return c;
879}
880
Nicolas Capens9c273852021-12-15 13:44:47 -0500881Vector4f SamplerCore::sampleFloat(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500882{
883 if(state.textureType != VK_IMAGE_VIEW_TYPE_3D)
884 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500885 return sampleFloat2D(texture, u, v, w, a, dRef, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500886 }
887 else
888 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500889 return sampleFloat3D(texture, u, v, w, dRef, offset, sample, lod, secondLOD);
Nicolas Capens157ba262019-12-10 17:49:14 -0500890 }
891}
892
Nicolas Capens9c273852021-12-15 13:44:47 -0500893Vector4f SamplerCore::sampleFloat2D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, const Float4 &a, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500894{
895 Vector4f c;
896
897 int componentCount = textureComponentCount();
898 bool gather = (state.textureFilter == FILTER_GATHER);
899
Nicolas Capens83373b92021-12-15 06:43:29 -0500900 Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
901 Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
Nicolas Capens157ba262019-12-10 17:49:14 -0500902
Nicolas Capens30873702020-08-01 09:17:23 -0400903 Int4 x0, x1, y0, y1;
904 Float4 fu, fv;
Nicolas Capens157ba262019-12-10 17:49:14 -0500905 Int4 filter = computeFilterOffset(lod);
Nicolas Capens9c273852021-12-15 13:44:47 -0500906 address(u, x0, x1, fu, mipmap, offset.x, filter, OFFSET(Mipmap, width), state.addressingModeU);
907 address(v, y0, y1, fv, mipmap, offset.y, filter, OFFSET(Mipmap, height), state.addressingModeV);
Nicolas Capens157ba262019-12-10 17:49:14 -0500908
Nicolas Capens71196862022-01-06 21:31:57 -0500909 Int4 pitchP = As<Int4>(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, pitchP), 16));
Nicolas Capens157ba262019-12-10 17:49:14 -0500910 y0 *= pitchP;
Nicolas Capens30873702020-08-01 09:17:23 -0400911
912 Int4 z;
Nicolas Capensa202c202020-08-05 14:42:22 -0400913 if(state.isCube() || state.isArrayed())
Nicolas Capens157ba262019-12-10 17:49:14 -0500914 {
Nicolas Capens30873702020-08-01 09:17:23 -0400915 Int4 face = As<Int4>(w);
Nicolas Capens9c273852021-12-15 13:44:47 -0500916 Int4 layerIndex = computeLayerIndex(a, mipmap);
Nicolas Capens30873702020-08-01 09:17:23 -0400917
918 // For cube maps, the layer argument is per cube, each of which has 6 layers
919 if(state.textureType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
920 {
921 layerIndex *= Int4(6);
922 }
923
Nicolas Capensa202c202020-08-05 14:42:22 -0400924 z = state.isCube() ? face : layerIndex;
Nicolas Capens30873702020-08-01 09:17:23 -0400925
926 if(state.textureType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
927 {
928 z += layerIndex;
929 }
930
931 z *= *Pointer<Int4>(mipmap + OFFSET(Mipmap, sliceP), 16);
Nicolas Capens157ba262019-12-10 17:49:14 -0500932 }
933
934 if(state.textureFilter == FILTER_POINT || (function == Fetch))
935 {
Nicolas Capens9c273852021-12-15 13:44:47 -0500936 c = sampleTexel(x0, y0, z, dRef, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500937 }
938 else
939 {
940 y1 *= pitchP;
941
Nicolas Capens9c273852021-12-15 13:44:47 -0500942 Vector4f c00 = sampleTexel(x0, y0, z, dRef, sample, mipmap, buffer);
943 Vector4f c10 = sampleTexel(x1, y0, z, dRef, sample, mipmap, buffer);
944 Vector4f c01 = sampleTexel(x0, y1, z, dRef, sample, mipmap, buffer);
945 Vector4f c11 = sampleTexel(x1, y1, z, dRef, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -0500946
Ben Claytonbc1c067be2019-12-17 20:37:37 +0000947 if(!gather) // Blend
Nicolas Capens157ba262019-12-10 17:49:14 -0500948 {
949 if(componentCount >= 1) c00.x = c00.x + fu * (c10.x - c00.x);
950 if(componentCount >= 2) c00.y = c00.y + fu * (c10.y - c00.y);
951 if(componentCount >= 3) c00.z = c00.z + fu * (c10.z - c00.z);
952 if(componentCount >= 4) c00.w = c00.w + fu * (c10.w - c00.w);
953
954 if(componentCount >= 1) c01.x = c01.x + fu * (c11.x - c01.x);
955 if(componentCount >= 2) c01.y = c01.y + fu * (c11.y - c01.y);
956 if(componentCount >= 3) c01.z = c01.z + fu * (c11.z - c01.z);
957 if(componentCount >= 4) c01.w = c01.w + fu * (c11.w - c01.w);
958
959 if(componentCount >= 1) c.x = c00.x + fv * (c01.x - c00.x);
960 if(componentCount >= 2) c.y = c00.y + fv * (c01.y - c00.y);
961 if(componentCount >= 3) c.z = c00.z + fv * (c01.z - c00.z);
962 if(componentCount >= 4) c.w = c00.w + fv * (c01.w - c00.w);
963 }
964 else // Gather
965 {
966 VkComponentSwizzle swizzle = gatherSwizzle();
967 switch(swizzle)
Nicolas Capens68a82382018-10-02 13:16:55 -0400968 {
Nicolas Capens112faf42019-12-13 17:32:26 -0500969 case VK_COMPONENT_SWIZZLE_ZERO:
970 case VK_COMPONENT_SWIZZLE_ONE:
971 // Handled at the final component swizzle.
972 break;
973 default:
974 c.x = c01[swizzle - VK_COMPONENT_SWIZZLE_R];
975 c.y = c11[swizzle - VK_COMPONENT_SWIZZLE_R];
976 c.z = c10[swizzle - VK_COMPONENT_SWIZZLE_R];
977 c.w = c00[swizzle - VK_COMPONENT_SWIZZLE_R];
978 break;
Nicolas Capens68a82382018-10-02 13:16:55 -0400979 }
980 }
Nicolas Capens157ba262019-12-10 17:49:14 -0500981 }
982
983 return c;
984}
985
Nicolas Capens9c273852021-12-15 13:44:47 -0500986Vector4f SamplerCore::sampleFloat3D(Pointer<Byte> &texture, Float4 &u, Float4 &v, Float4 &w, Float4 &dRef, Vector4i &offset, const Int4 &sample, Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -0500987{
988 Vector4f c;
989
990 int componentCount = textureComponentCount();
991
Nicolas Capens83373b92021-12-15 06:43:29 -0500992 Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
993 Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
Nicolas Capens157ba262019-12-10 17:49:14 -0500994
995 Int4 x0, x1, y0, y1, z0, z1;
996 Float4 fu, fv, fw;
997 Int4 filter = computeFilterOffset(lod);
Nicolas Capens9c273852021-12-15 13:44:47 -0500998 address(u, x0, x1, fu, mipmap, offset.x, filter, OFFSET(Mipmap, width), state.addressingModeU);
999 address(v, y0, y1, fv, mipmap, offset.y, filter, OFFSET(Mipmap, height), state.addressingModeV);
1000 address(w, z0, z1, fw, mipmap, offset.z, filter, OFFSET(Mipmap, depth), state.addressingModeW);
Nicolas Capens157ba262019-12-10 17:49:14 -05001001
Nicolas Capens71196862022-01-06 21:31:57 -05001002 Int4 pitchP = As<Int4>(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, pitchP), 16));
1003 Int4 sliceP = As<Int4>(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, sliceP), 16));
Nicolas Capens157ba262019-12-10 17:49:14 -05001004 y0 *= pitchP;
1005 z0 *= sliceP;
1006
1007 if(state.textureFilter == FILTER_POINT || (function == Fetch))
1008 {
Nicolas Capens9c273852021-12-15 13:44:47 -05001009 c = sampleTexel(x0, y0, z0, dRef, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -05001010 }
1011 else
1012 {
1013 y1 *= pitchP;
1014 z1 *= sliceP;
1015
Nicolas Capens9c273852021-12-15 13:44:47 -05001016 Vector4f c000 = sampleTexel(x0, y0, z0, dRef, sample, mipmap, buffer);
1017 Vector4f c100 = sampleTexel(x1, y0, z0, dRef, sample, mipmap, buffer);
1018 Vector4f c010 = sampleTexel(x0, y1, z0, dRef, sample, mipmap, buffer);
1019 Vector4f c110 = sampleTexel(x1, y1, z0, dRef, sample, mipmap, buffer);
1020 Vector4f c001 = sampleTexel(x0, y0, z1, dRef, sample, mipmap, buffer);
1021 Vector4f c101 = sampleTexel(x1, y0, z1, dRef, sample, mipmap, buffer);
1022 Vector4f c011 = sampleTexel(x0, y1, z1, dRef, sample, mipmap, buffer);
1023 Vector4f c111 = sampleTexel(x1, y1, z1, dRef, sample, mipmap, buffer);
Nicolas Capens157ba262019-12-10 17:49:14 -05001024
1025 // Blend first slice
1026 if(componentCount >= 1) c000.x = c000.x + fu * (c100.x - c000.x);
1027 if(componentCount >= 2) c000.y = c000.y + fu * (c100.y - c000.y);
1028 if(componentCount >= 3) c000.z = c000.z + fu * (c100.z - c000.z);
1029 if(componentCount >= 4) c000.w = c000.w + fu * (c100.w - c000.w);
1030
1031 if(componentCount >= 1) c010.x = c010.x + fu * (c110.x - c010.x);
1032 if(componentCount >= 2) c010.y = c010.y + fu * (c110.y - c010.y);
1033 if(componentCount >= 3) c010.z = c010.z + fu * (c110.z - c010.z);
1034 if(componentCount >= 4) c010.w = c010.w + fu * (c110.w - c010.w);
1035
1036 if(componentCount >= 1) c000.x = c000.x + fv * (c010.x - c000.x);
1037 if(componentCount >= 2) c000.y = c000.y + fv * (c010.y - c000.y);
1038 if(componentCount >= 3) c000.z = c000.z + fv * (c010.z - c000.z);
1039 if(componentCount >= 4) c000.w = c000.w + fv * (c010.w - c000.w);
1040
1041 // Blend second slice
1042 if(componentCount >= 1) c001.x = c001.x + fu * (c101.x - c001.x);
1043 if(componentCount >= 2) c001.y = c001.y + fu * (c101.y - c001.y);
1044 if(componentCount >= 3) c001.z = c001.z + fu * (c101.z - c001.z);
1045 if(componentCount >= 4) c001.w = c001.w + fu * (c101.w - c001.w);
1046
1047 if(componentCount >= 1) c011.x = c011.x + fu * (c111.x - c011.x);
1048 if(componentCount >= 2) c011.y = c011.y + fu * (c111.y - c011.y);
1049 if(componentCount >= 3) c011.z = c011.z + fu * (c111.z - c011.z);
1050 if(componentCount >= 4) c011.w = c011.w + fu * (c111.w - c011.w);
1051
1052 if(componentCount >= 1) c001.x = c001.x + fv * (c011.x - c001.x);
1053 if(componentCount >= 2) c001.y = c001.y + fv * (c011.y - c001.y);
1054 if(componentCount >= 3) c001.z = c001.z + fv * (c011.z - c001.z);
1055 if(componentCount >= 4) c001.w = c001.w + fv * (c011.w - c001.w);
1056
1057 // Blend slices
1058 if(componentCount >= 1) c.x = c000.x + fw * (c001.x - c000.x);
1059 if(componentCount >= 2) c.y = c000.y + fw * (c001.y - c000.y);
1060 if(componentCount >= 3) c.z = c000.z + fw * (c001.z - c000.z);
1061 if(componentCount >= 4) c.w = c000.w + fw * (c001.w - c000.w);
1062 }
1063
1064 return c;
1065}
1066
Nicolas Capens73c24e42020-08-05 09:50:20 -04001067static Float log2sqrt(Float lod)
Nicolas Capens157ba262019-12-10 17:49:14 -05001068{
Nicolas Capens73c24e42020-08-05 09:50:20 -04001069 // log2(sqrt(lod)) // Equals 0.25 * log2(lod^2).
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001070 lod *= lod; // Squaring doubles the exponent and produces an extra bit of precision.
1071 lod = Float(As<Int>(lod)) - Float(0x3F800000); // Interpret as integer and subtract the exponent bias.
1072 lod *= As<Float>(Int(0x33000000)); // Scale by 0.25 * 2^-23 (mantissa length).
Nicolas Capens157ba262019-12-10 17:49:14 -05001073
1074 return lod;
1075}
1076
Nicolas Capens73c24e42020-08-05 09:50:20 -04001077static Float log2(Float lod)
Nicolas Capens157ba262019-12-10 17:49:14 -05001078{
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001079 lod *= lod; // Squaring doubles the exponent and produces an extra bit of precision.
1080 lod = Float(As<Int>(lod)) - Float(0x3F800000); // Interpret as integer and subtract the exponent bias.
1081 lod *= As<Float>(Int(0x33800000)); // Scale by 0.5 * 2^-23 (mantissa length).
Nicolas Capens157ba262019-12-10 17:49:14 -05001082
1083 return lod;
1084}
1085
Nicolas Capens9c273852021-12-15 13:44:47 -05001086void SamplerCore::computeLod1D(Pointer<Byte> &texture, Float &lod, Float4 &uuuu, Float4 &dsx, Float4 &dsy)
Nicolas Capens73c24e42020-08-05 09:50:20 -04001087{
1088 Float4 dudxy;
1089
1090 if(function != Grad) // Implicit
1091 {
1092 dudxy = uuuu.yz - uuuu.xx;
1093 }
1094 else
1095 {
1096 dudxy = UnpackLow(dsx, dsy);
1097 }
1098
1099 // Scale by texture dimensions.
1100 Float4 dUdxy = dudxy * *Pointer<Float4>(texture + OFFSET(Texture, widthWidthHeightHeight));
1101
1102 // Note we could take the absolute value here and omit the square root below,
1103 // but this is more consistent with the 2D calculation and still cheap.
1104 Float4 dU2dxy = dUdxy * dUdxy;
1105
1106 lod = Max(Float(dU2dxy.x), Float(dU2dxy.y));
1107 lod = log2sqrt(lod);
1108}
1109
Nicolas Capens9c273852021-12-15 13:44:47 -05001110void SamplerCore::computeLod2D(Pointer<Byte> &texture, Float &lod, Float &anisotropy, Float4 &uDelta, Float4 &vDelta, Float4 &uuuu, Float4 &vvvv, Float4 &dsx, Float4 &dsy)
Nicolas Capens157ba262019-12-10 17:49:14 -05001111{
1112 Float4 duvdxy;
1113
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001114 if(function != Grad) // Implicit
Nicolas Capens157ba262019-12-10 17:49:14 -05001115 {
1116 duvdxy = Float4(uuuu.yz, vvvv.yz) - Float4(uuuu.xx, vvvv.xx);
1117 }
1118 else
1119 {
1120 Float4 dudxy = Float4(dsx.xx, dsy.xx);
1121 Float4 dvdxy = Float4(dsx.yy, dsy.yy);
1122
1123 duvdxy = Float4(dudxy.xz, dvdxy.xz);
1124 }
1125
1126 // Scale by texture dimensions.
1127 Float4 dUVdxy = duvdxy * *Pointer<Float4>(texture + OFFSET(Texture, widthWidthHeightHeight));
1128
1129 Float4 dUV2dxy = dUVdxy * dUVdxy;
1130 Float4 dUV2 = dUV2dxy.xy + dUV2dxy.zw;
1131
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001132 lod = Max(Float(dUV2.x), Float(dUV2.y)); // Square length of major axis
Nicolas Capens157ba262019-12-10 17:49:14 -05001133
1134 if(state.textureFilter == FILTER_ANISOTROPIC)
1135 {
1136 Float det = Abs(Float(dUVdxy.x) * Float(dUVdxy.w) - Float(dUVdxy.y) * Float(dUVdxy.z));
1137
1138 Float4 dudx = duvdxy.xxxx;
1139 Float4 dudy = duvdxy.yyyy;
1140 Float4 dvdx = duvdxy.zzzz;
1141 Float4 dvdy = duvdxy.wwww;
1142
1143 Int4 mask = As<Int4>(CmpNLT(dUV2.x, dUV2.y));
1144 uDelta = As<Float4>((As<Int4>(dudx) & mask) | ((As<Int4>(dudy) & ~mask)));
1145 vDelta = As<Float4>((As<Int4>(dvdx) & mask) | ((As<Int4>(dvdy) & ~mask)));
1146
Nicolas Capensd04f3f52022-02-05 01:19:14 -05001147 anisotropy = lod * Rcp(det, true /* relaxedPrecision */);
Nicolas Capens7f469172020-03-17 17:29:11 -04001148 anisotropy = Min(anisotropy, state.maxAnisotropy);
Nicolas Capens157ba262019-12-10 17:49:14 -05001149
Nicolas Capensd4483092021-08-03 11:11:10 -04001150 // TODO(b/151263485): While we always need `lod` above, when there's only
1151 // a single mipmap level the following calculations could be skipped.
Nicolas Capensd04f3f52022-02-05 01:19:14 -05001152 lod *= Rcp(anisotropy * anisotropy, true /* relaxedPrecision */);
Nicolas Capens157ba262019-12-10 17:49:14 -05001153 }
1154
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001155 lod = log2sqrt(lod); // log2(sqrt(lod))
Nicolas Capens157ba262019-12-10 17:49:14 -05001156}
1157
Nicolas Capens9c273852021-12-15 13:44:47 -05001158void SamplerCore::computeLodCube(Pointer<Byte> &texture, Float &lod, Float4 &u, Float4 &v, Float4 &w, Float4 &dsx, Float4 &dsy, Float4 &M)
Nicolas Capens157ba262019-12-10 17:49:14 -05001159{
1160 Float4 dudxy, dvdxy, dsdxy;
1161
1162 if(function != Grad) // Implicit
1163 {
1164 Float4 U = u * M;
1165 Float4 V = v * M;
1166 Float4 W = w * M;
1167
1168 dudxy = Abs(U - U.xxxx);
1169 dvdxy = Abs(V - V.xxxx);
1170 dsdxy = Abs(W - W.xxxx);
1171 }
1172 else
1173 {
1174 dudxy = Float4(dsx.xx, dsy.xx);
1175 dvdxy = Float4(dsx.yy, dsy.yy);
1176 dsdxy = Float4(dsx.zz, dsy.zz);
1177
1178 dudxy = Abs(dudxy * Float4(M.x));
1179 dvdxy = Abs(dvdxy * Float4(M.x));
1180 dsdxy = Abs(dsdxy * Float4(M.x));
1181 }
1182
1183 // Compute the largest Manhattan distance in two dimensions.
1184 // This takes the footprint across adjacent faces into account.
1185 Float4 duvdxy = dudxy + dvdxy;
1186 Float4 dusdxy = dudxy + dsdxy;
1187 Float4 dvsdxy = dvdxy + dsdxy;
1188
1189 dudxy = Max(Max(duvdxy, dusdxy), dvsdxy);
1190
Nicolas Capens9c273852021-12-15 13:44:47 -05001191 lod = Max(Float(dudxy.y), Float(dudxy.z)); // TODO: Max(dudxy.y, dudxy.z);
Nicolas Capens157ba262019-12-10 17:49:14 -05001192
1193 // Scale by texture dimension.
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001194 lod *= *Pointer<Float>(texture + OFFSET(Texture, width));
Nicolas Capens157ba262019-12-10 17:49:14 -05001195
1196 lod = log2(lod);
1197}
1198
Nicolas Capens9c273852021-12-15 13:44:47 -05001199void SamplerCore::computeLod3D(Pointer<Byte> &texture, Float &lod, Float4 &uuuu, Float4 &vvvv, Float4 &wwww, Float4 &dsx, Float4 &dsy)
Nicolas Capens157ba262019-12-10 17:49:14 -05001200{
1201 Float4 dudxy, dvdxy, dsdxy;
1202
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001203 if(function != Grad) // Implicit
Nicolas Capens157ba262019-12-10 17:49:14 -05001204 {
1205 dudxy = uuuu - uuuu.xxxx;
1206 dvdxy = vvvv - vvvv.xxxx;
1207 dsdxy = wwww - wwww.xxxx;
1208 }
1209 else
1210 {
1211 dudxy = Float4(dsx.xx, dsy.xx);
1212 dvdxy = Float4(dsx.yy, dsy.yy);
1213 dsdxy = Float4(dsx.zz, dsy.zz);
1214 }
1215
1216 // Scale by texture dimensions.
1217 dudxy *= *Pointer<Float4>(texture + OFFSET(Texture, width));
1218 dvdxy *= *Pointer<Float4>(texture + OFFSET(Texture, height));
1219 dsdxy *= *Pointer<Float4>(texture + OFFSET(Texture, depth));
1220
1221 dudxy *= dudxy;
1222 dvdxy *= dvdxy;
1223 dsdxy *= dsdxy;
1224
1225 dudxy += dvdxy;
1226 dudxy += dsdxy;
1227
Nicolas Capens9c273852021-12-15 13:44:47 -05001228 lod = Max(Float(dudxy.y), Float(dudxy.z)); // TODO: Max(dudxy.y, dudxy.z);
Nicolas Capens157ba262019-12-10 17:49:14 -05001229
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001230 lod = log2sqrt(lod); // log2(sqrt(lod))
Nicolas Capens157ba262019-12-10 17:49:14 -05001231}
1232
1233Int4 SamplerCore::cubeFace(Float4 &U, Float4 &V, Float4 &x, Float4 &y, Float4 &z, Float4 &M)
1234{
1235 // TODO: Comply with Vulkan recommendation:
1236 // Vulkan 1.1: "The rules should have as the first rule that rz wins over ry and rx, and the second rule that ry wins over rx."
1237
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001238 Int4 xn = CmpLT(x, Float4(0.0f)); // x < 0
1239 Int4 yn = CmpLT(y, Float4(0.0f)); // y < 0
1240 Int4 zn = CmpLT(z, Float4(0.0f)); // z < 0
Nicolas Capens157ba262019-12-10 17:49:14 -05001241
1242 Float4 absX = Abs(x);
1243 Float4 absY = Abs(y);
1244 Float4 absZ = Abs(z);
1245
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001246 Int4 xy = CmpNLE(absX, absY); // abs(x) > abs(y)
1247 Int4 yz = CmpNLE(absY, absZ); // abs(y) > abs(z)
1248 Int4 zx = CmpNLE(absZ, absX); // abs(z) > abs(x)
1249 Int4 xMajor = xy & ~zx; // abs(x) > abs(y) && abs(x) > abs(z)
1250 Int4 yMajor = yz & ~xy; // abs(y) > abs(z) && abs(y) > abs(x)
1251 Int4 zMajor = zx & ~yz; // abs(z) > abs(x) && abs(z) > abs(y)
Nicolas Capens157ba262019-12-10 17:49:14 -05001252
1253 // FACE_POSITIVE_X = 000b
1254 // FACE_NEGATIVE_X = 001b
1255 // FACE_POSITIVE_Y = 010b
1256 // FACE_NEGATIVE_Y = 011b
1257 // FACE_POSITIVE_Z = 100b
1258 // FACE_NEGATIVE_Z = 101b
1259
1260 Int yAxis = SignMask(yMajor);
1261 Int zAxis = SignMask(zMajor);
1262
1263 Int4 n = ((xn & xMajor) | (yn & yMajor) | (zn & zMajor)) & Int4(0x80000000);
1264 Int negative = SignMask(n);
1265
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001266 Int faces = *Pointer<Int>(constants + OFFSET(Constants, transposeBit0) + negative * 4);
1267 faces |= *Pointer<Int>(constants + OFFSET(Constants, transposeBit1) + yAxis * 4);
1268 faces |= *Pointer<Int>(constants + OFFSET(Constants, transposeBit2) + zAxis * 4);
Nicolas Capens157ba262019-12-10 17:49:14 -05001269
1270 Int4 face;
1271 face.x = faces & 0x7;
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001272 face.y = (faces >> 4) & 0x7;
1273 face.z = (faces >> 8) & 0x7;
Nicolas Capens157ba262019-12-10 17:49:14 -05001274 face.w = (faces >> 12) & 0x7;
1275
Nicolas Capens5f4e70b2020-09-30 23:53:28 -04001276 M = Max(Max(absX, absY), absZ);
Nicolas Capens157ba262019-12-10 17:49:14 -05001277
1278 // U = xMajor ? (neg ^ -z) : ((zMajor & neg) ^ x)
1279 U = As<Float4>((xMajor & (n ^ As<Int4>(-z))) | (~xMajor & ((zMajor & n) ^ As<Int4>(x))));
1280
1281 // V = !yMajor ? -y : (n ^ z)
1282 V = As<Float4>((~yMajor & As<Int4>(-y)) | (yMajor & (n ^ As<Int4>(z))));
1283
1284 M = reciprocal(M) * Float4(0.5f);
1285 U = U * M + Float4(0.5f);
1286 V = V * M + Float4(0.5f);
1287
1288 return face;
1289}
1290
Nicolas Capens2e40a572020-07-29 14:18:55 -04001291Short4 SamplerCore::applyOffset(Short4 &uvw, Int4 &offset, const Int4 &whd, AddressingMode mode)
Nicolas Capens157ba262019-12-10 17:49:14 -05001292{
1293 Int4 tmp = Int4(As<UShort4>(uvw));
Nicolas Capens2e40a572020-07-29 14:18:55 -04001294 tmp = tmp + offset;
Nicolas Capens157ba262019-12-10 17:49:14 -05001295
1296 switch(mode)
1297 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001298 case AddressingMode::ADDRESSING_WRAP:
1299 tmp = (tmp + whd * Int4(-MIN_TEXEL_OFFSET)) % whd;
1300 break;
1301 case AddressingMode::ADDRESSING_CLAMP:
1302 case AddressingMode::ADDRESSING_MIRROR:
1303 case AddressingMode::ADDRESSING_MIRRORONCE:
Nicolas Capens9c273852021-12-15 13:44:47 -05001304 case AddressingMode::ADDRESSING_BORDER: // TODO(b/29069044): Implement and test ADDRESSING_MIRROR, ADDRESSING_MIRRORONCE, ADDRESSING_BORDER
Nicolas Capens112faf42019-12-13 17:32:26 -05001305 tmp = Min(Max(tmp, Int4(0)), whd - Int4(1));
1306 break;
1307 case AddressingMode::ADDRESSING_SEAMLESS:
1308 ASSERT(false); // Cube sampling doesn't support offset.
1309 default:
1310 ASSERT(false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001311 }
1312
1313 return As<Short4>(UShort4(tmp));
1314}
1315
Nicolas Capens9c273852021-12-15 13:44:47 -05001316void SamplerCore::computeIndices(UInt index[4], Short4 uuuu, Short4 vvvv, Short4 wwww, const Short4 &layerIndex, Vector4i &offset, const Int4 &sample, const Pointer<Byte> &mipmap)
Nicolas Capens157ba262019-12-10 17:49:14 -05001317{
Nicolas Capens71196862022-01-06 21:31:57 -05001318 uuuu = MulHigh(As<UShort4>(uuuu), UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, width))));
Nicolas Capens157ba262019-12-10 17:49:14 -05001319
Nicolas Capens2e40a572020-07-29 14:18:55 -04001320 if(function.offset)
Nicolas Capens157ba262019-12-10 17:49:14 -05001321 {
Nicolas Capens71196862022-01-06 21:31:57 -05001322 uuuu = applyOffset(uuuu, offset.x, *Pointer<UInt4>(mipmap + OFFSET(Mipmap, width)), state.addressingModeU);
Nicolas Capens157ba262019-12-10 17:49:14 -05001323 }
1324
Nicolas Capensa00fa432020-08-03 16:01:16 -04001325 UInt4 indices = Int4(uuuu);
Nicolas Capens157ba262019-12-10 17:49:14 -05001326
Nicolas Capensa202c202020-08-05 14:42:22 -04001327 if(state.is2D() || state.is3D() || state.isCube())
Nicolas Capensa00fa432020-08-03 16:01:16 -04001328 {
Nicolas Capens71196862022-01-06 21:31:57 -05001329 vvvv = MulHigh(As<UShort4>(vvvv), UShort4(*Pointer<UInt4>(mipmap + OFFSET(Mipmap, height))));
Nicolas Capensa00fa432020-08-03 16:01:16 -04001330
1331 if(function.offset)
1332 {
Nicolas Capens71196862022-01-06 21:31:57 -05001333 vvvv = applyOffset(vvvv, offset.y, *Pointer<UInt4>(mipmap + OFFSET(Mipmap, height)), state.addressingModeV);
Nicolas Capensa00fa432020-08-03 16:01:16 -04001334 }
1335
1336 Short4 uv0uv1 = As<Short4>(UnpackLow(uuuu, vvvv));
1337 Short4 uv2uv3 = As<Short4>(UnpackHigh(uuuu, vvvv));
1338 Int2 i01 = MulAdd(uv0uv1, *Pointer<Short4>(mipmap + OFFSET(Mipmap, onePitchP)));
1339 Int2 i23 = MulAdd(uv2uv3, *Pointer<Short4>(mipmap + OFFSET(Mipmap, onePitchP)));
1340
1341 indices = UInt4(As<UInt2>(i01), As<UInt2>(i23));
1342 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001343
Nicolas Capensa202c202020-08-05 14:42:22 -04001344 if(state.is3D())
Nicolas Capens30873702020-08-01 09:17:23 -04001345 {
1346 wwww = MulHigh(As<UShort4>(wwww), UShort4(*Pointer<Int4>(mipmap + OFFSET(Mipmap, depth))));
1347
1348 if(function.offset)
1349 {
1350 wwww = applyOffset(wwww, offset.z, *Pointer<Int4>(mipmap + OFFSET(Mipmap, depth)), state.addressingModeW);
Nicolas Capens68a82382018-10-02 13:16:55 -04001351 }
1352
Nicolas Capensa00fa432020-08-03 16:01:16 -04001353 indices += As<UInt4>(Int4(As<UShort4>(wwww))) * *Pointer<UInt4>(mipmap + OFFSET(Mipmap, sliceP));
Nicolas Capens157ba262019-12-10 17:49:14 -05001354 }
Nicolas Capens30873702020-08-01 09:17:23 -04001355
1356 if(state.isArrayed())
Nicolas Capens157ba262019-12-10 17:49:14 -05001357 {
Nicolas Capens30873702020-08-01 09:17:23 -04001358 Int4 layer = Int4(As<UShort4>(layerIndex));
1359
1360 if(state.textureType == VK_IMAGE_VIEW_TYPE_CUBE_ARRAY)
1361 {
1362 layer *= Int4(6);
1363 }
1364
1365 UInt4 layerOffset = As<UInt4>(layer) * *Pointer<UInt4>(mipmap + OFFSET(Mipmap, sliceP));
1366
Nicolas Capensa00fa432020-08-03 16:01:16 -04001367 indices += layerOffset;
Nicolas Capens68a82382018-10-02 13:16:55 -04001368 }
1369
Nicolas Capens157ba262019-12-10 17:49:14 -05001370 if(function.sample)
Nicolas Capens68a82382018-10-02 13:16:55 -04001371 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04001372 UInt4 sampleOffset = Min(As<UInt4>(sample), *Pointer<UInt4>(mipmap + OFFSET(Mipmap, sampleMax), 16)) *
Nicolas Capens157ba262019-12-10 17:49:14 -05001373 *Pointer<UInt4>(mipmap + OFFSET(Mipmap, samplePitchP), 16);
Nicolas Capensa00fa432020-08-03 16:01:16 -04001374 indices += sampleOffset;
Nicolas Capens157ba262019-12-10 17:49:14 -05001375 }
1376
Nicolas Capensa00fa432020-08-03 16:01:16 -04001377 index[0] = Extract(indices, 0);
1378 index[1] = Extract(indices, 1);
1379 index[2] = Extract(indices, 2);
1380 index[3] = Extract(indices, 3);
Nicolas Capens157ba262019-12-10 17:49:14 -05001381}
1382
Nicolas Capens9c273852021-12-15 13:44:47 -05001383void SamplerCore::computeIndices(UInt index[4], Int4 uuuu, Int4 vvvv, Int4 wwww, const Int4 &sample, Int4 valid, const Pointer<Byte> &mipmap)
Nicolas Capens157ba262019-12-10 17:49:14 -05001384{
Nicolas Capensa00fa432020-08-03 16:01:16 -04001385 UInt4 indices = uuuu;
1386
Nicolas Capensa202c202020-08-05 14:42:22 -04001387 if(state.is2D() || state.is3D() || state.isCube())
Nicolas Capensa00fa432020-08-03 16:01:16 -04001388 {
1389 indices += As<UInt4>(vvvv);
1390 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001391
Nicolas Capensa202c202020-08-05 14:42:22 -04001392 if(state.is3D() || state.isCube() || state.isArrayed())
Nicolas Capens157ba262019-12-10 17:49:14 -05001393 {
1394 indices += As<UInt4>(wwww);
1395 }
1396
Nicolas Capens157ba262019-12-10 17:49:14 -05001397 if(function.sample)
1398 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04001399 indices += Min(As<UInt4>(sample), *Pointer<UInt4>(mipmap + OFFSET(Mipmap, sampleMax), 16)) *
Nicolas Capens157ba262019-12-10 17:49:14 -05001400 *Pointer<UInt4>(mipmap + OFFSET(Mipmap, samplePitchP), 16);
1401 }
1402
Nicolas Capensd0b7d1e2020-07-29 13:59:42 -04001403 if(borderModeActive())
1404 {
1405 // Texels out of range are still sampled before being replaced
1406 // with the border color, so sample them at linear index 0.
1407 indices &= As<UInt4>(valid);
1408 }
1409
Nicolas Capens157ba262019-12-10 17:49:14 -05001410 for(int i = 0; i < 4; i++)
1411 {
1412 index[i] = Extract(As<Int4>(indices), i);
1413 }
1414}
1415
1416Vector4s SamplerCore::sampleTexel(UInt index[4], Pointer<Byte> buffer)
1417{
1418 Vector4s c;
1419
Sean Risserc9625f12020-03-20 11:12:00 -04001420 if(has16bitPackedTextureFormat())
Nicolas Capens157ba262019-12-10 17:49:14 -05001421 {
1422 c.x = Insert(c.x, Pointer<Short>(buffer)[index[0]], 0);
1423 c.x = Insert(c.x, Pointer<Short>(buffer)[index[1]], 1);
1424 c.x = Insert(c.x, Pointer<Short>(buffer)[index[2]], 2);
1425 c.x = Insert(c.x, Pointer<Short>(buffer)[index[3]], 3);
1426
1427 switch(state.textureFormat)
1428 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001429 case VK_FORMAT_R5G6B5_UNORM_PACK16:
1430 c.z = (c.x & Short4(0x001Fu)) << 11;
1431 c.y = (c.x & Short4(0x07E0u)) << 5;
1432 c.x = (c.x & Short4(0xF800u));
1433 break;
Sean Risser584c2cf2021-09-14 04:07:02 -04001434 case VK_FORMAT_B5G6R5_UNORM_PACK16:
1435 c.z = (c.x & Short4(0xF800u));
1436 c.y = (c.x & Short4(0x07E0u)) << 5;
1437 c.x = (c.x & Short4(0x001Fu)) << 11;
1438 break;
1439 case VK_FORMAT_R4G4B4A4_UNORM_PACK16:
1440 c.w = (c.x << 12) & Short4(0xF000u);
1441 c.z = (c.x << 8) & Short4(0xF000u);
1442 c.y = (c.x << 4) & Short4(0xF000u);
1443 c.x = (c.x) & Short4(0xF000u);
1444 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05001445 case VK_FORMAT_B4G4R4A4_UNORM_PACK16:
1446 c.w = (c.x << 12) & Short4(0xF000u);
1447 c.z = (c.x) & Short4(0xF000u);
1448 c.y = (c.x << 4) & Short4(0xF000u);
1449 c.x = (c.x << 8) & Short4(0xF000u);
1450 break;
Alexis Hetu4c696962022-03-09 09:28:03 -05001451 case VK_FORMAT_A4R4G4B4_UNORM_PACK16:
Sean Risser159f5122021-09-24 13:08:16 -04001452 c.w = (c.x) & Short4(0xF000u);
1453 c.z = (c.x << 12) & Short4(0xF000u);
1454 c.y = (c.x << 8) & Short4(0xF000u);
1455 c.x = (c.x << 4) & Short4(0xF000u);
1456 break;
Alexis Hetu4c696962022-03-09 09:28:03 -05001457 case VK_FORMAT_A4B4G4R4_UNORM_PACK16:
Sean Risser159f5122021-09-24 13:08:16 -04001458 c.w = (c.x) & Short4(0xF000u);
1459 c.z = (c.x << 4) & Short4(0xF000u);
1460 c.y = (c.x << 8) & Short4(0xF000u);
1461 c.x = (c.x << 12) & Short4(0xF000u);
1462 break;
Sean Risser584c2cf2021-09-14 04:07:02 -04001463 case VK_FORMAT_R5G5B5A1_UNORM_PACK16:
1464 c.w = (c.x << 15) & Short4(0x8000u);
1465 c.z = (c.x << 10) & Short4(0xF800u);
1466 c.y = (c.x << 5) & Short4(0xF800u);
1467 c.x = (c.x) & Short4(0xF800u);
1468 break;
1469 case VK_FORMAT_B5G5R5A1_UNORM_PACK16:
1470 c.w = (c.x << 15) & Short4(0x8000u);
1471 c.z = (c.x) & Short4(0xF800u);
1472 c.y = (c.x << 5) & Short4(0xF800u);
1473 c.x = (c.x << 10) & Short4(0xF800u);
1474 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05001475 case VK_FORMAT_A1R5G5B5_UNORM_PACK16:
1476 c.w = (c.x) & Short4(0x8000u);
1477 c.z = (c.x << 11) & Short4(0xF800u);
1478 c.y = (c.x << 6) & Short4(0xF800u);
1479 c.x = (c.x << 1) & Short4(0xF800u);
1480 break;
1481 default:
1482 ASSERT(false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001483 }
1484 }
1485 else if(has8bitTextureComponents())
1486 {
1487 switch(textureComponentCount())
1488 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001489 case 4:
Nicolas Capens157ba262019-12-10 17:49:14 -05001490 {
1491 Byte4 c0 = Pointer<Byte4>(buffer)[index[0]];
1492 Byte4 c1 = Pointer<Byte4>(buffer)[index[1]];
1493 Byte4 c2 = Pointer<Byte4>(buffer)[index[2]];
1494 Byte4 c3 = Pointer<Byte4>(buffer)[index[3]];
1495 c.x = Unpack(c0, c1);
1496 c.y = Unpack(c2, c3);
1497
1498 switch(state.textureFormat)
1499 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001500 case VK_FORMAT_B8G8R8A8_UNORM:
1501 case VK_FORMAT_B8G8R8A8_SRGB:
1502 c.z = As<Short4>(UnpackLow(c.x, c.y));
1503 c.x = As<Short4>(UnpackHigh(c.x, c.y));
1504 c.y = c.z;
1505 c.w = c.x;
1506 c.z = UnpackLow(As<Byte8>(Short4(0)), As<Byte8>(c.z));
1507 c.y = UnpackHigh(As<Byte8>(Short4(0)), As<Byte8>(c.y));
1508 c.x = UnpackLow(As<Byte8>(Short4(0)), As<Byte8>(c.x));
1509 c.w = UnpackHigh(As<Byte8>(Short4(0)), As<Byte8>(c.w));
1510 break;
1511 case VK_FORMAT_R8G8B8A8_UNORM:
1512 case VK_FORMAT_R8G8B8A8_SNORM:
1513 case VK_FORMAT_R8G8B8A8_SINT:
1514 case VK_FORMAT_R8G8B8A8_SRGB:
1515 case VK_FORMAT_A8B8G8R8_UNORM_PACK32:
1516 case VK_FORMAT_A8B8G8R8_SNORM_PACK32:
1517 case VK_FORMAT_A8B8G8R8_SINT_PACK32:
1518 case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
1519 c.z = As<Short4>(UnpackHigh(c.x, c.y));
1520 c.x = As<Short4>(UnpackLow(c.x, c.y));
1521 c.y = c.x;
1522 c.w = c.z;
1523 c.x = UnpackLow(As<Byte8>(Short4(0)), As<Byte8>(c.x));
1524 c.y = UnpackHigh(As<Byte8>(Short4(0)), As<Byte8>(c.y));
1525 c.z = UnpackLow(As<Byte8>(Short4(0)), As<Byte8>(c.z));
1526 c.w = UnpackHigh(As<Byte8>(Short4(0)), As<Byte8>(c.w));
1527 // Propagate sign bit
1528 if(state.textureFormat == VK_FORMAT_R8G8B8A8_SINT ||
1529 state.textureFormat == VK_FORMAT_A8B8G8R8_SINT_PACK32)
1530 {
1531 c.x >>= 8;
1532 c.y >>= 8;
1533 c.z >>= 8;
1534 c.w >>= 8;
1535 }
1536 break;
1537 case VK_FORMAT_R8G8B8A8_UINT:
1538 case VK_FORMAT_A8B8G8R8_UINT_PACK32:
1539 c.z = As<Short4>(UnpackHigh(c.x, c.y));
1540 c.x = As<Short4>(UnpackLow(c.x, c.y));
1541 c.y = c.x;
1542 c.w = c.z;
1543 c.x = UnpackLow(As<Byte8>(c.x), As<Byte8>(Short4(0)));
1544 c.y = UnpackHigh(As<Byte8>(c.y), As<Byte8>(Short4(0)));
1545 c.z = UnpackLow(As<Byte8>(c.z), As<Byte8>(Short4(0)));
1546 c.w = UnpackHigh(As<Byte8>(c.w), As<Byte8>(Short4(0)));
1547 break;
1548 default:
1549 ASSERT(false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001550 }
1551 }
1552 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05001553 case 2:
1554 c.x = Insert(c.x, Pointer<Short>(buffer)[index[0]], 0);
1555 c.x = Insert(c.x, Pointer<Short>(buffer)[index[1]], 1);
1556 c.x = Insert(c.x, Pointer<Short>(buffer)[index[2]], 2);
1557 c.x = Insert(c.x, Pointer<Short>(buffer)[index[3]], 3);
Nicolas Capens68a82382018-10-02 13:16:55 -04001558
Nicolas Capens112faf42019-12-13 17:32:26 -05001559 switch(state.textureFormat)
1560 {
1561 case VK_FORMAT_R8G8_UNORM:
1562 case VK_FORMAT_R8G8_SNORM:
1563 case VK_FORMAT_R8G8_SRGB:
1564 c.y = (c.x & Short4(0xFF00u));
1565 c.x = (c.x << 8);
Nicolas Capens68a82382018-10-02 13:16:55 -04001566 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05001567 case VK_FORMAT_R8G8_SINT:
1568 c.y = c.x >> 8;
1569 c.x = (c.x << 8) >> 8; // Propagate sign bit
1570 break;
1571 case VK_FORMAT_R8G8_UINT:
1572 c.y = As<Short4>(As<UShort4>(c.x) >> 8);
1573 c.x &= Short4(0x00FFu);
1574 break;
1575 default:
1576 ASSERT(false);
1577 }
1578 break;
1579 case 1:
Nicolas Capens68a82382018-10-02 13:16:55 -04001580 {
Nicolas Capens157ba262019-12-10 17:49:14 -05001581 Int c0 = Int(*Pointer<Byte>(buffer + index[0]));
1582 Int c1 = Int(*Pointer<Byte>(buffer + index[1]));
1583 Int c2 = Int(*Pointer<Byte>(buffer + index[2]));
1584 Int c3 = Int(*Pointer<Byte>(buffer + index[3]));
1585 c0 = c0 | (c1 << 8) | (c2 << 16) | (c3 << 24);
Nicolas Capens68a82382018-10-02 13:16:55 -04001586
1587 switch(state.textureFormat)
1588 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001589 case VK_FORMAT_R8_SINT:
1590 case VK_FORMAT_R8_UINT:
1591 case VK_FORMAT_S8_UINT:
Nicolas Capens157ba262019-12-10 17:49:14 -05001592 {
1593 Int zero(0);
1594 c.x = Unpack(As<Byte4>(c0), As<Byte4>(zero));
1595 // Propagate sign bit
1596 if(state.textureFormat == VK_FORMAT_R8_SINT)
1597 {
1598 c.x = (c.x << 8) >> 8;
1599 }
1600 }
Chris Forbesf4f427a2019-05-17 13:30:38 -07001601 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05001602 case VK_FORMAT_R8_SNORM:
1603 case VK_FORMAT_R8_UNORM:
1604 case VK_FORMAT_R8_SRGB:
1605 // TODO: avoid populating the low bits at all.
1606 c.x = Unpack(As<Byte4>(c0));
1607 c.x &= Short4(0xFF00u);
1608 break;
1609 default:
1610 c.x = Unpack(As<Byte4>(c0));
1611 break;
Nicolas Capens68a82382018-10-02 13:16:55 -04001612 }
1613 }
Nicolas Capens60a6a2e2019-05-06 15:09:44 -04001614 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05001615 default:
1616 ASSERT(false);
Nicolas Capens60a6a2e2019-05-06 15:09:44 -04001617 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001618 }
1619 else if(has16bitTextureComponents())
1620 {
1621 switch(textureComponentCount())
1622 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001623 case 4:
1624 c.x = Pointer<Short4>(buffer)[index[0]];
1625 c.y = Pointer<Short4>(buffer)[index[1]];
1626 c.z = Pointer<Short4>(buffer)[index[2]];
1627 c.w = Pointer<Short4>(buffer)[index[3]];
1628 transpose4x4(c.x, c.y, c.z, c.w);
1629 break;
1630 case 2:
1631 c.x = *Pointer<Short4>(buffer + 4 * index[0]);
1632 c.x = As<Short4>(UnpackLow(c.x, *Pointer<Short4>(buffer + 4 * index[1])));
1633 c.z = *Pointer<Short4>(buffer + 4 * index[2]);
1634 c.z = As<Short4>(UnpackLow(c.z, *Pointer<Short4>(buffer + 4 * index[3])));
1635 c.y = c.x;
1636 c.x = UnpackLow(As<Int2>(c.x), As<Int2>(c.z));
1637 c.y = UnpackHigh(As<Int2>(c.y), As<Int2>(c.z));
1638 break;
1639 case 1:
1640 c.x = Insert(c.x, Pointer<Short>(buffer)[index[0]], 0);
1641 c.x = Insert(c.x, Pointer<Short>(buffer)[index[1]], 1);
1642 c.x = Insert(c.x, Pointer<Short>(buffer)[index[2]], 2);
1643 c.x = Insert(c.x, Pointer<Short>(buffer)[index[3]], 3);
1644 break;
1645 default:
1646 ASSERT(false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001647 }
1648 }
1649 else if(state.textureFormat == VK_FORMAT_A2B10G10R10_UNORM_PACK32)
1650 {
1651 Int4 cc;
1652 cc = Insert(cc, Pointer<Int>(buffer)[index[0]], 0);
1653 cc = Insert(cc, Pointer<Int>(buffer)[index[1]], 1);
1654 cc = Insert(cc, Pointer<Int>(buffer)[index[2]], 2);
1655 cc = Insert(cc, Pointer<Int>(buffer)[index[3]], 3);
Nicolas Capens60a6a2e2019-05-06 15:09:44 -04001656
Nicolas Capens03bb9e12021-11-29 16:54:44 -05001657 c.x = Short4(cc << 6) & Short4(0xFFC0u);
1658 c.y = Short4(cc >> 4) & Short4(0xFFC0u);
1659 c.z = Short4(cc >> 14) & Short4(0xFFC0u);
1660 c.w = Short4(cc >> 16) & Short4(0xC000u);
Nicolas Capens157ba262019-12-10 17:49:14 -05001661 }
Alexis Hetub8a61bf2020-01-09 15:26:34 -05001662 else if(state.textureFormat == VK_FORMAT_A2R10G10B10_UNORM_PACK32)
1663 {
1664 Int4 cc;
1665 cc = Insert(cc, Pointer<Int>(buffer)[index[0]], 0);
1666 cc = Insert(cc, Pointer<Int>(buffer)[index[1]], 1);
1667 cc = Insert(cc, Pointer<Int>(buffer)[index[2]], 2);
1668 cc = Insert(cc, Pointer<Int>(buffer)[index[3]], 3);
1669
Nicolas Capens03bb9e12021-11-29 16:54:44 -05001670 c.x = Short4(cc >> 14) & Short4(0xFFC0u);
1671 c.y = Short4(cc >> 4) & Short4(0xFFC0u);
1672 c.z = Short4(cc << 6) & Short4(0xFFC0u);
1673 c.w = Short4(cc >> 16) & Short4(0xC000u);
Alexis Hetub8a61bf2020-01-09 15:26:34 -05001674 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001675 else if(state.textureFormat == VK_FORMAT_A2B10G10R10_UINT_PACK32)
1676 {
1677 Int4 cc;
1678 cc = Insert(cc, Pointer<Int>(buffer)[index[0]], 0);
1679 cc = Insert(cc, Pointer<Int>(buffer)[index[1]], 1);
1680 cc = Insert(cc, Pointer<Int>(buffer)[index[2]], 2);
1681 cc = Insert(cc, Pointer<Int>(buffer)[index[3]], 3);
1682
Nicolas Capens03bb9e12021-11-29 16:54:44 -05001683 c.x = Short4(cc & Int4(0x3FF));
1684 c.y = Short4((cc >> 10) & Int4(0x3FF));
1685 c.z = Short4((cc >> 20) & Int4(0x3FF));
1686 c.w = Short4((cc >> 30) & Int4(0x3));
Nicolas Capens157ba262019-12-10 17:49:14 -05001687 }
Alexis Hetub8a61bf2020-01-09 15:26:34 -05001688 else if(state.textureFormat == VK_FORMAT_A2R10G10B10_UINT_PACK32)
1689 {
1690 Int4 cc;
1691 cc = Insert(cc, Pointer<Int>(buffer)[index[0]], 0);
1692 cc = Insert(cc, Pointer<Int>(buffer)[index[1]], 1);
1693 cc = Insert(cc, Pointer<Int>(buffer)[index[2]], 2);
1694 cc = Insert(cc, Pointer<Int>(buffer)[index[3]], 3);
1695
Alexis Hetuc236b572020-01-10 13:04:23 -05001696 c.z = Short4((cc & Int4(0x3FF)));
Alexis Hetub8a61bf2020-01-09 15:26:34 -05001697 c.y = Short4(((cc >> 10) & Int4(0x3FF)));
1698 c.x = Short4(((cc >> 20) & Int4(0x3FF)));
1699 c.w = Short4(((cc >> 30) & Int4(0x3)));
1700 }
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001701 else
1702 ASSERT(false);
Nicolas Capens157ba262019-12-10 17:49:14 -05001703
Nicolas Capens81bc9d92019-12-16 15:05:57 -05001704 if(state.textureFormat.isSRGBformat())
Nicolas Capens157ba262019-12-10 17:49:14 -05001705 {
1706 for(int i = 0; i < textureComponentCount(); i++)
1707 {
1708 if(isRGBComponent(i))
1709 {
Nicolas Capens2883de92020-01-27 14:58:14 -05001710 // The current table-based sRGB conversion requires 0xFF00 to represent 1.0.
1711 ASSERT(state.textureFormat.has8bitTextureComponents());
1712
1713 sRGBtoLinearFF00(c[i]);
Nicolas Capens157ba262019-12-10 17:49:14 -05001714 }
1715 }
Nicolas Capens60a6a2e2019-05-06 15:09:44 -04001716 }
1717
Nicolas Capens157ba262019-12-10 17:49:14 -05001718 return c;
1719}
Nicolas Capensbb575d42019-05-31 15:36:59 -04001720
Nicolas Capens9c273852021-12-15 13:44:47 -05001721Vector4s SamplerCore::sampleTexel(Short4 &uuuu, Short4 &vvvv, Short4 &wwww, const Short4 &layerIndex, Vector4i &offset, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer)
Nicolas Capens157ba262019-12-10 17:49:14 -05001722{
1723 Vector4s c;
1724
1725 UInt index[4];
Nicolas Capens9c273852021-12-15 13:44:47 -05001726 computeIndices(index, uuuu, vvvv, wwww, layerIndex, offset, sample, mipmap);
Nicolas Capens157ba262019-12-10 17:49:14 -05001727
1728 if(isYcbcrFormat())
1729 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001730 // Generates 15-bit output.
1731
Nicolas Capens157ba262019-12-10 17:49:14 -05001732 // Pointers to the planes of YCbCr images are stored in consecutive mipmap levels.
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001733 Pointer<Byte> bufferY = buffer; // *Pointer<Pointer<Byte>>(mipmap + 0 * sizeof(Mipmap) + OFFSET(Mipmap, buffer));
Nicolas Capens157ba262019-12-10 17:49:14 -05001734 Pointer<Byte> bufferU = *Pointer<Pointer<Byte>>(mipmap + 1 * sizeof(Mipmap) + OFFSET(Mipmap, buffer)); // U/V for 2-plane interleaved formats.
1735 Pointer<Byte> bufferV = *Pointer<Pointer<Byte>>(mipmap + 2 * sizeof(Mipmap) + OFFSET(Mipmap, buffer));
1736
Jason Macnak341ad7e2022-03-16 18:17:57 -07001737 // Luminance (either 8-bit or 10-bit in bottom bits).
Jason Macnak0587e072022-02-11 16:49:02 -08001738 UShort4 Y;
1739 {
1740 switch(state.textureFormat)
1741 {
1742 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1743 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1744 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001745 Y = Insert(Y, UShort(bufferY[index[0]]), 0);
1746 Y = Insert(Y, UShort(bufferY[index[1]]), 1);
1747 Y = Insert(Y, UShort(bufferY[index[2]]), 2);
1748 Y = Insert(Y, UShort(bufferY[index[3]]), 3);
Jason Macnak0587e072022-02-11 16:49:02 -08001749 }
1750 break;
1751 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1752 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001753 Y = Insert(Y, Pointer<UShort>(bufferY)[index[0]], 0);
Jason Macnak0587e072022-02-11 16:49:02 -08001754 Y = Insert(Y, Pointer<UShort>(bufferY)[index[1]], 1);
1755 Y = Insert(Y, Pointer<UShort>(bufferY)[index[2]], 2);
1756 Y = Insert(Y, Pointer<UShort>(bufferY)[index[3]], 3);
1757 // Top 10 bits of each 16 bits:
1758 Y = (Y & UShort4(0xFFC0u)) >> 6;
Jason Macnak0587e072022-02-11 16:49:02 -08001759 }
1760 break;
1761 default:
1762 UNSUPPORTED("state.textureFormat %d", (int)state.textureFormat);
1763 break;
1764 }
1765 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001766
Jason Macnak341ad7e2022-03-16 18:17:57 -07001767 // Chroma (either 8-bit or 10-bit in bottom bits).
Jason Macnak0587e072022-02-11 16:49:02 -08001768 UShort4 Cb, Cr;
Nicolas Capens68a82382018-10-02 13:16:55 -04001769 {
Nicolas Capens9c273852021-12-15 13:44:47 -05001770 computeIndices(index, uuuu, vvvv, wwww, layerIndex, offset, sample, mipmap + sizeof(Mipmap));
Nicolas Capens157ba262019-12-10 17:49:14 -05001771 UShort4 U, V;
1772
Jason Macnak0587e072022-02-11 16:49:02 -08001773 switch(state.textureFormat)
Nicolas Capens157ba262019-12-10 17:49:14 -05001774 {
Jason Macnak0587e072022-02-11 16:49:02 -08001775 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1776 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001777 U = Insert(U, UShort(bufferU[index[0]]), 0);
1778 U = Insert(U, UShort(bufferU[index[1]]), 1);
1779 U = Insert(U, UShort(bufferU[index[2]]), 2);
1780 U = Insert(U, UShort(bufferU[index[3]]), 3);
Nicolas Capens157ba262019-12-10 17:49:14 -05001781
Jason Macnak341ad7e2022-03-16 18:17:57 -07001782 V = Insert(V, UShort(bufferV[index[0]]), 0);
1783 V = Insert(V, UShort(bufferV[index[1]]), 1);
1784 V = Insert(V, UShort(bufferV[index[2]]), 2);
1785 V = Insert(V, UShort(bufferV[index[3]]), 3);
Jason Macnak0587e072022-02-11 16:49:02 -08001786 }
1787 break;
1788 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1789 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001790 UShort4 UV;
1791 UV = Insert(UV, Pointer<UShort>(bufferU)[index[0]], 0);
1792 UV = Insert(UV, Pointer<UShort>(bufferU)[index[1]], 1);
1793 UV = Insert(UV, Pointer<UShort>(bufferU)[index[2]], 2);
1794 UV = Insert(UV, Pointer<UShort>(bufferU)[index[3]], 3);
1795
1796 U = (UV & UShort4(0x00FFu));
1797 V = (UV & UShort4(0xFF00u)) >> 8;
Jason Macnak0587e072022-02-11 16:49:02 -08001798 }
1799 break;
1800 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1801 {
1802 UInt4 UV;
1803 UV = Insert(UV, Pointer<UInt>(bufferU)[index[0]], 0);
1804 UV = Insert(UV, Pointer<UInt>(bufferU)[index[1]], 1);
1805 UV = Insert(UV, Pointer<UInt>(bufferU)[index[2]], 2);
1806 UV = Insert(UV, Pointer<UInt>(bufferU)[index[3]], 3);
Jason Macnak341ad7e2022-03-16 18:17:57 -07001807 // Top 10 bits of first 16-bits:
1808 U = UShort4((UV & UInt4(0x0000FFC0u)) >> 6);
1809 // Top 10 bits of second 16-bits:
1810 V = UShort4((UV & UInt4(0xFFC00000u)) >> 22);
Jason Macnak0587e072022-02-11 16:49:02 -08001811 }
1812 break;
1813 default:
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001814 UNSUPPORTED("state.textureFormat %d", (int)state.textureFormat);
Jason Macnak0587e072022-02-11 16:49:02 -08001815 break;
1816 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001817
1818 if(!state.swappedChroma)
1819 {
1820 Cb = U;
1821 Cr = V;
1822 }
1823 else
1824 {
1825 Cb = V;
1826 Cr = U;
1827 }
1828 }
1829
Jason Macnak341ad7e2022-03-16 18:17:57 -07001830 uint8_t lumaBits = 8;
1831 uint8_t chromaBits = 8;
1832 switch(state.textureFormat)
1833 {
1834 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
1835 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
1836 lumaBits = 8;
1837 chromaBits = 8;
1838 break;
1839 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
1840 lumaBits = 10;
1841 chromaBits = 10;
1842 break;
1843 default:
1844 UNSUPPORTED("state.textureFormat %d", (int)state.textureFormat);
1845 break;
1846 }
1847
Nicolas Capens157ba262019-12-10 17:49:14 -05001848 if(state.ycbcrModel == VK_SAMPLER_YCBCR_MODEL_CONVERSION_RGB_IDENTITY)
1849 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001850 // Scale to the output 15-bit.
1851 c.x = Cr << (15 - chromaBits);
1852 c.y = Y << (15 - lumaBits);
1853 c.z = Cb << (15 - chromaBits);
Nicolas Capens68a82382018-10-02 13:16:55 -04001854 }
1855 else
1856 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001857 const float twoPowLumaBits = static_cast<float>(0x1u << lumaBits);
1858 const float twoPowLumaBitsMinus8 = static_cast<float>(0x1u << (lumaBits - 8));
1859 const float twoPowChromaBits = static_cast<float>(0x1u << chromaBits);
1860 const float twoPowChromaBitsMinus1 = static_cast<float>(0x1u << (chromaBits - 1));
1861 const float twoPowChromaBitsMinus8 = static_cast<float>(0x1u << (chromaBits - 8));
Nicolas Capens68a82382018-10-02 13:16:55 -04001862
Jason Macnak341ad7e2022-03-16 18:17:57 -07001863 Float4 y = Float4(Y);
1864 Float4 u = Float4(Cb);
1865 Float4 v = Float4(Cr);
1866
1867 if(state.studioSwing)
1868 {
1869 // See https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#QUANTIZATION_NARROW
1870 y = ((y / Float4(twoPowLumaBitsMinus8)) - Float4(16.0f)) / Float4(219.0f);
1871 u = ((u / Float4(twoPowChromaBitsMinus8)) - Float4(128.0f)) / Float4(224.0f);
1872 v = ((v / Float4(twoPowChromaBitsMinus8)) - Float4(128.0f)) / Float4(224.0f);
1873 }
1874 else
1875 {
1876 // See https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#QUANTIZATION_FULL
1877 y = y / Float4(twoPowLumaBits - 1.0f);
1878 u = (u - Float4(twoPowChromaBitsMinus1)) / Float4(twoPowChromaBits - 1.0f);
1879 v = (v - Float4(twoPowChromaBitsMinus1)) / Float4(twoPowChromaBits - 1.0f);
1880 }
1881
1882 // Now, `y` is in [0, 1] and `u` and `v` are in [-0.5, 0.5].
Nicolas Capens157ba262019-12-10 17:49:14 -05001883
1884 if(state.ycbcrModel == VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_IDENTITY)
Nicolas Capens68a82382018-10-02 13:16:55 -04001885 {
Jason Macnak341ad7e2022-03-16 18:17:57 -07001886 c.x = Short4(v * static_cast<float>(0x7FFF));
1887 c.y = Short4(y * static_cast<float>(0x7FFF));
1888 c.z = Short4(u * static_cast<float>(0x7FFF));
Nicolas Capens68a82382018-10-02 13:16:55 -04001889 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001890 else
Nicolas Capens68a82382018-10-02 13:16:55 -04001891 {
Nicolas Capens157ba262019-12-10 17:49:14 -05001892 // Generic YCbCr to RGB transformation:
1893 // R = Y + 2 * (1 - Kr) * Cr
1894 // G = Y - 2 * Kb * (1 - Kb) / Kg * Cb - 2 * Kr * (1 - Kr) / Kg * Cr
1895 // B = Y + 2 * (1 - Kb) * Cb
Nicolas Capens68a82382018-10-02 13:16:55 -04001896
Nicolas Capens157ba262019-12-10 17:49:14 -05001897 float Kb = 0.114f;
1898 float Kr = 0.299f;
Nicolas Capens68a82382018-10-02 13:16:55 -04001899
Nicolas Capens157ba262019-12-10 17:49:14 -05001900 switch(state.ycbcrModel)
Alexis Hetu036cc9a2019-05-16 17:24:22 -04001901 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001902 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709:
1903 Kb = 0.0722f;
1904 Kr = 0.2126f;
1905 break;
1906 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601:
1907 Kb = 0.114f;
1908 Kr = 0.299f;
1909 break;
1910 case VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_2020:
1911 Kb = 0.0593f;
1912 Kr = 0.2627f;
1913 break;
1914 default:
1915 UNSUPPORTED("ycbcrModel %d", int(state.ycbcrModel));
Alexis Hetu036cc9a2019-05-16 17:24:22 -04001916 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001917
1918 const float Kg = 1.0f - Kr - Kb;
1919
1920 const float Rr = 2 * (1 - Kr);
1921 const float Gb = -2 * Kb * (1 - Kb) / Kg;
1922 const float Gr = -2 * Kr * (1 - Kr) / Kg;
1923 const float Bb = 2 * (1 - Kb);
1924
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001925 Float4 r = y + Float4(Rr) * v;
Nicolas Capens157ba262019-12-10 17:49:14 -05001926 Float4 g = y + Float4(Gb) * u + Float4(Gr) * v;
Ben Claytonbc1c067be2019-12-17 20:37:37 +00001927 Float4 b = y + Float4(Bb) * u;
Nicolas Capens157ba262019-12-10 17:49:14 -05001928
Jason Macnak341ad7e2022-03-16 18:17:57 -07001929 c.x = Short4(r * static_cast<float>(0x7FFF));
1930 c.y = Short4(g * static_cast<float>(0x7FFF));
1931 c.z = Short4(b * static_cast<float>(0x7FFF));
Nicolas Capens68a82382018-10-02 13:16:55 -04001932 }
Nicolas Capens157ba262019-12-10 17:49:14 -05001933 }
1934 }
1935 else
1936 {
1937 return sampleTexel(index, buffer);
1938 }
1939
1940 return c;
1941}
1942
Nicolas Capens9c273852021-12-15 13:44:47 -05001943Vector4f SamplerCore::sampleTexel(Int4 &uuuu, Int4 &vvvv, Int4 &wwww, Float4 &dRef, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer)
Nicolas Capens157ba262019-12-10 17:49:14 -05001944{
1945 Int4 valid;
1946
1947 if(borderModeActive())
1948 {
1949 // Valid texels have positive coordinates.
Nicolas Capens30873702020-08-01 09:17:23 -04001950 Int4 negative = uuuu;
Nicolas Capensa202c202020-08-05 14:42:22 -04001951 if(state.is2D() || state.is3D() || state.isCube()) negative |= vvvv;
1952 if(state.is3D() || state.isCube() || state.isArrayed()) negative |= wwww;
Nicolas Capens157ba262019-12-10 17:49:14 -05001953 valid = CmpNLT(negative, Int4(0));
1954 }
1955
1956 UInt index[4];
Nicolas Capens9c273852021-12-15 13:44:47 -05001957 computeIndices(index, uuuu, vvvv, wwww, sample, valid, mipmap);
Nicolas Capens157ba262019-12-10 17:49:14 -05001958
1959 Vector4f c;
1960
1961 if(hasFloatTexture() || has32bitIntegerTextureComponents())
1962 {
Nicolas Capens30873702020-08-01 09:17:23 -04001963 UInt4 t0, t1, t2, t3;
1964
Nicolas Capens81bc9d92019-12-16 15:05:57 -05001965 switch(state.textureFormat)
Nicolas Capens157ba262019-12-10 17:49:14 -05001966 {
Nicolas Capens112faf42019-12-13 17:32:26 -05001967 case VK_FORMAT_R16_SFLOAT:
1968 t0 = Int4(*Pointer<UShort4>(buffer + index[0] * 2));
1969 t1 = Int4(*Pointer<UShort4>(buffer + index[1] * 2));
1970 t2 = Int4(*Pointer<UShort4>(buffer + index[2] * 2));
1971 t3 = Int4(*Pointer<UShort4>(buffer + index[3] * 2));
Nicolas Capens157ba262019-12-10 17:49:14 -05001972
Nicolas Capens112faf42019-12-13 17:32:26 -05001973 c.x.x = Extract(As<Float4>(halfToFloatBits(t0)), 0);
1974 c.x.y = Extract(As<Float4>(halfToFloatBits(t1)), 0);
1975 c.x.z = Extract(As<Float4>(halfToFloatBits(t2)), 0);
1976 c.x.w = Extract(As<Float4>(halfToFloatBits(t3)), 0);
1977 break;
1978 case VK_FORMAT_R16G16_SFLOAT:
1979 t0 = Int4(*Pointer<UShort4>(buffer + index[0] * 4));
1980 t1 = Int4(*Pointer<UShort4>(buffer + index[1] * 4));
1981 t2 = Int4(*Pointer<UShort4>(buffer + index[2] * 4));
1982 t3 = Int4(*Pointer<UShort4>(buffer + index[3] * 4));
Nicolas Capens157ba262019-12-10 17:49:14 -05001983
Nicolas Capens9c273852021-12-15 13:44:47 -05001984 // TODO: shuffles
Nicolas Capens112faf42019-12-13 17:32:26 -05001985 c.x = As<Float4>(halfToFloatBits(t0));
1986 c.y = As<Float4>(halfToFloatBits(t1));
1987 c.z = As<Float4>(halfToFloatBits(t2));
1988 c.w = As<Float4>(halfToFloatBits(t3));
1989 transpose4x4(c.x, c.y, c.z, c.w);
1990 break;
1991 case VK_FORMAT_R16G16B16A16_SFLOAT:
1992 t0 = Int4(*Pointer<UShort4>(buffer + index[0] * 8));
1993 t1 = Int4(*Pointer<UShort4>(buffer + index[1] * 8));
1994 t2 = Int4(*Pointer<UShort4>(buffer + index[2] * 8));
1995 t3 = Int4(*Pointer<UShort4>(buffer + index[3] * 8));
Nicolas Capens157ba262019-12-10 17:49:14 -05001996
Nicolas Capens112faf42019-12-13 17:32:26 -05001997 c.x = As<Float4>(halfToFloatBits(t0));
1998 c.y = As<Float4>(halfToFloatBits(t1));
1999 c.z = As<Float4>(halfToFloatBits(t2));
2000 c.w = As<Float4>(halfToFloatBits(t3));
2001 transpose4x4(c.x, c.y, c.z, c.w);
2002 break;
2003 case VK_FORMAT_R32_SFLOAT:
2004 case VK_FORMAT_R32_SINT:
2005 case VK_FORMAT_R32_UINT:
2006 case VK_FORMAT_D32_SFLOAT:
Nicolas Capens9c273852021-12-15 13:44:47 -05002007 // TODO: Optimal shuffling?
Nicolas Capens112faf42019-12-13 17:32:26 -05002008 c.x.x = *Pointer<Float>(buffer + index[0] * 4);
2009 c.x.y = *Pointer<Float>(buffer + index[1] * 4);
2010 c.x.z = *Pointer<Float>(buffer + index[2] * 4);
2011 c.x.w = *Pointer<Float>(buffer + index[3] * 4);
2012 break;
2013 case VK_FORMAT_R32G32_SFLOAT:
2014 case VK_FORMAT_R32G32_SINT:
2015 case VK_FORMAT_R32G32_UINT:
Nicolas Capens9c273852021-12-15 13:44:47 -05002016 // TODO: Optimal shuffling?
Nicolas Capens112faf42019-12-13 17:32:26 -05002017 c.x.xy = *Pointer<Float4>(buffer + index[0] * 8);
2018 c.x.zw = *Pointer<Float4>(buffer + index[1] * 8 - 8);
2019 c.z.xy = *Pointer<Float4>(buffer + index[2] * 8);
2020 c.z.zw = *Pointer<Float4>(buffer + index[3] * 8 - 8);
2021 c.y = c.x;
2022 c.x = Float4(c.x.xz, c.z.xz);
2023 c.y = Float4(c.y.yw, c.z.yw);
2024 break;
2025 case VK_FORMAT_R32G32B32A32_SFLOAT:
2026 case VK_FORMAT_R32G32B32A32_SINT:
2027 case VK_FORMAT_R32G32B32A32_UINT:
2028 c.x = *Pointer<Float4>(buffer + index[0] * 16, 16);
2029 c.y = *Pointer<Float4>(buffer + index[1] * 16, 16);
2030 c.z = *Pointer<Float4>(buffer + index[2] * 16, 16);
2031 c.w = *Pointer<Float4>(buffer + index[3] * 16, 16);
2032 transpose4x4(c.x, c.y, c.z, c.w);
2033 break;
2034 case VK_FORMAT_E5B9G9R9_UFLOAT_PACK32:
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002035 {
2036 Float4 t; // TODO: add Insert(UInt4, RValue<UInt>)
2037 t.x = *Pointer<Float>(buffer + index[0] * 4);
2038 t.y = *Pointer<Float>(buffer + index[1] * 4);
2039 t.z = *Pointer<Float>(buffer + index[2] * 4);
2040 t.w = *Pointer<Float>(buffer + index[3] * 4);
2041 t0 = As<UInt4>(t);
2042 c.w = Float4(UInt4(1) << ((t0 >> 27) & UInt4(0x1F))) * Float4(1.0f / (1 << 24));
Alexis Hetuc236b572020-01-10 13:04:23 -05002043 c.x = Float4(t0 & UInt4(0x1FF)) * c.w;
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002044 c.y = Float4((t0 >> 9) & UInt4(0x1FF)) * c.w;
2045 c.z = Float4((t0 >> 18) & UInt4(0x1FF)) * c.w;
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002046 }
Nicolas Capens112faf42019-12-13 17:32:26 -05002047 break;
2048 case VK_FORMAT_B10G11R11_UFLOAT_PACK32:
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002049 {
2050 Float4 t; // TODO: add Insert(UInt4, RValue<UInt>)
2051 t.x = *Pointer<Float>(buffer + index[0] * 4);
2052 t.y = *Pointer<Float>(buffer + index[1] * 4);
2053 t.z = *Pointer<Float>(buffer + index[2] * 4);
2054 t.w = *Pointer<Float>(buffer + index[3] * 4);
2055 t0 = As<UInt4>(t);
2056 c.x = As<Float4>(halfToFloatBits((t0 << 4) & UInt4(0x7FF0)));
2057 c.y = As<Float4>(halfToFloatBits((t0 >> 7) & UInt4(0x7FF0)));
2058 c.z = As<Float4>(halfToFloatBits((t0 >> 17) & UInt4(0x7FE0)));
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002059 }
Nicolas Capens112faf42019-12-13 17:32:26 -05002060 break;
2061 default:
2062 UNSUPPORTED("Format %d", VkFormat(state.textureFormat));
Nicolas Capens157ba262019-12-10 17:49:14 -05002063 }
2064 }
2065 else
2066 {
2067 ASSERT(!isYcbcrFormat());
2068
2069 Vector4s cs = sampleTexel(index, buffer);
2070
Nicolas Capens9d9f30d2020-01-12 03:26:18 -05002071 bool isInteger = state.textureFormat.isUnnormalizedInteger();
Nicolas Capens157ba262019-12-10 17:49:14 -05002072 int componentCount = textureComponentCount();
2073 for(int n = 0; n < componentCount; n++)
2074 {
2075 if(hasUnsignedTextureComponent(n))
Nicolas Capens57253152019-05-10 20:25:17 -07002076 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002077 if(isInteger)
Nicolas Capens57253152019-05-10 20:25:17 -07002078 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002079 c[n] = As<Float4>(Int4(As<UShort4>(cs[n])));
Nicolas Capens68a82382018-10-02 13:16:55 -04002080 }
2081 else
2082 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002083 c[n] = Float4(As<UShort4>(cs[n]));
Nicolas Capens68a82382018-10-02 13:16:55 -04002084 }
Nicolas Capens68a82382018-10-02 13:16:55 -04002085 }
Nicolas Capens157ba262019-12-10 17:49:14 -05002086 else
2087 {
2088 if(isInteger)
2089 {
2090 c[n] = As<Float4>(Int4(cs[n]));
2091 }
2092 else
2093 {
2094 c[n] = Float4(cs[n]);
2095 }
2096 }
2097 }
2098 }
2099
Nicolas Capensa190cee2022-04-02 01:52:41 -04002100 if(borderModeActive())
2101 {
2102 c = replaceBorderTexel(c, valid);
2103 }
2104
Nicolas Capens157ba262019-12-10 17:49:14 -05002105 if(state.compareEnable)
2106 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04002107 Float4 ref = dRef;
Nicolas Capens157ba262019-12-10 17:49:14 -05002108
2109 if(!hasFloatTexture())
2110 {
2111 // D16_UNORM: clamp reference, normalize texel value
2112 ref = Min(Max(ref, Float4(0.0f)), Float4(1.0f));
2113 c.x = c.x * Float4(1.0f / 0xFFFF);
2114 }
2115
2116 Int4 boolean;
2117
2118 switch(state.compareOp)
2119 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002120 case VK_COMPARE_OP_LESS_OR_EQUAL: boolean = CmpLE(ref, c.x); break;
2121 case VK_COMPARE_OP_GREATER_OR_EQUAL: boolean = CmpNLT(ref, c.x); break;
2122 case VK_COMPARE_OP_LESS: boolean = CmpLT(ref, c.x); break;
2123 case VK_COMPARE_OP_GREATER: boolean = CmpNLE(ref, c.x); break;
2124 case VK_COMPARE_OP_EQUAL: boolean = CmpEQ(ref, c.x); break;
2125 case VK_COMPARE_OP_NOT_EQUAL: boolean = CmpNEQ(ref, c.x); break;
2126 case VK_COMPARE_OP_ALWAYS: boolean = Int4(-1); break;
2127 case VK_COMPARE_OP_NEVER: boolean = Int4(0); break;
2128 default: ASSERT(false);
Nicolas Capens157ba262019-12-10 17:49:14 -05002129 }
2130
2131 c.x = As<Float4>(boolean & As<Int4>(Float4(1.0f)));
2132 c.y = Float4(0.0f);
2133 c.z = Float4(0.0f);
2134 c.w = Float4(1.0f);
2135 }
2136
Nicolas Capens157ba262019-12-10 17:49:14 -05002137 return c;
2138}
2139
2140Vector4f SamplerCore::replaceBorderTexel(const Vector4f &c, Int4 valid)
2141{
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002142 Vector4i border;
Nicolas Capens157ba262019-12-10 17:49:14 -05002143
Alexis Hetu1bf3ae22022-05-31 21:40:10 -04002144 const bool scaled = hasNormalizedFormat();
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002145 const sw::float4 scaleComp = scaled ? getComponentScale() : sw::float4(1.0f, 1.0f, 1.0f, 1.0f);
Nicolas Capens157ba262019-12-10 17:49:14 -05002146
2147 switch(state.border)
2148 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002149 case VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK:
2150 case VK_BORDER_COLOR_INT_TRANSPARENT_BLACK:
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002151 border.x = Int4(0);
2152 border.y = Int4(0);
2153 border.z = Int4(0);
2154 border.w = Int4(0);
Nicolas Capens112faf42019-12-13 17:32:26 -05002155 break;
2156 case VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK:
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002157 border.x = Int4(0);
2158 border.y = Int4(0);
2159 border.z = Int4(0);
Ari Suonpääa6ec85f2021-10-27 14:40:18 +03002160 border.w = Int4(bit_cast<int>(scaleComp.w));
Nicolas Capens112faf42019-12-13 17:32:26 -05002161 break;
2162 case VK_BORDER_COLOR_INT_OPAQUE_BLACK:
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002163 border.x = Int4(0);
2164 border.y = Int4(0);
2165 border.z = Int4(0);
2166 border.w = Int4(1);
Nicolas Capens112faf42019-12-13 17:32:26 -05002167 break;
2168 case VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE:
Ari Suonpääa6ec85f2021-10-27 14:40:18 +03002169 border.x = Int4(bit_cast<int>(scaleComp.x));
2170 border.y = Int4(bit_cast<int>(scaleComp.y));
2171 border.z = Int4(bit_cast<int>(scaleComp.z));
2172 border.w = Int4(bit_cast<int>(scaleComp.w));
Nicolas Capens112faf42019-12-13 17:32:26 -05002173 break;
2174 case VK_BORDER_COLOR_INT_OPAQUE_WHITE:
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002175 border.x = Int4(1);
2176 border.y = Int4(1);
2177 border.z = Int4(1);
2178 border.w = Int4(1);
2179 break;
2180 case VK_BORDER_COLOR_FLOAT_CUSTOM_EXT:
2181 // This bit-casts from float to int in C++ code instead of Reactor code
2182 // because Reactor does not guarantee preserving infinity (b/140302841).
Ari Suonpääa6ec85f2021-10-27 14:40:18 +03002183 border.x = Int4(bit_cast<int>(scaleComp.x * state.customBorder.float32[0]));
2184 border.y = Int4(bit_cast<int>(scaleComp.y * state.customBorder.float32[1]));
2185 border.z = Int4(bit_cast<int>(scaleComp.z * state.customBorder.float32[2]));
2186 border.w = Int4(bit_cast<int>(scaleComp.w * state.customBorder.float32[3]));
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002187 break;
2188 case VK_BORDER_COLOR_INT_CUSTOM_EXT:
2189 border.x = Int4(state.customBorder.int32[0]);
2190 border.y = Int4(state.customBorder.int32[1]);
2191 border.z = Int4(state.customBorder.int32[2]);
2192 border.w = Int4(state.customBorder.int32[3]);
Nicolas Capens112faf42019-12-13 17:32:26 -05002193 break;
2194 default:
2195 UNSUPPORTED("sint/uint/sfloat border: %u", state.border);
Nicolas Capens157ba262019-12-10 17:49:14 -05002196 }
2197
2198 Vector4f out;
Nicolas Capens8532b0f2021-05-20 02:52:30 -04002199 out.x = As<Float4>((valid & As<Int4>(c.x)) | (~valid & border.x)); // TODO: IfThenElse()
2200 out.y = As<Float4>((valid & As<Int4>(c.y)) | (~valid & border.y));
2201 out.z = As<Float4>((valid & As<Int4>(c.z)) | (~valid & border.z));
2202 out.w = As<Float4>((valid & As<Int4>(c.w)) | (~valid & border.w));
Nicolas Capens157ba262019-12-10 17:49:14 -05002203
2204 return out;
2205}
2206
Nicolas Capens83373b92021-12-15 06:43:29 -05002207Pointer<Byte> SamplerCore::selectMipmap(const Pointer<Byte> &texture, const Float &lod, bool secondLOD)
Nicolas Capens157ba262019-12-10 17:49:14 -05002208{
2209 Pointer<Byte> mipmap0 = texture + OFFSET(Texture, mipmap[0]);
2210
2211 if(state.mipmapFilter == MIPMAP_NONE)
2212 {
Nicolas Capens83373b92021-12-15 06:43:29 -05002213 return mipmap0;
Nicolas Capens157ba262019-12-10 17:49:14 -05002214 }
Nicolas Capens83373b92021-12-15 06:43:29 -05002215
2216 Int ilod;
2217
2218 if(state.mipmapFilter == MIPMAP_POINT)
Nicolas Capens157ba262019-12-10 17:49:14 -05002219 {
Nicolas Capens83373b92021-12-15 06:43:29 -05002220 // TODO: Preferred formula is ceil(lod + 0.5) - 1
2221 ilod = RoundInt(lod);
2222 }
2223 else // MIPMAP_LINEAR
2224 {
2225 ilod = Int(lod);
Nicolas Capens157ba262019-12-10 17:49:14 -05002226 }
2227
Nicolas Capens83373b92021-12-15 06:43:29 -05002228 return mipmap0 + ilod * sizeof(Mipmap) + secondLOD * sizeof(Mipmap);
Nicolas Capens157ba262019-12-10 17:49:14 -05002229}
2230
2231Int4 SamplerCore::computeFilterOffset(Float &lod)
2232{
2233 if(state.textureFilter == FILTER_POINT)
2234 {
2235 return Int4(0);
2236 }
2237 else if(state.textureFilter == FILTER_MIN_LINEAR_MAG_POINT)
2238 {
2239 return CmpNLE(Float4(lod), Float4(0.0f));
2240 }
2241 else if(state.textureFilter == FILTER_MIN_POINT_MAG_LINEAR)
2242 {
2243 return CmpLE(Float4(lod), Float4(0.0f));
2244 }
2245
2246 return Int4(~0);
2247}
2248
2249Short4 SamplerCore::address(const Float4 &uw, AddressingMode addressingMode, Pointer<Byte> &mipmap)
2250{
2251 if(addressingMode == ADDRESSING_UNUSED)
2252 {
Nicolas Capensa00fa432020-08-03 16:01:16 -04002253 return Short4(0); // TODO(b/134669567): Optimize for 1D filtering
Nicolas Capens157ba262019-12-10 17:49:14 -05002254 }
Nicolas Capens157ba262019-12-10 17:49:14 -05002255 else if(addressingMode == ADDRESSING_CLAMP || addressingMode == ADDRESSING_BORDER)
2256 {
2257 Float4 clamp = Min(Max(uw, Float4(0.0f)), Float4(65535.0f / 65536.0f));
2258
2259 return Short4(Int4(clamp * Float4(1 << 16)));
2260 }
2261 else if(addressingMode == ADDRESSING_MIRROR)
2262 {
2263 Int4 convert = Int4(uw * Float4(1 << 16));
2264 Int4 mirror = (convert << 15) >> 31;
2265
2266 convert ^= mirror;
2267
2268 return Short4(convert);
2269 }
2270 else if(addressingMode == ADDRESSING_MIRRORONCE)
2271 {
2272 // Absolute value
2273 Int4 convert = Int4(Abs(uw * Float4(1 << 16)));
2274
2275 // Clamp
2276 convert -= Int4(0x00008000, 0x00008000, 0x00008000, 0x00008000);
2277 convert = As<Int4>(PackSigned(convert, convert));
2278
2279 return As<Short4>(Int2(convert)) + Short4(0x8000u);
2280 }
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002281 else // Wrap
Nicolas Capens157ba262019-12-10 17:49:14 -05002282 {
2283 return Short4(Int4(uw * Float4(1 << 16)));
2284 }
2285}
2286
Nicolas Capens9c273852021-12-15 13:44:47 -05002287Short4 SamplerCore::computeLayerIndex16(const Float4 &a, Pointer<Byte> &mipmap)
Nicolas Capens30873702020-08-01 09:17:23 -04002288{
2289 if(!state.isArrayed())
2290 {
2291 return {};
2292 }
2293
2294 Int4 layers = *Pointer<Int4>(mipmap + OFFSET(Mipmap, depth));
2295
2296 return Short4(Min(Max(RoundInt(a), Int4(0)), layers - Int4(1)));
2297}
2298
Nicolas Capens157ba262019-12-10 17:49:14 -05002299// TODO: Eliminate when the gather + mirror addressing case is handled by mirroring the footprint.
2300static Int4 mirror(Int4 n)
2301{
2302 auto positive = CmpNLT(n, Int4(0));
2303 return (positive & n) | (~positive & (-(Int4(1) + n)));
2304}
2305
2306static Int4 mod(Int4 n, Int4 d)
2307{
2308 auto x = n % d;
2309 auto positive = CmpNLT(x, Int4(0));
2310 return (positive & x) | (~positive & (x + d));
2311}
2312
Nicolas Capens9c273852021-12-15 13:44:47 -05002313void SamplerCore::address(const Float4 &uvw, Int4 &xyz0, Int4 &xyz1, Float4 &f, Pointer<Byte> &mipmap, Int4 &offset, Int4 &filter, int whd, AddressingMode addressingMode)
Nicolas Capens157ba262019-12-10 17:49:14 -05002314{
2315 if(addressingMode == ADDRESSING_UNUSED)
2316 {
Nicolas Capensa00fa432020-08-03 16:01:16 -04002317 f = Float4(0.0f); // TODO(b/134669567): Optimize for 1D filtering
Nicolas Capens157ba262019-12-10 17:49:14 -05002318 return;
2319 }
2320
Nicolas Capens71196862022-01-06 21:31:57 -05002321 Int4 dim = As<Int4>(*Pointer<UInt4>(mipmap + whd, 16));
Nicolas Capens157ba262019-12-10 17:49:14 -05002322 Int4 maxXYZ = dim - Int4(1);
2323
Nicolas Capens30873702020-08-01 09:17:23 -04002324 if(function == Fetch) // Unnormalized coordinates
Nicolas Capens157ba262019-12-10 17:49:14 -05002325 {
Nicolas Capens30873702020-08-01 09:17:23 -04002326 Int4 xyz = function.offset ? As<Int4>(uvw) + offset : As<Int4>(uvw);
Nicolas Capens1c004412020-07-27 11:55:14 -04002327 xyz0 = Min(Max(xyz, Int4(0)), maxXYZ);
2328
2329 // VK_EXT_image_robustness requires checking for out-of-bounds accesses.
Nicolas Capens1c004412020-07-27 11:55:14 -04002330 // TODO(b/162327166): Only perform bounds checks when VK_EXT_image_robustness is enabled.
2331 // If the above clamping altered the result, the access is out-of-bounds.
2332 // In that case set the coordinate to -1 to perform texel replacement later.
2333 Int4 outOfBounds = CmpNEQ(xyz, xyz0);
2334 xyz0 |= outOfBounds;
Nicolas Capens157ba262019-12-10 17:49:14 -05002335 }
Nicolas Capens157ba262019-12-10 17:49:14 -05002336 else if(addressingMode == ADDRESSING_CUBEFACE)
2337 {
2338 xyz0 = As<Int4>(uvw);
2339 }
2340 else
2341 {
Nicolas Capenscd848cd2022-01-05 17:04:20 -05002342 const int oneBits = 0x3F7FFFFF; // Value just under 1.0f
Nicolas Capens157ba262019-12-10 17:49:14 -05002343
Nicolas Capens157ba262019-12-10 17:49:14 -05002344 Float4 coord = uvw;
2345
2346 if(state.unnormalizedCoordinates)
2347 {
2348 switch(addressingMode)
2349 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002350 case ADDRESSING_CLAMP:
2351 coord = Min(Max(coord, Float4(0.0f)), Float4(dim) * As<Float4>(Int4(oneBits)));
2352 break;
2353 case ADDRESSING_BORDER:
2354 // Don't map to a valid range here.
2355 break;
2356 default:
2357 // "If unnormalizedCoordinates is VK_TRUE, addressModeU and addressModeV must each be
2358 // either VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE or VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER"
2359 UNREACHABLE("addressingMode %d", int(addressingMode));
2360 break;
Nicolas Capens157ba262019-12-10 17:49:14 -05002361 }
2362 }
2363 else if(state.textureFilter == FILTER_GATHER && addressingMode == ADDRESSING_MIRROR)
2364 {
2365 // Gather requires the 'footprint' of the texels from which a component is taken, to also mirror around.
2366 // Therefore we can't just compute one texel's location and find the other ones at +1 offsets from it.
2367 // Here we handle that case separately by doing the mirroring per texel coordinate.
2368 // TODO: Mirror the footprint by adjusting the sign of the 0.5f and 1 offsets.
2369
2370 coord = coord * Float4(dim);
2371 coord -= Float4(0.5f);
2372 Float4 floor = Floor(coord);
2373 xyz0 = Int4(floor);
Nicolas Capens68a82382018-10-02 13:16:55 -04002374
Nicolas Capens2e40a572020-07-29 14:18:55 -04002375 if(function.offset)
Nicolas Capens68a82382018-10-02 13:16:55 -04002376 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04002377 xyz0 += offset;
Nicolas Capens68a82382018-10-02 13:16:55 -04002378 }
2379
Nicolas Capens157ba262019-12-10 17:49:14 -05002380 xyz1 = xyz0 + Int4(1);
Nicolas Capens68a82382018-10-02 13:16:55 -04002381
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002382 xyz0 = (maxXYZ)-mirror(mod(xyz0, Int4(2) * dim) - dim);
2383 xyz1 = (maxXYZ)-mirror(mod(xyz1, Int4(2) * dim) - dim);
Nicolas Capens68a82382018-10-02 13:16:55 -04002384
Nicolas Capens157ba262019-12-10 17:49:14 -05002385 return;
2386 }
2387 else
2388 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04002389 if(!function.offset)
Nicolas Capens68a82382018-10-02 13:16:55 -04002390 {
2391 switch(addressingMode)
2392 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002393 case ADDRESSING_CLAMP:
2394 case ADDRESSING_SEAMLESS:
2395 // While cube face coordinates are nominally already in the [0.0, 1.0] range
2396 // due to the projection, and numerical imprecision is tolerated due to the
2397 // border of pixels for seamless filtering, the projection doesn't cause
2398 // range normalization for Inf and NaN values. So we always clamp.
2399 {
2400 Float4 one = As<Float4>(Int4(oneBits));
2401 coord = Min(Max(coord, Float4(0.0f)), one);
2402 }
2403 break;
2404 case ADDRESSING_MIRROR:
Nicolas Capens157ba262019-12-10 17:49:14 -05002405 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002406 Float4 one = As<Float4>(Int4(oneBits));
Nicolas Capenscd848cd2022-01-05 17:04:20 -05002407 coord = coord * Float4(0.5f);
2408 coord = Float4(2.0f) * Abs(coord - Round(coord));
2409 coord = Min(coord, one);
Nicolas Capens157ba262019-12-10 17:49:14 -05002410 }
2411 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05002412 case ADDRESSING_MIRRORONCE:
Nicolas Capens157ba262019-12-10 17:49:14 -05002413 {
Nicolas Capens157ba262019-12-10 17:49:14 -05002414 Float4 one = As<Float4>(Int4(oneBits));
Nicolas Capensc3801992022-01-02 23:57:19 -05002415 coord = Min(Abs(coord), one);
Nicolas Capens157ba262019-12-10 17:49:14 -05002416 }
2417 break;
Nicolas Capens112faf42019-12-13 17:32:26 -05002418 case ADDRESSING_BORDER:
2419 // Don't map to a valid range here.
2420 break;
2421 default: // Wrap
2422 coord = Frac(coord);
2423 break;
Nicolas Capens68a82382018-10-02 13:16:55 -04002424 }
2425 }
Nicolas Capens157ba262019-12-10 17:49:14 -05002426
2427 coord = coord * Float4(dim);
Nicolas Capens68a82382018-10-02 13:16:55 -04002428 }
Nicolas Capens68a82382018-10-02 13:16:55 -04002429
Nicolas Capens157ba262019-12-10 17:49:14 -05002430 if(state.textureFilter == FILTER_POINT)
Nicolas Capens57253152019-05-10 20:25:17 -07002431 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04002432 if(addressingMode == ADDRESSING_BORDER || function.offset)
Nicolas Capens157ba262019-12-10 17:49:14 -05002433 {
2434 xyz0 = Int4(Floor(coord));
2435 }
2436 else // Can't have negative coordinates, so floor() is redundant when casting to int.
2437 {
2438 xyz0 = Int4(coord);
2439 }
2440 }
2441 else
2442 {
2443 if(state.textureFilter == FILTER_MIN_POINT_MAG_LINEAR ||
2444 state.textureFilter == FILTER_MIN_LINEAR_MAG_POINT)
2445 {
2446 coord -= As<Float4>(As<Int4>(Float4(0.5f)) & filter);
2447 }
2448 else
2449 {
2450 coord -= Float4(0.5f);
2451 }
2452
2453 Float4 floor = Floor(coord);
2454 xyz0 = Int4(floor);
2455 f = coord - floor;
2456 }
2457
Nicolas Capens2e40a572020-07-29 14:18:55 -04002458 if(function.offset)
Nicolas Capens157ba262019-12-10 17:49:14 -05002459 {
Nicolas Capens2e40a572020-07-29 14:18:55 -04002460 xyz0 += offset;
Nicolas Capens157ba262019-12-10 17:49:14 -05002461 }
2462
2463 if(addressingMode == ADDRESSING_SEAMLESS) // Adjust for border.
2464 {
2465 xyz0 += Int4(1);
2466 }
2467
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002468 xyz1 = xyz0 - filter; // Increment
Nicolas Capens157ba262019-12-10 17:49:14 -05002469
2470 if(addressingMode == ADDRESSING_BORDER)
2471 {
2472 // Replace the coordinates with -1 if they're out of range.
2473 Int4 border0 = CmpLT(xyz0, Int4(0)) | CmpNLT(xyz0, dim);
2474 Int4 border1 = CmpLT(xyz1, Int4(0)) | CmpNLT(xyz1, dim);
2475 xyz0 |= border0;
2476 xyz1 |= border1;
2477 }
Nicolas Capens2e40a572020-07-29 14:18:55 -04002478 else if(function.offset)
Nicolas Capens157ba262019-12-10 17:49:14 -05002479 {
2480 switch(addressingMode)
2481 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002482 case ADDRESSING_SEAMLESS:
2483 UNREACHABLE("addressingMode %d", int(addressingMode)); // Cube sampling doesn't support offset.
2484 case ADDRESSING_MIRROR:
2485 case ADDRESSING_MIRRORONCE:
Nicolas Capens9c273852021-12-15 13:44:47 -05002486 // TODO(b/29069044): Implement ADDRESSING_MIRROR and ADDRESSING_MIRRORONCE.
Nicolas Capens112faf42019-12-13 17:32:26 -05002487 // Fall through to Clamp.
2488 case ADDRESSING_CLAMP:
2489 xyz0 = Min(Max(xyz0, Int4(0)), maxXYZ);
2490 xyz1 = Min(Max(xyz1, Int4(0)), maxXYZ);
2491 break;
2492 default: // Wrap
2493 xyz0 = mod(xyz0, dim);
2494 xyz1 = mod(xyz1, dim);
2495 break;
Nicolas Capens157ba262019-12-10 17:49:14 -05002496 }
2497 }
2498 else if(state.textureFilter != FILTER_POINT)
2499 {
2500 switch(addressingMode)
2501 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002502 case ADDRESSING_SEAMLESS:
2503 break;
2504 case ADDRESSING_MIRROR:
2505 case ADDRESSING_MIRRORONCE:
2506 case ADDRESSING_CLAMP:
2507 xyz0 = Max(xyz0, Int4(0));
2508 xyz1 = Min(xyz1, maxXYZ);
2509 break;
2510 default: // Wrap
Nicolas Capens157ba262019-12-10 17:49:14 -05002511 {
2512 Int4 under = CmpLT(xyz0, Int4(0));
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002513 xyz0 = (under & maxXYZ) | (~under & xyz0); // xyz < 0 ? dim - 1 : xyz // TODO: IfThenElse()
Nicolas Capens157ba262019-12-10 17:49:14 -05002514
2515 Int4 nover = CmpLT(xyz1, dim);
Ben Claytonbc1c067be2019-12-17 20:37:37 +00002516 xyz1 = nover & xyz1; // xyz >= dim ? 0 : xyz
Nicolas Capens157ba262019-12-10 17:49:14 -05002517 }
2518 break;
2519 }
Nicolas Capens57253152019-05-10 20:25:17 -07002520 }
2521 }
Nicolas Capens68a82382018-10-02 13:16:55 -04002522}
Nicolas Capens157ba262019-12-10 17:49:14 -05002523
Nicolas Capens9c273852021-12-15 13:44:47 -05002524Int4 SamplerCore::computeLayerIndex(const Float4 &a, Pointer<Byte> &mipmap)
Nicolas Capens30873702020-08-01 09:17:23 -04002525{
2526 if(!state.isArrayed())
2527 {
2528 return {};
2529 }
2530
2531 Int4 layers = *Pointer<Int4>(mipmap + OFFSET(Mipmap, depth), 16);
2532 Int4 maxLayer = layers - Int4(1);
2533
2534 if(function == Fetch) // Unnormalized coordinates
2535 {
2536 Int4 xyz = As<Int4>(a);
2537 Int4 xyz0 = Min(Max(xyz, Int4(0)), maxLayer);
2538
2539 // VK_EXT_image_robustness requires checking for out-of-bounds accesses.
2540 // TODO(b/162327166): Only perform bounds checks when VK_EXT_image_robustness is enabled.
2541 // If the above clamping altered the result, the access is out-of-bounds.
2542 // In that case set the coordinate to -1 to perform texel replacement later.
2543 Int4 outOfBounds = CmpNEQ(xyz, xyz0);
2544 xyz0 |= outOfBounds;
2545
2546 return xyz0;
2547 }
2548 else
2549 {
2550 return Min(Max(RoundInt(a), Int4(0)), maxLayer);
2551 }
2552}
2553
Nicolas Capens2883de92020-01-27 14:58:14 -05002554void SamplerCore::sRGBtoLinearFF00(Short4 &c)
Nicolas Capens157ba262019-12-10 17:49:14 -05002555{
2556 c = As<UShort4>(c) >> 8;
2557
Nicolas Capens2883de92020-01-27 14:58:14 -05002558 Pointer<Byte> LUT = Pointer<Byte>(constants + OFFSET(Constants, sRGBtoLinearFF_FF00));
Nicolas Capens157ba262019-12-10 17:49:14 -05002559
2560 c = Insert(c, *Pointer<Short>(LUT + 2 * Int(Extract(c, 0))), 0);
2561 c = Insert(c, *Pointer<Short>(LUT + 2 * Int(Extract(c, 1))), 1);
2562 c = Insert(c, *Pointer<Short>(LUT + 2 * Int(Extract(c, 2))), 2);
2563 c = Insert(c, *Pointer<Short>(LUT + 2 * Int(Extract(c, 3))), 3);
2564}
2565
Nicolas Capens592d4132021-12-09 19:37:53 -05002566bool SamplerCore::hasNormalizedFormat() const
2567{
2568 return state.textureFormat.isSignedNormalized() || state.textureFormat.isUnsignedNormalized();
2569}
2570
Nicolas Capens157ba262019-12-10 17:49:14 -05002571bool SamplerCore::hasFloatTexture() const
2572{
2573 return state.textureFormat.isFloatFormat();
2574}
2575
2576bool SamplerCore::hasUnnormalizedIntegerTexture() const
2577{
Nicolas Capens9d9f30d2020-01-12 03:26:18 -05002578 return state.textureFormat.isUnnormalizedInteger();
Nicolas Capens157ba262019-12-10 17:49:14 -05002579}
2580
2581bool SamplerCore::hasUnsignedTextureComponent(int component) const
2582{
2583 return state.textureFormat.isUnsignedComponent(component);
2584}
2585
2586int SamplerCore::textureComponentCount() const
2587{
2588 return state.textureFormat.componentCount();
2589}
2590
Sean Risserc9625f12020-03-20 11:12:00 -04002591bool SamplerCore::has16bitPackedTextureFormat() const
Nicolas Capens157ba262019-12-10 17:49:14 -05002592{
Sean Risserc9625f12020-03-20 11:12:00 -04002593 return state.textureFormat.has16bitPackedTextureFormat();
Nicolas Capens157ba262019-12-10 17:49:14 -05002594}
2595
2596bool SamplerCore::has8bitTextureComponents() const
2597{
2598 return state.textureFormat.has8bitTextureComponents();
2599}
2600
2601bool SamplerCore::has16bitTextureComponents() const
2602{
2603 return state.textureFormat.has16bitTextureComponents();
2604}
2605
2606bool SamplerCore::has32bitIntegerTextureComponents() const
2607{
2608 return state.textureFormat.has32bitIntegerTextureComponents();
2609}
2610
2611bool SamplerCore::isYcbcrFormat() const
2612{
2613 return state.textureFormat.isYcbcrFormat();
2614}
2615
2616bool SamplerCore::isRGBComponent(int component) const
2617{
2618 return state.textureFormat.isRGBComponent(component);
2619}
2620
2621bool SamplerCore::borderModeActive() const
2622{
2623 return state.addressingModeU == ADDRESSING_BORDER ||
2624 state.addressingModeV == ADDRESSING_BORDER ||
2625 state.addressingModeW == ADDRESSING_BORDER;
2626}
2627
Nicolas Capens157ba262019-12-10 17:49:14 -05002628VkComponentSwizzle SamplerCore::gatherSwizzle() const
2629{
2630 switch(state.gatherComponent)
2631 {
Nicolas Capens112faf42019-12-13 17:32:26 -05002632 case 0: return state.swizzle.r;
2633 case 1: return state.swizzle.g;
2634 case 2: return state.swizzle.b;
2635 case 3: return state.swizzle.a;
2636 default:
2637 UNREACHABLE("Invalid component");
2638 return VK_COMPONENT_SWIZZLE_R;
Nicolas Capens157ba262019-12-10 17:49:14 -05002639 }
2640}
2641
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002642sw::float4 SamplerCore::getComponentScale() const
2643{
Nicolas Capens03bb9e12021-11-29 16:54:44 -05002644 // TODO(b/204709464): Unlike other formats, the fixed-point representation of the formats below are handled with bit extension.
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002645 // This special handling of such formats should be removed later.
Nicolas Capens03bb9e12021-11-29 16:54:44 -05002646 switch(state.textureFormat)
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002647 {
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002648 case VK_FORMAT_G8_B8_R8_3PLANE_420_UNORM:
2649 case VK_FORMAT_G8_B8R8_2PLANE_420_UNORM:
Jason Macnak0587e072022-02-11 16:49:02 -08002650 case VK_FORMAT_G10X6_B10X6R10X6_2PLANE_420_UNORM_3PACK16:
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002651 return sw::float4(0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF);
2652 default:
2653 break;
2654 };
2655
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002656 const sw::int4 bits = state.textureFormat.bitsPerComponent();
Nicolas Capensec4bdcf2021-12-02 16:50:32 -05002657 const sw::int4 shift = sw::int4(16 - bits.x, 16 - bits.y, 16 - bits.z, 16 - bits.w);
2658 const uint16_t sign = state.textureFormat.isUnsigned() ? 0xFFFF : 0x7FFF;
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002659
Nicolas Capensec4bdcf2021-12-02 16:50:32 -05002660 return sw::float4(static_cast<uint16_t>(0xFFFF << shift.x) & sign,
2661 static_cast<uint16_t>(0xFFFF << shift.y) & sign,
2662 static_cast<uint16_t>(0xFFFF << shift.z) & sign,
2663 static_cast<uint16_t>(0xFFFF << shift.w) & sign);
Ari Suonpääcbec1d42021-11-23 08:04:50 +02002664}
2665
2666int SamplerCore::getGatherComponent() const
2667{
2668 VkComponentSwizzle swizzle = gatherSwizzle();
2669
2670 switch(swizzle)
2671 {
2672 default: UNSUPPORTED("VkComponentSwizzle %d", (int)swizzle); return 0;
2673 case VK_COMPONENT_SWIZZLE_R:
2674 case VK_COMPONENT_SWIZZLE_G:
2675 case VK_COMPONENT_SWIZZLE_B:
2676 case VK_COMPONENT_SWIZZLE_A:
2677 // Normalize all components using the gather component scale.
2678 return swizzle - VK_COMPONENT_SWIZZLE_R;
2679 case VK_COMPONENT_SWIZZLE_ZERO:
2680 case VK_COMPONENT_SWIZZLE_ONE:
2681 // These cases are handled later.
2682 return 0;
2683 }
2684
2685 return 0;
2686}
2687
Nicolas Capens157ba262019-12-10 17:49:14 -05002688} // namespace sw