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