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