9c42a6b8151fd17b44b1a2886ba5ab5e94967b70
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_texture.c
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/u_format.h"
33 #include "util/hash_table.h"
34
35 #include "fd6_texture.h"
36 #include "fd6_format.h"
37 #include "fd6_emit.h"
38
39 static void fd6_texture_state_destroy(struct fd6_texture_state *state);
40
41 static enum a6xx_tex_clamp
42 tex_clamp(unsigned wrap, bool clamp_to_edge, bool *needs_border)
43 {
44 /* Hardware does not support _CLAMP, but we emulate it: */
45 if (wrap == PIPE_TEX_WRAP_CLAMP) {
46 wrap = (clamp_to_edge) ?
47 PIPE_TEX_WRAP_CLAMP_TO_EDGE : PIPE_TEX_WRAP_CLAMP_TO_BORDER;
48 }
49
50 switch (wrap) {
51 case PIPE_TEX_WRAP_REPEAT:
52 return A6XX_TEX_REPEAT;
53 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
54 return A6XX_TEX_CLAMP_TO_EDGE;
55 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
56 *needs_border = true;
57 return A6XX_TEX_CLAMP_TO_BORDER;
58 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
59 /* only works for PoT.. need to emulate otherwise! */
60 return A6XX_TEX_MIRROR_CLAMP;
61 case PIPE_TEX_WRAP_MIRROR_REPEAT:
62 return A6XX_TEX_MIRROR_REPEAT;
63 case PIPE_TEX_WRAP_MIRROR_CLAMP:
64 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
65 /* these two we could perhaps emulate, but we currently
66 * just don't advertise PIPE_CAP_TEXTURE_MIRROR_CLAMP
67 */
68 default:
69 DBG("invalid wrap: %u", wrap);
70 return 0;
71 }
72 }
73
74 static enum a6xx_tex_filter
75 tex_filter(unsigned filter, bool aniso)
76 {
77 switch (filter) {
78 case PIPE_TEX_FILTER_NEAREST:
79 return A6XX_TEX_NEAREST;
80 case PIPE_TEX_FILTER_LINEAR:
81 return aniso ? A6XX_TEX_ANISO : A6XX_TEX_LINEAR;
82 default:
83 DBG("invalid filter: %u", filter);
84 return 0;
85 }
86 }
87
88 static void *
89 fd6_sampler_state_create(struct pipe_context *pctx,
90 const struct pipe_sampler_state *cso)
91 {
92 struct fd6_sampler_stateobj *so = CALLOC_STRUCT(fd6_sampler_stateobj);
93 unsigned aniso = util_last_bit(MIN2(cso->max_anisotropy >> 1, 8));
94 bool miplinear = false;
95 bool clamp_to_edge;
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 /*
107 * For nearest filtering, _CLAMP means _CLAMP_TO_EDGE; for linear
108 * filtering, _CLAMP means _CLAMP_TO_BORDER while additionally
109 * clamping the texture coordinates to [0.0, 1.0].
110 *
111 * The clamping will be taken care of in the shaders. There are two
112 * filters here, but let the minification one has a say.
113 */
114 clamp_to_edge = (cso->min_img_filter == PIPE_TEX_FILTER_NEAREST);
115 if (!clamp_to_edge) {
116 so->saturate_s = (cso->wrap_s == PIPE_TEX_WRAP_CLAMP);
117 so->saturate_t = (cso->wrap_t == PIPE_TEX_WRAP_CLAMP);
118 so->saturate_r = (cso->wrap_r == PIPE_TEX_WRAP_CLAMP);
119 }
120
121 so->needs_border = false;
122 so->texsamp0 =
123 COND(miplinear, A6XX_TEX_SAMP_0_MIPFILTER_LINEAR_NEAR) |
124 A6XX_TEX_SAMP_0_XY_MAG(tex_filter(cso->mag_img_filter, aniso)) |
125 A6XX_TEX_SAMP_0_XY_MIN(tex_filter(cso->min_img_filter, aniso)) |
126 A6XX_TEX_SAMP_0_ANISO(aniso) |
127 A6XX_TEX_SAMP_0_WRAP_S(tex_clamp(cso->wrap_s, clamp_to_edge, &so->needs_border)) |
128 A6XX_TEX_SAMP_0_WRAP_T(tex_clamp(cso->wrap_t, clamp_to_edge, &so->needs_border)) |
129 A6XX_TEX_SAMP_0_WRAP_R(tex_clamp(cso->wrap_r, clamp_to_edge, &so->needs_border));
130
131 so->texsamp1 =
132 COND(!cso->seamless_cube_map, A6XX_TEX_SAMP_1_CUBEMAPSEAMLESSFILTOFF) |
133 COND(!cso->normalized_coords, A6XX_TEX_SAMP_1_UNNORM_COORDS);
134
135 if (cso->min_mip_filter != PIPE_TEX_MIPFILTER_NONE) {
136 so->texsamp0 |= A6XX_TEX_SAMP_0_LOD_BIAS(cso->lod_bias);
137 so->texsamp1 |=
138 A6XX_TEX_SAMP_1_MIN_LOD(cso->min_lod) |
139 A6XX_TEX_SAMP_1_MAX_LOD(cso->max_lod);
140 }
141
142 if (cso->compare_mode)
143 so->texsamp1 |= A6XX_TEX_SAMP_1_COMPARE_FUNC(cso->compare_func); /* maps 1:1 */
144
145 return so;
146 }
147
148 static void
149 fd6_sampler_state_delete(struct pipe_context *pctx, void *hwcso)
150 {
151 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
152 struct fd6_sampler_stateobj *samp = hwcso;
153
154 hash_table_foreach(fd6_ctx->tex_cache, entry) {
155 struct fd6_texture_state *state = entry->data;
156
157 for (unsigned i = 0; i < ARRAY_SIZE(state->key.samp); i++) {
158 if (samp->seqno == state->key.samp[i].seqno) {
159 fd6_texture_state_destroy(entry->data);
160 _mesa_hash_table_remove(fd6_ctx->tex_cache, entry);
161 break;
162 }
163 }
164 }
165
166 free(hwcso);
167 }
168
169 static void
170 fd6_sampler_states_bind(struct pipe_context *pctx,
171 enum pipe_shader_type shader, unsigned start,
172 unsigned nr, void **hwcso)
173 {
174 struct fd_context *ctx = fd_context(pctx);
175 struct fd6_context *fd6_ctx = fd6_context(ctx);
176 uint16_t saturate_s = 0, saturate_t = 0, saturate_r = 0;
177 unsigned i;
178
179 if (!hwcso)
180 nr = 0;
181
182 for (i = 0; i < nr; i++) {
183 if (hwcso[i]) {
184 struct fd6_sampler_stateobj *sampler =
185 fd6_sampler_stateobj(hwcso[i]);
186 if (sampler->saturate_s)
187 saturate_s |= (1 << i);
188 if (sampler->saturate_t)
189 saturate_t |= (1 << i);
190 if (sampler->saturate_r)
191 saturate_r |= (1 << i);
192 }
193 }
194
195 fd_sampler_states_bind(pctx, shader, start, nr, hwcso);
196
197 if (shader == PIPE_SHADER_FRAGMENT) {
198 fd6_ctx->fsaturate =
199 (saturate_s != 0) ||
200 (saturate_t != 0) ||
201 (saturate_r != 0);
202 fd6_ctx->fsaturate_s = saturate_s;
203 fd6_ctx->fsaturate_t = saturate_t;
204 fd6_ctx->fsaturate_r = saturate_r;
205 } else if (shader == PIPE_SHADER_VERTEX) {
206 fd6_ctx->vsaturate =
207 (saturate_s != 0) ||
208 (saturate_t != 0) ||
209 (saturate_r != 0);
210 fd6_ctx->vsaturate_s = saturate_s;
211 fd6_ctx->vsaturate_t = saturate_t;
212 fd6_ctx->vsaturate_r = saturate_r;
213 }
214 }
215
216 static struct pipe_sampler_view *
217 fd6_sampler_view_create(struct pipe_context *pctx, struct pipe_resource *prsc,
218 const struct pipe_sampler_view *cso)
219 {
220 struct fd6_pipe_sampler_view *so = CALLOC_STRUCT(fd6_pipe_sampler_view);
221 struct fd_resource *rsc = fd_resource(prsc);
222 enum pipe_format format = cso->format;
223 unsigned lvl, layers;
224
225 if (!so)
226 return NULL;
227
228 if (format == PIPE_FORMAT_X32_S8X24_UINT) {
229 rsc = rsc->stencil;
230 format = rsc->base.format;
231 }
232
233 so->base = *cso;
234 pipe_reference(NULL, &prsc->reference);
235 so->base.texture = prsc;
236 so->base.reference.count = 1;
237 so->base.context = pctx;
238 so->seqno = ++fd6_context(fd_context(pctx))->tex_seqno;
239
240 if (cso->target == PIPE_BUFFER) {
241 unsigned elements = cso->u.buf.size / util_format_get_blocksize(format);
242
243 lvl = 0;
244 so->texconst1 =
245 A6XX_TEX_CONST_1_WIDTH(elements & MASK(15)) |
246 A6XX_TEX_CONST_1_HEIGHT(elements >> 15);
247 so->texconst2 =
248 A6XX_TEX_CONST_2_UNK4 |
249 A6XX_TEX_CONST_2_UNK31;
250 so->offset = cso->u.buf.offset;
251 } else {
252 unsigned miplevels;
253
254 lvl = fd_sampler_first_level(cso);
255 miplevels = fd_sampler_last_level(cso) - lvl;
256 layers = cso->u.tex.last_layer - cso->u.tex.first_layer + 1;
257
258 so->texconst0 |= A6XX_TEX_CONST_0_MIPLVLS(miplevels);
259 so->texconst1 =
260 A6XX_TEX_CONST_1_WIDTH(u_minify(prsc->width0, lvl)) |
261 A6XX_TEX_CONST_1_HEIGHT(u_minify(prsc->height0, lvl));
262 so->texconst2 =
263 A6XX_TEX_CONST_2_FETCHSIZE(fd6_pipe2fetchsize(format)) |
264 A6XX_TEX_CONST_2_PITCH(
265 util_format_get_nblocksx(
266 format, rsc->slices[lvl].pitch) * rsc->cpp);
267 so->offset = fd_resource_offset(rsc, lvl, cso->u.tex.first_layer);
268 so->ubwc_offset = fd_resource_ubwc_offset(rsc, lvl, cso->u.tex.first_layer);
269 so->ubwc_enabled = fd_resource_ubwc_enabled(rsc, lvl);
270 }
271
272 so->texconst0 |= fd6_tex_const_0(prsc, lvl, cso->format,
273 cso->swizzle_r, cso->swizzle_g,
274 cso->swizzle_b, cso->swizzle_a);
275
276 if (so->ubwc_enabled) {
277 so->texconst9 |= A6XX_TEX_CONST_9_FLAG_BUFFER_ARRAY_PITCH(rsc->ubwc_size);
278 so->texconst10 |= A6XX_TEX_CONST_10_FLAG_BUFFER_PITCH(rsc->ubwc_pitch);
279 }
280
281 so->texconst2 |= A6XX_TEX_CONST_2_TYPE(fd6_tex_type(cso->target));
282
283 switch (cso->target) {
284 case PIPE_TEXTURE_RECT:
285 case PIPE_TEXTURE_1D:
286 case PIPE_TEXTURE_2D:
287 so->texconst3 =
288 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layer_size);
289 so->texconst5 =
290 A6XX_TEX_CONST_5_DEPTH(1);
291 break;
292 case PIPE_TEXTURE_1D_ARRAY:
293 case PIPE_TEXTURE_2D_ARRAY:
294 so->texconst3 =
295 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layer_size);
296 so->texconst5 =
297 A6XX_TEX_CONST_5_DEPTH(layers);
298 break;
299 case PIPE_TEXTURE_CUBE:
300 case PIPE_TEXTURE_CUBE_ARRAY:
301 so->texconst3 =
302 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->layer_size);
303 so->texconst5 =
304 A6XX_TEX_CONST_5_DEPTH(layers / 6);
305 break;
306 case PIPE_TEXTURE_3D:
307 so->texconst3 =
308 A6XX_TEX_CONST_3_MIN_LAYERSZ(rsc->slices[prsc->last_level].size0) |
309 A6XX_TEX_CONST_3_ARRAY_PITCH(rsc->slices[lvl].size0);
310 so->texconst5 =
311 A6XX_TEX_CONST_5_DEPTH(u_minify(prsc->depth0, lvl));
312 break;
313 default:
314 break;
315 }
316
317 if (so->ubwc_enabled) {
318 so->texconst3 |= A6XX_TEX_CONST_3_FLAG | A6XX_TEX_CONST_3_UNK27;
319 }
320
321 return &so->base;
322 }
323
324 static void
325 fd6_sampler_view_destroy(struct pipe_context *pctx,
326 struct pipe_sampler_view *_view)
327 {
328 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
329 struct fd6_pipe_sampler_view *view = fd6_pipe_sampler_view(_view);
330
331 hash_table_foreach(fd6_ctx->tex_cache, entry) {
332 struct fd6_texture_state *state = entry->data;
333
334 for (unsigned i = 0; i < ARRAY_SIZE(state->key.view); i++) {
335 if (view->seqno == state->key.view[i].seqno) {
336 fd6_texture_state_destroy(entry->data);
337 _mesa_hash_table_remove(fd6_ctx->tex_cache, entry);
338 break;
339 }
340 }
341 }
342
343 pipe_resource_reference(&view->base.texture, NULL);
344
345 free(view);
346 }
347
348
349 static uint32_t
350 key_hash(const void *_key)
351 {
352 const struct fd6_texture_key *key = _key;
353 uint32_t hash = _mesa_fnv32_1a_offset_bias;
354 hash = _mesa_fnv32_1a_accumulate_block(hash, key, sizeof(*key));
355 return hash;
356 }
357
358 static bool
359 key_equals(const void *_a, const void *_b)
360 {
361 const struct fd6_texture_key *a = _a;
362 const struct fd6_texture_key *b = _b;
363 return memcmp(a, b, sizeof(struct fd6_texture_key)) == 0;
364 }
365
366 struct fd6_texture_state *
367 fd6_texture_state(struct fd_context *ctx, enum pipe_shader_type type,
368 struct fd_texture_stateobj *tex)
369 {
370 struct fd6_context *fd6_ctx = fd6_context(ctx);
371 struct fd6_texture_key key;
372 bool needs_border = false;
373
374 memset(&key, 0, sizeof(key));
375
376 for (unsigned i = 0; i < tex->num_textures; i++) {
377 if (!tex->textures[i])
378 continue;
379
380 struct fd6_pipe_sampler_view *view =
381 fd6_pipe_sampler_view(tex->textures[i]);
382
383 key.view[i].rsc_seqno = fd_resource(view->base.texture)->seqno;
384 key.view[i].seqno = view->seqno;
385 }
386
387 for (unsigned i = 0; i < tex->num_samplers; i++) {
388 if (!tex->samplers[i])
389 continue;
390
391 struct fd6_sampler_stateobj *sampler =
392 fd6_sampler_stateobj(tex->samplers[i]);
393
394 key.samp[i].seqno = sampler->seqno;
395
396 needs_border |= sampler->needs_border;
397 }
398
399 key.bcolor_offset = fd6_border_color_offset(ctx, type, tex);
400
401 uint32_t hash = key_hash(&key);
402 struct hash_entry *entry =
403 _mesa_hash_table_search_pre_hashed(fd6_ctx->tex_cache, hash, &key);
404
405 if (entry) {
406 return entry->data;
407 }
408
409 struct fd6_texture_state *state = CALLOC_STRUCT(fd6_texture_state);
410
411 state->key = key;
412 state->stateobj = fd_ringbuffer_new_object(ctx->pipe, 0x1000);
413 state->needs_border = needs_border;
414
415 fd6_emit_textures(ctx->pipe, state->stateobj, type, tex, key.bcolor_offset,
416 NULL, NULL);
417
418 /* NOTE: uses copy of key in state obj, because pointer passed by caller
419 * is probably on the stack
420 */
421 _mesa_hash_table_insert_pre_hashed(fd6_ctx->tex_cache, hash,
422 &state->key, state);
423
424 return state;
425 }
426
427 static void
428 fd6_texture_state_destroy(struct fd6_texture_state *state)
429 {
430 fd_ringbuffer_del(state->stateobj);
431 free(state);
432 }
433
434 void
435 fd6_texture_init(struct pipe_context *pctx)
436 {
437 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
438
439 pctx->create_sampler_state = fd6_sampler_state_create;
440 pctx->delete_sampler_state = fd6_sampler_state_delete;
441 pctx->bind_sampler_states = fd6_sampler_states_bind;
442
443 pctx->create_sampler_view = fd6_sampler_view_create;
444 pctx->sampler_view_destroy = fd6_sampler_view_destroy;
445 pctx->set_sampler_views = fd_set_sampler_views;
446
447 fd6_ctx->tex_cache = _mesa_hash_table_create(NULL, key_hash, key_equals);
448 }
449
450 void
451 fd6_texture_fini(struct pipe_context *pctx)
452 {
453 struct fd6_context *fd6_ctx = fd6_context(fd_context(pctx));
454
455 hash_table_foreach(fd6_ctx->tex_cache, entry) {
456 fd6_texture_state_destroy(entry->data);
457 }
458 ralloc_free(fd6_ctx->tex_cache);
459 }