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