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 #ifndef H_ETNAVIV_RESOURCE
28 #define H_ETNAVIV_RESOURCE
29
30 #include "etnaviv_internal.h"
31 #include "etnaviv_tiling.h"
32 #include "pipe/p_state.h"
33 #include "util/format/u_format.h"
34 #include "util/list.h"
35 #include "util/hash_table.h"
36 #include "util/u_helpers.h"
37 #include "util/u_range.h"
38
39 #include "drm-uapi/drm_fourcc.h"
40
41 struct etna_context;
42 struct pipe_screen;
43 struct util_dynarray;
44
45 struct etna_ts_sw_meta {
46 uint16_t version;
47 struct {
48 uint16_t data_offset;
49 uint32_t data_size;
50 uint32_t layer_stride;
51 uint32_t comp_format;
52 uint64_t clear_value;
53 uint32_t seqno;
54 uint8_t valid;
55 uint8_t flushed;
56 uint8_t pad[6];
57 } v0;
58 };
59
60 struct etna_resource_level {
61 unsigned width, height; /* in pixels */
62 unsigned padded_width, padded_height; /* in samples */
63 unsigned depth;
64 unsigned offset; /* offset into memory area */
65 uint32_t stride; /* row stride */
66 uint32_t layer_stride; /* layer stride */
67 unsigned size; /* total size of memory area */
68
69 uint32_t ts_offset;
70 uint32_t ts_layer_stride;
71 uint32_t ts_size;
72 uint64_t clear_value; /* clear value of resource level (mainly for TS) */
73 bool ts_valid;
74 bool ts_flushed;
75 uint8_t ts_mode;
76 int8_t ts_compress_fmt; /* COLOR_COMPRESSION_FORMAT_* (-1 = disable) */
77
78 struct etna_ts_sw_meta *ts_meta; /* metadata for shared TS */
79
80 /* keep track if we have done some per block patching */
81 bool patched;
82 struct util_dynarray *patch_offsets;
83
84 uint32_t seqno;
85 };
86
87 /* returns TRUE if a is newer than b */
88 static inline bool
etna_resource_level_newer(struct etna_resource_level * a,struct etna_resource_level * b)89 etna_resource_level_newer(struct etna_resource_level *a,
90 struct etna_resource_level *b)
91 {
92 uint32_t a_seqno = a->ts_meta ? a->ts_meta->v0.seqno : a->seqno;
93 uint32_t b_seqno = b->ts_meta ? b->ts_meta->v0.seqno : b->seqno;
94
95 return (int)(a_seqno - b_seqno) > 0;
96 }
97
98 /* returns TRUE if a is older than b */
99 static inline bool
etna_resource_level_older(struct etna_resource_level * a,struct etna_resource_level * b)100 etna_resource_level_older(struct etna_resource_level *a,
101 struct etna_resource_level *b)
102 {
103 uint32_t a_seqno = a->ts_meta ? a->ts_meta->v0.seqno : a->seqno;
104 uint32_t b_seqno = b->ts_meta ? b->ts_meta->v0.seqno : b->seqno;
105
106 return (int)(a_seqno - b_seqno) < 0;
107 }
108
109 static inline bool
etna_resource_level_ts_valid(struct etna_resource_level * lvl)110 etna_resource_level_ts_valid(struct etna_resource_level *lvl)
111 {
112 if (unlikely(lvl->ts_meta))
113 return lvl->ts_meta->v0.valid;
114 else
115 return lvl->ts_valid;
116 }
117
118 static inline void
etna_resource_level_ts_mark_valid(struct etna_resource_level * lvl)119 etna_resource_level_ts_mark_valid(struct etna_resource_level *lvl)
120 {
121 if (unlikely(lvl->ts_meta))
122 lvl->ts_meta->v0.valid = 1;
123 else
124 lvl->ts_valid = true;
125 }
126
127 static inline void
etna_resource_level_ts_mark_invalid(struct etna_resource_level * lvl)128 etna_resource_level_ts_mark_invalid(struct etna_resource_level *lvl)
129 {
130 if (unlikely(lvl->ts_meta))
131 lvl->ts_meta->v0.valid = 0;
132 else
133 lvl->ts_valid = false;
134 }
135
136 /* returns TRUE if lvl has valid, unflushed TS data */
137 static inline bool
etna_resource_level_needs_flush(struct etna_resource_level * lvl)138 etna_resource_level_needs_flush(struct etna_resource_level *lvl)
139 {
140 if (!etna_resource_level_ts_valid(lvl))
141 return false;
142
143 if (unlikely(lvl->ts_meta))
144 return !lvl->ts_meta->v0.flushed;
145 else
146 return !lvl->ts_flushed;
147 }
148
149 static inline void
etna_resource_level_mark_flushed(struct etna_resource_level * lvl)150 etna_resource_level_mark_flushed(struct etna_resource_level *lvl)
151 {
152 if (unlikely(lvl->ts_meta))
153 lvl->ts_meta->v0.flushed = 1;
154 else
155 lvl->ts_flushed = true;
156 }
157
158 static inline void
etna_resource_level_mark_unflushed(struct etna_resource_level * lvl)159 etna_resource_level_mark_unflushed(struct etna_resource_level *lvl)
160 {
161 if (unlikely(lvl->ts_meta))
162 lvl->ts_meta->v0.flushed = 0;
163 else
164 lvl->ts_flushed = false;
165 }
166
167 static inline void
etna_resource_level_mark_changed(struct etna_resource_level * lvl)168 etna_resource_level_mark_changed(struct etna_resource_level *lvl)
169 {
170 if (unlikely(lvl->ts_meta))
171 lvl->ts_meta->v0.seqno++;
172 else
173 lvl->seqno++;
174 }
175
176 static inline void
etna_resource_level_copy_seqno(struct etna_resource_level * dst,struct etna_resource_level * src)177 etna_resource_level_copy_seqno(struct etna_resource_level *dst,
178 struct etna_resource_level *src)
179 {
180 uint32_t src_seqno = src->ts_meta ? src->ts_meta->v0.seqno : src->seqno;
181
182 if (unlikely(dst->ts_meta))
183 dst->ts_meta->v0.seqno = src_seqno;
184 else
185 dst->seqno = src_seqno;
186 }
187
188 /* status of queued up but not flushed reads and write operations.
189 * In _transfer_map() we need to know if queued up rendering needs
190 * to be flushed to preserve the order of cpu and gpu access. */
191 enum etna_resource_status {
192 ETNA_PENDING_WRITE = 0x01,
193 ETNA_PENDING_READ = 0x02,
194 };
195
196 struct etna_resource {
197 struct pipe_resource base;
198 struct renderonly_scanout *scanout;
199
200 /* only lod 0 used for non-texture buffers */
201 /* Layout for surface (tiled, multitiled, split tiled, ...) */
202 enum etna_surface_layout layout;
203 uint64_t modifier;
204 /* Horizontal alignment for texture unit (TEXTURE_HALIGN_*) */
205 unsigned halign;
206 struct etna_bo *bo; /* Surface video memory */
207 struct etna_bo *ts_bo; /* Tile status video memory */
208 struct renderonly_scanout *ts_scanout; /* display compatible TS */
209
210 struct etna_resource_level levels[ETNA_NUM_LOD];
211
212 /* buffer range that has been initialized */
213 struct util_range valid_buffer_range;
214
215 /* for when TE doesn't support the base layout */
216 struct pipe_resource *texture;
217 /* for when PE doesn't support the base layout */
218 struct pipe_resource *render;
219 /* frontend flushes resource via an explicit call to flush_resource */
220 bool explicit_flush;
221 /* resource is shared outside of the screen */
222 bool shared;
223 };
224
225 /* returns TRUE if a is newer than b */
226 static inline bool
etna_resource_newer(struct etna_resource * a,struct etna_resource * b)227 etna_resource_newer(struct etna_resource *a, struct etna_resource *b)
228 {
229 assert(a->base.last_level == b->base.last_level);
230
231 for (int level = 0; level <= a->base.last_level; level++)
232 if (etna_resource_level_newer(&a->levels[level], &b->levels[level]))
233 return true;
234
235 return false;
236 }
237
238 /* returns TRUE if a is older than b */
239 static inline bool
etna_resource_older(struct etna_resource * a,struct etna_resource * b)240 etna_resource_older(struct etna_resource *a, struct etna_resource *b)
241 {
242 assert(a->base.last_level == b->base.last_level);
243
244 for (int level = 0; level <= a->base.last_level; level++)
245 if (etna_resource_level_older(&a->levels[level], &b->levels[level]))
246 return true;
247
248 return false;
249 }
250
251 /* returns TRUE if the resource needs a resolve to itself */
252 bool etna_resource_needs_flush(struct etna_resource *res);
253
254 /* is the resource only used on the sampler? */
255 static inline bool
etna_resource_sampler_only(const struct pipe_resource * pres)256 etna_resource_sampler_only(const struct pipe_resource *pres)
257 {
258 return (pres->bind & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET |
259 PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_BLENDABLE)) ==
260 PIPE_BIND_SAMPLER_VIEW;
261 }
262
263 static inline bool
etna_resource_hw_tileable(bool use_blt,const struct pipe_resource * pres)264 etna_resource_hw_tileable(bool use_blt, const struct pipe_resource *pres)
265 {
266 if (use_blt)
267 return true;
268
269 /* RS can only tile 16bpp or 32bpp formats */
270 return util_format_get_blocksize(pres->format) == 2 ||
271 util_format_get_blocksize(pres->format) == 4;
272 }
273
274 /* returns TRUE if resource TS buffer is exposed externally */
275 static inline bool
etna_resource_ext_ts(const struct etna_resource * res)276 etna_resource_ext_ts(const struct etna_resource *res)
277 {
278 return res->modifier & VIVANTE_MOD_TS_MASK;
279 }
280
281 static inline struct etna_resource *
etna_resource(struct pipe_resource * p)282 etna_resource(struct pipe_resource *p)
283 {
284 return (struct etna_resource *)p;
285 }
286
287 void
288 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
289 enum etna_resource_status status);
290
291 static inline void
resource_read(struct etna_context * ctx,struct pipe_resource * prsc)292 resource_read(struct etna_context *ctx, struct pipe_resource *prsc)
293 {
294 etna_resource_used(ctx, prsc, ETNA_PENDING_READ);
295 }
296
297 static inline void
resource_written(struct etna_context * ctx,struct pipe_resource * prsc)298 resource_written(struct etna_context *ctx, struct pipe_resource *prsc)
299 {
300 etna_resource_used(ctx, prsc, ETNA_PENDING_WRITE);
301 }
302
303 enum etna_resource_status
304 etna_resource_status(struct etna_context *ctx, struct etna_resource *res);
305
306 /* Allocate Tile Status for an etna resource.
307 * Tile status is a cache of the clear status per tile. This means a smaller
308 * surface has to be cleared which is faster.
309 * This is also called "fast clear". */
310 bool
311 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
312 struct etna_resource *prsc,
313 uint64_t modifier);
314
315 struct pipe_resource *
316 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
317 uint64_t modifier, const struct pipe_resource *templat);
318
319 void
320 etna_resource_screen_init(struct pipe_screen *pscreen);
321
322 #endif
323