1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "pipe/p_state.h"
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci#include "util/u_inlines.h"
30bf215546Sopenharmony_ci#include "util/u_memory.h"
31bf215546Sopenharmony_ci#include "util/u_string.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "fd5_format.h"
34bf215546Sopenharmony_ci#include "fd5_texture.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic enum a5xx_tex_clamp
37bf215546Sopenharmony_citex_clamp(unsigned wrap, bool *needs_border)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci   switch (wrap) {
40bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_REPEAT:
41bf215546Sopenharmony_ci      return A5XX_TEX_REPEAT;
42bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
43bf215546Sopenharmony_ci      return A5XX_TEX_CLAMP_TO_EDGE;
44bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
45bf215546Sopenharmony_ci      *needs_border = true;
46bf215546Sopenharmony_ci      return A5XX_TEX_CLAMP_TO_BORDER;
47bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
48bf215546Sopenharmony_ci      /* only works for PoT.. need to emulate otherwise! */
49bf215546Sopenharmony_ci      return A5XX_TEX_MIRROR_CLAMP;
50bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_REPEAT:
51bf215546Sopenharmony_ci      return A5XX_TEX_MIRROR_REPEAT;
52bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP:
53bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
54bf215546Sopenharmony_ci      /* these two we could perhaps emulate, but we currently
55bf215546Sopenharmony_ci       * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
56bf215546Sopenharmony_ci       */
57bf215546Sopenharmony_ci   default:
58bf215546Sopenharmony_ci      DBG("invalid wrap: %u", wrap);
59bf215546Sopenharmony_ci      return 0;
60bf215546Sopenharmony_ci   }
61bf215546Sopenharmony_ci}
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistatic enum a5xx_tex_filter
64bf215546Sopenharmony_citex_filter(unsigned filter, bool aniso)
65bf215546Sopenharmony_ci{
66bf215546Sopenharmony_ci   switch (filter) {
67bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_NEAREST:
68bf215546Sopenharmony_ci      return A5XX_TEX_NEAREST;
69bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_LINEAR:
70bf215546Sopenharmony_ci      return aniso ? A5XX_TEX_ANISO : A5XX_TEX_LINEAR;
71bf215546Sopenharmony_ci   default:
72bf215546Sopenharmony_ci      DBG("invalid filter: %u", filter);
73bf215546Sopenharmony_ci      return 0;
74bf215546Sopenharmony_ci   }
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic void *
78bf215546Sopenharmony_cifd5_sampler_state_create(struct pipe_context *pctx,
79bf215546Sopenharmony_ci                         const struct pipe_sampler_state *cso)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   struct fd5_sampler_stateobj *so = CALLOC_STRUCT(fd5_sampler_stateobj);
82bf215546Sopenharmony_ci   unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
83bf215546Sopenharmony_ci   bool miplinear = false;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   if (!so)
86bf215546Sopenharmony_ci      return NULL;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   so->base = *cso;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci   if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
91bf215546Sopenharmony_ci      miplinear = true;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   so->needs_border = false;
94bf215546Sopenharmony_ci   so->texsamp0 =
95bf215546Sopenharmony_ci      COND(miplinear, A5XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
96bf215546Sopenharmony_ci      A5XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
97bf215546Sopenharmony_ci      A5XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
98bf215546Sopenharmony_ci      A5XX_TEX_SAMP_0_ANISO(aniso) |
99bf215546Sopenharmony_ci      A5XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |
100bf215546Sopenharmony_ci      A5XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |
101bf215546Sopenharmony_ci      A5XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   so->texsamp1 =
104bf215546Sopenharmony_ci      COND(!cso->seamless_cube_map, A5XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |
105bf215546Sopenharmony_ci      COND(!cso->normalized_coords, A5XX_TEX_SAMP_1_UNNORM_COORDS);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   so->texsamp0 |= A5XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
110bf215546Sopenharmony_ci      so->texsamp1 |= A5XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
111bf215546Sopenharmony_ci                      A5XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
112bf215546Sopenharmony_ci   } else {
113bf215546Sopenharmony_ci      /* If we're not doing mipmap filtering, we still need a slightly > 0
114bf215546Sopenharmony_ci       * LOD clamp so the HW can decide between min and mag filtering of
115bf215546Sopenharmony_ci       * level 0.
116bf215546Sopenharmony_ci       */
117bf215546Sopenharmony_ci      so->texsamp1 |= A5XX_TEX_SAMP_1_MIN_LOD(MIN2(cso->min_lod, 0.125f)) |
118bf215546Sopenharmony_ci                      A5XX_TEX_SAMP_1_MAX_LOD(MIN2(cso->max_lod, 0.125f));
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   if (cso->compare_mode)
122bf215546Sopenharmony_ci      so->texsamp1 |=
123bf215546Sopenharmony_ci         A5XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   return so;
126bf215546Sopenharmony_ci}
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_cistatic struct pipe_sampler_view *
129bf215546Sopenharmony_cifd5_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
130bf215546Sopenharmony_ci                        const struct pipe_sampler_view *cso)
131bf215546Sopenharmony_ci{
132bf215546Sopenharmony_ci   struct fd5_pipe_sampler_view *so = CALLOC_STRUCT(fd5_pipe_sampler_view);
133bf215546Sopenharmony_ci   struct fd_resource *rsc = fd_resource(prsc);
134bf215546Sopenharmony_ci   enum pipe_format format = cso->format;
135bf215546Sopenharmony_ci   unsigned lvl, layers = 0;
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   if (!so)
138bf215546Sopenharmony_ci      return NULL;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   if (format == PIPE_FORMAT_X32_S8X24_UINT) {
141bf215546Sopenharmony_ci      rsc = rsc->stencil;
142bf215546Sopenharmony_ci      format = rsc->b.b.format;
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   so->base = *cso;
146bf215546Sopenharmony_ci   pipe_reference(NULL, &prsc->reference);
147bf215546Sopenharmony_ci   so->base.texture = prsc;
148bf215546Sopenharmony_ci   so->base.reference.count = 1;
149bf215546Sopenharmony_ci   so->base.context = pctx;
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_ci   so->texconst0 = A5XX_TEX_CONST_0_FMT(fd5_pipe2tex(format)) |
152bf215546Sopenharmony_ci                   A5XX_TEX_CONST_0_SAMPLES(fd_msaa_samples(prsc->nr_samples)) |
153bf215546Sopenharmony_ci                   fd5_tex_swiz(format, cso->swizzle_r, cso->swizzle_g,
154bf215546Sopenharmony_ci                                cso->swizzle_b, cso->swizzle_a);
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   /* NOTE: since we sample z24s8 using 8888_UINT format, the swizzle
157bf215546Sopenharmony_ci    * we get isn't quite right.  Use SWAP(XYZW) as a cheap and cheerful
158bf215546Sopenharmony_ci    * way to re-arrange things so stencil component is where the swiz
159bf215546Sopenharmony_ci    * expects.
160bf215546Sopenharmony_ci    *
161bf215546Sopenharmony_ci    * Note that gallium expects stencil sampler to return (s,s,s,s)
162bf215546Sopenharmony_ci    * which isn't quite true.  To make that happen we'd have to massage
163bf215546Sopenharmony_ci    * the swizzle.  But in practice only the .x component is used.
164bf215546Sopenharmony_ci    */
165bf215546Sopenharmony_ci   if (format == PIPE_FORMAT_X24S8_UINT) {
166bf215546Sopenharmony_ci      so->texconst0 |= A5XX_TEX_CONST_0_SWAP(XYZW);
167bf215546Sopenharmony_ci   }
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci   if (util_format_is_srgb(format)) {
170bf215546Sopenharmony_ci      so->texconst0 |= A5XX_TEX_CONST_0_SRGB;
171bf215546Sopenharmony_ci   }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   if (cso->target == PIPE_BUFFER) {
174bf215546Sopenharmony_ci      unsigned elements = cso->u.buf.size / util_format_get_blocksize(format);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci      lvl = 0;
177bf215546Sopenharmony_ci      so->texconst1 = A5XX_TEX_CONST_1_WIDTH(elements & MASK(15)) |
178bf215546Sopenharmony_ci                      A5XX_TEX_CONST_1_HEIGHT(elements >> 15);
179bf215546Sopenharmony_ci      so->texconst2 = A5XX_TEX_CONST_2_BUFFER;
180bf215546Sopenharmony_ci      so->offset = cso->u.buf.offset;
181bf215546Sopenharmony_ci   } else {
182bf215546Sopenharmony_ci      unsigned miplevels;
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci      lvl = fd_sampler_first_level(cso);
185bf215546Sopenharmony_ci      miplevels = fd_sampler_last_level(cso) - lvl;
186bf215546Sopenharmony_ci      layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci      so->texconst0 |= A5XX_TEX_CONST_0_MIPLVLS(miplevels);
189bf215546Sopenharmony_ci      so->texconst1 = A5XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
190bf215546Sopenharmony_ci                      A5XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
191bf215546Sopenharmony_ci      so->texconst2 = A5XX_TEX_CONST_2_PITCHALIGN(rsc->layout.pitchalign - 6) |
192bf215546Sopenharmony_ci                      A5XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));
193bf215546Sopenharmony_ci      so->offset = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);
194bf215546Sopenharmony_ci   }
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   so->texconst2 |= A5XX_TEX_CONST_2_TYPE(fd5_tex_type(cso->target));
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   switch (cso->target) {
199bf215546Sopenharmony_ci   case PIPE_TEXTURE_RECT:
200bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D:
201bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D:
202bf215546Sopenharmony_ci      so->texconst3 = A5XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);
203bf215546Sopenharmony_ci      so->texconst5 = A5XX_TEX_CONST_5_DEPTH(1);
204bf215546Sopenharmony_ci      break;
205bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
206bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
207bf215546Sopenharmony_ci      so->texconst3 = A5XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);
208bf215546Sopenharmony_ci      so->texconst5 = A5XX_TEX_CONST_5_DEPTH(layers);
209bf215546Sopenharmony_ci      break;
210bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE:
211bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE_ARRAY:
212bf215546Sopenharmony_ci      so->texconst3 = A5XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);
213bf215546Sopenharmony_ci      so->texconst5 = A5XX_TEX_CONST_5_DEPTH(layers / 6);
214bf215546Sopenharmony_ci      break;
215bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
216bf215546Sopenharmony_ci      so->texconst3 =
217bf215546Sopenharmony_ci         A5XX_TEX_CONST_3_MIN_LAYERSZ(
218bf215546Sopenharmony_ci            fd_resource_slice(rsc, prsc->last_level)->size0) |
219bf215546Sopenharmony_ci         A5XX_TEX_CONST_3_ARRAY_PITCH(fd_resource_slice(rsc, lvl)->size0);
220bf215546Sopenharmony_ci      so->texconst5 = A5XX_TEX_CONST_5_DEPTH(u_minify(prsc->depth0, lvl));
221bf215546Sopenharmony_ci      break;
222bf215546Sopenharmony_ci   default:
223bf215546Sopenharmony_ci      so->texconst3 = 0x00000000;
224bf215546Sopenharmony_ci      break;
225bf215546Sopenharmony_ci   }
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   return &so->base;
228bf215546Sopenharmony_ci}
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_civoid
231bf215546Sopenharmony_cifd5_texture_init(struct pipe_context *pctx)
232bf215546Sopenharmony_ci{
233bf215546Sopenharmony_ci   pctx->create_sampler_state = fd5_sampler_state_create;
234bf215546Sopenharmony_ci   pctx->bind_sampler_states = fd_sampler_states_bind;
235bf215546Sopenharmony_ci   pctx->create_sampler_view = fd5_sampler_view_create;
236bf215546Sopenharmony_ci   pctx->set_sampler_views = fd_set_sampler_views;
237bf215546Sopenharmony_ci}
238