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