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/u_string.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
32 #include "util/format/u_format.h"
33 #include "util/hash_table.h"
34
35 #include "fd6_texture.h"
36 #include "fd6_resource.h"
37 #include "fd6_format.h"
38 #include "fd6_emit.h"
39
40 static void fd6_texture_state_destroy(struct fd6_texture_state *state);
41
42 static enum a6xx_tex_clamp
tex_clamp(unsigned wrap,bool clamp_to_edge,bool * needs_border)43 tex_clamp(unsigned wrap, bool clamp_to_edge, bool *needs_border)
44 {
45 /* Hardware does not support _CLAMP, but we emulate it: */
46 if (wrap == PIPE_TEX_WRAP_CLAMP) {
47 wrap = (clamp_to_edge) ?
48 PIPE_TEX_WRAP_CLAMP_TO_EDGE : PIPE_TEX_WRAP_CLAMP_TO_BORDER;
49 }
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 bool clamp_to_edge;
97
98 if (!so)
99 return NULL;
100
101 so->base = *cso;
102 so->seqno = ++fd6_context(fd_context(pctx))->tex_seqno;
103
104 if (cso->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
105 miplinear = true;
106
107 /*
108 * For nearest filtering, _CLAMP means _CLAMP_TO_EDGE; for linear
109 * filtering, _CLAMP means _CLAMP_TO_BORDER while additionally
110 * clamping the texture coordinates to [0.0, 1.0].
111 *
112 * The clamping will be taken care of in the shaders. There are two
113 * filters here, but let the minification one has a say.
114 */
115 clamp_to_edge = (cso->min_img_filter == PIPE_TEX_FILTER_NEAREST);
116 if (!clamp_to_edge) {
117 so->saturate_s = (cso->wrap_s == PIPE_TEX_WRAP_CLAMP);
118 so->saturate_t = (cso->wrap_t == PIPE_TEX_WRAP_CLAMP);
119 so->saturate_r = (cso->wrap_r == PIPE_TEX_WRAP_CLAMP);
120 }
121
122 so->needs_border = false;
123 so->texsamp0 =
124 COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
125 A6XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
126 A6XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
127 A6XX_TEX_SAMP_0_ANISO(aniso) |
128 A6XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, clamp_to_edge, &so->needs_border)) |
129 A6XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, clamp_to_edge, &so->needs_border)) |
130 A6XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, clamp_to_edge, &so->needs_border));
131
132 so->texsamp1 =
133 COND(cso->min_mip_filter == PIPE_TEX_MIPFILTER_NONE,
134 A6XX_TEX_SAMP_1_MIPFILTER_LINEAR_FAR) |
135 COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |
136 COND(!cso->normalized_coords, A6XX_TEX_SAMP_1_UNNORM_COORDS);
137
138 so->texsamp0 |= A6XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);
139 so->texsamp1 |=
140 A6XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
141 A6XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
142
143 if (cso->compare_mode)
144 so->texsamp1 |= A6XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
145
146 return so;
147 }
148
149 static void
fd6_sampler_state_delete(struct pipe_context * pctx,void * hwcso)150 fd6_sampler_state_delete(struct pipe_context *pctx, void *hwcso)
151 {
152 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
153 struct fd6_sampler_stateobj *samp = hwcso;
154
155 hash_table_foreach(fd6_ctx->tex_cache, entry) {
156 struct fd6_texture_state *state = entry->data;
157
158 for (unsigned i = 0; i < ARRAY_SIZE(state->key.samp); i++) {
159 if (samp->seqno == state->key.samp[i].seqno) {
160 fd6_texture_state_destroy(entry->data);
161 _mesa_hash_table_remove(fd6_ctx->tex_cache, entry);
162 break;
163 }
164 }
165 }
166
167 free(hwcso);
168 }
169
170 static void
fd6_sampler_states_bind(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start,unsigned nr,void ** hwcso)171 fd6_sampler_states_bind(struct pipe_context *pctx,
172 enum pipe_shader_type shader, unsigned start,
173 unsigned nr, void **hwcso)
174 {
175 struct fd_context *ctx = fd_context(pctx);
176 struct fd6_context *fd6_ctx = fd6_context(ctx);
177 uint16_t saturate_s = 0, saturate_t = 0, saturate_r = 0;
178 unsigned i;
179
180 if (!hwcso)
181 nr = 0;
182
183 for (i = 0; i < nr; i++) {
184 if (hwcso[i]) {
185 struct fd6_sampler_stateobj *sampler =
186 fd6_sampler_stateobj(hwcso[i]);
187 if (sampler->saturate_s)
188 saturate_s |= (1 << i);
189 if (sampler->saturate_t)
190 saturate_t |= (1 << i);
191 if (sampler->saturate_r)
192 saturate_r |= (1 << i);
193 }
194 }
195
196 fd_sampler_states_bind(pctx, shader, start, nr, hwcso);
197
198 if (shader == PIPE_SHADER_FRAGMENT) {
199 fd6_ctx->fsaturate =
200 (saturate_s != 0) ||
201 (saturate_t != 0) ||
202 (saturate_r != 0);
203 fd6_ctx->fsaturate_s = saturate_s;
204 fd6_ctx->fsaturate_t = saturate_t;
205 fd6_ctx->fsaturate_r = saturate_r;
206 } else if (shader == PIPE_SHADER_VERTEX) {
207 fd6_ctx->vsaturate =
208 (saturate_s != 0) ||
209 (saturate_t != 0) ||
210 (saturate_r != 0);
211 fd6_ctx->vsaturate_s = saturate_s;
212 fd6_ctx->vsaturate_t = saturate_t;
213 fd6_ctx->vsaturate_r = saturate_r;
214 }
215 }
216
217 static struct pipe_sampler_view *
fd6_sampler_view_create(struct pipe_context * pctx,struct pipe_resource * prsc,const struct pipe_sampler_view * cso)218 fd6_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
219 const struct pipe_sampler_view *cso)
220 {
221 struct fd6_pipe_sampler_view *so = CALLOC_STRUCT(fd6_pipe_sampler_view);
222 struct fd_resource *rsc = fd_resource(prsc);
223 enum pipe_format format = cso->format;
224 bool ubwc_enabled = false;
225 unsigned lvl, layers = 0;
226
227 if (!so)
228 return NULL;
229
230 fd6_validate_format(fd_context(pctx), rsc, format);
231
232 if (format == PIPE_FORMAT_X32_S8X24_UINT) {
233 rsc = rsc->stencil;
234 format = rsc->base.format;
235 }
236
237 so->base = *cso;
238 pipe_reference(NULL, &prsc->reference);
239 so->base.texture = prsc;
240 so->base.reference.count = 1;
241 so->base.context = pctx;
242 so->seqno = ++fd6_context(fd_context(pctx))->tex_seqno;
243 so->ptr1 = rsc;
244
245 if (cso->target == PIPE_BUFFER) {
246 unsigned elements = cso->u.buf.size / util_format_get_blocksize(format);
247
248 lvl = 0;
249 so->texconst1 =
250 A6XX_TEX_CONST_1_WIDTH(elements & MASK(15)) |
251 A6XX_TEX_CONST_1_HEIGHT(elements >> 15);
252 so->texconst2 =
253 A6XX_TEX_CONST_2_UNK4 |
254 A6XX_TEX_CONST_2_UNK31;
255 so->offset1 = cso->u.buf.offset;
256 } else {
257 unsigned miplevels;
258
259 lvl = fd_sampler_first_level(cso);
260 miplevels = fd_sampler_last_level(cso) - lvl;
261 layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1;
262
263 so->texconst0 |= A6XX_TEX_CONST_0_MIPLVLS(miplevels);
264 so->texconst1 =
265 A6XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
266 A6XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
267 so->texconst2 =
268 A6XX_TEX_CONST_2_PITCHALIGN(rsc->layout.pitchalign - 6) |
269 A6XX_TEX_CONST_2_PITCH(fd_resource_pitch(rsc, lvl));
270
271 ubwc_enabled = fd_resource_ubwc_enabled(rsc, lvl);
272
273 if (rsc->base.format == PIPE_FORMAT_R8_G8B8_420_UNORM) {
274 struct fd_resource *next = fd_resource(rsc->base.next);
275
276 /* In case of biplanar R8_G8B8, the UBWC metadata address in
277 * dwords 7 and 8, is instead the pointer to the second plane.
278 */
279 so->ptr2 = next;
280 so->texconst6 =
281 A6XX_TEX_CONST_6_PLANE_PITCH(fd_resource_pitch(next, lvl));
282
283 if (ubwc_enabled) {
284 /* Further, if using UBWC with R8_G8B8, we only point to the
285 * UBWC header and the color data is expected to follow immediately.
286 */
287 so->offset1 =
288 fd_resource_ubwc_offset(rsc, lvl, cso->u.tex.first_layer);
289 so->offset2 =
290 fd_resource_ubwc_offset(next, lvl, cso->u.tex.first_layer);
291 } else {
292 so->offset1 = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);
293 so->offset2 = fd_resource_offset(next, lvl, cso->u.tex.first_layer);
294 }
295 } else {
296 so->offset1 = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);
297 if (ubwc_enabled) {
298 so->ptr2 = rsc;
299 so->offset2 = fd_resource_ubwc_offset(rsc, lvl, cso->u.tex.first_layer);
300 }
301 }
302 }
303
304 so->texconst0 |= fd6_tex_const_0(prsc, lvl, cso->format,
305 cso->swizzle_r, cso->swizzle_g,
306 cso->swizzle_b, cso->swizzle_a);
307
308 so->texconst2 |= A6XX_TEX_CONST_2_TYPE(fd6_tex_type(cso->target));
309
310 switch (cso->target) {
311 case PIPE_TEXTURE_RECT:
312 case PIPE_TEXTURE_1D:
313 case PIPE_TEXTURE_2D:
314 so->texconst3 =
315 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);
316 so->texconst5 =
317 A6XX_TEX_CONST_5_DEPTH(1);
318 break;
319 case PIPE_TEXTURE_1D_ARRAY:
320 case PIPE_TEXTURE_2D_ARRAY:
321 so->texconst3 =
322 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);
323 so->texconst5 =
324 A6XX_TEX_CONST_5_DEPTH(layers);
325 break;
326 case PIPE_TEXTURE_CUBE:
327 case PIPE_TEXTURE_CUBE_ARRAY:
328 so->texconst3 =
329 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layout.layer_size);
330 so->texconst5 =
331 A6XX_TEX_CONST_5_DEPTH(layers / 6);
332 break;
333 case PIPE_TEXTURE_3D:
334 so->texconst3 =
335 A6XX_TEX_CONST_3_MIN_LAYERSZ(
336 fd_resource_slice(rsc, prsc->last_level)->size0) |
337 A6XX_TEX_CONST_3_ARRAY_PITCH(fd_resource_slice(rsc, lvl)->size0);
338 so->texconst5 =
339 A6XX_TEX_CONST_5_DEPTH(u_minify(prsc->depth0, lvl));
340 break;
341 default:
342 break;
343 }
344
345 if (rsc->layout.tile_all)
346 so->texconst3 |= A6XX_TEX_CONST_3_TILE_ALL;
347
348 if (ubwc_enabled) {
349 uint32_t block_width, block_height;
350 fdl6_get_ubwc_blockwidth(&rsc->layout, &block_width, &block_height);
351
352 so->texconst3 |= A6XX_TEX_CONST_3_FLAG;
353 so->texconst9 |= A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(rsc->layout.ubwc_layer_size >> 2);
354 so->texconst10 |=
355 A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(fdl_ubwc_pitch(&rsc->layout, lvl)) |
356 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGW(util_logbase2_ceil(DIV_ROUND_UP(u_minify(prsc->width0, lvl), block_width))) |
357 A6XX_TEX_CONST_10_FLAG_BUFFER_LOGH(util_logbase2_ceil(DIV_ROUND_UP(u_minify(prsc->height0, lvl), block_height)));
358 }
359
360 return &so->base;
361 }
362
363 static void
fd6_sampler_view_destroy(struct pipe_context * pctx,struct pipe_sampler_view * _view)364 fd6_sampler_view_destroy(struct pipe_context *pctx,
365 struct pipe_sampler_view *_view)
366 {
367 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
368 struct fd6_pipe_sampler_view *view = fd6_pipe_sampler_view(_view);
369
370 hash_table_foreach(fd6_ctx->tex_cache, entry) {
371 struct fd6_texture_state *state = entry->data;
372
373 for (unsigned i = 0; i < ARRAY_SIZE(state->key.view); i++) {
374 if (view->seqno == state->key.view[i].seqno) {
375 fd6_texture_state_destroy(entry->data);
376 _mesa_hash_table_remove(fd6_ctx->tex_cache, entry);
377 break;
378 }
379 }
380 }
381
382 pipe_resource_reference(&view->base.texture, NULL);
383
384 free(view);
385 }
386
387
388 static uint32_t
key_hash(const void * _key)389 key_hash(const void *_key)
390 {
391 const struct fd6_texture_key *key = _key;
392 return XXH32(key, sizeof(*key), 0);
393 }
394
395 static bool
key_equals(const void * _a,const void * _b)396 key_equals(const void *_a, const void *_b)
397 {
398 const struct fd6_texture_key *a = _a;
399 const struct fd6_texture_key *b = _b;
400 return memcmp(a, b, sizeof(struct fd6_texture_key)) == 0;
401 }
402
403 struct fd6_texture_state *
fd6_texture_state(struct fd_context * ctx,enum pipe_shader_type type,struct fd_texture_stateobj * tex)404 fd6_texture_state(struct fd_context *ctx, enum pipe_shader_type type,
405 struct fd_texture_stateobj *tex)
406 {
407 struct fd6_context *fd6_ctx = fd6_context(ctx);
408 struct fd6_texture_key key;
409 bool needs_border = false;
410
411 memset(&key, 0, sizeof(key));
412
413 for (unsigned i = 0; i < tex->num_textures; i++) {
414 if (!tex->textures[i])
415 continue;
416
417 struct fd6_pipe_sampler_view *view =
418 fd6_pipe_sampler_view(tex->textures[i]);
419
420 key.view[i].rsc_seqno = fd_resource(view->base.texture)->seqno;
421 key.view[i].seqno = view->seqno;
422 }
423
424 for (unsigned i = 0; i < tex->num_samplers; i++) {
425 if (!tex->samplers[i])
426 continue;
427
428 struct fd6_sampler_stateobj *sampler =
429 fd6_sampler_stateobj(tex->samplers[i]);
430
431 key.samp[i].seqno = sampler->seqno;
432
433 needs_border |= sampler->needs_border;
434 }
435
436 key.type = type;
437 key.bcolor_offset = fd6_border_color_offset(ctx, type, tex);
438
439 uint32_t hash = key_hash(&key);
440 struct hash_entry *entry =
441 _mesa_hash_table_search_pre_hashed(fd6_ctx->tex_cache, hash, &key);
442
443 if (entry) {
444 return entry->data;
445 }
446
447 struct fd6_texture_state *state = CALLOC_STRUCT(fd6_texture_state);
448
449 state->key = key;
450 state->stateobj = fd_ringbuffer_new_object(ctx->pipe, 0x1000);
451 state->needs_border = needs_border;
452
453 fd6_emit_textures(ctx->pipe, state->stateobj, type, tex, key.bcolor_offset,
454 NULL, NULL);
455
456 /* NOTE: uses copy of key in state obj, because pointer passed by caller
457 * is probably on the stack
458 */
459 _mesa_hash_table_insert_pre_hashed(fd6_ctx->tex_cache, hash,
460 &state->key, state);
461
462 return state;
463 }
464
465 static void
fd6_texture_state_destroy(struct fd6_texture_state * state)466 fd6_texture_state_destroy(struct fd6_texture_state *state)
467 {
468 fd_ringbuffer_del(state->stateobj);
469 free(state);
470 }
471
472 static void
fd6_rebind_resource(struct fd_context * ctx,struct fd_resource * rsc)473 fd6_rebind_resource(struct fd_context *ctx, struct fd_resource *rsc)
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 fd6_texture_state_destroy(entry->data);
486 _mesa_hash_table_remove(fd6_ctx->tex_cache, entry);
487 break;
488 }
489 }
490 }
491 }
492
493 void
fd6_texture_init(struct pipe_context * pctx)494 fd6_texture_init(struct pipe_context *pctx)
495 {
496 struct fd_context *ctx = fd_context(pctx);
497 struct fd6_context *fd6_ctx = fd6_context(ctx);
498
499 pctx->create_sampler_state = fd6_sampler_state_create;
500 pctx->delete_sampler_state = fd6_sampler_state_delete;
501 pctx->bind_sampler_states = fd6_sampler_states_bind;
502
503 pctx->create_sampler_view = fd6_sampler_view_create;
504 pctx->sampler_view_destroy = fd6_sampler_view_destroy;
505 pctx->set_sampler_views = fd_set_sampler_views;
506
507 ctx->rebind_resource = fd6_rebind_resource;
508
509 fd6_ctx->tex_cache = _mesa_hash_table_create(NULL, key_hash, key_equals);
510 }
511
512 void
fd6_texture_fini(struct pipe_context * pctx)513 fd6_texture_fini(struct pipe_context *pctx)
514 {
515 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
516
517 hash_table_foreach(fd6_ctx->tex_cache, entry) {
518 fd6_texture_state_destroy(entry->data);
519 }
520 ralloc_free(fd6_ctx->tex_cache);
521 }
522