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