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 "util/u_range.h"
29 #include "intel/isl/isl.h"
30 #include "iris_bufmgr.h"
31 
32 struct iris_batch;
33 struct iris_context;
34 struct shader_info;
35 
36 #define IRIS_MAX_MIPLEVELS 15
37 
38 struct iris_format_info {
39    enum isl_format fmt;
40    struct isl_swizzle swizzle;
41 };
42 
43 #define IRIS_RESOURCE_FLAG_SHADER_MEMZONE  (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
44 #define IRIS_RESOURCE_FLAG_SURFACE_MEMZONE (PIPE_RESOURCE_FLAG_DRV_PRIV << 1)
45 #define IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE (PIPE_RESOURCE_FLAG_DRV_PRIV << 2)
46 
47 /**
48  * Resources represent a GPU buffer object or image (mipmap tree).
49  *
50  * They contain the storage (BO) and layout information (ISL surface).
51  */
52 struct iris_resource {
53    struct pipe_resource base;
54    enum pipe_format internal_format;
55 
56    /**
57     * The ISL surface layout information for this resource.
58     *
59     * This is not filled out for PIPE_BUFFER resources, but is guaranteed
60     * to be zeroed.  Note that this also guarantees that res->surf.tiling
61     * will be ISL_TILING_LINEAR, so it's safe to check that.
62     */
63    struct isl_surf surf;
64 
65    /** Backing storage for the resource */
66    struct iris_bo *bo;
67 
68    /** offset at which data starts in the BO */
69    uint64_t offset;
70 
71    /**
72     * A bitfield of PIPE_BIND_* indicating how this resource was bound
73     * in the past.  Only meaningful for PIPE_BUFFER; used for flushing.
74     */
75    unsigned bind_history;
76 
77    /**
78     * A bitfield of MESA_SHADER_* stages indicating where this resource
79     * was bound.
80     */
81    unsigned bind_stages;
82 
83    /**
84     * For PIPE_BUFFER resources, a range which may contain valid data.
85     *
86     * This is a conservative estimate of what part of the buffer contains
87     * valid data that we have to preserve.  The rest of the buffer is
88     * considered invalid, and we can promote writes to that region to
89     * be unsynchronized writes, avoiding blit copies.
90     */
91    struct util_range valid_buffer_range;
92 
93    /**
94     * Auxiliary buffer information (CCS, MCS, or HiZ).
95     */
96    struct {
97       /** The surface layout for the auxiliary buffer. */
98       struct isl_surf surf;
99 
100       /** The buffer object containing the auxiliary data. */
101       struct iris_bo *bo;
102 
103       /** Offset into 'bo' where the auxiliary surface starts. */
104       uint32_t offset;
105 
106       struct {
107          struct isl_surf surf;
108 
109          /** Offset into 'bo' where the auxiliary surface starts. */
110          uint32_t offset;
111       } extra_aux;
112 
113       /**
114        * Fast clear color for this surface.  For depth surfaces, the clear
115        * value is stored as a float32 in the red component.
116        */
117       union isl_color_value clear_color;
118 
119       /** Buffer object containing the indirect clear color.  */
120       struct iris_bo *clear_color_bo;
121 
122       /** Offset into bo where the clear color can be found.  */
123       uint64_t clear_color_offset;
124 
125       /**
126        * \brief The type of auxiliary compression used by this resource.
127        *
128        * This describes the type of auxiliary compression that is intended to
129        * be used by this resource.  An aux usage of ISL_AUX_USAGE_NONE means
130        * that auxiliary compression is permanently disabled.  An aux usage
131        * other than ISL_AUX_USAGE_NONE does not imply that auxiliary
132        * compression will always be enabled for this surface.
133        */
134       enum isl_aux_usage usage;
135 
136       /**
137        * A bitfield of ISL_AUX_* modes that might this resource might use.
138        *
139        * For example, a surface might use both CCS_E and CCS_D at times.
140        */
141       unsigned possible_usages;
142 
143       /**
144        * Same as possible_usages, but only with modes supported for sampling.
145        */
146       unsigned sampler_usages;
147 
148       /**
149        * \brief Maps miptree slices to their current aux state.
150        *
151        * This two-dimensional array is indexed as [level][layer] and stores an
152        * aux state for each slice.
153        */
154       enum isl_aux_state **state;
155 
156       /**
157        * If (1 << level) is set, HiZ is enabled for that miplevel.
158        */
159       uint16_t has_hiz;
160    } aux;
161 
162    /**
163     * For external surfaces, this is format that was used to create or import
164     * the surface. For internal surfaces, this will always be
165     * PIPE_FORMAT_NONE.
166     */
167    enum pipe_format external_format;
168 
169    /**
170     * For external surfaces, this is DRM format modifier that was used to
171     * create or import the surface.  For internal surfaces, this will always
172     * be DRM_FORMAT_MOD_INVALID.
173     */
174    const struct isl_drm_modifier_info *mod_info;
175 };
176 
177 /**
178  * A simple <resource, offset> tuple for storing a reference to a
179  * piece of state stored in a GPU buffer object.
180  */
181 struct iris_state_ref {
182    struct pipe_resource *res;
183    uint32_t offset;
184 };
185 
186 /**
187  * The SURFACE_STATE descriptors for a resource.
188  */
189 struct iris_surface_state {
190    /**
191     * CPU-side copy of the packed SURFACE_STATE structures, already
192     * aligned so they can be uploaded as a contiguous pile of bytes.
193     *
194     * This can be updated and re-uploaded if (e.g.) addresses need to change.
195     */
196    uint32_t *cpu;
197 
198    /**
199     * How many states are there?  (Each aux mode has its own state.)
200     */
201    unsigned num_states;
202 
203    /**
204     * Address of the resource (res->bo->gtt_offset).  Note that "Surface
205     * Base Address" may be offset from this value.
206     */
207    uint64_t bo_address;
208 
209    /** A reference to the GPU buffer holding our uploaded SURFACE_STATE */
210    struct iris_state_ref ref;
211 };
212 
213 /**
214  * Gallium CSO for sampler views (texture views).
215  *
216  * In addition to the normal pipe_resource, this adds an ISL view
217  * which may reinterpret the format or restrict levels/layers.
218  *
219  * These can also be linear texture buffers.
220  */
221 struct iris_sampler_view {
222    struct pipe_sampler_view base;
223    struct isl_view view;
224 
225    union isl_color_value clear_color;
226 
227    /* A short-cut (not a reference) to the actual resource being viewed.
228     * Multi-planar (or depth+stencil) images may have multiple resources
229     * chained together; this skips having to traverse base->texture->*.
230     */
231    struct iris_resource *res;
232 
233    /** The resource (BO) holding our SURFACE_STATE. */
234    struct iris_surface_state surface_state;
235 };
236 
237 /**
238  * Image view representation.
239  */
240 struct iris_image_view {
241    struct pipe_image_view base;
242 
243    /** The resource (BO) holding our SURFACE_STATE. */
244    struct iris_surface_state surface_state;
245 };
246 
247 /**
248  * Gallium CSO for surfaces (framebuffer attachments).
249  *
250  * A view of a surface that can be bound to a color render target or
251  * depth/stencil attachment.
252  */
253 struct iris_surface {
254    struct pipe_surface base;
255    struct isl_view view;
256    struct isl_view read_view;
257    union isl_color_value clear_color;
258 
259    /** The resource (BO) holding our SURFACE_STATE. */
260    struct iris_surface_state surface_state;
261    /** The resource (BO) holding our SURFACE_STATE for read. */
262    struct iris_surface_state surface_state_read;
263 };
264 
265 /**
266  * Transfer object - information about a buffer mapping.
267  */
268 struct iris_transfer {
269    struct pipe_transfer base;
270    struct pipe_debug_callback *dbg;
271    void *buffer;
272    void *ptr;
273 
274    /** A linear staging resource for GPU-based copy_region transfers. */
275    struct pipe_resource *staging;
276    struct blorp_context *blorp;
277    struct iris_batch *batch;
278 
279    bool dest_had_defined_contents;
280 
281    void (*unmap)(struct iris_transfer *);
282 };
283 
284 /**
285  * Unwrap a pipe_resource to get the underlying iris_bo (for convenience).
286  */
287 static inline struct iris_bo *
iris_resource_bo(struct pipe_resource * p_res)288 iris_resource_bo(struct pipe_resource *p_res)
289 {
290    struct iris_resource *res = (void *) p_res;
291    return res->bo;
292 }
293 
294 static inline uint32_t
iris_mocs(const struct iris_bo * bo,const struct isl_device * dev,isl_surf_usage_flags_t usage)295 iris_mocs(const struct iris_bo *bo,
296           const struct isl_device *dev,
297           isl_surf_usage_flags_t usage)
298 {
299    return bo && bo->external ? dev->mocs.external : isl_mocs(dev, usage);
300 }
301 
302 struct iris_format_info iris_format_for_usage(const struct gen_device_info *,
303                                               enum pipe_format pf,
304                                               isl_surf_usage_flags_t usage);
305 
306 struct pipe_resource *iris_resource_get_separate_stencil(struct pipe_resource *);
307 
308 void iris_get_depth_stencil_resources(struct pipe_resource *res,
309                                       struct iris_resource **out_z,
310                                       struct iris_resource **out_s);
311 bool iris_resource_set_clear_color(struct iris_context *ice,
312                                    struct iris_resource *res,
313                                    union isl_color_value color);
314 union isl_color_value
315 iris_resource_get_clear_color(const struct iris_resource *res,
316                               struct iris_bo **clear_color_bo,
317                               uint64_t *clear_color_offset);
318 
319 void iris_init_screen_resource_functions(struct pipe_screen *pscreen);
320 
321 void iris_dirty_for_history(struct iris_context *ice,
322                             struct iris_resource *res);
323 uint32_t iris_flush_bits_for_history(struct iris_context *ice,
324                                      struct iris_resource *res);
325 
326 void iris_flush_and_dirty_for_history(struct iris_context *ice,
327                                       struct iris_batch *batch,
328                                       struct iris_resource *res,
329                                       uint32_t extra_flags,
330                                       const char *reason);
331 
332 unsigned iris_get_num_logical_layers(const struct iris_resource *res,
333                                      unsigned level);
334 
335 void iris_resource_disable_aux(struct iris_resource *res);
336 
337 #define INTEL_REMAINING_LAYERS UINT32_MAX
338 #define INTEL_REMAINING_LEVELS UINT32_MAX
339 
340 void
341 iris_hiz_exec(struct iris_context *ice,
342               struct iris_batch *batch,
343               struct iris_resource *res,
344               unsigned int level, unsigned int start_layer,
345               unsigned int num_layers, enum isl_aux_op op,
346               bool update_clear_depth);
347 
348 /**
349  * Prepare a miptree for access
350  *
351  * This function should be called prior to any access to miptree in order to
352  * perform any needed resolves.
353  *
354  * \param[in]  start_level    The first mip level to be accessed
355  *
356  * \param[in]  num_levels     The number of miplevels to be accessed or
357  *                            INTEL_REMAINING_LEVELS to indicate every level
358  *                            above start_level will be accessed
359  *
360  * \param[in]  start_layer    The first array slice or 3D layer to be accessed
361  *
362  * \param[in]  num_layers     The number of array slices or 3D layers be
363  *                            accessed or INTEL_REMAINING_LAYERS to indicate
364  *                            every layer above start_layer will be accessed
365  *
366  * \param[in]  aux_supported  Whether or not the access will support the
367  *                            miptree's auxiliary compression format;  this
368  *                            must be false for uncompressed miptrees
369  *
370  * \param[in]  fast_clear_supported Whether or not the access will support
371  *                                  fast clears in the miptree's auxiliary
372  *                                  compression format
373  */
374 void
375 iris_resource_prepare_access(struct iris_context *ice,
376                              struct iris_resource *res,
377                              uint32_t start_level, uint32_t num_levels,
378                              uint32_t start_layer, uint32_t num_layers,
379                              enum isl_aux_usage aux_usage,
380                              bool fast_clear_supported);
381 
382 /**
383  * Complete a write operation
384  *
385  * This function should be called after any operation writes to a miptree.
386  * This will update the miptree's compression state so that future resolves
387  * happen correctly.  Technically, this function can be called before the
388  * write occurs but the caller must ensure that they don't interlace
389  * iris_resource_prepare_access and iris_resource_finish_write calls to
390  * overlapping layer/level ranges.
391  *
392  * \param[in]  level             The mip level that was written
393  *
394  * \param[in]  start_layer       The first array slice or 3D layer written
395  *
396  * \param[in]  num_layers        The number of array slices or 3D layers
397  *                               written or INTEL_REMAINING_LAYERS to indicate
398  *                               every layer above start_layer was written
399  *
400  * \param[in]  written_with_aux  Whether or not the write was done with
401  *                               auxiliary compression enabled
402  */
403 void
404 iris_resource_finish_write(struct iris_context *ice,
405                            struct iris_resource *res, uint32_t level,
406                            uint32_t start_layer, uint32_t num_layers,
407                            enum isl_aux_usage aux_usage);
408 
409 /** Get the auxiliary compression state of a miptree slice */
410 enum isl_aux_state
411 iris_resource_get_aux_state(const struct iris_resource *res,
412                             uint32_t level, uint32_t layer);
413 
414 /**
415  * Set the auxiliary compression state of a miptree slice range
416  *
417  * This function directly sets the auxiliary compression state of a slice
418  * range of a miptree.  It only modifies data structures and does not do any
419  * resolves.  This should only be called by code which directly performs
420  * compression operations such as fast clears and resolves.  Most code should
421  * use iris_resource_prepare_access or iris_resource_finish_write.
422  */
423 void
424 iris_resource_set_aux_state(struct iris_context *ice,
425                             struct iris_resource *res, uint32_t level,
426                             uint32_t start_layer, uint32_t num_layers,
427                             enum isl_aux_state aux_state);
428 
429 /**
430  * Prepare a miptree for raw access
431  *
432  * This helper prepares the miptree for access that knows nothing about any
433  * sort of compression whatsoever.  This is useful when mapping the surface or
434  * using it with the blitter.
435  */
436 static inline void
iris_resource_access_raw(struct iris_context * ice,struct iris_resource * res,uint32_t level,uint32_t layer,uint32_t num_layers,bool write)437 iris_resource_access_raw(struct iris_context *ice,
438                          struct iris_resource *res,
439                          uint32_t level, uint32_t layer,
440                          uint32_t num_layers,
441                          bool write)
442 {
443    iris_resource_prepare_access(ice, res, level, 1, layer, num_layers,
444                                 ISL_AUX_USAGE_NONE, false);
445    if (write) {
446       iris_resource_finish_write(ice, res, level, layer, num_layers,
447                                  ISL_AUX_USAGE_NONE);
448    }
449 }
450 
451 enum isl_dim_layout iris_get_isl_dim_layout(const struct gen_device_info *devinfo,
452                                             enum isl_tiling tiling,
453                                             enum pipe_texture_target target);
454 enum isl_surf_dim target_to_isl_surf_dim(enum pipe_texture_target target);
455 uint32_t iris_resource_get_tile_offsets(const struct iris_resource *res,
456                                         uint32_t level, uint32_t z,
457                                         uint32_t *tile_x, uint32_t *tile_y);
458 enum isl_aux_usage iris_resource_texture_aux_usage(struct iris_context *ice,
459                                                    const struct iris_resource *res,
460                                                    enum isl_format view_fmt);
461 void iris_resource_prepare_texture(struct iris_context *ice,
462                                    struct iris_resource *res,
463                                    enum isl_format view_format,
464                                    uint32_t start_level, uint32_t num_levels,
465                                    uint32_t start_layer, uint32_t num_layers);
466 
467 enum isl_aux_usage iris_image_view_aux_usage(struct iris_context *ice,
468                                              const struct pipe_image_view *pview,
469                                              const struct shader_info *info);
470 enum isl_format iris_image_view_get_format(struct iris_context *ice,
471                                            const struct pipe_image_view *img);
472 
473 static inline bool
iris_resource_unfinished_aux_import(struct iris_resource * res)474 iris_resource_unfinished_aux_import(struct iris_resource *res)
475 {
476    return res->aux.bo == NULL && res->mod_info &&
477       res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
478 }
479 
480 void iris_resource_finish_aux_import(struct pipe_screen *pscreen,
481                                      struct iris_resource *res);
482 
483 bool iris_has_invalid_primary(const struct iris_resource *res,
484                               unsigned start_level, unsigned num_levels,
485                               unsigned start_layer, unsigned num_layers);
486 
487 void iris_resource_check_level_layer(const struct iris_resource *res,
488                                      uint32_t level, uint32_t layer);
489 
490 bool iris_resource_level_has_hiz(const struct iris_resource *res,
491                                  uint32_t level);
492 
493 bool iris_sample_with_depth_aux(const struct gen_device_info *devinfo,
494                                 const struct iris_resource *res);
495 
496 bool iris_has_color_unresolved(const struct iris_resource *res,
497                                unsigned start_level, unsigned num_levels,
498                                unsigned start_layer, unsigned num_layers);
499 
500 bool iris_render_formats_color_compatible(enum isl_format a,
501                                           enum isl_format b,
502                                           union isl_color_value color);
503 enum isl_aux_usage iris_resource_render_aux_usage(struct iris_context *ice,
504                                                   struct iris_resource *res,
505                                                   enum isl_format render_fmt,
506                                                   bool draw_aux_disabled);
507 void iris_resource_prepare_render(struct iris_context *ice,
508                                   struct iris_batch *batch,
509                                   struct iris_resource *res, uint32_t level,
510                                   uint32_t start_layer, uint32_t layer_count,
511                                   enum isl_aux_usage aux_usage);
512 void iris_resource_finish_render(struct iris_context *ice,
513                                  struct iris_resource *res, uint32_t level,
514                                  uint32_t start_layer, uint32_t layer_count,
515                                  enum isl_aux_usage aux_usage);
516 void iris_resource_prepare_depth(struct iris_context *ice,
517                                  struct iris_batch *batch,
518                                  struct iris_resource *res, uint32_t level,
519                                  uint32_t start_layer, uint32_t layer_count);
520 void iris_resource_finish_depth(struct iris_context *ice,
521                                 struct iris_resource *res, uint32_t level,
522                                 uint32_t start_layer, uint32_t layer_count,
523                                 bool depth_written);
524 #endif
525