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