1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2013 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 "fd3_format.h"
34bf215546Sopenharmony_ci#include "fd3_texture.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic enum a3xx_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 A3XX_TEX_REPEAT;
42bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
43bf215546Sopenharmony_ci      return A3XX_TEX_CLAMP_TO_EDGE;
44bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
45bf215546Sopenharmony_ci      *needs_border = true;
46bf215546Sopenharmony_ci      return A3XX_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 A3XX_TEX_MIRROR_CLAMP;
50bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_REPEAT:
51bf215546Sopenharmony_ci      return A3XX_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 a3xx_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 A3XX_TEX_NEAREST;
69bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_LINEAR:
70bf215546Sopenharmony_ci      return aniso ? A3XX_TEX_ANISO : A3XX_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_cifd3_sampler_state_create(struct pipe_context *pctx,
79bf215546Sopenharmony_ci                         const struct pipe_sampler_state *cso)
80bf215546Sopenharmony_ci{
81bf215546Sopenharmony_ci   struct fd3_sampler_stateobj *so = CALLOC_STRUCT(fd3_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   if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
89bf215546Sopenharmony_ci      miplinear = true;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   so->base = *cso;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   so->needs_border = false;
94bf215546Sopenharmony_ci   so->texsamp0 =
95bf215546Sopenharmony_ci      COND(!cso->normalized_coords, A3XX_TEX_SAMP_0_UNNORM_COORDS) |
96bf215546Sopenharmony_ci      COND(!cso->seamless_cube_map, A3XX_TEX_SAMP_0_CUBEMAPSEAMLESSFILTOFF) |
97bf215546Sopenharmony_ci      COND(miplinear, A3XX_TEX_SAMP_0_MIPFILTER_LINEAR) |
98bf215546Sopenharmony_ci      A3XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
99bf215546Sopenharmony_ci      A3XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
100bf215546Sopenharmony_ci      A3XX_TEX_SAMP_0_ANISO(aniso) |
101bf215546Sopenharmony_ci      A3XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |
102bf215546Sopenharmony_ci      A3XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |
103bf215546Sopenharmony_ci      A3XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   if (cso->compare_mode)
106bf215546Sopenharmony_ci      so->texsamp0 |=
107bf215546Sopenharmony_ci         A3XX_TEX_SAMP_0_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci   so->texsamp1 = A3XX_TEX_SAMP_1_LOD_BIAS(cso->lod_bias);
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
112bf215546Sopenharmony_ci      so->texsamp1 |= A3XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
113bf215546Sopenharmony_ci                      A3XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
114bf215546Sopenharmony_ci   } else {
115bf215546Sopenharmony_ci      /* If we're not doing mipmap filtering, we still need a slightly > 0
116bf215546Sopenharmony_ci       * LOD clamp so the HW can decide between min and mag filtering of
117bf215546Sopenharmony_ci       * level 0.
118bf215546Sopenharmony_ci       */
119bf215546Sopenharmony_ci      so->texsamp1 |= A3XX_TEX_SAMP_1_MIN_LOD(MIN2(cso->min_lod, 0.125f)) |
120bf215546Sopenharmony_ci                      A3XX_TEX_SAMP_1_MAX_LOD(MIN2(cso->max_lod, 0.125f));
121bf215546Sopenharmony_ci   }
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   return so;
124bf215546Sopenharmony_ci}
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_cistatic enum a3xx_tex_type
127bf215546Sopenharmony_citex_type(unsigned target)
128bf215546Sopenharmony_ci{
129bf215546Sopenharmony_ci   switch (target) {
130bf215546Sopenharmony_ci   default:
131bf215546Sopenharmony_ci      assert(0);
132bf215546Sopenharmony_ci   case PIPE_BUFFER:
133bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D:
134bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
135bf215546Sopenharmony_ci      return A3XX_TEX_1D;
136bf215546Sopenharmony_ci   case PIPE_TEXTURE_RECT:
137bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D:
138bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
139bf215546Sopenharmony_ci      return A3XX_TEX_2D;
140bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
141bf215546Sopenharmony_ci      return A3XX_TEX_3D;
142bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE:
143bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE_ARRAY:
144bf215546Sopenharmony_ci      return A3XX_TEX_CUBE;
145bf215546Sopenharmony_ci   }
146bf215546Sopenharmony_ci}
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_cistatic struct pipe_sampler_view *
149bf215546Sopenharmony_cifd3_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
150bf215546Sopenharmony_ci                        const struct pipe_sampler_view *cso)
151bf215546Sopenharmony_ci{
152bf215546Sopenharmony_ci   struct fd3_pipe_sampler_view *so = CALLOC_STRUCT(fd3_pipe_sampler_view);
153bf215546Sopenharmony_ci   struct fd_resource *rsc = fd_resource(prsc);
154bf215546Sopenharmony_ci   unsigned lvl;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   if (!so)
157bf215546Sopenharmony_ci      return NULL;
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ci   so->base = *cso;
160bf215546Sopenharmony_ci   pipe_reference(NULL, &prsc->reference);
161bf215546Sopenharmony_ci   so->base.texture = prsc;
162bf215546Sopenharmony_ci   so->base.reference.count = 1;
163bf215546Sopenharmony_ci   so->base.context = pctx;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   so->texconst0 = A3XX_TEX_CONST_0_TILE_MODE(rsc->layout.tile_mode) |
166bf215546Sopenharmony_ci                   A3XX_TEX_CONST_0_TYPE(tex_type(prsc->target)) |
167bf215546Sopenharmony_ci                   A3XX_TEX_CONST_0_FMT(fd3_pipe2tex(cso->format)) |
168bf215546Sopenharmony_ci                   fd3_tex_swiz(cso->format, cso->swizzle_r, cso->swizzle_g,
169bf215546Sopenharmony_ci                                cso->swizzle_b, cso->swizzle_a);
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   if (prsc->target == PIPE_BUFFER || util_format_is_pure_integer(cso->format))
172bf215546Sopenharmony_ci      so->texconst0 |= A3XX_TEX_CONST_0_NOCONVERT;
173bf215546Sopenharmony_ci   if (util_format_is_srgb(cso->format))
174bf215546Sopenharmony_ci      so->texconst0 |= A3XX_TEX_CONST_0_SRGB;
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   if (prsc->target == PIPE_BUFFER) {
177bf215546Sopenharmony_ci      lvl = 0;
178bf215546Sopenharmony_ci      so->texconst1 =
179bf215546Sopenharmony_ci         A3XX_TEX_CONST_1_WIDTH(cso->u.buf.size /
180bf215546Sopenharmony_ci                                util_format_get_blocksize(cso->format)) |
181bf215546Sopenharmony_ci         A3XX_TEX_CONST_1_HEIGHT(1);
182bf215546Sopenharmony_ci   } else {
183bf215546Sopenharmony_ci      unsigned miplevels;
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci      lvl = fd_sampler_first_level(cso);
186bf215546Sopenharmony_ci      miplevels = fd_sampler_last_level(cso) - lvl;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci      so->texconst0 |= A3XX_TEX_CONST_0_MIPLVLS(miplevels);
189bf215546Sopenharmony_ci      so->texconst1 = A3XX_TEX_CONST_1_PITCHALIGN(rsc->layout.pitchalign - 4) |
190bf215546Sopenharmony_ci                      A3XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
191bf215546Sopenharmony_ci                      A3XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
192bf215546Sopenharmony_ci   }
193bf215546Sopenharmony_ci   /* when emitted, A3XX_TEX_CONST_2_INDX() must be OR'd in: */
194bf215546Sopenharmony_ci   struct fdl_slice *slice = fd_resource_slice(rsc, lvl);
195bf215546Sopenharmony_ci   so->texconst2 = A3XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));
196bf215546Sopenharmony_ci   switch (prsc->target) {
197bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
198bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
199bf215546Sopenharmony_ci      so->texconst3 = A3XX_TEX_CONST_3_DEPTH(prsc->array_size - 1) |
200bf215546Sopenharmony_ci                      A3XX_TEX_CONST_3_LAYERSZ1(slice->size0);
201bf215546Sopenharmony_ci      break;
202bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
203bf215546Sopenharmony_ci      so->texconst3 = A3XX_TEX_CONST_3_DEPTH(u_minify(prsc->depth0, lvl)) |
204bf215546Sopenharmony_ci                      A3XX_TEX_CONST_3_LAYERSZ1(slice->size0);
205bf215546Sopenharmony_ci      so->texconst3 |= A3XX_TEX_CONST_3_LAYERSZ2(
206bf215546Sopenharmony_ci         fd_resource_slice(rsc, prsc->last_level)->size0);
207bf215546Sopenharmony_ci      break;
208bf215546Sopenharmony_ci   default:
209bf215546Sopenharmony_ci      so->texconst3 = 0x00000000;
210bf215546Sopenharmony_ci      break;
211bf215546Sopenharmony_ci   }
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci   return &so->base;
214bf215546Sopenharmony_ci}
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_civoid
217bf215546Sopenharmony_cifd3_texture_init(struct pipe_context *pctx)
218bf215546Sopenharmony_ci{
219bf215546Sopenharmony_ci   pctx->create_sampler_state = fd3_sampler_state_create;
220bf215546Sopenharmony_ci   pctx->bind_sampler_states = fd_sampler_states_bind;
221bf215546Sopenharmony_ci   pctx->create_sampler_view = fd3_sampler_view_create;
222bf215546Sopenharmony_ci   pctx->set_sampler_views = fd_set_sampler_views;
223bf215546Sopenharmony_ci}
224