iris: Rework image views to store pipe_image_view.
[mesa.git] / src / gallium / drivers / iris / iris_resource.h
1 /*
2 * Copyright 2017 Intel Corporation
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef IRIS_RESOURCE_H
24 #define IRIS_RESOURCE_H
25
26 #include "pipe/p_state.h"
27 #include "util/u_inlines.h"
28 #include "intel/isl/isl.h"
29
30 struct iris_batch;
31 struct iris_context;
32
33 #define IRIS_MAX_MIPLEVELS 15
34
35 struct iris_format_info {
36 enum isl_format fmt;
37 struct isl_swizzle swizzle;
38 };
39
40 #define IRIS_RESOURCE_FLAG_SHADER_MEMZONE (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
41 #define IRIS_RESOURCE_FLAG_SURFACE_MEMZONE (PIPE_RESOURCE_FLAG_DRV_PRIV << 1)
42 #define IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE (PIPE_RESOURCE_FLAG_DRV_PRIV << 2)
43
44 enum gen9_astc5x5_wa_tex_type {
45 GEN9_ASTC5X5_WA_TEX_TYPE_ASTC5x5 = 1 << 0,
46 GEN9_ASTC5X5_WA_TEX_TYPE_AUX = 1 << 1,
47 };
48
49 /**
50 * Resources represent a GPU buffer object or image (mipmap tree).
51 *
52 * They contain the storage (BO) and layout information (ISL surface).
53 */
54 struct iris_resource {
55 struct pipe_resource base;
56 enum pipe_format internal_format;
57
58 /**
59 * The ISL surface layout information for this resource.
60 *
61 * This is not filled out for PIPE_BUFFER resources, but is guaranteed
62 * to be zeroed. Note that this also guarantees that res->surf.tiling
63 * will be ISL_TILING_LINEAR, so it's safe to check that.
64 */
65 struct isl_surf surf;
66
67 /** Backing storage for the resource */
68 struct iris_bo *bo;
69
70 /**
71 * A bitfield of PIPE_BIND_* indicating how this resource was bound
72 * in the past. Only meaningful for PIPE_BUFFER; used for flushing.
73 */
74 unsigned bind_history;
75
76 /**
77 * Auxiliary buffer information (CCS, MCS, or HiZ).
78 */
79 struct {
80 /** The surface layout for the auxiliary buffer. */
81 struct isl_surf surf;
82
83 /** The buffer object containing the auxiliary data. */
84 struct iris_bo *bo;
85
86 /** Offset into 'bo' where the auxiliary surface starts. */
87 uint32_t offset;
88
89 /**
90 * Fast clear color for this surface. For depth surfaces, the clear
91 * value is stored as a float32 in the red component.
92 */
93 union isl_color_value clear_color;
94
95 /** Buffer object containing the indirect clear color. */
96 struct iris_bo *clear_color_bo;
97
98 /** Offset into bo where the clear color can be found. */
99 uint64_t clear_color_offset;
100
101 /**
102 * \brief The type of auxiliary compression used by this resource.
103 *
104 * This describes the type of auxiliary compression that is intended to
105 * be used by this resource. An aux usage of ISL_AUX_USAGE_NONE means
106 * that auxiliary compression is permanently disabled. An aux usage
107 * other than ISL_AUX_USAGE_NONE does not imply that auxiliary
108 * compression will always be enabled for this surface.
109 */
110 enum isl_aux_usage usage;
111
112 /**
113 * A bitfield of ISL_AUX_* modes that might this resource might use.
114 *
115 * For example, a surface might use both CCS_E and CCS_D at times.
116 */
117 unsigned possible_usages;
118
119 /**
120 * Same as possible_usages, but only with modes supported for sampling.
121 */
122 unsigned sampler_usages;
123
124 /**
125 * \brief Maps miptree slices to their current aux state.
126 *
127 * This two-dimensional array is indexed as [level][layer] and stores an
128 * aux state for each slice.
129 */
130 enum isl_aux_state **state;
131
132 /**
133 * If (1 << level) is set, HiZ is enabled for that miplevel.
134 */
135 uint16_t has_hiz;
136 } aux;
137
138 /**
139 * For external surfaces, this is DRM format modifier that was used to
140 * create or import the surface. For internal surfaces, this will always
141 * be DRM_FORMAT_MOD_INVALID.
142 */
143 const struct isl_drm_modifier_info *mod_info;
144 };
145
146 /**
147 * A simple <resource, offset> tuple for storing a reference to a
148 * piece of state stored in a GPU buffer object.
149 */
150 struct iris_state_ref {
151 struct pipe_resource *res;
152 uint32_t offset;
153 };
154
155 /**
156 * Gallium CSO for sampler views (texture views).
157 *
158 * In addition to the normal pipe_resource, this adds an ISL view
159 * which may reinterpret the format or restrict levels/layers.
160 *
161 * These can also be linear texture buffers.
162 */
163 struct iris_sampler_view {
164 struct pipe_sampler_view base;
165 struct isl_view view;
166
167 union isl_color_value clear_color;
168
169 /* A short-cut (not a reference) to the actual resource being viewed.
170 * Multi-planar (or depth+stencil) images may have multiple resources
171 * chained together; this skips having to traverse base->texture->*.
172 */
173 struct iris_resource *res;
174
175 /** The resource (BO) holding our SURFACE_STATE. */
176 struct iris_state_ref surface_state;
177 };
178
179 /**
180 * Image view representation.
181 */
182 struct iris_image_view {
183 struct pipe_image_view base;
184
185 /** The resource (BO) holding our SURFACE_STATE. */
186 struct iris_state_ref surface_state;
187 };
188
189 /**
190 * Gallium CSO for surfaces (framebuffer attachments).
191 *
192 * A view of a surface that can be bound to a color render target or
193 * depth/stencil attachment.
194 */
195 struct iris_surface {
196 struct pipe_surface base;
197 struct isl_view view;
198 union isl_color_value clear_color;
199
200 /** The resource (BO) holding our SURFACE_STATE. */
201 struct iris_state_ref surface_state;
202 };
203
204 /**
205 * Transfer object - information about a buffer mapping.
206 */
207 struct iris_transfer {
208 struct pipe_transfer base;
209 struct pipe_debug_callback *dbg;
210 void *buffer;
211 void *ptr;
212
213 /** A linear staging resource for GPU-based copy_region transfers. */
214 struct pipe_resource *staging;
215 struct blorp_context *blorp;
216 struct iris_batch *batch;
217
218 void (*unmap)(struct iris_transfer *);
219 };
220
221 /**
222 * Unwrap a pipe_resource to get the underlying iris_bo (for convenience).
223 */
224 static inline struct iris_bo *
225 iris_resource_bo(struct pipe_resource *p_res)
226 {
227 struct iris_resource *res = (void *) p_res;
228 return res->bo;
229 }
230
231 struct iris_format_info iris_format_for_usage(const struct gen_device_info *,
232 enum pipe_format pf,
233 isl_surf_usage_flags_t usage);
234
235 struct pipe_resource *iris_resource_get_separate_stencil(struct pipe_resource *);
236
237 void iris_get_depth_stencil_resources(struct pipe_resource *res,
238 struct iris_resource **out_z,
239 struct iris_resource **out_s);
240 bool iris_resource_set_clear_color(struct iris_context *ice,
241 struct iris_resource *res,
242 union isl_color_value color);
243 union isl_color_value
244 iris_resource_get_clear_color(const struct iris_resource *res,
245 struct iris_bo **clear_color_bo,
246 uint64_t *clear_color_offset);
247
248 void iris_init_screen_resource_functions(struct pipe_screen *pscreen);
249
250 void iris_flush_and_dirty_for_history(struct iris_context *ice,
251 struct iris_batch *batch,
252 struct iris_resource *res);
253
254 unsigned iris_get_num_logical_layers(const struct iris_resource *res,
255 unsigned level);
256
257 void iris_resource_disable_aux(struct iris_resource *res);
258
259 #define INTEL_REMAINING_LAYERS UINT32_MAX
260 #define INTEL_REMAINING_LEVELS UINT32_MAX
261
262 void
263 iris_hiz_exec(struct iris_context *ice,
264 struct iris_batch *batch,
265 struct iris_resource *res,
266 unsigned int level, unsigned int start_layer,
267 unsigned int num_layers, enum isl_aux_op op,
268 bool update_clear_depth);
269
270 /**
271 * Prepare a miptree for access
272 *
273 * This function should be called prior to any access to miptree in order to
274 * perform any needed resolves.
275 *
276 * \param[in] start_level The first mip level to be accessed
277 *
278 * \param[in] num_levels The number of miplevels to be accessed or
279 * INTEL_REMAINING_LEVELS to indicate every level
280 * above start_level will be accessed
281 *
282 * \param[in] start_layer The first array slice or 3D layer to be accessed
283 *
284 * \param[in] num_layers The number of array slices or 3D layers be
285 * accessed or INTEL_REMAINING_LAYERS to indicate
286 * every layer above start_layer will be accessed
287 *
288 * \param[in] aux_supported Whether or not the access will support the
289 * miptree's auxiliary compression format; this
290 * must be false for uncompressed miptrees
291 *
292 * \param[in] fast_clear_supported Whether or not the access will support
293 * fast clears in the miptree's auxiliary
294 * compression format
295 */
296 void
297 iris_resource_prepare_access(struct iris_context *ice,
298 struct iris_batch *batch,
299 struct iris_resource *res,
300 uint32_t start_level, uint32_t num_levels,
301 uint32_t start_layer, uint32_t num_layers,
302 enum isl_aux_usage aux_usage,
303 bool fast_clear_supported);
304
305 /**
306 * Complete a write operation
307 *
308 * This function should be called after any operation writes to a miptree.
309 * This will update the miptree's compression state so that future resolves
310 * happen correctly. Technically, this function can be called before the
311 * write occurs but the caller must ensure that they don't interlace
312 * iris_resource_prepare_access and iris_resource_finish_write calls to
313 * overlapping layer/level ranges.
314 *
315 * \param[in] level The mip level that was written
316 *
317 * \param[in] start_layer The first array slice or 3D layer written
318 *
319 * \param[in] num_layers The number of array slices or 3D layers
320 * written or INTEL_REMAINING_LAYERS to indicate
321 * every layer above start_layer was written
322 *
323 * \param[in] written_with_aux Whether or not the write was done with
324 * auxiliary compression enabled
325 */
326 void
327 iris_resource_finish_write(struct iris_context *ice,
328 struct iris_resource *res, uint32_t level,
329 uint32_t start_layer, uint32_t num_layers,
330 enum isl_aux_usage aux_usage);
331
332 /** Get the auxiliary compression state of a miptree slice */
333 enum isl_aux_state
334 iris_resource_get_aux_state(const struct iris_resource *res,
335 uint32_t level, uint32_t layer);
336
337 /**
338 * Set the auxiliary compression state of a miptree slice range
339 *
340 * This function directly sets the auxiliary compression state of a slice
341 * range of a miptree. It only modifies data structures and does not do any
342 * resolves. This should only be called by code which directly performs
343 * compression operations such as fast clears and resolves. Most code should
344 * use iris_resource_prepare_access or iris_resource_finish_write.
345 */
346 void
347 iris_resource_set_aux_state(struct iris_context *ice,
348 struct iris_resource *res, uint32_t level,
349 uint32_t start_layer, uint32_t num_layers,
350 enum isl_aux_state aux_state);
351
352 /**
353 * Prepare a miptree for raw access
354 *
355 * This helper prepares the miptree for access that knows nothing about any
356 * sort of compression whatsoever. This is useful when mapping the surface or
357 * using it with the blitter.
358 */
359 static inline void
360 iris_resource_access_raw(struct iris_context *ice,
361 struct iris_batch *batch,
362 struct iris_resource *res,
363 uint32_t level, uint32_t layer,
364 uint32_t num_layers,
365 bool write)
366 {
367 iris_resource_prepare_access(ice, batch, res, level, 1, layer, num_layers,
368 ISL_AUX_USAGE_NONE, false);
369 if (write) {
370 iris_resource_finish_write(ice, res, level, layer, num_layers,
371 ISL_AUX_USAGE_NONE);
372 }
373 }
374
375 enum isl_aux_usage iris_resource_texture_aux_usage(struct iris_context *ice,
376 const struct iris_resource *res,
377 enum isl_format view_fmt,
378 enum gen9_astc5x5_wa_tex_type);
379 void iris_resource_prepare_texture(struct iris_context *ice,
380 struct iris_batch *batch,
381 struct iris_resource *res,
382 enum isl_format view_format,
383 uint32_t start_level, uint32_t num_levels,
384 uint32_t start_layer, uint32_t num_layers,
385 enum gen9_astc5x5_wa_tex_type);
386 void iris_resource_prepare_image(struct iris_context *ice,
387 struct iris_batch *batch,
388 struct iris_resource *res);
389
390 void iris_resource_check_level_layer(const struct iris_resource *res,
391 uint32_t level, uint32_t layer);
392
393 bool iris_resource_level_has_hiz(const struct iris_resource *res,
394 uint32_t level);
395
396 enum isl_aux_usage iris_resource_render_aux_usage(struct iris_context *ice,
397 struct iris_resource *res,
398 enum isl_format render_fmt,
399 bool blend_enabled,
400 bool draw_aux_disabled);
401 void iris_resource_prepare_render(struct iris_context *ice,
402 struct iris_batch *batch,
403 struct iris_resource *res, uint32_t level,
404 uint32_t start_layer, uint32_t layer_count,
405 enum isl_aux_usage aux_usage);
406 void iris_resource_finish_render(struct iris_context *ice,
407 struct iris_resource *res, uint32_t level,
408 uint32_t start_layer, uint32_t layer_count,
409 enum isl_aux_usage aux_usage);
410 void iris_resource_prepare_depth(struct iris_context *ice,
411 struct iris_batch *batch,
412 struct iris_resource *res, uint32_t level,
413 uint32_t start_layer, uint32_t layer_count);
414 void iris_resource_finish_depth(struct iris_context *ice,
415 struct iris_resource *res, uint32_t level,
416 uint32_t start_layer, uint32_t layer_count,
417 bool depth_written);
418 #endif