i965/miptree: Directly gtt map the mcs buffer
[mesa.git] / src / mesa / drivers / dri / i965 / intel_mipmap_tree.c
1 /*
2 * Copyright 2006 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include <GL/gl.h>
27 #include <GL/internal/dri_interface.h>
28
29 #include "intel_batchbuffer.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_resolve_map.h"
32 #include "intel_tex.h"
33 #include "intel_blit.h"
34 #include "intel_fbo.h"
35
36 #include "brw_blorp.h"
37 #include "brw_context.h"
38 #include "brw_state.h"
39
40 #include "main/enums.h"
41 #include "main/fbobject.h"
42 #include "main/formats.h"
43 #include "main/glformats.h"
44 #include "main/texcompress_etc.h"
45 #include "main/teximage.h"
46 #include "main/streaming-load-memcpy.h"
47 #include "x86/common_x86_asm.h"
48
49 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
50
51 static void *intel_miptree_map_raw(struct brw_context *brw,
52 struct intel_mipmap_tree *mt);
53
54 static void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
55
56 static bool
57 intel_miptree_alloc_mcs(struct brw_context *brw,
58 struct intel_mipmap_tree *mt,
59 GLuint num_samples);
60
61 /**
62 * Determine which MSAA layout should be used by the MSAA surface being
63 * created, based on the chip generation and the surface type.
64 */
65 static enum intel_msaa_layout
66 compute_msaa_layout(struct brw_context *brw, mesa_format format,
67 bool disable_aux_buffers)
68 {
69 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
70 if (brw->gen < 7)
71 return INTEL_MSAA_LAYOUT_IMS;
72
73 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
74 switch (_mesa_get_format_base_format(format)) {
75 case GL_DEPTH_COMPONENT:
76 case GL_STENCIL_INDEX:
77 case GL_DEPTH_STENCIL:
78 return INTEL_MSAA_LAYOUT_IMS;
79 default:
80 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
81 *
82 * This field must be set to 0 for all SINT MSRTs when all RT channels
83 * are not written
84 *
85 * In practice this means that we have to disable MCS for all signed
86 * integer MSAA buffers. The alternative, to disable MCS only when one
87 * of the render target channels is disabled, is impractical because it
88 * would require converting between CMS and UMS MSAA layouts on the fly,
89 * which is expensive.
90 */
91 if (brw->gen == 7 && _mesa_get_format_datatype(format) == GL_INT) {
92 return INTEL_MSAA_LAYOUT_UMS;
93 } else if (disable_aux_buffers) {
94 /* We can't use the CMS layout because it uses an aux buffer, the MCS
95 * buffer. So fallback to UMS, which is identical to CMS without the
96 * MCS. */
97 return INTEL_MSAA_LAYOUT_UMS;
98 } else {
99 return INTEL_MSAA_LAYOUT_CMS;
100 }
101 }
102 }
103
104
105 /**
106 * For single-sampled render targets ("non-MSRT"), the MCS buffer is a
107 * scaled-down bitfield representation of the color buffer which is capable of
108 * recording when blocks of the color buffer are equal to the clear value.
109 * This function returns the block size that will be used by the MCS buffer
110 * corresponding to a certain color miptree.
111 *
112 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
113 * beneath the "Fast Color Clear" bullet (p327):
114 *
115 * The following table describes the RT alignment
116 *
117 * Pixels Lines
118 * TiledY RT CL
119 * bpp
120 * 32 8 4
121 * 64 4 4
122 * 128 2 4
123 * TiledX RT CL
124 * bpp
125 * 32 16 2
126 * 64 8 2
127 * 128 4 2
128 *
129 * This alignment has the following uses:
130 *
131 * - For figuring out the size of the MCS buffer. Each 4k tile in the MCS
132 * buffer contains 128 blocks horizontally and 256 blocks vertically.
133 *
134 * - For figuring out alignment restrictions for a fast clear operation. Fast
135 * clear operations must always clear aligned multiples of 16 blocks
136 * horizontally and 32 blocks vertically.
137 *
138 * - For scaling down the coordinates sent through the render pipeline during
139 * a fast clear. X coordinates must be scaled down by 8 times the block
140 * width, and Y coordinates by 16 times the block height.
141 *
142 * - For scaling down the coordinates sent through the render pipeline during
143 * a "Render Target Resolve" operation. X coordinates must be scaled down
144 * by half the block width, and Y coordinates by half the block height.
145 */
146 void
147 intel_get_non_msrt_mcs_alignment(const struct intel_mipmap_tree *mt,
148 unsigned *width_px, unsigned *height)
149 {
150 switch (mt->tiling) {
151 default:
152 unreachable("Non-MSRT MCS requires X or Y tiling");
153 /* In release builds, fall through */
154 case I915_TILING_Y:
155 *width_px = 32 / mt->cpp;
156 *height = 4;
157 break;
158 case I915_TILING_X:
159 *width_px = 64 / mt->cpp;
160 *height = 2;
161 }
162 }
163
164 bool
165 intel_tiling_supports_non_msrt_mcs(const struct brw_context *brw,
166 unsigned tiling)
167 {
168 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
169 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
170 *
171 * - Support is limited to tiled render targets.
172 *
173 * Gen9 changes the restriction to Y-tile only.
174 */
175 if (brw->gen >= 9)
176 return tiling == I915_TILING_Y;
177 else if (brw->gen >= 7)
178 return tiling != I915_TILING_NONE;
179 else
180 return false;
181 }
182
183 /**
184 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
185 * can be used. This doesn't (and should not) inspect any of the properties of
186 * the miptree's BO.
187 *
188 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
189 * beneath the "Fast Color Clear" bullet (p326):
190 *
191 * - Support is for non-mip-mapped and non-array surface types only.
192 *
193 * And then later, on p327:
194 *
195 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
196 * 64bpp, and 128bpp.
197 *
198 * From the Skylake documentation, it is made clear that X-tiling is no longer
199 * supported:
200 *
201 * - MCS and Lossless compression is supported for TiledY/TileYs/TileYf
202 * non-MSRTs only.
203 */
204 bool
205 intel_miptree_supports_non_msrt_fast_clear(struct brw_context *brw,
206 const struct intel_mipmap_tree *mt)
207 {
208 /* MCS support does not exist prior to Gen7 */
209 if (brw->gen < 7)
210 return false;
211
212 if (mt->disable_aux_buffers)
213 return false;
214
215 /* This function applies only to non-multisampled render targets. */
216 if (mt->num_samples > 1)
217 return false;
218
219 /* MCS is only supported for color buffers */
220 switch (_mesa_get_format_base_format(mt->format)) {
221 case GL_DEPTH_COMPONENT:
222 case GL_DEPTH_STENCIL:
223 case GL_STENCIL_INDEX:
224 return false;
225 }
226
227 if (mt->cpp != 4 && mt->cpp != 8 && mt->cpp != 16)
228 return false;
229 if (mt->first_level != 0 || mt->last_level != 0) {
230 if (brw->gen >= 8) {
231 perf_debug("Multi-LOD fast clear - giving up (%dx%dx%d).\n",
232 mt->logical_width0, mt->logical_height0, mt->last_level);
233 }
234
235 return false;
236 }
237
238 /* Check for layered surfaces. */
239 if (mt->physical_depth0 != 1) {
240 /* Multisample surfaces with the CMS layout are not layered surfaces,
241 * yet still have physical_depth0 > 1. Assert that we don't
242 * accidentally reject a multisampled surface here. We should have
243 * rejected it earlier by explicitly checking the sample count.
244 */
245 assert(mt->num_samples <= 1);
246
247 if (brw->gen >= 8) {
248 perf_debug("Layered fast clear - giving up. (%dx%d%d)\n",
249 mt->logical_width0, mt->logical_height0,
250 mt->physical_depth0);
251 }
252
253 return false;
254 }
255
256 /* There's no point in using an MCS buffer if the surface isn't in a
257 * renderable format.
258 */
259 if (!brw->format_supported_as_render_target[mt->format])
260 return false;
261
262 if (brw->gen >= 9) {
263 mesa_format linear_format = _mesa_get_srgb_format_linear(mt->format);
264 const uint32_t brw_format = brw_format_for_mesa_format(linear_format);
265 return isl_format_supports_lossless_compression(&brw->screen->devinfo,
266 brw_format);
267 } else
268 return true;
269 }
270
271 /* On Gen9 support for color buffer compression was extended to single
272 * sampled surfaces. This is a helper considering both auxiliary buffer
273 * type and number of samples telling if the given miptree represents
274 * the new single sampled case - also called lossless compression.
275 */
276 bool
277 intel_miptree_is_lossless_compressed(const struct brw_context *brw,
278 const struct intel_mipmap_tree *mt)
279 {
280 /* Only available from Gen9 onwards. */
281 if (brw->gen < 9)
282 return false;
283
284 /* Compression always requires auxiliary buffer. */
285 if (!mt->mcs_buf)
286 return false;
287
288 /* Single sample compression is represented re-using msaa compression
289 * layout type: "Compressed Multisampled Surfaces".
290 */
291 if (mt->msaa_layout != INTEL_MSAA_LAYOUT_CMS)
292 return false;
293
294 /* And finally distinguish between msaa and single sample case. */
295 return mt->num_samples <= 1;
296 }
297
298 bool
299 intel_miptree_supports_lossless_compressed(struct brw_context *brw,
300 const struct intel_mipmap_tree *mt)
301 {
302 /* For now compression is only enabled for integer formats even though
303 * there exist supported floating point formats also. This is a heuristic
304 * decision based on current public benchmarks. In none of the cases these
305 * formats provided any improvement but a few cases were seen to regress.
306 * Hence these are left to to be enabled in the future when they are known
307 * to improve things.
308 */
309 if (_mesa_get_format_datatype(mt->format) == GL_FLOAT)
310 return false;
311
312 /* Fast clear mechanism and lossless compression go hand in hand. */
313 if (!intel_miptree_supports_non_msrt_fast_clear(brw, mt))
314 return false;
315
316 /* Fast clear can be also used to clear srgb surfaces by using equivalent
317 * linear format. This trick, however, can't be extended to be used with
318 * lossless compression and therefore a check is needed to see if the format
319 * really is linear.
320 */
321 return _mesa_get_srgb_format_linear(mt->format) == mt->format;
322 }
323
324 /**
325 * Determine depth format corresponding to a depth+stencil format,
326 * for separate stencil.
327 */
328 mesa_format
329 intel_depth_format_for_depthstencil_format(mesa_format format) {
330 switch (format) {
331 case MESA_FORMAT_Z24_UNORM_S8_UINT:
332 return MESA_FORMAT_Z24_UNORM_X8_UINT;
333 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
334 return MESA_FORMAT_Z_FLOAT32;
335 default:
336 return format;
337 }
338 }
339
340
341 /**
342 * @param for_bo Indicates that the caller is
343 * intel_miptree_create_for_bo(). If true, then do not create
344 * \c stencil_mt.
345 */
346 static struct intel_mipmap_tree *
347 intel_miptree_create_layout(struct brw_context *brw,
348 GLenum target,
349 mesa_format format,
350 GLuint first_level,
351 GLuint last_level,
352 GLuint width0,
353 GLuint height0,
354 GLuint depth0,
355 GLuint num_samples,
356 uint32_t layout_flags)
357 {
358 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
359 if (!mt)
360 return NULL;
361
362 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__,
363 _mesa_enum_to_string(target),
364 _mesa_get_format_name(format),
365 first_level, last_level, depth0, mt);
366
367 if (target == GL_TEXTURE_1D_ARRAY)
368 assert(height0 == 1);
369
370 mt->target = target;
371 mt->format = format;
372 mt->first_level = first_level;
373 mt->last_level = last_level;
374 mt->logical_width0 = width0;
375 mt->logical_height0 = height0;
376 mt->logical_depth0 = depth0;
377 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
378 mt->disable_aux_buffers = (layout_flags & MIPTREE_LAYOUT_DISABLE_AUX) != 0;
379 mt->is_scanout = (layout_flags & MIPTREE_LAYOUT_FOR_SCANOUT) != 0;
380 exec_list_make_empty(&mt->hiz_map);
381 mt->cpp = _mesa_get_format_bytes(format);
382 mt->num_samples = num_samples;
383 mt->compressed = _mesa_is_format_compressed(format);
384 mt->msaa_layout = INTEL_MSAA_LAYOUT_NONE;
385 mt->refcount = 1;
386
387 int depth_multiply = 1;
388 if (num_samples > 1) {
389 /* Adjust width/height/depth for MSAA */
390 mt->msaa_layout = compute_msaa_layout(brw, format,
391 mt->disable_aux_buffers);
392 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
393 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
394 * "If the surface is multisampled and it is a depth or stencil
395 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
396 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
397 * proceeding:
398 *
399 * +----------------------------------------------------------------+
400 * | Num Multisamples | W_l = | H_l = |
401 * +----------------------------------------------------------------+
402 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
403 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
404 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
405 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
406 * +----------------------------------------------------------------+
407 * "
408 *
409 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
410 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
411 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
412 *
413 * Another more complicated explanation for these adjustments comes
414 * from the Sandybridge PRM, volume 4, part 1, page 31:
415 *
416 * "Any of the other messages (sample*, LOD, load4) used with a
417 * (4x) multisampled surface will in-effect sample a surface with
418 * double the height and width as that indicated in the surface
419 * state. Each pixel position on the original-sized surface is
420 * replaced with a 2x2 of samples with the following arrangement:
421 *
422 * sample 0 sample 2
423 * sample 1 sample 3"
424 *
425 * Thus, when sampling from a multisampled texture, it behaves as
426 * though the layout in memory for (x,y,sample) is:
427 *
428 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
429 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
430 *
431 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
432 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
433 *
434 * However, the actual layout of multisampled data in memory is:
435 *
436 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
437 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
438 *
439 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
440 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
441 *
442 * This pattern repeats for each 2x2 pixel block.
443 *
444 * As a result, when calculating the size of our 4-sample buffer for
445 * an odd width or height, we have to align before scaling up because
446 * sample 3 is in that bottom right 2x2 block.
447 */
448 switch (num_samples) {
449 case 2:
450 assert(brw->gen >= 8);
451 width0 = ALIGN(width0, 2) * 2;
452 height0 = ALIGN(height0, 2);
453 break;
454 case 4:
455 width0 = ALIGN(width0, 2) * 2;
456 height0 = ALIGN(height0, 2) * 2;
457 break;
458 case 8:
459 width0 = ALIGN(width0, 2) * 4;
460 height0 = ALIGN(height0, 2) * 2;
461 break;
462 case 16:
463 width0 = ALIGN(width0, 2) * 4;
464 height0 = ALIGN(height0, 2) * 4;
465 break;
466 default:
467 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
468 * or 16.
469 */
470 unreachable("not reached");
471 }
472 } else {
473 /* Non-interleaved */
474 depth_multiply = num_samples;
475 depth0 *= depth_multiply;
476 }
477 }
478
479 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
480 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
481 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
482 * used on Gen8 to make it pick a qpitch value which doesn't include space
483 * for the mipmaps. On Gen9 this is not necessary because it will
484 * automatically pick a packed qpitch value whenever mt->first_level ==
485 * mt->last_level.
486 * TODO: can we use it elsewhere?
487 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
488 */
489 if (brw->gen >= 9) {
490 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
491 } else {
492 switch (mt->msaa_layout) {
493 case INTEL_MSAA_LAYOUT_NONE:
494 case INTEL_MSAA_LAYOUT_IMS:
495 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
496 break;
497 case INTEL_MSAA_LAYOUT_UMS:
498 case INTEL_MSAA_LAYOUT_CMS:
499 mt->array_layout = ALL_SLICES_AT_EACH_LOD;
500 break;
501 }
502 }
503
504 if (target == GL_TEXTURE_CUBE_MAP)
505 assert(depth0 == 6 * depth_multiply);
506
507 mt->physical_width0 = width0;
508 mt->physical_height0 = height0;
509 mt->physical_depth0 = depth0;
510
511 if (!(layout_flags & MIPTREE_LAYOUT_FOR_BO) &&
512 _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
513 (brw->must_use_separate_stencil ||
514 (brw->has_separate_stencil &&
515 intel_miptree_wants_hiz_buffer(brw, mt)))) {
516 uint32_t stencil_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
517 if (brw->gen == 6) {
518 stencil_flags |= MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD |
519 MIPTREE_LAYOUT_TILING_ANY;
520 }
521
522 mt->stencil_mt = intel_miptree_create(brw,
523 mt->target,
524 MESA_FORMAT_S_UINT8,
525 mt->first_level,
526 mt->last_level,
527 mt->logical_width0,
528 mt->logical_height0,
529 mt->logical_depth0,
530 num_samples,
531 stencil_flags);
532
533 if (!mt->stencil_mt) {
534 intel_miptree_release(&mt);
535 return NULL;
536 }
537 mt->stencil_mt->r8stencil_needs_update = true;
538
539 /* Fix up the Z miptree format for how we're splitting out separate
540 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
541 */
542 mt->format = intel_depth_format_for_depthstencil_format(mt->format);
543 mt->cpp = 4;
544
545 if (format == mt->format) {
546 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
547 _mesa_get_format_name(mt->format));
548 }
549 }
550
551 if (layout_flags & MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD)
552 mt->array_layout = ALL_SLICES_AT_EACH_LOD;
553
554 /*
555 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
556 * multisampled or have an AUX buffer attached to it.
557 *
558 * GEN | MSRT | AUX_CCS_* or AUX_MCS
559 * -------------------------------------------
560 * 9 | HALIGN_16 | HALIGN_16
561 * 8 | HALIGN_ANY | HALIGN_16
562 * 7 | ? | ?
563 * 6 | ? | ?
564 */
565 if (intel_miptree_supports_non_msrt_fast_clear(brw, mt)) {
566 if (brw->gen >= 9 || (brw->gen == 8 && num_samples <= 1))
567 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
568 } else if (brw->gen >= 9 && num_samples > 1) {
569 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
570 } else {
571 const UNUSED bool is_lossless_compressed_aux =
572 brw->gen >= 9 && num_samples == 1 &&
573 mt->format == MESA_FORMAT_R_UINT32;
574
575 /* For now, nothing else has this requirement */
576 assert(is_lossless_compressed_aux ||
577 (layout_flags & MIPTREE_LAYOUT_FORCE_HALIGN16) == 0);
578 }
579
580 brw_miptree_layout(brw, mt, layout_flags);
581
582 if (mt->disable_aux_buffers)
583 assert(mt->msaa_layout != INTEL_MSAA_LAYOUT_CMS);
584
585 return mt;
586 }
587
588
589 /**
590 * Choose an appropriate uncompressed format for a requested
591 * compressed format, if unsupported.
592 */
593 mesa_format
594 intel_lower_compressed_format(struct brw_context *brw, mesa_format format)
595 {
596 /* No need to lower ETC formats on these platforms,
597 * they are supported natively.
598 */
599 if (brw->gen >= 8 || brw->is_baytrail)
600 return format;
601
602 switch (format) {
603 case MESA_FORMAT_ETC1_RGB8:
604 return MESA_FORMAT_R8G8B8X8_UNORM;
605 case MESA_FORMAT_ETC2_RGB8:
606 return MESA_FORMAT_R8G8B8X8_UNORM;
607 case MESA_FORMAT_ETC2_SRGB8:
608 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
609 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
610 return MESA_FORMAT_B8G8R8A8_SRGB;
611 case MESA_FORMAT_ETC2_RGBA8_EAC:
612 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
613 return MESA_FORMAT_R8G8B8A8_UNORM;
614 case MESA_FORMAT_ETC2_R11_EAC:
615 return MESA_FORMAT_R_UNORM16;
616 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
617 return MESA_FORMAT_R_SNORM16;
618 case MESA_FORMAT_ETC2_RG11_EAC:
619 return MESA_FORMAT_R16G16_UNORM;
620 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
621 return MESA_FORMAT_R16G16_SNORM;
622 default:
623 /* Non ETC1 / ETC2 format */
624 return format;
625 }
626 }
627
628 /* This function computes Yf/Ys tiled bo size, alignment and pitch. */
629 static unsigned long
630 intel_get_yf_ys_bo_size(struct intel_mipmap_tree *mt, unsigned *alignment,
631 unsigned long *pitch)
632 {
633 uint32_t tile_width, tile_height;
634 unsigned long stride, size, aligned_y;
635
636 assert(mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE);
637 intel_get_tile_dims(mt->tiling, mt->tr_mode, mt->cpp,
638 &tile_width, &tile_height);
639
640 aligned_y = ALIGN(mt->total_height, tile_height);
641 stride = mt->total_width * mt->cpp;
642 stride = ALIGN(stride, tile_width);
643 size = stride * aligned_y;
644
645 if (mt->tr_mode == INTEL_MIPTREE_TRMODE_YF) {
646 assert(size % 4096 == 0);
647 *alignment = 4096;
648 } else {
649 assert(size % (64 * 1024) == 0);
650 *alignment = 64 * 1024;
651 }
652 *pitch = stride;
653 return size;
654 }
655
656 static struct intel_mipmap_tree *
657 miptree_create(struct brw_context *brw,
658 GLenum target,
659 mesa_format format,
660 GLuint first_level,
661 GLuint last_level,
662 GLuint width0,
663 GLuint height0,
664 GLuint depth0,
665 GLuint num_samples,
666 uint32_t layout_flags)
667 {
668 struct intel_mipmap_tree *mt;
669 mesa_format tex_format = format;
670 mesa_format etc_format = MESA_FORMAT_NONE;
671 uint32_t alloc_flags = 0;
672
673 format = intel_lower_compressed_format(brw, format);
674
675 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
676
677 assert((layout_flags & MIPTREE_LAYOUT_FOR_BO) == 0);
678 mt = intel_miptree_create_layout(brw, target, format,
679 first_level, last_level, width0,
680 height0, depth0, num_samples,
681 layout_flags);
682 /*
683 * pitch == 0 || height == 0 indicates the null texture
684 */
685 if (!mt || !mt->total_width || !mt->total_height) {
686 intel_miptree_release(&mt);
687 return NULL;
688 }
689
690 if (mt->tiling == (I915_TILING_Y | I915_TILING_X))
691 mt->tiling = I915_TILING_Y;
692
693 if (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD)
694 alloc_flags |= BO_ALLOC_FOR_RENDER;
695
696 unsigned long pitch;
697 mt->etc_format = etc_format;
698
699 if (mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE) {
700 unsigned alignment = 0;
701 unsigned long size;
702 size = intel_get_yf_ys_bo_size(mt, &alignment, &pitch);
703 assert(size);
704 mt->bo = drm_intel_bo_alloc_for_render(brw->bufmgr, "miptree",
705 size, alignment);
706 } else {
707 if (format == MESA_FORMAT_S_UINT8) {
708 /* Align to size of W tile, 64x64. */
709 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
710 ALIGN(mt->total_width, 64),
711 ALIGN(mt->total_height, 64),
712 mt->cpp, &mt->tiling, &pitch,
713 alloc_flags);
714 } else {
715 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
716 mt->total_width, mt->total_height,
717 mt->cpp, &mt->tiling, &pitch,
718 alloc_flags);
719 }
720 }
721
722 mt->pitch = pitch;
723
724 return mt;
725 }
726
727 struct intel_mipmap_tree *
728 intel_miptree_create(struct brw_context *brw,
729 GLenum target,
730 mesa_format format,
731 GLuint first_level,
732 GLuint last_level,
733 GLuint width0,
734 GLuint height0,
735 GLuint depth0,
736 GLuint num_samples,
737 uint32_t layout_flags)
738 {
739 struct intel_mipmap_tree *mt = miptree_create(
740 brw, target, format,
741 first_level, last_level,
742 width0, height0, depth0, num_samples,
743 layout_flags);
744
745 /* If the BO is too large to fit in the aperture, we need to use the
746 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
747 * handle Y-tiling, so we need to fall back to X.
748 */
749 if (brw->gen < 6 && mt->bo->size >= brw->max_gtt_map_object_size &&
750 mt->tiling == I915_TILING_Y) {
751 unsigned long pitch = mt->pitch;
752 const uint32_t alloc_flags =
753 (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD) ?
754 BO_ALLOC_FOR_RENDER : 0;
755 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
756 mt->total_width, mt->total_height);
757
758 mt->tiling = I915_TILING_X;
759 drm_intel_bo_unreference(mt->bo);
760 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
761 mt->total_width, mt->total_height, mt->cpp,
762 &mt->tiling, &pitch, alloc_flags);
763 mt->pitch = pitch;
764 }
765
766 mt->offset = 0;
767
768 if (!mt->bo) {
769 intel_miptree_release(&mt);
770 return NULL;
771 }
772
773
774 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
775 assert(mt->num_samples > 1);
776 if (!intel_miptree_alloc_mcs(brw, mt, num_samples)) {
777 intel_miptree_release(&mt);
778 return NULL;
779 }
780 }
781
782 /* If this miptree is capable of supporting fast color clears, set
783 * fast_clear_state appropriately to ensure that fast clears will occur.
784 * Allocation of the MCS miptree will be deferred until the first fast
785 * clear actually occurs or when compressed single sampled buffer is
786 * written by the GPU for the first time.
787 */
788 if (intel_tiling_supports_non_msrt_mcs(brw, mt->tiling) &&
789 intel_miptree_supports_non_msrt_fast_clear(brw, mt)) {
790 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
791 assert(brw->gen < 8 || mt->halign == 16 || num_samples <= 1);
792
793 /* On Gen9+ clients are not currently capable of consuming compressed
794 * single-sampled buffers. Disabling compression allows us to skip
795 * resolves.
796 */
797 const bool lossless_compression_disabled = INTEL_DEBUG & DEBUG_NO_RBC;
798 const bool is_lossless_compressed =
799 unlikely(!lossless_compression_disabled) &&
800 brw->gen >= 9 && !mt->is_scanout &&
801 intel_miptree_supports_lossless_compressed(brw, mt);
802
803 if (is_lossless_compressed) {
804 intel_miptree_alloc_non_msrt_mcs(brw, mt, is_lossless_compressed);
805 }
806 }
807
808 return mt;
809 }
810
811 struct intel_mipmap_tree *
812 intel_miptree_create_for_bo(struct brw_context *brw,
813 drm_intel_bo *bo,
814 mesa_format format,
815 uint32_t offset,
816 uint32_t width,
817 uint32_t height,
818 uint32_t depth,
819 int pitch,
820 uint32_t layout_flags)
821 {
822 struct intel_mipmap_tree *mt;
823 uint32_t tiling, swizzle;
824 GLenum target;
825
826 drm_intel_bo_get_tiling(bo, &tiling, &swizzle);
827
828 /* Nothing will be able to use this miptree with the BO if the offset isn't
829 * aligned.
830 */
831 if (tiling != I915_TILING_NONE)
832 assert(offset % 4096 == 0);
833
834 /* miptrees can't handle negative pitch. If you need flipping of images,
835 * that's outside of the scope of the mt.
836 */
837 assert(pitch >= 0);
838
839 target = depth > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
840
841 /* The BO already has a tiling format and we shouldn't confuse the lower
842 * layers by making it try to find a tiling format again.
843 */
844 assert((layout_flags & MIPTREE_LAYOUT_TILING_ANY) == 0);
845 assert((layout_flags & MIPTREE_LAYOUT_TILING_NONE) == 0);
846
847 layout_flags |= MIPTREE_LAYOUT_FOR_BO;
848 mt = intel_miptree_create_layout(brw, target, format,
849 0, 0,
850 width, height, depth, 0,
851 layout_flags);
852 if (!mt)
853 return NULL;
854
855 drm_intel_bo_reference(bo);
856 mt->bo = bo;
857 mt->pitch = pitch;
858 mt->offset = offset;
859 mt->tiling = tiling;
860
861 return mt;
862 }
863
864 /**
865 * For a singlesample renderbuffer, this simply wraps the given BO with a
866 * miptree.
867 *
868 * For a multisample renderbuffer, this wraps the window system's
869 * (singlesample) BO with a singlesample miptree attached to the
870 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
871 * that will contain the actual rendering (which is lazily resolved to
872 * irb->singlesample_mt).
873 */
874 void
875 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
876 struct intel_renderbuffer *irb,
877 drm_intel_bo *bo,
878 uint32_t width, uint32_t height,
879 uint32_t pitch)
880 {
881 struct intel_mipmap_tree *singlesample_mt = NULL;
882 struct intel_mipmap_tree *multisample_mt = NULL;
883 struct gl_renderbuffer *rb = &irb->Base.Base;
884 mesa_format format = rb->Format;
885 int num_samples = rb->NumSamples;
886
887 /* Only the front and back buffers, which are color buffers, are allocated
888 * through the image loader.
889 */
890 assert(_mesa_get_format_base_format(format) == GL_RGB ||
891 _mesa_get_format_base_format(format) == GL_RGBA);
892
893 singlesample_mt = intel_miptree_create_for_bo(intel,
894 bo,
895 format,
896 0,
897 width,
898 height,
899 1,
900 pitch,
901 MIPTREE_LAYOUT_FOR_SCANOUT);
902 if (!singlesample_mt)
903 goto fail;
904
905 /* If this miptree is capable of supporting fast color clears, set
906 * mcs_state appropriately to ensure that fast clears will occur.
907 * Allocation of the MCS miptree will be deferred until the first fast
908 * clear actually occurs.
909 */
910 if (intel_tiling_supports_non_msrt_mcs(intel, singlesample_mt->tiling) &&
911 intel_miptree_supports_non_msrt_fast_clear(intel, singlesample_mt)) {
912 singlesample_mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
913 }
914
915 if (num_samples == 0) {
916 intel_miptree_release(&irb->mt);
917 irb->mt = singlesample_mt;
918
919 assert(!irb->singlesample_mt);
920 } else {
921 intel_miptree_release(&irb->singlesample_mt);
922 irb->singlesample_mt = singlesample_mt;
923
924 if (!irb->mt ||
925 irb->mt->logical_width0 != width ||
926 irb->mt->logical_height0 != height) {
927 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
928 format,
929 width,
930 height,
931 num_samples);
932 if (!multisample_mt)
933 goto fail;
934
935 irb->need_downsample = false;
936 intel_miptree_release(&irb->mt);
937 irb->mt = multisample_mt;
938 }
939 }
940 return;
941
942 fail:
943 intel_miptree_release(&irb->singlesample_mt);
944 intel_miptree_release(&irb->mt);
945 return;
946 }
947
948 struct intel_mipmap_tree*
949 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
950 mesa_format format,
951 uint32_t width,
952 uint32_t height,
953 uint32_t num_samples)
954 {
955 struct intel_mipmap_tree *mt;
956 uint32_t depth = 1;
957 bool ok;
958 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
959 const uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
960 MIPTREE_LAYOUT_TILING_ANY |
961 MIPTREE_LAYOUT_FOR_SCANOUT;
962
963 mt = intel_miptree_create(brw, target, format, 0, 0,
964 width, height, depth, num_samples,
965 layout_flags);
966 if (!mt)
967 goto fail;
968
969 if (intel_miptree_wants_hiz_buffer(brw, mt)) {
970 ok = intel_miptree_alloc_hiz(brw, mt);
971 if (!ok)
972 goto fail;
973 }
974
975 return mt;
976
977 fail:
978 intel_miptree_release(&mt);
979 return NULL;
980 }
981
982 void
983 intel_miptree_reference(struct intel_mipmap_tree **dst,
984 struct intel_mipmap_tree *src)
985 {
986 if (*dst == src)
987 return;
988
989 intel_miptree_release(dst);
990
991 if (src) {
992 src->refcount++;
993 DBG("%s %p refcount now %d\n", __func__, src, src->refcount);
994 }
995
996 *dst = src;
997 }
998
999
1000 void
1001 intel_miptree_release(struct intel_mipmap_tree **mt)
1002 {
1003 if (!*mt)
1004 return;
1005
1006 DBG("%s %p refcount will be %d\n", __func__, *mt, (*mt)->refcount - 1);
1007 if (--(*mt)->refcount <= 0) {
1008 GLuint i;
1009
1010 DBG("%s deleting %p\n", __func__, *mt);
1011
1012 drm_intel_bo_unreference((*mt)->bo);
1013 intel_miptree_release(&(*mt)->stencil_mt);
1014 intel_miptree_release(&(*mt)->r8stencil_mt);
1015 if ((*mt)->hiz_buf) {
1016 if ((*mt)->hiz_buf->mt)
1017 intel_miptree_release(&(*mt)->hiz_buf->mt);
1018 else
1019 drm_intel_bo_unreference((*mt)->hiz_buf->bo);
1020 free((*mt)->hiz_buf);
1021 }
1022 if ((*mt)->mcs_buf) {
1023 intel_miptree_release(&(*mt)->mcs_buf->mt);
1024 free((*mt)->mcs_buf);
1025 }
1026 intel_resolve_map_clear(&(*mt)->hiz_map);
1027
1028 intel_miptree_release(&(*mt)->plane[0]);
1029 intel_miptree_release(&(*mt)->plane[1]);
1030
1031 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
1032 free((*mt)->level[i].slice);
1033 }
1034
1035 free(*mt);
1036 }
1037 *mt = NULL;
1038 }
1039
1040
1041 void
1042 intel_get_image_dims(struct gl_texture_image *image,
1043 int *width, int *height, int *depth)
1044 {
1045 switch (image->TexObject->Target) {
1046 case GL_TEXTURE_1D_ARRAY:
1047 /* For a 1D Array texture the OpenGL API will treat the image height as
1048 * the number of array slices. For Intel hardware, we treat the 1D array
1049 * as a 2D Array with a height of 1. So, here we want to swap image
1050 * height and depth.
1051 */
1052 assert(image->Depth == 1);
1053 *width = image->Width;
1054 *height = 1;
1055 *depth = image->Height;
1056 break;
1057 case GL_TEXTURE_CUBE_MAP:
1058 /* For Cube maps, the mesa/main api layer gives us a depth of 1 even
1059 * though we really have 6 slices.
1060 */
1061 assert(image->Depth == 1);
1062 *width = image->Width;
1063 *height = image->Height;
1064 *depth = 6;
1065 break;
1066 default:
1067 *width = image->Width;
1068 *height = image->Height;
1069 *depth = image->Depth;
1070 break;
1071 }
1072 }
1073
1074 /**
1075 * Can the image be pulled into a unified mipmap tree? This mirrors
1076 * the completeness test in a lot of ways.
1077 *
1078 * Not sure whether I want to pass gl_texture_image here.
1079 */
1080 bool
1081 intel_miptree_match_image(struct intel_mipmap_tree *mt,
1082 struct gl_texture_image *image)
1083 {
1084 struct intel_texture_image *intelImage = intel_texture_image(image);
1085 GLuint level = intelImage->base.Base.Level;
1086 int width, height, depth;
1087
1088 /* glTexImage* choose the texture object based on the target passed in, and
1089 * objects can't change targets over their lifetimes, so this should be
1090 * true.
1091 */
1092 assert(image->TexObject->Target == mt->target);
1093
1094 mesa_format mt_format = mt->format;
1095 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
1096 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
1097 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
1098 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
1099 if (mt->etc_format != MESA_FORMAT_NONE)
1100 mt_format = mt->etc_format;
1101
1102 if (image->TexFormat != mt_format)
1103 return false;
1104
1105 intel_get_image_dims(image, &width, &height, &depth);
1106
1107 if (mt->target == GL_TEXTURE_CUBE_MAP)
1108 depth = 6;
1109
1110 int level_depth = mt->level[level].depth;
1111 if (mt->num_samples > 1) {
1112 switch (mt->msaa_layout) {
1113 case INTEL_MSAA_LAYOUT_NONE:
1114 case INTEL_MSAA_LAYOUT_IMS:
1115 break;
1116 case INTEL_MSAA_LAYOUT_UMS:
1117 case INTEL_MSAA_LAYOUT_CMS:
1118 level_depth /= mt->num_samples;
1119 break;
1120 }
1121 }
1122
1123 /* Test image dimensions against the base level image adjusted for
1124 * minification. This will also catch images not present in the
1125 * tree, changed targets, etc.
1126 */
1127 if (width != minify(mt->logical_width0, level - mt->first_level) ||
1128 height != minify(mt->logical_height0, level - mt->first_level) ||
1129 depth != level_depth) {
1130 return false;
1131 }
1132
1133 if (image->NumSamples != mt->num_samples)
1134 return false;
1135
1136 return true;
1137 }
1138
1139
1140 void
1141 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
1142 GLuint level,
1143 GLuint x, GLuint y, GLuint d)
1144 {
1145 mt->level[level].depth = d;
1146 mt->level[level].level_x = x;
1147 mt->level[level].level_y = y;
1148
1149 DBG("%s level %d, depth %d, offset %d,%d\n", __func__,
1150 level, d, x, y);
1151
1152 assert(mt->level[level].slice == NULL);
1153
1154 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
1155 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
1156 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
1157 }
1158
1159
1160 void
1161 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
1162 GLuint level, GLuint img,
1163 GLuint x, GLuint y)
1164 {
1165 if (img == 0 && level == 0)
1166 assert(x == 0 && y == 0);
1167
1168 assert(img < mt->level[level].depth);
1169
1170 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
1171 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
1172
1173 DBG("%s level %d img %d pos %d,%d\n",
1174 __func__, level, img,
1175 mt->level[level].slice[img].x_offset,
1176 mt->level[level].slice[img].y_offset);
1177 }
1178
1179 void
1180 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
1181 GLuint level, GLuint slice,
1182 GLuint *x, GLuint *y)
1183 {
1184 assert(slice < mt->level[level].depth);
1185
1186 *x = mt->level[level].slice[slice].x_offset;
1187 *y = mt->level[level].slice[slice].y_offset;
1188 }
1189
1190
1191 /**
1192 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1193 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1194 * and tile_h is set to 1.
1195 */
1196 void
1197 intel_get_tile_dims(uint32_t tiling, uint32_t tr_mode, uint32_t cpp,
1198 uint32_t *tile_w, uint32_t *tile_h)
1199 {
1200 if (tr_mode == INTEL_MIPTREE_TRMODE_NONE) {
1201 switch (tiling) {
1202 case I915_TILING_X:
1203 *tile_w = 512;
1204 *tile_h = 8;
1205 break;
1206 case I915_TILING_Y:
1207 *tile_w = 128;
1208 *tile_h = 32;
1209 break;
1210 case I915_TILING_NONE:
1211 *tile_w = cpp;
1212 *tile_h = 1;
1213 break;
1214 default:
1215 unreachable("not reached");
1216 }
1217 } else {
1218 uint32_t aspect_ratio = 1;
1219 assert(_mesa_is_pow_two(cpp));
1220
1221 switch (cpp) {
1222 case 1:
1223 *tile_h = 64;
1224 break;
1225 case 2:
1226 case 4:
1227 *tile_h = 32;
1228 break;
1229 case 8:
1230 case 16:
1231 *tile_h = 16;
1232 break;
1233 default:
1234 unreachable("not reached");
1235 }
1236
1237 if (cpp == 2 || cpp == 8)
1238 aspect_ratio = 2;
1239
1240 if (tr_mode == INTEL_MIPTREE_TRMODE_YS)
1241 *tile_h *= 4;
1242
1243 *tile_w = *tile_h * aspect_ratio * cpp;
1244 }
1245 }
1246
1247
1248 /**
1249 * This function computes masks that may be used to select the bits of the X
1250 * and Y coordinates that indicate the offset within a tile. If the BO is
1251 * untiled, the masks are set to 0.
1252 */
1253 void
1254 intel_get_tile_masks(uint32_t tiling, uint32_t tr_mode, uint32_t cpp,
1255 uint32_t *mask_x, uint32_t *mask_y)
1256 {
1257 uint32_t tile_w_bytes, tile_h;
1258
1259 intel_get_tile_dims(tiling, tr_mode, cpp, &tile_w_bytes, &tile_h);
1260
1261 *mask_x = tile_w_bytes / cpp - 1;
1262 *mask_y = tile_h - 1;
1263 }
1264
1265 /**
1266 * Compute the offset (in bytes) from the start of the BO to the given x
1267 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1268 * multiples of the tile size.
1269 */
1270 uint32_t
1271 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree *mt,
1272 uint32_t x, uint32_t y)
1273 {
1274 int cpp = mt->cpp;
1275 uint32_t pitch = mt->pitch;
1276 uint32_t tiling = mt->tiling;
1277
1278 switch (tiling) {
1279 default:
1280 unreachable("not reached");
1281 case I915_TILING_NONE:
1282 return y * pitch + x * cpp;
1283 case I915_TILING_X:
1284 assert((x % (512 / cpp)) == 0);
1285 assert((y % 8) == 0);
1286 return y * pitch + x / (512 / cpp) * 4096;
1287 case I915_TILING_Y:
1288 assert((x % (128 / cpp)) == 0);
1289 assert((y % 32) == 0);
1290 return y * pitch + x / (128 / cpp) * 4096;
1291 }
1292 }
1293
1294 /**
1295 * Rendering with tiled buffers requires that the base address of the buffer
1296 * be aligned to a page boundary. For renderbuffers, and sometimes with
1297 * textures, we may want the surface to point at a texture image level that
1298 * isn't at a page boundary.
1299 *
1300 * This function returns an appropriately-aligned base offset
1301 * according to the tiling restrictions, plus any required x/y offset
1302 * from there.
1303 */
1304 uint32_t
1305 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
1306 GLuint level, GLuint slice,
1307 uint32_t *tile_x,
1308 uint32_t *tile_y)
1309 {
1310 uint32_t x, y;
1311 uint32_t mask_x, mask_y;
1312
1313 intel_get_tile_masks(mt->tiling, mt->tr_mode, mt->cpp, &mask_x, &mask_y);
1314 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
1315
1316 *tile_x = x & mask_x;
1317 *tile_y = y & mask_y;
1318
1319 return intel_miptree_get_aligned_offset(mt, x & ~mask_x, y & ~mask_y);
1320 }
1321
1322 static void
1323 intel_miptree_copy_slice_sw(struct brw_context *brw,
1324 struct intel_mipmap_tree *dst_mt,
1325 struct intel_mipmap_tree *src_mt,
1326 int level,
1327 int slice,
1328 int width,
1329 int height)
1330 {
1331 void *src, *dst;
1332 ptrdiff_t src_stride, dst_stride;
1333 int cpp = dst_mt->cpp;
1334
1335 intel_miptree_map(brw, src_mt,
1336 level, slice,
1337 0, 0,
1338 width, height,
1339 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
1340 &src, &src_stride);
1341
1342 intel_miptree_map(brw, dst_mt,
1343 level, slice,
1344 0, 0,
1345 width, height,
1346 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
1347 BRW_MAP_DIRECT_BIT,
1348 &dst, &dst_stride);
1349
1350 DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
1351 _mesa_get_format_name(src_mt->format),
1352 src_mt, src, src_stride,
1353 _mesa_get_format_name(dst_mt->format),
1354 dst_mt, dst, dst_stride,
1355 width, height);
1356
1357 int row_size = cpp * width;
1358 if (src_stride == row_size &&
1359 dst_stride == row_size) {
1360 memcpy(dst, src, row_size * height);
1361 } else {
1362 for (int i = 0; i < height; i++) {
1363 memcpy(dst, src, row_size);
1364 dst += dst_stride;
1365 src += src_stride;
1366 }
1367 }
1368
1369 intel_miptree_unmap(brw, dst_mt, level, slice);
1370 intel_miptree_unmap(brw, src_mt, level, slice);
1371
1372 /* Don't forget to copy the stencil data over, too. We could have skipped
1373 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1374 * shuffling the two data sources in/out of temporary storage instead of
1375 * the direct mapping we get this way.
1376 */
1377 if (dst_mt->stencil_mt) {
1378 assert(src_mt->stencil_mt);
1379 intel_miptree_copy_slice_sw(brw, dst_mt->stencil_mt, src_mt->stencil_mt,
1380 level, slice, width, height);
1381 }
1382 }
1383
1384 static void
1385 intel_miptree_copy_slice(struct brw_context *brw,
1386 struct intel_mipmap_tree *dst_mt,
1387 struct intel_mipmap_tree *src_mt,
1388 int level,
1389 int face,
1390 int depth)
1391
1392 {
1393 mesa_format format = src_mt->format;
1394 uint32_t width = minify(src_mt->physical_width0, level - src_mt->first_level);
1395 uint32_t height = minify(src_mt->physical_height0, level - src_mt->first_level);
1396 int slice;
1397
1398 if (face > 0)
1399 slice = face;
1400 else
1401 slice = depth;
1402
1403 assert(depth < src_mt->level[level].depth);
1404 assert(src_mt->format == dst_mt->format);
1405
1406 if (dst_mt->compressed) {
1407 unsigned int i, j;
1408 _mesa_get_format_block_size(dst_mt->format, &i, &j);
1409 height = ALIGN_NPOT(height, j) / j;
1410 width = ALIGN_NPOT(width, i) / i;
1411 }
1412
1413 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1414 * below won't apply since we can't do the depth's Y tiling or the
1415 * stencil's W tiling in the blitter.
1416 */
1417 if (src_mt->stencil_mt) {
1418 intel_miptree_copy_slice_sw(brw,
1419 dst_mt, src_mt,
1420 level, slice,
1421 width, height);
1422 return;
1423 }
1424
1425 uint32_t dst_x, dst_y, src_x, src_y;
1426 intel_miptree_get_image_offset(dst_mt, level, slice, &dst_x, &dst_y);
1427 intel_miptree_get_image_offset(src_mt, level, slice, &src_x, &src_y);
1428
1429 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1430 _mesa_get_format_name(src_mt->format),
1431 src_mt, src_x, src_y, src_mt->pitch,
1432 _mesa_get_format_name(dst_mt->format),
1433 dst_mt, dst_x, dst_y, dst_mt->pitch,
1434 width, height);
1435
1436 if (!intel_miptree_blit(brw,
1437 src_mt, level, slice, 0, 0, false,
1438 dst_mt, level, slice, 0, 0, false,
1439 width, height, GL_COPY)) {
1440 perf_debug("miptree validate blit for %s failed\n",
1441 _mesa_get_format_name(format));
1442
1443 intel_miptree_copy_slice_sw(brw, dst_mt, src_mt, level, slice,
1444 width, height);
1445 }
1446 }
1447
1448 /**
1449 * Copies the image's current data to the given miptree, and associates that
1450 * miptree with the image.
1451 *
1452 * If \c invalidate is true, then the actual image data does not need to be
1453 * copied, but the image still needs to be associated to the new miptree (this
1454 * is set to true if we're about to clear the image).
1455 */
1456 void
1457 intel_miptree_copy_teximage(struct brw_context *brw,
1458 struct intel_texture_image *intelImage,
1459 struct intel_mipmap_tree *dst_mt,
1460 bool invalidate)
1461 {
1462 struct intel_mipmap_tree *src_mt = intelImage->mt;
1463 struct intel_texture_object *intel_obj =
1464 intel_texture_object(intelImage->base.Base.TexObject);
1465 int level = intelImage->base.Base.Level;
1466 int face = intelImage->base.Base.Face;
1467
1468 GLuint depth;
1469 if (intel_obj->base.Target == GL_TEXTURE_1D_ARRAY)
1470 depth = intelImage->base.Base.Height;
1471 else
1472 depth = intelImage->base.Base.Depth;
1473
1474 if (!invalidate) {
1475 for (int slice = 0; slice < depth; slice++) {
1476 intel_miptree_copy_slice(brw, dst_mt, src_mt, level, face, slice);
1477 }
1478 }
1479
1480 intel_miptree_reference(&intelImage->mt, dst_mt);
1481 intel_obj->needs_validate = true;
1482 }
1483
1484 static void
1485 intel_miptree_init_mcs(struct brw_context *brw,
1486 struct intel_mipmap_tree *mt,
1487 int init_value)
1488 {
1489 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1490 *
1491 * When MCS buffer is enabled and bound to MSRT, it is required that it
1492 * is cleared prior to any rendering.
1493 *
1494 * Since we don't use the MCS buffer for any purpose other than rendering,
1495 * it makes sense to just clear it immediately upon allocation.
1496 *
1497 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1498 */
1499 const int ret = brw_bo_map_gtt(brw, mt->mcs_buf->bo, "miptree");
1500 if (unlikely(ret)) {
1501 fprintf(stderr, "Failed to map mcs buffer into GTT\n");
1502 intel_miptree_release(&mt->mcs_buf->mt);
1503 free(mt->mcs_buf);
1504 return;
1505 }
1506 void *data = mt->mcs_buf->bo->virtual;
1507 memset(data, init_value,
1508 mt->mcs_buf->mt->total_height * mt->mcs_buf->mt->pitch);
1509 drm_intel_bo_unmap(mt->mcs_buf->bo);
1510 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
1511 }
1512
1513 static struct intel_miptree_aux_buffer *
1514 intel_mcs_miptree_buf_create(struct brw_context *brw,
1515 struct intel_mipmap_tree *mt,
1516 mesa_format format,
1517 unsigned mcs_width,
1518 unsigned mcs_height,
1519 uint32_t layout_flags)
1520 {
1521 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1522
1523 if (!buf)
1524 return NULL;
1525
1526 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1527 *
1528 * "The MCS surface must be stored as Tile Y."
1529 */
1530 layout_flags |= MIPTREE_LAYOUT_TILING_Y;
1531 buf->mt = miptree_create(brw,
1532 mt->target,
1533 format,
1534 mt->first_level,
1535 mt->last_level,
1536 mcs_width,
1537 mcs_height,
1538 mt->logical_depth0,
1539 0 /* num_samples */,
1540 layout_flags);
1541 if (!buf->mt) {
1542 free(buf);
1543 return NULL;
1544 }
1545
1546 buf->bo = buf->mt->bo;
1547 buf->pitch = buf->mt->pitch;
1548 buf->qpitch = buf->mt->qpitch;
1549
1550 return buf;
1551 }
1552
1553 static bool
1554 intel_miptree_alloc_mcs(struct brw_context *brw,
1555 struct intel_mipmap_tree *mt,
1556 GLuint num_samples)
1557 {
1558 assert(brw->gen >= 7); /* MCS only used on Gen7+ */
1559 assert(mt->mcs_buf == NULL);
1560 assert(!mt->disable_aux_buffers);
1561
1562 /* Choose the correct format for the MCS buffer. All that really matters
1563 * is that we allocate the right buffer size, since we'll always be
1564 * accessing this miptree using MCS-specific hardware mechanisms, which
1565 * infer the correct format based on num_samples.
1566 */
1567 mesa_format format;
1568 switch (num_samples) {
1569 case 2:
1570 case 4:
1571 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1572 * each sample).
1573 */
1574 format = MESA_FORMAT_R_UNORM8;
1575 break;
1576 case 8:
1577 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1578 * for each sample, plus 8 padding bits).
1579 */
1580 format = MESA_FORMAT_R_UINT32;
1581 break;
1582 case 16:
1583 /* 64 bits/pixel are required for MCS data when using 16x MSAA (4 bits
1584 * for each sample).
1585 */
1586 format = MESA_FORMAT_RG_UINT32;
1587 break;
1588 default:
1589 unreachable("Unrecognized sample count in intel_miptree_alloc_mcs");
1590 };
1591
1592 mt->mcs_buf =
1593 intel_mcs_miptree_buf_create(brw, mt,
1594 format,
1595 mt->logical_width0,
1596 mt->logical_height0,
1597 MIPTREE_LAYOUT_ACCELERATED_UPLOAD);
1598
1599 intel_miptree_init_mcs(brw, mt, 0xFF);
1600
1601 return mt->mcs_buf != NULL;
1602 }
1603
1604
1605 bool
1606 intel_miptree_alloc_non_msrt_mcs(struct brw_context *brw,
1607 struct intel_mipmap_tree *mt,
1608 bool is_lossless_compressed)
1609 {
1610 assert(mt->mcs_buf == NULL);
1611 assert(!mt->disable_aux_buffers);
1612
1613 /* The format of the MCS buffer is opaque to the driver; all that matters
1614 * is that we get its size and pitch right. We'll pretend that the format
1615 * is R32. Since an MCS tile covers 128 blocks horizontally, and a Y-tiled
1616 * R32 buffer is 32 pixels across, we'll need to scale the width down by
1617 * the block width and then a further factor of 4. Since an MCS tile
1618 * covers 256 blocks vertically, and a Y-tiled R32 buffer is 32 rows high,
1619 * we'll need to scale the height down by the block height and then a
1620 * further factor of 8.
1621 */
1622 const mesa_format format = MESA_FORMAT_R_UINT32;
1623 unsigned block_width_px;
1624 unsigned block_height;
1625 intel_get_non_msrt_mcs_alignment(mt, &block_width_px, &block_height);
1626 unsigned width_divisor = block_width_px * 4;
1627 unsigned height_divisor = block_height * 8;
1628
1629 /* The Skylake MCS is twice as tall as the Broadwell MCS.
1630 *
1631 * In pre-Skylake, each bit in the MCS contained the state of 2 cachelines
1632 * in the main surface. In Skylake, it's two bits. The extra bit
1633 * doubles the MCS height, not width, because in Skylake the MCS is always
1634 * Y-tiled.
1635 */
1636 if (brw->gen >= 9)
1637 height_divisor /= 2;
1638
1639 unsigned mcs_width =
1640 ALIGN(mt->logical_width0, width_divisor) / width_divisor;
1641 unsigned mcs_height =
1642 ALIGN(mt->logical_height0, height_divisor) / height_divisor;
1643 assert(mt->logical_depth0 == 1);
1644
1645 uint32_t layout_flags =
1646 (brw->gen >= 8) ? MIPTREE_LAYOUT_FORCE_HALIGN16 : 0;
1647 /* In case of compression mcs buffer needs to be initialised requiring the
1648 * buffer to be immediately mapped to cpu space for writing. Therefore do
1649 * not use the gpu access flag which can cause an unnecessary delay if the
1650 * backing pages happened to be just used by the GPU.
1651 */
1652 if (!is_lossless_compressed)
1653 layout_flags |= MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
1654
1655 mt->mcs_buf = intel_mcs_miptree_buf_create(brw, mt,
1656 format,
1657 mcs_width,
1658 mcs_height,
1659 layout_flags);
1660
1661 /* From Gen9 onwards single-sampled (non-msrt) auxiliary buffers are
1662 * used for lossless compression which requires similar initialisation
1663 * as multi-sample compression.
1664 */
1665 if (is_lossless_compressed) {
1666 /* Hardware sets the auxiliary buffer to all zeroes when it does full
1667 * resolve. Initialize it accordingly in case the first renderer is
1668 * cpu (or other none compression aware party).
1669 *
1670 * This is also explicitly stated in the spec (MCS Buffer for Render
1671 * Target(s)):
1672 * "If Software wants to enable Color Compression without Fast clear,
1673 * Software needs to initialize MCS with zeros."
1674 */
1675 intel_miptree_init_mcs(brw, mt, 0);
1676 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
1677 mt->msaa_layout = INTEL_MSAA_LAYOUT_CMS;
1678 }
1679
1680 return mt->mcs_buf != NULL;
1681 }
1682
1683 /**
1684 * Helper for intel_miptree_alloc_hiz() that sets
1685 * \c mt->level[level].has_hiz. Return true if and only if
1686 * \c has_hiz was set.
1687 */
1688 static bool
1689 intel_miptree_level_enable_hiz(struct brw_context *brw,
1690 struct intel_mipmap_tree *mt,
1691 uint32_t level)
1692 {
1693 assert(mt->hiz_buf);
1694
1695 if (brw->gen >= 8 || brw->is_haswell) {
1696 uint32_t width = minify(mt->physical_width0, level);
1697 uint32_t height = minify(mt->physical_height0, level);
1698
1699 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1700 * and the height is 4 aligned. This allows our HiZ support
1701 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1702 * we can grow the width & height to allow the HiZ op to
1703 * force the proper size alignments.
1704 */
1705 if (level > 0 && ((width & 7) || (height & 3))) {
1706 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1707 return false;
1708 }
1709 }
1710
1711 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1712 mt->level[level].has_hiz = true;
1713 return true;
1714 }
1715
1716
1717 /**
1718 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1719 * buffer dimensions and allocates a bo for the hiz buffer.
1720 */
1721 static struct intel_miptree_aux_buffer *
1722 intel_gen7_hiz_buf_create(struct brw_context *brw,
1723 struct intel_mipmap_tree *mt)
1724 {
1725 unsigned z_width = mt->logical_width0;
1726 unsigned z_height = mt->logical_height0;
1727 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1728 unsigned hz_width, hz_height;
1729 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1730
1731 if (!buf)
1732 return NULL;
1733
1734 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1735 * adjustments required for Z_Height and Z_Width based on multisampling.
1736 */
1737 switch (mt->num_samples) {
1738 case 0:
1739 case 1:
1740 break;
1741 case 2:
1742 case 4:
1743 z_width *= 2;
1744 z_height *= 2;
1745 break;
1746 case 8:
1747 z_width *= 4;
1748 z_height *= 2;
1749 break;
1750 default:
1751 unreachable("unsupported sample count");
1752 }
1753
1754 const unsigned vertical_align = 8; /* 'j' in the docs */
1755 const unsigned H0 = z_height;
1756 const unsigned h0 = ALIGN(H0, vertical_align);
1757 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1758 const unsigned Z0 = z_depth;
1759
1760 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1761 hz_width = ALIGN(z_width, 16);
1762
1763 if (mt->target == GL_TEXTURE_3D) {
1764 unsigned H_i = H0;
1765 unsigned Z_i = Z0;
1766 hz_height = 0;
1767 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1768 unsigned h_i = ALIGN(H_i, vertical_align);
1769 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1770 hz_height += h_i * Z_i;
1771 H_i = minify(H_i, 1);
1772 Z_i = minify(Z_i, 1);
1773 }
1774 /* HZ_Height =
1775 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1776 */
1777 hz_height = DIV_ROUND_UP(hz_height, 2);
1778 } else {
1779 const unsigned hz_qpitch = h0 + h1 + (12 * vertical_align);
1780 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1781 hz_height = DIV_ROUND_UP(hz_qpitch * Z0, 2 * 8) * 8;
1782 }
1783
1784 unsigned long pitch;
1785 uint32_t tiling = I915_TILING_Y;
1786 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1787 hz_width, hz_height, 1,
1788 &tiling, &pitch,
1789 BO_ALLOC_FOR_RENDER);
1790 if (!buf->bo) {
1791 free(buf);
1792 return NULL;
1793 } else if (tiling != I915_TILING_Y) {
1794 drm_intel_bo_unreference(buf->bo);
1795 free(buf);
1796 return NULL;
1797 }
1798
1799 buf->pitch = pitch;
1800
1801 return buf;
1802 }
1803
1804
1805 /**
1806 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1807 * buffer dimensions and allocates a bo for the hiz buffer.
1808 */
1809 static struct intel_miptree_aux_buffer *
1810 intel_gen8_hiz_buf_create(struct brw_context *brw,
1811 struct intel_mipmap_tree *mt)
1812 {
1813 unsigned z_width = mt->logical_width0;
1814 unsigned z_height = mt->logical_height0;
1815 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1816 unsigned hz_width, hz_height;
1817 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1818
1819 if (!buf)
1820 return NULL;
1821
1822 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1823 * adjustments required for Z_Height and Z_Width based on multisampling.
1824 */
1825 if (brw->gen < 9) {
1826 switch (mt->num_samples) {
1827 case 0:
1828 case 1:
1829 break;
1830 case 2:
1831 case 4:
1832 z_width *= 2;
1833 z_height *= 2;
1834 break;
1835 case 8:
1836 z_width *= 4;
1837 z_height *= 2;
1838 break;
1839 default:
1840 unreachable("unsupported sample count");
1841 }
1842 }
1843
1844 const unsigned vertical_align = 8; /* 'j' in the docs */
1845 const unsigned H0 = z_height;
1846 const unsigned h0 = ALIGN(H0, vertical_align);
1847 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1848 const unsigned Z0 = z_depth;
1849
1850 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1851 hz_width = ALIGN(z_width, 16);
1852
1853 unsigned H_i = H0;
1854 unsigned Z_i = Z0;
1855 unsigned sum_h_i = 0;
1856 unsigned hz_height_3d_sum = 0;
1857 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1858 unsigned i = level - mt->first_level;
1859 unsigned h_i = ALIGN(H_i, vertical_align);
1860 /* sum(i=2 to m; h_i) */
1861 if (i >= 2) {
1862 sum_h_i += h_i;
1863 }
1864 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1865 hz_height_3d_sum += h_i * Z_i;
1866 H_i = minify(H_i, 1);
1867 Z_i = minify(Z_i, 1);
1868 }
1869 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1870 buf->qpitch = h0 + MAX2(h1, sum_h_i);
1871
1872 if (mt->target == GL_TEXTURE_3D) {
1873 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1874 hz_height = DIV_ROUND_UP(hz_height_3d_sum, 2);
1875 } else {
1876 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1877 hz_height = DIV_ROUND_UP(buf->qpitch, 2 * 8) * 8 * Z0;
1878 }
1879
1880 unsigned long pitch;
1881 uint32_t tiling = I915_TILING_Y;
1882 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1883 hz_width, hz_height, 1,
1884 &tiling, &pitch,
1885 BO_ALLOC_FOR_RENDER);
1886 if (!buf->bo) {
1887 free(buf);
1888 return NULL;
1889 } else if (tiling != I915_TILING_Y) {
1890 drm_intel_bo_unreference(buf->bo);
1891 free(buf);
1892 return NULL;
1893 }
1894
1895 buf->pitch = pitch;
1896
1897 return buf;
1898 }
1899
1900
1901 static struct intel_miptree_aux_buffer *
1902 intel_hiz_miptree_buf_create(struct brw_context *brw,
1903 struct intel_mipmap_tree *mt)
1904 {
1905 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1906 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
1907
1908 if (brw->gen == 6)
1909 layout_flags |= MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD;
1910
1911 if (!buf)
1912 return NULL;
1913
1914 layout_flags |= MIPTREE_LAYOUT_TILING_ANY;
1915 buf->mt = intel_miptree_create(brw,
1916 mt->target,
1917 mt->format,
1918 mt->first_level,
1919 mt->last_level,
1920 mt->logical_width0,
1921 mt->logical_height0,
1922 mt->logical_depth0,
1923 mt->num_samples,
1924 layout_flags);
1925 if (!buf->mt) {
1926 free(buf);
1927 return NULL;
1928 }
1929
1930 buf->bo = buf->mt->bo;
1931 buf->pitch = buf->mt->pitch;
1932 buf->qpitch = buf->mt->qpitch;
1933
1934 return buf;
1935 }
1936
1937 bool
1938 intel_miptree_wants_hiz_buffer(struct brw_context *brw,
1939 struct intel_mipmap_tree *mt)
1940 {
1941 if (!brw->has_hiz)
1942 return false;
1943
1944 if (mt->hiz_buf != NULL)
1945 return false;
1946
1947 if (mt->disable_aux_buffers)
1948 return false;
1949
1950 switch (mt->format) {
1951 case MESA_FORMAT_Z_FLOAT32:
1952 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1953 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1954 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1955 case MESA_FORMAT_Z_UNORM16:
1956 return true;
1957 default:
1958 return false;
1959 }
1960 }
1961
1962 bool
1963 intel_miptree_alloc_hiz(struct brw_context *brw,
1964 struct intel_mipmap_tree *mt)
1965 {
1966 assert(mt->hiz_buf == NULL);
1967 assert(!mt->disable_aux_buffers);
1968
1969 if (brw->gen == 7) {
1970 mt->hiz_buf = intel_gen7_hiz_buf_create(brw, mt);
1971 } else if (brw->gen >= 8) {
1972 mt->hiz_buf = intel_gen8_hiz_buf_create(brw, mt);
1973 } else {
1974 mt->hiz_buf = intel_hiz_miptree_buf_create(brw, mt);
1975 }
1976
1977 if (!mt->hiz_buf)
1978 return false;
1979
1980 /* Mark that all slices need a HiZ resolve. */
1981 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1982 if (!intel_miptree_level_enable_hiz(brw, mt, level))
1983 continue;
1984
1985 for (unsigned layer = 0; layer < mt->level[level].depth; ++layer) {
1986 struct intel_resolve_map *m = malloc(sizeof(struct intel_resolve_map));
1987 exec_node_init(&m->link);
1988 m->level = level;
1989 m->layer = layer;
1990 m->need = BLORP_HIZ_OP_HIZ_RESOLVE;
1991
1992 exec_list_push_tail(&mt->hiz_map, &m->link);
1993 }
1994 }
1995
1996 return true;
1997 }
1998
1999 /**
2000 * Does the miptree slice have hiz enabled?
2001 */
2002 bool
2003 intel_miptree_level_has_hiz(struct intel_mipmap_tree *mt, uint32_t level)
2004 {
2005 intel_miptree_check_level_layer(mt, level, 0);
2006 return mt->level[level].has_hiz;
2007 }
2008
2009 void
2010 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
2011 uint32_t level,
2012 uint32_t layer)
2013 {
2014 if (!intel_miptree_level_has_hiz(mt, level))
2015 return;
2016
2017 intel_resolve_map_set(&mt->hiz_map,
2018 level, layer, BLORP_HIZ_OP_HIZ_RESOLVE);
2019 }
2020
2021
2022 void
2023 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
2024 uint32_t level,
2025 uint32_t layer)
2026 {
2027 if (!intel_miptree_level_has_hiz(mt, level))
2028 return;
2029
2030 intel_resolve_map_set(&mt->hiz_map,
2031 level, layer, BLORP_HIZ_OP_DEPTH_RESOLVE);
2032 }
2033
2034 void
2035 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
2036 uint32_t level)
2037 {
2038 uint32_t layer;
2039 uint32_t end_layer = mt->level[level].depth;
2040
2041 for (layer = 0; layer < end_layer; layer++) {
2042 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
2043 }
2044 }
2045
2046 static bool
2047 intel_miptree_slice_resolve(struct brw_context *brw,
2048 struct intel_mipmap_tree *mt,
2049 uint32_t level,
2050 uint32_t layer,
2051 enum blorp_hiz_op need)
2052 {
2053 intel_miptree_check_level_layer(mt, level, layer);
2054
2055 struct intel_resolve_map *item =
2056 intel_resolve_map_get(&mt->hiz_map, level, layer);
2057
2058 if (!item || item->need != need)
2059 return false;
2060
2061 intel_hiz_exec(brw, mt, level, layer, need);
2062 intel_resolve_map_remove(item);
2063 return true;
2064 }
2065
2066 bool
2067 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
2068 struct intel_mipmap_tree *mt,
2069 uint32_t level,
2070 uint32_t layer)
2071 {
2072 return intel_miptree_slice_resolve(brw, mt, level, layer,
2073 BLORP_HIZ_OP_HIZ_RESOLVE);
2074 }
2075
2076 bool
2077 intel_miptree_slice_resolve_depth(struct brw_context *brw,
2078 struct intel_mipmap_tree *mt,
2079 uint32_t level,
2080 uint32_t layer)
2081 {
2082 return intel_miptree_slice_resolve(brw, mt, level, layer,
2083 BLORP_HIZ_OP_DEPTH_RESOLVE);
2084 }
2085
2086 static bool
2087 intel_miptree_all_slices_resolve(struct brw_context *brw,
2088 struct intel_mipmap_tree *mt,
2089 enum blorp_hiz_op need)
2090 {
2091 bool did_resolve = false;
2092
2093 foreach_list_typed_safe(struct intel_resolve_map, map, link, &mt->hiz_map) {
2094 if (map->need != need)
2095 continue;
2096
2097 intel_hiz_exec(brw, mt, map->level, map->layer, need);
2098 intel_resolve_map_remove(map);
2099 did_resolve = true;
2100 }
2101
2102 return did_resolve;
2103 }
2104
2105 bool
2106 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
2107 struct intel_mipmap_tree *mt)
2108 {
2109 return intel_miptree_all_slices_resolve(brw, mt,
2110 BLORP_HIZ_OP_HIZ_RESOLVE);
2111 }
2112
2113 bool
2114 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
2115 struct intel_mipmap_tree *mt)
2116 {
2117 return intel_miptree_all_slices_resolve(brw, mt,
2118 BLORP_HIZ_OP_DEPTH_RESOLVE);
2119 }
2120
2121
2122 bool
2123 intel_miptree_resolve_color(struct brw_context *brw,
2124 struct intel_mipmap_tree *mt,
2125 int flags)
2126 {
2127 /* From gen9 onwards there is new compression scheme for single sampled
2128 * surfaces called "lossless compressed". These don't need to be always
2129 * resolved.
2130 */
2131 if ((flags & INTEL_MIPTREE_IGNORE_CCS_E) &&
2132 intel_miptree_is_lossless_compressed(brw, mt))
2133 return false;
2134
2135 switch (mt->fast_clear_state) {
2136 case INTEL_FAST_CLEAR_STATE_NO_MCS:
2137 case INTEL_FAST_CLEAR_STATE_RESOLVED:
2138 /* No resolve needed */
2139 return false;
2140 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
2141 case INTEL_FAST_CLEAR_STATE_CLEAR:
2142 /* Fast color clear resolves only make sense for non-MSAA buffers. */
2143 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE ||
2144 intel_miptree_is_lossless_compressed(brw, mt)) {
2145 brw_blorp_resolve_color(brw, mt);
2146 return true;
2147 } else {
2148 return false;
2149 }
2150 default:
2151 unreachable("Invalid fast clear state");
2152 }
2153 }
2154
2155
2156 /**
2157 * Make it possible to share the BO backing the given miptree with another
2158 * process or another miptree.
2159 *
2160 * Fast color clears are unsafe with shared buffers, so we need to resolve and
2161 * then discard the MCS buffer, if present. We also set the fast_clear_state
2162 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
2163 * allocated in the future.
2164 */
2165 void
2166 intel_miptree_make_shareable(struct brw_context *brw,
2167 struct intel_mipmap_tree *mt)
2168 {
2169 /* MCS buffers are also used for multisample buffers, but we can't resolve
2170 * away a multisample MCS buffer because it's an integral part of how the
2171 * pixel data is stored. Fortunately this code path should never be
2172 * reached for multisample buffers.
2173 */
2174 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE);
2175
2176 if (mt->mcs_buf) {
2177 intel_miptree_resolve_color(brw, mt, 0);
2178 intel_miptree_release(&mt->mcs_buf->mt);
2179 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
2180 }
2181 }
2182
2183
2184 /**
2185 * \brief Get pointer offset into stencil buffer.
2186 *
2187 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2188 * must decode the tile's layout in software.
2189 *
2190 * See
2191 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2192 * Format.
2193 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2194 *
2195 * Even though the returned offset is always positive, the return type is
2196 * signed due to
2197 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2198 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2199 */
2200 static intptr_t
2201 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2202 {
2203 uint32_t tile_size = 4096;
2204 uint32_t tile_width = 64;
2205 uint32_t tile_height = 64;
2206 uint32_t row_size = 64 * stride;
2207
2208 uint32_t tile_x = x / tile_width;
2209 uint32_t tile_y = y / tile_height;
2210
2211 /* The byte's address relative to the tile's base addres. */
2212 uint32_t byte_x = x % tile_width;
2213 uint32_t byte_y = y % tile_height;
2214
2215 uintptr_t u = tile_y * row_size
2216 + tile_x * tile_size
2217 + 512 * (byte_x / 8)
2218 + 64 * (byte_y / 8)
2219 + 32 * ((byte_y / 4) % 2)
2220 + 16 * ((byte_x / 4) % 2)
2221 + 8 * ((byte_y / 2) % 2)
2222 + 4 * ((byte_x / 2) % 2)
2223 + 2 * (byte_y % 2)
2224 + 1 * (byte_x % 2);
2225
2226 if (swizzled) {
2227 /* adjust for bit6 swizzling */
2228 if (((byte_x / 8) % 2) == 1) {
2229 if (((byte_y / 8) % 2) == 0) {
2230 u += 64;
2231 } else {
2232 u -= 64;
2233 }
2234 }
2235 }
2236
2237 return u;
2238 }
2239
2240 void
2241 intel_miptree_updownsample(struct brw_context *brw,
2242 struct intel_mipmap_tree *src,
2243 struct intel_mipmap_tree *dst)
2244 {
2245 brw_blorp_blit_miptrees(brw,
2246 src, 0 /* level */, 0 /* layer */,
2247 src->format, SWIZZLE_XYZW,
2248 dst, 0 /* level */, 0 /* layer */, dst->format,
2249 0, 0,
2250 src->logical_width0, src->logical_height0,
2251 0, 0,
2252 dst->logical_width0, dst->logical_height0,
2253 GL_NEAREST, false, false /*mirror x, y*/,
2254 false, false);
2255
2256 if (src->stencil_mt) {
2257 brw_blorp_blit_miptrees(brw,
2258 src->stencil_mt, 0 /* level */, 0 /* layer */,
2259 src->stencil_mt->format, SWIZZLE_XYZW,
2260 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2261 dst->stencil_mt->format,
2262 0, 0,
2263 src->logical_width0, src->logical_height0,
2264 0, 0,
2265 dst->logical_width0, dst->logical_height0,
2266 GL_NEAREST, false, false /*mirror x, y*/,
2267 false, false /* decode/encode srgb */);
2268 }
2269 }
2270
2271 void
2272 intel_update_r8stencil(struct brw_context *brw,
2273 struct intel_mipmap_tree *mt)
2274 {
2275 assert(brw->gen >= 7);
2276 struct intel_mipmap_tree *src =
2277 mt->format == MESA_FORMAT_S_UINT8 ? mt : mt->stencil_mt;
2278 if (!src || brw->gen >= 8 || !src->r8stencil_needs_update)
2279 return;
2280
2281 if (!mt->r8stencil_mt) {
2282 const uint32_t r8stencil_flags =
2283 MIPTREE_LAYOUT_ACCELERATED_UPLOAD | MIPTREE_LAYOUT_TILING_Y |
2284 MIPTREE_LAYOUT_DISABLE_AUX;
2285 assert(brw->gen > 6); /* Handle MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD */
2286 mt->r8stencil_mt = intel_miptree_create(brw,
2287 src->target,
2288 MESA_FORMAT_R_UINT8,
2289 src->first_level,
2290 src->last_level,
2291 src->logical_width0,
2292 src->logical_height0,
2293 src->logical_depth0,
2294 src->num_samples,
2295 r8stencil_flags);
2296 assert(mt->r8stencil_mt);
2297 }
2298
2299 struct intel_mipmap_tree *dst = mt->r8stencil_mt;
2300
2301 for (int level = src->first_level; level <= src->last_level; level++) {
2302 const unsigned depth = src->level[level].depth;
2303 const int layers_per_blit =
2304 (dst->msaa_layout == INTEL_MSAA_LAYOUT_UMS ||
2305 dst->msaa_layout == INTEL_MSAA_LAYOUT_CMS) ?
2306 dst->num_samples : 1;
2307
2308 for (unsigned layer = 0; layer < depth; layer++) {
2309 brw_blorp_blit_miptrees(brw,
2310 src, level, layer,
2311 src->format, SWIZZLE_X,
2312 dst, level, layers_per_blit * layer,
2313 MESA_FORMAT_R_UNORM8,
2314 0, 0,
2315 src->logical_width0, src->logical_height0,
2316 0, 0,
2317 dst->logical_width0, dst->logical_height0,
2318 GL_NEAREST, false, false /*mirror x, y*/,
2319 false, false /* decode/encode srgb */);
2320 }
2321 }
2322
2323 brw_render_cache_set_check_flush(brw, dst->bo);
2324 src->r8stencil_needs_update = false;
2325 }
2326
2327 static void *
2328 intel_miptree_map_raw(struct brw_context *brw, struct intel_mipmap_tree *mt)
2329 {
2330 /* CPU accesses to color buffers don't understand fast color clears, so
2331 * resolve any pending fast color clears before we map.
2332 */
2333 intel_miptree_resolve_color(brw, mt, 0);
2334
2335 drm_intel_bo *bo = mt->bo;
2336
2337 if (drm_intel_bo_references(brw->batch.bo, bo))
2338 intel_batchbuffer_flush(brw);
2339
2340 if (mt->tiling != I915_TILING_NONE)
2341 brw_bo_map_gtt(brw, bo, "miptree");
2342 else
2343 brw_bo_map(brw, bo, true, "miptree");
2344
2345 return bo->virtual;
2346 }
2347
2348 static void
2349 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2350 {
2351 drm_intel_bo_unmap(mt->bo);
2352 }
2353
2354 static void
2355 intel_miptree_map_gtt(struct brw_context *brw,
2356 struct intel_mipmap_tree *mt,
2357 struct intel_miptree_map *map,
2358 unsigned int level, unsigned int slice)
2359 {
2360 unsigned int bw, bh;
2361 void *base;
2362 unsigned int image_x, image_y;
2363 intptr_t x = map->x;
2364 intptr_t y = map->y;
2365
2366 /* For compressed formats, the stride is the number of bytes per
2367 * row of blocks. intel_miptree_get_image_offset() already does
2368 * the divide.
2369 */
2370 _mesa_get_format_block_size(mt->format, &bw, &bh);
2371 assert(y % bh == 0);
2372 assert(x % bw == 0);
2373 y /= bh;
2374 x /= bw;
2375
2376 base = intel_miptree_map_raw(brw, mt) + mt->offset;
2377
2378 if (base == NULL)
2379 map->ptr = NULL;
2380 else {
2381 /* Note that in the case of cube maps, the caller must have passed the
2382 * slice number referencing the face.
2383 */
2384 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2385 x += image_x;
2386 y += image_y;
2387
2388 map->stride = mt->pitch;
2389 map->ptr = base + y * map->stride + x * mt->cpp;
2390 }
2391
2392 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2393 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2394 map->x, map->y, map->w, map->h,
2395 mt, _mesa_get_format_name(mt->format),
2396 x, y, map->ptr, map->stride);
2397 }
2398
2399 static void
2400 intel_miptree_unmap_gtt(struct intel_mipmap_tree *mt)
2401 {
2402 intel_miptree_unmap_raw(mt);
2403 }
2404
2405 static void
2406 intel_miptree_map_blit(struct brw_context *brw,
2407 struct intel_mipmap_tree *mt,
2408 struct intel_miptree_map *map,
2409 unsigned int level, unsigned int slice)
2410 {
2411 map->linear_mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
2412 /* first_level */ 0,
2413 /* last_level */ 0,
2414 map->w, map->h, 1,
2415 /* samples */ 0,
2416 MIPTREE_LAYOUT_TILING_NONE);
2417
2418 if (!map->linear_mt) {
2419 fprintf(stderr, "Failed to allocate blit temporary\n");
2420 goto fail;
2421 }
2422 map->stride = map->linear_mt->pitch;
2423
2424 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2425 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2426 * invalidate is set, since we'll be writing the whole rectangle from our
2427 * temporary buffer back out.
2428 */
2429 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2430 if (!intel_miptree_blit(brw,
2431 mt, level, slice,
2432 map->x, map->y, false,
2433 map->linear_mt, 0, 0,
2434 0, 0, false,
2435 map->w, map->h, GL_COPY)) {
2436 fprintf(stderr, "Failed to blit\n");
2437 goto fail;
2438 }
2439 }
2440
2441 map->ptr = intel_miptree_map_raw(brw, map->linear_mt);
2442
2443 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2444 map->x, map->y, map->w, map->h,
2445 mt, _mesa_get_format_name(mt->format),
2446 level, slice, map->ptr, map->stride);
2447
2448 return;
2449
2450 fail:
2451 intel_miptree_release(&map->linear_mt);
2452 map->ptr = NULL;
2453 map->stride = 0;
2454 }
2455
2456 static void
2457 intel_miptree_unmap_blit(struct brw_context *brw,
2458 struct intel_mipmap_tree *mt,
2459 struct intel_miptree_map *map,
2460 unsigned int level,
2461 unsigned int slice)
2462 {
2463 struct gl_context *ctx = &brw->ctx;
2464
2465 intel_miptree_unmap_raw(map->linear_mt);
2466
2467 if (map->mode & GL_MAP_WRITE_BIT) {
2468 bool ok = intel_miptree_blit(brw,
2469 map->linear_mt, 0, 0,
2470 0, 0, false,
2471 mt, level, slice,
2472 map->x, map->y, false,
2473 map->w, map->h, GL_COPY);
2474 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
2475 }
2476
2477 intel_miptree_release(&map->linear_mt);
2478 }
2479
2480 /**
2481 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2482 */
2483 #if defined(USE_SSE41)
2484 static void
2485 intel_miptree_map_movntdqa(struct brw_context *brw,
2486 struct intel_mipmap_tree *mt,
2487 struct intel_miptree_map *map,
2488 unsigned int level, unsigned int slice)
2489 {
2490 assert(map->mode & GL_MAP_READ_BIT);
2491 assert(!(map->mode & GL_MAP_WRITE_BIT));
2492
2493 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2494 map->x, map->y, map->w, map->h,
2495 mt, _mesa_get_format_name(mt->format),
2496 level, slice, map->ptr, map->stride);
2497
2498 /* Map the original image */
2499 uint32_t image_x;
2500 uint32_t image_y;
2501 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2502 image_x += map->x;
2503 image_y += map->y;
2504
2505 void *src = intel_miptree_map_raw(brw, mt);
2506 if (!src)
2507 return;
2508
2509 src += mt->offset;
2510
2511 src += image_y * mt->pitch;
2512 src += image_x * mt->cpp;
2513
2514 /* Due to the pixel offsets for the particular image being mapped, our
2515 * src pointer may not be 16-byte aligned. However, if the pitch is
2516 * divisible by 16, then the amount by which it's misaligned will remain
2517 * consistent from row to row.
2518 */
2519 assert((mt->pitch % 16) == 0);
2520 const int misalignment = ((uintptr_t) src) & 15;
2521
2522 /* Create an untiled temporary buffer for the mapping. */
2523 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
2524
2525 map->stride = ALIGN(misalignment + width_bytes, 16);
2526
2527 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
2528 /* Offset the destination so it has the same misalignment as src. */
2529 map->ptr = map->buffer + misalignment;
2530
2531 assert((((uintptr_t) map->ptr) & 15) == misalignment);
2532
2533 for (uint32_t y = 0; y < map->h; y++) {
2534 void *dst_ptr = map->ptr + y * map->stride;
2535 void *src_ptr = src + y * mt->pitch;
2536
2537 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
2538 }
2539
2540 intel_miptree_unmap_raw(mt);
2541 }
2542
2543 static void
2544 intel_miptree_unmap_movntdqa(struct brw_context *brw,
2545 struct intel_mipmap_tree *mt,
2546 struct intel_miptree_map *map,
2547 unsigned int level,
2548 unsigned int slice)
2549 {
2550 _mesa_align_free(map->buffer);
2551 map->buffer = NULL;
2552 map->ptr = NULL;
2553 }
2554 #endif
2555
2556 static void
2557 intel_miptree_map_s8(struct brw_context *brw,
2558 struct intel_mipmap_tree *mt,
2559 struct intel_miptree_map *map,
2560 unsigned int level, unsigned int slice)
2561 {
2562 map->stride = map->w;
2563 map->buffer = map->ptr = malloc(map->stride * map->h);
2564 if (!map->buffer)
2565 return;
2566
2567 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2568 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2569 * invalidate is set, since we'll be writing the whole rectangle from our
2570 * temporary buffer back out.
2571 */
2572 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2573 uint8_t *untiled_s8_map = map->ptr;
2574 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2575 unsigned int image_x, image_y;
2576
2577 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2578
2579 for (uint32_t y = 0; y < map->h; y++) {
2580 for (uint32_t x = 0; x < map->w; x++) {
2581 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2582 x + image_x + map->x,
2583 y + image_y + map->y,
2584 brw->has_swizzling);
2585 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
2586 }
2587 }
2588
2589 intel_miptree_unmap_raw(mt);
2590
2591 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
2592 map->x, map->y, map->w, map->h,
2593 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
2594 } else {
2595 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2596 map->x, map->y, map->w, map->h,
2597 mt, map->ptr, map->stride);
2598 }
2599 }
2600
2601 static void
2602 intel_miptree_unmap_s8(struct brw_context *brw,
2603 struct intel_mipmap_tree *mt,
2604 struct intel_miptree_map *map,
2605 unsigned int level,
2606 unsigned int slice)
2607 {
2608 if (map->mode & GL_MAP_WRITE_BIT) {
2609 unsigned int image_x, image_y;
2610 uint8_t *untiled_s8_map = map->ptr;
2611 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2612
2613 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2614
2615 for (uint32_t y = 0; y < map->h; y++) {
2616 for (uint32_t x = 0; x < map->w; x++) {
2617 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2618 image_x + x + map->x,
2619 image_y + y + map->y,
2620 brw->has_swizzling);
2621 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
2622 }
2623 }
2624
2625 intel_miptree_unmap_raw(mt);
2626 }
2627
2628 free(map->buffer);
2629 }
2630
2631 static void
2632 intel_miptree_map_etc(struct brw_context *brw,
2633 struct intel_mipmap_tree *mt,
2634 struct intel_miptree_map *map,
2635 unsigned int level,
2636 unsigned int slice)
2637 {
2638 assert(mt->etc_format != MESA_FORMAT_NONE);
2639 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
2640 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
2641 }
2642
2643 assert(map->mode & GL_MAP_WRITE_BIT);
2644 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
2645
2646 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
2647 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
2648 map->w, map->h, 1));
2649 map->ptr = map->buffer;
2650 }
2651
2652 static void
2653 intel_miptree_unmap_etc(struct brw_context *brw,
2654 struct intel_mipmap_tree *mt,
2655 struct intel_miptree_map *map,
2656 unsigned int level,
2657 unsigned int slice)
2658 {
2659 uint32_t image_x;
2660 uint32_t image_y;
2661 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2662
2663 image_x += map->x;
2664 image_y += map->y;
2665
2666 uint8_t *dst = intel_miptree_map_raw(brw, mt)
2667 + image_y * mt->pitch
2668 + image_x * mt->cpp;
2669
2670 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
2671 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
2672 map->ptr, map->stride,
2673 map->w, map->h);
2674 else
2675 _mesa_unpack_etc2_format(dst, mt->pitch,
2676 map->ptr, map->stride,
2677 map->w, map->h, mt->etc_format);
2678
2679 intel_miptree_unmap_raw(mt);
2680 free(map->buffer);
2681 }
2682
2683 /**
2684 * Mapping function for packed depth/stencil miptrees backed by real separate
2685 * miptrees for depth and stencil.
2686 *
2687 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2688 * separate from the depth buffer. Yet at the GL API level, we have to expose
2689 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2690 * be able to map that memory for texture storage and glReadPixels-type
2691 * operations. We give Mesa core that access by mallocing a temporary and
2692 * copying the data between the actual backing store and the temporary.
2693 */
2694 static void
2695 intel_miptree_map_depthstencil(struct brw_context *brw,
2696 struct intel_mipmap_tree *mt,
2697 struct intel_miptree_map *map,
2698 unsigned int level, unsigned int slice)
2699 {
2700 struct intel_mipmap_tree *z_mt = mt;
2701 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2702 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2703 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
2704
2705 map->stride = map->w * packed_bpp;
2706 map->buffer = map->ptr = malloc(map->stride * map->h);
2707 if (!map->buffer)
2708 return;
2709
2710 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2711 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2712 * invalidate is set, since we'll be writing the whole rectangle from our
2713 * temporary buffer back out.
2714 */
2715 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2716 uint32_t *packed_map = map->ptr;
2717 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2718 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2719 unsigned int s_image_x, s_image_y;
2720 unsigned int z_image_x, z_image_y;
2721
2722 intel_miptree_get_image_offset(s_mt, level, slice,
2723 &s_image_x, &s_image_y);
2724 intel_miptree_get_image_offset(z_mt, level, slice,
2725 &z_image_x, &z_image_y);
2726
2727 for (uint32_t y = 0; y < map->h; y++) {
2728 for (uint32_t x = 0; x < map->w; x++) {
2729 int map_x = map->x + x, map_y = map->y + y;
2730 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2731 map_x + s_image_x,
2732 map_y + s_image_y,
2733 brw->has_swizzling);
2734 ptrdiff_t z_offset = ((map_y + z_image_y) *
2735 (z_mt->pitch / 4) +
2736 (map_x + z_image_x));
2737 uint8_t s = s_map[s_offset];
2738 uint32_t z = z_map[z_offset];
2739
2740 if (map_z32f_x24s8) {
2741 packed_map[(y * map->w + x) * 2 + 0] = z;
2742 packed_map[(y * map->w + x) * 2 + 1] = s;
2743 } else {
2744 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
2745 }
2746 }
2747 }
2748
2749 intel_miptree_unmap_raw(s_mt);
2750 intel_miptree_unmap_raw(z_mt);
2751
2752 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
2753 __func__,
2754 map->x, map->y, map->w, map->h,
2755 z_mt, map->x + z_image_x, map->y + z_image_y,
2756 s_mt, map->x + s_image_x, map->y + s_image_y,
2757 map->ptr, map->stride);
2758 } else {
2759 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2760 map->x, map->y, map->w, map->h,
2761 mt, map->ptr, map->stride);
2762 }
2763 }
2764
2765 static void
2766 intel_miptree_unmap_depthstencil(struct brw_context *brw,
2767 struct intel_mipmap_tree *mt,
2768 struct intel_miptree_map *map,
2769 unsigned int level,
2770 unsigned int slice)
2771 {
2772 struct intel_mipmap_tree *z_mt = mt;
2773 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2774 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2775
2776 if (map->mode & GL_MAP_WRITE_BIT) {
2777 uint32_t *packed_map = map->ptr;
2778 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2779 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2780 unsigned int s_image_x, s_image_y;
2781 unsigned int z_image_x, z_image_y;
2782
2783 intel_miptree_get_image_offset(s_mt, level, slice,
2784 &s_image_x, &s_image_y);
2785 intel_miptree_get_image_offset(z_mt, level, slice,
2786 &z_image_x, &z_image_y);
2787
2788 for (uint32_t y = 0; y < map->h; y++) {
2789 for (uint32_t x = 0; x < map->w; x++) {
2790 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2791 x + s_image_x + map->x,
2792 y + s_image_y + map->y,
2793 brw->has_swizzling);
2794 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
2795 (z_mt->pitch / 4) +
2796 (x + z_image_x + map->x));
2797
2798 if (map_z32f_x24s8) {
2799 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
2800 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
2801 } else {
2802 uint32_t packed = packed_map[y * map->w + x];
2803 s_map[s_offset] = packed >> 24;
2804 z_map[z_offset] = packed;
2805 }
2806 }
2807 }
2808
2809 intel_miptree_unmap_raw(s_mt);
2810 intel_miptree_unmap_raw(z_mt);
2811
2812 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2813 __func__,
2814 map->x, map->y, map->w, map->h,
2815 z_mt, _mesa_get_format_name(z_mt->format),
2816 map->x + z_image_x, map->y + z_image_y,
2817 s_mt, map->x + s_image_x, map->y + s_image_y,
2818 map->ptr, map->stride);
2819 }
2820
2821 free(map->buffer);
2822 }
2823
2824 /**
2825 * Create and attach a map to the miptree at (level, slice). Return the
2826 * attached map.
2827 */
2828 static struct intel_miptree_map*
2829 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
2830 unsigned int level,
2831 unsigned int slice,
2832 unsigned int x,
2833 unsigned int y,
2834 unsigned int w,
2835 unsigned int h,
2836 GLbitfield mode)
2837 {
2838 struct intel_miptree_map *map = calloc(1, sizeof(*map));
2839
2840 if (!map)
2841 return NULL;
2842
2843 assert(mt->level[level].slice[slice].map == NULL);
2844 mt->level[level].slice[slice].map = map;
2845
2846 map->mode = mode;
2847 map->x = x;
2848 map->y = y;
2849 map->w = w;
2850 map->h = h;
2851
2852 return map;
2853 }
2854
2855 /**
2856 * Release the map at (level, slice).
2857 */
2858 static void
2859 intel_miptree_release_map(struct intel_mipmap_tree *mt,
2860 unsigned int level,
2861 unsigned int slice)
2862 {
2863 struct intel_miptree_map **map;
2864
2865 map = &mt->level[level].slice[slice].map;
2866 free(*map);
2867 *map = NULL;
2868 }
2869
2870 static bool
2871 can_blit_slice(struct intel_mipmap_tree *mt,
2872 unsigned int level, unsigned int slice)
2873 {
2874 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2875 if (mt->pitch >= 32768)
2876 return false;
2877
2878 return true;
2879 }
2880
2881 static bool
2882 use_intel_mipree_map_blit(struct brw_context *brw,
2883 struct intel_mipmap_tree *mt,
2884 GLbitfield mode,
2885 unsigned int level,
2886 unsigned int slice)
2887 {
2888 if (brw->has_llc &&
2889 /* It's probably not worth swapping to the blit ring because of
2890 * all the overhead involved. But, we must use blitter for the
2891 * surfaces with INTEL_MIPTREE_TRMODE_{YF,YS}.
2892 */
2893 (!(mode & GL_MAP_WRITE_BIT) ||
2894 mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE) &&
2895 !mt->compressed &&
2896 (mt->tiling == I915_TILING_X ||
2897 /* Prior to Sandybridge, the blitter can't handle Y tiling */
2898 (brw->gen >= 6 && mt->tiling == I915_TILING_Y) ||
2899 /* Fast copy blit on skl+ supports all tiling formats. */
2900 brw->gen >= 9) &&
2901 can_blit_slice(mt, level, slice))
2902 return true;
2903
2904 if (mt->tiling != I915_TILING_NONE &&
2905 mt->bo->size >= brw->max_gtt_map_object_size) {
2906 assert(can_blit_slice(mt, level, slice));
2907 return true;
2908 }
2909
2910 return false;
2911 }
2912
2913 /**
2914 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
2915 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
2916 * arithmetic overflow.
2917 *
2918 * If you call this function and use \a out_stride, then you're doing pointer
2919 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
2920 * bugs. The caller must still take care to avoid 32-bit overflow errors in
2921 * all arithmetic expressions that contain buffer offsets and pixel sizes,
2922 * which usually have type uint32_t or GLuint.
2923 */
2924 void
2925 intel_miptree_map(struct brw_context *brw,
2926 struct intel_mipmap_tree *mt,
2927 unsigned int level,
2928 unsigned int slice,
2929 unsigned int x,
2930 unsigned int y,
2931 unsigned int w,
2932 unsigned int h,
2933 GLbitfield mode,
2934 void **out_ptr,
2935 ptrdiff_t *out_stride)
2936 {
2937 struct intel_miptree_map *map;
2938
2939 assert(mt->num_samples <= 1);
2940
2941 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
2942 if (!map){
2943 *out_ptr = NULL;
2944 *out_stride = 0;
2945 return;
2946 }
2947
2948 intel_miptree_slice_resolve_depth(brw, mt, level, slice);
2949 if (map->mode & GL_MAP_WRITE_BIT) {
2950 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
2951 }
2952
2953 if (mt->format == MESA_FORMAT_S_UINT8) {
2954 intel_miptree_map_s8(brw, mt, map, level, slice);
2955 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2956 !(mode & BRW_MAP_DIRECT_BIT)) {
2957 intel_miptree_map_etc(brw, mt, map, level, slice);
2958 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
2959 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
2960 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
2961 intel_miptree_map_blit(brw, mt, map, level, slice);
2962 #if defined(USE_SSE41)
2963 } else if (!(mode & GL_MAP_WRITE_BIT) &&
2964 !mt->compressed && cpu_has_sse4_1 &&
2965 (mt->pitch % 16 == 0)) {
2966 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
2967 #endif
2968 } else {
2969 /* intel_miptree_map_gtt() doesn't support surfaces with Yf/Ys tiling. */
2970 assert(mt->tr_mode == INTEL_MIPTREE_TRMODE_NONE);
2971 intel_miptree_map_gtt(brw, mt, map, level, slice);
2972 }
2973
2974 *out_ptr = map->ptr;
2975 *out_stride = map->stride;
2976
2977 if (map->ptr == NULL)
2978 intel_miptree_release_map(mt, level, slice);
2979 }
2980
2981 void
2982 intel_miptree_unmap(struct brw_context *brw,
2983 struct intel_mipmap_tree *mt,
2984 unsigned int level,
2985 unsigned int slice)
2986 {
2987 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
2988
2989 assert(mt->num_samples <= 1);
2990
2991 if (!map)
2992 return;
2993
2994 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
2995 mt, _mesa_get_format_name(mt->format), level, slice);
2996
2997 if (mt->format == MESA_FORMAT_S_UINT8) {
2998 intel_miptree_unmap_s8(brw, mt, map, level, slice);
2999 } else if (mt->etc_format != MESA_FORMAT_NONE &&
3000 !(map->mode & BRW_MAP_DIRECT_BIT)) {
3001 intel_miptree_unmap_etc(brw, mt, map, level, slice);
3002 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
3003 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
3004 } else if (map->linear_mt) {
3005 intel_miptree_unmap_blit(brw, mt, map, level, slice);
3006 #if defined(USE_SSE41)
3007 } else if (map->buffer && cpu_has_sse4_1) {
3008 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
3009 #endif
3010 } else {
3011 intel_miptree_unmap_gtt(mt);
3012 }
3013
3014 intel_miptree_release_map(mt, level, slice);
3015 }
3016
3017 enum isl_surf_dim
3018 get_isl_surf_dim(GLenum target)
3019 {
3020 switch (target) {
3021 case GL_TEXTURE_1D:
3022 case GL_TEXTURE_1D_ARRAY:
3023 return ISL_SURF_DIM_1D;
3024
3025 case GL_TEXTURE_2D:
3026 case GL_TEXTURE_2D_ARRAY:
3027 case GL_TEXTURE_RECTANGLE:
3028 case GL_TEXTURE_CUBE_MAP:
3029 case GL_TEXTURE_CUBE_MAP_ARRAY:
3030 case GL_TEXTURE_2D_MULTISAMPLE:
3031 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3032 case GL_TEXTURE_EXTERNAL_OES:
3033 return ISL_SURF_DIM_2D;
3034
3035 case GL_TEXTURE_3D:
3036 return ISL_SURF_DIM_3D;
3037 }
3038
3039 unreachable("Invalid texture target");
3040 }
3041
3042 enum isl_dim_layout
3043 get_isl_dim_layout(const struct gen_device_info *devinfo, uint32_t tiling,
3044 GLenum target)
3045 {
3046 switch (target) {
3047 case GL_TEXTURE_1D:
3048 case GL_TEXTURE_1D_ARRAY:
3049 return (devinfo->gen >= 9 && tiling == I915_TILING_NONE ?
3050 ISL_DIM_LAYOUT_GEN9_1D : ISL_DIM_LAYOUT_GEN4_2D);
3051
3052 case GL_TEXTURE_2D:
3053 case GL_TEXTURE_2D_ARRAY:
3054 case GL_TEXTURE_RECTANGLE:
3055 case GL_TEXTURE_2D_MULTISAMPLE:
3056 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3057 case GL_TEXTURE_EXTERNAL_OES:
3058 return ISL_DIM_LAYOUT_GEN4_2D;
3059
3060 case GL_TEXTURE_CUBE_MAP:
3061 case GL_TEXTURE_CUBE_MAP_ARRAY:
3062 return (devinfo->gen == 4 ? ISL_DIM_LAYOUT_GEN4_3D :
3063 ISL_DIM_LAYOUT_GEN4_2D);
3064
3065 case GL_TEXTURE_3D:
3066 return (devinfo->gen >= 9 ?
3067 ISL_DIM_LAYOUT_GEN4_2D : ISL_DIM_LAYOUT_GEN4_3D);
3068 }
3069
3070 unreachable("Invalid texture target");
3071 }
3072
3073 enum isl_tiling
3074 intel_miptree_get_isl_tiling(const struct intel_mipmap_tree *mt)
3075 {
3076 if (mt->format == MESA_FORMAT_S_UINT8) {
3077 return ISL_TILING_W;
3078 } else {
3079 switch (mt->tiling) {
3080 case I915_TILING_NONE:
3081 return ISL_TILING_LINEAR;
3082 case I915_TILING_X:
3083 return ISL_TILING_X;
3084 case I915_TILING_Y:
3085 switch (mt->tr_mode) {
3086 case INTEL_MIPTREE_TRMODE_NONE:
3087 return ISL_TILING_Y0;
3088 case INTEL_MIPTREE_TRMODE_YF:
3089 return ISL_TILING_Yf;
3090 case INTEL_MIPTREE_TRMODE_YS:
3091 return ISL_TILING_Ys;
3092 default:
3093 unreachable("Invalid tiled resource mode");
3094 }
3095 default:
3096 unreachable("Invalid tiling mode");
3097 }
3098 }
3099 }
3100
3101 void
3102 intel_miptree_get_isl_surf(struct brw_context *brw,
3103 const struct intel_mipmap_tree *mt,
3104 struct isl_surf *surf)
3105 {
3106 surf->dim = get_isl_surf_dim(mt->target);
3107 surf->dim_layout = get_isl_dim_layout(&brw->screen->devinfo,
3108 mt->tiling, mt->target);
3109
3110 if (mt->num_samples > 1) {
3111 switch (mt->msaa_layout) {
3112 case INTEL_MSAA_LAYOUT_IMS:
3113 surf->msaa_layout = ISL_MSAA_LAYOUT_INTERLEAVED;
3114 break;
3115 case INTEL_MSAA_LAYOUT_UMS:
3116 case INTEL_MSAA_LAYOUT_CMS:
3117 surf->msaa_layout = ISL_MSAA_LAYOUT_ARRAY;
3118 break;
3119 default:
3120 unreachable("Invalid MSAA layout");
3121 }
3122 } else {
3123 surf->msaa_layout = ISL_MSAA_LAYOUT_NONE;
3124 }
3125
3126 surf->tiling = intel_miptree_get_isl_tiling(mt);
3127
3128 if (mt->format == MESA_FORMAT_S_UINT8) {
3129 /* The ISL definition of row_pitch matches the surface state pitch field
3130 * a bit better than intel_mipmap_tree. In particular, ISL incorporates
3131 * the factor of 2 for W-tiling in row_pitch.
3132 */
3133 surf->row_pitch = 2 * mt->pitch;
3134 } else {
3135 surf->row_pitch = mt->pitch;
3136 }
3137
3138 surf->format = translate_tex_format(brw, mt->format, false);
3139
3140 if (brw->gen >= 9) {
3141 if (surf->dim == ISL_SURF_DIM_1D && surf->tiling == ISL_TILING_LINEAR) {
3142 /* For gen9 1-D surfaces, intel_mipmap_tree has a bogus alignment. */
3143 surf->image_alignment_el = isl_extent3d(64, 1, 1);
3144 } else {
3145 /* On gen9+, intel_mipmap_tree stores the horizontal and vertical
3146 * alignment in terms of surface elements like we want.
3147 */
3148 surf->image_alignment_el = isl_extent3d(mt->halign, mt->valign, 1);
3149 }
3150 } else {
3151 /* On earlier gens it's stored in pixels. */
3152 unsigned bw, bh;
3153 _mesa_get_format_block_size(mt->format, &bw, &bh);
3154 surf->image_alignment_el =
3155 isl_extent3d(mt->halign / bw, mt->valign / bh, 1);
3156 }
3157
3158 surf->logical_level0_px.width = mt->logical_width0;
3159 surf->logical_level0_px.height = mt->logical_height0;
3160 if (surf->dim == ISL_SURF_DIM_3D) {
3161 surf->logical_level0_px.depth = mt->logical_depth0;
3162 surf->logical_level0_px.array_len = 1;
3163 } else {
3164 surf->logical_level0_px.depth = 1;
3165 surf->logical_level0_px.array_len = mt->logical_depth0;
3166 }
3167
3168 surf->phys_level0_sa.width = mt->physical_width0;
3169 surf->phys_level0_sa.height = mt->physical_height0;
3170 if (surf->dim == ISL_SURF_DIM_3D) {
3171 surf->phys_level0_sa.depth = mt->physical_depth0;
3172 surf->phys_level0_sa.array_len = 1;
3173 } else {
3174 surf->phys_level0_sa.depth = 1;
3175 surf->phys_level0_sa.array_len = mt->physical_depth0;
3176 }
3177
3178 surf->levels = mt->last_level + 1;
3179 surf->samples = MAX2(mt->num_samples, 1);
3180
3181 surf->size = 0; /* TODO */
3182 surf->alignment = 0; /* TODO */
3183
3184 switch (surf->dim_layout) {
3185 case ISL_DIM_LAYOUT_GEN4_2D:
3186 case ISL_DIM_LAYOUT_GEN4_3D:
3187 if (brw->gen >= 9) {
3188 surf->array_pitch_el_rows = mt->qpitch;
3189 } else {
3190 unsigned bw, bh;
3191 _mesa_get_format_block_size(mt->format, &bw, &bh);
3192 assert(mt->qpitch % bh == 0);
3193 surf->array_pitch_el_rows = mt->qpitch / bh;
3194 }
3195 break;
3196 case ISL_DIM_LAYOUT_GEN9_1D:
3197 surf->array_pitch_el_rows = 1;
3198 break;
3199 }
3200
3201 switch (mt->array_layout) {
3202 case ALL_LOD_IN_EACH_SLICE:
3203 surf->array_pitch_span = ISL_ARRAY_PITCH_SPAN_FULL;
3204 break;
3205 case ALL_SLICES_AT_EACH_LOD:
3206 surf->array_pitch_span = ISL_ARRAY_PITCH_SPAN_COMPACT;
3207 break;
3208 default:
3209 unreachable("Invalid array layout");
3210 }
3211
3212 GLenum base_format = _mesa_get_format_base_format(mt->format);
3213 switch (base_format) {
3214 case GL_DEPTH_COMPONENT:
3215 surf->usage = ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
3216 break;
3217 case GL_STENCIL_INDEX:
3218 surf->usage = ISL_SURF_USAGE_STENCIL_BIT;
3219 if (brw->gen >= 8)
3220 surf->usage |= ISL_SURF_USAGE_TEXTURE_BIT;
3221 break;
3222 case GL_DEPTH_STENCIL:
3223 /* In this case we only texture from the depth part */
3224 surf->usage = ISL_SURF_USAGE_DEPTH_BIT | ISL_SURF_USAGE_STENCIL_BIT |
3225 ISL_SURF_USAGE_TEXTURE_BIT;
3226 break;
3227 default:
3228 surf->usage = ISL_SURF_USAGE_TEXTURE_BIT;
3229 if (brw->format_supported_as_render_target[mt->format])
3230 surf->usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
3231 break;
3232 }
3233
3234 if (_mesa_is_cube_map_texture(mt->target))
3235 surf->usage |= ISL_SURF_USAGE_CUBE_BIT;
3236 }
3237
3238 /* WARNING: THE SURFACE CREATED BY THIS FUNCTION IS NOT COMPLETE AND CANNOT BE
3239 * USED FOR ANY REAL CALCULATIONS. THE ONLY VALID USE OF SUCH A SURFACE IS TO
3240 * PASS IT INTO isl_surf_fill_state.
3241 */
3242 void
3243 intel_miptree_get_aux_isl_surf(struct brw_context *brw,
3244 const struct intel_mipmap_tree *mt,
3245 struct isl_surf *surf,
3246 enum isl_aux_usage *usage)
3247 {
3248 uint32_t aux_pitch, aux_qpitch;
3249 if (mt->mcs_buf) {
3250 aux_pitch = mt->mcs_buf->mt->pitch;
3251 aux_qpitch = mt->mcs_buf->mt->qpitch;
3252
3253 if (mt->num_samples > 1) {
3254 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS);
3255 *usage = ISL_AUX_USAGE_MCS;
3256 } else if (intel_miptree_is_lossless_compressed(brw, mt)) {
3257 assert(brw->gen >= 9);
3258 *usage = ISL_AUX_USAGE_CCS_E;
3259 } else if (mt->fast_clear_state != INTEL_FAST_CLEAR_STATE_NO_MCS) {
3260 *usage = ISL_AUX_USAGE_CCS_D;
3261 } else {
3262 unreachable("Invalid MCS miptree");
3263 }
3264 } else if (mt->hiz_buf) {
3265 if (mt->hiz_buf->mt) {
3266 aux_pitch = mt->hiz_buf->mt->pitch;
3267 aux_qpitch = mt->hiz_buf->mt->qpitch;
3268 } else {
3269 aux_pitch = mt->hiz_buf->pitch;
3270 aux_qpitch = mt->hiz_buf->qpitch;
3271 }
3272
3273 *usage = ISL_AUX_USAGE_HIZ;
3274 } else {
3275 *usage = ISL_AUX_USAGE_NONE;
3276 return;
3277 }
3278
3279 /* Start with a copy of the original surface. */
3280 intel_miptree_get_isl_surf(brw, mt, surf);
3281
3282 /* Figure out the format and tiling of the auxiliary surface */
3283 switch (*usage) {
3284 case ISL_AUX_USAGE_NONE:
3285 unreachable("Invalid auxiliary usage");
3286
3287 case ISL_AUX_USAGE_HIZ:
3288 isl_surf_get_hiz_surf(&brw->isl_dev, surf, surf);
3289 break;
3290
3291 case ISL_AUX_USAGE_MCS:
3292 /*
3293 * From the SKL PRM:
3294 * "When Auxiliary Surface Mode is set to AUX_CCS_D or AUX_CCS_E,
3295 * HALIGN 16 must be used."
3296 */
3297 if (brw->gen >= 9)
3298 assert(mt->halign == 16);
3299
3300 isl_surf_get_mcs_surf(&brw->isl_dev, surf, surf);
3301 break;
3302
3303 case ISL_AUX_USAGE_CCS_D:
3304 case ISL_AUX_USAGE_CCS_E:
3305 /*
3306 * From the BDW PRM, Volume 2d, page 260 (RENDER_SURFACE_STATE):
3307 *
3308 * "When MCS is enabled for non-MSRT, HALIGN_16 must be used"
3309 *
3310 * From the hardware spec for GEN9:
3311 *
3312 * "When Auxiliary Surface Mode is set to AUX_CCS_D or AUX_CCS_E,
3313 * HALIGN 16 must be used."
3314 */
3315 assert(mt->num_samples <= 1);
3316 if (brw->gen >= 8)
3317 assert(mt->halign == 16);
3318
3319 isl_surf_get_ccs_surf(&brw->isl_dev, surf, surf);
3320 break;
3321 }
3322
3323 /* We want the pitch of the actual aux buffer. */
3324 surf->row_pitch = aux_pitch;
3325
3326 /* Auxiliary surfaces in ISL have compressed formats and array_pitch_el_rows
3327 * is in elements. This doesn't match intel_mipmap_tree::qpitch which is
3328 * in elements of the primary color surface so we have to divide by the
3329 * compression block height.
3330 */
3331 surf->array_pitch_el_rows =
3332 aux_qpitch / isl_format_get_layout(surf->format)->bh;
3333 }
3334
3335 union isl_color_value
3336 intel_miptree_get_isl_clear_color(struct brw_context *brw,
3337 const struct intel_mipmap_tree *mt)
3338 {
3339 union isl_color_value clear_color;
3340
3341 if (_mesa_get_format_base_format(mt->format) == GL_DEPTH_COMPONENT) {
3342 clear_color.i32[0] = mt->depth_clear_value;
3343 clear_color.i32[1] = 0;
3344 clear_color.i32[2] = 0;
3345 clear_color.i32[3] = 0;
3346 } else if (brw->gen >= 9) {
3347 clear_color.i32[0] = mt->gen9_fast_clear_color.i[0];
3348 clear_color.i32[1] = mt->gen9_fast_clear_color.i[1];
3349 clear_color.i32[2] = mt->gen9_fast_clear_color.i[2];
3350 clear_color.i32[3] = mt->gen9_fast_clear_color.i[3];
3351 } else if (_mesa_is_format_integer(mt->format)) {
3352 clear_color.i32[0] = (mt->fast_clear_color_value & (1u << 31)) != 0;
3353 clear_color.i32[1] = (mt->fast_clear_color_value & (1u << 30)) != 0;
3354 clear_color.i32[2] = (mt->fast_clear_color_value & (1u << 29)) != 0;
3355 clear_color.i32[3] = (mt->fast_clear_color_value & (1u << 28)) != 0;
3356 } else {
3357 clear_color.f32[0] = (mt->fast_clear_color_value & (1u << 31)) != 0;
3358 clear_color.f32[1] = (mt->fast_clear_color_value & (1u << 30)) != 0;
3359 clear_color.f32[2] = (mt->fast_clear_color_value & (1u << 29)) != 0;
3360 clear_color.f32[3] = (mt->fast_clear_color_value & (1u << 28)) != 0;
3361 }
3362
3363 return clear_color;
3364 }