gallium: rename R4A4 and A4R4 formats to match their swizzle
[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 unsigned bo_flags;
85 };
86
87 /**
88 * A 3D image slice, cube face, or array layer.
89 */
90 struct ilo_texture_slice {
91 /* 2D offset to the slice */
92 unsigned x, y;
93 unsigned flags;
94
95 /*
96 * Slice clear value. It is served for two purposes
97 *
98 * - the clear value used in commands such as 3DSTATE_CLEAR_PARAMS
99 * - the clear value when ILO_TEXTURE_CLEAR is set
100 *
101 * Since commands such as 3DSTATE_CLEAR_PARAMS expect a single clear value
102 * for all slices, ilo_blit_resolve_slices() will silently make all slices
103 * to have the same clear value.
104 */
105 uint32_t clear_value;
106 };
107
108 struct ilo_texture {
109 struct pipe_resource base;
110
111 bool imported;
112 unsigned bo_flags;
113
114 enum pipe_format bo_format;
115 struct intel_bo *bo;
116
117 /*
118 * These are the values passed to or returned from winsys for bo
119 * allocation. As such,
120 *
121 * - width and height are in blocks,
122 * - cpp is the block size in bytes, and
123 * - stride is the distance in bytes between two block rows.
124 */
125 int bo_width, bo_height, bo_cpp, bo_stride;
126 enum intel_tiling_mode tiling;
127
128 bool compressed;
129 unsigned block_width;
130 unsigned block_height;
131
132 /* true if the mip level alignments are stricter */
133 bool halign_8, valign_4;
134 /* true if space is reserved between layers */
135 bool array_spacing_full;
136 /* true if samples are interleaved */
137 bool interleaved;
138
139 struct ilo_texture_slice *slices[PIPE_MAX_TEXTURE_LEVELS];
140
141 struct ilo_texture *separate_s8;
142
143 struct {
144 struct intel_bo *bo;
145 int bo_stride;
146 } hiz;
147 };
148
149 static inline struct ilo_buffer *
150 ilo_buffer(struct pipe_resource *res)
151 {
152 return (struct ilo_buffer *)
153 ((res && res->target == PIPE_BUFFER) ? res : NULL);
154 }
155
156 static inline struct ilo_texture *
157 ilo_texture(struct pipe_resource *res)
158 {
159 return (struct ilo_texture *)
160 ((res && res->target != PIPE_BUFFER) ? res : NULL);
161 }
162
163 void
164 ilo_init_resource_functions(struct ilo_screen *is);
165
166 bool
167 ilo_buffer_alloc_bo(struct ilo_buffer *buf);
168
169 bool
170 ilo_texture_alloc_bo(struct ilo_texture *tex);
171
172 static inline struct ilo_texture_slice *
173 ilo_texture_get_slice(const struct ilo_texture *tex,
174 unsigned level, unsigned slice)
175 {
176 assert(level <= tex->base.last_level);
177 assert(slice < ((tex->base.target == PIPE_TEXTURE_3D) ?
178 u_minify(tex->base.depth0, level) : tex->base.array_size));
179
180 return &tex->slices[level][slice];
181 }
182
183 unsigned
184 ilo_texture_get_slice_offset(const struct ilo_texture *tex,
185 unsigned level, unsigned slice,
186 unsigned *x_offset, unsigned *y_offset);
187
188 static inline void
189 ilo_texture_set_slice_flags(struct ilo_texture *tex, unsigned level,
190 unsigned first_slice, unsigned num_slices,
191 unsigned mask, unsigned value)
192 {
193 const struct ilo_texture_slice *last =
194 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
195 struct ilo_texture_slice *slice =
196 ilo_texture_get_slice(tex, level, first_slice);
197
198 while (slice <= last) {
199 slice->flags = (slice->flags & ~mask) | (value & mask);
200 slice++;
201 }
202 }
203
204 static inline void
205 ilo_texture_set_slice_clear_value(struct ilo_texture *tex, unsigned level,
206 unsigned first_slice, unsigned num_slices,
207 uint32_t clear_value)
208 {
209 const struct ilo_texture_slice *last =
210 ilo_texture_get_slice(tex, level, first_slice + num_slices - 1);
211 struct ilo_texture_slice *slice =
212 ilo_texture_get_slice(tex, level, first_slice);
213
214 while (slice <= last) {
215 slice->clear_value = clear_value;
216 slice++;
217 }
218 }
219
220 static inline bool
221 ilo_texture_can_enable_hiz(const struct ilo_texture *tex, unsigned level,
222 unsigned first_slice, unsigned num_slices)
223 {
224 const struct ilo_screen *is = ilo_screen(tex->base.screen);
225 const struct ilo_texture_slice *slice =
226 ilo_texture_get_slice(tex, level, first_slice);
227
228 if (!tex->hiz.bo)
229 return false;
230
231 /* we can adjust 3DSTATE_DEPTH_BUFFER for the first slice */
232 if (level == 0 && first_slice == 0 && num_slices == 1)
233 return true;
234
235 /* HiZ is non-mipmapped and non-array on GEN6 */
236 assert(is->dev.gen > ILO_GEN(6));
237
238 /*
239 * Either all or none of the slices in the same level have ILO_TEXTURE_HIZ
240 * set. It suffices to check only the first slice.
241 */
242 return (slice->flags & ILO_TEXTURE_HIZ);
243 }
244
245 #endif /* ILO_RESOURCE_H */