1f4c59b6cc3773a3273c383394a5d372be562c78
[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 static struct intel_mipmap_tree *
613 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 uint32_t alloc_flags = 0;
628
629 format = intel_lower_compressed_format(brw, format);
630
631 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
632
633 assert((layout_flags & MIPTREE_LAYOUT_DISABLE_AUX) == 0);
634 assert((layout_flags & MIPTREE_LAYOUT_FOR_BO) == 0);
635 mt = intel_miptree_create_layout(brw, target, format,
636 first_level, last_level, width0,
637 height0, depth0, num_samples,
638 layout_flags);
639 /*
640 * pitch == 0 || height == 0 indicates the null texture
641 */
642 if (!mt || !mt->total_width || !mt->total_height) {
643 intel_miptree_release(&mt);
644 return NULL;
645 }
646
647 if (mt->tiling == (I915_TILING_Y | I915_TILING_X))
648 mt->tiling = I915_TILING_Y;
649
650 if (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD)
651 alloc_flags |= BO_ALLOC_FOR_RENDER;
652
653 unsigned long pitch;
654 mt->etc_format = etc_format;
655
656 if (mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE) {
657 unsigned alignment = 0;
658 unsigned long size;
659 size = intel_get_yf_ys_bo_size(mt, &alignment, &pitch);
660 assert(size);
661 mt->bo = drm_intel_bo_alloc_for_render(brw->bufmgr, "miptree",
662 size, alignment);
663 } else {
664 if (format == MESA_FORMAT_S_UINT8) {
665 /* Align to size of W tile, 64x64. */
666 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
667 ALIGN(mt->total_width, 64),
668 ALIGN(mt->total_height, 64),
669 mt->cpp, &mt->tiling, &pitch,
670 alloc_flags);
671 } else {
672 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
673 mt->total_width, mt->total_height,
674 mt->cpp, &mt->tiling, &pitch,
675 alloc_flags);
676 }
677 }
678
679 mt->pitch = pitch;
680
681 return mt;
682 }
683
684 struct intel_mipmap_tree *
685 intel_miptree_create(struct brw_context *brw,
686 GLenum target,
687 mesa_format format,
688 GLuint first_level,
689 GLuint last_level,
690 GLuint width0,
691 GLuint height0,
692 GLuint depth0,
693 GLuint num_samples,
694 uint32_t layout_flags)
695 {
696 struct intel_mipmap_tree *mt = miptree_create(
697 brw, target, format,
698 first_level, last_level,
699 width0, height0, depth0, num_samples,
700 layout_flags);
701
702 /* If the BO is too large to fit in the aperture, we need to use the
703 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
704 * handle Y-tiling, so we need to fall back to X.
705 */
706 if (brw->gen < 6 && mt->bo->size >= brw->max_gtt_map_object_size &&
707 mt->tiling == I915_TILING_Y) {
708 unsigned long pitch = mt->pitch;
709 const uint32_t alloc_flags =
710 (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD) ?
711 BO_ALLOC_FOR_RENDER : 0;
712 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
713 mt->total_width, mt->total_height);
714
715 mt->tiling = I915_TILING_X;
716 drm_intel_bo_unreference(mt->bo);
717 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
718 mt->total_width, mt->total_height, mt->cpp,
719 &mt->tiling, &pitch, alloc_flags);
720 mt->pitch = pitch;
721 }
722
723 mt->offset = 0;
724
725 if (!mt->bo) {
726 intel_miptree_release(&mt);
727 return NULL;
728 }
729
730
731 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
732 assert(mt->num_samples > 1);
733 if (!intel_miptree_alloc_mcs(brw, mt, num_samples)) {
734 intel_miptree_release(&mt);
735 return NULL;
736 }
737 }
738
739 /* If this miptree is capable of supporting fast color clears, set
740 * fast_clear_state appropriately to ensure that fast clears will occur.
741 * Allocation of the MCS miptree will be deferred until the first fast
742 * clear actually occurs.
743 */
744 if (intel_tiling_supports_non_msrt_mcs(brw, mt->tiling) &&
745 intel_miptree_supports_non_msrt_fast_clear(brw, mt)) {
746 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
747 assert(brw->gen < 8 || mt->halign == 16 || num_samples <= 1);
748 }
749
750 return mt;
751 }
752
753 struct intel_mipmap_tree *
754 intel_miptree_create_for_bo(struct brw_context *brw,
755 drm_intel_bo *bo,
756 mesa_format format,
757 uint32_t offset,
758 uint32_t width,
759 uint32_t height,
760 uint32_t depth,
761 int pitch,
762 uint32_t layout_flags)
763 {
764 struct intel_mipmap_tree *mt;
765 uint32_t tiling, swizzle;
766 GLenum target;
767
768 drm_intel_bo_get_tiling(bo, &tiling, &swizzle);
769
770 /* Nothing will be able to use this miptree with the BO if the offset isn't
771 * aligned.
772 */
773 if (tiling != I915_TILING_NONE)
774 assert(offset % 4096 == 0);
775
776 /* miptrees can't handle negative pitch. If you need flipping of images,
777 * that's outside of the scope of the mt.
778 */
779 assert(pitch >= 0);
780
781 target = depth > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
782
783 /* The BO already has a tiling format and we shouldn't confuse the lower
784 * layers by making it try to find a tiling format again.
785 */
786 assert((layout_flags & MIPTREE_LAYOUT_TILING_ANY) == 0);
787 assert((layout_flags & MIPTREE_LAYOUT_TILING_NONE) == 0);
788
789 layout_flags |= MIPTREE_LAYOUT_FOR_BO;
790 mt = intel_miptree_create_layout(brw, target, format,
791 0, 0,
792 width, height, depth, 0,
793 layout_flags);
794 if (!mt)
795 return NULL;
796
797 drm_intel_bo_reference(bo);
798 mt->bo = bo;
799 mt->pitch = pitch;
800 mt->offset = offset;
801 mt->tiling = tiling;
802
803 return mt;
804 }
805
806 /**
807 * For a singlesample renderbuffer, this simply wraps the given BO with a
808 * miptree.
809 *
810 * For a multisample renderbuffer, this wraps the window system's
811 * (singlesample) BO with a singlesample miptree attached to the
812 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
813 * that will contain the actual rendering (which is lazily resolved to
814 * irb->singlesample_mt).
815 */
816 void
817 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
818 struct intel_renderbuffer *irb,
819 drm_intel_bo *bo,
820 uint32_t width, uint32_t height,
821 uint32_t pitch)
822 {
823 struct intel_mipmap_tree *singlesample_mt = NULL;
824 struct intel_mipmap_tree *multisample_mt = NULL;
825 struct gl_renderbuffer *rb = &irb->Base.Base;
826 mesa_format format = rb->Format;
827 int num_samples = rb->NumSamples;
828
829 /* Only the front and back buffers, which are color buffers, are allocated
830 * through the image loader.
831 */
832 assert(_mesa_get_format_base_format(format) == GL_RGB ||
833 _mesa_get_format_base_format(format) == GL_RGBA);
834
835 singlesample_mt = intel_miptree_create_for_bo(intel,
836 bo,
837 format,
838 0,
839 width,
840 height,
841 1,
842 pitch,
843 0);
844 if (!singlesample_mt)
845 goto fail;
846
847 /* If this miptree is capable of supporting fast color clears, set
848 * mcs_state appropriately to ensure that fast clears will occur.
849 * Allocation of the MCS miptree will be deferred until the first fast
850 * clear actually occurs.
851 */
852 if (intel_tiling_supports_non_msrt_mcs(intel, singlesample_mt->tiling) &&
853 intel_miptree_supports_non_msrt_fast_clear(intel, singlesample_mt)) {
854 singlesample_mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
855 }
856
857 if (num_samples == 0) {
858 intel_miptree_release(&irb->mt);
859 irb->mt = singlesample_mt;
860
861 assert(!irb->singlesample_mt);
862 } else {
863 intel_miptree_release(&irb->singlesample_mt);
864 irb->singlesample_mt = singlesample_mt;
865
866 if (!irb->mt ||
867 irb->mt->logical_width0 != width ||
868 irb->mt->logical_height0 != height) {
869 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
870 format,
871 width,
872 height,
873 num_samples);
874 if (!multisample_mt)
875 goto fail;
876
877 irb->need_downsample = false;
878 intel_miptree_release(&irb->mt);
879 irb->mt = multisample_mt;
880 }
881 }
882 return;
883
884 fail:
885 intel_miptree_release(&irb->singlesample_mt);
886 intel_miptree_release(&irb->mt);
887 return;
888 }
889
890 struct intel_mipmap_tree*
891 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
892 mesa_format format,
893 uint32_t width,
894 uint32_t height,
895 uint32_t num_samples)
896 {
897 struct intel_mipmap_tree *mt;
898 uint32_t depth = 1;
899 bool ok;
900 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
901 const uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
902 MIPTREE_LAYOUT_TILING_ANY;
903
904
905 mt = intel_miptree_create(brw, target, format, 0, 0,
906 width, height, depth, num_samples,
907 layout_flags);
908 if (!mt)
909 goto fail;
910
911 if (intel_miptree_wants_hiz_buffer(brw, mt)) {
912 ok = intel_miptree_alloc_hiz(brw, mt);
913 if (!ok)
914 goto fail;
915 }
916
917 return mt;
918
919 fail:
920 intel_miptree_release(&mt);
921 return NULL;
922 }
923
924 void
925 intel_miptree_reference(struct intel_mipmap_tree **dst,
926 struct intel_mipmap_tree *src)
927 {
928 if (*dst == src)
929 return;
930
931 intel_miptree_release(dst);
932
933 if (src) {
934 src->refcount++;
935 DBG("%s %p refcount now %d\n", __func__, src, src->refcount);
936 }
937
938 *dst = src;
939 }
940
941
942 void
943 intel_miptree_release(struct intel_mipmap_tree **mt)
944 {
945 if (!*mt)
946 return;
947
948 DBG("%s %p refcount will be %d\n", __func__, *mt, (*mt)->refcount - 1);
949 if (--(*mt)->refcount <= 0) {
950 GLuint i;
951
952 DBG("%s deleting %p\n", __func__, *mt);
953
954 drm_intel_bo_unreference((*mt)->bo);
955 intel_miptree_release(&(*mt)->stencil_mt);
956 if ((*mt)->hiz_buf) {
957 if ((*mt)->hiz_buf->mt)
958 intel_miptree_release(&(*mt)->hiz_buf->mt);
959 else
960 drm_intel_bo_unreference((*mt)->hiz_buf->bo);
961 free((*mt)->hiz_buf);
962 }
963 intel_miptree_release(&(*mt)->mcs_mt);
964 intel_resolve_map_clear(&(*mt)->hiz_map);
965
966 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
967 free((*mt)->level[i].slice);
968 }
969
970 free(*mt);
971 }
972 *mt = NULL;
973 }
974
975
976 void
977 intel_get_image_dims(struct gl_texture_image *image,
978 int *width, int *height, int *depth)
979 {
980 switch (image->TexObject->Target) {
981 case GL_TEXTURE_1D_ARRAY:
982 /* For a 1D Array texture the OpenGL API will treat the image height as
983 * the number of array slices. For Intel hardware, we treat the 1D array
984 * as a 2D Array with a height of 1. So, here we want to swap image
985 * height and depth.
986 */
987 *width = image->Width;
988 *height = 1;
989 *depth = image->Height;
990 break;
991 default:
992 *width = image->Width;
993 *height = image->Height;
994 *depth = image->Depth;
995 break;
996 }
997 }
998
999 /**
1000 * Can the image be pulled into a unified mipmap tree? This mirrors
1001 * the completeness test in a lot of ways.
1002 *
1003 * Not sure whether I want to pass gl_texture_image here.
1004 */
1005 bool
1006 intel_miptree_match_image(struct intel_mipmap_tree *mt,
1007 struct gl_texture_image *image)
1008 {
1009 struct intel_texture_image *intelImage = intel_texture_image(image);
1010 GLuint level = intelImage->base.Base.Level;
1011 int width, height, depth;
1012
1013 /* glTexImage* choose the texture object based on the target passed in, and
1014 * objects can't change targets over their lifetimes, so this should be
1015 * true.
1016 */
1017 assert(image->TexObject->Target == mt->target);
1018
1019 mesa_format mt_format = mt->format;
1020 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
1021 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
1022 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
1023 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
1024 if (mt->etc_format != MESA_FORMAT_NONE)
1025 mt_format = mt->etc_format;
1026
1027 if (image->TexFormat != mt_format)
1028 return false;
1029
1030 intel_get_image_dims(image, &width, &height, &depth);
1031
1032 if (mt->target == GL_TEXTURE_CUBE_MAP)
1033 depth = 6;
1034
1035 int level_depth = mt->level[level].depth;
1036 if (mt->num_samples > 1) {
1037 switch (mt->msaa_layout) {
1038 case INTEL_MSAA_LAYOUT_NONE:
1039 case INTEL_MSAA_LAYOUT_IMS:
1040 break;
1041 case INTEL_MSAA_LAYOUT_UMS:
1042 case INTEL_MSAA_LAYOUT_CMS:
1043 level_depth /= mt->num_samples;
1044 break;
1045 }
1046 }
1047
1048 /* Test image dimensions against the base level image adjusted for
1049 * minification. This will also catch images not present in the
1050 * tree, changed targets, etc.
1051 */
1052 if (width != minify(mt->logical_width0, level - mt->first_level) ||
1053 height != minify(mt->logical_height0, level - mt->first_level) ||
1054 depth != level_depth) {
1055 return false;
1056 }
1057
1058 if (image->NumSamples != mt->num_samples)
1059 return false;
1060
1061 return true;
1062 }
1063
1064
1065 void
1066 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
1067 GLuint level,
1068 GLuint x, GLuint y, GLuint d)
1069 {
1070 mt->level[level].depth = d;
1071 mt->level[level].level_x = x;
1072 mt->level[level].level_y = y;
1073
1074 DBG("%s level %d, depth %d, offset %d,%d\n", __func__,
1075 level, d, x, y);
1076
1077 assert(mt->level[level].slice == NULL);
1078
1079 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
1080 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
1081 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
1082 }
1083
1084
1085 void
1086 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
1087 GLuint level, GLuint img,
1088 GLuint x, GLuint y)
1089 {
1090 if (img == 0 && level == 0)
1091 assert(x == 0 && y == 0);
1092
1093 assert(img < mt->level[level].depth);
1094
1095 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
1096 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
1097
1098 DBG("%s level %d img %d pos %d,%d\n",
1099 __func__, level, img,
1100 mt->level[level].slice[img].x_offset,
1101 mt->level[level].slice[img].y_offset);
1102 }
1103
1104 void
1105 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
1106 GLuint level, GLuint slice,
1107 GLuint *x, GLuint *y)
1108 {
1109 assert(slice < mt->level[level].depth);
1110
1111 *x = mt->level[level].slice[slice].x_offset;
1112 *y = mt->level[level].slice[slice].y_offset;
1113 }
1114
1115
1116 /**
1117 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1118 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1119 * and tile_h is set to 1.
1120 */
1121 void
1122 intel_get_tile_dims(uint32_t tiling, uint32_t tr_mode, uint32_t cpp,
1123 uint32_t *tile_w, uint32_t *tile_h)
1124 {
1125 if (tr_mode == INTEL_MIPTREE_TRMODE_NONE) {
1126 switch (tiling) {
1127 case I915_TILING_X:
1128 *tile_w = 512;
1129 *tile_h = 8;
1130 break;
1131 case I915_TILING_Y:
1132 *tile_w = 128;
1133 *tile_h = 32;
1134 break;
1135 case I915_TILING_NONE:
1136 *tile_w = cpp;
1137 *tile_h = 1;
1138 break;
1139 default:
1140 unreachable("not reached");
1141 }
1142 } else {
1143 uint32_t aspect_ratio = 1;
1144 assert(_mesa_is_pow_two(cpp));
1145
1146 switch (cpp) {
1147 case 1:
1148 *tile_h = 64;
1149 break;
1150 case 2:
1151 case 4:
1152 *tile_h = 32;
1153 break;
1154 case 8:
1155 case 16:
1156 *tile_h = 16;
1157 break;
1158 default:
1159 unreachable("not reached");
1160 }
1161
1162 if (cpp == 2 || cpp == 8)
1163 aspect_ratio = 2;
1164
1165 if (tr_mode == INTEL_MIPTREE_TRMODE_YS)
1166 *tile_h *= 4;
1167
1168 *tile_w = *tile_h * aspect_ratio * cpp;
1169 }
1170 }
1171
1172
1173 /**
1174 * This function computes masks that may be used to select the bits of the X
1175 * and Y coordinates that indicate the offset within a tile. If the BO is
1176 * untiled, the masks are set to 0.
1177 */
1178 void
1179 intel_get_tile_masks(uint32_t tiling, uint32_t tr_mode, uint32_t cpp,
1180 bool map_stencil_as_y_tiled,
1181 uint32_t *mask_x, uint32_t *mask_y)
1182 {
1183 uint32_t tile_w_bytes, tile_h;
1184 if (map_stencil_as_y_tiled)
1185 tiling = I915_TILING_Y;
1186
1187 intel_get_tile_dims(tiling, tr_mode, cpp, &tile_w_bytes, &tile_h);
1188
1189 *mask_x = tile_w_bytes / cpp - 1;
1190 *mask_y = tile_h - 1;
1191 }
1192
1193 /**
1194 * Compute the offset (in bytes) from the start of the BO to the given x
1195 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1196 * multiples of the tile size.
1197 */
1198 uint32_t
1199 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree *mt,
1200 uint32_t x, uint32_t y,
1201 bool map_stencil_as_y_tiled)
1202 {
1203 int cpp = mt->cpp;
1204 uint32_t pitch = mt->pitch;
1205 uint32_t tiling = mt->tiling;
1206
1207 if (map_stencil_as_y_tiled) {
1208 tiling = I915_TILING_Y;
1209
1210 /* When mapping a W-tiled stencil buffer as Y-tiled, each 64-high W-tile
1211 * gets transformed into a 32-high Y-tile. Accordingly, the pitch of
1212 * the resulting surface is twice the pitch of the original miptree,
1213 * since each row in the Y-tiled view corresponds to two rows in the
1214 * actual W-tiled surface. So we need to correct the pitch before
1215 * computing the offsets.
1216 */
1217 pitch *= 2;
1218 }
1219
1220 switch (tiling) {
1221 default:
1222 unreachable("not reached");
1223 case I915_TILING_NONE:
1224 return y * pitch + x * cpp;
1225 case I915_TILING_X:
1226 assert((x % (512 / cpp)) == 0);
1227 assert((y % 8) == 0);
1228 return y * pitch + x / (512 / cpp) * 4096;
1229 case I915_TILING_Y:
1230 assert((x % (128 / cpp)) == 0);
1231 assert((y % 32) == 0);
1232 return y * pitch + x / (128 / cpp) * 4096;
1233 }
1234 }
1235
1236 /**
1237 * Rendering with tiled buffers requires that the base address of the buffer
1238 * be aligned to a page boundary. For renderbuffers, and sometimes with
1239 * textures, we may want the surface to point at a texture image level that
1240 * isn't at a page boundary.
1241 *
1242 * This function returns an appropriately-aligned base offset
1243 * according to the tiling restrictions, plus any required x/y offset
1244 * from there.
1245 */
1246 uint32_t
1247 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
1248 GLuint level, GLuint slice,
1249 uint32_t *tile_x,
1250 uint32_t *tile_y)
1251 {
1252 uint32_t x, y;
1253 uint32_t mask_x, mask_y;
1254
1255 intel_get_tile_masks(mt->tiling, mt->tr_mode, mt->cpp, false, &mask_x, &mask_y);
1256 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
1257
1258 *tile_x = x & mask_x;
1259 *tile_y = y & mask_y;
1260
1261 return intel_miptree_get_aligned_offset(mt, x & ~mask_x, y & ~mask_y, false);
1262 }
1263
1264 static void
1265 intel_miptree_copy_slice_sw(struct brw_context *brw,
1266 struct intel_mipmap_tree *dst_mt,
1267 struct intel_mipmap_tree *src_mt,
1268 int level,
1269 int slice,
1270 int width,
1271 int height)
1272 {
1273 void *src, *dst;
1274 ptrdiff_t src_stride, dst_stride;
1275 int cpp = dst_mt->cpp;
1276
1277 intel_miptree_map(brw, src_mt,
1278 level, slice,
1279 0, 0,
1280 width, height,
1281 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
1282 &src, &src_stride);
1283
1284 intel_miptree_map(brw, dst_mt,
1285 level, slice,
1286 0, 0,
1287 width, height,
1288 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
1289 BRW_MAP_DIRECT_BIT,
1290 &dst, &dst_stride);
1291
1292 DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
1293 _mesa_get_format_name(src_mt->format),
1294 src_mt, src, src_stride,
1295 _mesa_get_format_name(dst_mt->format),
1296 dst_mt, dst, dst_stride,
1297 width, height);
1298
1299 int row_size = cpp * width;
1300 if (src_stride == row_size &&
1301 dst_stride == row_size) {
1302 memcpy(dst, src, row_size * height);
1303 } else {
1304 for (int i = 0; i < height; i++) {
1305 memcpy(dst, src, row_size);
1306 dst += dst_stride;
1307 src += src_stride;
1308 }
1309 }
1310
1311 intel_miptree_unmap(brw, dst_mt, level, slice);
1312 intel_miptree_unmap(brw, src_mt, level, slice);
1313
1314 /* Don't forget to copy the stencil data over, too. We could have skipped
1315 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1316 * shuffling the two data sources in/out of temporary storage instead of
1317 * the direct mapping we get this way.
1318 */
1319 if (dst_mt->stencil_mt) {
1320 assert(src_mt->stencil_mt);
1321 intel_miptree_copy_slice_sw(brw, dst_mt->stencil_mt, src_mt->stencil_mt,
1322 level, slice, width, height);
1323 }
1324 }
1325
1326 static void
1327 intel_miptree_copy_slice(struct brw_context *brw,
1328 struct intel_mipmap_tree *dst_mt,
1329 struct intel_mipmap_tree *src_mt,
1330 int level,
1331 int face,
1332 int depth)
1333
1334 {
1335 mesa_format format = src_mt->format;
1336 uint32_t width = minify(src_mt->physical_width0, level - src_mt->first_level);
1337 uint32_t height = minify(src_mt->physical_height0, level - src_mt->first_level);
1338 int slice;
1339
1340 if (face > 0)
1341 slice = face;
1342 else
1343 slice = depth;
1344
1345 assert(depth < src_mt->level[level].depth);
1346 assert(src_mt->format == dst_mt->format);
1347
1348 if (dst_mt->compressed) {
1349 unsigned int i, j;
1350 _mesa_get_format_block_size(dst_mt->format, &i, &j);
1351 height = ALIGN_NPOT(height, j) / j;
1352 width = ALIGN_NPOT(width, i) / i;
1353 }
1354
1355 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1356 * below won't apply since we can't do the depth's Y tiling or the
1357 * stencil's W tiling in the blitter.
1358 */
1359 if (src_mt->stencil_mt) {
1360 intel_miptree_copy_slice_sw(brw,
1361 dst_mt, src_mt,
1362 level, slice,
1363 width, height);
1364 return;
1365 }
1366
1367 uint32_t dst_x, dst_y, src_x, src_y;
1368 intel_miptree_get_image_offset(dst_mt, level, slice, &dst_x, &dst_y);
1369 intel_miptree_get_image_offset(src_mt, level, slice, &src_x, &src_y);
1370
1371 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1372 _mesa_get_format_name(src_mt->format),
1373 src_mt, src_x, src_y, src_mt->pitch,
1374 _mesa_get_format_name(dst_mt->format),
1375 dst_mt, dst_x, dst_y, dst_mt->pitch,
1376 width, height);
1377
1378 if (!intel_miptree_blit(brw,
1379 src_mt, level, slice, 0, 0, false,
1380 dst_mt, level, slice, 0, 0, false,
1381 width, height, GL_COPY)) {
1382 perf_debug("miptree validate blit for %s failed\n",
1383 _mesa_get_format_name(format));
1384
1385 intel_miptree_copy_slice_sw(brw, dst_mt, src_mt, level, slice,
1386 width, height);
1387 }
1388 }
1389
1390 /**
1391 * Copies the image's current data to the given miptree, and associates that
1392 * miptree with the image.
1393 *
1394 * If \c invalidate is true, then the actual image data does not need to be
1395 * copied, but the image still needs to be associated to the new miptree (this
1396 * is set to true if we're about to clear the image).
1397 */
1398 void
1399 intel_miptree_copy_teximage(struct brw_context *brw,
1400 struct intel_texture_image *intelImage,
1401 struct intel_mipmap_tree *dst_mt,
1402 bool invalidate)
1403 {
1404 struct intel_mipmap_tree *src_mt = intelImage->mt;
1405 struct intel_texture_object *intel_obj =
1406 intel_texture_object(intelImage->base.Base.TexObject);
1407 int level = intelImage->base.Base.Level;
1408 int face = intelImage->base.Base.Face;
1409
1410 GLuint depth;
1411 if (intel_obj->base.Target == GL_TEXTURE_1D_ARRAY)
1412 depth = intelImage->base.Base.Height;
1413 else
1414 depth = intelImage->base.Base.Depth;
1415
1416 if (!invalidate) {
1417 for (int slice = 0; slice < depth; slice++) {
1418 intel_miptree_copy_slice(brw, dst_mt, src_mt, level, face, slice);
1419 }
1420 }
1421
1422 intel_miptree_reference(&intelImage->mt, dst_mt);
1423 intel_obj->needs_validate = true;
1424 }
1425
1426 static bool
1427 intel_miptree_alloc_mcs(struct brw_context *brw,
1428 struct intel_mipmap_tree *mt,
1429 GLuint num_samples)
1430 {
1431 assert(brw->gen >= 7); /* MCS only used on Gen7+ */
1432 assert(mt->mcs_mt == NULL);
1433 assert(!mt->disable_aux_buffers);
1434
1435 /* Choose the correct format for the MCS buffer. All that really matters
1436 * is that we allocate the right buffer size, since we'll always be
1437 * accessing this miptree using MCS-specific hardware mechanisms, which
1438 * infer the correct format based on num_samples.
1439 */
1440 mesa_format format;
1441 switch (num_samples) {
1442 case 2:
1443 case 4:
1444 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1445 * each sample).
1446 */
1447 format = MESA_FORMAT_R_UNORM8;
1448 break;
1449 case 8:
1450 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1451 * for each sample, plus 8 padding bits).
1452 */
1453 format = MESA_FORMAT_R_UINT32;
1454 break;
1455 case 16:
1456 /* 64 bits/pixel are required for MCS data when using 16x MSAA (4 bits
1457 * for each sample).
1458 */
1459 format = MESA_FORMAT_RG_UINT32;
1460 break;
1461 default:
1462 unreachable("Unrecognized sample count in intel_miptree_alloc_mcs");
1463 };
1464
1465 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1466 *
1467 * "The MCS surface must be stored as Tile Y."
1468 */
1469 const uint32_t mcs_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
1470 MIPTREE_LAYOUT_TILING_Y;
1471 mt->mcs_mt = miptree_create(brw,
1472 mt->target,
1473 format,
1474 mt->first_level,
1475 mt->last_level,
1476 mt->logical_width0,
1477 mt->logical_height0,
1478 mt->logical_depth0,
1479 0 /* num_samples */,
1480 mcs_flags);
1481
1482 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1483 *
1484 * When MCS buffer is enabled and bound to MSRT, it is required that it
1485 * is cleared prior to any rendering.
1486 *
1487 * Since we don't use the MCS buffer for any purpose other than rendering,
1488 * it makes sense to just clear it immediately upon allocation.
1489 *
1490 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1491 */
1492 void *data = intel_miptree_map_raw(brw, mt->mcs_mt);
1493 memset(data, 0xff, mt->mcs_mt->total_height * mt->mcs_mt->pitch);
1494 intel_miptree_unmap_raw(mt->mcs_mt);
1495 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
1496
1497 return mt->mcs_mt;
1498 }
1499
1500
1501 bool
1502 intel_miptree_alloc_non_msrt_mcs(struct brw_context *brw,
1503 struct intel_mipmap_tree *mt)
1504 {
1505 assert(mt->mcs_mt == NULL);
1506 assert(!mt->disable_aux_buffers);
1507
1508 /* The format of the MCS buffer is opaque to the driver; all that matters
1509 * is that we get its size and pitch right. We'll pretend that the format
1510 * is R32. Since an MCS tile covers 128 blocks horizontally, and a Y-tiled
1511 * R32 buffer is 32 pixels across, we'll need to scale the width down by
1512 * the block width and then a further factor of 4. Since an MCS tile
1513 * covers 256 blocks vertically, and a Y-tiled R32 buffer is 32 rows high,
1514 * we'll need to scale the height down by the block height and then a
1515 * further factor of 8.
1516 */
1517 const mesa_format format = MESA_FORMAT_R_UINT32;
1518 unsigned block_width_px;
1519 unsigned block_height;
1520 intel_get_non_msrt_mcs_alignment(mt, &block_width_px, &block_height);
1521 unsigned width_divisor = block_width_px * 4;
1522 unsigned height_divisor = block_height * 8;
1523
1524 /* The Skylake MCS is twice as tall as the Broadwell MCS.
1525 *
1526 * In pre-Skylake, each bit in the MCS contained the state of 2 cachelines
1527 * in the main surface. In Skylake, it's two bits. The extra bit
1528 * doubles the MCS height, not width, because in Skylake the MCS is always
1529 * Y-tiled.
1530 */
1531 if (brw->gen >= 9)
1532 height_divisor /= 2;
1533
1534 unsigned mcs_width =
1535 ALIGN(mt->logical_width0, width_divisor) / width_divisor;
1536 unsigned mcs_height =
1537 ALIGN(mt->logical_height0, height_divisor) / height_divisor;
1538 assert(mt->logical_depth0 == 1);
1539 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
1540 MIPTREE_LAYOUT_TILING_Y;
1541 if (brw->gen >= 8) {
1542 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
1543 }
1544 mt->mcs_mt = miptree_create(brw,
1545 mt->target,
1546 format,
1547 mt->first_level,
1548 mt->last_level,
1549 mcs_width,
1550 mcs_height,
1551 mt->logical_depth0,
1552 0 /* num_samples */,
1553 layout_flags);
1554
1555 return mt->mcs_mt;
1556 }
1557
1558
1559 /**
1560 * Helper for intel_miptree_alloc_hiz() that sets
1561 * \c mt->level[level].has_hiz. Return true if and only if
1562 * \c has_hiz was set.
1563 */
1564 static bool
1565 intel_miptree_level_enable_hiz(struct brw_context *brw,
1566 struct intel_mipmap_tree *mt,
1567 uint32_t level)
1568 {
1569 assert(mt->hiz_buf);
1570
1571 if (brw->gen >= 8 || brw->is_haswell) {
1572 uint32_t width = minify(mt->physical_width0, level);
1573 uint32_t height = minify(mt->physical_height0, level);
1574
1575 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1576 * and the height is 4 aligned. This allows our HiZ support
1577 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1578 * we can grow the width & height to allow the HiZ op to
1579 * force the proper size alignments.
1580 */
1581 if (level > 0 && ((width & 7) || (height & 3))) {
1582 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1583 return false;
1584 }
1585 }
1586
1587 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1588 mt->level[level].has_hiz = true;
1589 return true;
1590 }
1591
1592
1593 /**
1594 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1595 * buffer dimensions and allocates a bo for the hiz buffer.
1596 */
1597 static struct intel_miptree_aux_buffer *
1598 intel_gen7_hiz_buf_create(struct brw_context *brw,
1599 struct intel_mipmap_tree *mt)
1600 {
1601 unsigned z_width = mt->logical_width0;
1602 unsigned z_height = mt->logical_height0;
1603 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1604 unsigned hz_width, hz_height;
1605 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1606
1607 if (!buf)
1608 return NULL;
1609
1610 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1611 * adjustments required for Z_Height and Z_Width based on multisampling.
1612 */
1613 switch (mt->num_samples) {
1614 case 0:
1615 case 1:
1616 break;
1617 case 2:
1618 case 4:
1619 z_width *= 2;
1620 z_height *= 2;
1621 break;
1622 case 8:
1623 z_width *= 4;
1624 z_height *= 2;
1625 break;
1626 default:
1627 unreachable("unsupported sample count");
1628 }
1629
1630 const unsigned vertical_align = 8; /* 'j' in the docs */
1631 const unsigned H0 = z_height;
1632 const unsigned h0 = ALIGN(H0, vertical_align);
1633 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1634 const unsigned Z0 = z_depth;
1635
1636 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1637 hz_width = ALIGN(z_width, 16);
1638
1639 if (mt->target == GL_TEXTURE_3D) {
1640 unsigned H_i = H0;
1641 unsigned Z_i = Z0;
1642 hz_height = 0;
1643 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1644 unsigned h_i = ALIGN(H_i, vertical_align);
1645 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1646 hz_height += h_i * Z_i;
1647 H_i = minify(H_i, 1);
1648 Z_i = minify(Z_i, 1);
1649 }
1650 /* HZ_Height =
1651 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1652 */
1653 hz_height = DIV_ROUND_UP(hz_height, 2);
1654 } else {
1655 const unsigned hz_qpitch = h0 + h1 + (12 * vertical_align);
1656 if (mt->target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1657 mt->target == GL_TEXTURE_CUBE_MAP) {
1658 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth * 6/2) /8 ) * 8 */
1659 hz_height = DIV_ROUND_UP(hz_qpitch * Z0 * 6, 2 * 8) * 8;
1660 } else {
1661 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1662 hz_height = DIV_ROUND_UP(hz_qpitch * Z0, 2 * 8) * 8;
1663 }
1664 }
1665
1666 unsigned long pitch;
1667 uint32_t tiling = I915_TILING_Y;
1668 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1669 hz_width, hz_height, 1,
1670 &tiling, &pitch,
1671 BO_ALLOC_FOR_RENDER);
1672 if (!buf->bo) {
1673 free(buf);
1674 return NULL;
1675 } else if (tiling != I915_TILING_Y) {
1676 drm_intel_bo_unreference(buf->bo);
1677 free(buf);
1678 return NULL;
1679 }
1680
1681 buf->pitch = pitch;
1682
1683 return buf;
1684 }
1685
1686
1687 /**
1688 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1689 * buffer dimensions and allocates a bo for the hiz buffer.
1690 */
1691 static struct intel_miptree_aux_buffer *
1692 intel_gen8_hiz_buf_create(struct brw_context *brw,
1693 struct intel_mipmap_tree *mt)
1694 {
1695 unsigned z_width = mt->logical_width0;
1696 unsigned z_height = mt->logical_height0;
1697 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1698 unsigned hz_width, hz_height;
1699 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1700
1701 if (!buf)
1702 return NULL;
1703
1704 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1705 * adjustments required for Z_Height and Z_Width based on multisampling.
1706 */
1707 if (brw->gen < 9) {
1708 switch (mt->num_samples) {
1709 case 0:
1710 case 1:
1711 break;
1712 case 2:
1713 case 4:
1714 z_width *= 2;
1715 z_height *= 2;
1716 break;
1717 case 8:
1718 z_width *= 4;
1719 z_height *= 2;
1720 break;
1721 default:
1722 unreachable("unsupported sample count");
1723 }
1724 }
1725
1726 const unsigned vertical_align = 8; /* 'j' in the docs */
1727 const unsigned H0 = z_height;
1728 const unsigned h0 = ALIGN(H0, vertical_align);
1729 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1730 const unsigned Z0 = z_depth;
1731
1732 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1733 hz_width = ALIGN(z_width, 16);
1734
1735 unsigned H_i = H0;
1736 unsigned Z_i = Z0;
1737 unsigned sum_h_i = 0;
1738 unsigned hz_height_3d_sum = 0;
1739 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1740 unsigned i = level - mt->first_level;
1741 unsigned h_i = ALIGN(H_i, vertical_align);
1742 /* sum(i=2 to m; h_i) */
1743 if (i >= 2) {
1744 sum_h_i += h_i;
1745 }
1746 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1747 hz_height_3d_sum += h_i * Z_i;
1748 H_i = minify(H_i, 1);
1749 Z_i = minify(Z_i, 1);
1750 }
1751 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1752 buf->qpitch = h0 + MAX2(h1, sum_h_i);
1753
1754 if (mt->target == GL_TEXTURE_3D) {
1755 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1756 hz_height = DIV_ROUND_UP(hz_height_3d_sum, 2);
1757 } else {
1758 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1759 hz_height = DIV_ROUND_UP(buf->qpitch, 2 * 8) * 8 * Z0;
1760 if (mt->target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1761 mt->target == GL_TEXTURE_CUBE_MAP) {
1762 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * 6 * Z_Depth
1763 *
1764 * We can can just take our hz_height calculation from above, and
1765 * multiply by 6 for the cube map and cube map array types.
1766 */
1767 hz_height *= 6;
1768 }
1769 }
1770
1771 unsigned long pitch;
1772 uint32_t tiling = I915_TILING_Y;
1773 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1774 hz_width, hz_height, 1,
1775 &tiling, &pitch,
1776 BO_ALLOC_FOR_RENDER);
1777 if (!buf->bo) {
1778 free(buf);
1779 return NULL;
1780 } else if (tiling != I915_TILING_Y) {
1781 drm_intel_bo_unreference(buf->bo);
1782 free(buf);
1783 return NULL;
1784 }
1785
1786 buf->pitch = pitch;
1787
1788 return buf;
1789 }
1790
1791
1792 static struct intel_miptree_aux_buffer *
1793 intel_hiz_miptree_buf_create(struct brw_context *brw,
1794 struct intel_mipmap_tree *mt)
1795 {
1796 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1797 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
1798
1799 if (brw->gen == 6)
1800 layout_flags |= MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD;
1801
1802 if (!buf)
1803 return NULL;
1804
1805 layout_flags |= MIPTREE_LAYOUT_TILING_ANY;
1806 buf->mt = intel_miptree_create(brw,
1807 mt->target,
1808 mt->format,
1809 mt->first_level,
1810 mt->last_level,
1811 mt->logical_width0,
1812 mt->logical_height0,
1813 mt->logical_depth0,
1814 mt->num_samples,
1815 layout_flags);
1816 if (!buf->mt) {
1817 free(buf);
1818 return NULL;
1819 }
1820
1821 buf->bo = buf->mt->bo;
1822 buf->pitch = buf->mt->pitch;
1823 buf->qpitch = buf->mt->qpitch;
1824
1825 return buf;
1826 }
1827
1828 bool
1829 intel_miptree_wants_hiz_buffer(struct brw_context *brw,
1830 struct intel_mipmap_tree *mt)
1831 {
1832 if (!brw->has_hiz)
1833 return false;
1834
1835 if (mt->hiz_buf != NULL)
1836 return false;
1837
1838 if (mt->disable_aux_buffers)
1839 return false;
1840
1841 switch (mt->format) {
1842 case MESA_FORMAT_Z_FLOAT32:
1843 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1844 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1845 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1846 case MESA_FORMAT_Z_UNORM16:
1847 return true;
1848 default:
1849 return false;
1850 }
1851 }
1852
1853 bool
1854 intel_miptree_alloc_hiz(struct brw_context *brw,
1855 struct intel_mipmap_tree *mt)
1856 {
1857 assert(mt->hiz_buf == NULL);
1858 assert(!mt->disable_aux_buffers);
1859
1860 if (brw->gen == 7) {
1861 mt->hiz_buf = intel_gen7_hiz_buf_create(brw, mt);
1862 } else if (brw->gen >= 8) {
1863 mt->hiz_buf = intel_gen8_hiz_buf_create(brw, mt);
1864 } else {
1865 mt->hiz_buf = intel_hiz_miptree_buf_create(brw, mt);
1866 }
1867
1868 if (!mt->hiz_buf)
1869 return false;
1870
1871 /* Mark that all slices need a HiZ resolve. */
1872 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1873 if (!intel_miptree_level_enable_hiz(brw, mt, level))
1874 continue;
1875
1876 for (unsigned layer = 0; layer < mt->level[level].depth; ++layer) {
1877 struct intel_resolve_map *m = malloc(sizeof(struct intel_resolve_map));
1878 exec_node_init(&m->link);
1879 m->level = level;
1880 m->layer = layer;
1881 m->need = GEN6_HIZ_OP_HIZ_RESOLVE;
1882
1883 exec_list_push_tail(&mt->hiz_map, &m->link);
1884 }
1885 }
1886
1887 return true;
1888 }
1889
1890 /**
1891 * Does the miptree slice have hiz enabled?
1892 */
1893 bool
1894 intel_miptree_level_has_hiz(struct intel_mipmap_tree *mt, uint32_t level)
1895 {
1896 intel_miptree_check_level_layer(mt, level, 0);
1897 return mt->level[level].has_hiz;
1898 }
1899
1900 void
1901 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1902 uint32_t level,
1903 uint32_t layer)
1904 {
1905 if (!intel_miptree_level_has_hiz(mt, level))
1906 return;
1907
1908 intel_resolve_map_set(&mt->hiz_map,
1909 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
1910 }
1911
1912
1913 void
1914 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
1915 uint32_t level,
1916 uint32_t layer)
1917 {
1918 if (!intel_miptree_level_has_hiz(mt, level))
1919 return;
1920
1921 intel_resolve_map_set(&mt->hiz_map,
1922 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
1923 }
1924
1925 void
1926 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
1927 uint32_t level)
1928 {
1929 uint32_t layer;
1930 uint32_t end_layer = mt->level[level].depth;
1931
1932 for (layer = 0; layer < end_layer; layer++) {
1933 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
1934 }
1935 }
1936
1937 static bool
1938 intel_miptree_slice_resolve(struct brw_context *brw,
1939 struct intel_mipmap_tree *mt,
1940 uint32_t level,
1941 uint32_t layer,
1942 enum gen6_hiz_op need)
1943 {
1944 intel_miptree_check_level_layer(mt, level, layer);
1945
1946 struct intel_resolve_map *item =
1947 intel_resolve_map_get(&mt->hiz_map, level, layer);
1948
1949 if (!item || item->need != need)
1950 return false;
1951
1952 intel_hiz_exec(brw, mt, level, layer, need);
1953 intel_resolve_map_remove(item);
1954 return true;
1955 }
1956
1957 bool
1958 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
1959 struct intel_mipmap_tree *mt,
1960 uint32_t level,
1961 uint32_t layer)
1962 {
1963 return intel_miptree_slice_resolve(brw, mt, level, layer,
1964 GEN6_HIZ_OP_HIZ_RESOLVE);
1965 }
1966
1967 bool
1968 intel_miptree_slice_resolve_depth(struct brw_context *brw,
1969 struct intel_mipmap_tree *mt,
1970 uint32_t level,
1971 uint32_t layer)
1972 {
1973 return intel_miptree_slice_resolve(brw, mt, level, layer,
1974 GEN6_HIZ_OP_DEPTH_RESOLVE);
1975 }
1976
1977 static bool
1978 intel_miptree_all_slices_resolve(struct brw_context *brw,
1979 struct intel_mipmap_tree *mt,
1980 enum gen6_hiz_op need)
1981 {
1982 bool did_resolve = false;
1983
1984 foreach_list_typed_safe(struct intel_resolve_map, map, link, &mt->hiz_map) {
1985 if (map->need != need)
1986 continue;
1987
1988 intel_hiz_exec(brw, mt, map->level, map->layer, need);
1989 intel_resolve_map_remove(map);
1990 did_resolve = true;
1991 }
1992
1993 return did_resolve;
1994 }
1995
1996 bool
1997 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
1998 struct intel_mipmap_tree *mt)
1999 {
2000 return intel_miptree_all_slices_resolve(brw, mt,
2001 GEN6_HIZ_OP_HIZ_RESOLVE);
2002 }
2003
2004 bool
2005 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
2006 struct intel_mipmap_tree *mt)
2007 {
2008 return intel_miptree_all_slices_resolve(brw, mt,
2009 GEN6_HIZ_OP_DEPTH_RESOLVE);
2010 }
2011
2012
2013 void
2014 intel_miptree_resolve_color(struct brw_context *brw,
2015 struct intel_mipmap_tree *mt,
2016 int flags)
2017 {
2018 (void)flags;
2019
2020 switch (mt->fast_clear_state) {
2021 case INTEL_FAST_CLEAR_STATE_NO_MCS:
2022 case INTEL_FAST_CLEAR_STATE_RESOLVED:
2023 /* No resolve needed */
2024 break;
2025 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
2026 case INTEL_FAST_CLEAR_STATE_CLEAR:
2027 /* Fast color clear resolves only make sense for non-MSAA buffers. */
2028 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE)
2029 brw_meta_resolve_color(brw, mt);
2030 break;
2031 }
2032 }
2033
2034
2035 /**
2036 * Make it possible to share the BO backing the given miptree with another
2037 * process or another miptree.
2038 *
2039 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2040 * then discard the MCS buffer, if present. We also set the fast_clear_state
2041 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
2042 * allocated in the future.
2043 */
2044 void
2045 intel_miptree_make_shareable(struct brw_context *brw,
2046 struct intel_mipmap_tree *mt)
2047 {
2048 /* MCS buffers are also used for multisample buffers, but we can't resolve
2049 * away a multisample MCS buffer because it's an integral part of how the
2050 * pixel data is stored. Fortunately this code path should never be
2051 * reached for multisample buffers.
2052 */
2053 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE);
2054
2055 if (mt->mcs_mt) {
2056 intel_miptree_resolve_color(brw, mt, 0);
2057 intel_miptree_release(&mt->mcs_mt);
2058 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
2059 }
2060 }
2061
2062
2063 /**
2064 * \brief Get pointer offset into stencil buffer.
2065 *
2066 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2067 * must decode the tile's layout in software.
2068 *
2069 * See
2070 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2071 * Format.
2072 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2073 *
2074 * Even though the returned offset is always positive, the return type is
2075 * signed due to
2076 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2077 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2078 */
2079 static intptr_t
2080 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2081 {
2082 uint32_t tile_size = 4096;
2083 uint32_t tile_width = 64;
2084 uint32_t tile_height = 64;
2085 uint32_t row_size = 64 * stride;
2086
2087 uint32_t tile_x = x / tile_width;
2088 uint32_t tile_y = y / tile_height;
2089
2090 /* The byte's address relative to the tile's base addres. */
2091 uint32_t byte_x = x % tile_width;
2092 uint32_t byte_y = y % tile_height;
2093
2094 uintptr_t u = tile_y * row_size
2095 + tile_x * tile_size
2096 + 512 * (byte_x / 8)
2097 + 64 * (byte_y / 8)
2098 + 32 * ((byte_y / 4) % 2)
2099 + 16 * ((byte_x / 4) % 2)
2100 + 8 * ((byte_y / 2) % 2)
2101 + 4 * ((byte_x / 2) % 2)
2102 + 2 * (byte_y % 2)
2103 + 1 * (byte_x % 2);
2104
2105 if (swizzled) {
2106 /* adjust for bit6 swizzling */
2107 if (((byte_x / 8) % 2) == 1) {
2108 if (((byte_y / 8) % 2) == 0) {
2109 u += 64;
2110 } else {
2111 u -= 64;
2112 }
2113 }
2114 }
2115
2116 return u;
2117 }
2118
2119 void
2120 intel_miptree_updownsample(struct brw_context *brw,
2121 struct intel_mipmap_tree *src,
2122 struct intel_mipmap_tree *dst)
2123 {
2124 if (brw->gen < 8) {
2125 brw_blorp_blit_miptrees(brw,
2126 src, 0 /* level */, 0 /* layer */, src->format,
2127 dst, 0 /* level */, 0 /* layer */, dst->format,
2128 0, 0,
2129 src->logical_width0, src->logical_height0,
2130 0, 0,
2131 dst->logical_width0, dst->logical_height0,
2132 GL_NEAREST, false, false /*mirror x, y*/);
2133 } else if (src->format == MESA_FORMAT_S_UINT8) {
2134 brw_meta_stencil_updownsample(brw, src, dst);
2135 } else {
2136 brw_meta_updownsample(brw, src, dst);
2137 }
2138
2139 if (src->stencil_mt) {
2140 if (brw->gen >= 8) {
2141 brw_meta_stencil_updownsample(brw, src->stencil_mt, dst);
2142 return;
2143 }
2144
2145 brw_blorp_blit_miptrees(brw,
2146 src->stencil_mt, 0 /* level */, 0 /* layer */,
2147 src->stencil_mt->format,
2148 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2149 dst->stencil_mt->format,
2150 0, 0,
2151 src->logical_width0, src->logical_height0,
2152 0, 0,
2153 dst->logical_width0, dst->logical_height0,
2154 GL_NEAREST, false, false /*mirror x, y*/);
2155 }
2156 }
2157
2158 void *
2159 intel_miptree_map_raw(struct brw_context *brw, struct intel_mipmap_tree *mt)
2160 {
2161 /* CPU accesses to color buffers don't understand fast color clears, so
2162 * resolve any pending fast color clears before we map.
2163 */
2164 intel_miptree_resolve_color(brw, mt, 0);
2165
2166 drm_intel_bo *bo = mt->bo;
2167
2168 if (drm_intel_bo_references(brw->batch.bo, bo))
2169 intel_batchbuffer_flush(brw);
2170
2171 if (mt->tiling != I915_TILING_NONE)
2172 brw_bo_map_gtt(brw, bo, "miptree");
2173 else
2174 brw_bo_map(brw, bo, true, "miptree");
2175
2176 return bo->virtual;
2177 }
2178
2179 void
2180 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2181 {
2182 drm_intel_bo_unmap(mt->bo);
2183 }
2184
2185 static void
2186 intel_miptree_map_gtt(struct brw_context *brw,
2187 struct intel_mipmap_tree *mt,
2188 struct intel_miptree_map *map,
2189 unsigned int level, unsigned int slice)
2190 {
2191 unsigned int bw, bh;
2192 void *base;
2193 unsigned int image_x, image_y;
2194 intptr_t x = map->x;
2195 intptr_t y = map->y;
2196
2197 /* For compressed formats, the stride is the number of bytes per
2198 * row of blocks. intel_miptree_get_image_offset() already does
2199 * the divide.
2200 */
2201 _mesa_get_format_block_size(mt->format, &bw, &bh);
2202 assert(y % bh == 0);
2203 assert(x % bw == 0);
2204 y /= bh;
2205 x /= bw;
2206
2207 base = intel_miptree_map_raw(brw, mt) + mt->offset;
2208
2209 if (base == NULL)
2210 map->ptr = NULL;
2211 else {
2212 /* Note that in the case of cube maps, the caller must have passed the
2213 * slice number referencing the face.
2214 */
2215 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2216 x += image_x;
2217 y += image_y;
2218
2219 map->stride = mt->pitch;
2220 map->ptr = base + y * map->stride + x * mt->cpp;
2221 }
2222
2223 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2224 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2225 map->x, map->y, map->w, map->h,
2226 mt, _mesa_get_format_name(mt->format),
2227 x, y, map->ptr, map->stride);
2228 }
2229
2230 static void
2231 intel_miptree_unmap_gtt(struct intel_mipmap_tree *mt)
2232 {
2233 intel_miptree_unmap_raw(mt);
2234 }
2235
2236 static void
2237 intel_miptree_map_blit(struct brw_context *brw,
2238 struct intel_mipmap_tree *mt,
2239 struct intel_miptree_map *map,
2240 unsigned int level, unsigned int slice)
2241 {
2242 map->linear_mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
2243 /* first_level */ 0,
2244 /* last_level */ 0,
2245 map->w, map->h, 1,
2246 /* samples */ 0,
2247 MIPTREE_LAYOUT_TILING_NONE);
2248
2249 if (!map->linear_mt) {
2250 fprintf(stderr, "Failed to allocate blit temporary\n");
2251 goto fail;
2252 }
2253 map->stride = map->linear_mt->pitch;
2254
2255 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2256 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2257 * invalidate is set, since we'll be writing the whole rectangle from our
2258 * temporary buffer back out.
2259 */
2260 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2261 if (!intel_miptree_blit(brw,
2262 mt, level, slice,
2263 map->x, map->y, false,
2264 map->linear_mt, 0, 0,
2265 0, 0, false,
2266 map->w, map->h, GL_COPY)) {
2267 fprintf(stderr, "Failed to blit\n");
2268 goto fail;
2269 }
2270 }
2271
2272 map->ptr = intel_miptree_map_raw(brw, map->linear_mt);
2273
2274 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2275 map->x, map->y, map->w, map->h,
2276 mt, _mesa_get_format_name(mt->format),
2277 level, slice, map->ptr, map->stride);
2278
2279 return;
2280
2281 fail:
2282 intel_miptree_release(&map->linear_mt);
2283 map->ptr = NULL;
2284 map->stride = 0;
2285 }
2286
2287 static void
2288 intel_miptree_unmap_blit(struct brw_context *brw,
2289 struct intel_mipmap_tree *mt,
2290 struct intel_miptree_map *map,
2291 unsigned int level,
2292 unsigned int slice)
2293 {
2294 struct gl_context *ctx = &brw->ctx;
2295
2296 intel_miptree_unmap_raw(map->linear_mt);
2297
2298 if (map->mode & GL_MAP_WRITE_BIT) {
2299 bool ok = intel_miptree_blit(brw,
2300 map->linear_mt, 0, 0,
2301 0, 0, false,
2302 mt, level, slice,
2303 map->x, map->y, false,
2304 map->w, map->h, GL_COPY);
2305 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
2306 }
2307
2308 intel_miptree_release(&map->linear_mt);
2309 }
2310
2311 /**
2312 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2313 */
2314 #if defined(USE_SSE41)
2315 static void
2316 intel_miptree_map_movntdqa(struct brw_context *brw,
2317 struct intel_mipmap_tree *mt,
2318 struct intel_miptree_map *map,
2319 unsigned int level, unsigned int slice)
2320 {
2321 assert(map->mode & GL_MAP_READ_BIT);
2322 assert(!(map->mode & GL_MAP_WRITE_BIT));
2323
2324 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2325 map->x, map->y, map->w, map->h,
2326 mt, _mesa_get_format_name(mt->format),
2327 level, slice, map->ptr, map->stride);
2328
2329 /* Map the original image */
2330 uint32_t image_x;
2331 uint32_t image_y;
2332 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2333 image_x += map->x;
2334 image_y += map->y;
2335
2336 void *src = intel_miptree_map_raw(brw, mt);
2337 if (!src)
2338 return;
2339 src += image_y * mt->pitch;
2340 src += image_x * mt->cpp;
2341
2342 /* Due to the pixel offsets for the particular image being mapped, our
2343 * src pointer may not be 16-byte aligned. However, if the pitch is
2344 * divisible by 16, then the amount by which it's misaligned will remain
2345 * consistent from row to row.
2346 */
2347 assert((mt->pitch % 16) == 0);
2348 const int misalignment = ((uintptr_t) src) & 15;
2349
2350 /* Create an untiled temporary buffer for the mapping. */
2351 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
2352
2353 map->stride = ALIGN(misalignment + width_bytes, 16);
2354
2355 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
2356 /* Offset the destination so it has the same misalignment as src. */
2357 map->ptr = map->buffer + misalignment;
2358
2359 assert((((uintptr_t) map->ptr) & 15) == misalignment);
2360
2361 for (uint32_t y = 0; y < map->h; y++) {
2362 void *dst_ptr = map->ptr + y * map->stride;
2363 void *src_ptr = src + y * mt->pitch;
2364
2365 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
2366 }
2367
2368 intel_miptree_unmap_raw(mt);
2369 }
2370
2371 static void
2372 intel_miptree_unmap_movntdqa(struct brw_context *brw,
2373 struct intel_mipmap_tree *mt,
2374 struct intel_miptree_map *map,
2375 unsigned int level,
2376 unsigned int slice)
2377 {
2378 _mesa_align_free(map->buffer);
2379 map->buffer = NULL;
2380 map->ptr = NULL;
2381 }
2382 #endif
2383
2384 static void
2385 intel_miptree_map_s8(struct brw_context *brw,
2386 struct intel_mipmap_tree *mt,
2387 struct intel_miptree_map *map,
2388 unsigned int level, unsigned int slice)
2389 {
2390 map->stride = map->w;
2391 map->buffer = map->ptr = malloc(map->stride * map->h);
2392 if (!map->buffer)
2393 return;
2394
2395 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2396 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2397 * invalidate is set, since we'll be writing the whole rectangle from our
2398 * temporary buffer back out.
2399 */
2400 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2401 uint8_t *untiled_s8_map = map->ptr;
2402 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2403 unsigned int image_x, image_y;
2404
2405 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2406
2407 for (uint32_t y = 0; y < map->h; y++) {
2408 for (uint32_t x = 0; x < map->w; x++) {
2409 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2410 x + image_x + map->x,
2411 y + image_y + map->y,
2412 brw->has_swizzling);
2413 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
2414 }
2415 }
2416
2417 intel_miptree_unmap_raw(mt);
2418
2419 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
2420 map->x, map->y, map->w, map->h,
2421 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
2422 } else {
2423 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2424 map->x, map->y, map->w, map->h,
2425 mt, map->ptr, map->stride);
2426 }
2427 }
2428
2429 static void
2430 intel_miptree_unmap_s8(struct brw_context *brw,
2431 struct intel_mipmap_tree *mt,
2432 struct intel_miptree_map *map,
2433 unsigned int level,
2434 unsigned int slice)
2435 {
2436 if (map->mode & GL_MAP_WRITE_BIT) {
2437 unsigned int image_x, image_y;
2438 uint8_t *untiled_s8_map = map->ptr;
2439 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2440
2441 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2442
2443 for (uint32_t y = 0; y < map->h; y++) {
2444 for (uint32_t x = 0; x < map->w; x++) {
2445 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2446 x + map->x,
2447 y + map->y,
2448 brw->has_swizzling);
2449 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
2450 }
2451 }
2452
2453 intel_miptree_unmap_raw(mt);
2454 }
2455
2456 free(map->buffer);
2457 }
2458
2459 static void
2460 intel_miptree_map_etc(struct brw_context *brw,
2461 struct intel_mipmap_tree *mt,
2462 struct intel_miptree_map *map,
2463 unsigned int level,
2464 unsigned int slice)
2465 {
2466 assert(mt->etc_format != MESA_FORMAT_NONE);
2467 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
2468 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
2469 }
2470
2471 assert(map->mode & GL_MAP_WRITE_BIT);
2472 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
2473
2474 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
2475 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
2476 map->w, map->h, 1));
2477 map->ptr = map->buffer;
2478 }
2479
2480 static void
2481 intel_miptree_unmap_etc(struct brw_context *brw,
2482 struct intel_mipmap_tree *mt,
2483 struct intel_miptree_map *map,
2484 unsigned int level,
2485 unsigned int slice)
2486 {
2487 uint32_t image_x;
2488 uint32_t image_y;
2489 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2490
2491 image_x += map->x;
2492 image_y += map->y;
2493
2494 uint8_t *dst = intel_miptree_map_raw(brw, mt)
2495 + image_y * mt->pitch
2496 + image_x * mt->cpp;
2497
2498 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
2499 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
2500 map->ptr, map->stride,
2501 map->w, map->h);
2502 else
2503 _mesa_unpack_etc2_format(dst, mt->pitch,
2504 map->ptr, map->stride,
2505 map->w, map->h, mt->etc_format);
2506
2507 intel_miptree_unmap_raw(mt);
2508 free(map->buffer);
2509 }
2510
2511 /**
2512 * Mapping function for packed depth/stencil miptrees backed by real separate
2513 * miptrees for depth and stencil.
2514 *
2515 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2516 * separate from the depth buffer. Yet at the GL API level, we have to expose
2517 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2518 * be able to map that memory for texture storage and glReadPixels-type
2519 * operations. We give Mesa core that access by mallocing a temporary and
2520 * copying the data between the actual backing store and the temporary.
2521 */
2522 static void
2523 intel_miptree_map_depthstencil(struct brw_context *brw,
2524 struct intel_mipmap_tree *mt,
2525 struct intel_miptree_map *map,
2526 unsigned int level, unsigned int slice)
2527 {
2528 struct intel_mipmap_tree *z_mt = mt;
2529 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2530 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2531 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
2532
2533 map->stride = map->w * packed_bpp;
2534 map->buffer = map->ptr = malloc(map->stride * map->h);
2535 if (!map->buffer)
2536 return;
2537
2538 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2539 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2540 * invalidate is set, since we'll be writing the whole rectangle from our
2541 * temporary buffer back out.
2542 */
2543 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2544 uint32_t *packed_map = map->ptr;
2545 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2546 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2547 unsigned int s_image_x, s_image_y;
2548 unsigned int z_image_x, z_image_y;
2549
2550 intel_miptree_get_image_offset(s_mt, level, slice,
2551 &s_image_x, &s_image_y);
2552 intel_miptree_get_image_offset(z_mt, level, slice,
2553 &z_image_x, &z_image_y);
2554
2555 for (uint32_t y = 0; y < map->h; y++) {
2556 for (uint32_t x = 0; x < map->w; x++) {
2557 int map_x = map->x + x, map_y = map->y + y;
2558 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2559 map_x + s_image_x,
2560 map_y + s_image_y,
2561 brw->has_swizzling);
2562 ptrdiff_t z_offset = ((map_y + z_image_y) *
2563 (z_mt->pitch / 4) +
2564 (map_x + z_image_x));
2565 uint8_t s = s_map[s_offset];
2566 uint32_t z = z_map[z_offset];
2567
2568 if (map_z32f_x24s8) {
2569 packed_map[(y * map->w + x) * 2 + 0] = z;
2570 packed_map[(y * map->w + x) * 2 + 1] = s;
2571 } else {
2572 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
2573 }
2574 }
2575 }
2576
2577 intel_miptree_unmap_raw(s_mt);
2578 intel_miptree_unmap_raw(z_mt);
2579
2580 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
2581 __func__,
2582 map->x, map->y, map->w, map->h,
2583 z_mt, map->x + z_image_x, map->y + z_image_y,
2584 s_mt, map->x + s_image_x, map->y + s_image_y,
2585 map->ptr, map->stride);
2586 } else {
2587 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2588 map->x, map->y, map->w, map->h,
2589 mt, map->ptr, map->stride);
2590 }
2591 }
2592
2593 static void
2594 intel_miptree_unmap_depthstencil(struct brw_context *brw,
2595 struct intel_mipmap_tree *mt,
2596 struct intel_miptree_map *map,
2597 unsigned int level,
2598 unsigned int slice)
2599 {
2600 struct intel_mipmap_tree *z_mt = mt;
2601 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2602 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2603
2604 if (map->mode & GL_MAP_WRITE_BIT) {
2605 uint32_t *packed_map = map->ptr;
2606 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2607 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2608 unsigned int s_image_x, s_image_y;
2609 unsigned int z_image_x, z_image_y;
2610
2611 intel_miptree_get_image_offset(s_mt, level, slice,
2612 &s_image_x, &s_image_y);
2613 intel_miptree_get_image_offset(z_mt, level, slice,
2614 &z_image_x, &z_image_y);
2615
2616 for (uint32_t y = 0; y < map->h; y++) {
2617 for (uint32_t x = 0; x < map->w; x++) {
2618 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2619 x + s_image_x + map->x,
2620 y + s_image_y + map->y,
2621 brw->has_swizzling);
2622 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
2623 (z_mt->pitch / 4) +
2624 (x + z_image_x + map->x));
2625
2626 if (map_z32f_x24s8) {
2627 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
2628 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
2629 } else {
2630 uint32_t packed = packed_map[y * map->w + x];
2631 s_map[s_offset] = packed >> 24;
2632 z_map[z_offset] = packed;
2633 }
2634 }
2635 }
2636
2637 intel_miptree_unmap_raw(s_mt);
2638 intel_miptree_unmap_raw(z_mt);
2639
2640 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2641 __func__,
2642 map->x, map->y, map->w, map->h,
2643 z_mt, _mesa_get_format_name(z_mt->format),
2644 map->x + z_image_x, map->y + z_image_y,
2645 s_mt, map->x + s_image_x, map->y + s_image_y,
2646 map->ptr, map->stride);
2647 }
2648
2649 free(map->buffer);
2650 }
2651
2652 /**
2653 * Create and attach a map to the miptree at (level, slice). Return the
2654 * attached map.
2655 */
2656 static struct intel_miptree_map*
2657 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
2658 unsigned int level,
2659 unsigned int slice,
2660 unsigned int x,
2661 unsigned int y,
2662 unsigned int w,
2663 unsigned int h,
2664 GLbitfield mode)
2665 {
2666 struct intel_miptree_map *map = calloc(1, sizeof(*map));
2667
2668 if (!map)
2669 return NULL;
2670
2671 assert(mt->level[level].slice[slice].map == NULL);
2672 mt->level[level].slice[slice].map = map;
2673
2674 map->mode = mode;
2675 map->x = x;
2676 map->y = y;
2677 map->w = w;
2678 map->h = h;
2679
2680 return map;
2681 }
2682
2683 /**
2684 * Release the map at (level, slice).
2685 */
2686 static void
2687 intel_miptree_release_map(struct intel_mipmap_tree *mt,
2688 unsigned int level,
2689 unsigned int slice)
2690 {
2691 struct intel_miptree_map **map;
2692
2693 map = &mt->level[level].slice[slice].map;
2694 free(*map);
2695 *map = NULL;
2696 }
2697
2698 static bool
2699 can_blit_slice(struct intel_mipmap_tree *mt,
2700 unsigned int level, unsigned int slice)
2701 {
2702 uint32_t image_x;
2703 uint32_t image_y;
2704 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2705 if (image_x >= 32768 || image_y >= 32768)
2706 return false;
2707
2708 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2709 if (mt->pitch >= 32768)
2710 return false;
2711
2712 return true;
2713 }
2714
2715 static bool
2716 use_intel_mipree_map_blit(struct brw_context *brw,
2717 struct intel_mipmap_tree *mt,
2718 GLbitfield mode,
2719 unsigned int level,
2720 unsigned int slice)
2721 {
2722 if (brw->has_llc &&
2723 /* It's probably not worth swapping to the blit ring because of
2724 * all the overhead involved. But, we must use blitter for the
2725 * surfaces with INTEL_MIPTREE_TRMODE_{YF,YS}.
2726 */
2727 (!(mode & GL_MAP_WRITE_BIT) ||
2728 mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE) &&
2729 !mt->compressed &&
2730 (mt->tiling == I915_TILING_X ||
2731 /* Prior to Sandybridge, the blitter can't handle Y tiling */
2732 (brw->gen >= 6 && mt->tiling == I915_TILING_Y) ||
2733 /* Fast copy blit on skl+ supports all tiling formats. */
2734 brw->gen >= 9) &&
2735 can_blit_slice(mt, level, slice))
2736 return true;
2737
2738 if (mt->tiling != I915_TILING_NONE &&
2739 mt->bo->size >= brw->max_gtt_map_object_size) {
2740 assert(can_blit_slice(mt, level, slice));
2741 return true;
2742 }
2743
2744 return false;
2745 }
2746
2747 /**
2748 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
2749 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
2750 * arithmetic overflow.
2751 *
2752 * If you call this function and use \a out_stride, then you're doing pointer
2753 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
2754 * bugs. The caller must still take care to avoid 32-bit overflow errors in
2755 * all arithmetic expressions that contain buffer offsets and pixel sizes,
2756 * which usually have type uint32_t or GLuint.
2757 */
2758 void
2759 intel_miptree_map(struct brw_context *brw,
2760 struct intel_mipmap_tree *mt,
2761 unsigned int level,
2762 unsigned int slice,
2763 unsigned int x,
2764 unsigned int y,
2765 unsigned int w,
2766 unsigned int h,
2767 GLbitfield mode,
2768 void **out_ptr,
2769 ptrdiff_t *out_stride)
2770 {
2771 struct intel_miptree_map *map;
2772
2773 assert(mt->num_samples <= 1);
2774
2775 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
2776 if (!map){
2777 *out_ptr = NULL;
2778 *out_stride = 0;
2779 return;
2780 }
2781
2782 intel_miptree_slice_resolve_depth(brw, mt, level, slice);
2783 if (map->mode & GL_MAP_WRITE_BIT) {
2784 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
2785 }
2786
2787 if (mt->format == MESA_FORMAT_S_UINT8) {
2788 intel_miptree_map_s8(brw, mt, map, level, slice);
2789 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2790 !(mode & BRW_MAP_DIRECT_BIT)) {
2791 intel_miptree_map_etc(brw, mt, map, level, slice);
2792 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
2793 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
2794 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
2795 intel_miptree_map_blit(brw, mt, map, level, slice);
2796 #if defined(USE_SSE41)
2797 } else if (!(mode & GL_MAP_WRITE_BIT) &&
2798 !mt->compressed && cpu_has_sse4_1 &&
2799 (mt->pitch % 16 == 0)) {
2800 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
2801 #endif
2802 } else {
2803 /* intel_miptree_map_gtt() doesn't support surfaces with Yf/Ys tiling. */
2804 assert(mt->tr_mode == INTEL_MIPTREE_TRMODE_NONE);
2805 intel_miptree_map_gtt(brw, mt, map, level, slice);
2806 }
2807
2808 *out_ptr = map->ptr;
2809 *out_stride = map->stride;
2810
2811 if (map->ptr == NULL)
2812 intel_miptree_release_map(mt, level, slice);
2813 }
2814
2815 void
2816 intel_miptree_unmap(struct brw_context *brw,
2817 struct intel_mipmap_tree *mt,
2818 unsigned int level,
2819 unsigned int slice)
2820 {
2821 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
2822
2823 assert(mt->num_samples <= 1);
2824
2825 if (!map)
2826 return;
2827
2828 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
2829 mt, _mesa_get_format_name(mt->format), level, slice);
2830
2831 if (mt->format == MESA_FORMAT_S_UINT8) {
2832 intel_miptree_unmap_s8(brw, mt, map, level, slice);
2833 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2834 !(map->mode & BRW_MAP_DIRECT_BIT)) {
2835 intel_miptree_unmap_etc(brw, mt, map, level, slice);
2836 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
2837 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
2838 } else if (map->linear_mt) {
2839 intel_miptree_unmap_blit(brw, mt, map, level, slice);
2840 #if defined(USE_SSE41)
2841 } else if (map->buffer && cpu_has_sse4_1) {
2842 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
2843 #endif
2844 } else {
2845 intel_miptree_unmap_gtt(mt);
2846 }
2847
2848 intel_miptree_release_map(mt, level, slice);
2849 }