1 /*
2 * Copyright (c) 2017 Etnaviv Project
3 * Copyright (C) 2017 Zodiac Inflight Innovations
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, sub license,
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
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the 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 NON-INFRINGEMENT. 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
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Wladimir J. van der Laan <laanwj@gmail.com>
26 */
27
28 #include "etnaviv_texture_desc.h"
29
30 #include "hw/common.xml.h"
31 #include "hw/texdesc_3d.xml.h"
32
33 #include "etnaviv_clear_blit.h"
34 #include "etnaviv_context.h"
35 #include "etnaviv_emit.h"
36 #include "etnaviv_format.h"
37 #include "etnaviv_translate.h"
38 #include "etnaviv_texture.h"
39 #include "util/u_inlines.h"
40 #include "util/u_memory.h"
41
42 #include <drm_fourcc.h>
43
44 struct etna_sampler_state_desc {
45 struct pipe_sampler_state base;
46 uint32_t SAMP_CTRL0;
47 uint32_t SAMP_CTRL1;
48 uint32_t SAMP_LOD_MINMAX;
49 uint32_t SAMP_LOD_BIAS;
50 uint32_t SAMP_ANISOTROPY;
51 };
52
53 static inline struct etna_sampler_state_desc *
etna_sampler_state_desc(struct pipe_sampler_state * samp)54 etna_sampler_state_desc(struct pipe_sampler_state *samp)
55 {
56 return (struct etna_sampler_state_desc *)samp;
57 }
58
59 struct etna_sampler_view_desc {
60 struct pipe_sampler_view base;
61 /* format-dependent merged with sampler state */
62 uint32_t SAMP_CTRL0;
63 uint32_t SAMP_CTRL0_MASK;
64 uint32_t SAMP_CTRL1;
65
66 struct pipe_resource *res;
67 struct etna_reloc DESC_ADDR;
68 struct etna_sampler_ts ts;
69 };
70
71 static inline struct etna_sampler_view_desc *
etna_sampler_view_desc(struct pipe_sampler_view * view)72 etna_sampler_view_desc(struct pipe_sampler_view *view)
73 {
74 return (struct etna_sampler_view_desc *)view;
75 }
76
77 static void *
etna_create_sampler_state_desc(struct pipe_context * pipe,const struct pipe_sampler_state * ss)78 etna_create_sampler_state_desc(struct pipe_context *pipe,
79 const struct pipe_sampler_state *ss)
80 {
81 struct etna_sampler_state_desc *cs = CALLOC_STRUCT(etna_sampler_state_desc);
82 const bool ansio = ss->max_anisotropy > 1;
83
84 if (!cs)
85 return NULL;
86
87 cs->base = *ss;
88
89 cs->SAMP_CTRL0 =
90 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_UWRAP(translate_texture_wrapmode(ss->wrap_s)) |
91 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_VWRAP(translate_texture_wrapmode(ss->wrap_t)) |
92 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_WWRAP(translate_texture_wrapmode(ss->wrap_r)) |
93 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_MIN(translate_texture_filter(ss->min_img_filter)) |
94 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_MIP(translate_texture_mipfilter(ss->min_mip_filter)) |
95 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_MAG(translate_texture_filter(ss->mag_img_filter)) |
96 VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_UNK21;
97 /* no ROUND_UV bit? */
98 cs->SAMP_CTRL1 = VIVS_NTE_DESCRIPTOR_SAMP_CTRL1_UNK1;
99 uint32_t min_lod_fp8 = MIN2(etna_float_to_fixp88(ss->min_lod), 0xfff);
100 uint32_t max_lod_fp8 = MIN2(etna_float_to_fixp88(ss->max_lod), 0xfff);
101 uint32_t max_lod_min = ss->min_img_filter != ss->mag_img_filter ? 4 : 0;
102
103 cs->SAMP_LOD_MINMAX =
104 VIVS_NTE_DESCRIPTOR_SAMP_LOD_MINMAX_MAX(MAX2(max_lod_fp8, max_lod_min)) |
105 VIVS_NTE_DESCRIPTOR_SAMP_LOD_MINMAX_MIN(min_lod_fp8);
106
107 cs->SAMP_LOD_BIAS =
108 VIVS_NTE_DESCRIPTOR_SAMP_LOD_BIAS_BIAS(etna_float_to_fixp88(ss->lod_bias)) |
109 COND(ss->lod_bias != 0.0, VIVS_NTE_DESCRIPTOR_SAMP_LOD_BIAS_ENABLE);
110 cs->SAMP_ANISOTROPY = COND(ansio, etna_log2_fixp88(ss->max_anisotropy));
111
112 return cs;
113 }
114
115 static void
etna_delete_sampler_state_desc(struct pipe_context * pctx,void * ss)116 etna_delete_sampler_state_desc(struct pipe_context *pctx, void *ss)
117 {
118 FREE(ss);
119 }
120
121 static struct pipe_sampler_view *
etna_create_sampler_view_desc(struct pipe_context * pctx,struct pipe_resource * prsc,const struct pipe_sampler_view * so)122 etna_create_sampler_view_desc(struct pipe_context *pctx, struct pipe_resource *prsc,
123 const struct pipe_sampler_view *so)
124 {
125 const struct util_format_description *desc = util_format_description(so->format);
126 struct etna_sampler_view_desc *sv = CALLOC_STRUCT(etna_sampler_view_desc);
127 struct etna_context *ctx = etna_context(pctx);
128 const uint32_t format = translate_texture_format(so->format);
129 const bool ext = !!(format & EXT_FORMAT);
130 const bool astc = !!(format & ASTC_FORMAT);
131 const uint32_t swiz = get_texture_swiz(so->format, so->swizzle_r,
132 so->swizzle_g, so->swizzle_b,
133 so->swizzle_a);
134 unsigned suballoc_offset;
135
136 if (!sv)
137 return NULL;
138
139 struct etna_resource *res = etna_texture_handle_incompatible(pctx, prsc);
140 if (!res)
141 goto error;
142
143 sv->base = *so;
144 pipe_reference_init(&sv->base.reference, 1);
145 sv->base.texture = NULL;
146 pipe_resource_reference(&sv->base.texture, prsc);
147 sv->base.context = pctx;
148 sv->SAMP_CTRL0_MASK = 0xffffffff;
149
150 /* Determine whether target supported */
151 uint32_t target_hw = translate_texture_target(sv->base.target);
152 if (target_hw == ETNA_NO_MATCH) {
153 BUG("Unhandled texture target");
154 goto error;
155 }
156
157 /* Texture descriptor sampler bits */
158 if (util_format_is_srgb(so->format))
159 sv->SAMP_CTRL1 |= VIVS_NTE_DESCRIPTOR_SAMP_CTRL1_SRGB;
160
161 /* Create texture descriptor */
162 u_suballocator_alloc(&ctx->tex_desc_allocator, 256, 64,
163 &suballoc_offset, &sv->res);
164 if (!sv->res)
165 goto error;
166
167 uint32_t *buf = etna_bo_map(etna_resource(sv->res)->bo) + suballoc_offset;
168
169 /** GC7000 needs the size of the BASELOD level */
170 uint32_t base_width = u_minify(res->base.width0, sv->base.u.tex.first_level);
171 uint32_t base_height = u_minify(res->base.height0, sv->base.u.tex.first_level);
172 uint32_t base_depth = u_minify(res->base.depth0, sv->base.u.tex.first_level);
173 bool is_array = false;
174 bool sint = util_format_is_pure_sint(so->format);
175
176 switch(sv->base.target) {
177 case PIPE_TEXTURE_1D:
178 target_hw = TEXTURE_TYPE_2D;
179 sv->SAMP_CTRL0_MASK = ~VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_VWRAP__MASK;
180 sv->SAMP_CTRL0 = VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_VWRAP(TEXTURE_WRAPMODE_REPEAT);
181 break;
182 case PIPE_TEXTURE_1D_ARRAY:
183 is_array = true;
184 base_height = res->base.array_size;
185 break;
186 case PIPE_TEXTURE_2D_ARRAY:
187 is_array = true;
188 base_depth = res->base.array_size;
189 break;
190 default:
191 break;
192 }
193
194 #define DESC_SET(x, y) buf[(TEXDESC_##x)>>2] = (y)
195 DESC_SET(CONFIG0, COND(!ext && !astc, VIVS_TE_SAMPLER_CONFIG0_FORMAT(format))
196 | VIVS_TE_SAMPLER_CONFIG0_TYPE(target_hw) |
197 COND(res->layout == ETNA_LAYOUT_LINEAR && !util_format_is_compressed(so->format),
198 VIVS_TE_SAMPLER_CONFIG0_ADDRESSING_MODE(TEXTURE_ADDRESSING_MODE_LINEAR)));
199 DESC_SET(CONFIG1, COND(ext, VIVS_TE_SAMPLER_CONFIG1_FORMAT_EXT(format)) |
200 COND(astc, VIVS_TE_SAMPLER_CONFIG1_FORMAT_EXT(TEXTURE_FORMAT_EXT_ASTC)) |
201 COND(is_array, VIVS_TE_SAMPLER_CONFIG1_TEXTURE_ARRAY) |
202 VIVS_TE_SAMPLER_CONFIG1_HALIGN(res->halign) | swiz);
203 DESC_SET(CONFIG2, 0x00030000 |
204 COND(sint && desc->channel[0].size == 8, TE_SAMPLER_CONFIG2_SIGNED_INT8) |
205 COND(sint && desc->channel[0].size == 16, TE_SAMPLER_CONFIG2_SIGNED_INT16));
206 DESC_SET(LINEAR_STRIDE, res->levels[0].stride);
207 DESC_SET(VOLUME, etna_log2_fixp88(base_depth));
208 DESC_SET(SLICE, res->levels[0].layer_stride);
209 DESC_SET(3D_CONFIG, VIVS_TE_SAMPLER_3D_CONFIG_DEPTH(base_depth));
210 DESC_SET(ASTC0, COND(astc, VIVS_NTE_SAMPLER_ASTC0_ASTC_FORMAT(format)) |
211 VIVS_NTE_SAMPLER_ASTC0_UNK8(0xc) |
212 VIVS_NTE_SAMPLER_ASTC0_UNK16(0xc) |
213 VIVS_NTE_SAMPLER_ASTC0_UNK24(0xc));
214 DESC_SET(BASELOD, TEXDESC_BASELOD_BASELOD(sv->base.u.tex.first_level) |
215 TEXDESC_BASELOD_MAXLOD(MIN2(sv->base.u.tex.last_level, res->base.last_level)));
216 DESC_SET(LOG_SIZE_EXT, TEXDESC_LOG_SIZE_EXT_WIDTH(etna_log2_fixp88(base_width)) |
217 TEXDESC_LOG_SIZE_EXT_HEIGHT(etna_log2_fixp88(base_height)));
218 DESC_SET(SIZE, VIVS_TE_SAMPLER_SIZE_WIDTH(base_width) |
219 VIVS_TE_SAMPLER_SIZE_HEIGHT(base_height));
220 for (int lod = 0; lod <= res->base.last_level; ++lod)
221 DESC_SET(LOD_ADDR(lod), etna_bo_gpu_va(res->bo) + res->levels[lod].offset);
222 #undef DESC_SET
223
224 sv->DESC_ADDR.bo = etna_resource(sv->res)->bo;
225 sv->DESC_ADDR.offset = suballoc_offset;
226 sv->DESC_ADDR.flags = ETNA_RELOC_READ;
227
228 return &sv->base;
229
230 error:
231 free(sv);
232 return NULL;
233 }
234
235 static void
etna_sampler_view_update_descriptor(struct etna_context * ctx,struct etna_cmd_stream * stream,struct etna_sampler_view_desc * sv)236 etna_sampler_view_update_descriptor(struct etna_context *ctx,
237 struct etna_cmd_stream *stream,
238 struct etna_sampler_view_desc *sv)
239 {
240 struct etna_resource *res = etna_resource(sv->base.texture);
241
242 if (res->texture) {
243 res = etna_resource(res->texture);
244 }
245
246 /* No need to ref LOD levels individually as they'll always come from the same bo */
247 etna_cmd_stream_ref_bo(stream, res->bo, ETNA_RELOC_READ);
248 }
249
250 static void
etna_sampler_view_desc_destroy(struct pipe_context * pctx,struct pipe_sampler_view * so)251 etna_sampler_view_desc_destroy(struct pipe_context *pctx,
252 struct pipe_sampler_view *so)
253 {
254 struct etna_sampler_view_desc *sv = etna_sampler_view_desc(so);
255
256 pipe_resource_reference(&sv->base.texture, NULL);
257 pipe_resource_reference(&sv->res, NULL);
258 FREE(sv);
259 }
260
261 static void
etna_emit_texture_desc(struct etna_context * ctx)262 etna_emit_texture_desc(struct etna_context *ctx)
263 {
264 struct etna_cmd_stream *stream = ctx->stream;
265 uint32_t active_samplers = active_samplers_bits(ctx);
266 uint32_t dirty = ctx->dirty;
267
268 if (unlikely(dirty & ETNA_DIRTY_SAMPLER_VIEWS)) {
269 for (int x = 0; x < VIVS_TS_SAMPLER__LEN; ++x) {
270 if ((1 << x) & active_samplers) {
271 struct etna_sampler_view_desc *sv = etna_sampler_view_desc(ctx->sampler_view[x]);
272 struct etna_resource *res = etna_resource(sv->base.texture);
273 struct etna_reloc LOD_ADDR_0;
274
275 if (!sv->ts.enable)
276 continue;
277
278 etna_set_state(stream, VIVS_TS_SAMPLER_CONFIG(x), sv->ts.TS_SAMPLER_CONFIG);
279 etna_set_state_reloc(stream, VIVS_TS_SAMPLER_STATUS_BASE(x), &sv->ts.TS_SAMPLER_STATUS_BASE);
280 etna_set_state(stream, VIVS_TS_SAMPLER_CLEAR_VALUE(x), sv->ts.TS_SAMPLER_CLEAR_VALUE);
281 etna_set_state(stream, VIVS_TS_SAMPLER_CLEAR_VALUE2(x), sv->ts.TS_SAMPLER_CLEAR_VALUE2);
282
283 LOD_ADDR_0.bo = res->bo;
284 LOD_ADDR_0.offset = res->levels[0].offset;
285 LOD_ADDR_0.flags = ETNA_RELOC_READ;
286
287 etna_set_state_reloc(stream, VIVS_TS_SAMPLER_SURFACE_BASE(x), &LOD_ADDR_0);
288 }
289 }
290 }
291
292 if (unlikely(dirty & (ETNA_DIRTY_SAMPLERS | ETNA_DIRTY_SAMPLER_VIEWS))) {
293 for (int x = 0; x < PIPE_MAX_SAMPLERS; ++x) {
294 if ((1 << x) & active_samplers) {
295 struct etna_sampler_state_desc *ss = etna_sampler_state_desc(ctx->sampler[x]);
296 struct etna_sampler_view_desc *sv = etna_sampler_view_desc(ctx->sampler_view[x]);
297 uint32_t SAMP_CTRL0 = (ss->SAMP_CTRL0 & sv->SAMP_CTRL0_MASK) | sv->SAMP_CTRL0;
298
299 if (texture_use_int_filter(&sv->base, &ss->base, true))
300 SAMP_CTRL0 |= VIVS_NTE_DESCRIPTOR_SAMP_CTRL0_INT_FILTER;
301
302 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_TX_CTRL(x),
303 COND(sv->ts.enable, VIVS_NTE_DESCRIPTOR_TX_CTRL_TS_ENABLE) |
304 VIVS_NTE_DESCRIPTOR_TX_CTRL_TS_MODE(sv->ts.mode) |
305 VIVS_NTE_DESCRIPTOR_TX_CTRL_TS_INDEX(x)|
306 COND(sv->ts.comp, VIVS_NTE_DESCRIPTOR_TX_CTRL_COMPRESSION) |
307 COND(!sv->ts.mode, VIVS_NTE_DESCRIPTOR_TX_CTRL_128B_TILE));
308 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_SAMP_CTRL0(x), SAMP_CTRL0);
309 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_SAMP_CTRL1(x), ss->SAMP_CTRL1 | sv->SAMP_CTRL1);
310 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_SAMP_LOD_MINMAX(x), ss->SAMP_LOD_MINMAX);
311 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_SAMP_LOD_BIAS(x), ss->SAMP_LOD_BIAS);
312 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_SAMP_ANISOTROPY(x), ss->SAMP_ANISOTROPY);
313 }
314 }
315 }
316
317 if (unlikely(dirty & ETNA_DIRTY_SAMPLER_VIEWS)) {
318 /* Set texture descriptors */
319 for (int x = 0; x < PIPE_MAX_SAMPLERS; ++x) {
320 if ((1 << x) & ctx->dirty_sampler_views) {
321 if ((1 << x) & active_samplers) {
322 struct etna_sampler_view_desc *sv = etna_sampler_view_desc(ctx->sampler_view[x]);
323 etna_sampler_view_update_descriptor(ctx, stream, sv);
324 etna_set_state_reloc(stream, VIVS_NTE_DESCRIPTOR_ADDR(x), &sv->DESC_ADDR);
325 } else if ((1 << x) & ctx->prev_active_samplers){
326 /* dummy texture descriptors for unused samplers */
327 etna_set_state_reloc(stream, VIVS_NTE_DESCRIPTOR_ADDR(x),
328 &ctx->screen->dummy_desc_reloc);
329 }
330 }
331 }
332 }
333
334 if (unlikely(dirty & ETNA_DIRTY_SAMPLER_VIEWS)) {
335 /* Invalidate all dirty sampler views.
336 */
337 for (int x = 0; x < PIPE_MAX_SAMPLERS; ++x) {
338 if ((1 << x) & ctx->dirty_sampler_views) {
339 etna_set_state(stream, VIVS_NTE_DESCRIPTOR_INVALIDATE,
340 VIVS_NTE_DESCRIPTOR_INVALIDATE_UNK29 |
341 VIVS_NTE_DESCRIPTOR_INVALIDATE_IDX(x));
342 }
343 }
344 }
345
346 ctx->prev_active_samplers = active_samplers;
347 }
348
349 static struct etna_sampler_ts*
etna_ts_for_sampler_view_state(struct pipe_sampler_view * pview)350 etna_ts_for_sampler_view_state(struct pipe_sampler_view *pview)
351 {
352 struct etna_sampler_view_desc *sv = etna_sampler_view_desc(pview);
353 return &sv->ts;
354 }
355
356 void
etna_texture_desc_init(struct pipe_context * pctx)357 etna_texture_desc_init(struct pipe_context *pctx)
358 {
359 struct etna_context *ctx = etna_context(pctx);
360 DBG("etnaviv: Using descriptor-based texturing\n");
361 ctx->base.create_sampler_state = etna_create_sampler_state_desc;
362 ctx->base.delete_sampler_state = etna_delete_sampler_state_desc;
363 ctx->base.create_sampler_view = etna_create_sampler_view_desc;
364 ctx->base.sampler_view_destroy = etna_sampler_view_desc_destroy;
365 ctx->emit_texture_state = etna_emit_texture_desc;
366 ctx->ts_for_sampler_view = etna_ts_for_sampler_view_state;
367 }
368
369