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