1bf215546Sopenharmony_ci/**************************************************************************
2bf215546Sopenharmony_ci *
3bf215546Sopenharmony_ci * Copyright 2009 VMware, Inc.
4bf215546Sopenharmony_ci * All Rights Reserved.
5bf215546Sopenharmony_ci *
6bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
8bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
9bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
10bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
11bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
12bf215546Sopenharmony_ci * the following conditions:
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
15bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
16bf215546Sopenharmony_ci * of the Software.
17bf215546Sopenharmony_ci *
18bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci **************************************************************************/
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/**
29bf215546Sopenharmony_ci * @file
30bf215546Sopenharmony_ci * Texture sampling -- common code.
31bf215546Sopenharmony_ci *
32bf215546Sopenharmony_ci * @author Jose Fonseca <jfonseca@vmware.com>
33bf215546Sopenharmony_ci */
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "pipe/p_defines.h"
36bf215546Sopenharmony_ci#include "pipe/p_state.h"
37bf215546Sopenharmony_ci#include "util/format/u_format.h"
38bf215546Sopenharmony_ci#include "util/u_math.h"
39bf215546Sopenharmony_ci#include "util/u_cpu_detect.h"
40bf215546Sopenharmony_ci#include "lp_bld_arit.h"
41bf215546Sopenharmony_ci#include "lp_bld_const.h"
42bf215546Sopenharmony_ci#include "lp_bld_debug.h"
43bf215546Sopenharmony_ci#include "lp_bld_printf.h"
44bf215546Sopenharmony_ci#include "lp_bld_flow.h"
45bf215546Sopenharmony_ci#include "lp_bld_sample.h"
46bf215546Sopenharmony_ci#include "lp_bld_swizzle.h"
47bf215546Sopenharmony_ci#include "lp_bld_type.h"
48bf215546Sopenharmony_ci#include "lp_bld_logic.h"
49bf215546Sopenharmony_ci#include "lp_bld_pack.h"
50bf215546Sopenharmony_ci#include "lp_bld_quad.h"
51bf215546Sopenharmony_ci#include "lp_bld_bitarit.h"
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci/*
55bf215546Sopenharmony_ci * Bri-linear factor. Should be greater than one.
56bf215546Sopenharmony_ci */
57bf215546Sopenharmony_ci#define BRILINEAR_FACTOR 2
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci/**
60bf215546Sopenharmony_ci * Does the given texture wrap mode allow sampling the texture border color?
61bf215546Sopenharmony_ci * XXX maybe move this into gallium util code.
62bf215546Sopenharmony_ci */
63bf215546Sopenharmony_ciboolean
64bf215546Sopenharmony_cilp_sampler_wrap_mode_uses_border_color(enum pipe_tex_wrap mode,
65bf215546Sopenharmony_ci                                       enum pipe_tex_filter min_img_filter,
66bf215546Sopenharmony_ci                                       enum pipe_tex_filter mag_img_filter)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci   switch (mode) {
69bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_REPEAT:
70bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
71bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_REPEAT:
72bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
73bf215546Sopenharmony_ci      return FALSE;
74bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP:
75bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP:
76bf215546Sopenharmony_ci      if (min_img_filter == PIPE_TEX_FILTER_NEAREST &&
77bf215546Sopenharmony_ci          mag_img_filter == PIPE_TEX_FILTER_NEAREST) {
78bf215546Sopenharmony_ci         return FALSE;
79bf215546Sopenharmony_ci      } else {
80bf215546Sopenharmony_ci         return TRUE;
81bf215546Sopenharmony_ci      }
82bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
83bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
84bf215546Sopenharmony_ci      return TRUE;
85bf215546Sopenharmony_ci   default:
86bf215546Sopenharmony_ci      assert(0 && "unexpected wrap mode");
87bf215546Sopenharmony_ci      return FALSE;
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci/**
93bf215546Sopenharmony_ci * Initialize lp_sampler_static_texture_state object with the gallium
94bf215546Sopenharmony_ci * texture/sampler_view state (this contains the parts which are
95bf215546Sopenharmony_ci * considered static).
96bf215546Sopenharmony_ci */
97bf215546Sopenharmony_civoid
98bf215546Sopenharmony_cilp_sampler_static_texture_state(struct lp_static_texture_state *state,
99bf215546Sopenharmony_ci                                const struct pipe_sampler_view *view)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci   memset(state, 0, sizeof *state);
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   if (!view || !view->texture)
104bf215546Sopenharmony_ci      return;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   const struct pipe_resource *texture = view->texture;
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   state->format = view->format;
109bf215546Sopenharmony_ci   state->swizzle_r = view->swizzle_r;
110bf215546Sopenharmony_ci   state->swizzle_g = view->swizzle_g;
111bf215546Sopenharmony_ci   state->swizzle_b = view->swizzle_b;
112bf215546Sopenharmony_ci   state->swizzle_a = view->swizzle_a;
113bf215546Sopenharmony_ci   assert(state->swizzle_r < PIPE_SWIZZLE_NONE);
114bf215546Sopenharmony_ci   assert(state->swizzle_g < PIPE_SWIZZLE_NONE);
115bf215546Sopenharmony_ci   assert(state->swizzle_b < PIPE_SWIZZLE_NONE);
116bf215546Sopenharmony_ci   assert(state->swizzle_a < PIPE_SWIZZLE_NONE);
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   state->target = view->target;
119bf215546Sopenharmony_ci   state->pot_width = util_is_power_of_two_or_zero(texture->width0);
120bf215546Sopenharmony_ci   state->pot_height = util_is_power_of_two_or_zero(texture->height0);
121bf215546Sopenharmony_ci   state->pot_depth = util_is_power_of_two_or_zero(texture->depth0);
122bf215546Sopenharmony_ci   state->level_zero_only = !view->u.tex.last_level;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   /*
125bf215546Sopenharmony_ci    * the layer / element / level parameters are all either dynamic
126bf215546Sopenharmony_ci    * state or handled transparently wrt execution.
127bf215546Sopenharmony_ci    */
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci/**
131bf215546Sopenharmony_ci * Initialize lp_sampler_static_texture_state object with the gallium
132bf215546Sopenharmony_ci * texture/sampler_view state (this contains the parts which are
133bf215546Sopenharmony_ci * considered static).
134bf215546Sopenharmony_ci */
135bf215546Sopenharmony_civoid
136bf215546Sopenharmony_cilp_sampler_static_texture_state_image(struct lp_static_texture_state *state,
137bf215546Sopenharmony_ci                                      const struct pipe_image_view *view)
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci   memset(state, 0, sizeof *state);
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   if (!view || !view->resource)
142bf215546Sopenharmony_ci      return;
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   const struct pipe_resource *resource = view->resource;
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci   state->format = view->format;
147bf215546Sopenharmony_ci   state->swizzle_r = PIPE_SWIZZLE_X;
148bf215546Sopenharmony_ci   state->swizzle_g = PIPE_SWIZZLE_Y;
149bf215546Sopenharmony_ci   state->swizzle_b = PIPE_SWIZZLE_Z;
150bf215546Sopenharmony_ci   state->swizzle_a = PIPE_SWIZZLE_W;
151bf215546Sopenharmony_ci   assert(state->swizzle_r < PIPE_SWIZZLE_NONE);
152bf215546Sopenharmony_ci   assert(state->swizzle_g < PIPE_SWIZZLE_NONE);
153bf215546Sopenharmony_ci   assert(state->swizzle_b < PIPE_SWIZZLE_NONE);
154bf215546Sopenharmony_ci   assert(state->swizzle_a < PIPE_SWIZZLE_NONE);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   state->target = view->resource->target;
157bf215546Sopenharmony_ci   state->pot_width = util_is_power_of_two_or_zero(resource->width0);
158bf215546Sopenharmony_ci   state->pot_height = util_is_power_of_two_or_zero(resource->height0);
159bf215546Sopenharmony_ci   state->pot_depth = util_is_power_of_two_or_zero(resource->depth0);
160bf215546Sopenharmony_ci   state->level_zero_only = 0;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   /*
163bf215546Sopenharmony_ci    * the layer / element / level parameters are all either dynamic
164bf215546Sopenharmony_ci    * state or handled transparently wrt execution.
165bf215546Sopenharmony_ci    */
166bf215546Sopenharmony_ci}
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci/**
169bf215546Sopenharmony_ci * Initialize lp_sampler_static_sampler_state object with the gallium sampler
170bf215546Sopenharmony_ci * state (this contains the parts which are considered static).
171bf215546Sopenharmony_ci */
172bf215546Sopenharmony_civoid
173bf215546Sopenharmony_cilp_sampler_static_sampler_state(struct lp_static_sampler_state *state,
174bf215546Sopenharmony_ci                                const struct pipe_sampler_state *sampler)
175bf215546Sopenharmony_ci{
176bf215546Sopenharmony_ci   memset(state, 0, sizeof *state);
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci   if (!sampler)
179bf215546Sopenharmony_ci      return;
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   /*
182bf215546Sopenharmony_ci    * We don't copy sampler state over unless it is actually enabled, to avoid
183bf215546Sopenharmony_ci    * spurious recompiles, as the sampler static state is part of the shader
184bf215546Sopenharmony_ci    * key.
185bf215546Sopenharmony_ci    *
186bf215546Sopenharmony_ci    * Ideally gallium frontends or cso_cache module would make all state
187bf215546Sopenharmony_ci    * canonical, but until that happens it's better to be safe than sorry here.
188bf215546Sopenharmony_ci    *
189bf215546Sopenharmony_ci    * XXX: Actually there's much more than can be done here, especially
190bf215546Sopenharmony_ci    * regarding 1D/2D/3D/CUBE textures, wrap modes, etc.
191bf215546Sopenharmony_ci    */
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci   state->wrap_s            = sampler->wrap_s;
194bf215546Sopenharmony_ci   state->wrap_t            = sampler->wrap_t;
195bf215546Sopenharmony_ci   state->wrap_r            = sampler->wrap_r;
196bf215546Sopenharmony_ci   state->min_img_filter    = sampler->min_img_filter;
197bf215546Sopenharmony_ci   state->mag_img_filter    = sampler->mag_img_filter;
198bf215546Sopenharmony_ci   state->min_mip_filter    = sampler->min_mip_filter;
199bf215546Sopenharmony_ci   state->seamless_cube_map = sampler->seamless_cube_map;
200bf215546Sopenharmony_ci   state->reduction_mode    = sampler->reduction_mode;
201bf215546Sopenharmony_ci   state->aniso = sampler->max_anisotropy > 1.0f;
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci   if (sampler->max_lod > 0.0f) {
204bf215546Sopenharmony_ci      state->max_lod_pos = 1;
205bf215546Sopenharmony_ci   }
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci   if (sampler->lod_bias != 0.0f) {
208bf215546Sopenharmony_ci      state->lod_bias_non_zero = 1;
209bf215546Sopenharmony_ci   }
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   if (state->min_mip_filter != PIPE_TEX_MIPFILTER_NONE ||
212bf215546Sopenharmony_ci       state->min_img_filter != state->mag_img_filter) {
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci      /* If min_lod == max_lod we can greatly simplify mipmap selection.
215bf215546Sopenharmony_ci       * This is a case that occurs during automatic mipmap generation.
216bf215546Sopenharmony_ci       */
217bf215546Sopenharmony_ci      if (sampler->min_lod == sampler->max_lod) {
218bf215546Sopenharmony_ci         state->min_max_lod_equal = 1;
219bf215546Sopenharmony_ci      } else {
220bf215546Sopenharmony_ci         if (sampler->min_lod > 0.0f) {
221bf215546Sopenharmony_ci            state->apply_min_lod = 1;
222bf215546Sopenharmony_ci         }
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci         /*
225bf215546Sopenharmony_ci          * XXX this won't do anything with the mesa state tracker which always
226bf215546Sopenharmony_ci          * sets max_lod to not more than actually present mip maps...
227bf215546Sopenharmony_ci          */
228bf215546Sopenharmony_ci         if (sampler->max_lod < (PIPE_MAX_TEXTURE_LEVELS - 1)) {
229bf215546Sopenharmony_ci            state->apply_max_lod = 1;
230bf215546Sopenharmony_ci         }
231bf215546Sopenharmony_ci      }
232bf215546Sopenharmony_ci   }
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   state->compare_mode      = sampler->compare_mode;
235bf215546Sopenharmony_ci   if (sampler->compare_mode != PIPE_TEX_COMPARE_NONE) {
236bf215546Sopenharmony_ci      state->compare_func   = sampler->compare_func;
237bf215546Sopenharmony_ci   }
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   state->normalized_coords = sampler->normalized_coords;
240bf215546Sopenharmony_ci}
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci/* build aniso pmin value */
243bf215546Sopenharmony_cistatic LLVMValueRef
244bf215546Sopenharmony_cilp_build_pmin(struct lp_build_sample_context *bld,
245bf215546Sopenharmony_ci              unsigned texture_unit,
246bf215546Sopenharmony_ci              LLVMValueRef s,
247bf215546Sopenharmony_ci              LLVMValueRef t,
248bf215546Sopenharmony_ci              LLVMValueRef max_aniso)
249bf215546Sopenharmony_ci{
250bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld->gallivm;
251bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
252bf215546Sopenharmony_ci   struct lp_build_context *coord_bld = &bld->coord_bld;
253bf215546Sopenharmony_ci   struct lp_build_context *int_size_bld = &bld->int_size_in_bld;
254bf215546Sopenharmony_ci   struct lp_build_context *float_size_bld = &bld->float_size_in_bld;
255bf215546Sopenharmony_ci   struct lp_build_context *pmin_bld = &bld->lodf_bld;
256bf215546Sopenharmony_ci   LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
257bf215546Sopenharmony_ci   LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
258bf215546Sopenharmony_ci   LLVMValueRef index1 = LLVMConstInt(i32t, 1, 0);
259bf215546Sopenharmony_ci   LLVMValueRef ddx_ddy = lp_build_packed_ddx_ddy_twocoord(coord_bld, s, t);
260bf215546Sopenharmony_ci   LLVMValueRef int_size, float_size;
261bf215546Sopenharmony_ci   LLVMValueRef first_level, first_level_vec;
262bf215546Sopenharmony_ci   unsigned length = coord_bld->type.length;
263bf215546Sopenharmony_ci   unsigned num_quads = length / 4;
264bf215546Sopenharmony_ci   boolean pmin_per_quad = pmin_bld->type.length != length;
265bf215546Sopenharmony_ci   unsigned i;
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci   first_level = bld->dynamic_state->first_level(bld->dynamic_state, bld->gallivm,
268bf215546Sopenharmony_ci                                                 bld->context_ptr, texture_unit, NULL);
269bf215546Sopenharmony_ci   first_level_vec = lp_build_broadcast_scalar(int_size_bld, first_level);
270bf215546Sopenharmony_ci   int_size = lp_build_minify(int_size_bld, bld->int_size, first_level_vec, TRUE);
271bf215546Sopenharmony_ci   float_size = lp_build_int_to_float(float_size_bld, int_size);
272bf215546Sopenharmony_ci   max_aniso = lp_build_broadcast_scalar(coord_bld, max_aniso);
273bf215546Sopenharmony_ci   max_aniso = lp_build_mul(coord_bld, max_aniso, max_aniso);
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci   static const unsigned char swizzle01[] = { /* no-op swizzle */
276bf215546Sopenharmony_ci      0, 1,
277bf215546Sopenharmony_ci      LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
278bf215546Sopenharmony_ci   };
279bf215546Sopenharmony_ci   static const unsigned char swizzle23[] = {
280bf215546Sopenharmony_ci      2, 3,
281bf215546Sopenharmony_ci      LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
282bf215546Sopenharmony_ci   };
283bf215546Sopenharmony_ci   LLVMValueRef ddx_ddys, ddx_ddyt, floatdim, shuffles[LP_MAX_VECTOR_LENGTH / 4];
284bf215546Sopenharmony_ci
285bf215546Sopenharmony_ci   for (i = 0; i < num_quads; i++) {
286bf215546Sopenharmony_ci      shuffles[i*4+0] = shuffles[i*4+1] = index0;
287bf215546Sopenharmony_ci      shuffles[i*4+2] = shuffles[i*4+3] = index1;
288bf215546Sopenharmony_ci   }
289bf215546Sopenharmony_ci   floatdim = LLVMBuildShuffleVector(builder, float_size, float_size,
290bf215546Sopenharmony_ci                                     LLVMConstVector(shuffles, length), "");
291bf215546Sopenharmony_ci   ddx_ddy = lp_build_mul(coord_bld, ddx_ddy, floatdim);
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci   ddx_ddy = lp_build_mul(coord_bld, ddx_ddy, ddx_ddy);
294bf215546Sopenharmony_ci
295bf215546Sopenharmony_ci   ddx_ddys = lp_build_swizzle_aos(coord_bld, ddx_ddy, swizzle01);
296bf215546Sopenharmony_ci   ddx_ddyt = lp_build_swizzle_aos(coord_bld, ddx_ddy, swizzle23);
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci   LLVMValueRef px2_py2 = lp_build_add(coord_bld, ddx_ddys, ddx_ddyt);
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci   static const unsigned char swizzle0[] = { /* no-op swizzle */
301bf215546Sopenharmony_ci     0, LP_BLD_SWIZZLE_DONTCARE,
302bf215546Sopenharmony_ci     LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
303bf215546Sopenharmony_ci   };
304bf215546Sopenharmony_ci   static const unsigned char swizzle1[] = {
305bf215546Sopenharmony_ci     1, LP_BLD_SWIZZLE_DONTCARE,
306bf215546Sopenharmony_ci     LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
307bf215546Sopenharmony_ci   };
308bf215546Sopenharmony_ci   LLVMValueRef px2 = lp_build_swizzle_aos(coord_bld, px2_py2, swizzle0);
309bf215546Sopenharmony_ci   LLVMValueRef py2 = lp_build_swizzle_aos(coord_bld, px2_py2, swizzle1);
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   LLVMValueRef pmax2 = lp_build_max(coord_bld, px2, py2);
312bf215546Sopenharmony_ci   LLVMValueRef pmin2 = lp_build_min(coord_bld, px2, py2);
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci   LLVMValueRef temp = lp_build_mul(coord_bld, pmin2, max_aniso);
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci   LLVMValueRef comp = lp_build_compare(gallivm, coord_bld->type, PIPE_FUNC_GREATER,
317bf215546Sopenharmony_ci                                        pmin2, temp);
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci   LLVMValueRef pmin2_alt = lp_build_div(coord_bld, pmax2, max_aniso);
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci   pmin2 = lp_build_select(coord_bld, comp, pmin2_alt, pmin2);
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci   if (pmin_per_quad)
324bf215546Sopenharmony_ci      pmin2 = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
325bf215546Sopenharmony_ci                                        pmin_bld->type, pmin2, 0);
326bf215546Sopenharmony_ci   else
327bf215546Sopenharmony_ci      pmin2 = lp_build_swizzle_scalar_aos(pmin_bld, pmin2, 0, 4);
328bf215546Sopenharmony_ci   return pmin2;
329bf215546Sopenharmony_ci}
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci/**
332bf215546Sopenharmony_ci * Generate code to compute coordinate gradient (rho).
333bf215546Sopenharmony_ci * \param derivs  partial derivatives of (s, t, r, q) with respect to X and Y
334bf215546Sopenharmony_ci *
335bf215546Sopenharmony_ci * The resulting rho has bld->levelf format (per quad or per element).
336bf215546Sopenharmony_ci */
337bf215546Sopenharmony_cistatic LLVMValueRef
338bf215546Sopenharmony_cilp_build_rho(struct lp_build_sample_context *bld,
339bf215546Sopenharmony_ci             unsigned texture_unit,
340bf215546Sopenharmony_ci             LLVMValueRef s,
341bf215546Sopenharmony_ci             LLVMValueRef t,
342bf215546Sopenharmony_ci             LLVMValueRef r,
343bf215546Sopenharmony_ci             LLVMValueRef cube_rho,
344bf215546Sopenharmony_ci             const struct lp_derivatives *derivs)
345bf215546Sopenharmony_ci{
346bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld->gallivm;
347bf215546Sopenharmony_ci   struct lp_build_context *int_size_bld = &bld->int_size_in_bld;
348bf215546Sopenharmony_ci   struct lp_build_context *float_size_bld = &bld->float_size_in_bld;
349bf215546Sopenharmony_ci   struct lp_build_context *float_bld = &bld->float_bld;
350bf215546Sopenharmony_ci   struct lp_build_context *coord_bld = &bld->coord_bld;
351bf215546Sopenharmony_ci   struct lp_build_context *rho_bld = &bld->lodf_bld;
352bf215546Sopenharmony_ci   const unsigned dims = bld->dims;
353bf215546Sopenharmony_ci   LLVMValueRef ddx_ddy[2] = {NULL};
354bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
355bf215546Sopenharmony_ci   LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
356bf215546Sopenharmony_ci   LLVMValueRef index0 = LLVMConstInt(i32t, 0, 0);
357bf215546Sopenharmony_ci   LLVMValueRef index1 = LLVMConstInt(i32t, 1, 0);
358bf215546Sopenharmony_ci   LLVMValueRef index2 = LLVMConstInt(i32t, 2, 0);
359bf215546Sopenharmony_ci   LLVMValueRef rho_vec;
360bf215546Sopenharmony_ci   LLVMValueRef int_size, float_size;
361bf215546Sopenharmony_ci   LLVMValueRef rho;
362bf215546Sopenharmony_ci   LLVMValueRef first_level, first_level_vec;
363bf215546Sopenharmony_ci   unsigned length = coord_bld->type.length;
364bf215546Sopenharmony_ci   unsigned num_quads = length / 4;
365bf215546Sopenharmony_ci   boolean rho_per_quad = rho_bld->type.length != length;
366bf215546Sopenharmony_ci   boolean no_rho_opt = bld->no_rho_approx && (dims > 1);
367bf215546Sopenharmony_ci   unsigned i;
368bf215546Sopenharmony_ci   LLVMValueRef i32undef = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
369bf215546Sopenharmony_ci   LLVMValueRef rho_xvec, rho_yvec;
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   /* Note that all simplified calculations will only work for isotropic filtering */
372bf215546Sopenharmony_ci
373bf215546Sopenharmony_ci   /*
374bf215546Sopenharmony_ci    * rho calcs are always per quad except for explicit derivs (excluding
375bf215546Sopenharmony_ci    * the messy cube maps for now) when requested.
376bf215546Sopenharmony_ci    */
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci   first_level = bld->dynamic_state->first_level(bld->dynamic_state, bld->gallivm,
379bf215546Sopenharmony_ci                                                 bld->context_ptr, texture_unit, NULL);
380bf215546Sopenharmony_ci   first_level_vec = lp_build_broadcast_scalar(int_size_bld, first_level);
381bf215546Sopenharmony_ci   int_size = lp_build_minify(int_size_bld, bld->int_size, first_level_vec, TRUE);
382bf215546Sopenharmony_ci   float_size = lp_build_int_to_float(float_size_bld, int_size);
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci   if (cube_rho) {
385bf215546Sopenharmony_ci      LLVMValueRef cubesize;
386bf215546Sopenharmony_ci      LLVMValueRef index0 = lp_build_const_int32(gallivm, 0);
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci      /*
389bf215546Sopenharmony_ci       * Cube map code did already everything except size mul and per-quad extraction.
390bf215546Sopenharmony_ci       * Luckily cube maps are always quadratic!
391bf215546Sopenharmony_ci       */
392bf215546Sopenharmony_ci      if (rho_per_quad) {
393bf215546Sopenharmony_ci         rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
394bf215546Sopenharmony_ci                                         rho_bld->type, cube_rho, 0);
395bf215546Sopenharmony_ci      }
396bf215546Sopenharmony_ci      else {
397bf215546Sopenharmony_ci         rho = lp_build_swizzle_scalar_aos(coord_bld, cube_rho, 0, 4);
398bf215546Sopenharmony_ci      }
399bf215546Sopenharmony_ci      /* Could optimize this for single quad just skip the broadcast */
400bf215546Sopenharmony_ci      cubesize = lp_build_extract_broadcast(gallivm, bld->float_size_in_type,
401bf215546Sopenharmony_ci                                            rho_bld->type, float_size, index0);
402bf215546Sopenharmony_ci      /* skipping sqrt hence returning rho squared */
403bf215546Sopenharmony_ci      cubesize = lp_build_mul(rho_bld, cubesize, cubesize);
404bf215546Sopenharmony_ci      rho = lp_build_mul(rho_bld, cubesize, rho);
405bf215546Sopenharmony_ci   }
406bf215546Sopenharmony_ci   else if (derivs) {
407bf215546Sopenharmony_ci      LLVMValueRef ddmax[3] = { NULL }, ddx[3] = { NULL }, ddy[3] = { NULL };
408bf215546Sopenharmony_ci      for (i = 0; i < dims; i++) {
409bf215546Sopenharmony_ci         LLVMValueRef floatdim;
410bf215546Sopenharmony_ci         LLVMValueRef indexi = lp_build_const_int32(gallivm, i);
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci         floatdim = lp_build_extract_broadcast(gallivm, bld->float_size_in_type,
413bf215546Sopenharmony_ci                                               coord_bld->type, float_size, indexi);
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci         /*
416bf215546Sopenharmony_ci          * note that for rho_per_quad case could reduce math (at some shuffle
417bf215546Sopenharmony_ci          * cost), but for now use same code to per-pixel lod case.
418bf215546Sopenharmony_ci          */
419bf215546Sopenharmony_ci         if (no_rho_opt) {
420bf215546Sopenharmony_ci            ddx[i] = lp_build_mul(coord_bld, floatdim, derivs->ddx[i]);
421bf215546Sopenharmony_ci            ddy[i] = lp_build_mul(coord_bld, floatdim, derivs->ddy[i]);
422bf215546Sopenharmony_ci            ddx[i] = lp_build_mul(coord_bld, ddx[i], ddx[i]);
423bf215546Sopenharmony_ci            ddy[i] = lp_build_mul(coord_bld, ddy[i], ddy[i]);
424bf215546Sopenharmony_ci         }
425bf215546Sopenharmony_ci         else {
426bf215546Sopenharmony_ci            LLVMValueRef tmpx, tmpy;
427bf215546Sopenharmony_ci            tmpx = lp_build_abs(coord_bld, derivs->ddx[i]);
428bf215546Sopenharmony_ci            tmpy = lp_build_abs(coord_bld, derivs->ddy[i]);
429bf215546Sopenharmony_ci            ddmax[i] = lp_build_max(coord_bld, tmpx, tmpy);
430bf215546Sopenharmony_ci            ddmax[i] = lp_build_mul(coord_bld, floatdim, ddmax[i]);
431bf215546Sopenharmony_ci         }
432bf215546Sopenharmony_ci      }
433bf215546Sopenharmony_ci      if (no_rho_opt) {
434bf215546Sopenharmony_ci         rho_xvec = lp_build_add(coord_bld, ddx[0], ddx[1]);
435bf215546Sopenharmony_ci         rho_yvec = lp_build_add(coord_bld, ddy[0], ddy[1]);
436bf215546Sopenharmony_ci         if (dims > 2) {
437bf215546Sopenharmony_ci            rho_xvec = lp_build_add(coord_bld, rho_xvec, ddx[2]);
438bf215546Sopenharmony_ci            rho_yvec = lp_build_add(coord_bld, rho_yvec, ddy[2]);
439bf215546Sopenharmony_ci         }
440bf215546Sopenharmony_ci         rho = lp_build_max(coord_bld, rho_xvec, rho_yvec);
441bf215546Sopenharmony_ci         /* skipping sqrt hence returning rho squared */
442bf215546Sopenharmony_ci     }
443bf215546Sopenharmony_ci      else {
444bf215546Sopenharmony_ci         rho = ddmax[0];
445bf215546Sopenharmony_ci         if (dims > 1) {
446bf215546Sopenharmony_ci            rho = lp_build_max(coord_bld, rho, ddmax[1]);
447bf215546Sopenharmony_ci            if (dims > 2) {
448bf215546Sopenharmony_ci               rho = lp_build_max(coord_bld, rho, ddmax[2]);
449bf215546Sopenharmony_ci            }
450bf215546Sopenharmony_ci         }
451bf215546Sopenharmony_ci      }
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci      LLVMValueRef rho_is_inf = lp_build_is_inf_or_nan(gallivm, coord_bld->type, rho);
454bf215546Sopenharmony_ci      rho = lp_build_select(coord_bld, rho_is_inf, coord_bld->zero, rho);
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci      if (rho_per_quad) {
457bf215546Sopenharmony_ci         /*
458bf215546Sopenharmony_ci          * rho_vec contains per-pixel rho, convert to scalar per quad.
459bf215546Sopenharmony_ci          */
460bf215546Sopenharmony_ci         rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
461bf215546Sopenharmony_ci                                         rho_bld->type, rho, 0);
462bf215546Sopenharmony_ci      }
463bf215546Sopenharmony_ci   }
464bf215546Sopenharmony_ci   else {
465bf215546Sopenharmony_ci      /*
466bf215546Sopenharmony_ci       * This looks all a bit complex, but it's not that bad
467bf215546Sopenharmony_ci       * (the shuffle code makes it look worse than it is).
468bf215546Sopenharmony_ci       * Still, might not be ideal for all cases.
469bf215546Sopenharmony_ci       */
470bf215546Sopenharmony_ci      static const unsigned char swizzle0[] = { /* no-op swizzle */
471bf215546Sopenharmony_ci         0, LP_BLD_SWIZZLE_DONTCARE,
472bf215546Sopenharmony_ci         LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
473bf215546Sopenharmony_ci      };
474bf215546Sopenharmony_ci      static const unsigned char swizzle1[] = {
475bf215546Sopenharmony_ci         1, LP_BLD_SWIZZLE_DONTCARE,
476bf215546Sopenharmony_ci         LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
477bf215546Sopenharmony_ci      };
478bf215546Sopenharmony_ci      static const unsigned char swizzle2[] = {
479bf215546Sopenharmony_ci         2, LP_BLD_SWIZZLE_DONTCARE,
480bf215546Sopenharmony_ci         LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
481bf215546Sopenharmony_ci      };
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_ci      if (dims < 2) {
484bf215546Sopenharmony_ci         ddx_ddy[0] = lp_build_packed_ddx_ddy_onecoord(coord_bld, s);
485bf215546Sopenharmony_ci      }
486bf215546Sopenharmony_ci      else if (dims >= 2) {
487bf215546Sopenharmony_ci         ddx_ddy[0] = lp_build_packed_ddx_ddy_twocoord(coord_bld, s, t);
488bf215546Sopenharmony_ci         if (dims > 2) {
489bf215546Sopenharmony_ci            ddx_ddy[1] = lp_build_packed_ddx_ddy_onecoord(coord_bld, r);
490bf215546Sopenharmony_ci         }
491bf215546Sopenharmony_ci      }
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci      if (no_rho_opt) {
494bf215546Sopenharmony_ci         static const unsigned char swizzle01[] = { /* no-op swizzle */
495bf215546Sopenharmony_ci            0, 1,
496bf215546Sopenharmony_ci            LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
497bf215546Sopenharmony_ci         };
498bf215546Sopenharmony_ci         static const unsigned char swizzle23[] = {
499bf215546Sopenharmony_ci            2, 3,
500bf215546Sopenharmony_ci            LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
501bf215546Sopenharmony_ci         };
502bf215546Sopenharmony_ci         LLVMValueRef ddx_ddys, ddx_ddyt, floatdim, shuffles[LP_MAX_VECTOR_LENGTH / 4];
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci         for (i = 0; i < num_quads; i++) {
505bf215546Sopenharmony_ci            shuffles[i*4+0] = shuffles[i*4+1] = index0;
506bf215546Sopenharmony_ci            shuffles[i*4+2] = shuffles[i*4+3] = index1;
507bf215546Sopenharmony_ci         }
508bf215546Sopenharmony_ci         floatdim = LLVMBuildShuffleVector(builder, float_size, float_size,
509bf215546Sopenharmony_ci                                           LLVMConstVector(shuffles, length), "");
510bf215546Sopenharmony_ci         ddx_ddy[0] = lp_build_mul(coord_bld, ddx_ddy[0], floatdim);
511bf215546Sopenharmony_ci         ddx_ddy[0] = lp_build_mul(coord_bld, ddx_ddy[0], ddx_ddy[0]);
512bf215546Sopenharmony_ci         ddx_ddys = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle01);
513bf215546Sopenharmony_ci         ddx_ddyt = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle23);
514bf215546Sopenharmony_ci         rho_vec = lp_build_add(coord_bld, ddx_ddys, ddx_ddyt);
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci         if (dims > 2) {
517bf215546Sopenharmony_ci            static const unsigned char swizzle02[] = {
518bf215546Sopenharmony_ci               0, 2,
519bf215546Sopenharmony_ci               LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
520bf215546Sopenharmony_ci            };
521bf215546Sopenharmony_ci            floatdim = lp_build_extract_broadcast(gallivm, bld->float_size_in_type,
522bf215546Sopenharmony_ci                                                  coord_bld->type, float_size, index2);
523bf215546Sopenharmony_ci            ddx_ddy[1] = lp_build_mul(coord_bld, ddx_ddy[1], floatdim);
524bf215546Sopenharmony_ci            ddx_ddy[1] = lp_build_mul(coord_bld, ddx_ddy[1], ddx_ddy[1]);
525bf215546Sopenharmony_ci            ddx_ddy[1] = lp_build_swizzle_aos(coord_bld, ddx_ddy[1], swizzle02);
526bf215546Sopenharmony_ci            rho_vec = lp_build_add(coord_bld, rho_vec, ddx_ddy[1]);
527bf215546Sopenharmony_ci         }
528bf215546Sopenharmony_ci
529bf215546Sopenharmony_ci         rho_xvec = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle0);
530bf215546Sopenharmony_ci         rho_yvec = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle1);
531bf215546Sopenharmony_ci         rho = lp_build_max(coord_bld, rho_xvec, rho_yvec);
532bf215546Sopenharmony_ci
533bf215546Sopenharmony_ci         if (rho_per_quad) {
534bf215546Sopenharmony_ci            rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
535bf215546Sopenharmony_ci                                            rho_bld->type, rho, 0);
536bf215546Sopenharmony_ci         }
537bf215546Sopenharmony_ci         else {
538bf215546Sopenharmony_ci            rho = lp_build_swizzle_scalar_aos(coord_bld, rho, 0, 4);
539bf215546Sopenharmony_ci         }
540bf215546Sopenharmony_ci         /* skipping sqrt hence returning rho squared */
541bf215546Sopenharmony_ci      }
542bf215546Sopenharmony_ci      else {
543bf215546Sopenharmony_ci         ddx_ddy[0] = lp_build_abs(coord_bld, ddx_ddy[0]);
544bf215546Sopenharmony_ci         if (dims > 2) {
545bf215546Sopenharmony_ci            ddx_ddy[1] = lp_build_abs(coord_bld, ddx_ddy[1]);
546bf215546Sopenharmony_ci         }
547bf215546Sopenharmony_ci         else {
548bf215546Sopenharmony_ci            ddx_ddy[1] = NULL; /* silence compiler warning */
549bf215546Sopenharmony_ci         }
550bf215546Sopenharmony_ci
551bf215546Sopenharmony_ci         if (dims < 2) {
552bf215546Sopenharmony_ci            rho_xvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle0);
553bf215546Sopenharmony_ci            rho_yvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle2);
554bf215546Sopenharmony_ci         }
555bf215546Sopenharmony_ci         else if (dims == 2) {
556bf215546Sopenharmony_ci            static const unsigned char swizzle02[] = {
557bf215546Sopenharmony_ci               0, 2,
558bf215546Sopenharmony_ci               LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
559bf215546Sopenharmony_ci            };
560bf215546Sopenharmony_ci            static const unsigned char swizzle13[] = {
561bf215546Sopenharmony_ci               1, 3,
562bf215546Sopenharmony_ci               LP_BLD_SWIZZLE_DONTCARE, LP_BLD_SWIZZLE_DONTCARE
563bf215546Sopenharmony_ci            };
564bf215546Sopenharmony_ci            rho_xvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle02);
565bf215546Sopenharmony_ci            rho_yvec = lp_build_swizzle_aos(coord_bld, ddx_ddy[0], swizzle13);
566bf215546Sopenharmony_ci         }
567bf215546Sopenharmony_ci         else {
568bf215546Sopenharmony_ci            LLVMValueRef shuffles1[LP_MAX_VECTOR_LENGTH];
569bf215546Sopenharmony_ci            LLVMValueRef shuffles2[LP_MAX_VECTOR_LENGTH];
570bf215546Sopenharmony_ci            assert(dims == 3);
571bf215546Sopenharmony_ci            for (i = 0; i < num_quads; i++) {
572bf215546Sopenharmony_ci               shuffles1[4*i + 0] = lp_build_const_int32(gallivm, 4*i);
573bf215546Sopenharmony_ci               shuffles1[4*i + 1] = lp_build_const_int32(gallivm, 4*i + 2);
574bf215546Sopenharmony_ci               shuffles1[4*i + 2] = lp_build_const_int32(gallivm, length + 4*i);
575bf215546Sopenharmony_ci               shuffles1[4*i + 3] = i32undef;
576bf215546Sopenharmony_ci               shuffles2[4*i + 0] = lp_build_const_int32(gallivm, 4*i + 1);
577bf215546Sopenharmony_ci               shuffles2[4*i + 1] = lp_build_const_int32(gallivm, 4*i + 3);
578bf215546Sopenharmony_ci               shuffles2[4*i + 2] = lp_build_const_int32(gallivm, length + 4*i + 2);
579bf215546Sopenharmony_ci               shuffles2[4*i + 3] = i32undef;
580bf215546Sopenharmony_ci            }
581bf215546Sopenharmony_ci            rho_xvec = LLVMBuildShuffleVector(builder, ddx_ddy[0], ddx_ddy[1],
582bf215546Sopenharmony_ci                                              LLVMConstVector(shuffles1, length), "");
583bf215546Sopenharmony_ci            rho_yvec = LLVMBuildShuffleVector(builder, ddx_ddy[0], ddx_ddy[1],
584bf215546Sopenharmony_ci                                              LLVMConstVector(shuffles2, length), "");
585bf215546Sopenharmony_ci         }
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci         rho_vec = lp_build_max(coord_bld, rho_xvec, rho_yvec);
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_ci         if (bld->coord_type.length > 4) {
590bf215546Sopenharmony_ci            /* expand size to each quad */
591bf215546Sopenharmony_ci            if (dims > 1) {
592bf215546Sopenharmony_ci               /* could use some broadcast_vector helper for this? */
593bf215546Sopenharmony_ci               LLVMValueRef src[LP_MAX_VECTOR_LENGTH/4];
594bf215546Sopenharmony_ci               for (i = 0; i < num_quads; i++) {
595bf215546Sopenharmony_ci                  src[i] = float_size;
596bf215546Sopenharmony_ci               }
597bf215546Sopenharmony_ci               float_size = lp_build_concat(bld->gallivm, src, float_size_bld->type, num_quads);
598bf215546Sopenharmony_ci            }
599bf215546Sopenharmony_ci            else {
600bf215546Sopenharmony_ci               float_size = lp_build_broadcast_scalar(coord_bld, float_size);
601bf215546Sopenharmony_ci            }
602bf215546Sopenharmony_ci            rho_vec = lp_build_mul(coord_bld, rho_vec, float_size);
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_ci            if (dims <= 1) {
605bf215546Sopenharmony_ci               rho = rho_vec;
606bf215546Sopenharmony_ci            }
607bf215546Sopenharmony_ci            else {
608bf215546Sopenharmony_ci               if (dims >= 2) {
609bf215546Sopenharmony_ci                  LLVMValueRef rho_s, rho_t, rho_r;
610bf215546Sopenharmony_ci
611bf215546Sopenharmony_ci                  rho_s = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle0);
612bf215546Sopenharmony_ci                  rho_t = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle1);
613bf215546Sopenharmony_ci
614bf215546Sopenharmony_ci                  rho = lp_build_max(coord_bld, rho_s, rho_t);
615bf215546Sopenharmony_ci
616bf215546Sopenharmony_ci                  if (dims >= 3) {
617bf215546Sopenharmony_ci                     rho_r = lp_build_swizzle_aos(coord_bld, rho_vec, swizzle2);
618bf215546Sopenharmony_ci                     rho = lp_build_max(coord_bld, rho, rho_r);
619bf215546Sopenharmony_ci                  }
620bf215546Sopenharmony_ci               }
621bf215546Sopenharmony_ci            }
622bf215546Sopenharmony_ci            if (rho_per_quad) {
623bf215546Sopenharmony_ci               rho = lp_build_pack_aos_scalars(bld->gallivm, coord_bld->type,
624bf215546Sopenharmony_ci                                               rho_bld->type, rho, 0);
625bf215546Sopenharmony_ci            }
626bf215546Sopenharmony_ci            else {
627bf215546Sopenharmony_ci               rho = lp_build_swizzle_scalar_aos(coord_bld, rho, 0, 4);
628bf215546Sopenharmony_ci            }
629bf215546Sopenharmony_ci         }
630bf215546Sopenharmony_ci         else {
631bf215546Sopenharmony_ci            if (dims <= 1) {
632bf215546Sopenharmony_ci               rho_vec = LLVMBuildExtractElement(builder, rho_vec, index0, "");
633bf215546Sopenharmony_ci            }
634bf215546Sopenharmony_ci            rho_vec = lp_build_mul(float_size_bld, rho_vec, float_size);
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci            if (dims <= 1) {
637bf215546Sopenharmony_ci               rho = rho_vec;
638bf215546Sopenharmony_ci            }
639bf215546Sopenharmony_ci            else {
640bf215546Sopenharmony_ci               if (dims >= 2) {
641bf215546Sopenharmony_ci                  LLVMValueRef rho_s, rho_t, rho_r;
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_ci                  rho_s = LLVMBuildExtractElement(builder, rho_vec, index0, "");
644bf215546Sopenharmony_ci                  rho_t = LLVMBuildExtractElement(builder, rho_vec, index1, "");
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_ci                  rho = lp_build_max(float_bld, rho_s, rho_t);
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci                  if (dims >= 3) {
649bf215546Sopenharmony_ci                     rho_r = LLVMBuildExtractElement(builder, rho_vec, index2, "");
650bf215546Sopenharmony_ci                     rho = lp_build_max(float_bld, rho, rho_r);
651bf215546Sopenharmony_ci                  }
652bf215546Sopenharmony_ci               }
653bf215546Sopenharmony_ci            }
654bf215546Sopenharmony_ci            if (!rho_per_quad) {
655bf215546Sopenharmony_ci               rho = lp_build_broadcast_scalar(rho_bld, rho);
656bf215546Sopenharmony_ci            }
657bf215546Sopenharmony_ci         }
658bf215546Sopenharmony_ci      }
659bf215546Sopenharmony_ci   }
660bf215546Sopenharmony_ci
661bf215546Sopenharmony_ci   return rho;
662bf215546Sopenharmony_ci}
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci
665bf215546Sopenharmony_ci/*
666bf215546Sopenharmony_ci * Bri-linear lod computation
667bf215546Sopenharmony_ci *
668bf215546Sopenharmony_ci * Use a piece-wise linear approximation of log2 such that:
669bf215546Sopenharmony_ci * - round to nearest, for values in the neighborhood of -1, 0, 1, 2, etc.
670bf215546Sopenharmony_ci * - linear approximation for values in the neighborhood of 0.5, 1.5., etc,
671bf215546Sopenharmony_ci *   with the steepness specified in 'factor'
672bf215546Sopenharmony_ci * - exact result for 0.5, 1.5, etc.
673bf215546Sopenharmony_ci *
674bf215546Sopenharmony_ci *
675bf215546Sopenharmony_ci *   1.0 -              /----*
676bf215546Sopenharmony_ci *                     /
677bf215546Sopenharmony_ci *                    /
678bf215546Sopenharmony_ci *                   /
679bf215546Sopenharmony_ci *   0.5 -          *
680bf215546Sopenharmony_ci *                 /
681bf215546Sopenharmony_ci *                /
682bf215546Sopenharmony_ci *               /
683bf215546Sopenharmony_ci *   0.0 - *----/
684bf215546Sopenharmony_ci *
685bf215546Sopenharmony_ci *         |                 |
686bf215546Sopenharmony_ci *        2^0               2^1
687bf215546Sopenharmony_ci *
688bf215546Sopenharmony_ci * This is a technique also commonly used in hardware:
689bf215546Sopenharmony_ci * - http://ixbtlabs.com/articles2/gffx/nv40-rx800-3.html
690bf215546Sopenharmony_ci *
691bf215546Sopenharmony_ci * TODO: For correctness, this should only be applied when texture is known to
692bf215546Sopenharmony_ci * have regular mipmaps, i.e., mipmaps derived from the base level.
693bf215546Sopenharmony_ci *
694bf215546Sopenharmony_ci * TODO: This could be done in fixed point, where applicable.
695bf215546Sopenharmony_ci */
696bf215546Sopenharmony_cistatic void
697bf215546Sopenharmony_cilp_build_brilinear_lod(struct lp_build_context *bld,
698bf215546Sopenharmony_ci                       LLVMValueRef lod,
699bf215546Sopenharmony_ci                       double factor,
700bf215546Sopenharmony_ci                       LLVMValueRef *out_lod_ipart,
701bf215546Sopenharmony_ci                       LLVMValueRef *out_lod_fpart)
702bf215546Sopenharmony_ci{
703bf215546Sopenharmony_ci   LLVMValueRef lod_fpart;
704bf215546Sopenharmony_ci   double pre_offset = (factor - 0.5)/factor - 0.5;
705bf215546Sopenharmony_ci   double post_offset = 1 - factor;
706bf215546Sopenharmony_ci
707bf215546Sopenharmony_ci   if (0) {
708bf215546Sopenharmony_ci      lp_build_printf(bld->gallivm, "lod = %f\n", lod);
709bf215546Sopenharmony_ci   }
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_ci   lod = lp_build_add(bld, lod,
712bf215546Sopenharmony_ci                      lp_build_const_vec(bld->gallivm, bld->type, pre_offset));
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci   lp_build_ifloor_fract(bld, lod, out_lod_ipart, &lod_fpart);
715bf215546Sopenharmony_ci
716bf215546Sopenharmony_ci   lod_fpart = lp_build_mad(bld, lod_fpart,
717bf215546Sopenharmony_ci                            lp_build_const_vec(bld->gallivm, bld->type, factor),
718bf215546Sopenharmony_ci                            lp_build_const_vec(bld->gallivm, bld->type, post_offset));
719bf215546Sopenharmony_ci
720bf215546Sopenharmony_ci   /*
721bf215546Sopenharmony_ci    * It's not necessary to clamp lod_fpart since:
722bf215546Sopenharmony_ci    * - the above expression will never produce numbers greater than one.
723bf215546Sopenharmony_ci    * - the mip filtering branch is only taken if lod_fpart is positive
724bf215546Sopenharmony_ci    */
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci   *out_lod_fpart = lod_fpart;
727bf215546Sopenharmony_ci
728bf215546Sopenharmony_ci   if (0) {
729bf215546Sopenharmony_ci      lp_build_printf(bld->gallivm, "lod_ipart = %i\n", *out_lod_ipart);
730bf215546Sopenharmony_ci      lp_build_printf(bld->gallivm, "lod_fpart = %f\n\n", *out_lod_fpart);
731bf215546Sopenharmony_ci   }
732bf215546Sopenharmony_ci}
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_ci
735bf215546Sopenharmony_ci/*
736bf215546Sopenharmony_ci * Combined log2 and brilinear lod computation.
737bf215546Sopenharmony_ci *
738bf215546Sopenharmony_ci * It's in all identical to calling lp_build_fast_log2() and
739bf215546Sopenharmony_ci * lp_build_brilinear_lod() above, but by combining we can compute the integer
740bf215546Sopenharmony_ci * and fractional part independently.
741bf215546Sopenharmony_ci */
742bf215546Sopenharmony_cistatic void
743bf215546Sopenharmony_cilp_build_brilinear_rho(struct lp_build_context *bld,
744bf215546Sopenharmony_ci                       LLVMValueRef rho,
745bf215546Sopenharmony_ci                       double factor,
746bf215546Sopenharmony_ci                       LLVMValueRef *out_lod_ipart,
747bf215546Sopenharmony_ci                       LLVMValueRef *out_lod_fpart)
748bf215546Sopenharmony_ci{
749bf215546Sopenharmony_ci   LLVMValueRef lod_ipart;
750bf215546Sopenharmony_ci   LLVMValueRef lod_fpart;
751bf215546Sopenharmony_ci
752bf215546Sopenharmony_ci   const double pre_factor = (2*factor - 0.5)/(M_SQRT2*factor);
753bf215546Sopenharmony_ci   const double post_offset = 1 - 2*factor;
754bf215546Sopenharmony_ci
755bf215546Sopenharmony_ci   assert(bld->type.floating);
756bf215546Sopenharmony_ci
757bf215546Sopenharmony_ci   assert(lp_check_value(bld->type, rho));
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_ci   /*
760bf215546Sopenharmony_ci    * The pre factor will make the intersections with the exact powers of two
761bf215546Sopenharmony_ci    * happen precisely where we want them to be, which means that the integer
762bf215546Sopenharmony_ci    * part will not need any post adjustments.
763bf215546Sopenharmony_ci    */
764bf215546Sopenharmony_ci   rho = lp_build_mul(bld, rho,
765bf215546Sopenharmony_ci                      lp_build_const_vec(bld->gallivm, bld->type, pre_factor));
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_ci   /* ipart = ifloor(log2(rho)) */
768bf215546Sopenharmony_ci   lod_ipart = lp_build_extract_exponent(bld, rho, 0);
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ci   /* fpart = rho / 2**ipart */
771bf215546Sopenharmony_ci   lod_fpart = lp_build_extract_mantissa(bld, rho);
772bf215546Sopenharmony_ci
773bf215546Sopenharmony_ci   lod_fpart = lp_build_mad(bld, lod_fpart,
774bf215546Sopenharmony_ci                            lp_build_const_vec(bld->gallivm, bld->type, factor),
775bf215546Sopenharmony_ci                            lp_build_const_vec(bld->gallivm, bld->type, post_offset));
776bf215546Sopenharmony_ci
777bf215546Sopenharmony_ci   /*
778bf215546Sopenharmony_ci    * Like lp_build_brilinear_lod, it's not necessary to clamp lod_fpart since:
779bf215546Sopenharmony_ci    * - the above expression will never produce numbers greater than one.
780bf215546Sopenharmony_ci    * - the mip filtering branch is only taken if lod_fpart is positive
781bf215546Sopenharmony_ci    */
782bf215546Sopenharmony_ci
783bf215546Sopenharmony_ci   *out_lod_ipart = lod_ipart;
784bf215546Sopenharmony_ci   *out_lod_fpart = lod_fpart;
785bf215546Sopenharmony_ci}
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci
788bf215546Sopenharmony_ci/**
789bf215546Sopenharmony_ci * Fast implementation of iround(log2(sqrt(x))), based on
790bf215546Sopenharmony_ci * log2(x^n) == n*log2(x).
791bf215546Sopenharmony_ci *
792bf215546Sopenharmony_ci * Gives accurate results all the time.
793bf215546Sopenharmony_ci * (Could be trivially extended to handle other power-of-two roots.)
794bf215546Sopenharmony_ci */
795bf215546Sopenharmony_cistatic LLVMValueRef
796bf215546Sopenharmony_cilp_build_ilog2_sqrt(struct lp_build_context *bld,
797bf215546Sopenharmony_ci                    LLVMValueRef x)
798bf215546Sopenharmony_ci{
799bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
800bf215546Sopenharmony_ci   LLVMValueRef ipart;
801bf215546Sopenharmony_ci   struct lp_type i_type = lp_int_type(bld->type);
802bf215546Sopenharmony_ci   LLVMValueRef one = lp_build_const_int_vec(bld->gallivm, i_type, 1);
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci   assert(bld->type.floating);
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_ci   assert(lp_check_value(bld->type, x));
807bf215546Sopenharmony_ci
808bf215546Sopenharmony_ci   /* ipart = log2(x) + 0.5 = 0.5*(log2(x^2) + 1.0) */
809bf215546Sopenharmony_ci   ipart = lp_build_extract_exponent(bld, x, 1);
810bf215546Sopenharmony_ci   ipart = LLVMBuildAShr(builder, ipart, one, "");
811bf215546Sopenharmony_ci
812bf215546Sopenharmony_ci   return ipart;
813bf215546Sopenharmony_ci}
814bf215546Sopenharmony_ci
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_ci/**
817bf215546Sopenharmony_ci * Generate code to compute texture level of detail (lambda).
818bf215546Sopenharmony_ci * \param derivs  partial derivatives of (s, t, r, q) with respect to X and Y
819bf215546Sopenharmony_ci * \param lod_bias  optional float vector with the shader lod bias
820bf215546Sopenharmony_ci * \param explicit_lod  optional float vector with the explicit lod
821bf215546Sopenharmony_ci * \param cube_rho  rho calculated by cube coord mapping (optional)
822bf215546Sopenharmony_ci * \param out_lod_ipart  integer part of lod
823bf215546Sopenharmony_ci * \param out_lod_fpart  float part of lod (never larger than 1 but may be negative)
824bf215546Sopenharmony_ci * \param out_lod_positive  (mask) if lod is positive (i.e. texture is minified)
825bf215546Sopenharmony_ci *
826bf215546Sopenharmony_ci * The resulting lod can be scalar per quad or be per element.
827bf215546Sopenharmony_ci */
828bf215546Sopenharmony_civoid
829bf215546Sopenharmony_cilp_build_lod_selector(struct lp_build_sample_context *bld,
830bf215546Sopenharmony_ci                      boolean is_lodq,
831bf215546Sopenharmony_ci                      unsigned texture_unit,
832bf215546Sopenharmony_ci                      unsigned sampler_unit,
833bf215546Sopenharmony_ci                      LLVMValueRef s,
834bf215546Sopenharmony_ci                      LLVMValueRef t,
835bf215546Sopenharmony_ci                      LLVMValueRef r,
836bf215546Sopenharmony_ci                      LLVMValueRef cube_rho,
837bf215546Sopenharmony_ci                      const struct lp_derivatives *derivs,
838bf215546Sopenharmony_ci                      LLVMValueRef lod_bias, /* optional */
839bf215546Sopenharmony_ci                      LLVMValueRef explicit_lod, /* optional */
840bf215546Sopenharmony_ci                      enum pipe_tex_mipfilter mip_filter,
841bf215546Sopenharmony_ci                      LLVMValueRef max_aniso,
842bf215546Sopenharmony_ci                      LLVMValueRef *out_lod,
843bf215546Sopenharmony_ci                      LLVMValueRef *out_lod_ipart,
844bf215546Sopenharmony_ci                      LLVMValueRef *out_lod_fpart,
845bf215546Sopenharmony_ci                      LLVMValueRef *out_lod_positive)
846bf215546Sopenharmony_ci
847bf215546Sopenharmony_ci{
848bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
849bf215546Sopenharmony_ci   struct lp_sampler_dynamic_state *dynamic_state = bld->dynamic_state;
850bf215546Sopenharmony_ci   struct lp_build_context *lodf_bld = &bld->lodf_bld;
851bf215546Sopenharmony_ci   LLVMValueRef lod;
852bf215546Sopenharmony_ci
853bf215546Sopenharmony_ci   *out_lod_ipart = bld->lodi_bld.zero;
854bf215546Sopenharmony_ci   *out_lod_positive = bld->lodi_bld.zero;
855bf215546Sopenharmony_ci   *out_lod_fpart = lodf_bld->zero;
856bf215546Sopenharmony_ci
857bf215546Sopenharmony_ci   /*
858bf215546Sopenharmony_ci    * For determining min/mag, we follow GL 4.1 spec, 3.9.12 Texture Magnification:
859bf215546Sopenharmony_ci    * "Implementations may either unconditionally assume c = 0 for the minification
860bf215546Sopenharmony_ci    * vs. magnification switch-over point, or may choose to make c depend on the
861bf215546Sopenharmony_ci    * combination of minification and magnification modes as follows: if the
862bf215546Sopenharmony_ci    * magnification filter is given by LINEAR and the minification filter is given
863bf215546Sopenharmony_ci    * by NEAREST_MIPMAP_NEAREST or NEAREST_MIPMAP_LINEAR, then c = 0.5. This is
864bf215546Sopenharmony_ci    * done to ensure that a minified texture does not appear "sharper" than a
865bf215546Sopenharmony_ci    * magnified texture. Otherwise c = 0."
866bf215546Sopenharmony_ci    * And 3.9.11 Texture Minification:
867bf215546Sopenharmony_ci    * "If lod is less than or equal to the constant c (see section 3.9.12) the
868bf215546Sopenharmony_ci    * texture is said to be magnified; if it is greater, the texture is minified."
869bf215546Sopenharmony_ci    * So, using 0 as switchover point always, and using magnification for lod == 0.
870bf215546Sopenharmony_ci    * Note that the always c = 0 behavior is new (first appearing in GL 3.1 spec),
871bf215546Sopenharmony_ci    * old GL versions required 0.5 for the modes listed above.
872bf215546Sopenharmony_ci    * I have no clue about the (undocumented) wishes of d3d9/d3d10 here!
873bf215546Sopenharmony_ci    */
874bf215546Sopenharmony_ci
875bf215546Sopenharmony_ci   if (bld->static_sampler_state->min_max_lod_equal && !is_lodq) {
876bf215546Sopenharmony_ci      /* User is forcing sampling from a particular mipmap level.
877bf215546Sopenharmony_ci       * This is hit during mipmap generation.
878bf215546Sopenharmony_ci       */
879bf215546Sopenharmony_ci      LLVMValueRef min_lod =
880bf215546Sopenharmony_ci         dynamic_state->min_lod(dynamic_state, bld->gallivm,
881bf215546Sopenharmony_ci                                bld->context_ptr, sampler_unit);
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci      lod = lp_build_broadcast_scalar(lodf_bld, min_lod);
884bf215546Sopenharmony_ci   }
885bf215546Sopenharmony_ci   else {
886bf215546Sopenharmony_ci      if (explicit_lod) {
887bf215546Sopenharmony_ci         if (bld->num_lods != bld->coord_type.length)
888bf215546Sopenharmony_ci            lod = lp_build_pack_aos_scalars(bld->gallivm, bld->coord_bld.type,
889bf215546Sopenharmony_ci                                            lodf_bld->type, explicit_lod, 0);
890bf215546Sopenharmony_ci         else
891bf215546Sopenharmony_ci            lod = explicit_lod;
892bf215546Sopenharmony_ci      }
893bf215546Sopenharmony_ci      else {
894bf215546Sopenharmony_ci         LLVMValueRef rho;
895bf215546Sopenharmony_ci         boolean rho_squared = (bld->no_rho_approx &&
896bf215546Sopenharmony_ci                                (bld->dims > 1)) || cube_rho;
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci         if (bld->static_sampler_state->aniso &&
899bf215546Sopenharmony_ci             !explicit_lod) {
900bf215546Sopenharmony_ci            rho = lp_build_pmin(bld, texture_unit, s, t, max_aniso);
901bf215546Sopenharmony_ci            rho_squared = true;
902bf215546Sopenharmony_ci         } else
903bf215546Sopenharmony_ci            rho = lp_build_rho(bld, texture_unit, s, t, r, cube_rho, derivs);
904bf215546Sopenharmony_ci
905bf215546Sopenharmony_ci         /*
906bf215546Sopenharmony_ci          * Compute lod = log2(rho)
907bf215546Sopenharmony_ci          */
908bf215546Sopenharmony_ci
909bf215546Sopenharmony_ci         if (!lod_bias && !is_lodq &&
910bf215546Sopenharmony_ci             !bld->static_sampler_state->aniso &&
911bf215546Sopenharmony_ci             !bld->static_sampler_state->lod_bias_non_zero &&
912bf215546Sopenharmony_ci             !bld->static_sampler_state->apply_max_lod &&
913bf215546Sopenharmony_ci             !bld->static_sampler_state->apply_min_lod) {
914bf215546Sopenharmony_ci            /*
915bf215546Sopenharmony_ci             * Special case when there are no post-log2 adjustments, which
916bf215546Sopenharmony_ci             * saves instructions but keeping the integer and fractional lod
917bf215546Sopenharmony_ci             * computations separate from the start.
918bf215546Sopenharmony_ci             */
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci            if (mip_filter == PIPE_TEX_MIPFILTER_NONE ||
921bf215546Sopenharmony_ci                mip_filter == PIPE_TEX_MIPFILTER_NEAREST) {
922bf215546Sopenharmony_ci               /*
923bf215546Sopenharmony_ci                * Don't actually need both values all the time, lod_ipart is
924bf215546Sopenharmony_ci                * needed for nearest mipfilter, lod_positive if min != mag.
925bf215546Sopenharmony_ci                */
926bf215546Sopenharmony_ci               if (rho_squared) {
927bf215546Sopenharmony_ci                  *out_lod_ipart = lp_build_ilog2_sqrt(lodf_bld, rho);
928bf215546Sopenharmony_ci               }
929bf215546Sopenharmony_ci               else {
930bf215546Sopenharmony_ci                  *out_lod_ipart = lp_build_ilog2(lodf_bld, rho);
931bf215546Sopenharmony_ci               }
932bf215546Sopenharmony_ci               *out_lod_positive = lp_build_cmp(lodf_bld, PIPE_FUNC_GREATER,
933bf215546Sopenharmony_ci                                                rho, lodf_bld->one);
934bf215546Sopenharmony_ci               return;
935bf215546Sopenharmony_ci            }
936bf215546Sopenharmony_ci            if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR &&
937bf215546Sopenharmony_ci                !bld->no_brilinear && !rho_squared &&
938bf215546Sopenharmony_ci                !bld->static_sampler_state->aniso) {
939bf215546Sopenharmony_ci               /*
940bf215546Sopenharmony_ci                * This can't work if rho is squared. Not sure if it could be
941bf215546Sopenharmony_ci                * fixed while keeping it worthwile, could also do sqrt here
942bf215546Sopenharmony_ci                * but brilinear and no_rho_opt seems like a combination not
943bf215546Sopenharmony_ci                * making much sense anyway so just use ordinary path below.
944bf215546Sopenharmony_ci                */
945bf215546Sopenharmony_ci               lp_build_brilinear_rho(lodf_bld, rho, BRILINEAR_FACTOR,
946bf215546Sopenharmony_ci                                      out_lod_ipart, out_lod_fpart);
947bf215546Sopenharmony_ci               *out_lod_positive = lp_build_cmp(lodf_bld, PIPE_FUNC_GREATER,
948bf215546Sopenharmony_ci                                                rho, lodf_bld->one);
949bf215546Sopenharmony_ci               return;
950bf215546Sopenharmony_ci            }
951bf215546Sopenharmony_ci         }
952bf215546Sopenharmony_ci
953bf215546Sopenharmony_ci         if (0) {
954bf215546Sopenharmony_ci            lod = lp_build_log2(lodf_bld, rho);
955bf215546Sopenharmony_ci         }
956bf215546Sopenharmony_ci         else {
957bf215546Sopenharmony_ci            /* get more accurate results if we just sqaure rho always */
958bf215546Sopenharmony_ci            if (!rho_squared)
959bf215546Sopenharmony_ci               rho = lp_build_mul(lodf_bld, rho, rho);
960bf215546Sopenharmony_ci            lod = lp_build_fast_log2(lodf_bld, rho);
961bf215546Sopenharmony_ci         }
962bf215546Sopenharmony_ci
963bf215546Sopenharmony_ci         /* log2(x^2) == 0.5*log2(x) */
964bf215546Sopenharmony_ci         lod = lp_build_mul(lodf_bld, lod,
965bf215546Sopenharmony_ci                            lp_build_const_vec(bld->gallivm, lodf_bld->type, 0.5F));
966bf215546Sopenharmony_ci
967bf215546Sopenharmony_ci         /* add shader lod bias */
968bf215546Sopenharmony_ci         if (lod_bias) {
969bf215546Sopenharmony_ci            if (bld->num_lods != bld->coord_type.length)
970bf215546Sopenharmony_ci               lod_bias = lp_build_pack_aos_scalars(bld->gallivm, bld->coord_bld.type,
971bf215546Sopenharmony_ci                                                    lodf_bld->type, lod_bias, 0);
972bf215546Sopenharmony_ci            lod = LLVMBuildFAdd(builder, lod, lod_bias, "shader_lod_bias");
973bf215546Sopenharmony_ci         }
974bf215546Sopenharmony_ci      }
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_ci      /* add sampler lod bias */
977bf215546Sopenharmony_ci      if (bld->static_sampler_state->lod_bias_non_zero) {
978bf215546Sopenharmony_ci         LLVMValueRef sampler_lod_bias =
979bf215546Sopenharmony_ci            dynamic_state->lod_bias(dynamic_state, bld->gallivm,
980bf215546Sopenharmony_ci                                    bld->context_ptr, sampler_unit);
981bf215546Sopenharmony_ci         sampler_lod_bias = lp_build_broadcast_scalar(lodf_bld,
982bf215546Sopenharmony_ci                                                      sampler_lod_bias);
983bf215546Sopenharmony_ci         lod = LLVMBuildFAdd(builder, lod, sampler_lod_bias, "sampler_lod_bias");
984bf215546Sopenharmony_ci      }
985bf215546Sopenharmony_ci
986bf215546Sopenharmony_ci      if (is_lodq) {
987bf215546Sopenharmony_ci         *out_lod = lod;
988bf215546Sopenharmony_ci      }
989bf215546Sopenharmony_ci
990bf215546Sopenharmony_ci      /* clamp lod */
991bf215546Sopenharmony_ci      if (bld->static_sampler_state->apply_max_lod) {
992bf215546Sopenharmony_ci         LLVMValueRef max_lod =
993bf215546Sopenharmony_ci            dynamic_state->max_lod(dynamic_state, bld->gallivm,
994bf215546Sopenharmony_ci                                   bld->context_ptr, sampler_unit);
995bf215546Sopenharmony_ci         max_lod = lp_build_broadcast_scalar(lodf_bld, max_lod);
996bf215546Sopenharmony_ci
997bf215546Sopenharmony_ci         lod = lp_build_min(lodf_bld, lod, max_lod);
998bf215546Sopenharmony_ci      }
999bf215546Sopenharmony_ci      if (bld->static_sampler_state->apply_min_lod) {
1000bf215546Sopenharmony_ci         LLVMValueRef min_lod =
1001bf215546Sopenharmony_ci            dynamic_state->min_lod(dynamic_state, bld->gallivm,
1002bf215546Sopenharmony_ci                                   bld->context_ptr, sampler_unit);
1003bf215546Sopenharmony_ci         min_lod = lp_build_broadcast_scalar(lodf_bld, min_lod);
1004bf215546Sopenharmony_ci
1005bf215546Sopenharmony_ci         lod = lp_build_max(lodf_bld, lod, min_lod);
1006bf215546Sopenharmony_ci      }
1007bf215546Sopenharmony_ci
1008bf215546Sopenharmony_ci      if (is_lodq) {
1009bf215546Sopenharmony_ci         *out_lod_fpart = lod;
1010bf215546Sopenharmony_ci         return;
1011bf215546Sopenharmony_ci      }
1012bf215546Sopenharmony_ci   }
1013bf215546Sopenharmony_ci
1014bf215546Sopenharmony_ci   *out_lod_positive = lp_build_cmp(lodf_bld, PIPE_FUNC_GREATER,
1015bf215546Sopenharmony_ci                                    lod, lodf_bld->zero);
1016bf215546Sopenharmony_ci
1017bf215546Sopenharmony_ci   if (bld->static_sampler_state->aniso) {
1018bf215546Sopenharmony_ci      *out_lod_ipart = lp_build_itrunc(lodf_bld, lod);
1019bf215546Sopenharmony_ci   } else if (mip_filter == PIPE_TEX_MIPFILTER_LINEAR) {
1020bf215546Sopenharmony_ci      if (!bld->no_brilinear) {
1021bf215546Sopenharmony_ci         lp_build_brilinear_lod(lodf_bld, lod, BRILINEAR_FACTOR,
1022bf215546Sopenharmony_ci                                out_lod_ipart, out_lod_fpart);
1023bf215546Sopenharmony_ci      }
1024bf215546Sopenharmony_ci      else {
1025bf215546Sopenharmony_ci         lp_build_ifloor_fract(lodf_bld, lod, out_lod_ipart, out_lod_fpart);
1026bf215546Sopenharmony_ci      }
1027bf215546Sopenharmony_ci
1028bf215546Sopenharmony_ci      lp_build_name(*out_lod_fpart, "lod_fpart");
1029bf215546Sopenharmony_ci   }
1030bf215546Sopenharmony_ci   else {
1031bf215546Sopenharmony_ci      *out_lod_ipart = lp_build_iround(lodf_bld, lod);
1032bf215546Sopenharmony_ci   }
1033bf215546Sopenharmony_ci
1034bf215546Sopenharmony_ci   lp_build_name(*out_lod_ipart, "lod_ipart");
1035bf215546Sopenharmony_ci
1036bf215546Sopenharmony_ci   return;
1037bf215546Sopenharmony_ci}
1038bf215546Sopenharmony_ci
1039bf215546Sopenharmony_ci
1040bf215546Sopenharmony_ci/**
1041bf215546Sopenharmony_ci * For PIPE_TEX_MIPFILTER_NEAREST, convert int part of lod
1042bf215546Sopenharmony_ci * to actual mip level.
1043bf215546Sopenharmony_ci * Note: this is all scalar per quad code.
1044bf215546Sopenharmony_ci * \param lod_ipart  int texture level of detail
1045bf215546Sopenharmony_ci * \param level_out  returns integer
1046bf215546Sopenharmony_ci * \param out_of_bounds returns per coord out_of_bounds mask if provided
1047bf215546Sopenharmony_ci */
1048bf215546Sopenharmony_civoid
1049bf215546Sopenharmony_cilp_build_nearest_mip_level(struct lp_build_sample_context *bld,
1050bf215546Sopenharmony_ci                           unsigned texture_unit,
1051bf215546Sopenharmony_ci                           LLVMValueRef lod_ipart,
1052bf215546Sopenharmony_ci                           LLVMValueRef *level_out,
1053bf215546Sopenharmony_ci                           LLVMValueRef *out_of_bounds)
1054bf215546Sopenharmony_ci{
1055bf215546Sopenharmony_ci   struct lp_build_context *leveli_bld = &bld->leveli_bld;
1056bf215546Sopenharmony_ci   struct lp_sampler_dynamic_state *dynamic_state = bld->dynamic_state;
1057bf215546Sopenharmony_ci   LLVMValueRef first_level, last_level, level;
1058bf215546Sopenharmony_ci
1059bf215546Sopenharmony_ci   first_level = dynamic_state->first_level(dynamic_state, bld->gallivm,
1060bf215546Sopenharmony_ci                                            bld->context_ptr, texture_unit, NULL);
1061bf215546Sopenharmony_ci   last_level = dynamic_state->last_level(dynamic_state, bld->gallivm,
1062bf215546Sopenharmony_ci                                          bld->context_ptr, texture_unit, NULL);
1063bf215546Sopenharmony_ci   first_level = lp_build_broadcast_scalar(leveli_bld, first_level);
1064bf215546Sopenharmony_ci   last_level = lp_build_broadcast_scalar(leveli_bld, last_level);
1065bf215546Sopenharmony_ci
1066bf215546Sopenharmony_ci   level = lp_build_add(leveli_bld, lod_ipart, first_level);
1067bf215546Sopenharmony_ci
1068bf215546Sopenharmony_ci   if (out_of_bounds) {
1069bf215546Sopenharmony_ci      LLVMValueRef out, out1;
1070bf215546Sopenharmony_ci      out = lp_build_cmp(leveli_bld, PIPE_FUNC_LESS, level, first_level);
1071bf215546Sopenharmony_ci      out1 = lp_build_cmp(leveli_bld, PIPE_FUNC_GREATER, level, last_level);
1072bf215546Sopenharmony_ci      out = lp_build_or(leveli_bld, out, out1);
1073bf215546Sopenharmony_ci      if (bld->num_mips == bld->coord_bld.type.length) {
1074bf215546Sopenharmony_ci         *out_of_bounds = out;
1075bf215546Sopenharmony_ci      }
1076bf215546Sopenharmony_ci      else if (bld->num_mips == 1) {
1077bf215546Sopenharmony_ci         *out_of_bounds = lp_build_broadcast_scalar(&bld->int_coord_bld, out);
1078bf215546Sopenharmony_ci      }
1079bf215546Sopenharmony_ci      else {
1080bf215546Sopenharmony_ci         assert(bld->num_mips == bld->coord_bld.type.length / 4);
1081bf215546Sopenharmony_ci         *out_of_bounds = lp_build_unpack_broadcast_aos_scalars(bld->gallivm,
1082bf215546Sopenharmony_ci                                                                leveli_bld->type,
1083bf215546Sopenharmony_ci                                                                bld->int_coord_bld.type,
1084bf215546Sopenharmony_ci                                                                out);
1085bf215546Sopenharmony_ci      }
1086bf215546Sopenharmony_ci      level = lp_build_andnot(&bld->int_coord_bld, level, *out_of_bounds);
1087bf215546Sopenharmony_ci      *level_out = level;
1088bf215546Sopenharmony_ci   }
1089bf215546Sopenharmony_ci   else {
1090bf215546Sopenharmony_ci      /* clamp level to legal range of levels */
1091bf215546Sopenharmony_ci      *level_out = lp_build_clamp(leveli_bld, level, first_level, last_level);
1092bf215546Sopenharmony_ci
1093bf215546Sopenharmony_ci   }
1094bf215546Sopenharmony_ci}
1095bf215546Sopenharmony_ci
1096bf215546Sopenharmony_ci
1097bf215546Sopenharmony_ci/**
1098bf215546Sopenharmony_ci * For PIPE_TEX_MIPFILTER_LINEAR, convert per-quad (or per element) int LOD(s)
1099bf215546Sopenharmony_ci * to two (per-quad) (adjacent) mipmap level indexes, and fix up float lod
1100bf215546Sopenharmony_ci * part accordingly.
1101bf215546Sopenharmony_ci * Later, we'll sample from those two mipmap levels and interpolate between them.
1102bf215546Sopenharmony_ci */
1103bf215546Sopenharmony_civoid
1104bf215546Sopenharmony_cilp_build_linear_mip_levels(struct lp_build_sample_context *bld,
1105bf215546Sopenharmony_ci                           unsigned texture_unit,
1106bf215546Sopenharmony_ci                           LLVMValueRef lod_ipart,
1107bf215546Sopenharmony_ci                           LLVMValueRef *lod_fpart_inout,
1108bf215546Sopenharmony_ci                           LLVMValueRef *level0_out,
1109bf215546Sopenharmony_ci                           LLVMValueRef *level1_out)
1110bf215546Sopenharmony_ci{
1111bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
1112bf215546Sopenharmony_ci   struct lp_sampler_dynamic_state *dynamic_state = bld->dynamic_state;
1113bf215546Sopenharmony_ci   struct lp_build_context *leveli_bld = &bld->leveli_bld;
1114bf215546Sopenharmony_ci   struct lp_build_context *levelf_bld = &bld->levelf_bld;
1115bf215546Sopenharmony_ci   LLVMValueRef first_level, last_level;
1116bf215546Sopenharmony_ci   LLVMValueRef clamp_min;
1117bf215546Sopenharmony_ci   LLVMValueRef clamp_max;
1118bf215546Sopenharmony_ci
1119bf215546Sopenharmony_ci   assert(bld->num_lods == bld->num_mips);
1120bf215546Sopenharmony_ci
1121bf215546Sopenharmony_ci   first_level = dynamic_state->first_level(dynamic_state, bld->gallivm,
1122bf215546Sopenharmony_ci                                            bld->context_ptr, texture_unit, NULL);
1123bf215546Sopenharmony_ci   last_level = dynamic_state->last_level(dynamic_state, bld->gallivm,
1124bf215546Sopenharmony_ci                                          bld->context_ptr, texture_unit, NULL);
1125bf215546Sopenharmony_ci   first_level = lp_build_broadcast_scalar(leveli_bld, first_level);
1126bf215546Sopenharmony_ci   last_level = lp_build_broadcast_scalar(leveli_bld, last_level);
1127bf215546Sopenharmony_ci
1128bf215546Sopenharmony_ci   *level0_out = lp_build_add(leveli_bld, lod_ipart, first_level);
1129bf215546Sopenharmony_ci   *level1_out = lp_build_add(leveli_bld, *level0_out, leveli_bld->one);
1130bf215546Sopenharmony_ci
1131bf215546Sopenharmony_ci   /*
1132bf215546Sopenharmony_ci    * Clamp both *level0_out and *level1_out to [first_level, last_level], with
1133bf215546Sopenharmony_ci    * the minimum number of comparisons, and zeroing lod_fpart in the extreme
1134bf215546Sopenharmony_ci    * ends in the process.
1135bf215546Sopenharmony_ci    */
1136bf215546Sopenharmony_ci
1137bf215546Sopenharmony_ci   /* *level0_out < first_level */
1138bf215546Sopenharmony_ci   clamp_min = LLVMBuildICmp(builder, LLVMIntSLT,
1139bf215546Sopenharmony_ci                             *level0_out, first_level,
1140bf215546Sopenharmony_ci                             "clamp_lod_to_first");
1141bf215546Sopenharmony_ci
1142bf215546Sopenharmony_ci   *level0_out = LLVMBuildSelect(builder, clamp_min,
1143bf215546Sopenharmony_ci                                 first_level, *level0_out, "");
1144bf215546Sopenharmony_ci
1145bf215546Sopenharmony_ci   *level1_out = LLVMBuildSelect(builder, clamp_min,
1146bf215546Sopenharmony_ci                                 first_level, *level1_out, "");
1147bf215546Sopenharmony_ci
1148bf215546Sopenharmony_ci   *lod_fpart_inout = LLVMBuildSelect(builder, clamp_min,
1149bf215546Sopenharmony_ci                                      levelf_bld->zero, *lod_fpart_inout, "");
1150bf215546Sopenharmony_ci
1151bf215546Sopenharmony_ci   /* *level0_out >= last_level */
1152bf215546Sopenharmony_ci   clamp_max = LLVMBuildICmp(builder, LLVMIntSGE,
1153bf215546Sopenharmony_ci                             *level0_out, last_level,
1154bf215546Sopenharmony_ci                             "clamp_lod_to_last");
1155bf215546Sopenharmony_ci
1156bf215546Sopenharmony_ci   *level0_out = LLVMBuildSelect(builder, clamp_max,
1157bf215546Sopenharmony_ci                                 last_level, *level0_out, "");
1158bf215546Sopenharmony_ci
1159bf215546Sopenharmony_ci   *level1_out = LLVMBuildSelect(builder, clamp_max,
1160bf215546Sopenharmony_ci                                 last_level, *level1_out, "");
1161bf215546Sopenharmony_ci
1162bf215546Sopenharmony_ci   *lod_fpart_inout = LLVMBuildSelect(builder, clamp_max,
1163bf215546Sopenharmony_ci                                      levelf_bld->zero, *lod_fpart_inout, "");
1164bf215546Sopenharmony_ci
1165bf215546Sopenharmony_ci   lp_build_name(*level0_out, "texture%u_miplevel0", texture_unit);
1166bf215546Sopenharmony_ci   lp_build_name(*level1_out, "texture%u_miplevel1", texture_unit);
1167bf215546Sopenharmony_ci   lp_build_name(*lod_fpart_inout, "texture%u_mipweight", texture_unit);
1168bf215546Sopenharmony_ci}
1169bf215546Sopenharmony_ci
1170bf215546Sopenharmony_ci/**
1171bf215546Sopenharmony_ci * A helper function that factorizes this common pattern.
1172bf215546Sopenharmony_ci */
1173bf215546Sopenharmony_cistatic LLVMValueRef
1174bf215546Sopenharmony_ciload_mip(struct gallivm_state *gallivm, LLVMValueRef offsets, LLVMValueRef index1) {
1175bf215546Sopenharmony_ci   LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1176bf215546Sopenharmony_ci   LLVMValueRef indexes[2] = {zero, index1};
1177bf215546Sopenharmony_ci   LLVMValueRef ptr = LLVMBuildGEP(gallivm->builder, offsets, indexes, ARRAY_SIZE(indexes), "");
1178bf215546Sopenharmony_ci   return LLVMBuildLoad(gallivm->builder, ptr, "");
1179bf215546Sopenharmony_ci}
1180bf215546Sopenharmony_ci
1181bf215546Sopenharmony_ci/**
1182bf215546Sopenharmony_ci * Return pointer to a single mipmap level.
1183bf215546Sopenharmony_ci * \param level  integer mipmap level
1184bf215546Sopenharmony_ci */
1185bf215546Sopenharmony_ciLLVMValueRef
1186bf215546Sopenharmony_cilp_build_get_mipmap_level(struct lp_build_sample_context *bld,
1187bf215546Sopenharmony_ci                          LLVMValueRef level)
1188bf215546Sopenharmony_ci{
1189bf215546Sopenharmony_ci   LLVMValueRef mip_offset = load_mip(bld->gallivm, bld->mip_offsets, level);
1190bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
1191bf215546Sopenharmony_ci   LLVMValueRef data_ptr = LLVMBuildGEP(builder, bld->base_ptr, &mip_offset, 1, "");
1192bf215546Sopenharmony_ci   return data_ptr;
1193bf215546Sopenharmony_ci}
1194bf215546Sopenharmony_ci
1195bf215546Sopenharmony_ci/**
1196bf215546Sopenharmony_ci * Return (per-pixel) offsets to mip levels.
1197bf215546Sopenharmony_ci * \param level  integer mipmap level
1198bf215546Sopenharmony_ci */
1199bf215546Sopenharmony_ciLLVMValueRef
1200bf215546Sopenharmony_cilp_build_get_mip_offsets(struct lp_build_sample_context *bld,
1201bf215546Sopenharmony_ci                         LLVMValueRef level)
1202bf215546Sopenharmony_ci{
1203bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
1204bf215546Sopenharmony_ci   LLVMValueRef offsets, offset1;
1205bf215546Sopenharmony_ci
1206bf215546Sopenharmony_ci   if (bld->num_mips == 1) {
1207bf215546Sopenharmony_ci      offset1 = load_mip(bld->gallivm, bld->mip_offsets, level);
1208bf215546Sopenharmony_ci      offsets = lp_build_broadcast_scalar(&bld->int_coord_bld, offset1);
1209bf215546Sopenharmony_ci   }
1210bf215546Sopenharmony_ci   else if (bld->num_mips == bld->coord_bld.type.length / 4) {
1211bf215546Sopenharmony_ci      unsigned i;
1212bf215546Sopenharmony_ci
1213bf215546Sopenharmony_ci      offsets = bld->int_coord_bld.undef;
1214bf215546Sopenharmony_ci      for (i = 0; i < bld->num_mips; i++) {
1215bf215546Sopenharmony_ci         LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1216bf215546Sopenharmony_ci         offset1 = load_mip(bld->gallivm, bld->mip_offsets, LLVMBuildExtractElement(builder, level, indexi, ""));
1217bf215546Sopenharmony_ci         LLVMValueRef indexo = lp_build_const_int32(bld->gallivm, 4 * i);
1218bf215546Sopenharmony_ci         offsets = LLVMBuildInsertElement(builder, offsets, offset1, indexo, "");
1219bf215546Sopenharmony_ci      }
1220bf215546Sopenharmony_ci      offsets = lp_build_swizzle_scalar_aos(&bld->int_coord_bld, offsets, 0, 4);
1221bf215546Sopenharmony_ci   }
1222bf215546Sopenharmony_ci   else {
1223bf215546Sopenharmony_ci      unsigned i;
1224bf215546Sopenharmony_ci
1225bf215546Sopenharmony_ci      assert (bld->num_mips == bld->coord_bld.type.length);
1226bf215546Sopenharmony_ci
1227bf215546Sopenharmony_ci      offsets = bld->int_coord_bld.undef;
1228bf215546Sopenharmony_ci      for (i = 0; i < bld->num_mips; i++) {
1229bf215546Sopenharmony_ci         LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1230bf215546Sopenharmony_ci         offset1 = load_mip(bld->gallivm, bld->mip_offsets, LLVMBuildExtractElement(builder, level, indexi, ""));
1231bf215546Sopenharmony_ci         offsets = LLVMBuildInsertElement(builder, offsets, offset1, indexi, "");
1232bf215546Sopenharmony_ci      }
1233bf215546Sopenharmony_ci   }
1234bf215546Sopenharmony_ci   return offsets;
1235bf215546Sopenharmony_ci}
1236bf215546Sopenharmony_ci
1237bf215546Sopenharmony_ci
1238bf215546Sopenharmony_ci/**
1239bf215546Sopenharmony_ci * Codegen equivalent for u_minify().
1240bf215546Sopenharmony_ci * @param lod_scalar  if lod is a (broadcasted) scalar
1241bf215546Sopenharmony_ci * Return max(1, base_size >> level);
1242bf215546Sopenharmony_ci */
1243bf215546Sopenharmony_ciLLVMValueRef
1244bf215546Sopenharmony_cilp_build_minify(struct lp_build_context *bld,
1245bf215546Sopenharmony_ci                LLVMValueRef base_size,
1246bf215546Sopenharmony_ci                LLVMValueRef level,
1247bf215546Sopenharmony_ci                boolean lod_scalar)
1248bf215546Sopenharmony_ci{
1249bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
1250bf215546Sopenharmony_ci   assert(lp_check_value(bld->type, base_size));
1251bf215546Sopenharmony_ci   assert(lp_check_value(bld->type, level));
1252bf215546Sopenharmony_ci
1253bf215546Sopenharmony_ci   if (level == bld->zero) {
1254bf215546Sopenharmony_ci      /* if we're using mipmap level zero, no minification is needed */
1255bf215546Sopenharmony_ci      return base_size;
1256bf215546Sopenharmony_ci   }
1257bf215546Sopenharmony_ci   else {
1258bf215546Sopenharmony_ci      LLVMValueRef size;
1259bf215546Sopenharmony_ci      assert(bld->type.sign);
1260bf215546Sopenharmony_ci      if (lod_scalar ||
1261bf215546Sopenharmony_ci         (util_get_cpu_caps()->has_avx2 || !util_get_cpu_caps()->has_sse)) {
1262bf215546Sopenharmony_ci         size = LLVMBuildLShr(builder, base_size, level, "minify");
1263bf215546Sopenharmony_ci         size = lp_build_max(bld, size, bld->one);
1264bf215546Sopenharmony_ci      }
1265bf215546Sopenharmony_ci      else {
1266bf215546Sopenharmony_ci         /*
1267bf215546Sopenharmony_ci          * emulate shift with float mul, since intel "forgot" shifts with
1268bf215546Sopenharmony_ci          * per-element shift count until avx2, which results in terrible
1269bf215546Sopenharmony_ci          * scalar extraction (both count and value), scalar shift,
1270bf215546Sopenharmony_ci          * vector reinsertion. Should not be an issue on any non-x86 cpu
1271bf215546Sopenharmony_ci          * with a vector instruction set.
1272bf215546Sopenharmony_ci          * On cpus with AMD's XOP this should also be unnecessary but I'm
1273bf215546Sopenharmony_ci          * not sure if llvm would emit this with current flags.
1274bf215546Sopenharmony_ci          */
1275bf215546Sopenharmony_ci         LLVMValueRef const127, const23, lf;
1276bf215546Sopenharmony_ci         struct lp_type ftype;
1277bf215546Sopenharmony_ci         struct lp_build_context fbld;
1278bf215546Sopenharmony_ci         ftype = lp_type_float_vec(32, bld->type.length * bld->type.width);
1279bf215546Sopenharmony_ci         lp_build_context_init(&fbld, bld->gallivm, ftype);
1280bf215546Sopenharmony_ci         const127 = lp_build_const_int_vec(bld->gallivm, bld->type, 127);
1281bf215546Sopenharmony_ci         const23 = lp_build_const_int_vec(bld->gallivm, bld->type, 23);
1282bf215546Sopenharmony_ci
1283bf215546Sopenharmony_ci         /* calculate 2^(-level) float */
1284bf215546Sopenharmony_ci         lf = lp_build_sub(bld, const127, level);
1285bf215546Sopenharmony_ci         lf = lp_build_shl(bld, lf, const23);
1286bf215546Sopenharmony_ci         lf = LLVMBuildBitCast(builder, lf, fbld.vec_type, "");
1287bf215546Sopenharmony_ci
1288bf215546Sopenharmony_ci         /* finish shift operation by doing float mul */
1289bf215546Sopenharmony_ci         base_size = lp_build_int_to_float(&fbld, base_size);
1290bf215546Sopenharmony_ci         size = lp_build_mul(&fbld, base_size, lf);
1291bf215546Sopenharmony_ci         /*
1292bf215546Sopenharmony_ci          * do the max also with floats because
1293bf215546Sopenharmony_ci          * a) non-emulated int max requires sse41
1294bf215546Sopenharmony_ci          *    (this is actually a lie as we could cast to 16bit values
1295bf215546Sopenharmony_ci          *    as 16bit is sufficient and 16bit int max is sse2)
1296bf215546Sopenharmony_ci          * b) with avx we can do int max 4-wide but float max 8-wide
1297bf215546Sopenharmony_ci          */
1298bf215546Sopenharmony_ci         size = lp_build_max(&fbld, size, fbld.one);
1299bf215546Sopenharmony_ci         size = lp_build_itrunc(&fbld, size);
1300bf215546Sopenharmony_ci      }
1301bf215546Sopenharmony_ci      return size;
1302bf215546Sopenharmony_ci   }
1303bf215546Sopenharmony_ci}
1304bf215546Sopenharmony_ci
1305bf215546Sopenharmony_ci
1306bf215546Sopenharmony_ci/**
1307bf215546Sopenharmony_ci * Dereference stride_array[mipmap_level] array to get a stride.
1308bf215546Sopenharmony_ci * Return stride as a vector.
1309bf215546Sopenharmony_ci */
1310bf215546Sopenharmony_cistatic LLVMValueRef
1311bf215546Sopenharmony_cilp_build_get_level_stride_vec(struct lp_build_sample_context *bld,
1312bf215546Sopenharmony_ci                              LLVMValueRef stride_array, LLVMValueRef level)
1313bf215546Sopenharmony_ci{
1314bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
1315bf215546Sopenharmony_ci   LLVMValueRef stride, stride1;
1316bf215546Sopenharmony_ci   if (bld->num_mips == 1) {
1317bf215546Sopenharmony_ci      stride1 = load_mip(bld->gallivm, stride_array, level);
1318bf215546Sopenharmony_ci      stride = lp_build_broadcast_scalar(&bld->int_coord_bld, stride1);
1319bf215546Sopenharmony_ci   }
1320bf215546Sopenharmony_ci   else if (bld->num_mips == bld->coord_bld.type.length / 4) {
1321bf215546Sopenharmony_ci      LLVMValueRef stride1;
1322bf215546Sopenharmony_ci      unsigned i;
1323bf215546Sopenharmony_ci
1324bf215546Sopenharmony_ci      stride = bld->int_coord_bld.undef;
1325bf215546Sopenharmony_ci      for (i = 0; i < bld->num_mips; i++) {
1326bf215546Sopenharmony_ci         LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1327bf215546Sopenharmony_ci         stride1 = load_mip(bld->gallivm, stride_array, LLVMBuildExtractElement(builder, level, indexi, ""));
1328bf215546Sopenharmony_ci         LLVMValueRef indexo = lp_build_const_int32(bld->gallivm, 4 * i);
1329bf215546Sopenharmony_ci         stride = LLVMBuildInsertElement(builder, stride, stride1, indexo, "");
1330bf215546Sopenharmony_ci      }
1331bf215546Sopenharmony_ci      stride = lp_build_swizzle_scalar_aos(&bld->int_coord_bld, stride, 0, 4);
1332bf215546Sopenharmony_ci   }
1333bf215546Sopenharmony_ci   else {
1334bf215546Sopenharmony_ci      LLVMValueRef stride1;
1335bf215546Sopenharmony_ci      unsigned i;
1336bf215546Sopenharmony_ci
1337bf215546Sopenharmony_ci      assert (bld->num_mips == bld->coord_bld.type.length);
1338bf215546Sopenharmony_ci
1339bf215546Sopenharmony_ci      stride = bld->int_coord_bld.undef;
1340bf215546Sopenharmony_ci      for (i = 0; i < bld->coord_bld.type.length; i++) {
1341bf215546Sopenharmony_ci         LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1342bf215546Sopenharmony_ci         stride1 = load_mip(bld->gallivm, stride_array, LLVMBuildExtractElement(builder, level, indexi, ""));
1343bf215546Sopenharmony_ci         stride = LLVMBuildInsertElement(builder, stride, stride1, indexi, "");
1344bf215546Sopenharmony_ci      }
1345bf215546Sopenharmony_ci   }
1346bf215546Sopenharmony_ci   return stride;
1347bf215546Sopenharmony_ci}
1348bf215546Sopenharmony_ci
1349bf215546Sopenharmony_ci
1350bf215546Sopenharmony_ci/**
1351bf215546Sopenharmony_ci * When sampling a mipmap, we need to compute the width, height, depth
1352bf215546Sopenharmony_ci * of the source levels from the level indexes.  This helper function
1353bf215546Sopenharmony_ci * does that.
1354bf215546Sopenharmony_ci */
1355bf215546Sopenharmony_civoid
1356bf215546Sopenharmony_cilp_build_mipmap_level_sizes(struct lp_build_sample_context *bld,
1357bf215546Sopenharmony_ci                            LLVMValueRef ilevel,
1358bf215546Sopenharmony_ci                            LLVMValueRef *out_size,
1359bf215546Sopenharmony_ci                            LLVMValueRef *row_stride_vec,
1360bf215546Sopenharmony_ci                            LLVMValueRef *img_stride_vec)
1361bf215546Sopenharmony_ci{
1362bf215546Sopenharmony_ci   const unsigned dims = bld->dims;
1363bf215546Sopenharmony_ci   LLVMValueRef ilevel_vec;
1364bf215546Sopenharmony_ci
1365bf215546Sopenharmony_ci   /*
1366bf215546Sopenharmony_ci    * Compute width, height, depth at mipmap level 'ilevel'
1367bf215546Sopenharmony_ci    */
1368bf215546Sopenharmony_ci   if (bld->num_mips == 1) {
1369bf215546Sopenharmony_ci      ilevel_vec = lp_build_broadcast_scalar(&bld->int_size_bld, ilevel);
1370bf215546Sopenharmony_ci      *out_size = lp_build_minify(&bld->int_size_bld, bld->int_size, ilevel_vec, TRUE);
1371bf215546Sopenharmony_ci   }
1372bf215546Sopenharmony_ci   else {
1373bf215546Sopenharmony_ci      LLVMValueRef int_size_vec;
1374bf215546Sopenharmony_ci      LLVMValueRef tmp[LP_MAX_VECTOR_LENGTH];
1375bf215546Sopenharmony_ci      unsigned num_quads = bld->coord_bld.type.length / 4;
1376bf215546Sopenharmony_ci      unsigned i;
1377bf215546Sopenharmony_ci
1378bf215546Sopenharmony_ci      if (bld->num_mips == num_quads) {
1379bf215546Sopenharmony_ci         /*
1380bf215546Sopenharmony_ci          * XXX: this should be #ifndef SANE_INSTRUCTION_SET.
1381bf215546Sopenharmony_ci          * intel "forgot" the variable shift count instruction until avx2.
1382bf215546Sopenharmony_ci          * A harmless 8x32 shift gets translated into 32 instructions
1383bf215546Sopenharmony_ci          * (16 extracts, 8 scalar shifts, 8 inserts), llvm is apparently
1384bf215546Sopenharmony_ci          * unable to recognize if there are really just 2 different shift
1385bf215546Sopenharmony_ci          * count values. So do the shift 4-wide before expansion.
1386bf215546Sopenharmony_ci          */
1387bf215546Sopenharmony_ci         struct lp_build_context bld4;
1388bf215546Sopenharmony_ci         struct lp_type type4;
1389bf215546Sopenharmony_ci
1390bf215546Sopenharmony_ci         type4 = bld->int_coord_bld.type;
1391bf215546Sopenharmony_ci         type4.length = 4;
1392bf215546Sopenharmony_ci
1393bf215546Sopenharmony_ci         lp_build_context_init(&bld4, bld->gallivm, type4);
1394bf215546Sopenharmony_ci
1395bf215546Sopenharmony_ci         if (bld->dims == 1) {
1396bf215546Sopenharmony_ci            assert(bld->int_size_in_bld.type.length == 1);
1397bf215546Sopenharmony_ci            int_size_vec = lp_build_broadcast_scalar(&bld4,
1398bf215546Sopenharmony_ci                                                     bld->int_size);
1399bf215546Sopenharmony_ci         }
1400bf215546Sopenharmony_ci         else {
1401bf215546Sopenharmony_ci            assert(bld->int_size_in_bld.type.length == 4);
1402bf215546Sopenharmony_ci            int_size_vec = bld->int_size;
1403bf215546Sopenharmony_ci         }
1404bf215546Sopenharmony_ci
1405bf215546Sopenharmony_ci         for (i = 0; i < num_quads; i++) {
1406bf215546Sopenharmony_ci            LLVMValueRef ileveli;
1407bf215546Sopenharmony_ci            LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1408bf215546Sopenharmony_ci
1409bf215546Sopenharmony_ci            ileveli = lp_build_extract_broadcast(bld->gallivm,
1410bf215546Sopenharmony_ci                                                 bld->leveli_bld.type,
1411bf215546Sopenharmony_ci                                                 bld4.type,
1412bf215546Sopenharmony_ci                                                 ilevel,
1413bf215546Sopenharmony_ci                                                 indexi);
1414bf215546Sopenharmony_ci            tmp[i] = lp_build_minify(&bld4, int_size_vec, ileveli, TRUE);
1415bf215546Sopenharmony_ci         }
1416bf215546Sopenharmony_ci         /*
1417bf215546Sopenharmony_ci          * out_size is [w0, h0, d0, _, w1, h1, d1, _, ...] vector for dims > 1,
1418bf215546Sopenharmony_ci          * [w0, w0, w0, w0, w1, w1, w1, w1, ...] otherwise.
1419bf215546Sopenharmony_ci          */
1420bf215546Sopenharmony_ci         *out_size = lp_build_concat(bld->gallivm,
1421bf215546Sopenharmony_ci                                     tmp,
1422bf215546Sopenharmony_ci                                     bld4.type,
1423bf215546Sopenharmony_ci                                     num_quads);
1424bf215546Sopenharmony_ci      }
1425bf215546Sopenharmony_ci      else {
1426bf215546Sopenharmony_ci        /* FIXME: this is terrible and results in _huge_ vector
1427bf215546Sopenharmony_ci         * (for the dims > 1 case).
1428bf215546Sopenharmony_ci         * Should refactor this (together with extract_image_sizes) and do
1429bf215546Sopenharmony_ci         * something more useful. Could for instance if we have width,height
1430bf215546Sopenharmony_ci         * with 4-wide vector pack all elements into a 8xi16 vector
1431bf215546Sopenharmony_ci         * (on which we can still do useful math) instead of using a 16xi32
1432bf215546Sopenharmony_ci         * vector.
1433bf215546Sopenharmony_ci         * For dims == 1 this will create [w0, w1, w2, w3, ...] vector.
1434bf215546Sopenharmony_ci         * For dims > 1 this will create [w0, h0, d0, _, w1, h1, d1, _, ...] vector.
1435bf215546Sopenharmony_ci         */
1436bf215546Sopenharmony_ci         assert(bld->num_mips == bld->coord_bld.type.length);
1437bf215546Sopenharmony_ci         if (bld->dims == 1) {
1438bf215546Sopenharmony_ci            assert(bld->int_size_in_bld.type.length == 1);
1439bf215546Sopenharmony_ci            int_size_vec = lp_build_broadcast_scalar(&bld->int_coord_bld,
1440bf215546Sopenharmony_ci                                                     bld->int_size);
1441bf215546Sopenharmony_ci            *out_size = lp_build_minify(&bld->int_coord_bld, int_size_vec, ilevel, FALSE);
1442bf215546Sopenharmony_ci         }
1443bf215546Sopenharmony_ci         else {
1444bf215546Sopenharmony_ci            LLVMValueRef ilevel1;
1445bf215546Sopenharmony_ci            for (i = 0; i < bld->num_mips; i++) {
1446bf215546Sopenharmony_ci               LLVMValueRef indexi = lp_build_const_int32(bld->gallivm, i);
1447bf215546Sopenharmony_ci               ilevel1 = lp_build_extract_broadcast(bld->gallivm, bld->int_coord_type,
1448bf215546Sopenharmony_ci                                                    bld->int_size_in_bld.type, ilevel, indexi);
1449bf215546Sopenharmony_ci               tmp[i] = bld->int_size;
1450bf215546Sopenharmony_ci               tmp[i] = lp_build_minify(&bld->int_size_in_bld, tmp[i], ilevel1, TRUE);
1451bf215546Sopenharmony_ci            }
1452bf215546Sopenharmony_ci            *out_size = lp_build_concat(bld->gallivm, tmp,
1453bf215546Sopenharmony_ci                                        bld->int_size_in_bld.type,
1454bf215546Sopenharmony_ci                                        bld->num_mips);
1455bf215546Sopenharmony_ci         }
1456bf215546Sopenharmony_ci      }
1457bf215546Sopenharmony_ci   }
1458bf215546Sopenharmony_ci
1459bf215546Sopenharmony_ci   if (dims >= 2) {
1460bf215546Sopenharmony_ci      *row_stride_vec = lp_build_get_level_stride_vec(bld,
1461bf215546Sopenharmony_ci                                                      bld->row_stride_array,
1462bf215546Sopenharmony_ci                                                      ilevel);
1463bf215546Sopenharmony_ci   }
1464bf215546Sopenharmony_ci   if (dims == 3 || has_layer_coord(bld->static_texture_state->target)) {
1465bf215546Sopenharmony_ci      *img_stride_vec = lp_build_get_level_stride_vec(bld,
1466bf215546Sopenharmony_ci                                                      bld->img_stride_array,
1467bf215546Sopenharmony_ci                                                      ilevel);
1468bf215546Sopenharmony_ci   }
1469bf215546Sopenharmony_ci}
1470bf215546Sopenharmony_ci
1471bf215546Sopenharmony_ci
1472bf215546Sopenharmony_ci/**
1473bf215546Sopenharmony_ci * Extract and broadcast texture size.
1474bf215546Sopenharmony_ci *
1475bf215546Sopenharmony_ci * @param size_type   type of the texture size vector (either
1476bf215546Sopenharmony_ci *                    bld->int_size_type or bld->float_size_type)
1477bf215546Sopenharmony_ci * @param coord_type  type of the texture size vector (either
1478bf215546Sopenharmony_ci *                    bld->int_coord_type or bld->coord_type)
1479bf215546Sopenharmony_ci * @param size        vector with the texture size (width, height, depth)
1480bf215546Sopenharmony_ci */
1481bf215546Sopenharmony_civoid
1482bf215546Sopenharmony_cilp_build_extract_image_sizes(struct lp_build_sample_context *bld,
1483bf215546Sopenharmony_ci                             struct lp_build_context *size_bld,
1484bf215546Sopenharmony_ci                             struct lp_type coord_type,
1485bf215546Sopenharmony_ci                             LLVMValueRef size,
1486bf215546Sopenharmony_ci                             LLVMValueRef *out_width,
1487bf215546Sopenharmony_ci                             LLVMValueRef *out_height,
1488bf215546Sopenharmony_ci                             LLVMValueRef *out_depth)
1489bf215546Sopenharmony_ci{
1490bf215546Sopenharmony_ci   const unsigned dims = bld->dims;
1491bf215546Sopenharmony_ci   LLVMTypeRef i32t = LLVMInt32TypeInContext(bld->gallivm->context);
1492bf215546Sopenharmony_ci   struct lp_type size_type = size_bld->type;
1493bf215546Sopenharmony_ci
1494bf215546Sopenharmony_ci   if (bld->num_mips == 1) {
1495bf215546Sopenharmony_ci      *out_width = lp_build_extract_broadcast(bld->gallivm,
1496bf215546Sopenharmony_ci                                              size_type,
1497bf215546Sopenharmony_ci                                              coord_type,
1498bf215546Sopenharmony_ci                                              size,
1499bf215546Sopenharmony_ci                                              LLVMConstInt(i32t, 0, 0));
1500bf215546Sopenharmony_ci      if (dims >= 2) {
1501bf215546Sopenharmony_ci         *out_height = lp_build_extract_broadcast(bld->gallivm,
1502bf215546Sopenharmony_ci                                                  size_type,
1503bf215546Sopenharmony_ci                                                  coord_type,
1504bf215546Sopenharmony_ci                                                  size,
1505bf215546Sopenharmony_ci                                                  LLVMConstInt(i32t, 1, 0));
1506bf215546Sopenharmony_ci         if (dims == 3) {
1507bf215546Sopenharmony_ci            *out_depth = lp_build_extract_broadcast(bld->gallivm,
1508bf215546Sopenharmony_ci                                                    size_type,
1509bf215546Sopenharmony_ci                                                    coord_type,
1510bf215546Sopenharmony_ci                                                    size,
1511bf215546Sopenharmony_ci                                                    LLVMConstInt(i32t, 2, 0));
1512bf215546Sopenharmony_ci         }
1513bf215546Sopenharmony_ci      }
1514bf215546Sopenharmony_ci   }
1515bf215546Sopenharmony_ci   else {
1516bf215546Sopenharmony_ci      unsigned num_quads = bld->coord_bld.type.length / 4;
1517bf215546Sopenharmony_ci
1518bf215546Sopenharmony_ci      if (dims == 1) {
1519bf215546Sopenharmony_ci         *out_width = size;
1520bf215546Sopenharmony_ci      }
1521bf215546Sopenharmony_ci      else if (bld->num_mips == num_quads) {
1522bf215546Sopenharmony_ci         *out_width = lp_build_swizzle_scalar_aos(size_bld, size, 0, 4);
1523bf215546Sopenharmony_ci         if (dims >= 2) {
1524bf215546Sopenharmony_ci            *out_height = lp_build_swizzle_scalar_aos(size_bld, size, 1, 4);
1525bf215546Sopenharmony_ci            if (dims == 3) {
1526bf215546Sopenharmony_ci               *out_depth = lp_build_swizzle_scalar_aos(size_bld, size, 2, 4);
1527bf215546Sopenharmony_ci            }
1528bf215546Sopenharmony_ci         }
1529bf215546Sopenharmony_ci      }
1530bf215546Sopenharmony_ci      else {
1531bf215546Sopenharmony_ci         assert(bld->num_mips == bld->coord_type.length);
1532bf215546Sopenharmony_ci         *out_width = lp_build_pack_aos_scalars(bld->gallivm, size_type,
1533bf215546Sopenharmony_ci                                                coord_type, size, 0);
1534bf215546Sopenharmony_ci         if (dims >= 2) {
1535bf215546Sopenharmony_ci            *out_height = lp_build_pack_aos_scalars(bld->gallivm, size_type,
1536bf215546Sopenharmony_ci                                                    coord_type, size, 1);
1537bf215546Sopenharmony_ci            if (dims == 3) {
1538bf215546Sopenharmony_ci               *out_depth = lp_build_pack_aos_scalars(bld->gallivm, size_type,
1539bf215546Sopenharmony_ci                                                      coord_type, size, 2);
1540bf215546Sopenharmony_ci            }
1541bf215546Sopenharmony_ci         }
1542bf215546Sopenharmony_ci      }
1543bf215546Sopenharmony_ci   }
1544bf215546Sopenharmony_ci}
1545bf215546Sopenharmony_ci
1546bf215546Sopenharmony_ci
1547bf215546Sopenharmony_ci/**
1548bf215546Sopenharmony_ci * Unnormalize coords.
1549bf215546Sopenharmony_ci *
1550bf215546Sopenharmony_ci * @param flt_size  vector with the integer texture size (width, height, depth)
1551bf215546Sopenharmony_ci */
1552bf215546Sopenharmony_civoid
1553bf215546Sopenharmony_cilp_build_unnormalized_coords(struct lp_build_sample_context *bld,
1554bf215546Sopenharmony_ci                             LLVMValueRef flt_size,
1555bf215546Sopenharmony_ci                             LLVMValueRef *s,
1556bf215546Sopenharmony_ci                             LLVMValueRef *t,
1557bf215546Sopenharmony_ci                             LLVMValueRef *r)
1558bf215546Sopenharmony_ci{
1559bf215546Sopenharmony_ci   const unsigned dims = bld->dims;
1560bf215546Sopenharmony_ci   LLVMValueRef width;
1561bf215546Sopenharmony_ci   LLVMValueRef height = NULL;
1562bf215546Sopenharmony_ci   LLVMValueRef depth = NULL;
1563bf215546Sopenharmony_ci
1564bf215546Sopenharmony_ci   lp_build_extract_image_sizes(bld,
1565bf215546Sopenharmony_ci                                &bld->float_size_bld,
1566bf215546Sopenharmony_ci                                bld->coord_type,
1567bf215546Sopenharmony_ci                                flt_size,
1568bf215546Sopenharmony_ci                                &width,
1569bf215546Sopenharmony_ci                                &height,
1570bf215546Sopenharmony_ci                                &depth);
1571bf215546Sopenharmony_ci
1572bf215546Sopenharmony_ci   /* s = s * width, t = t * height */
1573bf215546Sopenharmony_ci   *s = lp_build_mul(&bld->coord_bld, *s, width);
1574bf215546Sopenharmony_ci   if (dims >= 2) {
1575bf215546Sopenharmony_ci      *t = lp_build_mul(&bld->coord_bld, *t, height);
1576bf215546Sopenharmony_ci      if (dims >= 3) {
1577bf215546Sopenharmony_ci         *r = lp_build_mul(&bld->coord_bld, *r, depth);
1578bf215546Sopenharmony_ci      }
1579bf215546Sopenharmony_ci   }
1580bf215546Sopenharmony_ci}
1581bf215546Sopenharmony_ci
1582bf215546Sopenharmony_ci/**
1583bf215546Sopenharmony_ci * Generate new coords and faces for cubemap texels falling off the face.
1584bf215546Sopenharmony_ci *
1585bf215546Sopenharmony_ci * @param face   face (center) of the pixel
1586bf215546Sopenharmony_ci * @param x0     lower x coord
1587bf215546Sopenharmony_ci * @param x1     higher x coord (must be x0 + 1)
1588bf215546Sopenharmony_ci * @param y0     lower y coord
1589bf215546Sopenharmony_ci * @param y1     higher y coord (must be x0 + 1)
1590bf215546Sopenharmony_ci * @param max_coord     texture cube (level) size - 1
1591bf215546Sopenharmony_ci * @param next_faces    new face values when falling off
1592bf215546Sopenharmony_ci * @param next_xcoords  new x coord values when falling off
1593bf215546Sopenharmony_ci * @param next_ycoords  new y coord values when falling off
1594bf215546Sopenharmony_ci *
1595bf215546Sopenharmony_ci * The arrays hold the new values when under/overflow of
1596bf215546Sopenharmony_ci * lower x, higher x, lower y, higher y coord would occur (in this order).
1597bf215546Sopenharmony_ci * next_xcoords/next_ycoords have two entries each (for both new lower and
1598bf215546Sopenharmony_ci * higher coord).
1599bf215546Sopenharmony_ci */
1600bf215546Sopenharmony_civoid
1601bf215546Sopenharmony_cilp_build_cube_new_coords(struct lp_build_context *ivec_bld,
1602bf215546Sopenharmony_ci                        LLVMValueRef face,
1603bf215546Sopenharmony_ci                        LLVMValueRef x0,
1604bf215546Sopenharmony_ci                        LLVMValueRef x1,
1605bf215546Sopenharmony_ci                        LLVMValueRef y0,
1606bf215546Sopenharmony_ci                        LLVMValueRef y1,
1607bf215546Sopenharmony_ci                        LLVMValueRef max_coord,
1608bf215546Sopenharmony_ci                        LLVMValueRef next_faces[4],
1609bf215546Sopenharmony_ci                        LLVMValueRef next_xcoords[4][2],
1610bf215546Sopenharmony_ci                        LLVMValueRef next_ycoords[4][2])
1611bf215546Sopenharmony_ci{
1612bf215546Sopenharmony_ci   /*
1613bf215546Sopenharmony_ci    * Lookup tables aren't nice for simd code hence try some logic here.
1614bf215546Sopenharmony_ci    * (Note that while it would not be necessary to do per-sample (4) lookups
1615bf215546Sopenharmony_ci    * when using a LUT as it's impossible that texels fall off of positive
1616bf215546Sopenharmony_ci    * and negative edges simultaneously, it would however be necessary to
1617bf215546Sopenharmony_ci    * do 2 lookups for corner handling as in this case texels both fall off
1618bf215546Sopenharmony_ci    * of x and y axes.)
1619bf215546Sopenharmony_ci    */
1620bf215546Sopenharmony_ci   /*
1621bf215546Sopenharmony_ci    * Next faces (for face 012345):
1622bf215546Sopenharmony_ci    * x < 0.0  : 451110
1623bf215546Sopenharmony_ci    * x >= 1.0 : 540001
1624bf215546Sopenharmony_ci    * y < 0.0  : 225422
1625bf215546Sopenharmony_ci    * y >= 1.0 : 334533
1626bf215546Sopenharmony_ci    * Hence nfx+ (and nfy+) == nfx- (nfy-) xor 1
1627bf215546Sopenharmony_ci    * nfx-: face > 1 ? (face == 5 ? 0 : 1) : (4 + face & 1)
1628bf215546Sopenharmony_ci    * nfy+: face & ~4 > 1 ? face + 2 : 3;
1629bf215546Sopenharmony_ci    * This could also use pshufb instead, but would need (manually coded)
1630bf215546Sopenharmony_ci    * ssse3 intrinsic (llvm won't do non-constant shuffles).
1631bf215546Sopenharmony_ci    */
1632bf215546Sopenharmony_ci   struct gallivm_state *gallivm = ivec_bld->gallivm;
1633bf215546Sopenharmony_ci   LLVMValueRef sel, sel_f2345, sel_f23, sel_f2, tmpsel, tmp;
1634bf215546Sopenharmony_ci   LLVMValueRef faceand1, sel_fand1, maxmx0, maxmx1, maxmy0, maxmy1;
1635bf215546Sopenharmony_ci   LLVMValueRef c2 = lp_build_const_int_vec(gallivm, ivec_bld->type, 2);
1636bf215546Sopenharmony_ci   LLVMValueRef c3 = lp_build_const_int_vec(gallivm, ivec_bld->type, 3);
1637bf215546Sopenharmony_ci   LLVMValueRef c4 = lp_build_const_int_vec(gallivm, ivec_bld->type, 4);
1638bf215546Sopenharmony_ci   LLVMValueRef c5 = lp_build_const_int_vec(gallivm, ivec_bld->type, 5);
1639bf215546Sopenharmony_ci
1640bf215546Sopenharmony_ci   sel = lp_build_cmp(ivec_bld, PIPE_FUNC_EQUAL, face, c5);
1641bf215546Sopenharmony_ci   tmpsel = lp_build_select(ivec_bld, sel, ivec_bld->zero, ivec_bld->one);
1642bf215546Sopenharmony_ci   sel_f2345 = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, face, ivec_bld->one);
1643bf215546Sopenharmony_ci   faceand1 = lp_build_and(ivec_bld, face, ivec_bld->one);
1644bf215546Sopenharmony_ci   tmp = lp_build_add(ivec_bld, faceand1, c4);
1645bf215546Sopenharmony_ci   next_faces[0] = lp_build_select(ivec_bld, sel_f2345, tmpsel, tmp);
1646bf215546Sopenharmony_ci   next_faces[1] = lp_build_xor(ivec_bld, next_faces[0], ivec_bld->one);
1647bf215546Sopenharmony_ci
1648bf215546Sopenharmony_ci   tmp = lp_build_andnot(ivec_bld, face, c4);
1649bf215546Sopenharmony_ci   sel_f23 = lp_build_cmp(ivec_bld, PIPE_FUNC_GREATER, tmp, ivec_bld->one);
1650bf215546Sopenharmony_ci   tmp = lp_build_add(ivec_bld, face, c2);
1651bf215546Sopenharmony_ci   next_faces[3] = lp_build_select(ivec_bld, sel_f23, tmp, c3);
1652bf215546Sopenharmony_ci   next_faces[2] = lp_build_xor(ivec_bld, next_faces[3], ivec_bld->one);
1653bf215546Sopenharmony_ci
1654bf215546Sopenharmony_ci   /*
1655bf215546Sopenharmony_ci    * new xcoords (for face 012345):
1656bf215546Sopenharmony_ci    * x < 0.0  : max   max   t     max-t max  max
1657bf215546Sopenharmony_ci    * x >= 1.0 : 0     0     max-t t     0    0
1658bf215546Sopenharmony_ci    * y < 0.0  : max   0     max-s s     s    max-s
1659bf215546Sopenharmony_ci    * y >= 1.0 : max   0     s     max-s s    max-s
1660bf215546Sopenharmony_ci    *
1661bf215546Sopenharmony_ci    * ncx[1] = face & ~4 > 1 ? (face == 2 ? max-t : t) : 0
1662bf215546Sopenharmony_ci    * ncx[0] = max - ncx[1]
1663bf215546Sopenharmony_ci    * ncx[3] = face > 1 ? (face & 1 ? max-s : s) : (face & 1) ? 0 : max
1664bf215546Sopenharmony_ci    * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1665bf215546Sopenharmony_ci    */
1666bf215546Sopenharmony_ci   sel_f2 = lp_build_cmp(ivec_bld, PIPE_FUNC_EQUAL, face, c2);
1667bf215546Sopenharmony_ci   maxmy0 = lp_build_sub(ivec_bld, max_coord, y0);
1668bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_f2, maxmy0, y0);
1669bf215546Sopenharmony_ci   next_xcoords[1][0] = lp_build_select(ivec_bld, sel_f23, tmp, ivec_bld->zero);
1670bf215546Sopenharmony_ci   next_xcoords[0][0] = lp_build_sub(ivec_bld, max_coord, next_xcoords[1][0]);
1671bf215546Sopenharmony_ci   maxmy1 = lp_build_sub(ivec_bld, max_coord, y1);
1672bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_f2, maxmy1, y1);
1673bf215546Sopenharmony_ci   next_xcoords[1][1] = lp_build_select(ivec_bld, sel_f23, tmp, ivec_bld->zero);
1674bf215546Sopenharmony_ci   next_xcoords[0][1] = lp_build_sub(ivec_bld, max_coord, next_xcoords[1][1]);
1675bf215546Sopenharmony_ci
1676bf215546Sopenharmony_ci   sel_fand1 = lp_build_cmp(ivec_bld, PIPE_FUNC_EQUAL, faceand1, ivec_bld->one);
1677bf215546Sopenharmony_ci
1678bf215546Sopenharmony_ci   tmpsel = lp_build_select(ivec_bld, sel_fand1, ivec_bld->zero, max_coord);
1679bf215546Sopenharmony_ci   maxmx0 = lp_build_sub(ivec_bld, max_coord, x0);
1680bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_fand1, maxmx0, x0);
1681bf215546Sopenharmony_ci   next_xcoords[3][0] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1682bf215546Sopenharmony_ci   tmp = lp_build_sub(ivec_bld, max_coord, next_xcoords[3][0]);
1683bf215546Sopenharmony_ci   next_xcoords[2][0] = lp_build_select(ivec_bld, sel_f23, tmp, next_xcoords[3][0]);
1684bf215546Sopenharmony_ci   maxmx1 = lp_build_sub(ivec_bld, max_coord, x1);
1685bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_fand1, maxmx1, x1);
1686bf215546Sopenharmony_ci   next_xcoords[3][1] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1687bf215546Sopenharmony_ci   tmp = lp_build_sub(ivec_bld, max_coord, next_xcoords[3][1]);
1688bf215546Sopenharmony_ci   next_xcoords[2][1] = lp_build_select(ivec_bld, sel_f23, tmp, next_xcoords[3][1]);
1689bf215546Sopenharmony_ci
1690bf215546Sopenharmony_ci   /*
1691bf215546Sopenharmony_ci    * new ycoords (for face 012345):
1692bf215546Sopenharmony_ci    * x < 0.0  : t     t     0     max   t    t
1693bf215546Sopenharmony_ci    * x >= 1.0 : t     t     0     max   t    t
1694bf215546Sopenharmony_ci    * y < 0.0  : max-s s     0     max   max  0
1695bf215546Sopenharmony_ci    * y >= 1.0 : s     max-s 0     max   0    max
1696bf215546Sopenharmony_ci    *
1697bf215546Sopenharmony_ci    * ncy[0] = face & ~4 > 1 ? (face == 2 ? 0 : max) : t
1698bf215546Sopenharmony_ci    * ncy[1] = ncy[0]
1699bf215546Sopenharmony_ci    * ncy[3] = face > 1 ? (face & 1 ? max : 0) : (face & 1) ? max-s : max
1700bf215546Sopenharmony_ci    * ncx[2] = face & ~4 > 1 ? max - ncx[3] : ncx[3]
1701bf215546Sopenharmony_ci    */
1702bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_f2, ivec_bld->zero, max_coord);
1703bf215546Sopenharmony_ci   next_ycoords[0][0] = lp_build_select(ivec_bld, sel_f23, tmp, y0);
1704bf215546Sopenharmony_ci   next_ycoords[1][0] = next_ycoords[0][0];
1705bf215546Sopenharmony_ci   next_ycoords[0][1] = lp_build_select(ivec_bld, sel_f23, tmp, y1);
1706bf215546Sopenharmony_ci   next_ycoords[1][1] = next_ycoords[0][1];
1707bf215546Sopenharmony_ci
1708bf215546Sopenharmony_ci   tmpsel = lp_build_select(ivec_bld, sel_fand1, maxmx0, x0);
1709bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_fand1, max_coord, ivec_bld->zero);
1710bf215546Sopenharmony_ci   next_ycoords[3][0] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1711bf215546Sopenharmony_ci   tmp = lp_build_sub(ivec_bld, max_coord, next_ycoords[3][0]);
1712bf215546Sopenharmony_ci   next_ycoords[2][0] = lp_build_select(ivec_bld, sel_f23, next_ycoords[3][0], tmp);
1713bf215546Sopenharmony_ci   tmpsel = lp_build_select(ivec_bld, sel_fand1, maxmx1, x1);
1714bf215546Sopenharmony_ci   tmp = lp_build_select(ivec_bld, sel_fand1, max_coord, ivec_bld->zero);
1715bf215546Sopenharmony_ci   next_ycoords[3][1] = lp_build_select(ivec_bld, sel_f2345, tmp, tmpsel);
1716bf215546Sopenharmony_ci   tmp = lp_build_sub(ivec_bld, max_coord, next_ycoords[3][1]);
1717bf215546Sopenharmony_ci   next_ycoords[2][1] = lp_build_select(ivec_bld, sel_f23, next_ycoords[3][1], tmp);
1718bf215546Sopenharmony_ci}
1719bf215546Sopenharmony_ci
1720bf215546Sopenharmony_ci
1721bf215546Sopenharmony_ci/** Helper used by lp_build_cube_lookup() */
1722bf215546Sopenharmony_cistatic LLVMValueRef
1723bf215546Sopenharmony_cilp_build_cube_imapos(struct lp_build_context *coord_bld, LLVMValueRef coord)
1724bf215546Sopenharmony_ci{
1725bf215546Sopenharmony_ci   /* ima = +0.5 / abs(coord); */
1726bf215546Sopenharmony_ci   LLVMValueRef posHalf = lp_build_const_vec(coord_bld->gallivm, coord_bld->type, 0.5);
1727bf215546Sopenharmony_ci   LLVMValueRef absCoord = lp_build_abs(coord_bld, coord);
1728bf215546Sopenharmony_ci   /* avoid div by zero */
1729bf215546Sopenharmony_ci   LLVMValueRef sel = lp_build_cmp(coord_bld, PIPE_FUNC_GREATER, absCoord, coord_bld->zero);
1730bf215546Sopenharmony_ci   LLVMValueRef div = lp_build_div(coord_bld, posHalf, absCoord);
1731bf215546Sopenharmony_ci   LLVMValueRef ima = lp_build_select(coord_bld, sel, div, coord_bld->zero);
1732bf215546Sopenharmony_ci   return ima;
1733bf215546Sopenharmony_ci}
1734bf215546Sopenharmony_ci
1735bf215546Sopenharmony_ci
1736bf215546Sopenharmony_ci/** Helper for doing 3-wise selection.
1737bf215546Sopenharmony_ci * Returns sel1 ? val2 : (sel0 ? val0 : val1).
1738bf215546Sopenharmony_ci */
1739bf215546Sopenharmony_cistatic LLVMValueRef
1740bf215546Sopenharmony_cilp_build_select3(struct lp_build_context *sel_bld,
1741bf215546Sopenharmony_ci                 LLVMValueRef sel0,
1742bf215546Sopenharmony_ci                 LLVMValueRef sel1,
1743bf215546Sopenharmony_ci                 LLVMValueRef val0,
1744bf215546Sopenharmony_ci                 LLVMValueRef val1,
1745bf215546Sopenharmony_ci                 LLVMValueRef val2)
1746bf215546Sopenharmony_ci{
1747bf215546Sopenharmony_ci   LLVMValueRef tmp;
1748bf215546Sopenharmony_ci   tmp = lp_build_select(sel_bld, sel0, val0, val1);
1749bf215546Sopenharmony_ci   return lp_build_select(sel_bld, sel1, val2, tmp);
1750bf215546Sopenharmony_ci}
1751bf215546Sopenharmony_ci
1752bf215546Sopenharmony_ci
1753bf215546Sopenharmony_ci/**
1754bf215546Sopenharmony_ci * Generate code to do cube face selection and compute per-face texcoords.
1755bf215546Sopenharmony_ci */
1756bf215546Sopenharmony_civoid
1757bf215546Sopenharmony_cilp_build_cube_lookup(struct lp_build_sample_context *bld,
1758bf215546Sopenharmony_ci                     LLVMValueRef *coords,
1759bf215546Sopenharmony_ci                     const struct lp_derivatives *derivs_in, /* optional */
1760bf215546Sopenharmony_ci                     LLVMValueRef *rho,
1761bf215546Sopenharmony_ci                     struct lp_derivatives *derivs_out, /* optional */
1762bf215546Sopenharmony_ci                     boolean need_derivs)
1763bf215546Sopenharmony_ci{
1764bf215546Sopenharmony_ci   struct lp_build_context *coord_bld = &bld->coord_bld;
1765bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
1766bf215546Sopenharmony_ci   struct gallivm_state *gallivm = bld->gallivm;
1767bf215546Sopenharmony_ci   LLVMValueRef si, ti, ri;
1768bf215546Sopenharmony_ci
1769bf215546Sopenharmony_ci   /*
1770bf215546Sopenharmony_ci    * Do per-pixel face selection. We cannot however (as we used to do)
1771bf215546Sopenharmony_ci    * simply calculate the derivs afterwards (which is very bogus for
1772bf215546Sopenharmony_ci    * explicit derivs btw) because the values would be "random" when
1773bf215546Sopenharmony_ci    * not all pixels lie on the same face. So what we do here is just
1774bf215546Sopenharmony_ci    * calculate the derivatives after scaling the coords by the absolute
1775bf215546Sopenharmony_ci    * value of the inverse major axis, and essentially do rho calculation
1776bf215546Sopenharmony_ci    * steps as if it were a 3d texture. This is perfect if all pixels hit
1777bf215546Sopenharmony_ci    * the same face, but not so great at edges, I believe the max error
1778bf215546Sopenharmony_ci    * should be sqrt(2) with no_rho_approx or 2 otherwise (essentially measuring
1779bf215546Sopenharmony_ci    * the 3d distance between 2 points on the cube instead of measuring up/down
1780bf215546Sopenharmony_ci    * the edge). Still this is possibly a win over just selecting the same face
1781bf215546Sopenharmony_ci    * for all pixels. Unfortunately, something like that doesn't work for
1782bf215546Sopenharmony_ci    * explicit derivatives.
1783bf215546Sopenharmony_ci    */
1784bf215546Sopenharmony_ci   struct lp_build_context *cint_bld = &bld->int_coord_bld;
1785bf215546Sopenharmony_ci   struct lp_type intctype = cint_bld->type;
1786bf215546Sopenharmony_ci   LLVMTypeRef coord_vec_type = coord_bld->vec_type;
1787bf215546Sopenharmony_ci   LLVMTypeRef cint_vec_type = cint_bld->vec_type;
1788bf215546Sopenharmony_ci   LLVMValueRef as, at, ar, face, face_s, face_t;
1789bf215546Sopenharmony_ci   LLVMValueRef as_ge_at, maxasat, ar_ge_as_at;
1790bf215546Sopenharmony_ci   LLVMValueRef snewx, tnewx, snewy, tnewy, snewz, tnewz;
1791bf215546Sopenharmony_ci   LLVMValueRef tnegi, rnegi;
1792bf215546Sopenharmony_ci   LLVMValueRef ma, mai, signma, signmabit, imahalfpos;
1793bf215546Sopenharmony_ci   LLVMValueRef posHalf = lp_build_const_vec(gallivm, coord_bld->type, 0.5);
1794bf215546Sopenharmony_ci   LLVMValueRef signmask = lp_build_const_int_vec(gallivm, intctype,
1795bf215546Sopenharmony_ci                                                  1LL << (intctype.width - 1));
1796bf215546Sopenharmony_ci   LLVMValueRef signshift = lp_build_const_int_vec(gallivm, intctype,
1797bf215546Sopenharmony_ci                                                   intctype.width -1);
1798bf215546Sopenharmony_ci   LLVMValueRef facex = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_X);
1799bf215546Sopenharmony_ci   LLVMValueRef facey = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_Y);
1800bf215546Sopenharmony_ci   LLVMValueRef facez = lp_build_const_int_vec(gallivm, intctype, PIPE_TEX_FACE_POS_Z);
1801bf215546Sopenharmony_ci   LLVMValueRef s = coords[0];
1802bf215546Sopenharmony_ci   LLVMValueRef t = coords[1];
1803bf215546Sopenharmony_ci   LLVMValueRef r = coords[2];
1804bf215546Sopenharmony_ci
1805bf215546Sopenharmony_ci   assert(PIPE_TEX_FACE_NEG_X == PIPE_TEX_FACE_POS_X + 1);
1806bf215546Sopenharmony_ci   assert(PIPE_TEX_FACE_NEG_Y == PIPE_TEX_FACE_POS_Y + 1);
1807bf215546Sopenharmony_ci   assert(PIPE_TEX_FACE_NEG_Z == PIPE_TEX_FACE_POS_Z + 1);
1808bf215546Sopenharmony_ci
1809bf215546Sopenharmony_ci   /*
1810bf215546Sopenharmony_ci    * get absolute value (for x/y/z face selection) and sign bit
1811bf215546Sopenharmony_ci    * (for mirroring minor coords and pos/neg face selection)
1812bf215546Sopenharmony_ci    * of the original coords.
1813bf215546Sopenharmony_ci    */
1814bf215546Sopenharmony_ci   as = lp_build_abs(&bld->coord_bld, s);
1815bf215546Sopenharmony_ci   at = lp_build_abs(&bld->coord_bld, t);
1816bf215546Sopenharmony_ci   ar = lp_build_abs(&bld->coord_bld, r);
1817bf215546Sopenharmony_ci
1818bf215546Sopenharmony_ci   /*
1819bf215546Sopenharmony_ci    * major face determination: select x if x > y else select y
1820bf215546Sopenharmony_ci    * select z if z >= max(x,y) else select previous result
1821bf215546Sopenharmony_ci    * if some axis are the same we chose z over y, y over x - the
1822bf215546Sopenharmony_ci    * dx10 spec seems to ask for it while OpenGL doesn't care (if we
1823bf215546Sopenharmony_ci    * wouldn't care could save a select or two if using different
1824bf215546Sopenharmony_ci    * compares and doing at_g_as_ar last since tnewx and tnewz are the
1825bf215546Sopenharmony_ci    * same).
1826bf215546Sopenharmony_ci    */
1827bf215546Sopenharmony_ci   as_ge_at = lp_build_cmp(coord_bld, PIPE_FUNC_GREATER, as, at);
1828bf215546Sopenharmony_ci   maxasat = lp_build_max(coord_bld, as, at);
1829bf215546Sopenharmony_ci   ar_ge_as_at = lp_build_cmp(coord_bld, PIPE_FUNC_GEQUAL, ar, maxasat);
1830bf215546Sopenharmony_ci
1831bf215546Sopenharmony_ci   if (need_derivs) {
1832bf215546Sopenharmony_ci      /*
1833bf215546Sopenharmony_ci       * XXX: This is really really complex.
1834bf215546Sopenharmony_ci       * It is a bit overkill to use this for implicit derivatives as well,
1835bf215546Sopenharmony_ci       * no way this is worth the cost in practice, but seems to be the
1836bf215546Sopenharmony_ci       * only way for getting accurate and per-pixel lod values.
1837bf215546Sopenharmony_ci       */
1838bf215546Sopenharmony_ci      LLVMValueRef ima, imahalf, tmp, ddx[3], ddy[3];
1839bf215546Sopenharmony_ci      LLVMValueRef madx, mady, madxdivma, madydivma;
1840bf215546Sopenharmony_ci      LLVMValueRef sdxi, tdxi, rdxi, sdyi, tdyi, rdyi;
1841bf215546Sopenharmony_ci      LLVMValueRef tdxnegi, rdxnegi, tdynegi, rdynegi;
1842bf215546Sopenharmony_ci      LLVMValueRef sdxnewx, sdxnewy, sdxnewz, tdxnewx, tdxnewy, tdxnewz;
1843bf215546Sopenharmony_ci      LLVMValueRef sdynewx, sdynewy, sdynewz, tdynewx, tdynewy, tdynewz;
1844bf215546Sopenharmony_ci      LLVMValueRef face_sdx, face_tdx, face_sdy, face_tdy;
1845bf215546Sopenharmony_ci      /*
1846bf215546Sopenharmony_ci       * s = 1/2 * ( sc / ma + 1)
1847bf215546Sopenharmony_ci       * t = 1/2 * ( tc / ma + 1)
1848bf215546Sopenharmony_ci       *
1849bf215546Sopenharmony_ci       * s' = 1/2 * (sc' * ma - sc * ma') / ma^2
1850bf215546Sopenharmony_ci       * t' = 1/2 * (tc' * ma - tc * ma') / ma^2
1851bf215546Sopenharmony_ci       *
1852bf215546Sopenharmony_ci       * dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma
1853bf215546Sopenharmony_ci       * dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma
1854bf215546Sopenharmony_ci       * dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma
1855bf215546Sopenharmony_ci       * dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma
1856bf215546Sopenharmony_ci       */
1857bf215546Sopenharmony_ci
1858bf215546Sopenharmony_ci      /* select ma, calculate ima */
1859bf215546Sopenharmony_ci      ma = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, s, t, r);
1860bf215546Sopenharmony_ci      mai = LLVMBuildBitCast(builder, ma, cint_vec_type, "");
1861bf215546Sopenharmony_ci      signmabit = LLVMBuildAnd(builder, mai, signmask, "");
1862bf215546Sopenharmony_ci      ima = lp_build_div(coord_bld, coord_bld->one, ma);
1863bf215546Sopenharmony_ci      imahalf = lp_build_mul(coord_bld, posHalf, ima);
1864bf215546Sopenharmony_ci      imahalfpos = lp_build_abs(coord_bld, imahalf);
1865bf215546Sopenharmony_ci
1866bf215546Sopenharmony_ci      if (!derivs_in) {
1867bf215546Sopenharmony_ci         ddx[0] = lp_build_ddx(coord_bld, s);
1868bf215546Sopenharmony_ci         ddx[1] = lp_build_ddx(coord_bld, t);
1869bf215546Sopenharmony_ci         ddx[2] = lp_build_ddx(coord_bld, r);
1870bf215546Sopenharmony_ci         ddy[0] = lp_build_ddy(coord_bld, s);
1871bf215546Sopenharmony_ci         ddy[1] = lp_build_ddy(coord_bld, t);
1872bf215546Sopenharmony_ci         ddy[2] = lp_build_ddy(coord_bld, r);
1873bf215546Sopenharmony_ci      }
1874bf215546Sopenharmony_ci      else {
1875bf215546Sopenharmony_ci         ddx[0] = derivs_in->ddx[0];
1876bf215546Sopenharmony_ci         ddx[1] = derivs_in->ddx[1];
1877bf215546Sopenharmony_ci         ddx[2] = derivs_in->ddx[2];
1878bf215546Sopenharmony_ci         ddy[0] = derivs_in->ddy[0];
1879bf215546Sopenharmony_ci         ddy[1] = derivs_in->ddy[1];
1880bf215546Sopenharmony_ci         ddy[2] = derivs_in->ddy[2];
1881bf215546Sopenharmony_ci      }
1882bf215546Sopenharmony_ci
1883bf215546Sopenharmony_ci      /* select major derivatives */
1884bf215546Sopenharmony_ci      madx = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, ddx[0], ddx[1], ddx[2]);
1885bf215546Sopenharmony_ci      mady = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, ddy[0], ddy[1], ddy[2]);
1886bf215546Sopenharmony_ci
1887bf215546Sopenharmony_ci      si = LLVMBuildBitCast(builder, s, cint_vec_type, "");
1888bf215546Sopenharmony_ci      ti = LLVMBuildBitCast(builder, t, cint_vec_type, "");
1889bf215546Sopenharmony_ci      ri = LLVMBuildBitCast(builder, r, cint_vec_type, "");
1890bf215546Sopenharmony_ci
1891bf215546Sopenharmony_ci      sdxi = LLVMBuildBitCast(builder, ddx[0], cint_vec_type, "");
1892bf215546Sopenharmony_ci      tdxi = LLVMBuildBitCast(builder, ddx[1], cint_vec_type, "");
1893bf215546Sopenharmony_ci      rdxi = LLVMBuildBitCast(builder, ddx[2], cint_vec_type, "");
1894bf215546Sopenharmony_ci
1895bf215546Sopenharmony_ci      sdyi = LLVMBuildBitCast(builder, ddy[0], cint_vec_type, "");
1896bf215546Sopenharmony_ci      tdyi = LLVMBuildBitCast(builder, ddy[1], cint_vec_type, "");
1897bf215546Sopenharmony_ci      rdyi = LLVMBuildBitCast(builder, ddy[2], cint_vec_type, "");
1898bf215546Sopenharmony_ci
1899bf215546Sopenharmony_ci      /*
1900bf215546Sopenharmony_ci       * compute all possible new s/t coords, which does the mirroring,
1901bf215546Sopenharmony_ci       * and do the same for derivs minor axes.
1902bf215546Sopenharmony_ci       * snewx = signma * -r;
1903bf215546Sopenharmony_ci       * tnewx = -t;
1904bf215546Sopenharmony_ci       * snewy = s;
1905bf215546Sopenharmony_ci       * tnewy = signma * r;
1906bf215546Sopenharmony_ci       * snewz = signma * s;
1907bf215546Sopenharmony_ci       * tnewz = -t;
1908bf215546Sopenharmony_ci       */
1909bf215546Sopenharmony_ci      tnegi = LLVMBuildXor(builder, ti, signmask, "");
1910bf215546Sopenharmony_ci      rnegi = LLVMBuildXor(builder, ri, signmask, "");
1911bf215546Sopenharmony_ci      tdxnegi = LLVMBuildXor(builder, tdxi, signmask, "");
1912bf215546Sopenharmony_ci      rdxnegi = LLVMBuildXor(builder, rdxi, signmask, "");
1913bf215546Sopenharmony_ci      tdynegi = LLVMBuildXor(builder, tdyi, signmask, "");
1914bf215546Sopenharmony_ci      rdynegi = LLVMBuildXor(builder, rdyi, signmask, "");
1915bf215546Sopenharmony_ci
1916bf215546Sopenharmony_ci      snewx = LLVMBuildXor(builder, signmabit, rnegi, "");
1917bf215546Sopenharmony_ci      tnewx = tnegi;
1918bf215546Sopenharmony_ci      sdxnewx = LLVMBuildXor(builder, signmabit, rdxnegi, "");
1919bf215546Sopenharmony_ci      tdxnewx = tdxnegi;
1920bf215546Sopenharmony_ci      sdynewx = LLVMBuildXor(builder, signmabit, rdynegi, "");
1921bf215546Sopenharmony_ci      tdynewx = tdynegi;
1922bf215546Sopenharmony_ci
1923bf215546Sopenharmony_ci      snewy = si;
1924bf215546Sopenharmony_ci      tnewy = LLVMBuildXor(builder, signmabit, ri, "");
1925bf215546Sopenharmony_ci      sdxnewy = sdxi;
1926bf215546Sopenharmony_ci      tdxnewy = LLVMBuildXor(builder, signmabit, rdxi, "");
1927bf215546Sopenharmony_ci      sdynewy = sdyi;
1928bf215546Sopenharmony_ci      tdynewy = LLVMBuildXor(builder, signmabit, rdyi, "");
1929bf215546Sopenharmony_ci
1930bf215546Sopenharmony_ci      snewz = LLVMBuildXor(builder, signmabit, si, "");
1931bf215546Sopenharmony_ci      tnewz = tnegi;
1932bf215546Sopenharmony_ci      sdxnewz = LLVMBuildXor(builder, signmabit, sdxi, "");
1933bf215546Sopenharmony_ci      tdxnewz = tdxnegi;
1934bf215546Sopenharmony_ci      sdynewz = LLVMBuildXor(builder, signmabit, sdyi, "");
1935bf215546Sopenharmony_ci      tdynewz = tdynegi;
1936bf215546Sopenharmony_ci
1937bf215546Sopenharmony_ci      /* select the mirrored values */
1938bf215546Sopenharmony_ci      face = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, facex, facey, facez);
1939bf215546Sopenharmony_ci      face_s = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, snewx, snewy, snewz);
1940bf215546Sopenharmony_ci      face_t = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tnewx, tnewy, tnewz);
1941bf215546Sopenharmony_ci      face_sdx = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, sdxnewx, sdxnewy, sdxnewz);
1942bf215546Sopenharmony_ci      face_tdx = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tdxnewx, tdxnewy, tdxnewz);
1943bf215546Sopenharmony_ci      face_sdy = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, sdynewx, sdynewy, sdynewz);
1944bf215546Sopenharmony_ci      face_tdy = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tdynewx, tdynewy, tdynewz);
1945bf215546Sopenharmony_ci
1946bf215546Sopenharmony_ci      face_s = LLVMBuildBitCast(builder, face_s, coord_vec_type, "");
1947bf215546Sopenharmony_ci      face_t = LLVMBuildBitCast(builder, face_t, coord_vec_type, "");
1948bf215546Sopenharmony_ci      face_sdx = LLVMBuildBitCast(builder, face_sdx, coord_vec_type, "");
1949bf215546Sopenharmony_ci      face_tdx = LLVMBuildBitCast(builder, face_tdx, coord_vec_type, "");
1950bf215546Sopenharmony_ci      face_sdy = LLVMBuildBitCast(builder, face_sdy, coord_vec_type, "");
1951bf215546Sopenharmony_ci      face_tdy = LLVMBuildBitCast(builder, face_tdy, coord_vec_type, "");
1952bf215546Sopenharmony_ci
1953bf215546Sopenharmony_ci      /* deriv math, dx.s = 0.5 * (dx.sc - sc * dx.ma / ma) / ma */
1954bf215546Sopenharmony_ci      madxdivma = lp_build_mul(coord_bld, madx, ima);
1955bf215546Sopenharmony_ci      tmp = lp_build_mul(coord_bld, madxdivma, face_s);
1956bf215546Sopenharmony_ci      tmp = lp_build_sub(coord_bld, face_sdx, tmp);
1957bf215546Sopenharmony_ci      derivs_out->ddx[0] = lp_build_mul(coord_bld, tmp, imahalf);
1958bf215546Sopenharmony_ci
1959bf215546Sopenharmony_ci      /* dx.t = 0.5 * (dx.tc - tc * dx.ma / ma) / ma */
1960bf215546Sopenharmony_ci      tmp = lp_build_mul(coord_bld, madxdivma, face_t);
1961bf215546Sopenharmony_ci      tmp = lp_build_sub(coord_bld, face_tdx, tmp);
1962bf215546Sopenharmony_ci      derivs_out->ddx[1] = lp_build_mul(coord_bld, tmp, imahalf);
1963bf215546Sopenharmony_ci
1964bf215546Sopenharmony_ci      /* dy.s = 0.5 * (dy.sc - sc * dy.ma / ma) / ma */
1965bf215546Sopenharmony_ci      madydivma = lp_build_mul(coord_bld, mady, ima);
1966bf215546Sopenharmony_ci      tmp = lp_build_mul(coord_bld, madydivma, face_s);
1967bf215546Sopenharmony_ci      tmp = lp_build_sub(coord_bld, face_sdy, tmp);
1968bf215546Sopenharmony_ci      derivs_out->ddy[0] = lp_build_mul(coord_bld, tmp, imahalf);
1969bf215546Sopenharmony_ci
1970bf215546Sopenharmony_ci      /* dy.t = 0.5 * (dy.tc - tc * dy.ma / ma) / ma */
1971bf215546Sopenharmony_ci      tmp = lp_build_mul(coord_bld, madydivma, face_t);
1972bf215546Sopenharmony_ci      tmp = lp_build_sub(coord_bld, face_tdy, tmp);
1973bf215546Sopenharmony_ci      derivs_out->ddy[1] = lp_build_mul(coord_bld, tmp, imahalf);
1974bf215546Sopenharmony_ci
1975bf215546Sopenharmony_ci      signma = LLVMBuildLShr(builder, mai, signshift, "");
1976bf215546Sopenharmony_ci      coords[2] = LLVMBuildOr(builder, face, signma, "face");
1977bf215546Sopenharmony_ci
1978bf215546Sopenharmony_ci      /* project coords */
1979bf215546Sopenharmony_ci      face_s = lp_build_mul(coord_bld, face_s, imahalfpos);
1980bf215546Sopenharmony_ci      face_t = lp_build_mul(coord_bld, face_t, imahalfpos);
1981bf215546Sopenharmony_ci
1982bf215546Sopenharmony_ci      coords[0] = lp_build_add(coord_bld, face_s, posHalf);
1983bf215546Sopenharmony_ci      coords[1] = lp_build_add(coord_bld, face_t, posHalf);
1984bf215546Sopenharmony_ci
1985bf215546Sopenharmony_ci      return;
1986bf215546Sopenharmony_ci   }
1987bf215546Sopenharmony_ci
1988bf215546Sopenharmony_ci   ma = lp_build_select3(coord_bld, as_ge_at, ar_ge_as_at, s, t, r);
1989bf215546Sopenharmony_ci   mai = LLVMBuildBitCast(builder, ma, cint_vec_type, "");
1990bf215546Sopenharmony_ci   signmabit = LLVMBuildAnd(builder, mai, signmask, "");
1991bf215546Sopenharmony_ci
1992bf215546Sopenharmony_ci   si = LLVMBuildBitCast(builder, s, cint_vec_type, "");
1993bf215546Sopenharmony_ci   ti = LLVMBuildBitCast(builder, t, cint_vec_type, "");
1994bf215546Sopenharmony_ci   ri = LLVMBuildBitCast(builder, r, cint_vec_type, "");
1995bf215546Sopenharmony_ci
1996bf215546Sopenharmony_ci   /*
1997bf215546Sopenharmony_ci    * compute all possible new s/t coords, which does the mirroring
1998bf215546Sopenharmony_ci    * snewx = signma * -r;
1999bf215546Sopenharmony_ci    * tnewx = -t;
2000bf215546Sopenharmony_ci    * snewy = s;
2001bf215546Sopenharmony_ci    * tnewy = signma * r;
2002bf215546Sopenharmony_ci    * snewz = signma * s;
2003bf215546Sopenharmony_ci    * tnewz = -t;
2004bf215546Sopenharmony_ci    */
2005bf215546Sopenharmony_ci   tnegi = LLVMBuildXor(builder, ti, signmask, "");
2006bf215546Sopenharmony_ci   rnegi = LLVMBuildXor(builder, ri, signmask, "");
2007bf215546Sopenharmony_ci
2008bf215546Sopenharmony_ci   snewx = LLVMBuildXor(builder, signmabit, rnegi, "");
2009bf215546Sopenharmony_ci   tnewx = tnegi;
2010bf215546Sopenharmony_ci
2011bf215546Sopenharmony_ci   snewy = si;
2012bf215546Sopenharmony_ci   tnewy = LLVMBuildXor(builder, signmabit, ri, "");
2013bf215546Sopenharmony_ci
2014bf215546Sopenharmony_ci   snewz = LLVMBuildXor(builder, signmabit, si, "");
2015bf215546Sopenharmony_ci   tnewz = tnegi;
2016bf215546Sopenharmony_ci
2017bf215546Sopenharmony_ci   /* select the mirrored values */
2018bf215546Sopenharmony_ci   face_s = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, snewx, snewy, snewz);
2019bf215546Sopenharmony_ci   face_t = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, tnewx, tnewy, tnewz);
2020bf215546Sopenharmony_ci   face = lp_build_select3(cint_bld, as_ge_at, ar_ge_as_at, facex, facey, facez);
2021bf215546Sopenharmony_ci
2022bf215546Sopenharmony_ci   face_s = LLVMBuildBitCast(builder, face_s, coord_vec_type, "");
2023bf215546Sopenharmony_ci   face_t = LLVMBuildBitCast(builder, face_t, coord_vec_type, "");
2024bf215546Sopenharmony_ci
2025bf215546Sopenharmony_ci   /* add +1 for neg face */
2026bf215546Sopenharmony_ci   /* XXX with AVX probably want to use another select here -
2027bf215546Sopenharmony_ci    * as long as we ensure vblendvps gets used we can actually
2028bf215546Sopenharmony_ci    * skip the comparison and just use sign as a "mask" directly.
2029bf215546Sopenharmony_ci    */
2030bf215546Sopenharmony_ci   signma = LLVMBuildLShr(builder, mai, signshift, "");
2031bf215546Sopenharmony_ci   coords[2] = LLVMBuildOr(builder, face, signma, "face");
2032bf215546Sopenharmony_ci
2033bf215546Sopenharmony_ci   /* project coords */
2034bf215546Sopenharmony_ci   if (!need_derivs) {
2035bf215546Sopenharmony_ci      imahalfpos = lp_build_cube_imapos(coord_bld, ma);
2036bf215546Sopenharmony_ci      face_s = lp_build_mul(coord_bld, face_s, imahalfpos);
2037bf215546Sopenharmony_ci      face_t = lp_build_mul(coord_bld, face_t, imahalfpos);
2038bf215546Sopenharmony_ci   }
2039bf215546Sopenharmony_ci
2040bf215546Sopenharmony_ci   coords[0] = lp_build_add(coord_bld, face_s, posHalf);
2041bf215546Sopenharmony_ci   coords[1] = lp_build_add(coord_bld, face_t, posHalf);
2042bf215546Sopenharmony_ci}
2043bf215546Sopenharmony_ci
2044bf215546Sopenharmony_ci
2045bf215546Sopenharmony_ci/**
2046bf215546Sopenharmony_ci * Compute the partial offset of a pixel block along an arbitrary axis.
2047bf215546Sopenharmony_ci *
2048bf215546Sopenharmony_ci * @param coord   coordinate in pixels
2049bf215546Sopenharmony_ci * @param stride  number of bytes between rows of successive pixel blocks
2050bf215546Sopenharmony_ci * @param block_length  number of pixels in a pixels block along the coordinate
2051bf215546Sopenharmony_ci *                      axis
2052bf215546Sopenharmony_ci * @param out_offset    resulting relative offset of the pixel block in bytes
2053bf215546Sopenharmony_ci * @param out_subcoord  resulting sub-block pixel coordinate
2054bf215546Sopenharmony_ci */
2055bf215546Sopenharmony_civoid
2056bf215546Sopenharmony_cilp_build_sample_partial_offset(struct lp_build_context *bld,
2057bf215546Sopenharmony_ci                               unsigned block_length,
2058bf215546Sopenharmony_ci                               LLVMValueRef coord,
2059bf215546Sopenharmony_ci                               LLVMValueRef stride,
2060bf215546Sopenharmony_ci                               LLVMValueRef *out_offset,
2061bf215546Sopenharmony_ci                               LLVMValueRef *out_subcoord)
2062bf215546Sopenharmony_ci{
2063bf215546Sopenharmony_ci   LLVMBuilderRef builder = bld->gallivm->builder;
2064bf215546Sopenharmony_ci   LLVMValueRef offset;
2065bf215546Sopenharmony_ci   LLVMValueRef subcoord;
2066bf215546Sopenharmony_ci
2067bf215546Sopenharmony_ci   if (block_length == 1) {
2068bf215546Sopenharmony_ci      subcoord = bld->zero;
2069bf215546Sopenharmony_ci   }
2070bf215546Sopenharmony_ci   else {
2071bf215546Sopenharmony_ci      /*
2072bf215546Sopenharmony_ci       * Pixel blocks have power of two dimensions. LLVM should convert the
2073bf215546Sopenharmony_ci       * rem/div to bit arithmetic.
2074bf215546Sopenharmony_ci       * TODO: Verify this.
2075bf215546Sopenharmony_ci       * It does indeed BUT it does transform it to scalar (and back) when doing so
2076bf215546Sopenharmony_ci       * (using roughly extract, shift/and, mov, unpack) (llvm 2.7).
2077bf215546Sopenharmony_ci       * The generated code looks seriously unfunny and is quite expensive.
2078bf215546Sopenharmony_ci       */
2079bf215546Sopenharmony_ci#if 0
2080bf215546Sopenharmony_ci      LLVMValueRef block_width = lp_build_const_int_vec(bld->type, block_length);
2081bf215546Sopenharmony_ci      subcoord = LLVMBuildURem(builder, coord, block_width, "");
2082bf215546Sopenharmony_ci      coord    = LLVMBuildUDiv(builder, coord, block_width, "");
2083bf215546Sopenharmony_ci#else
2084bf215546Sopenharmony_ci      unsigned logbase2 = util_logbase2(block_length);
2085bf215546Sopenharmony_ci      LLVMValueRef block_shift = lp_build_const_int_vec(bld->gallivm, bld->type, logbase2);
2086bf215546Sopenharmony_ci      LLVMValueRef block_mask = lp_build_const_int_vec(bld->gallivm, bld->type, block_length - 1);
2087bf215546Sopenharmony_ci      subcoord = LLVMBuildAnd(builder, coord, block_mask, "");
2088bf215546Sopenharmony_ci      coord = LLVMBuildLShr(builder, coord, block_shift, "");
2089bf215546Sopenharmony_ci#endif
2090bf215546Sopenharmony_ci   }
2091bf215546Sopenharmony_ci
2092bf215546Sopenharmony_ci   offset = lp_build_mul(bld, coord, stride);
2093bf215546Sopenharmony_ci
2094bf215546Sopenharmony_ci   assert(out_offset);
2095bf215546Sopenharmony_ci   assert(out_subcoord);
2096bf215546Sopenharmony_ci
2097bf215546Sopenharmony_ci   *out_offset = offset;
2098bf215546Sopenharmony_ci   *out_subcoord = subcoord;
2099bf215546Sopenharmony_ci}
2100bf215546Sopenharmony_ci
2101bf215546Sopenharmony_ci
2102bf215546Sopenharmony_ci/**
2103bf215546Sopenharmony_ci * Compute the offset of a pixel block.
2104bf215546Sopenharmony_ci *
2105bf215546Sopenharmony_ci * x, y, z, y_stride, z_stride are vectors, and they refer to pixels.
2106bf215546Sopenharmony_ci *
2107bf215546Sopenharmony_ci * Returns the relative offset and i,j sub-block coordinates
2108bf215546Sopenharmony_ci */
2109bf215546Sopenharmony_civoid
2110bf215546Sopenharmony_cilp_build_sample_offset(struct lp_build_context *bld,
2111bf215546Sopenharmony_ci                       const struct util_format_description *format_desc,
2112bf215546Sopenharmony_ci                       LLVMValueRef x,
2113bf215546Sopenharmony_ci                       LLVMValueRef y,
2114bf215546Sopenharmony_ci                       LLVMValueRef z,
2115bf215546Sopenharmony_ci                       LLVMValueRef y_stride,
2116bf215546Sopenharmony_ci                       LLVMValueRef z_stride,
2117bf215546Sopenharmony_ci                       LLVMValueRef *out_offset,
2118bf215546Sopenharmony_ci                       LLVMValueRef *out_i,
2119bf215546Sopenharmony_ci                       LLVMValueRef *out_j)
2120bf215546Sopenharmony_ci{
2121bf215546Sopenharmony_ci   LLVMValueRef x_stride;
2122bf215546Sopenharmony_ci   LLVMValueRef offset;
2123bf215546Sopenharmony_ci
2124bf215546Sopenharmony_ci   x_stride = lp_build_const_vec(bld->gallivm, bld->type,
2125bf215546Sopenharmony_ci                                 format_desc->block.bits/8);
2126bf215546Sopenharmony_ci
2127bf215546Sopenharmony_ci   lp_build_sample_partial_offset(bld,
2128bf215546Sopenharmony_ci                                  format_desc->block.width,
2129bf215546Sopenharmony_ci                                  x, x_stride,
2130bf215546Sopenharmony_ci                                  &offset, out_i);
2131bf215546Sopenharmony_ci
2132bf215546Sopenharmony_ci   if (y && y_stride) {
2133bf215546Sopenharmony_ci      LLVMValueRef y_offset;
2134bf215546Sopenharmony_ci      lp_build_sample_partial_offset(bld,
2135bf215546Sopenharmony_ci                                     format_desc->block.height,
2136bf215546Sopenharmony_ci                                     y, y_stride,
2137bf215546Sopenharmony_ci                                     &y_offset, out_j);
2138bf215546Sopenharmony_ci      offset = lp_build_add(bld, offset, y_offset);
2139bf215546Sopenharmony_ci   }
2140bf215546Sopenharmony_ci   else {
2141bf215546Sopenharmony_ci      *out_j = bld->zero;
2142bf215546Sopenharmony_ci   }
2143bf215546Sopenharmony_ci
2144bf215546Sopenharmony_ci   if (z && z_stride) {
2145bf215546Sopenharmony_ci      LLVMValueRef z_offset;
2146bf215546Sopenharmony_ci      LLVMValueRef k;
2147bf215546Sopenharmony_ci      lp_build_sample_partial_offset(bld,
2148bf215546Sopenharmony_ci                                     1, /* pixel blocks are always 2D */
2149bf215546Sopenharmony_ci                                     z, z_stride,
2150bf215546Sopenharmony_ci                                     &z_offset, &k);
2151bf215546Sopenharmony_ci      offset = lp_build_add(bld, offset, z_offset);
2152bf215546Sopenharmony_ci   }
2153bf215546Sopenharmony_ci
2154bf215546Sopenharmony_ci   *out_offset = offset;
2155bf215546Sopenharmony_ci}
2156bf215546Sopenharmony_ci
2157bf215546Sopenharmony_cistatic LLVMValueRef
2158bf215546Sopenharmony_cilp_build_sample_min(struct lp_build_context *bld,
2159bf215546Sopenharmony_ci                    LLVMValueRef x,
2160bf215546Sopenharmony_ci                    LLVMValueRef v0,
2161bf215546Sopenharmony_ci                    LLVMValueRef v1)
2162bf215546Sopenharmony_ci{
2163bf215546Sopenharmony_ci   /* if the incoming LERP weight is 0 then the min/max
2164bf215546Sopenharmony_ci    * should ignore that value. */
2165bf215546Sopenharmony_ci   LLVMValueRef mask = lp_build_compare(bld->gallivm,
2166bf215546Sopenharmony_ci                                        bld->type,
2167bf215546Sopenharmony_ci                                        PIPE_FUNC_NOTEQUAL,
2168bf215546Sopenharmony_ci                                        x, bld->zero);
2169bf215546Sopenharmony_ci   LLVMValueRef min = lp_build_min(bld, v0, v1);
2170bf215546Sopenharmony_ci
2171bf215546Sopenharmony_ci   return lp_build_select(bld, mask, min, v0);
2172bf215546Sopenharmony_ci}
2173bf215546Sopenharmony_ci
2174bf215546Sopenharmony_cistatic LLVMValueRef
2175bf215546Sopenharmony_cilp_build_sample_max(struct lp_build_context *bld,
2176bf215546Sopenharmony_ci                    LLVMValueRef x,
2177bf215546Sopenharmony_ci                    LLVMValueRef v0,
2178bf215546Sopenharmony_ci                    LLVMValueRef v1)
2179bf215546Sopenharmony_ci{
2180bf215546Sopenharmony_ci   /* if the incoming LERP weight is 0 then the min/max
2181bf215546Sopenharmony_ci    * should ignore that value. */
2182bf215546Sopenharmony_ci   LLVMValueRef mask = lp_build_compare(bld->gallivm,
2183bf215546Sopenharmony_ci                                        bld->type,
2184bf215546Sopenharmony_ci                                        PIPE_FUNC_NOTEQUAL,
2185bf215546Sopenharmony_ci                                        x, bld->zero);
2186bf215546Sopenharmony_ci   LLVMValueRef max = lp_build_max(bld, v0, v1);
2187bf215546Sopenharmony_ci
2188bf215546Sopenharmony_ci   return lp_build_select(bld, mask, max, v0);
2189bf215546Sopenharmony_ci}
2190bf215546Sopenharmony_ci
2191bf215546Sopenharmony_cistatic LLVMValueRef
2192bf215546Sopenharmony_cilp_build_sample_min_2d(struct lp_build_context *bld,
2193bf215546Sopenharmony_ci                       LLVMValueRef x,
2194bf215546Sopenharmony_ci                       LLVMValueRef y,
2195bf215546Sopenharmony_ci                       LLVMValueRef a,
2196bf215546Sopenharmony_ci                       LLVMValueRef b,
2197bf215546Sopenharmony_ci                       LLVMValueRef c,
2198bf215546Sopenharmony_ci                       LLVMValueRef d)
2199bf215546Sopenharmony_ci{
2200bf215546Sopenharmony_ci   LLVMValueRef v0 = lp_build_sample_min(bld, x, a, b);
2201bf215546Sopenharmony_ci   LLVMValueRef v1 = lp_build_sample_min(bld, x, c, d);
2202bf215546Sopenharmony_ci   return lp_build_sample_min(bld, y, v0, v1);
2203bf215546Sopenharmony_ci}
2204bf215546Sopenharmony_ci
2205bf215546Sopenharmony_cistatic LLVMValueRef
2206bf215546Sopenharmony_cilp_build_sample_max_2d(struct lp_build_context *bld,
2207bf215546Sopenharmony_ci                       LLVMValueRef x,
2208bf215546Sopenharmony_ci                       LLVMValueRef y,
2209bf215546Sopenharmony_ci                       LLVMValueRef a,
2210bf215546Sopenharmony_ci                       LLVMValueRef b,
2211bf215546Sopenharmony_ci                       LLVMValueRef c,
2212bf215546Sopenharmony_ci                       LLVMValueRef d)
2213bf215546Sopenharmony_ci{
2214bf215546Sopenharmony_ci   LLVMValueRef v0 = lp_build_sample_max(bld, x, a, b);
2215bf215546Sopenharmony_ci   LLVMValueRef v1 = lp_build_sample_max(bld, x, c, d);
2216bf215546Sopenharmony_ci   return lp_build_sample_max(bld, y, v0, v1);
2217bf215546Sopenharmony_ci}
2218bf215546Sopenharmony_ci
2219bf215546Sopenharmony_cistatic LLVMValueRef
2220bf215546Sopenharmony_cilp_build_sample_min_3d(struct lp_build_context *bld,
2221bf215546Sopenharmony_ci                LLVMValueRef x,
2222bf215546Sopenharmony_ci                LLVMValueRef y,
2223bf215546Sopenharmony_ci                LLVMValueRef z,
2224bf215546Sopenharmony_ci                LLVMValueRef a, LLVMValueRef b,
2225bf215546Sopenharmony_ci                LLVMValueRef c, LLVMValueRef d,
2226bf215546Sopenharmony_ci                LLVMValueRef e, LLVMValueRef f,
2227bf215546Sopenharmony_ci                LLVMValueRef g, LLVMValueRef h)
2228bf215546Sopenharmony_ci{
2229bf215546Sopenharmony_ci   LLVMValueRef v0 = lp_build_sample_min_2d(bld, x, y, a, b, c, d);
2230bf215546Sopenharmony_ci   LLVMValueRef v1 = lp_build_sample_min_2d(bld, x, y, e, f, g, h);
2231bf215546Sopenharmony_ci   return lp_build_sample_min(bld, z, v0, v1);
2232bf215546Sopenharmony_ci}
2233bf215546Sopenharmony_ci
2234bf215546Sopenharmony_cistatic LLVMValueRef
2235bf215546Sopenharmony_cilp_build_sample_max_3d(struct lp_build_context *bld,
2236bf215546Sopenharmony_ci                       LLVMValueRef x,
2237bf215546Sopenharmony_ci                       LLVMValueRef y,
2238bf215546Sopenharmony_ci                       LLVMValueRef z,
2239bf215546Sopenharmony_ci                       LLVMValueRef a, LLVMValueRef b,
2240bf215546Sopenharmony_ci                       LLVMValueRef c, LLVMValueRef d,
2241bf215546Sopenharmony_ci                       LLVMValueRef e, LLVMValueRef f,
2242bf215546Sopenharmony_ci                       LLVMValueRef g, LLVMValueRef h)
2243bf215546Sopenharmony_ci{
2244bf215546Sopenharmony_ci   LLVMValueRef v0 = lp_build_sample_max_2d(bld, x, y, a, b, c, d);
2245bf215546Sopenharmony_ci   LLVMValueRef v1 = lp_build_sample_max_2d(bld, x, y, e, f, g, h);
2246bf215546Sopenharmony_ci   return lp_build_sample_max(bld, z, v0, v1);
2247bf215546Sopenharmony_ci}
2248bf215546Sopenharmony_ci
2249bf215546Sopenharmony_civoid
2250bf215546Sopenharmony_cilp_build_reduce_filter(struct lp_build_context *bld,
2251bf215546Sopenharmony_ci                       enum pipe_tex_reduction_mode mode,
2252bf215546Sopenharmony_ci                       unsigned flags,
2253bf215546Sopenharmony_ci                       unsigned num_chan,
2254bf215546Sopenharmony_ci                       LLVMValueRef x,
2255bf215546Sopenharmony_ci                       LLVMValueRef *v00,
2256bf215546Sopenharmony_ci                       LLVMValueRef *v01,
2257bf215546Sopenharmony_ci                       LLVMValueRef *out)
2258bf215546Sopenharmony_ci{
2259bf215546Sopenharmony_ci   unsigned chan;
2260bf215546Sopenharmony_ci   switch (mode) {
2261bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_MIN:
2262bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2263bf215546Sopenharmony_ci         out[chan] = lp_build_sample_min(bld, x, v00[chan], v01[chan]);
2264bf215546Sopenharmony_ci      break;
2265bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_MAX:
2266bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2267bf215546Sopenharmony_ci         out[chan] = lp_build_sample_max(bld, x, v00[chan], v01[chan]);
2268bf215546Sopenharmony_ci      break;
2269bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_WEIGHTED_AVERAGE:
2270bf215546Sopenharmony_ci   default:
2271bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2272bf215546Sopenharmony_ci         out[chan] = lp_build_lerp(bld, x, v00[chan], v01[chan], flags);
2273bf215546Sopenharmony_ci      break;
2274bf215546Sopenharmony_ci   }
2275bf215546Sopenharmony_ci}
2276bf215546Sopenharmony_ci
2277bf215546Sopenharmony_civoid
2278bf215546Sopenharmony_cilp_build_reduce_filter_2d(struct lp_build_context *bld,
2279bf215546Sopenharmony_ci                          enum pipe_tex_reduction_mode mode,
2280bf215546Sopenharmony_ci                          unsigned flags,
2281bf215546Sopenharmony_ci                          unsigned num_chan,
2282bf215546Sopenharmony_ci                          LLVMValueRef x,
2283bf215546Sopenharmony_ci                          LLVMValueRef y,
2284bf215546Sopenharmony_ci                          LLVMValueRef *v00,
2285bf215546Sopenharmony_ci                          LLVMValueRef *v01,
2286bf215546Sopenharmony_ci                          LLVMValueRef *v10,
2287bf215546Sopenharmony_ci                          LLVMValueRef *v11,
2288bf215546Sopenharmony_ci                          LLVMValueRef *out)
2289bf215546Sopenharmony_ci{
2290bf215546Sopenharmony_ci   unsigned chan;
2291bf215546Sopenharmony_ci   switch (mode) {
2292bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_MIN:
2293bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2294bf215546Sopenharmony_ci         out[chan] = lp_build_sample_min_2d(bld, x, y, v00[chan], v01[chan], v10[chan], v11[chan]);
2295bf215546Sopenharmony_ci      break;
2296bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_MAX:
2297bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2298bf215546Sopenharmony_ci         out[chan] = lp_build_sample_max_2d(bld, x, y, v00[chan], v01[chan], v10[chan], v11[chan]);
2299bf215546Sopenharmony_ci      break;
2300bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_WEIGHTED_AVERAGE:
2301bf215546Sopenharmony_ci   default:
2302bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2303bf215546Sopenharmony_ci         out[chan] = lp_build_lerp_2d(bld, x, y, v00[chan], v01[chan], v10[chan], v11[chan], flags);
2304bf215546Sopenharmony_ci      break;
2305bf215546Sopenharmony_ci   }
2306bf215546Sopenharmony_ci}
2307bf215546Sopenharmony_ci
2308bf215546Sopenharmony_civoid
2309bf215546Sopenharmony_cilp_build_reduce_filter_3d(struct lp_build_context *bld,
2310bf215546Sopenharmony_ci                          enum pipe_tex_reduction_mode mode,
2311bf215546Sopenharmony_ci                          unsigned flags,
2312bf215546Sopenharmony_ci                          unsigned num_chan,
2313bf215546Sopenharmony_ci                          LLVMValueRef x,
2314bf215546Sopenharmony_ci                          LLVMValueRef y,
2315bf215546Sopenharmony_ci                          LLVMValueRef z,
2316bf215546Sopenharmony_ci                          LLVMValueRef *v000,
2317bf215546Sopenharmony_ci                          LLVMValueRef *v001,
2318bf215546Sopenharmony_ci                          LLVMValueRef *v010,
2319bf215546Sopenharmony_ci                          LLVMValueRef *v011,
2320bf215546Sopenharmony_ci                          LLVMValueRef *v100,
2321bf215546Sopenharmony_ci                          LLVMValueRef *v101,
2322bf215546Sopenharmony_ci                          LLVMValueRef *v110,
2323bf215546Sopenharmony_ci                          LLVMValueRef *v111,
2324bf215546Sopenharmony_ci                          LLVMValueRef *out)
2325bf215546Sopenharmony_ci{
2326bf215546Sopenharmony_ci   unsigned chan;
2327bf215546Sopenharmony_ci   switch (mode) {
2328bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_MIN:
2329bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2330bf215546Sopenharmony_ci         out[chan] = lp_build_sample_min_3d(bld, x, y, z,
2331bf215546Sopenharmony_ci                                     v000[chan], v001[chan], v010[chan], v011[chan],
2332bf215546Sopenharmony_ci                                     v100[chan], v101[chan], v110[chan], v111[chan]);
2333bf215546Sopenharmony_ci      break;
2334bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_MAX:
2335bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2336bf215546Sopenharmony_ci         out[chan] = lp_build_sample_max_3d(bld, x, y, z,
2337bf215546Sopenharmony_ci                                     v000[chan], v001[chan], v010[chan], v011[chan],
2338bf215546Sopenharmony_ci                                     v100[chan], v101[chan], v110[chan], v111[chan]);
2339bf215546Sopenharmony_ci      break;
2340bf215546Sopenharmony_ci   case PIPE_TEX_REDUCTION_WEIGHTED_AVERAGE:
2341bf215546Sopenharmony_ci   default:
2342bf215546Sopenharmony_ci      for (chan = 0; chan < num_chan; chan++)
2343bf215546Sopenharmony_ci         out[chan] = lp_build_lerp_3d(bld, x, y, z,
2344bf215546Sopenharmony_ci                                      v000[chan], v001[chan], v010[chan], v011[chan],
2345bf215546Sopenharmony_ci                                      v100[chan], v101[chan], v110[chan], v111[chan],
2346bf215546Sopenharmony_ci                                      flags);
2347bf215546Sopenharmony_ci      break;
2348bf215546Sopenharmony_ci   }
2349bf215546Sopenharmony_ci}
2350bf215546Sopenharmony_ci
2351bf215546Sopenharmony_ci/*
2352bf215546Sopenharmony_ci * generated from
2353bf215546Sopenharmony_ci * const float alpha = 2;
2354bf215546Sopenharmony_ci * for (unsigned i = 0; i < WEIGHT_LUT_SIZE; i++) {
2355bf215546Sopenharmony_ci *    const float r2 = (float) i / (float) (WEIGHT_LUT_SIZE - 1);
2356bf215546Sopenharmony_ci *    const float weight = (float)expf(-alpha * r2);
2357bf215546Sopenharmony_ci */
2358bf215546Sopenharmony_cistatic const float aniso_filter_table[1024] = {
2359bf215546Sopenharmony_ci   1.000000, 0.998047, 0.996098, 0.994152, 0.992210, 0.990272, 0.988338, 0.986408,
2360bf215546Sopenharmony_ci   0.984481, 0.982559, 0.980640, 0.978724, 0.976813, 0.974905, 0.973001, 0.971100,
2361bf215546Sopenharmony_ci   0.969204, 0.967311, 0.965421, 0.963536, 0.961654, 0.959776, 0.957901, 0.956030,
2362bf215546Sopenharmony_ci   0.954163, 0.952299, 0.950439, 0.948583, 0.946730, 0.944881, 0.943036, 0.941194,
2363bf215546Sopenharmony_ci   0.939356, 0.937521, 0.935690, 0.933862, 0.932038, 0.930218, 0.928401, 0.926588,
2364bf215546Sopenharmony_ci   0.924778, 0.922972, 0.921169, 0.919370, 0.917575, 0.915782, 0.913994, 0.912209,
2365bf215546Sopenharmony_ci   0.910427, 0.908649, 0.906874, 0.905103, 0.903335, 0.901571, 0.899810, 0.898052,
2366bf215546Sopenharmony_ci   0.896298, 0.894548, 0.892801, 0.891057, 0.889317, 0.887580, 0.885846, 0.884116,
2367bf215546Sopenharmony_ci   0.882389, 0.880666, 0.878946, 0.877229, 0.875516, 0.873806, 0.872099, 0.870396,
2368bf215546Sopenharmony_ci   0.868696, 0.866999, 0.865306, 0.863616, 0.861929, 0.860245, 0.858565, 0.856888,
2369bf215546Sopenharmony_ci   0.855215, 0.853544, 0.851877, 0.850213, 0.848553, 0.846896, 0.845241, 0.843591,
2370bf215546Sopenharmony_ci   0.841943, 0.840299, 0.838657, 0.837019, 0.835385, 0.833753, 0.832124, 0.830499,
2371bf215546Sopenharmony_ci   0.828877, 0.827258, 0.825643, 0.824030, 0.822421, 0.820814, 0.819211, 0.817611,
2372bf215546Sopenharmony_ci   0.816014, 0.814420, 0.812830, 0.811242, 0.809658, 0.808076, 0.806498, 0.804923,
2373bf215546Sopenharmony_ci   0.803351, 0.801782, 0.800216, 0.798653, 0.797093, 0.795536, 0.793982, 0.792432,
2374bf215546Sopenharmony_ci   0.790884, 0.789339, 0.787798, 0.786259, 0.784723, 0.783191, 0.781661, 0.780134,
2375bf215546Sopenharmony_ci   0.778610, 0.777090, 0.775572, 0.774057, 0.772545, 0.771037, 0.769531, 0.768028,
2376bf215546Sopenharmony_ci   0.766528, 0.765030, 0.763536, 0.762045, 0.760557, 0.759071, 0.757589, 0.756109,
2377bf215546Sopenharmony_ci   0.754632, 0.753158, 0.751687, 0.750219, 0.748754, 0.747291, 0.745832, 0.744375,
2378bf215546Sopenharmony_ci   0.742921, 0.741470, 0.740022, 0.738577, 0.737134, 0.735694, 0.734258, 0.732823,
2379bf215546Sopenharmony_ci   0.731392, 0.729964, 0.728538, 0.727115, 0.725695, 0.724278, 0.722863, 0.721451,
2380bf215546Sopenharmony_ci   0.720042, 0.718636, 0.717232, 0.715831, 0.714433, 0.713038, 0.711645, 0.710255,
2381bf215546Sopenharmony_ci   0.708868, 0.707483, 0.706102, 0.704723, 0.703346, 0.701972, 0.700601, 0.699233,
2382bf215546Sopenharmony_ci   0.697867, 0.696504, 0.695144, 0.693786, 0.692431, 0.691079, 0.689729, 0.688382,
2383bf215546Sopenharmony_ci   0.687037, 0.685696, 0.684356, 0.683020, 0.681686, 0.680354, 0.679025, 0.677699,
2384bf215546Sopenharmony_ci   0.676376, 0.675054, 0.673736, 0.672420, 0.671107, 0.669796, 0.668488, 0.667182,
2385bf215546Sopenharmony_ci   0.665879, 0.664579, 0.663281, 0.661985, 0.660692, 0.659402, 0.658114, 0.656828,
2386bf215546Sopenharmony_ci   0.655546, 0.654265, 0.652987, 0.651712, 0.650439, 0.649169, 0.647901, 0.646635,
2387bf215546Sopenharmony_ci   0.645372, 0.644112, 0.642854, 0.641598, 0.640345, 0.639095, 0.637846, 0.636601,
2388bf215546Sopenharmony_ci   0.635357, 0.634116, 0.632878, 0.631642, 0.630408, 0.629177, 0.627948, 0.626721,
2389bf215546Sopenharmony_ci   0.625497, 0.624276, 0.623056, 0.621839, 0.620625, 0.619413, 0.618203, 0.616996,
2390bf215546Sopenharmony_ci   0.615790, 0.614588, 0.613387, 0.612189, 0.610994, 0.609800, 0.608609, 0.607421,
2391bf215546Sopenharmony_ci   0.606234, 0.605050, 0.603868, 0.602689, 0.601512, 0.600337, 0.599165, 0.597994,
2392bf215546Sopenharmony_ci   0.596826, 0.595661, 0.594497, 0.593336, 0.592177, 0.591021, 0.589866, 0.588714,
2393bf215546Sopenharmony_ci   0.587564, 0.586417, 0.585272, 0.584128, 0.582988, 0.581849, 0.580712, 0.579578,
2394bf215546Sopenharmony_ci   0.578446, 0.577317, 0.576189, 0.575064, 0.573940, 0.572819, 0.571701, 0.570584,
2395bf215546Sopenharmony_ci   0.569470, 0.568357, 0.567247, 0.566139, 0.565034, 0.563930, 0.562829, 0.561729,
2396bf215546Sopenharmony_ci   0.560632, 0.559537, 0.558444, 0.557354, 0.556265, 0.555179, 0.554094, 0.553012,
2397bf215546Sopenharmony_ci   0.551932, 0.550854, 0.549778, 0.548704, 0.547633, 0.546563, 0.545496, 0.544430,
2398bf215546Sopenharmony_ci   0.543367, 0.542306, 0.541246, 0.540189, 0.539134, 0.538081, 0.537030, 0.535981,
2399bf215546Sopenharmony_ci   0.534935, 0.533890, 0.532847, 0.531806, 0.530768, 0.529731, 0.528696, 0.527664,
2400bf215546Sopenharmony_ci   0.526633, 0.525604, 0.524578, 0.523553, 0.522531, 0.521510, 0.520492, 0.519475,
2401bf215546Sopenharmony_ci   0.518460, 0.517448, 0.516437, 0.515429, 0.514422, 0.513417, 0.512414, 0.511414,
2402bf215546Sopenharmony_ci   0.510415, 0.509418, 0.508423, 0.507430, 0.506439, 0.505450, 0.504462, 0.503477,
2403bf215546Sopenharmony_ci   0.502494, 0.501512, 0.500533, 0.499555, 0.498580, 0.497606, 0.496634, 0.495664,
2404bf215546Sopenharmony_ci   0.494696, 0.493730, 0.492765, 0.491803, 0.490842, 0.489884, 0.488927, 0.487972,
2405bf215546Sopenharmony_ci   0.487019, 0.486068, 0.485118, 0.484171, 0.483225, 0.482281, 0.481339, 0.480399,
2406bf215546Sopenharmony_ci   0.479461, 0.478524, 0.477590, 0.476657, 0.475726, 0.474797, 0.473870, 0.472944,
2407bf215546Sopenharmony_ci   0.472020, 0.471098, 0.470178, 0.469260, 0.468343, 0.467429, 0.466516, 0.465605,
2408bf215546Sopenharmony_ci   0.464695, 0.463788, 0.462882, 0.461978, 0.461075, 0.460175, 0.459276, 0.458379,
2409bf215546Sopenharmony_ci   0.457484, 0.456590, 0.455699, 0.454809, 0.453920, 0.453034, 0.452149, 0.451266,
2410bf215546Sopenharmony_ci   0.450384, 0.449505, 0.448627, 0.447751, 0.446876, 0.446003, 0.445132, 0.444263,
2411bf215546Sopenharmony_ci   0.443395, 0.442529, 0.441665, 0.440802, 0.439941, 0.439082, 0.438224, 0.437368,
2412bf215546Sopenharmony_ci   0.436514, 0.435662, 0.434811, 0.433961, 0.433114, 0.432268, 0.431424, 0.430581,
2413bf215546Sopenharmony_ci   0.429740, 0.428901, 0.428063, 0.427227, 0.426393, 0.425560, 0.424729, 0.423899,
2414bf215546Sopenharmony_ci   0.423071, 0.422245, 0.421420, 0.420597, 0.419776, 0.418956, 0.418137, 0.417321,
2415bf215546Sopenharmony_ci   0.416506, 0.415692, 0.414880, 0.414070, 0.413261, 0.412454, 0.411648, 0.410844,
2416bf215546Sopenharmony_ci   0.410042, 0.409241, 0.408442, 0.407644, 0.406848, 0.406053, 0.405260, 0.404469,
2417bf215546Sopenharmony_ci   0.403679, 0.402890, 0.402103, 0.401318, 0.400534, 0.399752, 0.398971, 0.398192,
2418bf215546Sopenharmony_ci   0.397414, 0.396638, 0.395863, 0.395090, 0.394319, 0.393548, 0.392780, 0.392013,
2419bf215546Sopenharmony_ci   0.391247, 0.390483, 0.389720, 0.388959, 0.388199, 0.387441, 0.386684, 0.385929,
2420bf215546Sopenharmony_ci   0.385175, 0.384423, 0.383672, 0.382923, 0.382175, 0.381429, 0.380684, 0.379940,
2421bf215546Sopenharmony_ci   0.379198, 0.378457, 0.377718, 0.376980, 0.376244, 0.375509, 0.374776, 0.374044,
2422bf215546Sopenharmony_ci   0.373313, 0.372584, 0.371856, 0.371130, 0.370405, 0.369682, 0.368960, 0.368239,
2423bf215546Sopenharmony_ci   0.367520, 0.366802, 0.366086, 0.365371, 0.364657, 0.363945, 0.363234, 0.362525,
2424bf215546Sopenharmony_ci   0.361817, 0.361110, 0.360405, 0.359701, 0.358998, 0.358297, 0.357597, 0.356899,
2425bf215546Sopenharmony_ci   0.356202, 0.355506, 0.354812, 0.354119, 0.353427, 0.352737, 0.352048, 0.351360,
2426bf215546Sopenharmony_ci   0.350674, 0.349989, 0.349306, 0.348623, 0.347942, 0.347263, 0.346585, 0.345908,
2427bf215546Sopenharmony_ci   0.345232, 0.344558, 0.343885, 0.343213, 0.342543, 0.341874, 0.341206, 0.340540,
2428bf215546Sopenharmony_ci   0.339874, 0.339211, 0.338548, 0.337887, 0.337227, 0.336568, 0.335911, 0.335255,
2429bf215546Sopenharmony_ci   0.334600, 0.333947, 0.333294, 0.332643, 0.331994, 0.331345, 0.330698, 0.330052,
2430bf215546Sopenharmony_ci   0.329408, 0.328764, 0.328122, 0.327481, 0.326842, 0.326203, 0.325566, 0.324930,
2431bf215546Sopenharmony_ci   0.324296, 0.323662, 0.323030, 0.322399, 0.321770, 0.321141, 0.320514, 0.319888,
2432bf215546Sopenharmony_ci   0.319263, 0.318639, 0.318017, 0.317396, 0.316776, 0.316157, 0.315540, 0.314924,
2433bf215546Sopenharmony_ci   0.314309, 0.313695, 0.313082, 0.312470, 0.311860, 0.311251, 0.310643, 0.310036,
2434bf215546Sopenharmony_ci   0.309431, 0.308827, 0.308223, 0.307621, 0.307021, 0.306421, 0.305822, 0.305225,
2435bf215546Sopenharmony_ci   0.304629, 0.304034, 0.303440, 0.302847, 0.302256, 0.301666, 0.301076, 0.300488,
2436bf215546Sopenharmony_ci   0.299902, 0.299316, 0.298731, 0.298148, 0.297565, 0.296984, 0.296404, 0.295825,
2437bf215546Sopenharmony_ci   0.295247, 0.294671, 0.294095, 0.293521, 0.292948, 0.292375, 0.291804, 0.291234,
2438bf215546Sopenharmony_ci   0.290666, 0.290098, 0.289531, 0.288966, 0.288401, 0.287838, 0.287276, 0.286715,
2439bf215546Sopenharmony_ci   0.286155, 0.285596, 0.285038, 0.284482, 0.283926, 0.283371, 0.282818, 0.282266,
2440bf215546Sopenharmony_ci   0.281714, 0.281164, 0.280615, 0.280067, 0.279520, 0.278974, 0.278429, 0.277885,
2441bf215546Sopenharmony_ci   0.277342, 0.276801, 0.276260, 0.275721, 0.275182, 0.274645, 0.274108, 0.273573,
2442bf215546Sopenharmony_ci   0.273038, 0.272505, 0.271973, 0.271442, 0.270912, 0.270382, 0.269854, 0.269327,
2443bf215546Sopenharmony_ci   0.268801, 0.268276, 0.267752, 0.267229, 0.266707, 0.266186, 0.265667, 0.265148,
2444bf215546Sopenharmony_ci   0.264630, 0.264113, 0.263597, 0.263082, 0.262568, 0.262056, 0.261544, 0.261033,
2445bf215546Sopenharmony_ci   0.260523, 0.260014, 0.259506, 0.259000, 0.258494, 0.257989, 0.257485, 0.256982,
2446bf215546Sopenharmony_ci   0.256480, 0.255979, 0.255479, 0.254980, 0.254482, 0.253985, 0.253489, 0.252994,
2447bf215546Sopenharmony_ci   0.252500, 0.252007, 0.251515, 0.251023, 0.250533, 0.250044, 0.249555, 0.249068,
2448bf215546Sopenharmony_ci   0.248582, 0.248096, 0.247611, 0.247128, 0.246645, 0.246163, 0.245683, 0.245203,
2449bf215546Sopenharmony_ci   0.244724, 0.244246, 0.243769, 0.243293, 0.242818, 0.242343, 0.241870, 0.241398,
2450bf215546Sopenharmony_ci   0.240926, 0.240456, 0.239986, 0.239517, 0.239049, 0.238583, 0.238117, 0.237651,
2451bf215546Sopenharmony_ci   0.237187, 0.236724, 0.236262, 0.235800, 0.235340, 0.234880, 0.234421, 0.233963,
2452bf215546Sopenharmony_ci   0.233506, 0.233050, 0.232595, 0.232141, 0.231688, 0.231235, 0.230783, 0.230333,
2453bf215546Sopenharmony_ci   0.229883, 0.229434, 0.228986, 0.228538, 0.228092, 0.227647, 0.227202, 0.226758,
2454bf215546Sopenharmony_ci   0.226315, 0.225873, 0.225432, 0.224992, 0.224552, 0.224114, 0.223676, 0.223239,
2455bf215546Sopenharmony_ci   0.222803, 0.222368, 0.221934, 0.221500, 0.221068, 0.220636, 0.220205, 0.219775,
2456bf215546Sopenharmony_ci   0.219346, 0.218917, 0.218490, 0.218063, 0.217637, 0.217212, 0.216788, 0.216364,
2457bf215546Sopenharmony_ci   0.215942, 0.215520, 0.215099, 0.214679, 0.214260, 0.213841, 0.213423, 0.213007,
2458bf215546Sopenharmony_ci   0.212591, 0.212175, 0.211761, 0.211347, 0.210935, 0.210523, 0.210111, 0.209701,
2459bf215546Sopenharmony_ci   0.209291, 0.208883, 0.208475, 0.208068, 0.207661, 0.207256, 0.206851, 0.206447,
2460bf215546Sopenharmony_ci   0.206044, 0.205641, 0.205239, 0.204839, 0.204439, 0.204039, 0.203641, 0.203243,
2461bf215546Sopenharmony_ci   0.202846, 0.202450, 0.202054, 0.201660, 0.201266, 0.200873, 0.200481, 0.200089,
2462bf215546Sopenharmony_ci   0.199698, 0.199308, 0.198919, 0.198530, 0.198143, 0.197756, 0.197369, 0.196984,
2463bf215546Sopenharmony_ci   0.196599, 0.196215, 0.195832, 0.195449, 0.195068, 0.194687, 0.194306, 0.193927,
2464bf215546Sopenharmony_ci   0.193548, 0.193170, 0.192793, 0.192416, 0.192041, 0.191665, 0.191291, 0.190917,
2465bf215546Sopenharmony_ci   0.190545, 0.190172, 0.189801, 0.189430, 0.189060, 0.188691, 0.188323, 0.187955,
2466bf215546Sopenharmony_ci   0.187588, 0.187221, 0.186856, 0.186491, 0.186126, 0.185763, 0.185400, 0.185038,
2467bf215546Sopenharmony_ci   0.184676, 0.184316, 0.183956, 0.183597, 0.183238, 0.182880, 0.182523, 0.182166,
2468bf215546Sopenharmony_ci   0.181811, 0.181455, 0.181101, 0.180747, 0.180394, 0.180042, 0.179690, 0.179339,
2469bf215546Sopenharmony_ci   0.178989, 0.178640, 0.178291, 0.177942, 0.177595, 0.177248, 0.176902, 0.176556,
2470bf215546Sopenharmony_ci   0.176211, 0.175867, 0.175524, 0.175181, 0.174839, 0.174497, 0.174157, 0.173816,
2471bf215546Sopenharmony_ci   0.173477, 0.173138, 0.172800, 0.172462, 0.172126, 0.171789, 0.171454, 0.171119,
2472bf215546Sopenharmony_ci   0.170785, 0.170451, 0.170118, 0.169786, 0.169454, 0.169124, 0.168793, 0.168463,
2473bf215546Sopenharmony_ci   0.168134, 0.167806, 0.167478, 0.167151, 0.166825, 0.166499, 0.166174, 0.165849,
2474bf215546Sopenharmony_ci   0.165525, 0.165202, 0.164879, 0.164557, 0.164236, 0.163915, 0.163595, 0.163275,
2475bf215546Sopenharmony_ci   0.162957, 0.162638, 0.162321, 0.162004, 0.161687, 0.161371, 0.161056, 0.160742,
2476bf215546Sopenharmony_ci   0.160428, 0.160114, 0.159802, 0.159489, 0.159178, 0.158867, 0.158557, 0.158247,
2477bf215546Sopenharmony_ci   0.157938, 0.157630, 0.157322, 0.157014, 0.156708, 0.156402, 0.156096, 0.155791,
2478bf215546Sopenharmony_ci   0.155487, 0.155183, 0.154880, 0.154578, 0.154276, 0.153975, 0.153674, 0.153374,
2479bf215546Sopenharmony_ci   0.153074, 0.152775, 0.152477, 0.152179, 0.151882, 0.151585, 0.151289, 0.150994,
2480bf215546Sopenharmony_ci   0.150699, 0.150404, 0.150111, 0.149817, 0.149525, 0.149233, 0.148941, 0.148650,
2481bf215546Sopenharmony_ci   0.148360, 0.148070, 0.147781, 0.147492, 0.147204, 0.146917, 0.146630, 0.146344,
2482bf215546Sopenharmony_ci   0.146058, 0.145772, 0.145488, 0.145204, 0.144920, 0.144637, 0.144354, 0.144072,
2483bf215546Sopenharmony_ci   0.143791, 0.143510, 0.143230, 0.142950, 0.142671, 0.142392, 0.142114, 0.141837,
2484bf215546Sopenharmony_ci   0.141560, 0.141283, 0.141007, 0.140732, 0.140457, 0.140183, 0.139909, 0.139636,
2485bf215546Sopenharmony_ci   0.139363, 0.139091, 0.138819, 0.138548, 0.138277, 0.138007, 0.137738, 0.137469,
2486bf215546Sopenharmony_ci   0.137200, 0.136932, 0.136665, 0.136398, 0.136131, 0.135865, 0.135600, 0.135335,
2487bf215546Sopenharmony_ci};
2488bf215546Sopenharmony_ci
2489bf215546Sopenharmony_ciconst float *
2490bf215546Sopenharmony_cilp_build_sample_aniso_filter_table(void)
2491bf215546Sopenharmony_ci{
2492bf215546Sopenharmony_ci   return aniso_filter_table;
2493bf215546Sopenharmony_ci}
2494