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