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