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 struct etna_context;
40 struct pipe_screen;
41 struct util_dynarray;
42
43 struct etna_resource_level {
44 unsigned width, padded_width; /* in pixels */
45 unsigned height, padded_height; /* in samples */
46 unsigned depth;
47 unsigned offset; /* offset into memory area */
48 uint32_t stride; /* row stride */
49 uint32_t layer_stride; /* layer stride */
50 unsigned size; /* total size of memory area */
51
52 uint32_t ts_offset;
53 uint32_t ts_layer_stride;
54 uint32_t ts_size;
55 uint64_t clear_value; /* clear value of resource level (mainly for TS) */
56 bool ts_valid;
57 uint8_t ts_mode;
58 int8_t ts_compress_fmt; /* COLOR_COMPRESSION_FORMAT_* (-1 = disable) */
59
60 /* keep track if we have done some per block patching */
61 bool patched;
62 struct util_dynarray *patch_offsets;
63 };
64
65 /* status of queued up but not flushed reads and write operations.
66 * In _transfer_map() we need to know if queued up rendering needs
67 * to be flushed to preserve the order of cpu and gpu access. */
68 enum etna_resource_status {
69 ETNA_PENDING_WRITE = 0x01,
70 ETNA_PENDING_READ = 0x02,
71 };
72
73 struct etna_resource {
74 struct pipe_resource base;
75 struct renderonly_scanout *scanout;
76 uint32_t seqno;
77 uint32_t flush_seqno;
78
79 /* only lod 0 used for non-texture buffers */
80 /* Layout for surface (tiled, multitiled, split tiled, ...) */
81 enum etna_surface_layout layout;
82 /* Horizontal alignment for texture unit (TEXTURE_HALIGN_*) */
83 unsigned halign;
84 struct etna_bo *bo; /* Surface video memory */
85 struct etna_bo *ts_bo; /* Tile status video memory */
86
87 struct etna_resource_level levels[ETNA_NUM_LOD];
88
89 /* buffer range that has been initialized */
90 struct util_range valid_buffer_range;
91
92 /* for when TE doesn't support the base layout */
93 struct pipe_resource *texture;
94 /* for when PE doesn't support the base layout */
95 struct pipe_resource *render;
96 /* frontend flushes resource via an explicit call to flush_resource */
97 bool explicit_flush;
98 };
99
100 /* returns TRUE if a is newer than b */
101 static inline bool
etna_resource_newer(struct etna_resource * a,struct etna_resource * b)102 etna_resource_newer(struct etna_resource *a, struct etna_resource *b)
103 {
104 return (int)(a->seqno - b->seqno) > 0;
105 }
106
107 /* returns TRUE if a is older than b */
108 static inline bool
etna_resource_older(struct etna_resource * a,struct etna_resource * b)109 etna_resource_older(struct etna_resource *a, struct etna_resource *b)
110 {
111 return (int)(a->seqno - b->seqno) < 0;
112 }
113
114 /* returns TRUE if a resource has a TS, and it is valid for at least one level */
115 bool
116 etna_resource_has_valid_ts(struct etna_resource *res);
117
118 /* returns TRUE if the resource needs a resolve to itself */
119 static inline bool
etna_resource_needs_flush(struct etna_resource * res)120 etna_resource_needs_flush(struct etna_resource *res)
121 {
122 return etna_resource_has_valid_ts(res) && ((int)(res->seqno - res->flush_seqno) > 0);
123 }
124
125 /* is the resource only used on the sampler? */
126 static inline bool
etna_resource_sampler_only(const struct pipe_resource * pres)127 etna_resource_sampler_only(const struct pipe_resource *pres)
128 {
129 return (pres->bind & (PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET |
130 PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_BLENDABLE)) ==
131 PIPE_BIND_SAMPLER_VIEW;
132 }
133
134 static inline bool
etna_resource_hw_tileable(bool use_blt,const struct pipe_resource * pres)135 etna_resource_hw_tileable(bool use_blt, const struct pipe_resource *pres)
136 {
137 if (use_blt)
138 return true;
139
140 /* RS can only tile 16bpp or 32bpp formats */
141 return util_format_get_blocksize(pres->format) == 2 ||
142 util_format_get_blocksize(pres->format) == 4;
143 }
144
145 static inline struct etna_resource *
etna_resource(struct pipe_resource * p)146 etna_resource(struct pipe_resource *p)
147 {
148 return (struct etna_resource *)p;
149 }
150
151 void
152 etna_resource_used(struct etna_context *ctx, struct pipe_resource *prsc,
153 enum etna_resource_status status);
154
155 static inline void
resource_read(struct etna_context * ctx,struct pipe_resource * prsc)156 resource_read(struct etna_context *ctx, struct pipe_resource *prsc)
157 {
158 etna_resource_used(ctx, prsc, ETNA_PENDING_READ);
159 }
160
161 static inline void
resource_written(struct etna_context * ctx,struct pipe_resource * prsc)162 resource_written(struct etna_context *ctx, struct pipe_resource *prsc)
163 {
164 etna_resource_used(ctx, prsc, ETNA_PENDING_WRITE);
165 }
166
167 enum etna_resource_status
168 etna_resource_status(struct etna_context *ctx, struct etna_resource *res);
169
170 /* Allocate Tile Status for an etna resource.
171 * Tile status is a cache of the clear status per tile. This means a smaller
172 * surface has to be cleared which is faster.
173 * This is also called "fast clear". */
174 bool
175 etna_screen_resource_alloc_ts(struct pipe_screen *pscreen,
176 struct etna_resource *prsc);
177
178 struct pipe_resource *
179 etna_resource_alloc(struct pipe_screen *pscreen, unsigned layout,
180 uint64_t modifier, const struct pipe_resource *templat);
181
182 void
183 etna_resource_screen_init(struct pipe_screen *pscreen);
184
185 #endif
186