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