1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2017 Intel Corporation
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice shall be included
12b8e80941Smrg * in all copies or substantial portions of the Software.
13b8e80941Smrg *
14b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20b8e80941Smrg * DEALINGS IN THE SOFTWARE.
21b8e80941Smrg */
22b8e80941Smrg
23b8e80941Smrg/**
24b8e80941Smrg * @file iris_resource.c
25b8e80941Smrg *
26b8e80941Smrg * Resources are images, buffers, and other objects used by the GPU.
27b8e80941Smrg *
28b8e80941Smrg * XXX: explain resources
29b8e80941Smrg */
30b8e80941Smrg
31b8e80941Smrg#include <stdio.h>
32b8e80941Smrg#include <errno.h>
33b8e80941Smrg#include "pipe/p_defines.h"
34b8e80941Smrg#include "pipe/p_state.h"
35b8e80941Smrg#include "pipe/p_context.h"
36b8e80941Smrg#include "pipe/p_screen.h"
37b8e80941Smrg#include "util/os_memory.h"
38b8e80941Smrg#include "util/u_cpu_detect.h"
39b8e80941Smrg#include "util/u_inlines.h"
40b8e80941Smrg#include "util/u_format.h"
41b8e80941Smrg#include "util/u_threaded_context.h"
42b8e80941Smrg#include "util/u_transfer.h"
43b8e80941Smrg#include "util/u_transfer_helper.h"
44b8e80941Smrg#include "util/u_upload_mgr.h"
45b8e80941Smrg#include "util/ralloc.h"
46b8e80941Smrg#include "iris_batch.h"
47b8e80941Smrg#include "iris_context.h"
48b8e80941Smrg#include "iris_resource.h"
49b8e80941Smrg#include "iris_screen.h"
50b8e80941Smrg#include "intel/dev/gen_debug.h"
51b8e80941Smrg#include "isl/isl.h"
52b8e80941Smrg#include "drm-uapi/drm_fourcc.h"
53b8e80941Smrg#include "drm-uapi/i915_drm.h"
54b8e80941Smrg
55b8e80941Smrgenum modifier_priority {
56b8e80941Smrg   MODIFIER_PRIORITY_INVALID = 0,
57b8e80941Smrg   MODIFIER_PRIORITY_LINEAR,
58b8e80941Smrg   MODIFIER_PRIORITY_X,
59b8e80941Smrg   MODIFIER_PRIORITY_Y,
60b8e80941Smrg   MODIFIER_PRIORITY_Y_CCS,
61b8e80941Smrg};
62b8e80941Smrg
63b8e80941Smrgstatic const uint64_t priority_to_modifier[] = {
64b8e80941Smrg   [MODIFIER_PRIORITY_INVALID] = DRM_FORMAT_MOD_INVALID,
65b8e80941Smrg   [MODIFIER_PRIORITY_LINEAR] = DRM_FORMAT_MOD_LINEAR,
66b8e80941Smrg   [MODIFIER_PRIORITY_X] = I915_FORMAT_MOD_X_TILED,
67b8e80941Smrg   [MODIFIER_PRIORITY_Y] = I915_FORMAT_MOD_Y_TILED,
68b8e80941Smrg   [MODIFIER_PRIORITY_Y_CCS] = I915_FORMAT_MOD_Y_TILED_CCS,
69b8e80941Smrg};
70b8e80941Smrg
71b8e80941Smrgstatic bool
72b8e80941Smrgmodifier_is_supported(const struct gen_device_info *devinfo,
73b8e80941Smrg                      uint64_t modifier)
74b8e80941Smrg{
75b8e80941Smrg   /* XXX: do something real */
76b8e80941Smrg   switch (modifier) {
77b8e80941Smrg   case I915_FORMAT_MOD_Y_TILED:
78b8e80941Smrg   case I915_FORMAT_MOD_X_TILED:
79b8e80941Smrg   case DRM_FORMAT_MOD_LINEAR:
80b8e80941Smrg      return true;
81b8e80941Smrg   case I915_FORMAT_MOD_Y_TILED_CCS:
82b8e80941Smrg   case DRM_FORMAT_MOD_INVALID:
83b8e80941Smrg   default:
84b8e80941Smrg      return false;
85b8e80941Smrg   }
86b8e80941Smrg}
87b8e80941Smrg
88b8e80941Smrgstatic uint64_t
89b8e80941Smrgselect_best_modifier(struct gen_device_info *devinfo,
90b8e80941Smrg                     const uint64_t *modifiers,
91b8e80941Smrg                     int count)
92b8e80941Smrg{
93b8e80941Smrg   enum modifier_priority prio = MODIFIER_PRIORITY_INVALID;
94b8e80941Smrg
95b8e80941Smrg   for (int i = 0; i < count; i++) {
96b8e80941Smrg      if (!modifier_is_supported(devinfo, modifiers[i]))
97b8e80941Smrg         continue;
98b8e80941Smrg
99b8e80941Smrg      switch (modifiers[i]) {
100b8e80941Smrg      case I915_FORMAT_MOD_Y_TILED_CCS:
101b8e80941Smrg         prio = MAX2(prio, MODIFIER_PRIORITY_Y_CCS);
102b8e80941Smrg         break;
103b8e80941Smrg      case I915_FORMAT_MOD_Y_TILED:
104b8e80941Smrg         prio = MAX2(prio, MODIFIER_PRIORITY_Y);
105b8e80941Smrg         break;
106b8e80941Smrg      case I915_FORMAT_MOD_X_TILED:
107b8e80941Smrg         prio = MAX2(prio, MODIFIER_PRIORITY_X);
108b8e80941Smrg         break;
109b8e80941Smrg      case DRM_FORMAT_MOD_LINEAR:
110b8e80941Smrg         prio = MAX2(prio, MODIFIER_PRIORITY_LINEAR);
111b8e80941Smrg         break;
112b8e80941Smrg      case DRM_FORMAT_MOD_INVALID:
113b8e80941Smrg      default:
114b8e80941Smrg         break;
115b8e80941Smrg      }
116b8e80941Smrg   }
117b8e80941Smrg
118b8e80941Smrg   return priority_to_modifier[prio];
119b8e80941Smrg}
120b8e80941Smrg
121b8e80941Smrgstatic enum isl_surf_dim
122b8e80941Smrgtarget_to_isl_surf_dim(enum pipe_texture_target target)
123b8e80941Smrg{
124b8e80941Smrg   switch (target) {
125b8e80941Smrg   case PIPE_BUFFER:
126b8e80941Smrg   case PIPE_TEXTURE_1D:
127b8e80941Smrg   case PIPE_TEXTURE_1D_ARRAY:
128b8e80941Smrg      return ISL_SURF_DIM_1D;
129b8e80941Smrg   case PIPE_TEXTURE_2D:
130b8e80941Smrg   case PIPE_TEXTURE_CUBE:
131b8e80941Smrg   case PIPE_TEXTURE_RECT:
132b8e80941Smrg   case PIPE_TEXTURE_2D_ARRAY:
133b8e80941Smrg   case PIPE_TEXTURE_CUBE_ARRAY:
134b8e80941Smrg      return ISL_SURF_DIM_2D;
135b8e80941Smrg   case PIPE_TEXTURE_3D:
136b8e80941Smrg      return ISL_SURF_DIM_3D;
137b8e80941Smrg   case PIPE_MAX_TEXTURE_TYPES:
138b8e80941Smrg      break;
139b8e80941Smrg   }
140b8e80941Smrg   unreachable("invalid texture type");
141b8e80941Smrg}
142b8e80941Smrg
143b8e80941Smrgstatic void
144b8e80941Smrgiris_query_dmabuf_modifiers(struct pipe_screen *pscreen,
145b8e80941Smrg                            enum pipe_format pfmt,
146b8e80941Smrg                            int max,
147b8e80941Smrg                            uint64_t *modifiers,
148b8e80941Smrg                            unsigned int *external_only,
149b8e80941Smrg                            int *count)
150b8e80941Smrg{
151b8e80941Smrg   struct iris_screen *screen = (void *) pscreen;
152b8e80941Smrg   const struct gen_device_info *devinfo = &screen->devinfo;
153b8e80941Smrg
154b8e80941Smrg   uint64_t all_modifiers[] = {
155b8e80941Smrg      DRM_FORMAT_MOD_LINEAR,
156b8e80941Smrg      I915_FORMAT_MOD_X_TILED,
157b8e80941Smrg      I915_FORMAT_MOD_Y_TILED,
158b8e80941Smrg      // XXX: (broken) I915_FORMAT_MOD_Y_TILED_CCS,
159b8e80941Smrg   };
160b8e80941Smrg
161b8e80941Smrg   int supported_mods = 0;
162b8e80941Smrg
163b8e80941Smrg   for (int i = 0; i < ARRAY_SIZE(all_modifiers); i++) {
164b8e80941Smrg      if (!modifier_is_supported(devinfo, all_modifiers[i]))
165b8e80941Smrg         continue;
166b8e80941Smrg
167b8e80941Smrg      if (supported_mods < max) {
168b8e80941Smrg         if (modifiers)
169b8e80941Smrg            modifiers[supported_mods] = all_modifiers[i];
170b8e80941Smrg
171b8e80941Smrg         if (external_only)
172b8e80941Smrg            external_only[supported_mods] = util_format_is_yuv(pfmt);
173b8e80941Smrg      }
174b8e80941Smrg
175b8e80941Smrg      supported_mods++;
176b8e80941Smrg   }
177b8e80941Smrg
178b8e80941Smrg   *count = supported_mods;
179b8e80941Smrg}
180b8e80941Smrg
181b8e80941Smrgstatic isl_surf_usage_flags_t
182b8e80941Smrgpipe_bind_to_isl_usage(unsigned bindings)
183b8e80941Smrg{
184b8e80941Smrg   isl_surf_usage_flags_t usage = 0;
185b8e80941Smrg
186b8e80941Smrg   if (bindings & PIPE_BIND_RENDER_TARGET)
187b8e80941Smrg      usage |= ISL_SURF_USAGE_RENDER_TARGET_BIT;
188b8e80941Smrg
189b8e80941Smrg   if (bindings & PIPE_BIND_SAMPLER_VIEW)
190b8e80941Smrg      usage |= ISL_SURF_USAGE_TEXTURE_BIT;
191b8e80941Smrg
192b8e80941Smrg   if (bindings & (PIPE_BIND_SHADER_IMAGE | PIPE_BIND_SHADER_BUFFER))
193b8e80941Smrg      usage |= ISL_SURF_USAGE_STORAGE_BIT;
194b8e80941Smrg
195b8e80941Smrg   if (bindings & PIPE_BIND_DISPLAY_TARGET)
196b8e80941Smrg      usage |= ISL_SURF_USAGE_DISPLAY_BIT;
197b8e80941Smrg
198b8e80941Smrg   return usage;
199b8e80941Smrg}
200b8e80941Smrg
201b8e80941Smrgstruct pipe_resource *
202b8e80941Smrgiris_resource_get_separate_stencil(struct pipe_resource *p_res)
203b8e80941Smrg{
204b8e80941Smrg   /* For packed depth-stencil, we treat depth as the primary resource
205b8e80941Smrg    * and store S8 as the "second plane" resource.
206b8e80941Smrg    */
207b8e80941Smrg   return p_res->next;
208b8e80941Smrg}
209b8e80941Smrg
210b8e80941Smrgstatic void
211b8e80941Smrgiris_resource_set_separate_stencil(struct pipe_resource *p_res,
212b8e80941Smrg                                   struct pipe_resource *stencil)
213b8e80941Smrg{
214b8e80941Smrg   assert(util_format_has_depth(util_format_description(p_res->format)));
215b8e80941Smrg   pipe_resource_reference(&p_res->next, stencil);
216b8e80941Smrg}
217b8e80941Smrg
218b8e80941Smrgvoid
219b8e80941Smrgiris_get_depth_stencil_resources(struct pipe_resource *res,
220b8e80941Smrg                                 struct iris_resource **out_z,
221b8e80941Smrg                                 struct iris_resource **out_s)
222b8e80941Smrg{
223b8e80941Smrg   if (!res) {
224b8e80941Smrg      *out_z = NULL;
225b8e80941Smrg      *out_s = NULL;
226b8e80941Smrg      return;
227b8e80941Smrg   }
228b8e80941Smrg
229b8e80941Smrg   if (res->format != PIPE_FORMAT_S8_UINT) {
230b8e80941Smrg      *out_z = (void *) res;
231b8e80941Smrg      *out_s = (void *) iris_resource_get_separate_stencil(res);
232b8e80941Smrg   } else {
233b8e80941Smrg      *out_z = NULL;
234b8e80941Smrg      *out_s = (void *) res;
235b8e80941Smrg   }
236b8e80941Smrg}
237b8e80941Smrg
238b8e80941Smrgvoid
239b8e80941Smrgiris_resource_disable_aux(struct iris_resource *res)
240b8e80941Smrg{
241b8e80941Smrg   iris_bo_unreference(res->aux.bo);
242b8e80941Smrg   iris_bo_unreference(res->aux.clear_color_bo);
243b8e80941Smrg   free(res->aux.state);
244b8e80941Smrg
245b8e80941Smrg   res->aux.usage = ISL_AUX_USAGE_NONE;
246b8e80941Smrg   res->aux.possible_usages = 1 << ISL_AUX_USAGE_NONE;
247b8e80941Smrg   res->aux.sampler_usages = 1 << ISL_AUX_USAGE_NONE;
248b8e80941Smrg   res->aux.surf.size_B = 0;
249b8e80941Smrg   res->aux.bo = NULL;
250b8e80941Smrg   res->aux.clear_color_bo = NULL;
251b8e80941Smrg   res->aux.state = NULL;
252b8e80941Smrg}
253b8e80941Smrg
254b8e80941Smrgstatic void
255b8e80941Smrgiris_resource_destroy(struct pipe_screen *screen,
256b8e80941Smrg                      struct pipe_resource *resource)
257b8e80941Smrg{
258b8e80941Smrg   struct iris_resource *res = (struct iris_resource *)resource;
259b8e80941Smrg
260b8e80941Smrg   if (resource->target == PIPE_BUFFER)
261b8e80941Smrg      util_range_destroy(&res->valid_buffer_range);
262b8e80941Smrg
263b8e80941Smrg   iris_resource_disable_aux(res);
264b8e80941Smrg
265b8e80941Smrg   iris_bo_unreference(res->bo);
266b8e80941Smrg   free(res);
267b8e80941Smrg}
268b8e80941Smrg
269b8e80941Smrgstatic struct iris_resource *
270b8e80941Smrgiris_alloc_resource(struct pipe_screen *pscreen,
271b8e80941Smrg                    const struct pipe_resource *templ)
272b8e80941Smrg{
273b8e80941Smrg   struct iris_resource *res = calloc(1, sizeof(struct iris_resource));
274b8e80941Smrg   if (!res)
275b8e80941Smrg      return NULL;
276b8e80941Smrg
277b8e80941Smrg   res->base = *templ;
278b8e80941Smrg   res->base.screen = pscreen;
279b8e80941Smrg   pipe_reference_init(&res->base.reference, 1);
280b8e80941Smrg
281b8e80941Smrg   res->aux.possible_usages = 1 << ISL_AUX_USAGE_NONE;
282b8e80941Smrg   res->aux.sampler_usages = 1 << ISL_AUX_USAGE_NONE;
283b8e80941Smrg
284b8e80941Smrg   if (templ->target == PIPE_BUFFER)
285b8e80941Smrg      util_range_init(&res->valid_buffer_range);
286b8e80941Smrg
287b8e80941Smrg   return res;
288b8e80941Smrg}
289b8e80941Smrg
290b8e80941Smrgunsigned
291b8e80941Smrgiris_get_num_logical_layers(const struct iris_resource *res, unsigned level)
292b8e80941Smrg{
293b8e80941Smrg   if (res->surf.dim == ISL_SURF_DIM_3D)
294b8e80941Smrg      return minify(res->surf.logical_level0_px.depth, level);
295b8e80941Smrg   else
296b8e80941Smrg      return res->surf.logical_level0_px.array_len;
297b8e80941Smrg}
298b8e80941Smrg
299b8e80941Smrgstatic enum isl_aux_state **
300b8e80941Smrgcreate_aux_state_map(struct iris_resource *res, enum isl_aux_state initial)
301b8e80941Smrg{
302b8e80941Smrg   uint32_t total_slices = 0;
303b8e80941Smrg   for (uint32_t level = 0; level < res->surf.levels; level++)
304b8e80941Smrg      total_slices += iris_get_num_logical_layers(res, level);
305b8e80941Smrg
306b8e80941Smrg   const size_t per_level_array_size =
307b8e80941Smrg      res->surf.levels * sizeof(enum isl_aux_state *);
308b8e80941Smrg
309b8e80941Smrg   /* We're going to allocate a single chunk of data for both the per-level
310b8e80941Smrg    * reference array and the arrays of aux_state.  This makes cleanup
311b8e80941Smrg    * significantly easier.
312b8e80941Smrg    */
313b8e80941Smrg   const size_t total_size =
314b8e80941Smrg      per_level_array_size + total_slices * sizeof(enum isl_aux_state);
315b8e80941Smrg
316b8e80941Smrg   void *data = malloc(total_size);
317b8e80941Smrg   if (!data)
318b8e80941Smrg      return NULL;
319b8e80941Smrg
320b8e80941Smrg   enum isl_aux_state **per_level_arr = data;
321b8e80941Smrg   enum isl_aux_state *s = data + per_level_array_size;
322b8e80941Smrg   for (uint32_t level = 0; level < res->surf.levels; level++) {
323b8e80941Smrg      per_level_arr[level] = s;
324b8e80941Smrg      const unsigned level_layers = iris_get_num_logical_layers(res, level);
325b8e80941Smrg      for (uint32_t a = 0; a < level_layers; a++)
326b8e80941Smrg         *(s++) = initial;
327b8e80941Smrg   }
328b8e80941Smrg   assert((void *)s == data + total_size);
329b8e80941Smrg
330b8e80941Smrg   return per_level_arr;
331b8e80941Smrg}
332b8e80941Smrg
333b8e80941Smrg/**
334b8e80941Smrg * Allocate the initial aux surface for a resource based on aux.usage
335b8e80941Smrg */
336b8e80941Smrgstatic bool
337b8e80941Smrgiris_resource_alloc_aux(struct iris_screen *screen, struct iris_resource *res)
338b8e80941Smrg{
339b8e80941Smrg   struct isl_device *isl_dev = &screen->isl_dev;
340b8e80941Smrg   enum isl_aux_state initial_state;
341b8e80941Smrg   UNUSED bool ok = false;
342b8e80941Smrg   uint8_t memset_value = 0;
343b8e80941Smrg   uint32_t alloc_flags = 0;
344b8e80941Smrg   const struct gen_device_info *devinfo = &screen->devinfo;
345b8e80941Smrg   const unsigned clear_color_state_size = devinfo->gen >= 10 ?
346b8e80941Smrg      screen->isl_dev.ss.clear_color_state_size :
347b8e80941Smrg      (devinfo->gen >= 9 ? screen->isl_dev.ss.clear_value_size : 0);
348b8e80941Smrg
349b8e80941Smrg   assert(!res->aux.bo);
350b8e80941Smrg
351b8e80941Smrg   switch (res->aux.usage) {
352b8e80941Smrg   case ISL_AUX_USAGE_NONE:
353b8e80941Smrg      res->aux.surf.size_B = 0;
354b8e80941Smrg      break;
355b8e80941Smrg   case ISL_AUX_USAGE_HIZ:
356b8e80941Smrg      initial_state = ISL_AUX_STATE_AUX_INVALID;
357b8e80941Smrg      memset_value = 0;
358b8e80941Smrg      ok = isl_surf_get_hiz_surf(isl_dev, &res->surf, &res->aux.surf);
359b8e80941Smrg      break;
360b8e80941Smrg   case ISL_AUX_USAGE_MCS:
361b8e80941Smrg      /* The Ivybridge PRM, Vol 2 Part 1 p326 says:
362b8e80941Smrg       *
363b8e80941Smrg       *    "When MCS buffer is enabled and bound to MSRT, it is required
364b8e80941Smrg       *     that it is cleared prior to any rendering."
365b8e80941Smrg       *
366b8e80941Smrg       * Since we only use the MCS buffer for rendering, we just clear it
367b8e80941Smrg       * immediately on allocation.  The clear value for MCS buffers is all
368b8e80941Smrg       * 1's, so we simply memset it to 0xff.
369b8e80941Smrg       */
370b8e80941Smrg      initial_state = ISL_AUX_STATE_CLEAR;
371b8e80941Smrg      memset_value = 0xFF;
372b8e80941Smrg      ok = isl_surf_get_mcs_surf(isl_dev, &res->surf, &res->aux.surf);
373b8e80941Smrg      break;
374b8e80941Smrg   case ISL_AUX_USAGE_CCS_D:
375b8e80941Smrg   case ISL_AUX_USAGE_CCS_E:
376b8e80941Smrg      /* When CCS_E is used, we need to ensure that the CCS starts off in
377b8e80941Smrg       * a valid state.  From the Sky Lake PRM, "MCS Buffer for Render
378b8e80941Smrg       * Target(s)":
379b8e80941Smrg       *
380b8e80941Smrg       *    "If Software wants to enable Color Compression without Fast
381b8e80941Smrg       *     clear, Software needs to initialize MCS with zeros."
382b8e80941Smrg       *
383b8e80941Smrg       * A CCS value of 0 indicates that the corresponding block is in the
384b8e80941Smrg       * pass-through state which is what we want.
385b8e80941Smrg       *
386b8e80941Smrg       * For CCS_D, do the same thing.  On Gen9+, this avoids having any
387b8e80941Smrg       * undefined bits in the aux buffer.
388b8e80941Smrg       */
389b8e80941Smrg      initial_state = ISL_AUX_STATE_PASS_THROUGH;
390b8e80941Smrg      alloc_flags |= BO_ALLOC_ZEROED;
391b8e80941Smrg      ok = isl_surf_get_ccs_surf(isl_dev, &res->surf, &res->aux.surf, 0);
392b8e80941Smrg      break;
393b8e80941Smrg   }
394b8e80941Smrg
395b8e80941Smrg   /* No work is needed for a zero-sized auxiliary buffer. */
396b8e80941Smrg   if (res->aux.surf.size_B == 0)
397b8e80941Smrg      return true;
398b8e80941Smrg
399b8e80941Smrg   /* Assert that ISL gave us a valid aux surf */
400b8e80941Smrg   assert(ok);
401b8e80941Smrg
402b8e80941Smrg   /* Create the aux_state for the auxiliary buffer. */
403b8e80941Smrg   res->aux.state = create_aux_state_map(res, initial_state);
404b8e80941Smrg   if (!res->aux.state)
405b8e80941Smrg      return false;
406b8e80941Smrg
407b8e80941Smrg   uint64_t size = res->aux.surf.size_B;
408b8e80941Smrg
409b8e80941Smrg   /* Allocate space in the buffer for storing the clear color. On modern
410b8e80941Smrg    * platforms (gen > 9), we can read it directly from such buffer.
411b8e80941Smrg    *
412b8e80941Smrg    * On gen <= 9, we are going to store the clear color on the buffer
413b8e80941Smrg    * anyways, and copy it back to the surface state during state emission.
414b8e80941Smrg    */
415b8e80941Smrg   res->aux.clear_color_offset = size;
416b8e80941Smrg   size += clear_color_state_size;
417b8e80941Smrg
418b8e80941Smrg   /* Allocate the auxiliary buffer.  ISL has stricter set of alignment rules
419b8e80941Smrg    * the drm allocator.  Therefore, one can pass the ISL dimensions in terms
420b8e80941Smrg    * of bytes instead of trying to recalculate based on different format
421b8e80941Smrg    * block sizes.
422b8e80941Smrg    */
423b8e80941Smrg   res->aux.bo = iris_bo_alloc_tiled(screen->bufmgr, "aux buffer", size,
424b8e80941Smrg                                     IRIS_MEMZONE_OTHER, I915_TILING_Y,
425b8e80941Smrg                                     res->aux.surf.row_pitch_B, alloc_flags);
426b8e80941Smrg   if (!res->aux.bo) {
427b8e80941Smrg      return false;
428b8e80941Smrg   }
429b8e80941Smrg
430b8e80941Smrg   if (!(alloc_flags & BO_ALLOC_ZEROED)) {
431b8e80941Smrg      void *map = iris_bo_map(NULL, res->aux.bo, MAP_WRITE | MAP_RAW);
432b8e80941Smrg
433b8e80941Smrg      if (!map) {
434b8e80941Smrg         iris_resource_disable_aux(res);
435b8e80941Smrg         return false;
436b8e80941Smrg      }
437b8e80941Smrg
438b8e80941Smrg      if (memset_value != 0)
439b8e80941Smrg         memset(map, memset_value, res->aux.surf.size_B);
440b8e80941Smrg
441b8e80941Smrg      /* Zero the indirect clear color to match ::fast_clear_color. */
442b8e80941Smrg      memset((char *)map + res->aux.clear_color_offset, 0,
443b8e80941Smrg             clear_color_state_size);
444b8e80941Smrg
445b8e80941Smrg      iris_bo_unmap(res->aux.bo);
446b8e80941Smrg   }
447b8e80941Smrg
448b8e80941Smrg   if (clear_color_state_size > 0) {
449b8e80941Smrg      res->aux.clear_color_bo = res->aux.bo;
450b8e80941Smrg      iris_bo_reference(res->aux.clear_color_bo);
451b8e80941Smrg   }
452b8e80941Smrg
453b8e80941Smrg   if (res->aux.usage == ISL_AUX_USAGE_HIZ) {
454b8e80941Smrg      for (unsigned level = 0; level < res->surf.levels; ++level) {
455b8e80941Smrg         uint32_t width = u_minify(res->surf.phys_level0_sa.width, level);
456b8e80941Smrg         uint32_t height = u_minify(res->surf.phys_level0_sa.height, level);
457b8e80941Smrg
458b8e80941Smrg         /* Disable HiZ for LOD > 0 unless the width/height are 8x4 aligned.
459b8e80941Smrg          * For LOD == 0, we can grow the dimensions to make it work.
460b8e80941Smrg          */
461b8e80941Smrg         if (level == 0 || ((width & 7) == 0 && (height & 3) == 0))
462b8e80941Smrg            res->aux.has_hiz |= 1 << level;
463b8e80941Smrg      }
464b8e80941Smrg   }
465b8e80941Smrg
466b8e80941Smrg   return true;
467b8e80941Smrg}
468b8e80941Smrg
469b8e80941Smrgstatic bool
470b8e80941Smrgsupports_mcs(const struct isl_surf *surf)
471b8e80941Smrg{
472b8e80941Smrg   /* MCS compression only applies to multisampled resources. */
473b8e80941Smrg   if (surf->samples <= 1)
474b8e80941Smrg      return false;
475b8e80941Smrg
476b8e80941Smrg   /* See isl_surf_get_mcs_surf for details. */
477b8e80941Smrg   if (surf->samples == 16 && surf->logical_level0_px.width > 8192)
478b8e80941Smrg      return false;
479b8e80941Smrg
480b8e80941Smrg   /* Depth and stencil buffers use the IMS (interleaved) layout. */
481b8e80941Smrg   if (isl_surf_usage_is_depth_or_stencil(surf->usage))
482b8e80941Smrg      return false;
483b8e80941Smrg
484b8e80941Smrg   return true;
485b8e80941Smrg}
486b8e80941Smrg
487b8e80941Smrgstatic bool
488b8e80941Smrgsupports_ccs(const struct gen_device_info *devinfo,
489b8e80941Smrg             const struct isl_surf *surf)
490b8e80941Smrg{
491b8e80941Smrg   /* Gen9+ only supports CCS for Y-tiled buffers. */
492b8e80941Smrg   if (surf->tiling != ISL_TILING_Y0)
493b8e80941Smrg      return false;
494b8e80941Smrg
495b8e80941Smrg   /* CCS only supports singlesampled resources. */
496b8e80941Smrg   if (surf->samples > 1)
497b8e80941Smrg      return false;
498b8e80941Smrg
499b8e80941Smrg   /* The PRM doesn't say this explicitly, but fast-clears don't appear to
500b8e80941Smrg    * work for 3D textures until Gen9 where the layout of 3D textures changes
501b8e80941Smrg    * to match 2D array textures.
502b8e80941Smrg    */
503b8e80941Smrg   if (devinfo->gen < 9 && surf->dim != ISL_SURF_DIM_2D)
504b8e80941Smrg      return false;
505b8e80941Smrg
506b8e80941Smrg   /* Note: still need to check the format! */
507b8e80941Smrg
508b8e80941Smrg   return true;
509b8e80941Smrg}
510b8e80941Smrg
511b8e80941Smrgstatic struct pipe_resource *
512b8e80941Smrgiris_resource_create_for_buffer(struct pipe_screen *pscreen,
513b8e80941Smrg                                const struct pipe_resource *templ)
514b8e80941Smrg{
515b8e80941Smrg   struct iris_screen *screen = (struct iris_screen *)pscreen;
516b8e80941Smrg   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
517b8e80941Smrg
518b8e80941Smrg   assert(templ->target == PIPE_BUFFER);
519b8e80941Smrg   assert(templ->height0 <= 1);
520b8e80941Smrg   assert(templ->depth0 <= 1);
521b8e80941Smrg   assert(templ->format == PIPE_FORMAT_NONE ||
522b8e80941Smrg          util_format_get_blocksize(templ->format) == 1);
523b8e80941Smrg
524b8e80941Smrg   res->internal_format = templ->format;
525b8e80941Smrg   res->surf.tiling = ISL_TILING_LINEAR;
526b8e80941Smrg
527b8e80941Smrg   enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
528b8e80941Smrg   const char *name = templ->target == PIPE_BUFFER ? "buffer" : "miptree";
529b8e80941Smrg   if (templ->flags & IRIS_RESOURCE_FLAG_SHADER_MEMZONE) {
530b8e80941Smrg      memzone = IRIS_MEMZONE_SHADER;
531b8e80941Smrg      name = "shader kernels";
532b8e80941Smrg   } else if (templ->flags & IRIS_RESOURCE_FLAG_SURFACE_MEMZONE) {
533b8e80941Smrg      memzone = IRIS_MEMZONE_SURFACE;
534b8e80941Smrg      name = "surface state";
535b8e80941Smrg   } else if (templ->flags & IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE) {
536b8e80941Smrg      memzone = IRIS_MEMZONE_DYNAMIC;
537b8e80941Smrg      name = "dynamic state";
538b8e80941Smrg   }
539b8e80941Smrg
540b8e80941Smrg   res->bo = iris_bo_alloc(screen->bufmgr, name, templ->width0, memzone);
541b8e80941Smrg   if (!res->bo) {
542b8e80941Smrg      iris_resource_destroy(pscreen, &res->base);
543b8e80941Smrg      return NULL;
544b8e80941Smrg   }
545b8e80941Smrg
546b8e80941Smrg   return &res->base;
547b8e80941Smrg}
548b8e80941Smrg
549b8e80941Smrgstatic struct pipe_resource *
550b8e80941Smrgiris_resource_create_with_modifiers(struct pipe_screen *pscreen,
551b8e80941Smrg                                    const struct pipe_resource *templ,
552b8e80941Smrg                                    const uint64_t *modifiers,
553b8e80941Smrg                                    int modifiers_count)
554b8e80941Smrg{
555b8e80941Smrg   struct iris_screen *screen = (struct iris_screen *)pscreen;
556b8e80941Smrg   struct gen_device_info *devinfo = &screen->devinfo;
557b8e80941Smrg   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
558b8e80941Smrg
559b8e80941Smrg   if (!res)
560b8e80941Smrg      return NULL;
561b8e80941Smrg
562b8e80941Smrg   const struct util_format_description *format_desc =
563b8e80941Smrg      util_format_description(templ->format);
564b8e80941Smrg   const bool has_depth = util_format_has_depth(format_desc);
565b8e80941Smrg   uint64_t modifier =
566b8e80941Smrg      select_best_modifier(devinfo, modifiers, modifiers_count);
567b8e80941Smrg
568b8e80941Smrg   isl_tiling_flags_t tiling_flags = ISL_TILING_ANY_MASK;
569b8e80941Smrg
570b8e80941Smrg   if (modifier != DRM_FORMAT_MOD_INVALID) {
571b8e80941Smrg      res->mod_info = isl_drm_modifier_get_info(modifier);
572b8e80941Smrg
573b8e80941Smrg      tiling_flags = 1 << res->mod_info->tiling;
574b8e80941Smrg   } else {
575b8e80941Smrg      if (modifiers_count > 0) {
576b8e80941Smrg         fprintf(stderr, "Unsupported modifier, resource creation failed.\n");
577b8e80941Smrg         return NULL;
578b8e80941Smrg      }
579b8e80941Smrg
580b8e80941Smrg      /* No modifiers - we can select our own tiling. */
581b8e80941Smrg
582b8e80941Smrg      if (has_depth) {
583b8e80941Smrg         /* Depth must be Y-tiled */
584b8e80941Smrg         tiling_flags = ISL_TILING_Y0_BIT;
585b8e80941Smrg      } else if (templ->format == PIPE_FORMAT_S8_UINT) {
586b8e80941Smrg         /* Stencil must be W-tiled */
587b8e80941Smrg         tiling_flags = ISL_TILING_W_BIT;
588b8e80941Smrg      } else if (templ->target == PIPE_BUFFER ||
589b8e80941Smrg                 templ->target == PIPE_TEXTURE_1D ||
590b8e80941Smrg                 templ->target == PIPE_TEXTURE_1D_ARRAY) {
591b8e80941Smrg         /* Use linear for buffers and 1D textures */
592b8e80941Smrg         tiling_flags = ISL_TILING_LINEAR_BIT;
593b8e80941Smrg      }
594b8e80941Smrg
595b8e80941Smrg      /* Use linear for staging buffers */
596b8e80941Smrg      if (templ->usage == PIPE_USAGE_STAGING ||
597b8e80941Smrg          templ->bind & (PIPE_BIND_LINEAR | PIPE_BIND_CURSOR) )
598b8e80941Smrg         tiling_flags = ISL_TILING_LINEAR_BIT;
599b8e80941Smrg   }
600b8e80941Smrg
601b8e80941Smrg   isl_surf_usage_flags_t usage = pipe_bind_to_isl_usage(templ->bind);
602b8e80941Smrg
603b8e80941Smrg   if (templ->target == PIPE_TEXTURE_CUBE ||
604b8e80941Smrg       templ->target == PIPE_TEXTURE_CUBE_ARRAY)
605b8e80941Smrg      usage |= ISL_SURF_USAGE_CUBE_BIT;
606b8e80941Smrg
607b8e80941Smrg   if (templ->usage != PIPE_USAGE_STAGING) {
608b8e80941Smrg      if (templ->format == PIPE_FORMAT_S8_UINT)
609b8e80941Smrg         usage |= ISL_SURF_USAGE_STENCIL_BIT;
610b8e80941Smrg      else if (has_depth)
611b8e80941Smrg         usage |= ISL_SURF_USAGE_DEPTH_BIT;
612b8e80941Smrg   }
613b8e80941Smrg
614b8e80941Smrg   enum pipe_format pfmt = templ->format;
615b8e80941Smrg   res->internal_format = pfmt;
616b8e80941Smrg
617b8e80941Smrg   /* Should be handled by u_transfer_helper */
618b8e80941Smrg   assert(!util_format_is_depth_and_stencil(pfmt));
619b8e80941Smrg
620b8e80941Smrg   struct iris_format_info fmt = iris_format_for_usage(devinfo, pfmt, usage);
621b8e80941Smrg   assert(fmt.fmt != ISL_FORMAT_UNSUPPORTED);
622b8e80941Smrg
623b8e80941Smrg   UNUSED const bool isl_surf_created_successfully =
624b8e80941Smrg      isl_surf_init(&screen->isl_dev, &res->surf,
625b8e80941Smrg                    .dim = target_to_isl_surf_dim(templ->target),
626b8e80941Smrg                    .format = fmt.fmt,
627b8e80941Smrg                    .width = templ->width0,
628b8e80941Smrg                    .height = templ->height0,
629b8e80941Smrg                    .depth = templ->depth0,
630b8e80941Smrg                    .levels = templ->last_level + 1,
631b8e80941Smrg                    .array_len = templ->array_size,
632b8e80941Smrg                    .samples = MAX2(templ->nr_samples, 1),
633b8e80941Smrg                    .min_alignment_B = 0,
634b8e80941Smrg                    .row_pitch_B = 0,
635b8e80941Smrg                    .usage = usage,
636b8e80941Smrg                    .tiling_flags = tiling_flags);
637b8e80941Smrg   assert(isl_surf_created_successfully);
638b8e80941Smrg
639b8e80941Smrg   if (res->mod_info) {
640b8e80941Smrg      res->aux.possible_usages |= 1 << res->mod_info->aux_usage;
641b8e80941Smrg   } else if (supports_mcs(&res->surf)) {
642b8e80941Smrg      res->aux.possible_usages |= 1 << ISL_AUX_USAGE_MCS;
643b8e80941Smrg   } else if (has_depth) {
644b8e80941Smrg      if (likely(!(INTEL_DEBUG & DEBUG_NO_HIZ)))
645b8e80941Smrg         res->aux.possible_usages |= 1 << ISL_AUX_USAGE_HIZ;
646b8e80941Smrg   } else if (likely(!(INTEL_DEBUG & DEBUG_NO_RBC)) &&
647b8e80941Smrg              supports_ccs(devinfo, &res->surf)) {
648b8e80941Smrg      if (isl_format_supports_ccs_e(devinfo, res->surf.format))
649b8e80941Smrg         res->aux.possible_usages |= 1 << ISL_AUX_USAGE_CCS_E;
650b8e80941Smrg
651b8e80941Smrg      if (isl_format_supports_ccs_d(devinfo, res->surf.format))
652b8e80941Smrg         res->aux.possible_usages |= 1 << ISL_AUX_USAGE_CCS_D;
653b8e80941Smrg   }
654b8e80941Smrg
655b8e80941Smrg   res->aux.usage = util_last_bit(res->aux.possible_usages) - 1;
656b8e80941Smrg
657b8e80941Smrg   res->aux.sampler_usages = res->aux.possible_usages;
658b8e80941Smrg
659b8e80941Smrg   /* We don't always support sampling with hiz. But when we do, it must be
660b8e80941Smrg    * single sampled.
661b8e80941Smrg    */
662b8e80941Smrg   if (!devinfo->has_sample_with_hiz || res->surf.samples > 1) {
663b8e80941Smrg      res->aux.sampler_usages &= ~(1 << ISL_AUX_USAGE_HIZ);
664b8e80941Smrg   }
665b8e80941Smrg
666b8e80941Smrg   const char *name = "miptree";
667b8e80941Smrg   enum iris_memory_zone memzone = IRIS_MEMZONE_OTHER;
668b8e80941Smrg
669b8e80941Smrg   unsigned int flags = 0;
670b8e80941Smrg   if (templ->usage == PIPE_USAGE_STAGING)
671b8e80941Smrg      flags |= BO_ALLOC_COHERENT;
672b8e80941Smrg
673b8e80941Smrg   /* These are for u_upload_mgr buffers only */
674b8e80941Smrg   assert(!(templ->flags & (IRIS_RESOURCE_FLAG_SHADER_MEMZONE |
675b8e80941Smrg                            IRIS_RESOURCE_FLAG_SURFACE_MEMZONE |
676b8e80941Smrg                            IRIS_RESOURCE_FLAG_DYNAMIC_MEMZONE)));
677b8e80941Smrg
678b8e80941Smrg   res->bo = iris_bo_alloc_tiled(screen->bufmgr, name, res->surf.size_B,
679b8e80941Smrg                                 memzone,
680b8e80941Smrg                                 isl_tiling_to_i915_tiling(res->surf.tiling),
681b8e80941Smrg                                 res->surf.row_pitch_B, flags);
682b8e80941Smrg
683b8e80941Smrg   if (!res->bo)
684b8e80941Smrg      goto fail;
685b8e80941Smrg
686b8e80941Smrg   if (!iris_resource_alloc_aux(screen, res))
687b8e80941Smrg      goto fail;
688b8e80941Smrg
689b8e80941Smrg   return &res->base;
690b8e80941Smrg
691b8e80941Smrgfail:
692b8e80941Smrg   fprintf(stderr, "XXX: resource creation failed\n");
693b8e80941Smrg   iris_resource_destroy(pscreen, &res->base);
694b8e80941Smrg   return NULL;
695b8e80941Smrg
696b8e80941Smrg}
697b8e80941Smrg
698b8e80941Smrgstatic struct pipe_resource *
699b8e80941Smrgiris_resource_create(struct pipe_screen *pscreen,
700b8e80941Smrg                     const struct pipe_resource *templ)
701b8e80941Smrg{
702b8e80941Smrg   if (templ->target == PIPE_BUFFER)
703b8e80941Smrg      return iris_resource_create_for_buffer(pscreen, templ);
704b8e80941Smrg   else
705b8e80941Smrg      return iris_resource_create_with_modifiers(pscreen, templ, NULL, 0);
706b8e80941Smrg}
707b8e80941Smrg
708b8e80941Smrgstatic uint64_t
709b8e80941Smrgtiling_to_modifier(uint32_t tiling)
710b8e80941Smrg{
711b8e80941Smrg   static const uint64_t map[] = {
712b8e80941Smrg      [I915_TILING_NONE]   = DRM_FORMAT_MOD_LINEAR,
713b8e80941Smrg      [I915_TILING_X]      = I915_FORMAT_MOD_X_TILED,
714b8e80941Smrg      [I915_TILING_Y]      = I915_FORMAT_MOD_Y_TILED,
715b8e80941Smrg   };
716b8e80941Smrg
717b8e80941Smrg   assert(tiling < ARRAY_SIZE(map));
718b8e80941Smrg
719b8e80941Smrg   return map[tiling];
720b8e80941Smrg}
721b8e80941Smrg
722b8e80941Smrgstatic struct pipe_resource *
723b8e80941Smrgiris_resource_from_user_memory(struct pipe_screen *pscreen,
724b8e80941Smrg                               const struct pipe_resource *templ,
725b8e80941Smrg                               void *user_memory)
726b8e80941Smrg{
727b8e80941Smrg   struct iris_screen *screen = (struct iris_screen *)pscreen;
728b8e80941Smrg   struct iris_bufmgr *bufmgr = screen->bufmgr;
729b8e80941Smrg   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
730b8e80941Smrg   if (!res)
731b8e80941Smrg      return NULL;
732b8e80941Smrg
733b8e80941Smrg   assert(templ->target == PIPE_BUFFER);
734b8e80941Smrg
735b8e80941Smrg   res->internal_format = templ->format;
736b8e80941Smrg   res->bo = iris_bo_create_userptr(bufmgr, "user",
737b8e80941Smrg                                    user_memory, templ->width0,
738b8e80941Smrg                                    IRIS_MEMZONE_OTHER);
739b8e80941Smrg   if (!res->bo) {
740b8e80941Smrg      free(res);
741b8e80941Smrg      return NULL;
742b8e80941Smrg   }
743b8e80941Smrg
744b8e80941Smrg   util_range_add(&res->valid_buffer_range, 0, templ->width0);
745b8e80941Smrg
746b8e80941Smrg   return &res->base;
747b8e80941Smrg}
748b8e80941Smrg
749b8e80941Smrgstatic struct pipe_resource *
750b8e80941Smrgiris_resource_from_handle(struct pipe_screen *pscreen,
751b8e80941Smrg                          const struct pipe_resource *templ,
752b8e80941Smrg                          struct winsys_handle *whandle,
753b8e80941Smrg                          unsigned usage)
754b8e80941Smrg{
755b8e80941Smrg   struct iris_screen *screen = (struct iris_screen *)pscreen;
756b8e80941Smrg   struct gen_device_info *devinfo = &screen->devinfo;
757b8e80941Smrg   struct iris_bufmgr *bufmgr = screen->bufmgr;
758b8e80941Smrg   struct iris_resource *res = iris_alloc_resource(pscreen, templ);
759b8e80941Smrg   if (!res)
760b8e80941Smrg      return NULL;
761b8e80941Smrg
762b8e80941Smrg   if (whandle->offset != 0) {
763b8e80941Smrg      dbg_printf("Attempt to import unsupported winsys offset %u\n",
764b8e80941Smrg                 whandle->offset);
765b8e80941Smrg      goto fail;
766b8e80941Smrg   }
767b8e80941Smrg
768b8e80941Smrg   switch (whandle->type) {
769b8e80941Smrg   case WINSYS_HANDLE_TYPE_FD:
770b8e80941Smrg      res->bo = iris_bo_import_dmabuf(bufmgr, whandle->handle);
771b8e80941Smrg      break;
772b8e80941Smrg   case WINSYS_HANDLE_TYPE_SHARED:
773b8e80941Smrg      res->bo = iris_bo_gem_create_from_name(bufmgr, "winsys image",
774b8e80941Smrg                                             whandle->handle);
775b8e80941Smrg      break;
776b8e80941Smrg   default:
777b8e80941Smrg      unreachable("invalid winsys handle type");
778b8e80941Smrg   }
779b8e80941Smrg   if (!res->bo)
780b8e80941Smrg      return NULL;
781b8e80941Smrg
782b8e80941Smrg   uint64_t modifier = whandle->modifier;
783b8e80941Smrg   if (modifier == DRM_FORMAT_MOD_INVALID) {
784b8e80941Smrg      modifier = tiling_to_modifier(res->bo->tiling_mode);
785b8e80941Smrg   }
786b8e80941Smrg   res->mod_info = isl_drm_modifier_get_info(modifier);
787b8e80941Smrg   assert(res->mod_info);
788b8e80941Smrg
789b8e80941Smrg   isl_surf_usage_flags_t isl_usage = pipe_bind_to_isl_usage(templ->bind);
790b8e80941Smrg
791b8e80941Smrg   const struct iris_format_info fmt =
792b8e80941Smrg      iris_format_for_usage(devinfo, templ->format, isl_usage);
793b8e80941Smrg   res->internal_format = templ->format;
794b8e80941Smrg
795b8e80941Smrg   if (templ->target == PIPE_BUFFER) {
796b8e80941Smrg      res->surf.tiling = ISL_TILING_LINEAR;
797b8e80941Smrg   } else {
798b8e80941Smrg      isl_surf_init(&screen->isl_dev, &res->surf,
799b8e80941Smrg                    .dim = target_to_isl_surf_dim(templ->target),
800b8e80941Smrg                    .format = fmt.fmt,
801b8e80941Smrg                    .width = templ->width0,
802b8e80941Smrg                    .height = templ->height0,
803b8e80941Smrg                    .depth = templ->depth0,
804b8e80941Smrg                    .levels = templ->last_level + 1,
805b8e80941Smrg                    .array_len = templ->array_size,
806b8e80941Smrg                    .samples = MAX2(templ->nr_samples, 1),
807b8e80941Smrg                    .min_alignment_B = 0,
808b8e80941Smrg                    .row_pitch_B = whandle->stride,
809b8e80941Smrg                    .usage = isl_usage,
810b8e80941Smrg                    .tiling_flags = 1 << res->mod_info->tiling);
811b8e80941Smrg
812b8e80941Smrg      assert(res->bo->tiling_mode ==
813b8e80941Smrg             isl_tiling_to_i915_tiling(res->surf.tiling));
814b8e80941Smrg
815b8e80941Smrg      // XXX: create_ccs_buf_for_image?
816b8e80941Smrg      if (!iris_resource_alloc_aux(screen, res))
817b8e80941Smrg         goto fail;
818b8e80941Smrg   }
819b8e80941Smrg
820b8e80941Smrg   return &res->base;
821b8e80941Smrg
822b8e80941Smrgfail:
823b8e80941Smrg   iris_resource_destroy(pscreen, &res->base);
824b8e80941Smrg   return NULL;
825b8e80941Smrg}
826b8e80941Smrg
827b8e80941Smrgstatic void
828b8e80941Smrgiris_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
829b8e80941Smrg{
830b8e80941Smrg   struct iris_context *ice = (struct iris_context *)ctx;
831b8e80941Smrg   struct iris_batch *render_batch = &ice->batches[IRIS_BATCH_RENDER];
832b8e80941Smrg   struct iris_resource *res = (void *) resource;
833b8e80941Smrg   const struct isl_drm_modifier_info *mod = res->mod_info;
834b8e80941Smrg
835b8e80941Smrg   iris_resource_prepare_access(ice, render_batch, res,
836b8e80941Smrg                                0, INTEL_REMAINING_LEVELS,
837b8e80941Smrg                                0, INTEL_REMAINING_LAYERS,
838b8e80941Smrg                                mod ? mod->aux_usage : ISL_AUX_USAGE_NONE,
839b8e80941Smrg                                mod ? mod->supports_clear_color : false);
840b8e80941Smrg}
841b8e80941Smrg
842b8e80941Smrgstatic boolean
843b8e80941Smrgiris_resource_get_handle(struct pipe_screen *pscreen,
844b8e80941Smrg                         struct pipe_context *ctx,
845b8e80941Smrg                         struct pipe_resource *resource,
846b8e80941Smrg                         struct winsys_handle *whandle,
847b8e80941Smrg                         unsigned usage)
848b8e80941Smrg{
849b8e80941Smrg   struct iris_resource *res = (struct iris_resource *)resource;
850b8e80941Smrg
851b8e80941Smrg   /* Disable aux usage if explicit flush not set and this is the
852b8e80941Smrg    * first time we are dealing with this resource.
853b8e80941Smrg    */
854b8e80941Smrg   if ((!(usage & PIPE_HANDLE_USAGE_EXPLICIT_FLUSH) && res->aux.usage != 0)) {
855b8e80941Smrg      if (p_atomic_read(&resource->reference.count) == 1)
856b8e80941Smrg         iris_resource_disable_aux(res);
857b8e80941Smrg   }
858b8e80941Smrg
859b8e80941Smrg   /* If this is a buffer, stride should be 0 - no need to special case */
860b8e80941Smrg   whandle->stride = res->surf.row_pitch_B;
861b8e80941Smrg   whandle->modifier =
862b8e80941Smrg      res->mod_info ? res->mod_info->modifier
863b8e80941Smrg                    : tiling_to_modifier(res->bo->tiling_mode);
864b8e80941Smrg
865b8e80941Smrg#ifndef NDEBUG
866b8e80941Smrg   enum isl_aux_usage allowed_usage =
867b8e80941Smrg      res->mod_info ? res->mod_info->aux_usage : ISL_AUX_USAGE_NONE;
868b8e80941Smrg
869b8e80941Smrg   if (res->aux.usage != allowed_usage) {
870b8e80941Smrg      enum isl_aux_state aux_state = iris_resource_get_aux_state(res, 0, 0);
871b8e80941Smrg      assert(aux_state == ISL_AUX_STATE_RESOLVED ||
872b8e80941Smrg             aux_state == ISL_AUX_STATE_PASS_THROUGH);
873b8e80941Smrg   }
874b8e80941Smrg#endif
875b8e80941Smrg
876b8e80941Smrg   switch (whandle->type) {
877b8e80941Smrg   case WINSYS_HANDLE_TYPE_SHARED:
878b8e80941Smrg      return iris_bo_flink(res->bo, &whandle->handle) == 0;
879b8e80941Smrg   case WINSYS_HANDLE_TYPE_KMS:
880b8e80941Smrg      whandle->handle = iris_bo_export_gem_handle(res->bo);
881b8e80941Smrg      return true;
882b8e80941Smrg   case WINSYS_HANDLE_TYPE_FD:
883b8e80941Smrg      return iris_bo_export_dmabuf(res->bo, (int *) &whandle->handle) == 0;
884b8e80941Smrg   }
885b8e80941Smrg
886b8e80941Smrg   return false;
887b8e80941Smrg}
888b8e80941Smrg
889b8e80941Smrgstatic bool
890b8e80941Smrgresource_is_busy(struct iris_context *ice,
891b8e80941Smrg                 struct iris_resource *res)
892b8e80941Smrg{
893b8e80941Smrg   bool busy = iris_bo_busy(res->bo);
894b8e80941Smrg
895b8e80941Smrg   for (int i = 0; i < IRIS_BATCH_COUNT; i++)
896b8e80941Smrg      busy |= iris_batch_references(&ice->batches[i], res->bo);
897b8e80941Smrg
898b8e80941Smrg   return busy;
899b8e80941Smrg}
900b8e80941Smrg
901b8e80941Smrgstatic void
902b8e80941Smrgiris_invalidate_resource(struct pipe_context *ctx,
903b8e80941Smrg                         struct pipe_resource *resource)
904b8e80941Smrg{
905b8e80941Smrg   struct iris_screen *screen = (void *) ctx->screen;
906b8e80941Smrg   struct iris_context *ice = (void *) ctx;
907b8e80941Smrg   struct iris_resource *res = (void *) resource;
908b8e80941Smrg
909b8e80941Smrg   if (resource->target != PIPE_BUFFER)
910b8e80941Smrg      return;
911b8e80941Smrg
912b8e80941Smrg   if (!resource_is_busy(ice, res)) {
913b8e80941Smrg      /* The resource is idle, so just mark that it contains no data and
914b8e80941Smrg       * keep using the same underlying buffer object.
915b8e80941Smrg       */
916b8e80941Smrg      util_range_set_empty(&res->valid_buffer_range);
917b8e80941Smrg      return;
918b8e80941Smrg   }
919b8e80941Smrg
920b8e80941Smrg   /* Otherwise, try and replace the backing storage with a new BO. */
921b8e80941Smrg
922b8e80941Smrg   /* We can't reallocate memory we didn't allocate in the first place. */
923b8e80941Smrg   if (res->bo->userptr)
924b8e80941Smrg      return;
925b8e80941Smrg
926b8e80941Smrg   // XXX: We should support this.
927b8e80941Smrg   if (res->bind_history & PIPE_BIND_STREAM_OUTPUT)
928b8e80941Smrg      return;
929b8e80941Smrg
930b8e80941Smrg   struct iris_bo *old_bo = res->bo;
931b8e80941Smrg   struct iris_bo *new_bo =
932b8e80941Smrg      iris_bo_alloc(screen->bufmgr, res->bo->name, resource->width0,
933b8e80941Smrg                    iris_memzone_for_address(old_bo->gtt_offset));
934b8e80941Smrg   if (!new_bo)
935b8e80941Smrg      return;
936b8e80941Smrg
937b8e80941Smrg   /* Swap out the backing storage */
938b8e80941Smrg   res->bo = new_bo;
939b8e80941Smrg
940b8e80941Smrg   /* Rebind the buffer, replacing any state referring to the old BO's
941b8e80941Smrg    * address, and marking state dirty so it's reemitted.
942b8e80941Smrg    */
943b8e80941Smrg   ice->vtbl.rebind_buffer(ice, res, old_bo->gtt_offset);
944b8e80941Smrg
945b8e80941Smrg   util_range_set_empty(&res->valid_buffer_range);
946b8e80941Smrg
947b8e80941Smrg   iris_bo_unreference(old_bo);
948b8e80941Smrg}
949b8e80941Smrg
950b8e80941Smrgstatic void
951b8e80941Smrgiris_flush_staging_region(struct pipe_transfer *xfer,
952b8e80941Smrg                          const struct pipe_box *flush_box)
953b8e80941Smrg{
954b8e80941Smrg   if (!(xfer->usage & PIPE_TRANSFER_WRITE))
955b8e80941Smrg      return;
956b8e80941Smrg
957b8e80941Smrg   struct iris_transfer *map = (void *) xfer;
958b8e80941Smrg
959b8e80941Smrg   struct pipe_box src_box = *flush_box;
960b8e80941Smrg
961b8e80941Smrg   /* Account for extra alignment padding in staging buffer */
962b8e80941Smrg   if (xfer->resource->target == PIPE_BUFFER)
963b8e80941Smrg      src_box.x += xfer->box.x % IRIS_MAP_BUFFER_ALIGNMENT;
964b8e80941Smrg
965b8e80941Smrg   struct pipe_box dst_box = (struct pipe_box) {
966b8e80941Smrg      .x = xfer->box.x + flush_box->x,
967b8e80941Smrg      .y = xfer->box.y + flush_box->y,
968b8e80941Smrg      .z = xfer->box.z + flush_box->z,
969b8e80941Smrg      .width = flush_box->width,
970b8e80941Smrg      .height = flush_box->height,
971b8e80941Smrg      .depth = flush_box->depth,
972b8e80941Smrg   };
973b8e80941Smrg
974b8e80941Smrg   iris_copy_region(map->blorp, map->batch, xfer->resource, xfer->level,
975b8e80941Smrg                    dst_box.x, dst_box.y, dst_box.z, map->staging, 0,
976b8e80941Smrg                    &src_box);
977b8e80941Smrg}
978b8e80941Smrg
979b8e80941Smrgstatic void
980b8e80941Smrgiris_unmap_copy_region(struct iris_transfer *map)
981b8e80941Smrg{
982b8e80941Smrg   iris_resource_destroy(map->staging->screen, map->staging);
983b8e80941Smrg
984b8e80941Smrg   map->ptr = NULL;
985b8e80941Smrg}
986b8e80941Smrg
987b8e80941Smrgstatic void
988b8e80941Smrgiris_map_copy_region(struct iris_transfer *map)
989b8e80941Smrg{
990b8e80941Smrg   struct pipe_screen *pscreen = &map->batch->screen->base;
991b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
992b8e80941Smrg   struct pipe_box *box = &xfer->box;
993b8e80941Smrg   struct iris_resource *res = (void *) xfer->resource;
994b8e80941Smrg
995b8e80941Smrg   unsigned extra = xfer->resource->target == PIPE_BUFFER ?
996b8e80941Smrg                    box->x % IRIS_MAP_BUFFER_ALIGNMENT : 0;
997b8e80941Smrg
998b8e80941Smrg   struct pipe_resource templ = (struct pipe_resource) {
999b8e80941Smrg      .usage = PIPE_USAGE_STAGING,
1000b8e80941Smrg      .width0 = box->width + extra,
1001b8e80941Smrg      .height0 = box->height,
1002b8e80941Smrg      .depth0 = 1,
1003b8e80941Smrg      .nr_samples = xfer->resource->nr_samples,
1004b8e80941Smrg      .nr_storage_samples = xfer->resource->nr_storage_samples,
1005b8e80941Smrg      .array_size = box->depth,
1006b8e80941Smrg      .format = res->internal_format,
1007b8e80941Smrg   };
1008b8e80941Smrg
1009b8e80941Smrg   if (xfer->resource->target == PIPE_BUFFER)
1010b8e80941Smrg      templ.target = PIPE_BUFFER;
1011b8e80941Smrg   else if (templ.array_size > 1)
1012b8e80941Smrg      templ.target = PIPE_TEXTURE_2D_ARRAY;
1013b8e80941Smrg   else
1014b8e80941Smrg      templ.target = PIPE_TEXTURE_2D;
1015b8e80941Smrg
1016b8e80941Smrg   map->staging = iris_resource_create(pscreen, &templ);
1017b8e80941Smrg   assert(map->staging);
1018b8e80941Smrg
1019b8e80941Smrg   if (templ.target != PIPE_BUFFER) {
1020b8e80941Smrg      struct isl_surf *surf = &((struct iris_resource *) map->staging)->surf;
1021b8e80941Smrg      xfer->stride = isl_surf_get_row_pitch_B(surf);
1022b8e80941Smrg      xfer->layer_stride = isl_surf_get_array_pitch(surf);
1023b8e80941Smrg   }
1024b8e80941Smrg
1025b8e80941Smrg   if (!(xfer->usage & PIPE_TRANSFER_DISCARD_RANGE)) {
1026b8e80941Smrg      iris_copy_region(map->blorp, map->batch, map->staging, 0, extra, 0, 0,
1027b8e80941Smrg                       xfer->resource, xfer->level, box);
1028b8e80941Smrg      /* Ensure writes to the staging BO land before we map it below. */
1029b8e80941Smrg      iris_emit_pipe_control_flush(map->batch,
1030b8e80941Smrg                                   PIPE_CONTROL_RENDER_TARGET_FLUSH |
1031b8e80941Smrg                                   PIPE_CONTROL_CS_STALL);
1032b8e80941Smrg   }
1033b8e80941Smrg
1034b8e80941Smrg   struct iris_bo *staging_bo = iris_resource_bo(map->staging);
1035b8e80941Smrg
1036b8e80941Smrg   if (iris_batch_references(map->batch, staging_bo))
1037b8e80941Smrg      iris_batch_flush(map->batch);
1038b8e80941Smrg
1039b8e80941Smrg   map->ptr =
1040b8e80941Smrg      iris_bo_map(map->dbg, staging_bo, xfer->usage & MAP_FLAGS) + extra;
1041b8e80941Smrg
1042b8e80941Smrg   map->unmap = iris_unmap_copy_region;
1043b8e80941Smrg}
1044b8e80941Smrg
1045b8e80941Smrgstatic void
1046b8e80941Smrgget_image_offset_el(const struct isl_surf *surf, unsigned level, unsigned z,
1047b8e80941Smrg                    unsigned *out_x0_el, unsigned *out_y0_el)
1048b8e80941Smrg{
1049b8e80941Smrg   if (surf->dim == ISL_SURF_DIM_3D) {
1050b8e80941Smrg      isl_surf_get_image_offset_el(surf, level, 0, z, out_x0_el, out_y0_el);
1051b8e80941Smrg   } else {
1052b8e80941Smrg      isl_surf_get_image_offset_el(surf, level, z, 0, out_x0_el, out_y0_el);
1053b8e80941Smrg   }
1054b8e80941Smrg}
1055b8e80941Smrg
1056b8e80941Smrg/**
1057b8e80941Smrg * Get pointer offset into stencil buffer.
1058b8e80941Smrg *
1059b8e80941Smrg * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
1060b8e80941Smrg * must decode the tile's layout in software.
1061b8e80941Smrg *
1062b8e80941Smrg * See
1063b8e80941Smrg *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
1064b8e80941Smrg *     Format.
1065b8e80941Smrg *   - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
1066b8e80941Smrg *
1067b8e80941Smrg * Even though the returned offset is always positive, the return type is
1068b8e80941Smrg * signed due to
1069b8e80941Smrg *    commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
1070b8e80941Smrg *    mesa: Fix return type of  _mesa_get_format_bytes() (#37351)
1071b8e80941Smrg */
1072b8e80941Smrgstatic intptr_t
1073b8e80941Smrgs8_offset(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
1074b8e80941Smrg{
1075b8e80941Smrg   uint32_t tile_size = 4096;
1076b8e80941Smrg   uint32_t tile_width = 64;
1077b8e80941Smrg   uint32_t tile_height = 64;
1078b8e80941Smrg   uint32_t row_size = 64 * stride / 2; /* Two rows are interleaved. */
1079b8e80941Smrg
1080b8e80941Smrg   uint32_t tile_x = x / tile_width;
1081b8e80941Smrg   uint32_t tile_y = y / tile_height;
1082b8e80941Smrg
1083b8e80941Smrg   /* The byte's address relative to the tile's base addres. */
1084b8e80941Smrg   uint32_t byte_x = x % tile_width;
1085b8e80941Smrg   uint32_t byte_y = y % tile_height;
1086b8e80941Smrg
1087b8e80941Smrg   uintptr_t u = tile_y * row_size
1088b8e80941Smrg               + tile_x * tile_size
1089b8e80941Smrg               + 512 * (byte_x / 8)
1090b8e80941Smrg               +  64 * (byte_y / 8)
1091b8e80941Smrg               +  32 * ((byte_y / 4) % 2)
1092b8e80941Smrg               +  16 * ((byte_x / 4) % 2)
1093b8e80941Smrg               +   8 * ((byte_y / 2) % 2)
1094b8e80941Smrg               +   4 * ((byte_x / 2) % 2)
1095b8e80941Smrg               +   2 * (byte_y % 2)
1096b8e80941Smrg               +   1 * (byte_x % 2);
1097b8e80941Smrg
1098b8e80941Smrg   if (swizzled) {
1099b8e80941Smrg      /* adjust for bit6 swizzling */
1100b8e80941Smrg      if (((byte_x / 8) % 2) == 1) {
1101b8e80941Smrg         if (((byte_y / 8) % 2) == 0) {
1102b8e80941Smrg            u += 64;
1103b8e80941Smrg         } else {
1104b8e80941Smrg            u -= 64;
1105b8e80941Smrg         }
1106b8e80941Smrg      }
1107b8e80941Smrg   }
1108b8e80941Smrg
1109b8e80941Smrg   return u;
1110b8e80941Smrg}
1111b8e80941Smrg
1112b8e80941Smrgstatic void
1113b8e80941Smrgiris_unmap_s8(struct iris_transfer *map)
1114b8e80941Smrg{
1115b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
1116b8e80941Smrg   const struct pipe_box *box = &xfer->box;
1117b8e80941Smrg   struct iris_resource *res = (struct iris_resource *) xfer->resource;
1118b8e80941Smrg   struct isl_surf *surf = &res->surf;
1119b8e80941Smrg   const bool has_swizzling = false;
1120b8e80941Smrg
1121b8e80941Smrg   if (xfer->usage & PIPE_TRANSFER_WRITE) {
1122b8e80941Smrg      uint8_t *untiled_s8_map = map->ptr;
1123b8e80941Smrg      uint8_t *tiled_s8_map =
1124b8e80941Smrg         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1125b8e80941Smrg
1126b8e80941Smrg      for (int s = 0; s < box->depth; s++) {
1127b8e80941Smrg         unsigned x0_el, y0_el;
1128b8e80941Smrg         get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
1129b8e80941Smrg
1130b8e80941Smrg         for (uint32_t y = 0; y < box->height; y++) {
1131b8e80941Smrg            for (uint32_t x = 0; x < box->width; x++) {
1132b8e80941Smrg               ptrdiff_t offset = s8_offset(surf->row_pitch_B,
1133b8e80941Smrg                                            x0_el + box->x + x,
1134b8e80941Smrg                                            y0_el + box->y + y,
1135b8e80941Smrg                                            has_swizzling);
1136b8e80941Smrg               tiled_s8_map[offset] =
1137b8e80941Smrg                  untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x];
1138b8e80941Smrg            }
1139b8e80941Smrg         }
1140b8e80941Smrg      }
1141b8e80941Smrg   }
1142b8e80941Smrg
1143b8e80941Smrg   free(map->buffer);
1144b8e80941Smrg}
1145b8e80941Smrg
1146b8e80941Smrgstatic void
1147b8e80941Smrgiris_map_s8(struct iris_transfer *map)
1148b8e80941Smrg{
1149b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
1150b8e80941Smrg   const struct pipe_box *box = &xfer->box;
1151b8e80941Smrg   struct iris_resource *res = (struct iris_resource *) xfer->resource;
1152b8e80941Smrg   struct isl_surf *surf = &res->surf;
1153b8e80941Smrg
1154b8e80941Smrg   xfer->stride = surf->row_pitch_B;
1155b8e80941Smrg   xfer->layer_stride = xfer->stride * box->height;
1156b8e80941Smrg
1157b8e80941Smrg   /* The tiling and detiling functions require that the linear buffer has
1158b8e80941Smrg    * a 16-byte alignment (that is, its `x0` is 16-byte aligned).  Here we
1159b8e80941Smrg    * over-allocate the linear buffer to get the proper alignment.
1160b8e80941Smrg    */
1161b8e80941Smrg   map->buffer = map->ptr = malloc(xfer->layer_stride * box->depth);
1162b8e80941Smrg   assert(map->buffer);
1163b8e80941Smrg
1164b8e80941Smrg   const bool has_swizzling = false;
1165b8e80941Smrg
1166b8e80941Smrg   /* One of either READ_BIT or WRITE_BIT or both is set.  READ_BIT implies no
1167b8e80941Smrg    * INVALIDATE_RANGE_BIT.  WRITE_BIT needs the original values read in unless
1168b8e80941Smrg    * invalidate is set, since we'll be writing the whole rectangle from our
1169b8e80941Smrg    * temporary buffer back out.
1170b8e80941Smrg    */
1171b8e80941Smrg   if (!(xfer->usage & PIPE_TRANSFER_DISCARD_RANGE)) {
1172b8e80941Smrg      uint8_t *untiled_s8_map = map->ptr;
1173b8e80941Smrg      uint8_t *tiled_s8_map =
1174b8e80941Smrg         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1175b8e80941Smrg
1176b8e80941Smrg      for (int s = 0; s < box->depth; s++) {
1177b8e80941Smrg         unsigned x0_el, y0_el;
1178b8e80941Smrg         get_image_offset_el(surf, xfer->level, box->z + s, &x0_el, &y0_el);
1179b8e80941Smrg
1180b8e80941Smrg         for (uint32_t y = 0; y < box->height; y++) {
1181b8e80941Smrg            for (uint32_t x = 0; x < box->width; x++) {
1182b8e80941Smrg               ptrdiff_t offset = s8_offset(surf->row_pitch_B,
1183b8e80941Smrg                                            x0_el + box->x + x,
1184b8e80941Smrg                                            y0_el + box->y + y,
1185b8e80941Smrg                                            has_swizzling);
1186b8e80941Smrg               untiled_s8_map[s * xfer->layer_stride + y * xfer->stride + x] =
1187b8e80941Smrg                  tiled_s8_map[offset];
1188b8e80941Smrg            }
1189b8e80941Smrg         }
1190b8e80941Smrg      }
1191b8e80941Smrg   }
1192b8e80941Smrg
1193b8e80941Smrg   map->unmap = iris_unmap_s8;
1194b8e80941Smrg}
1195b8e80941Smrg
1196b8e80941Smrg/* Compute extent parameters for use with tiled_memcpy functions.
1197b8e80941Smrg * xs are in units of bytes and ys are in units of strides.
1198b8e80941Smrg */
1199b8e80941Smrgstatic inline void
1200b8e80941Smrgtile_extents(const struct isl_surf *surf,
1201b8e80941Smrg             const struct pipe_box *box,
1202b8e80941Smrg             unsigned level, int z,
1203b8e80941Smrg             unsigned *x1_B, unsigned *x2_B,
1204b8e80941Smrg             unsigned *y1_el, unsigned *y2_el)
1205b8e80941Smrg{
1206b8e80941Smrg   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1207b8e80941Smrg   const unsigned cpp = fmtl->bpb / 8;
1208b8e80941Smrg
1209b8e80941Smrg   assert(box->x % fmtl->bw == 0);
1210b8e80941Smrg   assert(box->y % fmtl->bh == 0);
1211b8e80941Smrg
1212b8e80941Smrg   unsigned x0_el, y0_el;
1213b8e80941Smrg   get_image_offset_el(surf, level, box->z + z, &x0_el, &y0_el);
1214b8e80941Smrg
1215b8e80941Smrg   *x1_B = (box->x / fmtl->bw + x0_el) * cpp;
1216b8e80941Smrg   *y1_el = box->y / fmtl->bh + y0_el;
1217b8e80941Smrg   *x2_B = (DIV_ROUND_UP(box->x + box->width, fmtl->bw) + x0_el) * cpp;
1218b8e80941Smrg   *y2_el = DIV_ROUND_UP(box->y + box->height, fmtl->bh) + y0_el;
1219b8e80941Smrg}
1220b8e80941Smrg
1221b8e80941Smrgstatic void
1222b8e80941Smrgiris_unmap_tiled_memcpy(struct iris_transfer *map)
1223b8e80941Smrg{
1224b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
1225b8e80941Smrg   const struct pipe_box *box = &xfer->box;
1226b8e80941Smrg   struct iris_resource *res = (struct iris_resource *) xfer->resource;
1227b8e80941Smrg   struct isl_surf *surf = &res->surf;
1228b8e80941Smrg
1229b8e80941Smrg   const bool has_swizzling = false;
1230b8e80941Smrg
1231b8e80941Smrg   if (xfer->usage & PIPE_TRANSFER_WRITE) {
1232b8e80941Smrg      char *dst =
1233b8e80941Smrg         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1234b8e80941Smrg
1235b8e80941Smrg      for (int s = 0; s < box->depth; s++) {
1236b8e80941Smrg         unsigned x1, x2, y1, y2;
1237b8e80941Smrg         tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
1238b8e80941Smrg
1239b8e80941Smrg         void *ptr = map->ptr + s * xfer->layer_stride;
1240b8e80941Smrg
1241b8e80941Smrg         isl_memcpy_linear_to_tiled(x1, x2, y1, y2, dst, ptr,
1242b8e80941Smrg                                    surf->row_pitch_B, xfer->stride,
1243b8e80941Smrg                                    has_swizzling, surf->tiling, ISL_MEMCPY);
1244b8e80941Smrg      }
1245b8e80941Smrg   }
1246b8e80941Smrg   os_free_aligned(map->buffer);
1247b8e80941Smrg   map->buffer = map->ptr = NULL;
1248b8e80941Smrg}
1249b8e80941Smrg
1250b8e80941Smrgstatic void
1251b8e80941Smrgiris_map_tiled_memcpy(struct iris_transfer *map)
1252b8e80941Smrg{
1253b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
1254b8e80941Smrg   const struct pipe_box *box = &xfer->box;
1255b8e80941Smrg   struct iris_resource *res = (struct iris_resource *) xfer->resource;
1256b8e80941Smrg   struct isl_surf *surf = &res->surf;
1257b8e80941Smrg
1258b8e80941Smrg   xfer->stride = ALIGN(surf->row_pitch_B, 16);
1259b8e80941Smrg   xfer->layer_stride = xfer->stride * box->height;
1260b8e80941Smrg
1261b8e80941Smrg   unsigned x1, x2, y1, y2;
1262b8e80941Smrg   tile_extents(surf, box, xfer->level, 0, &x1, &x2, &y1, &y2);
1263b8e80941Smrg
1264b8e80941Smrg   /* The tiling and detiling functions require that the linear buffer has
1265b8e80941Smrg    * a 16-byte alignment (that is, its `x0` is 16-byte aligned).  Here we
1266b8e80941Smrg    * over-allocate the linear buffer to get the proper alignment.
1267b8e80941Smrg    */
1268b8e80941Smrg   map->buffer =
1269b8e80941Smrg      os_malloc_aligned(xfer->layer_stride * box->depth, 16);
1270b8e80941Smrg   assert(map->buffer);
1271b8e80941Smrg   map->ptr = (char *)map->buffer + (x1 & 0xf);
1272b8e80941Smrg
1273b8e80941Smrg   const bool has_swizzling = false;
1274b8e80941Smrg
1275b8e80941Smrg   // XXX: PIPE_TRANSFER_READ?
1276b8e80941Smrg   if (!(xfer->usage & PIPE_TRANSFER_DISCARD_RANGE)) {
1277b8e80941Smrg      char *src =
1278b8e80941Smrg         iris_bo_map(map->dbg, res->bo, (xfer->usage | MAP_RAW) & MAP_FLAGS);
1279b8e80941Smrg
1280b8e80941Smrg      for (int s = 0; s < box->depth; s++) {
1281b8e80941Smrg         unsigned x1, x2, y1, y2;
1282b8e80941Smrg         tile_extents(surf, box, xfer->level, s, &x1, &x2, &y1, &y2);
1283b8e80941Smrg
1284b8e80941Smrg         /* Use 's' rather than 'box->z' to rebase the first slice to 0. */
1285b8e80941Smrg         void *ptr = map->ptr + s * xfer->layer_stride;
1286b8e80941Smrg
1287b8e80941Smrg         isl_memcpy_tiled_to_linear(x1, x2, y1, y2, ptr, src, xfer->stride,
1288b8e80941Smrg                                    surf->row_pitch_B, has_swizzling,
1289b8e80941Smrg                                    surf->tiling, ISL_MEMCPY_STREAMING_LOAD);
1290b8e80941Smrg      }
1291b8e80941Smrg   }
1292b8e80941Smrg
1293b8e80941Smrg   map->unmap = iris_unmap_tiled_memcpy;
1294b8e80941Smrg}
1295b8e80941Smrg
1296b8e80941Smrgstatic void
1297b8e80941Smrgiris_map_direct(struct iris_transfer *map)
1298b8e80941Smrg{
1299b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
1300b8e80941Smrg   struct pipe_box *box = &xfer->box;
1301b8e80941Smrg   struct iris_resource *res = (struct iris_resource *) xfer->resource;
1302b8e80941Smrg
1303b8e80941Smrg   void *ptr = iris_bo_map(map->dbg, res->bo, xfer->usage & MAP_FLAGS);
1304b8e80941Smrg
1305b8e80941Smrg   if (res->base.target == PIPE_BUFFER) {
1306b8e80941Smrg      xfer->stride = 0;
1307b8e80941Smrg      xfer->layer_stride = 0;
1308b8e80941Smrg
1309b8e80941Smrg      map->ptr = ptr + box->x;
1310b8e80941Smrg   } else {
1311b8e80941Smrg      struct isl_surf *surf = &res->surf;
1312b8e80941Smrg      const struct isl_format_layout *fmtl =
1313b8e80941Smrg         isl_format_get_layout(surf->format);
1314b8e80941Smrg      const unsigned cpp = fmtl->bpb / 8;
1315b8e80941Smrg      unsigned x0_el, y0_el;
1316b8e80941Smrg
1317b8e80941Smrg      get_image_offset_el(surf, xfer->level, box->z, &x0_el, &y0_el);
1318b8e80941Smrg
1319b8e80941Smrg      xfer->stride = isl_surf_get_row_pitch_B(surf);
1320b8e80941Smrg      xfer->layer_stride = isl_surf_get_array_pitch(surf);
1321b8e80941Smrg
1322b8e80941Smrg      map->ptr = ptr + (y0_el + box->y) * xfer->stride + (x0_el + box->x) * cpp;
1323b8e80941Smrg   }
1324b8e80941Smrg}
1325b8e80941Smrg
1326b8e80941Smrgstatic bool
1327b8e80941Smrgcan_promote_to_async(const struct iris_resource *res,
1328b8e80941Smrg                     const struct pipe_box *box,
1329b8e80941Smrg                     enum pipe_transfer_usage usage)
1330b8e80941Smrg{
1331b8e80941Smrg   /* If we're writing to a section of the buffer that hasn't even been
1332b8e80941Smrg    * initialized with useful data, then we can safely promote this write
1333b8e80941Smrg    * to be unsynchronized.  This helps the common pattern of appending data.
1334b8e80941Smrg    */
1335b8e80941Smrg   return res->base.target == PIPE_BUFFER && (usage & PIPE_TRANSFER_WRITE) &&
1336b8e80941Smrg          !(usage & TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED) &&
1337b8e80941Smrg          !util_ranges_intersect(&res->valid_buffer_range, box->x,
1338b8e80941Smrg                                 box->x + box->width);
1339b8e80941Smrg}
1340b8e80941Smrg
1341b8e80941Smrgstatic void *
1342b8e80941Smrgiris_transfer_map(struct pipe_context *ctx,
1343b8e80941Smrg                  struct pipe_resource *resource,
1344b8e80941Smrg                  unsigned level,
1345b8e80941Smrg                  enum pipe_transfer_usage usage,
1346b8e80941Smrg                  const struct pipe_box *box,
1347b8e80941Smrg                  struct pipe_transfer **ptransfer)
1348b8e80941Smrg{
1349b8e80941Smrg   struct iris_context *ice = (struct iris_context *)ctx;
1350b8e80941Smrg   struct iris_resource *res = (struct iris_resource *)resource;
1351b8e80941Smrg   struct isl_surf *surf = &res->surf;
1352b8e80941Smrg
1353b8e80941Smrg   if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
1354b8e80941Smrg      /* Replace the backing storage with a fresh buffer for non-async maps */
1355b8e80941Smrg      if (!(usage & (PIPE_TRANSFER_UNSYNCHRONIZED |
1356b8e80941Smrg                     TC_TRANSFER_MAP_NO_INVALIDATE)))
1357b8e80941Smrg         iris_invalidate_resource(ctx, resource);
1358b8e80941Smrg
1359b8e80941Smrg      /* If we can discard the whole resource, we can discard the range. */
1360b8e80941Smrg      usage |= PIPE_TRANSFER_DISCARD_RANGE;
1361b8e80941Smrg   }
1362b8e80941Smrg
1363b8e80941Smrg   bool map_would_stall = false;
1364b8e80941Smrg
1365b8e80941Smrg   if (resource->target != PIPE_BUFFER) {
1366b8e80941Smrg      iris_resource_access_raw(ice, &ice->batches[IRIS_BATCH_RENDER], res,
1367b8e80941Smrg                               level, box->z, box->depth,
1368b8e80941Smrg                               usage & PIPE_TRANSFER_WRITE);
1369b8e80941Smrg   }
1370b8e80941Smrg
1371b8e80941Smrg   if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED) &&
1372b8e80941Smrg       can_promote_to_async(res, box, usage)) {
1373b8e80941Smrg      usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
1374b8e80941Smrg   }
1375b8e80941Smrg
1376b8e80941Smrg   if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
1377b8e80941Smrg      map_would_stall = resource_is_busy(ice, res);
1378b8e80941Smrg
1379b8e80941Smrg      if (map_would_stall && (usage & PIPE_TRANSFER_DONTBLOCK) &&
1380b8e80941Smrg                             (usage & PIPE_TRANSFER_MAP_DIRECTLY))
1381b8e80941Smrg         return NULL;
1382b8e80941Smrg   }
1383b8e80941Smrg
1384b8e80941Smrg   if (surf->tiling != ISL_TILING_LINEAR &&
1385b8e80941Smrg       (usage & PIPE_TRANSFER_MAP_DIRECTLY))
1386b8e80941Smrg      return NULL;
1387b8e80941Smrg
1388b8e80941Smrg   struct iris_transfer *map = slab_alloc(&ice->transfer_pool);
1389b8e80941Smrg   struct pipe_transfer *xfer = &map->base;
1390b8e80941Smrg
1391b8e80941Smrg   if (!map)
1392b8e80941Smrg      return NULL;
1393b8e80941Smrg
1394b8e80941Smrg   memset(map, 0, sizeof(*map));
1395b8e80941Smrg   map->dbg = &ice->dbg;
1396b8e80941Smrg
1397b8e80941Smrg   pipe_resource_reference(&xfer->resource, resource);
1398b8e80941Smrg   xfer->level = level;
1399b8e80941Smrg   xfer->usage = usage;
1400b8e80941Smrg   xfer->box = *box;
1401b8e80941Smrg   *ptransfer = xfer;
1402b8e80941Smrg
1403b8e80941Smrg   if (usage & PIPE_TRANSFER_WRITE)
1404b8e80941Smrg      util_range_add(&res->valid_buffer_range, box->x, box->x + box->width);
1405b8e80941Smrg
1406b8e80941Smrg   /* Avoid using GPU copies for persistent/coherent buffers, as the idea
1407b8e80941Smrg    * there is to access them simultaneously on the CPU & GPU.  This also
1408b8e80941Smrg    * avoids trying to use GPU copies for our u_upload_mgr buffers which
1409b8e80941Smrg    * contain state we're constructing for a GPU draw call, which would
1410b8e80941Smrg    * kill us with infinite stack recursion.
1411b8e80941Smrg    */
1412b8e80941Smrg   bool no_gpu = usage & (PIPE_TRANSFER_PERSISTENT |
1413b8e80941Smrg                          PIPE_TRANSFER_COHERENT |
1414b8e80941Smrg                          PIPE_TRANSFER_MAP_DIRECTLY);
1415b8e80941Smrg
1416b8e80941Smrg   /* GPU copies are not useful for buffer reads.  Instead of stalling to
1417b8e80941Smrg    * read from the original buffer, we'd simply copy it to a temporary...
1418b8e80941Smrg    * then stall (a bit longer) to read from that buffer.
1419b8e80941Smrg    *
1420b8e80941Smrg    * Images are less clear-cut.  Color resolves are destructive, removing
1421b8e80941Smrg    * the underlying compression, so we'd rather blit the data to a linear
1422b8e80941Smrg    * temporary and map that, to avoid the resolve.  (It might be better to
1423b8e80941Smrg    * a tiled temporary and use the tiled_memcpy paths...)
1424b8e80941Smrg    */
1425b8e80941Smrg   if (!(usage & PIPE_TRANSFER_DISCARD_RANGE) &&
1426b8e80941Smrg       res->aux.usage != ISL_AUX_USAGE_CCS_E &&
1427b8e80941Smrg       res->aux.usage != ISL_AUX_USAGE_CCS_D) {
1428b8e80941Smrg      no_gpu = true;
1429b8e80941Smrg   }
1430b8e80941Smrg
1431b8e80941Smrg   const struct isl_format_layout *fmtl = isl_format_get_layout(surf->format);
1432b8e80941Smrg   if (fmtl->txc == ISL_TXC_ASTC)
1433b8e80941Smrg      no_gpu = true;
1434b8e80941Smrg
1435b8e80941Smrg   if ((map_would_stall || res->aux.usage == ISL_AUX_USAGE_CCS_E) && !no_gpu) {
1436b8e80941Smrg      /* If we need a synchronous mapping and the resource is busy,
1437b8e80941Smrg       * we copy to/from a linear temporary buffer using the GPU.
1438b8e80941Smrg       */
1439b8e80941Smrg      map->batch = &ice->batches[IRIS_BATCH_RENDER];
1440b8e80941Smrg      map->blorp = &ice->blorp;
1441b8e80941Smrg      iris_map_copy_region(map);
1442b8e80941Smrg   } else {
1443b8e80941Smrg      /* Otherwise we're free to map on the CPU.  Flush if needed. */
1444b8e80941Smrg      if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
1445b8e80941Smrg         for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
1446b8e80941Smrg            if (iris_batch_references(&ice->batches[i], res->bo))
1447b8e80941Smrg               iris_batch_flush(&ice->batches[i]);
1448b8e80941Smrg         }
1449b8e80941Smrg      }
1450b8e80941Smrg
1451b8e80941Smrg      if (surf->tiling == ISL_TILING_W) {
1452b8e80941Smrg         /* TODO: Teach iris_map_tiled_memcpy about W-tiling... */
1453b8e80941Smrg         iris_map_s8(map);
1454b8e80941Smrg      } else if (surf->tiling != ISL_TILING_LINEAR) {
1455b8e80941Smrg         iris_map_tiled_memcpy(map);
1456b8e80941Smrg      } else {
1457b8e80941Smrg         iris_map_direct(map);
1458b8e80941Smrg      }
1459b8e80941Smrg   }
1460b8e80941Smrg
1461b8e80941Smrg   return map->ptr;
1462b8e80941Smrg}
1463b8e80941Smrg
1464b8e80941Smrgstatic void
1465b8e80941Smrgiris_transfer_flush_region(struct pipe_context *ctx,
1466b8e80941Smrg                           struct pipe_transfer *xfer,
1467b8e80941Smrg                           const struct pipe_box *box)
1468b8e80941Smrg{
1469b8e80941Smrg   struct iris_context *ice = (struct iris_context *)ctx;
1470b8e80941Smrg   struct iris_resource *res = (struct iris_resource *) xfer->resource;
1471b8e80941Smrg   struct iris_transfer *map = (void *) xfer;
1472b8e80941Smrg
1473b8e80941Smrg   if (map->staging)
1474b8e80941Smrg      iris_flush_staging_region(xfer, box);
1475b8e80941Smrg
1476b8e80941Smrg   for (int i = 0; i < IRIS_BATCH_COUNT; i++) {
1477b8e80941Smrg      if (ice->batches[i].contains_draw ||
1478b8e80941Smrg          ice->batches[i].cache.render->entries) {
1479b8e80941Smrg         iris_batch_maybe_flush(&ice->batches[i], 24);
1480b8e80941Smrg         iris_flush_and_dirty_for_history(ice, &ice->batches[i], res);
1481b8e80941Smrg      }
1482b8e80941Smrg   }
1483b8e80941Smrg
1484b8e80941Smrg   /* Make sure we flag constants dirty even if there's no need to emit
1485b8e80941Smrg    * any PIPE_CONTROLs to a batch.
1486b8e80941Smrg    */
1487b8e80941Smrg   iris_dirty_for_history(ice, res);
1488b8e80941Smrg}
1489b8e80941Smrg
1490b8e80941Smrgstatic void
1491b8e80941Smrgiris_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *xfer)
1492b8e80941Smrg{
1493b8e80941Smrg   struct iris_context *ice = (struct iris_context *)ctx;
1494b8e80941Smrg   struct iris_transfer *map = (void *) xfer;
1495b8e80941Smrg
1496b8e80941Smrg   if (!(xfer->usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
1497b8e80941Smrg      struct pipe_box flush_box = {
1498b8e80941Smrg         .x = 0, .y = 0, .z = 0,
1499b8e80941Smrg         .width  = xfer->box.width,
1500b8e80941Smrg         .height = xfer->box.height,
1501b8e80941Smrg         .depth  = xfer->box.depth,
1502b8e80941Smrg      };
1503b8e80941Smrg      iris_transfer_flush_region(ctx, xfer, &flush_box);
1504b8e80941Smrg   }
1505b8e80941Smrg
1506b8e80941Smrg   if (map->unmap)
1507b8e80941Smrg      map->unmap(map);
1508b8e80941Smrg
1509b8e80941Smrg   pipe_resource_reference(&xfer->resource, NULL);
1510b8e80941Smrg   slab_free(&ice->transfer_pool, map);
1511b8e80941Smrg}
1512b8e80941Smrg
1513b8e80941Smrg/**
1514b8e80941Smrg * Mark state dirty that needs to be re-emitted when a resource is written.
1515b8e80941Smrg */
1516b8e80941Smrgvoid
1517b8e80941Smrgiris_dirty_for_history(struct iris_context *ice,
1518b8e80941Smrg                       struct iris_resource *res)
1519b8e80941Smrg{
1520b8e80941Smrg   uint64_t dirty = 0ull;
1521b8e80941Smrg
1522b8e80941Smrg   if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
1523b8e80941Smrg      dirty |= IRIS_DIRTY_CONSTANTS_VS |
1524b8e80941Smrg               IRIS_DIRTY_CONSTANTS_TCS |
1525b8e80941Smrg               IRIS_DIRTY_CONSTANTS_TES |
1526b8e80941Smrg               IRIS_DIRTY_CONSTANTS_GS |
1527b8e80941Smrg               IRIS_DIRTY_CONSTANTS_FS |
1528b8e80941Smrg               IRIS_DIRTY_CONSTANTS_CS |
1529b8e80941Smrg               IRIS_ALL_DIRTY_BINDINGS;
1530b8e80941Smrg   }
1531b8e80941Smrg
1532b8e80941Smrg   ice->state.dirty |= dirty;
1533b8e80941Smrg}
1534b8e80941Smrg
1535b8e80941Smrg/**
1536b8e80941Smrg * Produce a set of PIPE_CONTROL bits which ensure data written to a
1537b8e80941Smrg * resource becomes visible, and any stale read cache data is invalidated.
1538b8e80941Smrg */
1539b8e80941Smrguint32_t
1540b8e80941Smrgiris_flush_bits_for_history(struct iris_resource *res)
1541b8e80941Smrg{
1542b8e80941Smrg   uint32_t flush = PIPE_CONTROL_CS_STALL;
1543b8e80941Smrg
1544b8e80941Smrg   if (res->bind_history & PIPE_BIND_CONSTANT_BUFFER) {
1545b8e80941Smrg      flush |= PIPE_CONTROL_CONST_CACHE_INVALIDATE |
1546b8e80941Smrg               PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1547b8e80941Smrg   }
1548b8e80941Smrg
1549b8e80941Smrg   if (res->bind_history & PIPE_BIND_SAMPLER_VIEW)
1550b8e80941Smrg      flush |= PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
1551b8e80941Smrg
1552b8e80941Smrg   if (res->bind_history & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER))
1553b8e80941Smrg      flush |= PIPE_CONTROL_VF_CACHE_INVALIDATE;
1554b8e80941Smrg
1555b8e80941Smrg   if (res->bind_history & (PIPE_BIND_SHADER_BUFFER | PIPE_BIND_SHADER_IMAGE))
1556b8e80941Smrg      flush |= PIPE_CONTROL_DATA_CACHE_FLUSH;
1557b8e80941Smrg
1558b8e80941Smrg   return flush;
1559b8e80941Smrg}
1560b8e80941Smrg
1561b8e80941Smrgvoid
1562b8e80941Smrgiris_flush_and_dirty_for_history(struct iris_context *ice,
1563b8e80941Smrg                                 struct iris_batch *batch,
1564b8e80941Smrg                                 struct iris_resource *res)
1565b8e80941Smrg{
1566b8e80941Smrg   if (res->base.target != PIPE_BUFFER)
1567b8e80941Smrg      return;
1568b8e80941Smrg
1569b8e80941Smrg   uint32_t flush = iris_flush_bits_for_history(res);
1570b8e80941Smrg
1571b8e80941Smrg   /* We've likely used the rendering engine (i.e. BLORP) to write to this
1572b8e80941Smrg    * surface.  Flush the render cache so the data actually lands.
1573b8e80941Smrg    */
1574b8e80941Smrg   if (batch->name != IRIS_BATCH_COMPUTE)
1575b8e80941Smrg      flush |= PIPE_CONTROL_RENDER_TARGET_FLUSH;
1576b8e80941Smrg
1577b8e80941Smrg   iris_emit_pipe_control_flush(batch, flush);
1578b8e80941Smrg
1579b8e80941Smrg   iris_dirty_for_history(ice, res);
1580b8e80941Smrg}
1581b8e80941Smrg
1582b8e80941Smrgbool
1583b8e80941Smrgiris_resource_set_clear_color(struct iris_context *ice,
1584b8e80941Smrg                              struct iris_resource *res,
1585b8e80941Smrg                              union isl_color_value color)
1586b8e80941Smrg{
1587b8e80941Smrg   if (memcmp(&res->aux.clear_color, &color, sizeof(color)) != 0) {
1588b8e80941Smrg      res->aux.clear_color = color;
1589b8e80941Smrg      return true;
1590b8e80941Smrg   }
1591b8e80941Smrg
1592b8e80941Smrg   return false;
1593b8e80941Smrg}
1594b8e80941Smrg
1595b8e80941Smrgunion isl_color_value
1596b8e80941Smrgiris_resource_get_clear_color(const struct iris_resource *res,
1597b8e80941Smrg                              struct iris_bo **clear_color_bo,
1598b8e80941Smrg                              uint64_t *clear_color_offset)
1599b8e80941Smrg{
1600b8e80941Smrg   assert(res->aux.bo);
1601b8e80941Smrg
1602b8e80941Smrg   if (clear_color_bo)
1603b8e80941Smrg      *clear_color_bo = res->aux.clear_color_bo;
1604b8e80941Smrg   if (clear_color_offset)
1605b8e80941Smrg      *clear_color_offset = res->aux.clear_color_offset;
1606b8e80941Smrg   return res->aux.clear_color;
1607b8e80941Smrg}
1608b8e80941Smrg
1609b8e80941Smrgstatic enum pipe_format
1610b8e80941Smrgiris_resource_get_internal_format(struct pipe_resource *p_res)
1611b8e80941Smrg{
1612b8e80941Smrg   struct iris_resource *res = (void *) p_res;
1613b8e80941Smrg   return res->internal_format;
1614b8e80941Smrg}
1615b8e80941Smrg
1616b8e80941Smrgstatic const struct u_transfer_vtbl transfer_vtbl = {
1617b8e80941Smrg   .resource_create       = iris_resource_create,
1618b8e80941Smrg   .resource_destroy      = iris_resource_destroy,
1619b8e80941Smrg   .transfer_map          = iris_transfer_map,
1620b8e80941Smrg   .transfer_unmap        = iris_transfer_unmap,
1621b8e80941Smrg   .transfer_flush_region = iris_transfer_flush_region,
1622b8e80941Smrg   .get_internal_format   = iris_resource_get_internal_format,
1623b8e80941Smrg   .set_stencil           = iris_resource_set_separate_stencil,
1624b8e80941Smrg   .get_stencil           = iris_resource_get_separate_stencil,
1625b8e80941Smrg};
1626b8e80941Smrg
1627b8e80941Smrgvoid
1628b8e80941Smrgiris_init_screen_resource_functions(struct pipe_screen *pscreen)
1629b8e80941Smrg{
1630b8e80941Smrg   pscreen->query_dmabuf_modifiers = iris_query_dmabuf_modifiers;
1631b8e80941Smrg   pscreen->resource_create_with_modifiers =
1632b8e80941Smrg      iris_resource_create_with_modifiers;
1633b8e80941Smrg   pscreen->resource_create = u_transfer_helper_resource_create;
1634b8e80941Smrg   pscreen->resource_from_user_memory = iris_resource_from_user_memory;
1635b8e80941Smrg   pscreen->resource_from_handle = iris_resource_from_handle;
1636b8e80941Smrg   pscreen->resource_get_handle = iris_resource_get_handle;
1637b8e80941Smrg   pscreen->resource_destroy = u_transfer_helper_resource_destroy;
1638b8e80941Smrg   pscreen->transfer_helper =
1639b8e80941Smrg      u_transfer_helper_create(&transfer_vtbl, true, true, false, true);
1640b8e80941Smrg}
1641b8e80941Smrg
1642b8e80941Smrgvoid
1643b8e80941Smrgiris_init_resource_functions(struct pipe_context *ctx)
1644b8e80941Smrg{
1645b8e80941Smrg   ctx->flush_resource = iris_flush_resource;
1646b8e80941Smrg   ctx->invalidate_resource = iris_invalidate_resource;
1647b8e80941Smrg   ctx->transfer_map = u_transfer_helper_transfer_map;
1648b8e80941Smrg   ctx->transfer_flush_region = u_transfer_helper_transfer_flush_region;
1649b8e80941Smrg   ctx->transfer_unmap = u_transfer_helper_transfer_unmap;
1650b8e80941Smrg   ctx->buffer_subdata = u_default_buffer_subdata;
1651b8e80941Smrg   ctx->texture_subdata = u_default_texture_subdata;
1652b8e80941Smrg}
1653