vk: Add four unit tests for our lock-free data-structures
[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 switch (mt->num_samples) {
1460 case 0:
1461 case 1:
1462 break;
1463 case 2:
1464 case 4:
1465 z_width *= 2;
1466 z_height *= 2;
1467 break;
1468 case 8:
1469 z_width *= 4;
1470 z_height *= 2;
1471 break;
1472 default:
1473 unreachable("unsupported sample count");
1474 }
1475
1476 const unsigned vertical_align = 8; /* 'j' in the docs */
1477 const unsigned H0 = z_height;
1478 const unsigned h0 = ALIGN(H0, vertical_align);
1479 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1480 const unsigned Z0 = z_depth;
1481
1482 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1483 hz_width = ALIGN(z_width, 16);
1484
1485 if (mt->target == GL_TEXTURE_3D) {
1486 unsigned H_i = H0;
1487 unsigned Z_i = Z0;
1488 hz_height = 0;
1489 for (int level = mt->first_level; level <= mt->last_level; ++level) {
1490 unsigned h_i = ALIGN(H_i, vertical_align);
1491 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1492 hz_height += h_i * Z_i;
1493 H_i = minify(H_i, 1);
1494 Z_i = minify(Z_i, 1);
1495 }
1496 /* HZ_Height =
1497 * (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i)))
1498 */
1499 hz_height = DIV_ROUND_UP(hz_height, 2);
1500 } else {
1501 const unsigned hz_qpitch = h0 + h1 + (12 * vertical_align);
1502 if (mt->target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1503 mt->target == GL_TEXTURE_CUBE_MAP) {
1504 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth * 6/2) /8 ) * 8 */
1505 hz_height = DIV_ROUND_UP(hz_qpitch * Z0 * 6, 2 * 8) * 8;
1506 } else {
1507 /* HZ_Height (rows) = Ceiling ( ( Q_pitch * Z_depth/2) /8 ) * 8 */
1508 hz_height = DIV_ROUND_UP(hz_qpitch * Z0, 2 * 8) * 8;
1509 }
1510 }
1511
1512 unsigned long pitch;
1513 uint32_t tiling = I915_TILING_Y;
1514 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1515 hz_width, hz_height, 1,
1516 &tiling, &pitch,
1517 BO_ALLOC_FOR_RENDER);
1518 if (!buf->bo) {
1519 free(buf);
1520 return NULL;
1521 } else if (tiling != I915_TILING_Y) {
1522 drm_intel_bo_unreference(buf->bo);
1523 free(buf);
1524 return NULL;
1525 }
1526
1527 buf->pitch = pitch;
1528
1529 return buf;
1530 }
1531
1532
1533 /**
1534 * Helper for intel_miptree_alloc_hiz() that determines the required hiz
1535 * buffer dimensions and allocates a bo for the hiz buffer.
1536 */
1537 static struct intel_miptree_aux_buffer *
1538 intel_gen8_hiz_buf_create(struct brw_context *brw,
1539 struct intel_mipmap_tree *mt)
1540 {
1541 unsigned z_width = mt->logical_width0;
1542 unsigned z_height = mt->logical_height0;
1543 const unsigned z_depth = MAX2(mt->logical_depth0, 1);
1544 unsigned hz_width, hz_height;
1545 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1546
1547 if (!buf)
1548 return NULL;
1549
1550 /* Gen7 PRM Volume 2, Part 1, 11.5.3 "Hierarchical Depth Buffer" documents
1551 * adjustments required for Z_Height and Z_Width based on multisampling.
1552 */
1553 switch (mt->num_samples) {
1554 case 0:
1555 case 1:
1556 break;
1557 case 2:
1558 case 4:
1559 z_width *= 2;
1560 z_height *= 2;
1561 break;
1562 case 8:
1563 z_width *= 4;
1564 z_height *= 2;
1565 break;
1566 default:
1567 unreachable("unsupported sample count");
1568 }
1569
1570 const unsigned vertical_align = 8; /* 'j' in the docs */
1571 const unsigned H0 = z_height;
1572 const unsigned h0 = ALIGN(H0, vertical_align);
1573 const unsigned h1 = ALIGN(minify(H0, 1), vertical_align);
1574 const unsigned Z0 = z_depth;
1575
1576 /* HZ_Width (bytes) = ceiling(Z_Width / 16) * 16 */
1577 hz_width = ALIGN(z_width, 16);
1578
1579 unsigned H_i = H0;
1580 unsigned Z_i = Z0;
1581 unsigned sum_h_i = 0;
1582 unsigned hz_height_3d_sum = 0;
1583 for (int level = mt->first_level; level <= mt->last_level; ++level) {
1584 unsigned i = level - mt->first_level;
1585 unsigned h_i = ALIGN(H_i, vertical_align);
1586 /* sum(i=2 to m; h_i) */
1587 if (i >= 2) {
1588 sum_h_i += h_i;
1589 }
1590 /* sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1591 hz_height_3d_sum += h_i * Z_i;
1592 H_i = minify(H_i, 1);
1593 Z_i = minify(Z_i, 1);
1594 }
1595 /* HZ_QPitch = h0 + max(h1, sum(i=2 to m; h_i)) */
1596 buf->qpitch = h0 + MAX2(h1, sum_h_i);
1597
1598 if (mt->target == GL_TEXTURE_3D) {
1599 /* (1/2) * sum(i=0 to m; h_i * max(1, floor(Z_Depth/2**i))) */
1600 hz_height = DIV_ROUND_UP(hz_height_3d_sum, 2);
1601 } else {
1602 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * Z_Depth */
1603 hz_height = DIV_ROUND_UP(buf->qpitch, 2 * 8) * 8 * Z0;
1604 if (mt->target == GL_TEXTURE_CUBE_MAP_ARRAY ||
1605 mt->target == GL_TEXTURE_CUBE_MAP) {
1606 /* HZ_Height (rows) = ceiling( (HZ_QPitch/2)/8) *8 * 6 * Z_Depth
1607 *
1608 * We can can just take our hz_height calculation from above, and
1609 * multiply by 6 for the cube map and cube map array types.
1610 */
1611 hz_height *= 6;
1612 }
1613 }
1614
1615 unsigned long pitch;
1616 uint32_t tiling = I915_TILING_Y;
1617 buf->bo = drm_intel_bo_alloc_tiled(brw->bufmgr, "hiz",
1618 hz_width, hz_height, 1,
1619 &tiling, &pitch,
1620 BO_ALLOC_FOR_RENDER);
1621 if (!buf->bo) {
1622 free(buf);
1623 return NULL;
1624 } else if (tiling != I915_TILING_Y) {
1625 drm_intel_bo_unreference(buf->bo);
1626 free(buf);
1627 return NULL;
1628 }
1629
1630 buf->pitch = pitch;
1631
1632 return buf;
1633 }
1634
1635
1636 static struct intel_miptree_aux_buffer *
1637 intel_hiz_miptree_buf_create(struct brw_context *brw,
1638 struct intel_mipmap_tree *mt)
1639 {
1640 struct intel_miptree_aux_buffer *buf = calloc(sizeof(*buf), 1);
1641 uint32_t layout_flags = MIPTREE_LAYOUT_ACCELERATED_UPLOAD;
1642
1643 if (brw->gen == 6)
1644 layout_flags |= MIPTREE_LAYOUT_FORCE_ALL_SLICE_AT_LOD;
1645
1646 if (!buf)
1647 return NULL;
1648
1649 buf->mt = intel_miptree_create(brw,
1650 mt->target,
1651 mt->format,
1652 mt->first_level,
1653 mt->last_level,
1654 mt->logical_width0,
1655 mt->logical_height0,
1656 mt->logical_depth0,
1657 mt->num_samples,
1658 INTEL_MIPTREE_TILING_ANY,
1659 layout_flags);
1660 if (!buf->mt) {
1661 free(buf);
1662 return NULL;
1663 }
1664
1665 buf->bo = buf->mt->bo;
1666 buf->pitch = buf->mt->pitch;
1667 buf->qpitch = buf->mt->qpitch;
1668
1669 return buf;
1670 }
1671
1672 bool
1673 intel_miptree_wants_hiz_buffer(struct brw_context *brw,
1674 struct intel_mipmap_tree *mt)
1675 {
1676 if (!brw->has_hiz)
1677 return false;
1678
1679 if (mt->hiz_buf != NULL)
1680 return false;
1681
1682 if (mt->disable_aux_buffers)
1683 return false;
1684
1685 switch (mt->format) {
1686 case MESA_FORMAT_Z_FLOAT32:
1687 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
1688 case MESA_FORMAT_Z24_UNORM_X8_UINT:
1689 case MESA_FORMAT_Z24_UNORM_S8_UINT:
1690 case MESA_FORMAT_Z_UNORM16:
1691 return true;
1692 default:
1693 return false;
1694 }
1695 }
1696
1697 bool
1698 intel_miptree_alloc_hiz(struct brw_context *brw,
1699 struct intel_mipmap_tree *mt)
1700 {
1701 assert(mt->hiz_buf == NULL);
1702 assert(!mt->disable_aux_buffers);
1703
1704 if (brw->gen == 7) {
1705 mt->hiz_buf = intel_gen7_hiz_buf_create(brw, mt);
1706 } else if (brw->gen >= 8) {
1707 mt->hiz_buf = intel_gen8_hiz_buf_create(brw, mt);
1708 } else {
1709 mt->hiz_buf = intel_hiz_miptree_buf_create(brw, mt);
1710 }
1711
1712 if (!mt->hiz_buf)
1713 return false;
1714
1715 /* Mark that all slices need a HiZ resolve. */
1716 for (int level = mt->first_level; level <= mt->last_level; ++level) {
1717 if (!intel_miptree_level_enable_hiz(brw, mt, level))
1718 continue;
1719
1720 for (int layer = 0; layer < mt->level[level].depth; ++layer) {
1721 struct intel_resolve_map *m = malloc(sizeof(struct intel_resolve_map));
1722 exec_node_init(&m->link);
1723 m->level = level;
1724 m->layer = layer;
1725 m->need = GEN6_HIZ_OP_HIZ_RESOLVE;
1726
1727 exec_list_push_tail(&mt->hiz_map, &m->link);
1728 }
1729 }
1730
1731 return true;
1732 }
1733
1734 /**
1735 * Does the miptree slice have hiz enabled?
1736 */
1737 bool
1738 intel_miptree_level_has_hiz(struct intel_mipmap_tree *mt, uint32_t level)
1739 {
1740 intel_miptree_check_level_layer(mt, level, 0);
1741 return mt->level[level].has_hiz;
1742 }
1743
1744 void
1745 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
1746 uint32_t level,
1747 uint32_t layer)
1748 {
1749 if (!intel_miptree_level_has_hiz(mt, level))
1750 return;
1751
1752 intel_resolve_map_set(&mt->hiz_map,
1753 level, layer, GEN6_HIZ_OP_HIZ_RESOLVE);
1754 }
1755
1756
1757 void
1758 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
1759 uint32_t level,
1760 uint32_t layer)
1761 {
1762 if (!intel_miptree_level_has_hiz(mt, level))
1763 return;
1764
1765 intel_resolve_map_set(&mt->hiz_map,
1766 level, layer, GEN6_HIZ_OP_DEPTH_RESOLVE);
1767 }
1768
1769 void
1770 intel_miptree_set_all_slices_need_depth_resolve(struct intel_mipmap_tree *mt,
1771 uint32_t level)
1772 {
1773 uint32_t layer;
1774 uint32_t end_layer = mt->level[level].depth;
1775
1776 for (layer = 0; layer < end_layer; layer++) {
1777 intel_miptree_slice_set_needs_depth_resolve(mt, level, layer);
1778 }
1779 }
1780
1781 static bool
1782 intel_miptree_slice_resolve(struct brw_context *brw,
1783 struct intel_mipmap_tree *mt,
1784 uint32_t level,
1785 uint32_t layer,
1786 enum gen6_hiz_op need)
1787 {
1788 intel_miptree_check_level_layer(mt, level, layer);
1789
1790 struct intel_resolve_map *item =
1791 intel_resolve_map_get(&mt->hiz_map, level, layer);
1792
1793 if (!item || item->need != need)
1794 return false;
1795
1796 intel_hiz_exec(brw, mt, level, layer, need);
1797 intel_resolve_map_remove(item);
1798 return true;
1799 }
1800
1801 bool
1802 intel_miptree_slice_resolve_hiz(struct brw_context *brw,
1803 struct intel_mipmap_tree *mt,
1804 uint32_t level,
1805 uint32_t layer)
1806 {
1807 return intel_miptree_slice_resolve(brw, mt, level, layer,
1808 GEN6_HIZ_OP_HIZ_RESOLVE);
1809 }
1810
1811 bool
1812 intel_miptree_slice_resolve_depth(struct brw_context *brw,
1813 struct intel_mipmap_tree *mt,
1814 uint32_t level,
1815 uint32_t layer)
1816 {
1817 return intel_miptree_slice_resolve(brw, mt, level, layer,
1818 GEN6_HIZ_OP_DEPTH_RESOLVE);
1819 }
1820
1821 static bool
1822 intel_miptree_all_slices_resolve(struct brw_context *brw,
1823 struct intel_mipmap_tree *mt,
1824 enum gen6_hiz_op need)
1825 {
1826 bool did_resolve = false;
1827
1828 foreach_list_typed_safe(struct intel_resolve_map, map, link, &mt->hiz_map) {
1829 if (map->need != need)
1830 continue;
1831
1832 intel_hiz_exec(brw, mt, map->level, map->layer, need);
1833 intel_resolve_map_remove(map);
1834 did_resolve = true;
1835 }
1836
1837 return did_resolve;
1838 }
1839
1840 bool
1841 intel_miptree_all_slices_resolve_hiz(struct brw_context *brw,
1842 struct intel_mipmap_tree *mt)
1843 {
1844 return intel_miptree_all_slices_resolve(brw, mt,
1845 GEN6_HIZ_OP_HIZ_RESOLVE);
1846 }
1847
1848 bool
1849 intel_miptree_all_slices_resolve_depth(struct brw_context *brw,
1850 struct intel_mipmap_tree *mt)
1851 {
1852 return intel_miptree_all_slices_resolve(brw, mt,
1853 GEN6_HIZ_OP_DEPTH_RESOLVE);
1854 }
1855
1856
1857 void
1858 intel_miptree_resolve_color(struct brw_context *brw,
1859 struct intel_mipmap_tree *mt)
1860 {
1861 switch (mt->fast_clear_state) {
1862 case INTEL_FAST_CLEAR_STATE_NO_MCS:
1863 case INTEL_FAST_CLEAR_STATE_RESOLVED:
1864 /* No resolve needed */
1865 break;
1866 case INTEL_FAST_CLEAR_STATE_UNRESOLVED:
1867 case INTEL_FAST_CLEAR_STATE_CLEAR:
1868 /* Fast color clear resolves only make sense for non-MSAA buffers. */
1869 if (mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE)
1870 brw_meta_resolve_color(brw, mt);
1871 break;
1872 }
1873 }
1874
1875
1876 /**
1877 * Make it possible to share the BO backing the given miptree with another
1878 * process or another miptree.
1879 *
1880 * Fast color clears are unsafe with shared buffers, so we need to resolve and
1881 * then discard the MCS buffer, if present. We also set the fast_clear_state
1882 * to INTEL_FAST_CLEAR_STATE_NO_MCS to ensure that no MCS buffer gets
1883 * allocated in the future.
1884 */
1885 void
1886 intel_miptree_make_shareable(struct brw_context *brw,
1887 struct intel_mipmap_tree *mt)
1888 {
1889 /* MCS buffers are also used for multisample buffers, but we can't resolve
1890 * away a multisample MCS buffer because it's an integral part of how the
1891 * pixel data is stored. Fortunately this code path should never be
1892 * reached for multisample buffers.
1893 */
1894 assert(mt->msaa_layout == INTEL_MSAA_LAYOUT_NONE);
1895
1896 if (mt->mcs_mt) {
1897 intel_miptree_resolve_color(brw, mt);
1898 intel_miptree_release(&mt->mcs_mt);
1899 mt->fast_clear_state = INTEL_FAST_CLEAR_STATE_NO_MCS;
1900 }
1901 }
1902
1903
1904 /**
1905 * \brief Get pointer offset into stencil buffer.
1906 *
1907 * The stencil buffer is W tiled. Since the GTT is incapable of W fencing, we
1908 * must decode the tile's layout in software.
1909 *
1910 * See
1911 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.2.1 W-Major Tile
1912 * Format.
1913 * - PRM, 2011 Sandy Bridge, Volume 1, Part 2, Section 4.5.3 Tiling Algorithm
1914 *
1915 * Even though the returned offset is always positive, the return type is
1916 * signed due to
1917 * commit e8b1c6d6f55f5be3bef25084fdd8b6127517e137
1918 * mesa: Fix return type of _mesa_get_format_bytes() (#37351)
1919 */
1920 static intptr_t
1921 intel_offset_S8(uint32_t stride, uint32_t x, uint32_t y, bool swizzled)
1922 {
1923 uint32_t tile_size = 4096;
1924 uint32_t tile_width = 64;
1925 uint32_t tile_height = 64;
1926 uint32_t row_size = 64 * stride;
1927
1928 uint32_t tile_x = x / tile_width;
1929 uint32_t tile_y = y / tile_height;
1930
1931 /* The byte's address relative to the tile's base addres. */
1932 uint32_t byte_x = x % tile_width;
1933 uint32_t byte_y = y % tile_height;
1934
1935 uintptr_t u = tile_y * row_size
1936 + tile_x * tile_size
1937 + 512 * (byte_x / 8)
1938 + 64 * (byte_y / 8)
1939 + 32 * ((byte_y / 4) % 2)
1940 + 16 * ((byte_x / 4) % 2)
1941 + 8 * ((byte_y / 2) % 2)
1942 + 4 * ((byte_x / 2) % 2)
1943 + 2 * (byte_y % 2)
1944 + 1 * (byte_x % 2);
1945
1946 if (swizzled) {
1947 /* adjust for bit6 swizzling */
1948 if (((byte_x / 8) % 2) == 1) {
1949 if (((byte_y / 8) % 2) == 0) {
1950 u += 64;
1951 } else {
1952 u -= 64;
1953 }
1954 }
1955 }
1956
1957 return u;
1958 }
1959
1960 void
1961 intel_miptree_updownsample(struct brw_context *brw,
1962 struct intel_mipmap_tree *src,
1963 struct intel_mipmap_tree *dst)
1964 {
1965 if (brw->gen < 8) {
1966 brw_blorp_blit_miptrees(brw,
1967 src, 0 /* level */, 0 /* layer */, src->format,
1968 dst, 0 /* level */, 0 /* layer */, dst->format,
1969 0, 0,
1970 src->logical_width0, src->logical_height0,
1971 0, 0,
1972 dst->logical_width0, dst->logical_height0,
1973 GL_NEAREST, false, false /*mirror x, y*/);
1974 } else if (src->format == MESA_FORMAT_S_UINT8) {
1975 brw_meta_stencil_updownsample(brw, src, dst);
1976 } else {
1977 brw_meta_updownsample(brw, src, dst);
1978 }
1979
1980 if (src->stencil_mt) {
1981 if (brw->gen >= 8) {
1982 brw_meta_stencil_updownsample(brw, src->stencil_mt, dst);
1983 return;
1984 }
1985
1986 brw_blorp_blit_miptrees(brw,
1987 src->stencil_mt, 0 /* level */, 0 /* layer */,
1988 src->stencil_mt->format,
1989 dst->stencil_mt, 0 /* level */, 0 /* layer */,
1990 dst->stencil_mt->format,
1991 0, 0,
1992 src->logical_width0, src->logical_height0,
1993 0, 0,
1994 dst->logical_width0, dst->logical_height0,
1995 GL_NEAREST, false, false /*mirror x, y*/);
1996 }
1997 }
1998
1999 void *
2000 intel_miptree_map_raw(struct brw_context *brw, struct intel_mipmap_tree *mt)
2001 {
2002 /* CPU accesses to color buffers don't understand fast color clears, so
2003 * resolve any pending fast color clears before we map.
2004 */
2005 intel_miptree_resolve_color(brw, mt);
2006
2007 drm_intel_bo *bo = mt->bo;
2008
2009 if (drm_intel_bo_references(brw->batch.bo, bo))
2010 intel_batchbuffer_flush(brw);
2011
2012 if (mt->tiling != I915_TILING_NONE)
2013 brw_bo_map_gtt(brw, bo, "miptree");
2014 else
2015 brw_bo_map(brw, bo, true, "miptree");
2016
2017 return bo->virtual;
2018 }
2019
2020 void
2021 intel_miptree_unmap_raw(struct brw_context *brw,
2022 struct intel_mipmap_tree *mt)
2023 {
2024 drm_intel_bo_unmap(mt->bo);
2025 }
2026
2027 static void
2028 intel_miptree_map_gtt(struct brw_context *brw,
2029 struct intel_mipmap_tree *mt,
2030 struct intel_miptree_map *map,
2031 unsigned int level, unsigned int slice)
2032 {
2033 unsigned int bw, bh;
2034 void *base;
2035 unsigned int image_x, image_y;
2036 intptr_t x = map->x;
2037 intptr_t y = map->y;
2038
2039 /* For compressed formats, the stride is the number of bytes per
2040 * row of blocks. intel_miptree_get_image_offset() already does
2041 * the divide.
2042 */
2043 _mesa_get_format_block_size(mt->format, &bw, &bh);
2044 assert(y % bh == 0);
2045 y /= bh;
2046
2047 base = intel_miptree_map_raw(brw, mt) + mt->offset;
2048
2049 if (base == NULL)
2050 map->ptr = NULL;
2051 else {
2052 /* Note that in the case of cube maps, the caller must have passed the
2053 * slice number referencing the face.
2054 */
2055 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2056 x += image_x;
2057 y += image_y;
2058
2059 map->stride = mt->pitch;
2060 map->ptr = base + y * map->stride + x * mt->cpp;
2061 }
2062
2063 DBG("%s: %d,%d %dx%d from mt %p (%s) "
2064 "%"PRIiPTR",%"PRIiPTR" = %p/%d\n", __func__,
2065 map->x, map->y, map->w, map->h,
2066 mt, _mesa_get_format_name(mt->format),
2067 x, y, map->ptr, map->stride);
2068 }
2069
2070 static void
2071 intel_miptree_unmap_gtt(struct brw_context *brw,
2072 struct intel_mipmap_tree *mt,
2073 struct intel_miptree_map *map,
2074 unsigned int level,
2075 unsigned int slice)
2076 {
2077 intel_miptree_unmap_raw(brw, mt);
2078 }
2079
2080 static void
2081 intel_miptree_map_blit(struct brw_context *brw,
2082 struct intel_mipmap_tree *mt,
2083 struct intel_miptree_map *map,
2084 unsigned int level, unsigned int slice)
2085 {
2086 map->mt = intel_miptree_create(brw, GL_TEXTURE_2D, mt->format,
2087 0, 0,
2088 map->w, map->h, 1,
2089 0, INTEL_MIPTREE_TILING_NONE, 0);
2090
2091 if (!map->mt) {
2092 fprintf(stderr, "Failed to allocate blit temporary\n");
2093 goto fail;
2094 }
2095 map->stride = map->mt->pitch;
2096
2097 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2098 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2099 * invalidate is set, since we'll be writing the whole rectangle from our
2100 * temporary buffer back out.
2101 */
2102 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2103 if (!intel_miptree_blit(brw,
2104 mt, level, slice,
2105 map->x, map->y, false,
2106 map->mt, 0, 0,
2107 0, 0, false,
2108 map->w, map->h, GL_COPY)) {
2109 fprintf(stderr, "Failed to blit\n");
2110 goto fail;
2111 }
2112 }
2113
2114 map->ptr = intel_miptree_map_raw(brw, map->mt);
2115
2116 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2117 map->x, map->y, map->w, map->h,
2118 mt, _mesa_get_format_name(mt->format),
2119 level, slice, map->ptr, map->stride);
2120
2121 return;
2122
2123 fail:
2124 intel_miptree_release(&map->mt);
2125 map->ptr = NULL;
2126 map->stride = 0;
2127 }
2128
2129 static void
2130 intel_miptree_unmap_blit(struct brw_context *brw,
2131 struct intel_mipmap_tree *mt,
2132 struct intel_miptree_map *map,
2133 unsigned int level,
2134 unsigned int slice)
2135 {
2136 struct gl_context *ctx = &brw->ctx;
2137
2138 intel_miptree_unmap_raw(brw, map->mt);
2139
2140 if (map->mode & GL_MAP_WRITE_BIT) {
2141 bool ok = intel_miptree_blit(brw,
2142 map->mt, 0, 0,
2143 0, 0, false,
2144 mt, level, slice,
2145 map->x, map->y, false,
2146 map->w, map->h, GL_COPY);
2147 WARN_ONCE(!ok, "Failed to blit from linear temporary mapping");
2148 }
2149
2150 intel_miptree_release(&map->mt);
2151 }
2152
2153 /**
2154 * "Map" a buffer by copying it to an untiled temporary using MOVNTDQA.
2155 */
2156 #if defined(USE_SSE41)
2157 static void
2158 intel_miptree_map_movntdqa(struct brw_context *brw,
2159 struct intel_mipmap_tree *mt,
2160 struct intel_miptree_map *map,
2161 unsigned int level, unsigned int slice)
2162 {
2163 assert(map->mode & GL_MAP_READ_BIT);
2164 assert(!(map->mode & GL_MAP_WRITE_BIT));
2165
2166 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __func__,
2167 map->x, map->y, map->w, map->h,
2168 mt, _mesa_get_format_name(mt->format),
2169 level, slice, map->ptr, map->stride);
2170
2171 /* Map the original image */
2172 uint32_t image_x;
2173 uint32_t image_y;
2174 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2175 image_x += map->x;
2176 image_y += map->y;
2177
2178 void *src = intel_miptree_map_raw(brw, mt);
2179 if (!src)
2180 return;
2181 src += image_y * mt->pitch;
2182 src += image_x * mt->cpp;
2183
2184 /* Due to the pixel offsets for the particular image being mapped, our
2185 * src pointer may not be 16-byte aligned. However, if the pitch is
2186 * divisible by 16, then the amount by which it's misaligned will remain
2187 * consistent from row to row.
2188 */
2189 assert((mt->pitch % 16) == 0);
2190 const int misalignment = ((uintptr_t) src) & 15;
2191
2192 /* Create an untiled temporary buffer for the mapping. */
2193 const unsigned width_bytes = _mesa_format_row_stride(mt->format, map->w);
2194
2195 map->stride = ALIGN(misalignment + width_bytes, 16);
2196
2197 map->buffer = _mesa_align_malloc(map->stride * map->h, 16);
2198 /* Offset the destination so it has the same misalignment as src. */
2199 map->ptr = map->buffer + misalignment;
2200
2201 assert((((uintptr_t) map->ptr) & 15) == misalignment);
2202
2203 for (uint32_t y = 0; y < map->h; y++) {
2204 void *dst_ptr = map->ptr + y * map->stride;
2205 void *src_ptr = src + y * mt->pitch;
2206
2207 _mesa_streaming_load_memcpy(dst_ptr, src_ptr, width_bytes);
2208 }
2209
2210 intel_miptree_unmap_raw(brw, mt);
2211 }
2212
2213 static void
2214 intel_miptree_unmap_movntdqa(struct brw_context *brw,
2215 struct intel_mipmap_tree *mt,
2216 struct intel_miptree_map *map,
2217 unsigned int level,
2218 unsigned int slice)
2219 {
2220 _mesa_align_free(map->buffer);
2221 map->buffer = NULL;
2222 map->ptr = NULL;
2223 }
2224 #endif
2225
2226 static void
2227 intel_miptree_map_s8(struct brw_context *brw,
2228 struct intel_mipmap_tree *mt,
2229 struct intel_miptree_map *map,
2230 unsigned int level, unsigned int slice)
2231 {
2232 map->stride = map->w;
2233 map->buffer = map->ptr = malloc(map->stride * map->h);
2234 if (!map->buffer)
2235 return;
2236
2237 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2238 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2239 * invalidate is set, since we'll be writing the whole rectangle from our
2240 * temporary buffer back out.
2241 */
2242 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2243 uint8_t *untiled_s8_map = map->ptr;
2244 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2245 unsigned int image_x, image_y;
2246
2247 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2248
2249 for (uint32_t y = 0; y < map->h; y++) {
2250 for (uint32_t x = 0; x < map->w; x++) {
2251 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2252 x + image_x + map->x,
2253 y + image_y + map->y,
2254 brw->has_swizzling);
2255 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
2256 }
2257 }
2258
2259 intel_miptree_unmap_raw(brw, mt);
2260
2261 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __func__,
2262 map->x, map->y, map->w, map->h,
2263 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
2264 } else {
2265 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2266 map->x, map->y, map->w, map->h,
2267 mt, map->ptr, map->stride);
2268 }
2269 }
2270
2271 static void
2272 intel_miptree_unmap_s8(struct brw_context *brw,
2273 struct intel_mipmap_tree *mt,
2274 struct intel_miptree_map *map,
2275 unsigned int level,
2276 unsigned int slice)
2277 {
2278 if (map->mode & GL_MAP_WRITE_BIT) {
2279 unsigned int image_x, image_y;
2280 uint8_t *untiled_s8_map = map->ptr;
2281 uint8_t *tiled_s8_map = intel_miptree_map_raw(brw, mt);
2282
2283 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2284
2285 for (uint32_t y = 0; y < map->h; y++) {
2286 for (uint32_t x = 0; x < map->w; x++) {
2287 ptrdiff_t offset = intel_offset_S8(mt->pitch,
2288 x + map->x,
2289 y + map->y,
2290 brw->has_swizzling);
2291 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
2292 }
2293 }
2294
2295 intel_miptree_unmap_raw(brw, mt);
2296 }
2297
2298 free(map->buffer);
2299 }
2300
2301 static void
2302 intel_miptree_map_etc(struct brw_context *brw,
2303 struct intel_mipmap_tree *mt,
2304 struct intel_miptree_map *map,
2305 unsigned int level,
2306 unsigned int slice)
2307 {
2308 assert(mt->etc_format != MESA_FORMAT_NONE);
2309 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8) {
2310 assert(mt->format == MESA_FORMAT_R8G8B8X8_UNORM);
2311 }
2312
2313 assert(map->mode & GL_MAP_WRITE_BIT);
2314 assert(map->mode & GL_MAP_INVALIDATE_RANGE_BIT);
2315
2316 map->stride = _mesa_format_row_stride(mt->etc_format, map->w);
2317 map->buffer = malloc(_mesa_format_image_size(mt->etc_format,
2318 map->w, map->h, 1));
2319 map->ptr = map->buffer;
2320 }
2321
2322 static void
2323 intel_miptree_unmap_etc(struct brw_context *brw,
2324 struct intel_mipmap_tree *mt,
2325 struct intel_miptree_map *map,
2326 unsigned int level,
2327 unsigned int slice)
2328 {
2329 uint32_t image_x;
2330 uint32_t image_y;
2331 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2332
2333 image_x += map->x;
2334 image_y += map->y;
2335
2336 uint8_t *dst = intel_miptree_map_raw(brw, mt)
2337 + image_y * mt->pitch
2338 + image_x * mt->cpp;
2339
2340 if (mt->etc_format == MESA_FORMAT_ETC1_RGB8)
2341 _mesa_etc1_unpack_rgba8888(dst, mt->pitch,
2342 map->ptr, map->stride,
2343 map->w, map->h);
2344 else
2345 _mesa_unpack_etc2_format(dst, mt->pitch,
2346 map->ptr, map->stride,
2347 map->w, map->h, mt->etc_format);
2348
2349 intel_miptree_unmap_raw(brw, mt);
2350 free(map->buffer);
2351 }
2352
2353 /**
2354 * Mapping function for packed depth/stencil miptrees backed by real separate
2355 * miptrees for depth and stencil.
2356 *
2357 * On gen7, and to support HiZ pre-gen7, we have to have the stencil buffer
2358 * separate from the depth buffer. Yet at the GL API level, we have to expose
2359 * packed depth/stencil textures and FBO attachments, and Mesa core expects to
2360 * be able to map that memory for texture storage and glReadPixels-type
2361 * operations. We give Mesa core that access by mallocing a temporary and
2362 * copying the data between the actual backing store and the temporary.
2363 */
2364 static void
2365 intel_miptree_map_depthstencil(struct brw_context *brw,
2366 struct intel_mipmap_tree *mt,
2367 struct intel_miptree_map *map,
2368 unsigned int level, unsigned int slice)
2369 {
2370 struct intel_mipmap_tree *z_mt = mt;
2371 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2372 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2373 int packed_bpp = map_z32f_x24s8 ? 8 : 4;
2374
2375 map->stride = map->w * packed_bpp;
2376 map->buffer = map->ptr = malloc(map->stride * map->h);
2377 if (!map->buffer)
2378 return;
2379
2380 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
2381 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
2382 * invalidate is set, since we'll be writing the whole rectangle from our
2383 * temporary buffer back out.
2384 */
2385 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
2386 uint32_t *packed_map = map->ptr;
2387 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2388 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2389 unsigned int s_image_x, s_image_y;
2390 unsigned int z_image_x, z_image_y;
2391
2392 intel_miptree_get_image_offset(s_mt, level, slice,
2393 &s_image_x, &s_image_y);
2394 intel_miptree_get_image_offset(z_mt, level, slice,
2395 &z_image_x, &z_image_y);
2396
2397 for (uint32_t y = 0; y < map->h; y++) {
2398 for (uint32_t x = 0; x < map->w; x++) {
2399 int map_x = map->x + x, map_y = map->y + y;
2400 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2401 map_x + s_image_x,
2402 map_y + s_image_y,
2403 brw->has_swizzling);
2404 ptrdiff_t z_offset = ((map_y + z_image_y) *
2405 (z_mt->pitch / 4) +
2406 (map_x + z_image_x));
2407 uint8_t s = s_map[s_offset];
2408 uint32_t z = z_map[z_offset];
2409
2410 if (map_z32f_x24s8) {
2411 packed_map[(y * map->w + x) * 2 + 0] = z;
2412 packed_map[(y * map->w + x) * 2 + 1] = s;
2413 } else {
2414 packed_map[y * map->w + x] = (s << 24) | (z & 0x00ffffff);
2415 }
2416 }
2417 }
2418
2419 intel_miptree_unmap_raw(brw, s_mt);
2420 intel_miptree_unmap_raw(brw, z_mt);
2421
2422 DBG("%s: %d,%d %dx%d from z mt %p %d,%d, s mt %p %d,%d = %p/%d\n",
2423 __func__,
2424 map->x, map->y, map->w, map->h,
2425 z_mt, map->x + z_image_x, map->y + z_image_y,
2426 s_mt, map->x + s_image_x, map->y + s_image_y,
2427 map->ptr, map->stride);
2428 } else {
2429 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __func__,
2430 map->x, map->y, map->w, map->h,
2431 mt, map->ptr, map->stride);
2432 }
2433 }
2434
2435 static void
2436 intel_miptree_unmap_depthstencil(struct brw_context *brw,
2437 struct intel_mipmap_tree *mt,
2438 struct intel_miptree_map *map,
2439 unsigned int level,
2440 unsigned int slice)
2441 {
2442 struct intel_mipmap_tree *z_mt = mt;
2443 struct intel_mipmap_tree *s_mt = mt->stencil_mt;
2444 bool map_z32f_x24s8 = mt->format == MESA_FORMAT_Z_FLOAT32;
2445
2446 if (map->mode & GL_MAP_WRITE_BIT) {
2447 uint32_t *packed_map = map->ptr;
2448 uint8_t *s_map = intel_miptree_map_raw(brw, s_mt);
2449 uint32_t *z_map = intel_miptree_map_raw(brw, z_mt);
2450 unsigned int s_image_x, s_image_y;
2451 unsigned int z_image_x, z_image_y;
2452
2453 intel_miptree_get_image_offset(s_mt, level, slice,
2454 &s_image_x, &s_image_y);
2455 intel_miptree_get_image_offset(z_mt, level, slice,
2456 &z_image_x, &z_image_y);
2457
2458 for (uint32_t y = 0; y < map->h; y++) {
2459 for (uint32_t x = 0; x < map->w; x++) {
2460 ptrdiff_t s_offset = intel_offset_S8(s_mt->pitch,
2461 x + s_image_x + map->x,
2462 y + s_image_y + map->y,
2463 brw->has_swizzling);
2464 ptrdiff_t z_offset = ((y + z_image_y + map->y) *
2465 (z_mt->pitch / 4) +
2466 (x + z_image_x + map->x));
2467
2468 if (map_z32f_x24s8) {
2469 z_map[z_offset] = packed_map[(y * map->w + x) * 2 + 0];
2470 s_map[s_offset] = packed_map[(y * map->w + x) * 2 + 1];
2471 } else {
2472 uint32_t packed = packed_map[y * map->w + x];
2473 s_map[s_offset] = packed >> 24;
2474 z_map[z_offset] = packed;
2475 }
2476 }
2477 }
2478
2479 intel_miptree_unmap_raw(brw, s_mt);
2480 intel_miptree_unmap_raw(brw, z_mt);
2481
2482 DBG("%s: %d,%d %dx%d from z mt %p (%s) %d,%d, s mt %p %d,%d = %p/%d\n",
2483 __func__,
2484 map->x, map->y, map->w, map->h,
2485 z_mt, _mesa_get_format_name(z_mt->format),
2486 map->x + z_image_x, map->y + z_image_y,
2487 s_mt, map->x + s_image_x, map->y + s_image_y,
2488 map->ptr, map->stride);
2489 }
2490
2491 free(map->buffer);
2492 }
2493
2494 /**
2495 * Create and attach a map to the miptree at (level, slice). Return the
2496 * attached map.
2497 */
2498 static struct intel_miptree_map*
2499 intel_miptree_attach_map(struct intel_mipmap_tree *mt,
2500 unsigned int level,
2501 unsigned int slice,
2502 unsigned int x,
2503 unsigned int y,
2504 unsigned int w,
2505 unsigned int h,
2506 GLbitfield mode)
2507 {
2508 struct intel_miptree_map *map = calloc(1, sizeof(*map));
2509
2510 if (!map)
2511 return NULL;
2512
2513 assert(mt->level[level].slice[slice].map == NULL);
2514 mt->level[level].slice[slice].map = map;
2515
2516 map->mode = mode;
2517 map->x = x;
2518 map->y = y;
2519 map->w = w;
2520 map->h = h;
2521
2522 return map;
2523 }
2524
2525 /**
2526 * Release the map at (level, slice).
2527 */
2528 static void
2529 intel_miptree_release_map(struct intel_mipmap_tree *mt,
2530 unsigned int level,
2531 unsigned int slice)
2532 {
2533 struct intel_miptree_map **map;
2534
2535 map = &mt->level[level].slice[slice].map;
2536 free(*map);
2537 *map = NULL;
2538 }
2539
2540 static bool
2541 can_blit_slice(struct intel_mipmap_tree *mt,
2542 unsigned int level, unsigned int slice)
2543 {
2544 uint32_t image_x;
2545 uint32_t image_y;
2546 intel_miptree_get_image_offset(mt, level, slice, &image_x, &image_y);
2547 if (image_x >= 32768 || image_y >= 32768)
2548 return false;
2549
2550 /* See intel_miptree_blit() for details on the 32k pitch limit. */
2551 if (mt->pitch >= 32768)
2552 return false;
2553
2554 return true;
2555 }
2556
2557 static bool
2558 use_intel_mipree_map_blit(struct brw_context *brw,
2559 struct intel_mipmap_tree *mt,
2560 GLbitfield mode,
2561 unsigned int level,
2562 unsigned int slice)
2563 {
2564 if (brw->has_llc &&
2565 /* It's probably not worth swapping to the blit ring because of
2566 * all the overhead involved.
2567 */
2568 !(mode & GL_MAP_WRITE_BIT) &&
2569 !mt->compressed &&
2570 (mt->tiling == I915_TILING_X ||
2571 /* Prior to Sandybridge, the blitter can't handle Y tiling */
2572 (brw->gen >= 6 && mt->tiling == I915_TILING_Y)) &&
2573 can_blit_slice(mt, level, slice))
2574 return true;
2575
2576 if (mt->tiling != I915_TILING_NONE &&
2577 mt->bo->size >= brw->max_gtt_map_object_size) {
2578 assert(can_blit_slice(mt, level, slice));
2579 return true;
2580 }
2581
2582 return false;
2583 }
2584
2585 /**
2586 * Parameter \a out_stride has type ptrdiff_t not because the buffer stride may
2587 * exceed 32 bits but to diminish the likelihood subtle bugs in pointer
2588 * arithmetic overflow.
2589 *
2590 * If you call this function and use \a out_stride, then you're doing pointer
2591 * arithmetic on \a out_ptr. The type of \a out_stride doesn't prevent all
2592 * bugs. The caller must still take care to avoid 32-bit overflow errors in
2593 * all arithmetic expressions that contain buffer offsets and pixel sizes,
2594 * which usually have type uint32_t or GLuint.
2595 */
2596 void
2597 intel_miptree_map(struct brw_context *brw,
2598 struct intel_mipmap_tree *mt,
2599 unsigned int level,
2600 unsigned int slice,
2601 unsigned int x,
2602 unsigned int y,
2603 unsigned int w,
2604 unsigned int h,
2605 GLbitfield mode,
2606 void **out_ptr,
2607 ptrdiff_t *out_stride)
2608 {
2609 struct intel_miptree_map *map;
2610
2611 assert(mt->num_samples <= 1);
2612
2613 map = intel_miptree_attach_map(mt, level, slice, x, y, w, h, mode);
2614 if (!map){
2615 *out_ptr = NULL;
2616 *out_stride = 0;
2617 return;
2618 }
2619
2620 intel_miptree_slice_resolve_depth(brw, mt, level, slice);
2621 if (map->mode & GL_MAP_WRITE_BIT) {
2622 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
2623 }
2624
2625 if (mt->format == MESA_FORMAT_S_UINT8) {
2626 intel_miptree_map_s8(brw, mt, map, level, slice);
2627 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2628 !(mode & BRW_MAP_DIRECT_BIT)) {
2629 intel_miptree_map_etc(brw, mt, map, level, slice);
2630 } else if (mt->stencil_mt && !(mode & BRW_MAP_DIRECT_BIT)) {
2631 intel_miptree_map_depthstencil(brw, mt, map, level, slice);
2632 } else if (use_intel_mipree_map_blit(brw, mt, mode, level, slice)) {
2633 intel_miptree_map_blit(brw, mt, map, level, slice);
2634 #if defined(USE_SSE41)
2635 } else if (!(mode & GL_MAP_WRITE_BIT) &&
2636 !mt->compressed && cpu_has_sse4_1 &&
2637 (mt->pitch % 16 == 0)) {
2638 intel_miptree_map_movntdqa(brw, mt, map, level, slice);
2639 #endif
2640 } else {
2641 intel_miptree_map_gtt(brw, mt, map, level, slice);
2642 }
2643
2644 *out_ptr = map->ptr;
2645 *out_stride = map->stride;
2646
2647 if (map->ptr == NULL)
2648 intel_miptree_release_map(mt, level, slice);
2649 }
2650
2651 void
2652 intel_miptree_unmap(struct brw_context *brw,
2653 struct intel_mipmap_tree *mt,
2654 unsigned int level,
2655 unsigned int slice)
2656 {
2657 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
2658
2659 assert(mt->num_samples <= 1);
2660
2661 if (!map)
2662 return;
2663
2664 DBG("%s: mt %p (%s) level %d slice %d\n", __func__,
2665 mt, _mesa_get_format_name(mt->format), level, slice);
2666
2667 if (mt->format == MESA_FORMAT_S_UINT8) {
2668 intel_miptree_unmap_s8(brw, mt, map, level, slice);
2669 } else if (mt->etc_format != MESA_FORMAT_NONE &&
2670 !(map->mode & BRW_MAP_DIRECT_BIT)) {
2671 intel_miptree_unmap_etc(brw, mt, map, level, slice);
2672 } else if (mt->stencil_mt && !(map->mode & BRW_MAP_DIRECT_BIT)) {
2673 intel_miptree_unmap_depthstencil(brw, mt, map, level, slice);
2674 } else if (map->mt) {
2675 intel_miptree_unmap_blit(brw, mt, map, level, slice);
2676 #if defined(USE_SSE41)
2677 } else if (map->buffer && cpu_has_sse4_1) {
2678 intel_miptree_unmap_movntdqa(brw, mt, map, level, slice);
2679 #endif
2680 } else {
2681 intel_miptree_unmap_gtt(brw, mt, map, level, slice);
2682 }
2683
2684 intel_miptree_release_map(mt, level, slice);
2685 }