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