i965: Handle 16x MSAA in IMS dimension munging code.
[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
39 #include "main/enums.h"
40 #include "main/fbobject.h"
41 #include "main/formats.h"
42 #include "main/glformats.h"
43 #include "main/texcompress_etc.h"
44 #include "main/teximage.h"
45 #include "main/streaming-load-memcpy.h"
46 #include "x86/common_x86_asm.h"
47
48 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
49
50 static void *intel_miptree_map_raw(struct brw_context *brw,
51 struct intel_mipmap_tree *mt);
52
53 static void intel_miptree_unmap_raw(struct intel_mipmap_tree *mt);
54
55 static bool
56 intel_miptree_alloc_mcs(struct brw_context *brw,
57 struct intel_mipmap_tree *mt,
58 GLuint num_samples);
59
60 /**
61 * Determine which MSAA layout should be used by the MSAA surface being
62 * created, based on the chip generation and the surface type.
63 */
64 static enum intel_msaa_layout
65 compute_msaa_layout(struct brw_context *brw, mesa_format format,
66 bool disable_aux_buffers)
67 {
68 /* Prior to Gen7, all MSAA surfaces used IMS layout. */
69 if (brw->gen < 7)
70 return INTEL_MSAA_LAYOUT_IMS;
71
72 /* In Gen7, IMS layout is only used for depth and stencil buffers. */
73 switch (_mesa_get_format_base_format(format)) {
74 case GL_DEPTH_COMPONENT:
75 case GL_STENCIL_INDEX:
76 case GL_DEPTH_STENCIL:
77 return INTEL_MSAA_LAYOUT_IMS;
78 default:
79 /* From the Ivy Bridge PRM, Vol4 Part1 p77 ("MCS Enable"):
80 *
81 * This field must be set to 0 for all SINT MSRTs when all RT channels
82 * are not written
83 *
84 * In practice this means that we have to disable MCS for all signed
85 * integer MSAA buffers. The alternative, to disable MCS only when one
86 * of the render target channels is disabled, is impractical because it
87 * would require converting between CMS and UMS MSAA layouts on the fly,
88 * which is expensive.
89 */
90 if (brw->gen == 7 && _mesa_get_format_datatype(format) == GL_INT) {
91 return INTEL_MSAA_LAYOUT_UMS;
92 } else if (disable_aux_buffers) {
93 /* We can't use the CMS layout because it uses an aux buffer, the MCS
94 * buffer. So fallback to UMS, which is identical to CMS without the
95 * MCS. */
96 return INTEL_MSAA_LAYOUT_UMS;
97 } else {
98 return INTEL_MSAA_LAYOUT_CMS;
99 }
100 }
101 }
102
103
104 /**
105 * For single-sampled render targets ("non-MSRT"), the MCS buffer is a
106 * scaled-down bitfield representation of the color buffer which is capable of
107 * recording when blocks of the color buffer are equal to the clear value.
108 * This function returns the block size that will be used by the MCS buffer
109 * corresponding to a certain color miptree.
110 *
111 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
112 * beneath the "Fast Color Clear" bullet (p327):
113 *
114 * The following table describes the RT alignment
115 *
116 * Pixels Lines
117 * TiledY RT CL
118 * bpp
119 * 32 8 4
120 * 64 4 4
121 * 128 2 4
122 * TiledX RT CL
123 * bpp
124 * 32 16 2
125 * 64 8 2
126 * 128 4 2
127 *
128 * This alignment has the following uses:
129 *
130 * - For figuring out the size of the MCS buffer. Each 4k tile in the MCS
131 * buffer contains 128 blocks horizontally and 256 blocks vertically.
132 *
133 * - For figuring out alignment restrictions for a fast clear operation. Fast
134 * clear operations must always clear aligned multiples of 16 blocks
135 * horizontally and 32 blocks vertically.
136 *
137 * - For scaling down the coordinates sent through the render pipeline during
138 * a fast clear. X coordinates must be scaled down by 8 times the block
139 * width, and Y coordinates by 16 times the block height.
140 *
141 * - For scaling down the coordinates sent through the render pipeline during
142 * a "Render Target Resolve" operation. X coordinates must be scaled down
143 * by half the block width, and Y coordinates by half the block height.
144 */
145 void
146 intel_get_non_msrt_mcs_alignment(struct intel_mipmap_tree *mt,
147 unsigned *width_px, unsigned *height)
148 {
149 switch (mt->tiling) {
150 default:
151 unreachable("Non-MSRT MCS requires X or Y tiling");
152 /* In release builds, fall through */
153 case I915_TILING_Y:
154 *width_px = 32 / mt->cpp;
155 *height = 4;
156 break;
157 case I915_TILING_X:
158 *width_px = 64 / mt->cpp;
159 *height = 2;
160 }
161 }
162
163 static bool
164 intel_tiling_supports_non_msrt_mcs(struct brw_context *brw, unsigned tiling)
165 {
166 /* From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render
167 * Target(s)", beneath the "Fast Color Clear" bullet (p326):
168 *
169 * - Support is limited to tiled render targets.
170 *
171 * Gen9 changes the restriction to Y-tile only.
172 */
173 if (brw->gen >= 9)
174 return tiling == I915_TILING_Y;
175 else if (brw->gen >= 7)
176 return tiling != I915_TILING_NONE;
177 else
178 return false;
179 }
180
181 /**
182 * For a single-sampled render target ("non-MSRT"), determine if an MCS buffer
183 * can be used. This doesn't (and should not) inspect any of the properties of
184 * the miptree's BO.
185 *
186 * From the Ivy Bridge PRM, Vol2 Part1 11.7 "MCS Buffer for Render Target(s)",
187 * beneath the "Fast Color Clear" bullet (p326):
188 *
189 * - Support is for non-mip-mapped and non-array surface types only.
190 *
191 * And then later, on p327:
192 *
193 * - MCS buffer for non-MSRT is supported only for RT formats 32bpp,
194 * 64bpp, and 128bpp.
195 */
196 static bool
197 intel_miptree_supports_non_msrt_fast_clear(struct brw_context *brw,
198 struct intel_mipmap_tree *mt)
199 {
200 /* MCS support does not exist prior to Gen7 */
201 if (brw->gen < 7)
202 return false;
203
204 if (brw->gen >= 9) {
205 /* FINISHME: Enable singlesample fast MCS clears on SKL after all GPU
206 * FINISHME: hangs are resolved.
207 */
208 perf_debug("singlesample fast MCS clears disabled on gen9");
209 return false;
210 }
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 return true;
263 }
264
265
266 /**
267 * Determine depth format corresponding to a depth+stencil format,
268 * for separate stencil.
269 */
270 mesa_format
271 intel_depth_format_for_depthstencil_format(mesa_format format) {
272 switch (format) {
273 case MESA_FORMAT_Z24_UNORM_S8_UINT:
274 return MESA_FORMAT_Z24_UNORM_X8_UINT;
275 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
276 return MESA_FORMAT_Z_FLOAT32;
277 default:
278 return format;
279 }
280 }
281
282
283 /**
284 * @param for_bo Indicates that the caller is
285 * intel_miptree_create_for_bo(). If true, then do not create
286 * \c stencil_mt.
287 */
288 static struct intel_mipmap_tree *
289 intel_miptree_create_layout(struct brw_context *brw,
290 GLenum target,
291 mesa_format format,
292 GLuint first_level,
293 GLuint last_level,
294 GLuint width0,
295 GLuint height0,
296 GLuint depth0,
297 GLuint num_samples,
298 uint32_t layout_flags)
299 {
300 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
301 if (!mt)
302 return NULL;
303
304 DBG("%s target %s format %s level %d..%d slices %d <-- %p\n", __func__,
305 _mesa_enum_to_string(target),
306 _mesa_get_format_name(format),
307 first_level, last_level, depth0, mt);
308
309 if (target == GL_TEXTURE_1D_ARRAY) {
310 /* For a 1D Array texture the OpenGL API will treat the height0
311 * parameter as the number of array slices. For Intel hardware, we treat
312 * the 1D array as a 2D Array with a height of 1.
313 *
314 * So, when we first come through this path to create a 1D Array
315 * texture, height0 stores the number of slices, and depth0 is 1. In
316 * this case, we want to swap height0 and depth0.
317 *
318 * Since some miptrees will be created based on the base miptree, we may
319 * come through this path and see height0 as 1 and depth0 being the
320 * number of slices. In this case we don't need to do the swap.
321 */
322 assert(height0 == 1 || depth0 == 1);
323 if (height0 > 1) {
324 depth0 = height0;
325 height0 = 1;
326 }
327 }
328
329 mt->target = target;
330 mt->format = format;
331 mt->first_level = first_level;
332 mt->last_level = last_level;
333 mt->logical_width0 = width0;
334 mt->logical_height0 = height0;
335 mt->logical_depth0 = depth0;
336 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
337 mt->disable_aux_buffers = (layout_flags & MIPTREE_LAYOUT_DISABLE_AUX) != 0;
338 exec_list_make_empty(&mt->hiz_map);
339 mt->cpp = _mesa_get_format_bytes(format);
340 mt->num_samples = num_samples;
341 mt->compressed = _mesa_is_format_compressed(format);
342 mt->msaa_layout = INTEL_MSAA_LAYOUT_NONE;
343 mt->refcount = 1;
344
345 if (num_samples > 1) {
346 /* Adjust width/height/depth for MSAA */
347 mt->msaa_layout = compute_msaa_layout(brw, format,
348 mt->disable_aux_buffers);
349 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_IMS) {
350 /* From the Ivybridge PRM, Volume 1, Part 1, page 108:
351 * "If the surface is multisampled and it is a depth or stencil
352 * surface or Multisampled Surface StorageFormat in SURFACE_STATE is
353 * MSFMT_DEPTH_STENCIL, WL and HL must be adjusted as follows before
354 * proceeding:
355 *
356 * +----------------------------------------------------------------+
357 * | Num Multisamples | W_l = | H_l = |
358 * +----------------------------------------------------------------+
359 * | 2 | ceiling(W_l / 2) * 4 | H_l (no adjustment) |
360 * | 4 | ceiling(W_l / 2) * 4 | ceiling(H_l / 2) * 4 |
361 * | 8 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 4 |
362 * | 16 | ceiling(W_l / 2) * 8 | ceiling(H_l / 2) * 8 |
363 * +----------------------------------------------------------------+
364 * "
365 *
366 * Note that MSFMT_DEPTH_STENCIL just means the IMS (interleaved)
367 * format rather than UMS/CMS (array slices). The Sandybridge PRM,
368 * Volume 1, Part 1, Page 111 has the same formula for 4x MSAA.
369 *
370 * Another more complicated explanation for these adjustments comes
371 * from the Sandybridge PRM, volume 4, part 1, page 31:
372 *
373 * "Any of the other messages (sample*, LOD, load4) used with a
374 * (4x) multisampled surface will in-effect sample a surface with
375 * double the height and width as that indicated in the surface
376 * state. Each pixel position on the original-sized surface is
377 * replaced with a 2x2 of samples with the following arrangement:
378 *
379 * sample 0 sample 2
380 * sample 1 sample 3"
381 *
382 * Thus, when sampling from a multisampled texture, it behaves as
383 * though the layout in memory for (x,y,sample) is:
384 *
385 * (0,0,0) (0,0,2) (1,0,0) (1,0,2)
386 * (0,0,1) (0,0,3) (1,0,1) (1,0,3)
387 *
388 * (0,1,0) (0,1,2) (1,1,0) (1,1,2)
389 * (0,1,1) (0,1,3) (1,1,1) (1,1,3)
390 *
391 * However, the actual layout of multisampled data in memory is:
392 *
393 * (0,0,0) (1,0,0) (0,0,1) (1,0,1)
394 * (0,1,0) (1,1,0) (0,1,1) (1,1,1)
395 *
396 * (0,0,2) (1,0,2) (0,0,3) (1,0,3)
397 * (0,1,2) (1,1,2) (0,1,3) (1,1,3)
398 *
399 * This pattern repeats for each 2x2 pixel block.
400 *
401 * As a result, when calculating the size of our 4-sample buffer for
402 * an odd width or height, we have to align before scaling up because
403 * sample 3 is in that bottom right 2x2 block.
404 */
405 switch (num_samples) {
406 case 2:
407 assert(brw->gen >= 8);
408 width0 = ALIGN(width0, 2) * 2;
409 height0 = ALIGN(height0, 2);
410 break;
411 case 4:
412 width0 = ALIGN(width0, 2) * 2;
413 height0 = ALIGN(height0, 2) * 2;
414 break;
415 case 8:
416 width0 = ALIGN(width0, 2) * 4;
417 height0 = ALIGN(height0, 2) * 2;
418 break;
419 case 16:
420 width0 = ALIGN(width0, 2) * 4;
421 height0 = ALIGN(height0, 2) * 4;
422 break;
423 default:
424 /* num_samples should already have been quantized to 0, 1, 2, 4, 8
425 * or 16.
426 */
427 unreachable("not reached");
428 }
429 } else {
430 /* Non-interleaved */
431 depth0 *= num_samples;
432 }
433 }
434
435 /* Set array_layout to ALL_SLICES_AT_EACH_LOD when array_spacing_lod0 can
436 * be used. array_spacing_lod0 is only used for non-IMS MSAA surfaces on
437 * Gen 7 and 8. On Gen 8 and 9 this layout is not available but it is still
438 * used on Gen8 to make it pick a qpitch value which doesn't include space
439 * for the mipmaps. On Gen9 this is not necessary because it will
440 * automatically pick a packed qpitch value whenever mt->first_level ==
441 * mt->last_level.
442 * TODO: can we use it elsewhere?
443 * TODO: also disable this on Gen8 and pick the qpitch value like Gen9
444 */
445 if (brw->gen >= 9) {
446 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
447 } else {
448 switch (mt->msaa_layout) {
449 case INTEL_MSAA_LAYOUT_NONE:
450 case INTEL_MSAA_LAYOUT_IMS:
451 mt->array_layout = ALL_LOD_IN_EACH_SLICE;
452 break;
453 case INTEL_MSAA_LAYOUT_UMS:
454 case INTEL_MSAA_LAYOUT_CMS:
455 mt->array_layout = ALL_SLICES_AT_EACH_LOD;
456 break;
457 }
458 }
459
460 if (target == GL_TEXTURE_CUBE_MAP) {
461 assert(depth0 == 1);
462 depth0 = 6;
463 }
464
465 mt->physical_width0 = width0;
466 mt->physical_height0 = height0;
467 mt->physical_depth0 = depth0;
468
469 if (!(layout_flags & MIPTREE_LAYOUT_FOR_BO) &&
470 _mesa_get_format_base_format(format) == GL_DEPTH_STENCIL &&
471 (brw->must_use_separate_stencil ||
472 (brw->has_separate_stencil &&
473 intel_miptree_wants_hiz_buffer(brw, mt)))) {
474 uint32_t stencil_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
475 if (brw->gen == 6) {
476 stencil_flags |= MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD |
477 MIPTREE_LAYOUT_TILING_ANY;
478 }
479
480 mt->stencil_mt = intel_miptree_create(brw,
481 mt->target,
482 MESA_FORMAT_S_UINT8,
483 mt->first_level,
484 mt->last_level,
485 mt->logical_width0,
486 mt->logical_height0,
487 mt->logical_depth0,
488 num_samples,
489 stencil_flags);
490
491 if (!mt->stencil_mt) {
492 intel_miptree_release(&mt);
493 return NULL;
494 }
495
496 /* Fix up the Z miptree format for how we're splitting out separate
497 * stencil. Gen7 expects there to be no stencil bits in its depth buffer.
498 */
499 mt->format = intel_depth_format_for_depthstencil_format(mt->format);
500 mt->cpp = 4;
501
502 if (format == mt->format) {
503 _mesa_problem(NULL, "Unknown format %s in separate stencil mt\n",
504 _mesa_get_format_name(mt->format));
505 }
506 }
507
508 if (layout_flags & MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD)
509 mt->array_layout = ALL_SLICES_AT_EACH_LOD;
510
511 /*
512 * Obey HALIGN_16 constraints for Gen8 and Gen9 buffers which are
513 * multisampled or have an AUX buffer attached to it.
514 *
515 * GEN | MSRT | AUX_CCS_* or AUX_MCS
516 * -------------------------------------------
517 * 9 | HALIGN_16 | HALIGN_16
518 * 8 | HALIGN_ANY | HALIGN_16
519 * 7 | ? | ?
520 * 6 | ? | ?
521 */
522 if (intel_miptree_supports_non_msrt_fast_clear(brw, mt)) {
523 if (brw->gen >= 9 || (brw->gen == 8 && num_samples <= 1))
524 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
525 } else if (brw->gen >= 9 && num_samples > 1) {
526 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
527 } else {
528 /* For now, nothing else has this requirement */
529 assert((layout_flags & MIPTREE_LAYOUT_FORCE_HALIGN16) == 0);
530 }
531
532 brw_miptree_layout(brw, mt, layout_flags);
533
534 if (mt->disable_aux_buffers)
535 assert(mt->msaa_layout != INTEL_MSAA_LAYOUT_CMS);
536
537 return mt;
538 }
539
540
541 /**
542 * Choose an appropriate uncompressed format for a requested
543 * compressed format, if unsupported.
544 */
545 mesa_format
546 intel_lower_compressed_format(struct brw_context *brw, mesa_format format)
547 {
548 /* No need to lower ETC formats on these platforms,
549 * they are supported natively.
550 */
551 if (brw->gen >= 8 || brw->is_baytrail)
552 return format;
553
554 switch (format) {
555 case MESA_FORMAT_ETC1_RGB8:
556 return MESA_FORMAT_R8G8B8X8_UNORM;
557 case MESA_FORMAT_ETC2_RGB8:
558 return MESA_FORMAT_R8G8B8X8_UNORM;
559 case MESA_FORMAT_ETC2_SRGB8:
560 case MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC:
561 case MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1:
562 return MESA_FORMAT_B8G8R8A8_SRGB;
563 case MESA_FORMAT_ETC2_RGBA8_EAC:
564 case MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1:
565 return MESA_FORMAT_R8G8B8A8_UNORM;
566 case MESA_FORMAT_ETC2_R11_EAC:
567 return MESA_FORMAT_R_UNORM16;
568 case MESA_FORMAT_ETC2_SIGNED_R11_EAC:
569 return MESA_FORMAT_R_SNORM16;
570 case MESA_FORMAT_ETC2_RG11_EAC:
571 return MESA_FORMAT_R16G16_UNORM;
572 case MESA_FORMAT_ETC2_SIGNED_RG11_EAC:
573 return MESA_FORMAT_R16G16_SNORM;
574 default:
575 /* Non ETC1 / ETC2 format */
576 return format;
577 }
578 }
579
580 /* This function computes Yf/Ys tiled bo size, alignment and pitch. */
581 static unsigned long
582 intel_get_yf_ys_bo_size(struct intel_mipmap_tree *mt, unsigned *alignment,
583 unsigned long *pitch)
584 {
585 uint32_t tile_width, tile_height;
586 unsigned long stride, size, aligned_y;
587
588 assert(mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE);
589 intel_get_tile_dims(mt->tiling, mt->tr_mode, mt->cpp,
590 &tile_width, &tile_height);
591
592 aligned_y = ALIGN(mt->total_height, tile_height);
593 stride = mt->total_width * mt->cpp;
594 stride = ALIGN(stride, tile_width);
595 size = stride * aligned_y;
596
597 if (mt->tr_mode == INTEL_MIPTREE_TRMODE_YF) {
598 assert(size % 4096 == 0);
599 *alignment = 4096;
600 } else {
601 assert(size % (64 * 1024) == 0);
602 *alignment = 64 * 1024;
603 }
604 *pitch = stride;
605 return size;
606 }
607
608 struct intel_mipmap_tree *
609 intel_miptree_create(struct brw_context *brw,
610 GLenum target,
611 mesa_format format,
612 GLuint first_level,
613 GLuint last_level,
614 GLuint width0,
615 GLuint height0,
616 GLuint depth0,
617 GLuint num_samples,
618 uint32_t layout_flags)
619 {
620 struct intel_mipmap_tree *mt;
621 mesa_format tex_format = format;
622 mesa_format etc_format = MESA_FORMAT_NONE;
623 GLuint total_width, total_height;
624 uint32_t alloc_flags = 0;
625
626 format = intel_lower_compressed_format(brw, format);
627
628 etc_format = (format != tex_format) ? tex_format : MESA_FORMAT_NONE;
629
630 assert((layout_flags & MIPTREE_LAYOUT_DISABLE_AUX) == 0);
631 assert((layout_flags & MIPTREE_LAYOUT_FOR_BO) == 0);
632 mt = intel_miptree_create_layout(brw, target, format,
633 first_level, last_level, width0,
634 height0, depth0, num_samples,
635 layout_flags);
636 /*
637 * pitch == 0 || height == 0 indicates the null texture
638 */
639 if (!mt || !mt->total_width || !mt->total_height) {
640 intel_miptree_release(&mt);
641 return NULL;
642 }
643
644 total_width = mt->total_width;
645 total_height = mt->total_height;
646
647 if (format == MESA_FORMAT_S_UINT8) {
648 /* Align to size of W tile, 64x64. */
649 total_width = ALIGN(total_width, 64);
650 total_height = ALIGN(total_height, 64);
651 }
652
653 bool y_or_x = false;
654
655 if (mt->tiling == (I915_TILING_Y | I915_TILING_X)) {
656 y_or_x = true;
657 mt->tiling = I915_TILING_Y;
658 }
659
660 if (layout_flags & MIPTREE_LAYOUT_ACCELERATED_UPLOAD)
661 alloc_flags |= BO_ALLOC_FOR_RENDER;
662
663 unsigned long pitch;
664 mt->etc_format = etc_format;
665
666 if (mt->tr_mode != INTEL_MIPTREE_TRMODE_NONE) {
667 unsigned alignment = 0;
668 unsigned long size;
669 size = intel_get_yf_ys_bo_size(mt, &alignment, &pitch);
670 assert(size);
671 mt->bo = drm_intel_bo_alloc_for_render(brw->bufmgr, "miptree",
672 size, alignment);
673 } else {
674 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
675 total_width, total_height, mt->cpp,
676 &mt->tiling, &pitch,
677 alloc_flags);
678 }
679
680 mt->pitch = pitch;
681
682 /* If the BO is too large to fit in the aperture, we need to use the
683 * BLT engine to support it. Prior to Sandybridge, the BLT paths can't
684 * handle Y-tiling, so we need to fall back to X.
685 */
686 if (brw->gen < 6 && y_or_x && mt->bo->size >= brw->max_gtt_map_object_size) {
687 perf_debug("%dx%d miptree larger than aperture; falling back to X-tiled\n",
688 mt->total_width, mt->total_height);
689
690 mt->tiling = I915_TILING_X;
691 drm_intel_bo_unreference(mt->bo);
692 mt->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "miptree",
693 total_width, total_height, mt->cpp,
694 &mt->tiling, &pitch, alloc_flags);
695 mt->pitch = pitch;
696 }
697
698 mt->offset = 0;
699
700 if (!mt->bo) {
701 intel_miptree_release(&mt);
702 return NULL;
703 }
704
705
706 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_CMS) {
707 assert(mt->num_samples > 1);
708 if (!intel_miptree_alloc_mcs(brw, mt, num_samples)) {
709 intel_miptree_release(&mt);
710 return NULL;
711 }
712 }
713
714 /* If this miptree is capable of supporting fast color clears, set
715 * fast_clear_state appropriately to ensure that fast clears will occur.
716 * Allocation of the MCS miptree will be deferred until the first fast
717 * clear actually occurs.
718 */
719 if (intel_tiling_supports_non_msrt_mcs(brw, mt->tiling) &&
720 intel_miptree_supports_non_msrt_fast_clear(brw, mt)) {
721 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
722 assert(brw->gen < 8 || mt->halign == 16 || num_samples <= 1);
723 }
724
725 return mt;
726 }
727
728 struct intel_mipmap_tree *
729 intel_miptree_create_for_bo(struct brw_context *brw,
730 drm_intel_bo *bo,
731 mesa_format format,
732 uint32_t offset,
733 uint32_t width,
734 uint32_t height,
735 uint32_t depth,
736 int pitch,
737 uint32_t layout_flags)
738 {
739 struct intel_mipmap_tree *mt;
740 uint32_t tiling, swizzle;
741 GLenum target;
742
743 drm_intel_bo_get_tiling(bo, &tiling, &swizzle);
744
745 /* Nothing will be able to use this miptree with the BO if the offset isn't
746 * aligned.
747 */
748 if (tiling != I915_TILING_NONE)
749 assert(offset % 4096 == 0);
750
751 /* miptrees can't handle negative pitch. If you need flipping of images,
752 * that's outside of the scope of the mt.
753 */
754 assert(pitch >= 0);
755
756 target = depth > 1 ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;
757
758 /* The BO already has a tiling format and we shouldn't confuse the lower
759 * layers by making it try to find a tiling format again.
760 */
761 assert((layout_flags & MIPTREE_LAYOUT_TILING_ANY) == 0);
762 assert((layout_flags & MIPTREE_LAYOUT_TILING_NONE) == 0);
763
764 layout_flags |= MIPTREE_LAYOUT_FOR_BO;
765 mt = intel_miptree_create_layout(brw, target, format,
766 0, 0,
767 width, height, depth, 0,
768 layout_flags);
769 if (!mt)
770 return NULL;
771
772 drm_intel_bo_reference(bo);
773 mt->bo = bo;
774 mt->pitch = pitch;
775 mt->offset = offset;
776 mt->tiling = tiling;
777
778 return mt;
779 }
780
781 /**
782 * For a singlesample renderbuffer, this simply wraps the given BO with a
783 * miptree.
784 *
785 * For a multisample renderbuffer, this wraps the window system's
786 * (singlesample) BO with a singlesample miptree attached to the
787 * intel_renderbuffer, then creates a multisample miptree attached to irb->mt
788 * that will contain the actual rendering (which is lazily resolved to
789 * irb->singlesample_mt).
790 */
791 void
792 intel_update_winsys_renderbuffer_miptree(struct brw_context *intel,
793 struct intel_renderbuffer *irb,
794 drm_intel_bo *bo,
795 uint32_t width, uint32_t height,
796 uint32_t pitch)
797 {
798 struct intel_mipmap_tree *singlesample_mt = NULL;
799 struct intel_mipmap_tree *multisample_mt = NULL;
800 struct gl_renderbuffer *rb = &irb->Base.Base;
801 mesa_format format = rb->Format;
802 int num_samples = rb->NumSamples;
803
804 /* Only the front and back buffers, which are color buffers, are allocated
805 * through the image loader.
806 */
807 assert(_mesa_get_format_base_format(format) == GL_RGB ||
808 _mesa_get_format_base_format(format) == GL_RGBA);
809
810 singlesample_mt = intel_miptree_create_for_bo(intel,
811 bo,
812 format,
813 0,
814 width,
815 height,
816 1,
817 pitch,
818 0);
819 if (!singlesample_mt)
820 goto fail;
821
822 /* If this miptree is capable of supporting fast color clears, set
823 * mcs_state appropriately to ensure that fast clears will occur.
824 * Allocation of the MCS miptree will be deferred until the first fast
825 * clear actually occurs.
826 */
827 if (intel_tiling_supports_non_msrt_mcs(intel, singlesample_mt->tiling) &&
828 intel_miptree_supports_non_msrt_fast_clear(intel, singlesample_mt)) {
829 singlesample_mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_RESOLVED;
830 }
831
832 if (num_samples == 0) {
833 intel_miptree_release(&irb->mt);
834 irb->mt = singlesample_mt;
835
836 assert(!irb->singlesample_mt);
837 } else {
838 intel_miptree_release(&irb->singlesample_mt);
839 irb->singlesample_mt = singlesample_mt;
840
841 if (!irb->mt ||
842 irb->mt->logical_width0 != width ||
843 irb->mt->logical_height0 != height) {
844 multisample_mt = intel_miptree_create_for_renderbuffer(intel,
845 format,
846 width,
847 height,
848 num_samples);
849 if (!multisample_mt)
850 goto fail;
851
852 irb->need_downsample = false;
853 intel_miptree_release(&irb->mt);
854 irb->mt = multisample_mt;
855 }
856 }
857 return;
858
859 fail:
860 intel_miptree_release(&irb->singlesample_mt);
861 intel_miptree_release(&irb->mt);
862 return;
863 }
864
865 struct intel_mipmap_tree*
866 intel_miptree_create_for_renderbuffer(struct brw_context *brw,
867 mesa_format format,
868 uint32_t width,
869 uint32_t height,
870 uint32_t num_samples)
871 {
872 struct intel_mipmap_tree *mt;
873 uint32_t depth = 1;
874 bool ok;
875 GLenum target = num_samples > 1 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
876 const uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
877 MIPTREE_LAYOUT_TILING_ANY;
878
879
880 mt = intel_miptree_create(brw, target, format, 0, 0,
881 width, height, depth, num_samples,
882 layout_flags);
883 if (!mt)
884 goto fail;
885
886 if (intel_miptree_wants_hiz_buffer(brw, mt)) {
887 ok = intel_miptree_alloc_hiz(brw, mt);
888 if (!ok)
889 goto fail;
890 }
891
892 return mt;
893
894 fail:
895 intel_miptree_release(&mt);
896 return NULL;
897 }
898
899 void
900 intel_miptree_reference(struct intel_mipmap_tree **dst,
901 struct intel_mipmap_tree *src)
902 {
903 if (*dst == src)
904 return;
905
906 intel_miptree_release(dst);
907
908 if (src) {
909 src->refcount++;
910 DBG("%s %p refcount now %d\n", __func__, src, src->refcount);
911 }
912
913 *dst = src;
914 }
915
916
917 void
918 intel_miptree_release(struct intel_mipmap_tree **mt)
919 {
920 if (!*mt)
921 return;
922
923 DBG("%s %p refcount will be %d\n", __func__, *mt, (*mt)->refcount - 1);
924 if (--(*mt)->refcount <= 0) {
925 GLuint i;
926
927 DBG("%s deleting %p\n", __func__, *mt);
928
929 drm_intel_bo_unreference((*mt)->bo);
930 intel_miptree_release(&(*mt)->stencil_mt);
931 if ((*mt)->hiz_buf) {
932 if ((*mt)->hiz_buf->mt)
933 intel_miptree_release(&(*mt)->hiz_buf->mt);
934 else
935 drm_intel_bo_unreference((*mt)->hiz_buf->bo);
936 free((*mt)->hiz_buf);
937 }
938 intel_miptree_release(&(*mt)->mcs_mt);
939 intel_resolve_map_clear(&(*mt)->hiz_map);
940
941 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
942 free((*mt)->level[i].slice);
943 }
944
945 free(*mt);
946 }
947 *mt = NULL;
948 }
949
950
951 void
952 intel_get_image_dims(struct gl_texture_image *image,
953 int *width, int *height, int *depth)
954 {
955 switch (image->TexObject->Target) {
956 case GL_TEXTURE_1D_ARRAY:
957 /* For a 1D Array texture the OpenGL API will treat the image height as
958 * the number of array slices. For Intel hardware, we treat the 1D array
959 * as a 2D Array with a height of 1. So, here we want to swap image
960 * height and depth.
961 */
962 *width = image->Width;
963 *height = 1;
964 *depth = image->Height;
965 break;
966 default:
967 *width = image->Width;
968 *height = image->Height;
969 *depth = image->Depth;
970 break;
971 }
972 }
973
974 /**
975 * Can the image be pulled into a unified mipmap tree? This mirrors
976 * the completeness test in a lot of ways.
977 *
978 * Not sure whether I want to pass gl_texture_image here.
979 */
980 bool
981 intel_miptree_match_image(struct intel_mipmap_tree *mt,
982 struct gl_texture_image *image)
983 {
984 struct intel_texture_image *intelImage = intel_texture_image(image);
985 GLuint level = intelImage->base.Base.Level;
986 int width, height, depth;
987
988 /* glTexImage* choose the texture object based on the target passed in, and
989 * objects can't change targets over their lifetimes, so this should be
990 * true.
991 */
992 assert(image->TexObject->Target == mt->target);
993
994 mesa_format mt_format = mt->format;
995 if (mt->format == MESA_FORMAT_Z24_UNORM_X8_UINT && mt->stencil_mt)
996 mt_format = MESA_FORMAT_Z24_UNORM_S8_UINT;
997 if (mt->format == MESA_FORMAT_Z_FLOAT32 && mt->stencil_mt)
998 mt_format = MESA_FORMAT_Z32_FLOAT_S8X24_UINT;
999 if (mt->etc_format != MESA_FORMAT_NONE)
1000 mt_format = mt->etc_format;
1001
1002 if (image->TexFormat != mt_format)
1003 return false;
1004
1005 intel_get_image_dims(image, &width, &height, &depth);
1006
1007 if (mt->target == GL_TEXTURE_CUBE_MAP)
1008 depth = 6;
1009
1010 int level_depth = mt->level[level].depth;
1011 if (mt->num_samples > 1) {
1012 switch (mt->msaa_layout) {
1013 case INTEL_MSAA_LAYOUT_NONE:
1014 case INTEL_MSAA_LAYOUT_IMS:
1015 break;
1016 case INTEL_MSAA_LAYOUT_UMS:
1017 case INTEL_MSAA_LAYOUT_CMS:
1018 level_depth /= mt->num_samples;
1019 break;
1020 }
1021 }
1022
1023 /* Test image dimensions against the base level image adjusted for
1024 * minification. This will also catch images not present in the
1025 * tree, changed targets, etc.
1026 */
1027 if (width != minify(mt->logical_width0, level - mt->first_level) ||
1028 height != minify(mt->logical_height0, level - mt->first_level) ||
1029 depth != level_depth) {
1030 return false;
1031 }
1032
1033 if (image->NumSamples != mt->num_samples)
1034 return false;
1035
1036 return true;
1037 }
1038
1039
1040 void
1041 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
1042 GLuint level,
1043 GLuint x, GLuint y, GLuint d)
1044 {
1045 mt->level[level].depth = d;
1046 mt->level[level].level_x = x;
1047 mt->level[level].level_y = y;
1048
1049 DBG("%s level %d, depth %d, offset %d,%d\n", __func__,
1050 level, d, x, y);
1051
1052 assert(mt->level[level].slice == NULL);
1053
1054 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
1055 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
1056 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
1057 }
1058
1059
1060 void
1061 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
1062 GLuint level, GLuint img,
1063 GLuint x, GLuint y)
1064 {
1065 if (img == 0 && level == 0)
1066 assert(x == 0 && y == 0);
1067
1068 assert(img < mt->level[level].depth);
1069
1070 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
1071 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
1072
1073 DBG("%s level %d img %d pos %d,%d\n",
1074 __func__, level, img,
1075 mt->level[level].slice[img].x_offset,
1076 mt->level[level].slice[img].y_offset);
1077 }
1078
1079 void
1080 intel_miptree_get_image_offset(const struct intel_mipmap_tree *mt,
1081 GLuint level, GLuint slice,
1082 GLuint *x, GLuint *y)
1083 {
1084 assert(slice < mt->level[level].depth);
1085
1086 *x = mt->level[level].slice[slice].x_offset;
1087 *y = mt->level[level].slice[slice].y_offset;
1088 }
1089
1090
1091 /**
1092 * This function computes the tile_w (in bytes) and tile_h (in rows) of
1093 * different tiling patterns. If the BO is untiled, tile_w is set to cpp
1094 * and tile_h is set to 1.
1095 */
1096 void
1097 intel_get_tile_dims(uint32_t tiling, uint32_t tr_mode, uint32_t cpp,
1098 uint32_t *tile_w, uint32_t *tile_h)
1099 {
1100 if (tr_mode == INTEL_MIPTREE_TRMODE_NONE) {
1101 switch (tiling) {
1102 case I915_TILING_X:
1103 *tile_w = 512;
1104 *tile_h = 8;
1105 break;
1106 case I915_TILING_Y:
1107 *tile_w = 128;
1108 *tile_h = 32;
1109 break;
1110 case I915_TILING_NONE:
1111 *tile_w = cpp;
1112 *tile_h = 1;
1113 break;
1114 default:
1115 unreachable("not reached");
1116 }
1117 } else {
1118 uint32_t aspect_ratio = 1;
1119 assert(_mesa_is_pow_two(cpp));
1120
1121 switch (cpp) {
1122 case 1:
1123 *tile_h = 64;
1124 break;
1125 case 2:
1126 case 4:
1127 *tile_h = 32;
1128 break;
1129 case 8:
1130 case 16:
1131 *tile_h = 16;
1132 break;
1133 default:
1134 unreachable("not reached");
1135 }
1136
1137 if (cpp == 2 || cpp == 8)
1138 aspect_ratio = 2;
1139
1140 if (tr_mode == INTEL_MIPTREE_TRMODE_YS)
1141 *tile_h *= 4;
1142
1143 *tile_w = *tile_h * aspect_ratio * cpp;
1144 }
1145 }
1146
1147
1148 /**
1149 * This function computes masks that may be used to select the bits of the X
1150 * and Y coordinates that indicate the offset within a tile. If the BO is
1151 * untiled, the masks are set to 0.
1152 */
1153 void
1154 intel_get_tile_masks(uint32_t tiling, uint32_t tr_mode, uint32_t cpp,
1155 bool map_stencil_as_y_tiled,
1156 uint32_t *mask_x, uint32_t *mask_y)
1157 {
1158 uint32_t tile_w_bytes, tile_h;
1159 if (map_stencil_as_y_tiled)
1160 tiling = I915_TILING_Y;
1161
1162 intel_get_tile_dims(tiling, tr_mode, cpp, &tile_w_bytes, &tile_h);
1163
1164 *mask_x = tile_w_bytes / cpp - 1;
1165 *mask_y = tile_h - 1;
1166 }
1167
1168 /**
1169 * Compute the offset (in bytes) from the start of the BO to the given x
1170 * and y coordinate. For tiled BOs, caller must ensure that x and y are
1171 * multiples of the tile size.
1172 */
1173 uint32_t
1174 intel_miptree_get_aligned_offset(const struct intel_mipmap_tree *mt,
1175 uint32_t x, uint32_t y,
1176 bool map_stencil_as_y_tiled)
1177 {
1178 int cpp = mt->cpp;
1179 uint32_t pitch = mt->pitch;
1180 uint32_t tiling = mt->tiling;
1181
1182 if (map_stencil_as_y_tiled) {
1183 tiling = I915_TILING_Y;
1184
1185 /* When mapping a W-tiled stencil buffer as Y-tiled, each 64-high W-tile
1186 * gets transformed into a 32-high Y-tile. Accordingly, the pitch of
1187 * the resulting surface is twice the pitch of the original miptree,
1188 * since each row in the Y-tiled view corresponds to two rows in the
1189 * actual W-tiled surface. So we need to correct the pitch before
1190 * computing the offsets.
1191 */
1192 pitch *= 2;
1193 }
1194
1195 switch (tiling) {
1196 default:
1197 unreachable("not reached");
1198 case I915_TILING_NONE:
1199 return y * pitch + x * cpp;
1200 case I915_TILING_X:
1201 assert((x % (512 / cpp)) == 0);
1202 assert((y % 8) == 0);
1203 return y * pitch + x / (512 / cpp) * 4096;
1204 case I915_TILING_Y:
1205 assert((x % (128 / cpp)) == 0);
1206 assert((y % 32) == 0);
1207 return y * pitch + x / (128 / cpp) * 4096;
1208 }
1209 }
1210
1211 /**
1212 * Rendering with tiled buffers requires that the base address of the buffer
1213 * be aligned to a page boundary. For renderbuffers, and sometimes with
1214 * textures, we may want the surface to point at a texture image level that
1215 * isn't at a page boundary.
1216 *
1217 * This function returns an appropriately-aligned base offset
1218 * according to the tiling restrictions, plus any required x/y offset
1219 * from there.
1220 */
1221 uint32_t
1222 intel_miptree_get_tile_offsets(const struct intel_mipmap_tree *mt,
1223 GLuint level, GLuint slice,
1224 uint32_t *tile_x,
1225 uint32_t *tile_y)
1226 {
1227 uint32_t x, y;
1228 uint32_t mask_x, mask_y;
1229
1230 intel_get_tile_masks(mt->tiling, mt->tr_mode, mt->cpp, false, &mask_x, &mask_y);
1231 intel_miptree_get_image_offset(mt, level, slice, &x, &y);
1232
1233 *tile_x = x & mask_x;
1234 *tile_y = y & mask_y;
1235
1236 return intel_miptree_get_aligned_offset(mt, x & ~mask_x, y & ~mask_y, false);
1237 }
1238
1239 static void
1240 intel_miptree_copy_slice_sw(struct brw_context *brw,
1241 struct intel_mipmap_tree *dst_mt,
1242 struct intel_mipmap_tree *src_mt,
1243 int level,
1244 int slice,
1245 int width,
1246 int height)
1247 {
1248 void *src, *dst;
1249 ptrdiff_t src_stride, dst_stride;
1250 int cpp = dst_mt->cpp;
1251
1252 intel_miptree_map(brw, src_mt,
1253 level, slice,
1254 0, 0,
1255 width, height,
1256 GL_MAP_READ_BIT | BRW_MAP_DIRECT_BIT,
1257 &src, &src_stride);
1258
1259 intel_miptree_map(brw, dst_mt,
1260 level, slice,
1261 0, 0,
1262 width, height,
1263 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
1264 BRW_MAP_DIRECT_BIT,
1265 &dst, &dst_stride);
1266
1267 DBG("sw blit %s mt %p %p/%"PRIdPTR" -> %s mt %p %p/%"PRIdPTR" (%dx%d)\n",
1268 _mesa_get_format_name(src_mt->format),
1269 src_mt, src, src_stride,
1270 _mesa_get_format_name(dst_mt->format),
1271 dst_mt, dst, dst_stride,
1272 width, height);
1273
1274 int row_size = cpp * width;
1275 if (src_stride == row_size &&
1276 dst_stride == row_size) {
1277 memcpy(dst, src, row_size * height);
1278 } else {
1279 for (int i = 0; i < height; i++) {
1280 memcpy(dst, src, row_size);
1281 dst += dst_stride;
1282 src += src_stride;
1283 }
1284 }
1285
1286 intel_miptree_unmap(brw, dst_mt, level, slice);
1287 intel_miptree_unmap(brw, src_mt, level, slice);
1288
1289 /* Don't forget to copy the stencil data over, too. We could have skipped
1290 * passing BRW_MAP_DIRECT_BIT, but that would have meant intel_miptree_map
1291 * shuffling the two data sources in/out of temporary storage instead of
1292 * the direct mapping we get this way.
1293 */
1294 if (dst_mt->stencil_mt) {
1295 assert(src_mt->stencil_mt);
1296 intel_miptree_copy_slice_sw(brw, dst_mt->stencil_mt, src_mt->stencil_mt,
1297 level, slice, width, height);
1298 }
1299 }
1300
1301 static void
1302 intel_miptree_copy_slice(struct brw_context *brw,
1303 struct intel_mipmap_tree *dst_mt,
1304 struct intel_mipmap_tree *src_mt,
1305 int level,
1306 int face,
1307 int depth)
1308
1309 {
1310 mesa_format format = src_mt->format;
1311 uint32_t width = minify(src_mt->physical_width0, level - src_mt->first_level);
1312 uint32_t height = minify(src_mt->physical_height0, level - src_mt->first_level);
1313 int slice;
1314
1315 if (face > 0)
1316 slice = face;
1317 else
1318 slice = depth;
1319
1320 assert(depth < src_mt->level[level].depth);
1321 assert(src_mt->format == dst_mt->format);
1322
1323 if (dst_mt->compressed) {
1324 unsigned int i, j;
1325 _mesa_get_format_block_size(dst_mt->format, &i, &j);
1326 height = ALIGN_NPOT(height, j) / j;
1327 width = ALIGN_NPOT(width, i) / i;
1328 }
1329
1330 /* If it's a packed depth/stencil buffer with separate stencil, the blit
1331 * below won't apply since we can't do the depth's Y tiling or the
1332 * stencil's W tiling in the blitter.
1333 */
1334 if (src_mt->stencil_mt) {
1335 intel_miptree_copy_slice_sw(brw,
1336 dst_mt, src_mt,
1337 level, slice,
1338 width, height);
1339 return;
1340 }
1341
1342 uint32_t dst_x, dst_y, src_x, src_y;
1343 intel_miptree_get_image_offset(dst_mt, level, slice, &dst_x, &dst_y);
1344 intel_miptree_get_image_offset(src_mt, level, slice, &src_x, &src_y);
1345
1346 DBG("validate blit mt %s %p %d,%d/%d -> mt %s %p %d,%d/%d (%dx%d)\n",
1347 _mesa_get_format_name(src_mt->format),
1348 src_mt, src_x, src_y, src_mt->pitch,
1349 _mesa_get_format_name(dst_mt->format),
1350 dst_mt, dst_x, dst_y, dst_mt->pitch,
1351 width, height);
1352
1353 if (!intel_miptree_blit(brw,
1354 src_mt, level, slice, 0, 0, false,
1355 dst_mt, level, slice, 0, 0, false,
1356 width, height, GL_COPY)) {
1357 perf_debug("miptree validate blit for %s failed\n",
1358 _mesa_get_format_name(format));
1359
1360 intel_miptree_copy_slice_sw(brw, dst_mt, src_mt, level, slice,
1361 width, height);
1362 }
1363 }
1364
1365 /**
1366 * Copies the image's current data to the given miptree, and associates that
1367 * miptree with the image.
1368 *
1369 * If \c invalidate is true, then the actual image data does not need to be
1370 * copied, but the image still needs to be associated to the new miptree (this
1371 * is set to true if we're about to clear the image).
1372 */
1373 void
1374 intel_miptree_copy_teximage(struct brw_context *brw,
1375 struct intel_texture_image *intelImage,
1376 struct intel_mipmap_tree *dst_mt,
1377 bool invalidate)
1378 {
1379 struct intel_mipmap_tree *src_mt = intelImage->mt;
1380 struct intel_texture_object *intel_obj =
1381 intel_texture_object(intelImage->base.Base.TexObject);
1382 int level = intelImage->base.Base.Level;
1383 int face = intelImage->base.Base.Face;
1384
1385 GLuint depth;
1386 if (intel_obj->base.Target == GL_TEXTURE_1D_ARRAY)
1387 depth = intelImage->base.Base.Height;
1388 else
1389 depth = intelImage->base.Base.Depth;
1390
1391 if (!invalidate) {
1392 for (int slice = 0; slice < depth; slice++) {
1393 intel_miptree_copy_slice(brw, dst_mt, src_mt, level, face, slice);
1394 }
1395 }
1396
1397 intel_miptree_reference(&intelImage->mt, dst_mt);
1398 intel_obj->needs_validate = true;
1399 }
1400
1401 static bool
1402 intel_miptree_alloc_mcs(struct brw_context *brw,
1403 struct intel_mipmap_tree *mt,
1404 GLuint num_samples)
1405 {
1406 assert(brw->gen >= 7); /* MCS only used on Gen7+ */
1407 assert(mt->mcs_mt == NULL);
1408 assert(!mt->disable_aux_buffers);
1409
1410 /* Choose the correct format for the MCS buffer. All that really matters
1411 * is that we allocate the right buffer size, since we'll always be
1412 * accessing this miptree using MCS-specific hardware mechanisms, which
1413 * infer the correct format based on num_samples.
1414 */
1415 mesa_format format;
1416 switch (num_samples) {
1417 case 2:
1418 case 4:
1419 /* 8 bits/pixel are required for MCS data when using 4x MSAA (2 bits for
1420 * each sample).
1421 */
1422 format = MESA_FORMAT_R_UNORM8;
1423 break;
1424 case 8:
1425 /* 32 bits/pixel are required for MCS data when using 8x MSAA (3 bits
1426 * for each sample, plus 8 padding bits).
1427 */
1428 format = MESA_FORMAT_R_UINT32;
1429 break;
1430 default:
1431 unreachable("Unrecognized sample count in intel_miptree_alloc_mcs");
1432 };
1433
1434 /* From the Ivy Bridge PRM, Vol4 Part1 p76, "MCS Base Address":
1435 *
1436 * "The MCS surface must be stored as Tile Y."
1437 */
1438 const uint32_t mcs_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
1439 MIPTREE_LAYOUT_TILING_Y;
1440 mt->mcs_mt = intel_miptree_create(brw,
1441 mt->target,
1442 format,
1443 mt->first_level,
1444 mt->last_level,
1445 mt->logical_width0,
1446 mt->logical_height0,
1447 mt->logical_depth0,
1448 0 /* num_samples */,
1449 mcs_flags);
1450
1451 /* From the Ivy Bridge PRM, Vol 2 Part 1 p326:
1452 *
1453 * When MCS buffer is enabled and bound to MSRT, it is required that it
1454 * is cleared prior to any rendering.
1455 *
1456 * Since we don't use the MCS buffer for any purpose other than rendering,
1457 * it makes sense to just clear it immediately upon allocation.
1458 *
1459 * Note: the clear value for MCS buffers is all 1's, so we memset to 0xff.
1460 */
1461 void *data = intel_miptree_map_raw(brw, mt->mcs_mt);
1462 memset(data, 0xff, mt->mcs_mt->total_height * mt->mcs_mt->pitch);
1463 intel_miptree_unmap_raw(mt->mcs_mt);
1464 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_CLEAR;
1465
1466 return mt->mcs_mt;
1467 }
1468
1469
1470 bool
1471 intel_miptree_alloc_non_msrt_mcs(struct brw_context *brw,
1472 struct intel_mipmap_tree *mt)
1473 {
1474 assert(mt->mcs_mt == NULL);
1475 assert(!mt->disable_aux_buffers);
1476
1477 /* The format of the MCS buffer is opaque to the driver; all that matters
1478 * is that we get its size and pitch right. We'll pretend that the format
1479 * is R32. Since an MCS tile covers 128 blocks horizontally, and a Y-tiled
1480 * R32 buffer is 32 pixels across, we'll need to scale the width down by
1481 * the block width and then a further factor of 4. Since an MCS tile
1482 * covers 256 blocks vertically, and a Y-tiled R32 buffer is 32 rows high,
1483 * we'll need to scale the height down by the block height and then a
1484 * further factor of 8.
1485 */
1486 const mesa_format format = MESA_FORMAT_R_UINT32;
1487 unsigned block_width_px;
1488 unsigned block_height;
1489 intel_get_non_msrt_mcs_alignment(mt, &block_width_px, &block_height);
1490 unsigned width_divisor = block_width_px * 4;
1491 unsigned height_divisor = block_height * 8;
1492 unsigned mcs_width =
1493 ALIGN(mt->logical_width0, width_divisor) / width_divisor;
1494 unsigned mcs_height =
1495 ALIGN(mt->logical_height0, height_divisor) / height_divisor;
1496 assert(mt->logical_depth0 == 1);
1497 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD |
1498 MIPTREE_LAYOUT_TILING_Y;
1499 if (brw->gen >= 8) {
1500 layout_flags |= MIPTREE_LAYOUT_FORCE_HALIGN16;
1501 }
1502 mt->mcs_mt = intel_miptree_create(brw,
1503 mt->target,
1504 format,
1505 mt->first_level,
1506 mt->last_level,
1507 mcs_width,
1508 mcs_height,
1509 mt->logical_depth0,
1510 0 /* num_samples */,
1511 layout_flags);
1512
1513 return mt->mcs_mt;
1514 }
1515
1516
1517 /**
1518 * Helper for intel_miptree_alloc_hiz() that sets
1519 * \c mt->level[level].has_hiz. Return true if and only if
1520 * \c has_hiz was set.
1521 */
1522 static bool
1523 intel_miptree_level_enable_hiz(struct brw_context *brw,
1524 struct intel_mipmap_tree *mt,
1525 uint32_t level)
1526 {
1527 assert(mt->hiz_buf);
1528
1529 if (brw->gen >= 8 || brw->is_haswell) {
1530 uint32_t width = minify(mt->physical_width0, level);
1531 uint32_t height = minify(mt->physical_height0, level);
1532
1533 /* Disable HiZ for LOD > 0 unless the width is 8 aligned
1534 * and the height is 4 aligned. This allows our HiZ support
1535 * to fulfill Haswell restrictions for HiZ ops. For LOD == 0,
1536 * we can grow the width & height to allow the HiZ op to
1537 * force the proper size alignments.
1538 */
1539 if (level > 0 && ((width & 7) || (height & 3))) {
1540 DBG("mt %p level %d: HiZ DISABLED\n", mt, level);
1541 return false;
1542 }
1543 }
1544
1545 DBG("mt %p level %d: HiZ enabled\n", mt, level);
1546 mt->level[level].has_hiz = true;
1547 return true;
1548 }
1549
1550
1551 /**
1552 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1553 * buffer dimensions and allocates a bo for the hiz buffer.
1554 */
1555 static struct intel_miptree_aux_buffer *
1556 intel_gen7_hiz_buf_create(struct brw_context *brw,
1557 struct intel_mipmap_tree *mt)
1558 {
1559 unsigned z_width = mt->logical_width0;
1560 unsigned z_height = mt->logical_height0;
1561 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1562 unsigned hz_width, hz_height;
1563 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1564
1565 if (!buf)
1566 return NULL;
1567
1568 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1569 * adjustments required for Z_Height and Z_Width based on multisampling.
1570 */
1571 switch (mt->num_samples) {
1572 case 0:
1573 case 1:
1574 break;
1575 case 2:
1576 case 4:
1577 z_width *= 2;
1578 z_height *= 2;
1579 break;
1580 case 8:
1581 z_width *= 4;
1582 z_height *= 2;
1583 break;
1584 default:
1585 unreachable("unsupported sample count");
1586 }
1587
1588 const unsigned vertical_align = 8; /* 'j' in the docs */
1589 const unsigned H0 = z_height;
1590 const unsigned h0 = ALIGN(H0, vertical_align);
1591 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1592 const unsigned Z0 = z_depth;
1593
1594 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1595 hz_width = ALIGN(z_width, 16);
1596
1597 if (mt->target == GL_TEXTURE_3D) {
1598 unsigned H_i = H0;
1599 unsigned Z_i = Z0;
1600 hz_height = 0;
1601 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1602 unsigned h_i = ALIGN(H_i, vertical_align);
1603 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1604 hz_height += h_i * Z_i;
1605 H_i = minify(H_i, 1);
1606 Z_i = minify(Z_i, 1);
1607 }
1608 /* HZ_Height =
1609 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1610 */
1611 hz_height = DIV_ROUND_UP(hz_height, 2);
1612 } else {
1613 const unsigned hz_qpitch = h0 + h1 + (12 * vertical_align);
1614 if (mt->target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1615 mt->target == GL_TEXTURE_CUBE_MAP) {
1616 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth * 6/2) /8 ) * 8 */
1617 hz_height = DIV_ROUND_UP(hz_qpitch * Z0 * 6, 2 * 8) * 8;
1618 } else {
1619 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1620 hz_height = DIV_ROUND_UP(hz_qpitch * Z0, 2 * 8) * 8;
1621 }
1622 }
1623
1624 unsigned long pitch;
1625 uint32_t tiling = I915_TILING_Y;
1626 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1627 hz_width, hz_height, 1,
1628 &tiling, &pitch,
1629 BO_ALLOC_FOR_RENDER);
1630 if (!buf->bo) {
1631 free(buf);
1632 return NULL;
1633 } else if (tiling != I915_TILING_Y) {
1634 drm_intel_bo_unreference(buf->bo);
1635 free(buf);
1636 return NULL;
1637 }
1638
1639 buf->pitch = pitch;
1640
1641 return buf;
1642 }
1643
1644
1645 /**
1646 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1647 * buffer dimensions and allocates a bo for the hiz buffer.
1648 */
1649 static struct intel_miptree_aux_buffer *
1650 intel_gen8_hiz_buf_create(struct brw_context *brw,
1651 struct intel_mipmap_tree *mt)
1652 {
1653 unsigned z_width = mt->logical_width0;
1654 unsigned z_height = mt->logical_height0;
1655 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1656 unsigned hz_width, hz_height;
1657 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1658
1659 if (!buf)
1660 return NULL;
1661
1662 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1663 * adjustments required for Z_Height and Z_Width based on multisampling.
1664 */
1665 if (brw->gen < 9) {
1666 switch (mt->num_samples) {
1667 case 0:
1668 case 1:
1669 break;
1670 case 2:
1671 case 4:
1672 z_width *= 2;
1673 z_height *= 2;
1674 break;
1675 case 8:
1676 z_width *= 4;
1677 z_height *= 2;
1678 break;
1679 default:
1680 unreachable("unsupported sample count");
1681 }
1682 }
1683
1684 const unsigned vertical_align = 8; /* 'j' in the docs */
1685 const unsigned H0 = z_height;
1686 const unsigned h0 = ALIGN(H0, vertical_align);
1687 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1688 const unsigned Z0 = z_depth;
1689
1690 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1691 hz_width = ALIGN(z_width, 16);
1692
1693 unsigned H_i = H0;
1694 unsigned Z_i = Z0;
1695 unsigned sum_h_i = 0;
1696 unsigned hz_height_3d_sum = 0;
1697 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1698 unsigned i = level - mt->first_level;
1699 unsigned h_i = ALIGN(H_i, vertical_align);
1700 /* sum(i=2 to m; h_i) */
1701 if (i >= 2) {
1702 sum_h_i += h_i;
1703 }
1704 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1705 hz_height_3d_sum += h_i * Z_i;
1706 H_i = minify(H_i, 1);
1707 Z_i = minify(Z_i, 1);
1708 }
1709 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1710 buf->qpitch = h0 + MAX2(h1, sum_h_i);
1711
1712 if (mt->target == GL_TEXTURE_3D) {
1713 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1714 hz_height = DIV_ROUND_UP(hz_height_3d_sum, 2);
1715 } else {
1716 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1717 hz_height = DIV_ROUND_UP(buf->qpitch, 2 * 8) * 8 * Z0;
1718 if (mt->target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1719 mt->target == GL_TEXTURE_CUBE_MAP) {
1720 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * 6 * Z_Depth
1721 *
1722 * We can can just take our hz_height calculation from above, and
1723 * multiply by 6 for the cube map and cube map array types.
1724 */
1725 hz_height *= 6;
1726 }
1727 }
1728
1729 unsigned long pitch;
1730 uint32_t tiling = I915_TILING_Y;
1731 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1732 hz_width, hz_height, 1,
1733 &tiling, &pitch,
1734 BO_ALLOC_FOR_RENDER);
1735 if (!buf->bo) {
1736 free(buf);
1737 return NULL;
1738 } else if (tiling != I915_TILING_Y) {
1739 drm_intel_bo_unreference(buf->bo);
1740 free(buf);
1741 return NULL;
1742 }
1743
1744 buf->pitch = pitch;
1745
1746 return buf;
1747 }
1748
1749
1750 static struct intel_miptree_aux_buffer *
1751 intel_hiz_miptree_buf_create(struct brw_context *brw,
1752 struct intel_mipmap_tree *mt)
1753 {
1754 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1755 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
1756
1757 if (brw->gen == 6)
1758 layout_flags |= MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD;
1759
1760 if (!buf)
1761 return NULL;
1762
1763 layout_flags |= MIPTREE_LAYOUT_TILING_ANY;
1764 buf->mt = intel_miptree_create(brw,
1765 mt->target,
1766 mt->format,
1767 mt->first_level,
1768 mt->last_level,
1769 mt->logical_width0,
1770 mt->logical_height0,
1771 mt->logical_depth0,
1772 mt->num_samples,
1773 layout_flags);
1774 if (!buf->mt) {
1775 free(buf);
1776 return NULL;
1777 }
1778
1779 buf->bo = buf->mt->bo;
1780 buf->pitch = buf->mt->pitch;
1781 buf->qpitch = buf->mt->qpitch;
1782
1783 return buf;
1784 }
1785
1786 bool
1787 intel_miptree_wants_hiz_buffer(struct brw_context *brw,
1788 struct intel_mipmap_tree *mt)
1789 {
1790 if (!brw->has_hiz)
1791 return false;
1792
1793 if (mt->hiz_buf != NULL)
1794 return false;
1795
1796 if (mt->disable_aux_buffers)
1797 return false;
1798
1799 switch (mt->format) {
1800 case MESA_FORMAT_Z_FLOAT32:
1801 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1802 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1803 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1804 case MESA_FORMAT_Z_UNORM16:
1805 return true;
1806 default:
1807 return false;
1808 }
1809 }
1810
1811 bool
1812 intel_miptree_alloc_hiz(struct brw_context *brw,
1813 struct intel_mipmap_tree *mt)
1814 {
1815 assert(mt->hiz_buf == NULL);
1816 assert(!mt->disable_aux_buffers);
1817
1818 if (brw->gen == 7) {
1819 mt->hiz_buf = intel_gen7_hiz_buf_create(brw, mt);
1820 } else if (brw->gen >= 8) {
1821 mt->hiz_buf = intel_gen8_hiz_buf_create(brw, mt);
1822 } else {
1823 mt->hiz_buf = intel_hiz_miptree_buf_create(brw, mt);
1824 }
1825
1826 if (!mt->hiz_buf)
1827 return false;
1828
1829 /* Mark that all slices need a HiZ resolve. */
1830 for (unsigned level = mt->first_level; level <= mt->last_level; ++level) {
1831 if (!intel_miptree_level_enable_hiz(brw, mt, level))
1832 continue;
1833
1834 for (unsigned layer = 0; layer < mt->level[level].depth; ++layer) {
1835 struct intel_resolve_map *m = malloc(sizeof(struct intel_resolve_map));
1836 exec_node_init(&m->link);
1837 m->level = level;
1838 m->layer = layer;
1839 m->need = GEN6_HIZ_OP_HIZ_RESOLVE;
1840
1841 exec_list_push_tail(&mt->hiz_map, &m->link);
1842 }
1843 }
1844
1845 return true;
1846 }
1847
1848 /**
1849 * Does the miptree slice have hiz enabled?
1850 */
1851 bool
1852 intel_miptree_level_has_hiz(struct intel_mipmap_tree *mt, uint32_t level)
1853 {
1854 intel_miptree_check_level_layer(mt, level, 0);
1855 return mt->level[level].has_hiz;
1856 }
1857
1858 void
1859 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1860 uint32_t level,
1861 uint32_t layer)
1862 {
1863 if (!intel_miptree_level_has_hiz(mt, level))
1864 return;
1865
1866 intel_resolve_map_set(&mt->hiz_map,
1867 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
1868 }
1869
1870
1871 void
1872 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
1873 uint32_t level,
1874 uint32_t layer)
1875 {
1876 if (!intel_miptree_level_has_hiz(mt, level))
1877 return;
1878
1879 intel_resolve_map_set(&mt->hiz_map,
1880 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
1881 }
1882
1883 void
1884 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
1885 uint32_t level)
1886 {
1887 uint32_t layer;
1888 uint32_t end_layer = mt->level[level].depth;
1889
1890 for (layer = 0; layer < end_layer; layer++) {
1891 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
1892 }
1893 }
1894
1895 static bool
1896 intel_miptree_slice_resolve(struct brw_context *brw,
1897 struct intel_mipmap_tree *mt,
1898 uint32_t level,
1899 uint32_t layer,
1900 enum gen6_hiz_op need)
1901 {
1902 intel_miptree_check_level_layer(mt, level, layer);
1903
1904 struct intel_resolve_map *item =
1905 intel_resolve_map_get(&mt->hiz_map, level, layer);
1906
1907 if (!item || item->need != need)
1908 return false;
1909
1910 intel_hiz_exec(brw, mt, level, layer, need);
1911 intel_resolve_map_remove(item);
1912 return true;
1913 }
1914
1915 bool
1916 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
1917 struct intel_mipmap_tree *mt,
1918 uint32_t level,
1919 uint32_t layer)
1920 {
1921 return intel_miptree_slice_resolve(brw, mt, level, layer,
1922 GEN6_HIZ_OP_HIZ_RESOLVE);
1923 }
1924
1925 bool
1926 intel_miptree_slice_resolve_depth(struct brw_context *brw,
1927 struct intel_mipmap_tree *mt,
1928 uint32_t level,
1929 uint32_t layer)
1930 {
1931 return intel_miptree_slice_resolve(brw, mt, level, layer,
1932 GEN6_HIZ_OP_DEPTH_RESOLVE);
1933 }
1934
1935 static bool
1936 intel_miptree_all_slices_resolve(struct brw_context *brw,
1937 struct intel_mipmap_tree *mt,
1938 enum gen6_hiz_op need)
1939 {
1940 bool did_resolve = false;
1941
1942 foreach_list_typed_safe(struct intel_resolve_map, map, link, &mt->hiz_map) {
1943 if (map->need != need)
1944 continue;
1945
1946 intel_hiz_exec(brw, mt, map->level, map->layer, need);
1947 intel_resolve_map_remove(map);
1948 did_resolve = true;
1949 }
1950
1951 return did_resolve;
1952 }
1953
1954 bool
1955 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
1956 struct intel_mipmap_tree *mt)
1957 {
1958 return intel_miptree_all_slices_resolve(brw, mt,
1959 GEN6_HIZ_OP_HIZ_RESOLVE);
1960 }
1961
1962 bool
1963 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
1964 struct intel_mipmap_tree *mt)
1965 {
1966 return intel_miptree_all_slices_resolve(brw, mt,
1967 GEN6_HIZ_OP_DEPTH_RESOLVE);
1968 }
1969
1970
1971 void
1972 intel_miptree_resolve_color(struct brw_context *brw,
1973 struct intel_mipmap_tree *mt)
1974 {
1975 switch (mt->fast_clear_state) {
1976 case INTEL_FAST_CLEAR_STATE_NO_MCS:
1977 case INTEL_FAST_CLEAR_STATE_RESOLVED:
1978 /* No resolve needed */
1979 break;
1980 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
1981 case INTEL_FAST_CLEAR_STATE_CLEAR:
1982 /* Fast color clear resolves only make sense for non-MSAA buffers. */
1983 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE)
1984 brw_meta_resolve_color(brw, mt);
1985 break;
1986 }
1987 }
1988
1989
1990 /**
1991 * Make it possible to share the BO backing the given miptree with another
1992 * process or another miptree.
1993 *
1994 * Fast color clears are unsafe with shared buffers, so we need to resolve and
1995 * then discard the MCS buffer, if present. We also set the fast_clear_state
1996 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
1997 * allocated in the future.
1998 */
1999 void
2000 intel_miptree_make_shareable(struct brw_context *brw,
2001 struct intel_mipmap_tree *mt)
2002 {
2003 /* MCS buffers are also used for multisample buffers, but we can't resolve
2004 * away a multisample MCS buffer because it's an integral part of how the
2005 * pixel data is stored. Fortunately this code path should never be
2006 * reached for multisample buffers.
2007 */
2008 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE);
2009
2010 if (mt->mcs_mt) {
2011 intel_miptree_resolve_color(brw, mt);
2012 intel_miptree_release(&mt->mcs_mt);
2013 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
2014 }
2015 }
2016
2017
2018 /**
2019 * \brief Get pointer offset into stencil buffer.
2020 *
2021 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
2022 * must decode the tile's layout in software.
2023 *
2024 * See
2025 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
2026 * Format.
2027 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
2028 *
2029 * Even though the returned offset is always positive, the return type is
2030 * signed due to
2031 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
2032 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
2033 */
2034 static intptr_t
2035 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
2036 {
2037 uint32_t tile_size = 4096;
2038 uint32_t tile_width = 64;
2039 uint32_t tile_height = 64;
2040 uint32_t row_size = 64 * stride;
2041
2042 uint32_t tile_x = x / tile_width;
2043 uint32_t tile_y = y / tile_height;
2044
2045 /* The byte's address relative to the tile's base addres. */
2046 uint32_t byte_x = x % tile_width;
2047 uint32_t byte_y = y % tile_height;
2048
2049 uintptr_t u = tile_y * row_size
2050 + tile_x * tile_size
2051 + 512 * (byte_x / 8)
2052 + 64 * (byte_y / 8)
2053 + 32 * ((byte_y / 4) % 2)
2054 + 16 * ((byte_x / 4) % 2)
2055 + 8 * ((byte_y / 2) % 2)
2056 + 4 * ((byte_x / 2) % 2)
2057 + 2 * (byte_y % 2)
2058 + 1 * (byte_x % 2);
2059
2060 if (swizzled) {
2061 /* adjust for bit6 swizzling */
2062 if (((byte_x / 8) % 2) == 1) {
2063 if (((byte_y / 8) % 2) == 0) {
2064 u += 64;
2065 } else {
2066 u -= 64;
2067 }
2068 }
2069 }
2070
2071 return u;
2072 }
2073
2074 void
2075 intel_miptree_updownsample(struct brw_context *brw,
2076 struct intel_mipmap_tree *src,
2077 struct intel_mipmap_tree *dst)
2078 {
2079 if (brw->gen < 8) {
2080 brw_blorp_blit_miptrees(brw,
2081 src, 0 /* level */, 0 /* layer */, src->format,
2082 dst, 0 /* level */, 0 /* layer */, dst->format,
2083 0, 0,
2084 src->logical_width0, src->logical_height0,
2085 0, 0,
2086 dst->logical_width0, dst->logical_height0,
2087 GL_NEAREST, false, false /*mirror x, y*/);
2088 } else if (src->format == MESA_FORMAT_S_UINT8) {
2089 brw_meta_stencil_updownsample(brw, src, dst);
2090 } else {
2091 brw_meta_updownsample(brw, src, dst);
2092 }
2093
2094 if (src->stencil_mt) {
2095 if (brw->gen >= 8) {
2096 brw_meta_stencil_updownsample(brw, src->stencil_mt, dst);
2097 return;
2098 }
2099
2100 brw_blorp_blit_miptrees(brw,
2101 src->stencil_mt, 0 /* level */, 0 /* layer */,
2102 src->stencil_mt->format,
2103 dst->stencil_mt, 0 /* level */, 0 /* layer */,
2104 dst->stencil_mt->format,
2105 0, 0,
2106 src->logical_width0, src->logical_height0,
2107 0, 0,
2108 dst->logical_width0, dst->logical_height0,
2109 GL_NEAREST, false, false /*mirror x, y*/);
2110 }
2111 }
2112
2113 void *
2114 intel_miptree_map_raw(struct brw_context *brw, struct intel_mipmap_tree *mt)
2115 {
2116 /* CPU accesses to color buffers don't understand fast color clears, so
2117 * resolve any pending fast color clears before we map.
2118 */
2119 intel_miptree_resolve_color(brw, mt);
2120
2121 drm_intel_bo *bo = mt->bo;
2122
2123 if (drm_intel_bo_references(brw->batch.bo, bo))
2124 intel_batchbuffer_flush(brw);
2125
2126 if (mt->tiling != I915_TILING_NONE)
2127 brw_bo_map_gtt(brw, bo, "miptree");
2128 else
2129 brw_bo_map(brw, bo, true, "miptree");
2130
2131 return bo->virtual;
2132 }
2133
2134 void
2135 intel_miptree_unmap_raw(struct intel_mipmap_tree *mt)
2136 {
2137 drm_intel_bo_unmap(mt->bo);
2138 }
2139
2140 static void
2141 intel_miptree_map_gtt(struct brw_context *brw,
2142 struct intel_mipmap_tree *mt,
2143 struct intel_miptree_map *map,
2144 unsigned int level, unsigned int slice)
2145 {
2146 unsigned int bw, bh;
2147 void *base;
2148 unsigned int image_x, image_y;
2149 intptr_t x = map->x;
2150 intptr_t y = map->y;
2151
2152 /* For compressed formats, the stride is the number of bytes per
2153 * row of blocks. intel_miptree_get_image_offset() already does
2154 * the divide.
2155 */
2156 _mesa_get_format_block_size(mt->format, &bw, &bh);
2157 assert(y % bh == 0);
2158 assert(x % bw == 0);
2159 y /= bh;
2160 x /= bw;
2161
2162 base = intel_miptree_map_raw(brw, mt) + mt->offset;
2163
2164 if (base == NULL)
2165 map->ptr = NULL;
2166 else {
2167 /* Note that in the case of cube maps, the caller must have passed the
2168 * slice number referencing the face.
2169 */
2170 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2171 x += image_x;
2172 y += image_y;
2173
2174 map->stride = mt->pitch;
2175 map->ptr = base + y * map->stride + x * mt->cpp;
2176 }
2177
2178 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2179 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2180 map->x, map->y, map->w, map->h,
2181 mt, _mesa_get_format_name(mt->format),
2182 x, y, map->ptr, map->stride);
2183 }
2184
2185 static void
2186 intel_miptree_unmap_gtt(struct intel_mipmap_tree *mt)
2187 {
2188 intel_miptree_unmap_raw(mt);
2189 }
2190
2191 static void
2192 intel_miptree_map_blit(struct brw_context *brw,
2193 struct intel_mipmap_tree *mt,
2194 struct intel_miptree_map *map,
2195 unsigned int level, unsigned int slice)
2196 {
2197 map->linear_mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
2198 /* first_level */ 0,
2199 /* last_level */ 0,
2200 map->w, map->h, 1,
2201 /* samples */ 0,
2202 MIPTREE_LAYOUT_TILING_NONE);
2203
2204 if (!map->linear_mt) {
2205 fprintf(stderr, "Failed to allocate blit temporary\n");
2206 goto fail;
2207 }
2208 map->stride = map->linear_mt->pitch;
2209
2210 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2211 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2212 * invalidate is set, since we'll be writing the whole rectangle from our
2213 * temporary buffer back out.
2214 */
2215 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2216 if (!intel_miptree_blit(brw,
2217 mt, level, slice,
2218 map->x, map->y, false,
2219 map->linear_mt, 0, 0,
2220 0, 0, false,
2221 map->w, map->h, GL_COPY)) {
2222 fprintf(stderr, "Failed to blit\n");
2223 goto fail;
2224 }
2225 }
2226
2227 map->ptr = intel_miptree_map_raw(brw, map->linear_mt);
2228
2229 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2230 map->x, map->y, map->w, map->h,
2231 mt, _mesa_get_format_name(mt->format),
2232 level, slice, map->ptr, map->stride);
2233
2234 return;
2235
2236 fail:
2237 intel_miptree_release(&map->linear_mt);
2238 map->ptr = NULL;
2239 map->stride = 0;
2240 }
2241
2242 static void
2243 intel_miptree_unmap_blit(struct brw_context *brw,
2244 struct intel_mipmap_tree *mt,
2245 struct intel_miptree_map *map,
2246 unsigned int level,
2247 unsigned int slice)
2248 {
2249 struct gl_context *ctx = &brw->ctx;
2250
2251 intel_miptree_unmap_raw(map->linear_mt);
2252
2253 if (map->mode & GL_MAP_WRITE_BIT) {
2254 bool ok = intel_miptree_blit(brw,
2255 map->linear_mt, 0, 0,
2256 0, 0, false,
2257 mt, level, slice,
2258 map->x, map->y, false,
2259 map->w, map->h, GL_COPY);
2260 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
2261 }
2262
2263 intel_miptree_release(&map->linear_mt);
2264 }
2265
2266 /**
2267 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2268 */
2269 #if defined(USE_SSE41)
2270 static void
2271 intel_miptree_map_movntdqa(struct brw_context *brw,
2272 struct intel_mipmap_tree *mt,
2273 struct intel_miptree_map *map,
2274 unsigned int level, unsigned int slice)
2275 {
2276 assert(map->mode & GL_MAP_READ_BIT);
2277 assert(!(map->mode & GL_MAP_WRITE_BIT));
2278
2279 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2280 map->x, map->y, map->w, map->h,
2281 mt, _mesa_get_format_name(mt->format),
2282 level, slice, map->ptr, map->stride);
2283
2284 /* Map the original image */
2285 uint32_t image_x;
2286 uint32_t image_y;
2287 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2288 image_x += map->x;
2289 image_y += map->y;
2290
2291 void *src = intel_miptree_map_raw(brw, mt);
2292 if (!src)
2293 return;
2294 src += image_y * mt->pitch;
2295 src += image_x * mt->cpp;
2296
2297 /* Due to the pixel offsets for the particular image being mapped, our
2298 * src pointer may not be 16-byte aligned. However, if the pitch is
2299 * divisible by 16, then the amount by which it's misaligned will remain
2300 * consistent from row to row.
2301 */
2302 assert((mt->pitch % 16) == 0);
2303 const int misalignment = ((uintptr_t) src) & 15;
2304
2305 /* Create an untiled temporary buffer for the mapping. */
2306 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
2307
2308 map->stride = ALIGN(misalignment + width_bytes, 16);
2309
2310 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
2311 /* Offset the destination so it has the same misalignment as src. */
2312 map->ptr = map->buffer + misalignment;
2313
2314 assert((((uintptr_t) map->ptr) & 15) == misalignment);
2315
2316 for (uint32_t y = 0; y < map->h; y++) {
2317 void *dst_ptr = map->ptr + y * map->stride;
2318 void *src_ptr = src + y * mt->pitch;
2319
2320 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
2321 }
2322
2323 intel_miptree_unmap_raw(mt);
2324 }
2325
2326 static void
2327 intel_miptree_unmap_movntdqa(struct brw_context *brw,
2328 struct intel_mipmap_tree *mt,
2329 struct intel_miptree_map *map,
2330 unsigned int level,
2331 unsigned int slice)
2332 {
2333 _mesa_align_free(map->buffer);
2334 map->buffer = NULL;
2335 map->ptr = NULL;
2336 }
2337 #endif
2338
2339 static void
2340 intel_miptree_map_s8(struct brw_context *brw,
2341 struct intel_mipmap_tree *mt,
2342 struct intel_miptree_map *map,
2343 unsigned int level, unsigned int slice)
2344 {
2345 map->stride = map->w;
2346 map->buffer = map->ptr = malloc(map->stride * map->h);
2347 if (!map->buffer)
2348 return;
2349
2350 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2351 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2352 * invalidate is set, since we'll be writing the whole rectangle from our
2353 * temporary buffer back out.
2354 */
2355 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2356 uint8_t *untiled_s8_map = map->ptr;
2357 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2358 unsigned int image_x, image_y;
2359
2360 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2361
2362 for (uint32_t y = 0; y < map->h; y++) {
2363 for (uint32_t x = 0; x < map->w; x++) {
2364 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2365 x + image_x + map->x,
2366 y + image_y + map->y,
2367 brw->has_swizzling);
2368 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
2369 }
2370 }
2371
2372 intel_miptree_unmap_raw(mt);
2373
2374 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
2375 map->x, map->y, map->w, map->h,
2376 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
2377 } else {
2378 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2379 map->x, map->y, map->w, map->h,
2380 mt, map->ptr, map->stride);
2381 }
2382 }
2383
2384 static void
2385 intel_miptree_unmap_s8(struct brw_context *brw,
2386 struct intel_mipmap_tree *mt,
2387 struct intel_miptree_map *map,
2388 unsigned int level,
2389 unsigned int slice)
2390 {
2391 if (map->mode & GL_MAP_WRITE_BIT) {
2392 unsigned int image_x, image_y;
2393 uint8_t *untiled_s8_map = map->ptr;
2394 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2395
2396 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2397
2398 for (uint32_t y = 0; y < map->h; y++) {
2399 for (uint32_t x = 0; x < map->w; x++) {
2400 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2401 x + map->x,
2402 y + map->y,
2403 brw->has_swizzling);
2404 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
2405 }
2406 }
2407
2408 intel_miptree_unmap_raw(mt);
2409 }
2410
2411 free(map->buffer);
2412 }
2413
2414 static void
2415 intel_miptree_map_etc(struct brw_context *brw,
2416 struct intel_mipmap_tree *mt,
2417 struct intel_miptree_map *map,
2418 unsigned int level,
2419 unsigned int slice)
2420 {
2421 assert(mt->etc_format != MESA_FORMAT_NONE);
2422 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
2423 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
2424 }
2425
2426 assert(map->mode & GL_MAP_WRITE_BIT);
2427 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
2428
2429 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
2430 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
2431 map->w, map->h, 1));
2432 map->ptr = map->buffer;
2433 }
2434
2435 static void
2436 intel_miptree_unmap_etc(struct brw_context *brw,
2437 struct intel_mipmap_tree *mt,
2438 struct intel_miptree_map *map,
2439 unsigned int level,
2440 unsigned int slice)
2441 {
2442 uint32_t image_x;
2443 uint32_t image_y;
2444 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2445
2446 image_x += map->x;
2447 image_y += map->y;
2448
2449 uint8_t *dst = intel_miptree_map_raw(brw, mt)
2450 + image_y * mt->pitch
2451 + image_x * mt->cpp;
2452
2453 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
2454 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
2455 map->ptr, map->stride,
2456 map->w, map->h);
2457 else
2458 _mesa_unpack_etc2_format(dst, mt->pitch,
2459 map->ptr, map->stride,
2460 map->w, map->h, mt->etc_format);
2461
2462 intel_miptree_unmap_raw(mt);
2463 free(map->buffer);
2464 }
2465
2466 /**
2467 * Mapping function for packed depth/stencil miptrees backed by real separate
2468 * miptrees for depth and stencil.
2469 *
2470 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2471 * separate from the depth buffer. Yet at the GL API level, we have to expose
2472 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2473 * be able to map that memory for texture storage and glReadPixels-type
2474 * operations. We give Mesa core that access by mallocing a temporary and
2475 * copying the data between the actual backing store and the temporary.
2476 */
2477 static void
2478 intel_miptree_map_depthstencil(struct brw_context *brw,
2479 struct intel_mipmap_tree *mt,
2480 struct intel_miptree_map *map,
2481 unsigned int level, unsigned int slice)
2482 {
2483 struct intel_mipmap_tree *z_mt = mt;
2484 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2485 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2486 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
2487
2488 map->stride = map->w * packed_bpp;
2489 map->buffer = map->ptr = malloc(map->stride * map->h);
2490 if (!map->buffer)
2491 return;
2492
2493 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2494 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2495 * invalidate is set, since we'll be writing the whole rectangle from our
2496 * temporary buffer back out.
2497 */
2498 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2499 uint32_t *packed_map = map->ptr;
2500 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2501 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2502 unsigned int s_image_x, s_image_y;
2503 unsigned int z_image_x, z_image_y;
2504
2505 intel_miptree_get_image_offset(s_mt, level, slice,
2506 &s_image_x, &s_image_y);
2507 intel_miptree_get_image_offset(z_mt, level, slice,
2508 &z_image_x, &z_image_y);
2509
2510 for (uint32_t y = 0; y < map->h; y++) {
2511 for (uint32_t x = 0; x < map->w; x++) {
2512 int map_x = map->x + x, map_y = map->y + y;
2513 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2514 map_x + s_image_x,
2515 map_y + s_image_y,
2516 brw->has_swizzling);
2517 ptrdiff_t z_offset = ((map_y + z_image_y) *
2518 (z_mt->pitch / 4) +
2519 (map_x + z_image_x));
2520 uint8_t s = s_map[s_offset];
2521 uint32_t z = z_map[z_offset];
2522
2523 if (map_z32f_x24s8) {
2524 packed_map[(y * map->w + x) * 2 + 0] = z;
2525 packed_map[(y * map->w + x) * 2 + 1] = s;
2526 } else {
2527 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
2528 }
2529 }
2530 }
2531
2532 intel_miptree_unmap_raw(s_mt);
2533 intel_miptree_unmap_raw(z_mt);
2534
2535 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
2536 __func__,
2537 map->x, map->y, map->w, map->h,
2538 z_mt, map->x + z_image_x, map->y + z_image_y,
2539 s_mt, map->x + s_image_x, map->y + s_image_y,
2540 map->ptr, map->stride);
2541 } else {
2542 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2543 map->x, map->y, map->w, map->h,
2544 mt, map->ptr, map->stride);
2545 }
2546 }
2547
2548 static void
2549 intel_miptree_unmap_depthstencil(struct brw_context *brw,
2550 struct intel_mipmap_tree *mt,
2551 struct intel_miptree_map *map,
2552 unsigned int level,
2553 unsigned int slice)
2554 {
2555 struct intel_mipmap_tree *z_mt = mt;
2556 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2557 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2558
2559 if (map->mode & GL_MAP_WRITE_BIT) {
2560 uint32_t *packed_map = map->ptr;
2561 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2562 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2563 unsigned int s_image_x, s_image_y;
2564 unsigned int z_image_x, z_image_y;
2565
2566 intel_miptree_get_image_offset(s_mt, level, slice,
2567 &s_image_x, &s_image_y);
2568 intel_miptree_get_image_offset(z_mt, level, slice,
2569 &z_image_x, &z_image_y);
2570
2571 for (uint32_t y = 0; y < map->h; y++) {
2572 for (uint32_t x = 0; x < map->w; x++) {
2573 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2574 x + s_image_x + map->x,
2575 y + s_image_y + map->y,
2576 brw->has_swizzling);
2577 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
2578 (z_mt->pitch / 4) +
2579 (x + z_image_x + map->x));
2580
2581 if (map_z32f_x24s8) {
2582 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
2583 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
2584 } else {
2585 uint32_t packed = packed_map[y * map->w + x];
2586 s_map[s_offset] = packed >> 24;
2587 z_map[z_offset] = packed;
2588 }
2589 }
2590 }
2591
2592 intel_miptree_unmap_raw(s_mt);
2593 intel_miptree_unmap_raw(z_mt);
2594
2595 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2596 __func__,
2597 map->x, map->y, map->w, map->h,
2598 z_mt, _mesa_get_format_name(z_mt->format),
2599 map->x + z_image_x, map->y + z_image_y,
2600 s_mt, map->x + s_image_x, map->y + s_image_y,
2601 map->ptr, map->stride);
2602 }
2603
2604 free(map->buffer);
2605 }
2606
2607 /**
2608 * Create and attach a map to the miptree at (level, slice). Return the
2609 * attached map.
2610 */
2611 static struct intel_miptree_map*
2612 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
2613 unsigned int level,
2614 unsigned int slice,
2615 unsigned int x,
2616 unsigned int y,
2617 unsigned int w,
2618 unsigned int h,
2619 GLbitfield mode)
2620 {
2621 struct intel_miptree_map *map = calloc(1, sizeof(*map));
2622
2623 if (!map)
2624 return NULL;
2625
2626 assert(mt->level[level].slice[slice].map == NULL);
2627 mt->level[level].slice[slice].map = map;
2628
2629 map->mode = mode;
2630 map->x = x;
2631 map->y = y;
2632 map->w = w;
2633 map->h = h;
2634
2635 return map;
2636 }
2637
2638 /**
2639 * Release the map at (level, slice).
2640 */
2641 static void
2642 intel_miptree_release_map(struct intel_mipmap_tree *mt,
2643 unsigned int level,
2644 unsigned int slice)
2645 {
2646 struct intel_miptree_map **map;
2647
2648 map = &mt->level[level].slice[slice].map;
2649 free(*map);
2650 *map = NULL;
2651 }
2652
2653 static bool
2654 can_blit_slice(struct intel_mipmap_tree *mt,
2655 unsigned int level, unsigned int slice)
2656 {
2657 uint32_t image_x;
2658 uint32_t image_y;
2659 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2660 if (image_x >= 32768 || image_y >= 32768)
2661 return false;
2662
2663 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2664 if (mt->pitch >= 32768)
2665 return false;
2666
2667 return true;
2668 }
2669
2670 static bool
2671 use_intel_mipree_map_blit(struct brw_context *brw,
2672 struct intel_mipmap_tree *mt,
2673 GLbitfield mode,
2674 unsigned int level,
2675 unsigned int slice)
2676 {
2677 if (brw->has_llc &&
2678 /* It's probably not worth swapping to the blit ring because of
2679 * all the overhead involved.
2680 */
2681 !(mode & GL_MAP_WRITE_BIT) &&
2682 !mt->compressed &&
2683 (mt->tiling == I915_TILING_X ||
2684 /* Prior to Sandybridge, the blitter can't handle Y tiling */
2685 (brw->gen >= 6 && mt->tiling == I915_TILING_Y)) &&
2686 can_blit_slice(mt, level, slice))
2687 return true;
2688
2689 if (mt->tiling != I915_TILING_NONE &&
2690 mt->bo->size >= brw->max_gtt_map_object_size) {
2691 assert(can_blit_slice(mt, level, slice));
2692 return true;
2693 }
2694
2695 return false;
2696 }
2697
2698 /**
2699 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
2700 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
2701 * arithmetic overflow.
2702 *
2703 * If you call this function and use \a out_stride, then you're doing pointer
2704 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
2705 * bugs. The caller must still take care to avoid 32-bit overflow errors in
2706 * all arithmetic expressions that contain buffer offsets and pixel sizes,
2707 * which usually have type uint32_t or GLuint.
2708 */
2709 void
2710 intel_miptree_map(struct brw_context *brw,
2711 struct intel_mipmap_tree *mt,
2712 unsigned int level,
2713 unsigned int slice,
2714 unsigned int x,
2715 unsigned int y,
2716 unsigned int w,
2717 unsigned int h,
2718 GLbitfield mode,
2719 void **out_ptr,
2720 ptrdiff_t *out_stride)
2721 {
2722 struct intel_miptree_map *map;
2723
2724 assert(mt->num_samples <= 1);
2725
2726 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
2727 if (!map){
2728 *out_ptr = NULL;
2729 *out_stride = 0;
2730 return;
2731 }
2732
2733 intel_miptree_slice_resolve_depth(brw, mt, level, slice);
2734 if (map->mode & GL_MAP_WRITE_BIT) {
2735 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
2736 }
2737
2738 if (mt->format == MESA_FORMAT_S_UINT8) {
2739 intel_miptree_map_s8(brw, mt, map, level, slice);
2740 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2741 !(mode & BRW_MAP_DIRECT_BIT)) {
2742 intel_miptree_map_etc(brw, mt, map, level, slice);
2743 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
2744 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
2745 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
2746 intel_miptree_map_blit(brw, mt, map, level, slice);
2747 #if defined(USE_SSE41)
2748 } else if (!(mode & GL_MAP_WRITE_BIT) &&
2749 !mt->compressed && cpu_has_sse4_1 &&
2750 (mt->pitch % 16 == 0)) {
2751 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
2752 #endif
2753 } else {
2754 intel_miptree_map_gtt(brw, mt, map, level, slice);
2755 }
2756
2757 *out_ptr = map->ptr;
2758 *out_stride = map->stride;
2759
2760 if (map->ptr == NULL)
2761 intel_miptree_release_map(mt, level, slice);
2762 }
2763
2764 void
2765 intel_miptree_unmap(struct brw_context *brw,
2766 struct intel_mipmap_tree *mt,
2767 unsigned int level,
2768 unsigned int slice)
2769 {
2770 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
2771
2772 assert(mt->num_samples <= 1);
2773
2774 if (!map)
2775 return;
2776
2777 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
2778 mt, _mesa_get_format_name(mt->format), level, slice);
2779
2780 if (mt->format == MESA_FORMAT_S_UINT8) {
2781 intel_miptree_unmap_s8(brw, mt, map, level, slice);
2782 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2783 !(map->mode & BRW_MAP_DIRECT_BIT)) {
2784 intel_miptree_unmap_etc(brw, mt, map, level, slice);
2785 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
2786 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
2787 } else if (map->linear_mt) {
2788 intel_miptree_unmap_blit(brw, mt, map, level, slice);
2789 #if defined(USE_SSE41)
2790 } else if (map->buffer && cpu_has_sse4_1) {
2791 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
2792 #endif
2793 } else {
2794 intel_miptree_unmap_gtt(mt);
2795 }
2796
2797 intel_miptree_release_map(mt, level, slice);
2798 }