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