aedc12f125b301ebc4cc31bc874ecb1e9df5194a
[mesa.git] / src / gallium / drivers / iris / iris_resolve.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_resolve.c
25 *
26 * This file handles resolve tracking for main and auxiliary surfaces.
27 *
28 * It also handles our cache tracking. We have sets for the render cache,
29 * depth cache, and so on. If a BO is in a cache's set, then it may have
30 * data in that cache. The helpers take care of emitting flushes for
31 * render-to-texture, format reinterpretation issues, and other situations.
32 */
33
34 #include "util/hash_table.h"
35 #include "util/set.h"
36 #include "iris_context.h"
37
38 /**
39 * Disable auxiliary buffers if a renderbuffer is also bound as a texture
40 * or shader image. This causes a self-dependency, where both rendering
41 * and sampling may concurrently read or write the CCS buffer, causing
42 * incorrect pixels.
43 */
44 static bool
45 disable_rb_aux_buffer(struct iris_context *ice,
46 bool *draw_aux_buffer_disabled,
47 struct iris_resource *tex_res,
48 unsigned min_level, unsigned num_levels,
49 const char *usage)
50 {
51 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
52 bool found = false;
53
54 /* We only need to worry about color compression and fast clears. */
55 if (tex_res->aux.usage != ISL_AUX_USAGE_CCS_D &&
56 tex_res->aux.usage != ISL_AUX_USAGE_CCS_E)
57 return false;
58
59 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
60 struct iris_surface *surf = (void *) cso_fb->cbufs[i];
61 if (!surf)
62 continue;
63
64 struct iris_resource *rb_res = (void *) surf->base.texture;
65
66 if (rb_res->bo == tex_res->bo &&
67 surf->base.u.tex.level >= min_level &&
68 surf->base.u.tex.level < min_level + num_levels) {
69 found = draw_aux_buffer_disabled[i] = true;
70 }
71 }
72
73 if (found) {
74 perf_debug(&ice->dbg,
75 "Disabling CCS because a renderbuffer is also bound %s.\n",
76 usage);
77 }
78
79 return found;
80 }
81
82 static void
83 resolve_sampler_views(struct iris_context *ice,
84 struct iris_batch *batch,
85 struct iris_shader_state *shs,
86 bool *draw_aux_buffer_disabled)
87 {
88 uint32_t views = shs->bound_sampler_views;
89
90 unsigned astc5x5_wa_bits = 0; // XXX: actual tracking
91
92 while (views) {
93 const int i = u_bit_scan(&views);
94 struct iris_sampler_view *isv = shs->textures[i];
95 struct iris_resource *res = (void *) isv->base.texture;
96
97 if (batch->name != IRIS_BATCH_COMPUTE) {
98 disable_rb_aux_buffer(ice, draw_aux_buffer_disabled,
99 res, isv->view.base_level, isv->view.levels,
100 "for sampling");
101 }
102
103 iris_resource_prepare_texture(ice, batch, res, isv->view.format,
104 isv->view.base_level, isv->view.levels,
105 isv->view.base_array_layer,
106 isv->view.array_len,
107 astc5x5_wa_bits);
108
109 iris_cache_flush_for_read(batch, res->bo);
110 }
111 }
112
113 static void
114 resolve_image_views(struct iris_context *ice,
115 struct iris_batch *batch,
116 struct iris_shader_state *shs,
117 bool *draw_aux_buffer_disabled)
118 {
119 uint32_t views = shs->bound_image_views;
120
121 while (views) {
122 const int i = u_bit_scan(&views);
123 struct iris_resource *res = (void *) shs->image[i].res;
124
125 if (batch->name != IRIS_BATCH_COMPUTE) {
126 disable_rb_aux_buffer(ice, draw_aux_buffer_disabled,
127 res, 0, ~0, "as a shader image");
128 }
129
130 iris_resource_prepare_image(ice, batch, res);
131
132 iris_cache_flush_for_read(batch, res->bo);
133 }
134 }
135
136
137 /**
138 * \brief Resolve buffers before drawing.
139 *
140 * Resolve the depth buffer's HiZ buffer, resolve the depth buffer of each
141 * enabled depth texture, and flush the render cache for any dirty textures.
142 */
143 void
144 iris_predraw_resolve_inputs(struct iris_context *ice,
145 struct iris_batch *batch,
146 struct iris_shader_state *shs,
147 bool *draw_aux_buffer_disabled)
148 {
149 resolve_sampler_views(ice, batch, shs, draw_aux_buffer_disabled);
150 resolve_image_views(ice, batch, shs, draw_aux_buffer_disabled);
151
152 // XXX: ASTC hacks
153 }
154
155 void
156 iris_predraw_resolve_framebuffer(struct iris_context *ice,
157 struct iris_batch *batch,
158 bool *draw_aux_buffer_disabled)
159 {
160 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
161 struct pipe_surface *zs_surf = cso_fb->zsbuf;
162
163 if (zs_surf) {
164 struct iris_resource *z_res, *s_res;
165 iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);
166 unsigned num_layers =
167 zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;
168
169 if (z_res) {
170 iris_resource_prepare_depth(ice, batch, z_res, zs_surf->u.tex.level,
171 zs_surf->u.tex.first_layer, num_layers);
172 }
173 }
174
175 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
176 struct iris_surface *surf = (void *) cso_fb->cbufs[i];
177 if (!surf)
178 continue;
179
180 struct iris_resource *res = (void *) surf->base.texture;
181
182 enum isl_aux_usage aux_usage =
183 iris_resource_render_aux_usage(ice, res, surf->view.format,
184 ice->state.blend_enables & (1u << i),
185 draw_aux_buffer_disabled[i]);
186
187 // XXX: NEW_AUX_STATE
188 ice->state.draw_aux_usage[i] = aux_usage;
189
190 iris_resource_prepare_render(ice, batch, res, surf->view.base_level,
191 surf->view.base_array_layer,
192 surf->view.array_len,
193 aux_usage);
194
195 iris_cache_flush_for_render(batch, res->bo, surf->view.format,
196 ISL_AUX_USAGE_NONE);
197 }
198 }
199
200 /**
201 * \brief Call this after drawing to mark which buffers need resolving
202 *
203 * If the depth buffer was written to and if it has an accompanying HiZ
204 * buffer, then mark that it needs a depth resolve.
205 *
206 * If the color buffer is a multisample window system buffer, then
207 * mark that it needs a downsample.
208 *
209 * Also mark any render targets which will be textured as needing a render
210 * cache flush.
211 */
212 void
213 iris_postdraw_update_resolve_tracking(struct iris_context *ice,
214 struct iris_batch *batch)
215 {
216 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
217 struct pipe_surface *zs_surf = cso_fb->zsbuf;
218
219 // XXX: front buffer drawing?
220
221 if (zs_surf) {
222 struct iris_resource *z_res, *s_res;
223 iris_get_depth_stencil_resources(zs_surf->texture, &z_res, &s_res);
224 unsigned num_layers =
225 zs_surf->u.tex.last_layer - zs_surf->u.tex.first_layer + 1;
226
227 if (z_res) {
228 iris_resource_finish_depth(ice, z_res, zs_surf->u.tex.level,
229 zs_surf->u.tex.first_layer, num_layers,
230 ice->state.depth_writes_enabled);
231
232 if (ice->state.depth_writes_enabled)
233 iris_depth_cache_add_bo(batch, z_res->bo);
234 }
235
236 if (s_res) {
237 iris_resource_finish_write(ice, s_res, zs_surf->u.tex.level,
238 zs_surf->u.tex.first_layer, num_layers,
239 ISL_AUX_USAGE_NONE);
240
241 if (ice->state.stencil_writes_enabled)
242 iris_depth_cache_add_bo(batch, s_res->bo);
243 }
244 }
245
246 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
247 struct iris_surface *surf = (void *) cso_fb->cbufs[i];
248 if (!surf)
249 continue;
250
251 struct iris_resource *res = (void *) surf->base.texture;
252 union pipe_surface_desc *desc = &surf->base.u;
253 unsigned num_layers = desc->tex.last_layer - desc->tex.first_layer + 1;
254 enum isl_aux_usage aux_usage = ice->state.draw_aux_usage[i];
255
256 iris_render_cache_add_bo(batch, res->bo, surf->view.format, aux_usage);
257
258 iris_resource_finish_render(ice, res, desc->tex.level,
259 desc->tex.first_layer, num_layers,
260 aux_usage);
261 }
262 }
263
264 /**
265 * Clear the cache-tracking sets.
266 */
267 void
268 iris_cache_sets_clear(struct iris_batch *batch)
269 {
270 hash_table_foreach(batch->cache.render, render_entry)
271 _mesa_hash_table_remove(batch->cache.render, render_entry);
272
273 set_foreach(batch->cache.depth, depth_entry)
274 _mesa_set_remove(batch->cache.depth, depth_entry);
275 }
276
277 /**
278 * Emits an appropriate flush for a BO if it has been rendered to within the
279 * same batchbuffer as a read that's about to be emitted.
280 *
281 * The GPU has separate, incoherent caches for the render cache and the
282 * sampler cache, along with other caches. Usually data in the different
283 * caches don't interact (e.g. we don't render to our driver-generated
284 * immediate constant data), but for render-to-texture in FBOs we definitely
285 * do. When a batchbuffer is flushed, the kernel will ensure that everything
286 * necessary is flushed before another use of that BO, but for reuse from
287 * different caches within a batchbuffer, it's all our responsibility.
288 */
289 void
290 iris_flush_depth_and_render_caches(struct iris_batch *batch)
291 {
292 iris_emit_pipe_control_flush(batch,
293 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
294 PIPE_CONTROL_RENDER_TARGET_FLUSH |
295 PIPE_CONTROL_CS_STALL);
296
297 iris_emit_pipe_control_flush(batch,
298 PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE |
299 PIPE_CONTROL_CONST_CACHE_INVALIDATE);
300
301 iris_cache_sets_clear(batch);
302 }
303
304 void
305 iris_cache_flush_for_read(struct iris_batch *batch,
306 struct iris_bo *bo)
307 {
308 if (_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo) ||
309 _mesa_set_search_pre_hashed(batch->cache.depth, bo->hash, bo))
310 iris_flush_depth_and_render_caches(batch);
311 }
312
313 static void *
314 format_aux_tuple(enum isl_format format, enum isl_aux_usage aux_usage)
315 {
316 return (void *)(uintptr_t)((uint32_t)format << 8 | aux_usage);
317 }
318
319 void
320 iris_cache_flush_for_render(struct iris_batch *batch,
321 struct iris_bo *bo,
322 enum isl_format format,
323 enum isl_aux_usage aux_usage)
324 {
325 if (_mesa_set_search_pre_hashed(batch->cache.depth, bo->hash, bo))
326 iris_flush_depth_and_render_caches(batch);
327
328 /* Check to see if this bo has been used by a previous rendering operation
329 * but with a different format or aux usage. If it has, flush the render
330 * cache so we ensure that it's only in there with one format or aux usage
331 * at a time.
332 *
333 * Even though it's not obvious, this can easily happen in practice.
334 * Suppose a client is blending on a surface with sRGB encode enabled on
335 * gen9. This implies that you get AUX_USAGE_CCS_D at best. If the client
336 * then disables sRGB decode and continues blending we will flip on
337 * AUX_USAGE_CCS_E without doing any sort of resolve in-between (this is
338 * perfectly valid since CCS_E is a subset of CCS_D). However, this means
339 * that we have fragments in-flight which are rendering with UNORM+CCS_E
340 * and other fragments in-flight with SRGB+CCS_D on the same surface at the
341 * same time and the pixel scoreboard and color blender are trying to sort
342 * it all out. This ends badly (i.e. GPU hangs).
343 *
344 * To date, we have never observed GPU hangs or even corruption to be
345 * associated with switching the format, only the aux usage. However,
346 * there are comments in various docs which indicate that the render cache
347 * isn't 100% resilient to format changes. We may as well be conservative
348 * and flush on format changes too. We can always relax this later if we
349 * find it to be a performance problem.
350 */
351 struct hash_entry *entry =
352 _mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo);
353 if (entry && entry->data != format_aux_tuple(format, aux_usage))
354 iris_flush_depth_and_render_caches(batch);
355 }
356
357 void
358 iris_render_cache_add_bo(struct iris_batch *batch,
359 struct iris_bo *bo,
360 enum isl_format format,
361 enum isl_aux_usage aux_usage)
362 {
363 #ifndef NDEBUG
364 struct hash_entry *entry =
365 _mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo);
366 if (entry) {
367 /* Otherwise, someone didn't do a flush_for_render and that would be
368 * very bad indeed.
369 */
370 assert(entry->data == format_aux_tuple(format, aux_usage));
371 }
372 #endif
373
374 _mesa_hash_table_insert_pre_hashed(batch->cache.render, bo->hash, bo,
375 format_aux_tuple(format, aux_usage));
376 }
377
378 void
379 iris_cache_flush_for_depth(struct iris_batch *batch,
380 struct iris_bo *bo)
381 {
382 if (_mesa_hash_table_search_pre_hashed(batch->cache.render, bo->hash, bo))
383 iris_flush_depth_and_render_caches(batch);
384 }
385
386 void
387 iris_depth_cache_add_bo(struct iris_batch *batch, struct iris_bo *bo)
388 {
389 _mesa_set_add_pre_hashed(batch->cache.depth, bo->hash, bo);
390 }
391
392 static void
393 iris_resolve_color(struct iris_context *ice,
394 struct iris_batch *batch,
395 struct iris_resource *res,
396 unsigned level, unsigned layer,
397 enum isl_aux_op resolve_op)
398 {
399 //DBG("%s to mt %p level %u layer %u\n", __FUNCTION__, mt, level, layer);
400
401 struct blorp_surf surf;
402 iris_blorp_surf_for_resource(&surf, &res->base, res->aux.usage, true);
403
404 iris_batch_maybe_flush(batch, 1500);
405
406 /* Ivybridge PRM Vol 2, Part 1, "11.7 MCS Buffer for Render Target(s)":
407 *
408 * "Any transition from any value in {Clear, Render, Resolve} to a
409 * different value in {Clear, Render, Resolve} requires end of pipe
410 * synchronization."
411 *
412 * In other words, fast clear ops are not properly synchronized with
413 * other drawing. We need to use a PIPE_CONTROL to ensure that the
414 * contents of the previous draw hit the render target before we resolve
415 * and again afterwards to ensure that the resolve is complete before we
416 * do any more regular drawing.
417 */
418 iris_emit_end_of_pipe_sync(batch, PIPE_CONTROL_RENDER_TARGET_FLUSH);
419
420 struct blorp_batch blorp_batch;
421 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
422 blorp_ccs_resolve(&blorp_batch, &surf, level, layer, 1,
423 isl_format_srgb_to_linear(res->surf.format),
424 resolve_op);
425 blorp_batch_finish(&blorp_batch);
426
427 /* See comment above */
428 iris_emit_end_of_pipe_sync(batch, PIPE_CONTROL_RENDER_TARGET_FLUSH);
429 }
430
431 static void
432 iris_mcs_partial_resolve(struct iris_context *ice,
433 struct iris_batch *batch,
434 struct iris_resource *res,
435 uint32_t start_layer,
436 uint32_t num_layers)
437 {
438 //DBG("%s to mt %p layers %u-%u\n", __FUNCTION__, mt,
439 //start_layer, start_layer + num_layers - 1);
440
441 assert(res->aux.usage == ISL_AUX_USAGE_MCS);
442
443 struct blorp_surf surf;
444 iris_blorp_surf_for_resource(&surf, &res->base, res->aux.usage, true);
445
446 struct blorp_batch blorp_batch;
447 blorp_batch_init(&ice->blorp, &blorp_batch, batch, 0);
448 blorp_mcs_partial_resolve(&blorp_batch, &surf, res->surf.format,
449 start_layer, num_layers);
450 blorp_batch_finish(&blorp_batch);
451 }
452
453
454 /**
455 * Return true if the format that will be used to access the resource is
456 * CCS_E-compatible with the resource's linear/non-sRGB format.
457 *
458 * Why use the linear format? Well, although the resourcemay be specified
459 * with an sRGB format, the usage of that color space/format can be toggled.
460 * Since our HW tends to support more linear formats than sRGB ones, we use
461 * this format variant for check for CCS_E compatibility.
462 */
463 static bool
464 format_ccs_e_compat_with_resource(const struct gen_device_info *devinfo,
465 const struct iris_resource *res,
466 enum isl_format access_format)
467 {
468 assert(res->aux.usage == ISL_AUX_USAGE_CCS_E);
469
470 enum isl_format isl_format = isl_format_srgb_to_linear(res->surf.format);
471 return isl_formats_are_ccs_e_compatible(devinfo, isl_format, access_format);
472 }
473
474 static bool
475 sample_with_hiz(const struct gen_device_info *devinfo,
476 const struct iris_resource *res)
477 {
478 if (!devinfo->has_sample_with_hiz)
479 return false;
480
481 if (res->aux.usage != ISL_AUX_USAGE_HIZ)
482 return false;
483
484 /* It seems the hardware won't fallback to the depth buffer if some of the
485 * mipmap levels aren't available in the HiZ buffer. So we need all levels
486 * of the texture to be HiZ enabled.
487 */
488 for (unsigned level = 0; level < res->surf.levels; ++level) {
489 if (!iris_resource_level_has_hiz(res, level))
490 return false;
491 }
492
493 /* If compressed multisampling is enabled, then we use it for the auxiliary
494 * buffer instead.
495 *
496 * From the BDW PRM (Volume 2d: Command Reference: Structures
497 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
498 *
499 * "If this field is set to AUX_HIZ, Number of Multisamples must be
500 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
501 *
502 * There is no such blurb for 1D textures, but there is sufficient evidence
503 * that this is broken on SKL+.
504 */
505 // XXX: i965 disables this for arrays too, is that reasonable?
506 return res->surf.samples == 1 && res->surf.dim == ISL_SURF_DIM_2D;
507 }
508
509 /**
510 * Does the resource's slice have hiz enabled?
511 */
512 bool
513 iris_resource_level_has_hiz(const struct iris_resource *res, uint32_t level)
514 {
515 iris_resource_check_level_layer(res, level, 0);
516 // return res->level[level].has_hiz;
517 return false;
518 }
519
520 /** \brief Assert that the level and layer are valid for the resource. */
521 void
522 iris_resource_check_level_layer(UNUSED const struct iris_resource *res,
523 UNUSED uint32_t level, UNUSED uint32_t layer)
524 {
525 assert(level < res->surf.levels);
526 assert(layer < util_num_layers(&res->base, level));
527 }
528
529 static inline uint32_t
530 miptree_level_range_length(const struct iris_resource *res,
531 uint32_t start_level, uint32_t num_levels)
532 {
533 assert(start_level < res->surf.levels);
534
535 if (num_levels == INTEL_REMAINING_LAYERS)
536 num_levels = res->surf.levels;
537
538 /* Check for overflow */
539 assert(start_level + num_levels >= start_level);
540 assert(start_level + num_levels <= res->surf.levels);
541
542 return num_levels;
543 }
544
545 static inline uint32_t
546 miptree_layer_range_length(const struct iris_resource *res, uint32_t level,
547 uint32_t start_layer, uint32_t num_layers)
548 {
549 assert(level <= res->base.last_level);
550
551 const uint32_t total_num_layers = iris_get_num_logical_layers(res, level);
552 assert(start_layer < total_num_layers);
553 if (num_layers == INTEL_REMAINING_LAYERS)
554 num_layers = total_num_layers - start_layer;
555 /* Check for overflow */
556 assert(start_layer + num_layers >= start_layer);
557 assert(start_layer + num_layers <= total_num_layers);
558
559 return num_layers;
560 }
561
562 static bool
563 has_color_unresolved(const struct iris_resource *res,
564 unsigned start_level, unsigned num_levels,
565 unsigned start_layer, unsigned num_layers)
566 {
567 if (!res->aux.bo)
568 return false;
569
570 /* Clamp the level range to fit the resource */
571 num_levels = miptree_level_range_length(res, start_level, num_levels);
572
573 for (uint32_t l = 0; l < num_levels; l++) {
574 const uint32_t level = start_level + l;
575 const uint32_t level_layers =
576 miptree_layer_range_length(res, level, start_layer, num_layers);
577 for (unsigned a = 0; a < level_layers; a++) {
578 enum isl_aux_state aux_state =
579 iris_resource_get_aux_state(res, level, start_layer + a);
580 assert(aux_state != ISL_AUX_STATE_AUX_INVALID);
581 if (aux_state != ISL_AUX_STATE_PASS_THROUGH)
582 return true;
583 }
584 }
585
586 return false;
587 }
588
589 static enum isl_aux_op
590 get_ccs_d_resolve_op(enum isl_aux_state aux_state,
591 enum isl_aux_usage aux_usage,
592 bool fast_clear_supported)
593 {
594 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_CCS_D);
595
596 const bool ccs_supported = aux_usage == ISL_AUX_USAGE_CCS_D;
597
598 assert(ccs_supported == fast_clear_supported);
599
600 switch (aux_state) {
601 case ISL_AUX_STATE_CLEAR:
602 case ISL_AUX_STATE_PARTIAL_CLEAR:
603 if (!ccs_supported)
604 return ISL_AUX_OP_FULL_RESOLVE;
605 else
606 return ISL_AUX_OP_NONE;
607
608 case ISL_AUX_STATE_PASS_THROUGH:
609 return ISL_AUX_OP_NONE;
610
611 case ISL_AUX_STATE_RESOLVED:
612 case ISL_AUX_STATE_AUX_INVALID:
613 case ISL_AUX_STATE_COMPRESSED_CLEAR:
614 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
615 break;
616 }
617
618 unreachable("Invalid aux state for CCS_D");
619 }
620
621 static enum isl_aux_op
622 get_ccs_e_resolve_op(enum isl_aux_state aux_state,
623 enum isl_aux_usage aux_usage,
624 bool fast_clear_supported)
625 {
626 /* CCS_E surfaces can be accessed as CCS_D if we're careful. */
627 assert(aux_usage == ISL_AUX_USAGE_NONE ||
628 aux_usage == ISL_AUX_USAGE_CCS_D ||
629 aux_usage == ISL_AUX_USAGE_CCS_E);
630
631 if (aux_usage == ISL_AUX_USAGE_CCS_D)
632 assert(fast_clear_supported);
633
634 switch (aux_state) {
635 case ISL_AUX_STATE_CLEAR:
636 case ISL_AUX_STATE_PARTIAL_CLEAR:
637 if (fast_clear_supported)
638 return ISL_AUX_OP_NONE;
639 else if (aux_usage == ISL_AUX_USAGE_CCS_E)
640 return ISL_AUX_OP_PARTIAL_RESOLVE;
641 else
642 return ISL_AUX_OP_FULL_RESOLVE;
643
644 case ISL_AUX_STATE_COMPRESSED_CLEAR:
645 if (aux_usage != ISL_AUX_USAGE_CCS_E)
646 return ISL_AUX_OP_FULL_RESOLVE;
647 else if (!fast_clear_supported)
648 return ISL_AUX_OP_PARTIAL_RESOLVE;
649 else
650 return ISL_AUX_OP_NONE;
651
652 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
653 if (aux_usage != ISL_AUX_USAGE_CCS_E)
654 return ISL_AUX_OP_FULL_RESOLVE;
655 else
656 return ISL_AUX_OP_NONE;
657
658 case ISL_AUX_STATE_PASS_THROUGH:
659 return ISL_AUX_OP_NONE;
660
661 case ISL_AUX_STATE_RESOLVED:
662 case ISL_AUX_STATE_AUX_INVALID:
663 break;
664 }
665
666 unreachable("Invalid aux state for CCS_E");
667 }
668
669 static void
670 iris_resource_prepare_ccs_access(struct iris_context *ice,
671 struct iris_batch *batch,
672 struct iris_resource *res,
673 uint32_t level, uint32_t layer,
674 enum isl_aux_usage aux_usage,
675 bool fast_clear_supported)
676 {
677 enum isl_aux_state aux_state = iris_resource_get_aux_state(res, level, layer);
678
679 enum isl_aux_op resolve_op;
680 if (res->aux.usage == ISL_AUX_USAGE_CCS_E) {
681 resolve_op = get_ccs_e_resolve_op(aux_state, aux_usage,
682 fast_clear_supported);
683 } else {
684 assert(res->aux.usage == ISL_AUX_USAGE_CCS_D);
685 resolve_op = get_ccs_d_resolve_op(aux_state, aux_usage,
686 fast_clear_supported);
687 }
688
689 if (resolve_op != ISL_AUX_OP_NONE) {
690 iris_resolve_color(ice, batch, res, level, layer, resolve_op);
691
692 switch (resolve_op) {
693 case ISL_AUX_OP_FULL_RESOLVE:
694 /* The CCS full resolve operation destroys the CCS and sets it to the
695 * pass-through state. (You can also think of this as being both a
696 * resolve and an ambiguate in one operation.)
697 */
698 iris_resource_set_aux_state(res, level, layer, 1,
699 ISL_AUX_STATE_PASS_THROUGH);
700 break;
701
702 case ISL_AUX_OP_PARTIAL_RESOLVE:
703 iris_resource_set_aux_state(res, level, layer, 1,
704 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
705 break;
706
707 default:
708 unreachable("Invalid resolve op");
709 }
710 }
711 }
712
713 static void
714 iris_resource_finish_ccs_write(struct iris_context *ice,
715 struct iris_resource *res,
716 uint32_t level, uint32_t layer,
717 enum isl_aux_usage aux_usage)
718 {
719 assert(aux_usage == ISL_AUX_USAGE_NONE ||
720 aux_usage == ISL_AUX_USAGE_CCS_D ||
721 aux_usage == ISL_AUX_USAGE_CCS_E);
722
723 enum isl_aux_state aux_state =
724 iris_resource_get_aux_state(res, level, layer);
725
726 if (res->aux.usage == ISL_AUX_USAGE_CCS_E) {
727 switch (aux_state) {
728 case ISL_AUX_STATE_CLEAR:
729 case ISL_AUX_STATE_PARTIAL_CLEAR:
730 assert(aux_usage == ISL_AUX_USAGE_CCS_E ||
731 aux_usage == ISL_AUX_USAGE_CCS_D);
732
733 if (aux_usage == ISL_AUX_USAGE_CCS_E) {
734 iris_resource_set_aux_state(res, level, layer, 1,
735 ISL_AUX_STATE_COMPRESSED_CLEAR);
736 } else if (aux_state != ISL_AUX_STATE_PARTIAL_CLEAR) {
737 iris_resource_set_aux_state(res, level, layer, 1,
738 ISL_AUX_STATE_PARTIAL_CLEAR);
739 }
740 break;
741
742 case ISL_AUX_STATE_COMPRESSED_CLEAR:
743 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
744 assert(aux_usage == ISL_AUX_USAGE_CCS_E);
745 break; /* Nothing to do */
746
747 case ISL_AUX_STATE_PASS_THROUGH:
748 if (aux_usage == ISL_AUX_USAGE_CCS_E) {
749 iris_resource_set_aux_state(res, level, layer, 1,
750 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
751 } else {
752 /* Nothing to do */
753 }
754 break;
755
756 case ISL_AUX_STATE_RESOLVED:
757 case ISL_AUX_STATE_AUX_INVALID:
758 unreachable("Invalid aux state for CCS_E");
759 }
760 } else {
761 assert(res->aux.usage == ISL_AUX_USAGE_CCS_D);
762 /* CCS_D is a bit simpler */
763 switch (aux_state) {
764 case ISL_AUX_STATE_CLEAR:
765 assert(aux_usage == ISL_AUX_USAGE_CCS_D);
766 iris_resource_set_aux_state(res, level, layer, 1,
767 ISL_AUX_STATE_PARTIAL_CLEAR);
768 break;
769
770 case ISL_AUX_STATE_PARTIAL_CLEAR:
771 assert(aux_usage == ISL_AUX_USAGE_CCS_D);
772 break; /* Nothing to do */
773
774 case ISL_AUX_STATE_PASS_THROUGH:
775 /* Nothing to do */
776 break;
777
778 case ISL_AUX_STATE_COMPRESSED_CLEAR:
779 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
780 case ISL_AUX_STATE_RESOLVED:
781 case ISL_AUX_STATE_AUX_INVALID:
782 unreachable("Invalid aux state for CCS_D");
783 }
784 }
785 }
786
787 static void
788 iris_resource_prepare_mcs_access(struct iris_context *ice,
789 struct iris_batch *batch,
790 struct iris_resource *res,
791 uint32_t layer,
792 enum isl_aux_usage aux_usage,
793 bool fast_clear_supported)
794 {
795 assert(aux_usage == ISL_AUX_USAGE_MCS);
796
797 switch (iris_resource_get_aux_state(res, 0, layer)) {
798 case ISL_AUX_STATE_CLEAR:
799 case ISL_AUX_STATE_COMPRESSED_CLEAR:
800 if (!fast_clear_supported) {
801 iris_mcs_partial_resolve(ice, batch, res, layer, 1);
802 iris_resource_set_aux_state(res, 0, layer, 1,
803 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
804 }
805 break;
806
807 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
808 break; /* Nothing to do */
809
810 case ISL_AUX_STATE_RESOLVED:
811 case ISL_AUX_STATE_PASS_THROUGH:
812 case ISL_AUX_STATE_AUX_INVALID:
813 case ISL_AUX_STATE_PARTIAL_CLEAR:
814 unreachable("Invalid aux state for MCS");
815 }
816 }
817
818 static void
819 iris_resource_finish_mcs_write(struct iris_context *ice,
820 struct iris_resource *res,
821 uint32_t layer,
822 enum isl_aux_usage aux_usage)
823 {
824 assert(aux_usage == ISL_AUX_USAGE_MCS);
825
826 switch (iris_resource_get_aux_state(res, 0, layer)) {
827 case ISL_AUX_STATE_CLEAR:
828 iris_resource_set_aux_state(res, 0, layer, 1,
829 ISL_AUX_STATE_COMPRESSED_CLEAR);
830 break;
831
832 case ISL_AUX_STATE_COMPRESSED_CLEAR:
833 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
834 break; /* Nothing to do */
835
836 case ISL_AUX_STATE_RESOLVED:
837 case ISL_AUX_STATE_PASS_THROUGH:
838 case ISL_AUX_STATE_AUX_INVALID:
839 case ISL_AUX_STATE_PARTIAL_CLEAR:
840 unreachable("Invalid aux state for MCS");
841 }
842 }
843
844 static void
845 iris_resource_prepare_hiz_access(struct iris_context *ice,
846 struct iris_batch *batch,
847 struct iris_resource *res,
848 uint32_t level, uint32_t layer,
849 enum isl_aux_usage aux_usage,
850 bool fast_clear_supported)
851 {
852 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_HIZ);
853
854 enum isl_aux_op hiz_op = ISL_AUX_OP_NONE;
855 switch (iris_resource_get_aux_state(res, level, layer)) {
856 case ISL_AUX_STATE_CLEAR:
857 case ISL_AUX_STATE_COMPRESSED_CLEAR:
858 if (aux_usage != ISL_AUX_USAGE_HIZ || !fast_clear_supported)
859 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
860 break;
861
862 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
863 if (aux_usage != ISL_AUX_USAGE_HIZ)
864 hiz_op = ISL_AUX_OP_FULL_RESOLVE;
865 break;
866
867 case ISL_AUX_STATE_PASS_THROUGH:
868 case ISL_AUX_STATE_RESOLVED:
869 break;
870
871 case ISL_AUX_STATE_AUX_INVALID:
872 if (aux_usage == ISL_AUX_USAGE_HIZ)
873 hiz_op = ISL_AUX_OP_AMBIGUATE;
874 break;
875
876 case ISL_AUX_STATE_PARTIAL_CLEAR:
877 unreachable("Invalid HiZ state");
878 }
879
880 if (hiz_op != ISL_AUX_OP_NONE) {
881 // XXX: HiZ
882 //intel_hiz_exec(ice, res, level, layer, 1, hiz_op);
883
884 switch (hiz_op) {
885 case ISL_AUX_OP_FULL_RESOLVE:
886 iris_resource_set_aux_state(res, level, layer, 1,
887 ISL_AUX_STATE_RESOLVED);
888 break;
889
890 case ISL_AUX_OP_AMBIGUATE:
891 /* The HiZ resolve operation is actually an ambiguate */
892 iris_resource_set_aux_state(res, level, layer, 1,
893 ISL_AUX_STATE_PASS_THROUGH);
894 break;
895
896 default:
897 unreachable("Invalid HiZ op");
898 }
899 }
900 }
901
902 static void
903 iris_resource_finish_hiz_write(struct iris_context *ice,
904 struct iris_resource *res,
905 uint32_t level, uint32_t layer,
906 enum isl_aux_usage aux_usage)
907 {
908 assert(aux_usage == ISL_AUX_USAGE_NONE || aux_usage == ISL_AUX_USAGE_HIZ);
909
910 switch (iris_resource_get_aux_state(res, level, layer)) {
911 case ISL_AUX_STATE_CLEAR:
912 assert(aux_usage == ISL_AUX_USAGE_HIZ);
913 iris_resource_set_aux_state(res, level, layer, 1,
914 ISL_AUX_STATE_COMPRESSED_CLEAR);
915 break;
916
917 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
918 case ISL_AUX_STATE_COMPRESSED_CLEAR:
919 assert(aux_usage == ISL_AUX_USAGE_HIZ);
920 break; /* Nothing to do */
921
922 case ISL_AUX_STATE_RESOLVED:
923 if (aux_usage == ISL_AUX_USAGE_HIZ) {
924 iris_resource_set_aux_state(res, level, layer, 1,
925 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
926 } else {
927 iris_resource_set_aux_state(res, level, layer, 1,
928 ISL_AUX_STATE_AUX_INVALID);
929 }
930 break;
931
932 case ISL_AUX_STATE_PASS_THROUGH:
933 if (aux_usage == ISL_AUX_USAGE_HIZ) {
934 iris_resource_set_aux_state(res, level, layer, 1,
935 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
936 }
937 break;
938
939 case ISL_AUX_STATE_AUX_INVALID:
940 assert(aux_usage != ISL_AUX_USAGE_HIZ);
941 break;
942
943 case ISL_AUX_STATE_PARTIAL_CLEAR:
944 unreachable("Invalid HiZ state");
945 }
946 }
947
948 void
949 iris_resource_prepare_access(struct iris_context *ice,
950 struct iris_batch *batch,
951 struct iris_resource *res,
952 uint32_t start_level, uint32_t num_levels,
953 uint32_t start_layer, uint32_t num_layers,
954 enum isl_aux_usage aux_usage,
955 bool fast_clear_supported)
956 {
957 num_levels = miptree_level_range_length(res, start_level, num_levels);
958
959 switch (res->aux.usage) {
960 case ISL_AUX_USAGE_NONE:
961 /* Nothing to do */
962 break;
963
964 case ISL_AUX_USAGE_MCS:
965 assert(start_level == 0 && num_levels == 1);
966 const uint32_t level_layers =
967 miptree_layer_range_length(res, 0, start_layer, num_layers);
968 for (uint32_t a = 0; a < level_layers; a++) {
969 iris_resource_prepare_mcs_access(ice, batch, res, start_layer + a,
970 aux_usage, fast_clear_supported);
971 }
972 break;
973
974 case ISL_AUX_USAGE_CCS_D:
975 case ISL_AUX_USAGE_CCS_E:
976 for (uint32_t l = 0; l < num_levels; l++) {
977 const uint32_t level = start_level + l;
978 const uint32_t level_layers =
979 miptree_layer_range_length(res, level, start_layer, num_layers);
980 for (uint32_t a = 0; a < level_layers; a++) {
981 iris_resource_prepare_ccs_access(ice, batch, res, level,
982 start_layer + a,
983 aux_usage, fast_clear_supported);
984 }
985 }
986 break;
987
988 case ISL_AUX_USAGE_HIZ:
989 for (uint32_t l = 0; l < num_levels; l++) {
990 const uint32_t level = start_level + l;
991 if (!iris_resource_level_has_hiz(res, level))
992 continue;
993
994 const uint32_t level_layers =
995 miptree_layer_range_length(res, level, start_layer, num_layers);
996 for (uint32_t a = 0; a < level_layers; a++) {
997 iris_resource_prepare_hiz_access(ice, batch, res, level,
998 start_layer + a, aux_usage,
999 fast_clear_supported);
1000 }
1001 }
1002 break;
1003
1004 default:
1005 unreachable("Invalid aux usage");
1006 }
1007 }
1008
1009 void
1010 iris_resource_finish_write(struct iris_context *ice,
1011 struct iris_resource *res, uint32_t level,
1012 uint32_t start_layer, uint32_t num_layers,
1013 enum isl_aux_usage aux_usage)
1014 {
1015 num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);
1016
1017 switch (res->aux.usage) {
1018 case ISL_AUX_USAGE_NONE:
1019 break;
1020
1021 case ISL_AUX_USAGE_MCS:
1022 for (uint32_t a = 0; a < num_layers; a++) {
1023 iris_resource_finish_mcs_write(ice, res, start_layer + a,
1024 aux_usage);
1025 }
1026 break;
1027
1028 case ISL_AUX_USAGE_CCS_D:
1029 case ISL_AUX_USAGE_CCS_E:
1030 for (uint32_t a = 0; a < num_layers; a++) {
1031 iris_resource_finish_ccs_write(ice, res, level, start_layer + a,
1032 aux_usage);
1033 }
1034 break;
1035
1036 case ISL_AUX_USAGE_HIZ:
1037 if (!iris_resource_level_has_hiz(res, level))
1038 return;
1039
1040 for (uint32_t a = 0; a < num_layers; a++) {
1041 iris_resource_finish_hiz_write(ice, res, level, start_layer + a,
1042 aux_usage);
1043 }
1044 break;
1045
1046 default:
1047 unreachable("Invavlid aux usage");
1048 }
1049 }
1050
1051 enum isl_aux_state
1052 iris_resource_get_aux_state(const struct iris_resource *res,
1053 uint32_t level, uint32_t layer)
1054 {
1055 iris_resource_check_level_layer(res, level, layer);
1056
1057 if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
1058 assert(iris_resource_level_has_hiz(res, level));
1059 } else if (res->surf.usage & ISL_SURF_USAGE_STENCIL_BIT) {
1060 unreachable("Cannot get aux state for stencil");
1061 } else {
1062 assert(res->surf.samples == 1 ||
1063 res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
1064 }
1065
1066 return res->aux.state[level][layer];
1067 }
1068
1069 void
1070 iris_resource_set_aux_state(struct iris_resource *res, uint32_t level,
1071 uint32_t start_layer, uint32_t num_layers,
1072 enum isl_aux_state aux_state)
1073 {
1074 num_layers = miptree_layer_range_length(res, level, start_layer, num_layers);
1075
1076 if (res->surf.usage & ISL_SURF_USAGE_DEPTH_BIT) {
1077 assert(iris_resource_level_has_hiz(res, level));
1078 } else if (res->surf.usage & ISL_SURF_USAGE_STENCIL_BIT) {
1079 unreachable("Cannot set aux state for stencil");
1080 } else {
1081 assert(res->surf.samples == 1 ||
1082 res->surf.msaa_layout == ISL_MSAA_LAYOUT_ARRAY);
1083 }
1084
1085 for (unsigned a = 0; a < num_layers; a++) {
1086 if (res->aux.state[level][start_layer + a] != aux_state) {
1087 res->aux.state[level][start_layer + a] = aux_state;
1088 // XXX: dirty works differently
1089 // brw->ctx.NewDriverState |= BRW_NEW_AUX_STATE;
1090 }
1091 }
1092 }
1093
1094 /* On Gen9 color buffers may be compressed by the hardware (lossless
1095 * compression). There are, however, format restrictions and care needs to be
1096 * taken that the sampler engine is capable for re-interpreting a buffer with
1097 * format different the buffer was originally written with.
1098 *
1099 * For example, SRGB formats are not compressible and the sampler engine isn't
1100 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
1101 * color buffer needs to be resolved so that the sampling surface can be
1102 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
1103 * set).
1104 */
1105 static bool
1106 can_texture_with_ccs(const struct gen_device_info *devinfo,
1107 struct pipe_debug_callback *dbg,
1108 const struct iris_resource *res,
1109 enum isl_format view_format)
1110 {
1111 if (res->aux.usage != ISL_AUX_USAGE_CCS_E)
1112 return false;
1113
1114 if (!format_ccs_e_compat_with_resource(devinfo, res, view_format)) {
1115 const struct isl_format_layout *res_fmtl =
1116 isl_format_get_layout(res->surf.format);
1117 const struct isl_format_layout *view_fmtl =
1118 isl_format_get_layout(view_format);
1119
1120 perf_debug(dbg, "Incompatible sampling format (%s) for CCS (%s)\n",
1121 view_fmtl->name, res_fmtl->name);
1122
1123 return false;
1124 }
1125
1126 return true;
1127 }
1128
1129 enum isl_aux_usage
1130 iris_resource_texture_aux_usage(struct iris_context *ice,
1131 const struct iris_resource *res,
1132 enum isl_format view_format,
1133 enum gen9_astc5x5_wa_tex_type astc5x5_wa_bits)
1134 {
1135 struct iris_screen *screen = (void *) ice->ctx.screen;
1136 struct gen_device_info *devinfo = &screen->devinfo;
1137
1138 assert(devinfo->gen == 9 || astc5x5_wa_bits == 0);
1139
1140 /* On gen9, ASTC 5x5 textures cannot live in the sampler cache along side
1141 * CCS or HiZ compressed textures. See gen9_apply_astc5x5_wa_flush() for
1142 * details.
1143 */
1144 if ((astc5x5_wa_bits & GEN9_ASTC5X5_WA_TEX_TYPE_ASTC5x5) &&
1145 res->aux.usage != ISL_AUX_USAGE_MCS)
1146 return ISL_AUX_USAGE_NONE;
1147
1148 switch (res->aux.usage) {
1149 case ISL_AUX_USAGE_HIZ:
1150 if (sample_with_hiz(devinfo, res))
1151 return ISL_AUX_USAGE_HIZ;
1152 break;
1153
1154 case ISL_AUX_USAGE_MCS:
1155 return ISL_AUX_USAGE_MCS;
1156
1157 case ISL_AUX_USAGE_CCS_D:
1158 case ISL_AUX_USAGE_CCS_E:
1159 /* If we don't have any unresolved color, report an aux usage of
1160 * ISL_AUX_USAGE_NONE. This way, texturing won't even look at the
1161 * aux surface and we can save some bandwidth.
1162 */
1163 if (!has_color_unresolved(res, 0, INTEL_REMAINING_LEVELS,
1164 0, INTEL_REMAINING_LAYERS))
1165 return ISL_AUX_USAGE_NONE;
1166
1167 if (can_texture_with_ccs(devinfo, &ice->dbg, res, view_format))
1168 return ISL_AUX_USAGE_CCS_E;
1169 break;
1170
1171 default:
1172 break;
1173 }
1174
1175 return ISL_AUX_USAGE_NONE;
1176 }
1177
1178 static bool
1179 isl_formats_are_fast_clear_compatible(enum isl_format a, enum isl_format b)
1180 {
1181 /* On gen8 and earlier, the hardware was only capable of handling 0/1 clear
1182 * values so sRGB curve application was a no-op for all fast-clearable
1183 * formats.
1184 *
1185 * On gen9+, the hardware supports arbitrary clear values. For sRGB clear
1186 * values, the hardware interprets the floats, not as what would be
1187 * returned from the sampler (or written by the shader), but as being
1188 * between format conversion and sRGB curve application. This means that
1189 * we can switch between sRGB and UNORM without having to whack the clear
1190 * color.
1191 */
1192 return isl_format_srgb_to_linear(a) == isl_format_srgb_to_linear(b);
1193 }
1194
1195 void
1196 iris_resource_prepare_texture(struct iris_context *ice,
1197 struct iris_batch *batch,
1198 struct iris_resource *res,
1199 enum isl_format view_format,
1200 uint32_t start_level, uint32_t num_levels,
1201 uint32_t start_layer, uint32_t num_layers,
1202 enum gen9_astc5x5_wa_tex_type astc5x5_wa_bits)
1203 {
1204 enum isl_aux_usage aux_usage =
1205 iris_resource_texture_aux_usage(ice, res, view_format, astc5x5_wa_bits);
1206
1207 bool clear_supported = aux_usage != ISL_AUX_USAGE_NONE;
1208
1209 /* Clear color is specified as ints or floats and the conversion is done by
1210 * the sampler. If we have a texture view, we would have to perform the
1211 * clear color conversion manually. Just disable clear color.
1212 */
1213 if (!isl_formats_are_fast_clear_compatible(res->surf.format, view_format))
1214 clear_supported = false;
1215
1216 iris_resource_prepare_access(ice, batch, res, start_level, num_levels,
1217 start_layer, num_layers,
1218 aux_usage, clear_supported);
1219 }
1220
1221 void
1222 iris_resource_prepare_image(struct iris_context *ice,
1223 struct iris_batch *batch,
1224 struct iris_resource *res)
1225 {
1226 /* The data port doesn't understand any compression */
1227 iris_resource_prepare_access(ice, batch, res, 0, INTEL_REMAINING_LEVELS,
1228 0, INTEL_REMAINING_LAYERS,
1229 ISL_AUX_USAGE_NONE, false);
1230 }
1231
1232 enum isl_aux_usage
1233 iris_resource_render_aux_usage(struct iris_context *ice,
1234 struct iris_resource *res,
1235 enum isl_format render_format,
1236 bool blend_enabled,
1237 bool draw_aux_disabled)
1238 {
1239 struct iris_screen *screen = (void *) ice->ctx.screen;
1240 struct gen_device_info *devinfo = &screen->devinfo;
1241
1242 if (draw_aux_disabled)
1243 return ISL_AUX_USAGE_NONE;
1244
1245 switch (res->aux.usage) {
1246 case ISL_AUX_USAGE_MCS:
1247 return ISL_AUX_USAGE_MCS;
1248
1249 case ISL_AUX_USAGE_CCS_D:
1250 case ISL_AUX_USAGE_CCS_E:
1251 /* Gen9+ hardware technically supports non-0/1 clear colors with sRGB
1252 * formats. However, there are issues with blending where it doesn't
1253 * properly apply the sRGB curve to the clear color when blending.
1254 */
1255 /* XXX:
1256 if (devinfo->gen >= 9 && blend_enabled &&
1257 isl_format_is_srgb(render_format) &&
1258 !isl_color_value_is_zero_one(res->fast_clear_color, render_format))
1259 return ISL_AUX_USAGE_NONE;
1260 */
1261
1262 if (res->aux.usage == ISL_AUX_USAGE_CCS_E &&
1263 format_ccs_e_compat_with_resource(devinfo, res, render_format))
1264 return ISL_AUX_USAGE_CCS_E;
1265
1266 /* Otherwise, we have to fall back to CCS_D */
1267 return ISL_AUX_USAGE_CCS_D;
1268
1269 default:
1270 return ISL_AUX_USAGE_NONE;
1271 }
1272 }
1273
1274 void
1275 iris_resource_prepare_render(struct iris_context *ice,
1276 struct iris_batch *batch,
1277 struct iris_resource *res, uint32_t level,
1278 uint32_t start_layer, uint32_t layer_count,
1279 enum isl_aux_usage aux_usage)
1280 {
1281 iris_resource_prepare_access(ice, batch, res, level, 1, start_layer,
1282 layer_count, aux_usage,
1283 aux_usage != ISL_AUX_USAGE_NONE);
1284 }
1285
1286 void
1287 iris_resource_finish_render(struct iris_context *ice,
1288 struct iris_resource *res, uint32_t level,
1289 uint32_t start_layer, uint32_t layer_count,
1290 enum isl_aux_usage aux_usage)
1291 {
1292 iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1293 aux_usage);
1294 }
1295
1296 void
1297 iris_resource_prepare_depth(struct iris_context *ice,
1298 struct iris_batch *batch,
1299 struct iris_resource *res, uint32_t level,
1300 uint32_t start_layer, uint32_t layer_count)
1301 {
1302 iris_resource_prepare_access(ice, batch, res, level, 1, start_layer,
1303 layer_count, res->aux.usage, !!res->aux.bo);
1304 }
1305
1306 void
1307 iris_resource_finish_depth(struct iris_context *ice,
1308 struct iris_resource *res, uint32_t level,
1309 uint32_t start_layer, uint32_t layer_count,
1310 bool depth_written)
1311 {
1312 if (depth_written) {
1313 iris_resource_finish_write(ice, res, level, start_layer, layer_count,
1314 res->aux.usage);
1315 }
1316 }