i965: Use the new resolve function for several simple cases
[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 GLbitfield mode);
54
55 static void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
56
57 static bool
58 intel_miptree_alloc_mcs(struct brw_context *brw,
59 struct intel_mipmap_tree *mt,
60 GLuint num_samples);
61
62 /**
63 * Determine which MSAA layout should be used by the MSAA surface being
64 * created, based on the chip generation and the surface type.
65 */
66 static enum intel_msaa_layout
67 compute_msaa_layout(struct brw_context *brw, mesa_format format,
68 enum intel_aux_disable aux_disable)
69 {
70 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
71 if (brw->gen < 7)
72 return INTEL_MSAA_LAYOUT_IMS;
73
74 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
75 switch (_mesa_get_format_base_format(format)) {
76 case GL_DEPTH_COMPONENT:
77 case GL_STENCIL_INDEX:
78 case GL_DEPTH_STENCIL:
79 return INTEL_MSAA_LAYOUT_IMS;
80 default:
81 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
82 *
83 * This field must be set to 0 for all SINT MSRTs when all RT channels
84 * are not written
85 *
86 * In practice this means that we have to disable MCS for all signed
87 * integer MSAA buffers. The alternative, to disable MCS only when one
88 * of the render target channels is disabled, is impractical because it
89 * would require converting between CMS and UMS MSAA layouts on the fly,
90 * which is expensive.
91 */
92 if (brw->gen == 7 && _mesa_get_format_datatype(format) == GL_INT) {
93 return INTEL_MSAA_LAYOUT_UMS;
94 } else if (aux_disable & INTEL_AUX_DISABLE_MCS) {
95 /* We can't use the CMS layout because it uses an aux buffer, the MCS
96 * buffer. So fallback to UMS, which is identical to CMS without the
97 * MCS. */
98 return INTEL_MSAA_LAYOUT_UMS;
99 } else {
100 return INTEL_MSAA_LAYOUT_CMS;
101 }
102 }
103 }
104
105 bool
106 intel_tiling_supports_non_msrt_mcs(const struct brw_context *brw,
107 unsigned tiling)
108 {
109 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
110 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
111 *
112 * - Support is limited to tiled render targets.
113 *
114 * Gen9 changes the restriction to Y-tile only.
115 */
116 if (brw->gen >= 9)
117 return tiling == I915_TILING_Y;
118 else if (brw->gen >= 7)
119 return tiling != I915_TILING_NONE;
120 else
121 return false;
122 }
123
124 /**
125 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
126 * can be used. This doesn't (and should not) inspect any of the properties of
127 * the miptree's BO.
128 *
129 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
130 * beneath the "Fast Color Clear" bullet (p326):
131 *
132 * - Support is for non-mip-mapped and non-array surface types only.
133 *
134 * And then later, on p327:
135 *
136 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
137 * 64bpp, and 128bpp.
138 *
139 * From the Skylake documentation, it is made clear that X-tiling is no longer
140 * supported:
141 *
142 * - MCS and Lossless compression is supported for TiledY/TileYs/TileYf
143 * non-MSRTs only.
144 */
145 bool
146 intel_miptree_supports_non_msrt_fast_clear(struct brw_context *brw,
147 const struct intel_mipmap_tree *mt)
148 {
149 /* MCS support does not exist prior to Gen7 */
150 if (brw->gen < 7)
151 return false;
152
153 if (mt->aux_disable & INTEL_AUX_DISABLE_MCS)
154 return false;
155
156 /* This function applies only to non-multisampled render targets. */
157 if (mt->num_samples > 1)
158 return false;
159
160 /* MCS is only supported for color buffers */
161 switch (_mesa_get_format_base_format(mt->format)) {
162 case GL_DEPTH_COMPONENT:
163 case GL_DEPTH_STENCIL:
164 case GL_STENCIL_INDEX:
165 return false;
166 }
167
168 if (mt->cpp != 4 && mt->cpp != 8 && mt->cpp != 16)
169 return false;
170
171 const bool mip_mapped = mt->first_level != 0 || mt->last_level != 0;
172 const bool arrayed = mt->physical_depth0 != 1;
173
174 if (arrayed) {
175 /* Multisample surfaces with the CMS layout are not layered surfaces,
176 * yet still have physical_depth0 > 1. Assert that we don't
177 * accidentally reject a multisampled surface here. We should have
178 * rejected it earlier by explicitly checking the sample count.
179 */
180 assert(mt->num_samples <= 1);
181 }
182
183 /* Handle the hardware restrictions...
184 *
185 * All GENs have the following restriction: "MCS buffer for non-MSRT is
186 * supported only for RT formats 32bpp, 64bpp, and 128bpp."
187 *
188 * From the HSW PRM Volume 7: 3D-Media-GPGPU, page 652: (Color Clear of
189 * Non-MultiSampler Render Target Restrictions) Support is for
190 * non-mip-mapped and non-array surface types only.
191 *
192 * From the BDW PRM Volume 7: 3D-Media-GPGPU, page 649: (Color Clear of
193 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
194 * surfaces are supported with MCS buffer layout with these alignments in
195 * the RT space: Horizontal Alignment = 256 and Vertical Alignment = 128.
196 *
197 * From the SKL PRM Volume 7: 3D-Media-GPGPU, page 632: (Color Clear of
198 * Non-MultiSampler Render Target Restriction). Mip-mapped and arrayed
199 * surfaces are supported with MCS buffer layout with these alignments in
200 * the RT space: Horizontal Alignment = 128 and Vertical Alignment = 64.
201 */
202 if (brw->gen < 8 && (mip_mapped || arrayed))
203 return false;
204
205 /* There's no point in using an MCS buffer if the surface isn't in a
206 * renderable format.
207 */
208 if (!brw->format_supported_as_render_target[mt->format])
209 return false;
210
211 if (brw->gen >= 9) {
212 mesa_format linear_format = _mesa_get_srgb_format_linear(mt->format);
213 const enum isl_format isl_format =
214 brw_isl_format_for_mesa_format(linear_format);
215 return isl_format_supports_ccs_e(&brw->screen->devinfo, isl_format);
216 } else
217 return true;
218 }
219
220 /* On Gen9 support for color buffer compression was extended to single
221 * sampled surfaces. This is a helper considering both auxiliary buffer
222 * type and number of samples telling if the given miptree represents
223 * the new single sampled case - also called lossless compression.
224 */
225 bool
226 intel_miptree_is_lossless_compressed(const struct brw_context *brw,
227 const struct intel_mipmap_tree *mt)
228 {
229 /* Only available from Gen9 onwards. */
230 if (brw->gen < 9)
231 return false;
232
233 /* Compression always requires auxiliary buffer. */
234 if (!mt->mcs_buf)
235 return false;
236
237 /* Single sample compression is represented re-using msaa compression
238 * layout type: "Compressed Multisampled Surfaces".
239 */
240 if (mt->msaa_layout != INTEL_MSAA_LAYOUT_CMS)
241 return false;
242
243 /* And finally distinguish between msaa and single sample case. */
244 return mt->num_samples <= 1;
245 }
246
247 bool
248 intel_miptree_supports_lossless_compressed(struct brw_context *brw,
249 const struct intel_mipmap_tree *mt)
250 {
251 /* For now compression is only enabled for integer formats even though
252 * there exist supported floating point formats also. This is a heuristic
253 * decision based on current public benchmarks. In none of the cases these
254 * formats provided any improvement but a few cases were seen to regress.
255 * Hence these are left to to be enabled in the future when they are known
256 * to improve things.
257 */
258 if (_mesa_get_format_datatype(mt->format) == GL_FLOAT)
259 return false;
260
261 /* Fast clear mechanism and lossless compression go hand in hand. */
262 if (!intel_miptree_supports_non_msrt_fast_clear(brw, mt))
263 return false;
264
265 /* Fast clear can be also used to clear srgb surfaces by using equivalent
266 * linear format. This trick, however, can't be extended to be used with
267 * lossless compression and therefore a check is needed to see if the format
268 * really is linear.
269 */
270 return _mesa_get_srgb_format_linear(mt->format) == mt->format;
271 }
272
273 /**
274 * Determine depth format corresponding to a depth+stencil format,
275 * for separate stencil.
276 */
277 mesa_format
278 intel_depth_format_for_depthstencil_format(mesa_format format) {
279 switch (format) {
280 case MESA_FORMAT_Z24_UNORM_S8_UINT:
281 return MESA_FORMAT_Z24_UNORM_X8_UINT;
282 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
283 return MESA_FORMAT_Z_FLOAT32;
284 default:
285 return format;
286 }
287 }
288
289
290 /**
291 * @param for_bo Indicates that the caller is
292 * intel_miptree_create_for_bo(). If true, then do not create
293 * \c stencil_mt.
294 */
295 static struct intel_mipmap_tree *
296 intel_miptree_create_layout(struct brw_context *brw,
297 GLenum target,
298 mesa_format format,
299 GLuint first_level,
300 GLuint last_level,
301 GLuint width0,
302 GLuint height0,
303 GLuint depth0,
304 GLuint num_samples,
305 uint32_t layout_flags)
306 {
307 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
308 if (!mt)
309 return NULL;
310
311 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__,
312 _mesa_enum_to_string(target),
313 _mesa_get_format_name(format),
314 first_level, last_level, depth0, mt);
315
316 if (target == GL_TEXTURE_1D_ARRAY)
317 assert(height0 == 1);
318
319 mt->target = target;
320 mt->format = format;
321 mt->first_level = first_level;
322 mt->last_level = last_level;
323 mt->logical_width0 = width0;
324 mt->logical_height0 = height0;
325 mt->logical_depth0 = depth0;
326 mt->aux_disable = (layout_flags & MIPTREE_LAYOUT_DISABLE_AUX) != 0 ?
327 INTEL_AUX_DISABLE_ALL : INTEL_AUX_DISABLE_NONE;
328 mt->aux_disable |= INTEL_AUX_DISABLE_CCS;
329 mt->is_scanout = (layout_flags & MIPTREE_LAYOUT_FOR_SCANOUT) != 0;
330 exec_list_make_empty(&mt->hiz_map);
331 exec_list_make_empty(&mt->color_resolve_map);
332 mt->cpp = _mesa_get_format_bytes(format);
333 mt->num_samples = num_samples;
334 mt->compressed = _mesa_is_format_compressed(format);
335 mt->msaa_layout = INTEL_MSAA_LAYOUT_NONE;
336 mt->refcount = 1;
337
338 int depth_multiply = 1;
339 if (num_samples > 1) {
340 /* Adjust width/height/depth for MSAA */
341 mt->msaa_layout = compute_msaa_layout(brw, format, mt->aux_disable);
342 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
343 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
344 * "If the surface is multisampled and it is a depth or stencil
345 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
346 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
347 * proceeding:
348 *
349 * +----------------------------------------------------------------+
350 * | Num Multisamples | W_l = | H_l = |
351 * +----------------------------------------------------------------+
352 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
353 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
354 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
355 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
356 * +----------------------------------------------------------------+
357 * "
358 *
359 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
360 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
361 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
362 *
363 * Another more complicated explanation for these adjustments comes
364 * from the Sandybridge PRM, volume 4, part 1, page 31:
365 *
366 * "Any of the other messages (sample*, LOD, load4) used with a
367 * (4x) multisampled surface will in-effect sample a surface with
368 * double the height and width as that indicated in the surface
369 * state. Each pixel position on the original-sized surface is
370 * replaced with a 2x2 of samples with the following arrangement:
371 *
372 * sample 0 sample 2
373 * sample 1 sample 3"
374 *
375 * Thus, when sampling from a multisampled texture, it behaves as
376 * though the layout in memory for (x,y,sample) is:
377 *
378 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
379 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
380 *
381 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
382 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
383 *
384 * However, the actual layout of multisampled data in memory is:
385 *
386 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
387 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
388 *
389 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
390 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
391 *
392 * This pattern repeats for each 2x2 pixel block.
393 *
394 * As a result, when calculating the size of our 4-sample buffer for
395 * an odd width or height, we have to align before scaling up because
396 * sample 3 is in that bottom right 2x2 block.
397 */
398 switch (num_samples) {
399 case 2:
400 assert(brw->gen >= 8);
401 width0 = ALIGN(width0, 2) * 2;
402 height0 = ALIGN(height0, 2);
403 break;
404 case 4:
405 width0 = ALIGN(width0, 2) * 2;
406 height0 = ALIGN(height0, 2) * 2;
407 break;
408 case 8:
409 width0 = ALIGN(width0, 2) * 4;
410 height0 = ALIGN(height0, 2) * 2;
411 break;
412 case 16:
413 width0 = ALIGN(width0, 2) * 4;
414 height0 = ALIGN(height0, 2) * 4;
415 break;
416 default:
417 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
418 * or 16.
419 */
420 unreachable("not reached");
421 }
422 } else {
423 /* Non-interleaved */
424 depth_multiply = num_samples;
425 depth0 *= depth_multiply;
426 }
427 }
428
429 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
430 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
431 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
432 * used on Gen8 to make it pick a qpitch value which doesn't include space
433 * for the mipmaps. On Gen9 this is not necessary because it will
434 * automatically pick a packed qpitch value whenever mt->first_level ==
435 * mt->last_level.
436 * TODO: can we use it elsewhere?
437 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
438 */
439 if (brw->gen >= 9) {
440 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
441 } else {
442 switch (mt->msaa_layout) {
443 case INTEL_MSAA_LAYOUT_NONE:
444 case INTEL_MSAA_LAYOUT_IMS:
445 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
446 break;
447 case INTEL_MSAA_LAYOUT_UMS:
448 case INTEL_MSAA_LAYOUT_CMS:
449 mt->array_layout = ALL_SLICES_AT_EACH_LOD;
450 break;
451 }
452 }
453
454 if (target == GL_TEXTURE_CUBE_MAP)
455 assert(depth0 == 6 * depth_multiply);
456
457 mt->physical_width0 = width0;
458 mt->physical_height0 = height0;
459 mt->physical_depth0 = depth0;
460
461 if (!(layout_flags & MIPTREE_LAYOUT_FOR_BO) &&
462 _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
463 (brw->must_use_separate_stencil ||
464 (brw->has_separate_stencil &&
465 intel_miptree_wants_hiz_buffer(brw, mt)))) {
466 uint32_t stencil_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
467 if (brw->gen == 6) {
468 stencil_flags |= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL |
469 MIPTREE_LAYOUT_TILING_ANY;
470 }
471
472 mt->stencil_mt = intel_miptree_create(brw,
473 mt->target,
474 MESA_FORMAT_S_UINT8,
475 mt->first_level,
476 mt->last_level,
477 mt->logical_width0,
478 mt->logical_height0,
479 mt->logical_depth0,
480 num_samples,
481 stencil_flags);
482
483 if (!mt->stencil_mt) {
484 intel_miptree_release(&mt);
485 return NULL;
486 }
487 mt->stencil_mt->r8stencil_needs_update = true;
488
489 /* Fix up the Z miptree format for how we're splitting out separate
490 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
491 */
492 mt->format = intel_depth_format_for_depthstencil_format(mt->format);
493 mt->cpp = 4;
494
495 if (format == mt->format) {
496 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
497 _mesa_get_format_name(mt->format));
498 }
499 }
500
501 if (layout_flags & MIPTREE_LAYOUT_GEN6_HIZ_STENCIL)
502 mt->array_layout = GEN6_HIZ_STENCIL;
503
504 /*
505 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
506 * multisampled or have an AUX buffer attached to it.
507 *
508 * GEN | MSRT | AUX_CCS_* or AUX_MCS
509 * -------------------------------------------
510 * 9 | HALIGN_16 | HALIGN_16
511 * 8 | HALIGN_ANY | HALIGN_16
512 * 7 | ? | ?
513 * 6 | ? | ?
514 */
515 if (intel_miptree_supports_non_msrt_fast_clear(brw, mt)) {
516 if (brw->gen >= 9 || (brw->gen == 8 && num_samples <= 1))
517 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
518 } else if (brw->gen >= 9 && num_samples > 1) {
519 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
520 } else {
521 const UNUSED bool is_lossless_compressed_aux =
522 brw->gen >= 9 && num_samples == 1 &&
523 mt->format == MESA_FORMAT_R_UINT32;
524
525 /* For now, nothing else has this requirement */
526 assert(is_lossless_compressed_aux ||
527 (layout_flags & MIPTREE_LAYOUT_FORCE_HALIGN16) == 0);
528 }
529
530 if (!brw_miptree_layout(brw, mt, layout_flags)) {
531 intel_miptree_release(&mt);
532 return NULL;
533 }
534
535 if (mt->aux_disable & INTEL_AUX_DISABLE_MCS)
536 assert(mt->msaa_layout != INTEL_MSAA_LAYOUT_CMS);
537
538 return mt;
539 }
540
541
542 /**
543 * Choose an appropriate uncompressed format for a requested
544 * compressed format, if unsupported.
545 */
546 mesa_format
547 intel_lower_compressed_format(struct brw_context *brw, mesa_format format)
548 {
549 /* No need to lower ETC formats on these platforms,
550 * they are supported natively.
551 */
552 if (brw->gen >= 8 || brw->is_baytrail)
553 return format;
554
555 switch (format) {
556 case MESA_FORMAT_ETC1_RGB8:
557 return MESA_FORMAT_R8G8B8X8_UNORM;
558 case MESA_FORMAT_ETC2_RGB8:
559 return MESA_FORMAT_R8G8B8X8_UNORM;
560 case MESA_FORMAT_ETC2_SRGB8:
561 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
562 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
563 return MESA_FORMAT_B8G8R8A8_SRGB;
564 case MESA_FORMAT_ETC2_RGBA8_EAC:
565 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
566 return MESA_FORMAT_R8G8B8A8_UNORM;
567 case MESA_FORMAT_ETC2_R11_EAC:
568 return MESA_FORMAT_R_UNORM16;
569 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
570 return MESA_FORMAT_R_SNORM16;
571 case MESA_FORMAT_ETC2_RG11_EAC:
572 return MESA_FORMAT_R16G16_UNORM;
573 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
574 return MESA_FORMAT_R16G16_SNORM;
575 default:
576 /* Non ETC1 / ETC2 format */
577 return format;
578 }
579 }
580
581 static struct intel_mipmap_tree *
582 miptree_create(struct brw_context *brw,
583 GLenum target,
584 mesa_format format,
585 GLuint first_level,
586 GLuint last_level,
587 GLuint width0,
588 GLuint height0,
589 GLuint depth0,
590 GLuint num_samples,
591 uint32_t layout_flags)
592 {
593 struct intel_mipmap_tree *mt;
594 mesa_format tex_format = format;
595 mesa_format etc_format = MESA_FORMAT_NONE;
596 uint32_t alloc_flags = 0;
597
598 format = intel_lower_compressed_format(brw, format);
599
600 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
601
602 assert((layout_flags & MIPTREE_LAYOUT_FOR_BO) == 0);
603 mt = intel_miptree_create_layout(brw, target, format,
604 first_level, last_level, width0,
605 height0, depth0, num_samples,
606 layout_flags);
607 if (!mt)
608 return NULL;
609
610 if (mt->tiling == (I915_TILING_Y | I915_TILING_X))
611 mt->tiling = I915_TILING_Y;
612
613 if (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD)
614 alloc_flags |= BO_ALLOC_FOR_RENDER;
615
616 mt->etc_format = etc_format;
617
618 if (format == MESA_FORMAT_S_UINT8) {
619 /* Align to size of W tile, 64x64. */
620 mt->bo = brw_bo_alloc_tiled(brw->bufmgr, "miptree",
621 ALIGN(mt->total_width, 64),
622 ALIGN(mt->total_height, 64),
623 mt->cpp, mt->tiling, &mt->pitch,
624 alloc_flags);
625 } else {
626 mt->bo = brw_bo_alloc_tiled(brw->bufmgr, "miptree",
627 mt->total_width, mt->total_height,
628 mt->cpp, mt->tiling, &mt->pitch,
629 alloc_flags);
630 }
631
632 if (layout_flags & MIPTREE_LAYOUT_FOR_SCANOUT)
633 mt->bo->cache_coherent = false;
634
635 return mt;
636 }
637
638 struct intel_mipmap_tree *
639 intel_miptree_create(struct brw_context *brw,
640 GLenum target,
641 mesa_format format,
642 GLuint first_level,
643 GLuint last_level,
644 GLuint width0,
645 GLuint height0,
646 GLuint depth0,
647 GLuint num_samples,
648 uint32_t layout_flags)
649 {
650 struct intel_mipmap_tree *mt = miptree_create(
651 brw, target, format,
652 first_level, last_level,
653 width0, height0, depth0, num_samples,
654 layout_flags);
655
656 /* If the BO is too large to fit in the aperture, we need to use the
657 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
658 * handle Y-tiling, so we need to fall back to X.
659 */
660 if (brw->gen < 6 && mt->bo->size >= brw->max_gtt_map_object_size &&
661 mt->tiling == I915_TILING_Y) {
662 const uint32_t alloc_flags =
663 (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD) ?
664 BO_ALLOC_FOR_RENDER : 0;
665 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
666 mt->total_width, mt->total_height);
667
668 mt->tiling = I915_TILING_X;
669 brw_bo_unreference(mt->bo);
670 mt->bo = brw_bo_alloc_tiled(brw->bufmgr, "miptree",
671 mt->total_width, mt->total_height, mt->cpp,
672 mt->tiling, &mt->pitch, alloc_flags);
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 void *map = brw_bo_map(brw, mt->mcs_buf->bo, MAP_WRITE);
1390 if (unlikely(map == NULL)) {
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 = map;
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
1548 /* ISL has stricter set of alignment rules then the drm allocator.
1549 * Therefore one can pass the ISL dimensions in terms of bytes instead of
1550 * trying to recalculate based on different format block sizes.
1551 */
1552 buf->bo = brw_bo_alloc_tiled(brw->bufmgr, "ccs-miptree",
1553 buf->pitch, buf->size / buf->pitch,
1554 1, I915_TILING_Y, &buf->pitch, alloc_flags);
1555 if (!buf->bo) {
1556 free(buf);
1557 return false;
1558 }
1559
1560 mt->mcs_buf = buf;
1561
1562 /* From Gen9 onwards single-sampled (non-msrt) auxiliary buffers are
1563 * used for lossless compression which requires similar initialisation
1564 * as multi-sample compression.
1565 */
1566 if (is_lossless_compressed) {
1567 /* Hardware sets the auxiliary buffer to all zeroes when it does full
1568 * resolve. Initialize it accordingly in case the first renderer is
1569 * cpu (or other none compression aware party).
1570 *
1571 * This is also explicitly stated in the spec (MCS Buffer for Render
1572 * Target(s)):
1573 * "If Software wants to enable Color Compression without Fast clear,
1574 * Software needs to initialize MCS with zeros."
1575 */
1576 intel_miptree_init_mcs(brw, mt, 0);
1577 mt->msaa_layout = INTEL_MSAA_LAYOUT_CMS;
1578 }
1579
1580 return true;
1581 }
1582
1583 /**
1584 * Helper for intel_miptree_alloc_hiz() that sets
1585 * \c mt->level[level].has_hiz. Return true if and only if
1586 * \c has_hiz was set.
1587 */
1588 static bool
1589 intel_miptree_level_enable_hiz(struct brw_context *brw,
1590 struct intel_mipmap_tree *mt,
1591 uint32_t level)
1592 {
1593 assert(mt->hiz_buf);
1594
1595 if (brw->gen >= 8 || brw->is_haswell) {
1596 uint32_t width = minify(mt->physical_width0, level);
1597 uint32_t height = minify(mt->physical_height0, level);
1598
1599 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1600 * and the height is 4 aligned. This allows our HiZ support
1601 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1602 * we can grow the width & height to allow the HiZ op to
1603 * force the proper size alignments.
1604 */
1605 if (level > 0 && ((width & 7) || (height & 3))) {
1606 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1607 return false;
1608 }
1609 }
1610
1611 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1612 mt->level[level].has_hiz = true;
1613 return true;
1614 }
1615
1616
1617 /**
1618 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1619 * buffer dimensions and allocates a bo for the hiz buffer.
1620 */
1621 static struct intel_miptree_hiz_buffer *
1622 intel_gen7_hiz_buf_create(struct brw_context *brw,
1623 struct intel_mipmap_tree *mt)
1624 {
1625 unsigned z_width = mt->logical_width0;
1626 unsigned z_height = mt->logical_height0;
1627 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1628 unsigned hz_width, hz_height;
1629 struct intel_miptree_hiz_buffer *buf = calloc(sizeof(*buf), 1);
1630
1631 if (!buf)
1632 return NULL;
1633
1634 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1635 * adjustments required for Z_Height and Z_Width based on multisampling.
1636 */
1637 switch (mt->num_samples) {
1638 case 0:
1639 case 1:
1640 break;
1641 case 2:
1642 case 4:
1643 z_width *= 2;
1644 z_height *= 2;
1645 break;
1646 case 8:
1647 z_width *= 4;
1648 z_height *= 2;
1649 break;
1650 default:
1651 unreachable("unsupported sample count");
1652 }
1653
1654 const unsigned vertical_align = 8; /* 'j' in the docs */
1655 const unsigned H0 = z_height;
1656 const unsigned h0 = ALIGN(H0, vertical_align);
1657 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1658 const unsigned Z0 = z_depth;
1659
1660 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1661 hz_width = ALIGN(z_width, 16);
1662
1663 if (mt->target == GL_TEXTURE_3D) {
1664 unsigned H_i = H0;
1665 unsigned Z_i = Z0;
1666 hz_height = 0;
1667 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1668 unsigned h_i = ALIGN(H_i, vertical_align);
1669 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1670 hz_height += h_i * Z_i;
1671 H_i = minify(H_i, 1);
1672 Z_i = minify(Z_i, 1);
1673 }
1674 /* HZ_Height =
1675 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1676 */
1677 hz_height = DIV_ROUND_UP(hz_height, 2);
1678 } else {
1679 const unsigned hz_qpitch = h0 + h1 + (12 * vertical_align);
1680 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1681 hz_height = DIV_ROUND_UP(hz_qpitch * Z0, 2 * 8) * 8;
1682 }
1683
1684 buf->aux_base.bo = brw_bo_alloc_tiled(brw->bufmgr, "hiz",
1685 hz_width, hz_height, 1,
1686 I915_TILING_Y, &buf->aux_base.pitch,
1687 BO_ALLOC_FOR_RENDER);
1688 if (!buf->aux_base.bo) {
1689 free(buf);
1690 return NULL;
1691 }
1692
1693 buf->aux_base.size = hz_width * hz_height;
1694
1695 return buf;
1696 }
1697
1698
1699 /**
1700 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1701 * buffer dimensions and allocates a bo for the hiz buffer.
1702 */
1703 static struct intel_miptree_hiz_buffer *
1704 intel_gen8_hiz_buf_create(struct brw_context *brw,
1705 struct intel_mipmap_tree *mt)
1706 {
1707 unsigned z_width = mt->logical_width0;
1708 unsigned z_height = mt->logical_height0;
1709 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1710 unsigned hz_width, hz_height;
1711 struct intel_miptree_hiz_buffer *buf = calloc(sizeof(*buf), 1);
1712
1713 if (!buf)
1714 return NULL;
1715
1716 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1717 * adjustments required for Z_Height and Z_Width based on multisampling.
1718 */
1719 if (brw->gen < 9) {
1720 switch (mt->num_samples) {
1721 case 0:
1722 case 1:
1723 break;
1724 case 2:
1725 case 4:
1726 z_width *= 2;
1727 z_height *= 2;
1728 break;
1729 case 8:
1730 z_width *= 4;
1731 z_height *= 2;
1732 break;
1733 default:
1734 unreachable("unsupported sample count");
1735 }
1736 }
1737
1738 const unsigned vertical_align = 8; /* 'j' in the docs */
1739 const unsigned H0 = z_height;
1740 const unsigned h0 = ALIGN(H0, vertical_align);
1741 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1742 const unsigned Z0 = z_depth;
1743
1744 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1745 hz_width = ALIGN(z_width, 16);
1746
1747 unsigned H_i = H0;
1748 unsigned Z_i = Z0;
1749 unsigned sum_h_i = 0;
1750 unsigned hz_height_3d_sum = 0;
1751 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1752 unsigned i = level - mt->first_level;
1753 unsigned h_i = ALIGN(H_i, vertical_align);
1754 /* sum(i=2 to m; h_i) */
1755 if (i >= 2) {
1756 sum_h_i += h_i;
1757 }
1758 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1759 hz_height_3d_sum += h_i * Z_i;
1760 H_i = minify(H_i, 1);
1761 Z_i = minify(Z_i, 1);
1762 }
1763 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1764 buf->aux_base.qpitch = h0 + MAX2(h1, sum_h_i);
1765
1766 if (mt->target == GL_TEXTURE_3D) {
1767 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1768 hz_height = DIV_ROUND_UP(hz_height_3d_sum, 2);
1769 } else {
1770 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1771 hz_height = DIV_ROUND_UP(buf->aux_base.qpitch, 2 * 8) * 8 * Z0;
1772 }
1773
1774 buf->aux_base.bo = brw_bo_alloc_tiled(brw->bufmgr, "hiz",
1775 hz_width, hz_height, 1,
1776 I915_TILING_Y, &buf->aux_base.pitch,
1777 BO_ALLOC_FOR_RENDER);
1778 if (!buf->aux_base.bo) {
1779 free(buf);
1780 return NULL;
1781 }
1782
1783 buf->aux_base.size = hz_width * hz_height;
1784
1785 return buf;
1786 }
1787
1788
1789 static struct intel_miptree_hiz_buffer *
1790 intel_hiz_miptree_buf_create(struct brw_context *brw,
1791 struct intel_mipmap_tree *mt)
1792 {
1793 struct intel_miptree_hiz_buffer *buf = calloc(sizeof(*buf), 1);
1794 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
1795
1796 if (brw->gen == 6)
1797 layout_flags |= MIPTREE_LAYOUT_GEN6_HIZ_STENCIL;
1798
1799 if (!buf)
1800 return NULL;
1801
1802 layout_flags |= MIPTREE_LAYOUT_TILING_ANY;
1803 buf->mt = intel_miptree_create(brw,
1804 mt->target,
1805 mt->format,
1806 mt->first_level,
1807 mt->last_level,
1808 mt->logical_width0,
1809 mt->logical_height0,
1810 mt->logical_depth0,
1811 mt->num_samples,
1812 layout_flags);
1813 if (!buf->mt) {
1814 free(buf);
1815 return NULL;
1816 }
1817
1818 buf->aux_base.bo = buf->mt->bo;
1819 buf->aux_base.size = buf->mt->total_height * buf->mt->pitch;
1820 buf->aux_base.pitch = buf->mt->pitch;
1821 buf->aux_base.qpitch = buf->mt->qpitch * 2;
1822
1823 return buf;
1824 }
1825
1826 bool
1827 intel_miptree_wants_hiz_buffer(struct brw_context *brw,
1828 struct intel_mipmap_tree *mt)
1829 {
1830 if (!brw->has_hiz)
1831 return false;
1832
1833 if (mt->hiz_buf != NULL)
1834 return false;
1835
1836 if (mt->aux_disable & INTEL_AUX_DISABLE_HIZ)
1837 return false;
1838
1839 switch (mt->format) {
1840 case MESA_FORMAT_Z_FLOAT32:
1841 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1842 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1843 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1844 case MESA_FORMAT_Z_UNORM16:
1845 return true;
1846 default:
1847 return false;
1848 }
1849 }
1850
1851 bool
1852 intel_miptree_alloc_hiz(struct brw_context *brw,
1853 struct intel_mipmap_tree *mt)
1854 {
1855 assert(mt->hiz_buf == NULL);
1856 assert((mt->aux_disable & INTEL_AUX_DISABLE_HIZ) == 0);
1857
1858 if (brw->gen == 7) {
1859 mt->hiz_buf = intel_gen7_hiz_buf_create(brw, mt);
1860 } else if (brw->gen >= 8) {
1861 mt->hiz_buf = intel_gen8_hiz_buf_create(brw, mt);
1862 } else {
1863 mt->hiz_buf = intel_hiz_miptree_buf_create(brw, mt);
1864 }
1865
1866 if (!mt->hiz_buf)
1867 return false;
1868
1869 /* Mark that all slices need a HiZ resolve. */
1870 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1871 if (!intel_miptree_level_enable_hiz(brw, mt, level))
1872 continue;
1873
1874 for (unsigned layer = 0; layer < mt->level[level].depth; ++layer) {
1875 struct intel_resolve_map *m = malloc(sizeof(struct intel_resolve_map));
1876 exec_node_init(&m->link);
1877 m->level = level;
1878 m->layer = layer;
1879 m->need = BLORP_HIZ_OP_HIZ_RESOLVE;
1880
1881 exec_list_push_tail(&mt->hiz_map, &m->link);
1882 }
1883 }
1884
1885 return true;
1886 }
1887
1888 /**
1889 * Can the miptree sample using the hiz buffer?
1890 */
1891 bool
1892 intel_miptree_sample_with_hiz(struct brw_context *brw,
1893 struct intel_mipmap_tree *mt)
1894 {
1895 /* It's unclear how well supported sampling from the hiz buffer is on GEN8,
1896 * so keep things conservative for now and never enable it unless we're SKL+.
1897 */
1898 if (brw->gen < 9) {
1899 return false;
1900 }
1901
1902 if (!mt->hiz_buf) {
1903 return false;
1904 }
1905
1906 /* It seems the hardware won't fallback to the depth buffer if some of the
1907 * mipmap levels aren't available in the HiZ buffer. So we need all levels
1908 * of the texture to be HiZ enabled.
1909 */
1910 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1911 if (!intel_miptree_level_has_hiz(mt, level))
1912 return false;
1913 }
1914
1915 /* If compressed multisampling is enabled, then we use it for the auxiliary
1916 * buffer instead.
1917 *
1918 * From the BDW PRM (Volume 2d: Command Reference: Structures
1919 * RENDER_SURFACE_STATE.AuxiliarySurfaceMode):
1920 *
1921 * "If this field is set to AUX_HIZ, Number of Multisamples must be
1922 * MULTISAMPLECOUNT_1, and Surface Type cannot be SURFTYPE_3D.
1923 *
1924 * There is no such blurb for 1D textures, but there is sufficient evidence
1925 * that this is broken on SKL+.
1926 */
1927 return (mt->num_samples <= 1 &&
1928 mt->target != GL_TEXTURE_3D &&
1929 mt->target != GL_TEXTURE_1D /* gen9+ restriction */);
1930 }
1931
1932 /**
1933 * Does the miptree slice have hiz enabled?
1934 */
1935 bool
1936 intel_miptree_level_has_hiz(struct intel_mipmap_tree *mt, uint32_t level)
1937 {
1938 intel_miptree_check_level_layer(mt, level, 0);
1939 return mt->level[level].has_hiz;
1940 }
1941
1942 void
1943 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1944 uint32_t level,
1945 uint32_t layer)
1946 {
1947 if (!intel_miptree_level_has_hiz(mt, level))
1948 return;
1949
1950 intel_resolve_map_set(&mt->hiz_map,
1951 level, layer, BLORP_HIZ_OP_HIZ_RESOLVE);
1952 }
1953
1954
1955 void
1956 intel_miptree_slice_set_needs_depth_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_DEPTH_RESOLVE);
1965 }
1966
1967 void
1968 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
1969 uint32_t level)
1970 {
1971 uint32_t layer;
1972 uint32_t end_layer = mt->level[level].depth;
1973
1974 for (layer = 0; layer < end_layer; layer++) {
1975 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
1976 }
1977 }
1978
1979 static bool
1980 intel_miptree_depth_hiz_resolve(struct brw_context *brw,
1981 struct intel_mipmap_tree *mt,
1982 uint32_t start_level, uint32_t num_levels,
1983 uint32_t start_layer, uint32_t num_layers,
1984 enum blorp_hiz_op need)
1985 {
1986 bool did_resolve = false;
1987
1988 foreach_list_typed_safe(struct intel_resolve_map, map, link, &mt->hiz_map) {
1989 if (map->level < start_level ||
1990 map->level >= (start_level + num_levels) ||
1991 map->layer < start_layer ||
1992 map->layer >= (start_layer + num_layers))
1993 continue;
1994
1995 if (map->need != need)
1996 continue;
1997
1998 intel_hiz_exec(brw, mt, map->level, map->layer, 1, need);
1999 intel_resolve_map_remove(map);
2000 did_resolve = true;
2001 }
2002
2003 return did_resolve;
2004 }
2005
2006 bool
2007 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
2008 struct intel_mipmap_tree *mt,
2009 uint32_t level,
2010 uint32_t layer)
2011 {
2012 return intel_miptree_depth_hiz_resolve(brw, mt, level, 1, layer, 1,
2013 BLORP_HIZ_OP_HIZ_RESOLVE);
2014 }
2015
2016 bool
2017 intel_miptree_slice_resolve_depth(struct brw_context *brw,
2018 struct intel_mipmap_tree *mt,
2019 uint32_t level,
2020 uint32_t layer)
2021 {
2022 return intel_miptree_depth_hiz_resolve(brw, mt, level, 1, layer, 1,
2023 BLORP_HIZ_OP_DEPTH_RESOLVE);
2024 }
2025
2026 bool
2027 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
2028 struct intel_mipmap_tree *mt)
2029 {
2030 return intel_miptree_depth_hiz_resolve(brw, mt,
2031 0, UINT32_MAX, 0, UINT32_MAX,
2032 BLORP_HIZ_OP_HIZ_RESOLVE);
2033 }
2034
2035 bool
2036 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
2037 struct intel_mipmap_tree *mt)
2038 {
2039 return intel_miptree_depth_hiz_resolve(brw, mt,
2040 0, UINT32_MAX, 0, UINT32_MAX,
2041 BLORP_HIZ_OP_DEPTH_RESOLVE);
2042 }
2043
2044 enum intel_fast_clear_state
2045 intel_miptree_get_fast_clear_state(const struct intel_mipmap_tree *mt,
2046 unsigned level, unsigned layer)
2047 {
2048 intel_miptree_check_level_layer(mt, level, layer);
2049
2050 const struct intel_resolve_map *item =
2051 intel_resolve_map_const_get(&mt->color_resolve_map, level, layer);
2052
2053 if (!item)
2054 return INTEL_FAST_CLEAR_STATE_RESOLVED;
2055
2056 return item->fast_clear_state;
2057 }
2058
2059 static void
2060 intel_miptree_check_color_resolve(const struct brw_context *brw,
2061 const struct intel_mipmap_tree *mt,
2062 unsigned level, unsigned layer)
2063 {
2064
2065 if ((mt->aux_disable & INTEL_AUX_DISABLE_CCS) || !mt->mcs_buf)
2066 return;
2067
2068 /* Fast color clear is supported for mipmapped surfaces only on Gen8+. */
2069 assert(brw->gen >= 8 ||
2070 (level == 0 && mt->first_level == 0 && mt->last_level == 0));
2071
2072 /* Compression of arrayed msaa surfaces is supported. */
2073 if (mt->num_samples > 1)
2074 return;
2075
2076 /* Fast color clear is supported for non-msaa arrays only on Gen8+. */
2077 assert(brw->gen >= 8 || (layer == 0 && mt->logical_depth0 == 1));
2078
2079 (void)level;
2080 (void)layer;
2081 }
2082
2083 void
2084 intel_miptree_set_fast_clear_state(const struct brw_context *brw,
2085 struct intel_mipmap_tree *mt,
2086 unsigned level,
2087 unsigned first_layer,
2088 unsigned num_layers,
2089 enum intel_fast_clear_state new_state)
2090 {
2091 /* Setting the state to resolved means removing the item from the list
2092 * altogether.
2093 */
2094 assert(new_state != INTEL_FAST_CLEAR_STATE_RESOLVED);
2095
2096 intel_miptree_check_color_resolve(brw, mt, level, first_layer);
2097
2098 assert(first_layer + num_layers <= mt->physical_depth0);
2099
2100 for (unsigned i = 0; i < num_layers; i++)
2101 intel_resolve_map_set(&mt->color_resolve_map, level,
2102 first_layer + i, new_state);
2103 }
2104
2105 bool
2106 intel_miptree_has_color_unresolved(const struct intel_mipmap_tree *mt,
2107 unsigned start_level, unsigned num_levels,
2108 unsigned start_layer, unsigned num_layers)
2109 {
2110 return intel_resolve_map_find_any(&mt->color_resolve_map,
2111 start_level, num_levels,
2112 start_layer, num_layers) != NULL;
2113 }
2114
2115 void
2116 intel_miptree_used_for_rendering(const struct brw_context *brw,
2117 struct intel_mipmap_tree *mt, unsigned level,
2118 unsigned start_layer, unsigned num_layers)
2119 {
2120 const bool is_lossless_compressed =
2121 intel_miptree_is_lossless_compressed(brw, mt);
2122
2123 for (unsigned i = 0; i < num_layers; ++i) {
2124 const enum intel_fast_clear_state fast_clear_state =
2125 intel_miptree_get_fast_clear_state(mt, level, start_layer + i);
2126
2127 /* If the buffer was previously in fast clear state, change it to
2128 * unresolved state, since it won't be guaranteed to be clear after
2129 * rendering occurs.
2130 */
2131 if (is_lossless_compressed ||
2132 fast_clear_state == INTEL_FAST_CLEAR_STATE_CLEAR) {
2133 intel_miptree_set_fast_clear_state(
2134 brw, mt, level, start_layer + i, 1,
2135 INTEL_FAST_CLEAR_STATE_UNRESOLVED);
2136 }
2137 }
2138 }
2139
2140 static bool
2141 intel_miptree_needs_color_resolve(const struct brw_context *brw,
2142 const struct intel_mipmap_tree *mt,
2143 int flags)
2144 {
2145 if (mt->aux_disable & INTEL_AUX_DISABLE_CCS)
2146 return false;
2147
2148 const bool is_lossless_compressed =
2149 intel_miptree_is_lossless_compressed(brw, mt);
2150
2151 /* From gen9 onwards there is new compression scheme for single sampled
2152 * surfaces called "lossless compressed". These don't need to be always
2153 * resolved.
2154 */
2155 if ((flags & INTEL_MIPTREE_IGNORE_CCS_E) && is_lossless_compressed)
2156 return false;
2157
2158 /* Fast color clear resolves only make sense for non-MSAA buffers. */
2159 if (mt->msaa_layout != INTEL_MSAA_LAYOUT_NONE && !is_lossless_compressed)
2160 return false;
2161
2162 return true;
2163 }
2164
2165 bool
2166 intel_miptree_resolve_color(struct brw_context *brw,
2167 struct intel_mipmap_tree *mt,
2168 uint32_t start_level, uint32_t num_levels,
2169 uint32_t start_layer, uint32_t num_layers,
2170 int flags)
2171 {
2172 intel_miptree_check_color_resolve(brw, mt, start_level, start_layer);
2173
2174 if (!intel_miptree_needs_color_resolve(brw, mt, flags))
2175 return false;
2176
2177 enum blorp_fast_clear_op resolve_op;
2178 if (brw->gen >= 9) {
2179 if (intel_miptree_is_lossless_compressed(brw, mt)) {
2180 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2181 } else {
2182 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_PARTIAL;
2183 }
2184 } else {
2185 /* Broadwell and earlier do not have a partial resolve */
2186 assert(!intel_miptree_is_lossless_compressed(brw, mt));
2187 resolve_op = BLORP_FAST_CLEAR_OP_RESOLVE_FULL;
2188 }
2189
2190 bool resolved = false;
2191 foreach_list_typed_safe(struct intel_resolve_map, map, link,
2192 &mt->color_resolve_map) {
2193 if (map->level < start_level ||
2194 map->level >= (start_level + num_levels) ||
2195 map->layer < start_layer ||
2196 map->layer >= (start_layer + num_layers))
2197 continue;
2198
2199 /* Arrayed and mip-mapped fast clear is only supported for gen8+. */
2200 assert(brw->gen >= 8 || (map->level == 0 && map->layer == 0));
2201
2202 intel_miptree_check_level_layer(mt, map->level, map->layer);
2203
2204 assert(map->fast_clear_state != INTEL_FAST_CLEAR_STATE_RESOLVED);
2205
2206 brw_blorp_resolve_color(brw, mt, map->level, map->layer, resolve_op);
2207 intel_resolve_map_remove(map);
2208 resolved = true;
2209 }
2210
2211 return resolved;
2212 }
2213
2214 void
2215 intel_miptree_all_slices_resolve_color(struct brw_context *brw,
2216 struct intel_mipmap_tree *mt,
2217 int flags)
2218 {
2219
2220 intel_miptree_resolve_color(brw, mt, 0, UINT32_MAX, 0, UINT32_MAX, flags);
2221 }
2222
2223 static inline uint32_t
2224 miptree_level_range_length(const struct intel_mipmap_tree *mt,
2225 uint32_t start_level, uint32_t num_levels)
2226 {
2227 assert(start_level >= mt->first_level);
2228 assert(start_level <= mt->last_level);
2229
2230 if (num_levels == INTEL_REMAINING_LAYERS)
2231 num_levels = mt->last_level - start_level + 1;
2232 /* Check for overflow */
2233 assert(start_level + num_levels >= start_level);
2234 assert(start_level + num_levels <= mt->last_level + 1);
2235
2236 return num_levels;
2237 }
2238
2239 static inline uint32_t
2240 miptree_layer_range_length(const struct intel_mipmap_tree *mt, uint32_t level,
2241 uint32_t start_layer, uint32_t num_layers)
2242 {
2243 assert(level <= mt->last_level);
2244 uint32_t total_num_layers = mt->level[level].depth;
2245
2246 assert(start_layer < total_num_layers);
2247 if (num_layers == INTEL_REMAINING_LAYERS)
2248 num_layers = total_num_layers - start_layer;
2249 /* Check for overflow */
2250 assert(start_layer + num_layers >= start_layer);
2251 assert(start_layer + num_layers <= total_num_layers);
2252
2253 return num_layers;
2254 }
2255
2256 void
2257 intel_miptree_prepare_access(struct brw_context *brw,
2258 struct intel_mipmap_tree *mt,
2259 uint32_t start_level, uint32_t num_levels,
2260 uint32_t start_layer, uint32_t num_layers,
2261 bool aux_supported, bool fast_clear_supported)
2262 {
2263 num_levels = miptree_level_range_length(mt, start_level, num_levels);
2264
2265 if (_mesa_is_format_color_format(mt->format)) {
2266 if (!mt->mcs_buf)
2267 return;
2268
2269 if (mt->num_samples > 1) {
2270 /* Nothing to do for MSAA */
2271 } else {
2272 /* TODO: This is fairly terrible. We can do better. */
2273 if (!aux_supported || !fast_clear_supported) {
2274 intel_miptree_resolve_color(brw, mt, start_level, num_levels,
2275 start_layer, num_layers, 0);
2276 }
2277 }
2278 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2279 /* Nothing to do for stencil */
2280 } else {
2281 if (!mt->hiz_buf)
2282 return;
2283
2284 if (aux_supported) {
2285 assert(fast_clear_supported);
2286 intel_miptree_depth_hiz_resolve(brw, mt, start_level, num_levels,
2287 start_layer, num_layers,
2288 BLORP_HIZ_OP_HIZ_RESOLVE);
2289 } else {
2290 assert(!fast_clear_supported);
2291 intel_miptree_depth_hiz_resolve(brw, mt, start_level, num_levels,
2292 start_layer, num_layers,
2293 BLORP_HIZ_OP_DEPTH_RESOLVE);
2294 }
2295 }
2296 }
2297
2298 void
2299 intel_miptree_finish_write(struct brw_context *brw,
2300 struct intel_mipmap_tree *mt, uint32_t level,
2301 uint32_t start_layer, uint32_t num_layers,
2302 bool written_with_aux)
2303 {
2304 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2305
2306 if (_mesa_is_format_color_format(mt->format)) {
2307 if (mt->num_samples > 1) {
2308 /* Nothing to do for MSAA */
2309 } else {
2310 if (written_with_aux) {
2311 intel_miptree_used_for_rendering(brw, mt, level,
2312 start_layer, num_layers);
2313 }
2314 }
2315 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2316 /* Nothing to do for stencil */
2317 } else {
2318 if (written_with_aux) {
2319 for (unsigned a = 0; a < num_layers; a++) {
2320 intel_miptree_check_level_layer(mt, level, start_layer);
2321 intel_miptree_slice_set_needs_depth_resolve(mt, level,
2322 start_layer + a);
2323 }
2324 } else {
2325 for (unsigned a = 0; a < num_layers; a++) {
2326 intel_miptree_check_level_layer(mt, level, start_layer);
2327 intel_miptree_slice_set_needs_hiz_resolve(mt, level,
2328 start_layer + a);
2329 }
2330 }
2331 }
2332 }
2333
2334 enum isl_aux_state
2335 intel_miptree_get_aux_state(const struct intel_mipmap_tree *mt,
2336 uint32_t level, uint32_t layer)
2337 {
2338 if (_mesa_is_format_color_format(mt->format)) {
2339 assert(mt->mcs_buf != NULL);
2340 if (mt->num_samples > 1) {
2341 return ISL_AUX_STATE_COMPRESSED_CLEAR;
2342 } else {
2343 switch (intel_miptree_get_fast_clear_state(mt, level, layer)) {
2344 case INTEL_FAST_CLEAR_STATE_RESOLVED:
2345 return ISL_AUX_STATE_RESOLVED;
2346 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
2347 return ISL_AUX_STATE_COMPRESSED_CLEAR;
2348 case INTEL_FAST_CLEAR_STATE_CLEAR:
2349 return ISL_AUX_STATE_CLEAR;
2350 default:
2351 unreachable("Invalid fast clear state");
2352 }
2353 }
2354 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2355 unreachable("Cannot get aux state for stencil");
2356 } else {
2357 assert(mt->hiz_buf != NULL);
2358 const struct intel_resolve_map *map =
2359 intel_resolve_map_const_get(&mt->hiz_map, level, layer);
2360 if (!map)
2361 return ISL_AUX_STATE_RESOLVED;
2362 switch (map->need) {
2363 case BLORP_HIZ_OP_DEPTH_RESOLVE:
2364 return ISL_AUX_STATE_COMPRESSED_CLEAR;
2365 case BLORP_HIZ_OP_HIZ_RESOLVE:
2366 return ISL_AUX_STATE_AUX_INVALID;
2367 default:
2368 unreachable("Invalid hiz op");
2369 }
2370 }
2371 }
2372
2373 void
2374 intel_miptree_set_aux_state(struct brw_context *brw,
2375 struct intel_mipmap_tree *mt, uint32_t level,
2376 uint32_t start_layer, uint32_t num_layers,
2377 enum isl_aux_state aux_state)
2378 {
2379 num_layers = miptree_layer_range_length(mt, level, start_layer, num_layers);
2380
2381 /* Right now, this only applies to clears. */
2382 assert(aux_state == ISL_AUX_STATE_CLEAR);
2383
2384 if (_mesa_is_format_color_format(mt->format)) {
2385 if (mt->num_samples > 1)
2386 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS);
2387
2388 assert(level == 0 && start_layer == 0 && num_layers == 1);
2389 intel_miptree_set_fast_clear_state(brw, mt, 0, 0, 1,
2390 INTEL_FAST_CLEAR_STATE_CLEAR);
2391 } else if (mt->format == MESA_FORMAT_S_UINT8) {
2392 assert(!"Cannot set aux state for stencil");
2393 } else {
2394 for (unsigned a = 0; a < num_layers; a++) {
2395 intel_miptree_check_level_layer(mt, level, start_layer);
2396 intel_miptree_slice_set_needs_depth_resolve(mt, level,
2397 start_layer + a);
2398 }
2399 }
2400 }
2401
2402 /**
2403 * Make it possible to share the BO backing the given miptree with another
2404 * process or another miptree.
2405 *
2406 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2407 * then discard the MCS buffer, if present. We also set the no_ccs flag to
2408 * ensure that no MCS buffer gets allocated in the future.
2409 *
2410 * HiZ is similarly unsafe with shared buffers.
2411 */
2412 void
2413 intel_miptree_make_shareable(struct brw_context *brw,
2414 struct intel_mipmap_tree *mt)
2415 {
2416 /* MCS buffers are also used for multisample buffers, but we can't resolve
2417 * away a multisample MCS buffer because it's an integral part of how the
2418 * pixel data is stored. Fortunately this code path should never be
2419 * reached for multisample buffers.
2420 */
2421 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE || mt->num_samples <= 1);
2422
2423 intel_miptree_prepare_access(brw, mt, 0, INTEL_REMAINING_LEVELS,
2424 0, INTEL_REMAINING_LAYERS, false, false);
2425
2426 if (mt->mcs_buf) {
2427 mt->aux_disable |= (INTEL_AUX_DISABLE_CCS | INTEL_AUX_DISABLE_MCS);
2428 brw_bo_unreference(mt->mcs_buf->bo);
2429 free(mt->mcs_buf);
2430 mt->mcs_buf = NULL;
2431
2432 /* Any pending MCS/CCS operations are no longer needed. Trying to
2433 * execute any will likely crash due to the missing aux buffer. So let's
2434 * delete all pending ops.
2435 */
2436 exec_list_make_empty(&mt->color_resolve_map);
2437 }
2438
2439 if (mt->hiz_buf) {
2440 mt->aux_disable |= INTEL_AUX_DISABLE_HIZ;
2441 intel_miptree_hiz_buffer_free(mt->hiz_buf);
2442 mt->hiz_buf = NULL;
2443
2444 for (uint32_t l = mt->first_level; l <= mt->last_level; ++l) {
2445 mt->level[l].has_hiz = false;
2446 }
2447
2448 /* Any pending HiZ operations are no longer needed. Trying to execute
2449 * any will likely crash due to the missing aux buffer. So let's delete
2450 * all pending ops.
2451 */
2452 exec_list_make_empty(&mt->hiz_map);
2453 }
2454 }
2455
2456
2457 /**
2458 * \brief Get pointer offset into stencil buffer.
2459 *
2460 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2461 * must decode the tile's layout in software.
2462 *
2463 * See
2464 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2465 * Format.
2466 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2467 *
2468 * Even though the returned offset is always positive, the return type is
2469 * signed due to
2470 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2471 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2472 */
2473 static intptr_t
2474 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2475 {
2476 uint32_t tile_size = 4096;
2477 uint32_t tile_width = 64;
2478 uint32_t tile_height = 64;
2479 uint32_t row_size = 64 * stride;
2480
2481 uint32_t tile_x = x / tile_width;
2482 uint32_t tile_y = y / tile_height;
2483
2484 /* The byte's address relative to the tile's base addres. */
2485 uint32_t byte_x = x % tile_width;
2486 uint32_t byte_y = y % tile_height;
2487
2488 uintptr_t u = tile_y * row_size
2489 + tile_x * tile_size
2490 + 512 * (byte_x / 8)
2491 + 64 * (byte_y / 8)
2492 + 32 * ((byte_y / 4) % 2)
2493 + 16 * ((byte_x / 4) % 2)
2494 + 8 * ((byte_y / 2) % 2)
2495 + 4 * ((byte_x / 2) % 2)
2496 + 2 * (byte_y % 2)
2497 + 1 * (byte_x % 2);
2498
2499 if (swizzled) {
2500 /* adjust for bit6 swizzling */
2501 if (((byte_x / 8) % 2) == 1) {
2502 if (((byte_y / 8) % 2) == 0) {
2503 u += 64;
2504 } else {
2505 u -= 64;
2506 }
2507 }
2508 }
2509
2510 return u;
2511 }
2512
2513 void
2514 intel_miptree_updownsample(struct brw_context *brw,
2515 struct intel_mipmap_tree *src,
2516 struct intel_mipmap_tree *dst)
2517 {
2518 brw_blorp_blit_miptrees(brw,
2519 src, 0 /* level */, 0 /* layer */,
2520 src->format, SWIZZLE_XYZW,
2521 dst, 0 /* level */, 0 /* layer */, dst->format,
2522 0, 0,
2523 src->logical_width0, src->logical_height0,
2524 0, 0,
2525 dst->logical_width0, dst->logical_height0,
2526 GL_NEAREST, false, false /*mirror x, y*/,
2527 false, false);
2528
2529 if (src->stencil_mt) {
2530 brw_blorp_blit_miptrees(brw,
2531 src->stencil_mt, 0 /* level */, 0 /* layer */,
2532 src->stencil_mt->format, SWIZZLE_XYZW,
2533 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2534 dst->stencil_mt->format,
2535 0, 0,
2536 src->logical_width0, src->logical_height0,
2537 0, 0,
2538 dst->logical_width0, dst->logical_height0,
2539 GL_NEAREST, false, false /*mirror x, y*/,
2540 false, false /* decode/encode srgb */);
2541 }
2542 }
2543
2544 void
2545 intel_update_r8stencil(struct brw_context *brw,
2546 struct intel_mipmap_tree *mt)
2547 {
2548 assert(brw->gen >= 7);
2549 struct intel_mipmap_tree *src =
2550 mt->format == MESA_FORMAT_S_UINT8 ? mt : mt->stencil_mt;
2551 if (!src || brw->gen >= 8 || !src->r8stencil_needs_update)
2552 return;
2553
2554 if (!mt->r8stencil_mt) {
2555 const uint32_t r8stencil_flags =
2556 MIPTREE_LAYOUT_ACCELERATED_UPLOAD | MIPTREE_LAYOUT_TILING_Y |
2557 MIPTREE_LAYOUT_DISABLE_AUX;
2558 assert(brw->gen > 6); /* Handle MIPTREE_LAYOUT_GEN6_HIZ_STENCIL */
2559 mt->r8stencil_mt = intel_miptree_create(brw,
2560 src->target,
2561 MESA_FORMAT_R_UINT8,
2562 src->first_level,
2563 src->last_level,
2564 src->logical_width0,
2565 src->logical_height0,
2566 src->logical_depth0,
2567 src->num_samples,
2568 r8stencil_flags);
2569 assert(mt->r8stencil_mt);
2570 }
2571
2572 struct intel_mipmap_tree *dst = mt->r8stencil_mt;
2573
2574 for (int level = src->first_level; level <= src->last_level; level++) {
2575 const unsigned depth = src->level[level].depth;
2576
2577 for (unsigned layer = 0; layer < depth; layer++) {
2578 brw_blorp_copy_miptrees(brw,
2579 src, level, layer,
2580 dst, level, layer,
2581 0, 0, 0, 0,
2582 minify(src->logical_width0, level),
2583 minify(src->logical_height0, level));
2584 }
2585 }
2586
2587 brw_render_cache_set_check_flush(brw, dst->bo);
2588 src->r8stencil_needs_update = false;
2589 }
2590
2591 static void *
2592 intel_miptree_map_raw(struct brw_context *brw,
2593 struct intel_mipmap_tree *mt,
2594 GLbitfield mode)
2595 {
2596 struct brw_bo *bo = mt->bo;
2597
2598 if (brw_batch_references(&brw->batch, bo))
2599 intel_batchbuffer_flush(brw);
2600
2601 return brw_bo_map(brw, bo, mode);
2602 }
2603
2604 static void
2605 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2606 {
2607 brw_bo_unmap(mt->bo);
2608 }
2609
2610 static void
2611 intel_miptree_map_gtt(struct brw_context *brw,
2612 struct intel_mipmap_tree *mt,
2613 struct intel_miptree_map *map,
2614 unsigned int level, unsigned int slice)
2615 {
2616 unsigned int bw, bh;
2617 void *base;
2618 unsigned int image_x, image_y;
2619 intptr_t x = map->x;
2620 intptr_t y = map->y;
2621
2622 /* For compressed formats, the stride is the number of bytes per
2623 * row of blocks. intel_miptree_get_image_offset() already does
2624 * the divide.
2625 */
2626 _mesa_get_format_block_size(mt->format, &bw, &bh);
2627 assert(y % bh == 0);
2628 assert(x % bw == 0);
2629 y /= bh;
2630 x /= bw;
2631
2632 base = intel_miptree_map_raw(brw, mt, map->mode) + mt->offset;
2633
2634 if (base == NULL)
2635 map->ptr = NULL;
2636 else {
2637 /* Note that in the case of cube maps, the caller must have passed the
2638 * slice number referencing the face.
2639 */
2640 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2641 x += image_x;
2642 y += image_y;
2643
2644 map->stride = mt->pitch;
2645 map->ptr = base + y * map->stride + x * mt->cpp;
2646 }
2647
2648 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2649 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2650 map->x, map->y, map->w, map->h,
2651 mt, _mesa_get_format_name(mt->format),
2652 x, y, map->ptr, map->stride);
2653 }
2654
2655 static void
2656 intel_miptree_unmap_gtt(struct intel_mipmap_tree *mt)
2657 {
2658 intel_miptree_unmap_raw(mt);
2659 }
2660
2661 static void
2662 intel_miptree_map_blit(struct brw_context *brw,
2663 struct intel_mipmap_tree *mt,
2664 struct intel_miptree_map *map,
2665 unsigned int level, unsigned int slice)
2666 {
2667 map->linear_mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
2668 /* first_level */ 0,
2669 /* last_level */ 0,
2670 map->w, map->h, 1,
2671 /* samples */ 0,
2672 MIPTREE_LAYOUT_TILING_NONE);
2673
2674 if (!map->linear_mt) {
2675 fprintf(stderr, "Failed to allocate blit temporary\n");
2676 goto fail;
2677 }
2678 map->stride = map->linear_mt->pitch;
2679
2680 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2681 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2682 * invalidate is set, since we'll be writing the whole rectangle from our
2683 * temporary buffer back out.
2684 */
2685 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2686 if (!intel_miptree_copy(brw,
2687 mt, level, slice, map->x, map->y,
2688 map->linear_mt, 0, 0, 0, 0,
2689 map->w, map->h)) {
2690 fprintf(stderr, "Failed to blit\n");
2691 goto fail;
2692 }
2693 }
2694
2695 map->ptr = intel_miptree_map_raw(brw, map->linear_mt, map->mode);
2696
2697 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2698 map->x, map->y, map->w, map->h,
2699 mt, _mesa_get_format_name(mt->format),
2700 level, slice, map->ptr, map->stride);
2701
2702 return;
2703
2704 fail:
2705 intel_miptree_release(&map->linear_mt);
2706 map->ptr = NULL;
2707 map->stride = 0;
2708 }
2709
2710 static void
2711 intel_miptree_unmap_blit(struct brw_context *brw,
2712 struct intel_mipmap_tree *mt,
2713 struct intel_miptree_map *map,
2714 unsigned int level,
2715 unsigned int slice)
2716 {
2717 struct gl_context *ctx = &brw->ctx;
2718
2719 intel_miptree_unmap_raw(map->linear_mt);
2720
2721 if (map->mode & GL_MAP_WRITE_BIT) {
2722 bool ok = intel_miptree_copy(brw,
2723 map->linear_mt, 0, 0, 0, 0,
2724 mt, level, slice, map->x, map->y,
2725 map->w, map->h);
2726 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
2727 }
2728
2729 intel_miptree_release(&map->linear_mt);
2730 }
2731
2732 /**
2733 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2734 */
2735 #if defined(USE_SSE41)
2736 static void
2737 intel_miptree_map_movntdqa(struct brw_context *brw,
2738 struct intel_mipmap_tree *mt,
2739 struct intel_miptree_map *map,
2740 unsigned int level, unsigned int slice)
2741 {
2742 assert(map->mode & GL_MAP_READ_BIT);
2743 assert(!(map->mode & GL_MAP_WRITE_BIT));
2744
2745 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2746 map->x, map->y, map->w, map->h,
2747 mt, _mesa_get_format_name(mt->format),
2748 level, slice, map->ptr, map->stride);
2749
2750 /* Map the original image */
2751 uint32_t image_x;
2752 uint32_t image_y;
2753 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2754 image_x += map->x;
2755 image_y += map->y;
2756
2757 void *src = intel_miptree_map_raw(brw, mt, map->mode);
2758 if (!src)
2759 return;
2760
2761 src += mt->offset;
2762
2763 src += image_y * mt->pitch;
2764 src += image_x * mt->cpp;
2765
2766 /* Due to the pixel offsets for the particular image being mapped, our
2767 * src pointer may not be 16-byte aligned. However, if the pitch is
2768 * divisible by 16, then the amount by which it's misaligned will remain
2769 * consistent from row to row.
2770 */
2771 assert((mt->pitch % 16) == 0);
2772 const int misalignment = ((uintptr_t) src) & 15;
2773
2774 /* Create an untiled temporary buffer for the mapping. */
2775 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
2776
2777 map->stride = ALIGN(misalignment + width_bytes, 16);
2778
2779 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
2780 /* Offset the destination so it has the same misalignment as src. */
2781 map->ptr = map->buffer + misalignment;
2782
2783 assert((((uintptr_t) map->ptr) & 15) == misalignment);
2784
2785 for (uint32_t y = 0; y < map->h; y++) {
2786 void *dst_ptr = map->ptr + y * map->stride;
2787 void *src_ptr = src + y * mt->pitch;
2788
2789 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
2790 }
2791
2792 intel_miptree_unmap_raw(mt);
2793 }
2794
2795 static void
2796 intel_miptree_unmap_movntdqa(struct brw_context *brw,
2797 struct intel_mipmap_tree *mt,
2798 struct intel_miptree_map *map,
2799 unsigned int level,
2800 unsigned int slice)
2801 {
2802 _mesa_align_free(map->buffer);
2803 map->buffer = NULL;
2804 map->ptr = NULL;
2805 }
2806 #endif
2807
2808 static void
2809 intel_miptree_map_s8(struct brw_context *brw,
2810 struct intel_mipmap_tree *mt,
2811 struct intel_miptree_map *map,
2812 unsigned int level, unsigned int slice)
2813 {
2814 map->stride = map->w;
2815 map->buffer = map->ptr = malloc(map->stride * map->h);
2816 if (!map->buffer)
2817 return;
2818
2819 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2820 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2821 * invalidate is set, since we'll be writing the whole rectangle from our
2822 * temporary buffer back out.
2823 */
2824 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2825 uint8_t *untiled_s8_map = map->ptr;
2826 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_READ_BIT);
2827 unsigned int image_x, image_y;
2828
2829 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2830
2831 for (uint32_t y = 0; y < map->h; y++) {
2832 for (uint32_t x = 0; x < map->w; x++) {
2833 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2834 x + image_x + map->x,
2835 y + image_y + map->y,
2836 brw->has_swizzling);
2837 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
2838 }
2839 }
2840
2841 intel_miptree_unmap_raw(mt);
2842
2843 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
2844 map->x, map->y, map->w, map->h,
2845 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
2846 } else {
2847 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2848 map->x, map->y, map->w, map->h,
2849 mt, map->ptr, map->stride);
2850 }
2851 }
2852
2853 static void
2854 intel_miptree_unmap_s8(struct brw_context *brw,
2855 struct intel_mipmap_tree *mt,
2856 struct intel_miptree_map *map,
2857 unsigned int level,
2858 unsigned int slice)
2859 {
2860 if (map->mode & GL_MAP_WRITE_BIT) {
2861 unsigned int image_x, image_y;
2862 uint8_t *untiled_s8_map = map->ptr;
2863 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT);
2864
2865 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2866
2867 for (uint32_t y = 0; y < map->h; y++) {
2868 for (uint32_t x = 0; x < map->w; x++) {
2869 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2870 image_x + x + map->x,
2871 image_y + y + map->y,
2872 brw->has_swizzling);
2873 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
2874 }
2875 }
2876
2877 intel_miptree_unmap_raw(mt);
2878 }
2879
2880 free(map->buffer);
2881 }
2882
2883 static void
2884 intel_miptree_map_etc(struct brw_context *brw,
2885 struct intel_mipmap_tree *mt,
2886 struct intel_miptree_map *map,
2887 unsigned int level,
2888 unsigned int slice)
2889 {
2890 assert(mt->etc_format != MESA_FORMAT_NONE);
2891 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
2892 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
2893 }
2894
2895 assert(map->mode & GL_MAP_WRITE_BIT);
2896 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
2897
2898 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
2899 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
2900 map->w, map->h, 1));
2901 map->ptr = map->buffer;
2902 }
2903
2904 static void
2905 intel_miptree_unmap_etc(struct brw_context *brw,
2906 struct intel_mipmap_tree *mt,
2907 struct intel_miptree_map *map,
2908 unsigned int level,
2909 unsigned int slice)
2910 {
2911 uint32_t image_x;
2912 uint32_t image_y;
2913 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2914
2915 image_x += map->x;
2916 image_y += map->y;
2917
2918 uint8_t *dst = intel_miptree_map_raw(brw, mt, GL_MAP_WRITE_BIT)
2919 + image_y * mt->pitch
2920 + image_x * mt->cpp;
2921
2922 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
2923 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
2924 map->ptr, map->stride,
2925 map->w, map->h);
2926 else
2927 _mesa_unpack_etc2_format(dst, mt->pitch,
2928 map->ptr, map->stride,
2929 map->w, map->h, mt->etc_format);
2930
2931 intel_miptree_unmap_raw(mt);
2932 free(map->buffer);
2933 }
2934
2935 /**
2936 * Mapping function for packed depth/stencil miptrees backed by real separate
2937 * miptrees for depth and stencil.
2938 *
2939 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2940 * separate from the depth buffer. Yet at the GL API level, we have to expose
2941 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2942 * be able to map that memory for texture storage and glReadPixels-type
2943 * operations. We give Mesa core that access by mallocing a temporary and
2944 * copying the data between the actual backing store and the temporary.
2945 */
2946 static void
2947 intel_miptree_map_depthstencil(struct brw_context *brw,
2948 struct intel_mipmap_tree *mt,
2949 struct intel_miptree_map *map,
2950 unsigned int level, unsigned int slice)
2951 {
2952 struct intel_mipmap_tree *z_mt = mt;
2953 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2954 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2955 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
2956
2957 map->stride = map->w * packed_bpp;
2958 map->buffer = map->ptr = malloc(map->stride * map->h);
2959 if (!map->buffer)
2960 return;
2961
2962 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2963 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2964 * invalidate is set, since we'll be writing the whole rectangle from our
2965 * temporary buffer back out.
2966 */
2967 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2968 uint32_t *packed_map = map->ptr;
2969 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_READ_BIT);
2970 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_READ_BIT);
2971 unsigned int s_image_x, s_image_y;
2972 unsigned int z_image_x, z_image_y;
2973
2974 intel_miptree_get_image_offset(s_mt, level, slice,
2975 &s_image_x, &s_image_y);
2976 intel_miptree_get_image_offset(z_mt, level, slice,
2977 &z_image_x, &z_image_y);
2978
2979 for (uint32_t y = 0; y < map->h; y++) {
2980 for (uint32_t x = 0; x < map->w; x++) {
2981 int map_x = map->x + x, map_y = map->y + y;
2982 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2983 map_x + s_image_x,
2984 map_y + s_image_y,
2985 brw->has_swizzling);
2986 ptrdiff_t z_offset = ((map_y + z_image_y) *
2987 (z_mt->pitch / 4) +
2988 (map_x + z_image_x));
2989 uint8_t s = s_map[s_offset];
2990 uint32_t z = z_map[z_offset];
2991
2992 if (map_z32f_x24s8) {
2993 packed_map[(y * map->w + x) * 2 + 0] = z;
2994 packed_map[(y * map->w + x) * 2 + 1] = s;
2995 } else {
2996 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
2997 }
2998 }
2999 }
3000
3001 intel_miptree_unmap_raw(s_mt);
3002 intel_miptree_unmap_raw(z_mt);
3003
3004 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
3005 __func__,
3006 map->x, map->y, map->w, map->h,
3007 z_mt, map->x + z_image_x, map->y + z_image_y,
3008 s_mt, map->x + s_image_x, map->y + s_image_y,
3009 map->ptr, map->stride);
3010 } else {
3011 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
3012 map->x, map->y, map->w, map->h,
3013 mt, map->ptr, map->stride);
3014 }
3015 }
3016
3017 static void
3018 intel_miptree_unmap_depthstencil(struct brw_context *brw,
3019 struct intel_mipmap_tree *mt,
3020 struct intel_miptree_map *map,
3021 unsigned int level,
3022 unsigned int slice)
3023 {
3024 struct intel_mipmap_tree *z_mt = mt;
3025 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
3026 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
3027
3028 if (map->mode & GL_MAP_WRITE_BIT) {
3029 uint32_t *packed_map = map->ptr;
3030 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt, GL_MAP_WRITE_BIT);
3031 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt, GL_MAP_WRITE_BIT);
3032 unsigned int s_image_x, s_image_y;
3033 unsigned int z_image_x, z_image_y;
3034
3035 intel_miptree_get_image_offset(s_mt, level, slice,
3036 &s_image_x, &s_image_y);
3037 intel_miptree_get_image_offset(z_mt, level, slice,
3038 &z_image_x, &z_image_y);
3039
3040 for (uint32_t y = 0; y < map->h; y++) {
3041 for (uint32_t x = 0; x < map->w; x++) {
3042 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
3043 x + s_image_x + map->x,
3044 y + s_image_y + map->y,
3045 brw->has_swizzling);
3046 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
3047 (z_mt->pitch / 4) +
3048 (x + z_image_x + map->x));
3049
3050 if (map_z32f_x24s8) {
3051 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
3052 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
3053 } else {
3054 uint32_t packed = packed_map[y * map->w + x];
3055 s_map[s_offset] = packed >> 24;
3056 z_map[z_offset] = packed;
3057 }
3058 }
3059 }
3060
3061 intel_miptree_unmap_raw(s_mt);
3062 intel_miptree_unmap_raw(z_mt);
3063
3064 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
3065 __func__,
3066 map->x, map->y, map->w, map->h,
3067 z_mt, _mesa_get_format_name(z_mt->format),
3068 map->x + z_image_x, map->y + z_image_y,
3069 s_mt, map->x + s_image_x, map->y + s_image_y,
3070 map->ptr, map->stride);
3071 }
3072
3073 free(map->buffer);
3074 }
3075
3076 /**
3077 * Create and attach a map to the miptree at (level, slice). Return the
3078 * attached map.
3079 */
3080 static struct intel_miptree_map*
3081 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
3082 unsigned int level,
3083 unsigned int slice,
3084 unsigned int x,
3085 unsigned int y,
3086 unsigned int w,
3087 unsigned int h,
3088 GLbitfield mode)
3089 {
3090 struct intel_miptree_map *map = calloc(1, sizeof(*map));
3091
3092 if (!map)
3093 return NULL;
3094
3095 assert(mt->level[level].slice[slice].map == NULL);
3096 mt->level[level].slice[slice].map = map;
3097
3098 map->mode = mode;
3099 map->x = x;
3100 map->y = y;
3101 map->w = w;
3102 map->h = h;
3103
3104 return map;
3105 }
3106
3107 /**
3108 * Release the map at (level, slice).
3109 */
3110 static void
3111 intel_miptree_release_map(struct intel_mipmap_tree *mt,
3112 unsigned int level,
3113 unsigned int slice)
3114 {
3115 struct intel_miptree_map **map;
3116
3117 map = &mt->level[level].slice[slice].map;
3118 free(*map);
3119 *map = NULL;
3120 }
3121
3122 static bool
3123 can_blit_slice(struct intel_mipmap_tree *mt,
3124 unsigned int level, unsigned int slice)
3125 {
3126 /* See intel_miptree_blit() for details on the 32k pitch limit. */
3127 if (mt->pitch >= 32768)
3128 return false;
3129
3130 return true;
3131 }
3132
3133 static bool
3134 use_intel_mipree_map_blit(struct brw_context *brw,
3135 struct intel_mipmap_tree *mt,
3136 GLbitfield mode,
3137 unsigned int level,
3138 unsigned int slice)
3139 {
3140 if (brw->has_llc &&
3141 /* It's probably not worth swapping to the blit ring because of
3142 * all the overhead involved.
3143 */
3144 !(mode & GL_MAP_WRITE_BIT) &&
3145 !mt->compressed &&
3146 (mt->tiling == I915_TILING_X ||
3147 /* Prior to Sandybridge, the blitter can't handle Y tiling */
3148 (brw->gen >= 6 && mt->tiling == I915_TILING_Y) ||
3149 /* Fast copy blit on skl+ supports all tiling formats. */
3150 brw->gen >= 9) &&
3151 can_blit_slice(mt, level, slice))
3152 return true;
3153
3154 if (mt->tiling != I915_TILING_NONE &&
3155 mt->bo->size >= brw->max_gtt_map_object_size) {
3156 assert(can_blit_slice(mt, level, slice));
3157 return true;
3158 }
3159
3160 return false;
3161 }
3162
3163 /**
3164 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
3165 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
3166 * arithmetic overflow.
3167 *
3168 * If you call this function and use \a out_stride, then you're doing pointer
3169 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
3170 * bugs. The caller must still take care to avoid 32-bit overflow errors in
3171 * all arithmetic expressions that contain buffer offsets and pixel sizes,
3172 * which usually have type uint32_t or GLuint.
3173 */
3174 void
3175 intel_miptree_map(struct brw_context *brw,
3176 struct intel_mipmap_tree *mt,
3177 unsigned int level,
3178 unsigned int slice,
3179 unsigned int x,
3180 unsigned int y,
3181 unsigned int w,
3182 unsigned int h,
3183 GLbitfield mode,
3184 void **out_ptr,
3185 ptrdiff_t *out_stride)
3186 {
3187 struct intel_miptree_map *map;
3188
3189 assert(mt->num_samples <= 1);
3190
3191 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
3192 if (!map){
3193 *out_ptr = NULL;
3194 *out_stride = 0;
3195 return;
3196 }
3197
3198 intel_miptree_access_raw(brw, mt, level, slice,
3199 map->mode & GL_MAP_WRITE_BIT);
3200
3201 if (mt->format == MESA_FORMAT_S_UINT8) {
3202 intel_miptree_map_s8(brw, mt, map, level, slice);
3203 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3204 !(mode & BRW_MAP_DIRECT_BIT)) {
3205 intel_miptree_map_etc(brw, mt, map, level, slice);
3206 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
3207 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
3208 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
3209 intel_miptree_map_blit(brw, mt, map, level, slice);
3210 #if defined(USE_SSE41)
3211 } else if (!(mode & GL_MAP_WRITE_BIT) &&
3212 !mt->compressed && cpu_has_sse4_1 &&
3213 (mt->pitch % 16 == 0)) {
3214 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
3215 #endif
3216 } else {
3217 intel_miptree_map_gtt(brw, mt, map, level, slice);
3218 }
3219
3220 *out_ptr = map->ptr;
3221 *out_stride = map->stride;
3222
3223 if (map->ptr == NULL)
3224 intel_miptree_release_map(mt, level, slice);
3225 }
3226
3227 void
3228 intel_miptree_unmap(struct brw_context *brw,
3229 struct intel_mipmap_tree *mt,
3230 unsigned int level,
3231 unsigned int slice)
3232 {
3233 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
3234
3235 assert(mt->num_samples <= 1);
3236
3237 if (!map)
3238 return;
3239
3240 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
3241 mt, _mesa_get_format_name(mt->format), level, slice);
3242
3243 if (mt->format == MESA_FORMAT_S_UINT8) {
3244 intel_miptree_unmap_s8(brw, mt, map, level, slice);
3245 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3246 !(map->mode & BRW_MAP_DIRECT_BIT)) {
3247 intel_miptree_unmap_etc(brw, mt, map, level, slice);
3248 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
3249 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
3250 } else if (map->linear_mt) {
3251 intel_miptree_unmap_blit(brw, mt, map, level, slice);
3252 #if defined(USE_SSE41)
3253 } else if (map->buffer && cpu_has_sse4_1) {
3254 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
3255 #endif
3256 } else {
3257 intel_miptree_unmap_gtt(mt);
3258 }
3259
3260 intel_miptree_release_map(mt, level, slice);
3261 }
3262
3263 enum isl_surf_dim
3264 get_isl_surf_dim(GLenum target)
3265 {
3266 switch (target) {
3267 case GL_TEXTURE_1D:
3268 case GL_TEXTURE_1D_ARRAY:
3269 return ISL_SURF_DIM_1D;
3270
3271 case GL_TEXTURE_2D:
3272 case GL_TEXTURE_2D_ARRAY:
3273 case GL_TEXTURE_RECTANGLE:
3274 case GL_TEXTURE_CUBE_MAP:
3275 case GL_TEXTURE_CUBE_MAP_ARRAY:
3276 case GL_TEXTURE_2D_MULTISAMPLE:
3277 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3278 case GL_TEXTURE_EXTERNAL_OES:
3279 return ISL_SURF_DIM_2D;
3280
3281 case GL_TEXTURE_3D:
3282 return ISL_SURF_DIM_3D;
3283 }
3284
3285 unreachable("Invalid texture target");
3286 }
3287
3288 enum isl_dim_layout
3289 get_isl_dim_layout(const struct gen_device_info *devinfo, uint32_t tiling,
3290 GLenum target, enum miptree_array_layout array_layout)
3291 {
3292 if (array_layout == GEN6_HIZ_STENCIL)
3293 return ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ;
3294
3295 switch (target) {
3296 case GL_TEXTURE_1D:
3297 case GL_TEXTURE_1D_ARRAY:
3298 return (devinfo->gen >= 9 && tiling == I915_TILING_NONE ?
3299 ISL_DIM_LAYOUT_GEN9_1D : ISL_DIM_LAYOUT_GEN4_2D);
3300
3301 case GL_TEXTURE_2D:
3302 case GL_TEXTURE_2D_ARRAY:
3303 case GL_TEXTURE_RECTANGLE:
3304 case GL_TEXTURE_2D_MULTISAMPLE:
3305 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3306 case GL_TEXTURE_EXTERNAL_OES:
3307 return ISL_DIM_LAYOUT_GEN4_2D;
3308
3309 case GL_TEXTURE_CUBE_MAP:
3310 case GL_TEXTURE_CUBE_MAP_ARRAY:
3311 return (devinfo->gen == 4 ? ISL_DIM_LAYOUT_GEN4_3D :
3312 ISL_DIM_LAYOUT_GEN4_2D);
3313
3314 case GL_TEXTURE_3D:
3315 return (devinfo->gen >= 9 ?
3316 ISL_DIM_LAYOUT_GEN4_2D : ISL_DIM_LAYOUT_GEN4_3D);
3317 }
3318
3319 unreachable("Invalid texture target");
3320 }
3321
3322 enum isl_tiling
3323 intel_miptree_get_isl_tiling(const struct intel_mipmap_tree *mt)
3324 {
3325 if (mt->format == MESA_FORMAT_S_UINT8) {
3326 return ISL_TILING_W;
3327 } else {
3328 switch (mt->tiling) {
3329 case I915_TILING_NONE:
3330 return ISL_TILING_LINEAR;
3331 case I915_TILING_X:
3332 return ISL_TILING_X;
3333 case I915_TILING_Y:
3334 return ISL_TILING_Y0;
3335 default:
3336 unreachable("Invalid tiling mode");
3337 }
3338 }
3339 }
3340
3341 void
3342 intel_miptree_get_isl_surf(struct brw_context *brw,
3343 const struct intel_mipmap_tree *mt,
3344 struct isl_surf *surf)
3345 {
3346 surf->dim = get_isl_surf_dim(mt->target);
3347 surf->dim_layout = get_isl_dim_layout(&brw->screen->devinfo,
3348 mt->tiling, mt->target,
3349 mt->array_layout);
3350
3351 if (mt->num_samples > 1) {
3352 switch (mt->msaa_layout) {
3353 case INTEL_MSAA_LAYOUT_IMS:
3354 surf->msaa_layout = ISL_MSAA_LAYOUT_INTERLEAVED;
3355 break;
3356 case INTEL_MSAA_LAYOUT_UMS:
3357 case INTEL_MSAA_LAYOUT_CMS:
3358 surf->msaa_layout = ISL_MSAA_LAYOUT_ARRAY;
3359 break;
3360 default:
3361 unreachable("Invalid MSAA layout");
3362 }
3363 } else {
3364 surf->msaa_layout = ISL_MSAA_LAYOUT_NONE;
3365 }
3366
3367 surf->tiling = intel_miptree_get_isl_tiling(mt);
3368
3369 if (mt->format == MESA_FORMAT_S_UINT8) {
3370 /* The ISL definition of row_pitch matches the surface state pitch field
3371 * a bit better than intel_mipmap_tree. In particular, ISL incorporates
3372 * the factor of 2 for W-tiling in row_pitch.
3373 */
3374 surf->row_pitch = 2 * mt->pitch;
3375 } else {
3376 surf->row_pitch = mt->pitch;
3377 }
3378
3379 surf->format = translate_tex_format(brw, mt->format, false);
3380
3381 if (brw->gen >= 9) {
3382 if (surf->dim == ISL_SURF_DIM_1D && surf->tiling == ISL_TILING_LINEAR) {
3383 /* For gen9 1-D surfaces, intel_mipmap_tree has a bogus alignment. */
3384 surf->image_alignment_el = isl_extent3d(64, 1, 1);
3385 } else {
3386 /* On gen9+, intel_mipmap_tree stores the horizontal and vertical
3387 * alignment in terms of surface elements like we want.
3388 */
3389 surf->image_alignment_el = isl_extent3d(mt->halign, mt->valign, 1);
3390 }
3391 } else {
3392 /* On earlier gens it's stored in pixels. */
3393 unsigned bw, bh;
3394 _mesa_get_format_block_size(mt->format, &bw, &bh);
3395 surf->image_alignment_el =
3396 isl_extent3d(mt->halign / bw, mt->valign / bh, 1);
3397 }
3398
3399 surf->logical_level0_px.width = mt->logical_width0;
3400 surf->logical_level0_px.height = mt->logical_height0;
3401 if (surf->dim == ISL_SURF_DIM_3D) {
3402 surf->logical_level0_px.depth = mt->logical_depth0;
3403 surf->logical_level0_px.array_len = 1;
3404 } else {
3405 surf->logical_level0_px.depth = 1;
3406 surf->logical_level0_px.array_len = mt->logical_depth0;
3407 }
3408
3409 surf->phys_level0_sa.width = mt->physical_width0;
3410 surf->phys_level0_sa.height = mt->physical_height0;
3411 if (surf->dim == ISL_SURF_DIM_3D) {
3412 surf->phys_level0_sa.depth = mt->physical_depth0;
3413 surf->phys_level0_sa.array_len = 1;
3414 } else {
3415 surf->phys_level0_sa.depth = 1;
3416 surf->phys_level0_sa.array_len = mt->physical_depth0;
3417 }
3418
3419 surf->levels = mt->last_level - mt->first_level + 1;
3420 surf->samples = MAX2(mt->num_samples, 1);
3421
3422 surf->size = 0; /* TODO */
3423 surf->alignment = 0; /* TODO */
3424
3425 switch (surf->dim_layout) {
3426 case ISL_DIM_LAYOUT_GEN4_2D:
3427 case ISL_DIM_LAYOUT_GEN4_3D:
3428 case ISL_DIM_LAYOUT_GEN6_STENCIL_HIZ:
3429 if (brw->gen >= 9) {
3430 surf->array_pitch_el_rows = mt->qpitch;
3431 } else {
3432 unsigned bw, bh;
3433 _mesa_get_format_block_size(mt->format, &bw, &bh);
3434 assert(mt->qpitch % bh == 0);
3435 surf->array_pitch_el_rows = mt->qpitch / bh;
3436 }
3437 break;
3438 case ISL_DIM_LAYOUT_GEN9_1D:
3439 surf->array_pitch_el_rows = 1;
3440 break;
3441 }
3442
3443 switch (mt->array_layout) {
3444 case ALL_LOD_IN_EACH_SLICE:
3445 surf->array_pitch_span = ISL_ARRAY_PITCH_SPAN_FULL;
3446 break;
3447 case ALL_SLICES_AT_EACH_LOD:
3448 case GEN6_HIZ_STENCIL:
3449 surf->array_pitch_span = ISL_ARRAY_PITCH_SPAN_COMPACT;
3450 break;
3451 default:
3452 unreachable("Invalid array layout");
3453 }
3454
3455 GLenum base_format = _mesa_get_format_base_format(mt->format);
3456 switch (base_format) {
3457 case GL_DEPTH_COMPONENT:
3458 surf->usage = ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
3459 break;
3460 case GL_STENCIL_INDEX:
3461 surf->usage = ISL_SURF_USAGE_STENCIL_BIT;
3462 if (brw->gen >= 8)
3463 surf->usage |= ISL_SURF_USAGE_TEXTURE_BIT;
3464 break;
3465 case GL_DEPTH_STENCIL:
3466 /* In this case we only texture from the depth part */
3467 surf->usage = ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_STENCIL_BIT |
3468 ISL_SURF_USAGE_TEXTURE_BIT;
3469 break;
3470 default:
3471 surf->usage = ISL_SURF_USAGE_TEXTURE_BIT;
3472 if (brw->format_supported_as_render_target[mt->format])
3473 surf->usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
3474 break;
3475 }
3476
3477 if (_mesa_is_cube_map_texture(mt->target))
3478 surf->usage |= ISL_SURF_USAGE_CUBE_BIT;
3479 }
3480
3481 /* WARNING: THE SURFACE CREATED BY THIS FUNCTION IS NOT COMPLETE AND CANNOT BE
3482 * USED FOR ANY REAL CALCULATIONS. THE ONLY VALID USE OF SUCH A SURFACE IS TO
3483 * PASS IT INTO isl_surf_fill_state.
3484 */
3485 void
3486 intel_miptree_get_aux_isl_surf(struct brw_context *brw,
3487 const struct intel_mipmap_tree *mt,
3488 struct isl_surf *surf,
3489 enum isl_aux_usage *usage)
3490 {
3491 uint32_t aux_pitch, aux_qpitch;
3492 if (mt->mcs_buf) {
3493 aux_pitch = mt->mcs_buf->pitch;
3494 aux_qpitch = mt->mcs_buf->qpitch;
3495
3496 if (mt->num_samples > 1) {
3497 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS);
3498 *usage = ISL_AUX_USAGE_MCS;
3499 } else if (intel_miptree_is_lossless_compressed(brw, mt)) {
3500 assert(brw->gen >= 9);
3501 *usage = ISL_AUX_USAGE_CCS_E;
3502 } else if ((mt->aux_disable & INTEL_AUX_DISABLE_CCS) == 0) {
3503 *usage = ISL_AUX_USAGE_CCS_D;
3504 } else {
3505 unreachable("Invalid MCS miptree");
3506 }
3507 } else if (mt->hiz_buf) {
3508 aux_pitch = mt->hiz_buf->aux_base.pitch;
3509 aux_qpitch = mt->hiz_buf->aux_base.qpitch;
3510
3511 *usage = ISL_AUX_USAGE_HIZ;
3512 } else {
3513 *usage = ISL_AUX_USAGE_NONE;
3514 return;
3515 }
3516
3517 /* Start with a copy of the original surface. */
3518 intel_miptree_get_isl_surf(brw, mt, surf);
3519
3520 /* Figure out the format and tiling of the auxiliary surface */
3521 switch (*usage) {
3522 case ISL_AUX_USAGE_NONE:
3523 unreachable("Invalid auxiliary usage");
3524
3525 case ISL_AUX_USAGE_HIZ:
3526 isl_surf_get_hiz_surf(&brw->isl_dev, surf, surf);
3527 break;
3528
3529 case ISL_AUX_USAGE_MCS:
3530 /*
3531 * From the SKL PRM:
3532 * "When Auxiliary Surface Mode is set to AUX_CCS_D or AUX_CCS_E,
3533 * HALIGN 16 must be used."
3534 */
3535 if (brw->gen >= 9)
3536 assert(mt->halign == 16);
3537
3538 isl_surf_get_mcs_surf(&brw->isl_dev, surf, surf);
3539 break;
3540
3541 case ISL_AUX_USAGE_CCS_D:
3542 case ISL_AUX_USAGE_CCS_E:
3543 /*
3544 * From the BDW PRM, Volume 2d, page 260 (RENDER_SURFACE_STATE):
3545 *
3546 * "When MCS is enabled for non-MSRT, HALIGN_16 must be used"
3547 *
3548 * From the hardware spec for GEN9:
3549 *
3550 * "When Auxiliary Surface Mode is set to AUX_CCS_D or AUX_CCS_E,
3551 * HALIGN 16 must be used."
3552 */
3553 assert(mt->num_samples <= 1);
3554 if (brw->gen >= 8)
3555 assert(mt->halign == 16);
3556
3557 isl_surf_get_ccs_surf(&brw->isl_dev, surf, surf);
3558 break;
3559 }
3560
3561 /* We want the pitch of the actual aux buffer. */
3562 surf->row_pitch = aux_pitch;
3563
3564 /* Auxiliary surfaces in ISL have compressed formats and array_pitch_el_rows
3565 * is in elements. This doesn't match intel_mipmap_tree::qpitch which is
3566 * in elements of the primary color surface so we have to divide by the
3567 * compression block height.
3568 */
3569 surf->array_pitch_el_rows =
3570 aux_qpitch / isl_format_get_layout(surf->format)->bh;
3571 }