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