etnaviv: update Android build files
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_texture.c
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 <laanwj@gmail.com>
25 */
26
27 #include "etnaviv_texture.h"
28
29 #include "hw/common.xml.h"
30
31 #include "etnaviv_clear_blit.h"
32 #include "etnaviv_context.h"
33 #include "etnaviv_emit.h"
34 #include "etnaviv_format.h"
35 #include "etnaviv_texture_desc.h"
36 #include "etnaviv_texture_state.h"
37 #include "etnaviv_translate.h"
38 #include "util/u_inlines.h"
39 #include "util/u_memory.h"
40
41 #include "drm-uapi/drm_fourcc.h"
42
43 static void
44 etna_bind_sampler_states(struct pipe_context *pctx, enum pipe_shader_type shader,
45 unsigned start_slot, unsigned num_samplers,
46 void **samplers)
47 {
48 /* bind fragment sampler */
49 struct etna_context *ctx = etna_context(pctx);
50 int offset;
51
52 switch (shader) {
53 case PIPE_SHADER_FRAGMENT:
54 offset = 0;
55 ctx->num_fragment_samplers = num_samplers;
56 break;
57 case PIPE_SHADER_VERTEX:
58 offset = ctx->specs.vertex_sampler_offset;
59 break;
60 default:
61 assert(!"Invalid shader");
62 return;
63 }
64
65 uint32_t mask = 1 << offset;
66 for (int idx = 0; idx < num_samplers; ++idx, mask <<= 1) {
67 ctx->sampler[offset + idx] = samplers[idx];
68 if (samplers[idx])
69 ctx->active_samplers |= mask;
70 else
71 ctx->active_samplers &= ~mask;
72 }
73
74 ctx->dirty |= ETNA_DIRTY_SAMPLERS;
75 }
76
77 static bool
78 etna_configure_sampler_ts(struct etna_sampler_ts *sts, struct pipe_sampler_view *pview, bool enable)
79 {
80 bool dirty = (sts->enable != enable);
81
82 assert(sts);
83 sts->enable = enable;
84
85 if (!enable) {
86 sts->TS_SAMPLER_CONFIG = 0;
87 sts->TS_SAMPLER_STATUS_BASE.bo = NULL;
88 return dirty;
89 }
90
91 struct etna_resource *rsc = etna_resource(pview->texture);
92 struct etna_resource_level *lev = &rsc->levels[0];
93
94 if (lev->clear_value != sts->TS_SAMPLER_CLEAR_VALUE)
95 dirty = true;
96
97 assert(rsc->ts_bo && lev->ts_valid);
98
99 sts->mode = lev->ts_mode;
100 sts->TS_SAMPLER_CONFIG =
101 VIVS_TS_SAMPLER_CONFIG_ENABLE |
102 COND(lev->ts_compress_fmt >= 0, VIVS_TS_SAMPLER_CONFIG_COMPRESSION) |
103 VIVS_TS_SAMPLER_CONFIG_COMPRESSION_FORMAT(lev->ts_compress_fmt);
104 sts->TS_SAMPLER_CLEAR_VALUE = lev->clear_value;
105 sts->TS_SAMPLER_CLEAR_VALUE2 = lev->clear_value >> 32;
106 sts->TS_SAMPLER_STATUS_BASE.bo = rsc->ts_bo;
107 sts->TS_SAMPLER_STATUS_BASE.offset = lev->ts_offset;
108 sts->TS_SAMPLER_STATUS_BASE.flags = ETNA_RELOC_READ;
109
110 return dirty;
111 }
112
113 /* Return true if the GPU can use sampler TS with this sampler view.
114 * Sampler TS is an optimization used when rendering to textures, where
115 * a resolve-in-place can be avoided when rendering has left a (valid) TS.
116 */
117 static bool
118 etna_can_use_sampler_ts(struct pipe_sampler_view *view, int num)
119 {
120 /* Can use sampler TS when:
121 * - the hardware supports sampler TS.
122 * - the sampler view will be bound to sampler <VIVS_TS_SAMPLER__LEN.
123 * HALTI5 adds a mapping from sampler to sampler TS unit, but this is AFAIK
124 * absent on earlier models.
125 * - it is a texture, not a buffer.
126 * - the sampler view has a supported format for sampler TS.
127 * - the sampler will have one LOD, and it happens to be level 0.
128 * (it is not sure if the hw supports it for other levels, but available
129 * state strongly suggests only one at a time).
130 * - the resource TS is valid for level 0.
131 */
132 struct etna_resource *rsc = etna_resource(view->texture);
133 struct etna_screen *screen = etna_screen(rsc->base.screen);
134
135 return VIV_FEATURE(screen, chipMinorFeatures2, TEXTURE_TILED_READ) &&
136 num < VIVS_TS_SAMPLER__LEN &&
137 rsc->base.target != PIPE_BUFFER &&
138 (rsc->levels[0].ts_compress_fmt < 0 || screen->specs.v4_compression) &&
139 view->u.tex.first_level == 0 && MIN2(view->u.tex.last_level, rsc->base.last_level) == 0 &&
140 rsc->levels[0].ts_valid;
141 }
142
143 void
144 etna_update_sampler_source(struct pipe_sampler_view *view, int num)
145 {
146 struct etna_resource *base = etna_resource(view->texture);
147 struct etna_resource *to = base, *from = base;
148 struct etna_context *ctx = etna_context(view->context);
149 bool enable_sampler_ts = false;
150
151 if (base->render && etna_resource_newer(etna_resource(base->render), base))
152 from = etna_resource(base->render);
153
154 if (base->texture)
155 to = etna_resource(base->texture);
156
157 if ((to != from) && etna_resource_older(to, from)) {
158 etna_copy_resource(view->context, &to->base, &from->base, 0,
159 view->texture->last_level);
160 to->seqno = from->seqno;
161 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
162 } else if ((to == from) && etna_resource_needs_flush(to)) {
163 if (ctx->ts_for_sampler_view && etna_can_use_sampler_ts(view, num)) {
164 enable_sampler_ts = true;
165 /* Do not set flush_seqno because the resolve-to-self was bypassed */
166 } else {
167 /* Resolve TS if needed */
168 etna_copy_resource(view->context, &to->base, &from->base, 0,
169 view->texture->last_level);
170 to->flush_seqno = from->seqno;
171 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
172 }
173 } else if ((to == from) && (to->flush_seqno < from->seqno)) {
174 to->flush_seqno = from->seqno;
175 ctx->dirty |= ETNA_DIRTY_TEXTURE_CACHES;
176 }
177 if (ctx->ts_for_sampler_view &&
178 etna_configure_sampler_ts(ctx->ts_for_sampler_view(view), view, enable_sampler_ts)) {
179 ctx->dirty |= ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_TEXTURE_CACHES;
180 ctx->dirty_sampler_views |= (1 << num);
181 }
182 }
183
184 static bool
185 etna_resource_sampler_compatible(struct etna_resource *res)
186 {
187 if (util_format_is_compressed(res->base.format))
188 return true;
189
190 struct etna_screen *screen = etna_screen(res->base.screen);
191 /* This GPU supports texturing from supertiled textures? */
192 if (res->layout == ETNA_LAYOUT_SUPER_TILED && VIV_FEATURE(screen, chipMinorFeatures2, SUPERTILED_TEXTURE))
193 return true;
194
195 /* This GPU supports texturing from linear textures? */
196 if (res->layout == ETNA_LAYOUT_LINEAR && VIV_FEATURE(screen, chipMinorFeatures1, LINEAR_TEXTURE_SUPPORT))
197 return true;
198
199 /* Otherwise, only support tiled layouts */
200 if (res->layout != ETNA_LAYOUT_TILED)
201 return false;
202
203 /* If we have HALIGN support, we can allow for the RS padding */
204 if (VIV_FEATURE(screen, chipMinorFeatures1, TEXTURE_HALIGN))
205 return true;
206
207 /* Non-HALIGN GPUs only accept 4x4 tile-aligned textures */
208 if (res->halign != TEXTURE_HALIGN_FOUR)
209 return false;
210
211 return true;
212 }
213
214 struct etna_resource *
215 etna_texture_handle_incompatible(struct pipe_context *pctx, struct pipe_resource *prsc)
216 {
217 struct etna_resource *res = etna_resource(prsc);
218 if (!etna_resource_sampler_compatible(res)) {
219 /* The original resource is not compatible with the sampler.
220 * Allocate an appropriately tiled texture. */
221 if (!res->texture) {
222 struct pipe_resource templat = *prsc;
223
224 templat.bind &= ~(PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET |
225 PIPE_BIND_BLENDABLE);
226 res->texture =
227 etna_resource_alloc(pctx->screen, ETNA_LAYOUT_TILED,
228 DRM_FORMAT_MOD_LINEAR, &templat);
229 }
230
231 if (!res->texture) {
232 return NULL;
233 }
234 res = etna_resource(res->texture);
235 }
236 return res;
237 }
238
239 static void
240 set_sampler_views(struct etna_context *ctx, unsigned start, unsigned end,
241 unsigned nr, struct pipe_sampler_view **views)
242 {
243 unsigned i, j;
244 uint32_t mask = 1 << start;
245 uint32_t prev_active_sampler_views = ctx->active_sampler_views;
246
247 for (i = start, j = 0; j < nr; i++, j++, mask <<= 1) {
248 pipe_sampler_view_reference(&ctx->sampler_view[i], views[j]);
249 if (views[j]) {
250 ctx->active_sampler_views |= mask;
251 ctx->dirty_sampler_views |= mask;
252 } else
253 ctx->active_sampler_views &= ~mask;
254 }
255
256 for (; i < end; i++, mask <<= 1) {
257 pipe_sampler_view_reference(&ctx->sampler_view[i], NULL);
258 ctx->active_sampler_views &= ~mask;
259 }
260
261 /* sampler views that changed state (even to inactive) are also dirty */
262 ctx->dirty_sampler_views |= ctx->active_sampler_views ^ prev_active_sampler_views;
263 }
264
265 static inline void
266 etna_fragtex_set_sampler_views(struct etna_context *ctx, unsigned nr,
267 struct pipe_sampler_view **views)
268 {
269 unsigned start = 0;
270 unsigned end = start + ctx->specs.fragment_sampler_count;
271
272 set_sampler_views(ctx, start, end, nr, views);
273 ctx->num_fragment_sampler_views = nr;
274 }
275
276
277 static inline void
278 etna_vertex_set_sampler_views(struct etna_context *ctx, unsigned nr,
279 struct pipe_sampler_view **views)
280 {
281 unsigned start = ctx->specs.vertex_sampler_offset;
282 unsigned end = start + ctx->specs.vertex_sampler_count;
283
284 set_sampler_views(ctx, start, end, nr, views);
285 }
286
287 static void
288 etna_set_sampler_views(struct pipe_context *pctx, enum pipe_shader_type shader,
289 unsigned start_slot, unsigned num_views,
290 struct pipe_sampler_view **views)
291 {
292 struct etna_context *ctx = etna_context(pctx);
293 assert(start_slot == 0);
294
295 ctx->dirty |= ETNA_DIRTY_SAMPLER_VIEWS | ETNA_DIRTY_TEXTURE_CACHES;
296
297 switch (shader) {
298 case PIPE_SHADER_FRAGMENT:
299 etna_fragtex_set_sampler_views(ctx, num_views, views);
300 break;
301 case PIPE_SHADER_VERTEX:
302 etna_vertex_set_sampler_views(ctx, num_views, views);
303 break;
304 default:;
305 }
306 }
307
308 static void
309 etna_texture_barrier(struct pipe_context *pctx, unsigned flags)
310 {
311 struct etna_context *ctx = etna_context(pctx);
312 /* clear color and texture cache to make sure that texture unit reads
313 * what has been written */
314 mtx_lock(&ctx->lock);
315 etna_set_state(ctx->stream, VIVS_GL_FLUSH_CACHE, VIVS_GL_FLUSH_CACHE_COLOR | VIVS_GL_FLUSH_CACHE_TEXTURE);
316 mtx_unlock(&ctx->lock);
317 }
318
319 uint32_t
320 active_samplers_bits(struct etna_context *ctx)
321 {
322 return ctx->active_sampler_views & ctx->active_samplers;
323 }
324
325 void
326 etna_texture_init(struct pipe_context *pctx)
327 {
328 struct etna_context *ctx = etna_context(pctx);
329
330 pctx->bind_sampler_states = etna_bind_sampler_states;
331 pctx->set_sampler_views = etna_set_sampler_views;
332 pctx->texture_barrier = etna_texture_barrier;
333
334 if (ctx->specs.halti >= 5)
335 etna_texture_desc_init(pctx);
336 else
337 etna_texture_state_init(pctx);
338 }