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