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