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