1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci * Copyright © 2018 Google, Inc.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci * Authors:
25bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "pipe/p_state.h"
29bf215546Sopenharmony_ci#include "util/format/u_format.h"
30bf215546Sopenharmony_ci#include "util/hash_table.h"
31bf215546Sopenharmony_ci#include "util/u_inlines.h"
32bf215546Sopenharmony_ci#include "util/u_memory.h"
33bf215546Sopenharmony_ci#include "util/u_string.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci#include "freedreno_dev_info.h"
36bf215546Sopenharmony_ci#include "fd6_emit.h"
37bf215546Sopenharmony_ci#include "fd6_resource.h"
38bf215546Sopenharmony_ci#include "fd6_texture.h"
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic void
41bf215546Sopenharmony_ciremove_tex_entry(struct fd6_context *fd6_ctx, struct hash_entry *entry)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   struct fd6_texture_state *tex = entry->data;
44bf215546Sopenharmony_ci   _mesa_hash_table_remove(fd6_ctx->tex_cache, entry);
45bf215546Sopenharmony_ci   fd6_texture_state_reference(&tex, NULL);
46bf215546Sopenharmony_ci}
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistatic enum a6xx_tex_clamp
49bf215546Sopenharmony_citex_clamp(unsigned wrap, bool *needs_border)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci   switch (wrap) {
52bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_REPEAT:
53bf215546Sopenharmony_ci      return A6XX_TEX_REPEAT;
54bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
55bf215546Sopenharmony_ci      return A6XX_TEX_CLAMP_TO_EDGE;
56bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
57bf215546Sopenharmony_ci      *needs_border = true;
58bf215546Sopenharmony_ci      return A6XX_TEX_CLAMP_TO_BORDER;
59bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
60bf215546Sopenharmony_ci      /* only works for PoT.. need to emulate otherwise! */
61bf215546Sopenharmony_ci      return A6XX_TEX_MIRROR_CLAMP;
62bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_REPEAT:
63bf215546Sopenharmony_ci      return A6XX_TEX_MIRROR_REPEAT;
64bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP:
65bf215546Sopenharmony_ci   case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
66bf215546Sopenharmony_ci      /* these two we could perhaps emulate, but we currently
67bf215546Sopenharmony_ci       * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
68bf215546Sopenharmony_ci       */
69bf215546Sopenharmony_ci   default:
70bf215546Sopenharmony_ci      DBG("invalid wrap: %u", wrap);
71bf215546Sopenharmony_ci      return 0;
72bf215546Sopenharmony_ci   }
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistatic enum a6xx_tex_filter
76bf215546Sopenharmony_citex_filter(unsigned filter, bool aniso)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci   switch (filter) {
79bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_NEAREST:
80bf215546Sopenharmony_ci      return A6XX_TEX_NEAREST;
81bf215546Sopenharmony_ci   case PIPE_TEX_FILTER_LINEAR:
82bf215546Sopenharmony_ci      return aniso ? A6XX_TEX_ANISO : A6XX_TEX_LINEAR;
83bf215546Sopenharmony_ci   default:
84bf215546Sopenharmony_ci      DBG("invalid filter: %u", filter);
85bf215546Sopenharmony_ci      return 0;
86bf215546Sopenharmony_ci   }
87bf215546Sopenharmony_ci}
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_cistatic void *
90bf215546Sopenharmony_cifd6_sampler_state_create(struct pipe_context *pctx,
91bf215546Sopenharmony_ci                         const struct pipe_sampler_state *cso)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   struct fd6_sampler_stateobj *so = CALLOC_STRUCT(fd6_sampler_stateobj);
94bf215546Sopenharmony_ci   unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
95bf215546Sopenharmony_ci   bool miplinear = false;
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_ci   if (!so)
98bf215546Sopenharmony_ci      return NULL;
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_ci   so->base = *cso;
101bf215546Sopenharmony_ci   so->seqno = ++fd6_context(fd_context(pctx))->tex_seqno;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci   if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
104bf215546Sopenharmony_ci      miplinear = true;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   so->needs_border = false;
107bf215546Sopenharmony_ci   so->texsamp0 =
108bf215546Sopenharmony_ci      COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
109bf215546Sopenharmony_ci      A6XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
110bf215546Sopenharmony_ci      A6XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
111bf215546Sopenharmony_ci      A6XX_TEX_SAMP_0_ANISO(aniso) |
112bf215546Sopenharmony_ci      A6XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, &so->needs_border)) |
113bf215546Sopenharmony_ci      A6XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, &so->needs_border)) |
114bf215546Sopenharmony_ci      A6XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, &so->needs_border));
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   so->texsamp1 =
117bf215546Sopenharmony_ci      COND(cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE,
118bf215546Sopenharmony_ci           A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |
119bf215546Sopenharmony_ci      COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |
120bf215546Sopenharmony_ci      COND(!cso->normalized_coords, A6XX_TEX_SAMP_1_UNNORM_COORDS);
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   so->texsamp0 |= A6XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);
123bf215546Sopenharmony_ci   so->texsamp1 |= A6XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
124bf215546Sopenharmony_ci                   A6XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   if (cso->compare_mode)
127bf215546Sopenharmony_ci      so->texsamp1 |=
128bf215546Sopenharmony_ci         A6XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   return so;
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_cistatic void
134bf215546Sopenharmony_cifd6_sampler_state_delete(struct pipe_context *pctx, void *hwcso)
135bf215546Sopenharmony_ci{
136bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
137bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
138bf215546Sopenharmony_ci   struct fd6_sampler_stateobj *samp = hwcso;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   fd_screen_lock(ctx->screen);
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci   hash_table_foreach (fd6_ctx->tex_cache, entry) {
143bf215546Sopenharmony_ci      struct fd6_texture_state *state = entry->data;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci      for (unsigned i = 0; i < ARRAY_SIZE(state->key.samp); i++) {
146bf215546Sopenharmony_ci         if (samp->seqno == state->key.samp[i].seqno) {
147bf215546Sopenharmony_ci            remove_tex_entry(fd6_ctx, entry);
148bf215546Sopenharmony_ci            break;
149bf215546Sopenharmony_ci         }
150bf215546Sopenharmony_ci      }
151bf215546Sopenharmony_ci   }
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci   fd_screen_unlock(ctx->screen);
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   free(hwcso);
156bf215546Sopenharmony_ci}
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_cistatic struct pipe_sampler_view *
159bf215546Sopenharmony_cifd6_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
160bf215546Sopenharmony_ci                        const struct pipe_sampler_view *cso)
161bf215546Sopenharmony_ci{
162bf215546Sopenharmony_ci   struct fd6_pipe_sampler_view *so = CALLOC_STRUCT(fd6_pipe_sampler_view);
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   if (!so)
165bf215546Sopenharmony_ci      return NULL;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci   so->base = *cso;
168bf215546Sopenharmony_ci   pipe_reference(NULL, &prsc->reference);
169bf215546Sopenharmony_ci   so->base.texture = prsc;
170bf215546Sopenharmony_ci   so->base.reference.count = 1;
171bf215546Sopenharmony_ci   so->base.context = pctx;
172bf215546Sopenharmony_ci   so->needs_validate = true;
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   return &so->base;
175bf215546Sopenharmony_ci}
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_cistatic void
178bf215546Sopenharmony_cifd6_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
179bf215546Sopenharmony_ci                      unsigned start, unsigned nr,
180bf215546Sopenharmony_ci                      unsigned unbind_num_trailing_slots,
181bf215546Sopenharmony_ci                      bool take_ownership,
182bf215546Sopenharmony_ci                      struct pipe_sampler_view **views) in_dt
183bf215546Sopenharmony_ci{
184bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   fd_set_sampler_views(pctx, shader, start, nr, unbind_num_trailing_slots,
187bf215546Sopenharmony_ci                        take_ownership, views);
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci   if (!views)
190bf215546Sopenharmony_ci      return;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   for (unsigned i = 0; i < nr; i++) {
193bf215546Sopenharmony_ci      struct fd6_pipe_sampler_view *so = fd6_pipe_sampler_view(views[i]);
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci      if (!(so && so->needs_validate))
196bf215546Sopenharmony_ci         continue;
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci      struct fd_resource *rsc = fd_resource(so->base.texture);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci      fd6_validate_format(ctx, rsc, so->base.format);
201bf215546Sopenharmony_ci      fd6_sampler_view_update(ctx, so);
202bf215546Sopenharmony_ci
203bf215546Sopenharmony_ci      so->needs_validate = false;
204bf215546Sopenharmony_ci   }
205bf215546Sopenharmony_ci}
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_civoid
208bf215546Sopenharmony_cifd6_sampler_view_update(struct fd_context *ctx,
209bf215546Sopenharmony_ci                        struct fd6_pipe_sampler_view *so)
210bf215546Sopenharmony_ci{
211bf215546Sopenharmony_ci   const struct pipe_sampler_view *cso = &so->base;
212bf215546Sopenharmony_ci   struct pipe_resource *prsc = cso->texture;
213bf215546Sopenharmony_ci   struct fd_resource *rsc = fd_resource(prsc);
214bf215546Sopenharmony_ci   enum pipe_format format = cso->format;
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_ci   fd6_validate_format(ctx, rsc, cso->format);
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   if (format == PIPE_FORMAT_X32_S8X24_UINT) {
219bf215546Sopenharmony_ci      rsc = rsc->stencil;
220bf215546Sopenharmony_ci      format = rsc->b.b.format;
221bf215546Sopenharmony_ci   }
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_ci   so->seqno = ++fd6_context(ctx)->tex_seqno;
224bf215546Sopenharmony_ci   so->ptr1 = rsc;
225bf215546Sopenharmony_ci   so->rsc_seqno = rsc->seqno;
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   if (cso->target == PIPE_BUFFER) {
228bf215546Sopenharmony_ci      uint8_t swiz[4] = {cso->swizzle_r, cso->swizzle_g, cso->swizzle_b,
229bf215546Sopenharmony_ci                         cso->swizzle_a};
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci      /* Using relocs for addresses still */
232bf215546Sopenharmony_ci      uint64_t iova = cso->u.buf.offset;
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci      fdl6_buffer_view_init(so->descriptor, cso->format, swiz, iova,
235bf215546Sopenharmony_ci                            cso->u.buf.size);
236bf215546Sopenharmony_ci   } else {
237bf215546Sopenharmony_ci      struct fdl_view_args args = {
238bf215546Sopenharmony_ci         /* Using relocs for addresses still */
239bf215546Sopenharmony_ci         .iova = 0,
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci         .base_miplevel = fd_sampler_first_level(cso),
242bf215546Sopenharmony_ci         .level_count =
243bf215546Sopenharmony_ci            fd_sampler_last_level(cso) - fd_sampler_first_level(cso) + 1,
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci         .base_array_layer = cso->u.tex.first_layer,
246bf215546Sopenharmony_ci         .layer_count = cso->u.tex.last_layer - cso->u.tex.first_layer + 1,
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci         .format = format,
249bf215546Sopenharmony_ci         .swiz = {cso->swizzle_r, cso->swizzle_g, cso->swizzle_b,
250bf215546Sopenharmony_ci                  cso->swizzle_a},
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci         .type = fdl_type_from_pipe_target(cso->target),
253bf215546Sopenharmony_ci         .chroma_offsets = {FDL_CHROMA_LOCATION_COSITED_EVEN,
254bf215546Sopenharmony_ci                            FDL_CHROMA_LOCATION_COSITED_EVEN},
255bf215546Sopenharmony_ci      };
256bf215546Sopenharmony_ci      struct fd_resource *plane1 = fd_resource(rsc->b.b.next);
257bf215546Sopenharmony_ci      struct fd_resource *plane2 =
258bf215546Sopenharmony_ci         plane1 ? fd_resource(plane1->b.b.next) : NULL;
259bf215546Sopenharmony_ci      static const struct fdl_layout dummy_layout = {0};
260bf215546Sopenharmony_ci      const struct fdl_layout *layouts[3] = {
261bf215546Sopenharmony_ci         &rsc->layout,
262bf215546Sopenharmony_ci         plane1 ? &plane1->layout : &dummy_layout,
263bf215546Sopenharmony_ci         plane2 ? &plane2->layout : &dummy_layout,
264bf215546Sopenharmony_ci      };
265bf215546Sopenharmony_ci      struct fdl6_view view;
266bf215546Sopenharmony_ci      fdl6_view_init(&view, layouts, &args,
267bf215546Sopenharmony_ci                     ctx->screen->info->a6xx.has_z24uint_s8uint);
268bf215546Sopenharmony_ci      memcpy(so->descriptor, view.descriptor, sizeof(so->descriptor));
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci      if (rsc->b.b.format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
271bf215546Sopenharmony_ci         /* In case of biplanar R8_G8B8, the UBWC metadata address in
272bf215546Sopenharmony_ci          * dwords 7 and 8, is instead the pointer to the second plane.
273bf215546Sopenharmony_ci          */
274bf215546Sopenharmony_ci         so->ptr2 = plane1;
275bf215546Sopenharmony_ci      } else {
276bf215546Sopenharmony_ci         if (fd_resource_ubwc_enabled(rsc, fd_sampler_first_level(cso))) {
277bf215546Sopenharmony_ci            so->ptr2 = rsc;
278bf215546Sopenharmony_ci         }
279bf215546Sopenharmony_ci      }
280bf215546Sopenharmony_ci   }
281bf215546Sopenharmony_ci}
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci/* NOTE this can be called in either driver thread or frontend thread
284bf215546Sopenharmony_ci * depending on where the last unref comes from
285bf215546Sopenharmony_ci */
286bf215546Sopenharmony_cistatic void
287bf215546Sopenharmony_cifd6_sampler_view_destroy(struct pipe_context *pctx,
288bf215546Sopenharmony_ci                         struct pipe_sampler_view *_view)
289bf215546Sopenharmony_ci{
290bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
291bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
292bf215546Sopenharmony_ci   struct fd6_pipe_sampler_view *view = fd6_pipe_sampler_view(_view);
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   fd_screen_lock(ctx->screen);
295bf215546Sopenharmony_ci
296bf215546Sopenharmony_ci   hash_table_foreach (fd6_ctx->tex_cache, entry) {
297bf215546Sopenharmony_ci      struct fd6_texture_state *state = entry->data;
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci      for (unsigned i = 0; i < ARRAY_SIZE(state->key.view); i++) {
300bf215546Sopenharmony_ci         if (view->seqno == state->key.view[i].seqno) {
301bf215546Sopenharmony_ci            remove_tex_entry(fd6_ctx, entry);
302bf215546Sopenharmony_ci            break;
303bf215546Sopenharmony_ci         }
304bf215546Sopenharmony_ci      }
305bf215546Sopenharmony_ci   }
306bf215546Sopenharmony_ci
307bf215546Sopenharmony_ci   fd_screen_unlock(ctx->screen);
308bf215546Sopenharmony_ci
309bf215546Sopenharmony_ci   pipe_resource_reference(&view->base.texture, NULL);
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_ci   free(view);
312bf215546Sopenharmony_ci}
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_cistatic uint32_t
315bf215546Sopenharmony_cikey_hash(const void *_key)
316bf215546Sopenharmony_ci{
317bf215546Sopenharmony_ci   const struct fd6_texture_key *key = _key;
318bf215546Sopenharmony_ci   return XXH32(key, sizeof(*key), 0);
319bf215546Sopenharmony_ci}
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_cistatic bool
322bf215546Sopenharmony_cikey_equals(const void *_a, const void *_b)
323bf215546Sopenharmony_ci{
324bf215546Sopenharmony_ci   const struct fd6_texture_key *a = _a;
325bf215546Sopenharmony_ci   const struct fd6_texture_key *b = _b;
326bf215546Sopenharmony_ci   return memcmp(a, b, sizeof(struct fd6_texture_key)) == 0;
327bf215546Sopenharmony_ci}
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_cistruct fd6_texture_state *
330bf215546Sopenharmony_cifd6_texture_state(struct fd_context *ctx, enum pipe_shader_type type,
331bf215546Sopenharmony_ci                  struct fd_texture_stateobj *tex)
332bf215546Sopenharmony_ci{
333bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
334bf215546Sopenharmony_ci   struct fd6_texture_state *state = NULL;
335bf215546Sopenharmony_ci   struct fd6_texture_key key;
336bf215546Sopenharmony_ci   bool needs_border = false;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci   memset(&key, 0, sizeof(key));
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_ci   for (unsigned i = 0; i < tex->num_textures; i++) {
341bf215546Sopenharmony_ci      if (!tex->textures[i])
342bf215546Sopenharmony_ci         continue;
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci      struct fd6_pipe_sampler_view *view =
345bf215546Sopenharmony_ci         fd6_pipe_sampler_view(tex->textures[i]);
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci      /* NOTE that if the backing rsc was uncompressed between the
348bf215546Sopenharmony_ci       * time that the CSO was originally created and now, the rsc
349bf215546Sopenharmony_ci       * seqno would have changed, so we don't have to worry about
350bf215546Sopenharmony_ci       * getting a bogus cache hit.
351bf215546Sopenharmony_ci       */
352bf215546Sopenharmony_ci      key.view[i].rsc_seqno = fd_resource(view->base.texture)->seqno;
353bf215546Sopenharmony_ci      key.view[i].seqno = view->seqno;
354bf215546Sopenharmony_ci   }
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci   for (unsigned i = 0; i < tex->num_samplers; i++) {
357bf215546Sopenharmony_ci      if (!tex->samplers[i])
358bf215546Sopenharmony_ci         continue;
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci      struct fd6_sampler_stateobj *sampler =
361bf215546Sopenharmony_ci         fd6_sampler_stateobj(tex->samplers[i]);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci      key.samp[i].seqno = sampler->seqno;
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci      needs_border |= sampler->needs_border;
366bf215546Sopenharmony_ci   }
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci   key.type = type;
369bf215546Sopenharmony_ci   key.bcolor_offset = fd6_border_color_offset(ctx, type, tex);
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   uint32_t hash = key_hash(&key);
372bf215546Sopenharmony_ci   fd_screen_lock(ctx->screen);
373bf215546Sopenharmony_ci   struct hash_entry *entry =
374bf215546Sopenharmony_ci      _mesa_hash_table_search_pre_hashed(fd6_ctx->tex_cache, hash, &key);
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   if (entry) {
377bf215546Sopenharmony_ci      fd6_texture_state_reference(&state, entry->data);
378bf215546Sopenharmony_ci      goto out_unlock;
379bf215546Sopenharmony_ci   }
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci   state = CALLOC_STRUCT(fd6_texture_state);
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   /* NOTE: one ref for tex_cache, and second ref for returned state: */
384bf215546Sopenharmony_ci   pipe_reference_init(&state->reference, 2);
385bf215546Sopenharmony_ci   state->key = key;
386bf215546Sopenharmony_ci   state->stateobj = fd_ringbuffer_new_object(ctx->pipe, 32 * 4);
387bf215546Sopenharmony_ci   state->needs_border = needs_border;
388bf215546Sopenharmony_ci
389bf215546Sopenharmony_ci   fd6_emit_textures(ctx, state->stateobj, type, tex, key.bcolor_offset, NULL);
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci   /* NOTE: uses copy of key in state obj, because pointer passed by caller
392bf215546Sopenharmony_ci    * is probably on the stack
393bf215546Sopenharmony_ci    */
394bf215546Sopenharmony_ci   _mesa_hash_table_insert_pre_hashed(fd6_ctx->tex_cache, hash, &state->key,
395bf215546Sopenharmony_ci                                      state);
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ciout_unlock:
398bf215546Sopenharmony_ci   fd_screen_unlock(ctx->screen);
399bf215546Sopenharmony_ci   return state;
400bf215546Sopenharmony_ci}
401bf215546Sopenharmony_ci
402bf215546Sopenharmony_civoid
403bf215546Sopenharmony_ci__fd6_texture_state_describe(char *buf, const struct fd6_texture_state *tex)
404bf215546Sopenharmony_ci{
405bf215546Sopenharmony_ci   sprintf(buf, "fd6_texture_state<%p>", tex);
406bf215546Sopenharmony_ci}
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_civoid
409bf215546Sopenharmony_ci__fd6_texture_state_destroy(struct fd6_texture_state *state)
410bf215546Sopenharmony_ci{
411bf215546Sopenharmony_ci   fd_ringbuffer_del(state->stateobj);
412bf215546Sopenharmony_ci   free(state);
413bf215546Sopenharmony_ci}
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_cistatic void
416bf215546Sopenharmony_cifd6_rebind_resource(struct fd_context *ctx, struct fd_resource *rsc) assert_dt
417bf215546Sopenharmony_ci{
418bf215546Sopenharmony_ci   fd_screen_assert_locked(ctx->screen);
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   if (!(rsc->dirty & FD_DIRTY_TEX))
421bf215546Sopenharmony_ci      return;
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci   hash_table_foreach (fd6_ctx->tex_cache, entry) {
426bf215546Sopenharmony_ci      struct fd6_texture_state *state = entry->data;
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_ci      for (unsigned i = 0; i < ARRAY_SIZE(state->key.view); i++) {
429bf215546Sopenharmony_ci         if (rsc->seqno == state->key.view[i].rsc_seqno) {
430bf215546Sopenharmony_ci            remove_tex_entry(fd6_ctx, entry);
431bf215546Sopenharmony_ci            break;
432bf215546Sopenharmony_ci         }
433bf215546Sopenharmony_ci      }
434bf215546Sopenharmony_ci   }
435bf215546Sopenharmony_ci}
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_civoid
438bf215546Sopenharmony_cifd6_texture_init(struct pipe_context *pctx) disable_thread_safety_analysis
439bf215546Sopenharmony_ci{
440bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
441bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci   pctx->create_sampler_state = fd6_sampler_state_create;
444bf215546Sopenharmony_ci   pctx->delete_sampler_state = fd6_sampler_state_delete;
445bf215546Sopenharmony_ci   pctx->bind_sampler_states = fd_sampler_states_bind;
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci   pctx->create_sampler_view = fd6_sampler_view_create;
448bf215546Sopenharmony_ci   pctx->sampler_view_destroy = fd6_sampler_view_destroy;
449bf215546Sopenharmony_ci   pctx->set_sampler_views = fd6_set_sampler_views;
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_ci   ctx->rebind_resource = fd6_rebind_resource;
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci   fd6_ctx->tex_cache = _mesa_hash_table_create(NULL, key_hash, key_equals);
454bf215546Sopenharmony_ci}
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_civoid
457bf215546Sopenharmony_cifd6_texture_fini(struct pipe_context *pctx)
458bf215546Sopenharmony_ci{
459bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
460bf215546Sopenharmony_ci   struct fd6_context *fd6_ctx = fd6_context(ctx);
461bf215546Sopenharmony_ci
462bf215546Sopenharmony_ci   fd_screen_lock(ctx->screen);
463bf215546Sopenharmony_ci
464bf215546Sopenharmony_ci   hash_table_foreach (fd6_ctx->tex_cache, entry) {
465bf215546Sopenharmony_ci      remove_tex_entry(fd6_ctx, entry);
466bf215546Sopenharmony_ci   }
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_ci   fd_screen_unlock(ctx->screen);
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci   ralloc_free(fd6_ctx->tex_cache);
471bf215546Sopenharmony_ci}
472