i965/miptree: Add support for window system images to create_for_dri_image
[mesa.git] / src / mesa / drivers / dri / i965 / intel_mipmap_tree.c
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <GL/gl.h>
27 #include <GL/internal/dri_interface.h>
28
29 #include "intel_batchbuffer.h"
30 #include "intel_image.h"
31 #include "intel_mipmap_tree.h"
32 #include "intel_tex.h"
33 #include "intel_blit.h"
34 #include "intel_fbo.h"
35
36 #include "brw_blorp.h"
37 #include "brw_context.h"
38 #include "brw_state.h"
39
40 #include "main/enums.h"
41 #include "main/fbobject.h"
42 #include "main/formats.h"
43 #include "main/glformats.h"
44 #include "main/texcompress_etc.h"
45 #include "main/teximage.h"
46 #include "main/streaming-load-memcpy.h"
47 #include "x86/common_x86_asm.h"
48
49 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
50
51 static void *intel_miptree_map_raw(struct brw_context *brw,
52 struct intel_mipmap_tree *mt,
53 GLbitfield mode);
54
55 static void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
56
57 static bool
58 intel_miptree_alloc_mcs(struct brw_context *brw,
59 struct intel_mipmap_tree *mt,
60 GLuint num_samples);
61
62 /**
63 * Determine which MSAA layout should be used by the MSAA surface being
64 * created, based on the chip generation and the surface type.
65 */
66 static enum intel_msaa_layout
67 compute_msaa_layout(struct brw_context *brw, mesa_format format,
68 uint32_t layout_flags)
69 {
70 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
71 if (brw->gen < 7)
72 return INTEL_MSAA_LAYOUT_IMS;
73
74 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
75 switch (_mesa_get_format_base_format(format)) {
76 case GL_DEPTH_COMPONENT:
77 case GL_STENCIL_INDEX:
78 case GL_DEPTH_STENCIL:
79 return INTEL_MSAA_LAYOUT_IMS;
80 default:
81 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
82 *
83 * This field must be set to 0 for all SINT MSRTs when all RT channels
84 * are not written
85 *
86 * In practice this means that we have to disable MCS for all signed
87 * integer MSAA buffers. The alternative, to disable MCS only when one
88 * of the render target channels is disabled, is impractical because it
89 * would require converting between CMS and UMS MSAA layouts on the fly,
90 * which is expensive.
91 */
92 if (brw->gen == 7 && _mesa_get_format_datatype(format) == GL_INT) {
93 return INTEL_MSAA_LAYOUT_UMS;
94 } else if (layout_flags & MIPTREE_LAYOUT_DISABLE_AUX) {
95 /* We can't use the CMS layout because it uses an aux buffer, the MCS
96 * buffer. So fallback to UMS, which is identical to CMS without the
97 * MCS. */
98 return INTEL_MSAA_LAYOUT_UMS;
99 } else {
100 return INTEL_MSAA_LAYOUT_CMS;
101 }
102 }
103 }
104
105 static bool
106 intel_tiling_supports_ccs(const struct brw_context *brw, unsigned tiling)
107 {
108 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
109 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
110 *
111 * - Support is limited to tiled render targets.
112 *
113 * Gen9 changes the restriction to Y-tile only.
114 */
115 if (brw->gen >= 9)
116 return tiling == I915_TILING_Y;
117 else if (brw->gen >= 7)
118 return tiling != I915_TILING_NONE;
119 else
120 return false;
121 }
122
123 /**
124 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
125 * can be used. This doesn't (and should not) inspect any of the properties of
126 * the miptree's BO.
127 *
128 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
129 * beneath the "Fast Color Clear" bullet (p326):
130 *
131 * - Support is for non-mip-mapped and non-array surface types only.
132 *
133 * And then later, on p327:
134 *
135 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
136 * 64bpp, and 128bpp.
137 *
138 * From the Skylake documentation, it is made clear that X-tiling is no longer
139 * supported:
140 *
141 * - MCS and Lossless compression is supported for TiledY/TileYs/TileYf
142 * non-MSRTs only.
143 */
144 static bool
145 intel_miptree_supports_ccs(struct brw_context *brw,
146 const struct intel_mipmap_tree *mt)
147 {
148 /* MCS support does not exist prior to Gen7 */
149 if (brw->gen < 7)
150 return false;
151
152 /* This function applies only to non-multisampled render targets. */
153 if (mt->num_samples > 1)
154 return false;
155
156 /* MCS is only supported for color buffers */
157 switch (_mesa_get_format_base_format(mt->format)) {
158 case GL_DEPTH_COMPONENT:
159 case GL_DEPTH_STENCIL:
160 case GL_STENCIL_INDEX:
161 return false;
162 }
163
164 if (mt->cpp != 4 && mt->cpp != 8 && mt->cpp != 16)
165 return false;
166
167 const bool mip_mapped = mt->first_level != 0 || mt->last_level != 0;
168 const bool arrayed = mt->physical_depth0 != 1;
169
170 if (arrayed) {
171 /* Multisample surfaces with the CMS layout are not layered surfaces,
172 * yet still have physical_depth0 > 1. Assert that we don't
173 * accidentally reject a multisampled surface here. We should have
174 * rejected it earlier by explicitly checking the sample count.
175 */
176 assert(mt->num_samples <= 1);
177 }
178
179 /* Handle the hardware restrictions...
180 *
181 * All GENs have the following restriction: "MCS buffer for non-MSRT is
182 * supported only for RT formats 32bpp, 64bpp, and 128bpp."
183 *
184 * From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652: (Color Clear of
185 * Non-MultiSampler Render Target Restrictions) Support is for
186 * non-mip-mapped and non-array surface types only.
187 *
188 * From the BDW PRM Volume 7: 3D-Media-GPGPU, page 649: (Color Clear of
189 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
190 * surfaces are supported with MCS buffer layout with these alignments in
191 * the RT space: Horizontal Alignment = 256 and Vertical Alignment = 128.
192 *
193 * From the SKL PRM Volume 7: 3D-Media-GPGPU, page 632: (Color Clear of
194 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
195 * surfaces are supported with MCS buffer layout with these alignments in
196 * the RT space: Horizontal Alignment = 128 and Vertical Alignment = 64.
197 */
198 if (brw->gen < 8 && (mip_mapped || arrayed))
199 return false;
200
201 /* There's no point in using an MCS buffer if the surface isn't in a
202 * renderable format.
203 */
204 if (!brw->mesa_format_supports_render[mt->format])
205 return false;
206
207 if (brw->gen >= 9) {
208 mesa_format linear_format = _mesa_get_srgb_format_linear(mt->format);
209 const enum isl_format isl_format =
210 brw_isl_format_for_mesa_format(linear_format);
211 return isl_format_supports_ccs_e(&brw->screen->devinfo, isl_format);
212 } else
213 return true;
214 }
215
216 static bool
217 intel_miptree_supports_hiz(struct brw_context *brw,
218 struct intel_mipmap_tree *mt)
219 {
220 if (!brw->has_hiz)
221 return false;
222
223 switch (mt->format) {
224 case MESA_FORMAT_Z_FLOAT32:
225 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
226 case MESA_FORMAT_Z24_UNORM_X8_UINT:
227 case MESA_FORMAT_Z24_UNORM_S8_UINT:
228 case MESA_FORMAT_Z_UNORM16:
229 return true;
230 default:
231 return false;
232 }
233 }
234
235
236 /* On Gen9 support for color buffer compression was extended to single
237 * sampled surfaces. This is a helper considering both auxiliary buffer
238 * type and number of samples telling if the given miptree represents
239 * the new single sampled case - also called lossless compression.
240 */
241 bool
242 intel_miptree_is_lossless_compressed(const struct brw_context *brw,
243 const struct intel_mipmap_tree *mt)
244 {
245 /* Only available from Gen9 onwards. */
246 if (brw->gen < 9)
247 return false;
248
249 /* Compression always requires auxiliary buffer. */
250 if (!mt->mcs_buf)
251 return false;
252
253 /* Single sample compression is represented re-using msaa compression
254 * layout type: "Compressed Multisampled Surfaces".
255 */
256 if (mt->msaa_layout != INTEL_MSAA_LAYOUT_CMS)
257 return false;
258
259 /* And finally distinguish between msaa and single sample case. */
260 return mt->num_samples <= 1;
261 }
262
263 static bool
264 intel_miptree_supports_ccs_e(struct brw_context *brw,
265 const struct intel_mipmap_tree *mt)
266 {
267 /* For now compression is only enabled for integer formats even though
268 * there exist supported floating point formats also. This is a heuristic
269 * decision based on current public benchmarks. In none of the cases these
270 * formats provided any improvement but a few cases were seen to regress.
271 * Hence these are left to to be enabled in the future when they are known
272 * to improve things.
273 */
274 if (_mesa_get_format_datatype(mt->format) == GL_FLOAT)
275 return false;
276
277 if (!intel_miptree_supports_ccs(brw, mt))
278 return false;
279
280 /* Fast clear can be also used to clear srgb surfaces by using equivalent
281 * linear format. This trick, however, can't be extended to be used with
282 * lossless compression and therefore a check is needed to see if the format
283 * really is linear.
284 */
285 return _mesa_get_srgb_format_linear(mt->format) == mt->format;
286 }
287
288 /**
289 * Determine depth format corresponding to a depth+stencil format,
290 * for separate stencil.
291 */
292 mesa_format
293 intel_depth_format_for_depthstencil_format(mesa_format format) {
294 switch (format) {
295 case MESA_FORMAT_Z24_UNORM_S8_UINT:
296 return MESA_FORMAT_Z24_UNORM_X8_UINT;
297 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
298 return MESA_FORMAT_Z_FLOAT32;
299 default:
300 return format;
301 }
302 }
303
304 static bool
305 create_mapping_table(GLenum target, unsigned first_level, unsigned last_level,
306 unsigned depth0, struct intel_mipmap_level *table)
307 {
308 for (unsigned level = first_level; level <= last_level; level++) {
309 const unsigned d =
310 target == GL_TEXTURE_3D ? minify(depth0, level) : depth0;
311
312 table[level].slice = calloc(d, sizeof(*table[0].slice));
313 if (!table[level].slice)
314 goto unwind;
315 }
316
317 return true;
318
319 unwind:
320 for (unsigned level = first_level; level <= last_level; level++)
321 free(table[level].slice);
322
323 return false;
324 }
325
326 /**
327 * @param for_bo Indicates that the caller is
328 * intel_miptree_create_for_bo(). If true, then do not create
329 * \c stencil_mt.
330 */
331 static struct intel_mipmap_tree *
332 intel_miptree_create_layout(struct brw_context *brw,
333 GLenum target,
334 mesa_format format,
335 GLuint first_level,
336 GLuint last_level,
337 GLuint width0,
338 GLuint height0,
339 GLuint depth0,
340 GLuint num_samples,
341 uint32_t layout_flags)
342 {
343 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
344 if (!mt)
345 return NULL;
346
347 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__,
348 _mesa_enum_to_string(target),
349 _mesa_get_format_name(format),
350 first_level, last_level, depth0, mt);
351
352 if (target == GL_TEXTURE_1D_ARRAY)
353 assert(height0 == 1);
354
355 mt->target = target;
356 mt->format = format;
357 mt->first_level = first_level;
358 mt->last_level = last_level;
359 mt->logical_width0 = width0;
360 mt->logical_height0 = height0;
361 mt->logical_depth0 = depth0;
362 mt->is_scanout = (layout_flags & MIPTREE_LAYOUT_FOR_SCANOUT) != 0;
363 mt->aux_usage = ISL_AUX_USAGE_NONE;
364 mt->supports_fast_clear = false;
365 mt->aux_state = NULL;
366 mt->cpp = _mesa_get_format_bytes(format);
367 mt->num_samples = num_samples;
368 mt->compressed = _mesa_is_format_compressed(format);
369 mt->msaa_layout = INTEL_MSAA_LAYOUT_NONE;
370 mt->refcount = 1;
371
372 if (brw->gen == 6 && format == MESA_FORMAT_S_UINT8)
373 layout_flags |= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL;
374
375 int depth_multiply = 1;
376 if (num_samples > 1) {
377 /* Adjust width/height/depth for MSAA */
378 mt->msaa_layout = compute_msaa_layout(brw, format, layout_flags);
379 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
380 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
381 * "If the surface is multisampled and it is a depth or stencil
382 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
383 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
384 * proceeding:
385 *
386 * +----------------------------------------------------------------+
387 * | Num Multisamples | W_l = | H_l = |
388 * +----------------------------------------------------------------+
389 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
390 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
391 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
392 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
393 * +----------------------------------------------------------------+
394 * "
395 *
396 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
397 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
398 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
399 *
400 * Another more complicated explanation for these adjustments comes
401 * from the Sandybridge PRM, volume 4, part 1, page 31:
402 *
403 * "Any of the other messages (sample*, LOD, load4) used with a
404 * (4x) multisampled surface will in-effect sample a surface with
405 * double the height and width as that indicated in the surface
406 * state. Each pixel position on the original-sized surface is
407 * replaced with a 2x2 of samples with the following arrangement:
408 *
409 * sample 0 sample 2
410 * sample 1 sample 3"
411 *
412 * Thus, when sampling from a multisampled texture, it behaves as
413 * though the layout in memory for (x,y,sample) is:
414 *
415 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
416 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
417 *
418 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
419 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
420 *
421 * However, the actual layout of multisampled data in memory is:
422 *
423 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
424 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
425 *
426 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
427 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
428 *
429 * This pattern repeats for each 2x2 pixel block.
430 *
431 * As a result, when calculating the size of our 4-sample buffer for
432 * an odd width or height, we have to align before scaling up because
433 * sample 3 is in that bottom right 2x2 block.
434 */
435 switch (num_samples) {
436 case 2:
437 assert(brw->gen >= 8);
438 width0 = ALIGN(width0, 2) * 2;
439 height0 = ALIGN(height0, 2);
440 break;
441 case 4:
442 width0 = ALIGN(width0, 2) * 2;
443 height0 = ALIGN(height0, 2) * 2;
444 break;
445 case 8:
446 width0 = ALIGN(width0, 2) * 4;
447 height0 = ALIGN(height0, 2) * 2;
448 break;
449 case 16:
450 width0 = ALIGN(width0, 2) * 4;
451 height0 = ALIGN(height0, 2) * 4;
452 break;
453 default:
454 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
455 * or 16.
456 */
457 unreachable("not reached");
458 }
459 } else {
460 /* Non-interleaved */
461 depth_multiply = num_samples;
462 depth0 *= depth_multiply;
463 }
464 }
465
466 if (!create_mapping_table(target, first_level, last_level, depth0,
467 mt->level)) {
468 free(mt);
469 return NULL;
470 }
471
472 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
473 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
474 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
475 * used on Gen8 to make it pick a qpitch value which doesn't include space
476 * for the mipmaps. On Gen9 this is not necessary because it will
477 * automatically pick a packed qpitch value whenever mt->first_level ==
478 * mt->last_level.
479 * TODO: can we use it elsewhere?
480 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
481 */
482 if (brw->gen >= 9) {
483 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
484 } else {
485 switch (mt->msaa_layout) {
486 case INTEL_MSAA_LAYOUT_NONE:
487 case INTEL_MSAA_LAYOUT_IMS:
488 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
489 break;
490 case INTEL_MSAA_LAYOUT_UMS:
491 case INTEL_MSAA_LAYOUT_CMS:
492 mt->array_layout = ALL_SLICES_AT_EACH_LOD;
493 break;
494 }
495 }
496
497 if (target == GL_TEXTURE_CUBE_MAP)
498 assert(depth0 == 6 * depth_multiply);
499
500 mt->physical_width0 = width0;
501 mt->physical_height0 = height0;
502 mt->physical_depth0 = depth0;
503
504 if (!(layout_flags & MIPTREE_LAYOUT_FOR_BO) &&
505 _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
506 (brw->must_use_separate_stencil ||
507 (brw->has_separate_stencil && intel_miptree_supports_hiz(brw, mt)))) {
508 uint32_t stencil_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
509 if (brw->gen == 6) {
510 stencil_flags |= MIPTREE_LAYOUT_TILING_ANY;
511 }
512
513 mt->stencil_mt = intel_miptree_create(brw,
514 mt->target,
515 MESA_FORMAT_S_UINT8,
516 mt->first_level,
517 mt->last_level,
518 mt->logical_width0,
519 mt->logical_height0,
520 mt->logical_depth0,
521 num_samples,
522 stencil_flags);
523
524 if (!mt->stencil_mt) {
525 intel_miptree_release(&mt);
526 return NULL;
527 }
528 mt->stencil_mt->r8stencil_needs_update = true;
529
530 /* Fix up the Z miptree format for how we're splitting out separate
531 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
532 */
533 mt->format = intel_depth_format_for_depthstencil_format(mt->format);
534 mt->cpp = 4;
535
536 if (format == mt->format) {
537 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
538 _mesa_get_format_name(mt->format));
539 }
540 }
541
542 if (layout_flags & MIPTREE_LAYOUT_GEN6_HIZ_STENCIL)
543 mt->array_layout = GEN6_HIZ_STENCIL;
544
545 /*
546 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
547 * multisampled or have an AUX buffer attached to it.
548 *
549 * GEN | MSRT | AUX_CCS_* or AUX_MCS
550 * -------------------------------------------
551 * 9 | HALIGN_16 | HALIGN_16
552 * 8 | HALIGN_ANY | HALIGN_16
553 * 7 | ? | ?
554 * 6 | ? | ?
555 */
556 if (intel_miptree_supports_ccs(brw, mt)) {
557 if (brw->gen >= 9 || (brw->gen == 8 && num_samples <= 1))
558 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
559 } else if (brw->gen >= 9 && num_samples > 1) {
560 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
561 } else {
562 const UNUSED bool is_lossless_compressed_aux =
563 brw->gen >= 9 && num_samples == 1 &&
564 mt->format == MESA_FORMAT_R_UINT32;
565
566 /* For now, nothing else has this requirement */
567 assert(is_lossless_compressed_aux ||
568 (layout_flags & MIPTREE_LAYOUT_FORCE_HALIGN16) == 0);
569 }
570
571 if (!brw_miptree_layout(brw, mt, layout_flags)) {
572 intel_miptree_release(&mt);
573 return NULL;
574 }
575
576 return mt;
577 }
578
579
580 /**
581 * Choose the aux usage for this miptree. This function must be called fairly
582 * late in the miptree create process after we have a tiling.
583 */
584 static void
585 intel_miptree_choose_aux_usage(struct brw_context *brw,
586 struct intel_mipmap_tree *mt)
587 {
588 assert(mt->aux_usage == ISL_AUX_USAGE_NONE);
589
590 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
591 mt->aux_usage = ISL_AUX_USAGE_MCS;
592 } else if (intel_tiling_supports_ccs(brw, mt->tiling) &&
593 intel_miptree_supports_ccs(brw, mt)) {
594 if (!unlikely(INTEL_DEBUG & DEBUG_NO_RBC) &&
595 brw->gen >= 9 && !mt->is_scanout &&
596 intel_miptree_supports_ccs_e(brw, mt)) {
597 mt->aux_usage = ISL_AUX_USAGE_CCS_E;
598 } else {
599 mt->aux_usage = ISL_AUX_USAGE_CCS_D;
600 }
601 } else if (intel_miptree_supports_hiz(brw, mt)) {
602 mt->aux_usage = ISL_AUX_USAGE_HIZ;
603 }
604
605 /* We can do fast-clear on all auxiliary surface types that are
606 * allocated through the normal texture creation paths.
607 */
608 if (mt->aux_usage != ISL_AUX_USAGE_NONE)
609 mt->supports_fast_clear = true;
610 }
611
612
613 /**
614 * Choose an appropriate uncompressed format for a requested
615 * compressed format, if unsupported.
616 */
617 mesa_format
618 intel_lower_compressed_format(struct brw_context *brw, mesa_format format)
619 {
620 /* No need to lower ETC formats on these platforms,
621 * they are supported natively.
622 */
623 if (brw->gen >= 8 || brw->is_baytrail)
624 return format;
625
626 switch (format) {
627 case MESA_FORMAT_ETC1_RGB8:
628 return MESA_FORMAT_R8G8B8X8_UNORM;
629 case MESA_FORMAT_ETC2_RGB8:
630 return MESA_FORMAT_R8G8B8X8_UNORM;
631 case MESA_FORMAT_ETC2_SRGB8:
632 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
633 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
634 return MESA_FORMAT_B8G8R8A8_SRGB;
635 case MESA_FORMAT_ETC2_RGBA8_EAC:
636 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
637 return MESA_FORMAT_R8G8B8A8_UNORM;
638 case MESA_FORMAT_ETC2_R11_EAC:
639 return MESA_FORMAT_R_UNORM16;
640 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
641 return MESA_FORMAT_R_SNORM16;
642 case MESA_FORMAT_ETC2_RG11_EAC:
643 return MESA_FORMAT_R16G16_UNORM;
644 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
645 return MESA_FORMAT_R16G16_SNORM;
646 default:
647 /* Non ETC1 / ETC2 format */
648 return format;
649 }
650 }
651
652 /** \brief Assert that the level and layer are valid for the miptree. */
653 void
654 intel_miptree_check_level_layer(const struct intel_mipmap_tree *mt,
655 uint32_t level,
656 uint32_t layer)
657 {
658 (void) mt;
659 (void) level;
660 (void) layer;
661
662 assert(level >= mt->first_level);
663 assert(level <= mt->last_level);
664
665 if (mt->surf.size > 0)
666 assert(layer < (mt->surf.dim == ISL_SURF_DIM_3D ?
667 minify(mt->surf.phys_level0_sa.depth, level) :
668 mt->surf.phys_level0_sa.array_len));
669 else
670 assert(layer < mt->level[level].depth);
671 }
672
673 static enum isl_aux_state **
674 create_aux_state_map(struct intel_mipmap_tree *mt,
675 enum isl_aux_state initial)
676 {
677 const uint32_t levels = mt->last_level + 1;
678
679 uint32_t total_slices = 0;
680 for (uint32_t level = 0; level < levels; level++)
681 total_slices += mt->level[level].depth;
682
683 const size_t per_level_array_size = levels * sizeof(enum isl_aux_state *);
684
685 /* We're going to allocate a single chunk of data for both the per-level
686 * reference array and the arrays of aux_state. This makes cleanup
687 * significantly easier.
688 */
689 const size_t total_size = per_level_array_size +
690 total_slices * sizeof(enum isl_aux_state);
691 void *data = malloc(total_size);
692 if (data == NULL)
693 return NULL;
694
695 enum isl_aux_state **per_level_arr = data;
696 enum isl_aux_state *s = data + per_level_array_size;
697 for (uint32_t level = 0; level < levels; level++) {
698 per_level_arr[level] = s;
699 for (uint32_t a = 0; a < mt->level[level].depth; a++)
700 *(s++) = initial;
701 }
702 assert((void *)s == data + total_size);
703
704 return per_level_arr;
705 }
706
707 static void
708 free_aux_state_map(enum isl_aux_state **state)
709 {
710 free(state);
711 }
712
713 static struct intel_mipmap_tree *
714 make_surface(struct brw_context *brw, GLenum target, mesa_format format,
715 unsigned first_level, unsigned last_level,
716 unsigned width0, unsigned height0, unsigned depth0,
717 unsigned num_samples, enum isl_tiling isl_tiling,
718 isl_surf_usage_flags_t isl_usage_flags, uint32_t alloc_flags,
719 struct brw_bo *bo)
720 {
721 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
722 if (!mt)
723 return NULL;
724
725 if (!create_mapping_table(target, first_level, last_level, depth0,
726 mt->level)) {
727 free(mt);
728 return NULL;
729 }
730
731 if (target == GL_TEXTURE_CUBE_MAP ||
732 target == GL_TEXTURE_CUBE_MAP_ARRAY)
733 isl_usage_flags |= ISL_SURF_USAGE_CUBE_BIT;
734
735 DBG("%s: %s %s %ux %u:%u:%u %d..%d <-- %p\n",
736 __func__,
737 _mesa_enum_to_string(target),
738 _mesa_get_format_name(format),
739 num_samples, width0, height0, depth0,
740 first_level, last_level, mt);
741
742 struct isl_surf_init_info init_info = {
743 .dim = get_isl_surf_dim(target),
744 .format = translate_tex_format(brw, format, false),
745 .width = width0,
746 .height = height0,
747 .depth = target == GL_TEXTURE_3D ? depth0 : 1,
748 .levels = last_level - first_level + 1,
749 .array_len = target == GL_TEXTURE_3D ? 1 : depth0,
750 .samples = MAX2(num_samples, 1),
751 .usage = isl_usage_flags,
752 .tiling_flags = 1u << isl_tiling
753 };
754
755 if (!isl_surf_init_s(&brw->isl_dev, &mt->surf, &init_info))
756 goto fail;
757
758 assert(mt->surf.size % mt->surf.row_pitch == 0);
759
760 if (!bo) {
761 mt->bo = brw_bo_alloc_tiled(brw->bufmgr, "isl-miptree",
762 mt->surf.size,
763 isl_tiling_to_bufmgr_tiling(isl_tiling),
764 mt->surf.row_pitch, alloc_flags);
765 if (!mt->bo)
766 goto fail;
767 } else {
768 mt->bo = bo;
769 }
770
771 mt->first_level = first_level;
772 mt->last_level = last_level;
773 mt->target = target;
774 mt->format = format;
775 mt->refcount = 1;
776 mt->aux_state = NULL;
777
778 return mt;
779
780 fail:
781 intel_miptree_release(&mt);
782 return NULL;
783 }
784
785 static struct intel_mipmap_tree *
786 miptree_create(struct brw_context *brw,
787 GLenum target,
788 mesa_format format,
789 GLuint first_level,
790 GLuint last_level,
791 GLuint width0,
792 GLuint height0,
793 GLuint depth0,
794 GLuint num_samples,
795 uint32_t layout_flags)
796 {
797 if (brw->gen == 6 && format == MESA_FORMAT_S_UINT8)
798 return make_surface(brw, target, format, first_level, last_level,
799 width0, height0, depth0, num_samples, ISL_TILING_W,
800 ISL_SURF_USAGE_STENCIL_BIT |
801 ISL_SURF_USAGE_TEXTURE_BIT,
802 BO_ALLOC_FOR_RENDER, NULL);
803
804 struct intel_mipmap_tree *mt;
805 mesa_format tex_format = format;
806 mesa_format etc_format = MESA_FORMAT_NONE;
807 uint32_t alloc_flags = 0;
808
809 format = intel_lower_compressed_format(brw, format);
810
811 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
812
813 assert((layout_flags & MIPTREE_LAYOUT_FOR_BO) == 0);
814 mt = intel_miptree_create_layout(brw, target, format,
815 first_level, last_level, width0,
816 height0, depth0, num_samples,
817 layout_flags);
818 if (!mt)
819 return NULL;
820
821 if (mt->tiling == (I915_TILING_Y | I915_TILING_X))
822 mt->tiling = I915_TILING_Y;
823
824 if (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD)
825 alloc_flags |= BO_ALLOC_FOR_RENDER;
826
827 mt->etc_format = etc_format;
828
829 if (format == MESA_FORMAT_S_UINT8) {
830 /* Align to size of W tile, 64x64. */
831 mt->bo = brw_bo_alloc_tiled_2d(brw->bufmgr, "miptree",
832 ALIGN(mt->total_width, 64),
833 ALIGN(mt->total_height, 64),
834 mt->cpp, mt->tiling, &mt->pitch,
835 alloc_flags);
836 } else {
837 mt->bo = brw_bo_alloc_tiled_2d(brw->bufmgr, "miptree",
838 mt->total_width, mt->total_height,
839 mt->cpp, mt->tiling, &mt->pitch,
840 alloc_flags);
841 }
842
843 if (layout_flags & MIPTREE_LAYOUT_FOR_SCANOUT)
844 mt->bo->cache_coherent = false;
845
846 if (!(layout_flags & MIPTREE_LAYOUT_DISABLE_AUX))
847 intel_miptree_choose_aux_usage(brw, mt);
848
849 return mt;
850 }
851
852 struct intel_mipmap_tree *
853 intel_miptree_create(struct brw_context *brw,
854 GLenum target,
855 mesa_format format,
856 GLuint first_level,
857 GLuint last_level,
858 GLuint width0,
859 GLuint height0,
860 GLuint depth0,
861 GLuint num_samples,
862 uint32_t layout_flags)
863 {
864 struct intel_mipmap_tree *mt = miptree_create(
865 brw, target, format,
866 first_level, last_level,
867 width0, height0, depth0, num_samples,
868 layout_flags);
869
870 /* If the BO is too large to fit in the aperture, we need to use the
871 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
872 * handle Y-tiling, so we need to fall back to X.
873 */
874 if (brw->gen < 6 && mt->bo->size >= brw->max_gtt_map_object_size &&
875 mt->tiling == I915_TILING_Y) {
876 const uint32_t alloc_flags =
877 (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD) ?
878 BO_ALLOC_FOR_RENDER : 0;
879 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
880 mt->total_width, mt->total_height);
881
882 mt->tiling = I915_TILING_X;
883 brw_bo_unreference(mt->bo);
884 mt->bo = brw_bo_alloc_tiled_2d(brw->bufmgr, "miptree",
885 mt->total_width, mt->total_height, mt->cpp,
886 mt->tiling, &mt->pitch, alloc_flags);
887 }
888
889 mt->offset = 0;
890
891 if (!mt->bo) {
892 intel_miptree_release(&mt);
893 return NULL;
894 }
895
896
897 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
898 assert(mt->num_samples > 1);
899 if (!intel_miptree_alloc_mcs(brw, mt, num_samples)) {
900 intel_miptree_release(&mt);
901 return NULL;
902 }
903 }
904
905 /* Since CCS_E can compress more than just clear color, we create the CCS
906 * for it up-front. For CCS_D which only compresses clears, we create the
907 * CCS on-demand when a clear occurs that wants one.
908 */
909 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
910 if (!intel_miptree_alloc_ccs(brw, mt)) {
911 intel_miptree_release(&mt);
912 return NULL;
913 }
914 }
915
916 return mt;
917 }
918
919 struct intel_mipmap_tree *
920 intel_miptree_create_for_bo(struct brw_context *brw,
921 struct brw_bo *bo,
922 mesa_format format,
923 uint32_t offset,
924 uint32_t width,
925 uint32_t height,
926 uint32_t depth,
927 int pitch,
928 uint32_t layout_flags)
929 {
930 struct intel_mipmap_tree *mt;
931 uint32_t tiling, swizzle;
932 const GLenum target = depth > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
933
934 if (brw->gen == 6 && format == MESA_FORMAT_S_UINT8) {
935 mt = make_surface(brw, target, MESA_FORMAT_S_UINT8,
936 0, 0, width, height, depth, 1, ISL_TILING_W,
937 ISL_SURF_USAGE_STENCIL_BIT |
938 ISL_SURF_USAGE_TEXTURE_BIT,
939 BO_ALLOC_FOR_RENDER, bo);
940 if (!mt)
941 return NULL;
942
943 assert(bo->size >= mt->surf.size);
944
945 brw_bo_reference(bo);
946 return mt;
947 }
948
949 brw_bo_get_tiling(bo, &tiling, &swizzle);
950
951 /* Nothing will be able to use this miptree with the BO if the offset isn't
952 * aligned.
953 */
954 if (tiling != I915_TILING_NONE)
955 assert(offset % 4096 == 0);
956
957 /* miptrees can't handle negative pitch. If you need flipping of images,
958 * that's outside of the scope of the mt.
959 */
960 assert(pitch >= 0);
961
962 /* The BO already has a tiling format and we shouldn't confuse the lower
963 * layers by making it try to find a tiling format again.
964 */
965 assert((layout_flags & MIPTREE_LAYOUT_TILING_ANY) == 0);
966 assert((layout_flags & MIPTREE_LAYOUT_TILING_NONE) == 0);
967
968 layout_flags |= MIPTREE_LAYOUT_FOR_BO;
969 mt = intel_miptree_create_layout(brw, target, format,
970 0, 0,
971 width, height, depth, 0,
972 layout_flags);
973 if (!mt)
974 return NULL;
975
976 brw_bo_reference(bo);
977 mt->bo = bo;
978 mt->pitch = pitch;
979 mt->offset = offset;
980 mt->tiling = tiling;
981
982 if (!(layout_flags & MIPTREE_LAYOUT_DISABLE_AUX)) {
983 intel_miptree_choose_aux_usage(brw, mt);
984
985 /* Since CCS_E can compress more than just clear color, we create the
986 * CCS for it up-front. For CCS_D which only compresses clears, we
987 * create the CCS on-demand when a clear occurs that wants one.
988 */
989 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
990 if (!intel_miptree_alloc_ccs(brw, mt)) {
991 intel_miptree_release(&mt);
992 return NULL;
993 }
994 }
995 }
996
997 return mt;
998 }
999
1000 static struct intel_mipmap_tree *
1001 miptree_create_for_planar_image(struct brw_context *brw,
1002 __DRIimage *image, GLenum target)
1003 {
1004 struct intel_image_format *f = image->planar_format;
1005 struct intel_mipmap_tree *planar_mt;
1006
1007 for (int i = 0; i < f->nplanes; i++) {
1008 const int index = f->planes[i].buffer_index;
1009 const uint32_t dri_format = f->planes[i].dri_format;
1010 const mesa_format format = driImageFormatToGLFormat(dri_format);
1011 const uint32_t width = image->width >> f->planes[i].width_shift;
1012 const uint32_t height = image->height >> f->planes[i].height_shift;
1013
1014 /* Disable creation of the texture's aux buffers because the driver
1015 * exposes no EGL API to manage them. That is, there is no API for
1016 * resolving the aux buffer's content to the main buffer nor for
1017 * invalidating the aux buffer's content.
1018 */
1019 struct intel_mipmap_tree *mt =
1020 intel_miptree_create_for_bo(brw, image->bo, format,
1021 image->offsets[index],
1022 width, height, 1,
1023 image->strides[index],
1024 MIPTREE_LAYOUT_DISABLE_AUX);
1025 if (mt == NULL)
1026 return NULL;
1027
1028 mt->target = target;
1029 mt->total_width = width;
1030 mt->total_height = height;
1031
1032 if (i == 0)
1033 planar_mt = mt;
1034 else
1035 planar_mt->plane[i - 1] = mt;
1036 }
1037
1038 return planar_mt;
1039 }
1040
1041 struct intel_mipmap_tree *
1042 intel_miptree_create_for_dri_image(struct brw_context *brw,
1043 __DRIimage *image, GLenum target,
1044 enum isl_colorspace colorspace,
1045 bool is_winsys_image)
1046 {
1047 if (image->planar_format && image->planar_format->nplanes > 0) {
1048 assert(colorspace == ISL_COLORSPACE_NONE ||
1049 colorspace == ISL_COLORSPACE_YUV);
1050 return miptree_create_for_planar_image(brw, image, target);
1051 }
1052
1053 mesa_format format = image->format;
1054 switch (colorspace) {
1055 case ISL_COLORSPACE_NONE:
1056 /* Keep the image format unmodified */
1057 break;
1058
1059 case ISL_COLORSPACE_LINEAR:
1060 format =_mesa_get_srgb_format_linear(format);
1061 break;
1062
1063 case ISL_COLORSPACE_SRGB:
1064 format =_mesa_get_linear_format_srgb(format);
1065 break;
1066
1067 default:
1068 unreachable("Inalid colorspace for non-planar image");
1069 }
1070
1071 if (!brw->ctx.TextureFormatSupported[format]) {
1072 /* The texture storage paths in core Mesa detect if the driver does not
1073 * support the user-requested format, and then searches for a
1074 * fallback format. The DRIimage code bypasses core Mesa, though. So we
1075 * do the fallbacks here for important formats.
1076 *
1077 * We must support DRM_FOURCC_XBGR8888 textures because the Android
1078 * framework produces HAL_PIXEL_FORMAT_RGBX8888 winsys surfaces, which
1079 * the Chrome OS compositor consumes as dma_buf EGLImages.
1080 */
1081 format = _mesa_format_fallback_rgbx_to_rgba(format);
1082 }
1083
1084 if (!brw->ctx.TextureFormatSupported[format])
1085 return NULL;
1086
1087 /* If this image comes in from a window system, we have different
1088 * requirements than if it comes in via an EGL import operation. Window
1089 * system images can use any form of auxiliary compression we wish because
1090 * they get "flushed" before being handed off to the window system and we
1091 * have the opportunity to do resolves. Window system buffers also may be
1092 * used for scanout so we need to flag that appropriately.
1093 */
1094 const uint32_t mt_layout_flags =
1095 is_winsys_image ? MIPTREE_LAYOUT_FOR_SCANOUT : MIPTREE_LAYOUT_DISABLE_AUX;
1096
1097 /* Disable creation of the texture's aux buffers because the driver exposes
1098 * no EGL API to manage them. That is, there is no API for resolving the aux
1099 * buffer's content to the main buffer nor for invalidating the aux buffer's
1100 * content.
1101 */
1102 struct intel_mipmap_tree *mt =
1103 intel_miptree_create_for_bo(brw, image->bo, format,
1104 image->offset, image->width, image->height, 1,
1105 image->pitch, mt_layout_flags);
1106 if (mt == NULL)
1107 return NULL;
1108
1109 mt->target = target;
1110 mt->level[0].level_x = image->tile_x;
1111 mt->level[0].level_y = image->tile_y;
1112 mt->level[0].slice[0].x_offset = image->tile_x;
1113 mt->level[0].slice[0].y_offset = image->tile_y;
1114 mt->total_width += image->tile_x;
1115 mt->total_height += image->tile_y;
1116
1117 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
1118 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
1119 * trouble resolving back to destination image due to alignment issues.
1120 */
1121 if (!brw->has_surface_tile_offset) {
1122 uint32_t draw_x, draw_y;
1123 intel_miptree_get_tile_offsets(mt, 0, 0, &draw_x, &draw_y);
1124
1125 if (draw_x != 0 || draw_y != 0) {
1126 _mesa_error(&brw->ctx, GL_INVALID_OPERATION, __func__);
1127 intel_miptree_release(&mt);
1128 return NULL;
1129 }
1130 }
1131
1132 return mt;
1133 }
1134
1135 /**
1136 * For a singlesample renderbuffer, this simply wraps the given BO with a
1137 * miptree.
1138 *
1139 * For a multisample renderbuffer, this wraps the window system's
1140 * (singlesample) BO with a singlesample miptree attached to the
1141 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
1142 * that will contain the actual rendering (which is lazily resolved to
1143 * irb->singlesample_mt).
1144 */
1145 bool
1146 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
1147 struct intel_renderbuffer *irb,
1148 struct intel_mipmap_tree *singlesample_mt,
1149 uint32_t width, uint32_t height,
1150 uint32_t pitch)
1151 {
1152 struct intel_mipmap_tree *multisample_mt = NULL;
1153 struct gl_renderbuffer *rb = &irb->Base.Base;
1154 mesa_format format = rb->Format;
1155 int num_samples = rb->NumSamples;
1156
1157 /* Only the front and back buffers, which are color buffers, are allocated
1158 * through the image loader.
1159 */
1160 assert(_mesa_get_format_base_format(format) == GL_RGB ||
1161 _mesa_get_format_base_format(format) == GL_RGBA);
1162
1163 assert(singlesample_mt);
1164
1165 if (num_samples == 0) {
1166 intel_miptree_release(&irb->mt);
1167 irb->mt = singlesample_mt;
1168
1169 assert(!irb->singlesample_mt);
1170 } else {
1171 intel_miptree_release(&irb->singlesample_mt);
1172 irb->singlesample_mt = singlesample_mt;
1173
1174 if (!irb->mt ||
1175 irb->mt->logical_width0 != width ||
1176 irb->mt->logical_height0 != height) {
1177 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
1178 format,
1179 width,
1180 height,
1181 num_samples);
1182 if (!multisample_mt)
1183 goto fail;
1184
1185 irb->need_downsample = false;
1186 intel_miptree_release(&irb->mt);
1187 irb->mt = multisample_mt;
1188 }
1189 }
1190 return true;
1191
1192 fail:
1193 intel_miptree_release(&irb->mt);
1194 return false;
1195 }
1196
1197 struct intel_mipmap_tree*
1198 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
1199 mesa_format format,
1200 uint32_t width,
1201 uint32_t height,
1202 uint32_t num_samples)
1203 {
1204 struct intel_mipmap_tree *mt;
1205 uint32_t depth = 1;
1206 bool ok;
1207 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
1208 const uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
1209 MIPTREE_LAYOUT_TILING_ANY |
1210 MIPTREE_LAYOUT_FOR_SCANOUT;
1211
1212 mt = intel_miptree_create(brw, target, format, 0, 0,
1213 width, height, depth, num_samples,
1214 layout_flags);
1215 if (!mt)
1216 goto fail;
1217
1218 if (mt->aux_usage == ISL_AUX_USAGE_HIZ) {
1219 ok = intel_miptree_alloc_hiz(brw, mt);
1220 if (!ok)
1221 goto fail;
1222 }
1223
1224 return mt;
1225
1226 fail:
1227 intel_miptree_release(&mt);
1228 return NULL;
1229 }
1230
1231 void
1232 intel_miptree_reference(struct intel_mipmap_tree **dst,
1233 struct intel_mipmap_tree *src)
1234 {
1235 if (*dst == src)
1236 return;
1237
1238 intel_miptree_release(dst);
1239
1240 if (src) {
1241 src->refcount++;
1242 DBG("%s %p refcount now %d\n", __func__, src, src->refcount);
1243 }
1244
1245 *dst = src;
1246 }
1247
1248 static void
1249 intel_miptree_aux_buffer_free(struct intel_miptree_aux_buffer *aux_buf)
1250 {
1251 if (aux_buf == NULL)
1252 return;
1253
1254 brw_bo_unreference(aux_buf->bo);
1255
1256 free(aux_buf);
1257 }
1258
1259 void
1260 intel_miptree_release(struct intel_mipmap_tree **mt)
1261 {
1262 if (!*mt)
1263 return;
1264
1265 DBG("%s %p refcount will be %d\n", __func__, *mt, (*mt)->refcount - 1);
1266 if (--(*mt)->refcount <= 0) {
1267 GLuint i;
1268
1269 DBG("%s deleting %p\n", __func__, *mt);
1270
1271 brw_bo_unreference((*mt)->bo);
1272 intel_miptree_release(&(*mt)->stencil_mt);
1273 intel_miptree_release(&(*mt)->r8stencil_mt);
1274 intel_miptree_aux_buffer_free((*mt)->hiz_buf);
1275 intel_miptree_aux_buffer_free((*mt)->mcs_buf);
1276 free_aux_state_map((*mt)->aux_state);
1277
1278 intel_miptree_release(&(*mt)->plane[0]);
1279 intel_miptree_release(&(*mt)->plane[1]);
1280
1281 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
1282 free((*mt)->level[i].slice);
1283 }
1284
1285 free(*mt);
1286 }
1287 *mt = NULL;
1288 }
1289
1290
1291 void
1292 intel_get_image_dims(struct gl_texture_image *image,
1293 int *width, int *height, int *depth)
1294 {
1295 switch (image->TexObject->Target) {
1296 case GL_TEXTURE_1D_ARRAY:
1297 /* For a 1D Array texture the OpenGL API will treat the image height as
1298 * the number of array slices. For Intel hardware, we treat the 1D array
1299 * as a 2D Array with a height of 1. So, here we want to swap image
1300 * height and depth.
1301 */
1302 assert(image->Depth == 1);
1303 *width = image->Width;
1304 *height = 1;
1305 *depth = image->Height;
1306 break;
1307 case GL_TEXTURE_CUBE_MAP:
1308 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
1309 * though we really have 6 slices.
1310 */
1311 assert(image->Depth == 1);
1312 *width = image->Width;
1313 *height = image->Height;
1314 *depth = 6;
1315 break;
1316 default:
1317 *width = image->Width;
1318 *height = image->Height;
1319 *depth = image->Depth;
1320 break;
1321 }
1322 }
1323
1324 /**
1325 * Can the image be pulled into a unified mipmap tree? This mirrors
1326 * the completeness test in a lot of ways.
1327 *
1328 * Not sure whether I want to pass gl_texture_image here.
1329 */
1330 bool
1331 intel_miptree_match_image(struct intel_mipmap_tree *mt,
1332 struct gl_texture_image *image)
1333 {
1334 struct intel_texture_image *intelImage = intel_texture_image(image);
1335 GLuint level = intelImage->base.Base.Level;
1336 int width, height, depth;
1337
1338 /* glTexImage* choose the texture object based on the target passed in, and
1339 * objects can't change targets over their lifetimes, so this should be
1340 * true.
1341 */
1342 assert(image->TexObject->Target == mt->target);
1343
1344 mesa_format mt_format = mt->format;
1345 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
1346 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
1347 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
1348 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
1349 if (mt->etc_format != MESA_FORMAT_NONE)
1350 mt_format = mt->etc_format;
1351
1352 if (image->TexFormat != mt_format)
1353 return false;
1354
1355 intel_get_image_dims(image, &width, &height, &depth);
1356
1357 if (mt->target == GL_TEXTURE_CUBE_MAP)
1358 depth = 6;
1359
1360 if (mt->surf.size > 0) {
1361 if (level >= mt->surf.levels)
1362 return false;
1363
1364 const unsigned level_depth =
1365 mt->surf.dim == ISL_SURF_DIM_3D ?
1366 minify(mt->surf.logical_level0_px.depth, level) :
1367 mt->surf.logical_level0_px.array_len;
1368
1369 return width == minify(mt->surf.logical_level0_px.width, level) &&
1370 height == minify(mt->surf.logical_level0_px.height, level) &&
1371 depth == level_depth &&
1372 MAX2(image->NumSamples, 1) == mt->surf.samples;
1373 }
1374
1375 int level_depth = mt->level[level].depth;
1376 if (mt->num_samples > 1) {
1377 switch (mt->msaa_layout) {
1378 case INTEL_MSAA_LAYOUT_NONE:
1379 case INTEL_MSAA_LAYOUT_IMS:
1380 break;
1381 case INTEL_MSAA_LAYOUT_UMS:
1382 case INTEL_MSAA_LAYOUT_CMS:
1383 level_depth /= mt->num_samples;
1384 break;
1385 }
1386 }
1387
1388 /* Test image dimensions against the base level image adjusted for
1389 * minification. This will also catch images not present in the
1390 * tree, changed targets, etc.
1391 */
1392 if (width != minify(mt->logical_width0, level - mt->first_level) ||
1393 height != minify(mt->logical_height0, level - mt->first_level) ||
1394 depth != level_depth) {
1395 return false;
1396 }
1397
1398 if (image->NumSamples != mt->num_samples)
1399 return false;
1400
1401 return true;
1402 }
1403
1404
1405 void
1406 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
1407 GLuint level,
1408 GLuint x, GLuint y, GLuint d)
1409 {
1410 mt->level[level].depth = d;
1411 mt->level[level].level_x = x;
1412 mt->level[level].level_y = y;
1413
1414 DBG("%s level %d, depth %d, offset %d,%d\n", __func__,
1415 level, d, x, y);
1416
1417 assert(mt->level[level].slice);
1418
1419 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
1420 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
1421 }
1422
1423
1424 void
1425 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
1426 GLuint level, GLuint img,
1427 GLuint x, GLuint y)
1428 {
1429 if (img == 0 && level == 0)
1430 assert(x == 0 && y == 0);
1431
1432 assert(img < mt->level[level].depth);
1433
1434 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
1435 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
1436
1437 DBG("%s level %d img %d pos %d,%d\n",
1438 __func__, level, img,
1439 mt->level[level].slice[img].x_offset,
1440 mt->level[level].slice[img].y_offset);
1441 }
1442
1443 void
1444 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
1445 GLuint level, GLuint slice,
1446 GLuint *x, GLuint *y)
1447 {
1448 if (mt->surf.size > 0) {
1449 uint32_t x_offset_sa, y_offset_sa;
1450
1451 /* Given level is relative to level zero while the miptree may be
1452 * represent just a subset of all levels starting from 'first_level'.
1453 */
1454 assert(level >= mt->first_level);
1455 level -= mt->first_level;
1456
1457 const unsigned z = mt->surf.dim == ISL_SURF_DIM_3D ? slice : 0;
1458 slice = mt->surf.dim == ISL_SURF_DIM_3D ? 0 : slice;
1459 isl_surf_get_image_offset_sa(&mt->surf, level, slice, z,
1460 &x_offset_sa, &y_offset_sa);
1461
1462 *x = x_offset_sa;
1463 *y = y_offset_sa;
1464 return;
1465 }
1466
1467 assert(slice < mt->level[level].depth);
1468
1469 *x = mt->level[level].slice[slice].x_offset;
1470 *y = mt->level[level].slice[slice].y_offset;
1471 }
1472
1473
1474 /**
1475 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1476 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1477 * and tile_h is set to 1.
1478 */
1479 void
1480 intel_get_tile_dims(uint32_t tiling, uint32_t cpp,
1481 uint32_t *tile_w, uint32_t *tile_h)
1482 {
1483 switch (tiling) {
1484 case I915_TILING_X:
1485 *tile_w = 512;
1486 *tile_h = 8;
1487 break;
1488 case I915_TILING_Y:
1489 *tile_w = 128;
1490 *tile_h = 32;
1491 break;
1492 case I915_TILING_NONE:
1493 *tile_w = cpp;
1494 *tile_h = 1;
1495 break;
1496 default:
1497 unreachable("not reached");
1498 }
1499 }
1500
1501
1502 /**
1503 * This function computes masks that may be used to select the bits of the X
1504 * and Y coordinates that indicate the offset within a tile. If the BO is
1505 * untiled, the masks are set to 0.
1506 */
1507 void
1508 intel_get_tile_masks(uint32_t tiling, uint32_t cpp,
1509 uint32_t *mask_x, uint32_t *mask_y)
1510 {
1511 uint32_t tile_w_bytes, tile_h;
1512
1513 intel_get_tile_dims(tiling, cpp, &tile_w_bytes, &tile_h);
1514
1515 *mask_x = tile_w_bytes / cpp - 1;
1516 *mask_y = tile_h - 1;
1517 }
1518
1519 /**
1520 * Compute the offset (in bytes) from the start of the BO to the given x
1521 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1522 * multiples of the tile size.
1523 */
1524 uint32_t
1525 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree *mt,
1526 uint32_t x, uint32_t y)
1527 {
1528 int cpp = mt->cpp;
1529 uint32_t pitch = mt->pitch;
1530 uint32_t tiling = mt->tiling;
1531
1532 switch (tiling) {
1533 default:
1534 unreachable("not reached");
1535 case I915_TILING_NONE:
1536 return y * pitch + x * cpp;
1537 case I915_TILING_X:
1538 assert((x % (512 / cpp)) == 0);
1539 assert((y % 8) == 0);
1540 return y * pitch + x / (512 / cpp) * 4096;
1541 case I915_TILING_Y:
1542 assert((x % (128 / cpp)) == 0);
1543 assert((y % 32) == 0);
1544 return y * pitch + x / (128 / cpp) * 4096;
1545 }
1546 }
1547
1548 /**
1549 * Rendering with tiled buffers requires that the base address of the buffer
1550 * be aligned to a page boundary. For renderbuffers, and sometimes with
1551 * textures, we may want the surface to point at a texture image level that
1552 * isn't at a page boundary.
1553 *
1554 * This function returns an appropriately-aligned base offset
1555 * according to the tiling restrictions, plus any required x/y offset
1556 * from there.
1557 */
1558 uint32_t
1559 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
1560 GLuint level, GLuint slice,
1561 uint32_t *tile_x,
1562 uint32_t *tile_y)
1563 {
1564 uint32_t x, y;
1565 uint32_t mask_x, mask_y;
1566
1567 intel_get_tile_masks(mt->tiling, mt->cpp, &mask_x, &mask_y);
1568 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
1569
1570 *tile_x = x & mask_x;
1571 *tile_y = y & mask_y;
1572
1573 return intel_miptree_get_aligned_offset(mt, x & ~mask_x, y & ~mask_y);
1574 }
1575
1576 static void
1577 intel_miptree_copy_slice_sw(struct brw_context *brw,
1578 struct intel_mipmap_tree *src_mt,
1579 unsigned src_level, unsigned src_layer,
1580 struct intel_mipmap_tree *dst_mt,
1581 unsigned dst_level, unsigned dst_layer,
1582 unsigned width, unsigned height)
1583 {
1584 void *src, *dst;
1585 ptrdiff_t src_stride, dst_stride;
1586 const unsigned cpp = dst_mt->surf.size > 0 ?
1587 (isl_format_get_layout(dst_mt->surf.format)->bpb / 8) : dst_mt->cpp;
1588
1589 intel_miptree_map(brw, src_mt,
1590 src_level, src_layer,
1591 0, 0,
1592 width, height,
1593 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
1594 &src, &src_stride);
1595
1596 intel_miptree_map(brw, dst_mt,
1597 dst_level, dst_layer,
1598 0, 0,
1599 width, height,
1600 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
1601 BRW_MAP_DIRECT_BIT,
1602 &dst, &dst_stride);
1603
1604 DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
1605 _mesa_get_format_name(src_mt->format),
1606 src_mt, src, src_stride,
1607 _mesa_get_format_name(dst_mt->format),
1608 dst_mt, dst, dst_stride,
1609 width, height);
1610
1611 int row_size = cpp * width;
1612 if (src_stride == row_size &&
1613 dst_stride == row_size) {
1614 memcpy(dst, src, row_size * height);
1615 } else {
1616 for (int i = 0; i < height; i++) {
1617 memcpy(dst, src, row_size);
1618 dst += dst_stride;
1619 src += src_stride;
1620 }
1621 }
1622
1623 intel_miptree_unmap(brw, dst_mt, dst_level, dst_layer);
1624 intel_miptree_unmap(brw, src_mt, src_level, src_layer);
1625
1626 /* Don't forget to copy the stencil data over, too. We could have skipped
1627 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1628 * shuffling the two data sources in/out of temporary storage instead of
1629 * the direct mapping we get this way.
1630 */
1631 if (dst_mt->stencil_mt) {
1632 assert(src_mt->stencil_mt);
1633 intel_miptree_copy_slice_sw(brw,
1634 src_mt->stencil_mt, src_level, src_layer,
1635 dst_mt->stencil_mt, dst_level, dst_layer,
1636 width, height);
1637 }
1638 }
1639
1640 void
1641 intel_miptree_copy_slice(struct brw_context *brw,
1642 struct intel_mipmap_tree *src_mt,
1643 unsigned src_level, unsigned src_layer,
1644 struct intel_mipmap_tree *dst_mt,
1645 unsigned dst_level, unsigned dst_layer)
1646
1647 {
1648 mesa_format format = src_mt->format;
1649 uint32_t width, height;
1650
1651 if (src_mt->surf.size > 0) {
1652 width = minify(src_mt->surf.phys_level0_sa.width,
1653 src_level - src_mt->first_level);
1654 height = minify(src_mt->surf.phys_level0_sa.height,
1655 src_level - src_mt->first_level);
1656
1657 if (src_mt->surf.dim == ISL_SURF_DIM_3D)
1658 assert(src_layer < minify(src_mt->surf.phys_level0_sa.depth,
1659 src_level - src_mt->first_level));
1660 else
1661 assert(src_layer < src_mt->surf.phys_level0_sa.array_len);
1662 } else {
1663 width = minify(src_mt->physical_width0,
1664 src_level - src_mt->first_level);
1665 height = minify(src_mt->physical_height0,
1666 src_level - src_mt->first_level);
1667 assert(src_layer < src_mt->level[src_level].depth);
1668 }
1669
1670 assert(src_mt->format == dst_mt->format);
1671
1672 if (dst_mt->compressed) {
1673 unsigned int i, j;
1674 _mesa_get_format_block_size(dst_mt->format, &i, &j);
1675 height = ALIGN_NPOT(height, j) / j;
1676 width = ALIGN_NPOT(width, i) / i;
1677 }
1678
1679 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1680 * below won't apply since we can't do the depth's Y tiling or the
1681 * stencil's W tiling in the blitter.
1682 */
1683 if (src_mt->stencil_mt) {
1684 intel_miptree_copy_slice_sw(brw,
1685 src_mt, src_level, src_layer,
1686 dst_mt, dst_level, dst_layer,
1687 width, height);
1688 return;
1689 }
1690
1691 uint32_t dst_x, dst_y, src_x, src_y;
1692 intel_miptree_get_image_offset(dst_mt, dst_level, dst_layer,
1693 &dst_x, &dst_y);
1694 intel_miptree_get_image_offset(src_mt, src_level, src_layer,
1695 &src_x, &src_y);
1696
1697 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1698 _mesa_get_format_name(src_mt->format),
1699 src_mt, src_x, src_y, src_mt->pitch,
1700 _mesa_get_format_name(dst_mt->format),
1701 dst_mt, dst_x, dst_y, dst_mt->pitch,
1702 width, height);
1703
1704 if (!intel_miptree_blit(brw,
1705 src_mt, src_level, src_layer, 0, 0, false,
1706 dst_mt, dst_level, dst_layer, 0, 0, false,
1707 width, height, GL_COPY)) {
1708 perf_debug("miptree validate blit for %s failed\n",
1709 _mesa_get_format_name(format));
1710
1711 intel_miptree_copy_slice_sw(brw,
1712 src_mt, src_level, src_layer,
1713 dst_mt, dst_level, dst_layer,
1714 width, height);
1715 }
1716 }
1717
1718 /**
1719 * Copies the image's current data to the given miptree, and associates that
1720 * miptree with the image.
1721 *
1722 * If \c invalidate is true, then the actual image data does not need to be
1723 * copied, but the image still needs to be associated to the new miptree (this
1724 * is set to true if we're about to clear the image).
1725 */
1726 void
1727 intel_miptree_copy_teximage(struct brw_context *brw,
1728 struct intel_texture_image *intelImage,
1729 struct intel_mipmap_tree *dst_mt,
1730 bool invalidate)
1731 {
1732 struct intel_mipmap_tree *src_mt = intelImage->mt;
1733 struct intel_texture_object *intel_obj =
1734 intel_texture_object(intelImage->base.Base.TexObject);
1735 int level = intelImage->base.Base.Level;
1736 const unsigned face = intelImage->base.Base.Face;
1737 unsigned start_layer, end_layer;
1738
1739 if (intel_obj->base.Target == GL_TEXTURE_1D_ARRAY) {
1740 assert(face == 0);
1741 assert(intelImage->base.Base.Height);
1742 start_layer = 0;
1743 end_layer = intelImage->base.Base.Height - 1;
1744 } else if (face > 0) {
1745 start_layer = face;
1746 end_layer = face;
1747 } else {
1748 assert(intelImage->base.Base.Depth);
1749 start_layer = 0;
1750 end_layer = intelImage->base.Base.Depth - 1;
1751 }
1752
1753 if (!invalidate) {
1754 for (unsigned i = start_layer; i <= end_layer; i++) {
1755 intel_miptree_copy_slice(brw,
1756 src_mt, level, i,
1757 dst_mt, level, i);
1758 }
1759 }
1760
1761 intel_miptree_reference(&intelImage->mt, dst_mt);
1762 intel_obj->needs_validate = true;
1763 }
1764
1765 static void
1766 intel_miptree_init_mcs(struct brw_context *brw,
1767 struct intel_mipmap_tree *mt,
1768 int init_value)
1769 {
1770 assert(mt->mcs_buf != NULL);
1771
1772 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1773 *
1774 * When MCS buffer is enabled and bound to MSRT, it is required that it
1775 * is cleared prior to any rendering.
1776 *
1777 * Since we don't use the MCS buffer for any purpose other than rendering,
1778 * it makes sense to just clear it immediately upon allocation.
1779 *
1780 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1781 */
1782 void *map = brw_bo_map(brw, mt->mcs_buf->bo, MAP_WRITE);
1783 if (unlikely(map == NULL)) {
1784 fprintf(stderr, "Failed to map mcs buffer into GTT\n");
1785 brw_bo_unreference(mt->mcs_buf->bo);
1786 free(mt->mcs_buf);
1787 return;
1788 }
1789 void *data = map;
1790 memset(data, init_value, mt->mcs_buf->size);
1791 brw_bo_unmap(mt->mcs_buf->bo);
1792 }
1793
1794 static struct intel_miptree_aux_buffer *
1795 intel_alloc_aux_buffer(struct brw_context *brw,
1796 const char *name,
1797 const struct isl_surf *aux_surf,
1798 uint32_t alloc_flags,
1799 struct intel_mipmap_tree *mt)
1800 {
1801 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1802 if (!buf)
1803 return false;
1804
1805 buf->size = aux_surf->size;
1806 buf->pitch = aux_surf->row_pitch;
1807 buf->qpitch = isl_surf_get_array_pitch_sa_rows(aux_surf);
1808
1809 /* ISL has stricter set of alignment rules then the drm allocator.
1810 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1811 * trying to recalculate based on different format block sizes.
1812 */
1813 buf->bo = brw_bo_alloc_tiled(brw->bufmgr, name, buf->size,
1814 I915_TILING_Y, buf->pitch, alloc_flags);
1815 if (!buf->bo) {
1816 free(buf);
1817 return NULL;
1818 }
1819
1820 buf->surf = *aux_surf;
1821
1822 return buf;
1823 }
1824
1825 static bool
1826 intel_miptree_alloc_mcs(struct brw_context *brw,
1827 struct intel_mipmap_tree *mt,
1828 GLuint num_samples)
1829 {
1830 assert(brw->gen >= 7); /* MCS only used on Gen7+ */
1831 assert(mt->mcs_buf == NULL);
1832 assert(mt->aux_usage == ISL_AUX_USAGE_MCS);
1833
1834 /* Multisampled miptrees are only supported for single level. */
1835 assert(mt->first_level == 0);
1836 enum isl_aux_state **aux_state =
1837 create_aux_state_map(mt, ISL_AUX_STATE_CLEAR);
1838 if (!aux_state)
1839 return false;
1840
1841 struct isl_surf temp_main_surf;
1842 struct isl_surf temp_mcs_surf;
1843
1844 /* Create first an ISL presentation for the main color surface and let ISL
1845 * calculate equivalent MCS surface against it.
1846 */
1847 intel_miptree_get_isl_surf(brw, mt, &temp_main_surf);
1848 MAYBE_UNUSED bool ok =
1849 isl_surf_get_mcs_surf(&brw->isl_dev, &temp_main_surf, &temp_mcs_surf);
1850 assert(ok);
1851
1852 /* Buffer needs to be initialised requiring the buffer to be immediately
1853 * mapped to cpu space for writing. Therefore do not use the gpu access
1854 * flag which can cause an unnecessary delay if the backing pages happened
1855 * to be just used by the GPU.
1856 */
1857 const uint32_t alloc_flags = 0;
1858 mt->mcs_buf = intel_alloc_aux_buffer(brw, "mcs-miptree",
1859 &temp_mcs_surf, alloc_flags, mt);
1860 if (!mt->mcs_buf) {
1861 free(aux_state);
1862 return false;
1863 }
1864
1865 mt->aux_state = aux_state;
1866
1867 intel_miptree_init_mcs(brw, mt, 0xFF);
1868
1869 return true;
1870 }
1871
1872 bool
1873 intel_miptree_alloc_ccs(struct brw_context *brw,
1874 struct intel_mipmap_tree *mt)
1875 {
1876 assert(mt->mcs_buf == NULL);
1877 assert(mt->aux_usage == ISL_AUX_USAGE_CCS_E ||
1878 mt->aux_usage == ISL_AUX_USAGE_CCS_D);
1879
1880 struct isl_surf temp_main_surf;
1881 struct isl_surf temp_ccs_surf;
1882
1883 /* Create first an ISL presentation for the main color surface and let ISL
1884 * calculate equivalent CCS surface against it.
1885 */
1886 intel_miptree_get_isl_surf(brw, mt, &temp_main_surf);
1887 if (!isl_surf_get_ccs_surf(&brw->isl_dev, &temp_main_surf, &temp_ccs_surf))
1888 return false;
1889
1890 assert(temp_ccs_surf.size &&
1891 (temp_ccs_surf.size % temp_ccs_surf.row_pitch == 0));
1892
1893 enum isl_aux_state **aux_state =
1894 create_aux_state_map(mt, ISL_AUX_STATE_PASS_THROUGH);
1895 if (!aux_state)
1896 return false;
1897
1898 /* In case of compression mcs buffer needs to be initialised requiring the
1899 * buffer to be immediately mapped to cpu space for writing. Therefore do
1900 * not use the gpu access flag which can cause an unnecessary delay if the
1901 * backing pages happened to be just used by the GPU.
1902 */
1903 const uint32_t alloc_flags =
1904 mt->aux_usage == ISL_AUX_USAGE_CCS_E ? 0 : BO_ALLOC_FOR_RENDER;
1905 mt->mcs_buf = intel_alloc_aux_buffer(brw, "ccs-miptree",
1906 &temp_ccs_surf, alloc_flags, mt);
1907 if (!mt->mcs_buf) {
1908 free(aux_state);
1909 return false;
1910 }
1911
1912 mt->aux_state = aux_state;
1913
1914 /* From Gen9 onwards single-sampled (non-msrt) auxiliary buffers are
1915 * used for lossless compression which requires similar initialisation
1916 * as multi-sample compression.
1917 */
1918 if (mt->aux_usage == ISL_AUX_USAGE_CCS_E) {
1919 /* Hardware sets the auxiliary buffer to all zeroes when it does full
1920 * resolve. Initialize it accordingly in case the first renderer is
1921 * cpu (or other none compression aware party).
1922 *
1923 * This is also explicitly stated in the spec (MCS Buffer for Render
1924 * Target(s)):
1925 * "If Software wants to enable Color Compression without Fast clear,
1926 * Software needs to initialize MCS with zeros."
1927 */
1928 intel_miptree_init_mcs(brw, mt, 0);
1929 mt->msaa_layout = INTEL_MSAA_LAYOUT_CMS;
1930 }
1931
1932 return true;
1933 }
1934
1935 /**
1936 * Helper for intel_miptree_alloc_hiz() that sets
1937 * \c mt->level[level].has_hiz. Return true if and only if
1938 * \c has_hiz was set.
1939 */
1940 static bool
1941 intel_miptree_level_enable_hiz(struct brw_context *brw,
1942 struct intel_mipmap_tree *mt,
1943 uint32_t level)
1944 {
1945 assert(mt->hiz_buf);
1946
1947 if (brw->gen >= 8 || brw->is_haswell) {
1948 uint32_t width = minify(mt->physical_width0, level);
1949 uint32_t height = minify(mt->physical_height0, level);
1950
1951 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1952 * and the height is 4 aligned. This allows our HiZ support
1953 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1954 * we can grow the width & height to allow the HiZ op to
1955 * force the proper size alignments.
1956 */
1957 if (level > 0 && ((width & 7) || (height & 3))) {
1958 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1959 return false;
1960 }
1961 }
1962
1963 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1964 mt->level[level].has_hiz = true;
1965 return true;
1966 }
1967
1968 bool
1969 intel_miptree_alloc_hiz(struct brw_context *brw,
1970 struct intel_mipmap_tree *mt)
1971 {
1972 assert(mt->hiz_buf == NULL);
1973 assert(mt->aux_usage == ISL_AUX_USAGE_HIZ);
1974
1975 enum isl_aux_state **aux_state =
1976 create_aux_state_map(mt, ISL_AUX_STATE_AUX_INVALID);
1977 if (!aux_state)
1978 return false;
1979
1980 struct isl_surf temp_main_surf;
1981 struct isl_surf temp_hiz_surf;
1982
1983 intel_miptree_get_isl_surf(brw, mt, &temp_main_surf);
1984 MAYBE_UNUSED bool ok =
1985 isl_surf_get_hiz_surf(&brw->isl_dev, &temp_main_surf, &temp_hiz_surf);
1986 assert(ok);
1987
1988 const uint32_t alloc_flags = BO_ALLOC_FOR_RENDER;
1989 mt->hiz_buf = intel_alloc_aux_buffer(brw, "hiz-miptree",
1990 &temp_hiz_surf, alloc_flags, mt);
1991
1992 if (!mt->hiz_buf) {
1993 free(aux_state);
1994 return false;
1995 }
1996
1997 for (unsigned level = mt->first_level; level <= mt->last_level; ++level)
1998 intel_miptree_level_enable_hiz(brw, mt, level);
1999
2000 mt->aux_state = aux_state;
2001
2002 return true;
2003 }
2004
2005 /**
2006 * Can the miptree sample using the hiz buffer?
2007 */
2008 bool
2009 intel_miptree_sample_with_hiz(struct brw_context *brw,
2010 struct intel_mipmap_tree *mt)
2011 {
2012 /* It's unclear how well supported sampling from the hiz buffer is on GEN8,
2013 * so keep things conservative for now and never enable it unless we're SKL+.
2014 */
2015 if (brw->gen < 9) {
2016 return false;
2017 }
2018
2019 if (!mt->hiz_buf) {
2020 return false;
2021 }
2022
2023 /* It seems the hardware won't fallback to the depth buffer if some of the
2024 * mipmap levels aren't available in the HiZ buffer. So we need all levels
2025 * of the texture to be HiZ enabled.
2026 */
2027 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
2028 if (!intel_miptree_level_has_hiz(mt, level))
2029 return false;
2030 }
2031
2032 /* If compressed multisampling is enabled, then we use it for the auxiliary
2033 * buffer instead.
2034 *
2035 * From the BDW PRM (Volume 2d: Command Reference: Structures
2036 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
2037 *
2038 * "If this field is set to AUX_HIZ, Number of Multisamples must be
2039 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
2040 *
2041 * There is no such blurb for 1D textures, but there is sufficient evidence
2042 * that this is broken on SKL+.
2043 */
2044 return (mt->num_samples <= 1 &&
2045 mt->target != GL_TEXTURE_3D &&
2046 mt->target != GL_TEXTURE_1D /* gen9+ restriction */);
2047 }
2048
2049 /**
2050 * Does the miptree slice have hiz enabled?
2051 */
2052 bool
2053 intel_miptree_level_has_hiz(const struct intel_mipmap_tree *mt, uint32_t level)
2054 {
2055 intel_miptree_check_level_layer(mt, level, 0);
2056 return mt->level[level].has_hiz;
2057 }
2058
2059 bool
2060 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree *mt,
2061 unsigned start_level, unsigned num_levels,
2062 unsigned start_layer, unsigned num_layers)
2063 {
2064 assert(_mesa_is_format_color_format(mt->format));
2065
2066 if (!mt->mcs_buf)
2067 return false;
2068
2069 /* Clamp the level range to fit the miptree */
2070 assert(start_level + num_levels >= start_level);
2071 const uint32_t last_level =
2072 MIN2(mt->last_level, start_level + num_levels - 1);
2073 start_level = MAX2(mt->first_level, start_level);
2074 num_levels = last_level - start_level + 1;
2075
2076 for (uint32_t level = start_level; level <= last_level; level++) {
2077 const uint32_t level_layers = MIN2(num_layers, mt->level[level].depth);
2078 for (unsigned a = 0; a < level_layers; a++) {
2079 enum isl_aux_state aux_state =
2080 intel_miptree_get_aux_state(mt, level, start_layer + a);
2081 assert(aux_state != ISL_AUX_STATE_AUX_INVALID);
2082 if (aux_state != ISL_AUX_STATE_PASS_THROUGH)
2083 return true;
2084 }
2085 }
2086
2087 return false;
2088 }
2089
2090 static void
2091 intel_miptree_check_color_resolve(const struct brw_context *brw,
2092 const struct intel_mipmap_tree *mt,
2093 unsigned level, unsigned layer)
2094 {
2095
2096 if (!mt->mcs_buf)
2097 return;
2098
2099 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
2100 assert(brw->gen >= 8 ||
2101 (level == 0 && mt->first_level == 0 && mt->last_level == 0));
2102
2103 /* Compression of arrayed msaa surfaces is supported. */
2104 if (mt->num_samples > 1)
2105 return;
2106
2107 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
2108 assert(brw->gen >= 8 || (layer == 0 && mt->logical_depth0 == 1));
2109
2110 (void)level;
2111 (void)layer;
2112 }
2113
2114 static enum blorp_fast_clear_op
2115 get_ccs_d_resolve_op(enum isl_aux_state aux_state,
2116 bool ccs_supported, bool fast_clear_supported)
2117 {
2118 assert(ccs_supported == fast_clear_supported);
2119
2120 switch (aux_state) {
2121 case ISL_AUX_STATE_CLEAR:
2122 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2123 if (!ccs_supported)
2124 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2125 else
2126 return BLORP_FAST_CLEAR_OP_NONE;
2127
2128 case ISL_AUX_STATE_PASS_THROUGH:
2129 return BLORP_FAST_CLEAR_OP_NONE;
2130
2131 case ISL_AUX_STATE_RESOLVED:
2132 case ISL_AUX_STATE_AUX_INVALID:
2133 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2134 break;
2135 }
2136
2137 unreachable("Invalid aux state for CCS_D");
2138 }
2139
2140 static enum blorp_fast_clear_op
2141 get_ccs_e_resolve_op(enum isl_aux_state aux_state,
2142 bool ccs_supported, bool fast_clear_supported)
2143 {
2144 switch (aux_state) {
2145 case ISL_AUX_STATE_CLEAR:
2146 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2147 if (!ccs_supported)
2148 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2149 else if (!fast_clear_supported)
2150 return BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
2151 else
2152 return BLORP_FAST_CLEAR_OP_NONE;
2153
2154 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2155 if (!ccs_supported)
2156 return BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2157 else
2158 return BLORP_FAST_CLEAR_OP_NONE;
2159
2160 case ISL_AUX_STATE_PASS_THROUGH:
2161 return BLORP_FAST_CLEAR_OP_NONE;
2162
2163 case ISL_AUX_STATE_RESOLVED:
2164 case ISL_AUX_STATE_AUX_INVALID:
2165 break;
2166 }
2167
2168 unreachable("Invalid aux state for CCS_E");
2169 }
2170
2171 static void
2172 intel_miptree_prepare_ccs_access(struct brw_context *brw,
2173 struct intel_mipmap_tree *mt,
2174 uint32_t level, uint32_t layer,
2175 bool aux_supported,
2176 bool fast_clear_supported)
2177 {
2178 enum isl_aux_state aux_state = intel_miptree_get_aux_state(mt, level, layer);
2179
2180 enum blorp_fast_clear_op resolve_op;
2181 if (intel_miptree_is_lossless_compressed(brw, mt)) {
2182 resolve_op = get_ccs_e_resolve_op(aux_state, aux_supported,
2183 fast_clear_supported);
2184 } else {
2185 resolve_op = get_ccs_d_resolve_op(aux_state, aux_supported,
2186 fast_clear_supported);
2187 }
2188
2189 if (resolve_op != BLORP_FAST_CLEAR_OP_NONE) {
2190 intel_miptree_check_color_resolve(brw, mt, level, layer);
2191 brw_blorp_resolve_color(brw, mt, level, layer, resolve_op);
2192
2193 switch (resolve_op) {
2194 case BLORP_FAST_CLEAR_OP_RESOLVE_FULL:
2195 /* The CCS full resolve operation destroys the CCS and sets it to the
2196 * pass-through state. (You can also think of this as being both a
2197 * resolve and an ambiguate in one operation.)
2198 */
2199 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2200 ISL_AUX_STATE_PASS_THROUGH);
2201 break;
2202
2203 case BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL:
2204 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2205 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2206 break;
2207
2208 default:
2209 unreachable("Invalid resolve op");
2210 }
2211 }
2212 }
2213
2214 static void
2215 intel_miptree_finish_ccs_write(struct brw_context *brw,
2216 struct intel_mipmap_tree *mt,
2217 uint32_t level, uint32_t layer,
2218 bool written_with_ccs)
2219 {
2220 enum isl_aux_state aux_state = intel_miptree_get_aux_state(mt, level, layer);
2221
2222 if (intel_miptree_is_lossless_compressed(brw, mt)) {
2223 switch (aux_state) {
2224 case ISL_AUX_STATE_CLEAR:
2225 assert(written_with_ccs);
2226 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2227 ISL_AUX_STATE_COMPRESSED_CLEAR);
2228 break;
2229
2230 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2231 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2232 assert(written_with_ccs);
2233 break; /* Nothing to do */
2234
2235 case ISL_AUX_STATE_PASS_THROUGH:
2236 if (written_with_ccs) {
2237 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2238 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2239 } else {
2240 /* Nothing to do */
2241 }
2242 break;
2243
2244 case ISL_AUX_STATE_RESOLVED:
2245 case ISL_AUX_STATE_AUX_INVALID:
2246 unreachable("Invalid aux state for CCS_E");
2247 }
2248 } else {
2249 /* CCS_D is a bit simpler */
2250 switch (aux_state) {
2251 case ISL_AUX_STATE_CLEAR:
2252 assert(written_with_ccs);
2253 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2254 ISL_AUX_STATE_COMPRESSED_CLEAR);
2255 break;
2256
2257 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2258 assert(written_with_ccs);
2259 break; /* Nothing to do */
2260
2261 case ISL_AUX_STATE_PASS_THROUGH:
2262 /* Nothing to do */
2263 break;
2264
2265 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2266 case ISL_AUX_STATE_RESOLVED:
2267 case ISL_AUX_STATE_AUX_INVALID:
2268 unreachable("Invalid aux state for CCS_D");
2269 }
2270 }
2271 }
2272
2273 static void
2274 intel_miptree_finish_mcs_write(struct brw_context *brw,
2275 struct intel_mipmap_tree *mt,
2276 uint32_t level, uint32_t layer,
2277 bool written_with_aux)
2278 {
2279 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2280 case ISL_AUX_STATE_CLEAR:
2281 assert(written_with_aux);
2282 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2283 ISL_AUX_STATE_COMPRESSED_CLEAR);
2284 break;
2285
2286 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2287 assert(written_with_aux);
2288 break; /* Nothing to do */
2289
2290 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2291 case ISL_AUX_STATE_RESOLVED:
2292 case ISL_AUX_STATE_PASS_THROUGH:
2293 case ISL_AUX_STATE_AUX_INVALID:
2294 unreachable("Invalid aux state for MCS");
2295 }
2296 }
2297
2298 static void
2299 intel_miptree_prepare_hiz_access(struct brw_context *brw,
2300 struct intel_mipmap_tree *mt,
2301 uint32_t level, uint32_t layer,
2302 bool hiz_supported, bool fast_clear_supported)
2303 {
2304 enum blorp_hiz_op hiz_op = BLORP_HIZ_OP_NONE;
2305 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2306 case ISL_AUX_STATE_CLEAR:
2307 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2308 if (!hiz_supported || !fast_clear_supported)
2309 hiz_op = BLORP_HIZ_OP_DEPTH_RESOLVE;
2310 break;
2311
2312 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2313 if (!hiz_supported)
2314 hiz_op = BLORP_HIZ_OP_DEPTH_RESOLVE;
2315 break;
2316
2317 case ISL_AUX_STATE_PASS_THROUGH:
2318 case ISL_AUX_STATE_RESOLVED:
2319 break;
2320
2321 case ISL_AUX_STATE_AUX_INVALID:
2322 if (hiz_supported)
2323 hiz_op = BLORP_HIZ_OP_HIZ_RESOLVE;
2324 break;
2325 }
2326
2327 if (hiz_op != BLORP_HIZ_OP_NONE) {
2328 intel_hiz_exec(brw, mt, level, layer, 1, hiz_op);
2329
2330 switch (hiz_op) {
2331 case BLORP_HIZ_OP_DEPTH_RESOLVE:
2332 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2333 ISL_AUX_STATE_RESOLVED);
2334 break;
2335
2336 case BLORP_HIZ_OP_HIZ_RESOLVE:
2337 /* The HiZ resolve operation is actually an ambiguate */
2338 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2339 ISL_AUX_STATE_PASS_THROUGH);
2340 break;
2341
2342 default:
2343 unreachable("Invalid HiZ op");
2344 }
2345 }
2346 }
2347
2348 static void
2349 intel_miptree_finish_hiz_write(struct brw_context *brw,
2350 struct intel_mipmap_tree *mt,
2351 uint32_t level, uint32_t layer,
2352 bool written_with_hiz)
2353 {
2354 switch (intel_miptree_get_aux_state(mt, level, layer)) {
2355 case ISL_AUX_STATE_CLEAR:
2356 assert(written_with_hiz);
2357 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2358 ISL_AUX_STATE_COMPRESSED_CLEAR);
2359 break;
2360
2361 case ISL_AUX_STATE_COMPRESSED_NO_CLEAR:
2362 case ISL_AUX_STATE_COMPRESSED_CLEAR:
2363 assert(written_with_hiz);
2364 break; /* Nothing to do */
2365
2366 case ISL_AUX_STATE_RESOLVED:
2367 if (written_with_hiz) {
2368 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2369 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2370 } else {
2371 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2372 ISL_AUX_STATE_AUX_INVALID);
2373 }
2374 break;
2375
2376 case ISL_AUX_STATE_PASS_THROUGH:
2377 if (written_with_hiz) {
2378 intel_miptree_set_aux_state(brw, mt, level, layer, 1,
2379 ISL_AUX_STATE_COMPRESSED_NO_CLEAR);
2380 }
2381 break;
2382
2383 case ISL_AUX_STATE_AUX_INVALID:
2384 assert(!written_with_hiz);
2385 break;
2386 }
2387 }
2388
2389 static inline uint32_t
2390 miptree_level_range_length(const struct intel_mipmap_tree *mt,
2391 uint32_t start_level, uint32_t num_levels)
2392 {
2393 assert(start_level >= mt->first_level);
2394 assert(start_level <= mt->last_level);
2395
2396 if (num_levels == INTEL_REMAINING_LAYERS)
2397 num_levels = mt->last_level - start_level + 1;
2398 /* Check for overflow */
2399 assert(start_level + num_levels >= start_level);
2400 assert(start_level + num_levels <= mt->last_level + 1);
2401
2402 return num_levels;
2403 }
2404
2405 static inline uint32_t
2406 miptree_layer_range_length(const struct intel_mipmap_tree *mt, uint32_t level,
2407 uint32_t start_layer, uint32_t num_layers)
2408 {
2409 assert(level <= mt->last_level);
2410 uint32_t total_num_layers;
2411
2412 if (mt->surf.size > 0)
2413 total_num_layers = mt->surf.dim == ISL_SURF_DIM_3D ?
2414 minify(mt->surf.phys_level0_sa.depth, level) :
2415 mt->surf.phys_level0_sa.array_len;
2416 else
2417 total_num_layers = mt->level[level].depth;
2418
2419 assert(start_layer < total_num_layers);
2420 if (num_layers == INTEL_REMAINING_LAYERS)
2421 num_layers = total_num_layers - start_layer;
2422 /* Check for overflow */
2423 assert(start_layer + num_layers >= start_layer);
2424 assert(start_layer + num_layers <= total_num_layers);
2425
2426 return num_layers;
2427 }
2428
2429 void
2430 intel_miptree_prepare_access(struct brw_context *brw,
2431 struct intel_mipmap_tree *mt,
2432 uint32_t start_level, uint32_t num_levels,
2433 uint32_t start_layer, uint32_t num_layers,
2434 bool aux_supported, bool fast_clear_supported)
2435 {
2436 num_levels = miptree_level_range_length(mt, start_level, num_levels);
2437
2438 if (_mesa_is_format_color_format(mt->format)) {
2439 if (!mt->mcs_buf)
2440 return;
2441
2442 if (mt->num_samples > 1) {
2443 /* Nothing to do for MSAA */
2444 assert(aux_supported && fast_clear_supported);
2445 } else {
2446 for (uint32_t l = 0; l < num_levels; l++) {
2447 const uint32_t level = start_level + l;
2448 const uint32_t level_layers =
2449 miptree_layer_range_length(mt, level, start_layer, num_layers);
2450 for (uint32_t a = 0; a < level_layers; a++) {
2451 intel_miptree_prepare_ccs_access(brw, mt, level,
2452 start_layer + a, aux_supported,
2453 fast_clear_supported);
2454 }
2455 }
2456 }
2457 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2458 /* Nothing to do for stencil */
2459 } else {
2460 if (!mt->hiz_buf)
2461 return;
2462
2463 for (uint32_t l = 0; l < num_levels; l++) {
2464 const uint32_t level = start_level + l;
2465 if (!intel_miptree_level_has_hiz(mt, level))
2466 continue;
2467
2468 const uint32_t level_layers =
2469 miptree_layer_range_length(mt, level, start_layer, num_layers);
2470 for (uint32_t a = 0; a < level_layers; a++) {
2471 intel_miptree_prepare_hiz_access(brw, mt, level, start_layer + a,
2472 aux_supported,
2473 fast_clear_supported);
2474 }
2475 }
2476 }
2477 }
2478
2479 void
2480 intel_miptree_finish_write(struct brw_context *brw,
2481 struct intel_mipmap_tree *mt, uint32_t level,
2482 uint32_t start_layer, uint32_t num_layers,
2483 bool written_with_aux)
2484 {
2485 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2486
2487 if (_mesa_is_format_color_format(mt->format)) {
2488 if (!mt->mcs_buf)
2489 return;
2490
2491 if (mt->num_samples > 1) {
2492 for (uint32_t a = 0; a < num_layers; a++) {
2493 intel_miptree_finish_mcs_write(brw, mt, level, start_layer + a,
2494 written_with_aux);
2495 }
2496 } else {
2497 for (uint32_t a = 0; a < num_layers; a++) {
2498 intel_miptree_finish_ccs_write(brw, mt, level, start_layer + a,
2499 written_with_aux);
2500 }
2501 }
2502 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2503 /* Nothing to do for stencil */
2504 } else {
2505 if (!intel_miptree_level_has_hiz(mt, level))
2506 return;
2507
2508 for (uint32_t a = 0; a < num_layers; a++) {
2509 intel_miptree_finish_hiz_write(brw, mt, level, start_layer + a,
2510 written_with_aux);
2511 }
2512 }
2513 }
2514
2515 enum isl_aux_state
2516 intel_miptree_get_aux_state(const struct intel_mipmap_tree *mt,
2517 uint32_t level, uint32_t layer)
2518 {
2519 intel_miptree_check_level_layer(mt, level, layer);
2520
2521 if (_mesa_is_format_color_format(mt->format)) {
2522 assert(mt->mcs_buf != NULL);
2523 assert(mt->num_samples <= 1 || mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS);
2524 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2525 unreachable("Cannot get aux state for stencil");
2526 } else {
2527 assert(intel_miptree_level_has_hiz(mt, level));
2528 }
2529
2530 return mt->aux_state[level][layer];
2531 }
2532
2533 void
2534 intel_miptree_set_aux_state(struct brw_context *brw,
2535 struct intel_mipmap_tree *mt, uint32_t level,
2536 uint32_t start_layer, uint32_t num_layers,
2537 enum isl_aux_state aux_state)
2538 {
2539 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2540
2541 if (_mesa_is_format_color_format(mt->format)) {
2542 assert(mt->mcs_buf != NULL);
2543 assert(mt->num_samples <= 1 || mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS);
2544 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2545 unreachable("Cannot get aux state for stencil");
2546 } else {
2547 assert(intel_miptree_level_has_hiz(mt, level));
2548 }
2549
2550 for (unsigned a = 0; a < num_layers; a++)
2551 mt->aux_state[level][start_layer + a] = aux_state;
2552 }
2553
2554 /* On Gen9 color buffers may be compressed by the hardware (lossless
2555 * compression). There are, however, format restrictions and care needs to be
2556 * taken that the sampler engine is capable for re-interpreting a buffer with
2557 * format different the buffer was originally written with.
2558 *
2559 * For example, SRGB formats are not compressible and the sampler engine isn't
2560 * capable of treating RGBA_UNORM as SRGB_ALPHA. In such a case the underlying
2561 * color buffer needs to be resolved so that the sampling surface can be
2562 * sampled as non-compressed (i.e., without the auxiliary MCS buffer being
2563 * set).
2564 */
2565 static bool
2566 can_texture_with_ccs(struct brw_context *brw,
2567 struct intel_mipmap_tree *mt,
2568 mesa_format view_format)
2569 {
2570 if (!intel_miptree_is_lossless_compressed(brw, mt))
2571 return false;
2572
2573 enum isl_format isl_mt_format = brw_isl_format_for_mesa_format(mt->format);
2574 enum isl_format isl_view_format = brw_isl_format_for_mesa_format(view_format);
2575
2576 if (!isl_formats_are_ccs_e_compatible(&brw->screen->devinfo,
2577 isl_mt_format, isl_view_format)) {
2578 perf_debug("Incompatible sampling format (%s) for rbc (%s)\n",
2579 _mesa_get_format_name(view_format),
2580 _mesa_get_format_name(mt->format));
2581 return false;
2582 }
2583
2584 return true;
2585 }
2586
2587 static void
2588 intel_miptree_prepare_texture_slices(struct brw_context *brw,
2589 struct intel_mipmap_tree *mt,
2590 mesa_format view_format,
2591 uint32_t start_level, uint32_t num_levels,
2592 uint32_t start_layer, uint32_t num_layers,
2593 bool *aux_supported_out)
2594 {
2595 bool aux_supported, clear_supported;
2596 if (_mesa_is_format_color_format(mt->format)) {
2597 if (mt->num_samples > 1) {
2598 aux_supported = clear_supported = true;
2599 } else {
2600 aux_supported = can_texture_with_ccs(brw, mt, view_format);
2601
2602 /* Clear color is specified as ints or floats and the conversion is
2603 * done by the sampler. If we have a texture view, we would have to
2604 * perform the clear color conversion manually. Just disable clear
2605 * color.
2606 */
2607 clear_supported = aux_supported && (mt->format == view_format);
2608 }
2609 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2610 aux_supported = clear_supported = false;
2611 } else {
2612 aux_supported = clear_supported = intel_miptree_sample_with_hiz(brw, mt);
2613 }
2614
2615 intel_miptree_prepare_access(brw, mt, start_level, num_levels,
2616 start_layer, num_layers,
2617 aux_supported, clear_supported);
2618 if (aux_supported_out)
2619 *aux_supported_out = aux_supported;
2620 }
2621
2622 void
2623 intel_miptree_prepare_texture(struct brw_context *brw,
2624 struct intel_mipmap_tree *mt,
2625 mesa_format view_format,
2626 bool *aux_supported_out)
2627 {
2628 intel_miptree_prepare_texture_slices(brw, mt, view_format,
2629 0, INTEL_REMAINING_LEVELS,
2630 0, INTEL_REMAINING_LAYERS,
2631 aux_supported_out);
2632 }
2633
2634 void
2635 intel_miptree_prepare_image(struct brw_context *brw,
2636 struct intel_mipmap_tree *mt)
2637 {
2638 /* The data port doesn't understand any compression */
2639 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2640 0, INTEL_REMAINING_LAYERS, false, false);
2641 }
2642
2643 void
2644 intel_miptree_prepare_fb_fetch(struct brw_context *brw,
2645 struct intel_mipmap_tree *mt, uint32_t level,
2646 uint32_t start_layer, uint32_t num_layers)
2647 {
2648 intel_miptree_prepare_texture_slices(brw, mt, mt->format, level, 1,
2649 start_layer, num_layers, NULL);
2650 }
2651
2652 void
2653 intel_miptree_prepare_render(struct brw_context *brw,
2654 struct intel_mipmap_tree *mt, uint32_t level,
2655 uint32_t start_layer, uint32_t layer_count,
2656 bool srgb_enabled)
2657 {
2658 /* If FRAMEBUFFER_SRGB is used on Gen9+ then we need to resolve any of
2659 * the single-sampled color renderbuffers because the CCS buffer isn't
2660 * supported for SRGB formats. This only matters if FRAMEBUFFER_SRGB is
2661 * enabled because otherwise the surface state will be programmed with
2662 * the linear equivalent format anyway.
2663 */
2664 if (brw->gen == 9 && srgb_enabled && mt->num_samples <= 1 &&
2665 _mesa_get_srgb_format_linear(mt->format) != mt->format) {
2666
2667 /* Lossless compression is not supported for SRGB formats, it
2668 * should be impossible to get here with such surfaces.
2669 */
2670 assert(!intel_miptree_is_lossless_compressed(brw, mt));
2671 intel_miptree_prepare_access(brw, mt, level, 1, start_layer, layer_count,
2672 false, false);
2673 }
2674 }
2675
2676 void
2677 intel_miptree_finish_render(struct brw_context *brw,
2678 struct intel_mipmap_tree *mt, uint32_t level,
2679 uint32_t start_layer, uint32_t layer_count)
2680 {
2681 assert(_mesa_is_format_color_format(mt->format));
2682 intel_miptree_finish_write(brw, mt, level, start_layer, layer_count,
2683 mt->mcs_buf != NULL);
2684 }
2685
2686 void
2687 intel_miptree_prepare_depth(struct brw_context *brw,
2688 struct intel_mipmap_tree *mt, uint32_t level,
2689 uint32_t start_layer, uint32_t layer_count)
2690 {
2691 intel_miptree_prepare_access(brw, mt, level, 1, start_layer, layer_count,
2692 mt->hiz_buf != NULL, mt->hiz_buf != NULL);
2693 }
2694
2695 void
2696 intel_miptree_finish_depth(struct brw_context *brw,
2697 struct intel_mipmap_tree *mt, uint32_t level,
2698 uint32_t start_layer, uint32_t layer_count,
2699 bool depth_written)
2700 {
2701 if (depth_written) {
2702 intel_miptree_finish_write(brw, mt, level, start_layer, layer_count,
2703 mt->hiz_buf != NULL);
2704 }
2705 }
2706
2707 /**
2708 * Make it possible to share the BO backing the given miptree with another
2709 * process or another miptree.
2710 *
2711 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2712 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2713 * ensure that no MCS buffer gets allocated in the future.
2714 *
2715 * HiZ is similarly unsafe with shared buffers.
2716 */
2717 void
2718 intel_miptree_make_shareable(struct brw_context *brw,
2719 struct intel_mipmap_tree *mt)
2720 {
2721 /* MCS buffers are also used for multisample buffers, but we can't resolve
2722 * away a multisample MCS buffer because it's an integral part of how the
2723 * pixel data is stored. Fortunately this code path should never be
2724 * reached for multisample buffers.
2725 */
2726 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE || mt->num_samples <= 1);
2727
2728 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2729 0, INTEL_REMAINING_LAYERS, false, false);
2730
2731 if (mt->mcs_buf) {
2732 brw_bo_unreference(mt->mcs_buf->bo);
2733 free(mt->mcs_buf);
2734 mt->mcs_buf = NULL;
2735
2736 /* Any pending MCS/CCS operations are no longer needed. Trying to
2737 * execute any will likely crash due to the missing aux buffer. So let's
2738 * delete all pending ops.
2739 */
2740 free(mt->aux_state);
2741 mt->aux_state = NULL;
2742 }
2743
2744 if (mt->hiz_buf) {
2745 intel_miptree_aux_buffer_free(mt->hiz_buf);
2746 mt->hiz_buf = NULL;
2747
2748 for (uint32_t l = mt->first_level; l <= mt->last_level; ++l) {
2749 mt->level[l].has_hiz = false;
2750 }
2751
2752 /* Any pending HiZ operations are no longer needed. Trying to execute
2753 * any will likely crash due to the missing aux buffer. So let's delete
2754 * all pending ops.
2755 */
2756 free(mt->aux_state);
2757 mt->aux_state = NULL;
2758 }
2759
2760 mt->aux_usage = ISL_AUX_USAGE_NONE;
2761 }
2762
2763
2764 /**
2765 * \brief Get pointer offset into stencil buffer.
2766 *
2767 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2768 * must decode the tile's layout in software.
2769 *
2770 * See
2771 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2772 * Format.
2773 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2774 *
2775 * Even though the returned offset is always positive, the return type is
2776 * signed due to
2777 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2778 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2779 */
2780 static intptr_t
2781 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2782 {
2783 uint32_t tile_size = 4096;
2784 uint32_t tile_width = 64;
2785 uint32_t tile_height = 64;
2786 uint32_t row_size = 64 * stride;
2787
2788 uint32_t tile_x = x / tile_width;
2789 uint32_t tile_y = y / tile_height;
2790
2791 /* The byte's address relative to the tile's base addres. */
2792 uint32_t byte_x = x % tile_width;
2793 uint32_t byte_y = y % tile_height;
2794
2795 uintptr_t u = tile_y * row_size
2796 + tile_x * tile_size
2797 + 512 * (byte_x / 8)
2798 + 64 * (byte_y / 8)
2799 + 32 * ((byte_y / 4) % 2)
2800 + 16 * ((byte_x / 4) % 2)
2801 + 8 * ((byte_y / 2) % 2)
2802 + 4 * ((byte_x / 2) % 2)
2803 + 2 * (byte_y % 2)
2804 + 1 * (byte_x % 2);
2805
2806 if (swizzled) {
2807 /* adjust for bit6 swizzling */
2808 if (((byte_x / 8) % 2) == 1) {
2809 if (((byte_y / 8) % 2) == 0) {
2810 u += 64;
2811 } else {
2812 u -= 64;
2813 }
2814 }
2815 }
2816
2817 return u;
2818 }
2819
2820 void
2821 intel_miptree_updownsample(struct brw_context *brw,
2822 struct intel_mipmap_tree *src,
2823 struct intel_mipmap_tree *dst)
2824 {
2825 unsigned src_w, src_h, dst_w, dst_h;
2826
2827 if (src->surf.size > 0) {
2828 src_w = src->surf.logical_level0_px.width;
2829 src_h = src->surf.logical_level0_px.height;
2830 } else {
2831 src_w = src->logical_width0;
2832 src_h = src->logical_height0;
2833 }
2834
2835 if (dst->surf.size > 0) {
2836 dst_w = dst->surf.logical_level0_px.width;
2837 dst_h = dst->surf.logical_level0_px.height;
2838 } else {
2839 dst_w = dst->logical_width0;
2840 dst_h = dst->logical_height0;
2841 }
2842
2843 brw_blorp_blit_miptrees(brw,
2844 src, 0 /* level */, 0 /* layer */,
2845 src->format, SWIZZLE_XYZW,
2846 dst, 0 /* level */, 0 /* layer */, dst->format,
2847 0, 0, src_w, src_h,
2848 0, 0, dst_w, dst_h,
2849 GL_NEAREST, false, false /*mirror x, y*/,
2850 false, false);
2851
2852 if (src->stencil_mt) {
2853 if (src->stencil_mt->surf.size > 0) {
2854 src_w = src->stencil_mt->surf.logical_level0_px.width;
2855 src_h = src->stencil_mt->surf.logical_level0_px.height;
2856 } else {
2857 src_w = src->stencil_mt->logical_width0;
2858 src_h = src->stencil_mt->logical_height0;
2859 }
2860
2861 if (dst->stencil_mt->surf.size > 0) {
2862 dst_w = dst->stencil_mt->surf.logical_level0_px.width;
2863 dst_h = dst->stencil_mt->surf.logical_level0_px.height;
2864 } else {
2865 dst_w = dst->stencil_mt->logical_width0;
2866 dst_h = dst->stencil_mt->logical_height0;
2867 }
2868
2869 brw_blorp_blit_miptrees(brw,
2870 src->stencil_mt, 0 /* level */, 0 /* layer */,
2871 src->stencil_mt->format, SWIZZLE_XYZW,
2872 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2873 dst->stencil_mt->format,
2874 0, 0, src_w, src_h,
2875 0, 0, dst_w, dst_h,
2876 GL_NEAREST, false, false /*mirror x, y*/,
2877 false, false /* decode/encode srgb */);
2878 }
2879 }
2880
2881 void
2882 intel_update_r8stencil(struct brw_context *brw,
2883 struct intel_mipmap_tree *mt)
2884 {
2885 assert(brw->gen >= 7);
2886 struct intel_mipmap_tree *src =
2887 mt->format == MESA_FORMAT_S_UINT8 ? mt : mt->stencil_mt;
2888 if (!src || brw->gen >= 8 || !src->r8stencil_needs_update)
2889 return;
2890
2891 if (!mt->r8stencil_mt) {
2892 const uint32_t r8stencil_flags =
2893 MIPTREE_LAYOUT_ACCELERATED_UPLOAD | MIPTREE_LAYOUT_TILING_Y |
2894 MIPTREE_LAYOUT_DISABLE_AUX;
2895 assert(brw->gen > 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2896 mt->r8stencil_mt = intel_miptree_create(brw,
2897 src->target,
2898 MESA_FORMAT_R_UINT8,
2899 src->first_level,
2900 src->last_level,
2901 src->logical_width0,
2902 src->logical_height0,
2903 src->logical_depth0,
2904 src->num_samples,
2905 r8stencil_flags);
2906 assert(mt->r8stencil_mt);
2907 }
2908
2909 struct intel_mipmap_tree *dst = mt->r8stencil_mt;
2910
2911 for (int level = src->first_level; level <= src->last_level; level++) {
2912 const unsigned depth = src->level[level].depth;
2913
2914 for (unsigned layer = 0; layer < depth; layer++) {
2915 brw_blorp_copy_miptrees(brw,
2916 src, level, layer,
2917 dst, level, layer,
2918 0, 0, 0, 0,
2919 minify(src->logical_width0, level),
2920 minify(src->logical_height0, level));
2921 }
2922 }
2923
2924 brw_render_cache_set_check_flush(brw, dst->bo);
2925 src->r8stencil_needs_update = false;
2926 }
2927
2928 static void *
2929 intel_miptree_map_raw(struct brw_context *brw,
2930 struct intel_mipmap_tree *mt,
2931 GLbitfield mode)
2932 {
2933 struct brw_bo *bo = mt->bo;
2934
2935 if (brw_batch_references(&brw->batch, bo))
2936 intel_batchbuffer_flush(brw);
2937
2938 return brw_bo_map(brw, bo, mode);
2939 }
2940
2941 static void
2942 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2943 {
2944 brw_bo_unmap(mt->bo);
2945 }
2946
2947 static void
2948 intel_miptree_map_gtt(struct brw_context *brw,
2949 struct intel_mipmap_tree *mt,
2950 struct intel_miptree_map *map,
2951 unsigned int level, unsigned int slice)
2952 {
2953 unsigned int bw, bh;
2954 void *base;
2955 unsigned int image_x, image_y;
2956 intptr_t x = map->x;
2957 intptr_t y = map->y;
2958
2959 /* For compressed formats, the stride is the number of bytes per
2960 * row of blocks. intel_miptree_get_image_offset() already does
2961 * the divide.
2962 */
2963 _mesa_get_format_block_size(mt->format, &bw, &bh);
2964 assert(y % bh == 0);
2965 assert(x % bw == 0);
2966 y /= bh;
2967 x /= bw;
2968
2969 base = intel_miptree_map_raw(brw, mt, map->mode) + mt->offset;
2970
2971 if (base == NULL)
2972 map->ptr = NULL;
2973 else {
2974 /* Note that in the case of cube maps, the caller must have passed the
2975 * slice number referencing the face.
2976 */
2977 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2978 x += image_x;
2979 y += image_y;
2980
2981 map->stride = mt->pitch;
2982 map->ptr = base + y * map->stride + x * mt->cpp;
2983 }
2984
2985 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2986 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2987 map->x, map->y, map->w, map->h,
2988 mt, _mesa_get_format_name(mt->format),
2989 x, y, map->ptr, map->stride);
2990 }
2991
2992 static void
2993 intel_miptree_unmap_gtt(struct intel_mipmap_tree *mt)
2994 {
2995 intel_miptree_unmap_raw(mt);
2996 }
2997
2998 static void
2999 intel_miptree_map_blit(struct brw_context *brw,
3000 struct intel_mipmap_tree *mt,
3001 struct intel_miptree_map *map,
3002 unsigned int level, unsigned int slice)
3003 {
3004 map->linear_mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
3005 /* first_level */ 0,
3006 /* last_level */ 0,
3007 map->w, map->h, 1,
3008 /* samples */ 0,
3009 MIPTREE_LAYOUT_TILING_NONE);
3010
3011 if (!map->linear_mt) {
3012 fprintf(stderr, "Failed to allocate blit temporary\n");
3013 goto fail;
3014 }
3015 map->stride = map->linear_mt->pitch;
3016
3017 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3018 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3019 * invalidate is set, since we'll be writing the whole rectangle from our
3020 * temporary buffer back out.
3021 */
3022 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3023 if (!intel_miptree_copy(brw,
3024 mt, level, slice, map->x, map->y,
3025 map->linear_mt, 0, 0, 0, 0,
3026 map->w, map->h)) {
3027 fprintf(stderr, "Failed to blit\n");
3028 goto fail;
3029 }
3030 }
3031
3032 map->ptr = intel_miptree_map_raw(brw, map->linear_mt, map->mode);
3033
3034 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
3035 map->x, map->y, map->w, map->h,
3036 mt, _mesa_get_format_name(mt->format),
3037 level, slice, map->ptr, map->stride);
3038
3039 return;
3040
3041 fail:
3042 intel_miptree_release(&map->linear_mt);
3043 map->ptr = NULL;
3044 map->stride = 0;
3045 }
3046
3047 static void
3048 intel_miptree_unmap_blit(struct brw_context *brw,
3049 struct intel_mipmap_tree *mt,
3050 struct intel_miptree_map *map,
3051 unsigned int level,
3052 unsigned int slice)
3053 {
3054 struct gl_context *ctx = &brw->ctx;
3055
3056 intel_miptree_unmap_raw(map->linear_mt);
3057
3058 if (map->mode & GL_MAP_WRITE_BIT) {
3059 bool ok = intel_miptree_copy(brw,
3060 map->linear_mt, 0, 0, 0, 0,
3061 mt, level, slice, map->x, map->y,
3062 map->w, map->h);
3063 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
3064 }
3065
3066 intel_miptree_release(&map->linear_mt);
3067 }
3068
3069 /**
3070 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
3071 */
3072 #if defined(USE_SSE41)
3073 static void
3074 intel_miptree_map_movntdqa(struct brw_context *brw,
3075 struct intel_mipmap_tree *mt,
3076 struct intel_miptree_map *map,
3077 unsigned int level, unsigned int slice)
3078 {
3079 assert(map->mode & GL_MAP_READ_BIT);
3080 assert(!(map->mode & GL_MAP_WRITE_BIT));
3081
3082 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
3083 map->x, map->y, map->w, map->h,
3084 mt, _mesa_get_format_name(mt->format),
3085 level, slice, map->ptr, map->stride);
3086
3087 /* Map the original image */
3088 uint32_t image_x;
3089 uint32_t image_y;
3090 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3091 image_x += map->x;
3092 image_y += map->y;
3093
3094 void *src = intel_miptree_map_raw(brw, mt, map->mode);
3095 if (!src)
3096 return;
3097
3098 src += mt->offset;
3099
3100 src += image_y * mt->pitch;
3101 src += image_x * mt->cpp;
3102
3103 /* Due to the pixel offsets for the particular image being mapped, our
3104 * src pointer may not be 16-byte aligned. However, if the pitch is
3105 * divisible by 16, then the amount by which it's misaligned will remain
3106 * consistent from row to row.
3107 */
3108 assert((mt->pitch % 16) == 0);
3109 const int misalignment = ((uintptr_t) src) & 15;
3110
3111 /* Create an untiled temporary buffer for the mapping. */
3112 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
3113
3114 map->stride = ALIGN(misalignment + width_bytes, 16);
3115
3116 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
3117 /* Offset the destination so it has the same misalignment as src. */
3118 map->ptr = map->buffer + misalignment;
3119
3120 assert((((uintptr_t) map->ptr) & 15) == misalignment);
3121
3122 for (uint32_t y = 0; y < map->h; y++) {
3123 void *dst_ptr = map->ptr + y * map->stride;
3124 void *src_ptr = src + y * mt->pitch;
3125
3126 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
3127 }
3128
3129 intel_miptree_unmap_raw(mt);
3130 }
3131
3132 static void
3133 intel_miptree_unmap_movntdqa(struct brw_context *brw,
3134 struct intel_mipmap_tree *mt,
3135 struct intel_miptree_map *map,
3136 unsigned int level,
3137 unsigned int slice)
3138 {
3139 _mesa_align_free(map->buffer);
3140 map->buffer = NULL;
3141 map->ptr = NULL;
3142 }
3143 #endif
3144
3145 static void
3146 intel_miptree_map_s8(struct brw_context *brw,
3147 struct intel_mipmap_tree *mt,
3148 struct intel_miptree_map *map,
3149 unsigned int level, unsigned int slice)
3150 {
3151 map->stride = map->w;
3152 map->buffer = map->ptr = malloc(map->stride * map->h);
3153 if (!map->buffer)
3154 return;
3155
3156 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3157 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3158 * invalidate is set, since we'll be writing the whole rectangle from our
3159 * temporary buffer back out.
3160 */
3161 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3162 /* ISL uses a stencil pitch value that is expected by hardware whereas
3163 * traditional miptree uses half of that. Below the value gets supplied
3164 * to intel_offset_S8() which expects the legacy interpretation.
3165 */
3166 const unsigned pitch = mt->surf.size > 0 ?
3167 mt->surf.row_pitch / 2 : mt->pitch;
3168 uint8_t *untiled_s8_map = map->ptr;
3169 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_READ_BIT);
3170 unsigned int image_x, image_y;
3171
3172 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3173
3174 for (uint32_t y = 0; y < map->h; y++) {
3175 for (uint32_t x = 0; x < map->w; x++) {
3176 ptrdiff_t offset = intel_offset_S8(pitch,
3177 x + image_x + map->x,
3178 y + image_y + map->y,
3179 brw->has_swizzling);
3180 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
3181 }
3182 }
3183
3184 intel_miptree_unmap_raw(mt);
3185
3186 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
3187 map->x, map->y, map->w, map->h,
3188 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
3189 } else {
3190 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3191 map->x, map->y, map->w, map->h,
3192 mt, map->ptr, map->stride);
3193 }
3194 }
3195
3196 static void
3197 intel_miptree_unmap_s8(struct brw_context *brw,
3198 struct intel_mipmap_tree *mt,
3199 struct intel_miptree_map *map,
3200 unsigned int level,
3201 unsigned int slice)
3202 {
3203 if (map->mode & GL_MAP_WRITE_BIT) {
3204 /* ISL uses a stencil pitch value that is expected by hardware whereas
3205 * traditional miptree uses half of that. Below the value gets supplied
3206 * to intel_offset_S8() which expects the legacy interpretation.
3207 */
3208 const unsigned pitch = mt->surf.size > 0 ?
3209 mt->surf.row_pitch / 2: mt->pitch;
3210 unsigned int image_x, image_y;
3211 uint8_t *untiled_s8_map = map->ptr;
3212 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT);
3213
3214 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3215
3216 for (uint32_t y = 0; y < map->h; y++) {
3217 for (uint32_t x = 0; x < map->w; x++) {
3218 ptrdiff_t offset = intel_offset_S8(pitch,
3219 image_x + x + map->x,
3220 image_y + y + map->y,
3221 brw->has_swizzling);
3222 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
3223 }
3224 }
3225
3226 intel_miptree_unmap_raw(mt);
3227 }
3228
3229 free(map->buffer);
3230 }
3231
3232 static void
3233 intel_miptree_map_etc(struct brw_context *brw,
3234 struct intel_mipmap_tree *mt,
3235 struct intel_miptree_map *map,
3236 unsigned int level,
3237 unsigned int slice)
3238 {
3239 assert(mt->etc_format != MESA_FORMAT_NONE);
3240 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
3241 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
3242 }
3243
3244 assert(map->mode & GL_MAP_WRITE_BIT);
3245 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
3246
3247 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
3248 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
3249 map->w, map->h, 1));
3250 map->ptr = map->buffer;
3251 }
3252
3253 static void
3254 intel_miptree_unmap_etc(struct brw_context *brw,
3255 struct intel_mipmap_tree *mt,
3256 struct intel_miptree_map *map,
3257 unsigned int level,
3258 unsigned int slice)
3259 {
3260 uint32_t image_x;
3261 uint32_t image_y;
3262 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
3263
3264 image_x += map->x;
3265 image_y += map->y;
3266
3267 uint8_t *dst = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT)
3268 + image_y * mt->pitch
3269 + image_x * mt->cpp;
3270
3271 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
3272 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
3273 map->ptr, map->stride,
3274 map->w, map->h);
3275 else
3276 _mesa_unpack_etc2_format(dst, mt->pitch,
3277 map->ptr, map->stride,
3278 map->w, map->h, mt->etc_format);
3279
3280 intel_miptree_unmap_raw(mt);
3281 free(map->buffer);
3282 }
3283
3284 /**
3285 * Mapping function for packed depth/stencil miptrees backed by real separate
3286 * miptrees for depth and stencil.
3287 *
3288 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
3289 * separate from the depth buffer. Yet at the GL API level, we have to expose
3290 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
3291 * be able to map that memory for texture storage and glReadPixels-type
3292 * operations. We give Mesa core that access by mallocing a temporary and
3293 * copying the data between the actual backing store and the temporary.
3294 */
3295 static void
3296 intel_miptree_map_depthstencil(struct brw_context *brw,
3297 struct intel_mipmap_tree *mt,
3298 struct intel_miptree_map *map,
3299 unsigned int level, unsigned int slice)
3300 {
3301 struct intel_mipmap_tree *z_mt = mt;
3302 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3303 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3304 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
3305
3306 map->stride = map->w * packed_bpp;
3307 map->buffer = map->ptr = malloc(map->stride * map->h);
3308 if (!map->buffer)
3309 return;
3310
3311 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
3312 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
3313 * invalidate is set, since we'll be writing the whole rectangle from our
3314 * temporary buffer back out.
3315 */
3316 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
3317 /* ISL uses a stencil pitch value that is expected by hardware whereas
3318 * traditional miptree uses half of that. Below the value gets supplied
3319 * to intel_offset_S8() which expects the legacy interpretation.
3320 */
3321 const unsigned s_pitch = s_mt->surf.size > 0 ?
3322 s_mt->surf.row_pitch / 2 : s_mt->pitch;
3323 uint32_t *packed_map = map->ptr;
3324 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_READ_BIT);
3325 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_READ_BIT);
3326 unsigned int s_image_x, s_image_y;
3327 unsigned int z_image_x, z_image_y;
3328
3329 intel_miptree_get_image_offset(s_mt, level, slice,
3330 &s_image_x, &s_image_y);
3331 intel_miptree_get_image_offset(z_mt, level, slice,
3332 &z_image_x, &z_image_y);
3333
3334 for (uint32_t y = 0; y < map->h; y++) {
3335 for (uint32_t x = 0; x < map->w; x++) {
3336 int map_x = map->x + x, map_y = map->y + y;
3337 ptrdiff_t s_offset = intel_offset_S8(s_pitch,
3338 map_x + s_image_x,
3339 map_y + s_image_y,
3340 brw->has_swizzling);
3341 ptrdiff_t z_offset = ((map_y + z_image_y) *
3342 (z_mt->pitch / 4) +
3343 (map_x + z_image_x));
3344 uint8_t s = s_map[s_offset];
3345 uint32_t z = z_map[z_offset];
3346
3347 if (map_z32f_x24s8) {
3348 packed_map[(y * map->w + x) * 2 + 0] = z;
3349 packed_map[(y * map->w + x) * 2 + 1] = s;
3350 } else {
3351 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
3352 }
3353 }
3354 }
3355
3356 intel_miptree_unmap_raw(s_mt);
3357 intel_miptree_unmap_raw(z_mt);
3358
3359 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3360 __func__,
3361 map->x, map->y, map->w, map->h,
3362 z_mt, map->x + z_image_x, map->y + z_image_y,
3363 s_mt, map->x + s_image_x, map->y + s_image_y,
3364 map->ptr, map->stride);
3365 } else {
3366 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3367 map->x, map->y, map->w, map->h,
3368 mt, map->ptr, map->stride);
3369 }
3370 }
3371
3372 static void
3373 intel_miptree_unmap_depthstencil(struct brw_context *brw,
3374 struct intel_mipmap_tree *mt,
3375 struct intel_miptree_map *map,
3376 unsigned int level,
3377 unsigned int slice)
3378 {
3379 struct intel_mipmap_tree *z_mt = mt;
3380 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3381 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3382
3383 if (map->mode & GL_MAP_WRITE_BIT) {
3384 /* ISL uses a stencil pitch value that is expected by hardware whereas
3385 * traditional miptree uses half of that. Below the value gets supplied
3386 * to intel_offset_S8() which expects the legacy interpretation.
3387 */
3388 const unsigned s_pitch = s_mt->surf.size > 0 ?
3389 s_mt->surf.row_pitch / 2 : s_mt->pitch;
3390 uint32_t *packed_map = map->ptr;
3391 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_WRITE_BIT);
3392 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_WRITE_BIT);
3393 unsigned int s_image_x, s_image_y;
3394 unsigned int z_image_x, z_image_y;
3395
3396 intel_miptree_get_image_offset(s_mt, level, slice,
3397 &s_image_x, &s_image_y);
3398 intel_miptree_get_image_offset(z_mt, level, slice,
3399 &z_image_x, &z_image_y);
3400
3401 for (uint32_t y = 0; y < map->h; y++) {
3402 for (uint32_t x = 0; x < map->w; x++) {
3403 ptrdiff_t s_offset = intel_offset_S8(s_pitch,
3404 x + s_image_x + map->x,
3405 y + s_image_y + map->y,
3406 brw->has_swizzling);
3407 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
3408 (z_mt->pitch / 4) +
3409 (x + z_image_x + map->x));
3410
3411 if (map_z32f_x24s8) {
3412 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
3413 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
3414 } else {
3415 uint32_t packed = packed_map[y * map->w + x];
3416 s_map[s_offset] = packed >> 24;
3417 z_map[z_offset] = packed;
3418 }
3419 }
3420 }
3421
3422 intel_miptree_unmap_raw(s_mt);
3423 intel_miptree_unmap_raw(z_mt);
3424
3425 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3426 __func__,
3427 map->x, map->y, map->w, map->h,
3428 z_mt, _mesa_get_format_name(z_mt->format),
3429 map->x + z_image_x, map->y + z_image_y,
3430 s_mt, map->x + s_image_x, map->y + s_image_y,
3431 map->ptr, map->stride);
3432 }
3433
3434 free(map->buffer);
3435 }
3436
3437 /**
3438 * Create and attach a map to the miptree at (level, slice). Return the
3439 * attached map.
3440 */
3441 static struct intel_miptree_map*
3442 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
3443 unsigned int level,
3444 unsigned int slice,
3445 unsigned int x,
3446 unsigned int y,
3447 unsigned int w,
3448 unsigned int h,
3449 GLbitfield mode)
3450 {
3451 struct intel_miptree_map *map = calloc(1, sizeof(*map));
3452
3453 if (!map)
3454 return NULL;
3455
3456 assert(mt->level[level].slice[slice].map == NULL);
3457 mt->level[level].slice[slice].map = map;
3458
3459 map->mode = mode;
3460 map->x = x;
3461 map->y = y;
3462 map->w = w;
3463 map->h = h;
3464
3465 return map;
3466 }
3467
3468 /**
3469 * Release the map at (level, slice).
3470 */
3471 static void
3472 intel_miptree_release_map(struct intel_mipmap_tree *mt,
3473 unsigned int level,
3474 unsigned int slice)
3475 {
3476 struct intel_miptree_map **map;
3477
3478 map = &mt->level[level].slice[slice].map;
3479 free(*map);
3480 *map = NULL;
3481 }
3482
3483 static bool
3484 can_blit_slice(struct intel_mipmap_tree *mt,
3485 unsigned int level, unsigned int slice)
3486 {
3487 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3488 if (mt->pitch >= 32768)
3489 return false;
3490
3491 return true;
3492 }
3493
3494 static bool
3495 use_intel_mipree_map_blit(struct brw_context *brw,
3496 struct intel_mipmap_tree *mt,
3497 GLbitfield mode,
3498 unsigned int level,
3499 unsigned int slice)
3500 {
3501 if (brw->has_llc &&
3502 /* It's probably not worth swapping to the blit ring because of
3503 * all the overhead involved.
3504 */
3505 !(mode & GL_MAP_WRITE_BIT) &&
3506 !mt->compressed &&
3507 (mt->tiling == I915_TILING_X ||
3508 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3509 (brw->gen >= 6 && mt->tiling == I915_TILING_Y) ||
3510 /* Fast copy blit on skl+ supports all tiling formats. */
3511 brw->gen >= 9) &&
3512 can_blit_slice(mt, level, slice))
3513 return true;
3514
3515 if (mt->tiling != I915_TILING_NONE &&
3516 mt->bo->size >= brw->max_gtt_map_object_size) {
3517 assert(can_blit_slice(mt, level, slice));
3518 return true;
3519 }
3520
3521 return false;
3522 }
3523
3524 /**
3525 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3526 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3527 * arithmetic overflow.
3528 *
3529 * If you call this function and use \a out_stride, then you're doing pointer
3530 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3531 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3532 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3533 * which usually have type uint32_t or GLuint.
3534 */
3535 void
3536 intel_miptree_map(struct brw_context *brw,
3537 struct intel_mipmap_tree *mt,
3538 unsigned int level,
3539 unsigned int slice,
3540 unsigned int x,
3541 unsigned int y,
3542 unsigned int w,
3543 unsigned int h,
3544 GLbitfield mode,
3545 void **out_ptr,
3546 ptrdiff_t *out_stride)
3547 {
3548 struct intel_miptree_map *map;
3549
3550 assert(mt->num_samples <= 1);
3551
3552 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
3553 if (!map){
3554 *out_ptr = NULL;
3555 *out_stride = 0;
3556 return;
3557 }
3558
3559 intel_miptree_access_raw(brw, mt, level, slice,
3560 map->mode & GL_MAP_WRITE_BIT);
3561
3562 if (mt->format == MESA_FORMAT_S_UINT8) {
3563 intel_miptree_map_s8(brw, mt, map, level, slice);
3564 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3565 !(mode & BRW_MAP_DIRECT_BIT)) {
3566 intel_miptree_map_etc(brw, mt, map, level, slice);
3567 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
3568 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
3569 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
3570 intel_miptree_map_blit(brw, mt, map, level, slice);
3571 #if defined(USE_SSE41)
3572 } else if (!(mode & GL_MAP_WRITE_BIT) &&
3573 !mt->compressed && cpu_has_sse4_1 &&
3574 (mt->pitch % 16 == 0)) {
3575 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
3576 #endif
3577 } else {
3578 intel_miptree_map_gtt(brw, mt, map, level, slice);
3579 }
3580
3581 *out_ptr = map->ptr;
3582 *out_stride = map->stride;
3583
3584 if (map->ptr == NULL)
3585 intel_miptree_release_map(mt, level, slice);
3586 }
3587
3588 void
3589 intel_miptree_unmap(struct brw_context *brw,
3590 struct intel_mipmap_tree *mt,
3591 unsigned int level,
3592 unsigned int slice)
3593 {
3594 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
3595
3596 assert(mt->num_samples <= 1);
3597
3598 if (!map)
3599 return;
3600
3601 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
3602 mt, _mesa_get_format_name(mt->format), level, slice);
3603
3604 if (mt->format == MESA_FORMAT_S_UINT8) {
3605 intel_miptree_unmap_s8(brw, mt, map, level, slice);
3606 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3607 !(map->mode & BRW_MAP_DIRECT_BIT)) {
3608 intel_miptree_unmap_etc(brw, mt, map, level, slice);
3609 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
3610 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
3611 } else if (map->linear_mt) {
3612 intel_miptree_unmap_blit(brw, mt, map, level, slice);
3613 #if defined(USE_SSE41)
3614 } else if (map->buffer && cpu_has_sse4_1) {
3615 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
3616 #endif
3617 } else {
3618 intel_miptree_unmap_gtt(mt);
3619 }
3620
3621 intel_miptree_release_map(mt, level, slice);
3622 }
3623
3624 enum isl_surf_dim
3625 get_isl_surf_dim(GLenum target)
3626 {
3627 switch (target) {
3628 case GL_TEXTURE_1D:
3629 case GL_TEXTURE_1D_ARRAY:
3630 return ISL_SURF_DIM_1D;
3631
3632 case GL_TEXTURE_2D:
3633 case GL_TEXTURE_2D_ARRAY:
3634 case GL_TEXTURE_RECTANGLE:
3635 case GL_TEXTURE_CUBE_MAP:
3636 case GL_TEXTURE_CUBE_MAP_ARRAY:
3637 case GL_TEXTURE_2D_MULTISAMPLE:
3638 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3639 case GL_TEXTURE_EXTERNAL_OES:
3640 return ISL_SURF_DIM_2D;
3641
3642 case GL_TEXTURE_3D:
3643 return ISL_SURF_DIM_3D;
3644 }
3645
3646 unreachable("Invalid texture target");
3647 }
3648
3649 enum isl_dim_layout
3650 get_isl_dim_layout(const struct gen_device_info *devinfo, uint32_t tiling,
3651 GLenum target, enum miptree_array_layout array_layout)
3652 {
3653 if (array_layout == GEN6_HIZ_STENCIL)
3654 return ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ;
3655
3656 switch (target) {
3657 case GL_TEXTURE_1D:
3658 case GL_TEXTURE_1D_ARRAY:
3659 return (devinfo->gen >= 9 && tiling == I915_TILING_NONE ?
3660 ISL_DIM_LAYOUT_GEN9_1D : ISL_DIM_LAYOUT_GEN4_2D);
3661
3662 case GL_TEXTURE_2D:
3663 case GL_TEXTURE_2D_ARRAY:
3664 case GL_TEXTURE_RECTANGLE:
3665 case GL_TEXTURE_2D_MULTISAMPLE:
3666 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3667 case GL_TEXTURE_EXTERNAL_OES:
3668 return ISL_DIM_LAYOUT_GEN4_2D;
3669
3670 case GL_TEXTURE_CUBE_MAP:
3671 case GL_TEXTURE_CUBE_MAP_ARRAY:
3672 return (devinfo->gen == 4 ? ISL_DIM_LAYOUT_GEN4_3D :
3673 ISL_DIM_LAYOUT_GEN4_2D);
3674
3675 case GL_TEXTURE_3D:
3676 return (devinfo->gen >= 9 ?
3677 ISL_DIM_LAYOUT_GEN4_2D : ISL_DIM_LAYOUT_GEN4_3D);
3678 }
3679
3680 unreachable("Invalid texture target");
3681 }
3682
3683 enum isl_tiling
3684 intel_miptree_get_isl_tiling(const struct intel_mipmap_tree *mt)
3685 {
3686 if (mt->format == MESA_FORMAT_S_UINT8) {
3687 return ISL_TILING_W;
3688 } else {
3689 switch (mt->tiling) {
3690 case I915_TILING_NONE:
3691 return ISL_TILING_LINEAR;
3692 case I915_TILING_X:
3693 return ISL_TILING_X;
3694 case I915_TILING_Y:
3695 return ISL_TILING_Y0;
3696 default:
3697 unreachable("Invalid tiling mode");
3698 }
3699 }
3700 }
3701
3702 void
3703 intel_miptree_get_isl_surf(struct brw_context *brw,
3704 const struct intel_mipmap_tree *mt,
3705 struct isl_surf *surf)
3706 {
3707 surf->dim = get_isl_surf_dim(mt->target);
3708 surf->dim_layout = get_isl_dim_layout(&brw->screen->devinfo,
3709 mt->tiling, mt->target,
3710 mt->array_layout);
3711
3712 if (mt->num_samples > 1) {
3713 switch (mt->msaa_layout) {
3714 case INTEL_MSAA_LAYOUT_IMS:
3715 surf->msaa_layout = ISL_MSAA_LAYOUT_INTERLEAVED;
3716 break;
3717 case INTEL_MSAA_LAYOUT_UMS:
3718 case INTEL_MSAA_LAYOUT_CMS:
3719 surf->msaa_layout = ISL_MSAA_LAYOUT_ARRAY;
3720 break;
3721 default:
3722 unreachable("Invalid MSAA layout");
3723 }
3724 } else {
3725 surf->msaa_layout = ISL_MSAA_LAYOUT_NONE;
3726 }
3727
3728 surf->tiling = intel_miptree_get_isl_tiling(mt);
3729
3730 if (mt->format == MESA_FORMAT_S_UINT8) {
3731 /* The ISL definition of row_pitch matches the surface state pitch field
3732 * a bit better than intel_mipmap_tree. In particular, ISL incorporates
3733 * the factor of 2 for W-tiling in row_pitch.
3734 */
3735 surf->row_pitch = 2 * mt->pitch;
3736 } else {
3737 surf->row_pitch = mt->pitch;
3738 }
3739
3740 surf->format = translate_tex_format(brw, mt->format, false);
3741
3742 if (brw->gen >= 9) {
3743 if (surf->dim == ISL_SURF_DIM_1D && surf->tiling == ISL_TILING_LINEAR) {
3744 /* For gen9 1-D surfaces, intel_mipmap_tree has a bogus alignment. */
3745 surf->image_alignment_el = isl_extent3d(64, 1, 1);
3746 } else {
3747 /* On gen9+, intel_mipmap_tree stores the horizontal and vertical
3748 * alignment in terms of surface elements like we want.
3749 */
3750 surf->image_alignment_el = isl_extent3d(mt->halign, mt->valign, 1);
3751 }
3752 } else {
3753 /* On earlier gens it's stored in pixels. */
3754 unsigned bw, bh;
3755 _mesa_get_format_block_size(mt->format, &bw, &bh);
3756 surf->image_alignment_el =
3757 isl_extent3d(mt->halign / bw, mt->valign / bh, 1);
3758 }
3759
3760 surf->logical_level0_px.width = mt->logical_width0;
3761 surf->logical_level0_px.height = mt->logical_height0;
3762 if (surf->dim == ISL_SURF_DIM_3D) {
3763 surf->logical_level0_px.depth = mt->logical_depth0;
3764 surf->logical_level0_px.array_len = 1;
3765 } else {
3766 surf->logical_level0_px.depth = 1;
3767 surf->logical_level0_px.array_len = mt->logical_depth0;
3768 }
3769
3770 surf->phys_level0_sa.width = mt->physical_width0;
3771 surf->phys_level0_sa.height = mt->physical_height0;
3772 if (surf->dim == ISL_SURF_DIM_3D) {
3773 surf->phys_level0_sa.depth = mt->physical_depth0;
3774 surf->phys_level0_sa.array_len = 1;
3775 } else {
3776 surf->phys_level0_sa.depth = 1;
3777 surf->phys_level0_sa.array_len = mt->physical_depth0;
3778 }
3779
3780 surf->levels = mt->last_level - mt->first_level + 1;
3781 surf->samples = MAX2(mt->num_samples, 1);
3782
3783 surf->size = 0; /* TODO */
3784 surf->alignment = 0; /* TODO */
3785
3786 switch (surf->dim_layout) {
3787 case ISL_DIM_LAYOUT_GEN4_2D:
3788 case ISL_DIM_LAYOUT_GEN4_3D:
3789 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
3790 if (brw->gen >= 9) {
3791 surf->array_pitch_el_rows = mt->qpitch;
3792 } else {
3793 unsigned bw, bh;
3794 _mesa_get_format_block_size(mt->format, &bw, &bh);
3795 assert(mt->qpitch % bh == 0);
3796 surf->array_pitch_el_rows = mt->qpitch / bh;
3797 }
3798 break;
3799 case ISL_DIM_LAYOUT_GEN9_1D:
3800 surf->array_pitch_el_rows = 1;
3801 break;
3802 }
3803
3804 switch (mt->array_layout) {
3805 case ALL_LOD_IN_EACH_SLICE:
3806 surf->array_pitch_span = ISL_ARRAY_PITCH_SPAN_FULL;
3807 break;
3808 case ALL_SLICES_AT_EACH_LOD:
3809 case GEN6_HIZ_STENCIL:
3810 surf->array_pitch_span = ISL_ARRAY_PITCH_SPAN_COMPACT;
3811 break;
3812 default:
3813 unreachable("Invalid array layout");
3814 }
3815
3816 GLenum base_format = _mesa_get_format_base_format(mt->format);
3817 switch (base_format) {
3818 case GL_DEPTH_COMPONENT:
3819 surf->usage = ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
3820 break;
3821 case GL_STENCIL_INDEX:
3822 surf->usage = ISL_SURF_USAGE_STENCIL_BIT;
3823 if (brw->gen >= 8)
3824 surf->usage |= ISL_SURF_USAGE_TEXTURE_BIT;
3825 break;
3826 case GL_DEPTH_STENCIL:
3827 /* In this case we only texture from the depth part */
3828 surf->usage = ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_STENCIL_BIT |
3829 ISL_SURF_USAGE_TEXTURE_BIT;
3830 break;
3831 default:
3832 surf->usage = ISL_SURF_USAGE_TEXTURE_BIT;
3833 if (brw->mesa_format_supports_render[mt->format])
3834 surf->usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
3835 break;
3836 }
3837
3838 if (_mesa_is_cube_map_texture(mt->target))
3839 surf->usage |= ISL_SURF_USAGE_CUBE_BIT;
3840 }
3841
3842 enum isl_aux_usage
3843 intel_miptree_get_aux_isl_usage(const struct brw_context *brw,
3844 const struct intel_mipmap_tree *mt)
3845 {
3846 if (mt->hiz_buf)
3847 return ISL_AUX_USAGE_HIZ;
3848
3849 if (!mt->mcs_buf)
3850 return ISL_AUX_USAGE_NONE;
3851
3852 return mt->aux_usage;
3853 }