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