1 /*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Wladimir J. van der Laan <[email protected]>
25 */
26
27 #include "etnaviv_texture.h"
28
29 #include "etnaviv_clear_blit.h"
30 #include "etnaviv_context.h"
31 #include "etnaviv_emit.h"
32 #include "etnaviv_format.h"
33 #include "etnaviv_texture_desc.h"
34 #include "etnaviv_texture_state.h"
35 #include "etnaviv_translate.h"
36 #include "util/u_inlines.h"
37 #include "util/u_memory.h"
38
39 #include "drm-uapi/drm_fourcc.h"
40
41 static void
etna_bind_sampler_states(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start_slot,unsigned num_samplers,void ** samplers)42 etna_bind_sampler_states(struct pipe_context *pctx, enum pipe_shader_type shader,
43 unsigned start_slot, unsigned num_samplers,
44 void **samplers)
45 {
46 /* bind fragment sampler */
47 struct etna_context *ctx = etna_context(pctx);
48 struct etna_screen *screen = ctx->screen;
49 int offset;
50
51 switch (shader) {
52 case PIPE_SHADER_FRAGMENT:
53 offset = 0;
54 ctx->num_fragment_samplers = num_samplers;
55 break;
56 case PIPE_SHADER_VERTEX:
57 offset = screen->specs.vertex_sampler_offset;
58 break;
59 default:
60 assert(!"Invalid shader");
61 return;
62 }
63
64 uint32_t mask = 1 << offset;
65 for (int idx = 0; idx < num_samplers; ++idx, mask <<= 1) {
66 ctx->sampler[offset + idx] = samplers[idx];
67 if (samplers[idx])
68 ctx->active_samplers |= mask;
69 else
70 ctx->active_samplers &= ~mask;
71 }
72
73 ctx->dirty |= ETNA_DIRTY_SAMPLERS;
74 }
75
76 static bool
etna_configure_sampler_ts(struct etna_sampler_ts * sts,struct pipe_sampler_view * pview,bool enable)77 etna_configure_sampler_ts(struct etna_sampler_ts *sts, struct pipe_sampler_view *pview, bool enable)
78 {
79 bool dirty = (sts->enable != enable);
80
81 assert(sts);
82 sts->enable = enable;
83
84 if (!enable) {
85 sts->TS_SAMPLER_CONFIG = 0;
86 sts->TS_SAMPLER_STATUS_BASE.bo = NULL;
87 return dirty;
88 }
89
90 struct etna_resource *rsc = etna_resource(pview->texture);
91 struct etna_resource_level *lev = &rsc->levels[0];
92
93 if ((lev->clear_value & 0xffffffff) != sts->TS_SAMPLER_CLEAR_VALUE ||
94 (lev->clear_value >> 32) != sts->TS_SAMPLER_CLEAR_VALUE2)
95 dirty = true;
96
97 assert(rsc->ts_bo && etna_resource_level_ts_valid(lev));
98
99 sts->mode = lev->ts_mode;
100 sts->comp = lev->ts_compress_fmt >= 0;
101 sts->TS_SAMPLER_CONFIG =
102 VIVS_TS_SAMPLER_CONFIG_ENABLE |
103 COND(lev->ts_compress_fmt >= 0, VIVS_TS_SAMPLER_CONFIG_COMPRESSION) |
104 VIVS_TS_SAMPLER_CONFIG_COMPRESSION_FORMAT(lev->ts_compress_fmt);
105 sts->TS_SAMPLER_CLEAR_VALUE = lev->clear_value;
106 sts->TS_SAMPLER_CLEAR_VALUE2 = lev->clear_value >> 32;
107 sts->TS_SAMPLER_STATUS_BASE.bo = rsc->ts_bo;
108 sts->TS_SAMPLER_STATUS_BASE.offset = lev->ts_offset;
109 sts->TS_SAMPLER_STATUS_BASE.flags = ETNA_RELOC_READ;
110
111 return dirty;
112 }
113
114 /* Return true if the GPU can use sampler TS with this sampler view.
115 * Sampler TS is an optimization used when rendering to textures, where
116 * a resolve-in-place can be avoided when rendering has left a (valid) TS.
117 */
118 static bool
etna_can_use_sampler_ts(struct pipe_sampler_view * view,int num)119 etna_can_use_sampler_ts(struct pipe_sampler_view *view, int num)
120 {
121 struct etna_resource *rsc = etna_resource(view->texture);
122 struct etna_screen *screen = etna_screen(rsc->base.screen);
123
124 /* Sampler TS can be used under the following conditions: */
125
126 /* The resource TS is valid for level 0. */
127 if (!etna_resource_level_ts_valid(&rsc->levels[0]))
128 return false;
129
130 /* The hardware supports it. */
131 if (!VIV_FEATURE(screen, ETNA_FEATURE_TEXTURE_TILED_READ))
132 return false;
133
134 /* The sampler view will be bound to sampler < VIVS_TS_SAMPLER__LEN.
135 * HALTI5 adds a mapping from sampler to sampler TS unit, but this is AFAIK
136 * absent on earlier models. */
137 if (num >= VIVS_TS_SAMPLER__LEN)
138 return false;
139
140 /* It is a texture, not a buffer. */
141 if (rsc->base.target == PIPE_BUFFER)
142 return false;
143
144 /* Does not use compression or the hardware supports V4 compression. */
145 if (rsc->levels[0].ts_compress_fmt >= 0 && !screen->specs.v4_compression)
146 return false;
147
148 /* The sampler will have one LOD, and it happens to be level 0.
149 * (It is not sure if the hw supports it for other levels, but available
150 * state strongly suggests only one at a time). */
151 if (view->u.tex.first_level != 0 ||
152 MIN2(view->u.tex.last_level, rsc->base.last_level) != 0)
153 return false;
154
155 return true;
156 }
157
158 void
etna_update_sampler_source(struct pipe_sampler_view * view,int num)159 etna_update_sampler_source(struct pipe_sampler_view *view, int num)
160 {
161 struct etna_resource *base = etna_resource(view->texture);
162 struct etna_resource *to = base, *from = base;
163 struct etna_context *ctx = etna_context(view->context);
164 bool enable_sampler_ts = false;
165
166 if (base->shared && !_mesa_set_search(ctx->updated_resources, view->texture)) {
167 for (int i = view->u.tex.first_level; i <= view->u.tex.last_level; i++)
168 etna_resource_level_mark_changed(&base->levels[i]);
169
170 pipe_reference(NULL, &view->texture->reference);
171 _mesa_set_add(ctx->updated_resources, view->texture);
172 }
173
174 if (base->render && etna_resource_newer(etna_resource(base->render), base))
175 from = etna_resource(base->render);
176
177 if (base->texture)
178 to = etna_resource(base->texture);
179
180 if ((to != from) && etna_resource_older(to, from)) {
181 etna_copy_resource(view->context, &to->base, &from->base,
182 view->u.tex.first_level,
183 MIN2(view->texture->last_level, view->u.tex.last_level));
184 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
185 } else if (to == from) {
186 if (etna_can_use_sampler_ts(view, num)) {
187 enable_sampler_ts = true;
188 } else if (etna_resource_needs_flush(to)) {
189 /* Resolve TS if needed */
190 etna_copy_resource(view->context, &to->base, &from->base,
191 view->u.tex.first_level,
192 MIN2(view->texture->last_level, view->u.tex.last_level));
193 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
194 }
195 }
196
197 if (etna_configure_sampler_ts(ctx->ts_for_sampler_view(view), view, enable_sampler_ts)) {
198 ctx->dirty |= ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_TEXTURE_CACHES;
199 ctx->dirty_sampler_views |= (1 << num);
200 }
201 }
202
203 static bool
etna_resource_sampler_compatible(struct etna_resource * res)204 etna_resource_sampler_compatible(struct etna_resource *res)
205 {
206 if (util_format_is_compressed(res->base.format))
207 return true;
208
209 struct etna_screen *screen = etna_screen(res->base.screen);
210 /* This GPU supports texturing from supertiled textures? */
211 if (res->layout == ETNA_LAYOUT_SUPER_TILED && VIV_FEATURE(screen, ETNA_FEATURE_SUPERTILED_TEXTURE))
212 return true;
213
214 /* This GPU supports texturing from linear textures? */
215 if (res->layout == ETNA_LAYOUT_LINEAR && VIV_FEATURE(screen, ETNA_FEATURE_LINEAR_TEXTURE_SUPPORT))
216 return true;
217
218 /* Otherwise, only support tiled layouts */
219 if (res->layout != ETNA_LAYOUT_TILED)
220 return false;
221
222 /* If we have HALIGN support, we can allow for the RS padding */
223 if (VIV_FEATURE(screen, ETNA_FEATURE_TEXTURE_HALIGN))
224 return true;
225
226 /* Non-HALIGN GPUs only accept 4x4 tile-aligned textures */
227 if (res->halign != TEXTURE_HALIGN_FOUR)
228 return false;
229
230 return true;
231 }
232
233 struct etna_resource *
etna_texture_handle_incompatible(struct pipe_context * pctx,struct pipe_resource * prsc)234 etna_texture_handle_incompatible(struct pipe_context *pctx, struct pipe_resource *prsc)
235 {
236 struct etna_resource *res = etna_resource(prsc);
237
238 if (!etna_resource_sampler_compatible(res)) {
239 /* The original resource is not compatible with the sampler.
240 * Allocate an appropriately tiled texture. */
241 if (!res->texture) {
242 struct pipe_resource templat = *prsc;
243
244 templat.bind &= ~(PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET |
245 PIPE_BIND_BLENDABLE);
246 res->texture =
247 etna_resource_alloc(pctx->screen, ETNA_LAYOUT_TILED,
248 DRM_FORMAT_MOD_LINEAR, &templat);
249 }
250
251 if (!res->texture) {
252 return NULL;
253 }
254 res = etna_resource(res->texture);
255 }
256 return res;
257 }
258
259 static void
set_sampler_views(struct etna_context * ctx,unsigned start,unsigned end,unsigned nr,bool take_ownership,struct pipe_sampler_view ** views)260 set_sampler_views(struct etna_context *ctx, unsigned start, unsigned end,
261 unsigned nr, bool take_ownership, struct pipe_sampler_view **views)
262 {
263 unsigned i, j;
264 uint32_t mask = 1 << start;
265 uint32_t prev_active_sampler_views = ctx->active_sampler_views;
266
267 for (i = start, j = 0; j < nr; i++, j++, mask <<= 1) {
268 struct pipe_sampler_view *view = views ? views[j] : NULL;
269
270 if (take_ownership) {
271 pipe_sampler_view_reference(&ctx->sampler_view[i], NULL);
272 ctx->sampler_view[i] = view;
273 } else {
274 pipe_sampler_view_reference(&ctx->sampler_view[i], view);
275 }
276 if (view) {
277 ctx->active_sampler_views |= mask;
278 ctx->dirty_sampler_views |= mask;
279 } else
280 ctx->active_sampler_views &= ~mask;
281 }
282
283 for (; i < end; i++, mask <<= 1) {
284 pipe_sampler_view_reference(&ctx->sampler_view[i], NULL);
285 ctx->active_sampler_views &= ~mask;
286 }
287
288 /* sampler views that changed state (even to inactive) are also dirty */
289 ctx->dirty_sampler_views |= ctx->active_sampler_views ^ prev_active_sampler_views;
290 }
291
292 static inline void
etna_fragtex_set_sampler_views(struct etna_context * ctx,unsigned nr,bool take_ownership,struct pipe_sampler_view ** views)293 etna_fragtex_set_sampler_views(struct etna_context *ctx, unsigned nr,
294 bool take_ownership,
295 struct pipe_sampler_view **views)
296 {
297 struct etna_screen *screen = ctx->screen;
298 unsigned start = 0;
299 unsigned end = start + screen->specs.fragment_sampler_count;
300
301 set_sampler_views(ctx, start, end, nr, take_ownership, views);
302 ctx->num_fragment_sampler_views = nr;
303 }
304
305
306 static inline void
etna_vertex_set_sampler_views(struct etna_context * ctx,unsigned nr,bool take_ownership,struct pipe_sampler_view ** views)307 etna_vertex_set_sampler_views(struct etna_context *ctx, unsigned nr,
308 bool take_ownership,
309 struct pipe_sampler_view **views)
310 {
311 struct etna_screen *screen = ctx->screen;
312 unsigned start = screen->specs.vertex_sampler_offset;
313 unsigned end = start + screen->specs.vertex_sampler_count;
314
315 set_sampler_views(ctx, start, end, nr, take_ownership, views);
316 }
317
318 static void
etna_set_sampler_views(struct pipe_context * pctx,enum pipe_shader_type shader,unsigned start_slot,unsigned num_views,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)319 etna_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
320 unsigned start_slot, unsigned num_views,
321 unsigned unbind_num_trailing_slots,
322 bool take_ownership,
323 struct pipe_sampler_view **views)
324 {
325 struct etna_context *ctx = etna_context(pctx);
326 assert(start_slot == 0);
327
328 ctx->dirty |= ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_TEXTURE_CACHES;
329
330 switch (shader) {
331 case PIPE_SHADER_FRAGMENT:
332 etna_fragtex_set_sampler_views(ctx, num_views, take_ownership, views);
333 break;
334 case PIPE_SHADER_VERTEX:
335 etna_vertex_set_sampler_views(ctx, num_views, take_ownership, views);
336 break;
337 default:;
338 }
339 }
340
341 static void
etna_texture_barrier(struct pipe_context * pctx,unsigned flags)342 etna_texture_barrier(struct pipe_context *pctx, unsigned flags)
343 {
344 struct etna_context *ctx = etna_context(pctx);
345
346 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
347 VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_DEPTH |
348 VIVS_GL_FLUSH_CACHE_TEXTURE);
349 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE,
350 VIVS_GL_FLUSH_CACHE_TEXTUREVS);
351 etna_stall(ctx->stream, SYNC_RECIPIENT_RA, SYNC_RECIPIENT_PE);
352 }
353
354 uint32_t
active_samplers_bits(struct etna_context * ctx)355 active_samplers_bits(struct etna_context *ctx)
356 {
357 return ctx->active_sampler_views & ctx->active_samplers;
358 }
359
360 void
etna_texture_init(struct pipe_context * pctx)361 etna_texture_init(struct pipe_context *pctx)
362 {
363 struct etna_context *ctx = etna_context(pctx);
364 struct etna_screen *screen = ctx->screen;
365
366 pctx->bind_sampler_states = etna_bind_sampler_states;
367 pctx->set_sampler_views = etna_set_sampler_views;
368 pctx->texture_barrier = etna_texture_barrier;
369
370 if (screen->info->halti >= 5) {
371 u_suballocator_init(&ctx->tex_desc_allocator, pctx, 4096, 0,
372 PIPE_USAGE_IMMUTABLE, 0, true);
373 etna_texture_desc_init(pctx);
374 } else {
375 etna_texture_state_init(pctx);
376 }
377 }
378
379 void
etna_texture_fini(struct pipe_context * pctx)380 etna_texture_fini(struct pipe_context *pctx)
381 {
382 struct etna_context *ctx = etna_context(pctx);
383 struct etna_screen *screen = ctx->screen;
384
385 if (screen->info->halti >= 5)
386 u_suballocator_destroy(&ctx->tex_desc_allocator);
387 }
388