i965: Delete the intel_regions.c code.
[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
50 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
51
52 /**
53 * Determine which MSAA layout should be used by the MSAA surface being
54 * created, based on the chip generation and the surface type.
55 */
56 static enum intel_msaa_layout
57 compute_msaa_layout(struct brw_context *brw, mesa_format format, GLenum target)
58 {
59 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
60 if (brw->gen < 7)
61 return INTEL_MSAA_LAYOUT_IMS;
62
63 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
64 switch (_mesa_get_format_base_format(format)) {
65 case GL_DEPTH_COMPONENT:
66 case GL_STENCIL_INDEX:
67 case GL_DEPTH_STENCIL:
68 return INTEL_MSAA_LAYOUT_IMS;
69 default:
70 /* Disable MCS on Broadwell for now. We can enable it once things
71 * are working without it.
72 */
73 if (brw->gen >= 8) {
74 perf_debug("Missing CMS support on Broadwell.\n");
75 return INTEL_MSAA_LAYOUT_UMS;
76 }
77
78 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
79 *
80 * This field must be set to 0 for all SINT MSRTs when all RT channels
81 * are not written
82 *
83 * In practice this means that we have to disable MCS for all signed
84 * integer MSAA buffers. The alternative, to disable MCS only when one
85 * of the render target channels is disabled, is impractical because it
86 * would require converting between CMS and UMS MSAA layouts on the fly,
87 * which is expensive.
88 */
89 if (_mesa_get_format_datatype(format) == GL_INT) {
90 /* TODO: is this workaround needed for future chipsets? */
91 assert(brw->gen == 7);
92 return INTEL_MSAA_LAYOUT_UMS;
93 } else {
94 return INTEL_MSAA_LAYOUT_CMS;
95 }
96 }
97 }
98
99
100 /**
101 * For single-sampled render targets ("non-MSRT"), the MCS buffer is a
102 * scaled-down bitfield representation of the color buffer which is capable of
103 * recording when blocks of the color buffer are equal to the clear value.
104 * This function returns the block size that will be used by the MCS buffer
105 * corresponding to a certain color miptree.
106 *
107 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
108 * beneath the "Fast Color Clear" bullet (p327):
109 *
110 * The following table describes the RT alignment
111 *
112 * Pixels Lines
113 * TiledY RT CL
114 * bpp
115 * 32 8 4
116 * 64 4 4
117 * 128 2 4
118 * TiledX RT CL
119 * bpp
120 * 32 16 2
121 * 64 8 2
122 * 128 4 2
123 *
124 * This alignment has the following uses:
125 *
126 * - For figuring out the size of the MCS buffer. Each 4k tile in the MCS
127 * buffer contains 128 blocks horizontally and 256 blocks vertically.
128 *
129 * - For figuring out alignment restrictions for a fast clear operation. Fast
130 * clear operations must always clear aligned multiples of 16 blocks
131 * horizontally and 32 blocks vertically.
132 *
133 * - For scaling down the coordinates sent through the render pipeline during
134 * a fast clear. X coordinates must be scaled down by 8 times the block
135 * width, and Y coordinates by 16 times the block height.
136 *
137 * - For scaling down the coordinates sent through the render pipeline during
138 * a "Render Target Resolve" operation. X coordinates must be scaled down
139 * by half the block width, and Y coordinates by half the block height.
140 */
141 void
142 intel_get_non_msrt_mcs_alignment(struct brw_context *brw,
143 struct intel_mipmap_tree *mt,
144 unsigned *width_px, unsigned *height)
145 {
146 switch (mt->tiling) {
147 default:
148 assert(!"Non-MSRT MCS requires X or Y tiling");
149 /* In release builds, fall through */
150 case I915_TILING_Y:
151 *width_px = 32 / mt->cpp;
152 *height = 4;
153 break;
154 case I915_TILING_X:
155 *width_px = 64 / mt->cpp;
156 *height = 2;
157 }
158 }
159
160
161 /**
162 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
163 * can be used.
164 *
165 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
166 * beneath the "Fast Color Clear" bullet (p326):
167 *
168 * - Support is limited to tiled render targets.
169 * - Support is for non-mip-mapped and non-array surface types only.
170 *
171 * And then later, on p327:
172 *
173 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
174 * 64bpp, and 128bpp.
175 */
176 bool
177 intel_is_non_msrt_mcs_buffer_supported(struct brw_context *brw,
178 struct intel_mipmap_tree *mt)
179 {
180 /* MCS support does not exist prior to Gen7 */
181 if (brw->gen < 7 || brw->gen >= 8)
182 return false;
183
184 /* MCS is only supported for color buffers */
185 switch (_mesa_get_format_base_format(mt->format)) {
186 case GL_DEPTH_COMPONENT:
187 case GL_DEPTH_STENCIL:
188 case GL_STENCIL_INDEX:
189 return false;
190 }
191
192 if (mt->tiling != I915_TILING_X &&
193 mt->tiling != I915_TILING_Y)
194 return false;
195 if (mt->cpp != 4 && mt->cpp != 8 && mt->cpp != 16)
196 return false;
197 if (mt->first_level != 0 || mt->last_level != 0)
198 return false;
199 if (mt->physical_depth0 != 1)
200 return false;
201
202 /* There's no point in using an MCS buffer if the surface isn't in a
203 * renderable format.
204 */
205 if (!brw->format_supported_as_render_target[mt->format])
206 return false;
207
208 return true;
209 }
210
211
212 /**
213 * Determine depth format corresponding to a depth+stencil format,
214 * for separate stencil.
215 */
216 mesa_format
217 intel_depth_format_for_depthstencil_format(mesa_format format) {
218 switch (format) {
219 case MESA_FORMAT_Z24_UNORM_S8_UINT:
220 return MESA_FORMAT_Z24_UNORM_X8_UINT;
221 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
222 return MESA_FORMAT_Z_FLOAT32;
223 default:
224 return format;
225 }
226 }
227
228
229 /**
230 * @param for_bo Indicates that the caller is
231 * intel_miptree_create_for_bo(). If true, then do not create
232 * \c stencil_mt.
233 */
234 struct intel_mipmap_tree *
235 intel_miptree_create_layout(struct brw_context *brw,
236 GLenum target,
237 mesa_format format,
238 GLuint first_level,
239 GLuint last_level,
240 GLuint width0,
241 GLuint height0,
242 GLuint depth0,
243 bool for_bo,
244 GLuint num_samples)
245 {
246 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
247 if (!mt)
248 return NULL;
249
250 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __FUNCTION__,
251 _mesa_lookup_enum_by_nr(target),
252 _mesa_get_format_name(format),
253 first_level, last_level, depth0, mt);
254
255 mt->target = target;
256 mt->format = format;
257 mt->first_level = first_level;
258 mt->last_level = last_level;
259 mt->logical_width0 = width0;
260 mt->logical_height0 = height0;
261 mt->logical_depth0 = depth0;
262 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
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 4:
1255 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1256 * each sample).
1257 */
1258 format = MESA_FORMAT_R_UNORM8;
1259 break;
1260 case 8:
1261 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1262 * for each sample, plus 8 padding bits).
1263 */
1264 format = MESA_FORMAT_R_UINT32;
1265 break;
1266 default:
1267 assert(!"Unrecognized sample count in intel_miptree_alloc_mcs");
1268 return false;
1269 };
1270
1271 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1272 *
1273 * "The MCS surface must be stored as Tile Y."
1274 */
1275 mt->mcs_mt = intel_miptree_create(brw,
1276 mt->target,
1277 format,
1278 mt->first_level,
1279 mt->last_level,
1280 mt->logical_width0,
1281 mt->logical_height0,
1282 mt->logical_depth0,
1283 true,
1284 0 /* num_samples */,
1285 INTEL_MIPTREE_TILING_Y);
1286
1287 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1288 *
1289 * When MCS buffer is enabled and bound to MSRT, it is required that it
1290 * is cleared prior to any rendering.
1291 *
1292 * Since we don't use the MCS buffer for any purpose other than rendering,
1293 * it makes sense to just clear it immediately upon allocation.
1294 *
1295 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1296 */
1297 void *data = intel_miptree_map_raw(brw, mt->mcs_mt);
1298 memset(data, 0xff, mt->mcs_mt->total_height * mt->mcs_mt->pitch);
1299 intel_miptree_unmap_raw(brw, mt->mcs_mt);
1300 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
1301
1302 return mt->mcs_mt;
1303 }
1304
1305
1306 bool
1307 intel_miptree_alloc_non_msrt_mcs(struct brw_context *brw,
1308 struct intel_mipmap_tree *mt)
1309 {
1310 assert(mt->mcs_mt == NULL);
1311
1312 /* The format of the MCS buffer is opaque to the driver; all that matters
1313 * is that we get its size and pitch right. We'll pretend that the format
1314 * is R32. Since an MCS tile covers 128 blocks horizontally, and a Y-tiled
1315 * R32 buffer is 32 pixels across, we'll need to scale the width down by
1316 * the block width and then a further factor of 4. Since an MCS tile
1317 * covers 256 blocks vertically, and a Y-tiled R32 buffer is 32 rows high,
1318 * we'll need to scale the height down by the block height and then a
1319 * further factor of 8.
1320 */
1321 const mesa_format format = MESA_FORMAT_R_UINT32;
1322 unsigned block_width_px;
1323 unsigned block_height;
1324 intel_get_non_msrt_mcs_alignment(brw, mt, &block_width_px, &block_height);
1325 unsigned width_divisor = block_width_px * 4;
1326 unsigned height_divisor = block_height * 8;
1327 unsigned mcs_width =
1328 ALIGN(mt->logical_width0, width_divisor) / width_divisor;
1329 unsigned mcs_height =
1330 ALIGN(mt->logical_height0, height_divisor) / height_divisor;
1331 assert(mt->logical_depth0 == 1);
1332 mt->mcs_mt = intel_miptree_create(brw,
1333 mt->target,
1334 format,
1335 mt->first_level,
1336 mt->last_level,
1337 mcs_width,
1338 mcs_height,
1339 mt->logical_depth0,
1340 true,
1341 0 /* num_samples */,
1342 INTEL_MIPTREE_TILING_Y);
1343
1344 return mt->mcs_mt;
1345 }
1346
1347
1348 /**
1349 * Helper for intel_miptree_alloc_hiz() that sets
1350 * \c mt->level[level].slice[layer].has_hiz. Return true if and only if
1351 * \c has_hiz was set.
1352 */
1353 static bool
1354 intel_miptree_slice_enable_hiz(struct brw_context *brw,
1355 struct intel_mipmap_tree *mt,
1356 uint32_t level,
1357 uint32_t layer)
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 return false;
1373 }
1374 }
1375
1376 mt->level[level].slice[layer].has_hiz = true;
1377 return true;
1378 }
1379
1380
1381
1382 bool
1383 intel_miptree_alloc_hiz(struct brw_context *brw,
1384 struct intel_mipmap_tree *mt)
1385 {
1386 assert(mt->hiz_mt == NULL);
1387 mt->hiz_mt = intel_miptree_create(brw,
1388 mt->target,
1389 mt->format,
1390 mt->first_level,
1391 mt->last_level,
1392 mt->logical_width0,
1393 mt->logical_height0,
1394 mt->logical_depth0,
1395 true,
1396 mt->num_samples,
1397 INTEL_MIPTREE_TILING_ANY);
1398
1399 if (!mt->hiz_mt)
1400 return false;
1401
1402 /* Mark that all slices need a HiZ resolve. */
1403 struct intel_resolve_map *head = &mt->hiz_map;
1404 for (int level = mt->first_level; level <= mt->last_level; ++level) {
1405 for (int layer = 0; layer < mt->level[level].depth; ++layer) {
1406 if (!intel_miptree_slice_enable_hiz(brw, mt, level, layer))
1407 continue;
1408
1409 head->next = malloc(sizeof(*head->next));
1410 head->next->prev = head;
1411 head->next->next = NULL;
1412 head = head->next;
1413
1414 head->level = level;
1415 head->layer = layer;
1416 head->need = GEN6_HIZ_OP_HIZ_RESOLVE;
1417 }
1418 }
1419
1420 return true;
1421 }
1422
1423 /**
1424 * Does the miptree slice have hiz enabled?
1425 */
1426 bool
1427 intel_miptree_slice_has_hiz(struct intel_mipmap_tree *mt,
1428 uint32_t level,
1429 uint32_t layer)
1430 {
1431 intel_miptree_check_level_layer(mt, level, layer);
1432 return mt->level[level].slice[layer].has_hiz;
1433 }
1434
1435 void
1436 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1437 uint32_t level,
1438 uint32_t layer)
1439 {
1440 if (!intel_miptree_slice_has_hiz(mt, level, layer))
1441 return;
1442
1443 intel_resolve_map_set(&mt->hiz_map,
1444 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
1445 }
1446
1447
1448 void
1449 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
1450 uint32_t level,
1451 uint32_t layer)
1452 {
1453 if (!intel_miptree_slice_has_hiz(mt, level, layer))
1454 return;
1455
1456 intel_resolve_map_set(&mt->hiz_map,
1457 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
1458 }
1459
1460 void
1461 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
1462 uint32_t level)
1463 {
1464 uint32_t layer;
1465 uint32_t end_layer = mt->level[level].depth;
1466
1467 for (layer = 0; layer < end_layer; layer++) {
1468 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
1469 }
1470 }
1471
1472 static bool
1473 intel_miptree_slice_resolve(struct brw_context *brw,
1474 struct intel_mipmap_tree *mt,
1475 uint32_t level,
1476 uint32_t layer,
1477 enum gen6_hiz_op need)
1478 {
1479 intel_miptree_check_level_layer(mt, level, layer);
1480
1481 struct intel_resolve_map *item =
1482 intel_resolve_map_get(&mt->hiz_map, level, layer);
1483
1484 if (!item || item->need != need)
1485 return false;
1486
1487 intel_hiz_exec(brw, mt, level, layer, need);
1488 intel_resolve_map_remove(item);
1489 return true;
1490 }
1491
1492 bool
1493 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
1494 struct intel_mipmap_tree *mt,
1495 uint32_t level,
1496 uint32_t layer)
1497 {
1498 return intel_miptree_slice_resolve(brw, mt, level, layer,
1499 GEN6_HIZ_OP_HIZ_RESOLVE);
1500 }
1501
1502 bool
1503 intel_miptree_slice_resolve_depth(struct brw_context *brw,
1504 struct intel_mipmap_tree *mt,
1505 uint32_t level,
1506 uint32_t layer)
1507 {
1508 return intel_miptree_slice_resolve(brw, mt, level, layer,
1509 GEN6_HIZ_OP_DEPTH_RESOLVE);
1510 }
1511
1512 static bool
1513 intel_miptree_all_slices_resolve(struct brw_context *brw,
1514 struct intel_mipmap_tree *mt,
1515 enum gen6_hiz_op need)
1516 {
1517 bool did_resolve = false;
1518 struct intel_resolve_map *i, *next;
1519
1520 for (i = mt->hiz_map.next; i; i = next) {
1521 next = i->next;
1522 if (i->need != need)
1523 continue;
1524
1525 intel_hiz_exec(brw, mt, i->level, i->layer, need);
1526 intel_resolve_map_remove(i);
1527 did_resolve = true;
1528 }
1529
1530 return did_resolve;
1531 }
1532
1533 bool
1534 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
1535 struct intel_mipmap_tree *mt)
1536 {
1537 return intel_miptree_all_slices_resolve(brw, mt,
1538 GEN6_HIZ_OP_HIZ_RESOLVE);
1539 }
1540
1541 bool
1542 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
1543 struct intel_mipmap_tree *mt)
1544 {
1545 return intel_miptree_all_slices_resolve(brw, mt,
1546 GEN6_HIZ_OP_DEPTH_RESOLVE);
1547 }
1548
1549
1550 void
1551 intel_miptree_resolve_color(struct brw_context *brw,
1552 struct intel_mipmap_tree *mt)
1553 {
1554 switch (mt->fast_clear_state) {
1555 case INTEL_FAST_CLEAR_STATE_NO_MCS:
1556 case INTEL_FAST_CLEAR_STATE_RESOLVED:
1557 /* No resolve needed */
1558 break;
1559 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
1560 case INTEL_FAST_CLEAR_STATE_CLEAR:
1561 /* Fast color clear resolves only make sense for non-MSAA buffers. */
1562 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE)
1563 brw_blorp_resolve_color(brw, mt);
1564 break;
1565 }
1566 }
1567
1568
1569 /**
1570 * Make it possible to share the BO backing the given miptree with another
1571 * process or another miptree.
1572 *
1573 * Fast color clears are unsafe with shared buffers, so we need to resolve and
1574 * then discard the MCS buffer, if present. We also set the fast_clear_state
1575 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
1576 * allocated in the future.
1577 */
1578 void
1579 intel_miptree_make_shareable(struct brw_context *brw,
1580 struct intel_mipmap_tree *mt)
1581 {
1582 /* MCS buffers are also used for multisample buffers, but we can't resolve
1583 * away a multisample MCS buffer because it's an integral part of how the
1584 * pixel data is stored. Fortunately this code path should never be
1585 * reached for multisample buffers.
1586 */
1587 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE);
1588
1589 if (mt->mcs_mt) {
1590 intel_miptree_resolve_color(brw, mt);
1591 intel_miptree_release(&mt->mcs_mt);
1592 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
1593 }
1594 }
1595
1596
1597 /**
1598 * \brief Get pointer offset into stencil buffer.
1599 *
1600 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
1601 * must decode the tile's layout in software.
1602 *
1603 * See
1604 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
1605 * Format.
1606 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
1607 *
1608 * Even though the returned offset is always positive, the return type is
1609 * signed due to
1610 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
1611 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
1612 */
1613 static intptr_t
1614 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
1615 {
1616 uint32_t tile_size = 4096;
1617 uint32_t tile_width = 64;
1618 uint32_t tile_height = 64;
1619 uint32_t row_size = 64 * stride;
1620
1621 uint32_t tile_x = x / tile_width;
1622 uint32_t tile_y = y / tile_height;
1623
1624 /* The byte's address relative to the tile's base addres. */
1625 uint32_t byte_x = x % tile_width;
1626 uint32_t byte_y = y % tile_height;
1627
1628 uintptr_t u = tile_y * row_size
1629 + tile_x * tile_size
1630 + 512 * (byte_x / 8)
1631 + 64 * (byte_y / 8)
1632 + 32 * ((byte_y / 4) % 2)
1633 + 16 * ((byte_x / 4) % 2)
1634 + 8 * ((byte_y / 2) % 2)
1635 + 4 * ((byte_x / 2) % 2)
1636 + 2 * (byte_y % 2)
1637 + 1 * (byte_x % 2);
1638
1639 if (swizzled) {
1640 /* adjust for bit6 swizzling */
1641 if (((byte_x / 8) % 2) == 1) {
1642 if (((byte_y / 8) % 2) == 0) {
1643 u += 64;
1644 } else {
1645 u -= 64;
1646 }
1647 }
1648 }
1649
1650 return u;
1651 }
1652
1653 void
1654 intel_miptree_updownsample(struct brw_context *brw,
1655 struct intel_mipmap_tree *src,
1656 struct intel_mipmap_tree *dst)
1657 {
1658 if (brw->gen < 8 || src->format == MESA_FORMAT_S_UINT8) {
1659 brw_blorp_blit_miptrees(brw,
1660 src, 0 /* level */, 0 /* layer */,
1661 dst, 0 /* level */, 0 /* layer */,
1662 0, 0,
1663 src->logical_width0, src->logical_height0,
1664 0, 0,
1665 dst->logical_width0, dst->logical_height0,
1666 GL_NEAREST, false, false /*mirror x, y*/);
1667 } else {
1668 brw_meta_updownsample(brw, src, dst);
1669 }
1670
1671 if (src->stencil_mt) {
1672 brw_blorp_blit_miptrees(brw,
1673 src->stencil_mt, 0 /* level */, 0 /* layer */,
1674 dst->stencil_mt, 0 /* level */, 0 /* layer */,
1675 0, 0,
1676 src->logical_width0, src->logical_height0,
1677 0, 0,
1678 dst->logical_width0, dst->logical_height0,
1679 GL_NEAREST, false, false /*mirror x, y*/);
1680 }
1681 }
1682
1683 void *
1684 intel_miptree_map_raw(struct brw_context *brw, struct intel_mipmap_tree *mt)
1685 {
1686 /* CPU accesses to color buffers don't understand fast color clears, so
1687 * resolve any pending fast color clears before we map.
1688 */
1689 intel_miptree_resolve_color(brw, mt);
1690
1691 drm_intel_bo *bo = mt->bo;
1692
1693 if (drm_intel_bo_references(brw->batch.bo, bo))
1694 intel_batchbuffer_flush(brw);
1695
1696 if (mt->tiling != I915_TILING_NONE)
1697 brw_bo_map_gtt(brw, bo, "miptree");
1698 else
1699 brw_bo_map(brw, bo, true, "miptree");
1700
1701 return bo->virtual;
1702 }
1703
1704 void
1705 intel_miptree_unmap_raw(struct brw_context *brw,
1706 struct intel_mipmap_tree *mt)
1707 {
1708 drm_intel_bo_unmap(mt->bo);
1709 }
1710
1711 static void
1712 intel_miptree_map_gtt(struct brw_context *brw,
1713 struct intel_mipmap_tree *mt,
1714 struct intel_miptree_map *map,
1715 unsigned int level, unsigned int slice)
1716 {
1717 unsigned int bw, bh;
1718 void *base;
1719 unsigned int image_x, image_y;
1720 int x = map->x;
1721 int y = map->y;
1722
1723 /* For compressed formats, the stride is the number of bytes per
1724 * row of blocks. intel_miptree_get_image_offset() already does
1725 * the divide.
1726 */
1727 _mesa_get_format_block_size(mt->format, &bw, &bh);
1728 assert(y % bh == 0);
1729 y /= bh;
1730
1731 base = intel_miptree_map_raw(brw, mt) + mt->offset;
1732
1733 if (base == NULL)
1734 map->ptr = NULL;
1735 else {
1736 /* Note that in the case of cube maps, the caller must have passed the
1737 * slice number referencing the face.
1738 */
1739 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1740 x += image_x;
1741 y += image_y;
1742
1743 map->stride = mt->pitch;
1744 map->ptr = base + y * map->stride + x * mt->cpp;
1745 }
1746
1747 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1748 map->x, map->y, map->w, map->h,
1749 mt, _mesa_get_format_name(mt->format),
1750 x, y, map->ptr, map->stride);
1751 }
1752
1753 static void
1754 intel_miptree_unmap_gtt(struct brw_context *brw,
1755 struct intel_mipmap_tree *mt,
1756 struct intel_miptree_map *map,
1757 unsigned int level,
1758 unsigned int slice)
1759 {
1760 intel_miptree_unmap_raw(brw, mt);
1761 }
1762
1763 static void
1764 intel_miptree_map_blit(struct brw_context *brw,
1765 struct intel_mipmap_tree *mt,
1766 struct intel_miptree_map *map,
1767 unsigned int level, unsigned int slice)
1768 {
1769 map->mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
1770 0, 0,
1771 map->w, map->h, 1,
1772 false, 0,
1773 INTEL_MIPTREE_TILING_NONE);
1774 if (!map->mt) {
1775 fprintf(stderr, "Failed to allocate blit temporary\n");
1776 goto fail;
1777 }
1778 map->stride = map->mt->pitch;
1779
1780 if (!intel_miptree_blit(brw,
1781 mt, level, slice,
1782 map->x, map->y, false,
1783 map->mt, 0, 0,
1784 0, 0, false,
1785 map->w, map->h, GL_COPY)) {
1786 fprintf(stderr, "Failed to blit\n");
1787 goto fail;
1788 }
1789
1790 map->ptr = intel_miptree_map_raw(brw, map->mt);
1791
1792 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1793 map->x, map->y, map->w, map->h,
1794 mt, _mesa_get_format_name(mt->format),
1795 level, slice, map->ptr, map->stride);
1796
1797 return;
1798
1799 fail:
1800 intel_miptree_release(&map->mt);
1801 map->ptr = NULL;
1802 map->stride = 0;
1803 }
1804
1805 static void
1806 intel_miptree_unmap_blit(struct brw_context *brw,
1807 struct intel_mipmap_tree *mt,
1808 struct intel_miptree_map *map,
1809 unsigned int level,
1810 unsigned int slice)
1811 {
1812 struct gl_context *ctx = &brw->ctx;
1813
1814 intel_miptree_unmap_raw(brw, map->mt);
1815
1816 if (map->mode & GL_MAP_WRITE_BIT) {
1817 bool ok = intel_miptree_blit(brw,
1818 map->mt, 0, 0,
1819 0, 0, false,
1820 mt, level, slice,
1821 map->x, map->y, false,
1822 map->w, map->h, GL_COPY);
1823 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
1824 }
1825
1826 intel_miptree_release(&map->mt);
1827 }
1828
1829 #ifdef __SSE4_1__
1830 /**
1831 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
1832 */
1833 static void
1834 intel_miptree_map_movntdqa(struct brw_context *brw,
1835 struct intel_mipmap_tree *mt,
1836 struct intel_miptree_map *map,
1837 unsigned int level, unsigned int slice)
1838 {
1839 assert(map->mode & GL_MAP_READ_BIT);
1840 assert(!(map->mode & GL_MAP_WRITE_BIT));
1841
1842 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
1843 map->x, map->y, map->w, map->h,
1844 mt, _mesa_get_format_name(mt->format),
1845 level, slice, map->ptr, map->stride);
1846
1847 /* Map the original image */
1848 uint32_t image_x;
1849 uint32_t image_y;
1850 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1851 image_x += map->x;
1852 image_y += map->y;
1853
1854 void *src = intel_miptree_map_raw(brw, mt);
1855 if (!src)
1856 return;
1857 src += image_y * mt->pitch;
1858 src += image_x * mt->cpp;
1859
1860 /* Due to the pixel offsets for the particular image being mapped, our
1861 * src pointer may not be 16-byte aligned. However, if the pitch is
1862 * divisible by 16, then the amount by which it's misaligned will remain
1863 * consistent from row to row.
1864 */
1865 assert((mt->pitch % 16) == 0);
1866 const int misalignment = ((uintptr_t) src) & 15;
1867
1868 /* Create an untiled temporary buffer for the mapping. */
1869 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
1870
1871 map->stride = ALIGN(misalignment + width_bytes, 16);
1872
1873 map->buffer = malloc(map->stride * map->h);
1874 /* Offset the destination so it has the same misalignment as src. */
1875 map->ptr = map->buffer + misalignment;
1876
1877 assert((((uintptr_t) map->ptr) & 15) == misalignment);
1878
1879 for (uint32_t y = 0; y < map->h; y++) {
1880 void *dst_ptr = map->ptr + y * map->stride;
1881 void *src_ptr = src + y * mt->pitch;
1882
1883 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
1884 }
1885
1886 intel_miptree_unmap_raw(brw, mt);
1887 }
1888
1889 static void
1890 intel_miptree_unmap_movntdqa(struct brw_context *brw,
1891 struct intel_mipmap_tree *mt,
1892 struct intel_miptree_map *map,
1893 unsigned int level,
1894 unsigned int slice)
1895 {
1896 free(map->buffer);
1897 map->buffer = NULL;
1898 map->ptr = NULL;
1899 }
1900 #endif
1901
1902 static void
1903 intel_miptree_map_s8(struct brw_context *brw,
1904 struct intel_mipmap_tree *mt,
1905 struct intel_miptree_map *map,
1906 unsigned int level, unsigned int slice)
1907 {
1908 map->stride = map->w;
1909 map->buffer = map->ptr = malloc(map->stride * map->h);
1910 if (!map->buffer)
1911 return;
1912
1913 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
1914 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
1915 * invalidate is set, since we'll be writing the whole rectangle from our
1916 * temporary buffer back out.
1917 */
1918 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
1919 uint8_t *untiled_s8_map = map->ptr;
1920 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
1921 unsigned int image_x, image_y;
1922
1923 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1924
1925 for (uint32_t y = 0; y < map->h; y++) {
1926 for (uint32_t x = 0; x < map->w; x++) {
1927 ptrdiff_t offset = intel_offset_S8(mt->pitch,
1928 x + image_x + map->x,
1929 y + image_y + map->y,
1930 brw->has_swizzling);
1931 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
1932 }
1933 }
1934
1935 intel_miptree_unmap_raw(brw, mt);
1936
1937 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
1938 map->x, map->y, map->w, map->h,
1939 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
1940 } else {
1941 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
1942 map->x, map->y, map->w, map->h,
1943 mt, map->ptr, map->stride);
1944 }
1945 }
1946
1947 static void
1948 intel_miptree_unmap_s8(struct brw_context *brw,
1949 struct intel_mipmap_tree *mt,
1950 struct intel_miptree_map *map,
1951 unsigned int level,
1952 unsigned int slice)
1953 {
1954 if (map->mode & GL_MAP_WRITE_BIT) {
1955 unsigned int image_x, image_y;
1956 uint8_t *untiled_s8_map = map->ptr;
1957 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
1958
1959 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
1960
1961 for (uint32_t y = 0; y < map->h; y++) {
1962 for (uint32_t x = 0; x < map->w; x++) {
1963 ptrdiff_t offset = intel_offset_S8(mt->pitch,
1964 x + map->x,
1965 y + map->y,
1966 brw->has_swizzling);
1967 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
1968 }
1969 }
1970
1971 intel_miptree_unmap_raw(brw, mt);
1972 }
1973
1974 free(map->buffer);
1975 }
1976
1977 static void
1978 intel_miptree_map_etc(struct brw_context *brw,
1979 struct intel_mipmap_tree *mt,
1980 struct intel_miptree_map *map,
1981 unsigned int level,
1982 unsigned int slice)
1983 {
1984 assert(mt->etc_format != MESA_FORMAT_NONE);
1985 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
1986 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
1987 }
1988
1989 assert(map->mode & GL_MAP_WRITE_BIT);
1990 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
1991
1992 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
1993 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
1994 map->w, map->h, 1));
1995 map->ptr = map->buffer;
1996 }
1997
1998 static void
1999 intel_miptree_unmap_etc(struct brw_context *brw,
2000 struct intel_mipmap_tree *mt,
2001 struct intel_miptree_map *map,
2002 unsigned int level,
2003 unsigned int slice)
2004 {
2005 uint32_t image_x;
2006 uint32_t image_y;
2007 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2008
2009 image_x += map->x;
2010 image_y += map->y;
2011
2012 uint8_t *dst = intel_miptree_map_raw(brw, mt)
2013 + image_y * mt->pitch
2014 + image_x * mt->cpp;
2015
2016 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
2017 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
2018 map->ptr, map->stride,
2019 map->w, map->h);
2020 else
2021 _mesa_unpack_etc2_format(dst, mt->pitch,
2022 map->ptr, map->stride,
2023 map->w, map->h, mt->etc_format);
2024
2025 intel_miptree_unmap_raw(brw, mt);
2026 free(map->buffer);
2027 }
2028
2029 /**
2030 * Mapping function for packed depth/stencil miptrees backed by real separate
2031 * miptrees for depth and stencil.
2032 *
2033 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2034 * separate from the depth buffer. Yet at the GL API level, we have to expose
2035 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2036 * be able to map that memory for texture storage and glReadPixels-type
2037 * operations. We give Mesa core that access by mallocing a temporary and
2038 * copying the data between the actual backing store and the temporary.
2039 */
2040 static void
2041 intel_miptree_map_depthstencil(struct brw_context *brw,
2042 struct intel_mipmap_tree *mt,
2043 struct intel_miptree_map *map,
2044 unsigned int level, unsigned int slice)
2045 {
2046 struct intel_mipmap_tree *z_mt = mt;
2047 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2048 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2049 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
2050
2051 map->stride = map->w * packed_bpp;
2052 map->buffer = map->ptr = malloc(map->stride * map->h);
2053 if (!map->buffer)
2054 return;
2055
2056 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2057 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2058 * invalidate is set, since we'll be writing the whole rectangle from our
2059 * temporary buffer back out.
2060 */
2061 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2062 uint32_t *packed_map = map->ptr;
2063 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2064 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2065 unsigned int s_image_x, s_image_y;
2066 unsigned int z_image_x, z_image_y;
2067
2068 intel_miptree_get_image_offset(s_mt, level, slice,
2069 &s_image_x, &s_image_y);
2070 intel_miptree_get_image_offset(z_mt, level, slice,
2071 &z_image_x, &z_image_y);
2072
2073 for (uint32_t y = 0; y < map->h; y++) {
2074 for (uint32_t x = 0; x < map->w; x++) {
2075 int map_x = map->x + x, map_y = map->y + y;
2076 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2077 map_x + s_image_x,
2078 map_y + s_image_y,
2079 brw->has_swizzling);
2080 ptrdiff_t z_offset = ((map_y + z_image_y) *
2081 (z_mt->pitch / 4) +
2082 (map_x + z_image_x));
2083 uint8_t s = s_map[s_offset];
2084 uint32_t z = z_map[z_offset];
2085
2086 if (map_z32f_x24s8) {
2087 packed_map[(y * map->w + x) * 2 + 0] = z;
2088 packed_map[(y * map->w + x) * 2 + 1] = s;
2089 } else {
2090 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
2091 }
2092 }
2093 }
2094
2095 intel_miptree_unmap_raw(brw, s_mt);
2096 intel_miptree_unmap_raw(brw, z_mt);
2097
2098 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
2099 __FUNCTION__,
2100 map->x, map->y, map->w, map->h,
2101 z_mt, map->x + z_image_x, map->y + z_image_y,
2102 s_mt, map->x + s_image_x, map->y + s_image_y,
2103 map->ptr, map->stride);
2104 } else {
2105 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
2106 map->x, map->y, map->w, map->h,
2107 mt, map->ptr, map->stride);
2108 }
2109 }
2110
2111 static void
2112 intel_miptree_unmap_depthstencil(struct brw_context *brw,
2113 struct intel_mipmap_tree *mt,
2114 struct intel_miptree_map *map,
2115 unsigned int level,
2116 unsigned int slice)
2117 {
2118 struct intel_mipmap_tree *z_mt = mt;
2119 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2120 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2121
2122 if (map->mode & GL_MAP_WRITE_BIT) {
2123 uint32_t *packed_map = map->ptr;
2124 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2125 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2126 unsigned int s_image_x, s_image_y;
2127 unsigned int z_image_x, z_image_y;
2128
2129 intel_miptree_get_image_offset(s_mt, level, slice,
2130 &s_image_x, &s_image_y);
2131 intel_miptree_get_image_offset(z_mt, level, slice,
2132 &z_image_x, &z_image_y);
2133
2134 for (uint32_t y = 0; y < map->h; y++) {
2135 for (uint32_t x = 0; x < map->w; x++) {
2136 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2137 x + s_image_x + map->x,
2138 y + s_image_y + map->y,
2139 brw->has_swizzling);
2140 ptrdiff_t z_offset = ((y + z_image_y) *
2141 (z_mt->pitch / 4) +
2142 (x + z_image_x));
2143
2144 if (map_z32f_x24s8) {
2145 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
2146 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
2147 } else {
2148 uint32_t packed = packed_map[y * map->w + x];
2149 s_map[s_offset] = packed >> 24;
2150 z_map[z_offset] = packed;
2151 }
2152 }
2153 }
2154
2155 intel_miptree_unmap_raw(brw, s_mt);
2156 intel_miptree_unmap_raw(brw, z_mt);
2157
2158 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2159 __FUNCTION__,
2160 map->x, map->y, map->w, map->h,
2161 z_mt, _mesa_get_format_name(z_mt->format),
2162 map->x + z_image_x, map->y + z_image_y,
2163 s_mt, map->x + s_image_x, map->y + s_image_y,
2164 map->ptr, map->stride);
2165 }
2166
2167 free(map->buffer);
2168 }
2169
2170 /**
2171 * Create and attach a map to the miptree at (level, slice). Return the
2172 * attached map.
2173 */
2174 static struct intel_miptree_map*
2175 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
2176 unsigned int level,
2177 unsigned int slice,
2178 unsigned int x,
2179 unsigned int y,
2180 unsigned int w,
2181 unsigned int h,
2182 GLbitfield mode)
2183 {
2184 struct intel_miptree_map *map = calloc(1, sizeof(*map));
2185
2186 if (!map)
2187 return NULL;
2188
2189 assert(mt->level[level].slice[slice].map == NULL);
2190 mt->level[level].slice[slice].map = map;
2191
2192 map->mode = mode;
2193 map->x = x;
2194 map->y = y;
2195 map->w = w;
2196 map->h = h;
2197
2198 return map;
2199 }
2200
2201 /**
2202 * Release the map at (level, slice).
2203 */
2204 static void
2205 intel_miptree_release_map(struct intel_mipmap_tree *mt,
2206 unsigned int level,
2207 unsigned int slice)
2208 {
2209 struct intel_miptree_map **map;
2210
2211 map = &mt->level[level].slice[slice].map;
2212 free(*map);
2213 *map = NULL;
2214 }
2215
2216 static bool
2217 can_blit_slice(struct intel_mipmap_tree *mt,
2218 unsigned int level, unsigned int slice)
2219 {
2220 uint32_t image_x;
2221 uint32_t image_y;
2222 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2223 if (image_x >= 32768 || image_y >= 32768)
2224 return false;
2225
2226 if (mt->pitch >= 32768)
2227 return false;
2228
2229 return true;
2230 }
2231
2232 void
2233 intel_miptree_map(struct brw_context *brw,
2234 struct intel_mipmap_tree *mt,
2235 unsigned int level,
2236 unsigned int slice,
2237 unsigned int x,
2238 unsigned int y,
2239 unsigned int w,
2240 unsigned int h,
2241 GLbitfield mode,
2242 void **out_ptr,
2243 int *out_stride)
2244 {
2245 struct intel_miptree_map *map;
2246
2247 assert(mt->num_samples <= 1);
2248
2249 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
2250 if (!map){
2251 *out_ptr = NULL;
2252 *out_stride = 0;
2253 return;
2254 }
2255
2256 intel_miptree_slice_resolve_depth(brw, mt, level, slice);
2257 if (map->mode & GL_MAP_WRITE_BIT) {
2258 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
2259 }
2260
2261 if (mt->format == MESA_FORMAT_S_UINT8) {
2262 intel_miptree_map_s8(brw, mt, map, level, slice);
2263 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2264 !(mode & BRW_MAP_DIRECT_BIT)) {
2265 intel_miptree_map_etc(brw, mt, map, level, slice);
2266 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
2267 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
2268 }
2269 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2270 else if (brw->has_llc &&
2271 !(mode & GL_MAP_WRITE_BIT) &&
2272 !mt->compressed &&
2273 (mt->tiling == I915_TILING_X ||
2274 (brw->gen >= 6 && mt->tiling == I915_TILING_Y)) &&
2275 can_blit_slice(mt, level, slice)) {
2276 intel_miptree_map_blit(brw, mt, map, level, slice);
2277 } else if (mt->tiling != I915_TILING_NONE &&
2278 mt->bo->size >= brw->max_gtt_map_object_size) {
2279 assert(can_blit_slice(mt, level, slice));
2280 intel_miptree_map_blit(brw, mt, map, level, slice);
2281 #ifdef __SSE4_1__
2282 } else if (!(mode & GL_MAP_WRITE_BIT) && !mt->compressed) {
2283 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
2284 #endif
2285 } else {
2286 intel_miptree_map_gtt(brw, mt, map, level, slice);
2287 }
2288
2289 *out_ptr = map->ptr;
2290 *out_stride = map->stride;
2291
2292 if (map->ptr == NULL)
2293 intel_miptree_release_map(mt, level, slice);
2294 }
2295
2296 void
2297 intel_miptree_unmap(struct brw_context *brw,
2298 struct intel_mipmap_tree *mt,
2299 unsigned int level,
2300 unsigned int slice)
2301 {
2302 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
2303
2304 assert(mt->num_samples <= 1);
2305
2306 if (!map)
2307 return;
2308
2309 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
2310 mt, _mesa_get_format_name(mt->format), level, slice);
2311
2312 if (mt->format == MESA_FORMAT_S_UINT8) {
2313 intel_miptree_unmap_s8(brw, mt, map, level, slice);
2314 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2315 !(map->mode & BRW_MAP_DIRECT_BIT)) {
2316 intel_miptree_unmap_etc(brw, mt, map, level, slice);
2317 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
2318 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
2319 } else if (map->mt) {
2320 intel_miptree_unmap_blit(brw, mt, map, level, slice);
2321 #ifdef __SSE4_1__
2322 } else if (map->buffer) {
2323 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
2324 #endif
2325 } else {
2326 intel_miptree_unmap_gtt(brw, mt, map, level, slice);
2327 }
2328
2329 intel_miptree_release_map(mt, level, slice);
2330 }