blob: 337b282f4e34e77507b70eec831d832fa29f0301 [file] [log] [blame]
Chia-I Wu4bc47012014-08-14 13:03:25 +08001/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080028#include "dev.h"
Chia-I Wu1bf06df2014-08-16 12:33:13 +080029#include "format.h"
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080030#include "gpu.h"
31#include "layout.h"
Chia-I Wu4bc47012014-08-14 13:03:25 +080032
33enum {
34 LAYOUT_TILING_NONE = 1 << INTEL_TILING_NONE,
35 LAYOUT_TILING_X = 1 << INTEL_TILING_X,
36 LAYOUT_TILING_Y = 1 << INTEL_TILING_Y,
37 LAYOUT_TILING_W = 1 << (INTEL_TILING_Y + 1),
38
39 LAYOUT_TILING_ALL = (LAYOUT_TILING_NONE |
40 LAYOUT_TILING_X |
41 LAYOUT_TILING_Y |
42 LAYOUT_TILING_W)
43};
44
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080045struct intel_layout_params {
46 const struct intel_gpu *gpu;
47 const XGL_IMAGE_CREATE_INFO *info;
Chia-I Wu4bc47012014-08-14 13:03:25 +080048
49 bool compressed;
50
51 unsigned h0, h1;
52 unsigned max_x, max_y;
53};
54
55static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080056layout_get_slice_size(const struct intel_layout *layout,
57 const struct intel_layout_params *params,
Chia-I Wu4bc47012014-08-14 13:03:25 +080058 unsigned level, unsigned *width, unsigned *height)
59{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080060 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +080061 unsigned w, h;
62
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080063 w = u_minify(info->extent.width, level);
64 h = u_minify(info->extent.height, level);
Chia-I Wu4bc47012014-08-14 13:03:25 +080065
66 /*
67 * From the Sandy Bridge PRM, volume 1 part 1, page 114:
68 *
69 * "The dimensions of the mip maps are first determined by applying the
70 * sizing algorithm presented in Non-Power-of-Two Mipmaps above. Then,
71 * if necessary, they are padded out to compression block boundaries."
72 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +080073 w = u_align(w, layout->block_width);
74 h = u_align(h, layout->block_height);
Chia-I Wu4bc47012014-08-14 13:03:25 +080075
76 /*
77 * From the Sandy Bridge PRM, volume 1 part 1, page 111:
78 *
79 * "If the surface is multisampled (4x), these values must be adjusted
80 * as follows before proceeding:
81 *
82 * W_L = ceiling(W_L / 2) * 4
83 * H_L = ceiling(H_L / 2) * 4"
84 *
85 * From the Ivy Bridge PRM, volume 1 part 1, page 108:
86 *
87 * "If the surface is multisampled and it is a depth or stencil surface
88 * or Multisampled Surface StorageFormat in SURFACE_STATE is
89 * MSFMT_DEPTH_STENCIL, W_L and H_L must be adjusted as follows before
90 * proceeding:
91 *
92 * #samples W_L = H_L =
93 * 2 ceiling(W_L / 2) * 4 HL [no adjustment]
94 * 4 ceiling(W_L / 2) * 4 ceiling(H_L / 2) * 4
95 * 8 ceiling(W_L / 2) * 8 ceiling(H_L / 2) * 4
96 * 16 ceiling(W_L / 2) * 8 ceiling(H_L / 2) * 8"
97 *
98 * For interleaved samples (4x), where pixels
99 *
100 * (x, y ) (x+1, y )
101 * (x, y+1) (x+1, y+1)
102 *
103 * would be is occupied by
104 *
105 * (x, y , si0) (x+1, y , si0) (x, y , si1) (x+1, y , si1)
106 * (x, y+1, si0) (x+1, y+1, si0) (x, y+1, si1) (x+1, y+1, si1)
107 * (x, y , si2) (x+1, y , si2) (x, y , si3) (x+1, y , si3)
108 * (x, y+1, si2) (x+1, y+1, si2) (x, y+1, si3) (x+1, y+1, si3)
109 *
110 * Thus the need to
111 *
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800112 * w = u_align(w, 2) * 2;
113 * y = u_align(y, 2) * 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800114 */
115 if (layout->interleaved_samples) {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800116 switch (info->samples) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800117 case 0:
118 case 1:
119 break;
120 case 2:
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800121 w = u_align(w, 2) * 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800122 break;
123 case 4:
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800124 w = u_align(w, 2) * 2;
125 h = u_align(h, 2) * 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800126 break;
127 case 8:
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800128 w = u_align(w, 2) * 4;
129 h = u_align(h, 2) * 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800130 break;
131 case 16:
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800132 w = u_align(w, 2) * 4;
133 h = u_align(h, 2) * 4;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800134 break;
135 default:
136 assert(!"unsupported sample count");
137 break;
138 }
139 }
140
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800141 w = u_align(w, layout->align_i);
142 h = u_align(h, layout->align_j);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800143
144 *width = w;
145 *height = h;
146}
147
148static unsigned
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800149layout_get_num_layers(const struct intel_layout *layout,
150 const struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800151{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800152 const XGL_IMAGE_CREATE_INFO *info = params->info;
153 unsigned num_layers = info->arraySize;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800154
155 /* samples of the same index are stored in a layer */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800156 if (info->samples > 1 && !layout->interleaved_samples)
157 num_layers *= info->samples;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800158
159 return num_layers;
160}
161
162static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800163layout_init_layer_height(struct intel_layout *layout,
164 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800165{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800166 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800167 unsigned num_layers;
168
169 num_layers = layout_get_num_layers(layout, params);
170 if (num_layers <= 1)
171 return;
172
173 if (!layout->full_layers) {
174 layout->layer_height = params->h0;
175 params->max_y += params->h0 * (num_layers - 1);
176 return;
177 }
178
179 /*
180 * From the Sandy Bridge PRM, volume 1 part 1, page 115:
181 *
182 * "The following equation is used for surface formats other than
183 * compressed textures:
184 *
185 * QPitch = (h0 + h1 + 11j)"
186 *
187 * "The equation for compressed textures (BC* and FXT1 surface formats)
188 * follows:
189 *
190 * QPitch = (h0 + h1 + 11j) / 4"
191 *
192 * "[DevSNB] Errata: Sampler MSAA Qpitch will be 4 greater than the
193 * value calculated in the equation above, for every other odd Surface
194 * Height starting from 1 i.e. 1,5,9,13"
195 *
196 * From the Ivy Bridge PRM, volume 1 part 1, page 111-112:
197 *
198 * "If Surface Array Spacing is set to ARYSPC_FULL (note that the depth
199 * buffer and stencil buffer have an implied value of ARYSPC_FULL):
200 *
201 * QPitch = (h0 + h1 + 12j)
202 * QPitch = (h0 + h1 + 12j) / 4 (compressed)
203 *
204 * (There are many typos or missing words here...)"
205 *
206 * To access the N-th slice, an offset of (Stride * QPitch * N) is added to
207 * the base address. The PRM divides QPitch by 4 for compressed formats
208 * because the block height for those formats are 4, and it wants QPitch to
209 * mean the number of memory rows, as opposed to texel rows, between
210 * slices. Since we use texel rows everywhere, we do not need to divide
211 * QPitch by 4.
212 */
213 layout->layer_height = params->h0 + params->h1 +
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800214 ((intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) ? 12 : 11) * layout->align_j;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800215
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800216 if (intel_gpu_gen(params->gpu) == INTEL_GEN(6) && info->samples > 1 &&
217 info->extent.height % 4 == 1)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800218 layout->layer_height += 4;
219
220 params->max_y += layout->layer_height * (num_layers - 1);
221}
222
223static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800224layout_init_levels(struct intel_layout *layout,
225 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800226{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800227 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800228 unsigned cur_x, cur_y;
229 unsigned lv;
230
231 cur_x = 0;
232 cur_y = 0;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800233 for (lv = 0; lv < info->mipLevels; lv++) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800234 unsigned level_w, level_h;
235
236 layout_get_slice_size(layout, params, lv, &level_w, &level_h);
237
238 layout->levels[lv].x = cur_x;
239 layout->levels[lv].y = cur_y;
240 layout->levels[lv].slice_width = level_w;
241 layout->levels[lv].slice_height = level_h;
242
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800243 if (info->imageType == XGL_IMAGE_3D) {
244 const unsigned num_slices = u_minify(info->extent.depth, lv);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800245 const unsigned num_slices_per_row = 1 << lv;
246 const unsigned num_rows =
247 (num_slices + num_slices_per_row - 1) / num_slices_per_row;
248
249 level_w *= num_slices_per_row;
250 level_h *= num_rows;
251
252 cur_y += level_h;
253 } else {
254 /* MIPLAYOUT_BELOW */
255 if (lv == 1)
256 cur_x += level_w;
257 else
258 cur_y += level_h;
259 }
260
261 if (params->max_x < layout->levels[lv].x + level_w)
262 params->max_x = layout->levels[lv].x + level_w;
263 if (params->max_y < layout->levels[lv].y + level_h)
264 params->max_y = layout->levels[lv].y + level_h;
265 }
266
267 params->h0 = layout->levels[0].slice_height;
268 if (layout->full_layers) {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800269 if (info->mipLevels > 1)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800270 params->h1 = layout->levels[1].slice_height;
271 else
272 layout_get_slice_size(layout, params, 1, &cur_x, &params->h1);
273 }
274}
275
276static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800277layout_init_alignments(struct intel_layout *layout,
278 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800279{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800280 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800281
282 /*
283 * From the Sandy Bridge PRM, volume 1 part 1, page 113:
284 *
285 * "surface format align_i align_j
286 * YUV 4:2:2 formats 4 *see below
287 * BC1-5 4 4
288 * FXT1 8 4
289 * all other formats 4 *see below"
290 *
291 * "- align_j = 4 for any depth buffer
292 * - align_j = 2 for separate stencil buffer
293 * - align_j = 4 for any render target surface is multisampled (4x)
294 * - align_j = 4 for any render target surface with Surface Vertical
295 * Alignment = VALIGN_4
296 * - align_j = 2 for any render target surface with Surface Vertical
297 * Alignment = VALIGN_2
298 * - align_j = 2 for all other render target surface
299 * - align_j = 2 for any sampling engine surface with Surface Vertical
300 * Alignment = VALIGN_2
301 * - align_j = 4 for any sampling engine surface with Surface Vertical
302 * Alignment = VALIGN_4"
303 *
304 * From the Sandy Bridge PRM, volume 4 part 1, page 86:
305 *
306 * "This field (Surface Vertical Alignment) must be set to VALIGN_2 if
307 * the Surface Format is 96 bits per element (BPE)."
308 *
309 * They can be rephrased as
310 *
311 * align_i align_j
312 * compressed formats block width block height
313 * PIPE_FORMAT_S8_UINT 4 2
314 * other depth/stencil formats 4 4
315 * 4x multisampled 4 4
316 * bpp 96 4 2
317 * others 4 2 or 4
318 */
319
320 /*
321 * From the Ivy Bridge PRM, volume 1 part 1, page 110:
322 *
323 * "surface defined by surface format align_i align_j
324 * 3DSTATE_DEPTH_BUFFER D16_UNORM 8 4
325 * not D16_UNORM 4 4
326 * 3DSTATE_STENCIL_BUFFER N/A 8 8
327 * SURFACE_STATE BC*, ETC*, EAC* 4 4
328 * FXT1 8 4
329 * all others (set by SURFACE_STATE)"
330 *
331 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
332 *
333 * "- This field (Surface Vertical Aligment) is intended to be set to
334 * VALIGN_4 if the surface was rendered as a depth buffer, for a
335 * multisampled (4x) render target, or for a multisampled (8x)
336 * render target, since these surfaces support only alignment of 4.
337 * - Use of VALIGN_4 for other surfaces is supported, but uses more
338 * memory.
339 * - This field must be set to VALIGN_4 for all tiled Y Render Target
340 * surfaces.
341 * - Value of 1 is not supported for format YCRCB_NORMAL (0x182),
342 * YCRCB_SWAPUVY (0x183), YCRCB_SWAPUV (0x18f), YCRCB_SWAPY (0x190)
343 * - If Number of Multisamples is not MULTISAMPLECOUNT_1, this field
344 * must be set to VALIGN_4."
345 * - VALIGN_4 is not supported for surface format R32G32B32_FLOAT."
346 *
347 * "- This field (Surface Horizontal Aligment) is intended to be set to
348 * HALIGN_8 only if the surface was rendered as a depth buffer with
349 * Z16 format or a stencil buffer, since these surfaces support only
350 * alignment of 8.
351 * - Use of HALIGN_8 for other surfaces is supported, but uses more
352 * memory.
353 * - This field must be set to HALIGN_4 if the Surface Format is BC*.
354 * - This field must be set to HALIGN_8 if the Surface Format is
355 * FXT1."
356 *
357 * They can be rephrased as
358 *
359 * align_i align_j
360 * compressed formats block width block height
361 * PIPE_FORMAT_Z16_UNORM 8 4
362 * PIPE_FORMAT_S8_UINT 8 8
363 * other depth/stencil formats 4 or 8 4
364 * 2x or 4x multisampled 4 or 8 4
365 * tiled Y 4 or 8 4 (if rt)
366 * PIPE_FORMAT_R32G32B32_FLOAT 4 or 8 2
367 * others 4 or 8 2 or 4
368 */
369
370 if (params->compressed) {
371 /* this happens to be the case */
372 layout->align_i = layout->block_width;
373 layout->align_j = layout->block_height;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800374 } else if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
375 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) {
376 switch (layout->format.channelFormat) {
377 case XGL_CH_FMT_R16:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800378 layout->align_i = 8;
379 layout->align_j = 4;
380 break;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800381 case XGL_CH_FMT_R8:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800382 layout->align_i = 8;
383 layout->align_j = 8;
384 break;
385 default:
386 layout->align_i = 4;
387 layout->align_j = 4;
388 break;
389 }
390 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800391 switch (layout->format.channelFormat) {
392 case XGL_CH_FMT_R8:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800393 layout->align_i = 4;
394 layout->align_j = 2;
395 break;
396 default:
397 layout->align_i = 4;
398 layout->align_j = 4;
399 break;
400 }
401 }
402 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800403 const bool valign_4 = (info->samples > 1) ||
404 (intel_gpu_gen(params->gpu) >= INTEL_GEN(7) &&
Chia-I Wu4bc47012014-08-14 13:03:25 +0800405 layout->tiling == INTEL_TILING_Y &&
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800406 (info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT));
Chia-I Wu4bc47012014-08-14 13:03:25 +0800407
408 if (valign_4)
409 assert(layout->block_size != 12);
410
411 layout->align_i = 4;
412 layout->align_j = (valign_4) ? 4 : 2;
413 }
414
415 /*
416 * the fact that align i and j are multiples of block width and height
417 * respectively is what makes the size of the bo a multiple of the block
418 * size, slices start at block boundaries, and many of the computations
419 * work.
420 */
421 assert(layout->align_i % layout->block_width == 0);
422 assert(layout->align_j % layout->block_height == 0);
423
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800424 /* make sure u_align() works */
425 assert(u_is_pow2(layout->align_i) &&
426 u_is_pow2(layout->align_j));
427 assert(u_is_pow2(layout->block_width) &&
428 u_is_pow2(layout->block_height));
Chia-I Wu4bc47012014-08-14 13:03:25 +0800429}
430
431static unsigned
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800432layout_get_valid_tilings(const struct intel_layout *layout,
433 const struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800434{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800435 const XGL_IMAGE_CREATE_INFO *info = params->info;
436 const XGL_FORMAT format = layout->format;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800437 unsigned valid_tilings = LAYOUT_TILING_ALL;
438
439 /*
Chia-I Wu4bc47012014-08-14 13:03:25 +0800440 * From the Sandy Bridge PRM, volume 2 part 1, page 318:
441 *
442 * "[DevSNB+]: This field (Tiled Surface) must be set to TRUE. Linear
443 * Depth Buffer is not supported."
444 *
445 * "The Depth Buffer, if tiled, must use Y-Major tiling."
446 *
447 * From the Sandy Bridge PRM, volume 1 part 2, page 22:
448 *
449 * "W-Major Tile Format is used for separate stencil."
450 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800451 if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
452 switch (format.channelFormat) {
453 case XGL_CH_FMT_R8:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800454 valid_tilings &= LAYOUT_TILING_W;
455 break;
456 default:
457 valid_tilings &= LAYOUT_TILING_Y;
458 break;
459 }
460 }
461
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800462 if (info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800463 /*
464 * From the Sandy Bridge PRM, volume 1 part 2, page 32:
465 *
466 * "NOTE: 128BPE Format Color buffer ( render target ) MUST be
467 * either TileX or Linear."
468 */
469 if (layout->block_size == 16)
470 valid_tilings &= ~LAYOUT_TILING_Y;
471
472 /*
473 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
474 *
475 * "This field (Surface Vertical Aligment) must be set to VALIGN_4
476 * for all tiled Y Render Target surfaces."
477 *
478 * "VALIGN_4 is not supported for surface format R32G32B32_FLOAT."
479 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800480 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7) && layout->block_size == 12)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800481 valid_tilings &= ~LAYOUT_TILING_Y;
482 }
483
484 /* no conflicting binding flags */
485 assert(valid_tilings);
486
487 return valid_tilings;
488}
489
490static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800491layout_init_tiling(struct intel_layout *layout,
492 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800493{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800494 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800495 unsigned valid_tilings = layout_get_valid_tilings(layout, params);
496
497 layout->valid_tilings = valid_tilings;
498
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800499 if (info->usage & (XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800500 /*
501 * heuristically set a minimum width/height for enabling tiling
502 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800503 if (info->extent.width < 64 && (valid_tilings & ~LAYOUT_TILING_X))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800504 valid_tilings &= ~LAYOUT_TILING_X;
505
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800506 if ((info->extent.width < 32 || info->extent.height < 16) &&
507 (info->extent.width < 16 || info->extent.height < 32) &&
Chia-I Wu4bc47012014-08-14 13:03:25 +0800508 (valid_tilings & ~LAYOUT_TILING_Y))
509 valid_tilings &= ~LAYOUT_TILING_Y;
510 } else {
511 /* force linear if we are not sure where the texture is bound to */
512 if (valid_tilings & LAYOUT_TILING_NONE)
513 valid_tilings &= LAYOUT_TILING_NONE;
514 }
515
516 /* prefer tiled over linear */
517 if (valid_tilings & LAYOUT_TILING_Y)
518 layout->tiling = INTEL_TILING_Y;
519 else if (valid_tilings & LAYOUT_TILING_X)
520 layout->tiling = INTEL_TILING_X;
521 else /* linear or W-tiled, which has no hardware support */
522 layout->tiling = INTEL_TILING_NONE;
523}
524
525static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800526layout_init_arrangements_gen7(struct intel_layout *layout,
527 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800528{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800529 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800530
531 /*
532 * It is not explicitly states, but render targets are expected to be
533 * UMS/CMS (samples non-interleaved) and depth/stencil buffers are expected
534 * to be IMS (samples interleaved).
535 *
536 * See "Multisampled Surface Storage Format" field of SURFACE_STATE.
537 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800538 if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800539 layout->interleaved_samples = true;
540
541 /*
542 * From the Ivy Bridge PRM, volume 1 part 1, page 111:
543 *
544 * "note that the depth buffer and stencil buffer have an implied
545 * value of ARYSPC_FULL"
546 */
547 layout->full_layers = true;
548 } else {
549 layout->interleaved_samples = false;
550
551 /*
552 * From the Ivy Bridge PRM, volume 4 part 1, page 66:
553 *
554 * "If Multisampled Surface Storage Format is MSFMT_MSS and Number
555 * of Multisamples is not MULTISAMPLECOUNT_1, this field (Surface
556 * Array Spacing) must be set to ARYSPC_LOD0."
557 *
558 * As multisampled resources are not mipmapped, we never use
559 * ARYSPC_FULL for them.
560 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800561 if (info->samples > 1)
562 assert(info->mipLevels == 1);
563 layout->full_layers = (info->mipLevels > 1);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800564 }
565}
566
567static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800568layout_init_arrangements_gen6(struct intel_layout *layout,
569 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800570{
571 /* GEN6 supports only interleaved samples */
572 layout->interleaved_samples = true;
573
574 /*
575 * From the Sandy Bridge PRM, volume 1 part 1, page 115:
576 *
577 * "The separate stencil buffer does not support mip mapping, thus the
578 * storage for LODs other than LOD 0 is not needed. The following
579 * QPitch equation applies only to the separate stencil buffer:
580 *
581 * QPitch = h_0"
582 *
583 * GEN6 does not support compact spacing otherwise.
584 */
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800585 layout->full_layers = !intel_format_is_stencil(layout->format);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800586}
587
588static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800589layout_init_arrangements(struct intel_layout *layout,
590 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800591{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800592 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800593 layout_init_arrangements_gen7(layout, params);
594 else
595 layout_init_arrangements_gen6(layout, params);
596
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800597 layout->is_2d = (params->info->imageType != XGL_IMAGE_3D);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800598}
599
600static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800601layout_init_format(struct intel_layout *layout,
602 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800603{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800604 const XGL_IMAGE_CREATE_INFO *info = params->info;
605 XGL_FORMAT format = params->info->format;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800606 bool require_separate_stencil;
607
608 /*
609 * From the Sandy Bridge PRM, volume 2 part 1, page 317:
610 *
611 * "This field (Separate Stencil Buffer Enable) must be set to the same
612 * value (enabled or disabled) as Hierarchical Depth Buffer Enable."
613 *
614 * GEN7+ requires separate stencil buffers.
615 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800616 if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
617 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800618 require_separate_stencil = true;
619 else
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800620 require_separate_stencil =(layout->aux_type == INTEL_LAYOUT_AUX_HIZ);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800621 }
622
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800623 if (format.numericFormat == XGL_NUM_FMT_DS) {
624 switch (format.channelFormat) {
625 case XGL_CH_FMT_R32G8:
626 if (require_separate_stencil) {
627 format.channelFormat = XGL_CH_FMT_R32;
628 layout->separate_stencil = true;
629 }
630 break;
631 default:
632 break;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800633 }
Chia-I Wu4bc47012014-08-14 13:03:25 +0800634 }
635
Chia-I Wu4bc47012014-08-14 13:03:25 +0800636 layout->format = format;
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800637 layout->block_width = icd_format_get_block_width(format);
638 layout->block_height = layout->block_width;
639 layout->block_size = icd_format_get_size(format);
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800640
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800641 params->compressed = icd_format_is_compressed(format);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800642}
643
644static bool
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800645layout_want_mcs(struct intel_layout *layout,
646 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800647{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800648 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800649 bool want_mcs = false;
650
651 /* MCS is for RT on GEN7+ */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800652 if (intel_gpu_gen(params->gpu) < INTEL_GEN(7))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800653 return false;
654
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800655 if (info->imageType != XGL_IMAGE_2D ||
656 !(info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800657 return false;
658
659 /*
660 * From the Ivy Bridge PRM, volume 4 part 1, page 77:
661 *
662 * "For Render Target and Sampling Engine Surfaces:If the surface is
663 * multisampled (Number of Multisamples any value other than
664 * MULTISAMPLECOUNT_1), this field (MCS Enable) must be enabled."
665 *
666 * "This field must be set to 0 for all SINT MSRTs when all RT channels
667 * are not written"
668 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800669 if (info->samples > 1 && !layout->interleaved_samples &&
670 !(info->format.numericFormat == XGL_NUM_FMT_UINT ||
671 info->format.numericFormat == XGL_NUM_FMT_SINT)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800672 want_mcs = true;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800673 } else if (info->samples <= 1) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800674 /*
675 * From the Ivy Bridge PRM, volume 2 part 1, page 326:
676 *
677 * "When MCS is buffer is used for color clear of non-multisampler
678 * render target, the following restrictions apply.
679 * - Support is limited to tiled render targets.
680 * - Support is for non-mip-mapped and non-array surface types
681 * only.
682 * - Clear is supported only on the full RT; i.e., no partial clear
683 * or overlapping clears.
684 * - MCS buffer for non-MSRT is supported only for RT formats
685 * 32bpp, 64bpp and 128bpp.
686 * ..."
687 */
688 if (layout->tiling != INTEL_TILING_NONE &&
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800689 info->mipLevels == 1 && info->arraySize == 1) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800690 switch (layout->block_size) {
691 case 4:
692 case 8:
693 case 16:
694 want_mcs = true;
695 break;
696 default:
697 break;
698 }
699 }
700 }
701
702 return want_mcs;
703}
704
705static bool
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800706layout_want_hiz(const struct intel_layout *layout,
707 const struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800708{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800709 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800710 bool want_hiz = false;
711
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800712 if (!(info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800713 return false;
714
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800715 if (!intel_format_is_depth(info->format))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800716 return false;
717
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800718 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800719 want_hiz = true;
720 } else {
721 /*
722 * From the Sandy Bridge PRM, volume 2 part 1, page 312:
723 *
724 * "The hierarchical depth buffer does not support the LOD field, it
725 * is assumed by hardware to be zero. A separate hierarachical
726 * depth buffer is required for each LOD used, and the
727 * corresponding buffer's state delivered to hardware each time a
728 * new depth buffer state with modified LOD is delivered."
729 *
730 * But we have a stronger requirement. Because of layer offsetting
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800731 * (check out the callers of intel_layout_get_slice_tile_offset()), we
Chia-I Wu4bc47012014-08-14 13:03:25 +0800732 * already have to require the texture to be non-mipmapped and
733 * non-array.
734 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800735 if (info->mipLevels == 1 && info->arraySize == 1 &&
736 info->extent.depth == 1)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800737 want_hiz = true;
738 }
739
740 return want_hiz;
741}
742
743static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800744layout_init_aux(struct intel_layout *layout,
745 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800746{
747 if (layout_want_hiz(layout, params))
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800748 layout->aux_type = INTEL_LAYOUT_AUX_HIZ;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800749 else if (layout_want_mcs(layout, params))
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800750 layout->aux_type = INTEL_LAYOUT_AUX_MCS;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800751}
752
753static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800754layout_align(struct intel_layout *layout, struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800755{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800756 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800757 int align_w = 1, align_h = 1, pad_h = 0;
758
759 /*
760 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
761 *
762 * "To determine the necessary padding on the bottom and right side of
763 * the surface, refer to the table in Section 7.18.3.4 for the i and j
764 * parameters for the surface format in use. The surface must then be
765 * extended to the next multiple of the alignment unit size in each
766 * dimension, and all texels contained in this extended surface must
767 * have valid GTT entries."
768 *
769 * "For cube surfaces, an additional two rows of padding are required
770 * at the bottom of the surface. This must be ensured regardless of
771 * whether the surface is stored tiled or linear. This is due to the
772 * potential rotation of cache line orientation from memory to cache."
773 *
774 * "For compressed textures (BC* and FXT1 surface formats), padding at
775 * the bottom of the surface is to an even compressed row, which is
776 * equal to a multiple of 8 uncompressed texel rows. Thus, for padding
777 * purposes, these surfaces behave as if j = 8 only for surface
778 * padding purposes. The value of 4 for j still applies for mip level
779 * alignment and QPitch calculation."
780 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800781 if (info->usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) {
782 if (align_w < layout->align_i)
783 align_w = layout->align_i;
784 if (align_h < layout->align_j)
785 align_h = layout->align_j;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800786
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800787 /* in case it is used as a cube */
788 if (info->imageType == XGL_IMAGE_2D)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800789 pad_h += 2;
790
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800791 if (params->compressed && align_h < layout->align_j * 2)
792 align_h = layout->align_j * 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800793 }
794
795 /*
796 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
797 *
798 * "If the surface contains an odd number of rows of data, a final row
799 * below the surface must be allocated."
800 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800801 if ((info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) && align_h < 2)
802 align_h = 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800803
804 /*
805 * Depth Buffer Clear/Resolve works in 8x4 sample blocks. In
806 * ilo_texture_can_enable_hiz(), we always return true for the first slice.
807 * To avoid out-of-bound access, we have to pad.
808 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800809 if (layout->aux_type == INTEL_LAYOUT_AUX_HIZ) {
810 if (align_w < 8)
811 align_w = 8;
812 if (align_h < 4)
813 align_h = 4;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800814 }
815
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800816 params->max_x = u_align(params->max_x, align_w);
817 params->max_y = u_align(params->max_y + pad_h, align_h);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800818}
819
820/* note that this may force the texture to be linear */
821static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800822layout_calculate_bo_size(struct intel_layout *layout,
823 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800824{
825 assert(params->max_x % layout->block_width == 0);
826 assert(params->max_y % layout->block_height == 0);
827 assert(layout->layer_height % layout->block_height == 0);
828
829 layout->bo_stride =
830 (params->max_x / layout->block_width) * layout->block_size;
831 layout->bo_height = params->max_y / layout->block_height;
832
833 while (true) {
834 unsigned w = layout->bo_stride, h = layout->bo_height;
835 unsigned align_w, align_h;
836
837 /*
838 * From the Haswell PRM, volume 5, page 163:
839 *
840 * "For linear surfaces, additional padding of 64 bytes is required
841 * at the bottom of the surface. This is in addition to the padding
842 * required above."
843 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800844 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7.5) &&
845 (params->info->usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
Chia-I Wu4bc47012014-08-14 13:03:25 +0800846 layout->tiling == INTEL_TILING_NONE) {
847 layout->bo_height +=
848 (64 + layout->bo_stride - 1) / layout->bo_stride;
849 }
850
851 /*
852 * From the Sandy Bridge PRM, volume 4 part 1, page 81:
853 *
854 * "- For linear render target surfaces, the pitch must be a
855 * multiple of the element size for non-YUV surface formats.
856 * Pitch must be a multiple of 2 * element size for YUV surface
857 * formats.
858 * - For other linear surfaces, the pitch can be any multiple of
859 * bytes.
860 * - For tiled surfaces, the pitch must be a multiple of the tile
861 * width."
862 *
863 * Different requirements may exist when the bo is used in different
864 * places, but our alignments here should be good enough that we do not
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800865 * need to check layout->info->usage.
Chia-I Wu4bc47012014-08-14 13:03:25 +0800866 */
867 switch (layout->tiling) {
868 case INTEL_TILING_X:
869 align_w = 512;
870 align_h = 8;
871 break;
872 case INTEL_TILING_Y:
873 align_w = 128;
874 align_h = 32;
875 break;
876 default:
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800877 if (intel_format_is_stencil(layout->format)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800878 /*
879 * From the Sandy Bridge PRM, volume 1 part 2, page 22:
880 *
881 * "A 4KB tile is subdivided into 8-high by 8-wide array of
882 * Blocks for W-Major Tiles (W Tiles). Each Block is 8 rows by 8
883 * bytes."
884 *
885 * Since we asked for INTEL_TILING_NONE instead of the non-existent
886 * INTEL_TILING_W, we want to align to W tiles here.
887 */
888 align_w = 64;
889 align_h = 64;
890 } else {
891 /* some good enough values */
892 align_w = 64;
893 align_h = 2;
894 }
895 break;
896 }
897
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800898 w = u_align(w, align_w);
899 h = u_align(h, align_h);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800900
901 /* make sure the bo is mappable */
902 if (layout->tiling != INTEL_TILING_NONE) {
903 /*
904 * Usually only the first 256MB of the GTT is mappable.
905 *
906 * See also how intel_context::max_gtt_map_object_size is calculated.
907 */
908 const size_t mappable_gtt_size = 256 * 1024 * 1024;
909
910 /*
911 * Be conservative. We may be able to switch from VALIGN_4 to
912 * VALIGN_2 if the layout was Y-tiled, but let's keep it simple.
913 */
914 if (mappable_gtt_size / w / 4 < h) {
915 if (layout->valid_tilings & LAYOUT_TILING_NONE) {
916 layout->tiling = INTEL_TILING_NONE;
917 /* MCS support for non-MSRTs is limited to tiled RTs */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800918 if (layout->aux_type == INTEL_LAYOUT_AUX_MCS &&
919 params->info->samples <= 1)
920 layout->aux_type = INTEL_LAYOUT_AUX_NONE;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800921
922 continue;
923 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800924 /* mapping will fail */
Chia-I Wu4bc47012014-08-14 13:03:25 +0800925 }
926 }
927 }
928
929 layout->bo_stride = w;
930 layout->bo_height = h;
931 break;
932 }
933}
934
935static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800936layout_calculate_hiz_size(struct intel_layout *layout,
937 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800938{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800939 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800940 const int hz_align_j = 8;
941 int hz_width, hz_height;
942
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800943 assert(layout->aux_type == INTEL_LAYOUT_AUX_HIZ);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800944
945 /*
946 * See the Sandy Bridge PRM, volume 2 part 1, page 312, and the Ivy Bridge
947 * PRM, volume 2 part 1, page 312-313.
948 *
949 * It seems HiZ buffer is aligned to 8x8, with every two rows packed into a
950 * memory row.
951 */
952
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800953 hz_width = u_align(layout->levels[0].slice_width, 16);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800954
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800955 if (info->imageType == XGL_IMAGE_3D) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800956 unsigned lv;
957
958 hz_height = 0;
959
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800960 for (lv = 0; lv < info->mipLevels; lv++) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800961 const unsigned h =
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800962 u_align(layout->levels[lv].slice_height, hz_align_j);
963 hz_height += h * u_minify(info->extent.depth, lv);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800964 }
965
966 hz_height /= 2;
967 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800968 const unsigned h0 = u_align(params->h0, hz_align_j);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800969 unsigned hz_qpitch = h0;
970
971 if (layout->full_layers) {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800972 const unsigned h1 = u_align(params->h1, hz_align_j);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800973 const unsigned htail =
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800974 ((intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) ? 12 : 11) * hz_align_j;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800975
976 hz_qpitch += h1 + htail;
977 }
978
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800979 hz_height = hz_qpitch * info->arraySize / 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800980
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800981 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
982 hz_height = u_align(hz_height, 8);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800983 }
984
985 /* align to Y-tile */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800986 layout->aux_stride = u_align(hz_width, 128);
987 layout->aux_height = u_align(hz_height, 32);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800988}
989
990static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800991layout_calculate_mcs_size(struct intel_layout *layout,
992 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800993{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800994 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800995 int mcs_width, mcs_height, mcs_cpp;
996 int downscale_x, downscale_y;
997
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800998 assert(layout->aux_type == INTEL_LAYOUT_AUX_MCS);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800999
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001000 if (info->samples > 1) {
Chia-I Wu4bc47012014-08-14 13:03:25 +08001001 /*
1002 * From the Ivy Bridge PRM, volume 2 part 1, page 326, the clear
1003 * rectangle is scaled down by 8x2 for 4X MSAA and 2x2 for 8X MSAA. The
1004 * need of scale down could be that the clear rectangle is used to clear
1005 * the MCS instead of the RT.
1006 *
1007 * For 8X MSAA, we need 32 bits in MCS for every pixel in the RT. The
1008 * 2x2 factor could come from that the hardware writes 128 bits (an
1009 * OWord) at a time, and the OWord in MCS maps to a 2x2 pixel block in
1010 * the RT. For 4X MSAA, we need 8 bits in MCS for every pixel in the
1011 * RT. Similarly, we could reason that an OWord in 4X MCS maps to a 8x2
1012 * pixel block in the RT.
1013 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001014 switch (info->samples) {
Chia-I Wu4bc47012014-08-14 13:03:25 +08001015 case 2:
1016 case 4:
1017 downscale_x = 8;
1018 downscale_y = 2;
1019 mcs_cpp = 1;
1020 break;
1021 case 8:
1022 downscale_x = 2;
1023 downscale_y = 2;
1024 mcs_cpp = 4;
1025 break;
1026 case 16:
1027 downscale_x = 2;
1028 downscale_y = 1;
1029 mcs_cpp = 8;
1030 break;
1031 default:
1032 assert(!"unsupported sample count");
1033 return;
1034 break;
1035 }
1036
1037 /*
1038 * It also appears that the 2x2 subspans generated by the scaled-down
1039 * clear rectangle cannot be masked. The scale-down clear rectangle
1040 * thus must be aligned to 2x2, and we need to pad.
1041 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001042 mcs_width = u_align(info->extent.width, downscale_x * 2);
1043 mcs_height = u_align(info->extent.height, downscale_y * 2);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001044 }
1045 else {
1046 /*
1047 * From the Ivy Bridge PRM, volume 2 part 1, page 327:
1048 *
1049 * " Pixels Lines
1050 * TiledY RT CL
1051 * bpp
1052 * 32 8 4
1053 * 64 4 4
1054 * 128 2 4
1055 *
1056 * TiledX RT CL
1057 * bpp
1058 * 32 16 2
1059 * 64 8 2
1060 * 128 4 2"
1061 *
1062 * This table and the two following tables define the RT alignments, the
1063 * clear rectangle alignments, and the clear rectangle scale factors.
1064 * Viewing the RT alignments as the sizes of 128-byte blocks, we can see
1065 * that the clear rectangle alignments are 16x32 blocks, and the clear
1066 * rectangle scale factors are 8x16 blocks.
1067 *
1068 * For non-MSAA RT, we need 1 bit in MCS for every 128-byte block in the
1069 * RT. Similar to the MSAA cases, we can argue that an OWord maps to
1070 * 8x16 blocks.
1071 *
1072 * One problem with this reasoning is that a Y-tile in MCS has 8x32
1073 * OWords and maps to 64x512 128-byte blocks. This differs from i965,
1074 * which says that a Y-tile maps to 128x256 blocks (\see
1075 * intel_get_non_msrt_mcs_alignment). It does not really change
1076 * anything except for the size of the allocated MCS. Let's see if we
1077 * hit out-of-bound access.
1078 */
1079 switch (layout->tiling) {
1080 case INTEL_TILING_X:
1081 downscale_x = 64 / layout->block_size;
1082 downscale_y = 2;
1083 break;
1084 case INTEL_TILING_Y:
1085 downscale_x = 32 / layout->block_size;
1086 downscale_y = 4;
1087 break;
1088 default:
1089 assert(!"unsupported tiling mode");
1090 return;
1091 break;
1092 }
1093
1094 downscale_x *= 8;
1095 downscale_y *= 16;
1096
1097 /*
1098 * From the Haswell PRM, volume 7, page 652:
1099 *
1100 * "Clear rectangle must be aligned to two times the number of
1101 * pixels in the table shown below due to 16X16 hashing across the
1102 * slice."
1103 *
1104 * The scaled-down clear rectangle must be aligned to 4x4 instead of
1105 * 2x2, and we need to pad.
1106 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001107 mcs_width = u_align(info->extent.width, downscale_x * 4) / downscale_x;
1108 mcs_height = u_align(info->extent.height, downscale_y * 4) / downscale_y;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001109 mcs_cpp = 16; /* an OWord */
1110 }
1111
1112 /* align to Y-tile */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001113 layout->aux_stride = u_align(mcs_width * mcs_cpp, 128);
1114 layout->aux_height = u_align(mcs_height, 32);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001115}
1116
1117/**
1118 * Initialize the layout. Callers should zero-initialize \p layout first.
1119 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001120void intel_layout_init(struct intel_layout *layout,
1121 const struct intel_dev *dev,
1122 const XGL_IMAGE_CREATE_INFO *info)
Chia-I Wu4bc47012014-08-14 13:03:25 +08001123{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001124 struct intel_layout_params params;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001125
1126 memset(&params, 0, sizeof(params));
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001127 params.gpu = dev->gpu;
1128 params.info = info;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001129
1130 /* note that there are dependencies between these functions */
1131 layout_init_aux(layout, &params);
1132 layout_init_format(layout, &params);
1133 layout_init_arrangements(layout, &params);
1134 layout_init_tiling(layout, &params);
1135 layout_init_alignments(layout, &params);
1136 layout_init_levels(layout, &params);
1137 layout_init_layer_height(layout, &params);
1138
1139 layout_align(layout, &params);
1140 layout_calculate_bo_size(layout, &params);
1141
1142 switch (layout->aux_type) {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001143 case INTEL_LAYOUT_AUX_HIZ:
Chia-I Wu4bc47012014-08-14 13:03:25 +08001144 layout_calculate_hiz_size(layout, &params);
1145 break;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001146 case INTEL_LAYOUT_AUX_MCS:
Chia-I Wu4bc47012014-08-14 13:03:25 +08001147 layout_calculate_mcs_size(layout, &params);
1148 break;
1149 default:
1150 break;
1151 }
1152}
1153
1154/**
1155 * Update the tiling mode and bo stride (for imported resources).
1156 */
1157bool
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001158intel_layout_update_for_imported_bo(struct intel_layout *layout,
1159 enum intel_tiling_mode tiling,
1160 unsigned bo_stride)
Chia-I Wu4bc47012014-08-14 13:03:25 +08001161{
1162 if (!(layout->valid_tilings & (1 << tiling)))
1163 return false;
1164
1165 if ((tiling == INTEL_TILING_X && bo_stride % 512) ||
1166 (tiling == INTEL_TILING_Y && bo_stride % 128))
1167 return false;
1168
1169 layout->tiling = tiling;
1170 layout->bo_stride = bo_stride;
1171
1172 return true;
1173}