android: gallium/radeon: attempt to fix the android build
[mesa.git] / src / gallium / drivers / ilo / ilo_resource.h
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #ifndef ILO_RESOURCE_H
29 #define ILO_RESOURCE_H
30
31 #include "intel_winsys.h"
32
33 #include "ilo_common.h"
34 #include "ilo_screen.h"
35
36 enum ilo_texture_flags {
37 /*
38 * Possible writers of a texture. There can be at most one writer at any
39 * time.
40 *
41 * Wine set in resolve flags (in ilo_blit_resolve_slices()), they indicate
42 * the new writer. When set in slice flags (ilo_texture_slice::flags),
43 * they indicate the writer since last resolve.
44 */
45 ILO_TEXTURE_RENDER_WRITE = 1 << 0,
46 ILO_TEXTURE_BLT_WRITE = 1 << 1,
47 ILO_TEXTURE_CPU_WRITE = 1 << 2,
48
49 /*
50 * Possible readers of a texture. There may be multiple readers at any
51 * time.
52 *
53 * When set in resolve flags, they indicate the new readers. They are
54 * never set in slice flags.
55 */
56 ILO_TEXTURE_RENDER_READ = 1 << 3,
57 ILO_TEXTURE_BLT_READ = 1 << 4,
58 ILO_TEXTURE_CPU_READ = 1 << 5,
59
60 /*
61 * Set when the texture is cleared.
62 *
63 * When set in resolve flags, the new writer will clear. When set in slice
64 * flags, the slice has been cleared to ilo_texture_slice::clear_value.
65 */
66 ILO_TEXTURE_CLEAR = 1 << 6,
67
68 /*
69 * Set when HiZ can be enabled.
70 *
71 * It is never set in resolve flags. When set in slice flags, the slice
72 * can have HiZ enabled. It is to be noted that this bit is always set for
73 * either all or none of the slices in a level, allowing quick check in
74 * case of layered rendering.
75 */
76 ILO_TEXTURE_HIZ = 1 << 7,
77 };
78
79 struct ilo_buffer {
80 struct pipe_resource base;
81
82 struct intel_bo *bo;
83 unsigned bo_size;
84 };
85
86 /**
87 * A 3D image slice, cube face, or array layer.
88 */
89 struct ilo_texture_slice {
90 /* 2D offset to the slice */
91 unsigned x, y;
92 unsigned flags;
93
94 /*
95 * Slice clear value. It is served for two purposes
96 *
97 * - the clear value used in commands such as 3DSTATE_CLEAR_PARAMS
98 * - the clear value when ILO_TEXTURE_CLEAR is set
99 *
100 * Since commands such as 3DSTATE_CLEAR_PARAMS expect a single clear value
101 * for all slices, ilo_blit_resolve_slices() will silently make all slices
102 * to have the same clear value.
103 */
104 uint32_t clear_value;
105 };
106
107 struct ilo_texture {
108 struct pipe_resource base;
109
110 bool imported;
111
112 enum pipe_format bo_format;
113 struct intel_bo *bo;
114
115 enum intel_tiling_mode tiling;
116 unsigned long bo_stride; /* distance between two block rows in bytes */
117 unsigned long bo_height;
118
119 unsigned block_width;
120 unsigned block_height;
121 unsigned block_size;
122
123 /* true if the mip level alignments are stricter */
124 bool halign_8, valign_4;
125 /* true if space is reserved between layers */
126 bool array_spacing_full;
127 /* true if samples are interleaved */
128 bool interleaved;
129
130 struct ilo_texture_slice *slices[PIPE_MAX_TEXTURE_LEVELS];
131
132 struct ilo_texture *separate_s8;
133
134 struct {
135 struct intel_bo *bo;
136 unsigned long bo_stride;
137 } hiz;
138 };
139
140 static inline struct ilo_buffer *
141 ilo_buffer(struct pipe_resource *res)
142 {
143 return (struct ilo_buffer *)
144 ((res && res->target == PIPE_BUFFER) ? res : NULL);
145 }
146
147 static inline struct ilo_texture *
148 ilo_texture(struct pipe_resource *res)
149 {
150 return (struct ilo_texture *)
151 ((res && res->target != PIPE_BUFFER) ? res : NULL);
152 }
153
154 void
155 ilo_init_resource_functions(struct ilo_screen *is);
156
157 bool
158 ilo_buffer_rename_bo(struct ilo_buffer *buf);
159
160 bool
161 ilo_texture_rename_bo(struct ilo_texture *tex);
162
163 /**
164 * Return the bo of the resource.
165 */
166 static inline struct intel_bo *
167 ilo_resource_get_bo(struct pipe_resource *res)
168 {
169 return (res->target == PIPE_BUFFER) ?
170 ilo_buffer(res)->bo : ilo_texture(res)->bo;
171 }
172
173 static inline struct ilo_texture_slice *
174 ilo_texture_get_slice(const struct ilo_texture *tex,
175 unsigned level, unsigned slice)
176 {
177 assert(level <= tex->base.last_level);
178 assert(slice < ((tex->base.target == PIPE_TEXTURE_3D) ?
179 u_minify(tex->base.depth0, level) : tex->base.array_size));
180
181 return &tex->slices[level][slice];
182 }
183
184 unsigned
185 ilo_texture_get_slice_offset(const struct ilo_texture *tex,
186 unsigned level, unsigned slice,
187 unsigned *x_offset, unsigned *y_offset);
188
189 static inline void
190 ilo_texture_set_slice_flags(struct ilo_texture *tex, unsigned level,
191 unsigned first_slice, unsigned num_slices,
192 unsigned mask, unsigned value)
193 {
194 const struct ilo_texture_slice *last =
195 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
196 struct ilo_texture_slice *slice =
197 ilo_texture_get_slice(tex, level, first_slice);
198
199 while (slice <= last) {
200 slice->flags = (slice->flags & ~mask) | (value & mask);
201 slice++;
202 }
203 }
204
205 static inline void
206 ilo_texture_set_slice_clear_value(struct ilo_texture *tex, unsigned level,
207 unsigned first_slice, unsigned num_slices,
208 uint32_t clear_value)
209 {
210 const struct ilo_texture_slice *last =
211 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
212 struct ilo_texture_slice *slice =
213 ilo_texture_get_slice(tex, level, first_slice);
214
215 while (slice <= last) {
216 slice->clear_value = clear_value;
217 slice++;
218 }
219 }
220
221 static inline bool
222 ilo_texture_can_enable_hiz(const struct ilo_texture *tex, unsigned level,
223 unsigned first_slice, unsigned num_slices)
224 {
225 const struct ilo_screen *is = ilo_screen(tex->base.screen);
226 const struct ilo_texture_slice *slice =
227 ilo_texture_get_slice(tex, level, first_slice);
228
229 if (!tex->hiz.bo)
230 return false;
231
232 /* we can adjust 3DSTATE_DEPTH_BUFFER for the first slice */
233 if (level == 0 && first_slice == 0 && num_slices == 1)
234 return true;
235
236 /* HiZ is non-mipmapped and non-array on GEN6 */
237 assert(is->dev.gen > ILO_GEN(6));
238
239 /*
240 * Either all or none of the slices in the same level have ILO_TEXTURE_HIZ
241 * set. It suffices to check only the first slice.
242 */
243 return (slice->flags & ILO_TEXTURE_HIZ);
244 }
245
246 #endif /* ILO_RESOURCE_H */