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