blob: 51bbd1cf74af08c0ed6aba76026e76c4a05a3773 [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 Wu457d0a62014-08-18 13:02:26 +080063 w = u_minify(layout->width0, level);
64 h = u_minify(layout->height0, 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 Wu457d0a62014-08-18 13:02:26 +0800112 * w = align(w, 2) * 2;
113 * y = 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 Wu457d0a62014-08-18 13:02:26 +0800141 /*
142 * From the Ivy Bridge PRM, volume 1 part 1, page 108:
143 *
144 * "For separate stencil buffer, the width must be mutiplied by 2 and
145 * height divided by 2..."
146 *
147 * To make things easier (for transfer), we will just double the stencil
148 * stride in 3DSTATE_STENCIL_BUFFER.
149 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800150 w = u_align(w, layout->align_i);
151 h = u_align(h, layout->align_j);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800152
153 *width = w;
154 *height = h;
155}
156
157static unsigned
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800158layout_get_num_layers(const struct intel_layout *layout,
159 const struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800160{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800161 const XGL_IMAGE_CREATE_INFO *info = params->info;
162 unsigned num_layers = info->arraySize;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800163
164 /* samples of the same index are stored in a layer */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800165 if (info->samples > 1 && !layout->interleaved_samples)
166 num_layers *= info->samples;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800167
168 return num_layers;
169}
170
171static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800172layout_init_layer_height(struct intel_layout *layout,
173 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800174{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800175 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800176 unsigned num_layers;
177
Chia-I Wu457d0a62014-08-18 13:02:26 +0800178 if (layout->walk != INTEL_LAYOUT_WALK_LAYER)
179 return;
180
Chia-I Wu4bc47012014-08-14 13:03:25 +0800181 num_layers = layout_get_num_layers(layout, params);
182 if (num_layers <= 1)
183 return;
184
Chia-I Wu4bc47012014-08-14 13:03:25 +0800185 /*
186 * From the Sandy Bridge PRM, volume 1 part 1, page 115:
187 *
188 * "The following equation is used for surface formats other than
189 * compressed textures:
190 *
191 * QPitch = (h0 + h1 + 11j)"
192 *
193 * "The equation for compressed textures (BC* and FXT1 surface formats)
194 * follows:
195 *
196 * QPitch = (h0 + h1 + 11j) / 4"
197 *
198 * "[DevSNB] Errata: Sampler MSAA Qpitch will be 4 greater than the
199 * value calculated in the equation above, for every other odd Surface
200 * Height starting from 1 i.e. 1,5,9,13"
201 *
202 * From the Ivy Bridge PRM, volume 1 part 1, page 111-112:
203 *
204 * "If Surface Array Spacing is set to ARYSPC_FULL (note that the depth
205 * buffer and stencil buffer have an implied value of ARYSPC_FULL):
206 *
207 * QPitch = (h0 + h1 + 12j)
208 * QPitch = (h0 + h1 + 12j) / 4 (compressed)
209 *
210 * (There are many typos or missing words here...)"
211 *
212 * To access the N-th slice, an offset of (Stride * QPitch * N) is added to
213 * the base address. The PRM divides QPitch by 4 for compressed formats
214 * because the block height for those formats are 4, and it wants QPitch to
215 * mean the number of memory rows, as opposed to texel rows, between
216 * slices. Since we use texel rows everywhere, we do not need to divide
217 * QPitch by 4.
218 */
219 layout->layer_height = params->h0 + params->h1 +
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800220 ((intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) ? 12 : 11) * layout->align_j;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800221
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800222 if (intel_gpu_gen(params->gpu) == INTEL_GEN(6) && info->samples > 1 &&
Chia-I Wu457d0a62014-08-18 13:02:26 +0800223 layout->height0 % 4 == 1)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800224 layout->layer_height += 4;
225
226 params->max_y += layout->layer_height * (num_layers - 1);
227}
228
229static void
Chia-I Wu457d0a62014-08-18 13:02:26 +0800230layout_init_lods(struct intel_layout *layout,
231 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800232{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800233 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800234 unsigned cur_x, cur_y;
235 unsigned lv;
236
237 cur_x = 0;
238 cur_y = 0;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800239 for (lv = 0; lv < info->mipLevels; lv++) {
Chia-I Wu457d0a62014-08-18 13:02:26 +0800240 unsigned lod_w, lod_h;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800241
Chia-I Wu457d0a62014-08-18 13:02:26 +0800242 layout_get_slice_size(layout, params, lv, &lod_w, &lod_h);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800243
Chia-I Wu457d0a62014-08-18 13:02:26 +0800244 layout->lods[lv].x = cur_x;
245 layout->lods[lv].y = cur_y;
246 layout->lods[lv].slice_width = lod_w;
247 layout->lods[lv].slice_height = lod_h;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800248
Chia-I Wu457d0a62014-08-18 13:02:26 +0800249 switch (layout->walk) {
250 case INTEL_LAYOUT_WALK_LOD:
251 lod_h *= layout_get_num_layers(layout, params);
252 if (lv == 1)
253 cur_x += lod_w;
254 else
255 cur_y += lod_h;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800256
Chia-I Wu457d0a62014-08-18 13:02:26 +0800257 /* every LOD begins at tile boundaries */
258 if (info->mipLevels > 1) {
259 intel_format_is_stencil(params->gpu, layout->format);
260 cur_x = u_align(cur_x, 64);
261 cur_y = u_align(cur_y, 64);
262 }
263 break;
264 case INTEL_LAYOUT_WALK_LAYER:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800265 /* MIPLAYOUT_BELOW */
266 if (lv == 1)
Chia-I Wu457d0a62014-08-18 13:02:26 +0800267 cur_x += lod_w;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800268 else
Chia-I Wu457d0a62014-08-18 13:02:26 +0800269 cur_y += lod_h;
270 break;
271 case INTEL_LAYOUT_WALK_3D:
272 {
273 const unsigned num_slices = u_minify(info->extent.depth, lv);
274 const unsigned num_slices_per_row = 1 << lv;
275 const unsigned num_rows =
276 (num_slices + num_slices_per_row - 1) / num_slices_per_row;
277
278 lod_w *= num_slices_per_row;
279 lod_h *= num_rows;
280
281 cur_y += lod_h;
282 }
283 break;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800284 }
285
Chia-I Wu457d0a62014-08-18 13:02:26 +0800286 if (params->max_x < layout->lods[lv].x + lod_w)
287 params->max_x = layout->lods[lv].x + lod_w;
288 if (params->max_y < layout->lods[lv].y + lod_h)
289 params->max_y = layout->lods[lv].y + lod_h;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800290 }
291
Chia-I Wu457d0a62014-08-18 13:02:26 +0800292 if (layout->walk == INTEL_LAYOUT_WALK_LAYER) {
293 params->h0 = layout->lods[0].slice_height;
294
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800295 if (info->mipLevels > 1)
Chia-I Wu457d0a62014-08-18 13:02:26 +0800296 params->h1 = layout->lods[1].slice_height;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800297 else
298 layout_get_slice_size(layout, params, 1, &cur_x, &params->h1);
299 }
300}
301
302static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800303layout_init_alignments(struct intel_layout *layout,
304 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800305{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800306 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800307
308 /*
309 * From the Sandy Bridge PRM, volume 1 part 1, page 113:
310 *
311 * "surface format align_i align_j
312 * YUV 4:2:2 formats 4 *see below
313 * BC1-5 4 4
314 * FXT1 8 4
315 * all other formats 4 *see below"
316 *
317 * "- align_j = 4 for any depth buffer
318 * - align_j = 2 for separate stencil buffer
319 * - align_j = 4 for any render target surface is multisampled (4x)
320 * - align_j = 4 for any render target surface with Surface Vertical
321 * Alignment = VALIGN_4
322 * - align_j = 2 for any render target surface with Surface Vertical
323 * Alignment = VALIGN_2
324 * - align_j = 2 for all other render target surface
325 * - align_j = 2 for any sampling engine surface with Surface Vertical
326 * Alignment = VALIGN_2
327 * - align_j = 4 for any sampling engine surface with Surface Vertical
328 * Alignment = VALIGN_4"
329 *
330 * From the Sandy Bridge PRM, volume 4 part 1, page 86:
331 *
332 * "This field (Surface Vertical Alignment) must be set to VALIGN_2 if
333 * the Surface Format is 96 bits per element (BPE)."
334 *
335 * They can be rephrased as
336 *
337 * align_i align_j
338 * compressed formats block width block height
339 * PIPE_FORMAT_S8_UINT 4 2
340 * other depth/stencil formats 4 4
341 * 4x multisampled 4 4
342 * bpp 96 4 2
343 * others 4 2 or 4
344 */
345
346 /*
347 * From the Ivy Bridge PRM, volume 1 part 1, page 110:
348 *
349 * "surface defined by surface format align_i align_j
350 * 3DSTATE_DEPTH_BUFFER D16_UNORM 8 4
351 * not D16_UNORM 4 4
352 * 3DSTATE_STENCIL_BUFFER N/A 8 8
353 * SURFACE_STATE BC*, ETC*, EAC* 4 4
354 * FXT1 8 4
355 * all others (set by SURFACE_STATE)"
356 *
357 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
358 *
359 * "- This field (Surface Vertical Aligment) is intended to be set to
360 * VALIGN_4 if the surface was rendered as a depth buffer, for a
361 * multisampled (4x) render target, or for a multisampled (8x)
362 * render target, since these surfaces support only alignment of 4.
363 * - Use of VALIGN_4 for other surfaces is supported, but uses more
364 * memory.
365 * - This field must be set to VALIGN_4 for all tiled Y Render Target
366 * surfaces.
367 * - Value of 1 is not supported for format YCRCB_NORMAL (0x182),
368 * YCRCB_SWAPUVY (0x183), YCRCB_SWAPUV (0x18f), YCRCB_SWAPY (0x190)
369 * - If Number of Multisamples is not MULTISAMPLECOUNT_1, this field
370 * must be set to VALIGN_4."
371 * - VALIGN_4 is not supported for surface format R32G32B32_FLOAT."
372 *
373 * "- This field (Surface Horizontal Aligment) is intended to be set to
374 * HALIGN_8 only if the surface was rendered as a depth buffer with
375 * Z16 format or a stencil buffer, since these surfaces support only
376 * alignment of 8.
377 * - Use of HALIGN_8 for other surfaces is supported, but uses more
378 * memory.
379 * - This field must be set to HALIGN_4 if the Surface Format is BC*.
380 * - This field must be set to HALIGN_8 if the Surface Format is
381 * FXT1."
382 *
383 * They can be rephrased as
384 *
385 * align_i align_j
386 * compressed formats block width block height
387 * PIPE_FORMAT_Z16_UNORM 8 4
388 * PIPE_FORMAT_S8_UINT 8 8
Chia-I Wu457d0a62014-08-18 13:02:26 +0800389 * other depth/stencil formats 4 4
Chia-I Wu4bc47012014-08-14 13:03:25 +0800390 * 2x or 4x multisampled 4 or 8 4
391 * tiled Y 4 or 8 4 (if rt)
392 * PIPE_FORMAT_R32G32B32_FLOAT 4 or 8 2
393 * others 4 or 8 2 or 4
394 */
395
396 if (params->compressed) {
397 /* this happens to be the case */
398 layout->align_i = layout->block_width;
399 layout->align_j = layout->block_height;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800400 } else if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
401 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) {
402 switch (layout->format.channelFormat) {
403 case XGL_CH_FMT_R16:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800404 layout->align_i = 8;
405 layout->align_j = 4;
406 break;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800407 case XGL_CH_FMT_R8:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800408 layout->align_i = 8;
409 layout->align_j = 8;
410 break;
411 default:
412 layout->align_i = 4;
413 layout->align_j = 4;
414 break;
415 }
416 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800417 switch (layout->format.channelFormat) {
418 case XGL_CH_FMT_R8:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800419 layout->align_i = 4;
420 layout->align_j = 2;
421 break;
422 default:
423 layout->align_i = 4;
424 layout->align_j = 4;
425 break;
426 }
427 }
428 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800429 const bool valign_4 = (info->samples > 1) ||
430 (intel_gpu_gen(params->gpu) >= INTEL_GEN(7) &&
Chia-I Wu4bc47012014-08-14 13:03:25 +0800431 layout->tiling == INTEL_TILING_Y &&
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800432 (info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT));
Chia-I Wu4bc47012014-08-14 13:03:25 +0800433
434 if (valign_4)
435 assert(layout->block_size != 12);
436
437 layout->align_i = 4;
438 layout->align_j = (valign_4) ? 4 : 2;
439 }
440
441 /*
442 * the fact that align i and j are multiples of block width and height
443 * respectively is what makes the size of the bo a multiple of the block
444 * size, slices start at block boundaries, and many of the computations
445 * work.
446 */
447 assert(layout->align_i % layout->block_width == 0);
448 assert(layout->align_j % layout->block_height == 0);
449
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800450 /* make sure u_align() works */
451 assert(u_is_pow2(layout->align_i) &&
452 u_is_pow2(layout->align_j));
453 assert(u_is_pow2(layout->block_width) &&
454 u_is_pow2(layout->block_height));
Chia-I Wu4bc47012014-08-14 13:03:25 +0800455}
456
457static unsigned
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800458layout_get_valid_tilings(const struct intel_layout *layout,
459 const struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800460{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800461 const XGL_IMAGE_CREATE_INFO *info = params->info;
462 const XGL_FORMAT format = layout->format;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800463 unsigned valid_tilings = LAYOUT_TILING_ALL;
464
465 /*
Chia-I Wu4bc47012014-08-14 13:03:25 +0800466 * From the Sandy Bridge PRM, volume 2 part 1, page 318:
467 *
468 * "[DevSNB+]: This field (Tiled Surface) must be set to TRUE. Linear
469 * Depth Buffer is not supported."
470 *
471 * "The Depth Buffer, if tiled, must use Y-Major tiling."
472 *
473 * From the Sandy Bridge PRM, volume 1 part 2, page 22:
474 *
475 * "W-Major Tile Format is used for separate stencil."
476 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800477 if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
478 switch (format.channelFormat) {
479 case XGL_CH_FMT_R8:
Chia-I Wu4bc47012014-08-14 13:03:25 +0800480 valid_tilings &= LAYOUT_TILING_W;
481 break;
482 default:
483 valid_tilings &= LAYOUT_TILING_Y;
484 break;
485 }
486 }
487
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800488 if (info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800489 /*
490 * From the Sandy Bridge PRM, volume 1 part 2, page 32:
491 *
492 * "NOTE: 128BPE Format Color buffer ( render target ) MUST be
493 * either TileX or Linear."
494 */
495 if (layout->block_size == 16)
496 valid_tilings &= ~LAYOUT_TILING_Y;
497
498 /*
499 * From the Ivy Bridge PRM, volume 4 part 1, page 63:
500 *
501 * "This field (Surface Vertical Aligment) must be set to VALIGN_4
502 * for all tiled Y Render Target surfaces."
503 *
504 * "VALIGN_4 is not supported for surface format R32G32B32_FLOAT."
505 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800506 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7) && layout->block_size == 12)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800507 valid_tilings &= ~LAYOUT_TILING_Y;
508 }
509
510 /* no conflicting binding flags */
511 assert(valid_tilings);
512
513 return valid_tilings;
514}
515
516static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800517layout_init_tiling(struct intel_layout *layout,
518 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800519{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800520 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800521 unsigned valid_tilings = layout_get_valid_tilings(layout, params);
522
Chia-I Wu457d0a62014-08-18 13:02:26 +0800523 /* no hardware support for W-tile */
524 if (valid_tilings & LAYOUT_TILING_W)
525 valid_tilings = (valid_tilings & ~LAYOUT_TILING_W) | LAYOUT_TILING_NONE;
526
Chia-I Wu4bc47012014-08-14 13:03:25 +0800527 layout->valid_tilings = valid_tilings;
528
Chia-I Wu457d0a62014-08-18 13:02:26 +0800529 if (info->usage & (XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
530 XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800531 /*
532 * heuristically set a minimum width/height for enabling tiling
533 */
Chia-I Wu457d0a62014-08-18 13:02:26 +0800534 if (layout->width0 < 64 && (valid_tilings & ~LAYOUT_TILING_X))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800535 valid_tilings &= ~LAYOUT_TILING_X;
536
Chia-I Wu457d0a62014-08-18 13:02:26 +0800537 if ((layout->width0 < 32 || layout->height0 < 16) &&
538 (layout->width0 < 16 || layout->height0 < 32) &&
Chia-I Wu4bc47012014-08-14 13:03:25 +0800539 (valid_tilings & ~LAYOUT_TILING_Y))
540 valid_tilings &= ~LAYOUT_TILING_Y;
541 } else {
542 /* force linear if we are not sure where the texture is bound to */
543 if (valid_tilings & LAYOUT_TILING_NONE)
544 valid_tilings &= LAYOUT_TILING_NONE;
545 }
546
547 /* prefer tiled over linear */
548 if (valid_tilings & LAYOUT_TILING_Y)
549 layout->tiling = INTEL_TILING_Y;
550 else if (valid_tilings & LAYOUT_TILING_X)
551 layout->tiling = INTEL_TILING_X;
Chia-I Wu457d0a62014-08-18 13:02:26 +0800552 else
Chia-I Wu4bc47012014-08-14 13:03:25 +0800553 layout->tiling = INTEL_TILING_NONE;
554}
555
556static void
Chia-I Wu457d0a62014-08-18 13:02:26 +0800557layout_init_walk_gen7(struct intel_layout *layout,
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800558 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800559{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800560 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800561
562 /*
563 * It is not explicitly states, but render targets are expected to be
564 * UMS/CMS (samples non-interleaved) and depth/stencil buffers are expected
565 * to be IMS (samples interleaved).
566 *
567 * See "Multisampled Surface Storage Format" field of SURFACE_STATE.
568 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800569 if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800570 /*
571 * From the Ivy Bridge PRM, volume 1 part 1, page 111:
572 *
573 * "note that the depth buffer and stencil buffer have an implied
574 * value of ARYSPC_FULL"
575 */
Chia-I Wu457d0a62014-08-18 13:02:26 +0800576 layout->walk = (info->imageType == XGL_IMAGE_3D) ?
577 INTEL_LAYOUT_WALK_3D : INTEL_LAYOUT_WALK_LAYER;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800578
Chia-I Wu457d0a62014-08-18 13:02:26 +0800579 layout->interleaved_samples = true;
580 } else {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800581 /*
582 * From the Ivy Bridge PRM, volume 4 part 1, page 66:
583 *
584 * "If Multisampled Surface Storage Format is MSFMT_MSS and Number
585 * of Multisamples is not MULTISAMPLECOUNT_1, this field (Surface
586 * Array Spacing) must be set to ARYSPC_LOD0."
587 *
588 * As multisampled resources are not mipmapped, we never use
589 * ARYSPC_FULL for them.
590 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800591 if (info->samples > 1)
592 assert(info->mipLevels == 1);
Chia-I Wu457d0a62014-08-18 13:02:26 +0800593
594 layout->walk =
595 (info->imageType == XGL_IMAGE_3D) ? INTEL_LAYOUT_WALK_3D :
596 (info->mipLevels > 1) ? INTEL_LAYOUT_WALK_LAYER :
597 INTEL_LAYOUT_WALK_LOD;
598
599 layout->interleaved_samples = false;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800600 }
601}
602
603static void
Chia-I Wu457d0a62014-08-18 13:02:26 +0800604layout_init_walk_gen6(struct intel_layout *layout,
605 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800606{
Chia-I Wu4bc47012014-08-14 13:03:25 +0800607 /*
608 * From the Sandy Bridge PRM, volume 1 part 1, page 115:
609 *
610 * "The separate stencil buffer does not support mip mapping, thus the
611 * storage for LODs other than LOD 0 is not needed. The following
612 * QPitch equation applies only to the separate stencil buffer:
613 *
614 * QPitch = h_0"
615 *
616 * GEN6 does not support compact spacing otherwise.
617 */
Chia-I Wu457d0a62014-08-18 13:02:26 +0800618 layout->walk =
619 (params->info->imageType == XGL_IMAGE_3D) ? INTEL_LAYOUT_WALK_3D :
620 intel_format_is_stencil(params->gpu, layout->format) ? INTEL_LAYOUT_WALK_LOD :
621 INTEL_LAYOUT_WALK_LAYER;
622
623 /* GEN6 supports only interleaved samples */
624 layout->interleaved_samples = true;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800625}
626
627static void
Chia-I Wu457d0a62014-08-18 13:02:26 +0800628layout_init_walk(struct intel_layout *layout,
629 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800630{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800631 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
Chia-I Wu457d0a62014-08-18 13:02:26 +0800632 layout_init_walk_gen7(layout, params);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800633 else
Chia-I Wu457d0a62014-08-18 13:02:26 +0800634 layout_init_walk_gen6(layout, params);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800635}
636
637static void
Chia-I Wu457d0a62014-08-18 13:02:26 +0800638layout_init_size_and_format(struct intel_layout *layout,
639 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800640{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800641 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu457d0a62014-08-18 13:02:26 +0800642 XGL_FORMAT format = info->format;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800643 bool require_separate_stencil;
644
Chia-I Wu457d0a62014-08-18 13:02:26 +0800645 layout->width0 = info->extent.width;
646 layout->height0 = info->extent.height;
647
Chia-I Wu4bc47012014-08-14 13:03:25 +0800648 /*
649 * From the Sandy Bridge PRM, volume 2 part 1, page 317:
650 *
651 * "This field (Separate Stencil Buffer Enable) must be set to the same
652 * value (enabled or disabled) as Hierarchical Depth Buffer Enable."
653 *
654 * GEN7+ requires separate stencil buffers.
655 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800656 if (info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT) {
657 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800658 require_separate_stencil = true;
659 else
Chia-I Wu457d0a62014-08-18 13:02:26 +0800660 require_separate_stencil = (layout->aux == INTEL_LAYOUT_AUX_HIZ);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800661 }
662
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800663 if (format.numericFormat == XGL_NUM_FMT_DS) {
664 switch (format.channelFormat) {
665 case XGL_CH_FMT_R32G8:
666 if (require_separate_stencil) {
667 format.channelFormat = XGL_CH_FMT_R32;
668 layout->separate_stencil = true;
669 }
670 break;
671 default:
672 break;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800673 }
Chia-I Wu4bc47012014-08-14 13:03:25 +0800674 }
675
Chia-I Wu4bc47012014-08-14 13:03:25 +0800676 layout->format = format;
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800677 layout->block_width = icd_format_get_block_width(format);
678 layout->block_height = layout->block_width;
679 layout->block_size = icd_format_get_size(format);
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800680
Chia-I Wu1bf06df2014-08-16 12:33:13 +0800681 params->compressed = icd_format_is_compressed(format);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800682}
683
684static bool
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800685layout_want_mcs(struct intel_layout *layout,
686 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800687{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800688 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800689 bool want_mcs = false;
690
691 /* MCS is for RT on GEN7+ */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800692 if (intel_gpu_gen(params->gpu) < INTEL_GEN(7))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800693 return false;
694
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800695 if (info->imageType != XGL_IMAGE_2D ||
696 !(info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800697 return false;
698
699 /*
700 * From the Ivy Bridge PRM, volume 4 part 1, page 77:
701 *
702 * "For Render Target and Sampling Engine Surfaces:If the surface is
703 * multisampled (Number of Multisamples any value other than
704 * MULTISAMPLECOUNT_1), this field (MCS Enable) must be enabled."
705 *
706 * "This field must be set to 0 for all SINT MSRTs when all RT channels
707 * are not written"
708 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800709 if (info->samples > 1 && !layout->interleaved_samples &&
Chia-I Wu457d0a62014-08-18 13:02:26 +0800710 !icd_format_is_int(info->format)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800711 want_mcs = true;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800712 } else if (info->samples <= 1) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800713 /*
714 * From the Ivy Bridge PRM, volume 2 part 1, page 326:
715 *
716 * "When MCS is buffer is used for color clear of non-multisampler
717 * render target, the following restrictions apply.
718 * - Support is limited to tiled render targets.
719 * - Support is for non-mip-mapped and non-array surface types
720 * only.
721 * - Clear is supported only on the full RT; i.e., no partial clear
722 * or overlapping clears.
723 * - MCS buffer for non-MSRT is supported only for RT formats
724 * 32bpp, 64bpp and 128bpp.
725 * ..."
726 */
727 if (layout->tiling != INTEL_TILING_NONE &&
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800728 info->mipLevels == 1 && info->arraySize == 1) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800729 switch (layout->block_size) {
730 case 4:
731 case 8:
732 case 16:
733 want_mcs = true;
734 break;
735 default:
736 break;
737 }
738 }
739 }
740
741 return want_mcs;
742}
743
744static bool
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800745layout_want_hiz(const struct intel_layout *layout,
746 const struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800747{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800748 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800749
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800750 if (!(info->usage & XGL_IMAGE_USAGE_DEPTH_STENCIL_BIT))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800751 return false;
752
Chia-I Wufb240262014-08-16 13:26:06 +0800753 if (!intel_format_is_depth(params->gpu, info->format))
Chia-I Wu4bc47012014-08-14 13:03:25 +0800754 return false;
755
Chia-I Wu457d0a62014-08-18 13:02:26 +0800756 /*
757 * As can be seen in layout_calculate_hiz_size(), HiZ may not be enabled
758 * for every level. This is generally fine except on GEN6, where HiZ and
759 * separate stencil are enabled and disabled at the same time. When the
760 * format is PIPE_FORMAT_Z32_FLOAT_S8X24_UINT, enabling and disabling HiZ
761 * can result in incompatible formats.
762 */
763 if (intel_gpu_gen(params->gpu) == INTEL_GEN(6) &&
764 info->format.channelFormat == XGL_CH_FMT_R32G8 &&
765 info->mipLevels > 1)
766 return false;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800767
Chia-I Wu457d0a62014-08-18 13:02:26 +0800768 return true;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800769}
770
771static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800772layout_init_aux(struct intel_layout *layout,
773 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800774{
775 if (layout_want_hiz(layout, params))
Chia-I Wu457d0a62014-08-18 13:02:26 +0800776 layout->aux = INTEL_LAYOUT_AUX_HIZ;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800777 else if (layout_want_mcs(layout, params))
Chia-I Wu457d0a62014-08-18 13:02:26 +0800778 layout->aux = INTEL_LAYOUT_AUX_MCS;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800779}
780
781static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800782layout_align(struct intel_layout *layout, struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800783{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800784 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800785 int align_w = 1, align_h = 1, pad_h = 0;
786
787 /*
788 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
789 *
790 * "To determine the necessary padding on the bottom and right side of
791 * the surface, refer to the table in Section 7.18.3.4 for the i and j
792 * parameters for the surface format in use. The surface must then be
793 * extended to the next multiple of the alignment unit size in each
794 * dimension, and all texels contained in this extended surface must
795 * have valid GTT entries."
796 *
797 * "For cube surfaces, an additional two rows of padding are required
798 * at the bottom of the surface. This must be ensured regardless of
799 * whether the surface is stored tiled or linear. This is due to the
800 * potential rotation of cache line orientation from memory to cache."
801 *
802 * "For compressed textures (BC* and FXT1 surface formats), padding at
803 * the bottom of the surface is to an even compressed row, which is
804 * equal to a multiple of 8 uncompressed texel rows. Thus, for padding
805 * purposes, these surfaces behave as if j = 8 only for surface
806 * padding purposes. The value of 4 for j still applies for mip level
807 * alignment and QPitch calculation."
808 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800809 if (info->usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) {
810 if (align_w < layout->align_i)
811 align_w = layout->align_i;
812 if (align_h < layout->align_j)
813 align_h = layout->align_j;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800814
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800815 /* in case it is used as a cube */
816 if (info->imageType == XGL_IMAGE_2D)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800817 pad_h += 2;
818
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800819 if (params->compressed && align_h < layout->align_j * 2)
820 align_h = layout->align_j * 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800821 }
822
823 /*
824 * From the Sandy Bridge PRM, volume 1 part 1, page 118:
825 *
826 * "If the surface contains an odd number of rows of data, a final row
827 * below the surface must be allocated."
828 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800829 if ((info->usage & XGL_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) && align_h < 2)
830 align_h = 2;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800831
832 /*
833 * Depth Buffer Clear/Resolve works in 8x4 sample blocks. In
Chia-I Wu457d0a62014-08-18 13:02:26 +0800834 * intel_texture_can_enable_hiz(), we always return true for the first slice.
Chia-I Wu4bc47012014-08-14 13:03:25 +0800835 * To avoid out-of-bound access, we have to pad.
836 */
Chia-I Wu457d0a62014-08-18 13:02:26 +0800837 if (layout->aux == INTEL_LAYOUT_AUX_HIZ &&
838 info->mipLevels == 1 &&
839 info->arraySize == 1 &&
840 info->extent.depth == 1) {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800841 if (align_w < 8)
842 align_w = 8;
843 if (align_h < 4)
844 align_h = 4;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800845 }
846
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800847 params->max_x = u_align(params->max_x, align_w);
848 params->max_y = u_align(params->max_y + pad_h, align_h);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800849}
850
851/* note that this may force the texture to be linear */
852static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800853layout_calculate_bo_size(struct intel_layout *layout,
854 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800855{
856 assert(params->max_x % layout->block_width == 0);
857 assert(params->max_y % layout->block_height == 0);
858 assert(layout->layer_height % layout->block_height == 0);
859
860 layout->bo_stride =
861 (params->max_x / layout->block_width) * layout->block_size;
862 layout->bo_height = params->max_y / layout->block_height;
863
864 while (true) {
865 unsigned w = layout->bo_stride, h = layout->bo_height;
866 unsigned align_w, align_h;
867
868 /*
869 * From the Haswell PRM, volume 5, page 163:
870 *
871 * "For linear surfaces, additional padding of 64 bytes is required
872 * at the bottom of the surface. This is in addition to the padding
873 * required above."
874 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800875 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7.5) &&
876 (params->info->usage & XGL_IMAGE_USAGE_SHADER_ACCESS_READ_BIT) &&
Chia-I Wu4bc47012014-08-14 13:03:25 +0800877 layout->tiling == INTEL_TILING_NONE) {
878 layout->bo_height +=
879 (64 + layout->bo_stride - 1) / layout->bo_stride;
880 }
881
882 /*
883 * From the Sandy Bridge PRM, volume 4 part 1, page 81:
884 *
885 * "- For linear render target surfaces, the pitch must be a
886 * multiple of the element size for non-YUV surface formats.
887 * Pitch must be a multiple of 2 * element size for YUV surface
888 * formats.
889 * - For other linear surfaces, the pitch can be any multiple of
890 * bytes.
891 * - For tiled surfaces, the pitch must be a multiple of the tile
892 * width."
893 *
894 * Different requirements may exist when the bo is used in different
895 * places, but our alignments here should be good enough that we do not
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800896 * need to check layout->info->usage.
Chia-I Wu4bc47012014-08-14 13:03:25 +0800897 */
898 switch (layout->tiling) {
899 case INTEL_TILING_X:
900 align_w = 512;
901 align_h = 8;
902 break;
903 case INTEL_TILING_Y:
904 align_w = 128;
905 align_h = 32;
906 break;
907 default:
Chia-I Wufb240262014-08-16 13:26:06 +0800908 if (intel_format_is_stencil(params->gpu, layout->format)) {
Chia-I Wu4bc47012014-08-14 13:03:25 +0800909 /*
910 * From the Sandy Bridge PRM, volume 1 part 2, page 22:
911 *
912 * "A 4KB tile is subdivided into 8-high by 8-wide array of
913 * Blocks for W-Major Tiles (W Tiles). Each Block is 8 rows by 8
914 * bytes."
915 *
916 * Since we asked for INTEL_TILING_NONE instead of the non-existent
917 * INTEL_TILING_W, we want to align to W tiles here.
918 */
919 align_w = 64;
920 align_h = 64;
921 } else {
922 /* some good enough values */
923 align_w = 64;
924 align_h = 2;
925 }
926 break;
927 }
928
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800929 w = u_align(w, align_w);
930 h = u_align(h, align_h);
Chia-I Wu4bc47012014-08-14 13:03:25 +0800931
932 /* make sure the bo is mappable */
933 if (layout->tiling != INTEL_TILING_NONE) {
934 /*
935 * Usually only the first 256MB of the GTT is mappable.
936 *
937 * See also how intel_context::max_gtt_map_object_size is calculated.
938 */
939 const size_t mappable_gtt_size = 256 * 1024 * 1024;
940
941 /*
942 * Be conservative. We may be able to switch from VALIGN_4 to
943 * VALIGN_2 if the layout was Y-tiled, but let's keep it simple.
944 */
945 if (mappable_gtt_size / w / 4 < h) {
946 if (layout->valid_tilings & LAYOUT_TILING_NONE) {
947 layout->tiling = INTEL_TILING_NONE;
948 /* MCS support for non-MSRTs is limited to tiled RTs */
Chia-I Wu457d0a62014-08-18 13:02:26 +0800949 if (layout->aux == INTEL_LAYOUT_AUX_MCS &&
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800950 params->info->samples <= 1)
Chia-I Wu457d0a62014-08-18 13:02:26 +0800951 layout->aux = INTEL_LAYOUT_AUX_NONE;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800952
953 continue;
954 } else {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800955 /* mapping will fail */
Chia-I Wu4bc47012014-08-14 13:03:25 +0800956 }
957 }
958 }
959
960 layout->bo_stride = w;
961 layout->bo_height = h;
962 break;
963 }
964}
965
966static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800967layout_calculate_hiz_size(struct intel_layout *layout,
968 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +0800969{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +0800970 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu457d0a62014-08-18 13:02:26 +0800971 const unsigned hz_align_j = 8;
972 enum intel_layout_walk_type hz_walk;
973 unsigned hz_width, hz_height, lv;
974 unsigned hz_clear_w, hz_clear_h;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800975
Chia-I Wu457d0a62014-08-18 13:02:26 +0800976 assert(layout->aux == INTEL_LAYOUT_AUX_HIZ);
977
978 assert(layout->walk == INTEL_LAYOUT_WALK_LAYER ||
979 layout->walk == INTEL_LAYOUT_WALK_3D);
980
981 /*
982 * From the Sandy Bridge PRM, volume 2 part 1, page 312:
983 *
984 * "The hierarchical depth buffer does not support the LOD field, it is
985 * assumed by hardware to be zero. A separate hierarachical depth
986 * buffer is required for each LOD used, and the corresponding
987 * buffer's state delivered to hardware each time a new depth buffer
988 * state with modified LOD is delivered."
989 *
990 * We will put all LODs in a single bo with INTEL_LAYOUT_WALK_LOD.
991 */
992 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
993 hz_walk = layout->walk;
994 else
995 hz_walk = INTEL_LAYOUT_WALK_LOD;
Chia-I Wu4bc47012014-08-14 13:03:25 +0800996
997 /*
998 * See the Sandy Bridge PRM, volume 2 part 1, page 312, and the Ivy Bridge
999 * PRM, volume 2 part 1, page 312-313.
1000 *
1001 * It seems HiZ buffer is aligned to 8x8, with every two rows packed into a
1002 * memory row.
1003 */
Chia-I Wu457d0a62014-08-18 13:02:26 +08001004 switch (hz_walk) {
1005 case INTEL_LAYOUT_WALK_LOD:
1006 {
1007 unsigned lod_tx[INTEL_LAYOUT_MAX_LEVELS];
1008 unsigned lod_ty[INTEL_LAYOUT_MAX_LEVELS];
1009 unsigned cur_tx, cur_ty;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001010
Chia-I Wu457d0a62014-08-18 13:02:26 +08001011 /* figure out the tile offsets of LODs */
1012 hz_width = 0;
1013 hz_height = 0;
1014 cur_tx = 0;
1015 cur_ty = 0;
1016 for (lv = 0; lv < info->mipLevels; lv++) {
1017 unsigned tw, th;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001018
Chia-I Wu457d0a62014-08-18 13:02:26 +08001019 lod_tx[lv] = cur_tx;
1020 lod_ty[lv] = cur_ty;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001021
Chia-I Wu457d0a62014-08-18 13:02:26 +08001022 tw = u_align(layout->lods[lv].slice_width, 16);
1023 th = u_align(layout->lods[lv].slice_height, hz_align_j) *
1024 info->arraySize / 2;
1025 /* convert to Y-tiles */
1026 tw = u_align(tw, 128) / 128;
1027 th = u_align(th, 32) / 32;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001028
Chia-I Wu457d0a62014-08-18 13:02:26 +08001029 if (hz_width < cur_tx + tw)
1030 hz_width = cur_tx + tw;
1031 if (hz_height < cur_ty + th)
1032 hz_height = cur_ty + th;
1033
1034 if (lv == 1)
1035 cur_tx += tw;
1036 else
1037 cur_ty += th;
1038 }
1039
1040 /* convert tile offsets to memory offsets */
1041 for (lv = 0; lv < info->mipLevels; lv++) {
1042 layout->aux_offsets[lv] =
1043 (lod_ty[lv] * hz_width + lod_tx[lv]) * 4096;
1044 }
1045 hz_width *= 128;
1046 hz_height *= 32;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001047 }
Chia-I Wu457d0a62014-08-18 13:02:26 +08001048 break;
1049 case INTEL_LAYOUT_WALK_LAYER:
1050 {
1051 const unsigned h0 = u_align(params->h0, hz_align_j);
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001052 const unsigned h1 = u_align(params->h1, hz_align_j);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001053 const unsigned htail =
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001054 ((intel_gpu_gen(params->gpu) >= INTEL_GEN(7)) ? 12 : 11) * hz_align_j;
Chia-I Wu457d0a62014-08-18 13:02:26 +08001055 const unsigned hz_qpitch = h0 + h1 + htail;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001056
Chia-I Wu457d0a62014-08-18 13:02:26 +08001057 hz_width = u_align(layout->lods[0].slice_width, 16);
1058
1059 hz_height = hz_qpitch * info->arraySize / 2;
1060 if (intel_gpu_gen(params->gpu) >= INTEL_GEN(7))
1061 hz_height = u_align(hz_height, 8);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001062 }
Chia-I Wu457d0a62014-08-18 13:02:26 +08001063 break;
1064 case INTEL_LAYOUT_WALK_3D:
1065 hz_width = u_align(layout->lods[0].slice_width, 16);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001066
Chia-I Wu457d0a62014-08-18 13:02:26 +08001067 hz_height = 0;
1068 for (lv = 0; lv < info->mipLevels; lv++) {
1069 const unsigned h = u_align(layout->lods[lv].slice_height, hz_align_j);
1070 /* according to the formula, slices are packed together vertically */
1071 hz_height += h * u_minify(info->extent.depth, lv);
1072 }
1073 hz_height /= 2;
1074 break;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001075 }
1076
Chia-I Wu457d0a62014-08-18 13:02:26 +08001077 /*
1078 * In hiz_align_fb(), we will align the LODs to 8x4 sample blocks.
1079 * Experiments on Haswell show that aligning the RECTLIST primitive and
1080 * 3DSTATE_DRAWING_RECTANGLE alone are not enough. The LOD sizes must be
1081 * aligned.
1082 */
1083 hz_clear_w = 8;
1084 hz_clear_h = 4;
1085 switch (info->samples) {
1086 case 0:
1087 case 1:
1088 default:
1089 break;
1090 case 2:
1091 hz_clear_w /= 2;
1092 break;
1093 case 4:
1094 hz_clear_w /= 2;
1095 hz_clear_h /= 2;
1096 break;
1097 case 8:
1098 hz_clear_w /= 4;
1099 hz_clear_h /= 2;
1100 break;
1101 case 16:
1102 hz_clear_w /= 4;
1103 hz_clear_h /= 4;
1104 break;
1105 }
1106
1107 for (lv = 0; lv < info->mipLevels; lv++) {
1108 if (u_minify(layout->width0, lv) % hz_clear_w ||
1109 u_minify(layout->height0, lv) % hz_clear_h)
1110 break;
1111 layout->aux_enables |= 1 << lv;
1112 }
1113
1114 /* we padded to allow this in layout_align() */
1115 if (info->mipLevels == 1 && info->arraySize == 1 && info->extent.depth == 1)
1116 layout->aux_enables |= 0x1;
1117
Chia-I Wu4bc47012014-08-14 13:03:25 +08001118 /* align to Y-tile */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001119 layout->aux_stride = u_align(hz_width, 128);
1120 layout->aux_height = u_align(hz_height, 32);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001121}
1122
1123static void
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001124layout_calculate_mcs_size(struct intel_layout *layout,
1125 struct intel_layout_params *params)
Chia-I Wu4bc47012014-08-14 13:03:25 +08001126{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001127 const XGL_IMAGE_CREATE_INFO *info = params->info;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001128 int mcs_width, mcs_height, mcs_cpp;
1129 int downscale_x, downscale_y;
1130
Chia-I Wu457d0a62014-08-18 13:02:26 +08001131 assert(layout->aux == INTEL_LAYOUT_AUX_MCS);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001132
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001133 if (info->samples > 1) {
Chia-I Wu4bc47012014-08-14 13:03:25 +08001134 /*
1135 * From the Ivy Bridge PRM, volume 2 part 1, page 326, the clear
1136 * rectangle is scaled down by 8x2 for 4X MSAA and 2x2 for 8X MSAA. The
1137 * need of scale down could be that the clear rectangle is used to clear
1138 * the MCS instead of the RT.
1139 *
1140 * For 8X MSAA, we need 32 bits in MCS for every pixel in the RT. The
1141 * 2x2 factor could come from that the hardware writes 128 bits (an
1142 * OWord) at a time, and the OWord in MCS maps to a 2x2 pixel block in
1143 * the RT. For 4X MSAA, we need 8 bits in MCS for every pixel in the
1144 * RT. Similarly, we could reason that an OWord in 4X MCS maps to a 8x2
1145 * pixel block in the RT.
1146 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001147 switch (info->samples) {
Chia-I Wu4bc47012014-08-14 13:03:25 +08001148 case 2:
1149 case 4:
1150 downscale_x = 8;
1151 downscale_y = 2;
1152 mcs_cpp = 1;
1153 break;
1154 case 8:
1155 downscale_x = 2;
1156 downscale_y = 2;
1157 mcs_cpp = 4;
1158 break;
1159 case 16:
1160 downscale_x = 2;
1161 downscale_y = 1;
1162 mcs_cpp = 8;
1163 break;
1164 default:
1165 assert(!"unsupported sample count");
1166 return;
1167 break;
1168 }
1169
1170 /*
1171 * It also appears that the 2x2 subspans generated by the scaled-down
1172 * clear rectangle cannot be masked. The scale-down clear rectangle
1173 * thus must be aligned to 2x2, and we need to pad.
1174 */
Chia-I Wu457d0a62014-08-18 13:02:26 +08001175 mcs_width = u_align(layout->width0, downscale_x * 2);
1176 mcs_height = u_align(layout->height0, downscale_y * 2);
1177 } else {
Chia-I Wu4bc47012014-08-14 13:03:25 +08001178 /*
1179 * From the Ivy Bridge PRM, volume 2 part 1, page 327:
1180 *
1181 * " Pixels Lines
1182 * TiledY RT CL
1183 * bpp
1184 * 32 8 4
1185 * 64 4 4
1186 * 128 2 4
1187 *
1188 * TiledX RT CL
1189 * bpp
1190 * 32 16 2
1191 * 64 8 2
1192 * 128 4 2"
1193 *
1194 * This table and the two following tables define the RT alignments, the
1195 * clear rectangle alignments, and the clear rectangle scale factors.
1196 * Viewing the RT alignments as the sizes of 128-byte blocks, we can see
1197 * that the clear rectangle alignments are 16x32 blocks, and the clear
1198 * rectangle scale factors are 8x16 blocks.
1199 *
1200 * For non-MSAA RT, we need 1 bit in MCS for every 128-byte block in the
1201 * RT. Similar to the MSAA cases, we can argue that an OWord maps to
1202 * 8x16 blocks.
1203 *
1204 * One problem with this reasoning is that a Y-tile in MCS has 8x32
1205 * OWords and maps to 64x512 128-byte blocks. This differs from i965,
1206 * which says that a Y-tile maps to 128x256 blocks (\see
1207 * intel_get_non_msrt_mcs_alignment). It does not really change
1208 * anything except for the size of the allocated MCS. Let's see if we
1209 * hit out-of-bound access.
1210 */
1211 switch (layout->tiling) {
1212 case INTEL_TILING_X:
1213 downscale_x = 64 / layout->block_size;
1214 downscale_y = 2;
1215 break;
1216 case INTEL_TILING_Y:
1217 downscale_x = 32 / layout->block_size;
1218 downscale_y = 4;
1219 break;
1220 default:
1221 assert(!"unsupported tiling mode");
1222 return;
1223 break;
1224 }
1225
1226 downscale_x *= 8;
1227 downscale_y *= 16;
1228
1229 /*
1230 * From the Haswell PRM, volume 7, page 652:
1231 *
1232 * "Clear rectangle must be aligned to two times the number of
1233 * pixels in the table shown below due to 16X16 hashing across the
1234 * slice."
1235 *
1236 * The scaled-down clear rectangle must be aligned to 4x4 instead of
1237 * 2x2, and we need to pad.
1238 */
Chia-I Wu457d0a62014-08-18 13:02:26 +08001239 mcs_width = u_align(layout->width0, downscale_x * 4) / downscale_x;
1240 mcs_height = u_align(layout->height0, downscale_y * 4) / downscale_y;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001241 mcs_cpp = 16; /* an OWord */
1242 }
1243
Chia-I Wu457d0a62014-08-18 13:02:26 +08001244 layout->aux_enables = (1 << info->mipLevels) - 1;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001245 /* align to Y-tile */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001246 layout->aux_stride = u_align(mcs_width * mcs_cpp, 128);
1247 layout->aux_height = u_align(mcs_height, 32);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001248}
1249
1250/**
1251 * Initialize the layout. Callers should zero-initialize \p layout first.
1252 */
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001253void intel_layout_init(struct intel_layout *layout,
1254 const struct intel_dev *dev,
1255 const XGL_IMAGE_CREATE_INFO *info)
Chia-I Wu4bc47012014-08-14 13:03:25 +08001256{
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001257 struct intel_layout_params params;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001258
1259 memset(&params, 0, sizeof(params));
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001260 params.gpu = dev->gpu;
1261 params.info = info;
Chia-I Wu4bc47012014-08-14 13:03:25 +08001262
1263 /* note that there are dependencies between these functions */
1264 layout_init_aux(layout, &params);
Chia-I Wu457d0a62014-08-18 13:02:26 +08001265 layout_init_size_and_format(layout, &params);
1266 layout_init_walk(layout, &params);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001267 layout_init_tiling(layout, &params);
1268 layout_init_alignments(layout, &params);
Chia-I Wu457d0a62014-08-18 13:02:26 +08001269 layout_init_lods(layout, &params);
Chia-I Wu4bc47012014-08-14 13:03:25 +08001270 layout_init_layer_height(layout, &params);
1271
1272 layout_align(layout, &params);
1273 layout_calculate_bo_size(layout, &params);
1274
Chia-I Wu457d0a62014-08-18 13:02:26 +08001275 switch (layout->aux) {
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001276 case INTEL_LAYOUT_AUX_HIZ:
Chia-I Wu4bc47012014-08-14 13:03:25 +08001277 layout_calculate_hiz_size(layout, &params);
1278 break;
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001279 case INTEL_LAYOUT_AUX_MCS:
Chia-I Wu4bc47012014-08-14 13:03:25 +08001280 layout_calculate_mcs_size(layout, &params);
1281 break;
1282 default:
1283 break;
1284 }
1285}
1286
1287/**
1288 * Update the tiling mode and bo stride (for imported resources).
1289 */
1290bool
Chia-I Wu8a8d8b62014-08-14 13:26:26 +08001291intel_layout_update_for_imported_bo(struct intel_layout *layout,
Chia-I Wu457d0a62014-08-18 13:02:26 +08001292 enum intel_tiling_mode tiling,
1293 unsigned bo_stride)
Chia-I Wu4bc47012014-08-14 13:03:25 +08001294{
1295 if (!(layout->valid_tilings & (1 << tiling)))
1296 return false;
1297
1298 if ((tiling == INTEL_TILING_X && bo_stride % 512) ||
1299 (tiling == INTEL_TILING_Y && bo_stride % 128))
1300 return false;
1301
1302 layout->tiling = tiling;
1303 layout->bo_stride = bo_stride;
1304
1305 return true;
1306}