1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
12bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/**
24bf215546Sopenharmony_ci * @file iris_resource.c
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci * Resources are images, buffers, and other objects used by the GPU.
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci * XXX: explain resources
29bf215546Sopenharmony_ci */
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include <stdio.h>
32bf215546Sopenharmony_ci#include <errno.h>
33bf215546Sopenharmony_ci#include "pipe/p_defines.h"
34bf215546Sopenharmony_ci#include "pipe/p_state.h"
35bf215546Sopenharmony_ci#include "pipe/p_context.h"
36bf215546Sopenharmony_ci#include "pipe/p_screen.h"
37bf215546Sopenharmony_ci#include "util/os_memory.h"
38bf215546Sopenharmony_ci#include "util/u_cpu_detect.h"
39bf215546Sopenharmony_ci#include "util/u_inlines.h"
40bf215546Sopenharmony_ci#include "util/format/u_format.h"
41bf215546Sopenharmony_ci#include "util/u_memory.h"
42bf215546Sopenharmony_ci#include "util/u_threaded_context.h"
43bf215546Sopenharmony_ci#include "util/u_transfer.h"
44bf215546Sopenharmony_ci#include "util/u_transfer_helper.h"
45bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
46bf215546Sopenharmony_ci#include "util/ralloc.h"
47bf215546Sopenharmony_ci#include "iris_batch.h"
48bf215546Sopenharmony_ci#include "iris_context.h"
49bf215546Sopenharmony_ci#include "iris_resource.h"
50bf215546Sopenharmony_ci#include "iris_screen.h"
51bf215546Sopenharmony_ci#include "intel/common/intel_aux_map.h"
52bf215546Sopenharmony_ci#include "intel/dev/intel_debug.h"
53bf215546Sopenharmony_ci#include "isl/isl.h"
54bf215546Sopenharmony_ci#include "drm-uapi/drm_fourcc.h"
55bf215546Sopenharmony_ci#include "drm-uapi/i915_drm.h"
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cienum modifier_priority {
58bf215546Sopenharmony_ci   MODIFIER_PRIORITY_INVALID = 0,
59bf215546Sopenharmony_ci   MODIFIER_PRIORITY_LINEAR,
60bf215546Sopenharmony_ci   MODIFIER_PRIORITY_X,
61bf215546Sopenharmony_ci   MODIFIER_PRIORITY_Y,
62bf215546Sopenharmony_ci   MODIFIER_PRIORITY_Y_CCS,
63bf215546Sopenharmony_ci   MODIFIER_PRIORITY_Y_GFX12_RC_CCS,
64bf215546Sopenharmony_ci   MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC,
65bf215546Sopenharmony_ci   MODIFIER_PRIORITY_4,
66bf215546Sopenharmony_ci   MODIFIER_PRIORITY_4_DG2_RC_CCS,
67bf215546Sopenharmony_ci   MODIFIER_PRIORITY_4_DG2_RC_CCS_CC,
68bf215546Sopenharmony_ci};
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_cistatic const uint64_t priority_to_modifier[] = {
71bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
72bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
73bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
74bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
75bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
76bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_Y_GFX12_RC_CCS] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
77bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC] = I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,
78bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_4] = I915_FORMAT_MOD_4_TILED,
79bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_4_DG2_RC_CCS] = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS,
80bf215546Sopenharmony_ci   [MODIFIER_PRIORITY_4_DG2_RC_CCS_CC] = I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC,
81bf215546Sopenharmony_ci};
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic bool
84bf215546Sopenharmony_cimodifier_is_supported(const struct intel_device_info *devinfo,
85bf215546Sopenharmony_ci                      enum pipe_format pfmt, unsigned bind,
86bf215546Sopenharmony_ci                      uint64_t modifier)
87bf215546Sopenharmony_ci{
88bf215546Sopenharmony_ci   /* Check for basic device support. */
89bf215546Sopenharmony_ci   switch (modifier) {
90bf215546Sopenharmony_ci   case DRM_FORMAT_MOD_LINEAR:
91bf215546Sopenharmony_ci   case I915_FORMAT_MOD_X_TILED:
92bf215546Sopenharmony_ci      break;
93bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED:
94bf215546Sopenharmony_ci      if (devinfo->ver <= 8 && (bind & PIPE_BIND_SCANOUT))
95bf215546Sopenharmony_ci         return false;
96bf215546Sopenharmony_ci      if (devinfo->verx10 >= 125)
97bf215546Sopenharmony_ci         return false;
98bf215546Sopenharmony_ci      break;
99bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_CCS:
100bf215546Sopenharmony_ci      if (devinfo->ver <= 8 || devinfo->ver >= 12)
101bf215546Sopenharmony_ci         return false;
102bf215546Sopenharmony_ci      break;
103bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
104bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
105bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
106bf215546Sopenharmony_ci      if (devinfo->verx10 != 120)
107bf215546Sopenharmony_ci         return false;
108bf215546Sopenharmony_ci      break;
109bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED:
110bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
111bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
112bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
113bf215546Sopenharmony_ci      if (devinfo->verx10 < 125)
114bf215546Sopenharmony_ci         return false;
115bf215546Sopenharmony_ci      break;
116bf215546Sopenharmony_ci   case DRM_FORMAT_MOD_INVALID:
117bf215546Sopenharmony_ci   default:
118bf215546Sopenharmony_ci      return false;
119bf215546Sopenharmony_ci   }
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   /* Check remaining requirements. */
122bf215546Sopenharmony_ci   switch (modifier) {
123bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
124bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
125bf215546Sopenharmony_ci      if (INTEL_DEBUG(DEBUG_NO_CCS))
126bf215546Sopenharmony_ci         return false;
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci      if (pfmt != PIPE_FORMAT_BGRA8888_UNORM &&
129bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_RGBA8888_UNORM &&
130bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_BGRX8888_UNORM &&
131bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_RGBX8888_UNORM &&
132bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_NV12 &&
133bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_P010 &&
134bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_P012 &&
135bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_P016 &&
136bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_YUYV &&
137bf215546Sopenharmony_ci          pfmt != PIPE_FORMAT_UYVY) {
138bf215546Sopenharmony_ci         return false;
139bf215546Sopenharmony_ci      }
140bf215546Sopenharmony_ci      break;
141bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
142bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
143bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
144bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
145bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_CCS: {
146bf215546Sopenharmony_ci      if (INTEL_DEBUG(DEBUG_NO_CCS))
147bf215546Sopenharmony_ci         return false;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci      enum isl_format rt_format =
150bf215546Sopenharmony_ci         iris_format_for_usage(devinfo, pfmt,
151bf215546Sopenharmony_ci                               ISL_SURF_USAGE_RENDER_TARGET_BIT).fmt;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci      if (rt_format == ISL_FORMAT_UNSUPPORTED ||
154bf215546Sopenharmony_ci          !isl_format_supports_ccs_e(devinfo, rt_format))
155bf215546Sopenharmony_ci         return false;
156bf215546Sopenharmony_ci      break;
157bf215546Sopenharmony_ci   }
158bf215546Sopenharmony_ci   default:
159bf215546Sopenharmony_ci      break;
160bf215546Sopenharmony_ci   }
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci   return true;
163bf215546Sopenharmony_ci}
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_cistatic uint64_t
166bf215546Sopenharmony_ciselect_best_modifier(struct intel_device_info *devinfo,
167bf215546Sopenharmony_ci                     const struct pipe_resource *templ,
168bf215546Sopenharmony_ci                     const uint64_t *modifiers,
169bf215546Sopenharmony_ci                     int count)
170bf215546Sopenharmony_ci{
171bf215546Sopenharmony_ci   enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   for (int i = 0; i < count; i++) {
174bf215546Sopenharmony_ci      if (!modifier_is_supported(devinfo, templ->format, templ->bind,
175bf215546Sopenharmony_ci                                 modifiers[i]))
176bf215546Sopenharmony_ci         continue;
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci      switch (modifiers[i]) {
179bf215546Sopenharmony_ci      case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
180bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_4_DG2_RC_CCS_CC);
181bf215546Sopenharmony_ci         break;
182bf215546Sopenharmony_ci      case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
183bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_4_DG2_RC_CCS);
184bf215546Sopenharmony_ci         break;
185bf215546Sopenharmony_ci      case I915_FORMAT_MOD_4_TILED:
186bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_4);
187bf215546Sopenharmony_ci         break;
188bf215546Sopenharmony_ci      case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
189bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_Y_GFX12_RC_CCS_CC);
190bf215546Sopenharmony_ci         break;
191bf215546Sopenharmony_ci      case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
192bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_Y_GFX12_RC_CCS);
193bf215546Sopenharmony_ci         break;
194bf215546Sopenharmony_ci      case I915_FORMAT_MOD_Y_TILED_CCS:
195bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
196bf215546Sopenharmony_ci         break;
197bf215546Sopenharmony_ci      case I915_FORMAT_MOD_Y_TILED:
198bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_Y);
199bf215546Sopenharmony_ci         break;
200bf215546Sopenharmony_ci      case I915_FORMAT_MOD_X_TILED:
201bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_X);
202bf215546Sopenharmony_ci         break;
203bf215546Sopenharmony_ci      case DRM_FORMAT_MOD_LINEAR:
204bf215546Sopenharmony_ci         prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
205bf215546Sopenharmony_ci         break;
206bf215546Sopenharmony_ci      case DRM_FORMAT_MOD_INVALID:
207bf215546Sopenharmony_ci      default:
208bf215546Sopenharmony_ci         break;
209bf215546Sopenharmony_ci      }
210bf215546Sopenharmony_ci   }
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   return priority_to_modifier[prio];
213bf215546Sopenharmony_ci}
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_cistatic inline bool is_modifier_external_only(enum pipe_format pfmt,
216bf215546Sopenharmony_ci                                             uint64_t modifier)
217bf215546Sopenharmony_ci{
218bf215546Sopenharmony_ci   /* Only allow external usage for the following cases: YUV formats
219bf215546Sopenharmony_ci    * and the media-compression modifier. The render engine lacks
220bf215546Sopenharmony_ci    * support for rendering to a media-compressed surface if the
221bf215546Sopenharmony_ci    * compression ratio is large enough. By requiring external usage
222bf215546Sopenharmony_ci    * of media-compressed surfaces, resolves are avoided.
223bf215546Sopenharmony_ci    */
224bf215546Sopenharmony_ci   return util_format_is_yuv(pfmt) ||
225bf215546Sopenharmony_ci          isl_drm_modifier_get_info(modifier)->aux_usage == ISL_AUX_USAGE_MC;
226bf215546Sopenharmony_ci}
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_cistatic void
229bf215546Sopenharmony_ciiris_query_dmabuf_modifiers(struct pipe_screen *pscreen,
230bf215546Sopenharmony_ci                            enum pipe_format pfmt,
231bf215546Sopenharmony_ci                            int max,
232bf215546Sopenharmony_ci                            uint64_t *modifiers,
233bf215546Sopenharmony_ci                            unsigned int *external_only,
234bf215546Sopenharmony_ci                            int *count)
235bf215546Sopenharmony_ci{
236bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) pscreen;
237bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &screen->devinfo;
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   uint64_t all_modifiers[] = {
240bf215546Sopenharmony_ci      DRM_FORMAT_MOD_LINEAR,
241bf215546Sopenharmony_ci      I915_FORMAT_MOD_X_TILED,
242bf215546Sopenharmony_ci      I915_FORMAT_MOD_4_TILED,
243bf215546Sopenharmony_ci      I915_FORMAT_MOD_4_TILED_DG2_RC_CCS,
244bf215546Sopenharmony_ci      I915_FORMAT_MOD_4_TILED_DG2_MC_CCS,
245bf215546Sopenharmony_ci      I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC,
246bf215546Sopenharmony_ci      I915_FORMAT_MOD_Y_TILED,
247bf215546Sopenharmony_ci      I915_FORMAT_MOD_Y_TILED_CCS,
248bf215546Sopenharmony_ci      I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS,
249bf215546Sopenharmony_ci      I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS,
250bf215546Sopenharmony_ci      I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC,
251bf215546Sopenharmony_ci   };
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci   int supported_mods = 0;
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_ci   for (int i = 0; i < ARRAY_SIZE(all_modifiers); i++) {
256bf215546Sopenharmony_ci      if (!modifier_is_supported(devinfo, pfmt, 0, all_modifiers[i]))
257bf215546Sopenharmony_ci         continue;
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci      if (supported_mods < max) {
260bf215546Sopenharmony_ci         if (modifiers)
261bf215546Sopenharmony_ci            modifiers[supported_mods] = all_modifiers[i];
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci         if (external_only) {
264bf215546Sopenharmony_ci            external_only[supported_mods] =
265bf215546Sopenharmony_ci               is_modifier_external_only(pfmt, all_modifiers[i]);
266bf215546Sopenharmony_ci         }
267bf215546Sopenharmony_ci      }
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci      supported_mods++;
270bf215546Sopenharmony_ci   }
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   *count = supported_mods;
273bf215546Sopenharmony_ci}
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_cistatic bool
276bf215546Sopenharmony_ciiris_is_dmabuf_modifier_supported(struct pipe_screen *pscreen,
277bf215546Sopenharmony_ci                                  uint64_t modifier, enum pipe_format pfmt,
278bf215546Sopenharmony_ci                                  bool *external_only)
279bf215546Sopenharmony_ci{
280bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) pscreen;
281bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &screen->devinfo;
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci   if (modifier_is_supported(devinfo, pfmt, 0, modifier)) {
284bf215546Sopenharmony_ci      if (external_only)
285bf215546Sopenharmony_ci         *external_only = is_modifier_external_only(pfmt, modifier);
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci      return true;
288bf215546Sopenharmony_ci   }
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci   return false;
291bf215546Sopenharmony_ci}
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_cistatic unsigned int
294bf215546Sopenharmony_ciiris_get_dmabuf_modifier_planes(struct pipe_screen *pscreen, uint64_t modifier,
295bf215546Sopenharmony_ci                                enum pipe_format format)
296bf215546Sopenharmony_ci{
297bf215546Sopenharmony_ci   unsigned int planes = util_format_get_num_planes(format);
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci   switch (modifier) {
300bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
301bf215546Sopenharmony_ci      return 3;
302bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
303bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
304bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
305bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_CCS:
306bf215546Sopenharmony_ci      return 2 * planes;
307bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
308bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
309bf215546Sopenharmony_ci   default:
310bf215546Sopenharmony_ci      return planes;
311bf215546Sopenharmony_ci   }
312bf215546Sopenharmony_ci}
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_cienum isl_format
315bf215546Sopenharmony_ciiris_image_view_get_format(struct iris_context *ice,
316bf215546Sopenharmony_ci                           const struct pipe_image_view *img)
317bf215546Sopenharmony_ci{
318bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
319bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &screen->devinfo;
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci   isl_surf_usage_flags_t usage = ISL_SURF_USAGE_STORAGE_BIT;
322bf215546Sopenharmony_ci   enum isl_format isl_fmt =
323bf215546Sopenharmony_ci      iris_format_for_usage(devinfo, img->format, usage).fmt;
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci   if (img->shader_access & PIPE_IMAGE_ACCESS_READ) {
326bf215546Sopenharmony_ci      /* On Gfx8, try to use typed surfaces reads (which support a
327bf215546Sopenharmony_ci       * limited number of formats), and if not possible, fall back
328bf215546Sopenharmony_ci       * to untyped reads.
329bf215546Sopenharmony_ci       */
330bf215546Sopenharmony_ci      if (devinfo->ver == 8 &&
331bf215546Sopenharmony_ci          !isl_has_matching_typed_storage_image_format(devinfo, isl_fmt))
332bf215546Sopenharmony_ci         return ISL_FORMAT_RAW;
333bf215546Sopenharmony_ci      else
334bf215546Sopenharmony_ci         return isl_lower_storage_image_format(devinfo, isl_fmt);
335bf215546Sopenharmony_ci   }
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci   return isl_fmt;
338bf215546Sopenharmony_ci}
339bf215546Sopenharmony_ci
340bf215546Sopenharmony_cistatic struct pipe_memory_object *
341bf215546Sopenharmony_ciiris_memobj_create_from_handle(struct pipe_screen *pscreen,
342bf215546Sopenharmony_ci                               struct winsys_handle *whandle,
343bf215546Sopenharmony_ci                               bool dedicated)
344bf215546Sopenharmony_ci{
345bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
346bf215546Sopenharmony_ci   struct iris_memory_object *memobj = CALLOC_STRUCT(iris_memory_object);
347bf215546Sopenharmony_ci   struct iris_bo *bo;
348bf215546Sopenharmony_ci
349bf215546Sopenharmony_ci   if (!memobj)
350bf215546Sopenharmony_ci      return NULL;
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci   switch (whandle->type) {
353bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_SHARED:
354bf215546Sopenharmony_ci      bo = iris_bo_gem_create_from_name(screen->bufmgr, "winsys image",
355bf215546Sopenharmony_ci                                        whandle->handle);
356bf215546Sopenharmony_ci      break;
357bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_FD:
358bf215546Sopenharmony_ci      bo = iris_bo_import_dmabuf(screen->bufmgr, whandle->handle);
359bf215546Sopenharmony_ci      break;
360bf215546Sopenharmony_ci   default:
361bf215546Sopenharmony_ci      unreachable("invalid winsys handle type");
362bf215546Sopenharmony_ci   }
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci   if (!bo) {
365bf215546Sopenharmony_ci      free(memobj);
366bf215546Sopenharmony_ci      return NULL;
367bf215546Sopenharmony_ci   }
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   memobj->b.dedicated = dedicated;
370bf215546Sopenharmony_ci   memobj->bo = bo;
371bf215546Sopenharmony_ci   memobj->format = whandle->format;
372bf215546Sopenharmony_ci   memobj->stride = whandle->stride;
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci   return &memobj->b;
375bf215546Sopenharmony_ci}
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_cistatic void
378bf215546Sopenharmony_ciiris_memobj_destroy(struct pipe_screen *pscreen,
379bf215546Sopenharmony_ci                    struct pipe_memory_object *pmemobj)
380bf215546Sopenharmony_ci{
381bf215546Sopenharmony_ci   struct iris_memory_object *memobj = (struct iris_memory_object *)pmemobj;
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   iris_bo_unreference(memobj->bo);
384bf215546Sopenharmony_ci   free(memobj);
385bf215546Sopenharmony_ci}
386bf215546Sopenharmony_ci
387bf215546Sopenharmony_cistruct pipe_resource *
388bf215546Sopenharmony_ciiris_resource_get_separate_stencil(struct pipe_resource *p_res)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci   /* For packed depth-stencil, we treat depth as the primary resource
391bf215546Sopenharmony_ci    * and store S8 as the "second plane" resource.
392bf215546Sopenharmony_ci    */
393bf215546Sopenharmony_ci   if (p_res->next && p_res->next->format == PIPE_FORMAT_S8_UINT)
394bf215546Sopenharmony_ci      return p_res->next;
395bf215546Sopenharmony_ci
396bf215546Sopenharmony_ci   return NULL;
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci}
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_cistatic void
401bf215546Sopenharmony_ciiris_resource_set_separate_stencil(struct pipe_resource *p_res,
402bf215546Sopenharmony_ci                                   struct pipe_resource *stencil)
403bf215546Sopenharmony_ci{
404bf215546Sopenharmony_ci   assert(util_format_has_depth(util_format_description(p_res->format)));
405bf215546Sopenharmony_ci   pipe_resource_reference(&p_res->next, stencil);
406bf215546Sopenharmony_ci}
407bf215546Sopenharmony_ci
408bf215546Sopenharmony_civoid
409bf215546Sopenharmony_ciiris_get_depth_stencil_resources(struct pipe_resource *res,
410bf215546Sopenharmony_ci                                 struct iris_resource **out_z,
411bf215546Sopenharmony_ci                                 struct iris_resource **out_s)
412bf215546Sopenharmony_ci{
413bf215546Sopenharmony_ci   if (!res) {
414bf215546Sopenharmony_ci      *out_z = NULL;
415bf215546Sopenharmony_ci      *out_s = NULL;
416bf215546Sopenharmony_ci      return;
417bf215546Sopenharmony_ci   }
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci   if (res->format != PIPE_FORMAT_S8_UINT) {
420bf215546Sopenharmony_ci      *out_z = (void *) res;
421bf215546Sopenharmony_ci      *out_s = (void *) iris_resource_get_separate_stencil(res);
422bf215546Sopenharmony_ci   } else {
423bf215546Sopenharmony_ci      *out_z = NULL;
424bf215546Sopenharmony_ci      *out_s = (void *) res;
425bf215546Sopenharmony_ci   }
426bf215546Sopenharmony_ci}
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_civoid
429bf215546Sopenharmony_ciiris_resource_disable_aux(struct iris_resource *res)
430bf215546Sopenharmony_ci{
431bf215546Sopenharmony_ci   iris_bo_unreference(res->aux.bo);
432bf215546Sopenharmony_ci   iris_bo_unreference(res->aux.clear_color_bo);
433bf215546Sopenharmony_ci   free(res->aux.state);
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ci   res->aux.usage = ISL_AUX_USAGE_NONE;
436bf215546Sopenharmony_ci   res->aux.surf.size_B = 0;
437bf215546Sopenharmony_ci   res->aux.bo = NULL;
438bf215546Sopenharmony_ci   res->aux.extra_aux.surf.size_B = 0;
439bf215546Sopenharmony_ci   res->aux.clear_color_bo = NULL;
440bf215546Sopenharmony_ci   res->aux.state = NULL;
441bf215546Sopenharmony_ci}
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_cistatic uint32_t
444bf215546Sopenharmony_ciiris_resource_alloc_flags(const struct iris_screen *screen,
445bf215546Sopenharmony_ci                          const struct pipe_resource *templ,
446bf215546Sopenharmony_ci                          enum isl_aux_usage aux_usage)
447bf215546Sopenharmony_ci{
448bf215546Sopenharmony_ci   if (templ->flags & IRIS_RESOURCE_FLAG_DEVICE_MEM)
449bf215546Sopenharmony_ci      return 0;
450bf215546Sopenharmony_ci
451bf215546Sopenharmony_ci   uint32_t flags = 0;
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci   switch (templ->usage) {
454bf215546Sopenharmony_ci   case PIPE_USAGE_STAGING:
455bf215546Sopenharmony_ci      flags |= BO_ALLOC_SMEM | BO_ALLOC_COHERENT;
456bf215546Sopenharmony_ci      break;
457bf215546Sopenharmony_ci   case PIPE_USAGE_STREAM:
458bf215546Sopenharmony_ci      flags |= BO_ALLOC_SMEM;
459bf215546Sopenharmony_ci      break;
460bf215546Sopenharmony_ci   case PIPE_USAGE_DYNAMIC:
461bf215546Sopenharmony_ci   case PIPE_USAGE_DEFAULT:
462bf215546Sopenharmony_ci   case PIPE_USAGE_IMMUTABLE:
463bf215546Sopenharmony_ci      /* Use LMEM for these if possible */
464bf215546Sopenharmony_ci      break;
465bf215546Sopenharmony_ci   }
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_SCANOUT)
468bf215546Sopenharmony_ci      flags |= BO_ALLOC_SCANOUT;
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci   if (templ->flags & (PIPE_RESOURCE_FLAG_MAP_COHERENT |
471bf215546Sopenharmony_ci                       PIPE_RESOURCE_FLAG_MAP_PERSISTENT))
472bf215546Sopenharmony_ci      flags |= BO_ALLOC_SMEM;
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci   if (screen->devinfo.verx10 >= 125 && isl_aux_usage_has_ccs(aux_usage)) {
475bf215546Sopenharmony_ci      assert((flags & BO_ALLOC_SMEM) == 0);
476bf215546Sopenharmony_ci      flags |= BO_ALLOC_LMEM;
477bf215546Sopenharmony_ci   }
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci   if ((templ->bind & PIPE_BIND_SHARED) ||
480bf215546Sopenharmony_ci       util_format_get_num_planes(templ->format) > 1)
481bf215546Sopenharmony_ci      flags |= BO_ALLOC_NO_SUBALLOC;
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_ci   return flags;
484bf215546Sopenharmony_ci}
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_cistatic void
487bf215546Sopenharmony_ciiris_resource_destroy(struct pipe_screen *screen,
488bf215546Sopenharmony_ci                      struct pipe_resource *p_res)
489bf215546Sopenharmony_ci{
490bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) p_res;
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci   if (p_res->target == PIPE_BUFFER)
493bf215546Sopenharmony_ci      util_range_destroy(&res->valid_buffer_range);
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci   iris_resource_disable_aux(res);
496bf215546Sopenharmony_ci
497bf215546Sopenharmony_ci   threaded_resource_deinit(p_res);
498bf215546Sopenharmony_ci   iris_bo_unreference(res->bo);
499bf215546Sopenharmony_ci   iris_pscreen_unref(res->orig_screen);
500bf215546Sopenharmony_ci
501bf215546Sopenharmony_ci   free(res);
502bf215546Sopenharmony_ci}
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_cistatic struct iris_resource *
505bf215546Sopenharmony_ciiris_alloc_resource(struct pipe_screen *pscreen,
506bf215546Sopenharmony_ci                    const struct pipe_resource *templ)
507bf215546Sopenharmony_ci{
508bf215546Sopenharmony_ci   struct iris_resource *res = calloc(1, sizeof(struct iris_resource));
509bf215546Sopenharmony_ci   if (!res)
510bf215546Sopenharmony_ci      return NULL;
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   res->base.b = *templ;
513bf215546Sopenharmony_ci   res->base.b.screen = pscreen;
514bf215546Sopenharmony_ci   res->orig_screen = iris_pscreen_ref(pscreen);
515bf215546Sopenharmony_ci   pipe_reference_init(&res->base.b.reference, 1);
516bf215546Sopenharmony_ci   threaded_resource_init(&res->base.b, false);
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_ci   if (templ->target == PIPE_BUFFER)
519bf215546Sopenharmony_ci      util_range_init(&res->valid_buffer_range);
520bf215546Sopenharmony_ci
521bf215546Sopenharmony_ci   return res;
522bf215546Sopenharmony_ci}
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ciunsigned
525bf215546Sopenharmony_ciiris_get_num_logical_layers(const struct iris_resource *res, unsigned level)
526bf215546Sopenharmony_ci{
527bf215546Sopenharmony_ci   if (res->surf.dim == ISL_SURF_DIM_3D)
528bf215546Sopenharmony_ci      return u_minify(res->surf.logical_level0_px.depth, level);
529bf215546Sopenharmony_ci   else
530bf215546Sopenharmony_ci      return res->surf.logical_level0_px.array_len;
531bf215546Sopenharmony_ci}
532bf215546Sopenharmony_ci
533bf215546Sopenharmony_cistatic enum isl_aux_state **
534bf215546Sopenharmony_cicreate_aux_state_map(struct iris_resource *res, enum isl_aux_state initial)
535bf215546Sopenharmony_ci{
536bf215546Sopenharmony_ci   assert(res->aux.state == NULL);
537bf215546Sopenharmony_ci
538bf215546Sopenharmony_ci   uint32_t total_slices = 0;
539bf215546Sopenharmony_ci   for (uint32_t level = 0; level < res->surf.levels; level++)
540bf215546Sopenharmony_ci      total_slices += iris_get_num_logical_layers(res, level);
541bf215546Sopenharmony_ci
542bf215546Sopenharmony_ci   const size_t per_level_array_size =
543bf215546Sopenharmony_ci      res->surf.levels * sizeof(enum isl_aux_state *);
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_ci   /* We're going to allocate a single chunk of data for both the per-level
546bf215546Sopenharmony_ci    * reference array and the arrays of aux_state.  This makes cleanup
547bf215546Sopenharmony_ci    * significantly easier.
548bf215546Sopenharmony_ci    */
549bf215546Sopenharmony_ci   const size_t total_size =
550bf215546Sopenharmony_ci      per_level_array_size + total_slices * sizeof(enum isl_aux_state);
551bf215546Sopenharmony_ci
552bf215546Sopenharmony_ci   void *data = malloc(total_size);
553bf215546Sopenharmony_ci   if (!data)
554bf215546Sopenharmony_ci      return NULL;
555bf215546Sopenharmony_ci
556bf215546Sopenharmony_ci   enum isl_aux_state **per_level_arr = data;
557bf215546Sopenharmony_ci   enum isl_aux_state *s = data + per_level_array_size;
558bf215546Sopenharmony_ci   for (uint32_t level = 0; level < res->surf.levels; level++) {
559bf215546Sopenharmony_ci      per_level_arr[level] = s;
560bf215546Sopenharmony_ci      const unsigned level_layers = iris_get_num_logical_layers(res, level);
561bf215546Sopenharmony_ci      for (uint32_t a = 0; a < level_layers; a++)
562bf215546Sopenharmony_ci         *(s++) = initial;
563bf215546Sopenharmony_ci   }
564bf215546Sopenharmony_ci   assert((void *)s == data + total_size);
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci   return per_level_arr;
567bf215546Sopenharmony_ci}
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_cistatic unsigned
570bf215546Sopenharmony_ciiris_get_aux_clear_color_state_size(struct iris_screen *screen,
571bf215546Sopenharmony_ci                                    struct iris_resource *res)
572bf215546Sopenharmony_ci{
573bf215546Sopenharmony_ci   if (!isl_aux_usage_has_fast_clears(res->aux.usage))
574bf215546Sopenharmony_ci      return 0;
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci   assert(!isl_surf_usage_is_stencil(res->surf.usage));
577bf215546Sopenharmony_ci
578bf215546Sopenharmony_ci   /* Depth packets can't specify indirect clear values. The only time depth
579bf215546Sopenharmony_ci    * buffers can use indirect clear values is when they're accessed by the
580bf215546Sopenharmony_ci    * sampler via render surface state objects.
581bf215546Sopenharmony_ci    */
582bf215546Sopenharmony_ci   if (isl_surf_usage_is_depth(res->surf.usage) &&
583bf215546Sopenharmony_ci       !iris_sample_with_depth_aux(&screen->devinfo, res))
584bf215546Sopenharmony_ci      return 0;
585bf215546Sopenharmony_ci
586bf215546Sopenharmony_ci   return screen->isl_dev.ss.clear_color_state_size;
587bf215546Sopenharmony_ci}
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_cistatic void
590bf215546Sopenharmony_cimap_aux_addresses(struct iris_screen *screen, struct iris_resource *res,
591bf215546Sopenharmony_ci                  enum pipe_format pfmt, unsigned plane)
592bf215546Sopenharmony_ci{
593bf215546Sopenharmony_ci   void *aux_map_ctx = iris_bufmgr_get_aux_map_context(screen->bufmgr);
594bf215546Sopenharmony_ci   if (!aux_map_ctx)
595bf215546Sopenharmony_ci      return;
596bf215546Sopenharmony_ci
597bf215546Sopenharmony_ci   if (isl_aux_usage_has_ccs(res->aux.usage)) {
598bf215546Sopenharmony_ci      const unsigned aux_offset = res->aux.extra_aux.surf.size_B > 0 ?
599bf215546Sopenharmony_ci         res->aux.extra_aux.offset : res->aux.offset;
600bf215546Sopenharmony_ci      const enum isl_format format =
601bf215546Sopenharmony_ci         iris_format_for_usage(&screen->devinfo, pfmt, res->surf.usage).fmt;
602bf215546Sopenharmony_ci      const uint64_t format_bits =
603bf215546Sopenharmony_ci         intel_aux_map_format_bits(res->surf.tiling, format, plane);
604bf215546Sopenharmony_ci      intel_aux_map_add_mapping(aux_map_ctx, res->bo->address + res->offset,
605bf215546Sopenharmony_ci                                res->aux.bo->address + aux_offset,
606bf215546Sopenharmony_ci                                res->surf.size_B, format_bits);
607bf215546Sopenharmony_ci      res->bo->aux_map_address = res->aux.bo->address;
608bf215546Sopenharmony_ci   }
609bf215546Sopenharmony_ci}
610bf215546Sopenharmony_ci
611bf215546Sopenharmony_cistatic bool
612bf215546Sopenharmony_ciwant_ccs_e_for_format(const struct intel_device_info *devinfo,
613bf215546Sopenharmony_ci                      enum isl_format format)
614bf215546Sopenharmony_ci{
615bf215546Sopenharmony_ci   if (!isl_format_supports_ccs_e(devinfo, format))
616bf215546Sopenharmony_ci      return false;
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci   const struct isl_format_layout *fmtl = isl_format_get_layout(format);
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_ci   /* Prior to TGL, CCS_E seems to significantly hurt performance with 32-bit
621bf215546Sopenharmony_ci    * floating point formats.  For example, Paraview's "Wavelet Volume" case
622bf215546Sopenharmony_ci    * uses both R32_FLOAT and R32G32B32A32_FLOAT, and enabling CCS_E for those
623bf215546Sopenharmony_ci    * formats causes a 62% FPS drop.
624bf215546Sopenharmony_ci    *
625bf215546Sopenharmony_ci    * However, many benchmarks seem to use 16-bit float with no issues.
626bf215546Sopenharmony_ci    */
627bf215546Sopenharmony_ci   if (devinfo->ver <= 11 &&
628bf215546Sopenharmony_ci       fmtl->channels.r.bits == 32 && fmtl->channels.r.type == ISL_SFLOAT)
629bf215546Sopenharmony_ci      return false;
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci   return true;
632bf215546Sopenharmony_ci}
633bf215546Sopenharmony_ci
634bf215546Sopenharmony_cistatic enum isl_surf_dim
635bf215546Sopenharmony_citarget_to_isl_surf_dim(enum pipe_texture_target target)
636bf215546Sopenharmony_ci{
637bf215546Sopenharmony_ci   switch (target) {
638bf215546Sopenharmony_ci   case PIPE_BUFFER:
639bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D:
640bf215546Sopenharmony_ci   case PIPE_TEXTURE_1D_ARRAY:
641bf215546Sopenharmony_ci      return ISL_SURF_DIM_1D;
642bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D:
643bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE:
644bf215546Sopenharmony_ci   case PIPE_TEXTURE_RECT:
645bf215546Sopenharmony_ci   case PIPE_TEXTURE_2D_ARRAY:
646bf215546Sopenharmony_ci   case PIPE_TEXTURE_CUBE_ARRAY:
647bf215546Sopenharmony_ci      return ISL_SURF_DIM_2D;
648bf215546Sopenharmony_ci   case PIPE_TEXTURE_3D:
649bf215546Sopenharmony_ci      return ISL_SURF_DIM_3D;
650bf215546Sopenharmony_ci   case PIPE_MAX_TEXTURE_TYPES:
651bf215546Sopenharmony_ci      break;
652bf215546Sopenharmony_ci   }
653bf215546Sopenharmony_ci   unreachable("invalid texture type");
654bf215546Sopenharmony_ci}
655bf215546Sopenharmony_ci
656bf215546Sopenharmony_cistatic bool
657bf215546Sopenharmony_ciiris_resource_configure_main(const struct iris_screen *screen,
658bf215546Sopenharmony_ci                             struct iris_resource *res,
659bf215546Sopenharmony_ci                             const struct pipe_resource *templ,
660bf215546Sopenharmony_ci                             uint64_t modifier, uint32_t row_pitch_B)
661bf215546Sopenharmony_ci{
662bf215546Sopenharmony_ci   res->mod_info = isl_drm_modifier_get_info(modifier);
663bf215546Sopenharmony_ci
664bf215546Sopenharmony_ci   if (modifier != DRM_FORMAT_MOD_INVALID && res->mod_info == NULL)
665bf215546Sopenharmony_ci      return false;
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_ci   isl_tiling_flags_t tiling_flags = 0;
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci   if (res->mod_info != NULL) {
670bf215546Sopenharmony_ci      tiling_flags = 1 << res->mod_info->tiling;
671bf215546Sopenharmony_ci   } else if (templ->usage == PIPE_USAGE_STAGING ||
672bf215546Sopenharmony_ci              templ->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR)) {
673bf215546Sopenharmony_ci      tiling_flags = ISL_TILING_LINEAR_BIT;
674bf215546Sopenharmony_ci   } else if (!screen->devinfo.has_tiling_uapi &&
675bf215546Sopenharmony_ci              (templ->bind & (PIPE_BIND_SCANOUT | PIPE_BIND_SHARED))) {
676bf215546Sopenharmony_ci      tiling_flags = ISL_TILING_LINEAR_BIT;
677bf215546Sopenharmony_ci   } else if (templ->bind & PIPE_BIND_SCANOUT) {
678bf215546Sopenharmony_ci      tiling_flags = ISL_TILING_X_BIT;
679bf215546Sopenharmony_ci   } else {
680bf215546Sopenharmony_ci      tiling_flags = ISL_TILING_ANY_MASK;
681bf215546Sopenharmony_ci   }
682bf215546Sopenharmony_ci
683bf215546Sopenharmony_ci   isl_surf_usage_flags_t usage = 0;
684bf215546Sopenharmony_ci
685bf215546Sopenharmony_ci   if (res->mod_info && res->mod_info->aux_usage == ISL_AUX_USAGE_NONE)
686bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_DISABLE_AUX_BIT;
687bf215546Sopenharmony_ci
688bf215546Sopenharmony_ci   if (templ->usage == PIPE_USAGE_STAGING)
689bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_STAGING_BIT;
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_RENDER_TARGET)
692bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
693bf215546Sopenharmony_ci
694bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_SAMPLER_VIEW)
695bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_TEXTURE_BIT;
696bf215546Sopenharmony_ci
697bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_SHADER_IMAGE)
698bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_STORAGE_BIT;
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_SCANOUT)
701bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_DISPLAY_BIT;
702bf215546Sopenharmony_ci
703bf215546Sopenharmony_ci   if (templ->target == PIPE_TEXTURE_CUBE ||
704bf215546Sopenharmony_ci       templ->target == PIPE_TEXTURE_CUBE_ARRAY) {
705bf215546Sopenharmony_ci      usage |= ISL_SURF_USAGE_CUBE_BIT;
706bf215546Sopenharmony_ci   }
707bf215546Sopenharmony_ci
708bf215546Sopenharmony_ci   if (templ->usage != PIPE_USAGE_STAGING &&
709bf215546Sopenharmony_ci       util_format_is_depth_or_stencil(templ->format)) {
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_ci      /* Should be handled by u_transfer_helper */
712bf215546Sopenharmony_ci      assert(!util_format_is_depth_and_stencil(templ->format));
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci      usage |= templ->format == PIPE_FORMAT_S8_UINT ?
715bf215546Sopenharmony_ci               ISL_SURF_USAGE_STENCIL_BIT : ISL_SURF_USAGE_DEPTH_BIT;
716bf215546Sopenharmony_ci   }
717bf215546Sopenharmony_ci
718bf215546Sopenharmony_ci   const enum isl_format format =
719bf215546Sopenharmony_ci      iris_format_for_usage(&screen->devinfo, templ->format, usage).fmt;
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_ci   const struct isl_surf_init_info init_info = {
722bf215546Sopenharmony_ci      .dim = target_to_isl_surf_dim(templ->target),
723bf215546Sopenharmony_ci      .format = format,
724bf215546Sopenharmony_ci      .width = templ->width0,
725bf215546Sopenharmony_ci      .height = templ->height0,
726bf215546Sopenharmony_ci      .depth = templ->depth0,
727bf215546Sopenharmony_ci      .levels = templ->last_level + 1,
728bf215546Sopenharmony_ci      .array_len = templ->array_size,
729bf215546Sopenharmony_ci      .samples = MAX2(templ->nr_samples, 1),
730bf215546Sopenharmony_ci      .min_alignment_B = 0,
731bf215546Sopenharmony_ci      .row_pitch_B = row_pitch_B,
732bf215546Sopenharmony_ci      .usage = usage,
733bf215546Sopenharmony_ci      .tiling_flags = tiling_flags
734bf215546Sopenharmony_ci   };
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci   if (!isl_surf_init_s(&screen->isl_dev, &res->surf, &init_info))
737bf215546Sopenharmony_ci      return false;
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci   res->internal_format = templ->format;
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci   return true;
742bf215546Sopenharmony_ci}
743bf215546Sopenharmony_ci
744bf215546Sopenharmony_cistatic bool
745bf215546Sopenharmony_ciiris_get_ccs_surf_or_support(const struct isl_device *dev,
746bf215546Sopenharmony_ci                             const struct isl_surf *surf,
747bf215546Sopenharmony_ci                             struct isl_surf *aux_surf,
748bf215546Sopenharmony_ci                             struct isl_surf *extra_aux_surf)
749bf215546Sopenharmony_ci{
750bf215546Sopenharmony_ci   assert(extra_aux_surf->size_B == 0);
751bf215546Sopenharmony_ci
752bf215546Sopenharmony_ci   struct isl_surf *ccs_surf;
753bf215546Sopenharmony_ci   const struct isl_surf *hiz_or_mcs_surf;
754bf215546Sopenharmony_ci   if (aux_surf->size_B > 0) {
755bf215546Sopenharmony_ci      assert(aux_surf->usage & (ISL_SURF_USAGE_HIZ_BIT |
756bf215546Sopenharmony_ci                                ISL_SURF_USAGE_MCS_BIT));
757bf215546Sopenharmony_ci      hiz_or_mcs_surf = aux_surf;
758bf215546Sopenharmony_ci      ccs_surf = extra_aux_surf;
759bf215546Sopenharmony_ci   } else {
760bf215546Sopenharmony_ci      hiz_or_mcs_surf = NULL;
761bf215546Sopenharmony_ci      ccs_surf = aux_surf;
762bf215546Sopenharmony_ci   }
763bf215546Sopenharmony_ci
764bf215546Sopenharmony_ci   if (dev->info->verx10 >= 125) {
765bf215546Sopenharmony_ci      /* CCS doesn't require VMA on XeHP. So, instead of creating a separate
766bf215546Sopenharmony_ci       * surface, we can just return whether CCS is supported for the given
767bf215546Sopenharmony_ci       * input surfaces.
768bf215546Sopenharmony_ci       */
769bf215546Sopenharmony_ci      return isl_surf_supports_ccs(dev, surf, hiz_or_mcs_surf);
770bf215546Sopenharmony_ci   } else  {
771bf215546Sopenharmony_ci      return isl_surf_get_ccs_surf(dev, surf, hiz_or_mcs_surf, ccs_surf, 0);
772bf215546Sopenharmony_ci   }
773bf215546Sopenharmony_ci}
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci/**
776bf215546Sopenharmony_ci * Configure aux for the resource, but don't allocate it. For images which
777bf215546Sopenharmony_ci * might be shared with modifiers, we must allocate the image and aux data in
778bf215546Sopenharmony_ci * a single bo.
779bf215546Sopenharmony_ci *
780bf215546Sopenharmony_ci * Returns false on unexpected error (e.g. allocation failed, or invalid
781bf215546Sopenharmony_ci * configuration result).
782bf215546Sopenharmony_ci */
783bf215546Sopenharmony_cistatic bool
784bf215546Sopenharmony_ciiris_resource_configure_aux(struct iris_screen *screen,
785bf215546Sopenharmony_ci                            struct iris_resource *res, bool imported)
786bf215546Sopenharmony_ci{
787bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &screen->devinfo;
788bf215546Sopenharmony_ci
789bf215546Sopenharmony_ci   const bool has_mcs =
790bf215546Sopenharmony_ci      isl_surf_get_mcs_surf(&screen->isl_dev, &res->surf, &res->aux.surf);
791bf215546Sopenharmony_ci
792bf215546Sopenharmony_ci   const bool has_hiz = !INTEL_DEBUG(DEBUG_NO_HIZ) &&
793bf215546Sopenharmony_ci      isl_surf_get_hiz_surf(&screen->isl_dev, &res->surf, &res->aux.surf);
794bf215546Sopenharmony_ci
795bf215546Sopenharmony_ci   const bool has_ccs = !INTEL_DEBUG(DEBUG_NO_CCS) &&
796bf215546Sopenharmony_ci      iris_get_ccs_surf_or_support(&screen->isl_dev, &res->surf,
797bf215546Sopenharmony_ci                                   &res->aux.surf, &res->aux.extra_aux.surf);
798bf215546Sopenharmony_ci
799bf215546Sopenharmony_ci   if (has_mcs) {
800bf215546Sopenharmony_ci      assert(!res->mod_info);
801bf215546Sopenharmony_ci      assert(!has_hiz);
802bf215546Sopenharmony_ci      if (has_ccs) {
803bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_MCS_CCS;
804bf215546Sopenharmony_ci      } else {
805bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_MCS;
806bf215546Sopenharmony_ci      }
807bf215546Sopenharmony_ci   } else if (has_hiz) {
808bf215546Sopenharmony_ci      assert(!res->mod_info);
809bf215546Sopenharmony_ci      assert(!has_mcs);
810bf215546Sopenharmony_ci      if (!has_ccs) {
811bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_HIZ;
812bf215546Sopenharmony_ci      } else if (res->surf.samples == 1 &&
813bf215546Sopenharmony_ci                 (res->surf.usage & ISL_SURF_USAGE_TEXTURE_BIT)) {
814bf215546Sopenharmony_ci         /* If this resource is single-sampled and will be used as a texture,
815bf215546Sopenharmony_ci          * put the HiZ surface in write-through mode so that we can sample
816bf215546Sopenharmony_ci          * from it.
817bf215546Sopenharmony_ci          */
818bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_HIZ_CCS_WT;
819bf215546Sopenharmony_ci      } else {
820bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_HIZ_CCS;
821bf215546Sopenharmony_ci      }
822bf215546Sopenharmony_ci   } else if (has_ccs) {
823bf215546Sopenharmony_ci      if (res->mod_info) {
824bf215546Sopenharmony_ci         res->aux.usage = res->mod_info->aux_usage;
825bf215546Sopenharmony_ci      } else if (isl_surf_usage_is_stencil(res->surf.usage)) {
826bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_STC_CCS;
827bf215546Sopenharmony_ci      } else if (want_ccs_e_for_format(devinfo, res->surf.format)) {
828bf215546Sopenharmony_ci         res->aux.usage = devinfo->ver < 12 ?
829bf215546Sopenharmony_ci            ISL_AUX_USAGE_CCS_E : ISL_AUX_USAGE_GFX12_CCS_E;
830bf215546Sopenharmony_ci      } else {
831bf215546Sopenharmony_ci         assert(isl_format_supports_ccs_d(devinfo, res->surf.format));
832bf215546Sopenharmony_ci         res->aux.usage = ISL_AUX_USAGE_CCS_D;
833bf215546Sopenharmony_ci      }
834bf215546Sopenharmony_ci   }
835bf215546Sopenharmony_ci
836bf215546Sopenharmony_ci   enum isl_aux_state initial_state;
837bf215546Sopenharmony_ci   switch (res->aux.usage) {
838bf215546Sopenharmony_ci   case ISL_AUX_USAGE_NONE:
839bf215546Sopenharmony_ci      /* Having no aux buffer is only okay if there's no modifier with aux. */
840bf215546Sopenharmony_ci      return !res->mod_info || res->mod_info->aux_usage == ISL_AUX_USAGE_NONE;
841bf215546Sopenharmony_ci   case ISL_AUX_USAGE_HIZ:
842bf215546Sopenharmony_ci   case ISL_AUX_USAGE_HIZ_CCS:
843bf215546Sopenharmony_ci   case ISL_AUX_USAGE_HIZ_CCS_WT:
844bf215546Sopenharmony_ci      initial_state = ISL_AUX_STATE_AUX_INVALID;
845bf215546Sopenharmony_ci      break;
846bf215546Sopenharmony_ci   case ISL_AUX_USAGE_MCS:
847bf215546Sopenharmony_ci   case ISL_AUX_USAGE_MCS_CCS:
848bf215546Sopenharmony_ci      /* The Ivybridge PRM, Vol 2 Part 1 p326 says:
849bf215546Sopenharmony_ci       *
850bf215546Sopenharmony_ci       *    "When MCS buffer is enabled and bound to MSRT, it is required
851bf215546Sopenharmony_ci       *     that it is cleared prior to any rendering."
852bf215546Sopenharmony_ci       *
853bf215546Sopenharmony_ci       * Since we only use the MCS buffer for rendering, we just clear it
854bf215546Sopenharmony_ci       * immediately on allocation.  The clear value for MCS buffers is all
855bf215546Sopenharmony_ci       * 1's, so we simply memset it to 0xff.
856bf215546Sopenharmony_ci       */
857bf215546Sopenharmony_ci      initial_state = ISL_AUX_STATE_CLEAR;
858bf215546Sopenharmony_ci      break;
859bf215546Sopenharmony_ci   case ISL_AUX_USAGE_CCS_D:
860bf215546Sopenharmony_ci   case ISL_AUX_USAGE_CCS_E:
861bf215546Sopenharmony_ci   case ISL_AUX_USAGE_GFX12_CCS_E:
862bf215546Sopenharmony_ci   case ISL_AUX_USAGE_STC_CCS:
863bf215546Sopenharmony_ci   case ISL_AUX_USAGE_MC:
864bf215546Sopenharmony_ci      if (imported) {
865bf215546Sopenharmony_ci         assert(res->aux.usage != ISL_AUX_USAGE_STC_CCS);
866bf215546Sopenharmony_ci         initial_state =
867bf215546Sopenharmony_ci            isl_drm_modifier_get_default_aux_state(res->mod_info->modifier);
868bf215546Sopenharmony_ci      } else if (devinfo->verx10 >= 125) {
869bf215546Sopenharmony_ci         assert(res->aux.surf.size_B == 0);
870bf215546Sopenharmony_ci         /* From Bspec 47709, "MCS/CCS Buffers for Render Target(s)":
871bf215546Sopenharmony_ci          *
872bf215546Sopenharmony_ci          *    "CCS surface does not require initialization. Illegal CCS
873bf215546Sopenharmony_ci          *     [values] are treated as uncompressed memory."
874bf215546Sopenharmony_ci          *
875bf215546Sopenharmony_ci          * Even if we wanted to, we can't initialize the CCS via CPU map. So,
876bf215546Sopenharmony_ci          * we choose an aux state which describes the current state and helps
877bf215546Sopenharmony_ci          * avoid ambiguating (something not currently supported for STC_CCS).
878bf215546Sopenharmony_ci          */
879bf215546Sopenharmony_ci         assert(isl_aux_usage_has_compression(res->aux.usage));
880bf215546Sopenharmony_ci         initial_state = isl_aux_usage_has_fast_clears(res->aux.usage) ?
881bf215546Sopenharmony_ci                         ISL_AUX_STATE_COMPRESSED_CLEAR :
882bf215546Sopenharmony_ci                         ISL_AUX_STATE_COMPRESSED_NO_CLEAR;
883bf215546Sopenharmony_ci      } else {
884bf215546Sopenharmony_ci         assert(res->aux.surf.size_B > 0);
885bf215546Sopenharmony_ci         /* When CCS is used, we need to ensure that it starts off in a valid
886bf215546Sopenharmony_ci          * state. From the Sky Lake PRM, "MCS Buffer for Render Target(s)":
887bf215546Sopenharmony_ci          *
888bf215546Sopenharmony_ci          *    "If Software wants to enable Color Compression without Fast
889bf215546Sopenharmony_ci          *     clear, Software needs to initialize MCS with zeros."
890bf215546Sopenharmony_ci          *
891bf215546Sopenharmony_ci          * A CCS surface initialized to zero is in the pass-through state.
892bf215546Sopenharmony_ci          * This state can avoid the need to ambiguate in some cases. We'll
893bf215546Sopenharmony_ci          * map and zero the CCS later on in iris_resource_init_aux_buf.
894bf215546Sopenharmony_ci          */
895bf215546Sopenharmony_ci         initial_state = ISL_AUX_STATE_PASS_THROUGH;
896bf215546Sopenharmony_ci      }
897bf215546Sopenharmony_ci      break;
898bf215546Sopenharmony_ci   default:
899bf215546Sopenharmony_ci      unreachable("Unsupported aux mode");
900bf215546Sopenharmony_ci   }
901bf215546Sopenharmony_ci
902bf215546Sopenharmony_ci   /* Create the aux_state for the auxiliary buffer. */
903bf215546Sopenharmony_ci   res->aux.state = create_aux_state_map(res, initial_state);
904bf215546Sopenharmony_ci   if (!res->aux.state)
905bf215546Sopenharmony_ci      return false;
906bf215546Sopenharmony_ci
907bf215546Sopenharmony_ci   return true;
908bf215546Sopenharmony_ci}
909bf215546Sopenharmony_ci
910bf215546Sopenharmony_ci/**
911bf215546Sopenharmony_ci * Initialize the aux buffer contents.
912bf215546Sopenharmony_ci *
913bf215546Sopenharmony_ci * Returns false on unexpected error (e.g. mapping a BO failed).
914bf215546Sopenharmony_ci */
915bf215546Sopenharmony_cistatic bool
916bf215546Sopenharmony_ciiris_resource_init_aux_buf(struct iris_screen *screen,
917bf215546Sopenharmony_ci                           struct iris_resource *res)
918bf215546Sopenharmony_ci{
919bf215546Sopenharmony_ci   void *map = NULL;
920bf215546Sopenharmony_ci
921bf215546Sopenharmony_ci   if (iris_resource_get_aux_state(res, 0, 0) != ISL_AUX_STATE_AUX_INVALID &&
922bf215546Sopenharmony_ci       res->aux.surf.size_B > 0) {
923bf215546Sopenharmony_ci      if (!map)
924bf215546Sopenharmony_ci         map = iris_bo_map(NULL, res->bo, MAP_WRITE | MAP_RAW);
925bf215546Sopenharmony_ci      if (!map)
926bf215546Sopenharmony_ci         return false;
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci      /* See iris_resource_configure_aux for the memset_value rationale. */
929bf215546Sopenharmony_ci      uint8_t memset_value = isl_aux_usage_has_mcs(res->aux.usage) ? 0xFF : 0;
930bf215546Sopenharmony_ci      memset((char*)map + res->aux.offset, memset_value,
931bf215546Sopenharmony_ci             res->aux.surf.size_B);
932bf215546Sopenharmony_ci   }
933bf215546Sopenharmony_ci
934bf215546Sopenharmony_ci   if (res->aux.extra_aux.surf.size_B > 0) {
935bf215546Sopenharmony_ci      if (!map)
936bf215546Sopenharmony_ci         map = iris_bo_map(NULL, res->bo, MAP_WRITE | MAP_RAW);
937bf215546Sopenharmony_ci      if (!map)
938bf215546Sopenharmony_ci         return false;
939bf215546Sopenharmony_ci
940bf215546Sopenharmony_ci      memset((char*)map + res->aux.extra_aux.offset,
941bf215546Sopenharmony_ci             0, res->aux.extra_aux.surf.size_B);
942bf215546Sopenharmony_ci   }
943bf215546Sopenharmony_ci
944bf215546Sopenharmony_ci   unsigned clear_color_size = iris_get_aux_clear_color_state_size(screen, res);
945bf215546Sopenharmony_ci   if (clear_color_size > 0) {
946bf215546Sopenharmony_ci      if (iris_bo_mmap_mode(res->bo) != IRIS_MMAP_NONE) {
947bf215546Sopenharmony_ci         if (!map)
948bf215546Sopenharmony_ci            map = iris_bo_map(NULL, res->bo, MAP_WRITE | MAP_RAW);
949bf215546Sopenharmony_ci         if (!map)
950bf215546Sopenharmony_ci            return false;
951bf215546Sopenharmony_ci
952bf215546Sopenharmony_ci         /* Zero the indirect clear color to match ::fast_clear_color. */
953bf215546Sopenharmony_ci         memset((char *)map + res->aux.clear_color_offset, 0, clear_color_size);
954bf215546Sopenharmony_ci      } else {
955bf215546Sopenharmony_ci         res->aux.clear_color_unknown = true;
956bf215546Sopenharmony_ci      }
957bf215546Sopenharmony_ci   }
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_ci   if (map)
960bf215546Sopenharmony_ci      iris_bo_unmap(res->bo);
961bf215546Sopenharmony_ci
962bf215546Sopenharmony_ci   if (res->aux.surf.size_B > 0) {
963bf215546Sopenharmony_ci      res->aux.bo = res->bo;
964bf215546Sopenharmony_ci      iris_bo_reference(res->aux.bo);
965bf215546Sopenharmony_ci      map_aux_addresses(screen, res, res->internal_format, 0);
966bf215546Sopenharmony_ci   }
967bf215546Sopenharmony_ci
968bf215546Sopenharmony_ci   if (clear_color_size > 0) {
969bf215546Sopenharmony_ci      res->aux.clear_color_bo = res->bo;
970bf215546Sopenharmony_ci      iris_bo_reference(res->aux.clear_color_bo);
971bf215546Sopenharmony_ci   }
972bf215546Sopenharmony_ci
973bf215546Sopenharmony_ci   return true;
974bf215546Sopenharmony_ci}
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_cistatic void
977bf215546Sopenharmony_ciimport_aux_info(struct iris_resource *res,
978bf215546Sopenharmony_ci                const struct iris_resource *aux_res)
979bf215546Sopenharmony_ci{
980bf215546Sopenharmony_ci   assert(aux_res->aux.surf.row_pitch_B && aux_res->aux.offset);
981bf215546Sopenharmony_ci   assert(res->bo == aux_res->aux.bo);
982bf215546Sopenharmony_ci   assert(res->aux.surf.row_pitch_B == aux_res->aux.surf.row_pitch_B);
983bf215546Sopenharmony_ci   assert(res->bo->size >= aux_res->aux.offset + res->aux.surf.size_B);
984bf215546Sopenharmony_ci
985bf215546Sopenharmony_ci   iris_bo_reference(aux_res->aux.bo);
986bf215546Sopenharmony_ci   res->aux.bo = aux_res->aux.bo;
987bf215546Sopenharmony_ci   res->aux.offset = aux_res->aux.offset;
988bf215546Sopenharmony_ci}
989bf215546Sopenharmony_ci
990bf215546Sopenharmony_cistatic void
991bf215546Sopenharmony_ciiris_resource_finish_aux_import(struct pipe_screen *pscreen,
992bf215546Sopenharmony_ci                                struct iris_resource *res)
993bf215546Sopenharmony_ci{
994bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
995bf215546Sopenharmony_ci
996bf215546Sopenharmony_ci   /* Create an array of resources. Combining main and aux planes is easier
997bf215546Sopenharmony_ci    * with indexing as opposed to scanning the linked list.
998bf215546Sopenharmony_ci    */
999bf215546Sopenharmony_ci   struct iris_resource *r[4] = { NULL, };
1000bf215546Sopenharmony_ci   unsigned num_planes = 0;
1001bf215546Sopenharmony_ci   unsigned num_main_planes = 0;
1002bf215546Sopenharmony_ci   for (struct pipe_resource *p_res = &res->base.b; p_res; p_res = p_res->next) {
1003bf215546Sopenharmony_ci      r[num_planes] = (struct iris_resource *)p_res;
1004bf215546Sopenharmony_ci      num_main_planes += r[num_planes++]->bo != NULL;
1005bf215546Sopenharmony_ci   }
1006bf215546Sopenharmony_ci
1007bf215546Sopenharmony_ci   /* Combine main and aux plane information. */
1008bf215546Sopenharmony_ci   switch (res->mod_info->modifier) {
1009bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_CCS:
1010bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1011bf215546Sopenharmony_ci      assert(num_main_planes == 1 && num_planes == 2);
1012bf215546Sopenharmony_ci      import_aux_info(r[0], r[1]);
1013bf215546Sopenharmony_ci      map_aux_addresses(screen, r[0], res->external_format, 0);
1014bf215546Sopenharmony_ci
1015bf215546Sopenharmony_ci      /* Add on a clear color BO.
1016bf215546Sopenharmony_ci       *
1017bf215546Sopenharmony_ci       * Also add some padding to make sure the fast clear color state buffer
1018bf215546Sopenharmony_ci       * starts at a 4K alignment to avoid some unknown issues.  See the
1019bf215546Sopenharmony_ci       * matching comment in iris_resource_create_with_modifiers().
1020bf215546Sopenharmony_ci       */
1021bf215546Sopenharmony_ci      if (iris_get_aux_clear_color_state_size(screen, res) > 0) {
1022bf215546Sopenharmony_ci         res->aux.clear_color_bo =
1023bf215546Sopenharmony_ci            iris_bo_alloc(screen->bufmgr, "clear color_buffer",
1024bf215546Sopenharmony_ci                          iris_get_aux_clear_color_state_size(screen, res),
1025bf215546Sopenharmony_ci                          4096, IRIS_MEMZONE_OTHER, BO_ALLOC_ZEROED);
1026bf215546Sopenharmony_ci      }
1027bf215546Sopenharmony_ci      break;
1028bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS:
1029bf215546Sopenharmony_ci      assert(num_main_planes == 1);
1030bf215546Sopenharmony_ci      assert(num_planes == 1);
1031bf215546Sopenharmony_ci      res->aux.clear_color_bo =
1032bf215546Sopenharmony_ci         iris_bo_alloc(screen->bufmgr, "clear color_buffer",
1033bf215546Sopenharmony_ci                       iris_get_aux_clear_color_state_size(screen, res),
1034bf215546Sopenharmony_ci                       4096, IRIS_MEMZONE_OTHER, BO_ALLOC_ZEROED);
1035bf215546Sopenharmony_ci      break;
1036bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1037bf215546Sopenharmony_ci      assert(num_main_planes == 1 && num_planes == 3);
1038bf215546Sopenharmony_ci      import_aux_info(r[0], r[1]);
1039bf215546Sopenharmony_ci      map_aux_addresses(screen, r[0], res->external_format, 0);
1040bf215546Sopenharmony_ci
1041bf215546Sopenharmony_ci      /* Import the clear color BO. */
1042bf215546Sopenharmony_ci      iris_bo_reference(r[2]->aux.clear_color_bo);
1043bf215546Sopenharmony_ci      r[0]->aux.clear_color_bo = r[2]->aux.clear_color_bo;
1044bf215546Sopenharmony_ci      r[0]->aux.clear_color_offset = r[2]->aux.clear_color_offset;
1045bf215546Sopenharmony_ci      r[0]->aux.clear_color_unknown = true;
1046bf215546Sopenharmony_ci      break;
1047bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
1048bf215546Sopenharmony_ci      assert(num_main_planes == 1);
1049bf215546Sopenharmony_ci      assert(num_planes == 2);
1050bf215546Sopenharmony_ci
1051bf215546Sopenharmony_ci      /* Import the clear color BO. */
1052bf215546Sopenharmony_ci      iris_bo_reference(r[1]->aux.clear_color_bo);
1053bf215546Sopenharmony_ci      r[0]->aux.clear_color_bo = r[1]->aux.clear_color_bo;
1054bf215546Sopenharmony_ci      r[0]->aux.clear_color_offset = r[1]->aux.clear_color_offset;
1055bf215546Sopenharmony_ci      r[0]->aux.clear_color_unknown = true;
1056bf215546Sopenharmony_ci      break;
1057bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1058bf215546Sopenharmony_ci      if (num_main_planes == 1 && num_planes == 2) {
1059bf215546Sopenharmony_ci         import_aux_info(r[0], r[1]);
1060bf215546Sopenharmony_ci         map_aux_addresses(screen, r[0], res->external_format, 0);
1061bf215546Sopenharmony_ci      } else {
1062bf215546Sopenharmony_ci         assert(num_main_planes == 2 && num_planes == 4);
1063bf215546Sopenharmony_ci         import_aux_info(r[0], r[2]);
1064bf215546Sopenharmony_ci         import_aux_info(r[1], r[3]);
1065bf215546Sopenharmony_ci         map_aux_addresses(screen, r[0], res->external_format, 0);
1066bf215546Sopenharmony_ci         map_aux_addresses(screen, r[1], res->external_format, 1);
1067bf215546Sopenharmony_ci      }
1068bf215546Sopenharmony_ci      assert(!isl_aux_usage_has_fast_clears(res->mod_info->aux_usage));
1069bf215546Sopenharmony_ci      break;
1070bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_MC_CCS:
1071bf215546Sopenharmony_ci      assert(!isl_aux_usage_has_fast_clears(res->mod_info->aux_usage));
1072bf215546Sopenharmony_ci      break;
1073bf215546Sopenharmony_ci   default:
1074bf215546Sopenharmony_ci      assert(res->mod_info->aux_usage == ISL_AUX_USAGE_NONE);
1075bf215546Sopenharmony_ci      break;
1076bf215546Sopenharmony_ci   }
1077bf215546Sopenharmony_ci}
1078bf215546Sopenharmony_ci
1079bf215546Sopenharmony_cistatic uint32_t
1080bf215546Sopenharmony_ciiris_buffer_alignment(uint64_t size)
1081bf215546Sopenharmony_ci{
1082bf215546Sopenharmony_ci   /* Some buffer operations want some amount of alignment.  The largest
1083bf215546Sopenharmony_ci    * buffer texture pixel size is 4 * 4 = 16B.  OpenCL data is also supposed
1084bf215546Sopenharmony_ci    * to be aligned and largest OpenCL data type is a double16 which is
1085bf215546Sopenharmony_ci    * 8 * 16 = 128B.  Align to the largest power of 2 which fits in the size,
1086bf215546Sopenharmony_ci    * up to 128B.
1087bf215546Sopenharmony_ci    */
1088bf215546Sopenharmony_ci   uint32_t align = MAX2(4 * 4, 8 * 16);
1089bf215546Sopenharmony_ci   while (align > size)
1090bf215546Sopenharmony_ci      align >>= 1;
1091bf215546Sopenharmony_ci
1092bf215546Sopenharmony_ci   return align;
1093bf215546Sopenharmony_ci}
1094bf215546Sopenharmony_ci
1095bf215546Sopenharmony_cistatic struct pipe_resource *
1096bf215546Sopenharmony_ciiris_resource_create_for_buffer(struct pipe_screen *pscreen,
1097bf215546Sopenharmony_ci                                const struct pipe_resource *templ)
1098bf215546Sopenharmony_ci{
1099bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
1100bf215546Sopenharmony_ci   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1101bf215546Sopenharmony_ci
1102bf215546Sopenharmony_ci   assert(templ->target == PIPE_BUFFER);
1103bf215546Sopenharmony_ci   assert(templ->height0 <= 1);
1104bf215546Sopenharmony_ci   assert(templ->depth0 <= 1);
1105bf215546Sopenharmony_ci   assert(templ->format == PIPE_FORMAT_NONE ||
1106bf215546Sopenharmony_ci          util_format_get_blocksize(templ->format) == 1);
1107bf215546Sopenharmony_ci
1108bf215546Sopenharmony_ci   res->internal_format = templ->format;
1109bf215546Sopenharmony_ci   res->surf.tiling = ISL_TILING_LINEAR;
1110bf215546Sopenharmony_ci
1111bf215546Sopenharmony_ci   enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
1112bf215546Sopenharmony_ci   const char *name = templ->target == PIPE_BUFFER ? "buffer" : "miptree";
1113bf215546Sopenharmony_ci   if (templ->flags & IRIS_RESOURCE_FLAG_SHADER_MEMZONE) {
1114bf215546Sopenharmony_ci      memzone = IRIS_MEMZONE_SHADER;
1115bf215546Sopenharmony_ci      name = "shader kernels";
1116bf215546Sopenharmony_ci   } else if (templ->flags & IRIS_RESOURCE_FLAG_SURFACE_MEMZONE) {
1117bf215546Sopenharmony_ci      memzone = IRIS_MEMZONE_SURFACE;
1118bf215546Sopenharmony_ci      name = "surface state";
1119bf215546Sopenharmony_ci   } else if (templ->flags & IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE) {
1120bf215546Sopenharmony_ci      memzone = IRIS_MEMZONE_DYNAMIC;
1121bf215546Sopenharmony_ci      name = "dynamic state";
1122bf215546Sopenharmony_ci   } else if (templ->flags & IRIS_RESOURCE_FLAG_BINDLESS_MEMZONE) {
1123bf215546Sopenharmony_ci      memzone = IRIS_MEMZONE_BINDLESS;
1124bf215546Sopenharmony_ci      name = "bindless surface state";
1125bf215546Sopenharmony_ci   }
1126bf215546Sopenharmony_ci
1127bf215546Sopenharmony_ci   unsigned flags = iris_resource_alloc_flags(screen, templ, res->aux.usage);
1128bf215546Sopenharmony_ci
1129bf215546Sopenharmony_ci   res->bo = iris_bo_alloc(screen->bufmgr, name, templ->width0,
1130bf215546Sopenharmony_ci                           iris_buffer_alignment(templ->width0),
1131bf215546Sopenharmony_ci                           memzone, flags);
1132bf215546Sopenharmony_ci
1133bf215546Sopenharmony_ci   if (!res->bo) {
1134bf215546Sopenharmony_ci      iris_resource_destroy(pscreen, &res->base.b);
1135bf215546Sopenharmony_ci      return NULL;
1136bf215546Sopenharmony_ci   }
1137bf215546Sopenharmony_ci
1138bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_SHARED) {
1139bf215546Sopenharmony_ci      iris_bo_mark_exported(res->bo);
1140bf215546Sopenharmony_ci      res->base.is_shared = true;
1141bf215546Sopenharmony_ci   }
1142bf215546Sopenharmony_ci
1143bf215546Sopenharmony_ci   return &res->base.b;
1144bf215546Sopenharmony_ci}
1145bf215546Sopenharmony_ci
1146bf215546Sopenharmony_cistatic struct pipe_resource *
1147bf215546Sopenharmony_ciiris_resource_create_with_modifiers(struct pipe_screen *pscreen,
1148bf215546Sopenharmony_ci                                    const struct pipe_resource *templ,
1149bf215546Sopenharmony_ci                                    const uint64_t *modifiers,
1150bf215546Sopenharmony_ci                                    int modifiers_count)
1151bf215546Sopenharmony_ci{
1152bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
1153bf215546Sopenharmony_ci   struct intel_device_info *devinfo = &screen->devinfo;
1154bf215546Sopenharmony_ci   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1155bf215546Sopenharmony_ci
1156bf215546Sopenharmony_ci   if (!res)
1157bf215546Sopenharmony_ci      return NULL;
1158bf215546Sopenharmony_ci
1159bf215546Sopenharmony_ci   uint64_t modifier =
1160bf215546Sopenharmony_ci      select_best_modifier(devinfo, templ, modifiers, modifiers_count);
1161bf215546Sopenharmony_ci
1162bf215546Sopenharmony_ci   if (modifier == DRM_FORMAT_MOD_INVALID && modifiers_count > 0) {
1163bf215546Sopenharmony_ci      fprintf(stderr, "Unsupported modifier, resource creation failed.\n");
1164bf215546Sopenharmony_ci      goto fail;
1165bf215546Sopenharmony_ci   }
1166bf215546Sopenharmony_ci
1167bf215546Sopenharmony_ci   UNUSED const bool isl_surf_created_successfully =
1168bf215546Sopenharmony_ci      iris_resource_configure_main(screen, res, templ, modifier, 0);
1169bf215546Sopenharmony_ci   assert(isl_surf_created_successfully);
1170bf215546Sopenharmony_ci
1171bf215546Sopenharmony_ci   if (!iris_resource_configure_aux(screen, res, false))
1172bf215546Sopenharmony_ci      goto fail;
1173bf215546Sopenharmony_ci
1174bf215546Sopenharmony_ci   const char *name = "miptree";
1175bf215546Sopenharmony_ci   enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
1176bf215546Sopenharmony_ci
1177bf215546Sopenharmony_ci   unsigned flags = iris_resource_alloc_flags(screen, templ, res->aux.usage);
1178bf215546Sopenharmony_ci
1179bf215546Sopenharmony_ci   /* These are for u_upload_mgr buffers only */
1180bf215546Sopenharmony_ci   assert(!(templ->flags & (IRIS_RESOURCE_FLAG_SHADER_MEMZONE |
1181bf215546Sopenharmony_ci                            IRIS_RESOURCE_FLAG_SURFACE_MEMZONE |
1182bf215546Sopenharmony_ci                            IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE |
1183bf215546Sopenharmony_ci                            IRIS_RESOURCE_FLAG_BINDLESS_MEMZONE)));
1184bf215546Sopenharmony_ci
1185bf215546Sopenharmony_ci   /* Modifiers require the aux data to be in the same buffer as the main
1186bf215546Sopenharmony_ci    * surface, but we combine them even when a modifier is not being used.
1187bf215546Sopenharmony_ci    */
1188bf215546Sopenharmony_ci   uint64_t bo_size = res->surf.size_B;
1189bf215546Sopenharmony_ci
1190bf215546Sopenharmony_ci   /* Allocate space for the aux buffer. */
1191bf215546Sopenharmony_ci   if (res->aux.surf.size_B > 0) {
1192bf215546Sopenharmony_ci      res->aux.offset = ALIGN(bo_size, res->aux.surf.alignment_B);
1193bf215546Sopenharmony_ci      bo_size = res->aux.offset + res->aux.surf.size_B;
1194bf215546Sopenharmony_ci   }
1195bf215546Sopenharmony_ci
1196bf215546Sopenharmony_ci   /* Allocate space for the extra aux buffer. */
1197bf215546Sopenharmony_ci   if (res->aux.extra_aux.surf.size_B > 0) {
1198bf215546Sopenharmony_ci      res->aux.extra_aux.offset =
1199bf215546Sopenharmony_ci         ALIGN(bo_size, res->aux.extra_aux.surf.alignment_B);
1200bf215546Sopenharmony_ci      bo_size = res->aux.extra_aux.offset + res->aux.extra_aux.surf.size_B;
1201bf215546Sopenharmony_ci   }
1202bf215546Sopenharmony_ci
1203bf215546Sopenharmony_ci   /* Allocate space for the indirect clear color.
1204bf215546Sopenharmony_ci    *
1205bf215546Sopenharmony_ci    * Also add some padding to make sure the fast clear color state buffer
1206bf215546Sopenharmony_ci    * starts at a 4K alignment. We believe that 256B might be enough, but due
1207bf215546Sopenharmony_ci    * to lack of testing we will leave this as 4K for now.
1208bf215546Sopenharmony_ci    */
1209bf215546Sopenharmony_ci   if (iris_get_aux_clear_color_state_size(screen, res) > 0) {
1210bf215546Sopenharmony_ci      res->aux.clear_color_offset = ALIGN(bo_size, 4096);
1211bf215546Sopenharmony_ci      bo_size = res->aux.clear_color_offset +
1212bf215546Sopenharmony_ci                iris_get_aux_clear_color_state_size(screen, res);
1213bf215546Sopenharmony_ci   }
1214bf215546Sopenharmony_ci
1215bf215546Sopenharmony_ci   uint32_t alignment = MAX2(4096, res->surf.alignment_B);
1216bf215546Sopenharmony_ci   res->bo =
1217bf215546Sopenharmony_ci      iris_bo_alloc(screen->bufmgr, name, bo_size, alignment, memzone, flags);
1218bf215546Sopenharmony_ci
1219bf215546Sopenharmony_ci   if (!res->bo)
1220bf215546Sopenharmony_ci      goto fail;
1221bf215546Sopenharmony_ci
1222bf215546Sopenharmony_ci   if (res->aux.usage != ISL_AUX_USAGE_NONE &&
1223bf215546Sopenharmony_ci       !iris_resource_init_aux_buf(screen, res))
1224bf215546Sopenharmony_ci      goto fail;
1225bf215546Sopenharmony_ci
1226bf215546Sopenharmony_ci   if (templ->bind & PIPE_BIND_SHARED) {
1227bf215546Sopenharmony_ci      iris_bo_mark_exported(res->bo);
1228bf215546Sopenharmony_ci      res->base.is_shared = true;
1229bf215546Sopenharmony_ci   }
1230bf215546Sopenharmony_ci
1231bf215546Sopenharmony_ci   return &res->base.b;
1232bf215546Sopenharmony_ci
1233bf215546Sopenharmony_cifail:
1234bf215546Sopenharmony_ci   fprintf(stderr, "XXX: resource creation failed\n");
1235bf215546Sopenharmony_ci   iris_resource_destroy(pscreen, &res->base.b);
1236bf215546Sopenharmony_ci   return NULL;
1237bf215546Sopenharmony_ci}
1238bf215546Sopenharmony_ci
1239bf215546Sopenharmony_cistatic struct pipe_resource *
1240bf215546Sopenharmony_ciiris_resource_create(struct pipe_screen *pscreen,
1241bf215546Sopenharmony_ci                     const struct pipe_resource *templ)
1242bf215546Sopenharmony_ci{
1243bf215546Sopenharmony_ci   if (templ->target == PIPE_BUFFER)
1244bf215546Sopenharmony_ci      return iris_resource_create_for_buffer(pscreen, templ);
1245bf215546Sopenharmony_ci   else
1246bf215546Sopenharmony_ci      return iris_resource_create_with_modifiers(pscreen, templ, NULL, 0);
1247bf215546Sopenharmony_ci}
1248bf215546Sopenharmony_ci
1249bf215546Sopenharmony_cistatic uint64_t
1250bf215546Sopenharmony_citiling_to_modifier(uint32_t tiling)
1251bf215546Sopenharmony_ci{
1252bf215546Sopenharmony_ci   static const uint64_t map[] = {
1253bf215546Sopenharmony_ci      [I915_TILING_NONE]   = DRM_FORMAT_MOD_LINEAR,
1254bf215546Sopenharmony_ci      [I915_TILING_X]      = I915_FORMAT_MOD_X_TILED,
1255bf215546Sopenharmony_ci      [I915_TILING_Y]      = I915_FORMAT_MOD_Y_TILED,
1256bf215546Sopenharmony_ci   };
1257bf215546Sopenharmony_ci
1258bf215546Sopenharmony_ci   assert(tiling < ARRAY_SIZE(map));
1259bf215546Sopenharmony_ci
1260bf215546Sopenharmony_ci   return map[tiling];
1261bf215546Sopenharmony_ci}
1262bf215546Sopenharmony_ci
1263bf215546Sopenharmony_cistatic struct pipe_resource *
1264bf215546Sopenharmony_ciiris_resource_from_user_memory(struct pipe_screen *pscreen,
1265bf215546Sopenharmony_ci                               const struct pipe_resource *templ,
1266bf215546Sopenharmony_ci                               void *user_memory)
1267bf215546Sopenharmony_ci{
1268bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
1269bf215546Sopenharmony_ci   struct iris_bufmgr *bufmgr = screen->bufmgr;
1270bf215546Sopenharmony_ci   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1271bf215546Sopenharmony_ci   if (!res)
1272bf215546Sopenharmony_ci      return NULL;
1273bf215546Sopenharmony_ci
1274bf215546Sopenharmony_ci   if (templ->target != PIPE_BUFFER &&
1275bf215546Sopenharmony_ci       templ->target != PIPE_TEXTURE_1D &&
1276bf215546Sopenharmony_ci       templ->target != PIPE_TEXTURE_2D)
1277bf215546Sopenharmony_ci      return NULL;
1278bf215546Sopenharmony_ci
1279bf215546Sopenharmony_ci   if (templ->array_size > 1)
1280bf215546Sopenharmony_ci      return NULL;
1281bf215546Sopenharmony_ci
1282bf215546Sopenharmony_ci   size_t res_size = templ->width0;
1283bf215546Sopenharmony_ci   if (templ->target != PIPE_BUFFER) {
1284bf215546Sopenharmony_ci      const uint32_t row_pitch_B =
1285bf215546Sopenharmony_ci         templ->width0 * util_format_get_blocksize(templ->format);
1286bf215546Sopenharmony_ci      res_size = templ->height0 * row_pitch_B;
1287bf215546Sopenharmony_ci
1288bf215546Sopenharmony_ci      if (!iris_resource_configure_main(screen, res, templ,
1289bf215546Sopenharmony_ci                                        DRM_FORMAT_MOD_LINEAR,
1290bf215546Sopenharmony_ci                                        row_pitch_B)) {
1291bf215546Sopenharmony_ci         iris_resource_destroy(pscreen, &res->base.b);
1292bf215546Sopenharmony_ci         return NULL;
1293bf215546Sopenharmony_ci      }
1294bf215546Sopenharmony_ci      assert(res->surf.size_B <= res_size);
1295bf215546Sopenharmony_ci   }
1296bf215546Sopenharmony_ci
1297bf215546Sopenharmony_ci   /* The userptr ioctl only works on whole pages.  Because we know that
1298bf215546Sopenharmony_ci    * things will exist in memory at a page granularity, we can expand the
1299bf215546Sopenharmony_ci    * range given by the client into the whole number of pages and use an
1300bf215546Sopenharmony_ci    * offset on the resource to make it looks like it starts at the user's
1301bf215546Sopenharmony_ci    * pointer.
1302bf215546Sopenharmony_ci    */
1303bf215546Sopenharmony_ci   size_t page_size = getpagesize();
1304bf215546Sopenharmony_ci   assert(util_is_power_of_two_nonzero(page_size));
1305bf215546Sopenharmony_ci   size_t offset = (uintptr_t)user_memory & (page_size - 1);
1306bf215546Sopenharmony_ci   void *mem_start = (char *)user_memory - offset;
1307bf215546Sopenharmony_ci   size_t mem_size = offset + res_size;
1308bf215546Sopenharmony_ci   mem_size = ALIGN_NPOT(mem_size, page_size);
1309bf215546Sopenharmony_ci
1310bf215546Sopenharmony_ci   res->internal_format = templ->format;
1311bf215546Sopenharmony_ci   res->base.is_user_ptr = true;
1312bf215546Sopenharmony_ci   res->bo = iris_bo_create_userptr(bufmgr, "user", mem_start, mem_size,
1313bf215546Sopenharmony_ci                                    IRIS_MEMZONE_OTHER);
1314bf215546Sopenharmony_ci   res->offset = offset;
1315bf215546Sopenharmony_ci   if (!res->bo) {
1316bf215546Sopenharmony_ci      iris_resource_destroy(pscreen, &res->base.b);
1317bf215546Sopenharmony_ci      return NULL;
1318bf215546Sopenharmony_ci   }
1319bf215546Sopenharmony_ci
1320bf215546Sopenharmony_ci   util_range_add(&res->base.b, &res->valid_buffer_range, 0, templ->width0);
1321bf215546Sopenharmony_ci
1322bf215546Sopenharmony_ci   return &res->base.b;
1323bf215546Sopenharmony_ci}
1324bf215546Sopenharmony_ci
1325bf215546Sopenharmony_cistatic bool
1326bf215546Sopenharmony_cimod_plane_is_clear_color(uint64_t modifier, uint32_t plane)
1327bf215546Sopenharmony_ci{
1328bf215546Sopenharmony_ci   ASSERTED const struct isl_drm_modifier_info *mod_info =
1329bf215546Sopenharmony_ci      isl_drm_modifier_get_info(modifier);
1330bf215546Sopenharmony_ci   assert(mod_info);
1331bf215546Sopenharmony_ci
1332bf215546Sopenharmony_ci   switch (modifier) {
1333bf215546Sopenharmony_ci   case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1334bf215546Sopenharmony_ci      assert(mod_info->supports_clear_color);
1335bf215546Sopenharmony_ci      return plane == 2;
1336bf215546Sopenharmony_ci   case I915_FORMAT_MOD_4_TILED_DG2_RC_CCS_CC:
1337bf215546Sopenharmony_ci      assert(mod_info->supports_clear_color);
1338bf215546Sopenharmony_ci      return plane == 1;
1339bf215546Sopenharmony_ci   default:
1340bf215546Sopenharmony_ci      assert(!mod_info->supports_clear_color);
1341bf215546Sopenharmony_ci      return false;
1342bf215546Sopenharmony_ci   }
1343bf215546Sopenharmony_ci}
1344bf215546Sopenharmony_ci
1345bf215546Sopenharmony_cistatic unsigned
1346bf215546Sopenharmony_ciget_num_planes(const struct pipe_resource *resource)
1347bf215546Sopenharmony_ci{
1348bf215546Sopenharmony_ci   unsigned count = 0;
1349bf215546Sopenharmony_ci   for (const struct pipe_resource *cur = resource; cur; cur = cur->next)
1350bf215546Sopenharmony_ci      count++;
1351bf215546Sopenharmony_ci
1352bf215546Sopenharmony_ci   return count;
1353bf215546Sopenharmony_ci}
1354bf215546Sopenharmony_ci
1355bf215546Sopenharmony_cistatic struct pipe_resource *
1356bf215546Sopenharmony_ciiris_resource_from_handle(struct pipe_screen *pscreen,
1357bf215546Sopenharmony_ci                          const struct pipe_resource *templ,
1358bf215546Sopenharmony_ci                          struct winsys_handle *whandle,
1359bf215546Sopenharmony_ci                          unsigned usage)
1360bf215546Sopenharmony_ci{
1361bf215546Sopenharmony_ci   assert(templ->target != PIPE_BUFFER);
1362bf215546Sopenharmony_ci
1363bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
1364bf215546Sopenharmony_ci   struct iris_bufmgr *bufmgr = screen->bufmgr;
1365bf215546Sopenharmony_ci   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1366bf215546Sopenharmony_ci   if (!res)
1367bf215546Sopenharmony_ci      return NULL;
1368bf215546Sopenharmony_ci
1369bf215546Sopenharmony_ci   switch (whandle->type) {
1370bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_FD:
1371bf215546Sopenharmony_ci      res->bo = iris_bo_import_dmabuf(bufmgr, whandle->handle);
1372bf215546Sopenharmony_ci      break;
1373bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_SHARED:
1374bf215546Sopenharmony_ci      res->bo = iris_bo_gem_create_from_name(bufmgr, "winsys image",
1375bf215546Sopenharmony_ci                                             whandle->handle);
1376bf215546Sopenharmony_ci      break;
1377bf215546Sopenharmony_ci   default:
1378bf215546Sopenharmony_ci      unreachable("invalid winsys handle type");
1379bf215546Sopenharmony_ci   }
1380bf215546Sopenharmony_ci   if (!res->bo)
1381bf215546Sopenharmony_ci      goto fail;
1382bf215546Sopenharmony_ci
1383bf215546Sopenharmony_ci   res->offset = whandle->offset;
1384bf215546Sopenharmony_ci   res->external_format = whandle->format;
1385bf215546Sopenharmony_ci
1386bf215546Sopenharmony_ci   /* Create a surface for each plane specified by the external format. */
1387bf215546Sopenharmony_ci   if (whandle->plane < util_format_get_num_planes(whandle->format)) {
1388bf215546Sopenharmony_ci      uint64_t modifier = whandle->modifier;
1389bf215546Sopenharmony_ci
1390bf215546Sopenharmony_ci      if (whandle->modifier == DRM_FORMAT_MOD_INVALID) {
1391bf215546Sopenharmony_ci         /* We don't have a modifier; match whatever GEM_GET_TILING says */
1392bf215546Sopenharmony_ci         uint32_t tiling;
1393bf215546Sopenharmony_ci         iris_gem_get_tiling(res->bo, &tiling);
1394bf215546Sopenharmony_ci         modifier = tiling_to_modifier(tiling);
1395bf215546Sopenharmony_ci      }
1396bf215546Sopenharmony_ci
1397bf215546Sopenharmony_ci      UNUSED const bool isl_surf_created_successfully =
1398bf215546Sopenharmony_ci         iris_resource_configure_main(screen, res, templ, modifier,
1399bf215546Sopenharmony_ci                                      whandle->stride);
1400bf215546Sopenharmony_ci      assert(isl_surf_created_successfully);
1401bf215546Sopenharmony_ci
1402bf215546Sopenharmony_ci      UNUSED const bool ok = iris_resource_configure_aux(screen, res, true);
1403bf215546Sopenharmony_ci      assert(ok);
1404bf215546Sopenharmony_ci      /* The gallium dri layer will create a separate plane resource for the
1405bf215546Sopenharmony_ci       * aux image. iris_resource_finish_aux_import will merge the separate aux
1406bf215546Sopenharmony_ci       * parameters back into a single iris_resource.
1407bf215546Sopenharmony_ci       */
1408bf215546Sopenharmony_ci   } else if (mod_plane_is_clear_color(whandle->modifier, whandle->plane)) {
1409bf215546Sopenharmony_ci      res->aux.clear_color_offset = whandle->offset;
1410bf215546Sopenharmony_ci      res->aux.clear_color_bo = res->bo;
1411bf215546Sopenharmony_ci      res->bo = NULL;
1412bf215546Sopenharmony_ci   } else {
1413bf215546Sopenharmony_ci      /* Save modifier import information to reconstruct later. After import,
1414bf215546Sopenharmony_ci       * this will be available under a second image accessible from the main
1415bf215546Sopenharmony_ci       * image with res->base.next. See iris_resource_finish_aux_import.
1416bf215546Sopenharmony_ci       */
1417bf215546Sopenharmony_ci      res->aux.surf.row_pitch_B = whandle->stride;
1418bf215546Sopenharmony_ci      res->aux.offset = whandle->offset;
1419bf215546Sopenharmony_ci      res->aux.bo = res->bo;
1420bf215546Sopenharmony_ci      res->bo = NULL;
1421bf215546Sopenharmony_ci   }
1422bf215546Sopenharmony_ci
1423bf215546Sopenharmony_ci   if (get_num_planes(&res->base.b) ==
1424bf215546Sopenharmony_ci       iris_get_dmabuf_modifier_planes(pscreen, whandle->modifier,
1425bf215546Sopenharmony_ci                                       whandle->format)) {
1426bf215546Sopenharmony_ci      iris_resource_finish_aux_import(pscreen, res);
1427bf215546Sopenharmony_ci   }
1428bf215546Sopenharmony_ci
1429bf215546Sopenharmony_ci   return &res->base.b;
1430bf215546Sopenharmony_ci
1431bf215546Sopenharmony_cifail:
1432bf215546Sopenharmony_ci   iris_resource_destroy(pscreen, &res->base.b);
1433bf215546Sopenharmony_ci   return NULL;
1434bf215546Sopenharmony_ci}
1435bf215546Sopenharmony_ci
1436bf215546Sopenharmony_cistatic struct pipe_resource *
1437bf215546Sopenharmony_ciiris_resource_from_memobj(struct pipe_screen *pscreen,
1438bf215546Sopenharmony_ci                          const struct pipe_resource *templ,
1439bf215546Sopenharmony_ci                          struct pipe_memory_object *pmemobj,
1440bf215546Sopenharmony_ci                          uint64_t offset)
1441bf215546Sopenharmony_ci{
1442bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
1443bf215546Sopenharmony_ci   struct iris_memory_object *memobj = (struct iris_memory_object *)pmemobj;
1444bf215546Sopenharmony_ci   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
1445bf215546Sopenharmony_ci
1446bf215546Sopenharmony_ci   if (!res)
1447bf215546Sopenharmony_ci      return NULL;
1448bf215546Sopenharmony_ci
1449bf215546Sopenharmony_ci   if (templ->flags & PIPE_RESOURCE_FLAG_TEXTURING_MORE_LIKELY) {
1450bf215546Sopenharmony_ci      UNUSED const bool isl_surf_created_successfully =
1451bf215546Sopenharmony_ci         iris_resource_configure_main(screen, res, templ, DRM_FORMAT_MOD_INVALID, 0);
1452bf215546Sopenharmony_ci      assert(isl_surf_created_successfully);
1453bf215546Sopenharmony_ci   }
1454bf215546Sopenharmony_ci
1455bf215546Sopenharmony_ci   res->bo = memobj->bo;
1456bf215546Sopenharmony_ci   res->offset = offset;
1457bf215546Sopenharmony_ci   res->external_format = memobj->format;
1458bf215546Sopenharmony_ci   res->internal_format = templ->format;
1459bf215546Sopenharmony_ci
1460bf215546Sopenharmony_ci   iris_bo_reference(memobj->bo);
1461bf215546Sopenharmony_ci
1462bf215546Sopenharmony_ci   return &res->base.b;
1463bf215546Sopenharmony_ci}
1464bf215546Sopenharmony_ci
1465bf215546Sopenharmony_ci/* Handle combined depth/stencil with memory objects.
1466bf215546Sopenharmony_ci *
1467bf215546Sopenharmony_ci * This function is modeled after u_transfer_helper_resource_create.
1468bf215546Sopenharmony_ci */
1469bf215546Sopenharmony_cistatic struct pipe_resource *
1470bf215546Sopenharmony_ciiris_resource_from_memobj_wrapper(struct pipe_screen *pscreen,
1471bf215546Sopenharmony_ci                                  const struct pipe_resource *templ,
1472bf215546Sopenharmony_ci                                  struct pipe_memory_object *pmemobj,
1473bf215546Sopenharmony_ci                                  uint64_t offset)
1474bf215546Sopenharmony_ci{
1475bf215546Sopenharmony_ci   enum pipe_format format = templ->format;
1476bf215546Sopenharmony_ci
1477bf215546Sopenharmony_ci   /* Normal case, no special handling: */
1478bf215546Sopenharmony_ci   if (!(util_format_is_depth_and_stencil(format)))
1479bf215546Sopenharmony_ci      return iris_resource_from_memobj(pscreen, templ, pmemobj, offset);
1480bf215546Sopenharmony_ci
1481bf215546Sopenharmony_ci   struct pipe_resource t = *templ;
1482bf215546Sopenharmony_ci   t.format = util_format_get_depth_only(format);
1483bf215546Sopenharmony_ci
1484bf215546Sopenharmony_ci   struct pipe_resource *prsc =
1485bf215546Sopenharmony_ci      iris_resource_from_memobj(pscreen, &t, pmemobj, offset);
1486bf215546Sopenharmony_ci   if (!prsc)
1487bf215546Sopenharmony_ci      return NULL;
1488bf215546Sopenharmony_ci
1489bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) prsc;
1490bf215546Sopenharmony_ci
1491bf215546Sopenharmony_ci   /* Stencil offset in the buffer without aux. */
1492bf215546Sopenharmony_ci   uint64_t s_offset = offset +
1493bf215546Sopenharmony_ci      ALIGN(res->surf.size_B, res->surf.alignment_B);
1494bf215546Sopenharmony_ci
1495bf215546Sopenharmony_ci   prsc->format = format; /* frob the format back to the "external" format */
1496bf215546Sopenharmony_ci
1497bf215546Sopenharmony_ci   t.format = PIPE_FORMAT_S8_UINT;
1498bf215546Sopenharmony_ci   struct pipe_resource *stencil =
1499bf215546Sopenharmony_ci      iris_resource_from_memobj(pscreen, &t, pmemobj, s_offset);
1500bf215546Sopenharmony_ci   if (!stencil) {
1501bf215546Sopenharmony_ci      iris_resource_destroy(pscreen, prsc);
1502bf215546Sopenharmony_ci      return NULL;
1503bf215546Sopenharmony_ci   }
1504bf215546Sopenharmony_ci
1505bf215546Sopenharmony_ci   iris_resource_set_separate_stencil(prsc, stencil);
1506bf215546Sopenharmony_ci   return prsc;
1507bf215546Sopenharmony_ci}
1508bf215546Sopenharmony_ci
1509bf215546Sopenharmony_cistatic void
1510bf215546Sopenharmony_ciiris_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
1511bf215546Sopenharmony_ci{
1512bf215546Sopenharmony_ci   struct iris_context *ice = (struct iris_context *)ctx;
1513bf215546Sopenharmony_ci   struct iris_resource *res = (void *) resource;
1514bf215546Sopenharmony_ci   const struct isl_drm_modifier_info *mod = res->mod_info;
1515bf215546Sopenharmony_ci
1516bf215546Sopenharmony_ci   iris_resource_prepare_access(ice, res,
1517bf215546Sopenharmony_ci                                0, INTEL_REMAINING_LEVELS,
1518bf215546Sopenharmony_ci                                0, INTEL_REMAINING_LAYERS,
1519bf215546Sopenharmony_ci                                mod ? mod->aux_usage : ISL_AUX_USAGE_NONE,
1520bf215546Sopenharmony_ci                                mod ? mod->supports_clear_color : false);
1521bf215546Sopenharmony_ci
1522bf215546Sopenharmony_ci   if (!res->mod_info && res->aux.usage != ISL_AUX_USAGE_NONE) {
1523bf215546Sopenharmony_ci      /* flush_resource may be used to prepare an image for sharing external
1524bf215546Sopenharmony_ci       * to the driver (e.g. via eglCreateImage). To account for this, make
1525bf215546Sopenharmony_ci       * sure to get rid of any compression that a consumer wouldn't know how
1526bf215546Sopenharmony_ci       * to handle.
1527bf215546Sopenharmony_ci       */
1528bf215546Sopenharmony_ci      iris_foreach_batch(ice, batch) {
1529bf215546Sopenharmony_ci         if (iris_batch_references(batch, res->bo))
1530bf215546Sopenharmony_ci            iris_batch_flush(batch);
1531bf215546Sopenharmony_ci      }
1532bf215546Sopenharmony_ci
1533bf215546Sopenharmony_ci      iris_resource_disable_aux(res);
1534bf215546Sopenharmony_ci   }
1535bf215546Sopenharmony_ci}
1536bf215546Sopenharmony_ci
1537bf215546Sopenharmony_ci/**
1538bf215546Sopenharmony_ci * Reallocate a (non-external) resource into new storage, copying the data
1539bf215546Sopenharmony_ci * and modifying the original resource to point at the new storage.
1540bf215546Sopenharmony_ci *
1541bf215546Sopenharmony_ci * This is useful for e.g. moving a suballocated internal resource to a
1542bf215546Sopenharmony_ci * dedicated allocation that can be exported by itself.
1543bf215546Sopenharmony_ci */
1544bf215546Sopenharmony_cistatic void
1545bf215546Sopenharmony_ciiris_reallocate_resource_inplace(struct iris_context *ice,
1546bf215546Sopenharmony_ci                                 struct iris_resource *old_res,
1547bf215546Sopenharmony_ci                                 unsigned new_bind_flag)
1548bf215546Sopenharmony_ci{
1549bf215546Sopenharmony_ci   struct pipe_screen *pscreen = ice->ctx.screen;
1550bf215546Sopenharmony_ci
1551bf215546Sopenharmony_ci   if (iris_bo_is_external(old_res->bo))
1552bf215546Sopenharmony_ci      return;
1553bf215546Sopenharmony_ci
1554bf215546Sopenharmony_ci   assert(old_res->mod_info == NULL);
1555bf215546Sopenharmony_ci   assert(old_res->bo == old_res->aux.bo || old_res->aux.bo == NULL);
1556bf215546Sopenharmony_ci   assert(old_res->bo == old_res->aux.clear_color_bo ||
1557bf215546Sopenharmony_ci          old_res->aux.clear_color_bo == NULL);
1558bf215546Sopenharmony_ci   assert(old_res->external_format == PIPE_FORMAT_NONE);
1559bf215546Sopenharmony_ci
1560bf215546Sopenharmony_ci   struct pipe_resource templ = old_res->base.b;
1561bf215546Sopenharmony_ci   templ.bind |= new_bind_flag;
1562bf215546Sopenharmony_ci
1563bf215546Sopenharmony_ci   struct iris_resource *new_res =
1564bf215546Sopenharmony_ci      (void *) pscreen->resource_create(pscreen, &templ);
1565bf215546Sopenharmony_ci
1566bf215546Sopenharmony_ci   assert(iris_bo_is_real(new_res->bo));
1567bf215546Sopenharmony_ci
1568bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
1569bf215546Sopenharmony_ci
1570bf215546Sopenharmony_ci   if (old_res->base.b.target == PIPE_BUFFER) {
1571bf215546Sopenharmony_ci      struct pipe_box box = (struct pipe_box) {
1572bf215546Sopenharmony_ci         .width = old_res->base.b.width0,
1573bf215546Sopenharmony_ci         .height = 1,
1574bf215546Sopenharmony_ci      };
1575bf215546Sopenharmony_ci
1576bf215546Sopenharmony_ci      iris_copy_region(&ice->blorp, batch, &new_res->base.b, 0, 0, 0, 0,
1577bf215546Sopenharmony_ci                       &old_res->base.b, 0, &box);
1578bf215546Sopenharmony_ci   } else {
1579bf215546Sopenharmony_ci      for (unsigned l = 0; l <= templ.last_level; l++) {
1580bf215546Sopenharmony_ci         struct pipe_box box = (struct pipe_box) {
1581bf215546Sopenharmony_ci            .width = u_minify(templ.width0, l),
1582bf215546Sopenharmony_ci            .height = u_minify(templ.height0, l),
1583bf215546Sopenharmony_ci            .depth = util_num_layers(&templ, l),
1584bf215546Sopenharmony_ci         };
1585bf215546Sopenharmony_ci
1586bf215546Sopenharmony_ci         iris_copy_region(&ice->blorp, batch, &new_res->base.b, l, 0, 0, 0,
1587bf215546Sopenharmony_ci                          &old_res->base.b, l, &box);
1588bf215546Sopenharmony_ci      }
1589bf215546Sopenharmony_ci   }
1590bf215546Sopenharmony_ci
1591bf215546Sopenharmony_ci   iris_flush_resource(&ice->ctx, &new_res->base.b);
1592bf215546Sopenharmony_ci
1593bf215546Sopenharmony_ci   struct iris_bo *old_bo = old_res->bo;
1594bf215546Sopenharmony_ci   struct iris_bo *old_aux_bo = old_res->aux.bo;
1595bf215546Sopenharmony_ci   struct iris_bo *old_clear_color_bo = old_res->aux.clear_color_bo;
1596bf215546Sopenharmony_ci
1597bf215546Sopenharmony_ci   /* Replace the structure fields with the new ones */
1598bf215546Sopenharmony_ci   old_res->base.b.bind = templ.bind;
1599bf215546Sopenharmony_ci   old_res->bo = new_res->bo;
1600bf215546Sopenharmony_ci   old_res->aux.surf = new_res->aux.surf;
1601bf215546Sopenharmony_ci   old_res->aux.bo = new_res->aux.bo;
1602bf215546Sopenharmony_ci   old_res->aux.offset = new_res->aux.offset;
1603bf215546Sopenharmony_ci   old_res->aux.extra_aux.surf = new_res->aux.extra_aux.surf;
1604bf215546Sopenharmony_ci   old_res->aux.extra_aux.offset = new_res->aux.extra_aux.offset;
1605bf215546Sopenharmony_ci   old_res->aux.clear_color_bo = new_res->aux.clear_color_bo;
1606bf215546Sopenharmony_ci   old_res->aux.clear_color_offset = new_res->aux.clear_color_offset;
1607bf215546Sopenharmony_ci   old_res->aux.usage = new_res->aux.usage;
1608bf215546Sopenharmony_ci
1609bf215546Sopenharmony_ci   if (new_res->aux.state) {
1610bf215546Sopenharmony_ci      assert(old_res->aux.state);
1611bf215546Sopenharmony_ci      for (unsigned l = 0; l <= templ.last_level; l++) {
1612bf215546Sopenharmony_ci         unsigned layers = util_num_layers(&templ, l);
1613bf215546Sopenharmony_ci         for (unsigned z = 0; z < layers; z++) {
1614bf215546Sopenharmony_ci            enum isl_aux_state aux =
1615bf215546Sopenharmony_ci               iris_resource_get_aux_state(new_res, l, z);
1616bf215546Sopenharmony_ci            iris_resource_set_aux_state(ice, old_res, l, z, 1, aux);
1617bf215546Sopenharmony_ci         }
1618bf215546Sopenharmony_ci      }
1619bf215546Sopenharmony_ci   }
1620bf215546Sopenharmony_ci
1621bf215546Sopenharmony_ci   /* old_res now points at the new BOs, make new_res point at the old ones
1622bf215546Sopenharmony_ci    * so they'll be freed when we unreference the resource below.
1623bf215546Sopenharmony_ci    */
1624bf215546Sopenharmony_ci   new_res->bo = old_bo;
1625bf215546Sopenharmony_ci   new_res->aux.bo = old_aux_bo;
1626bf215546Sopenharmony_ci   new_res->aux.clear_color_bo = old_clear_color_bo;
1627bf215546Sopenharmony_ci
1628bf215546Sopenharmony_ci   pipe_resource_reference((struct pipe_resource **)&new_res, NULL);
1629bf215546Sopenharmony_ci}
1630bf215546Sopenharmony_ci
1631bf215546Sopenharmony_cistatic void
1632bf215546Sopenharmony_ciiris_resource_disable_suballoc_on_first_query(struct pipe_screen *pscreen,
1633bf215546Sopenharmony_ci                                              struct pipe_context *ctx,
1634bf215546Sopenharmony_ci                                              struct iris_resource *res)
1635bf215546Sopenharmony_ci{
1636bf215546Sopenharmony_ci   if (iris_bo_is_real(res->bo))
1637bf215546Sopenharmony_ci      return;
1638bf215546Sopenharmony_ci
1639bf215546Sopenharmony_ci   assert(!(res->base.b.bind & PIPE_BIND_SHARED));
1640bf215546Sopenharmony_ci
1641bf215546Sopenharmony_ci   bool destroy_context;
1642bf215546Sopenharmony_ci   if (ctx) {
1643bf215546Sopenharmony_ci      ctx = threaded_context_unwrap_sync(ctx);
1644bf215546Sopenharmony_ci      destroy_context = false;
1645bf215546Sopenharmony_ci   } else {
1646bf215546Sopenharmony_ci      /* We need to execute a blit on some GPU context, but the DRI layer
1647bf215546Sopenharmony_ci       * often doesn't give us one.  So we have to invent a temporary one.
1648bf215546Sopenharmony_ci       *
1649bf215546Sopenharmony_ci       * We can't store a permanent context in the screen, as it would cause
1650bf215546Sopenharmony_ci       * circular refcounting where screens reference contexts that reference
1651bf215546Sopenharmony_ci       * resources, while resources reference screens...causing nothing to be
1652bf215546Sopenharmony_ci       * freed.  So we just create and destroy a temporary one here.
1653bf215546Sopenharmony_ci       */
1654bf215546Sopenharmony_ci      ctx = iris_create_context(pscreen, NULL, 0);
1655bf215546Sopenharmony_ci      destroy_context = true;
1656bf215546Sopenharmony_ci   }
1657bf215546Sopenharmony_ci
1658bf215546Sopenharmony_ci   struct iris_context *ice = (struct iris_context *)ctx;
1659bf215546Sopenharmony_ci
1660bf215546Sopenharmony_ci   iris_reallocate_resource_inplace(ice, res, PIPE_BIND_SHARED);
1661bf215546Sopenharmony_ci   assert(res->base.b.bind & PIPE_BIND_SHARED);
1662bf215546Sopenharmony_ci
1663bf215546Sopenharmony_ci   if (destroy_context)
1664bf215546Sopenharmony_ci      iris_destroy_context(ctx);
1665bf215546Sopenharmony_ci}
1666bf215546Sopenharmony_ci
1667bf215546Sopenharmony_ci
1668bf215546Sopenharmony_cistatic void
1669bf215546Sopenharmony_ciiris_resource_disable_aux_on_first_query(struct pipe_resource *resource,
1670bf215546Sopenharmony_ci                                         unsigned usage)
1671bf215546Sopenharmony_ci{
1672bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *)resource;
1673bf215546Sopenharmony_ci   bool mod_with_aux =
1674bf215546Sopenharmony_ci      res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
1675bf215546Sopenharmony_ci
1676bf215546Sopenharmony_ci   /* Disable aux usage if explicit flush not set and this is the first time
1677bf215546Sopenharmony_ci    * we are dealing with this resource and the resource was not created with
1678bf215546Sopenharmony_ci    * a modifier with aux.
1679bf215546Sopenharmony_ci    */
1680bf215546Sopenharmony_ci   if (!mod_with_aux &&
1681bf215546Sopenharmony_ci      (!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && res->aux.usage != 0) &&
1682bf215546Sopenharmony_ci       p_atomic_read(&resource->reference.count) == 1) {
1683bf215546Sopenharmony_ci         iris_resource_disable_aux(res);
1684bf215546Sopenharmony_ci   }
1685bf215546Sopenharmony_ci}
1686bf215546Sopenharmony_ci
1687bf215546Sopenharmony_cistatic bool
1688bf215546Sopenharmony_ciiris_resource_get_param(struct pipe_screen *pscreen,
1689bf215546Sopenharmony_ci                        struct pipe_context *ctx,
1690bf215546Sopenharmony_ci                        struct pipe_resource *resource,
1691bf215546Sopenharmony_ci                        unsigned plane,
1692bf215546Sopenharmony_ci                        unsigned layer,
1693bf215546Sopenharmony_ci                        unsigned level,
1694bf215546Sopenharmony_ci                        enum pipe_resource_param param,
1695bf215546Sopenharmony_ci                        unsigned handle_usage,
1696bf215546Sopenharmony_ci                        uint64_t *value)
1697bf215546Sopenharmony_ci{
1698bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *)pscreen;
1699bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *)resource;
1700bf215546Sopenharmony_ci   bool mod_with_aux =
1701bf215546Sopenharmony_ci      res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
1702bf215546Sopenharmony_ci   bool wants_aux = mod_with_aux && plane > 0;
1703bf215546Sopenharmony_ci   bool wants_cc = mod_with_aux &&
1704bf215546Sopenharmony_ci      mod_plane_is_clear_color(res->mod_info->modifier, plane);
1705bf215546Sopenharmony_ci   bool result;
1706bf215546Sopenharmony_ci   unsigned handle;
1707bf215546Sopenharmony_ci
1708bf215546Sopenharmony_ci   iris_resource_disable_aux_on_first_query(resource, handle_usage);
1709bf215546Sopenharmony_ci   iris_resource_disable_suballoc_on_first_query(pscreen, ctx, res);
1710bf215546Sopenharmony_ci
1711bf215546Sopenharmony_ci   struct iris_bo *bo = wants_cc ? res->aux.clear_color_bo :
1712bf215546Sopenharmony_ci                        wants_aux ? res->aux.bo : res->bo;
1713bf215546Sopenharmony_ci
1714bf215546Sopenharmony_ci   assert(iris_bo_is_real(bo));
1715bf215546Sopenharmony_ci
1716bf215546Sopenharmony_ci   switch (param) {
1717bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_NPLANES:
1718bf215546Sopenharmony_ci      if (mod_with_aux) {
1719bf215546Sopenharmony_ci         *value = iris_get_dmabuf_modifier_planes(pscreen,
1720bf215546Sopenharmony_ci                                                  res->mod_info->modifier,
1721bf215546Sopenharmony_ci                                                  res->external_format);
1722bf215546Sopenharmony_ci      } else {
1723bf215546Sopenharmony_ci         *value = get_num_planes(&res->base.b);
1724bf215546Sopenharmony_ci      }
1725bf215546Sopenharmony_ci      return true;
1726bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_STRIDE:
1727bf215546Sopenharmony_ci      *value = wants_cc ? 64 :
1728bf215546Sopenharmony_ci               wants_aux ? res->aux.surf.row_pitch_B : res->surf.row_pitch_B;
1729bf215546Sopenharmony_ci
1730bf215546Sopenharmony_ci      /* Mesa's implementation of eglCreateImage rejects strides of zero (see
1731bf215546Sopenharmony_ci       * dri2_check_dma_buf_attribs). Ensure we return a non-zero stride as
1732bf215546Sopenharmony_ci       * this value may be queried from GBM and passed into EGL.
1733bf215546Sopenharmony_ci       *
1734bf215546Sopenharmony_ci       * Also, although modifiers which use a clear color plane specify that
1735bf215546Sopenharmony_ci       * the plane's pitch should be ignored, some kernels have been found to
1736bf215546Sopenharmony_ci       * require 64-byte alignment.
1737bf215546Sopenharmony_ci       */
1738bf215546Sopenharmony_ci      assert(*value != 0 && (!wants_cc || *value % 64 == 0));
1739bf215546Sopenharmony_ci
1740bf215546Sopenharmony_ci      return true;
1741bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_OFFSET:
1742bf215546Sopenharmony_ci      *value = wants_cc ? res->aux.clear_color_offset :
1743bf215546Sopenharmony_ci               wants_aux ? res->aux.offset : 0;
1744bf215546Sopenharmony_ci      return true;
1745bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_MODIFIER:
1746bf215546Sopenharmony_ci      *value = res->mod_info ? res->mod_info->modifier :
1747bf215546Sopenharmony_ci               tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));
1748bf215546Sopenharmony_ci      return true;
1749bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_HANDLE_TYPE_SHARED:
1750bf215546Sopenharmony_ci      if (!wants_aux)
1751bf215546Sopenharmony_ci         iris_gem_set_tiling(bo, &res->surf);
1752bf215546Sopenharmony_ci
1753bf215546Sopenharmony_ci      result = iris_bo_flink(bo, &handle) == 0;
1754bf215546Sopenharmony_ci      if (result)
1755bf215546Sopenharmony_ci         *value = handle;
1756bf215546Sopenharmony_ci      return result;
1757bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_HANDLE_TYPE_KMS: {
1758bf215546Sopenharmony_ci      if (!wants_aux)
1759bf215546Sopenharmony_ci         iris_gem_set_tiling(bo, &res->surf);
1760bf215546Sopenharmony_ci
1761bf215546Sopenharmony_ci      /* Because we share the same drm file across multiple iris_screen, when
1762bf215546Sopenharmony_ci       * we export a GEM handle we must make sure it is valid in the DRM file
1763bf215546Sopenharmony_ci       * descriptor the caller is using (this is the FD given at screen
1764bf215546Sopenharmony_ci       * creation).
1765bf215546Sopenharmony_ci       */
1766bf215546Sopenharmony_ci      uint32_t handle;
1767bf215546Sopenharmony_ci      if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))
1768bf215546Sopenharmony_ci         return false;
1769bf215546Sopenharmony_ci      *value = handle;
1770bf215546Sopenharmony_ci      return true;
1771bf215546Sopenharmony_ci   }
1772bf215546Sopenharmony_ci
1773bf215546Sopenharmony_ci   case PIPE_RESOURCE_PARAM_HANDLE_TYPE_FD:
1774bf215546Sopenharmony_ci      if (!wants_aux)
1775bf215546Sopenharmony_ci         iris_gem_set_tiling(bo, &res->surf);
1776bf215546Sopenharmony_ci
1777bf215546Sopenharmony_ci      result = iris_bo_export_dmabuf(bo, (int *) &handle) == 0;
1778bf215546Sopenharmony_ci      if (result)
1779bf215546Sopenharmony_ci         *value = handle;
1780bf215546Sopenharmony_ci      return result;
1781bf215546Sopenharmony_ci   default:
1782bf215546Sopenharmony_ci      return false;
1783bf215546Sopenharmony_ci   }
1784bf215546Sopenharmony_ci}
1785bf215546Sopenharmony_ci
1786bf215546Sopenharmony_cistatic bool
1787bf215546Sopenharmony_ciiris_resource_get_handle(struct pipe_screen *pscreen,
1788bf215546Sopenharmony_ci                         struct pipe_context *ctx,
1789bf215546Sopenharmony_ci                         struct pipe_resource *resource,
1790bf215546Sopenharmony_ci                         struct winsys_handle *whandle,
1791bf215546Sopenharmony_ci                         unsigned usage)
1792bf215546Sopenharmony_ci{
1793bf215546Sopenharmony_ci   struct iris_screen *screen = (struct iris_screen *) pscreen;
1794bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *)resource;
1795bf215546Sopenharmony_ci   bool mod_with_aux =
1796bf215546Sopenharmony_ci      res->mod_info && res->mod_info->aux_usage != ISL_AUX_USAGE_NONE;
1797bf215546Sopenharmony_ci
1798bf215546Sopenharmony_ci   iris_resource_disable_aux_on_first_query(resource, usage);
1799bf215546Sopenharmony_ci   iris_resource_disable_suballoc_on_first_query(pscreen, ctx, res);
1800bf215546Sopenharmony_ci
1801bf215546Sopenharmony_ci   assert(iris_bo_is_real(res->bo));
1802bf215546Sopenharmony_ci
1803bf215546Sopenharmony_ci   struct iris_bo *bo;
1804bf215546Sopenharmony_ci   if (res->mod_info &&
1805bf215546Sopenharmony_ci       mod_plane_is_clear_color(res->mod_info->modifier, whandle->plane)) {
1806bf215546Sopenharmony_ci      bo = res->aux.clear_color_bo;
1807bf215546Sopenharmony_ci      whandle->offset = res->aux.clear_color_offset;
1808bf215546Sopenharmony_ci   } else if (mod_with_aux && whandle->plane > 0) {
1809bf215546Sopenharmony_ci      bo = res->aux.bo;
1810bf215546Sopenharmony_ci      whandle->stride = res->aux.surf.row_pitch_B;
1811bf215546Sopenharmony_ci      whandle->offset = res->aux.offset;
1812bf215546Sopenharmony_ci   } else {
1813bf215546Sopenharmony_ci      /* If this is a buffer, stride should be 0 - no need to special case */
1814bf215546Sopenharmony_ci      whandle->stride = res->surf.row_pitch_B;
1815bf215546Sopenharmony_ci      bo = res->bo;
1816bf215546Sopenharmony_ci   }
1817bf215546Sopenharmony_ci
1818bf215546Sopenharmony_ci   whandle->format = res->external_format;
1819bf215546Sopenharmony_ci   whandle->modifier =
1820bf215546Sopenharmony_ci      res->mod_info ? res->mod_info->modifier
1821bf215546Sopenharmony_ci                    : tiling_to_modifier(isl_tiling_to_i915_tiling(res->surf.tiling));
1822bf215546Sopenharmony_ci
1823bf215546Sopenharmony_ci#ifndef NDEBUG
1824bf215546Sopenharmony_ci   enum isl_aux_usage allowed_usage =
1825bf215546Sopenharmony_ci      usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH ? res->aux.usage :
1826bf215546Sopenharmony_ci      res->mod_info ? res->mod_info->aux_usage : ISL_AUX_USAGE_NONE;
1827bf215546Sopenharmony_ci
1828bf215546Sopenharmony_ci   if (res->aux.usage != allowed_usage) {
1829bf215546Sopenharmony_ci      enum isl_aux_state aux_state = iris_resource_get_aux_state(res, 0, 0);
1830bf215546Sopenharmony_ci      assert(aux_state == ISL_AUX_STATE_RESOLVED ||
1831bf215546Sopenharmony_ci             aux_state == ISL_AUX_STATE_PASS_THROUGH);
1832bf215546Sopenharmony_ci   }
1833bf215546Sopenharmony_ci#endif
1834bf215546Sopenharmony_ci
1835bf215546Sopenharmony_ci   switch (whandle->type) {
1836bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_SHARED:
1837bf215546Sopenharmony_ci      iris_gem_set_tiling(bo, &res->surf);
1838bf215546Sopenharmony_ci      return iris_bo_flink(bo, &whandle->handle) == 0;
1839bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_KMS: {
1840bf215546Sopenharmony_ci      iris_gem_set_tiling(bo, &res->surf);
1841bf215546Sopenharmony_ci
1842bf215546Sopenharmony_ci      /* Because we share the same drm file across multiple iris_screen, when
1843bf215546Sopenharmony_ci       * we export a GEM handle we must make sure it is valid in the DRM file
1844bf215546Sopenharmony_ci       * descriptor the caller is using (this is the FD given at screen
1845bf215546Sopenharmony_ci       * creation).
1846bf215546Sopenharmony_ci       */
1847bf215546Sopenharmony_ci      uint32_t handle;
1848bf215546Sopenharmony_ci      if (iris_bo_export_gem_handle_for_device(bo, screen->winsys_fd, &handle))
1849bf215546Sopenharmony_ci         return false;
1850bf215546Sopenharmony_ci      whandle->handle = handle;
1851bf215546Sopenharmony_ci      return true;
1852bf215546Sopenharmony_ci   }
1853bf215546Sopenharmony_ci   case WINSYS_HANDLE_TYPE_FD:
1854bf215546Sopenharmony_ci      iris_gem_set_tiling(bo, &res->surf);
1855bf215546Sopenharmony_ci      return iris_bo_export_dmabuf(bo, (int *) &whandle->handle) == 0;
1856bf215546Sopenharmony_ci   }
1857bf215546Sopenharmony_ci
1858bf215546Sopenharmony_ci   return false;
1859bf215546Sopenharmony_ci}
1860bf215546Sopenharmony_ci
1861bf215546Sopenharmony_cistatic bool
1862bf215546Sopenharmony_ciresource_is_busy(struct iris_context *ice,
1863bf215546Sopenharmony_ci                 struct iris_resource *res)
1864bf215546Sopenharmony_ci{
1865bf215546Sopenharmony_ci   bool busy = iris_bo_busy(res->bo);
1866bf215546Sopenharmony_ci
1867bf215546Sopenharmony_ci   iris_foreach_batch(ice, batch)
1868bf215546Sopenharmony_ci      busy |= iris_batch_references(batch, res->bo);
1869bf215546Sopenharmony_ci
1870bf215546Sopenharmony_ci   return busy;
1871bf215546Sopenharmony_ci}
1872bf215546Sopenharmony_ci
1873bf215546Sopenharmony_civoid
1874bf215546Sopenharmony_ciiris_replace_buffer_storage(struct pipe_context *ctx,
1875bf215546Sopenharmony_ci                            struct pipe_resource *p_dst,
1876bf215546Sopenharmony_ci                            struct pipe_resource *p_src,
1877bf215546Sopenharmony_ci                            unsigned num_rebinds,
1878bf215546Sopenharmony_ci                            uint32_t rebind_mask,
1879bf215546Sopenharmony_ci                            uint32_t delete_buffer_id)
1880bf215546Sopenharmony_ci{
1881bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) ctx->screen;
1882bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
1883bf215546Sopenharmony_ci   struct iris_resource *dst = (void *) p_dst;
1884bf215546Sopenharmony_ci   struct iris_resource *src = (void *) p_src;
1885bf215546Sopenharmony_ci
1886bf215546Sopenharmony_ci   assert(memcmp(&dst->surf, &src->surf, sizeof(dst->surf)) == 0);
1887bf215546Sopenharmony_ci
1888bf215546Sopenharmony_ci   struct iris_bo *old_bo = dst->bo;
1889bf215546Sopenharmony_ci
1890bf215546Sopenharmony_ci   /* Swap out the backing storage */
1891bf215546Sopenharmony_ci   iris_bo_reference(src->bo);
1892bf215546Sopenharmony_ci   dst->bo = src->bo;
1893bf215546Sopenharmony_ci
1894bf215546Sopenharmony_ci   /* Rebind the buffer, replacing any state referring to the old BO's
1895bf215546Sopenharmony_ci    * address, and marking state dirty so it's reemitted.
1896bf215546Sopenharmony_ci    */
1897bf215546Sopenharmony_ci   screen->vtbl.rebind_buffer(ice, dst);
1898bf215546Sopenharmony_ci
1899bf215546Sopenharmony_ci   iris_bo_unreference(old_bo);
1900bf215546Sopenharmony_ci}
1901bf215546Sopenharmony_ci
1902bf215546Sopenharmony_cistatic void
1903bf215546Sopenharmony_ciiris_invalidate_resource(struct pipe_context *ctx,
1904bf215546Sopenharmony_ci                         struct pipe_resource *resource)
1905bf215546Sopenharmony_ci{
1906bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) ctx->screen;
1907bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
1908bf215546Sopenharmony_ci   struct iris_resource *res = (void *) resource;
1909bf215546Sopenharmony_ci
1910bf215546Sopenharmony_ci   if (resource->target != PIPE_BUFFER)
1911bf215546Sopenharmony_ci      return;
1912bf215546Sopenharmony_ci
1913bf215546Sopenharmony_ci   /* If it's already invalidated, don't bother doing anything. */
1914bf215546Sopenharmony_ci   if (res->valid_buffer_range.start > res->valid_buffer_range.end)
1915bf215546Sopenharmony_ci      return;
1916bf215546Sopenharmony_ci
1917bf215546Sopenharmony_ci   if (!resource_is_busy(ice, res)) {
1918bf215546Sopenharmony_ci      /* The resource is idle, so just mark that it contains no data and
1919bf215546Sopenharmony_ci       * keep using the same underlying buffer object.
1920bf215546Sopenharmony_ci       */
1921bf215546Sopenharmony_ci      util_range_set_empty(&res->valid_buffer_range);
1922bf215546Sopenharmony_ci      return;
1923bf215546Sopenharmony_ci   }
1924bf215546Sopenharmony_ci
1925bf215546Sopenharmony_ci   /* Otherwise, try and replace the backing storage with a new BO. */
1926bf215546Sopenharmony_ci
1927bf215546Sopenharmony_ci   /* We can't reallocate memory we didn't allocate in the first place. */
1928bf215546Sopenharmony_ci   if (res->bo->gem_handle && res->bo->real.userptr)
1929bf215546Sopenharmony_ci      return;
1930bf215546Sopenharmony_ci
1931bf215546Sopenharmony_ci   struct iris_bo *old_bo = res->bo;
1932bf215546Sopenharmony_ci   struct iris_bo *new_bo =
1933bf215546Sopenharmony_ci      iris_bo_alloc(screen->bufmgr, res->bo->name, resource->width0,
1934bf215546Sopenharmony_ci                    iris_buffer_alignment(resource->width0),
1935bf215546Sopenharmony_ci                    iris_memzone_for_address(old_bo->address), 0);
1936bf215546Sopenharmony_ci   if (!new_bo)
1937bf215546Sopenharmony_ci      return;
1938bf215546Sopenharmony_ci
1939bf215546Sopenharmony_ci   /* Swap out the backing storage */
1940bf215546Sopenharmony_ci   res->bo = new_bo;
1941bf215546Sopenharmony_ci
1942bf215546Sopenharmony_ci   /* Rebind the buffer, replacing any state referring to the old BO's
1943bf215546Sopenharmony_ci    * address, and marking state dirty so it's reemitted.
1944bf215546Sopenharmony_ci    */
1945bf215546Sopenharmony_ci   screen->vtbl.rebind_buffer(ice, res);
1946bf215546Sopenharmony_ci
1947bf215546Sopenharmony_ci   util_range_set_empty(&res->valid_buffer_range);
1948bf215546Sopenharmony_ci
1949bf215546Sopenharmony_ci   iris_bo_unreference(old_bo);
1950bf215546Sopenharmony_ci}
1951bf215546Sopenharmony_ci
1952bf215546Sopenharmony_cistatic void
1953bf215546Sopenharmony_ciiris_flush_staging_region(struct pipe_transfer *xfer,
1954bf215546Sopenharmony_ci                          const struct pipe_box *flush_box)
1955bf215546Sopenharmony_ci{
1956bf215546Sopenharmony_ci   if (!(xfer->usage & PIPE_MAP_WRITE))
1957bf215546Sopenharmony_ci      return;
1958bf215546Sopenharmony_ci
1959bf215546Sopenharmony_ci   struct iris_transfer *map = (void *) xfer;
1960bf215546Sopenharmony_ci
1961bf215546Sopenharmony_ci   struct pipe_box src_box = *flush_box;
1962bf215546Sopenharmony_ci
1963bf215546Sopenharmony_ci   /* Account for extra alignment padding in staging buffer */
1964bf215546Sopenharmony_ci   if (xfer->resource->target == PIPE_BUFFER)
1965bf215546Sopenharmony_ci      src_box.x += xfer->box.x % IRIS_MAP_BUFFER_ALIGNMENT;
1966bf215546Sopenharmony_ci
1967bf215546Sopenharmony_ci   struct pipe_box dst_box = (struct pipe_box) {
1968bf215546Sopenharmony_ci      .x = xfer->box.x + flush_box->x,
1969bf215546Sopenharmony_ci      .y = xfer->box.y + flush_box->y,
1970bf215546Sopenharmony_ci      .z = xfer->box.z + flush_box->z,
1971bf215546Sopenharmony_ci      .width = flush_box->width,
1972bf215546Sopenharmony_ci      .height = flush_box->height,
1973bf215546Sopenharmony_ci      .depth = flush_box->depth,
1974bf215546Sopenharmony_ci   };
1975bf215546Sopenharmony_ci
1976bf215546Sopenharmony_ci   iris_copy_region(map->blorp, map->batch, xfer->resource, xfer->level,
1977bf215546Sopenharmony_ci                    dst_box.x, dst_box.y, dst_box.z, map->staging, 0,
1978bf215546Sopenharmony_ci                    &src_box);
1979bf215546Sopenharmony_ci}
1980bf215546Sopenharmony_ci
1981bf215546Sopenharmony_cistatic void
1982bf215546Sopenharmony_ciiris_unmap_copy_region(struct iris_transfer *map)
1983bf215546Sopenharmony_ci{
1984bf215546Sopenharmony_ci   iris_resource_destroy(map->staging->screen, map->staging);
1985bf215546Sopenharmony_ci
1986bf215546Sopenharmony_ci   map->ptr = NULL;
1987bf215546Sopenharmony_ci}
1988bf215546Sopenharmony_ci
1989bf215546Sopenharmony_cistatic void
1990bf215546Sopenharmony_ciiris_map_copy_region(struct iris_transfer *map)
1991bf215546Sopenharmony_ci{
1992bf215546Sopenharmony_ci   struct pipe_screen *pscreen = &map->batch->screen->base;
1993bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
1994bf215546Sopenharmony_ci   struct pipe_box *box = &xfer->box;
1995bf215546Sopenharmony_ci   struct iris_resource *res = (void *) xfer->resource;
1996bf215546Sopenharmony_ci
1997bf215546Sopenharmony_ci   unsigned extra = xfer->resource->target == PIPE_BUFFER ?
1998bf215546Sopenharmony_ci                    box->x % IRIS_MAP_BUFFER_ALIGNMENT : 0;
1999bf215546Sopenharmony_ci
2000bf215546Sopenharmony_ci   struct pipe_resource templ = (struct pipe_resource) {
2001bf215546Sopenharmony_ci      .usage = PIPE_USAGE_STAGING,
2002bf215546Sopenharmony_ci      .width0 = box->width + extra,
2003bf215546Sopenharmony_ci      .height0 = box->height,
2004bf215546Sopenharmony_ci      .depth0 = 1,
2005bf215546Sopenharmony_ci      .nr_samples = xfer->resource->nr_samples,
2006bf215546Sopenharmony_ci      .nr_storage_samples = xfer->resource->nr_storage_samples,
2007bf215546Sopenharmony_ci      .array_size = box->depth,
2008bf215546Sopenharmony_ci      .format = res->internal_format,
2009bf215546Sopenharmony_ci   };
2010bf215546Sopenharmony_ci
2011bf215546Sopenharmony_ci   if (xfer->resource->target == PIPE_BUFFER)
2012bf215546Sopenharmony_ci      templ.target = PIPE_BUFFER;
2013bf215546Sopenharmony_ci   else if (templ.array_size > 1)
2014bf215546Sopenharmony_ci      templ.target = PIPE_TEXTURE_2D_ARRAY;
2015bf215546Sopenharmony_ci   else
2016bf215546Sopenharmony_ci      templ.target = PIPE_TEXTURE_2D;
2017bf215546Sopenharmony_ci
2018bf215546Sopenharmony_ci   map->staging = iris_resource_create(pscreen, &templ);
2019bf215546Sopenharmony_ci   assert(map->staging);
2020bf215546Sopenharmony_ci
2021bf215546Sopenharmony_ci   if (templ.target != PIPE_BUFFER) {
2022bf215546Sopenharmony_ci      struct isl_surf *surf = &((struct iris_resource *) map->staging)->surf;
2023bf215546Sopenharmony_ci      xfer->stride = isl_surf_get_row_pitch_B(surf);
2024bf215546Sopenharmony_ci      xfer->layer_stride = isl_surf_get_array_pitch(surf);
2025bf215546Sopenharmony_ci   }
2026bf215546Sopenharmony_ci
2027bf215546Sopenharmony_ci   if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {
2028bf215546Sopenharmony_ci      iris_copy_region(map->blorp, map->batch, map->staging, 0, extra, 0, 0,
2029bf215546Sopenharmony_ci                       xfer->resource, xfer->level, box);
2030bf215546Sopenharmony_ci      /* Ensure writes to the staging BO land before we map it below. */
2031bf215546Sopenharmony_ci      iris_emit_pipe_control_flush(map->batch,
2032bf215546Sopenharmony_ci                                   "transfer read: flush before mapping",
2033bf215546Sopenharmony_ci                                   PIPE_CONTROL_RENDER_TARGET_FLUSH |
2034bf215546Sopenharmony_ci                                   PIPE_CONTROL_TILE_CACHE_FLUSH |
2035bf215546Sopenharmony_ci                                   PIPE_CONTROL_CS_STALL);
2036bf215546Sopenharmony_ci   }
2037bf215546Sopenharmony_ci
2038bf215546Sopenharmony_ci   struct iris_bo *staging_bo = iris_resource_bo(map->staging);
2039bf215546Sopenharmony_ci
2040bf215546Sopenharmony_ci   if (iris_batch_references(map->batch, staging_bo))
2041bf215546Sopenharmony_ci      iris_batch_flush(map->batch);
2042bf215546Sopenharmony_ci
2043bf215546Sopenharmony_ci   assert(((struct iris_resource *)map->staging)->offset == 0);
2044bf215546Sopenharmony_ci   map->ptr =
2045bf215546Sopenharmony_ci      iris_bo_map(map->dbg, staging_bo, xfer->usage & MAP_FLAGS) + extra;
2046bf215546Sopenharmony_ci
2047bf215546Sopenharmony_ci   map->unmap = iris_unmap_copy_region;
2048bf215546Sopenharmony_ci}
2049bf215546Sopenharmony_ci
2050bf215546Sopenharmony_cistatic void
2051bf215546Sopenharmony_ciget_image_offset_el(const struct isl_surf *surf, unsigned level, unsigned z,
2052bf215546Sopenharmony_ci                    unsigned *out_x0_el, unsigned *out_y0_el)
2053bf215546Sopenharmony_ci{
2054bf215546Sopenharmony_ci   ASSERTED uint32_t z0_el, a0_el;
2055bf215546Sopenharmony_ci   if (surf->dim == ISL_SURF_DIM_3D) {
2056bf215546Sopenharmony_ci      isl_surf_get_image_offset_el(surf, level, 0, z,
2057bf215546Sopenharmony_ci                                   out_x0_el, out_y0_el, &z0_el, &a0_el);
2058bf215546Sopenharmony_ci   } else {
2059bf215546Sopenharmony_ci      isl_surf_get_image_offset_el(surf, level, z, 0,
2060bf215546Sopenharmony_ci                                   out_x0_el, out_y0_el, &z0_el, &a0_el);
2061bf215546Sopenharmony_ci   }
2062bf215546Sopenharmony_ci   assert(z0_el == 0 && a0_el == 0);
2063bf215546Sopenharmony_ci}
2064bf215546Sopenharmony_ci
2065bf215546Sopenharmony_ci/**
2066bf215546Sopenharmony_ci * Get pointer offset into stencil buffer.
2067bf215546Sopenharmony_ci *
2068bf215546Sopenharmony_ci * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2069bf215546Sopenharmony_ci * must decode the tile's layout in software.
2070bf215546Sopenharmony_ci *
2071bf215546Sopenharmony_ci * See
2072bf215546Sopenharmony_ci *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2073bf215546Sopenharmony_ci *     Format.
2074bf215546Sopenharmony_ci *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2075bf215546Sopenharmony_ci *
2076bf215546Sopenharmony_ci * Even though the returned offset is always positive, the return type is
2077bf215546Sopenharmony_ci * signed due to
2078bf215546Sopenharmony_ci *    commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2079bf215546Sopenharmony_ci *    mesa: Fix return type of  _mesa_get_format_bytes() (#37351)
2080bf215546Sopenharmony_ci */
2081bf215546Sopenharmony_cistatic intptr_t
2082bf215546Sopenharmony_cis8_offset(uint32_t stride, uint32_t x, uint32_t y)
2083bf215546Sopenharmony_ci{
2084bf215546Sopenharmony_ci   uint32_t tile_size = 4096;
2085bf215546Sopenharmony_ci   uint32_t tile_width = 64;
2086bf215546Sopenharmony_ci   uint32_t tile_height = 64;
2087bf215546Sopenharmony_ci   uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */
2088bf215546Sopenharmony_ci
2089bf215546Sopenharmony_ci   uint32_t tile_x = x / tile_width;
2090bf215546Sopenharmony_ci   uint32_t tile_y = y / tile_height;
2091bf215546Sopenharmony_ci
2092bf215546Sopenharmony_ci   /* The byte's address relative to the tile's base addres. */
2093bf215546Sopenharmony_ci   uint32_t byte_x = x % tile_width;
2094bf215546Sopenharmony_ci   uint32_t byte_y = y % tile_height;
2095bf215546Sopenharmony_ci
2096bf215546Sopenharmony_ci   uintptr_t u = tile_y * row_size
2097bf215546Sopenharmony_ci               + tile_x * tile_size
2098bf215546Sopenharmony_ci               + 512 * (byte_x / 8)
2099bf215546Sopenharmony_ci               +  64 * (byte_y / 8)
2100bf215546Sopenharmony_ci               +  32 * ((byte_y / 4) % 2)
2101bf215546Sopenharmony_ci               +  16 * ((byte_x / 4) % 2)
2102bf215546Sopenharmony_ci               +   8 * ((byte_y / 2) % 2)
2103bf215546Sopenharmony_ci               +   4 * ((byte_x / 2) % 2)
2104bf215546Sopenharmony_ci               +   2 * (byte_y % 2)
2105bf215546Sopenharmony_ci               +   1 * (byte_x % 2);
2106bf215546Sopenharmony_ci
2107bf215546Sopenharmony_ci   return u;
2108bf215546Sopenharmony_ci}
2109bf215546Sopenharmony_ci
2110bf215546Sopenharmony_cistatic void
2111bf215546Sopenharmony_ciiris_unmap_s8(struct iris_transfer *map)
2112bf215546Sopenharmony_ci{
2113bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
2114bf215546Sopenharmony_ci   const struct pipe_box *box = &xfer->box;
2115bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) xfer->resource;
2116bf215546Sopenharmony_ci   struct isl_surf *surf = &res->surf;
2117bf215546Sopenharmony_ci
2118bf215546Sopenharmony_ci   if (xfer->usage & PIPE_MAP_WRITE) {
2119bf215546Sopenharmony_ci      uint8_t *untiled_s8_map = map->ptr;
2120bf215546Sopenharmony_ci      uint8_t *tiled_s8_map = res->offset +
2121bf215546Sopenharmony_ci         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2122bf215546Sopenharmony_ci
2123bf215546Sopenharmony_ci      for (int s = 0; s < box->depth; s++) {
2124bf215546Sopenharmony_ci         unsigned x0_el, y0_el;
2125bf215546Sopenharmony_ci         get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
2126bf215546Sopenharmony_ci
2127bf215546Sopenharmony_ci         for (uint32_t y = 0; y < box->height; y++) {
2128bf215546Sopenharmony_ci            for (uint32_t x = 0; x < box->width; x++) {
2129bf215546Sopenharmony_ci               ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2130bf215546Sopenharmony_ci                                            x0_el + box->x + x,
2131bf215546Sopenharmony_ci                                            y0_el + box->y + y);
2132bf215546Sopenharmony_ci               tiled_s8_map[offset] =
2133bf215546Sopenharmony_ci                  untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x];
2134bf215546Sopenharmony_ci            }
2135bf215546Sopenharmony_ci         }
2136bf215546Sopenharmony_ci      }
2137bf215546Sopenharmony_ci   }
2138bf215546Sopenharmony_ci
2139bf215546Sopenharmony_ci   free(map->buffer);
2140bf215546Sopenharmony_ci}
2141bf215546Sopenharmony_ci
2142bf215546Sopenharmony_cistatic void
2143bf215546Sopenharmony_ciiris_map_s8(struct iris_transfer *map)
2144bf215546Sopenharmony_ci{
2145bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
2146bf215546Sopenharmony_ci   const struct pipe_box *box = &xfer->box;
2147bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) xfer->resource;
2148bf215546Sopenharmony_ci   struct isl_surf *surf = &res->surf;
2149bf215546Sopenharmony_ci
2150bf215546Sopenharmony_ci   xfer->stride = surf->row_pitch_B;
2151bf215546Sopenharmony_ci   xfer->layer_stride = xfer->stride * box->height;
2152bf215546Sopenharmony_ci
2153bf215546Sopenharmony_ci   /* The tiling and detiling functions require that the linear buffer has
2154bf215546Sopenharmony_ci    * a 16-byte alignment (that is, its `x0` is 16-byte aligned).  Here we
2155bf215546Sopenharmony_ci    * over-allocate the linear buffer to get the proper alignment.
2156bf215546Sopenharmony_ci    */
2157bf215546Sopenharmony_ci   map->buffer = map->ptr = malloc(xfer->layer_stride * box->depth);
2158bf215546Sopenharmony_ci   assert(map->buffer);
2159bf215546Sopenharmony_ci
2160bf215546Sopenharmony_ci   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
2161bf215546Sopenharmony_ci    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
2162bf215546Sopenharmony_ci    * invalidate is set, since we'll be writing the whole rectangle from our
2163bf215546Sopenharmony_ci    * temporary buffer back out.
2164bf215546Sopenharmony_ci    */
2165bf215546Sopenharmony_ci   if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {
2166bf215546Sopenharmony_ci      uint8_t *untiled_s8_map = map->ptr;
2167bf215546Sopenharmony_ci      uint8_t *tiled_s8_map = res->offset +
2168bf215546Sopenharmony_ci         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2169bf215546Sopenharmony_ci
2170bf215546Sopenharmony_ci      for (int s = 0; s < box->depth; s++) {
2171bf215546Sopenharmony_ci         unsigned x0_el, y0_el;
2172bf215546Sopenharmony_ci         get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
2173bf215546Sopenharmony_ci
2174bf215546Sopenharmony_ci         for (uint32_t y = 0; y < box->height; y++) {
2175bf215546Sopenharmony_ci            for (uint32_t x = 0; x < box->width; x++) {
2176bf215546Sopenharmony_ci               ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2177bf215546Sopenharmony_ci                                            x0_el + box->x + x,
2178bf215546Sopenharmony_ci                                            y0_el + box->y + y);
2179bf215546Sopenharmony_ci               untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x] =
2180bf215546Sopenharmony_ci                  tiled_s8_map[offset];
2181bf215546Sopenharmony_ci            }
2182bf215546Sopenharmony_ci         }
2183bf215546Sopenharmony_ci      }
2184bf215546Sopenharmony_ci   }
2185bf215546Sopenharmony_ci
2186bf215546Sopenharmony_ci   map->unmap = iris_unmap_s8;
2187bf215546Sopenharmony_ci}
2188bf215546Sopenharmony_ci
2189bf215546Sopenharmony_ci/* Compute extent parameters for use with tiled_memcpy functions.
2190bf215546Sopenharmony_ci * xs are in units of bytes and ys are in units of strides.
2191bf215546Sopenharmony_ci */
2192bf215546Sopenharmony_cistatic inline void
2193bf215546Sopenharmony_citile_extents(const struct isl_surf *surf,
2194bf215546Sopenharmony_ci             const struct pipe_box *box,
2195bf215546Sopenharmony_ci             unsigned level, int z,
2196bf215546Sopenharmony_ci             unsigned *x1_B, unsigned *x2_B,
2197bf215546Sopenharmony_ci             unsigned *y1_el, unsigned *y2_el)
2198bf215546Sopenharmony_ci{
2199bf215546Sopenharmony_ci   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
2200bf215546Sopenharmony_ci   const unsigned cpp = fmtl->bpb / 8;
2201bf215546Sopenharmony_ci
2202bf215546Sopenharmony_ci   assert(box->x % fmtl->bw == 0);
2203bf215546Sopenharmony_ci   assert(box->y % fmtl->bh == 0);
2204bf215546Sopenharmony_ci
2205bf215546Sopenharmony_ci   unsigned x0_el, y0_el;
2206bf215546Sopenharmony_ci   get_image_offset_el(surf, level, box->z + z, &x0_el, &y0_el);
2207bf215546Sopenharmony_ci
2208bf215546Sopenharmony_ci   *x1_B = (box->x / fmtl->bw + x0_el) * cpp;
2209bf215546Sopenharmony_ci   *y1_el = box->y / fmtl->bh + y0_el;
2210bf215546Sopenharmony_ci   *x2_B = (DIV_ROUND_UP(box->x + box->width, fmtl->bw) + x0_el) * cpp;
2211bf215546Sopenharmony_ci   *y2_el = DIV_ROUND_UP(box->y + box->height, fmtl->bh) + y0_el;
2212bf215546Sopenharmony_ci}
2213bf215546Sopenharmony_ci
2214bf215546Sopenharmony_cistatic void
2215bf215546Sopenharmony_ciiris_unmap_tiled_memcpy(struct iris_transfer *map)
2216bf215546Sopenharmony_ci{
2217bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
2218bf215546Sopenharmony_ci   const struct pipe_box *box = &xfer->box;
2219bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) xfer->resource;
2220bf215546Sopenharmony_ci   struct isl_surf *surf = &res->surf;
2221bf215546Sopenharmony_ci
2222bf215546Sopenharmony_ci   const bool has_swizzling = false;
2223bf215546Sopenharmony_ci
2224bf215546Sopenharmony_ci   if (xfer->usage & PIPE_MAP_WRITE) {
2225bf215546Sopenharmony_ci      char *dst = res->offset +
2226bf215546Sopenharmony_ci         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2227bf215546Sopenharmony_ci
2228bf215546Sopenharmony_ci      for (int s = 0; s < box->depth; s++) {
2229bf215546Sopenharmony_ci         unsigned x1, x2, y1, y2;
2230bf215546Sopenharmony_ci         tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
2231bf215546Sopenharmony_ci
2232bf215546Sopenharmony_ci         void *ptr = map->ptr + s * xfer->layer_stride;
2233bf215546Sopenharmony_ci
2234bf215546Sopenharmony_ci         isl_memcpy_linear_to_tiled(x1, x2, y1, y2, dst, ptr,
2235bf215546Sopenharmony_ci                                    surf->row_pitch_B, xfer->stride,
2236bf215546Sopenharmony_ci                                    has_swizzling, surf->tiling, ISL_MEMCPY);
2237bf215546Sopenharmony_ci      }
2238bf215546Sopenharmony_ci   }
2239bf215546Sopenharmony_ci   os_free_aligned(map->buffer);
2240bf215546Sopenharmony_ci   map->buffer = map->ptr = NULL;
2241bf215546Sopenharmony_ci}
2242bf215546Sopenharmony_ci
2243bf215546Sopenharmony_cistatic void
2244bf215546Sopenharmony_ciiris_map_tiled_memcpy(struct iris_transfer *map)
2245bf215546Sopenharmony_ci{
2246bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
2247bf215546Sopenharmony_ci   const struct pipe_box *box = &xfer->box;
2248bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) xfer->resource;
2249bf215546Sopenharmony_ci   struct isl_surf *surf = &res->surf;
2250bf215546Sopenharmony_ci
2251bf215546Sopenharmony_ci   xfer->stride = ALIGN(surf->row_pitch_B, 16);
2252bf215546Sopenharmony_ci   xfer->layer_stride = xfer->stride * box->height;
2253bf215546Sopenharmony_ci
2254bf215546Sopenharmony_ci   unsigned x1, x2, y1, y2;
2255bf215546Sopenharmony_ci   tile_extents(surf, box, xfer->level, 0, &x1, &x2, &y1, &y2);
2256bf215546Sopenharmony_ci
2257bf215546Sopenharmony_ci   /* The tiling and detiling functions require that the linear buffer has
2258bf215546Sopenharmony_ci    * a 16-byte alignment (that is, its `x0` is 16-byte aligned).  Here we
2259bf215546Sopenharmony_ci    * over-allocate the linear buffer to get the proper alignment.
2260bf215546Sopenharmony_ci    */
2261bf215546Sopenharmony_ci   map->buffer =
2262bf215546Sopenharmony_ci      os_malloc_aligned(xfer->layer_stride * box->depth, 16);
2263bf215546Sopenharmony_ci   assert(map->buffer);
2264bf215546Sopenharmony_ci   map->ptr = (char *)map->buffer + (x1 & 0xf);
2265bf215546Sopenharmony_ci
2266bf215546Sopenharmony_ci   const bool has_swizzling = false;
2267bf215546Sopenharmony_ci
2268bf215546Sopenharmony_ci   if (!(xfer->usage & PIPE_MAP_DISCARD_RANGE)) {
2269bf215546Sopenharmony_ci      char *src = res->offset +
2270bf215546Sopenharmony_ci         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
2271bf215546Sopenharmony_ci
2272bf215546Sopenharmony_ci      for (int s = 0; s < box->depth; s++) {
2273bf215546Sopenharmony_ci         unsigned x1, x2, y1, y2;
2274bf215546Sopenharmony_ci         tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
2275bf215546Sopenharmony_ci
2276bf215546Sopenharmony_ci         /* Use 's' rather than 'box->z' to rebase the first slice to 0. */
2277bf215546Sopenharmony_ci         void *ptr = map->ptr + s * xfer->layer_stride;
2278bf215546Sopenharmony_ci
2279bf215546Sopenharmony_ci         isl_memcpy_tiled_to_linear(x1, x2, y1, y2, ptr, src, xfer->stride,
2280bf215546Sopenharmony_ci                                    surf->row_pitch_B, has_swizzling,
2281bf215546Sopenharmony_ci                                    surf->tiling, ISL_MEMCPY_STREAMING_LOAD);
2282bf215546Sopenharmony_ci      }
2283bf215546Sopenharmony_ci   }
2284bf215546Sopenharmony_ci
2285bf215546Sopenharmony_ci   map->unmap = iris_unmap_tiled_memcpy;
2286bf215546Sopenharmony_ci}
2287bf215546Sopenharmony_ci
2288bf215546Sopenharmony_cistatic void
2289bf215546Sopenharmony_ciiris_map_direct(struct iris_transfer *map)
2290bf215546Sopenharmony_ci{
2291bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
2292bf215546Sopenharmony_ci   struct pipe_box *box = &xfer->box;
2293bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) xfer->resource;
2294bf215546Sopenharmony_ci
2295bf215546Sopenharmony_ci   void *ptr = res->offset +
2296bf215546Sopenharmony_ci      iris_bo_map(map->dbg, res->bo, xfer->usage & MAP_FLAGS);
2297bf215546Sopenharmony_ci
2298bf215546Sopenharmony_ci   if (res->base.b.target == PIPE_BUFFER) {
2299bf215546Sopenharmony_ci      xfer->stride = 0;
2300bf215546Sopenharmony_ci      xfer->layer_stride = 0;
2301bf215546Sopenharmony_ci
2302bf215546Sopenharmony_ci      map->ptr = ptr + box->x;
2303bf215546Sopenharmony_ci   } else {
2304bf215546Sopenharmony_ci      struct isl_surf *surf = &res->surf;
2305bf215546Sopenharmony_ci      const struct isl_format_layout *fmtl =
2306bf215546Sopenharmony_ci         isl_format_get_layout(surf->format);
2307bf215546Sopenharmony_ci      const unsigned cpp = fmtl->bpb / 8;
2308bf215546Sopenharmony_ci      unsigned x0_el, y0_el;
2309bf215546Sopenharmony_ci
2310bf215546Sopenharmony_ci      assert(box->x % fmtl->bw == 0);
2311bf215546Sopenharmony_ci      assert(box->y % fmtl->bh == 0);
2312bf215546Sopenharmony_ci      get_image_offset_el(surf, xfer->level, box->z, &x0_el, &y0_el);
2313bf215546Sopenharmony_ci
2314bf215546Sopenharmony_ci      x0_el += box->x / fmtl->bw;
2315bf215546Sopenharmony_ci      y0_el += box->y / fmtl->bh;
2316bf215546Sopenharmony_ci
2317bf215546Sopenharmony_ci      xfer->stride = isl_surf_get_row_pitch_B(surf);
2318bf215546Sopenharmony_ci      xfer->layer_stride = isl_surf_get_array_pitch(surf);
2319bf215546Sopenharmony_ci
2320bf215546Sopenharmony_ci      map->ptr = ptr + y0_el * xfer->stride + x0_el * cpp;
2321bf215546Sopenharmony_ci   }
2322bf215546Sopenharmony_ci}
2323bf215546Sopenharmony_ci
2324bf215546Sopenharmony_cistatic bool
2325bf215546Sopenharmony_cican_promote_to_async(const struct iris_resource *res,
2326bf215546Sopenharmony_ci                     const struct pipe_box *box,
2327bf215546Sopenharmony_ci                     enum pipe_map_flags usage)
2328bf215546Sopenharmony_ci{
2329bf215546Sopenharmony_ci   /* If we're writing to a section of the buffer that hasn't even been
2330bf215546Sopenharmony_ci    * initialized with useful data, then we can safely promote this write
2331bf215546Sopenharmony_ci    * to be unsynchronized.  This helps the common pattern of appending data.
2332bf215546Sopenharmony_ci    */
2333bf215546Sopenharmony_ci   return res->base.b.target == PIPE_BUFFER && (usage & PIPE_MAP_WRITE) &&
2334bf215546Sopenharmony_ci          !(usage & TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED) &&
2335bf215546Sopenharmony_ci          !util_ranges_intersect(&res->valid_buffer_range, box->x,
2336bf215546Sopenharmony_ci                                 box->x + box->width);
2337bf215546Sopenharmony_ci}
2338bf215546Sopenharmony_ci
2339bf215546Sopenharmony_cistatic void *
2340bf215546Sopenharmony_ciiris_transfer_map(struct pipe_context *ctx,
2341bf215546Sopenharmony_ci                  struct pipe_resource *resource,
2342bf215546Sopenharmony_ci                  unsigned level,
2343bf215546Sopenharmony_ci                  enum pipe_map_flags usage,
2344bf215546Sopenharmony_ci                  const struct pipe_box *box,
2345bf215546Sopenharmony_ci                  struct pipe_transfer **ptransfer)
2346bf215546Sopenharmony_ci{
2347bf215546Sopenharmony_ci   struct iris_context *ice = (struct iris_context *)ctx;
2348bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *)resource;
2349bf215546Sopenharmony_ci   struct isl_surf *surf = &res->surf;
2350bf215546Sopenharmony_ci
2351bf215546Sopenharmony_ci   if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE) {
2352bf215546Sopenharmony_ci      /* Replace the backing storage with a fresh buffer for non-async maps */
2353bf215546Sopenharmony_ci      if (!(usage & (PIPE_MAP_UNSYNCHRONIZED |
2354bf215546Sopenharmony_ci                     TC_TRANSFER_MAP_NO_INVALIDATE)))
2355bf215546Sopenharmony_ci         iris_invalidate_resource(ctx, resource);
2356bf215546Sopenharmony_ci
2357bf215546Sopenharmony_ci      /* If we can discard the whole resource, we can discard the range. */
2358bf215546Sopenharmony_ci      usage |= PIPE_MAP_DISCARD_RANGE;
2359bf215546Sopenharmony_ci   }
2360bf215546Sopenharmony_ci
2361bf215546Sopenharmony_ci   if (!(usage & PIPE_MAP_UNSYNCHRONIZED) &&
2362bf215546Sopenharmony_ci       can_promote_to_async(res, box, usage)) {
2363bf215546Sopenharmony_ci      usage |= PIPE_MAP_UNSYNCHRONIZED;
2364bf215546Sopenharmony_ci   }
2365bf215546Sopenharmony_ci
2366bf215546Sopenharmony_ci   /* Avoid using GPU copies for persistent/coherent buffers, as the idea
2367bf215546Sopenharmony_ci    * there is to access them simultaneously on the CPU & GPU.  This also
2368bf215546Sopenharmony_ci    * avoids trying to use GPU copies for our u_upload_mgr buffers which
2369bf215546Sopenharmony_ci    * contain state we're constructing for a GPU draw call, which would
2370bf215546Sopenharmony_ci    * kill us with infinite stack recursion.
2371bf215546Sopenharmony_ci    */
2372bf215546Sopenharmony_ci   if (usage & (PIPE_MAP_PERSISTENT | PIPE_MAP_COHERENT))
2373bf215546Sopenharmony_ci      usage |= PIPE_MAP_DIRECTLY;
2374bf215546Sopenharmony_ci
2375bf215546Sopenharmony_ci   /* We cannot provide a direct mapping of tiled resources, and we
2376bf215546Sopenharmony_ci    * may not be able to mmap imported BOs since they may come from
2377bf215546Sopenharmony_ci    * other devices that I915_GEM_MMAP cannot work with.
2378bf215546Sopenharmony_ci    */
2379bf215546Sopenharmony_ci   if ((usage & PIPE_MAP_DIRECTLY) &&
2380bf215546Sopenharmony_ci       (surf->tiling != ISL_TILING_LINEAR || iris_bo_is_imported(res->bo)))
2381bf215546Sopenharmony_ci      return NULL;
2382bf215546Sopenharmony_ci
2383bf215546Sopenharmony_ci   bool map_would_stall = false;
2384bf215546Sopenharmony_ci
2385bf215546Sopenharmony_ci   if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
2386bf215546Sopenharmony_ci      map_would_stall =
2387bf215546Sopenharmony_ci         resource_is_busy(ice, res) ||
2388bf215546Sopenharmony_ci         iris_has_invalid_primary(res, level, 1, box->z, box->depth);
2389bf215546Sopenharmony_ci
2390bf215546Sopenharmony_ci      if (map_would_stall && (usage & PIPE_MAP_DONTBLOCK) &&
2391bf215546Sopenharmony_ci                             (usage & PIPE_MAP_DIRECTLY))
2392bf215546Sopenharmony_ci         return NULL;
2393bf215546Sopenharmony_ci   }
2394bf215546Sopenharmony_ci
2395bf215546Sopenharmony_ci   struct iris_transfer *map;
2396bf215546Sopenharmony_ci
2397bf215546Sopenharmony_ci   if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
2398bf215546Sopenharmony_ci      map = slab_zalloc(&ice->transfer_pool_unsync);
2399bf215546Sopenharmony_ci   else
2400bf215546Sopenharmony_ci      map = slab_zalloc(&ice->transfer_pool);
2401bf215546Sopenharmony_ci
2402bf215546Sopenharmony_ci   if (!map)
2403bf215546Sopenharmony_ci      return NULL;
2404bf215546Sopenharmony_ci
2405bf215546Sopenharmony_ci   struct pipe_transfer *xfer = &map->base.b;
2406bf215546Sopenharmony_ci
2407bf215546Sopenharmony_ci   map->dbg = &ice->dbg;
2408bf215546Sopenharmony_ci
2409bf215546Sopenharmony_ci   pipe_resource_reference(&xfer->resource, resource);
2410bf215546Sopenharmony_ci   xfer->level = level;
2411bf215546Sopenharmony_ci   xfer->usage = usage;
2412bf215546Sopenharmony_ci   xfer->box = *box;
2413bf215546Sopenharmony_ci   *ptransfer = xfer;
2414bf215546Sopenharmony_ci
2415bf215546Sopenharmony_ci   map->dest_had_defined_contents =
2416bf215546Sopenharmony_ci      util_ranges_intersect(&res->valid_buffer_range, box->x,
2417bf215546Sopenharmony_ci                            box->x + box->width);
2418bf215546Sopenharmony_ci
2419bf215546Sopenharmony_ci   if (usage & PIPE_MAP_WRITE)
2420bf215546Sopenharmony_ci      util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
2421bf215546Sopenharmony_ci
2422bf215546Sopenharmony_ci   if (iris_bo_mmap_mode(res->bo) != IRIS_MMAP_NONE) {
2423bf215546Sopenharmony_ci      /* GPU copies are not useful for buffer reads.  Instead of stalling to
2424bf215546Sopenharmony_ci       * read from the original buffer, we'd simply copy it to a temporary...
2425bf215546Sopenharmony_ci       * then stall (a bit longer) to read from that buffer.
2426bf215546Sopenharmony_ci       *
2427bf215546Sopenharmony_ci       * Images are less clear-cut.  Resolves can be destructive, removing
2428bf215546Sopenharmony_ci       * some of the underlying compression, so we'd rather blit the data to
2429bf215546Sopenharmony_ci       * a linear temporary and map that, to avoid the resolve.
2430bf215546Sopenharmony_ci       */
2431bf215546Sopenharmony_ci      if (!(usage & PIPE_MAP_DISCARD_RANGE) &&
2432bf215546Sopenharmony_ci          !iris_has_invalid_primary(res, level, 1, box->z, box->depth)) {
2433bf215546Sopenharmony_ci         usage |= PIPE_MAP_DIRECTLY;
2434bf215546Sopenharmony_ci      }
2435bf215546Sopenharmony_ci
2436bf215546Sopenharmony_ci      /* We can map directly if it wouldn't stall, there's no compression,
2437bf215546Sopenharmony_ci       * and we aren't doing an uncached read.
2438bf215546Sopenharmony_ci       */
2439bf215546Sopenharmony_ci      if (!map_would_stall &&
2440bf215546Sopenharmony_ci          !isl_aux_usage_has_compression(res->aux.usage) &&
2441bf215546Sopenharmony_ci          !((usage & PIPE_MAP_READ) &&
2442bf215546Sopenharmony_ci            iris_bo_mmap_mode(res->bo) != IRIS_MMAP_WB)) {
2443bf215546Sopenharmony_ci         usage |= PIPE_MAP_DIRECTLY;
2444bf215546Sopenharmony_ci      }
2445bf215546Sopenharmony_ci   }
2446bf215546Sopenharmony_ci
2447bf215546Sopenharmony_ci   /* TODO: Teach iris_map_tiled_memcpy about Tile4... */
2448bf215546Sopenharmony_ci   if (res->surf.tiling == ISL_TILING_4)
2449bf215546Sopenharmony_ci      usage &= ~PIPE_MAP_DIRECTLY;
2450bf215546Sopenharmony_ci
2451bf215546Sopenharmony_ci   if (!(usage & PIPE_MAP_DIRECTLY)) {
2452bf215546Sopenharmony_ci      /* If we need a synchronous mapping and the resource is busy, or needs
2453bf215546Sopenharmony_ci       * resolving, we copy to/from a linear temporary buffer using the GPU.
2454bf215546Sopenharmony_ci       */
2455bf215546Sopenharmony_ci      map->batch = &ice->batches[IRIS_BATCH_RENDER];
2456bf215546Sopenharmony_ci      map->blorp = &ice->blorp;
2457bf215546Sopenharmony_ci      iris_map_copy_region(map);
2458bf215546Sopenharmony_ci   } else {
2459bf215546Sopenharmony_ci      /* Otherwise we're free to map on the CPU. */
2460bf215546Sopenharmony_ci
2461bf215546Sopenharmony_ci      if (resource->target != PIPE_BUFFER) {
2462bf215546Sopenharmony_ci         iris_resource_access_raw(ice, res, level, box->z, box->depth,
2463bf215546Sopenharmony_ci                                  usage & PIPE_MAP_WRITE);
2464bf215546Sopenharmony_ci      }
2465bf215546Sopenharmony_ci
2466bf215546Sopenharmony_ci      if (!(usage & PIPE_MAP_UNSYNCHRONIZED)) {
2467bf215546Sopenharmony_ci         iris_foreach_batch(ice, batch) {
2468bf215546Sopenharmony_ci            if (iris_batch_references(batch, res->bo))
2469bf215546Sopenharmony_ci               iris_batch_flush(batch);
2470bf215546Sopenharmony_ci         }
2471bf215546Sopenharmony_ci      }
2472bf215546Sopenharmony_ci
2473bf215546Sopenharmony_ci      if (surf->tiling == ISL_TILING_W) {
2474bf215546Sopenharmony_ci         /* TODO: Teach iris_map_tiled_memcpy about W-tiling... */
2475bf215546Sopenharmony_ci         iris_map_s8(map);
2476bf215546Sopenharmony_ci      } else if (surf->tiling != ISL_TILING_LINEAR) {
2477bf215546Sopenharmony_ci         iris_map_tiled_memcpy(map);
2478bf215546Sopenharmony_ci      } else {
2479bf215546Sopenharmony_ci         iris_map_direct(map);
2480bf215546Sopenharmony_ci      }
2481bf215546Sopenharmony_ci   }
2482bf215546Sopenharmony_ci
2483bf215546Sopenharmony_ci   return map->ptr;
2484bf215546Sopenharmony_ci}
2485bf215546Sopenharmony_ci
2486bf215546Sopenharmony_cistatic void
2487bf215546Sopenharmony_ciiris_transfer_flush_region(struct pipe_context *ctx,
2488bf215546Sopenharmony_ci                           struct pipe_transfer *xfer,
2489bf215546Sopenharmony_ci                           const struct pipe_box *box)
2490bf215546Sopenharmony_ci{
2491bf215546Sopenharmony_ci   struct iris_context *ice = (struct iris_context *)ctx;
2492bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *) xfer->resource;
2493bf215546Sopenharmony_ci   struct iris_transfer *map = (void *) xfer;
2494bf215546Sopenharmony_ci
2495bf215546Sopenharmony_ci   if (map->staging)
2496bf215546Sopenharmony_ci      iris_flush_staging_region(xfer, box);
2497bf215546Sopenharmony_ci
2498bf215546Sopenharmony_ci   if (res->base.b.target == PIPE_BUFFER) {
2499bf215546Sopenharmony_ci      util_range_add(&res->base.b, &res->valid_buffer_range, box->x, box->x + box->width);
2500bf215546Sopenharmony_ci   }
2501bf215546Sopenharmony_ci
2502bf215546Sopenharmony_ci   /* Make sure we flag constants dirty even if there's no need to emit
2503bf215546Sopenharmony_ci    * any PIPE_CONTROLs to a batch.
2504bf215546Sopenharmony_ci    */
2505bf215546Sopenharmony_ci   iris_dirty_for_history(ice, res);
2506bf215546Sopenharmony_ci}
2507bf215546Sopenharmony_ci
2508bf215546Sopenharmony_cistatic void
2509bf215546Sopenharmony_ciiris_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *xfer)
2510bf215546Sopenharmony_ci{
2511bf215546Sopenharmony_ci   struct iris_context *ice = (struct iris_context *)ctx;
2512bf215546Sopenharmony_ci   struct iris_transfer *map = (void *) xfer;
2513bf215546Sopenharmony_ci
2514bf215546Sopenharmony_ci   if (!(xfer->usage & (PIPE_MAP_FLUSH_EXPLICIT |
2515bf215546Sopenharmony_ci                        PIPE_MAP_COHERENT))) {
2516bf215546Sopenharmony_ci      struct pipe_box flush_box = {
2517bf215546Sopenharmony_ci         .x = 0, .y = 0, .z = 0,
2518bf215546Sopenharmony_ci         .width  = xfer->box.width,
2519bf215546Sopenharmony_ci         .height = xfer->box.height,
2520bf215546Sopenharmony_ci         .depth  = xfer->box.depth,
2521bf215546Sopenharmony_ci      };
2522bf215546Sopenharmony_ci      iris_transfer_flush_region(ctx, xfer, &flush_box);
2523bf215546Sopenharmony_ci   }
2524bf215546Sopenharmony_ci
2525bf215546Sopenharmony_ci   if (map->unmap)
2526bf215546Sopenharmony_ci      map->unmap(map);
2527bf215546Sopenharmony_ci
2528bf215546Sopenharmony_ci   pipe_resource_reference(&xfer->resource, NULL);
2529bf215546Sopenharmony_ci
2530bf215546Sopenharmony_ci   /* transfer_unmap is always called from the driver thread, so we have to
2531bf215546Sopenharmony_ci    * use transfer_pool, not transfer_pool_unsync.  Freeing an object into a
2532bf215546Sopenharmony_ci    * different pool is allowed, however.
2533bf215546Sopenharmony_ci    */
2534bf215546Sopenharmony_ci   slab_free(&ice->transfer_pool, map);
2535bf215546Sopenharmony_ci}
2536bf215546Sopenharmony_ci
2537bf215546Sopenharmony_ci/**
2538bf215546Sopenharmony_ci * The pipe->texture_subdata() driver hook.
2539bf215546Sopenharmony_ci *
2540bf215546Sopenharmony_ci * Mesa's state tracker takes this path whenever possible, even with
2541bf215546Sopenharmony_ci * PIPE_CAP_TEXTURE_TRANSFER_MODES set.
2542bf215546Sopenharmony_ci */
2543bf215546Sopenharmony_cistatic void
2544bf215546Sopenharmony_ciiris_texture_subdata(struct pipe_context *ctx,
2545bf215546Sopenharmony_ci                     struct pipe_resource *resource,
2546bf215546Sopenharmony_ci                     unsigned level,
2547bf215546Sopenharmony_ci                     unsigned usage,
2548bf215546Sopenharmony_ci                     const struct pipe_box *box,
2549bf215546Sopenharmony_ci                     const void *data,
2550bf215546Sopenharmony_ci                     unsigned stride,
2551bf215546Sopenharmony_ci                     unsigned layer_stride)
2552bf215546Sopenharmony_ci{
2553bf215546Sopenharmony_ci   struct iris_context *ice = (struct iris_context *)ctx;
2554bf215546Sopenharmony_ci   struct iris_resource *res = (struct iris_resource *)resource;
2555bf215546Sopenharmony_ci   const struct isl_surf *surf = &res->surf;
2556bf215546Sopenharmony_ci
2557bf215546Sopenharmony_ci   assert(resource->target != PIPE_BUFFER);
2558bf215546Sopenharmony_ci
2559bf215546Sopenharmony_ci   /* Just use the transfer-based path for linear buffers - it will already
2560bf215546Sopenharmony_ci    * do a direct mapping, or a simple linear staging buffer.
2561bf215546Sopenharmony_ci    *
2562bf215546Sopenharmony_ci    * Linear staging buffers appear to be better than tiled ones, too, so
2563bf215546Sopenharmony_ci    * take that path if we need the GPU to perform color compression, or
2564bf215546Sopenharmony_ci    * stall-avoidance blits.
2565bf215546Sopenharmony_ci    *
2566bf215546Sopenharmony_ci    * TODO: Teach isl_memcpy_linear_to_tiled about Tile4...
2567bf215546Sopenharmony_ci    */
2568bf215546Sopenharmony_ci   if (surf->tiling == ISL_TILING_LINEAR ||
2569bf215546Sopenharmony_ci       surf->tiling == ISL_TILING_4 ||
2570bf215546Sopenharmony_ci       isl_aux_usage_has_compression(res->aux.usage) ||
2571bf215546Sopenharmony_ci       resource_is_busy(ice, res) ||
2572bf215546Sopenharmony_ci       iris_bo_mmap_mode(res->bo) == IRIS_MMAP_NONE) {
2573bf215546Sopenharmony_ci      return u_default_texture_subdata(ctx, resource, level, usage, box,
2574bf215546Sopenharmony_ci                                       data, stride, layer_stride);
2575bf215546Sopenharmony_ci   }
2576bf215546Sopenharmony_ci
2577bf215546Sopenharmony_ci   /* No state trackers pass any flags other than PIPE_MAP_WRITE */
2578bf215546Sopenharmony_ci
2579bf215546Sopenharmony_ci   iris_resource_access_raw(ice, res, level, box->z, box->depth, true);
2580bf215546Sopenharmony_ci
2581bf215546Sopenharmony_ci   iris_foreach_batch(ice, batch) {
2582bf215546Sopenharmony_ci      if (iris_batch_references(batch, res->bo))
2583bf215546Sopenharmony_ci         iris_batch_flush(batch);
2584bf215546Sopenharmony_ci   }
2585bf215546Sopenharmony_ci
2586bf215546Sopenharmony_ci   uint8_t *dst = iris_bo_map(&ice->dbg, res->bo, MAP_WRITE | MAP_RAW);
2587bf215546Sopenharmony_ci
2588bf215546Sopenharmony_ci   for (int s = 0; s < box->depth; s++) {
2589bf215546Sopenharmony_ci      const uint8_t *src = data + s * layer_stride;
2590bf215546Sopenharmony_ci
2591bf215546Sopenharmony_ci      if (surf->tiling == ISL_TILING_W) {
2592bf215546Sopenharmony_ci         unsigned x0_el, y0_el;
2593bf215546Sopenharmony_ci         get_image_offset_el(surf, level, box->z + s, &x0_el, &y0_el);
2594bf215546Sopenharmony_ci
2595bf215546Sopenharmony_ci         for (unsigned y = 0; y < box->height; y++) {
2596bf215546Sopenharmony_ci            for (unsigned x = 0; x < box->width; x++) {
2597bf215546Sopenharmony_ci               ptrdiff_t offset = s8_offset(surf->row_pitch_B,
2598bf215546Sopenharmony_ci                                            x0_el + box->x + x,
2599bf215546Sopenharmony_ci                                            y0_el + box->y + y);
2600bf215546Sopenharmony_ci               dst[offset] = src[y * stride + x];
2601bf215546Sopenharmony_ci            }
2602bf215546Sopenharmony_ci         }
2603bf215546Sopenharmony_ci      } else {
2604bf215546Sopenharmony_ci         unsigned x1, x2, y1, y2;
2605bf215546Sopenharmony_ci
2606bf215546Sopenharmony_ci         tile_extents(surf, box, level, s, &x1, &x2, &y1, &y2);
2607bf215546Sopenharmony_ci
2608bf215546Sopenharmony_ci         isl_memcpy_linear_to_tiled(x1, x2, y1, y2,
2609bf215546Sopenharmony_ci                                    (void *)dst, (void *)src,
2610bf215546Sopenharmony_ci                                    surf->row_pitch_B, stride,
2611bf215546Sopenharmony_ci                                    false, surf->tiling, ISL_MEMCPY);
2612bf215546Sopenharmony_ci      }
2613bf215546Sopenharmony_ci   }
2614bf215546Sopenharmony_ci}
2615bf215546Sopenharmony_ci
2616bf215546Sopenharmony_ci/**
2617bf215546Sopenharmony_ci * Mark state dirty that needs to be re-emitted when a resource is written.
2618bf215546Sopenharmony_ci */
2619bf215546Sopenharmony_civoid
2620bf215546Sopenharmony_ciiris_dirty_for_history(struct iris_context *ice,
2621bf215546Sopenharmony_ci                       struct iris_resource *res)
2622bf215546Sopenharmony_ci{
2623bf215546Sopenharmony_ci   const uint64_t stages = res->bind_stages;
2624bf215546Sopenharmony_ci   uint64_t dirty = 0ull;
2625bf215546Sopenharmony_ci   uint64_t stage_dirty = 0ull;
2626bf215546Sopenharmony_ci
2627bf215546Sopenharmony_ci   if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
2628bf215546Sopenharmony_ci      for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
2629bf215546Sopenharmony_ci         if (stages & (1u << stage)) {
2630bf215546Sopenharmony_ci            struct iris_shader_state *shs = &ice->state.shaders[stage];
2631bf215546Sopenharmony_ci            shs->dirty_cbufs |= ~0u;
2632bf215546Sopenharmony_ci         }
2633bf215546Sopenharmony_ci      }
2634bf215546Sopenharmony_ci      dirty |= IRIS_DIRTY_RENDER_MISC_BUFFER_FLUSHES |
2635bf215546Sopenharmony_ci               IRIS_DIRTY_COMPUTE_MISC_BUFFER_FLUSHES;
2636bf215546Sopenharmony_ci      stage_dirty |= (stages << IRIS_SHIFT_FOR_STAGE_DIRTY_CONSTANTS);
2637bf215546Sopenharmony_ci   }
2638bf215546Sopenharmony_ci
2639bf215546Sopenharmony_ci   if (res->bind_history & (PIPE_BIND_SAMPLER_VIEW |
2640bf215546Sopenharmony_ci                            PIPE_BIND_SHADER_IMAGE)) {
2641bf215546Sopenharmony_ci      dirty |= IRIS_DIRTY_RENDER_RESOLVES_AND_FLUSHES |
2642bf215546Sopenharmony_ci               IRIS_DIRTY_COMPUTE_RESOLVES_AND_FLUSHES;
2643bf215546Sopenharmony_ci      stage_dirty |= (stages << IRIS_SHIFT_FOR_STAGE_DIRTY_BINDINGS);
2644bf215546Sopenharmony_ci   }
2645bf215546Sopenharmony_ci
2646bf215546Sopenharmony_ci   if (res->bind_history & PIPE_BIND_SHADER_BUFFER) {
2647bf215546Sopenharmony_ci      dirty |= IRIS_DIRTY_RENDER_MISC_BUFFER_FLUSHES |
2648bf215546Sopenharmony_ci               IRIS_DIRTY_COMPUTE_MISC_BUFFER_FLUSHES;
2649bf215546Sopenharmony_ci      stage_dirty |= (stages << IRIS_SHIFT_FOR_STAGE_DIRTY_BINDINGS);
2650bf215546Sopenharmony_ci   }
2651bf215546Sopenharmony_ci
2652bf215546Sopenharmony_ci   if (res->bind_history & PIPE_BIND_VERTEX_BUFFER)
2653bf215546Sopenharmony_ci      dirty |= IRIS_DIRTY_VERTEX_BUFFER_FLUSHES;
2654bf215546Sopenharmony_ci
2655bf215546Sopenharmony_ci   if (ice->state.streamout_active && (res->bind_history & PIPE_BIND_STREAM_OUTPUT))
2656bf215546Sopenharmony_ci      dirty |= IRIS_DIRTY_SO_BUFFERS;
2657bf215546Sopenharmony_ci
2658bf215546Sopenharmony_ci   ice->state.dirty |= dirty;
2659bf215546Sopenharmony_ci   ice->state.stage_dirty |= stage_dirty;
2660bf215546Sopenharmony_ci}
2661bf215546Sopenharmony_ci
2662bf215546Sopenharmony_cibool
2663bf215546Sopenharmony_ciiris_resource_set_clear_color(struct iris_context *ice,
2664bf215546Sopenharmony_ci                              struct iris_resource *res,
2665bf215546Sopenharmony_ci                              union isl_color_value color)
2666bf215546Sopenharmony_ci{
2667bf215546Sopenharmony_ci   if (res->aux.clear_color_unknown ||
2668bf215546Sopenharmony_ci       memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0) {
2669bf215546Sopenharmony_ci      res->aux.clear_color = color;
2670bf215546Sopenharmony_ci      res->aux.clear_color_unknown = false;
2671bf215546Sopenharmony_ci      return true;
2672bf215546Sopenharmony_ci   }
2673bf215546Sopenharmony_ci
2674bf215546Sopenharmony_ci   return false;
2675bf215546Sopenharmony_ci}
2676bf215546Sopenharmony_ci
2677bf215546Sopenharmony_cistatic enum pipe_format
2678bf215546Sopenharmony_ciiris_resource_get_internal_format(struct pipe_resource *p_res)
2679bf215546Sopenharmony_ci{
2680bf215546Sopenharmony_ci   struct iris_resource *res = (void *) p_res;
2681bf215546Sopenharmony_ci   return res->internal_format;
2682bf215546Sopenharmony_ci}
2683bf215546Sopenharmony_ci
2684bf215546Sopenharmony_cistatic const struct u_transfer_vtbl transfer_vtbl = {
2685bf215546Sopenharmony_ci   .resource_create       = iris_resource_create,
2686bf215546Sopenharmony_ci   .resource_destroy      = iris_resource_destroy,
2687bf215546Sopenharmony_ci   .transfer_map          = iris_transfer_map,
2688bf215546Sopenharmony_ci   .transfer_unmap        = iris_transfer_unmap,
2689bf215546Sopenharmony_ci   .transfer_flush_region = iris_transfer_flush_region,
2690bf215546Sopenharmony_ci   .get_internal_format   = iris_resource_get_internal_format,
2691bf215546Sopenharmony_ci   .set_stencil           = iris_resource_set_separate_stencil,
2692bf215546Sopenharmony_ci   .get_stencil           = iris_resource_get_separate_stencil,
2693bf215546Sopenharmony_ci};
2694bf215546Sopenharmony_ci
2695bf215546Sopenharmony_civoid
2696bf215546Sopenharmony_ciiris_init_screen_resource_functions(struct pipe_screen *pscreen)
2697bf215546Sopenharmony_ci{
2698bf215546Sopenharmony_ci   pscreen->query_dmabuf_modifiers = iris_query_dmabuf_modifiers;
2699bf215546Sopenharmony_ci   pscreen->is_dmabuf_modifier_supported = iris_is_dmabuf_modifier_supported;
2700bf215546Sopenharmony_ci   pscreen->get_dmabuf_modifier_planes = iris_get_dmabuf_modifier_planes;
2701bf215546Sopenharmony_ci   pscreen->resource_create_with_modifiers =
2702bf215546Sopenharmony_ci      iris_resource_create_with_modifiers;
2703bf215546Sopenharmony_ci   pscreen->resource_create = u_transfer_helper_resource_create;
2704bf215546Sopenharmony_ci   pscreen->resource_from_user_memory = iris_resource_from_user_memory;
2705bf215546Sopenharmony_ci   pscreen->resource_from_handle = iris_resource_from_handle;
2706bf215546Sopenharmony_ci   pscreen->resource_from_memobj = iris_resource_from_memobj_wrapper;
2707bf215546Sopenharmony_ci   pscreen->resource_get_handle = iris_resource_get_handle;
2708bf215546Sopenharmony_ci   pscreen->resource_get_param = iris_resource_get_param;
2709bf215546Sopenharmony_ci   pscreen->resource_destroy = u_transfer_helper_resource_destroy;
2710bf215546Sopenharmony_ci   pscreen->memobj_create_from_handle = iris_memobj_create_from_handle;
2711bf215546Sopenharmony_ci   pscreen->memobj_destroy = iris_memobj_destroy;
2712bf215546Sopenharmony_ci   pscreen->transfer_helper =
2713bf215546Sopenharmony_ci      u_transfer_helper_create(&transfer_vtbl, true, true, false, true, false);
2714bf215546Sopenharmony_ci}
2715bf215546Sopenharmony_ci
2716bf215546Sopenharmony_civoid
2717bf215546Sopenharmony_ciiris_init_resource_functions(struct pipe_context *ctx)
2718bf215546Sopenharmony_ci{
2719bf215546Sopenharmony_ci   ctx->flush_resource = iris_flush_resource;
2720bf215546Sopenharmony_ci   ctx->invalidate_resource = iris_invalidate_resource;
2721bf215546Sopenharmony_ci   ctx->buffer_map = u_transfer_helper_transfer_map;
2722bf215546Sopenharmony_ci   ctx->texture_map = u_transfer_helper_transfer_map;
2723bf215546Sopenharmony_ci   ctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
2724bf215546Sopenharmony_ci   ctx->buffer_unmap = u_transfer_helper_transfer_unmap;
2725bf215546Sopenharmony_ci   ctx->texture_unmap = u_transfer_helper_transfer_unmap;
2726bf215546Sopenharmony_ci   ctx->buffer_subdata = u_default_buffer_subdata;
2727bf215546Sopenharmony_ci   ctx->clear_buffer = u_default_clear_buffer;
2728bf215546Sopenharmony_ci   ctx->texture_subdata = iris_texture_subdata;
2729bf215546Sopenharmony_ci}
2730